A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
| #!/usr/bin/env python | |
| import random | |
| class Markov: | |
| def __init__(self, file, size): | |
| self.size = size | |
| self.starts = [] | |
| self.cache = {} | |
| self.file_to_words(file) | |
| self.parse_words() |
| #See: http://daringfireball.net/2010/07/improved_regex_for_matching_urls | |
| import re, urllib | |
| GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))') | |
| for line in urllib.urlopen("http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text"): | |
| print [ mgroups[0] for mgroups in GRUBER_URLINTEXT_PAT.findall(line) ] | |
| import objc | |
| import AddressBook as ab | |
| import pprint as pp | |
| def pythonize(objc_obj): | |
| if isinstance(objc_obj, objc.pyobjc_unicode): | |
| return unicode(objc_obj) | |
| elif isinstance(objc_obj, ab.NSDate): | |
| return objc_obj.description() |
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use HTTP::Response; | |
| my $response = HTTP::Response->new( | |
| 200, 'OK', [ 'Content-Type' => 'multipart/form-data' ] | |
| ); |
| import numpy | |
| # Imagine these come from a db cursor or something | |
| coordinates = [(1,2,3), (4,5,6), (7,8,9)] | |
| def my_gen(some_tuple): | |
| for x, y, z in some_tuple: | |
| yield x, y, z | |
| a = numpy.fromiter(my_gen(coordinates), dtype=[('x', 'l'), ('y', 'l'), ('z', 'l')]) | |
| """ |
| from datetime import datetime, date | |
| from sqlalchemy.orm.query import Query | |
| def render_query(statement, bind=None): | |
| """ | |
| Generate an SQL expression string with bound parameters rendered inline | |
| for the given SQLAlchemy statement. | |
| WARNING: This method of escaping is insecure, incomplete, and for debugging | |
| purposes only. Executing SQL statements with inline-rendered user values is |
| This is free and unencumbered software released into the public domain. | |
| Anyone is free to copy, modify, publish, use, compile, sell, or | |
| distribute this software, either in source code form or as a compiled | |
| binary, for any purpose, commercial or non-commercial, and by any | |
| means. | |
| In jurisdictions that recognize copyright laws, the author or authors | |
| of this software dedicate any and all copyright interest in the | |
| software to the public domain. We make this dedication for the benefit |
| # gap.py | |
| # (c) 2013 Mikael Vejdemo-Johansson | |
| # BSD License | |
| # | |
| # SciPy function to compute the gap statistic for evaluating k-means clustering. | |
| # Gap statistic defined in | |
| # Tibshirani, Walther, Hastie: | |
| # Estimating the number of clusters in a data set via the gap statistic | |
| # J. R. Statist. Soc. B (2001) 63, Part 2, pp 411-423 |
| import unittest | |
| from mock import call, patch | |
| class PatcherTestCase(unittest.TestCase): | |
| def setUp(self): | |
| self.patcher = patch('some_module.some_object') | |
| self.mock_object = self.patcher.start() |