code:
#!/usr/bin/env python

# inspired by your code
memo = {'even': {0: 1, 2: 2}, 'odd': {1: 1}}

def f(k):
    if k in memo['even']:
        return memo['even'][k]
    if k in memo['odd']:
        return memo['odd'][k]
    else:
        if k % 2 == 0:
            memo['even'][k] = (k / 2) + f((k / 2) + 1) + f(k / 2)
        else:
            memo['odd'][k] = 1 + f(((k - 1) / 2) - 1) + f((k - 1) / 2)
    return f(k)

# binary search on even values
def seek_even(x):
    left = 0
    right = f(x)
    if right % 2 != 0:
        right += 1
    middle = (right - left) / 2
    if middle % 2 != 0:
        middle += 1
    while f(middle) != x and left < middle < right:
        if f(middle) < x:
            left = middle
        else:
            right = middle
        middle = (left + right) / 2
        if middle % 2 != 0:
            middle += 1
    return middle

# binary search on odd values
def seek_odd(x):
    left = 1
    right = x
    if right % 2 == 0:
        right += 1
    middle = (right - left) / 2
    if middle % 2 == 0:
        middle += 1
    while f(middle) != x and left < middle < right:

        if f(middle) < x:
            left = middle
        else:
            right = middle
        middle = (left + right) / 2
        if middle % 2 == 0:
            middle += 1
    return middle

def seek(x):
    indices = [seek_even(x), seek_odd(x)]
    print "closest indices: " % indices
    return max(filter(lambda c: f(c) == x, indices) + [-1])

tests = [264227, 22, 10, 100000000000000000000, 31]
for i in tests:
    print "seek range [0, %d] for %d" % (f(i), i), seek(i)

rawDescription: 2 Recurrence equations - Finding index

description: 2 Recurrence equations - Finding index


code:
import socket

baidu = ('www.baidu.com', 80)
zero = ('0.0.0.0', 9001)

def test(addr, src_addr):
    try:
        sock = socket.create_connection(addr, 3, src_addr)
        print sock.getsockname()
        print
        sock.sendall('GET / HTTP/1.1\r\n\r\n')
        data = sock.recv(1024)
        print data
    finally:
        try:
            sock.close()
        except:
            pass

test(baidu, zero)
#test(baidu, None)

rawDescription: When source_address of python socket.create_connection is used?


code:
#!/usr/bin/env python

from evdev import InputDevice, ecodes, list_devices, categorize
import signal, sys

scancodes = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
barCodeDeviceString = "Metrologic Metrologic Scanner"

devices = map(InputDevice, list_devices())
for device in devices:
    if device.name == barCodeDeviceString:
        dev = InputDevice(device.fn)

def signal_handler(signal, frame):
    print 'Stopping'
    dev.ungrab()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

dev.grab()

barcode = ""
for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        data = categorize(event)
        if data.keystate == 1 and data.scancode != 42: # Catch only keydown, and not Enter
            if data.scancode == 28:
                print barcode
                barcode = ""
            else:
                barcode += keys[data.scancode]

rawDescription: Python evdev and bar code scanner

description: Python evdev and bar code scanner


code:
before = ['JACKIE:34', 'MATT:444', 'CEN:12', 'PETE:12', 'RANDY:92', 'MITCH:2', 'JAN:2']
after = [(name, int(value)) for name, value in (x.split(':') for x in before)]
print after

rawDescription: Python conversion from list to tuple

description: Python conversion from list to tuple


code:
import Tkinter
import tkFileDialog
import os

root = Tkinter.Tk()
root.withdraw() #use to hide tkinter window

currdir = os.getcwd()
tempdir = tkFileDialog.askdirectory(parent=root, initialdir=currdir, title='Please select a directory')
if len(tempdir) > 0:
    print "You chose %s" % tempdir

rawDescription: Browse for file path in python

description: Browse for file path in python


rawDescription: Python, convert a number to a string 'as is'


code:
import re
s = 'JHDKFksdKSJDsGGGG'
print re.sub('[A-Z]+','_',s)

rawDescription: How to replace all contiguous uppercase letters with a single underscore in Python

description: Replace all contiguous uppercase letters with a single underscore in Python


code:
import numpy as np
import os
import sys

# WARNING: this will work on little-endian architectures (eg Intel x86) only!
if '__main__' == __name__:
    if len(sys.argv) > 1:
        with open(sys.argv[1], 'rb') as f:
            magic = np.fromfile(f, np.float32, count=1)
            if 202021.25 != magic:
                print 'Magic number incorrect. Invalid .flo file'
            else:
                w = np.fromfile(f, np.int32, count=1)
                h = np.fromfile(f, np.int32, count=1)
                print 'Reading %d x %d flo file' % (w, h)
                data = np.fromfile(f, np.float32, count=2*w*h)
                # Reshape data into 3D array (columns, rows, bands)
                data2D = np.resize(data, (w, h, 2))
    else:
        print 'Specify a .flo file on the command line.'

rawDescription: reading middlebury 'flow' files with python (bytes array & numpy)

description: Reading middlebury 'flow' files with python (bytes array & numpy)


code:
import numpy as np

x = np.arange(10)
mask = x > 5

print x[mask]
print x[~mask]

rawDescription: Inverted fancy indexing

description: Inverted fancy indexing


code:
# drag: store off the data in a safe place, and serialize a cooky
# that the drop target can use to retrieve the data.
self.__tmp_storage_dct = { self.__tmp_storage_cooky: stuff }
m.setData(self.rowlistptr_mime_type, QByteArray(pickle.dumps(self.__tmp_storage_cooky)))
self.__tmp_storage_cooky += 1

# drop:
if mime.hasFormat(self.rowlistptr_mime_type):
  print "got tmpstorage"
  cooky = pickle.loads(mime.data(self.rowlistptr_mime_type).data())
  nodes = self.__tmp_storage_dct.pop(cooky)

rawDescription: Python object in QMimeData

description: Python object in QMimeData


code:
im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))

print r, g, b
(65, 100, 137)

rawDescription: Get pixel's RGB using PIL

description: Get pixel's RGB using PIL


code:
import os
import Image
import pyexiv2

fp = '/home/klaus/workspace'
fn = 'img_2380.jpg'

full_path = os.path.join(fp, fn)
print full_path

im = Image.open(full_path)
size = 512, 512
im.thumbnail(size, Image.ANTIALIAS)
im.save('bla.jpg', 'JPEG')

oldmeta = pyexiv2.ImageMetadata(full_path)
oldmeta.read()
# read metadata of the original file

newmeta = pyexiv2.ImageMetadata('bla.jpg')
newmeta.read()
# read metadata of the new file
# yes, there aren't any, but this is crucial!
# you need this class as the target for copying!

oldmeta.copy(newmeta)

newmeta.write()
# don't forget to write the data to the new file

rawDescription: how maintain exif data of images resizes using PIL

description: How maintain exif data of images resizes using PIL


code:
def __setattr__(self, name, value):
    if name == "x":
        super(Test, self).__setattr__(name, value)
    else:
        print "setting attr %s" % name

rawDescription: Class properties and __setattr__

description: Class properties and __setattr__


code:
Testfile = "test.txt"
with open(TestFile) as testSet:
    for line in testSet:
        line = line.strip()
        if "#*#*# ham" in line or "#*#*# spam" in line:
            print line

rawDescription: String Equivalence/Comparison in Python

description: String Equivalence/Comparison in Python


code:
d={}
for item in list_of_names:
    d['x_'+i]=[1,2,3]
    print "done"

rawDescription: create new variable name for each loop in a for loop

description: Create new variable name for each loop in a for loop


code:
if my_answer != your_question:
    print "I did not understand your question. Please rephrase is."
else:
    print "Good luck. Python is fun."

rawDescription: Floating Point in Python

description: Floating Point in Python


code:
def rm_suffix(s,suffixes):
    for suf in suffixes:
       if s.endswith(suf):
          return s[:-len(suf)]
    return s

alist = ['bar','barbar','foo','foos','barbares','foofoos','bares']
salist = set(alist)
suffixes = ('es','s')
blist = [x for x in alist
         if (not x.endswith(suffixes)) or (rm_suffix(x,suffixes) not in salist)]
print blist  # ['bar', 'barbar', 'foo', 'foofoos']

rawDescription: How to detect string suffixes and remove these suffixed elements from list? - Python

description: Detect string suffixes and remove these suffixed elements from list? - Python


code:
maximum = max(scores.values())
for key in scores:
    if scores[key] == maximum:
        print key, scores[key]

rawDescription: Python print several (same) values

description: Python print several (same) values


code:
for g in ParameterGrid(grid):
    rf.set_params(**g)
    rf.fit(X,y)
    if rf.oob_score_ < best_score:
        best_score = rf.oob_score_
        best_grid = g

print "OOB: %0.5f" % best_score
print "Grid:", best_grid

rawDescription: Is there easy way to grid search without cross validation in python?


code:
from threading import Timer

def gameover():
    print "Game Over"

t = Timer(5.0, gameover)
t.start()

rawDescription: In python I am trying to set an alarm for an action to happen


code:
list1 = [101, 110, 136, 380]
list2 = [101.04, 110.009, 264.5, 379.9, 466.4, 629.6, 724.4, 799.8, 914.3]
#guarantee that lists are sorted
list1.sort()
list2.sort()
#Set margin differently as needed
margin = 100
idx = 0;
for i in list1:
    while i > list2[idx] and not abs(i - list2[idx]) <= margin:
        idx+=1
    tempIdx = idx
    #Print out all the elements in list2 that are within the margin for list1
    while abs(i - list2[tempIdx]) <= margin:
        print list2[tempIdx]
        tempIdx+=1

rawDescription: Compare 2 lists to find an equal or approximate match without doing N^2 iteration


code:
function_table = {}

def add_to_table(name):
    def dec(func):
        def inner_func(*args, **kwargs):
            return func(*args, **kwargs)
        function_table[name] = inner_func
        return inner_func
    return dec

@add_to_table("my_addition_function")
def myfunction(a, b):
    return a + b

@add_to_table("my_subtraction_function")
def myfunction2(a, b):
    return a - b

print myfunction(1, 3)
# 4
print function_table["my_addition_function"](1, 4)
# 5
print function_table["my_subtraction_function"](1, 4)
# -3

rawDescription: How to call specific function based on parameters to decorator

description: Call specific function based on parameters to decorator


code:
myList = []
mySet = set()
co1 = (12,20) # and so on..
valuesToCheck = [co1,co2,co3,co4,co5,co6,co7,co8,co9]

# In each step:
# Adding 2 coordinates
myList.append((x1,y1))
myList.append((x2,y2))
mySet.add((x1, y1))
mySet.add((x2, y2))
# Searching 9 specific coordinates among all
for coordinate in valuesToCheck:
    if coordinate in mySet:
        print "Hit!"
        break
# Note that the valuesToCheck will change in each step.

del valuesToCheck[0]
valuesToCheck.append(co10)

rawDescription: Efficient way to store and search coordinates in python


code:
from ftplib import FTP

hosts = [('10.1.0.1', 'abc', '123'), ('10.1.0.2', 'abc', '123')]
local_file = r'/my_folder/my_file.txt'
remote_file = 'my_file.txt'

for host, name, password in hosts:
    f = FTP(host, name, password)
    f.cwd('my_remote_folder')

    with open(local_file, 'rb') as f_local:
        f.storbinary('STOR {}'.format(remote_file), f_local)

    print "{} - done".format(host)
    f.quit()

rawDescription: Python FTP code sample

description: Python FTP code sample


code:
import apt
cache = apt.Cache()

for mypkg in apt.Cache():
    if cache[mypkg.name].is_installed:
        print mypkg.name

rawDescription: Python show all installed packages

description: Python show all installed packages


code:
import urllib2, BeautifulSoup

query = 'http://myexample.file.com/file.xml'
f = urllib2.urlopen(query)
content = f.read()
print len(content)
soup = BeautifulSoup.BeautifulStoneSoup(content)

rawDescription: determing file object size before using file object

description: Determing file object size before using file object


code:
mylist= [['A', 'B'],
 ['A', 'C'],
 ['A', 'D'],
 ['A', 'E'],
 ['A', 'F'],
 ['B', 'C'],
 ['B', 'D'],
 ['B', 'E'],
 ['B', 'F'],
 ['C', 'D'],
 ['C', 'E'],
 ['C', 'F'],
 ['D', 'E'],
 ['D', 'F'],
 ['E', 'F']]

a=mylist[:] #this used to assign all elements to a so u have ur mylist safe
b=[]
b.append(a[0]) #this appends the first list in the list
del a[0]  #now deleting appended list
while len(a)>0:
    for val,i in enumerate(a):# enumerte gives index and value of list
        if len(set(b[len(b)-1]).intersection(set(i)))==0: # this checks intersection so that both list should not have same elements
            b.append(a[val])
            del a[val]
print b

#output [['A', 'B'], ['C', 'D'], ['E', 'F'], ['A', 'C'], ['B', 'D'], ['C', 'E'], ['D', 'F'], ['A', 'E'], ['B', 'C'], ['D', 'E'], ['A', 'F'], ['B', 'E'], ['C', 'F'], ['A', 'D'], ['B', 'F']]

rawDescription: Sorting a list in Python without repeat previous item


code:
import numpy as np
import cv2
import time

cap = cv2.VideoCapture(0)
#print cap.get(5)

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')

num_frames = 0

start = time.time()
while(cap.isOpened()):

    ret, frame = cap.read()
    if num_frames < 500:
        frame = cv2.flip(frame,1)
        num_frames = num_frames + 1;
    else:
        break

total_time = (time.time() - start)
fps = (num_frames / total_time)
print str(num_frames) + ' frames in ' + str(total_time) + ' seconds = ' + str(fps) + ' fps'

# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()

rawDescription: OpenCV Python Video capture - can someone check my code


code:
from sqlalchemy import Column, String, Integer, create_engine
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Model(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    data = Column(String)

e = create_engine("sqlite:///somefile.db")

Base.metadata.create_all(e)

session = Session(e)

for index in xrange(10000000):
    session.add(Model(data="data %d" % index))

    if index % 1000 == 0:
        print "flushing... %d" % index
        session.flush()
        session.commit()

rawDescription: SQLAlchemy eating RAM

description: SQLAlchemy eating RAM


code:
import pytz, datetime, time
import os

originalTimeStamp = os.stat("/tmp/file-from-us-west-coast").st_mtime

# prints e.g. 2010-03-31 13:01:18
print "original:",datetime.datetime.fromtimestamp(originalTimeStamp)

# re-interpret
originalTimeZone = "America/Los_Angeles"
targetTimeZone   = "Europe/Paris"

newTimeStamp = pytz.timezone(originalTimeZone).localize(datetime.datetime.fromtimestamp(originalTimeStamp)).astimezone(pytz.timezone(targetTimeZone))

# prints e.g. 2010-03-31 22:01:18+02:00
print "new:     ",newTimeStamp

# convert back to seconds since epoch
newTimeStamp = time.mktime(newTimeStamp.timetuple())

# print time difference in hours
print (newTimeStamp - originalTimeStamp) / 3600.0

rawDescription: Changing a unix timestamp to a different timezone


code:
import gio
mime_type = gio.content_type_guess('foo.txt')
app_infos = gio.gio.app_info_get_all_for_type(mime_type)
for app_info in app_infos:
    print app_info.get_name(), app_info.get_executable(), app_info.get_icon()
# pure bliss

rawDescription: Find the default application name for a given file


code:
from collections import Counter, defaultdict

def findpattern():
    frequencies = defaultdict(Counter)

    for line in syslog:
        if re.search(r"regexforhostname",line):
            hostname= line.strip()[16:27]
            frequencies[hostname].update(f for f in word if f in line)

    return frequencies

result = findpattern()
for device, frequencies in result.iteritems():
    print '{}: {}'.format(
        device,
        ', '.join(['{}x {}'.format(c, f) for f, c in frequencies.most_common()]))

rawDescription: count in defaultdict(list) python

description: Count in defaultdict(list) python


code:
uri = "http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"

while True:
    dec = urllib2.unquote(uri)
    if dec == uri:
        break
    uri = dec

uri = uri.decode('utf8')
print '%r' % uri
# u'http://www.yelp.com/biz/carriage-house-caf\xe9-houston-2'

rawDescription: python: recursive decoding of uri component in python like javascript

description: Python: recursive decoding of uri component in python like javascript


code:
import re
arr = re.findall("-(.*?)<(.*?)>", "-user <username> -id <userid> -sub <subname>")

for x in arr:
   print "\"" + x[0].strip() + "\" : ", x[1].strip()

rawDescription: what is the best way to extract from a line in Python

description: What is the best way to extract from a line in Python


code:
import requests

headers = {"Content-type": "application/json"}
data = {"externalId": "801411",
        "name": "RD Core",
        "description": "Tenant create",
        "subscriptionType": "MINIMAL",
        "features": {"capture": False,
                     "correspondence": True,
                     "vault": False}
        }

response = requests.post(url='http://localhost:9100/tenants/',
                         headers=headers,
                         json=data)

print response.status_code, response.reason

rawDescription: Python Post call throwing 400 Bad Request


code:
counter = 2

while counter <= 1024:
    print counter
    counter *= 2

rawDescription: For loop iterate over powers of 2

description: For loop iterate over powers of 2


code: print n[-4::]

rawDescription: Extracting indices from numpy array python


code:
def count(data):
    """ (list of list of str) -> list of int

    Return the numbers of occurrences of strings that end in digits 0, 1, or 2
    for each string.

    >>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
    >>> count(data)
    [1, 1, 2]
    """

    num_list = []
    num = 0

    for sublist in data:
        for item in sublist:
            if item[-1] in "012":
                num += 1

        # item in sublist body is over
        # num is now total occurrences of 0,1,2 in sublist
        num_list.append(num)
        num = 0
    return num_list

print count([['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']])

rawDescription: Counting digits in a list of list of strings and returning them as a list of integers


code:
import os
import time
import errno
import sys

io = os.open(expanduser("~/named_pipes/cob_input"), os.O_RDONLY | os.O_NONBLOCK)

# For implementing non-blocking IO
def read_pipe_non_blocking(input_pipe, size):
    try:
        in_buffer = os.read(input_pipe, size)
    except OSError as err:
        if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
            in_buffer = None
        else:
            raise  # something else has happened -- better reraise
    return in_buffer

def get_azimuth(input_pipe):
    in_buffer = read_pipe_non_blocking(input_pipe, 1)
    print(in_buffer)
    if(in_buffer is None):
        sys.stderr.write("n")
        return ""
    else:
        tmp_buffer = None
        while(tmp_buffer != "a"):
            sys.stderr.write("m")
            time.sleep(0.1)
            tmp_buffer = read_pipe_non_blocking(input_pipe, 1)
            if(tmp_buffer != None and tmp_buffer != "a"):
                in_buffer += tmp_buffer
        read_pipe_non_blocking(input_pipe, 1) #Read in the newline character and the toss it
        sys.stderr.write("\nReturning \{%s\}" %in_buffer)
        return in_buffer

i = 0
while 1:
    print i
    time.sleep(1)
    i += 1
    get_azimuth(io)

rawDescription: Read (os.read) FIFO non-blockingly without fixed buffer size


code:
print "Files: "
folders = set()
for files in bucket.list():
  if '/' not in files.name:
    print "\t", files.name
  else:
    folders.add(files.name[:files.name.rfind('/')])

print "Folders: "
for folder in folders:
  print "\t", folder

rawDescription: Classify between files and fodler in S3 bucket -Python


rawDescription: trying to prepend zeroes in python but getting strange results

description: Trying to prepend zeroes in python but getting strange results


code:
import csv
import sys
from collections import defaultdict

HEADERS = ["x", "y", "field1", "field2", "field3"]

def read_data(source):
    data = defaultdict(list)
    reader = csv.DictReader(source, fieldnames=HEADERS)
    next(reader) # skip headers
    for row in reader:
        # this will at once build the key tuple
        # and remove the "x" and "y" keys from the
        # row dict
        key = row.pop("x"), row.pop("y")
        data[key].append(row)
    return data

with open('data.csv') as source:
    data = read_data(source)

print data

rawDescription: Creating a dictionary of dictionaries (or a tuple?) from CSV in Python


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

# uncomment if using Firefox web browser
driver = webdriver.Firefox()

# uncomment if using Phantomjs
#driver = webdriver.PhantomJS()

url = 'http://www.heritage.org/research/all-research.aspx?nomobile&categories=report'
driver.get(url)

# set initial page count
pages = 1
with open('heritageURLs.txt', 'w') as f:
    while True:
        try:
            # sleep here to allow time for page load
            sleep(5)
            # grab the Next button if it exists
            btn_next = driver.find_element_by_class_name('next')
            # find all item-title a href and write to file
            links = driver.find_elements_by_class_name('item-title')
            print "Page: {} -- {} urls to write...".format(pages, len(links))
            for link in links:
                f.write(link.get_attribute('href')+'\n')
            # Exit if no more Next button is found, ie. last page
            if btn_next is None:
                print "crawling completed."
                exit(-1)
            # otherwise click the Next button and repeat crawling the urls
            pages += 1
            btn_next.send_keys(Keys.RETURN)
        # you should specify the exception here
        except:
            print "Error found, crawling stopped"
            exit(-1)

rawDescription: Scrape with BeautifulSoup from site that uses AJAX pagination using Python


code:
print "A. Create Username"
print "B. Edit Username"
input = raw_input("Enter your choice")

if input == "A":
    print "A was given"
if input == "B":
    print "B was given"

rawDescription: How to create a menu or directory?

description: Create a menu or directory


code:
import Queue

# Initialise queue as:
queue = Queue.Queue()
Finished = object()   # Unique marker the producer will put in the queue when finished

# Consumer:
try:
    while True:
        next_item = self.queue.get(timeout=15)
        if next_item is Finished: break
        yield next_item

except Queue.Empty:
    print "Timeout exceeded"

rawDescription: Python - Threading and a While True Loop


code:
import random

items = yourdict.items()
random.shuffle(items)

for key, value in items:
    print key
    print '\n'.join(value)

rawDescription: Randomize dictionary sorting

description: Randomize dictionary sorting


code:
from twisted import protocols
from twisted.protocols import basic
from twisted.internet import threads, protocol, reactor
from twisted.application import service, internet
import re
import time

def slowFunction(arg):
    print "starting process"
    time.sleep(20)
    print "processed "+arg

class MySimpleServer(basic.LineReceiver):

    PROCESS_COMMAND = "process (.*)" #re pattern
    clients = []

    def connectionMade(self):
        print "Client connected"
        MySimpleServer.clients.append(self)

    def connectionLost(self, reason):
        print "Client gone"
        MySimpleServer.clients.remove(self)

    def onProcessDone(self, result):
        self.message("Process done")

    def onError(self, result):
        self.message("Process fail with error")

    def processFunction(self, processArgument):
        slowFunction(processArgument)

    def lineReceived(self, line):
        processArgumentResult = re.search(MySimpleServer.PROCESS_COMMAND, line)
        if not processArgumentResult == None:
            processArgument = processArgumentResult.groups()[0]
            deferred = threads.deferToThread(self.processFunction, processArgument)
            deferred.addCallback(self.onProcessDone)
            deferred.addErrback(self.onError)
            self.message("processing your request")
        else:
            print "Unknown message line: "+line

    def message(self, message):
        self.transport.write(message + '\n')

if __name__ == '__main__':
    factory = protocol.ServerFactory()
    factory.protocol = MySimpleServer
    factory.client = []

    reactor.listenTCP(8000, factory)
    reactor.run()

rawDescription: Python Twisted deferToThread function is not working as expected


code:
import random
for i in range(10):
  print random.random() * 9 + 21

rawDescription: How to use random.random() in python

description: Use random.random() in python


code:
import win32clipboard

# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()

# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print data

rawDescription: How do I read text from the (windows) clipboard from python?

description: Read text from the (windows) clipboard from python


code:
if lowercaseusername not in lines_seen:
    lines_seen.add(lowercaseusername) # <-- facepalm here
    print line.strip()

rawDescription: Only print lines that are unique in the second column


code:
import time

def testFor(w, fn):
    n = 0
    start = time.time()
    while (time.time() - start) < w:
        fn()
        n = n + 1
    return n

window = 10

while True:
    n = testFor(window, test)
    print "n: %s" % n

rawDescription: Repeat forever but report each x seconds


code:
import urllib
f = urllib.urlopen("http://tycho.usno.navy.mil/cgi-bin/timer.pl")
time_page = f.readlines()
for line in time_page:
    if line.find("Eastern Time") != -1:
        ny_time = line[4:24]
        break
print ny_time

rawDescription: Python: What date/time is it in NYC? (without pytz)


code:
y = 1 + 1
list1 = []
list1.append(y)
print list1
print l

rawDescription: How to Append The Calculated Arithmetic to The List?


code:
for i in dir(X):
   print i,"  ",type(getattr(X,i))

rawDescription: How to get all objects in a module in python?

description: Get all objects in a module in python


code:
import numpy, bisect
a = numpy.array([1, 2, 5, 10, 25, 36, 66, 90, 121, 230, 333, 500])
idx = bisect.bisect(a, 60)
print a[:idx]

rawDescription: Python/numpy: all values in array up to x?


code:
from tempfile import mktemp

my_code = '''
a = 10
print a
'''

tmp_filename = mktemp('.py', 'temp_file_')
with open(tmp_filename, 'w') as f:
    f.write(my_code)
obj = compile(my_code, tmp_filename, 'exec')
exec obj #Place breakpoint here: when stepping in it should get to the code.

rawDescription: Pydevd with virtual code or (source provider)


code:
class KCounter(object):

    def __init__(self):
        self.k = 0

    def print_k(self):
        while True:
            if self.k % 2 == 1:
                print(self.k, "is even.")
            time.sleep(2)

    def main(self):
        self.k = 1
        while self.k != 200:
            self.k += 1
            print self.k
            time.sleep(0.5)

if __name__ == '__main__':
    kcounter = KCounter()
    Thread(target=kcounter.print_k).start()
    Thread(target=kcounter.main).start()

rawDescription: Accessing a function's variable from another function


code:
import datetime
found_datetimes = []
DATE_FORMAT = "%Y-%m-%d"

for item in lista:
    if isinstance(item, datetime.datetime):
        found_datetimes.append(datetime.datetime.strftime(item, DATE_FORMAT))

print found_datetimes

rawDescription: Check a datetime in pythonlist and convert to string


code:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image1.jpg')

#get rid of very bright and very dark regions
delta=30
lower_gray = np.array([delta, delta,delta])
upper_gray = np.array([255-delta,255-delta,255-delta])
# Threshold the image to get only selected
mask = cv2.inRange(img, lower_gray, upper_gray)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)

#Convert to HSV space
HSV_img = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
hue = HSV_img[:, :, 0]

#select maximum value of H component from histogram
hist = cv2.calcHist([hue],[0],None,[256],[0,256])
hist= hist[1:, :] #suppress black value
elem = np.argmax(hist)
print np.max(hist), np.argmax(hist)

tolerance=10
lower_gray = np.array([elem-tolerance, 0,0])
upper_gray = np.array([elem+tolerance,255,255])
# Threshold the image to get only selected
mask = cv2.inRange(HSV_img, lower_gray, upper_gray)
# Bitwise-AND mask and original image
res2 = cv2.bitwise_and(img,img, mask= mask)

titles = ['Original Image', 'Selected Gray Values', 'Hue', 'Result']
images = [img, res, hue, res2]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

rawDescription: Python + OpenCV color segmentation using Kmeans


code:
import xlrd.open_workbook

workbook = xlrd.open_workbook('tmp.xls', formatting_info=True)
sheet = wb.sheet_by_name("1")
cell = sheet.cell(6, 0)
format = wb.xf_list[cell.xf_index]
print "type(format) is", type(format)
print
print "format.dump():"
format.dump()

rawDescription: Reading strikethroughs in Excel with Pandas


code:
import numpy as np
from matplotlib import pyplot as plt
import time
import matplotlib.widgets as widgets
import threading #this is new

fig, ax = plt.subplots()
# it is not always column 0 and 1
sctPlot = ax.scatter([-0.3,0,0.3], [0.3,0.3,0.3],  c="blue", picker = 2, s=[50]*3)
fig.subplots_adjust(bottom=0.3, left=0.1)
plt.grid(False)
plt.axis([-0.6, 0.6, -0.6, 0.6])

loopBool = True

def closeLooping(event):
    global loopBool
    loopBool = False

def looping(event):
    global loopBool
    while (loopBool == True):
        print "Still Looping!"
        time.sleep(1)
    print "Stop!!"

def looping_pre(event): #this is new
    thread=threading.Thread(target=looping,args=(event,))
    #thread.daemon=True   #might or might not be needed
    thread.start()

axCloseButton = plt.axes([0.1, 0.15, 0.2, 0.06])
bClose = widgets.Button(axCloseButton, "Close", hovercolor = '0.975')
bClose.on_clicked(closeLooping)
plt.show() #this is new

fig.canvas.mpl_connect('pick_event', looping_pre) #this is changed

rawDescription: Matplotlib, button to close a loop, python


code:
addresses = {}
addresses['john'] = 'somewhere' # Set john's address
print "John's address is", addresses['john'] # Retrieve it

rawDescription: Can anyone summarize the noticeable difference between list, tuple, dictionary in Python?


code:
import netaddr

ip = netaddr.IPAddress('192.168.1.168').value
network = netaddr.IPNetwork('192.168.1.0/24')

if ip >= network.first and ip <= network.last:
    print True
else:
    print False

rawDescription: How to find out if an IP address belong to a network with python2?

description: Find out if an IP address belong to a network with python2


code:
from itertools import permutations

original = str(input('What word would you like to unscramble?: '))

perms = []
for bob in permutations(original):
    perms.append(''.join(bob))

inputFile = open(dic.txt, 'r')
dict_entries = inputFile.read().split('\n')
inputFile.close()

for word in [perm for perm in perms if perm in dict_entries]:
    print word

rawDescription: How do I see if a value matches another value in a text file in Python?

description: See if a value matches another value in a text file in Python


code:
def conservative_smooth(array2D, kernel_size = 3):
    stepsize = 1
    if(kernel_size % 2 != 0 and kernel_size >= 3):
        window = np.ones([kernel_size,kernel_size])
    elif(kernel_size % 2 == 0 or kernel_size < 3):
        print "kernel is even - it needs to be odd and at least of a value of 3"
        os._exit(1)
    nxwind, nywind = array2D.shape
    for i in range(0, nxwind, stepsize):
        for j in range(0, nywind, stepsize):
            # CALCULATE MAX AND MIN RANGES OF ROWS AND COLS THAT CAN BE ACCESSED
            # BY THE WINDOW
            imin=max(0,i-((kernel_size-1)/2))
            imax=min(nxwind-1,i+((kernel_size-1)/2))+1
            jmin=max(0,j-((kernel_size-1)/2))
            jmax=min(nywind-1,j+((kernel_size-1)/2))+1
            centre_value = array2D[i,j]
            array2D[i,j] = np.nan
            max_value = np.nanmax(array2D[imin:imax,jmin:jmax])
            min_value = np.nanmin(array2D[imin:imax,jmin:jmax])
            if(centre_value > max_value):
                centre_value = max_value
            elif(centre_value < min_value):
                centre_value = min_value
            else:
                centre_value = centre_value
            ## Append new centre value to output array
            array2D[i,j] = centre_value
    return array2D

rawDescription: Python: Access index of moving window array that matches current position of array being looped over


code:
print sorted(student_tuples, key=lambda t: (-t[2], t[0]))
# [('john', 'A', 15), ('dave', 'C', 12), ('peter', 'B', 12)]

rawDescription: How to sort in decreasing value first then increasing in second value

description: Sort in decreasing value first then increasing in second value


code:
import requests

session = requests.Session()

#Get session cookies (session ID)
index_url = 'http://propaccess.traviscad.org/clientdb/?cid=1'
index_request = session.get(index_url)
session_cookies = index_request.cookies

#Download pages
for prop_id in range(400000, 410001):
    url = 'http://propaccess.traviscad.org/clientdb/Property.aspx?prop_id=%s' % prop_id
    r = session.get(url,cookies = session_cookies)
    print r.text #r.text contains page content

rawDescription: how to save webpages to files with their addresses that has .aspx in it

description: Save webpages to files with their addresses that has .aspx in it


code:
x = 10
now = time.localtime()
print [time.localtime(time.mktime((now.tm_year, now.tm_mon - n, 1, 0, 0, 0, 0, 0, 0)))[:2] for n in range(x)]

rawDescription: Get (year,month) for the last X months

description: Get (year,month) for the last X months


code: print [item for item in dir(adfix) if not item.startswith("__")]

rawDescription: How to get a list of variables in specific Python module?

description: Get a list of variables in specific Python module


code:
p = """I am working on a project where me and my 'instructor'
       have the same book, same volume, and same version. Given
       a page already (aka, we're both using the first main page),
       find the phrase given a set of 'plot points', i.e. 8:1, 13:0, etc.
    """

lines = p.split('\n') # this gives you a list of lines, so you can access the nth item in a list
line = lines[3]       # this is the 4th line
words = line.split()  # this splits a line into a list of words
word = words[0]       # this is the first word in line 3 (fourth line)

print word
#find

rawDescription: Assigning Numbers To Words In A Paragraph In Python


code:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.graphics import Color
from kivy.clock import Clock
import random

class RootWidget(GridLayout):
    pass

class MainApp(App):

    def build(self):
        parent = GridLayout(cols=6)
        Colour=[0,0,0,0]
        self.create_button(parent,Colour,1,1)
        Clock.schedule_interval(lambda a:self.update(parent), 1)
        return parent

    def update(self,obj):
        print "I am update function"
        obj.clear_widgets()
        print "random value is ",random.random()
        for i in (1,2,3,4,5,6):
            for j in (1,2,3,4,5,6):
                c=[random.random(),random.random(),random.random(),random.random()]
                d=[i,j]
                self.create_button(obj,c,i,j)

    def create_button(self,obj,color,i,j):

        a=Button(background_color=color,text='Hello World %s%s'%(i,j))
        obj.add_widget(a)

if __name__ == '__main__':
    MainApp().run()

rawDescription: I want to dynamically change the color of widget in grid Layout in kivy


code:
import sys
import urllib2
import networkx as nx
data = urllib2.urlopen('http://gexf.net/data/viz.gexf')
G = nx.read_gexf(data)
print G.node['a']
G.node['a']['viz']['position']['x']=10
G.node['a']['viz']['position']['y']=20
print G.node['a']
nx.write_gexf(G,sys.stdout)

rawDescription: Node location when using networkx and gexf format


code:
from nltk.corpus import wordnet as wn

print wn.synsets('answered')
# [Synset('answer.v.01'), Synset('answer.v.02'), Synset('answer.v.03'), Synset('answer.v.04'), Synset('answer.v.05'), Synset('answer.v.06'), Synset('suffice.v.01'), Synset('answer.v.08'), Synset('answer.v.09'), Synset('answer.v.10')]

print wn.synsets('unanswered')
# [Synset('unanswered.s.01')]

print wn.synsets('notaword')
# []

rawDescription: Missing words in NLTK vocabulary - Python


code:
import random
step = 1
count = 0
while count < len(record):
    print count
    step = random.randint(1, 5)
    count += step

rawDescription: Python generate random steps for range function in each iteration of a for loop


code:
class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

def hotPotato(namelist, num):
    simqueue = Queue()
    for name in namelist:
        simqueue.enqueue(name)

    while simqueue.size() > 1:
        for i in range(num):
            simqueue.enqueue(simqueue.dequeue())

        simqueue.dequeue()

    return simqueue.dequeue()
print hotPotato([0,1,2,3,4],2)

rawDescription: Flavius Josephus/ Hot Pocket simulation

description: Flavius Josephus/ Hot Pocket simulation


code:
from myApp.myPackage import myModule
import myApp.myPackage.myModule as sameModule
import myPackage.myModule
print myModule is sameModule
print myModule is myPackage.myModule

rawDescription: Python: Making a class variable static even when a module is imported in different ways


code:
class Base(object):
    pass
class Derived(Base):
    def print_base(self):
        for base in self.__class__.__bases__:
            print base.__name__
foo = Derived()
foo.print_base()

rawDescription: Get parent class name in Python

description: Get parent class name in Python


code: print reduce(lambda i, j: int(i) ^ int(j), bit)

rawDescription: xor of element of a list/tuple

description: Xor of element of a list/tuple


code:
def whatYouWantToMeasure():
    pass

if __name__=='__main__':
    from timeit import Timer
    t = Timer("whatYouWantToMeasure()", "from __main__ import test")
    print t.timeit(number=1)

rawDescription: Is it possible to use Python to measure response time?


code:
letter = 'X' if (strikeratecalc2 >= 0.20 and strikeratecalc2 < 0.30) else 'Y' if \
 (strikeratecalc2 >=  0.30 and strikeratecalc2 <  0.40) \
 else 'Z' if (strikeratecalc2 >= 0.40 and strikeratecalc2 < \
 0.50 and user_numberofselections > 100) else 'ZA' if \
 (strikeratecalc2 >= 0.50 and user_numberofselections  > 100) \
 else 'Default value here'

print letter, 'Strike rate = ' ,strikeratecalc2, '%'

rawDescription: using numerical filters to define what print list is used (I assume if filters)

description: Using numerical filters to define what print list is used (I assume if filters)


code:
key_word = ['width', 'height', 'left', 'right']
regex_template = r'{}\s*:\s*\d+'
for key in key_word:
    print re.search(regex_template.format(key), line)

rawDescription: python: create raw string of variables

description: Python: create raw string of variables


rawDescription: loop for tuple of list on python

description: Loop for tuple of list on python


code:
c = cmds.fileDialog2(fm=0, ff="Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)", dialogStyle=2)
if c:
    print c
else:
    print "user cancelled"

rawDescription: Maya Pymel: pass fileDialog2's return to a UI textfield


code:
from datetime import timedelta
from tornado import gen, ioloop, web

i = 0

@gen.coroutine
def run():
    global i
    loop = ioloop.IOLoop.current()
    while True:
        print "This is run function print"
        yield gen.Task(loop.add_timeout, timedelta(seconds=1))
        i += 1

def get_dict():
    return {'a': i}

class DictHandler(web.RequestHandler):
    def get(self):
        # Tornado converts dicts to JSON.
        self.finish(get_dict())

if __name__ == "__main__":
    application = web.Application([
        ('/dict', DictHandler),
    ])
    application.listen(8888)
    print 'Listening on http://localhost:8888'
    ioloop.IOLoop.current().run_sync(run)

rawDescription: Asynchronous request to a process with python


code:
from collections import Counter

def getRecipe(name, quantity=1):
  if not name in recipes: return Counter({name: quantity})

  subitems = recipes[name]["ingredients"]["input"]
  return sum((getRecipe(item, quantity) for item in subitems),
             Counter())

print repr(dict(getRecipe("solar_panel")))
# => {'copper': 39, 'refined_iron': 10, 'glass': 3,
#     'rubber': 78, 'cobblestone': 8, 'tin': 4,
#     'coal_dust': 3, 'nothing': 10, 'redstone': 6}

rawDescription: Python Recursive Data Reading

description: Python Recursive Data Reading


code:
print [y.replace('$', x)
  for y in (v for y in DICTB.values() for v in y)
  for x in (v for y in DICTA.values() for v in y)
]

rawDescription: How to extract every possible values of python Dict's values to list

description: Extract every possible values of python Dict's values to list


code:
import numpy as np
A, B, C = np.polyfit([1,2,3],[4,7,12],2)
print A, B, C
print A, 'x^2 +', B, 'x +', C

rawDescription: create quadratic equation using Python library

description: Create quadratic equation using Python library


code:
from boto.regioninfo import *
from boto.ec2.connection import EC2Connection

# AWS connect info
aws_access_key_id='########## AWS Access Key ############'
aws_secret_access_key='########### AWS Secret Key ############'
region_name='ap-southeast-1'
region_ec2_endpoint='ec2.ap-southeast-1.amazonaws.com'

# Connect EC2
aws_region = RegionInfo(name=region_name, endpoint=region_endpoint)
conn = EC2Connection(aws_access_key_id,aws_secret_access_key,region=aws_region)

reservations = conn.get_all_instances(instance_ids=[my_ids])
for each in reservations:
    for vol in each.instances[0].block_device_mapping.items():
        print str(each.instances[0].id) + " " +  vol[1].volume_id + " "+   vol[1].status

rawDescription: boto interpretation of the CLI command

description: Boto interpretation of the CLI command


rawDescription: How to write values from a list after certain whitespaces?

description: Write values from a list after certain whitespaces


code:
import re
your_string = '<python><regex><split>'
parts = re.findall(r'<.+?>', your_string)
print parts # ['<python>', '<regex>', '<split>']

rawDescription: How to split the string into segments in python

description: Split the string into segments in python


code:
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://example.com')
print browser.page_source
browser.quit()

rawDescription: Python Selenium 2 - Grabbing HTML Source with Minimal Impact


code:
base = '/Users/paganotti/Documents/Project/simple/work/paga/'
rel = '../test.txt'

print os.path.normpath(os.path.join(base, rel))

rawDescription: Python function translate relative path ../../ into full path


code:
try:
    commandString = [
        'python',
        os.path.join(SCRIPT_DIR, 'ffmpeg.py'),
        '-i', os.path.join('/srv/nfsshare/transcode50', userFolder, directory, title),
        '-d', os.path.join('/srv/nfsshare/transcode50', userFolder, directory),
        '-r', request.POST['framerate'],
        '-p 2', '-f', ",".join(formats), '-t', ",".join(rasters)
    ]

    # call transcode50 script to generate condor_execute.py
    subprocess.check_call(commandString,
        stdout=open('/srv/nfsshare/transcode50/output.txt', 'w'),
        stderr=subprocess.STDOUT)

except Exception, e:
    # you can do fancier logging, but this is quick
    open('/tmp/test_exception.txt', 'w').write(str(e))
    raise

rawDescription: Trying to Execute Python Script Using Subprocess (Django)


code:
#remember to set restrictive file permissions on this file. This is OS-dependent
USER_CODE_FILE="/home/user/user_code_file.py"

#absolute path to python binary (executable)
PYTHON_PATH="/usr/bin/python"

import subprocess
import json

user_code= '''
import json
my_data= {"a":[1,2,3]}
print json.dumps(my_data)
'''

with open(USER_CODE_FILE,"wb") as f:
    f.write(user_code)

user_result_str= subprocess.check_output([PYTHON_PATH, USER_CODE_FILE])
user_result= json.loads( user_result_str )
print user_result

rawDescription: Is it possible to restrict access to globals for a block of code in python?


code:
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import Comment
from BeautifulSoup import NavigableString

text = BeautifulSoup("""
a

b

c

a

b

c
""")

comments = text.findAll(text=lambda elm: isinstance(elm, Comment))
for comment in comments:
    next_sib = comment.nextSibling
    while not isinstance(next_sib, Comment) and \
        not isinstance(next_sib, NavigableString) and next_sib:
        # This prints each sibling while it isn't whitespace or another comment
        # Append next_sib to a list, dictionary, etc, etc and
        # do what you want with it
        print next_sib
        next_sib = next_sib.nextSibling

rawDescription: Python / BeautifulSoup: How to look directly beneath a code comment?


code:
for monthlyRate in (x/100.0 for x in range(4,9)):
    print monthlyRate

0.04
0.05
0.06
0.07
0.08

rawDescription: Python int to percent

description: Python int to percent


code:
hosts = {
    "rack": ["rack1", "rack2"],
    "external_rack": ["external_rack1", "external_rack2"]
}

env.roledefs = {"rack_machines": hosts["rack"]}

@task
def set_hosts(hostset="rack"):
    if hostset in hosts:
        env.roledefs["rack_machines"] = hosts[hostset]
    else:
        print "Invalid hostset"

@roles("rack_machines")
def business():
    pass

rawDescription: How to make Fabric use a different role depending on which network it currently is?

description: Make Fabric use a different role depending on which network it currently is


code:
import time

def get_profile(js):
    def cas(prev, el):
        if hasattr(prev, "get") and prev:
            return prev.get(el, prev)
        return prev
    def getget(default, *elements):
        return reduce(cas, elements[1:], js.get(elements[0], default))

    d = {}
    d['links'] = getget([], 'entry', 'gd$feedLink')
    d['statistics'] = getget({}, 'entry', 'yt$statistics')
    d['published'] = getget('', 'entry', 'published', '$t')
    d['updated'] = getget('', 'entry', 'updated', '$t')
    d['age'] = getget(0, 'entry', 'yt$age', '$t')
    d['name'] = getget('', 'entry', 'author', 0, 'name' '$t')
    return d

print get_profile({
    'entry':{
        'gd$feedLink':range(4),
        'yt$statistics':{'foo':1, 'bar':2},
        'published':{
            "$t":time.strftime("%x %X"),
        },
        'updated':{
            "$t":time.strftime("%x %X"),
        },
        'yt$age':{
            "$t":"infinity years",
        },
        'author':{0:{'name':{'$t':"I am a cow"}}},
    }
})

rawDescription: Python - is there an elegant way to avoid dozens try/except blocks while getting data out of a json object?


code:
import re
x = "Server:x.x.x.x # U:100 # P:100 # Pre:0810 # Tel:xxxxxxxxxx"
num = re.findall(r'Pre:(\d{4})', x)[0] # re.findall returns a list
print num

rawDescription: Python Copying part of string

description: Python Copying part of string


code:
print '{:>8s}'.format('ありがとう')
print u'{:>8s}'.format(u'ありがとう')
print u'{:\u3000>8s}'.format(u'ありがとう')

rawDescription: Aligning Japanese characters in python

description: Aligning Japanese characters in python


code:
import tempfile, msvcrt
import clr, msvcrt
from System.IO import FileStream, FileAccess
from System import IntPtr

with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as pyf:
    fileno=pyf.fileno()
    print "fileno", fileno
    handle = msvcrt.get_osfhandle(fileno)
    print "HANDLE", handle

    pyf.write("Python\n")
    pyf.flush()

    cs_handle = IntPtr.Overloads[long](handle)
    cs_fs = FileStream(cs_handle, FileAccess.Write)
    cs_fs.Write("CLR\n", 0, 4)
    cs_fs.Flush()

print "file should contain a line from Python and a line from CLR: ", pyf.name

rawDescription: How can I open a .NET FileStream object from a Python file handle?

description: Open a .NET FileStream object from a Python file handle


code:
import urllib2

class MyHttpHandler(urllib2.HTTPHandler):
    def http_response(self, request, response):
        for l in response:
            print l
        return response

u = urllib2.build_opener(MyHttpHandler())
for i in range(1, 5):
    u.open('http://stackoverflow.com')

rawDescription: Python Open Multiple URL Connections - urllib2


code:
import urllib, json

service_url = 'https://www.googleapis.com/freebase/v1/mqlread'
query = [{ "name": "citigroup", "/common/topic/alias": []}]

params = { 'query': json.dumps(query), 'key': key, 'limit':5}

url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())
print response

rawDescription: Freebase query to get alias using python


code:
import sha1

def stored_hash(store, hash):
    store.append(hash)

store = []
loop = 1
while loop == 1:
    data = raw_input('Enter text to hash: ')
    hash = sha1.run(data)
    stored_hash(store, hash)
    print store

rawDescription: Appending to lists in Python

description: Appending to lists in Python


code:
bstr = "তরদজ"
for i in bstr.decode('utf-8'):
    print i

rawDescription: Accessing Bangla (UTF-8) string by index in Python


rawDescription: Python copy file with Fabric - Windows to Debian


code:
def __init__(self, api=None):
    super(StdOutListener, self).__init__()
    self.num_tweets = 0

def on_status(self, status):
    record = {'Text': status.text, 'Created At': status.created_at}
    print record #See Tweepy documentation to learn how to access other fields
    self.num_tweets += 1
    if self.num_tweets < 20:
        collection.insert(record)
        return True
    else:
        return False

rawDescription: Tweepy Streaming - Stop collecting tweets at x amount


rawDescription: How can I add non-sequential numbers to a range?

description: Add non-sequential numbers to a range


code:
query = User.select(User, fn.Count(Tweet.id).alias('num_tweets'))
for user in query:
    print user.username, user.num_tweets
users = list(query)

rawDescription: How to use fn Object in Peewee ORM

description: Use fn Object in Peewee ORM


code:
class ExtendedCurl:
    invpycurl = {v:k for k, v in pycurl.__dict__.items()}
    def __init__(self):
        self._pycurl = pycurl.Curl()
        self._options = {}
    def setopt(self, opt, val):
        self._pycurl.setopt(self, opt, val)
        self._options[opt] = val
    def unsetopt(self, opt):
        self._pycurl.setopt(self, opt, val)
        del self._options[opt]
    def reset(self):
        self._pycurl.setopt(self, opt, val)
        self._options = dict()
    def getopts(self):
        for opt, val in self._options.iteritems():
            if opt in self.invpycurl.keys():
                print "{}: {}".formart(opt, val)
    def errstr(self):
        return self._pycurl.errstr()
    def perform(self):
        return self._pycurl.perform()
    def close(self):
        return self._pycurl.close()

rawDescription: Pycurl list set options

description: Pycurl list set options


code:
T = int(raw_input())
a=[]

for i in range(0,T):
    a.append(raw_input())

for i in range(len(a)):
    for j in range(i+1, len(a)):
        if a[i]==a[j]:
            a[j]=''

while '' in a:
    a.remove('')

print a

rawDescription: Deleting similar elements from a list in python


code:
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
import random

listOfNumbers =[(23, 11), (10, 16), (13, 11),  (10, 16), (10, 16), (10, 16), (10, 16)]
b = Counter(listOfNumbers)
c = []
pres=[]
for k,v in b.most_common():
    c.append(float(v)/float(len(listOfNumbers)))
    pres.append(k)

resultIndex = np.random.choice(np.arange(len(b)), 3, p=c, replace=False)

ass=[]
for res in resultIndex:
    ass.append(pres[res])

print ass

rawDescription: Generate a list of random weighted tuples from a list


code:
import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()

def prepend_ns(s):
    return '{http://www.w3.org/2005/Atom}' + s

for entry in root.findall(prepend_ns('entry')):
    print 'Entry:'
    print '    Title: '   + entry.find(prepend_ns('title')).text
    print '    Author: '  + entry.find(prepend_ns('author')).find(prepend_ns('name')).text
    print '    URL: '     + entry.find(prepend_ns('link')).attrib['href']
    print '    Summary: ' + entry.find(prepend_ns('summary')).text

rawDescription: Parsing XML with Python ElementTree with incorrect tags


code:
import re

INFILE = 'test.html'
OUTFILE = 'replaced.html'

new_site_name = raw_input('Enter input: ')
print new_site_name, 'will be the new site.'

pattern = '.+'
replacement = 'Local website'  % new_site_name

with open(INFILE, 'r') as f:
    html_text = f.read()

with open(OUTFILE, 'w') as f:
    f.write(re.sub('.+', replacement, html_text))

rawDescription: Read file / match string / replace part of the string with user input / write file


code:
address = 16
print "address is %x" % address

rawDescription: format print in python

description: Format print in python


code:
import matplotlib.pyplot as plt
import numpy as np

with open('3phases.txt', 'r') as f:
   for j, line in enumerate(f):
      print line
      x = np.array([int(l) for l in line.split()])
      fig, ax = plt.subplots()

      ax.plot(x, x**2, 'ro')
      plt.savefig(str(j)+'.eps')

rawDescription: multiple plots and save them as an increment

description: Multiple plots and save them as an increment


code:
db2 = sqlalchemy.create_engine('ibm_db_sa://user:pswd@localhost:50001/mydatabase')
conn = db2.connect()
trans = conn.begin()
try:
    sql = "select REPORT_ID from FINAL TABLE(insert into MY_TABLE values(DEFAULT,CURRENT TIMESTAMP,EMPTY_BLOB(),10,'success'));"
    result = conn.execute(sql)
    for item in result:
        id = item[0]
    print id
    trans.commit()
except:
    trans.rollback()
    raise

rawDescription: select from insert into not working with sqlalchemy

description: Select from insert into not working with sqlalchemy


rawDescription: Sum multiple values for same key in lists using python


code:
import numpy as np
x = np.arange(100).reshape(10,10)
print x[0,0]
print np.atleast_1d(x[0,0])
print np.atleast_1d(x[:,:3])

rawDescription: numpy slicing that would always return an array

description: Numpy slicing that would always return an array


code:
from sqlalchemy.schema import CreateTable
print CreateTable(Customer.__table__).compile(engine)

rawDescription: covert a table definition class from SQLAlchemy into vanilla MySQL

description: Covert a table definition class from SQLAlchemy into vanilla MySQL


code:
from __future__ import division
import pandas as pd
import numpy as np
from pandas import Series
from bokeh.plotting import figure, show, output_file

N = 100
img = np.empty((N,N), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((N, N, 4))
for i in range(N):
    for j in range(N):
        view[i, j, 0] = int(i/N*255)
        view[i, j, 1] = 158
        view[i, j, 2] = int(j/N*255)
        view[i, j, 3] = 255

output_file("image_rgba.html", title="image_rgba.py example")

print img.shape
p = figure(x_range=[0,20], y_range=[0,160])
p.image_rgba(image=[img], x=[0], y=[0], dw=[20], dh=[160])

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    # all_data[ticker] = web.get_data_google(ticker, '10/1/2015', '11/1/2015')
    path = 'C:/Users/myuser/Downloads/' + ticker.lower() + '.csv'
    all_data[ticker] = pd.read_csv(path)

AAPL = Series(all_data['AAPL']['Close'])
GOOG = Series(all_data['GOOG']['Close'])
MSFT = Series(all_data['MSFT']['Close'])
IBM = Series(all_data['IBM']['Close'])

p.line(AAPL.index, AAPL, color='#A6CEE3', legend='Apple')
p.line(IBM.index, IBM, color='#0022CC', legend='IBM')
p.line(MSFT.index, MSFT, color='#3322DD', legend='MICROSOFT')

show(p)  # open a browser

rawDescription: Superimposing static bokeh graphs

description: Superimposing static bokeh graphs


code:
import os
import itertools

generators = [os.walk(d) for d in directories]
for root, dirs, files in itertools.chain(*generators):
    print root, dirs, files

rawDescription: Adding elements to python generators

description: Adding elements to python generators


code:
import os, sys, select

# the pipe would fail for some reason if I didn't write to stdout at some point
# so I write a space, then backspace (will show as empty in a normal terminal)
sys.stdout.write(' \b')
pipe_out, pipe_in = os.pipe()
# save a copy of stdout
stdout = os.dup(1)
# replace stdout with our write pipe
os.dup2(pipe_in, 1)

# check if we have more to read from the pipe
def more_data():
        r, _, _ = select.select([pipe_out], [], [], 0)
        return bool(r)

# read the whole pipe
def read_pipe():
        out = ''
        while more_data():
                out += os.read(pipe_out, 1024)

        return out

# testing print methods
import ctypes
libc = ctypes.CDLL('libc.so.6')

print 'This text gets captured by myStdOut'
libc.printf('This text fails to be captured by myStdOut\n')

# put stdout back in place
os.dup2(stdout, 1)
print 'Contents of our stdout pipe:'
print read_pipe()

rawDescription: Capturing print output from shared library called from python with ctypes module


code:
import time
from boto.cloudfront import CloudFrontConnection
from boto.cloudfront.distribution import Distribution
from config import settings
import logging
from django.template.context import RequestContext
from django.shortcuts import render_to_response
logger = logging.getLogger('boto')
logger.setLevel(logging.CRITICAL) #disable DEBUG logging that's enabled in AWS by default (outside of django)

AWS_ACCESS_KEY="AKABCDE1235ABCDEF22A"#SAMPLE
AWS_SECRET_KEY="a1wd2sD1A/GS8qggkXK1u8kHlh+BiLp0C3nBJ2wW" #SAMPLE
key_pair_id="APKABCDEF123ABCDEFAG" #SAMPLE
DOWNLOAD_DIST_ID = "E1ABCDEF3ABCDE" #SAMPLE replace with the ID of your Cloudfront dist from Cloudfront console

############################################
def generate_signed_cookies(resource,expire_minutes=5):
    """
    @resource   path to s3 object inside bucket(or a wildcard path,e.g. '/blah/*' or  '*')
    @expire_minutes     how many minutes before we expire these access credentials (within cookie)
    return tuple of domain used in resource URL & dict of name=>value cookies
    """
    if not resource:
        resource = 'images/*'
    dist_id = DOWNLOAD_DIST_ID
    conn = CloudFrontConnection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
    dist = SignedCookiedCloudfrontDistribution(conn,dist_id)
    return dist.create_signed_cookies(resource,expire_minutes=expire_minutes)

############################################
class SignedCookiedCloudfrontDistribution():

    def __init__(self,connection,download_dist_id,cname=True):
        """
        @download_dist_id   id of your Cloudfront download distribution
        @cname          boolean True to use first domain cname, False to use
                        cloudfront domain name, defaults to cname
                        which presumably matches your writeable cookies ( .mydomain.com)
        """
        self.download_dist = None
        self.domain = None
        try:
            download_dist = connection.get_distribution_info(download_dist_id)
            if cname and download_dist.config.cnames:
                self.domain = download_dist.config.cnames[0] #use first cname if defined
            else:
                self.domain = download_dist.domain_name
            self.download_dist = download_dist
        except Exception, ex:
            logging.error(ex)

    def get_http_resource_url(self,resource=None,secure=False):
        """
        @resource   optional path and/or filename to the resource
                   (e.g. /mydir/somefile.txt);
                    defaults to wildcard if unset '*'
        @secure     whether to use https or http protocol for Cloudfront URL - update
                    to match your distribution settings
        return constructed URL
        """
        if not resource:
            resource = '*'
        protocol = "http" if not secure else "https"
        http_resource = '%s://%s/%s' % (protocol,self.domain,resource)
        return http_resource

    def create_signed_cookies(self,resource,expire_minutes=3):
        """
        generate the Cloudfront download distirbution signed cookies
        @resource   path to the file, path, or wildcard pattern to generate policy for
        @expire_minutes  number of minutes until expiration
        return      tuple with domain used within policy (so it matches
                    cookie domain), and dict of cloudfront cookies you
                    should set in request header
        """
        http_resource = self.get_http_resource_url(resource,secure=False)    #per-file access #NOTE secure should match security settings of cloudfront distribution
    #    http_resource = self.get_http_resource_url("somedir/*")  #blanket access to all /somedir files inside my bucket
    #    http_resource = self.get_http_resource_url("*")          #blanket access to all files inside my bucket

        #generate no-whitespace json policy, then base64 encode & make url safe
        policy = Distribution._canned_policy(http_resource,SignedCookiedCloudfrontDistribution.get_expires(expire_minutes))
        encoded_policy = Distribution._url_base64_encode(policy)

        #assemble the 3 Cloudfront cookies
        signature = SignedCookiedCloudfrontDistribution.generate_signature(policy,private_key_file=settings.AMAZON_PRIV_KEY_FILE)
        cookies = {
            "CloudFront-Policy" :encoded_policy,
            "CloudFront-Signature" :signature,
            "CloudFront-Key-Pair-Id" :key_pair_id #e.g, APKA..... -> same value you use when you sign URLs with boto distribution.create_signed_url() function
        }
        return self.domain,cookies

    @staticmethod
    def get_expires(minutes):
        unixTime = time.time() + (minutes * 60)
        expires = int(unixTime)  #if not converted to int causes Malformed Policy error and has 2 decimals in value
        return expires

    @staticmethod
    def generate_signature(policy,private_key_file=None):
        """
        @policy     no-whitespace json str (NOT encoded yet)
        @private_key_file   your .pem file with which to sign the policy
        return encoded signature for use in cookie
        """
        #sign the policy - code borrowed from Distribution._create_signing_params()
        signature = Distribution._sign_string(policy, private_key_file)
        #now base64 encode the signature & make URL safe
        encoded_signature = Distribution._url_base64_encode(signature)
        return encoded_signature

############################################
def sample_django_view_method(request,template="mytemplate.html"):
    expireLen = 30 #30 minutes
    s3resource = "somepath_in_my_bucket/afile.mp4"
    context = {} #variables I'm passing to my html template
    response = render_to_response(template, context, context_instance=RequestContext(request))
    domain,cookies = generate_signed_cookies(s3resource,expire_minutes=expireLen)
    #TROUBLESHOOTING COOKIES:
    #NOTE - Cookie Domain must be a domain you control that spans your app & your Cloudfront CNAME
    #NOTE - (e.g. if my webapp is www.mydomain.com and my AWS Download Distribution has cname cloud.mydomain.com, cant set cookies from webapp to
            # www.mydomain.com or localhost.mydomain.com or cloud.mydomain.com and have them work
        # -> instead set cookies to .mydomain.com to work across sub-domains, you can then verify in request headers to CloudFront that these cookies get passed.
        # TIP - if you set_cookies from a page with a .mydomain.com suffix, but don't see them get set in Chrome they didn't get set because of permissions - can't set to a diff subdomain or diff base domain
        # TIP - if you set_cookies and see them in Chrome but don't see them in request headers to Cloudfront, cookie domain is likely too narrow, need to widen to span subdomains
    base_domain = '.mydomain.com'
    # NOTE: Sanity check when testing so you can flag any gotchas - I have not fully tested using non-cname urls inside policy vs all possible domains for cookie itself
    if not domain.endswith(base_domain):
        logger.warn("This likely won't work - your resource permissions use a different domain than your cookies")
    for name,value in cookies.items():
        response.set_cookie(name,value=value,httponly=True,domain=base_domain)
    return response

############################################

if __name__ == '__main__':
    domain,cookies = generate_signed_cookies('images/*',expire_minutes=30)

rawDescription: Creating Signed Cookies for Amazon CloudFront


code:
for c in data:
    print ord(c)

rawDescription: receiving symbols from udp, why ?

description: Receiving symbols from udp, why


code:
import urllib
import lxml.html
import os
import shutil

# index page
pattern_files_url = "http://download.nai.com/products/licensed/superdat/english/intel"
# relative url references based here
pattern_files_base = '/'.join(pattern_files_url.split('/')[:-1])

# scrape the index page for latest file list
doc = lxml.html.parse(pattern_files_url)
pattern_files = [ref for ref in doc.xpath("//a/@href") if ref.endswith('xdat.exe')]
if pattern_files:
    pattern_files.sort()
    newest = pattern_files[-1]
    local_name = newest.split('/')[-1]
    # grab it if we don't already have it
    if not os.path.exists(local_name):
        url = pattern_files_base + '/' + newest
        print("downloading %s to %s" % (url, local_name))
        remote = urllib.urlopen(url)
        print dir(remote)
        with open(local_name, 'w') as local:
            shutil.copyfileobj(remote, local, length=65536)

rawDescription: Download a file from Web when file name change every time


code:
import time
import numpy as np
from scipy import spatial
import multiprocessing as mp

pool = mp.Pool(4)

test_data = np.random.random(100000*500).reshape([100000,500])

outfile = open('/tmp/test.out','w')

def split(data,size):
    for i in xrange(0, len(data), size):
        yield data[i:i+size]

def distance(vecs):
    return spatial.distance.cdist(vecs,test_data)

chunks = list(split(test_data,100))
for chunk in chunks:
    t0 = time.time()
    distances = spatial.distance.cdist(chunk,test_data)
    outfile.write(' '.join([str(x) for x in distances]))
    print 'estimated: %.2f secs'%((time.time()-t0)*len(chunks))

rawDescription: Optimize adjacency matrix computation

description: Optimize adjacency matrix computation


code:
def testKeys():
    import pygame
    from time import time
    screen = pygame.display.set_mode((100,100))
    pygame.key.set_repeat(1,100)
    ticks = 0
    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE: print 'space'
                elif event.key == pygame.K_ESCAPE: done = True
        new_ticks = time()
        if new_ticks - ticks >= 0.1:
            ticks = new_ticks
            if pygame.key.get_pressed()[pygame.K_LSHIFT]:
                print 'shift'

rawDescription: Pygame - why LSHIFT does not respond in key repeat mode


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Item(Base):
    __tablename__ = 'item'

    id = Column(Integer, primary_key=True)
    description_id = Column(Integer, ForeignKey('item_description.id'))

class ItemDescription(Base):
    __tablename__ = 'item_description'

    id = Column(Integer, primary_key=True)

    any_item_id = column_property(
        select(
            [Item.id],
            id == Item.description_id,
            limit=1,
        ).label('any_item_id'),
        deferred=True,
    )

e = create_engine('sqlite://', echo=True)
Base.metadata.create_all(e)

s = Session(e)

descriptions = [
    ItemDescription(id=1),
    ItemDescription(id=2),
    ItemDescription(id=3),
]

s.add_all(descriptions)

items = [
    Item(id=1, description_id=1),
    Item(id=2, description_id=1),
    Item(id=3, description_id=1),
    Item(id=4, description_id=2),
    Item(id=5, description_id=2),
    Item(id=6, description_id=3),
    Item(id=7, description_id=3),
    Item(id=8, description_id=3),
]

s.add_all(items)

query = s.query(ItemDescription).options(undefer('any_item_id'))
for description in query:
    print description.any_item_id, description.id

# alternative way without using column_property
query = s.query(
    select(
        [Item.id],
        ItemDescription.id == Item.description_id,
        limit=1,
    ).label('any_item_id'),
    ItemDescription,
)
for item_id, description in query:
    print item_id, description.id

rawDescription: How to select one of table A based on table B in SqlAlchemy?

description: Select one of table A based on table B in SqlAlchemy


code:
with open('ipaddr.txt', "r") as f:
    lines=f.readlines()
    table = [line.split("\t") for line in lines]
    print table[22][39:]
    print table[18][39:]
    print table[26][39:]
    f.close()

rawDescription: Pulling specific words out of a line of a text file in python


code:
import re
word = 'fubar'
regexp = re.compile(r'ba[r|z|d]')
if regexp.search(word) is not None:
  print 'matched'

rawDescription: python's re: return True if regex contains in the string

description: Python's re: return True if regex contains in the string


code:
import datetime

dt = datetime.datetime(2010, 12, 01)
end = datetime.datetime(2010, 12, 30, 23, 59, 59)
step = datetime.timedelta(seconds=5)

result = []

while dt < end:
    result.append(dt.strftime('%Y-%m-%d %H:%M:%S'))
    dt += step

rawDescription: Python generate dates series

description: Python generate dates series


code:
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('8805198.cfg')
d = dict((section, dict(config.items(section))) for section in config.sections())
print d
print d['Message 1']
print d['Message 1']['3']

rawDescription: Need help parsing config data

description: Need help parsing config data


code:
#!/usr/bin/env python

def format_box(title, body, width=80):
    box_line = lambda text: "*  " + text + (" " * (width - 6 - len(text))) + "  *"

    print "*" * width
    print box_line(title)
    print "*" * width
    print box_line("")

    for line in body.split("\n"):
        print box_line(line.expandtabs())

    print box_line("")
    print "*" * width

format_box(
    "Here's the title",

    "The body will go in here.  Say I put a line break here ---> \n"
    "it will go to the next line.  I also want to keep track\n"
    "\t<----- of tabs so I can space things out on lines if i have to"
);

rawDescription: Formatting text into boxes in the Python Shell


code:
i = open('BGBiasRE_IM3_wCSrescaled.txt', 'r')

Xnames, Ynames = [], []

count = 0
for line in i:
    if count > 0:
        line_tmp = line.split()
        if line_tmp[0] == 'Step':
            step = (line_tmp[2].split('='))[1]
            varnameX = 'Temp' + str(step) +'_Xvals'
            varnameY = 'Temp' + str(step) +'_Yvals'
            globals()[varnameX] = []
            globals()[varnameY] = []
            Xnames.append(varnameX)
            Ynames.append(varnameY)
        else:
            globals()[varnameX].append(float(line_tmp[0]))
            globals()[varnameY].append(float(line_tmp[1]))
    count += 1
i.close()

for name in Xnames:
    print name + ' = ' + str(eval(name))

for name in Ynames:
    print name + ' = ' + str(eval(name))

rawDescription: Splitting up a data file in Python, round 2


code:
#!/bin/sh
"""":
python2 -c "" 2>/dev/null && exec python2 $0 ${1+"$@"}
python  -c "" 2>/dev/null && exec python  $0 ${1+"$@"}
echo "Could not find a python interpreter."
exit 1
"""

print "hello python"

rawDescription: Scripting Hashbang: How to get the python 2 interpreter?


code:
while doingStuff:
    msg = "\rMerged %s out of %s" % (idx, totalCount)
    print msg,

rawDescription: Python command line UI

description: Python command line UI


code:
print time.strftime(("%m/%d/%Y %I:%M:%S "
    "%p"), time.localtime(os.path.getmtime(fname)))

rawDescription: python code formatting: how to avoid extra spaces within a string that is cut off by the line-breaker "\"

description: Python code formatting: how to avoid extra spaces within a string that is cut off by the line-breaker "\"


code:
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from os import urandom
import hashlib
import binascii

def key_generation(password, salt, iterations):
    assert iterations > 0
    print "Salt: " + tohex(salt), '\n', "Password: " + password
    key = password + salt #Combines [password] and [salt] to create a [key]
    for i in range(iterations): #Hashes the [key]
        key = hashlib.sha256(key).digest() #Using Sha256 it hashes the [key] based on amount of [iterations]
    return key

def pad(s):
    return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

def encryption(message, password, salt, iterations, key_size=256):

    print " === ENCRYPTION === "

    key = key_generation(password, salt, iterations)
    print "Key: " + tohex(key)
    message = pad(message)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    print "Random IV: " + tohex(iv)
    enc = salt + iv + cipher.encrypt(message)
    return enc

def decryption(ciphertext, password, iterations):

    print " === DECRYPTION === "

    salt = ciphertext[0:10]
    iv = ciphertext[10:10+AES.block_size]
    print "Random IV: " + tohex(iv)
    key = key_generation(password, salt, iterations)
    print "Key: " + tohex(key)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext[10+AES.block_size:])
    print "Plaintext: " + plaintext
    return plaintext.rstrip(b"\0")

def encrypt_file(file_name, password, salt, iterations):
    with open(file_name, 'rb') as fo:
        plaintext = fo.read()
        print "Text: " + plaintext
    enc = encryption(plaintext, password, salt, iterations)
    print "Encrypted Contents: " + enc
    with open(file_name + ".enc", 'wb') as fo:
        fo.write(enc)

def decrypt_file(file_name, password, iterations):
    with open(file_name, 'rb') as fo:
        ciphertext = fo.read()
        print "Cipher'd Text: " + ciphertext
    dec = decryption(ciphertext, password, iterations)
    with open(file_name + ".dec", 'wb') as fo:
        fo.write(dec)

def tohex(data):
    return binascii.hexlify(data)

encrypt_file('HelloWorld.txt', 'password', 'randomsalt', 64000)
decrypt_file('HelloWorld.txt.enc', 'password', 64000)

rawDescription: Decryption AES decrypt() not yielding results


code:
import json
from pprint import pprint

data = json.load(json_data)
pprint(data)

for category in data['query']['categorymembers']:
    print 'PageID: %d and Title: %s' % (category['pageid'], category['title'])

rawDescription: Parsing JSON format

description: Parsing JSON format


code:
d_items = sorted(d.items(), key=lambda x: -x[1])
print d_items[:2]
[('Beth', 9102), ('Cecil', 3258)]

rawDescription: Finding the dictionary keys whose values are numerically highest


code:
import numpy as np
from scipy.spatial.distance import cdist
from scipy.optimize import fmin
import scipy

# Draw a fuzzy circle to test
N = 15
THETA = np.random.random(15)*2*np.pi
R     = 1.5 + (.1*np.random.random(15) - .05)
X = R*np.cos(THETA) + 5
Y = R*np.sin(THETA) - 2

# Choose the inital center of fit circle as the CM
xm = X.mean()
ym = Y.mean()

# Choose the inital radius as the average distance to the CM
cm = np.array([xm,ym]).reshape(1,2)
rm = cdist(cm, np.array([X,Y]).T).mean()

# Best fit a circle to these points
def err((w,v,r)):
    pts = [np.linalg.norm([x-w,y-v])-r for x,y in zip(X,Y)]
    return (np.array(pts)**2).sum()

xf,yf,rf = scipy.optimize.fmin(err,[xm,ym,rm])

# Viszualize the results
import pylab as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

# Show the inital guess circle
circ = plt.Circle((xm, ym), radius=rm, color='y',lw=2,alpha=.5)
ax.add_patch(circ)

# Show the fit circle
circ = plt.Circle((xf, yf), radius=rf, color='b',lw=2,alpha=.5)
ax.add_patch(circ)

plt.axis('equal')
plt.scatter(X,Y)
plt.show()

rawDescription: Approximating a polygon with a circle

description: Approximating a polygon with a circle


code:
def work(container):
    container[0] = True

a = [False]
t = Timer(10, work, args=(a,))
t.start()

while not a[0]:
    print "Waiting, a[0]={0}...".format(a[0])
    time.sleep(1)

print "Done, result: {0}.".format(a[0])

rawDescription: python- how to get the output of the function used in Timer

description: Python- how to get the output of the function used in Timer


code:
#!/usr/bin/python
import argparse

if __name__=="__main__":
    parser = argparse.ArgumentParser(description='eg $python myargs.py --base arg1 --pattern arg2 [--verbose] [--live]')
    parser.add_argument('--base', required=True, type=str)
    parser.add_argument('--pattern', required=True, type=str)
    parser.add_argument('--verbose', action='store_true')
    parser.add_argument('--live', action='store_true')

    args = parser.parse_args()

    print "args.base=" + str(args.base)
    print "args.pattern=" + str(args.pattern)
    print "args.verbose=" + str(args.verbose)
    print "args.live=" + str(args.live)

rawDescription: argparse coding issue

description: Argparse coding issue


code:
from scipy.integrate import quad
from math import sqrt
from math import pi

def x_max(y,r):
    return sqrt(r**2-y**2)

def x_min(y,r):
    return max(-sqrt(r**2 - y**2), -sqrt(R**2 - y**2) + b)

def y_max(r):
    if (R<b and b-R<r) or (R>b and b-R>r):
        return sqrt( R**2 - (R**2-r**2+b**2)**2/(4.*b**2) )
    elif r+R<b:
        return 0.
    else: #r+b<R
        return r

def z_max():
    if R>b:
        return R
    else:
        return sqrt(2.*b*R - b**2)

def delta_x(y, r):
    return  x_max(y,r) - x_min(y,r)

def int_xy(z):
    r = sqrt(R**2 - z**2)
    return quad(delta_x, 0., y_max(r), args=(r))

def int_xyz():
    return quad(lambda z: int_xy(z)[0], 0., z_max())

R=1.
Rprim=1.
b=0.5
print 4*int_xyz()[0]

rawDescription: 3d integral, python, integration set constrained


code:
rect = driver.find_element_by_xpath('//div[@id="workload-analysis-chart"]/div/svg/g/g[@data-selected="true"]/rect')
print rect.get_attribute('style')

rawDescription: How to formulate an Xpath expression to get an attribute from a child node?

description: Formulate an Xpath expression to get an attribute from a child node


code:
from collections import namedtuple

CallbackVar = namedtuple('CallbackVar', ['change_callback', 'read_callback'])

class CallbackDict(dict):
    'Variant of dict that does callbacks for instances of CallbackVar'
    def __getitem__(self, key):
        value = dict.__getitem__(self, key)
        if isinstance(value, CallbackVar):
            return value.read_callback(key)
    def __setitem__(self, key, value):
        try:
            realvalue = dict.__getitem__(self, key)
            if isinstance(realvalue, CallbackVar):
                return realvalue.change_callback(key, value)
        except KeyError:
            pass
        return dict.__setitem__(self, key, value)

stmts = '''
x = CallbackVar(setter, getter)     # Attach getter() and setter() to "x"
x = 1                               # Invoke the setter()
x                                   # Invoke the getter()
'''

def getter(key):
    print 'Getting', key
    return 42

def setter(key, value):
    print 'Setting', key, 'to', value

exec stmts in globals(), CallbackDict()

rawDescription: creating a class that behaves like any variable but has callback on change/read

description: Creating a class that behaves like any variable but has callback on change/read


code:
htm = '''
ABE   m
  English
, Hebrew

Short form of ABRAHAM'''

import BeautifulSoup

soup = BeautifulSoup.BeautifulSoup(htm)

for p in soup.findAll('p'):
  firsta = True
  shortf = False
  for c in p.recursiveChildGenerator():
    if isinstance(c, BeautifulSoup.NavigableString):
      if 'Short form of' in str(c):
        shortf = True
    elif c.name == 'a':
      if firsta or shortf:
        print c.renderContents()
        firsta = shortf = False

rawDescription: How to use BeautifulSoup to extract from within a HTML paragraph?

description: Use BeautifulSoup to extract from within a HTML paragraph


code:
field_length = df.MixedField.astype(str).map(len)
print df.loc[field_length.argmax(), 'MixedField']
print df.loc[field_length.argmin(), 'MixedField']

rawDescription: How to get the longest length string/integer/float from a pandas column when there are strings in the column

description: Get the longest length string/integer/float from a pandas column when there are strings in the column


code:
from collections import defaultdict
d = defaultdict(set)
with open(datafile) as f:
    for line in f:
        my_id,item = line.split()
        d[item].add(my_id)

for item in d:
    print item,len(d[item]),sorted(d[item])

rawDescription: python listing and counting values

description: Python listing and counting values


code:
import sys
class LoggedStdout():
    def __init__(self, filename = None):
        self.filename = filename
    def write(self, text):
        sys.__stdout__.write(text)
        if not self.filename is None:
            self.log(text)
    def log(self, line):
        with open(self.filename, "a") as f:
            f.write('\n' + line)

sys.stdout = LoggedStdout('file.txt')

print 'Hello world!'

rawDescription: Log everything printed into a file

description: Log everything printed into a file


code:
arrays = []
with open('salida.txt', 'r') as wordfile:
    for line in wordfile:
        # Split the line on commas.
        words = line.split(',')
        for count, word in enumerate(words):
            # Remove any whitespace.
            word = word.strip()
            # That might leave a blank string, e.g. at the end.
            if word:
                # Do we need to add another array to our list of arrays?
                if count == len(arrays):
                    arrays.append([])
                arrays[count].append(word)
print arrays

rawDescription: Read a file and create arrays with the words of each column


code:
from urllib import unquote
test_mailto = 'mailto:me@mail.com?subject=mysubject&body=mybody'

def parse_mailto(mailto):
   result = dict()
   colon_split = mailto.split(':',1)
   quest_split = colon_split[1].split('?',1)
   result['email'] = quest_split[0]

   for pair in quest_split[1].split('&'):
      name = unquote(pair.split('=')[0])
      value = unquote(pair.split('=')[1])
      result[name] = value

   return result

print parse_mailto(test_mailto)

rawDescription: Parse mailto urls in Python

description: Parse mailto urls in Python


code:
getch = _Getch()

print 'Please enter your guess: '
x = getch()

if (int(x) == 1):
    print 'correct'
else:
    print 'wrong'

rawDescription: How to enter a input without pressing enter

description: Enter a input without pressing enter


code:
adds = map((lambda (j, f, a):
  j.join([f.format(i) for i in parse.options[a]]) if parse.options[a] else ''),
  ((' ', ' -not -path "{0}" ', 'exclude'),
  (' -or ', '-path "{0}"', 'include')))

rawDescription: How to pass items as args lists in map?

description: Pass items as args lists in map


code:
list = [1,2,3,4,5,6,7,8,9]
a=3
c=7
split_list = list[a:(c+1)] #<-----The +1 is because the splice syntax goes to end-1
print split_list

rawDescription: Slicing Lists with Variable Based Index

description: Slicing Lists with Variable Based Index


code:
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)
cur = conn.cursor()
cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
print cur.fetchall()

rawDescription: Can not see the DB query output in python


code:
for u in dir(form):
    if not u.startswith('__'):
        if u.startswith('node_'):
            print getattr(form, u)

rawDescription: Accessing python objects within objects in Python


code:
for item in list2:
    if any(item == c.name for c in list):
        print 'it is a member'

rawDescription: Check if item is member of list of objects


code:
biterms = u'''Difensori dei diritti umani, libertà di espressione > Human rights defenders, freedom of expression
sgomberi forzati, violazioni dei diritti umani > forced evictions, human rights violations'''.splitlines()
bigrams = []
for line in biterms:
    l = []
    trans = line.split(' > ')
    left =  trans[0].split(', ')
    right = trans[1].split(', ')
    for i in left:
        for j in right:
            l.append((i, j))
    bigrams.append(l)

for g in bigrams:
    print g

rawDescription: create bigrams for every line in a bilingual terms list

description: Create bigrams for every line in a bilingual terms list


code:
from datetime import *
from dateutil.rrule import rrule, YEARLY

# GLOBAL CONFIG
td_8am = timedelta(seconds=3600*8)
td_jobfrequency = timedelta(seconds=3600) # hourly job

# USER DATA::
# birthday: assumed to be retrieved from some data source
bday = date(1960, 5, 12)
# reminder delta: number of days before the b-day
td_delta = timedelta(days=6)
# difference between the user TZ and the server TZ
tz_diff = timedelta(seconds=3600*5) # example: +5h

# from current time minus the job periodicity and the delta
sday = date.today()
# occr will return the first birthday from today on
occr = rrule(YEARLY, bymonth=bday.month, bymonthday=bday.day, dtstart=sday, count=1)[0]

# adjust: subtract the reminder delta, fixed 8h (8am) and tz difference
occr -= (td_delta + td_8am + tz_diff)

# send the reminder when the adjusted occurance is within this job time period
if datetime.now() - td_jobfrequency < occr < datetime.now():
    print occr, '@todo: send the reminder'
else:
    print occr, 'no reminder'

rawDescription: Recurrent yearly date alert in Python

description: Recurrent yearly date alert in Python


code:
import copy

class Foo(object):
    def __init__(self):
        self.x = None
        self.y = None
        self.z = None
    def copy(self):
        return copy.deepcopy(self)

a = Foo()
a.x = 1
a.y = 2
a.z = 3

b = a.copy()
print b.x
print b.y
print b.z

rawDescription: Is there a decent way of creating a copy constructor in python?


code:
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class TTTClientProtocol(LineReceiver):
    def lineReceived(self, line):
        line = line.strip()
        if line == 'You are connected':
            self.sendLine(self.factory.username)
        else:
            print 'SERVER SAYS:', line

class TTTClientFactory(ClientFactory):
    protocol = TTTClientProtocol

    def __init__(self, name):
        self.username = name

name = raw_input('Please enter your name: ')
print 'Connecting...'

reactor.connectTCP('localhost', 8007, TTTClientFactory(name))

reactor.run()

rawDescription: I have a twisted reactor running, how do I connect to it?


code:
def start_av(t):
  if t == 0: return 0, 0
  acc, vel = start_av(t-1)
  thr = 10
  c = -0.1
  fric = c*vel
  acc = thr + fric
  return (acc, vel + acc)

def given_av(t, acc=0, vel=0):
  if t == 0: return acc, vel
  thr = 10
  c = -0.1
  fric = c*vel
  acc = thr + fric
  return given_av(t-1, acc, vel + acc)

print start_av(10)
print given_av(10)

rawDescription: Convert a for-loop to a recursive function


code: print dir(getattr(sys, function))

rawDescription: Python dir() function with variable arguments


code:
def delayedDoSomethings():
    def work():
        for i in rance(1, 10000000):
            print 'hi',i
    import thread
    thread.start_new_thread(separateThread, ()) #run the work function in a separate thread.

rawDescription: press more than one Tkinter button

description: Press more than one Tkinter button


code:
def PrintEntryDetails(entry):
   print 'Video title: %s' % entry.media.title.text
   print 'Video published on: %s ' % entry.published.text
   print 'Video description: %s' % entry.media.description.text
   print 'Video category: %s' % entry.media.category[0].text
   print 'Video tags: %s' % entry.media.keywords.text
   print 'Video watch page: %s' % entry.media.player.url
   print 'Video flash player URL: %s' % entry.GetSwfUrl()
   print 'Video duration: %s' % entry.media.duration.seconds

rawDescription: Which is the best way to get the tags from Youtube with python?


rawDescription: Parse JSON in python to a dictionary

description: Parse JSON in python to a dictionary


code:
import sys
from time import sleep
# For python 2 import this
#from itertools import izip as zip
# Delay in seconds to wait before printing each *character* of the song lyrics
time_delays = [0.1, 0.1, 0.1, 0.5, 0.2, 0.1, 0.1]
song_lyrics = "thesong"

print "Let's sing a song..."
for song_char, char_delay in zip(song_lyrics, time_delays):
    sleep(char_delay)
    sys.stdout.write(song_char)
    sys.stdout.flush()

rawDescription: How To Read a Song in python letter by letter while playing the sound file of that song


code:
line = p.stdout.readline()
while line:
    print line
    line = p.stdout.readline()

rawDescription: Unbuffered read from process using subprocess in Python


code:
#!/usr/bin/python
import os
import subprocess
proc=raw_input("Enter the mode :")
os.environ["JAVA_HOME"] = '/usr/lib/jvm/java-7-openjdk-amd64'
os.environ["CATALINA_HOME"] = '/export/apps/tomcat7'
if proc == "start":
  os.getcwd()
  os.chdir("/export/apps/tomcat7/bin/")
  os.getcwd()
  subprocess.call('sh catalina.sh start',shell=True)
  print "Tomcat started successfully"
elif proc == "stop":
  os.getcwd()
  os.chdir("/export/apps/tomcat7/bin/")
  os.getcwd()
  subprocess.call('sh catalina.sh stop',shell=True)
  print "Tomcat stopped successfully"
elif proc == "restart":
  os.getcwd()
  os.chdir("/export/apps/tomcat7/bin/")
  os.getcwd()
  subprocess.call('sh catalina.sh stop',shell=True)
  subprocess.call('sh catalina.sh start',shell=True)
  print "tomcat restarted successfully"
else:
 print "error: give any mode"
print "Thank you"

rawDescription: Need to start tomcat automatically

description: Need to start tomcat automatically


code:
import os
import time
from multiprocessing import Process

def subprocess():
    while True:
        ppid = os.getppid()
        print "Parent process id:", ppid
        if ppid == 1:
            print "Parent process has terminated"
            break
        time.sleep(1)

p = Process(target=subprocess)
p.start()

rawDescription: Python multiprocessing. How do you get the status of the parent from the child?


code:
import simplejson
from twython import Twython

def get_twitter():
    t = Twython(app_key='',
    app_secret='',
    oauth_token='',
    oauth_token_secret='')

    user = t.lookup_user(screen_name = username)
    print "Tweets: " + str(user[0]['statuses_count'])
    print "Followers: " + str(user[0]['followers_count'])
    print "Likes: " + str(user[0]['favourites_count'])

rawDescription: Can't parse twitter hidden input with BeautifulSoup


code:
import types
print [f for f in globals().values() if type(f) == types.FunctionType]

rawDescription: Display a list of user defined functions in the Python IDLE session


code:
import fileinput

for line in fileinput.input(['test.py'], inplace=True):
  print line.replace('027FC8EBC2D1', 'NewValue'),

rawDescription: Cannot get variable.replace working properly


code:
#!/usr/bin/env python

import sys

def main(args):
    print args

if __name__ == '__main__':
    main(sys.argv)

rawDescription: Python's Equivalent of "public static void main"


code:
"""Convert DICOM files to PNG format, remove blank areas. The blank erea
   removal is based on the image size of a Philips flat detector. The image
   goes from 2048x2048 pixels to 1574x2048 pixels."""

import os
import sys
import subprocess
from multiprocessing import Pool, Lock

globallock = Lock()

def checkfor(args):
    """Make sure that a program necessary for using this script is
    available."""
    if isinstance(args, str):
        args = args.split()
    try:
        f = open('/dev/null')
        subprocess.call(args, stderr=subprocess.STDOUT, stdout=f)
        f.close()
    except:
        print "Required program '{}' not found! exiting.".format(args[0])
        sys.exit(1)

def processfile(fname):
    """Use the convert(1) program from the ImageMagick suite to convert the
       image and crop it."""
    size = '1574x2048'
    args = ['convert', fname, '-units', 'PixelsPerInch', '-density', '300',
            '-crop', size+'+232+0', '-page', size+'+0+0', fname+'.png']
    rv = subprocess.call(args)
    globallock.acquire()
    if rv != 0:
        print "Error '{}' when processing file '{}'.".format(rv, fname)
    else:
        print "File '{}' processed.".format(fname)
    globallock.release()

def main(argv):
    """Main program.

    Keyword arguments:
    argv -- command line arguments
    """
    if len(argv) == 1:
        path, binary = os.path.split(argv[0])
        print "Usage: {} [file ...]".format(binary)
        sys.exit(0)
    checkfor('convert')
    p = Pool()
    p.map(processfile, argv[1:])
    p.close()

## This is the main program ##
if __name__ == '__main__':
    main(sys.argv)

rawDescription: Distribute multiple processes over different cores in python


code:
def x(*args, **kwargs):
    a = kwargs.get('a', False)
    print args, a

x(1,2,3,a=42)

rawDescription: How can I write a python function to accept *arguments and a keyword argument?

description: Write a python function to accept *arguments and a keyword argument


code:
def read_items(prompt):
    items = []
    while True:
        answer = raw_input('%s (blank to end): ' % prompt)
        if not answer:
            return items
        items.append(answer)

ingredients = read_items('enter ingredients')
counts = []
for item in ingredients:
    cnt = raw_input('how much of %s:' % item)
    counts.append(int(cnt))

print zip(ingredients, counts)

rawDescription: Running a loop (x) times

description: Running a loop (x) times


code:
def maskWord(state, word, guess):
    state = list(state)
    for i in range(len(word)):
        if word[i] == guess:
             state[i] = guess
    return "".join(state)

# Lets test if it works..:
word = "electricity"
state = "-" * len(word)
tries = 0

play = True
while play:
    if tries == len(word)*2:
        print "Fail...";
        play = False
    guess = raw_input("Guess: ")
    tries +=1
    state = maskWord(state, word, guess)
    print state
    if maskWord(state, word, guess) == word:
        print "WIN, WIN!!";
        play = False

rawDescription: guess the word game in python - how to mask a word (eg. ------)

description: Guess the word game in python - how to mask a word (eg. ------)


code:
from twisted.internet import task

class Intake:
    def __init__(self):

        #
        # aggregate dict to store all the counters
        #
        self.counters = {}
        self.count = 0
        #
        # start a looping call to run reach minute
        #
        self.lc = task.LoopingCall(self.aggregate, self.counters)
        self.lc.start(1)

    def aggregate(self, counters):
        print '%d, %r, %r' % (self.count, counters, self.counters)
        self.count += 1
        self.counters = {}

if __name__ == "__main__":
    from twisted.internet import reactor
    r = Intake()
    reactor.run()

rawDescription: twisted loopingcall access class vars

description: Twisted loopingcall access class vars


code:
import itertools

iterables = [ [1,2,3,4], [88,99], ['a','b'] ]

for t in itertools.product(*iterables):
    print t

rawDescription: Generating Combinations in python

description: Generating Combinations in python


code:
if your_list[0] == 'MATE555':
   print your_list[1]
else:
   print 'no key'

rawDescription: How to check if the key the i want is existing in python

description: Check if the key the i want is existing in python


code:
class Project(models.Model):
    # your fields....

    @property
    def size(self):
        return self.order_size_price * self.order_size_weight

# now this should work for every Project instance:
project = Project.objects.get(pk=1)
print project.size

rawDescription: Model manager and model methods for a get query


code:
import pandas as pd
import datetime

dates =  [datetime.date(2015,01,05),
          datetime.date(2015,01,18),
          datetime.date(2015,01,25),
          datetime.date(2015,02,18),
          datetime.date(2015,03,07),
          datetime.date(2015,03,27),]

df = pd.DataFrame({'dates':dates})
df.dates = pd.to_datetime(df.dates)

df.groupby(df.dates.dt.month).count().plot(kind='bar')

rawDescription: Matplotlib even frequency binned by month bar chart


code:
import re

def largest_substring(string):

    length = 0
    x=0
    y=0

    for y in range(len(string)):
        for x in range(len(string)):
            substring = string[y:x]
            if len(list(re.finditer(substring,string))) > 1  and len(substring) > length:
                match = substring
                length = len(substring)
    return match

print largest_substring("this is repeated is repeated is repeated")

rawDescription: Finding the largest repeating substring

description: Finding the largest repeating substring