[Python-Dev] Python-Dev Digest, Vol 81, Issue 31
Denis Kolodin
deniskolodin at gmail.com
Sat Apr 10 20:53:38 CEST 2010
Hello!
My name is Denis Kolodin. I live in Russia, Tambov.
I was developing much time with C, Java, C#, R. But two month ago I'm using
Python.
It's really cool. Now, I move ALL my projects to it fully and have some
ideas which API's
extensions may will be useful.
The first thing I want to say about is an extension of CSV api. In R
language I could to set types for
the every column in a csv file. I propose to add a same function to the
Python's standard library.
Here it is (Python 3 version):
import csv
def reader2(csvfile, frame, *delimiter**=**';'*, **fmtparams):
reader = csv.reader(csvfile, delimiter=delimiter, **fmtparams)
for row in reader:
l = min(len(row), len(frame))
yield [frame[idx](row[idx]) for idx in range(l)]
This's generator function which converts an every column to the associated
type.
In *frame *argument you must to set tuple/list of functions which will uses
to
convert values in same positions of row from csv file. Frame looks like list
of types )))
By default it uses ';' delimiter to make float values conversion are
possible.
As a sample you have the csv file like:
*Any spam...; 1; 2.0; 3*
I've saved it to "sample.csv" :)
If you are using function reader in the standard "csv" module you get rows
as a list of strings :(
*>>> reader = csv.reader(open("sample.csv"), delimiter=";")*
*>>> print(next(reader))*
*['Any spam...', ' 1', ' 2.0', ' 3']*
*
*
*
It's not bad in certan situatiuons. But with "reader2" function you can get
a list with necessary types:
>>> reader = reader2(open("foodstuffs.csv"), (str, int, float, int))
>>> print(next(reader))
['Any spam...', 1, 2.0, 3]
Now you can work with items without extra conversions. [?]
I think it's good to add this function to the standard library. I've already
used it many times.
This function can be useful for many people who works with csv files.
And I suppose it conforms to "batteries included" philosophy.
What do you think about this extension?
Is it possible to add this function to standard library or to add the same
behavior to
the standard "readed" function
in "csv" Python's module?
Best Regards,
Denis Kolodin
Russia, Tambov
*
2010/4/10 <python-dev-request at python.org>
> Send Python-Dev mailing list submissions to
> python-dev at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/python-dev
> or, via email, send a message with subject or body 'help' to
> python-dev-request at python.org
>
> You can reach the person managing the list at
> python-dev-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Python-Dev digest..."
>
>
> Today's Topics:
>
> 1. Re: PEP 3147, __cached__, and PyImport_ExecCodeModuleEx()
> (Guido van Rossum)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 9 Apr 2010 17:41:48 -0700
> From: Guido van Rossum <guido at python.org>
> To: Paul Moore <p.f.moore at gmail.com>
> Cc: Python-Dev Dev <python-dev at python.org>
> Subject: Re: [Python-Dev] PEP 3147, __cached__, and
> PyImport_ExecCodeModuleEx()
> Message-ID:
> <h2pca471dc21004091741tb818fdb0k86f65f7adedb7d12 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Fri, Apr 9, 2010 at 3:54 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> > On 9 April 2010 23:00, Barry Warsaw <barry at python.org> wrote:
> >> On Apr 09, 2010, at 02:52 PM, Guido van Rossum wrote:
> >>
> >>>It may be undocumented but it doesn't start with _ and it exists to
> >>>preserve backwards compatibility. So I recommend adding
> >>>PyImport_ExecCodeModuleExEx().
> >>
> >> Cool, thanks. ?Now I can't wait for PyImport_ExecCodeModuleExExEx() :)
> >
> > Would it be better to name this one _PyImport_ExecCodeModuleExEx (with
> > an underscore) so that we *don't* need to create an ExExEx version in
> > future? (Sorry, Barry :-))
>
> I don't care about what name you pick, and my ExEx proposal was meant
> to include half a wink, but http://docs.python.org/c-api/import.html
> makes it clear that PyImport_ExecCodeModuleEx() is far from private!
> (I don't know where Barry got that idea.) While Google Code Search
> finds mostly references to PyImport_ExecCodeModuleEx in the Python
> source code and various copies of it, it also shows some real uses,
> e.g.
>
> http://www.google.com/codesearch/p?hl=en#bkFK9YpaWlI/ubuntu/pool/universe/y/yehia/yehia_0.5.4.orig.tar.gz|PZ0_Xf7QzC0/yehia-0.5.4.orig/plugins/python/python-loader.cc&q=PyImport_ExecCodeModuleEx
>
> --
> --Guido van Rossum (python.org/~guido)
>
>
> ------------------------------
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
>
>
> End of Python-Dev Digest, Vol 81, Issue 31
> ******************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100410/3b282197/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 360.gif
Type: image/gif
Size: 453 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100410/3b282197/attachment.gif>
More information about the Python-Dev
mailing list