Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
For completeness I was close this is the working code. def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set def data_att

Re: Screwing Up looping in Generator

2017-01-06 Thread Steve D'Aprano
On Wed, 4 Jan 2017 01:09 pm, Sayth Renshaw wrote: > Untested as i wrote this in notepad at work but, if i first use the > generator to create a set of filenames and then iterate it then call the > generator anew to process file may work? It "may" work. Or it "may not" work. It is hard to tell bec

Re: Screwing Up looping in Generator

2017-01-06 Thread Rhodri James
On 04/01/17 02:10, Deborah Swanson wrote: > Sayth Renshaw wrote, on January 03, 2017 5:36 PM >> >> So can I call the generator twice and receive the same file >> twice in 2 for loops? >> >> Once to get the files name and the second to process? >> >> for file in rootobs: >> base = os.path.b

RE: Screwing Up looping in Generator

2017-01-06 Thread Deborah Swanson
Erik wrote, on January 03, 2017 5:26 PM > Hi, > > On 04/01/17 01:12, Deborah Swanson wrote: > > The main reason you might want to catch the StopIteration > exception is > > to do something else before your code simply stops running. If all > > you're doing is run a generator til it's out of gas, an

RE: Screwing Up looping in Generator

2017-01-06 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 5:36 PM > > So can I call the generator twice and receive the same file > twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join

RE: Screwing Up looping in Generator

2017-01-06 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 5:55 PM > > On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > > So can I call the generator twice and receive the same file > twice in 2 > > for loops? > > > > Once to get the files name and the second to process? > > > > for file in roo

Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
Untested as i wrote this in notepad at work but, if i first use the generator to create a set of filenames and then iterate it then call the generator anew to process file may work? Good idea or better available? def get_list_of_names(generator_arg): name_set = set() for name in generat

RE: Screwing Up looping in Generator

2017-01-06 Thread Deborah Swanson
Chris Angelico wrote, on January 03, 2017 3:35 PM > > On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson > wrote: > > Ok, I learned how to use generators in Python 2.7.8, which may be > > different from Python 3 for generators. But I learned from MIT's > > online introduction to python course, and t

Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
So can I call the generator twice and receive the same file twice in 2 for loops? Once to get the files name and the second to process? for file in rootobs: base = os.path.basename(file.name) write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") with open

RE: Screwing Up looping in Generator

2017-01-06 Thread Deborah Swanson
Erik wrote, on January 03, 2017 3:53 PM > > On 03/01/17 23:05, Deborah Swanson wrote: > > And yes, we usually used for loops for generators, unless you don't > > know when the generator will be exhausted. As in this case, > where the > > number of files the generator can provide is unknown. Then >

Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > So can I call the generator twice and receive the same file twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to

Re: Screwing Up looping in Generator

2017-01-06 Thread Erik
Hi, On 04/01/17 01:12, Deborah Swanson wrote: > The main reason you might want to catch the StopIteration exception is > to do something else before your code simply stops running. If all > you're doing is run a generator til it's out of gas, and that's all you > want it to do, then there's no nee

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
Erik wrote, on January 03, 2017 3:45 PM > > Hi, > > On 03/01/17 22:14, Deborah Swanson wrote: > > ...you have to create the generator object first and use it to call > > the next function. And I really don't think you can use a > generator as > > your range in a for loop. So I'd use a 'while True',

Re: Screwing Up looping in Generator

2017-01-05 Thread Erik
On 03/01/17 23:05, Deborah Swanson wrote: > And yes, we usually used for loops for generators, unless you don't know > when the generator will be exhausted. As in this case, where the number > of files the generator can provide is unknown. Then we used the while > True, break on StopIteration metho

Re: Screwing Up looping in Generator

2017-01-05 Thread Erik
Hi, On 03/01/17 22:14, Deborah Swanson wrote: > ...you have to create the generator object first and use it to call the > next function. And I really don't think you can use a generator as your > range in a for loop. So I'd use a 'while True', and break out of the > loop when you hit the StopItera

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
-Original Message- From: Matt Wheeler [mailto:m...@funkyhat.org] Sent: Tuesday, January 03, 2017 1:47 PM To: pyt...@deborahswanson.net; Sayth Renshaw; python-list@python.org Subject: Re: Screwing Up looping in Generator On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > What&#x

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
Chris Angelico wrote, on January 03, 2017 2:31 PM > > On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson > wrote: > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > Small side point: Try to avoid calling a generator object's > .next() method directly.

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
Terry Reedy > > On 1/3/2017 3:53 PM, Deborah Swanson wrote: > > >> I think you're expecting > >> > >>for file in rootobs > >> > >> to get the next yield for you from rootobs, but unless someone > >> corrects me, I don't think you can expect a 'for' statement to do > >> that. You need to have a

