A blatant repost, mostly so I can easily find this excellent overview of fast python data structures later.
Posts Tagged python
I have been hacking at a quick command line tool for python for managing my address book. Being a bit of a over-sharer I’ve been planning to distribute it by Python Package Index (PyPI) to share, in case other folks find it handy. Along the way I’ve run into several problems distributing it, ‘natch.
Rather than dirty up my address book workspace with testing, I created (and distributed) cltool, an example python command line tool. The cltool project is a simple demo of how distribute and setup tools work to create an installer. Here’s a quick overview of it.
All of the good sauce of the project is in the setup.py script. Python’s distribute tools are pretty powerful, except the lack of an good uninstall. My first attempt was using just the scripts entry to list what scripts get installed. However after some extra digging, I discovered that you can create an entry point called console_scripts to point directly to python functions to execute.
TESTING.md and READING.md
These two files contain a detailed overview of what the project does, and gets into some of the key points of how setup works. TESTING.md also includes recommendations for local testing of your new package.
If you ever want to deliver a command line tool for easy_install or pip, cltool is a good template to start from
I met someone in Philly via the interwebs that wanted to learn some python and work on a nity project. We grabbed some coffee, and decided to tag-team updating and expanding dandies.org python tool and moving the dandies code in a git repo so that other people can contribute to the code base.
If you want to learn some python, and work on some cool grey hat tools, there’s plenty to do to clean and expand that code, so drop a comment and we can rock from there!
Boilerplate python has been on hold for a long time, but I do have an entry to add to it. I wanted to track some changes on a static website by the City of Philadelphia, and a few minutes with google turned op some solutions, but none of what I wanted.
So, in true coder form, I spent a couple of hours and created a quick static page watcher program. It simply makes a local cache of a page, and watches for changes from that. It’s dirt simple, and it gets the job done. If I end up using it a lot, I’ll add some extra features (real parameters, date/time generated info not being counted as changes, etc. But for now, it watches the pages I want to watch, and helps me keep an eye out for sneaky govn’t updates to obscure policies,etc.
PEP 08 is pretty awesome, and is (pretty much) the standard for coding in python at a low level. But style guidelines are beyond that, the google python style guide is pretty good. It’s a good overview of what google does or does not do, and some of it (limited List Comprehension for sanity) is pretty sharp. There are a few things I’m not so sure on though.
I’m not sure about the Default Iterator, which I think work well if your objects are named clearly, but can be hard to gronk if the object types aren’t totally clear. And the ‘generators’ description was a bit weak. I see a lot of generator failure and abuse, using generators for for loops, or very poorly build hard to understand yields, and they don’t cover that much at all. Avoiding apply, filter, map, reduce isn’t something I do as of now, and I’d like to hear more reasons why they consider it bad practice. Default Argument Values (don’t use mutable objs as default values) is pretty smart, I hadn’t thought about it, but as soon as I read it, I realize it’s a good idea, along with Calling code must use named values for the default args. I’ve never used Lexical Scoping at all, exactly because of the PEP-0227 unclear-bug-thing.Modules should begin with #!/usr/bin/env python
Python-twitter is a great way to get into twitter from python language. To use it, first you need to grab python-twitter and simplejson you can do that from the command line on most machines by running:
easy_installl simplejson easy_install python-twitter
Now that python twitter is on you machine, you can make a dirt simple command line interface by using the following code:
__code_uuid__ = '02E0412A-6C88-43C2-BB6D-CDB76AE6156D' import twitter from optparse import OptionParser def main(): oParser = OptionParser() oParser.add_option("-u", "--username", dest="username", help="your twitter username") oParser.add_option("-p", "--password", dest="password", help="your twitter password") (options, args) = oParser.parse_args() #print 'main' try: #open your twitter interface interface = twitter.Api(username=options.username, password=options.password) # turn your text into a single string if you forgot to put it in quotes if( len(args) > 1): args = ' '.join(args) # post your message to twitter result = interface.PostUpdate(args) except Exception, inst: # in case of error, print it out. Probably a network problem. print str(inst) print result if __name__ == "__main__": main()
You can get more info at this great weblog post.
Python has some nifty built in logging functionality, using the logging module. But sometimes the complexity of the logging module can get it’s own way. So here is logging for the thoughtless! To setup python logging, add to the top of your python code:
import logging # print everything from logging.debug() right to console (via stderr). logObj = logging.getLogger("log_to_stderr") logObj.setLevel(logging.DEBUG) # setup so it just writes the message, and nothing else h=logging.StreamHandler() h.setFormatter(logging.Formatter("%(message)s")) logObj.addHandler(h) #make a short alais for logging, so code is more compact and easier to read log = logObj.debug log_exception = logObj.exception
That sets up basic debug logging, and gives you an easy log(“Message Goes Here”) logging style to use. What are you still reading for? Go log some debugging messages my friend. Or if you are procrastinating hang around. I’ll go into logging details a bit more after the break.
Read the rest of this entry »