eDonkey2000 Hash in Python
As part of a filesystem watcher program I maintain as part of a larger site, I need to obtain the ed2k hash of some files. Believe me when I tell you I have searched high and low for an Edonkey2000 hash algorithm in python. I’ve seen massive libraries that scared the bejesus out of me, but nothing clean, concise, and without external dependencies. Enjoy the below. It’s worth noting it’s just as fast as the C version.
import hashlib def hash_file(self, file_path): """ Returns the ed2k hash of a given file. """ md4 = hashlib.new('md4').copy def gen(f): while True: x = f.read(9728000) if x: yield x else: return def md4_hash(data): m = md4() m.update(data) return m with open(file_path, 'rb') as f: a = gen(f) hashes = [md4_hash(data).digest() for data in a] if len(hashes) == 1: return hashes[0].encode("hex") else: return md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
Django On-Demand Model Fields
I'm writing an Inventory system for work in my downtime, part to brush up my rusting Django skills, part because we really need one.
We started with a few requirements for what would make a good inventory system to meet our needs, and one of those was that it would be painless to add new types of items and have the ability to customize them. If you know much about Django models, you probably know enough to know that models are typically hard-baked. If you're writing a blog system, you might create a model with fields for Title, Author, Date, Text. Well for us, what if I want Widget X to have a field for Serial Number, and Widget Y to have a field for Firmware?
So, Passenger is actually pretty useful
I have to admit, I'd heard of Passenger/mod_rails before, but I only found out yesterday by chance that it also handles WSGI. Since most of any serious work I do is with Python, this caught my attention. Passenger turned out to be simple to setup, and braindead-simple to get running.
I've gone through several iterations of how to run my WSGI, from Apache mod_wsgi to Nginx mod_wsgi to Nginx proxy + CherryPy, and now back to Apache “mod_rails”. As an aside, Nginx mod_wsgi sounds like a good idea, but it isn't. The author has written about this as well, and I encourage you to Google for more information.
One thing to remember when you setup your WSGI app with Passenger is that your passenger_wsgi.py file should be in the directory above your DocumentRoot. So if your docroot is /var/www/my.site/public, put the .py in /var/www/my.site/
I also run two redmines, so this simplifies that as well, since Passenger does rails primarily.
Goodbye, Nginx TCP proxying.
Reverse Bitwise Flag Calculator
Sometimes when you need to deal with bitwise flags, it can get a little difficult to figure out what that gigantic number you’re using really consists of. The below should help:
#!/usr/bin/python #bc.py - ./bc.py 108 = flags that make up 108. import sys def decompose(i): limit = i+1 tmpi = 1 while tmpi < limit: if i & tmpi == tmpi: print tmpi tmpi *= 2 if __name__ == "__main__": try: b = int(sys.argv[1]) decompose(b) except: print "You need to specify an integer" exit(0)