code:
import numpy as np

x = "hello"
y = np.frombuffer(x, dtype=np.uint8)

# Make "y" writeable...
y.flags.writeable = True

# Prove that we're using the same memory
y[0] = 121
print x # <-- Notice that we're outputting changing y and printing x...

rawDescription: efficient numpy.fromfile on zipped files?

description: Efficient numpy.fromfile on zipped files


code:
class Parser(object):

    def __init__(self, filename):

        def tokens(filename):
            with open(filename) as infile:
                for line in infile:
                    for item in line.split():
                        yield int(item)

        self.tokens = tokens(filename)

    def ballots(self):
        ballot = []
        for t in self.tokens:
            if t:
                ballot.append(t)
            else:
                yield ballot
                ballot = []

p = Parser("datafile.txt")

for b in p.ballots():
    print b

rawDescription: reading very large file where format is newline independent

description: Reading very large file where format is newline independent


code: blank=True, null=True

rawDescription: How to set NULL for IntegerField instead of setting 0?

description: Set NULL for IntegerField instead of setting 0


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

def create_sample_set(mask, N=36, shape_color=[0,0,1.,1.]):
    rv = np.ones((N, mask.shape[0], mask.shape[1], 4),dtype=np.float)
    mask = mask.astype(bool)
    for i in range(N):
        for j in range(3):
            current_color_layer = rv[i,:,:,j]
            current_color_layer[:,:] *= np.random.random()
            current_color_layer[mask] = np.ones((mask.sum())) * shape_color[j]
    return rv

# create set of sample image and plot them
image = PIL.Image.open("14767594_in.png")
image_data = np.asarray(image)
image_data_blue = image_data[:,:,2]
median_blue = np.median(image_data_blue)
sample_images = create_sample_set(image_data_blue>median_blue)
plt.figure(1)
for i in range(36):
    plt.subplot(6,6,i+1)
    plt.imshow(sample_images[i,...])
    plt.axis("off")
plt.subplots_adjust(0,0,1,1,0,0)

# determine per-pixel variablility, std() over all images
variability = sample_images.std(axis=0).sum(axis=2)

# show image of these variabilities
plt.figure(2)
plt.imshow(variability, cmap=plt.cm.gray, interpolation="nearest", origin="lower")

# determine bounding box
mean_variability = variability.mean()
non_empty_columns = np.where(variability.min(axis=0)<mean_variability)[0]
non_empty_rows = np.where(variability.min(axis=1)<mean_variability)[0]
boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))

# plot and print boundingBox
bb = boundingBox
plt.plot([bb[2], bb[3], bb[3], bb[2], bb[2]],
         [bb[0], bb[0],bb[1], bb[1], bb[0]],
         "r-")
plt.xlim(0,variability.shape[1])
plt.ylim(variability.shape[0],0)

print boundingBox
plt.show()

rawDescription: How to detect object on images?

description: Detect object on images


code:
d = {'q1': {'d1': 1.2 , 'd5': 5.2},
     'q2': {'d4': 3.23, 'd7': 3.9 , 'd8': 4.1},
     'q3': {'d3': 0.5}}

# First, flatten to a list of one key-value pair dictionaries
inner_dicts = [
    dict([(k,v)])
    for inner_d in d.values()
        for (k, v) in inner_d.items()]

# Second, sort that list based on the value in each dict
dictlist_sorted = sorted(
    inner_dicts,
    key=lambda temp_d: temp_d.values()[0],
    reverse=True)

# Third, extract the key of each dict
keys_sorted = [temp_d.keys()[0] for temp_d in dictlist_sorted]

print keys_sorted

rawDescription: How to order a dictionary of dictionaries based on inner dictionary's value

description: Order a dictionary of dictionaries based on inner dictionary's value


rawDescription: Order multiple sorted arrays

description: Order multiple sorted arrays


code:
from lxml import etree
s = """

Event	Start Date	End Date

a	b	c

d	e	f

g	h	i

"""
table = etree.XML(s)
rows = iter(table)
headers = [col.text for col in next(rows)]
for row in rows:
    values = [col.text for col in row]
    print dict(zip(headers, values))

rawDescription: Parse HTML table to Python list?

description: Parse HTML table to Python list


code:
while True:
    say = raw_input('say: ')
    if say == 'hello':
        print "hi!"
        break
    elif say == 'bye':
        print "bye!"
        break

rawDescription: how to go back and check for another input

description: Go back and check for another input


code:
class Logger(object):
    def __init__(self, name):
        self.name = name
    def run_logged(self, value):
        print "Running", self.name, "with info", self.info()
        return self.run(value)

class BLogged(B, Logger):
    def __init__(self, i, j):
        B.__init__(self, i, j)
        Logger.__init__("B")
    def info(self):
        return 42

rawDescription: Why is super() broken in Python-2.x?

description: Why is super() broken in Python-2.x


code:
import datetime
datetime.datetime.now()
print datetime.datetime.now() + datetime.timedelta(days=1)
print datetime.datetime.now() + datetime.timedelta(days=2)
print datetime.datetime.now() - datetime.timedelta(days=1)

rawDescription: Arithmetic on datetime objects

description: Arithmetic on datetime objects


code:
#contents of C:\tmp\eggs\test.py:

import sys
sys.path.insert(1, 'libs')
from pkg_resources import require

require('foo<1.5')
import foo
print foo.version
# will output (1,0)

rawDescription: How can I call python module inside versioned package folder?

description: Call python module inside versioned package folder


code:
import matplotlib.pyplot as plt
X=[1,2,3,4,5,6,7,8,9,10]
Y=[1,1,1,2,10,2,1,1,1,1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X,Y,label="prova") #plot the function
#plt.legend(loc=1, ncol=1, shadow=True)
plt.xlim(min(X) * 0.9, max(X) * 1.1)
plt.ylim(min(Y) * 0.9, max(Y) * 1.1)
plt.ylabel(r'Y axis')
plt.xlabel(r'X axis')
Diz=dict(zip(X,Y)) #create a dictionary that associate X with Y
Interval=[] #interval where the user search for peaks
PeaksList=[] #list of peaks found
def onclick(event):
    print 'First limit at =%f'%(event.xdata)
    Interval.append(event.xdata)
    if len(Interval)%2==0:
        a=Interval[-2]
        b=Interval[-1]
        if b<a: #if the user select first the highest value these statements filp it!
            A=b
            B=a
        else:
            A=a
            B=b
      #find the max Y value: the peak!
        peakY=0 #max Y value
        piccoX=0 #value of the X associate to the peak
        for i in [ j for j in X if A<j<B] :
            if Diz[i]>peakY:
                peakY=Diz[i]
                piccoX=i
        print "Interval: %f - %f  Peak at: %f " %(a,b,piccoX)
        PeaksList.append([piccoX,peakY])
        ax.annotate("picco", xy=(piccoX,peakY),  xycoords='data',
                xytext=(-50, 30), textcoords='offset points',
                arrowprops=dict(arrowstyle="->")
                )
        plt.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

rawDescription: How to interactively find and annotate local max (peaks) in matplotlib?

description: Interactively find and annotate local max (peaks) in matplotlib


code:
"""Secure python variables: encrypt, decrypt, import into global namespace."""
__module__    = 'security.py'
__author__    = "Kenneth A Younge"
__copyright__ = "Copyright (c) 2014, Kenneth A. Younge"
__license__   = "GNU General Public License"
__email__     = "kenyounge@gmail.com"

import os

def crypt(data, key):
    x = 0
    box = range(256)
    for i in range(256):
        x = (x + box[i] + ord(key[i % len(key)])) % 256
        box[i], box[x] = box[x], box[i]
    x = 0
    y = 0
    out = []
    for char in data:
        x = (x + 1) % 256
        y = (y + box[x]) % 256
        box[x], box[y] = box[y], box[x]
        out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))
    return ''.join(out)

def secure(file_names=('passwords.py',),
           key_name='security.key',
           key_path='~/',
           pvt_path='_private/',
           verbose=False):
    """Transform files (encrypt and/or decrypt _private files).

    Keyword arguments:
        filenames  --  sequence of file names to encrypt/decrypt
        key_name   --  file name of your personal rc4 encryption key
        key_path   --  location of encryption key on production machines
        pvt_path   --  location of private files and encryption key during dev
        verbose    --   print info

    Defaults:
        filenames  --  passwords.py    currently a tuple with one file
        key_name   --  security.key
        key_path   --  ~/
        pvt_path   --  _private/
        verbose    --  False
    """

    # Load key (try production location first)
    if os.path.exists(os.path.join(key_path, key_name)):
        key = open(os.path.join(key_path, key_name), 'r').read()
    elif os.path.exists(os.path.join(
            os.path.dirname(__file__), pvt_path + key_name)):
        key = open(os.path.join(
            os.path.dirname(__file__), pvt_path + key_name), 'r').read()
    else:
        key = open(os.path.join(
            os.path.dirname(__file__), key_name), 'r').read()

    # secure each file
    code_lines = []
    for filename in file_names:

        filename_raw = os.path.join(
            os.path.dirname(__file__), pvt_path + filename)
        filename_rc4 = os.path.join(
            os.path.dirname(__file__),
            os.path.basename(filename).replace('.py', '.rc4'))

        # Encrypt
        try:
            if os.path.exists(filename_raw):
                with open(filename_raw, 'r') as f:
                    text = f.read()
                with open(filename_rc4, 'w') as f:
                    f.write(crypt(str(text).strip(), key).encode('hex'))
                if verbose:
                    print 'Encrypted ' + filename_raw
            else:
                if verbose:
                    print('File (' + filename_raw + ') not found')
        except Exception as e:
            print(str(e))

        # Decrypt
        try:
            if os.path.exists(filename_rc4):
                with open(filename_rc4, 'r') as f:
                    text = crypt(str(f.read()).strip().decode('hex'), key)
                    lines = [str(line).strip() for line in text.splitlines()]
                if lines: code_lines.extend(lines)
                if verbose:
                    print 'Encrypted ' + filename_rc4
            else:
                print('File ' + filename_rc4 + ' not found')
        except Exception as e:
            print(str(e))
    return code_lines

rawDescription: How can credentials be securely distributed in a cloud computing environment?


code:
file1=[['a',1,2],['a',3,4],['b',4,5],['c',3,5]]

li1=[] #first obj
li2=[] #second obj
skip_next = False
file_len = len(file1)

for current_row_num,row in enumerate(file1):
    if skip_next:
        skip_next = False
        pass
    else:
        if current_row_num == (file_len-1):
            # alone
            li2.append(row)
        elif row[0]!=file1[current_row_num+1][0]:
            #current row belongs in obj2
            li2.append(row)
        else:
            # current row has same first col value as next row
            # append both to li1
            li1.append(row)
            li1.append(file1[current_row_num+1])
            skip_next=True

print "first obj\n"+str(li1)

print "second obj\n"+str(li2)

rawDescription: how to compare two fields in python

description: Compare two fields in python


code:
print c2.blue  # 4
c2.green = 3  # assign a new value to existing green attribute

rawDescription: Does this Python class look stupid?

description: Does this Python class look stupid


code:
import re
inputString='type="NN" span="123..145" confidence="1.0" '
pat=re.compile('"([^"]*)"')
while True:
        mat=pat.search(inputString)
        if mat is None:
                break
        strings.append(mat.group(1))
        inputString=inputString[mat.end():]
print strings

rawDescription: Breaking up substrings in Python based on characters


