This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def _mangle_name(self, name): | |
| """ | |
| Any identifier of the form __spam (at least two leading underscores, | |
| at most one trailing underscore) is textually replaced with | |
| _classname__spam, where classname is the current class name with | |
| leading underscore(s) stripped. | |
| Truncation may occur when the mangled name would be longer than | |
| 255 characters. When the class name consists of only underscores, | |
| no mangling occurs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| stmt = dbm.prepare "INSERT OR REPLACE INTO items VALUES ( ?, ?, ? );" | |
| begin | |
| items.each do |item| | |
| stmt.execute(*item) | |
| end | |
| ensure | |
| stmt.close rescue nil | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Random sort an array (via http://d.hatena.ne.jp/tanakaBox/20070512/1178915094) | |
| # Not perfect, but simple. | |
| # Usage: | |
| # >> (1..10).to_a.shuffle | |
| # => [1, 3, 2, 5, 9, 7, 6, 8, 4, 10] | |
| class Array | |
| def shuffle() self.to_a.shuffle!; end | |
| def shuffle!() self.sort! { rand(3) - 1 }; end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % easy_install --find-links http://www.pythonware.com/products/pil/ Imaging |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Handling urllib2.HTTPError, urllib2.URLError | |
| # (via http://www.voidspace.org.uk/python/articles/urllib2.shtml) | |
| from urllib2 import Request, urlopen, URLError, HTTPError | |
| req = Request(someurl) | |
| try: | |
| response = urlopen(req) | |
| except HTTPError, e: | |
| print 'The server couldn\'t fulfill the request.' | |
| print 'Error code: ', e.code | |
| except URLError, e: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| def _configureLogger(): | |
| """Returns configured default logger for module.""" | |
| handler = logging.StreamHandler(sys.stderr) | |
| handler.setFormatter(logging.Formatter(LOGGER_FORMAT)) | |
| logger = logging.getLogger('org.metareal.XmlPropertyListParser') | |
| logger.setLevel(LOGGER_LEVEL) | |
| logger.addHandler(handler) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Apple Property List parser for Python | |
| # (via http://effbot.org/zone/element-iterparse.htm) | |
| # | |
| # ``xml.etree.ElementTree`` is fast (and smart looks) for small xml, | |
| # but for large (more than 5MB) xml, SAX implementation is still fast. | |
| def xml_plist_parse(self, xml_input): | |
| import base64, time, datetime | |
| from xml.etree.ElementTree import iterparse | |
| callbacks = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import math | |
| import array | |
| class BitArray(object): | |
| """ | |
| A bit array (or bitmap, in some cases) is an array data structure which | |
| compactly stores individual bits (0 or 1). | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def insertion_sort(lst): | |
| """ | |
| >>> insertion_sort([3, 4, 2, 10, 1]) | |
| [1, 2, 3, 4, 10] | |
| >>> insertion_sort([1, 2, 3, 4, 5]) | |
| [1, 2, 3, 4, 5] | |
| >>> insertion_sort([]) | |
| [] | |
| """ | |
| for i in xrange(1, len(lst)): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def factorial(n): | |
| return reduce(lambda x, y: x*y, range(1, n+1)) | |