Popular recipes by James Mills http://code.activestate.com/recipes/users/4167757/2013-07-31T23:23:02-07:00ActiveState Code RecipesSending Email in Python (Python)
2013-02-24T13:47:01-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578472-sending-email-in-python/
<p style="color: grey">
Python
recipe 578472
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/email_sending/">email_sending</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/smtp/">smtp</a>).
</p>
<p>Every Python Application needs to send email at some point. Whether it's for reporting errors, status updates or simply the core functionality of the system this little recipe should help! Documented and Tested.</p>
System Authentication against /etc/shadow or /etc/passwd (Python)
2013-03-11T12:40:10-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578489-system-authentication-against-etcshadow-or-etcpass/
<p style="color: grey">
Python
recipe 578489
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/authentication/">authentication</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/system/">system</a>).
</p>
<p>Sometimes it's useful to perform System Authentication against a Local System using the /etc/shadow or /etc/passwd password databases. This recipe provides a simple function that does exactly that.</p>
Password Generator (mkpasswd) (Python)
2013-07-31T23:23:02-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578468-password-generator-mkpasswd/
<p style="color: grey">
Python
recipe 578468
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/mkpasswd/">mkpasswd</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/secure/">secure</a>).
Revision 3.
</p>
<p>Since everyone is posting one of these, I thought I'd post mine. I wrote this many years ago and use it everywhere.</p>
First Class Enums in Python (Python)
2013-03-07T13:10:29-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578485-first-class-enums-in-python/
<p style="color: grey">
Python
recipe 578485
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/immutable/">immutable</a>).
</p>
<p>True immutable symbolic enumeration with qualified value access.</p>
Calculating Swatch Internet Time (or Beats) (Python)
2013-02-25T11:58:05-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578473-calculating-swatch-internet-time-or-beats/
<p style="color: grey">
Python
recipe 578473
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/beats/">beats</a>, <a href="/recipes/tags/internet_time/">internet_time</a>, <a href="/recipes/tags/swatch/">swatch</a>).
</p>
<p>Simple function calculating Swatch Internet Time (or no. of beats).</p>
Formatting numbers with a state machine (implementation of a regex pattern) (Python)
2011-03-22T03:40:45-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577618-formatting-numbers-with-a-state-machine-implementa/
<p style="color: grey">
Python
recipe 577618
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>).
</p>
<p>I was once asked to explain how the following regular expression works which formats any integer with commas for every thousand (or group of 3 digits):</p>
<pre class="prettyprint"><code>(\d)(?=(\d{3})+$)
</code></pre>
<p>Example:</p>
<pre class="prettyprint"><code>>>> import re
>>> re.sub("(\d)(?=(\d{3})+$)", "\\1,", "1234")
'1,234'
</code></pre>
<p>So here is an implementation of the above regular expression (as best as I could over a lunch break) that will hopefully highlight
how a regular expression engine and finite automa work.</p>
<p>Comments and feedback welcome!</p>
<p>--JamesMills / prologic</p>
Populate SQL tables from CSV data files (Python)
2011-02-02T21:21:49-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577559-populate-sql-tables-from-csv-data-files/
<p style="color: grey">
Python
recipe 577559
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sql/">sql</a>).
</p>
<p>Just a quick recipe I developed a few years ago that I thought
might be useful to others. Basically it takes as input a
data file with comma separated values (CSV) and translates
this into a series of SQL "INSERT" statements allowing you
to then feed this into MySQL, SQLite, or any other database.</p>
<p>Example Usage:</p>
<p>$ cat cars.csv
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38</p>
<p>$ sqlite3 cars.db "CREATE TABLE cars (Year, Make, Model, Length)"</p>
<p>$ ./csv2sql.py cars.csv | sqlite3 cars.db </p>
<p>$ sqlite3 cars.db "SELECT * FROM cars"
1997|Ford|E350|2.34
2000|Mercury|Cougar|2.38</p>
<p>Enjoy! Feedback welcome!</p>
<p>cheers
James Mills / prologic</p>
Filtering CSV data by fields (cut for csv) (Python)
2011-02-02T21:39:45-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577560-filtering-csv-data-by-fields-cut-for-csv/
<p style="color: grey">
Python
recipe 577560
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/cut/">cut</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Ever wanted to take a CSV file as input, cut it up
and only extract the fields that you want ?</p>
<p>Here's how!</p>
<p>$ cat cars.csv
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38</p>
<p>$ csvcut.py -f 0 -f -1 - < cars.csv
Year,Length
1997,2.34
2000,2.38</p>
<p>--JamesMills (prologic)</p>
Simple Web Crawler (Python)
2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/
<p style="color: grey">
Python
recipe 576551
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p>
<p>This is a simple web crawler I wrote to
test websites and links. It will traverse
all links found to any given depth.</p>
<p>See --help for usage.</p>
<p>I'm posting this recipe as this kind of
problem has been asked on the Python
Mailing List a number of times... I
thought I'd share my simple little
implementation based on the standard
library and BeautifulSoup.</p>
<p>--JamesMills</p>