code:
class LazyMeta(type):
    def __new__(cls, clsname, bases, clsdict):
        d = dict(clsdict)
        props = d.get("lazy_props")
        def lazy_load(_attr):
            def wrapped_method(self):
                variable = "_" + _attr
                if not hasattr(self, variable):
                    self.Load()
                return getattr(self, variable)
            return wrapped_method

        for prop in props:
            _method = lazy_load(prop)
            _property = property(_method)
            d[prop] = _property

        return type.__new__(cls, clsname, bases, d)

# Your class uses LazyMeta as metaclass
class LazyClass:
    __metaclass__ = LazyMeta
    lazy_props = ['test1', 'test2']
    def Load(self):
        print "Loading ..."
        self._test1=123
        self._test2=456

rawDescription: Batch-adding properties in Python?

description: Batch-adding properties in Python


code:
import os
import random

def synthesizeFrameAndCreateMovie(folder):

  # Create an empty movie to receive the frames
  movie = makeMovie()

  # Compute & save the frames
  w = 40
  h = 25
  nb_frames = 60       # Will give 60 frames at 30 fps => movie duration : 2 sec.
  for z in range(0, nb_frames):
    pic=makeEmptyPicture(w, h)
    for x in range (0, w):
      for y in range (0, h):
        #makeColor() takes red, green, and blue (in that order) between 0 and 255
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        color = makeColor(r,g,b)
        px = getPixel(pic, x, y)
        setColor(px, color)

    # Create one frame and inject in the movie object
    filename = os.path.join(folder, 'pic%03d.png' % z)
    writePictureTo(pic, filename)
    addFrameToMovie(filename, movie)

  # return the movie
  return movie

movie = synthesizeFrameAndCreateMovie("D:\\FOLDER")
print movie
#writeQuicktime(movie,"D:\\FOLDER\\movie.mov", 30)
playMovie(movie)

rawDescription: Creating a movie in Jython/Python

description: Creating a movie in Jython/Python


code:
def findFourPlus(itemCount, seq, goal):
    goalDifference = float("inf")
    closestPartial = []
    hello = subset_sum(itemCount, seq, goal, 0, closestPartial, partial=[])
    return hello #doesn't return value from subset_sum()

def subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial):
    s = sum(partial)

    # check if the partial sum is equals to target
    if(len(partial) == itemCount):
        if s == goal:
                print("FOUND YAA")
                return partial #right now doesn't return anything. I intend for it to break out of this function as soon as it finds one pair of solution.

        else:
            if( abs(goal - s) <= goalDifference):
                #print(abs(goal-s), goalDifference, closestPartial)
                goalDifference = abs(goal - s)
                closestPartial[:] = partial
                #print(abs(goal-s), goalDifference, closestPartial)
                return closestPartial

    for i in range(len(seq)):
        n = seq[i]
        remaining = seq[i+1:]
        t = subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n])
        print t
        if t:
            return t

a = findFourPlus(2, [1,2,3,4,5], 6)
print "a=", a

rawDescription: python can't return a variable but can print

description: Python can't return a variable but can print


code:
import sys
sys.path.remove('')
from getopt import getopt
sys.path.insert(0,'')
opts, args = getopt(sys.argv[1:], "h:s")
for key,value in opts:
    print key, "=>", value

rawDescription: python name a file same as a lib

description: Python name a file same as a lib


code:
aa = {'ALA':'A'}
test = ['ALA']  ### note this is now a list!

for i in test:
    if i in aa:
        print i, aa[i]  #### note i is the key, aa[i] is the value

rawDescription: Python: Simple Dictionary referencing Problem


code: r = re.compile(ur'\b[^\Wa]+\b', re.UNICODE)

rawDescription: Python Regex Picking "not include" word

description: Python Regex Picking "not include" word


code:
F, H, A, B, C = Bools('F H A B C')
s = Solver()
s.add(F, H, Implies(A, B))
print s

rawDescription: How to use Implies and if boolean commands in z3 python API

description: Use Implies and if boolean commands in z3 python API


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup("""

   Question 1: What is your name?
   My name is Peter.

   Question 2: What is your native language?

Esperanto

""")

for div in soup.find('body').findAll('div', recursive=False):
    question = div.find('b')
    print question.text
    print question.nextSibling.strip() or question.nextSibling.nextSibling.text.strip()

rawDescription: Python xml iterate over questions and answer


code:
import re

text = "First line\n" + \
    "Second line\n" + \
    "Important line! E-mail:mail@domain.de, Phone:991\n" + \
    "Another important line! E-mail:tom@gmail.com, Phone:001\n" + \
    "Another line"
print text

emails = re.findall("E-mail:([\w@.-]+)", text)
print "Found email(s): " + ', '.join(emails)

rawDescription: Whole words in python regular expression


code:
from datetime import date
print date(1850, 1, 1).weekday()  # 1, which is Tuesday
# (Mon is 0)

rawDescription: How to get a timestamp older than 1901

description: Get a timestamp older than 1901


code:
class BaseFilter(object):
    setters = ('SampleRate', 'Gain', 'BitDepth',)

    def set(self, method_id, value):
        print 'BaseFilter.set(): setting {} to {}'.format(method_id, value)

class Filter(BaseFilter):
    def __getattribute__(self, name):
        method_id = name.lstrip('set')
        if name.startswith('set') and method_id in super(Filter, self).setters:
            def _set(value):
                return self.set(method_id, value)
            return _set
        else:
            return super(Filter, self).__getattribute__(name)

rawDescription: How to create methods dynamically in Python ( name + code )

description: Create methods dynamically in Python ( name + code )


code:
import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum

signal.signal(signal.SIGINT, handler)
#do your stuff

rawDescription: Exit a Python process not kill it (via ssh)


code: print [ord(char) - 96 for char in raw_input('Write Text: ').lower()]

rawDescription: How to convert letters to numbers using python

description: Convert letters to numbers using python


rawDescription: Creating python dictionary with a particular column value as key and removing that column from value


code:
class MinimumCoinChangeSolver(object):
    def __init__(self):
        self.coins = None
        self.count = None
        self.__cache = {}

    def solve(self, coins, amount):
        self.reset_cache()
        self.coins = self.__recursive_solve(coins, amount)
        self.count = len(self.coins)

    def reset_cache(self):
        self.__cache = {}

    def __recursive_solve(self, coins, amount):
        # try to find solution in cache
        if amount in self.__cache:
            solution = self.__cache[amount]

        else:
            # if given amount equals to one of teh coins, return list which contains corresponding coin (it equals to amount)
            if amount in coins:
                solution = [amount]

            else:
                # find coins candidates, they are less then given amount
                coins_cands = filter(
                    lambda coin: coin < amount,
                    coins
                )

                # if there is no coins candidate, return no coins (it is an empty list on coins)
                if not coins_cands:
                    solution = []

                else:
                    # try to use every coin among the candidates, and recursively find list of coins in every case
                    solutions = map(
                        lambda coin: self.__recursive_solve(coins, amount - coin),
                        coins_cands
                    )

                    # ignoring empty solutions
                    solutions = filter(
                        lambda sol: bool(sol),
                        solutions
                    )

                    # if there is no non-empty solutons, return empty list
                    if not solutions:
                        solution = []

                    else:
                        # choose the solution which has minimum number of coins
                        min_solution = min(
                            solutions,
                            key=lambda sol: len(sol)
                        )

                        # remember corresponding coin
                        coin = amount - sum(min_solution)

                        # solution
                        solution = min_solution + [coin]

        # save solution in cache
        self.__cache[amount] = solution

        # return
        return solution

if __name__ == "__main__":
    solver = MinimumCoinChangeSolver()
    solver.solve([1, 5, 10, 25, 21], 63)
    print solver.coins
    print solver.count

rawDescription: Minimum coin change: reconstruct solution from this algorithm


code:
class A:
    def __init__(self, data):
        self.values_to_counts = {}
        for val in data:
            if val in self.values_to_counts:
                self.values_to_counts[val] += 1
            else:
                self.values_to_counts[val] = 1
    @classmethod
    def from_values_to_counts(cls, values_to_counts):
        self = cls([])
        self.values_to_counts = values_to_counts
        return self

class B(A):
    def __init__(self, data, parameter):
        super(B, self).__init__(data)
        self.parameter = parameter
    def print_parameter(self):
        print self.parameter

rawDescription: Inheritance of from_<type> in Python

description: Inheritance of from_<type> in Python


code:
#!/usr/bin/env python

import csv

value = 1.42

out = open("test.csv","rU")
dataf=csv.reader(out, delimiter=',')
matches = [float(row[0]) >= value for row in dataf]
matches.reverse()

for i,m in enumerate(matches):
    if m:
        print i

rawDescription: Find position in a csv list

description: Find position in a csv list


code:
import re
data = [[u'text'], [u'element'], [u'text00']]
patterns = [u'text', u'EL', u'00']
results = [[x[0] for x in data if re.search(pattern, x[0], flags=re.I)] for pattern in patterns]
print  results

rawDescription: Python search regex from variable inside a list


code:
def synchronized(f):
    @wraps(f)
    def wrapper(self, *args, **kwargs):
        with self.lock:
            print "here"
            return f(self, *args, **kwargs)
    return wrapper

class Synchronized(object):
    def __init__(self):
        self.lock = threading.Lock()

    @synchronized
    def go(self):
        print 1

class B(Synchronized):
    @synchronized
    def foo(self):
        return 1

rawDescription: How does Python resolve decorator names

description: How does Python resolve decorator names


code:
# Single quotes added so you can see any extra spaces in the console
print "'%s'" % state

rawDescription: Django Query doesn't match

description: Django Query doesn't match


code:
api = twitter.Api()
for page in xrange(1, 501):
    tweets = api.GetSearch("#Burma", page=page, per_page=100)
    for t in tweets:
        print t.text

rawDescription: Result duplication while crawling Twitter


code:
#!/usr/bin/python

import logging
import subprocess
import sys

def forever():
    CMD = [sys.executable, 'infinit.py']
    while 1:
        try:
            print 'running new instance of:'
            print ' '.join(CMD)

            popen = subprocess.Popen(CMD, stdout=subprocess.PIPE)
            for line in iter(popen.stdout.readline, ""):
                print line,

        except Exception as e:
            print 'Something bad happend'
            logging.error(e)
        finally:
            print

if __name__ == '__main__':
    forever()

rawDescription: How to get subprocess output during its running?

description: Get subprocess output during its running


code:
#! python
for line in open('/var/log/apache2/access.log'):
        ip = line.split(' ')[0]
        print ip

rawDescription: Trying to parse source IP's from apache access log and write the output to a file but only 1 ip is written


code:
# Repeats the character 'A' 5 times
print "A"*5

rawDescription: How can I create a decremented numberPyramid(num) in Python?

description: Create a decremented numberPyramid(num) in Python


code:
import requests
sess = requests.session()
sess.post("http://somesite.com/someform.php", data={"username": "me", "password": "pass"})
#Everything after that POST will retain the login session
print sess.get("http://somesite.com/otherpage.php").text

rawDescription: Python how to preserve HTTP cookies

description: Python how to preserve HTTP cookies


code:
import re
print re.sub('<<<(.*?)>>>', lambda L, nt=led_coeffs: str(getattr(nt, L.group(1))), your_string)

rawDescription: Replacing or substituting in a python string does not work


code: for i in get_string(r"'Hello World!'"): print i

rawDescription: Python find python strings in string

description: Python find python strings in string


code:
string = 'I love my           world of dreams'
print re.search (r'(?<=my) +([^ -.]*)', string).group(1)

rawDescription: Regular expression to find a word after multiple spaces


code:
if find in things:
    print things[find]

rawDescription: More efficient way of getting things from a dictionary


