original) (raw)
(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 csvdef 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@python.org>
Send Python-Dev mailing list submissions to
python-dev@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@python.org
You can reach the person managing the list at
python-dev-owner@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@python.org>
To: Paul Moore <p.f.moore@gmail.com>
Cc: Python-Dev Dev <python-dev@python.org>
Subject: Re: \[Python-Dev\] PEP 3147, \_\_cached\_\_, and
PyImport\_ExecCodeModuleEx()
Message-ID:
<h2pca471dc21004091741tb818fdb0k86f65f7adedb7d12@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@gmail.com> wrote:
> On 9 April 2010 23:00, Barry Warsaw <barry@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@python.org
http://mail.python.org/mailman/listinfo/python-dev
End of Python-Dev Digest, Vol 81, Issue 31
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*