Re: Screwing Up looping in Generator

2017-01-05 Thread Chris Angelico
On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson wrote: > Ok, I learned how to use generators in Python 2.7.8, which may be > different from Python 3 for generators. But I learned from MIT's online > introduction to python course, and they certainly seem to know python > well. So what is the corre

Re: Screwing Up looping in Generator

2017-01-05 Thread Chris Angelico
On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson wrote: > while True: > try: > file = files.next() > except StopIteration: > break Small side point: Try to avoid calling a generator object's .next() method directly. Normally, when you _do_ want to do this, you should be calling next(

Re: Screwing Up looping in Generator

2017-01-05 Thread Terry Reedy
On 1/3/2017 3:53 PM, Deborah Swanson wrote: >> I think you're expecting >> >> for file in rootobs >> >> to get the next yield for you from rootobs, but unless >> someone corrects me, I don't think you can expect a 'for' >> statement to do that. You need to have a 'next' statement >> inside yo

Re: Screwing Up looping in Generator

2017-01-05 Thread Matt Wheeler
On Tue, 3 Jan 2017 at 21:46 Matt Wheeler wrote: > range() is not part of the for syntax at all, it's completely separate, it > simply returns an iterator which the for loop can use, like any other. > *iterable -- -- Matt Wheeler http://funkyh.at -- https://mail.python.org/mailman/listinfo/pyt

Re: Screwing Up looping in Generator

2017-01-05 Thread Matt Wheeler
On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless someone corrects > me, I don't think you ca

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
> > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > > > Hi > > > > > > > > This is simple, but its getting me confused. > > > > > > > > I have a csv writer that opens a file and loops each line of the > > > > file for each file and then closes, writing one file. > > > > > > > > I wa

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
> > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > Hi > > > > > > This is simple, but its getting me confused. > > > > > > I have a csv writer that opens a file and loops each line of the > > > file for each file and then closes, writing one file. > > > > > > I want to alter the behav

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > Hi > > This is simple, but its getting me confused. > > I have a csv writer that opens a file and loops each line of > the file for each file and then closes, writing one file. > > I want to alter the behaviour to be a written file for each > inp

RE: Screwing Up looping in Generator

2017-01-05 Thread Deborah Swanson
> Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > Hi > > > > This is simple, but its getting me confused. > > > > I have a csv writer that opens a file and loops each line of > > the file for each file and then closes, writing one file. > > > > I want to alter the behaviour to be a written

Re: Screwing Up looping in Generator

2017-01-04 Thread Sayth Renshaw
For completeness I was close this is the working code. def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set def data_att

Re: Screwing Up looping in Generator

2017-01-04 Thread Rhodri James
On 04/01/17 02:10, Deborah Swanson wrote: Sayth Renshaw wrote, on January 03, 2017 5:36 PM So can I call the generator twice and receive the same file twice in 2 for loops? Once to get the files name and the second to process? for file in rootobs: base = os.path.basename(file.name)

Re: Screwing Up looping in Generator

2017-01-04 Thread Steve D'Aprano
On Wed, 4 Jan 2017 01:09 pm, Sayth Renshaw wrote: > Untested as i wrote this in notepad at work but, if i first use the > generator to create a set of filenames and then iterate it then call the > generator anew to process file may work? It "may" work. Or it "may not" work. It is hard to tell bec

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Erik wrote, on January 03, 2017 5:26 PM > Hi, > > On 04/01/17 01:12, Deborah Swanson wrote: > > The main reason you might want to catch the StopIteration > exception is > > to do something else before your code simply stops running. If all > > you're doing is run a generator til it's out of gas

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 5:55 PM > > On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > > So can I call the generator twice and receive the same file > twice in 2 > > for loops? > > > > Once to get the files name and the second to process? > > > > for file i

Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
Untested as i wrote this in notepad at work but, if i first use the generator to create a set of filenames and then iterate it then call the generator anew to process file may work? Good idea or better available? def get_list_of_names(generator_arg): name_set = set() for name in generat

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 5:36 PM > > So can I call the generator twice and receive the same file > twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.pat

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Chris Angelico wrote, on January 03, 2017 3:35 PM > > On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson > wrote: > > Ok, I learned how to use generators in Python 2.7.8, which may be > > different from Python 3 for generators. But I learned from MIT's > > online introduction to python course, a

Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > So can I call the generator twice and receive the same file twice in 2 for > loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > w

Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
So can I call the generator twice and receive the same file twice in 2 for loops? Once to get the files name and the second to process? for file in rootobs: base = os.path.basename(file.name) write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") with o

Re: Screwing Up looping in Generator