code:
from xml.dom import minidom
import xml.etree.cElementTree as ET

root = ET.Element("root")

doc = ET.SubElement(root, "doc")

field1 = ET.SubElement(doc, "field1")
field1.set("name", "blah")
field1.text = "some value1"

field2 = ET.SubElement(doc, "field2")
field2.set("name", "asdfasd")
field2.text = "some vlaue2"

dom = minidom.parseString(ET.tostring(root))
print dom.toprettyxml(indent='\t')

rawDescription: how to insert tabs instead of spaces when creating xml files in python

description: Insert tabs instead of spaces when creating xml files in python


code: print map("".join, zip(*[iter(data)] * 2))

rawDescription: Best way to regroup a string into a list of 2-character string

description: Regroup a string into a list of 2-character string


code:
#!/usr/bin/env python
import shlex
from subprocess import Popen, PIPE

p = Popen(shlex.split('unrar.exe x -y myfile.rar'), stdout=PIPE, bufsize=1)
count = 0
for line in iter(p.stdout.readline, b''):
    count += 1
    print count
p.stdout.close()
p.wait()

rawDescription: Work with the stdout of some other process (created with Popen)


code:
for ctr in range(6):
    print divmod(ctr, 2)

rawDescription: Iteration to place widgets in Tkinter and Pmw


code:
pool = Pool(4)
print pool.map(final_op, (add, sub, div, mul))

rawDescription: Asynchronously apply list of python functions to single argument


code:
import getpass

credentials = {}                                                            ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f:                                       ## Opens the file and reads it
    for line in f:  ## For each line
        username, delim, password = line.strip().partition(':')                        ## Separate each line into username and password, splitting after a colon
        credentials[username] = password.split(';')                                    ## Links username to password

while True:
    username = raw_input("Please enter your username: ")                    ## Asks for username
    if username in credentials:                                           ## If the username is in the credentials array
        while True:
            password = getpass.getpass("Please enter your password: ")      ## Asks for password
            if password == credentials[username][0]:
                print "Logged in successfully as " + username               ## Log in
                break
            elif password in credentials[username]:                         ## If the password is linked to the username
                print "Specific error message " + username               ## Log in
            else:
                print "Password incorrect!"
        break
    else:
        print "Username incorrect!"

rawDescription: Python Login Script; multiple passwords per account


code: print '\n'.join('{} \t {}'.format(aa,bb) for aa,bb in zip(a,b))

rawDescription: Combine two lists into a single dictionary or Print them (shortest way)


code:
file_paths = [os.path.join(root, f) for root, _, files in os.walk('.') for f in files]
for path in file_paths:
    print path
    shutil.copy(path, target)

rawDescription: Copying recursively files in Python

description: Copying recursively files in Python


code:
for k in my_list:
    if k in my_dict:
        print k, my_dict[k]

rawDescription: Dictionary keys match on list; get key/value pair


code:
if not name.startswith('doc'):
     print name

rawDescription: In python, how do I exclude files from a loop if they begin with a specific set of letters?


code:
def pretty_print(item_list, indentation = ""):

        temp = []

        for items in item_list:
            if isinstance(items, list):
                temp.append( pretty_print(items, indentation = indentation+"[----]"))
            else:
                temp.append( indentation + str(items) + '\n')

        return ( ''.join(temp ) )

print pretty_print( [1,2,3,[1,2,3], 1,[1,2,[1,2, ['a', 'this is outer'],3], 3], 2,3] )

rawDescription: pretty print a list using recurison returning single item in a list

description: Pretty print a list using recurison returning single item in a list


code:
from geopy.geocoders import GoogleV3
def get_lat_lng(zip_code):
    lat, lng = False, False
    geocoder = GoogleV3()
    location = geocoder.geocode(query=str(zip_code), exactly_one=False)
    if location:
        print [loc.address for loc in location]
print(get_lat_lng(54000))

rawDescription: geopy does not show correct output

description: Geopy does not show correct output


code:
data.sort(key=lambda x:order.index(x[0]))
print data
[('name', '<object>'), ('email', '<object>'), ('phone', '<object>'), ('zip', '<object>')]

rawDescription: Order list of tuples based on a list of strings


code:
import tweepy
import MySQLdb

conn = MySQLdb.connect(
            host = 'localhost',
            user = 'root',
            passwd='',
            db='python',
            charset='utf8',
            use_unicode=True
            )
x = conn.cursor()

consumer_key=""
consumer_secret=""

access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()

    def on_status(self, status):

        print status.text,'\n'
        print status.user.screen_name, "\n"
        print status.created_at, "\n"
        if status.place:
            print status.place.full_name
            x.execute(
            "INSERT INTO tweeter (time, username, tweet,location) VALUES (%s,%s,%s,%s)",
            (status.created_at, status.user.screen_name, status.text,status.place.full_name))
            conn.commit()

        else:
            x.execute(
            "INSERT INTO tweeter (time, username, tweet,location) VALUES (%s,%s,%s,%s)",
            (status.created_at, status.user.screen_name, status.text,"None"))
            conn.commit()

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True

sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=["earthquake"])

rawDescription: Get location using json and tweepy?

description: Get location using json and tweepy


code:
x = [[[1,2],3],[[3,4],5], [[1,2],3]]
x = tuple(tuple(tuple(j) if isinstance(j, list) else j for j in i) for i in x)
from collections import OrderedDict
print [[list(j) if isinstance(j, tuple) else j for j in i] for i in OrderedDict.fromkeys(x).keys()]
# [[[1, 2], 3], [[3, 4], 5]]

rawDescription: Uniqueness in a list of lists with lists


code:
from PyQt4 import QtGui
import PyQt4.uic
import sys

class TestController(QtGui.QWidget):

    def __init__(self):
        super(TestController, self).__init__()

        # load view
        uifile = '/path/to/some/widget.ui'
        PyQt4.uic.loadUi(uifile, self)

        # create connections (assuming there is a widget called 'btn' that is loaded)
        self.btn.clicked.connect(self.buttonClicked)

    def buttonClicked(self):
        print 'clicked'

def main():
    app = QtGui.QApplication(sys.argv)
    view = TestController()
    view.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: How to connect a signal from the controller in PyQt4? (iOS like MVC structure in PyQt4)

description: Connect a signal from the controller in PyQt4? (iOS like MVC structure in PyQt4)


code:
import threading
from collections import deque
import time
import psutil

def calc_ul_dl(rate, dt=3, interface='WiFi'):
    t0 = time.time()
    counter = psutil.net_io_counters(pernic=True)[interface]
    tot = (counter.bytes_sent, counter.bytes_recv)

    while True:
        last_tot = tot
        time.sleep(dt)
        counter = psutil.net_io_counters(pernic=True)[interface]
        t1 = time.time()
        tot = (counter.bytes_sent, counter.bytes_recv)
        ul, dl = [(now - last) / (t1 - t0) / 1000.0
                  for now, last in zip(tot, last_tot)]
        rate.append((ul, dl))
        t0 = time.time()

def print_rate(rate):
    try:
        print 'UL: {0:.0f} kB/s / DL: {1:.0f} kB/s'.format(*rate[-1])
    except IndexError:
        'UL: - kB/s/ DL: - kB/s'

# Create the ul/dl thread and a deque of length 1 to hold the ul/dl- values
transfer_rate = deque(maxlen=1)
t = threading.Thread(target=calc_ul_dl, args=(transfer_rate,))

# The program will exit if there are only daemonic threads left.
t.daemon = True
t.start()

# The rest of your program, emulated by me using a while True loop
while True:
    print_rate(transfer_rate)
    time.sleep(5)

rawDescription: Get upload/download kbps speed

description: Get upload/download kbps speed


code:
def foo(bar, spam='eggs', foobarred={}):
    foobarred[bar] = spam
    print foobarred

documented = add_docs(foo)
help(documented)
documented('monty', 'python')
documented('Eric', 'Idle')

rawDescription: Python dynamic help and autocomplete generation


code:
df1 = df.copy()

def a(df):
    df = df.astype(str);
    df['Month'] = df['Month'].str.zfill(2);
    df['Day'] = df['Day'].str.zfill(2);
    return df

print df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
print a(df)

rawDescription: Append based on length of string

description: Append based on length of string


code:
import re

with open('input.txt', 'r') as f_input:
    data = f_input.read()
    if re.search(r'(mutant\s.*?\scauses.*?GS)', data, re.S):
        print 'found'

rawDescription: Searching for specific phrase pattern within lines. python


code:
darkblue = ['first', 'second']
teal = ['first-teal', 'second-teal']
pink = ['first-pink', 'second-pink']
# etc...

for (color,value) in colors:
    print color #teal, pink, red, etc...
    for val in locals()[color]:
        print val #first-teal, second-teal, first-pink, second-pink, etc...

rawDescription: python help listing values in nested for loop

description: Python help listing values in nested for loop


code:
from bs4 import BeautifulSoup
import requests

def extract_blogs(content):
    first_page = BeautifulSoup(content)
    for link in first_page.select('div.directory-blog img'):
        print link.get('title')

# extract blogs from the main page
response = requests.get('http://hypem.com/blogs')
extract_blogs(response.content)

# paginate over rest results until there would be an empty response
page = 2
url = 'http://hypem.com/inc/serve_sites.php?featured=true&page={page}'

while True:
    response = requests.get(url.format(page=page))
    if not response.content.strip():
        break
    extract_blogs(response.content)
    page += 1

rawDescription: How to parse text in websites that display additional text after clicking a button, but that text is not in the base html

description: Parse text in websites that display additional text after clicking a button, but that text is not in the base html


code:
import time
from threading import Thread

class Monitor(object):

    def __init__(self):
        self._stop = False

    def run(self, inputFile, secs=3):
        self._stop = False

        with open(inputFile) as monitor:

            while True:
                line = monitor.readline().strip()
                if line.isdigit():

                    # this is where you would notify somehow
                    print int(line)

                time.sleep(secs)

                if self._stop:
                    return

    def stop(self):
        self._stop = True

if __name__ == "__main__":

    inputFile = "write.txt"

    monitor = Monitor()

    monitorThread = Thread(target=monitor.run, args=(inputFile, 1))
    monitorThread.start()

    try:
        while True:
            time.sleep(.25)

    except:
        monitor.stop()

rawDescription: Play Different Video Files Depending On Value of a Variable At Runtime


code:
c = C()
c.a
c.a = 3
print c.__dict__['a']
print C.__dict__['a']
del c.a
c.a

rawDescription: Python descriptors not working in Python 2.7


code:
counter = 1
suffixes = {1: 'st', 2:'nd', 3:'rd'}
for key, group in itertools.groupby(the_sequence):
    if key == 0:
        print 'The length of {}{} sequence of zeros is: {}'.format(counter,
                                                                   suffixes.get(counter, 'th'),
                                                                   len(tuple(group)))
        counter += 1

rawDescription: Finding the length of each set of successive values using python or excel


code:
import pandas as pd

#read subset from csv and remove duplicate indices
subset = pd.read_csv('8kx1.csv', index_col=[0]).index.unique()
print subset

#use subset as filter of columns
tp = pd.read_csv('input.csv',iterator=True, chunksize=1000, usecols=subset)
df = pd.concat(tp, ignore_index=True)

print df.head()
print df.shape

#write to csv
df.to_csv('output.csv',iterator=True, chunksize=1000)

rawDescription: How to improve the code with more elegant way and low memory consumed?

description: Improve the code with more elegant way and low memory consumed


code:
import csv
with open('mydata.txt', 'rb') as f:
    for row in csv.DictReader(f, delimiter='\t'):
        print row

rawDescription: Initialize/Create/Populate a Dict of a Dict of a Dict in Python


code:
from z3 import *
x = Int('x')
y = Int('y')
s = Solver()
try:
    f = open("read.txt","r")
    try:
        for l in f:
            s.add(eval(l))
    finally:
        f.close()
except IOError:
    pass
print s.check()
print s.model()

rawDescription: what form of input does z3 smt solver take??can we use a file to read the equation required to be solved?if yes please tell how?

description: What form of input does z3 smt solver take??can we use a file to read the equation required to be solved?if yes please tell how


code:
def func():
    print 'hello'
    return

rawDescription: Python: "return" in functions

description: Python: "return" in functions


code:
source
import glob, multiprocessing, os

def proc(path):
    """
    Create AudioAnalysis Objects from directory and call object_analysis().
"""
    # audiofile = audio.LocalAudioFile(path) # hungry!
    return 'woot: {}'.format(path)

if __name__=='__main__':  # required for Windows
    pool = multiprocessing.Pool()   # one Process per CPU
    for output in pool.map(proc, [
            os.path.abspath(name) for name in glob.glob('q*.py')
            ]):
        print 'output:',output

rawDescription: Exchange Time for Memory in Python

description: Exchange Time for Memory in Python


code:
three_skip_trigrams = list(skipgrams(text, n=3, k=3))
three_skip_fourgrams = list(skipgrams(text, n=4, k=3))
four_skip_fourgrams  = list(skipgrams(text, n=4, k=4))
four_skip_fivegrams  = list(skipgrams(text, n=5, k=4))
print len(three_skip_trigrams), three_skip_trigrams
print len(three_skip_fourgrams), three_skip_fourgrams
print len(four_skip_fourgrams), four_skip_fourgrams
print len(four_skip_fivegrams), four_skip_fivegrams

rawDescription: How to compute skipgrams in python?

description: Compute skipgrams in python


code: print [ch for ch in data[data.index('Ph'):].partition('Dets=')[0].split(' ', 1)[1] if ch != ' ']

rawDescription: Find specific character with python regex


code:
# lines in file2 common with file1
data1=map(str.rstrip,open("file1").readlines())
for line in open("file2"):
    line=line.rstrip()
    if line in data1:
        print line

# lines in file2 not in file1, use "not"
data1=map(str.rstrip,open("file1").readlines())
for line in open("file2"):
    line=line.rstrip()
    if not line in data1:
        print line

rawDescription: Convert Perl script to Python: dedupe 2 files based on hash keys


rawDescription: How to access sparse matrix elements?

description: Access sparse matrix elements


code:
for cnt in range(10):
    print "%d + 1 = %d".format(cnt, cnt + 1)

rawDescription: Using a variable as both an integer and a string (Python)


rawDescription: How to iterate two successive elements of the lists of lists in python ?

description: Iterate two successive elements of the lists of lists in python


code:
print numpy.amax(numpy.unique(all_data[:, 3].astype(int)))
print numpy.amin(numpy.unique(all_data[:, 3].astype(int)))

rawDescription: numpy.unique doesn't give the expected output

description: Numpy.unique doesn't give the expected output


code:
def f():pass
print f()

rawDescription: multiples sequence using range

description: Multiples sequence using range


code:
s = "AppleStrawberryBananaCitrusOrange"
print s

rawDescription: create and insert randomly duplicates

description: Create and insert randomly duplicates


code:
if e.key() == Qt.Key_Backspace:
    print 'The backspace key was pressed'

rawDescription: How to handle keyboard shortcuts using keyPressEvent()? And should it be used for it?

description: Handle keyboard shortcuts using keyPressEvent()? And should it be used for it


code:
Example
from datetime import datetime, timedelta

# dummy data
duration = timedelta(minutes=10)
start = datetime.utcnow()
end = start + timedelta(hours=16)

# use list instead of tee(), islice() for simplicity
lst = [dt.strftime('%H:%M') for dt in time_range(start, end, duration)]
for interval in zip(lst, lst[1:]):
    print "%s-%s," % interval,
print

rawDescription: Difference between two time intervals in series


code: print x[1]

rawDescription: accessing 2d numpy arrays

description: Accessing 2d numpy arrays


code:
import urllib2, json
response = urllib2.urlopen('https://www.udemy.com/api-2.0/channels/1640/courses?is_angular_app=true&p=2')
data = json.load(response)

i = 0;
for data["url"] in data['results']:
    print data['results'][i]['url']
    i+=1

rawDescription: extract key value from json python

description: Extract key value from json python


code:
for i in range(5):
    for p in itertools.product(('0', '1', '2'), repeat=i):
        print ''.join(p),
print

rawDescription: Number permutations in python iterative

description: Number permutations in python iterative


code:
flags = {True: 'abc', False: 'de'}
flags = dict(invert_dict(flags))
print flags

rawDescription: More elegant way of declaring multiple variables at the same time


code:
for group, items in itertools.groupby('aaAaAAbBbcCdDdDeE', lambda x: x.lower()):
    print group, list(items)

rawDescription: Regarding the correct usage of groupby(): Python 3


code:
import pbs
print pbs.nfc_mfclassic("r", "a", "dumptest.mfd")

rawDescription: How to use subprocess module in python

description: Use subprocess module in python


code:
import csv

f1 = open ("olddata/olddata.csv")
oldFile1 = csv.reader(f1, delimiter='\t')
oldList1 = list(oldFile1)

f2 = open ("newdata/newdata.csv")
newFile2 = csv.reader(f2, delimiter='\t')
newList2 = list(newFile2)

f1.close()
f2.close()

output1 =  [row for row in newList2 if row not in oldList1]
output2 =  [row for row in oldList1 if row not in newList2]

print output1 + output2

rawDescription: Compare 2 csv files and output different rows to a 3rd CSV file using Python 2.7


code:
def test_var_kwargs(farg, **kwargs):
    print "formal arg:", farg
    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

test_var_kwargs(farg=1, myarg2="two", myarg3=3)

rawDescription: Having more than one parameter with def in python


code:
print max, max_letter
print min, min_letter

rawDescription: Program that counts most and least common non-space characters


code:
# the advantage of using `Next` is it is web text based which is more reliable.
import urllib
from bs4 import BeautifulSoup
import re
url = 'http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-1-0-0-0-0.html'
pageurl = urllib.urlopen(url)
soup = BeautifulSoup(pageurl)
print soup.find('ul',{'class':'div_pages'}).find(text=re.compile("Next")).find_parent('a')['href']

rawDescription: Why <ul> tag does not give any output?

description: Why <ul> tag does not give any output


code:
import wikipedia

page = wikipedia.page("List_of_works_by_Vincent_van_Gogh")
print page.links

rawDescription: Wikipedia API JSON with Python

description: Wikipedia API JSON with Python


code:
import myDict

list_name = 'wallet'
print getattr(myDict, list_name)

rawDescription: Print variable from other file with raw_input


code:
import csv

word_list = []
number_list = []

with open("file.txt", 'r') as f:
    reader = csv.reader(f)
    for line in reader:
        if line[0].strip().isdigit():
            number_list.append(line)
        else:
            word_list.append(line)

print number_list
print word_list

rawDescription: Checking next Line to then implement where the line should go to


code:
import sys

sentinel = object()

class FakeLoader(object):
  def find_module(self, fullname, path=None):
    return self
  def load_module(*_):
    return sentinel

def fakeHook(apath):
  if apath == 'GIVINGUP!!!':
    return FakeLoader()
  raise ImportError

sys.path.append('GIVINGUP!!!')
sys.path_hooks.append(fakeHook)

def isModuleOK(modulename):
  result = __import__(modulename)
  return result is not sentinel

print 'sys', isModuleOK('sys')
print 'Cookie', isModuleOK('Cookie')
print 'nonexistent', isModuleOK('nonexistent')

rawDescription: Check for a module in Python without using exceptions


code:
import re

def some_func():
    settings = {'result_dir':'dir'}
    args = {'run_id':'id'}

    vars = locals()

    print re.sub(r'\$(\w+)(\.(\w+))?', lambda m: vars[m.group(1)][m.group(3)], '$settings.result_dir + $args.run_id')

some_func()

rawDescription: How to extract all substrings between certain characters by using regular expression in Python?

description: Extract all substrings between certain characters by using regular expression in Python


code:
foos = [1.0,2.0,3.0,4.0,5.0]
bars = [1,2,3]

def maptest(foo):
    print foo, bars

map(maptest, foos)

rawDescription: Using python map and other functional tools


code:
lines = open('nyt.txt')
words = []
for line in lines:
    words.extend(line.split())
print words

rawDescription: manipulating and searching within large files in python

description: Manipulating and searching within large files in python


code:
animal_list = ['cat', 'elephant']
your_list = ['dog', 'horse', 'cat', 'shark', 'cancer', 'elephant']
res = map(lambda item: item in animal_list, your_list)
print res

rawDescription: List of Elements to Boolean Array

description: List of Elements to Boolean Array


rawDescription: Generating recurring dates using python?

description: Generating recurring dates using python


code:
from dateutil import parser
from dateutil import tz
dt = parser.parse('2014-02-06 13:30:00-05:00')
print dt.astimezone(tz.tzutc()).isoformat(' ')

rawDescription: Python: time conversion

description: Python: time conversion


rawDescription: concatenate and remove duplicate element from arrays

description: Concatenate and remove duplicate element from arrays


code:
class hidden(object): # or whatever its parent class is
    def __init__(self):
        self.value = 4
    def show(self):
        return self.value
    def change(self,n):
        self.value = n
    def __getattr__(self, attrname):
        # put the dep'd method/attribute names here
        deprecateds = ['dep_show','dep_change']
        if attrname in deprecateds:
            print("These aren't the methods you're looking for.")
            def dep_change(n):
                self.value = n
            def dep_show():
                return self.value
            return eval(attrname)
        else:
            raise AttributeError, attrname

rawDescription: Hide deprecated methods from tab completion


code:
import argparse

parser = argparse.ArgumentParser(description='View arguments')
parser.add_argument('arguments', nargs='+', help='passed arguments')
args = parser.parse_args()
print args.arguments

rawDescription: Writing and documenting a Python script with variable number of arguments


code:
payload = {'f':'set_email_user', 'email_user': 'sark'}
req2 = s.post('http://api.guerrillamail.com/ajax.php', data=payload)
print req2.text #prints out the current email address

rawDescription: Pass JSON function arguements using requests


code:
data1 = set(l.rstrip() for l in open(raw_input("Which is list1?")))
data2 = set(l.rstrip() for l in open(raw_input("Which is list2?")))
for x in data1 - data2:
     print x

rawDescription: Simple Nested loop with Python

description: Simple Nested loop with Python


code:
import re

tgt='''\
section aaa:
   some values
   value 5
section bbb:
   more values
   value 6
section ccc:
   some values
   value 5
section ddd:
   more values
   value 6'''

pat=re.compile(r'^section (\w+):.*?value (\d+)',flags=re.MULTILINE|re.DOTALL)

print [(m.group(1),m.start(),m.end())
          for m in pat.finditer(tgt)
          if m.group(2)=='6']

rawDescription: Regex: Matching the closest value backwards


rawDescription: How to interpolate using nearest neighbours for high dimension numpy python arrays

description: Interpolate using nearest neighbours for high dimension numpy python arrays


rawDescription: Python Regular Expression of long complex string


code:
import pandas as pd
df = pd.DataFrame({'x': [1, 1, 2, 2, 1, 1], 'y':[1, 2, 2, 2, 2, 1]})
print pd.factorize(pd.lib.fast_zip([df.x, df.y]))[0]

rawDescription: multi-column factorize in pandas

description: Multi-column factorize in pandas


code:
import numpy as np
import itertools

def ranges(s):
    """ Converts a list of integers into start, end pairs """
    for a, b in itertools.groupby(enumerate(s), lambda(x, y): y - x):
        b = list(b)
        yield b[0][1], b[-1][1]

def intersect(*args):
    """ Converts any number of numpy arrays containing start, end pairs
        into a set of indexes """
    s = set()
    for start, end in np.vstack(args):
        s = s | set(range(start,end+1))
    return s

a = np.array([[5,7],[12, 18],[20,29]])
b = np.array([[2,4],[8,11],[33,35]])

result = np.array(list(ranges(intersect(a,b))))

rawDescription: numpy: How to join arrays? ( to get the union of several ranges)

description: Numpy: How to join arrays? ( to get the union of several ranges)


code:
#!/usr/bin/env python
from __future__ import division
import numpy as np
from matplotlib import pyplot as plt

# Sample data with two peaks: small one at t=0.4, large one at t=0.8
ts = np.arange(0, 1, 0.01)
xs = np.exp(-((ts-0.4)/0.1)**2) + 2*np.exp(-((ts-0.8)/0.1)**2)

# Say we have an approximate starting point of 0.35
start_point = 0.35

# Nearest index in "ts" to this starting point is...
start_index = np.argmin(np.abs(ts - start_point))

# Find the local maxima in our data by looking for a sign change in
# the first difference
# From http://stackoverflow.com/a/9667121/188535
maxes = (np.diff(np.sign(np.diff(xs))) < 0).nonzero()[0] + 1

# Find which of these peaks is closest to our starting point
index_of_peak = maxes[np.argmin(np.abs(maxes - start_index))]

print "Peak centre at: %.3f" % ts[index_of_peak]

# Quick plot showing the results: blue line is data, green dot is
# starting point, red dot is peak location
plt.plot(ts, xs, '-b')
plt.plot(ts[start_index], xs[start_index], 'og')
plt.plot(ts[index_of_peak], xs[index_of_peak], 'or')
plt.show()

rawDescription: These spectrum bands used to be judged by eye, how to do it programmatically?


code:
sheet1 = pd.Series([3,2,1], index=['Bread', 'Milk', 'Eggs'], name='Values_sheet1')
sheet2 = pd.Series([6,2,11], index=['Bread', 'Cheese', 'Salad'], name='Values_sheet2')

# concatenate the two series and fill in the missing data with zeros
result = pd.concat([sheet1,sheet2], axis=1).fillna(0)

print result

rawDescription: Reformatting data imported from an excel file in pandas


code:
import sys
import os
import urllib
import threading
from Queue import Queue

class DownloadThread(threading.Thread):
    def __init__(self, queue, destfolder):
        super(DownloadThread, self).__init__()
        self.queue = queue
        self.destfolder = destfolder
        self.daemon = True

    def run(self):
        while True:
            url = self.queue.get()
            try:
                self.download_url(url)
            except Exception,e:
                print "   Error: %s"%e
            self.queue.task_done()

    def download_url(self, url):
        # change it to a different way if you require
        name = url.split('/')[-1]
        dest = os.path.join(self.destfolder, name)
        print "[%s] Downloading %s -> %s"%(self.ident, url, dest)
        urllib.urlretrieve(url, dest)

def download(urls, destfolder, numthreads=4):
    queue = Queue()
    for url in urls:
        queue.put(url)

    for i in range(numthreads):
        t = DownloadThread(queue, destfolder)
        t.start()

    queue.join()

if __name__ == "__main__":
    download(sys.argv[1:], "/tmp")

rawDescription: Asynchronous file downloads in Python

description: Asynchronous file downloads in Python


code:
import re

def successive_matches(pattern,text,pos=0):
  ptrn = re.compile(pattern)
  match = ptrn.match(text,pos)
  while match:
    yield match.group()
    if match.end() == pos:
      break
    pos = match.end()
    match = ptrn.match(text,pos)
  if pos < len(text) - 1:
    yield text[pos:]

for matched_text in successive_matches(r"('[^']*'|[^',]*),\s*","21, 2, '23.5R25 ETADT', 'description, with a comma'"):
    print matched_text

rawDescription: Anchor to End of Last Match

description: Anchor to End of Last Match


rawDescription: From Matlab to Python - Solve function

description: From Matlab to Python - Solve function


code:
for n in nodes:
    print n

rawDescription: using python in maya do for-loop through selected nodes

description: Using python in maya do for-loop through selected nodes


code:
import geopandas as gpd
from shapely.geometry import LineString, Point

r1=LineString([(-1,2),(3,2.5)])
r2=LineString([(-1,4),(3,0)])
roads=gpd.GeoDataFrame(['Main St','Spruce St'],geometry=[r1,r2], columns=['Name'])

c1=LineString(Point(1,2).buffer(.5).exterior)
c2=LineString(Point(1,2).buffer(.75).exterior)
c3=LineString(Point(1,2).buffer(.9).exterior)
contours=gpd.GeoDataFrame([100,90,80],geometry=[c1,c2,c3], columns=['Elevation'])

columns_data = []
geoms = []
for _, n, r in roads.itertuples():
    for _, el, c in contours.itertuples():
        intersect = r.intersection(c)
        columns_data.append( (n,el) )
        geoms.append(intersect)

all_intersection = gpd.GeoDataFrame(columns_data, geometry=geoms,
                    columns=['Name', 'Elevation'])

print all_intersection

rawDescription: Intersection of Two LineStrings Geopandas


code:
sentences = ['The girls are gorgeous', "I'm mexican"]
from collections import Counter
print Counter(item for items in sentences for item in items.split())
# Counter({'mexican': 1, 'girls': 1, 'are': 1, 'gorgeous': 1, "I'm": 1, 'The':1})

rawDescription: How to create a list from splitting a list elements in python?

description: Create a list from splitting a list elements in python


code:
from PySide.QtCore import *
from PySide.QtGui import *
import sys

import pyside # my code generated by QT Design

class MainDialog(QMainWindow, pyside.Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainDialog,self).__init__(parent)
        self.setupUi(self)

        # use new-style signals & slots!
        self.Connect_buttom.clicked.connect(self.get_fb_token)

    def get_fb_token(self):
        email = self.FB_username.text()
        password = self.FB_password.text()

        print email
        print passwd

app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()

rawDescription: QT python connect passing arguments to function


code:
print df.index
PeriodIndex(['2015-11'], dtype='int64', name=u'', freq='M')

d = "2015-09-01"
d1 = "2015-10-01"
print df.index.isin([pd.to_datetime(d).to_period('M'), pd.to_datetime(d1).to_period('M')])
[False]

d = "2015-11-01"
d1 = "2015-11-01"
print df.index.isin([pd.to_datetime(d).to_period('M'), pd.to_datetime(d1).to_period('M')])
[ True]

rawDescription: Check if a pandas.Timestamp is in a pandas.Period


rawDescription: Statistical counting in Python

description: Statistical counting in Python


code:
a = "abc def,ghi remove - this keep-that foo + bar +keep!"
import re
print re.sub(r'[^\w\s+-]|(?<!\w)-(?!\w)|\+(?!\w)', '', a)
#abc defghi remove  this keep-that foo  bar +keep

rawDescription: Invert alternation regex

description: Invert alternation regex


code:
def my_abs(value):
    if value >= 0:
        return value
    else:
        return -value

print my_abs(-7)  ##-7
print my_abs(2)  ##2

rawDescription: Basic Python. "Boolean-valued functions"


code:
prices = {k:v for items in [fruits, veggies, drinks] for k, v in items}
fru,veg,dri=[i[0] for i in fruits],[i[0] for i in veggies],[i[0] for i in drinks]

from itertools import combinations, product, chain
for items in product(*(combinations(i, r = 2) for i in (fru, veg, dri))):
    total = sum(prices[i] for item in items for i in item)
    if 12 <= total <= 14:
        print tuple(chain.from_iterable(items)) + (total,)

rawDescription: python - check if permutation exists/combination is unique

description: Python - check if permutation exists/combination is unique


code:
def vigenere(row, column):
    '''
    Return a character from a vigenere square by
    row and column letter.
    vigenere('L', 'G') returns 'R'
    '''
    alpha = string.ascii_uppercase
    rowindex = alpha.find(row)
    columnindex = alpha.find(column)
    return vigsquare()[rowindex*26 + columnindex]

print vigsquare(printable=True)
vigenere('L', 'G')

rawDescription: For Kasiski Test,How to implement 26x26 table in Python


code:
print a1 is a2  # False
print(__dict.get(a1)) # Node(key=A,weight=1)
print(__dict.get(a2)) # None

rawDescription: is Dict.get in Python use is to compare keys in dict?

description: Is Dict.get in Python use is to compare keys in dict


code:
def value(line):
    if line.startswith("Game="):
        return 0
    elif line.startswith("Sports="):
        return 2
    else:
        return 1

text = """I like to play all games and sports
Game=chess
Sports=Baseball
I also like to play other games
Game=carrom
Sports=cricket
Game=tennis"""

lines = text.split("\n")
lines.sort(key=value)
print "\n".join(lines)

rawDescription: parse to add string to head and foot

description: Parse to add string to head and foot


code:
from BeautifulSoup import BeautifulSoup
import urllib2

pageSource=urllib2.urlopen('http://www.livescore.com/worldcup/tables/').read()

soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"class":"league-wc table bh"} )

results=[]
for table in alltables:
    rows = table.findAll('tr')
    _table = []
    for tr in rows[1:]:
        _row = []
        cols = tr.findAll('td')
        for td in cols:
            if td.findAll('a'):
                text=td.a.renderContents().strip()
            else:
                text=td.renderContents().strip()
            _row.append(text)
        _table.append(_row)
    results.append(_table)

# print results
index = 1
for table in results:
    for row in table:
        print ','.join([str(index)] + row[1:])
        index += 1

rawDescription: how do I separate each tr in a table in python?

description: Separate each tr in a table in python


code:
from lxml import etree
from io import StringIO

parser = etree.HTMLParser()
tree   = etree.parse(StringIO(html), parser)
hrefs = tree.xpath('//li[@class="dropdown" and a[starts-with(.,"TEXT_1")]]/ul[@class="dropdown-menu"]/li/a/@href')

print hrefs

rawDescription: Python nested html tags with Beautifulsoup


code:
import fileinput, sys, csv

with open('fileB', 'rb') as file_b:
    r = csv.reader(file_b)
    next(r) #skip header
    seen = {(row[0], row[2]) for row in r}

f = fileinput.input('fileA', inplace=True) # sys.stdout is redirected to the file
print next(f), # write header as first line

w = csv.writer(sys.stdout)
for row in csv.reader(f):
   if (row[0], row[2]) in seen: # write it if it's in B
       w.writerow(row)

rawDescription: How to Delete Rows CSV in python

description: Delete Rows CSV in python


code:
def main():
    rains = []
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    for month in range(12):
        rain = int(raw_input('Enter the amount of rain for ' + months[month] + ': '))
        print(rain)
        rains.append(rain)
    print rains

main()

rawDescription: Input looping through a list

description: Input looping through a list


code:
from dateutil import parser

d1 = parser.parse("Fri, 16 Oct 2015 18:06:37 +0530")
d2 = parser.parse("Thu, 1 Oct 2015 17:12:00 +0530")

print d1-d2
print (d1-d2).days

rawDescription: Date formats and its difference in python


