Python for Pinocchio
So I discovered Dave’s Web Of Lies a while ago, and was at about the same time looking for random things I could code in Python to improve my skills…
Having now finished my job at the ADC, I’ve had time to put it together. I present my python Lie Generator. It’s not much, but it was fun to make. It honed my regex skills rather a lot (DWOL doesn’t include any kind of API), and is also able to deal gracefully with internet-connectivity problems. It works with Python 3 only (due to changes to their urllib library) and must be called from the command line. I might someday work it into the website in some fashion.
I should probably reiterate what’s stated in the help text: copyright for all lies remains as stated on DWOL.
Thought I’d give you a few comments:
1) It’s best to write python scripts like C, with a main() function, and then put
if __name__ == “__main__”:
main(*sys.argv[1:])
at the bottom. This makes it feel more like a program than a hacky script (even though it’s no more effort), and encourages you to write it in a clean, readable top-down style.
2) If it’s xml/xhtml, then try using xml.etree.ElementTree.parse() (etree’s API is a lot nicer than DOM) or something. Rexexps and hard-coded line numbers are fragile.
3) Try using “if ‘-r’ in sys.argv:” if a flag is really what you want. single-letter flags are very old-style unix though. I’m not going to recommend optparse for this because it’s overkill, but maybe an aptitude/svn/bzr-style subcommand (like $ tellalie.py random or $ tellalie.py daily) might be more appropriate in this day and age.
4) Please Please Please read pep8: http://www.python.org/dev/peps/pep-0008/. Style is very important, especially if you’re ever thinking of reading your code in the future. Obvious thing include comments in the wrong place and multiple imports on the same line.
5) It is currently recommended that you write API-compatible 2.6 versions of any python3 modules you write (so i’d be surprised if urllib/urllib2 didn’t have some backwards compatible functions. In the worst case, you can wrap your imports in try/catch). If you want to “from __future__ import print_function” then that’s cool too.
Thank you
I’m well aware that the line numbers are technically fragile (though in the case of DWOL it’s very unlikely there’ll ever be a redesign), but the regexps were a large part of why I wanted to write the thing in the first place!
I’ll have a look at etree nonetheless, but for this instance, the coding was to teach me something specific (ish) rather than to learn a new library.
I think a flag is actually easier, given that there’s only really one that matters (-d). -r is there for completeness but it defaults to a random lie anyway. I’ve always preferred these to the longer-hand form that apt/bzr/etc use.
I happen to live with the man who wrote DWOL, so he can probably provide an API, but still, it was a learning process, right?
Indeed, I remember finding that out a couple of years back, in The Bar, I think. However I notice now he’s not mentioned at all on the site, whilst three other blokes are…? All part of the lie?
When I said I found out “a while ago” – I did originally discover it yonks back, forgot, remembered whilst talking to Ian, forgot, and discovered it again a month before this blog post.
May do an updated version someday, especially if a simple API was created… though currently working on a rather more complex python app that finds optimal arrangements of “items” that have preferences on which other “items” they’re near, for a given configuration of “slots” that these “items” can be placed in. Savvy?