Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, May 29, 2014

Function Caching via Memoization

To explain memoization, see these different versions of Fibonacci:

Purely recursive. Highly inefficient. Recalculates every value for every calculation.

Iterative. Much faster.

Here is the memoization technique. A 'cache' dictionary of { (args) : (return_val), } is created. This allows us to quickly locate and reuse any value that has already been calculated.

Here is the same memoization design, implemented as a decorator.

Monday, April 28, 2014

Fabric is cool.

[Fabric](http://www.fabfile.org/) is cool. More python, less Bash. For example, I can automate my django db rebuild process, like so:

Monday, March 25, 2013

Using python pymssql to query SQLServer from linux

Getting pyssmql to work was pretty easy. (I had less luck using pyodbc.)

sudo apt-get install freetds

sudo pip install pymssql

Create entries for your sqlservers in /etc/freetds/freetds.conf like so:


[dev01]
host = YOUR_SQL_SERVER_HOST
port = 1433
tds version = 8.0



Here's a simple script using it:


import pymssql
import getpass

pwd = getpass.getpass()

conn = pymssql.connect(host='YOUR_SQL_SERVER_HOST',
                       user='YOUR_DOMAIN\\YOUR_USERNAME', password=pwd)
cur = conn.cursor()
cur.execute('use YOUR_DB')

query = "select top 10 id, name, lastmodifieddate from campaign"

cur.execute(query)

for row in cur:
    print "{0}\n{1}\n{2}\n".format(row[0], row[1], row[2])