Hello!<div>My name is Denis Kolodin. I live in Russia, Tambov.<div>I was developing much time with C, Java, C#, R. But two month ago I'm using Python.</div><div>It's really cool. Now, I move ALL my projects to it fully and have some ideas which API's</div>
<div>extensions may will be useful.</div><div>The first thing I want to say about is an extension of CSV api. In R language I could to set types for</div><div>the every column in a csv file. I propose to add a same function to the Python's standard library.</div>
<div>Here it is (Python 3 version):</div><div><span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: medium; "><pre style="background-color: transparent; border-top-width: 1px; border-top-style: none; border-top-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: none; border-bottom-color: rgb(204, 204, 204); padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-size: 13px; font-family: 'Bitstream Vera Sans Mono', monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-right-style: none; border-left-style: none; border-width: initial; border-color: initial; overflow-x: auto; overflow-y: auto; ">
<span class="kn" style="color: rgb(0, 112, 32); font-weight: bold; ">import</span> <span class="nn" style="color: rgb(14, 132, 181); font-weight: bold; ">csv</span>
<span class="k" style="color: rgb(0, 112, 32); font-weight: bold; ">def</span> <span class="nf" style="color: rgb(6, 40, 126); ">reader2</span><span class="p">(</span><span class="n">csvfile</span><span class="p">,</span> <span class="n">frame</span><span class="p">,</span> <span class="n"><b>delimiter</b></span><span class="o" style="color: rgb(102, 102, 102); "><b>=</b></span><span class="s" style="color: rgb(64, 112, 160); "><b>';'</b></span><span class="p">,</span> <span class="o" style="color: rgb(102, 102, 102); ">**</span><span class="n">fmtparams</span><span class="p">):</span>
<span class="n">reader</span> <span class="o" style="color: rgb(102, 102, 102); ">=</span> <span class="n">csv</span><span class="o" style="color: rgb(102, 102, 102); ">.</span><span class="n">reader</span><span class="p">(</span><span class="n">csvfile</span><span class="p">,</span> delimiter=delimiter, <span class="o" style="color: rgb(102, 102, 102); ">**</span><span class="n">fmtparams</span><span class="p">)</span>
<span class="k" style="color: rgb(0, 112, 32); font-weight: bold; ">for</span> <span class="n">row</span> <span class="ow" style="color: rgb(0, 112, 32); font-weight: bold; ">in</span> <span class="n">reader</span><span class="p">:</span>
<span class="n">l</span> <span class="o" style="color: rgb(102, 102, 102); ">=</span> <span class="nb" style="color: rgb(0, 112, 32); ">min</span><span class="p">(</span><span class="nb" style="color: rgb(0, 112, 32); ">len</span><span class="p">(</span><span class="n">row</span><span class="p">),</span> <span class="nb" style="color: rgb(0, 112, 32); ">len</span><span class="p">(</span><span class="n">frame</span><span class="p">))</span>
<span class="k" style="color: rgb(0, 112, 32); font-weight: bold; ">yield</span> <span class="p">[</span><span class="n">frame</span><span class="p">[</span><span class="n">idx</span><span class="p">](</span><span class="n">row</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span> <span class="k" style="color: rgb(0, 112, 32); font-weight: bold; ">for</span> <span class="n">idx</span> <span class="ow" style="color: rgb(0, 112, 32); font-weight: bold; ">in</span> <span class="nb" style="color: rgb(0, 112, 32); ">range</span><span class="p">(</span><span class="n">l</span><span class="p">)]</span></pre>
</span></div><div>This's generator function which converts an every column to the associated type.</div><div>In <b><i>frame </i></b>argument you must to set tuple/list of functions which will uses to</div><div>convert values in same positions of row from csv file. Frame looks like list of types )))</div>
<div>By default it uses ';' delimiter to make float values conversion are possible.</div><div><br></div><div>As a sample you have the csv file like:</div><div><b><i>Any spam...; 1; 2.0; 3</i></b></div><div><br></div>
<div>I've saved it to "sample.csv" :)</div><div><br></div><div>If you are using function reader in the standard "csv" module you get rows as a list of strings :(</div><div><i>>>> reader = csv.reader(open("sample.csv"), delimiter=";")</i></div>
<div><div><i>>>> print(next(reader))</i></div><div><i>['Any spam...', ' 1', ' 2.0', ' 3']</i></div><div><i><br></i></div><div><i><span class="Apple-style-span" style="font-style: normal; "><div>
It's not bad in certan situatiuons. But with "reader2" function you can get a list with necessary types:</div><div><br></div><div><div><i>>>> reader = reader2(open("foodstuffs.csv"), (str, int, float, int))</i></div>
<div><i>>>> print(next(reader))</i></div><div><i>['Any spam...', 1, 2.0, 3]</i></div><div><br></div><div>Now you can work with items without extra conversions. <img src="cid:360@goomoji.gmail" style="margin-top: 0px; margin-right: 0.2ex; margin-bottom: 0px; margin-left: 0.2ex; vertical-align: middle; " goomoji="360"></div>
<div>I think it's good to add this function to the standard library. I've already used it many times.</div><div>This function can be useful for many people who works with csv files.</div><div>And I suppose it conforms to "batteries included" philosophy.</div>
<div><br></div><div>What do you think about this extension?</div><div>Is it possible to add this function to standard library or to add the same behavior to</div><div>the standard "readed" function <i><span class="Apple-style-span" style="font-style: normal; "><div style="display: inline !important; ">
<div style="display: inline !important; ">in "csv" Python's module?</div></div></span></i></div><div><br></div><div>Best Regards,</div><div>Denis Kolodin</div></div><div>Russia, Tambov</div></span></i></div>
<div><br></div></div><div><br><div class="gmail_quote">2010/4/10 <span dir="ltr"><<a href="mailto:python-dev-request@python.org">python-dev-request@python.org</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Send Python-Dev mailing list submissions to<br>
<a href="mailto:python-dev@python.org">python-dev@python.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
or, via email, send a message with subject or body 'help' to<br>
<a href="mailto:python-dev-request@python.org">python-dev-request@python.org</a><br>
<br>
You can reach the person managing the list at<br>
<a href="mailto:python-dev-owner@python.org">python-dev-owner@python.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Python-Dev digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
1. Re: PEP 3147, __cached__, and PyImport_ExecCodeModuleEx()<br>
(Guido van Rossum)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Fri, 9 Apr 2010 17:41:48 -0700<br>
From: Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>><br>
To: Paul Moore <<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a>><br>
Cc: Python-Dev Dev <<a href="mailto:python-dev@python.org">python-dev@python.org</a>><br>
Subject: Re: [Python-Dev] PEP 3147, __cached__, and<br>
PyImport_ExecCodeModuleEx()<br>
Message-ID:<br>
<<a href="mailto:h2pca471dc21004091741tb818fdb0k86f65f7adedb7d12@mail.gmail.com">h2pca471dc21004091741tb818fdb0k86f65f7adedb7d12@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=ISO-8859-1<br>
<br>
On Fri, Apr 9, 2010 at 3:54 PM, Paul Moore <<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a>> wrote:<br>
> On 9 April 2010 23:00, Barry Warsaw <<a href="mailto:barry@python.org">barry@python.org</a>> wrote:<br>
>> On Apr 09, 2010, at 02:52 PM, Guido van Rossum wrote:<br>
>><br>
>>>It may be undocumented but it doesn't start with _ and it exists to<br>
>>>preserve backwards compatibility. So I recommend adding<br>
>>>PyImport_ExecCodeModuleExEx().<br>
>><br>
>> Cool, thanks. ?Now I can't wait for PyImport_ExecCodeModuleExExEx() :)<br>
><br>
> Would it be better to name this one _PyImport_ExecCodeModuleExEx (with<br>
> an underscore) so that we *don't* need to create an ExExEx version in<br>
> future? (Sorry, Barry :-))<br>
<br>
I don't care about what name you pick, and my ExEx proposal was meant<br>
to include half a wink, but <a href="http://docs.python.org/c-api/import.html" target="_blank">http://docs.python.org/c-api/import.html</a><br>
makes it clear that PyImport_ExecCodeModuleEx() is far from private!<br>
(I don't know where Barry got that idea.) While Google Code Search<br>
finds mostly references to PyImport_ExecCodeModuleEx in the Python<br>
source code and various copies of it, it also shows some real uses,<br>
e.g.<br>
<a href="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" target="_blank">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</a><br>
<br>
--<br>
--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)<br>
<br>
<br>
------------------------------<br>
<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
<br>
<br>
End of Python-Dev Digest, Vol 81, Issue 31<br>
******************************************<br>
</blockquote></div><br></div></div>