rawDescription: Regex: Why do empty strings get included (in a list of tuples) in re.findall()?


code:
class Everything(object):
    def __contains__(self, other):
        return True

everything = Everything()

print 212134 in everything
print everything in everything
print None in everything
for i in everything:
    print i # TypeError.

rawDescription: A Python "Everything" keyword that always returns True for membership tests


code:
a = 1 #a references the integer object 1
b = a #b and a reference the same object
a = 2 #a now references a new object (2)
print b # prints 1 because you changed what a references, not b

rawDescription: python byRef // copy

description: Python byRef // copy


code: print [i+ j for i in list1 for j in list2]

rawDescription: Element to Element addition of two lists in python


code:
from django.db import models
from django.db.models import signals

def create_badge(sender, instance, created, **kwargs):
    print "Post save emited for", instance

class BadgeCategory(models.Model):
    name = models.CharField(max_length=100)

class Badge(models.Model):

    name = models.CharField(max_length=16, help_text="Name for Badge")
    category = models.ForeignKey(BadgeCategory, help_text="Category for badge")
    description = models.CharField(max_length=32, help_text="A brief description")

signals.post_save.connect(create_badge, sender=Badge)

rawDescription: django post_save call from within sending Model?

description: Django post_save call from within sending Model


code:
from dateutil import rrule
from datetime import datetime, timedelta

now = datetime.now()
hundredDaysLater = now + timedelta(days=100)

for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, until=hundredDaysLater):
    print dt

rawDescription: How to iterate over a timespan after days, hours, weeks and months in Python?

description: Iterate over a timespan after days, hours, weeks and months in Python


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_list', nargs="+", default=[], help='Input list of cell names')
parser.add_argument('-o', '--output', help='Output file destination/name')
cli = parser.parse_args()
print cli

rawDescription: Error with argparse

description: Error with argparse


code:
for product in products:
    print product.xpath('.//tr[contains(td[1], "Merk")]/td[2]/a/text()').extract()

rawDescription: Scrapy scraping content of td with multiple td's of same class


rawDescription: Delete Data from a pandas array that is out of range


code:
import numpy as np
import mpmath as mp
from scipy.optimize import minimize
from scipy.integrate import quad
import matplotlib.pyplot as plt

def debye_integral_sym_scalar(x):
     """
     Calculate the Debye integral for a scalar using multi precision math,
     as otherwise it overflows with 64bit floats
     """
     exp_x = mp.exp(x)

     m1 = -120*mp.polylog(5, exp_x)
     m2 = 120*x*mp.polylog(4, exp_x)
     m3 = -60*x**2*mp.polylog(3, exp_x)
     m4 = 20*x**3*mp.polylog(2, exp_x)
     m5 = 5*x**4*mp.log(1 - exp_x)
     m6 = - x**5*exp_x

     return m1 + m2 + m3 + m4 + m5 + m6/(exp_x - 1) + 120*mp.zeta(5)

 # this is the actual function that we can use

def debye_integral_sym(x):
     f =  np.vectorize(debye_integral_sym_scalar, otypes=[np.complex])
     return f(x).real

def debye_integrand(x,  n):
     return x**n/((np.exp(x) - 1)*(1 - np.exp(-x)))

# test that debye_integral_sym returns the same result as quad
a = 10.0
res0 =  quad(debye_integrand, 0, a, args=(5,))[0]
res1 = debye_integral_sym(a)
np.testing.assert_allclose(res0, res1)

def resistivity_fit(p, T):
    rho0, AD, TD = p
    coeff = AD*np.power(T, 5)/np.power(TD, 4)
    return rho0 + coeff * debye_integral_sym(TD/(2*T))

def debye_err_func(p, T, r):
    return  np.sum((r - resistivity_fit(p, T))**2)

# wget "http://pastebin.com/raw.php?i=tvzcdxYA" -O salita.txt
data = np.loadtxt('salita.txt')

temp_exp = data[:, 0]
res_exp = data[:, 2]

p0 = np.array([0.0001 , 0.00001, 50])
p_opt = minimize(debye_err_func, p0, args=(temp_exp, res_exp))

print p_opt

temp = np.linspace(temp_exp.min(), temp_exp.max(), 100)

plt.plot(temp_exp, res_exp, '.', label='Experimental data')
plt.plot(temp, resistivity_fit(p_opt.x, temp), 'r', label='Bloch-Gruneisen fit')
plt.legend(loc='best')
plt.xlabel('Temperature [K]')
plt.ylabel('Resistivity')
plt.show()

rawDescription: Fit an integral function with parametric limit to data with Python (Debye Model)


code:
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
    print word

rawDescription: I do not understand this piece of tutorial code?


code:
f = open("hello.txt")
try:
    for line in f:
        print line,
finally:
    f.close()

rawDescription: Conditionally writing to different output files


code:
def unzip_artifact( local_directory, file_path ):
    fileName, ext = os.path.splitext( file_path )
    if ext == ".zip":
        print 'unzipping file ' + basename(fileName) + ext
        try:
            with zipfile.ZipFile(file_path) as zf:
                for member in zf.infolist():
                        # Path traversal defense copied from
                        # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
                        zf.extract(member, local_directory)
        except zipfile.error, e:
            print "Bad zipfile: %s" % (e)
        return

rawDescription: unzip extract in python

description: Unzip extract in python


code:
import itertools

n = 4
data = range(n)
for item1 in itertools.combinations(data, 2):
    for item2 in itertools.combinations(data, 2):
        if item1 < item2 and not set(item1) & set(item2):
            print item1, item2

rawDescription: Iterate over all disjoint pairs of pairs


code:
print su

print "\ncatching all the lettered strings after <!--"
print "re.compile('^.+?<!--|([a-z]+)',re.DOTALL)"
rgx = re.compile('^.+?<!--|([a-z]+)',re.DOTALL)
print [x.group(1) for x in rgx.finditer(su) if x.group(1)]

print ("\ncatching all the lettered strings after <!--\n"
       "excluding all the words 'world'")
print "re.compile('^.+?<!--|([a-z]+)(?<!world)',re.DOTALL)"
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!world)',re.DOTALL)
print [x.group(1) for x in rgx.finditer(su) if x.group(1)]

print ("\ncatching all the lettered strings after <!--\n"
       "excluding all the words 'hello'")
print "re.compile('^.+?<!--|([a-z]+)(?<!hello)',re.DOTALL)"
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!hello)',re.DOTALL)
print [x.group(1) for x in rgx.finditer(su) if x.group(1)]

print ("\ncatching all the lettered strings after <!--\n"
       "excluding all the words 'hello' belonging to a string 'hello world'")
print "re.compile('^.+?<!--|([a-z]+)(?<!hello(?= world))',re.DOTALL)"
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!hello(?= world))',re.DOTALL)
print [x.group(1) for x in rgx.finditer(su) if x.group(1)]

rawDescription: Python Regex: Matching a character on multi line?


rawDescription: How to set parameters in Python zlib module

description: Set parameters in Python zlib module


code:
NumOfN = int(input("What is N? : "))
runningTab = []
for i in range(NumOfN, -1, -1):
    if i != 0:
        runningTab.append(1.0/(i))

print "the sum is ", sum(runningTab)

rawDescription: Python 2 - "For counting loop"

description: Python 2 - "For counting loop"


code:
import csv
from bs4 import BeautifulSoup

data = """
your HTML here
"""

soup = BeautifulSoup(data)

data = soup.pre.find_all(text=True)
for row in data:
    row = row.split('\n')
    for item in row:
        item = item.strip()
        if item and (item.startswith('No.') or item[0].isdigit()):
            print item

rawDescription: BeautifulSoup to extract csv data from website


code:
for entry in dir(obj):
    print repr(entry)

rawDescription: Possible to output dir() in python as a vertical list?


code:
import re
line=r'mj\195\164ger'
def replace(match):
    return chr(int(match.group(1)))
regex = re.compile(r"\\(\d{1,3})")
new = regex.sub(replace, line)
new
print new

rawDescription: Process decimal escape in string

description: Process decimal escape in string


code:
import time
import numpy
from numpy.random import random
from multiprocessing import Process

def testAddOne(data):
    return data + 1

def testAddN(data,N):
    # print "testAddN", N
    for x in xrange(N):
        testAddOne(data)

if __name__ == '__main__':
    matrix_size = 1000
    num_adds = 10000
    num_processes = 4

    data = random((matrix_size,matrix_size))

    start = time.clock()
    if num_processes > 1:
        processes = [Process(target=testAddN, args=(data,num_adds/num_processes))
                     for i in range(num_processes)]
        for p in processes:
            p.start()
        for p in processes:
            p.join()
    else:
        testAddN(data,num_adds)

    stop = time.clock()
    print "Elapsed", stop - start

rawDescription: Python array sum vs MATLAB

description: Python array sum vs MATLAB


code:
with open('C:\Python27\Fake1.txt', 'r') as f:
    data = [map(float, line.split()) for line in f]

num_rows = len(data)
num_cols = len(data[0])

totals = num_cols * [0.0]
for line in data:
    for index in xrange(num_cols):
        totals[index] += line[index]

averages = [total / num_rows for total in totals]
print averages

rawDescription: Calculating the average of a column in a list in Python


code:
possibleNames = ['map', 'Map', 'MAP']
sheetNames = book.sheet_names()
name = intersect(possibleNames, sheetNames)
if len(name) < 1:
    print "Error"
    # break program appropiately
sheet = book.sheet_by_name(name[0])

def intersect(a, b):
    return list(set(a) & set(b))

rawDescription: Nested try statements to open an XL sheet


code:
import collections
with open("sample.txt", "r") as datafile:
    lines = datafile.read()
    words = lines.split()
    words_hist = collections.Counter(words)
    print words_hist

rawDescription: Split file and turn it into dictionary in python


code:
from stem import CircStatus
from stem.control import Controller

with Controller.from_port() as controller:
  controller.authenticate()

  for circ in controller.get_circuits():
    if circ.status != CircStatus.BUILT:
      continue  # skip circuits that aren't yet usable

    entry_fingerprint = circ.path[0][0]
    entry_descriptor = controller.get_network_status(entry_fingerprint, None)

    if entry_descriptor:
      print "Circuit %s starts with %s" % (circ.id, entry_descriptor.address)
    else:
      print "Unable to determine the address belonging to circuit %s" % circ.id

rawDescription: How do I get the IP address of the TOR entry node in use

description: Get the IP address of the TOR entry node in use


code:
class Vertex(object):
    @classmethod
    def foo(cls):
        print cls.resource

Vertex.resource = 'something'
v = Vertex()
v.foo()

rawDescription: When using the proxy pattern in Python, how can proxy classes access state in the calling object?


code:
class Config(object):
    DB_STRING = 'some default value'
    ANOTHER_SETTING = 'another default'
    DEBUG = True

    def dostuff(self):
      print 'I did stuff to ',self.DEBUG

class ProductionConfig(Config):
    DEBUG = False # only turn of debugging

class DevelopmentConfig(Config):
    DB_STRING = 'localhost'

    def dostuff(self):
       print 'Warning! Development system ',self.DEBUG

rawDescription: How to initialize helper method?

description: Initialize helper method


code:
import requests

url = 'http://stats.nba.com/stats/playergamelog'

params = {
    'Season': '2013-14',
    'SeasonType': 'Regular Season',
    'LeagueID': '00',
    'PlayerID': '2544',
    'pageNo': '1',
    'rowsPerPage': '100'
}
response = requests.post(url, data=params)

print response.json()

rawDescription: Scraping website that uses javascript

description: Scraping website that uses javascript


