background preloader

How to Tango with Django: A Python Django Tutorial

How to Tango with Django: A Python Django Tutorial

Argument binding in python - Alon Horev After a recent debate about argument binding I decided to list some pros and cons of the different methods to bind arguments in python. Let’s start with a review of available methods: My beef with partial partial is not a function and misbehaves where functions are expected. partial can be easily implemented in pure python but I can only guess it was implemented in C for performance considerations. Lets review some examples: 1. Why is that? You know how self always gets assigned the instance when calling an instance method? Here’s how a method call works: 2. partial cannot be inspected: 3. partial could be safer, validating argument counts and names: Alternatives to partial You could implement your own partial, that actually returns a function: Note: don’t use this code, it doesn’t enforce unique keyword arguments. You could also use a lambda function: My problem with lambdas As my friend @EyalIL wrote: Lambdas capture the variable, partial captures the value. Why is this happening?

Python Metaclasses without Magic — kr41 July 04, 2013 Python Metaclasses without Magic Every article about Python metaclasses contains a quotation (yep, this one is not exception) by Tim Peters: “Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).” I completely disagree with this saying. Why? As you know, classes in Python are full-featured objects. >>> class SomeClass(object):... pass...>>> SomeClass. When you need to get a custom metaclass, you should inherit it from type. >>> class SomeMetaClass(type):... pass...>>> class AnotherClass(object): # Python 2.x syntax... The syntax shown above usually confuses newbies. >>> obj = SomeClass() What happens in this single line of code? >>> AnotherClass = SomeMetaClass('AnotherClass', (object,), {}) And what is there? If you work with JavaScript, it should be familiar for you. Subscribe a handler on it:

python 3.1 - Creating normal distribution AES encryption of files in Python with PyCrypto [Updated 15.11.2013: passing IV is required in the new PyCrypto] The PyCrypto module seems to provide all one needs for employing strong cryptography in a program. It wraps a highly optimized C implementation of many popular encryption algorithms with a Python interface. PyCrypto can be built from source on Linux, and Windows binaries for various versions of Python 2.x were kindly made available by Michael Foord on this page. My only gripe with PyCrypto is its documentation. In this article I want to present how to use PyCrypto for simple symmetric encryption and decryption of files using the AES algorithm. Simple AES encryption Here's how one can encrypt a string with AES: from Crypto.Cipher import AES key = '0123456789abcdef' IV = 16 * '\x00' # Initialization vector: discussed later mode = AES.MODE_CBC encryptor = AES.new(key, mode, IV=IV) text = 'j' * 64 + 'i' * 128 ciphertext = encryptor.encrypt(text) import hashlib password = 'kitty' key = hashlib.sha256(password).digest() Decryption

Uploading to Dropbox with Python July 21, 2010 I've been searching for an easy way to backup some files that are located on my web server. I use Dropbox to backup lots of important files on my computer, but I don't want to have this software running on my web server for security and performance reasons. So, I used Python to automate the process of uploading a file via the web interface. The process is pretty simple. br = mechanize.Browser() Then I open up the Dropbox login page. br.open(' Now I need to find the proper form to enter the credentials into. isLoginForm = lambda f: f.action == " br.select_form(predicate=isLoginForm) Now that I have the right form, I just set my credentials and submit. br["login_email"] = email br["login_password"] = password response = br.submit() I'm now logged in to Dropbox. isUploadForm = lambda f: f.action == " br.select_form(predicate=isUploadForm)

Python: if/else in list comprehension PEP 378 -- Format Specifier for Thousands Separator PEP Index> PEP 378 -- Format Specifier for Thousands Separator Provide a simple, non-locale aware way to format a number with a thousands separator. Adding thousands separators is one of the simplest ways to humanize a program's output, improving its professional appearance and readability. In the finance world, output with thousands separators is the norm. Finance users and non-professional programmers find the locale approach to be frustrating, arcane and non-obvious. The locale module presents two other challenges. It is not the goal to replace the locale module, to perform internationalization tasks, or accommodate every possible convention. A comma will be added to the format() specifier mini-language: [[fill]align][sign][#][0][width][,][.precision][type] The ',' option indicates that commas should be included in the output as a thousands separator. The proposal works well with floats, ints, and decimals. format(n, "6,d").replace(",", "_") format(n, "6,f").replace(",", "X").replace("

Related: