[Tutor] More Pythonic?

richard kappler richkappler at gmail.com
Wed Sep 9 15:47:04 CEST 2015


> It's not clear why you need the try...except: pass. Please provide some
more background information.

I don't need the try, this was more of a "are there different ways to do
this, which is better and why?" experiment. I am learning, so tend to write
script that is more brute force than elegant and pythonic, wish to write
better code. I do okay, but there are many nuances to Python that I just
haven't run across. For example:

> with open(sourcefile) as instream:
>    with open(destfile, "a") as outstream:
>        outstream.writelines(process_lines(instream))

I had no idea I could nest with statements like that. It seems obvious now,
but I didn't know.

For the record, I have made a couple other posts this morning that explain
the script constraints far better than I did here. For the sake of brevity
I shant repeat the info here other than to say it's not reading from stdin,
but from a log file to simulate stdin in a test environment.

regards, Richard

On Wed, Sep 9, 2015 at 9:37 AM, Peter Otten <__peter__ at web.de> wrote:

> richard kappler wrote:
>
> > Would either or both of these work, if both, which is the better or more
> > Pythonic way to do it, and why?
> >
> > #######################
> >
> > import whatIsNeeded
> >
> > writefile = open("writefile", 'a')
> >
> > with open(readfile, 'r') as f:
> >     for line in f:
> >         if keyword in line:
> >             do stuff
> >             f1.write(line)
> >         else:
> >             f1.write(line)
>
> Why do you invoke f1.write() twice?
>
> > writefile.close()
> >
> > ######################
> >
> > import whatIsNeeded
> >
> > with open(readfile, 'r') as f:
> >     for line in f:
> >         try:
> >             if keyword in line:
> >                 do stuff
> >         except:
>
> What exceptions are you expecting here? Be explicit. You probably don't
> want
> to swallow a KeyboardInterrupt. And if something unexpected goes wrong a
> noisy complaint gives you the chance to either fix an underlying bug or
> explicitly handle the exception in future runs of the script.
>
> >             do nothing
>
> That's spelt
>               pass
>
> >         with open(writefile, 'a') as f1:
> >             f1.write(line)
>
> Opening the file once per line written seems over-the-top to me.
>
> > ######################
> >
> > or something else altogether?
>
> I tend to put the processing into into a generator. That makes it easy to
> replace the source or the consumer:
>
> def process_lines(instream):
>     for line in instream:
>         if keyword in line:
>             do stuff
>             yield line
>
> with open(sourcefile) as instream:
>     with open(destfile, "a") as outstream:
>         outstream.writelines(process_lines(instream))
>
> Now if you want to read from stdin and print to stdout:
>
> sys.stdout.writelines(process_lines(sys.stdin))
>
> > I'm thinking the first way is better as it only opens the files once
> > whereas it seems to me the second script would open and close the
> > writefile once per iteration, and the do nothing in the except seems just
> > wrong to me.
>
> It's not clear why you need the try...except: pass.
> Please provide some more background information.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 

All internal models of the world are approximate. ~ Sebastian Thrun


More information about the Tutor mailing list