2017-01-03 Thread Erik
Hi, On 04/01/17 01:12, Deborah Swanson wrote: The main reason you might want to catch the StopIteration exception is to do something else before your code simply stops running. If all you're doing is run a generator til it's out of gas, and that's all you want it to do, then there's no need to c

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Erik wrote, on January 03, 2017 3:53 PM > > On 03/01/17 23:05, Deborah Swanson wrote: > > And yes, we usually used for loops for generators, unless you don't > > know when the generator will be exhausted. As in this case, > where the > > number of files the generator can provide is unknown. The

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Erik wrote, on January 03, 2017 3:45 PM > > Hi, > > On 03/01/17 22:14, Deborah Swanson wrote: > > ...you have to create the generator object first and use it to call > > the next function. And I really don't think you can use a > generator as > > your range in a for loop. So I'd use a 'while T

Re: Screwing Up looping in Generator

2017-01-03 Thread Erik
On 03/01/17 23:05, Deborah Swanson wrote: And yes, we usually used for loops for generators, unless you don't know when the generator will be exhausted. As in this case, where the number of files the generator can provide is unknown. Then we used the while True, break on StopIteration method. O

Re: Screwing Up looping in Generator

2017-01-03 Thread Erik
Hi, On 03/01/17 22:14, Deborah Swanson wrote: ...you have to create the generator object first and use it to call the next function. And I really don't think you can use a generator as your range in a for loop. So I'd use a 'while True', and break out of the loop when you hit the StopIteration e

Re: Screwing Up looping in Generator

2017-01-03 Thread Chris Angelico
On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson wrote: > Ok, I learned how to use generators in Python 2.7.8, which may be > different from Python 3 for generators. But I learned from MIT's online > introduction to python course, and they certainly seem to know python > well. So what is the corre

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
-Original Message- From: Matt Wheeler [mailto:m...@funkyhat.org] Sent: Tuesday, January 03, 2017 1:47 PM To: pyt...@deborahswanson.net; Sayth Renshaw; python-list@python.org Subject: Re: Screwing Up looping in Generator On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > Wha

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Chris Angelico wrote, on January 03, 2017 2:31 PM > > On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson > wrote: > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > Small side point: Try to avoid calling a generator object's > .next() method direc

Re: Screwing Up looping in Generator

2017-01-03 Thread Chris Angelico
On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson wrote: > while True: > try: > file = files.next() > except StopIteration: > break Small side point: Try to avoid calling a generator object's .next() method directly. Normally, when you _do_ want to do this, you should be calling next(fi

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Terry Reedy > > On 1/3/2017 3:53 PM, Deborah Swanson wrote: > > >> I think you're expecting > >> > >>for file in rootobs > >> > >> to get the next yield for you from rootobs, but unless someone > >> corrects me, I don't think you can expect a 'for' statement to do > >> that. You need to hav

Re: Screwing Up looping in Generator

2017-01-03 Thread Terry Reedy
On 1/3/2017 3:53 PM, Deborah Swanson wrote: I think you're expecting for file in rootobs to get the next yield for you from rootobs, but unless someone corrects me, I don't think you can expect a 'for' statement to do that. You need to have a 'next' statement inside your for loop to ge

Re: Screwing Up looping in Generator

2017-01-03 Thread Matt Wheeler
On Tue, 3 Jan 2017 at 21:46 Matt Wheeler wrote: > range() is not part of the for syntax at all, it's completely separate, it > simply returns an iterator which the for loop can use, like any other. > *iterable -- -- Matt Wheeler http://funkyh.at -- https://mail.python.org/mailman/listinfo/pyt

Re: Screwing Up looping in Generator

2017-01-03 Thread Matt Wheeler
On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless someone corrects > me, I don't think you ca

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
> > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > > > Hi > > > > > > > > This is simple, but its getting me confused. > > > > > > > > I have a csv writer that opens a file and loops each line of the > > > > file for each file and then closes, writing one file. > > > > > > > >

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
> > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > Hi > > > > > > This is simple, but its getting me confused. > > > > > > I have a csv writer that opens a file and loops each line of the > > > file for each file and then closes, writing one file. > > > > > > I want to alter the

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
> Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > Hi > > > > This is simple, but its getting me confused. > > > > I have a csv writer that opens a file and loops each line of > > the file for each file and then closes, writing one file. > > > > I want to alter the behaviour to be a wri

RE: Screwing Up looping in Generator

2017-01-03 Thread Deborah Swanson
Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > Hi > > This is simple, but its getting me confused. > > I have a csv writer that opens a file and loops each line of > the file for each file and then closes, writing one file. > > I want to alter the behaviour to be a written file for each

Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
Hi This is simple, but its getting me confused. I have a csv writer that opens a file and loops each line of the file for each file and then closes, writing one file. I want to alter the behaviour to be a written file for each input file. I saw a roundrobin example however it failed for me as