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
| import time | |
| def RateLimited(maxPerSecond): | |
| minInterval = 1.0 / float(maxPerSecond) | |
| def decorate(func): | |
| lastTimeCalled = [0.0] | |
| def rateLimitedFunction(*args,**kargs): | |
| elapsed = time.clock() - lastTimeCalled[0] | |
| leftToWait = minInterval - elapsed | |
| if leftToWait>0: |
| import time | |
| from itertools import islice | |
| def batch(batch_size,batch_frequency): | |
| def wrapper(func): | |
| def batch_sizing(iterable): | |
| for i in range(0,len(iterable),batch_size): | |
| new = func(islice(iterable,i,i+batch_size)) | |
| time.sleep(batch_frequency) | |
| return new |
| " enable syntax highlighting | |
| syntax enable | |
| " Color Scheme | |
| set t_Co=256 | |
| colorscheme monokai | |
| " show line numbers | |
| set number |
| from fabric.api import * | |
| import os,json | |
| from os.path import expanduser | |
| env.forward_agent = True | |
| USER_DETAILS = os.path.join(expanduser('~'),'userdetails.json') | |
| def commit(message): | |
| local('git add .') | |
| local('git commit -m "' + message + '"') |
| # Given a two dictionaries empdesig and empdet containing details about employees and managers in a key value pair - print a hierarchical tree | |
| # empdesig = {'Simha': 'Khan', 'Khan': '', 'Vijay': 'Simha', 'Sai': 'Khan', 'Kiran': 'Kiran'} | |
| # empdet = {'Simha': 'Khan', 'Khan': '', 'Vijay': 'Simha', 'Sai': 'Khan', 'Kiran': 'Kiran'} | |
| # If an employee has manager null(like Khan) or has manager as himself - then they are board members and have no bosses. | |
| for emp,mgr in empdesig.items(): | |
| new[emp] = {key:'' for key,value in empdesig.items() if value == emp} | |
| for k,v in new.items(): |
| # Create a new pair | |
| openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout myPrivateKey.key -out myCert.pem | |
| chmod 600 myPrivateKey.key | |
| # Create from existing pair | |
| openssl req -x509 -key ~/.ssh/id_rsa -nodes -days 365 -newkey rsa:2048 -out myCert.pem |
| This lists databases: | |
| SELECT datname FROM pg_database | |
| WHERE datistemplate = false; | |
| This lists tables in the current database | |
| SELECT table_schema,table_name | |
| FROM information_schema.tables | |
| ORDER BY table_schema,table_name; |
| """Splay tree | |
| Logan Ingalls <log@plutor.org> | |
| Sept 3, 2012 | |
| Note that I only implemented insert and find. Delete is trivial, | |
| and isn't the interesting part of splay trees. Some of these | |
| algorithms would be simpler, but I chose to do this without parent | |
| links from the tree nodes. | |
| Example output on my desktop computer: |
| [program:hippe] | |
| directory = /home/vijay/Desktop/hippo/hippolite/ | |
| user = vijay | |
| command = /home/vijay/Desktop/hippo/hippolite/deploy.sh |