[Tutor] Fwd: make a sqlite3 database from an ordinary text file

avi.e.gross at gmail.com avi.e.gross at gmail.com
Sun Jun 19 19:43:14 EDT 2022


I may not be looking at this right since much happens in functions not seen
but does cur.execute(many) expect two items of text or two items of type
integer or will it handle a list or tuple with arbitrary numbers of contents
and so on, is what makes the design decision.

Deeper within that function, it presumable maps your passed function to code
that may do,

First, Second = f()

And so on. Or it may just take a single result or other choices. It is a bit
confusing to pass a filename as someone pointed out. 

In what follows, either I am confused or the one asking the question is.

The goal of the iterator seems to be to avoid reading all the data in at
once, right. Otherwise, why bother when you can do something like reading in
the entire file and splitting and putting the results in something like a
list structure, or perhaps in something from numpy.

So why does the code use readline() (no 's') to read in a single line and
only once? 

As I read the somewhat confusing code, the goal is to open the file the
first time and read a line and throw the results away as it is not clear
where it is saved. Then the goal is to loop on nothing (or maybe one line)
and yield the results of splitting it and pause till called again. But since
only no or maybe one line have been received, the call to iterate should
just exit as the loop is done.

So I think reading a single line should be moved down within the loop!

And you need some kind of test to know when there are no remaining lines in
the file or the current line is empty and instead of yielding a non-existent
result, you should return what the iteration protocol needs and close the
file, albeit the with does that for you silently as part of that protocol.

But as others have noted, it is not clear other than as an exercise, if this
iterator is needed. Your other modules may supply something for you.

Where a generator like this is even more useful is when at some point you
want to stop and perhaps resume a bit later elsewhere. For example, if your
code wanted random numbers that had been precalculated and use it for
various purposes until done, the iterator can in principle be called from
multiple places and keep marching along and eventually stop without reading
the entire file. Another example would be a generator that yields all primes
needed in order, and generally that never goes long enough to run out of
primes, LOL!




-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Sunday, June 19, 2022 2:09 PM
To: tutor at python.org
Subject: Re: [Tutor] Fwd: make a sqlite3 database from an ordinary text file

On 19/06/2022 18:52, Manprit Singh wrote:

> One more thing :
> def data_generator(workfile):
>     with open(workfile) as obj:
>         obj.readline()
>         for line in obj:
>             yield tuple(line.split())
> 
> Instead of yield tuple(line.split())    i can write yield line.split()
> too.
> 
> Am i right ?

It depends what you want. The first one gives a tuple back, the second gives
a list.

Often it won't make any difference but if you were using the value as a
dictionary key, say, then only the first would work.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list