code:
import os
from multiprocessing import Process, Pipe

def f(conn):
    conn.send([os.getpid()])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints os.getpid()
    p.join()

rawDescription: How to get id of process from which came signal in Python?

description: Get id of process from which came signal in Python


code:
from datetime import datetime,timedelta
usetimes = sorted(datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in timestamp)
idletime = timedelta(minutes = 30)
usage = sum((x - y for x,y in zip(usetimes[1:],usetimes[:-1]) if x - y < idletime),timedelta())
print "total usage:", usage

rawDescription: Python - generate the time difference

description: Python - generate the time difference


code:
def get_info(string_to_search):
    res_dict = {}
    import re

    find_type = re.compile("Type:[\s]*[\w]*")
    res = find_type.search(string_to_search)
    res_dict["Type"] = res.group(0).split(":")[1].strip()

    find_Status = re.compile("Status:[\s]*[\w]*")
    res = find_Status.search(string_to_search)
    res_dict["Status"] = res.group(0).split(":")[1].strip()

    find_date = re.compile("Date:[\s]*[/0-9]*")
    res = find_date.search(string_to_search)
    res_dict["Date"] = res.group(0).split(":")[1].strip()

    res_dict["description"] = string_to_search.split("Description:")[1].replace("Thank you","")
    return res_dict

search_string = """> Irrelevant data
> Irrelevant data
> Type:     YUK
> Status:   OPEN
> Date:     6/22/2015
> Description: ----
>
> Description blah blah blah
> Thank you
"""
info =  get_info(search_string)

print info
print info["Type"]
print info["Status"]
print info["Date"]
print info["description"]

rawDescription: Parse text from a .txt file using csv module


code:
import bs4
import requests

page = requests.get('http://accountantlist.com.au/x123-Accountants-in-Sydney.aspx?Page=0')
soup = bs4.BeautifulSoup(page.content)

for company in soup.select('table#ctl00_ContentPlaceHolder1_dgLawyers tr > td > table'):
    name = company.a.text
    number = company.tr.find_all('td')[-1].text
    address = company.find_all('tr')[-1].td.text

    print {'name': name, 'number': number, 'address': address}

rawDescription: Webscraping a messy webpage with python and beautiful soup


code:
print df
df1 = df.copy()

def orig(df):
    groups=df.groupby(['BlockRange','Street'])

    #Write function that calculates the mid point of the block range
    def get_mid(x):
        block_nums=[int(y) for y in x.split('-')]
        return sum(block_nums)/len(block_nums)
    final=pd.DataFrame()

    for groupkey,group in groups:
        block_mid=get_mid(groupkey[0])
        halfway_point=len(group)/2
        group['Block']=0
        group.iloc[:halfway_point]['Block']=block_mid
        group.iloc[halfway_point:]['Block']=block_mid+1
        final=final.append(group)
    return final

def new(df):
    def f(x):
        df = pd.DataFrame([y.split('-') for y in x['BlockRange'].tolist() ])
        df = df.astype(int)
        block_nums = df.sum(axis=1) / 2
        x['Block'] = block_nums[0]
        halfway_point=len(x)/2
        x.iloc[halfway_point:, 2] = block_nums[0] + 1
        return x

    return df.groupby(['BlockRange','Street']).apply(f)

print orig(df)
print new(df1)

rawDescription: Pandas groupby - apply different functions to half the records in each group


code:
import pandas as pd

data = [{"low": 0, "high": 10, "name": "a"},
        {"low": 10, "high": 20, "name": "b"},
        {"low": 20, "high": 30, "name": "c"},
        {"low": 30, "high": 40, "name": "d"},
        {"low": 40, "high": 50, "name": "e"},]

myDF = pd.DataFrame(data)

#data to be binned
mySeries = pd.Series([5.7, 30.4, 21, 35.1])

#create bins from original data
bins = list(myDF["high"])
bins.insert(0,0)

print pd.cut(mySeries, bins, labels = myDF["name"])

rawDescription: Classify data by value in pandas

description: Classify data by value in pandas


code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys

class Browser(QWebView):
    def __init__(self):
        super(Browser, self).__init__()
        self.loadFinished.connect(self.print_load_finished)
        self.loadProgress.connect(self.print_percent)

    def print_percent(self, percent):
        print percent

    def print_load_finished(self):
        print 'Load Finished'
        self.print_html()

    def print_html(self):
        frame = self.page().mainFrame()
        print unicode(frame.toHtml()).encode('utf-8')

if __name__=='__main__':
    app = QApplication(sys.argv)
    br = Browser()
    url = QUrl('http://python.org')
    br.load(url)
    br.show()
    app.exec_()

rawDescription: print html of the website after loadFinished signal in PyQt4. Python

description: Print html of the website after loadFinished signal in PyQt4. Python


code:
from functools import wraps
def my_decorator(f):
    @wraps(f)
    def wrapper(*args, **kwds):
        print 'Calling decorated function'
        return f(*args, **kwds)
    return wrapper

@my_decorator
def example():
    """Docstring"""
    print 'Called example function'

example
example.func_closure
example.func_closure[0].cell_contents
example()
example.func_closure[0].cell_contents()

rawDescription: Accessing original decorated function for test purposes


code:
from mechanize import Browser

br = Browser()

r = br.open('http://www.example.com/')
r = br.open('http://www.google.com/')
r = br.back()

print r.read()

rawDescription: Mechanize with multiple pages

description: Mechanize with multiple pages


code:
for (i,j) in ( (i,j) for i in range(rows) for j in range(columns) ):
    print depth[i][j]

rawDescription: Iterating within bounds of a 2D list (matrix) in python


rawDescription: Assignment to value returned from list

description: Assignment to value returned from list


code:
for ConfLine, patterns in ((c, p) for c in Conf.values() for p in c):
    patterns = DateString + patterns
    if re.match(patterns, LogLine):
        break
else:
    print LogLine.strip()

rawDescription: Output Print is Slow

description: Output Print is Slow


code:
for i in range(10):
    print i

rawDescription: How to put statement before for-loop in python

description: Put statement before for-loop in python


code:
import csv

with open('ascii_delim.adt', 'w') as f:
    writer = csv.writer(f, delimiter=chr(31), lineterminator=chr(30))
    writer.writerow(('Sir Lancelot of Camelot', 'To seek the Holy Grail', 'blue'))
    writer.writerow(('Sir Galahad of Camelot', 'I seek the Grail', 'blue... no yellow!'))

def readlines(f, newline='\n'):
    while True:
        line = []
        while True:
            ch = f.read(1)
            if ch == '':  # end of file?
                return
            elif ch == newline:  # end of line?
                line.append('\n')
                break
            line.append(ch)
        yield ''.join(line)

with open('ascii_delim.adt', 'rb') as f:
    reader = csv.reader(readlines(f, newline=chr(30)), delimiter=chr(31))
    for row in reader:
        print row

rawDescription: Python CSV reading ascii delimited text?

description: Python CSV reading ascii delimited text


code:
import codecs

xlsx_sig = b'\x50\x4B\x05\06'
xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'

filenames = [
    ('spreadsheet.xls', 0, 512, 8),
    ('spreadsheet.xlsx', 2, -22, 4)]

for filename, whence, offset, size in filenames:
    with open(filename, 'rb') as f:
        f.seek(offset, whence) # Seek to the offset.
        bytes = f.read(size)   # Capture the specified number of bytes.

        print codecs.getencoder('hex')(bytes)

        if bytes == xls_sig:
            msg = '"{}" is an xls.'
        elif bytes == xlsx_sig:
            msg = '"{}" is an xlsx.'
        else:
            msg = '"{}" is not an Excel document.'
        print msg.format(filename)

rawDescription: How to check the uploaded file is csv or xls in python?

description: Check the uploaded file is csv or xls in python


code:
a = Num(100)
print a
b = a + 50
print b
c = Num(200)
d = a + c
print d

rawDescription: python and overflowing byte?

description: Python and overflowing byte


code:
symlist = xmldoc.getElementsByTagName('Symbol')
print "There are currently %d symbols" % len(symlist)
for s in symlist:
    print s.attributes['SymbolName'].value

rawDescription: Using minidom to break down an xml file into managable files. Python


code:
class A(object):
    def foo(self):
        print 'Foo'

    def bar(self, an_argument):
        print 'Bar', an_argument

rawDescription: Python calling method in class

description: Python calling method in class


code:
startmarker = "ohai"
endmarker = "meheer?"
marking = False
result = []

with open("somefile") as f:
  for line in f:
    if line.startswith(startmarker): marking = True
    elif line.startswith(endmarker): marking = False

    if marking: result.append(line)

if len(result) > 1:
  print "".join(result[1:])

rawDescription: How to grab a chunk of data from a file?

description: Grab a chunk of data from a file


code:
a = Time('now')
print vars(a)
a = Time(hours=19, minutes=4, seconds=5)
print vars(a)

rawDescription: Accept a single string instead of normal parameters


code:
list = [4,3,2,1]

print [x for x in list[::-1]]

rawDescription: How to access all list elements from last to first in python list

description: Access all list elements from last to first in python list


code:
import csv
import ast
import datetime

def guess_type(x):
    attempt_fns = [ast.literal_eval,
                   float,
                   lambda x: datetime.datetime.strptime(x,
                                                    "%Y-%m-%d %H:%M:%S")
                   ]
    for fn in attempt_fns:
        try:
            return fn(x)
        except (ValueError, SyntaxError):
            pass
    return x

with open("untyped.csv", "rb") as fp:
    reader = csv.reader(fp)
    for row in reader:
        row = [guess_type(x) for x in row]
        print row
        print map(type, row)

rawDescription: CSV reader and DictReader turn numeric fields into strings


code:
product_one_weights = product_one.weight_set.all()
for weight in product_one_weights:
    print weight

rawDescription: How do you implement multiple data types for an object in Django?

description: Implement multiple data types for an object in Django


code:
number = raw_input('Enter a number! ')
print number

rawDescription: How do I repeat my initial input code?

description: Repeat my initial input code


code:
import re

d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}

a = '111 = dw11qdwq\n111 = aaaaa'

print repr(a)
a= re.sub('111\s*=\s*(\S*)',lambda mat: d[mat.group(1)],a)
print repr(a)

rawDescription: how to get the two '111 = ' using python Regular Expressions

description: Get the two '111 = ' using python Regular Expressions


rawDescription: Checking if a lists' items are in the same order in another list


code:
command = r'abc\\\\\"abc'
search_string = r'.{2}(?:\\){5}".{2}'
print re.compile(search_string).search(command).group()

rawDescription: Python regex, pattern-match multiple backslash characters


rawDescription: Merge several tuples in python list

description: Merge several tuples in python list


code:
import csv
import urllib2

class RemoveBlank(object):
    def __init__(self, response):
        self.response = response
    def __iter__(self):
        return self
    def next(self):
        line = '\n'
        while line == '\n' or line == '<br>\n':
            line = next(self.response)
        return line

url = 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KMDLAURE4&day=9&month=6&year=2013&graphspan=day&format=1'
response = urllib2.urlopen(url)
cr = csv.reader(RemoveBlank(response))

for row in cr:
    print row

rawDescription: Reading .csv data from URL in python: extra lines


code:
words = {
    'gf': 'girlfriend',
    'bf': 'boyfriend',
    'btw': 'by the way',
    'hi': 'hello',
}

pat = re.compile(r"\b(%s)\b" % "|".join(words))

text = "The gf and the bf say hi btw."

new_text = pat.sub(lambda m: words.get(m.group()), text)

print new_text

rawDescription: Regex - How to replace a word in a string with its entry in a python dictionary?


