Link:
http://dannyman.toldme.com/2012/02/08/derr/
From an email to colleagues:
Yesterday I got Dr Sick Wife to drop me off at the Santa Clara Convention Center so Mr Sicker Light-Headed Husband wouldn’t miss any PYCON. After an awkward twenty minutes of asking people there for the lighting and LED conference where the Python was, I checked my smart phone and noted that … PYCON is *MARCH* 7th.
So I took the light rail home and told $BOSS I was on PTO (well, I call it MLK day due to Puppet Training) I then slept a lot, and did other things sick people do that don’t bear repeating in a professional context, and watched Dr Who save the Earth on TV, slept some more, and I am feeling way better today, which means I feel regular sick, not super sick.
So, I’ll be WFH today. Trust me, whatever this is, you’re lucky to miss out! I don’t normally get sick so this is a novel experience … I’ll likely be seen in the office next week, though if I’m coughy or sneezy I’ll keep that train wreck at home, because, as you might gather, you don’t want a piece of this!
If you’re attending PyCon, I look forward to seeing you there … next month! Hopefully I won’t be light-headed!
Feedback Welcome
Link:
http://dannyman.toldme.com/2010/06/30/python-list-comma-comma-and/
Well, I am working on extending a Django application to add log entries to the django.contrib.admin.models.LogEntry which may be fodder for another post, but while composing a change_message I wanted to convert a list of “changes” into a string like “Changed this thing, that thing, and that other thing.”
Here is what I have got, and since it is Python I bet $1 that someone will comment with a better way. (I couldn’t figure a good search query for seeking the answer so I had to use my brain.)
if len(updated_list) > 1:
rs = "Changed " + ", ".join(map(str, updated_list[:-1])) + " and " + updated_list[-1] + "."
else:
rs = "Changed " + updated_list[0] + "."
>>> updated_list = ['just one thing']
>>> print "Changed " + updated_list[0] + "."
Changed just one thing.
>>> updated_list = ['one thing', 'another thing']
>>> print "Changed " + ", ".join(map(str, updated_list[:-1])) + " and " + updated_list[-1] + "."
Changed one thing and another thing.
>>> updated_list = ['this thing', 'that thing', 'that other thing']
>>> print "Changed " + ", ".join(map(str, updated_list[:-1])) + " and " + updated_list[-1] + "."
Changed this thing, that thing and that other thing.
Feedback Welcome
Link:
http://dannyman.toldme.com/2009/12/08/perl-vs-python-documentation/
Based on something I posted to Facebook:
Perl’s “natural language” emphasis and non-obvious locutions encourage developers to document their code. Many times I have put some hard work into a line or a block of code, and ended up writing in some comments as a part of the process.
Python code is nice and readable but often very poorly documented. “The code should speak for itself,” and while it is easier to read Python code sometimes a few human-language comments would save some time and annoyance.
Maintaining Python code is easier than maintaining Perl, but using Perl modules is often easier than using Python modules because Perl developers are kinda forced to explain their work.
One of the first things I had to accept about pydoc is it is almost universally worthless, and even good functional documentation will often omit the sort of useful examples that I am most likely to find quickly intuitive.
This is why I have transitioned so slowly. Python’s strengths breed a certain weakness just as Perl’s weaknesses breed a certain strength.
At this point, I would consider myself approaching bi-linguality, and pretty comfortable in either language. If I have a preference it would be for Python, because I am lazy about documentation and even maintaining my own code, it is a lot easier to figure out what I wrote in Python somewhat after the fact. But my fondness for Perl and its generally more approachable documentation stands, and I’m not about to dis what has served me so long and so well.
1 Comment
Link:
http://dannyman.toldme.com/2009/12/04/django-migrate-database-sqlite-mysql/
This was painful. I’ll be doing this again next week, then hopefully never again.
First off, if you ever hit an error, drop and re-create your MySQL database, or at least drop the tables, or things just get weirder.
Grab the data from SQLite:
python manage.py dumpdata > $APPNAME.json
The, update settings.py to connect to the MySQL database, and if you are really lucky:
python manage.py syncdb
python manage.py reset contenttypes
python manage.py loaddata $APPNAME
(Thank you, Carl Meyer!)
Now, here’s the cute things I ran into. I had originally built a model with a TextField primary key. This is fine by SQLite but when Django tries to create a BLOB field you get in trouble asking for it to be unique, never mind a primary key. Fortunately, it was easy enough for me to simply change it to a CharField, which will tell Django to use VARCHAR. (SQLite certainly didn’t mind.)
The other was that neither Django nor SQLite were enforcing field length limitations, so I would hit some errors when loaddata tried to bring in database entries that were too long. I worked around this by raising my length limitations. There was also some ugliness with a UTF-8 string, which I solved by creating the text object in question.
1 Comment
Link:
http://dannyman.toldme.com/2009/06/11/howto-python-feedparser-twiki-rss/
To make up for my snarkiness in my last post . . . it is an easy matter of fetching the WebRss node from Twiki and running it through the Universal Feed Parser:
# Twiki RSS Feed
twiki_rss_url='http://localhost/twiki/bin/view/Main/WebRss'
import feedparser
import time
import calendar
# http://www.feedparser.org/
d = feedparser.parse(twiki_rss_url)
for e in d['entries']:
# e.updated_parsed = tuple UTC
# calendar.timegm = seconds UTC
# time.localtime = tuple locale
print time.strftime("%Y-%m-%d %H:%M",
time.localtime(calendar.timegm(e.updated_parsed)))
print e.rdf_value # Author
print e.title
print
1 Comment