code:
import struct
print [hex(ord(b)) for b in struct.pack('>L',777007543)]
# ['0x2e', '0x50', '0x31', '0xb7']

rawDescription: How do you convert a decimal number into a list of bytes in python

description: Convert a decimal number into a list of bytes in python


code:
import re

hay = "The fox jUMPed up the tree."
needle = "umpe"

regex = re.compile('(%s)' % needle, re.I)
print regex.sub('<strong>\\1</strong>', hay)

rawDescription: How do I do this regex in Python?

description: Do this regex in Python


rawDescription: Django testing external script

description: Django testing external script


code:
import sys

class CrossTab(object):

    def __init__(
        self,
        missing=0, # what to return for an empty cell. Alternatives: '', 0.0, None, 'NULL'
        ):
        self.missing = missing
        self.col_key_set = set()
        self.cell_dict = {}
        self.headings_OK = False

    def add_item(self, row_key, col_key, value):
        self.col_key_set.add(col_key)
        try:
            self.cell_dict[row_key][col_key] += value
        except KeyError:
            try:
                self.cell_dict[row_key][col_key] = value
            except KeyError:
                self.cell_dict[row_key] = {col_key: value}

    def _process_headings(self):
        if self.headings_OK:
            return
        self.row_headings = list(sorted(self.cell_dict.iterkeys()))
        self.col_headings = list(sorted(self.col_key_set))
        self.headings_OK = True

    def get_col_headings(self):
        self._process_headings()
        return self.col_headings

    def generate_row_info(self):
        self._process_headings()
        for row_key in self.row_headings:
            row_dict = self.cell_dict[row_key]
            row_vals = [row_dict.get(col_key, self.missing) for col_key in self.col_headings]
            yield row_key, row_vals

    def dump(self, f=None, header=None, footer='', ):
        if f is None:
            f = sys.stdout
        alist = self.__dict__.items()
        alist.sort()
        if header is not None:
            print >> f, header
        for attr, value in alist:
            print >> f, "%s: %r" % (attr, value)
        if footer is not None:
            print >> f, footer

if __name__ == "__main__":

    data = [
        ['Rob', 'Morn', 240],
        ['Rob', 'Aft',  300],
        ['Joe', 'Morn',  70],
        ['Joe', 'Aft',   80],
        ['Jill', 'Morn', 100],
        ['Jill', 'Aft',  150],
        ['Rob', 'Aft',   40],
        ['Rob', 'aft',    5],
        ['Dozy', 'Aft',   1],
        # Dozy doesn't show up till lunch-time
        ['Nemo', 'never', -1],
        ]
    NAME, TIME, AMOUNT = range(3)
    xlate_time = {'morn': "AM", "aft": "PM"}

    print
    ctab = CrossTab(missing=None, )
    # ctab.dump(header='=== after init ===')
    for s in data:
        ctab.add_item(
            row_key=s[NAME],
            col_key= xlate_time.get(s[TIME].lower(), "XXXX"),
            value=s[AMOUNT])
        # ctab.dump(header='=== after add_item ===')
    print ctab.get_col_headings()
    # ctab.dump(header='=== after get_col_headings ===')
    for x in ctab.generate_row_info():
        print x

rawDescription: Pivoting SQLite table, setwise like SQL should be


rawDescription: Finding remainder mod involving exponent and division involving huge numbers


rawDescription: python merging dictionary of lists of lists/tuples

description: Python merging dictionary of lists of lists/tuples


code:
for script in dir(tpp):
    if not "__" in script: #not a builtin...
        docstrings1 = getattr( tpp, script).__doc__
        if docstrings1:  #objects without docstrings return None above, which can't be split.
            docstrings2 = " ".join(docstrings1.split())#clean out any newline chars
            print script, docstrings2

rawDescription: Getting docstrings from Python package __init__ - based imports


code:
tf.reset_default_graph()

num_examples = 5
num_features = 2
data = np.reshape(np.arange(num_examples*num_features), (num_examples, num_features))
print data

(data_node,) = tf.slice_input_producer([tf.constant(data)], num_epochs=1, shuffle=False)
data_node_debug = tf.Print(data_node, [data_node], "Dequeueing from data_node ")
data_batch = tf.batch([data_node_debug], batch_size=2)
data_batch_debug = tf.Print(data_batch, [data_batch], "Dequeueing from data_batch ")

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
tf.get_default_graph().finalize()
tf.start_queue_runners()

try:
  while True:
    print sess.run(data_batch_debug)
except tf.errors.OutOfRangeError as e:
  print "No more inputs."

rawDescription: How to *actually* read CSV data in TensorFlow?

description: *actually* read CSV data in TensorFlow


code:
def prime():
    from gmpy2 import is_prime
    import time
    start_time = time.clock()

    print "total sum is %d" % (sum(filter(is_prime, xrange(3,2000000,2)))+2)

    print time.clock() - start_time, "seconds"

prime()

rawDescription: how to increase the efficiency of this program?

description: Increase the efficiency of this program


code:
#!/usr/bin/python
import serial
import syslog
import time

#The following line is for serial over GPIO
port = '/dev/tty.usbmodem1411' # note I'm using Mac OS-X

ard = serial.Serial(port,9600,timeout=5)
time.sleep(2) # wait for Arduino

i = 0

while (i < 4):
    # Serial write section

    setTempCar1 = 63
    setTempCar2 = 37
    ard.flush()
    setTemp1 = str(setTempCar1)
    setTemp2 = str(setTempCar2)
    print ("Python value sent: ")
    print (setTemp1)
    ard.write(setTemp1)
    time.sleep(1) # I shortened this to match the new value in your Arduino code

    # Serial read section
    msg = ard.read(ard.inWaiting()) # read all characters in buffer
    print ("Message from arduino: ")
    print (msg)
    i = i + 1
else:
    print "Exiting"
exit()

rawDescription: python to arduino serial read & write

description: Python to arduino serial read & write


code:
it = itertools.cycle( ['D1', 'D2', 'D3'] )

for x in ['F1', 'F2', 'F3', 'F4' , 'F5', 'F6']:
    print x, 'write in', next(it)

rawDescription: Writing files to directories in a round robin pattern


code:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get('http://www.jabong.com/athena-Red-Black-Top-476472.html?pos=3')

wait = WebDriverWait(driver, 10)
chart = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "size chart")))
chart.click()

for title in driver.find_elements_by_css_selector("div.size-chart-body div.size-chart table th"):
    print title.text

driver.close()

rawDescription: Get JavaScript function call value using Selenium


code:
import numpy as np
import theano
import theano.tensor as T

def addf(a1,a2):
        print a1.type
        print a2.type
        return a1+a2

i = T.iscalar('i')
x0 = T.iscalar('x0')
step= T.iscalar('step')

results, updates = theano.scan(fn=addf,
                   outputs_info=[{'initial':x0, 'taps':[-1]}],
                   non_sequences=step,
                   n_steps=i)

f=theano.function([x0,i,step],results)

print f(1,10,2)

rawDescription: Python - Theano scan() function

description: Python - Theano scan() function


code:
def encrypt(key, msg):
    encryped = []
    for i, c in enumerate(msg):
        key_c = ord(key[i % len(key)])
        msg_c = ord(c)
        encryped.append(chr((msg_c + key_c) % 127))
    return ''.join(encryped)

def decrypt(key, encryped):
    msg = []
    for i, c in enumerate(encryped):
        key_c = ord(key[i % len(key)])
        enc_c = ord(c)
        msg.append(chr((enc_c - key_c) % 127))
    return ''.join(msg)

if __name__ == '__main__':
    key = 'This_is_my_awsome_secret_key'
    msg = 'Hello world'
    encrypted = encrypt(key, msg)
    decrypted = decrypt(key, encrypted)

    print 'Message:', repr(msg)
    print 'Key:', repr(key)
    print 'Encrypted:', repr(encrypted)
    print 'Decrypted:', repr(decrypted)

rawDescription: Custom Python Encryption algorithm

description: Custom Python Encryption algorithm


code:
x = [1,2,3,1,2,1]
count = len([i for i in x if i == 1])
print count

rawDescription: Count the number of occurrences of an item in a list without list.count


code:
if (re.search(r'/\*!\*/;',a)):
   print 'found'

rawDescription: Regex for finding /*!*/; in a file

description: Regex for finding /*!*/; in a file


code:
import test

len,str = test.foo()

print len
print str

rawDescription: How to receive reference and pointer arguments in Python + SWIG?

description: Receive reference and pointer arguments in Python + SWIG


code:
pattern1 = 'TTT'
pattern2 = 'HTT'
trials = 1000
d = {pattern1: 0, pattern2: 0}

for _ in range(trials):
    sequence = random.choice("HT") + random.choice("HT") + random.choice("HT")
    while sequence not in {pattern1, pattern2}:
        sequence = sequence[-2:] + random.choice("HT")
    d[sequence] += 1

print pattern1, 'wins =', d[pattern1]
print pattern2, 'wins =', d[pattern2]
print str((max([d[pattern1], d[pattern2]]) / float(trials) * 100)) + '%'

rawDescription: Detect all certain substrings in Python sequence


code:
import requests
from bs4 import BeautifulSoup as bs

url = "https://finance.yahoo.com/q/ks?s=AAL+Key+Statistics"

r = requests.get(url)

soup = bs(r.text)

HTML_MarketCap = soup.find("sup", text="5").parent.parent.find_next_sibling("td").find("span").text

print HTML_MarketCap

rawDescription: beautifulsoup parsing - dealing with superscript?

description: Beautifulsoup parsing - dealing with superscript


code:
islink, join, isdir = path.islink, path.join, path.isdir

# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains.  os.path.walk
# always suppressed the exception then, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit.  That logic is copied here.
try:
    # Note that listdir and error are globals in this module due
    # to earlier import-*.
    names = listdir(top)
except error, err:
    if onerror is not None:
        onerror(err)
    return

dirs, nondirs = [], []
for name in names:
    if isdir(join(top, name)):
        dirs.append(name)
    else:
        nondirs.append(name)  # not interesting

if topdown: # not inretesting
    yield top, dirs, nondirs
for name in dirs:
    new_path = join(top, name)
    if followlinks or not islink(new_path): # not interesting
        for x in walk(new_path, topdown, onerror, followlinks):
            yield x
if not topdown: # not inretesting
    yield top, dirs, nondirs # not inretesting

rawDescription: List all branches of subfolders in directory with python


code:
print type(type(type(A)))
print type(type(type(type(type(A)))))

rawDescription: How does everything is an object even work?


code:
data = data.replace('ObjectID =', '').replace('\nX = ', ',').replace('\nY = ', ',')
print data

rawDescription: Extracting data from a text file to an output file


code:
data = mime.data('application/x-qt-windws-mime;value="Csv"')
# convert from QByteArray to python string
data = str(data.data())
print data

rawDescription: PyQt4 table for inserting a table from the clipboard - tableWidget?


code:
y = 'stackoverflow'
x = 'stackover'
if re.match('^%s' % x, y):
    print "success"

rawDescription: including a bound string object in regular expression in python

description: Including a bound string object in regular expression in python


code:
s = Counter()
for filename in glob.iglob('*.txt'):
    with open(filename, 'r') as f:
        for line in f:
            k, v = line.split()
            s[k] += float(v)
print s
with open('sum.txt', 'w') as f:
    for k, v in sorted(s.iteritems()):
        f.write('{0} {1}\n'.format(k, v))

rawDescription: iterating through folder for files and add up values within the columned file in Python

description: Iterating through folder for files and add up values within the columned file in Python