code:
import inspect

def calling_scope_variable(name):
  frame = inspect.stack()[1][0]
  while name not in frame.f_locals:
    frame = frame.f_back
    if frame is None:
      return None
  return frame.f_locals[name]

z = 1

def a():
  z = calling_scope_variable('z')
  z = z * 2
  print z

def b():
  z = calling_scope_variable('z')
  z = z + 1
  print z
  a()
  print z

b()

rawDescription: Sharing scope in Python between called and calling functions


code:
for i in range(len(self.sensors)):
    print self.sensors[i].position()

rawDescription: Accessing methods from objects within a list


code:
import timeit
import random

with open('/usr/share/dict/words','r') as di:  # UNIX 250k unique word list
    all_words_set={line.strip() for line in di}

all_words_list=list(all_words_set)    # slightly faster if this list is sorted...

test_list=[random.choice(all_words_list) for i in range(10000)]
test_set=set(test_list)

def set_f():
    count = 0
    for word in test_set:
        if word in all_words_set:
           count+=1
    return count

def list_f():
    count = 0
    for word in test_list:
        if word in all_words_list:
           count+=1
    return count

def mix_f():
    # use list for source, set for membership testing
    count = 0
    for word in test_list:
        if word in all_words_set:
           count+=1
    return count

print "list:", timeit.Timer(list_f).timeit(1),"secs"
print "set:", timeit.Timer(set_f).timeit(1),"secs"
print "mixed:", timeit.Timer(mix_f).timeit(1),"secs"

rawDescription: Python: how to check that if an item is in a list efficiently?


rawDescription: Why Beautiful Soup Ignoring CDATA

description: Why Beautiful Soup Ignoring CDATA


code:
pos = {y:x for x,y in labels.iteritems()}
G2 = nx.erdos_renyi_graph(100,0.1)
nx.draw_networkx(G2, pos=pos, with_labels=True, node_size = 300)
print G.nodes()
plt.axis('off')
plt.show()

rawDescription: NetworkX: how to build an Erdos-Renyi graph from a set of predetermined positions?


code:
for u,outer_d in G.nodes(data=True):
   center = outer_d['center']
   print u, "with center", center
   for v, inner_d in G.nodes(data=True):
        #Don't compare self to self
        if u != v:
            # Create a source image
            src = np.zeros((400,400),np.uint8)
            # draw an polygon on image src
            points = np.array(inner_d['points'],np.int0)
            cv2.polylines(src,[points],True,255,3)
            contours,_ = cv2.findContours(src,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
            if cv2.pointPolygonTest(contours[0],center,True) <= 0:
                print 'outside',v
            else:
                print 'inside',v

rawDescription: looping through nodes and extract attributes in Networkx

description: Looping through nodes and extract attributes in Networkx


code:
if name in dct:
    print dct[name]

rawDescription: How do I print the following objects of a list

description: Print the following objects of a list


code:
import re
from functools import partial

pattern = '^[\w-]*_(?P<pos>[A-Z]\d{2})_T\d{4}(?P<fID>F\d{3})L\d{2}A\d{2}(?P<zID>Z\d{2})(?P<cID>C\d{2})\.tif$'
filename = '151006_655866_Z01_T0001F015L01A02Z01C03.tif'

def replace_closure(subgroup, replacement, m):
    if m.group(subgroup) not in [None, '']:
        start = m.start(subgroup)
        end = m.end(subgroup)
        return m.group()[:start] + replacement + m.group()[end:]

subgroup_list = ['pos', 'fID', 'zID', 'cID']
replacement = '---'

for subgroup in subgroup_list:
    print re.sub(pattern, partial(replace_closure, subgroup, replacement), filename)

rawDescription: Replace named group in regex match

description: Replace named group in regex match


code:
class FlagChecker:
    def __init__(self,flags):
       self.flags = flags
    def __getattr__(self,attr):
       if attr.startswith("is"):
          flg = getattr(self.flags,attr[2:].upper(),0)
          return lambda v:bool(v & flg)

checker = FlagChecker(CandleFlags)
print checker.isHammer(candleValue)
print not checker.isDoji(candleValue)
print checker.isYellow(candleValue) #always false ... no YELLOW flag

rawDescription: How to present a flag in python?

description: Present a flag in python


rawDescription: How to mutate a list with a function in python?

description: Mutate a list with a function in python


rawDescription: How to get the IP prefix while we have subnet and IPv4 address using python-netaddr?

description: Get the IP prefix while we have subnet and IPv4 address using python-netaddr


code: print [[x,y,z] for x in range(20) for y in range(x+5,20) for z in range(y+5,20)]

rawDescription: Python: find all sets of numbers inside a range where each set contains values that are x distance apart and don't exceed the range


code:
#!/usr/bin/python
import re
r=re.compile("ABC(\(L[0-9]+\)|\([0-9]+\)|)*DEF");
lines = [
        "ABC(L30)(345)DEF",
        "ABC(L2)(45)DEF",
        "ABC(L30)DEF",
        "ABC(345)DEF",
        "ABCDEF",
        "ABCxyzDEF",
        "ABC(L)DEF",
        "ABC(A)DEF",
        "ABC()DEF",
]
for str in lines:
        if r.match(str):
                print "    match : %s" % str
        else:
                print "not match : %s" % str

rawDescription: capturing only certain characters in regular expression by python

description: Capturing only certain characters in regular expression by python


rawDescription: Slice away first element in each (list) element in a list in Python


code:
import math

# calculate and return the distance unit2
# needs to move to reach unit1
def get_distance(unit1, unit2):
    phi = abs(unit2-unit1) % 360
    sign = 1
    # used to calculate sign
    if not ((unit1-unit2 >= 0 and unit1-unit2 <= 180) or (
            unit1-unit2 <= -180 and unit1-unit2 >= -360)):
        sign = -1
    if phi > 180:
        result = 360-phi
    else:
        result = phi

    return result*sign

# distance unit2 needs to move to reach unit1
print get_distance(90,45)  # output 45
print get_distance(270, 350) # output -80
print get_distance(350, 30) # output -40 (unit2 moves -40 degrees)
print get_distance(30, 350) # output 40

unit1 = 30
unit2 = 350
# then calculate distance to move by taking 20 from distance
distance_to_move = (get_distance(unit1, unit2) - 20)

print (unit2 + distance_to_move)%360 # new position 10

rawDescription: Calculate difference between two angles

description: Calculate difference between two angles


code:
import requests
r = requests.get('https://example.com')
print r.status_code

rawDescription: Python send https data to website

description: Python send https data to website


code:
def counter(number, delta):
    number = number + delta
    return number

def iterator(iteration, function, *args):
    args = list(args)
    for i in range(iteration):
        ret = function(*args)
        args[0] = ret
    return ret

number = 0
number = iterator(5, counter, number, 5)
print number # => 25

rawDescription: Python: Outputting data from a function called in a function


code:
print re.findall(namepat, CV2)

['123 Cooper Union ',
 '124 Rochester Institute of Technology ',
 '125 Bucknell University ',
 '126 University of Massachusetts Foundation, Inc. ',
 '127 Virginia Tech Foundation ',
 '128 Queen(tm)s University# ',
 '129 Colby College ',
 '130 Loma Linda University ',
 '131 Principia Corporation ',
 '132 College of the Holy Cross ',
 '133 Mount Holyoke College ',
 '134 Oklahoma State Regents for Higher Education ',
 '135 Rensselaer Polytechnic Institute ']

rawDescription: Capturing TM in Regex python

description: Capturing TM in Regex python


code:
def foo(b=None):
    if b is None:
        b = 5 if test() else 10

    print b

rawDescription: Python function optional arguments - possible to add as condition?


code: print [item for items in Temp for item in items.split(",")]

rawDescription: Combining list elements into one element in python


code:
def getUniqueWords(allWords) :
    uniqueWords = []
    for i in allWords:
        if not i in uniqueWords:
            uniqueWords.append(i)
    return uniqueWords

print getUniqueWords(['a','b','c','a','b']);

rawDescription: finding all unique words from a list using loops

description: Finding all unique words from a list using loops


code:
def eval_iter(expr_str, global_d, local_d, dict_iter, sep="|"):
  expr_l=expr_str.split(sep)
  if isinstance(dict_iter, dict): dict_iter=dict_iter.itervalues()

  aggregation_l=[]
  for eval_locals in dict_iter:
    locals().update(eval_locals)
    aggregation_l.append(
      eval(",".join(expr_l[1::2]).join("[]"), globals(), locals()))
    for key in eval_locals: del locals()[key] # cleanup a bit

  expr_l[1::2]=["aggregation_l[%d]"%enum for enum in range(len(expr_l)/2)]

  local_d["aggregation_l"]=zip(*aggregation_l)
  return eval("".join(expr_l), global_d, local_d)

discount=0.10 # 10%
desc_f="In %s purchased %s fruit at the total price of $%.2f"

# The QUERY: -------- 8>< - - - - cut here - - - -
for city,city_purchases_d in itertools.groupby(transaction_l,
                                               lambda d: d["city"]):
  print desc_f%eval_iter("city,sum(|qty|),sum(|qty * price|)*(1-discount)",
                         globals(), locals(), city_purchases_d)

rawDescription: Generating sums, and/or sums of products etc. from the results of itertools groupby aggrigation


code:
# pip_user_installs.py
import sys
import pkg_resources
for module in pkg_resources.working_set:
    if sys.argv[1] in module.location:
        print module.project_name

rawDescription: How can I pip freeze and get only pip --user installs, no system installs?

description: Pip freeze and get only pip --user installs, no system installs


code: print 'Hello %s!' %(name)

rawDescription: Unwanted space in Python

description: Unwanted space in Python


code:
import sys

f = open(sys.argv[1],"r")
contents = f.read()
f.close()
print contents

rawDescription: I want to read in a file from the command line in python


code:
import csv
from collections import OrderedDict, namedtuple

with open('abc1') as f:
    reader = csv.reader(f)
    tup = namedtuple('tup', ['price', 'year', 'month'])
    d = OrderedDict()
    names = next(reader)[2:]
    for name in names:
        #initialize the dict
        d[name] = tup(0, 'year', 'month')
    for row in reader:
        year, month = row[:2]         # Use year, month, *prices = row in py3.x
        for name, price in zip(names, map(int, row[2:])): # map(int, prices) py3.x
            if d[name].price < price:
                d[name] = tup(price, year, month)
print d

rawDescription: getting max value from each column of the csv file

description: Getting max value from each column of the csv file


code:
import numpy as np
nk=np.genfromtxt('input.txt', dtype=None)
result1={}
for x in nk:
    result1.setdefault(x[3].split('_')[0],[]).append(x[4])
for x in nk:
    if x[4]==max(result1[x[3].split('_')[0]]):print x

rawDescription: filter text file based on multiple columns

description: Filter text file based on multiple columns


code:
import math

for a in range(1, 10):
    x = 3.0
    while True:
        y = (x + a/x) / 2
        if y == x:
            break
        x = y
    sq = math.sqrt(a)
    print '{:>5} {:>5.10f} {:>5.10f} {:>5.10f}'.format(float(a), sq, x, abs(sq - x))

rawDescription: How do I organize the output of this code?

description: Organize the output of this code


rawDescription: Filtering tuples in a list

description: Filtering tuples in a list


code:
def function(s):
    if len(s) == 1:
        print 'single:', s[0],  # (A) this is where your first output is generated for a single character
    else:
        print 'calling function again with ',s[1:]
        function(s[1:]) # (B) you call function again, i.e., your recursive call
        print ' unwind->', s[0], # (C) the "unwinding" step, i.e, finish the rest
                                 # of the function when you return from the recursive call

rawDescription: Why does this output occur?

description: Why does this output occur


rawDescription: Python and Floats - Print only the Whole Number


rawDescription: How to split a csv file on date using python

description: Split a csv file on date using python


code:
for result in tree.xpath('//div[@class="grouptext"]/span/following-sibling::text()'):
    print result.strip()

rawDescription: Extracting the value by xpath in python between tags


code:
s = "%u003c%u0062%u0072%u003e%u003c%u0064%u0069%u0076%u0020%u0063%u006c......"
s = s.replace("%", "\\")

html = s.decode('unicode-escape')
html = BeautifulSoup(html)

print html.find("img").text

rawDescription: Beautiful Soup decode string

description: Beautiful Soup decode string


code:
class Person(object):
    """Information about a single name"""
    def __init__(self,name):
        self.name = name
        self.parent = None
        self.children = []

def search_people(people,name):
    """Searches for a name in known people and returns the corresponding Person object or None if not found"""
    try:
        return filter(lambda y: y.name == name,people)[0]
    except IndexError:
        return None

def search_add_and_return(people,name):
    """Search for a name in list of people. If not found add to people. Return the Person object in either case"""
    old_name = search_people(people,name)
    if old_name is None:
        #First time entry for the name
        old_name = Person(name)
        people.append(old_name)
    return old_name

def read_file(file_name,people):
    fp = open(file_name,'r')
    while True:
        l = fp.readline()
        l.strip('\n').strip()
        if not l:
            break
        names = l.split(':')
        mother = names[0].strip()
        children = [x.strip() for x in names[1].split(',')]
        old_mother = search_add_and_return(people,mother)
        #Now get the children objects
        child_objects = []
        for child in children:
            old_child = search_add_and_return(people,child)
            child_objects.append(old_child)
        #All children have been gathered. Link them up
        #Set parent in child and add child to parent's 'children'
        old_mother.children.extend(child_objects)
        for c in child_objects:
            c.parent = old_mother
    fp.close()

def main():
    file_name = 'try.txt'
    people = []
    read_file(file_name,people)

    #Now lets define the language and start a loop
    while True:
        command = raw_input("Enter your command or 0 to quit\n")
        if command == '0':
            break
        coms = command.split()
        if len(coms) < 2:
            print "Wrong Command"
            continue
        action = coms[0]
        param = coms[1]
        if action == "mother":
            person = search_people(people,param)
            if person == None:
                print "person not found"
                continue
            else:
                if person.parent is None:
                    print "mother not known"
                else:
                    print person.parent.name
        elif action == "ancestors":
            person = search_people(people,param)
            if person == None:
                print "person not found"
                continue
            else:
                ancestor_list = []
                #Need to keep looking up parent till we don't reach a dead end
                #And collect names of each ancestor
                while True:
                    person = person.parent
                    if person is None:
                        break
                    ancestor_list.append(person.name)
                if ancestor_list:
                    print ",".join(ancestor_list)
                else:
                    print "No known ancestors"

if __name__ == '__main__':
    main()

rawDescription: Looping dictionary values

description: Looping dictionary values


code:
class X(object):
    def m(self,x):
        print x

print X.m           #>>> <unbound method X.m>
print vars(X)['m']  #>>> <function m at 0x9e17e64>

def increase_decorator(function):
    return lambda self,x: function(self,x+1)

rawDescription: How can I add a decorator to an existing object method?

description: Add a decorator to an existing object method


code: print "".join(lines)

rawDescription: Python: How to extract data in text file based on class information from another text file?


code:
if key_value == None:
   print 'there was no key'
else:
   print 'key_value was', key_value

rawDescription: Check redis get key available, if else python script


code:
try:
  i = int(temp[0])
except ValueError:
  print "not an integer\n"

try:
  i = float(temp[0])
except ValueError:
  print "not a number\n"

rawDescription: How to check if an element of a list is a number?

description: Check if an element of a list is a number


code:
print '[{}]'.format(', '.join(['{%(score)s-%(numrep)s}' % d for d in lst]))
from string import Template
f = Template('{$score-$numrep}')
print '[{}]'.format(', '.join([f.substitute(d) for d in lst]))

rawDescription: List of dictionaries - how to format print output


code:
#!/usr/bin/python

import time
import re

p1 = re.compile(r'toto')
p2 = re.compile(r'^toto')

ssize = 1000

s1 = 'toto abcdefghijklmnopqrstuvwxyz012356789'*ssize
s2 = 'titi abcdefghijklmnopqrstuvwxyz012356789'*ssize

nb = 1000

i = 0
t0 = time.time()
while i < nb:
    p1.match(s1)
    i += 1
t1 = time.time()

i = 0
t2 = time.time()
while i < nb:
    p2.search(s1)
    i += 1
t3 = time.time()

print "\nsucceed\nmatch:"
print (t1-t0)
print "search:"
print (t3-t2)

i = 0
t0 = time.time()
while i < nb:
    p1.match(s2)
    i += 1
t1 = time.time()

i = 0
t2 = time.time()
while i < nb:
    p2.search(s2)
    i += 1
t3 = time.time()

print "\nfail\nmatch:"
print (t1-t0)
print "search:"
print (t3-t2)

rawDescription: Why have re.match()?

description: Why have re.match()


code:
def getFeatures(text, word_features):
    text = text.lower()
    f = {word: word in text for word in word_features}
    return f

def FindFeaturesForList(MovieList):
    featureSet = []
    splitter = re.compile('\\W*')
    all_words = nltk.FreqDist(
        s.lower()
        for w in MovieList
        for s in RemoveStopWords(splitter.split(w['imdb']['plot']))
        if len(s) > 5 and len(s) < 15)
    word_features = all_words.keys()[:2000]
    for w in MovieList:
        print w['imdb']['title']
        try:
            for genre in w['imdb']['genres']:
                featureSet.append(
                    (getFeatures(w['imdb']['plot'], word_features), genre))
        except:
            print "Error when retriving genre, skipping element"

    return featureSet

rawDescription: NLTK Naive bayes Classifier wierd results


code:
def doublefunc(fn):
    def wrapped(*arg, **kwargs):
        unpassed_defaults = get_unpassed_defaults(fn, arg, kwargs)
        kwargs.update(unpassed_defaults)

        arg = (i if type(i) is not int else i*2 for i in arg)
        for k, v in kwargs.iteritems():
            kwargs[k] = v *2 if type(v) is int else v

        return fn(*arg, **kwargs)
    return wrapped

@doublefunc
def add(x, y, m, z=10, v='bbaa'):
    print 'x=%s, y=%s, m=%s, z=%s, v=%s' % (x, y, m, z, v)
    return x + y + z

print add(1, 1, "teststring", v=3)

rawDescription: Is python parameters' default value accepted by args or kwargs?


code:
from bs4 import BeautifulSoup

txt = '''

Cost for 2:
Rs. 350

'''

soup = BeautifulSoup(txt)

for div in soup.find_all("div", { "class" : "search-page-text" }):
    print ''.join(div.find_all(text=True, recursive=False)).strip()
    #print div.find_all(text=True, recursive=False)[1].strip()

rawDescription: Unable to fetch <div> tag values in python


code:
import wx

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, None, -1, *args, **kwargs)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        b = wx.Button(self, -1, "Test")
        sizer.Add(b)
        b.Bind(wx.EVT_BUTTON, TestHandler("foo"))
        self.Show()

class TestHandler:
    def __init__(self, value):
        self.value = value

    def __call__(self, event):
        print event
        print "My value is:", self.value

if __name__ == "__main__":
    app = wx.App()
    TestFrame("")
    app.MainLoop()

rawDescription: How can I give variables to the event handler?

description: Give variables to the event handler


code:
import inspect
import re

def echo(arg):
    frame = inspect.currentframe()
    try:
        context = inspect.getframeinfo(frame.f_back).code_context
        caller_lines = ''.join([line.strip() for line in context])
        m = re.search(r'echo\s*\((.+?)\)$', caller_lines)
        if m:
            caller_lines = m.group(1)
        print caller_lines, arg
    finally:
        del frame

foo = 7
bar = 3
baz = 11
echo(foo)
echo(foo + bar)
echo((foo + bar)*baz/(bar+foo))

rawDescription: Find the name of a Python variable that was passed to a function


code:
size_of_new = 5
print zip(*[iter(x)]*size_of_new)

rawDescription: Python long list separated

description: Python long list separated


code:
data={}
for t in zip(*reader):
    data[t[0]]=t[1:]

print data
# {'A': ('12', '10'), 'B': ('15', '14'), 'Classification Type': ('Commercial Homes', 'Residential Homes')}

rawDescription: Read columns into separate lists

description: Read columns into separate lists


code:
from datetime import datetime
limit = datetime(2011,01,05,17,0,0) #5pm , Jun 5 2011
file_date_string_list = ["%s %s"%(x[0],date.fromtimestamp(x[1])) \
                                       for x in file_date_tuple_list \
                                       if datetime.fromtimestamp(x[1]) > limit ]

rawDescription: How to make a chronological list of files, with the file modification date

description: Make a chronological list of files, with the file modification date


code:
from collections import defaultdict
from datetime import timedelta
import csv

durations = defaultdict(lambda: {'In': timedelta(), 'Out': timedelta()})

with open("./Test.csv", "rb") as inf:
    reader = csv.reader(inf, delimiter=delim)
    for name, direction, duration in reader:
        hours, minutes, seconds = map(int, duration.split(':'))
        duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
        durations[name][direction] += duration

for name, directions in durations.items():
    print '{:10} In    {}'.format(name, directions['In'])
    print '           Out   {}'.format(directions['Out'])
    print '           Total {}'.format(
        directions['In'] + directions['Out'])

rawDescription: Adding time/duration from CSV file

description: Adding time/duration from CSV file


code:
from fabric.api import *

env.password = "mypassword"

print env.user
print env.password
def update():
    local("echo {} | sudo -S apt-get update".format(env.password))

rawDescription: how to force fabric to use env.password setting

description: Force fabric to use env.password setting


code:
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        print "Going through 302:\n"
        print headers
        return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)

rawDescription: How to programmatically retrieve access_token from client-side OAuth flow using Python?

description: Programmatically retrieve access_token from client-side OAuth flow using Python


code:
for roomname, room in rooms.items():
    print roomname, room.x, room.y

rawDescription: Can you make a function that would create several instances of a class for you in Python?

description: Make a function that would create several instances of a class for you in Python


code:
csvReader = csv.reader(merged)
for row in csvReader:
    #Changes 'Process(python)\% Processor Time' into '% Processor Time'
    row = truncate_string(row[0],"\\",1)
    print row

rawDescription: Changes to a Merged CSV file not saving

description: Changes to a Merged CSV file not saving


code:
import sys
import tempfile
from subprocess import Popen, PIPE

cmd = [sys.executable, '-c', 'print raw_input()']

# Using a temp file to give input data to the subprocess instead of stdin.write to avoid deadlocks.
with tempfile.TemporaryFile() as f:
    f.write('foobar')
    f.seek(0)  # Return at the start of the file so that the subprocess p1 can read what we wrote.
    p1 = Popen(cmd, stdin=f, stdout=PIPE)

p2 = Popen(cmd, stdin=p1.stdout, stdout=PIPE)
p3 = Popen(cmd, stdin=p2.stdout, stdout=PIPE)

# No order needed.
p1.stdout.close()
p2.stdout.close()

# Using communicate() instead of stdout.read to avoid deadlocks.
print p3.communicate()[0]

rawDescription: piping together several subprocesses

description: Piping together several subprocesses


code:
import re

text = '''"Mon.":[11.76,7.13],"Tue.":[11.76,7.19],"Wed.":[11.91,6.94]'''
values = re.findall(r'\[(.*?)\]', text)
values = map(lambda x: x.split(','), values)
values = zip(*values)
print max(map(float, values[0]))
print max(map(float, values[1]))

rawDescription: Finding max value of float in a string - regex


rawDescription: convert a hexadecimal file and extract / display characters that belongs to a language

description: Convert a hexadecimal file and extract / display characters that belongs to a language


code:
my_row = ADryRow()
my_row.ripeness = "tart"
my_row.colour = "#8C2"
my_row.juiciness = 0.3479
my_row.seeds = 19
print my_row.to_row()
print my_row.to_row()

rawDescription: OO design: an object that can be exported to a "row", while accessing header names, without repeating myself


code:
s = "%blah=5, %note=44"
print dict([item.split('=') for item in s.replace(' ','').replace('%', '').split(',')])

rawDescription: Read string variables into variables

description: Read string variables into variables


code:
def get_type(word):
    for key,values in wordDict.items():
        if word in values:
            return key

for word in words:
    print word, get_type(word)

rawDescription: understanding a selector from within a dict

description: Understanding a selector from within a dict


code:
for engine in engines:
    print engine[0]

rawDescription: Dealing with a list of tuples - SQLite3 & Python 2.7


code:
from lxml import etree as ET

data = """<organizations>
    <organization>
        <orgID>152</orgID>
        <orgName>This is A</orgName>
    </organization>
<organization>
        <orgID>1352</orgID>
        <orgName>This is B</orgName>
    </organization>
    <organization>
        <orgID>1522</orgID>
        <orgName>This is C</orgName>
    </organization>
    <organization>
        <orgID>1512</orgID>
        <orgName>This is D</orgName>
    </organization>
</organizations>"""

tree = ET.fromstring(data)
print tree.xpath('//organization[descendant::text()="1512"]/orgName/text()')

rawDescription: Find Node value in xml using etree

description: Find Node value in xml using etree


code:
with open('filename.ext') as input_file:
    for i, line in enumerate(input_file):
        print line,
print "{0} line(s) printed".format(i+1)

rawDescription: python looping through input file

description: Python looping through input file


code:
def print_keyword_args(**kwargs):
    # kwargs is a dict of the keyword args passed to the function
    for key, value in kwargs.iteritems():
        print "%s = %s" % (key, value)

print_keyword_args(first_name="John", last_name="Doe")

rawDescription: Understanding kwargs in Python

description: Understanding kwargs in Python


code:
import wx

class GUI(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(700, 400))

        panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE)
        sizerControl = wx.GridBagSizer(hgap=4,vgap = 4)

        lblTitle = wx.StaticText(panelControl, label="Pick Scores")
        self.btnRoll = wx.Button(panelControl, label="Roll!")

        sizerControl.Add(lblTitle, pos=(0,0),
                         flag=wx.ALIGN_CENTER|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
        sizerControl.Add(self.btnRoll, pos=(0,1),
                         span=(1,5), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)

        rboxPick = ["Default", "Strength", "Dexterity", "Constitution",
                    "Intelligence", "Wisdom", "Charisma"
                    ]
        labels = ["One", "Two", "Three", "Four"]

        #Create, layout and bind the RadioBoxes
        for row, label in enumerate(labels):
            lbl = wx.StaticText(panelControl)
            rbox = wx.RadioBox(panelControl, label="Roll %s"%(label), choices=rboxPick)
            self.Bind(wx.EVT_RADIOBOX, self.onRadioBox, rbox)
            sizerControl.Add(rbox, pos=(row+1, 1), span=(1,5),
                             flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=2)

        sizerMain = wx.BoxSizer()
        sizerMain.Add(sizerControl)
        panelControl.SetSizerAndFit(sizerMain)

    def onRadioBox(self, evt):
        """Event handler for RadioBox.."""

        rbox = evt.GetEventObject()#Get a reference to the RadioBox
        rboxLbl = rbox.GetLabel()   #We can identify the RadioBox with its label
        selection = rbox.GetSelection()

        print rboxLbl
        print selection

        if rboxLbl == "Roll One":
            #do something
            pass
        elif rboxLbl == "Roll Two":
             #do something else
            pass

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = GUI(None, -1, "")
    frame.Show(1)
    app.MainLoop()

rawDescription: wxpython - Creating Unique GUI Elements With a Loop

description: Wxpython - Creating Unique GUI Elements With a Loop


code:
from itertools import product, izip
l = [[1,2,15,16], [6,7,10,11], [3,8,9,14], [4,5,12,13]]
prod = list(product(*l)) # get all possible products of the elements

def grouper(iterable, n):
    args = [iter(iterable)] * n
    return izip(*args)
                                     # 4 choices for each list so 4*4*4*4= 256
groups = grouper(prod,len(prod)/ 4)  # group prod in 4 equal groups(len of each sub list)

count = 0
flatten_check = []  # keep list of elements of all products that equal 34
for comb in groups:
    # check if any product is equal to 34 in groups
    check = [c for c in comb if sum(c) == 34]
    flatten_check += [y for x in check for y in x]
    if check:
        count += 1 # if at least one of the products for each comb equals 34 add 1 to  counter
flatten_l = [ x for y in l for x in y] # flatten original list to compare elements

# if each comb had a product that equaled 34, count should equal 4
# if we used all elements in l we satisfied our requirement
if count == 4 and all(item in flatten_check for item in flatten_l):
    print l # print original list

rawDescription: How to do a three level for loop

description: Do a three level for loop


code:
from collections import defaultdict, Counter
from nltk.corpus import brown

# Keeps words and pos into a dictionary
# where the key is a word and
# the value is a counter of POS and counts
word_tags = defaultdict(Counter)
for word, pos in brown.tagged_words():
    word_tags[word][pos] +=1

# To access the POS counter.
print 'Red', word_tags['Red']
print 'Marlowe', word_tags['Marlowe']
print

# Greatest number of distinct tag.
word_with_most_distinct_pos = sorted(word_tags, key=lambda x: len(word_tags[x]), reverse=True)[0]

print word_with_most_distinct_pos
print word_tags[word_with_most_distinct_pos]
print len(word_tags[word_with_most_distinct_pos])

rawDescription: NLTK Brown Corpus Tags

description: NLTK Brown Corpus Tags


code:
a = filter(None, iterable)
for item in a:
    print item

rawDescription: Regarding the use of filter()

description: Regarding the use of filter()


code:
from itertools import groupby
from Bio       import SeqIO

records = list(SeqIO.parse(file('spoo.fa'),'fasta'))

def seq_getter(s): return str(s.seq)
records.sort(key=seq_getter)

for seq,equal in groupby(records, seq_getter):
  ids = ','.join(s.id for s in equal)
  print '>%s' % ids
  print seq

rawDescription: best way to compare sequence of letters inside file?

description: Compare sequence of letters inside file


code:
def triangle(n):
    s = ""
    for i in xrange(0,n):
        s +=  "{}".format((i+1)%10)
        print s
triangle(15)

rawDescription: How to create a numeric triangle with user input in python?

description: Create a numeric triangle with user input in python


code:
def find_usb(self):
    bus = dbus.SystemBus()
    ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
    ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')

    for dev in ud_manager.EnumerateDevices():
        device_obj = bus.get_object("org.freedesktop.UDisks", dev)
        device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
        if device_props.Get('org.freedesktop.UDisks.Device', "DriveConnectionInterface") == "usb" and device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartition"):
            if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted"):
                device_file = device_props.Get('org.freedesktop.UDisks.Device', "DeviceFile")
                #print device_file
                yield device_file

            else:
                print "Device not mounted"

    return found

rawDescription: Python - Returning list/ arry from function


code:
your_file=open("file_name.ext")
without_blanks=[x for x in your_file if not x.isspace()]
your_file.close()

for line in without_blanks:
    print line

rawDescription: Delete a blank line from a file using python


code:
def stripString(ann):
    return ann.strip().lstrip("(").rstrip(")").split("..")

def parseAnnotation(ann):
    return [tuple(map(int, stripString(j))) for j in ann.split(",")]

print parseAnnotation('(459..521),(1834..2736)')

rawDescription: how do I make a list of tuples from a string?

description: Make a list of tuples from a string


code:
import numpy as np
a = np.arange(12)**2
b = a.reshape(4,3)
print np.std(b, axis=1)

rawDescription: Apply function to 3 elements at a time in numpy


code:
def first():
    return "In function first"

def second():
    return "In function section"

print first(), second()

rawDescription: Python Coding (call function / return values)


code:
start = 2011
end = 2220

with open('file.log') as f:
    for line_number, line in enumerate(f):
        if line_number > end:
            break
        if line_number > start:
            print line

rawDescription: How can I get select specific lines of a text file in python?

description: Get select specific lines of a text file in python


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

Base = declarative_base()

role_to_user = Table('role_to_user', Base.metadata,
    Column('role_id', Integer, ForeignKey('role.id'), primary_key=True),
    Column('user_id', Integer, ForeignKey('user.id'), primary_key=True),
)

class Role(Base):
    __tablename__ = 'role'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)

    discrim = select([Role.name])
    discrim_col = discrim.label("foo")
    type = column_property(discrim_col)
    roles = relationship("Role", secondary=role_to_user)
    __mapper_args__ = {'polymorphic_on': discrim_col}

User.discrim.append_whereclause(Role.id==role_to_user.c.role_id)
User.discrim.append_whereclause(role_to_user.c.user_id==User.id)

class Customer(User):
    __tablename__ = 'customer'
    id = Column(Integer, ForeignKey(User.id), primary_key=True)
    __mapper_args__ = {'polymorphic_identity':'customer'}

class Mechanic(User):
    __tablename__ = 'mechanic'
    id = Column(Integer, ForeignKey(User.id), primary_key=True)

    __mapper_args__ = {'polymorphic_identity':'mechanic'}

e = create_engine('sqlite://', echo=True)

Base.metadata.create_all(e)

s = Session(e)

m, c = Role(name='mechanic'), Role(name='customer')
s.add_all([m, c])
s.add_all([Mechanic(roles=[m]), Customer(roles=[c])])
s.commit()

print Session(e).query(User).all()

rawDescription: How do I correctly model joined table inheritance with discriminator based on Role in SQLAlchemy

description: Correctly model joined table inheritance with discriminator based on Role in SQLAlchemy


code:
import numpy as np

# Generate some random floating-point data
x = np.random.random(100)

# Convert it to fixed-length strings with a maximum length of 5 characters
y = x.astype('|S5')

print 'Original Array'
print x
print 'Converted to fixed-length strings'
print y

rawDescription: Converting long numpy array for string efficiently


code:
import json

obj1 = json.loads('{"key": {"field_string": ["string"],"field_num": 1}}')
obj2 = json.loads('{"key": {"field_string": ["another_string"],"field_num": 2}}')

def combine(obj1, obj2):
    for k, v in obj1.items():
        if k in obj2:
            if isinstance(v, dict):
                combine(obj1[k], obj2[k])
            else:
                # assume list or int based on problem description
                obj1[k] = obj1[k] + obj2[k]

combine(obj1, obj2)
print json.dumps(obj1)

rawDescription: Python 2.7 - Take two JSON's and combine the string field values together and add int fields


code:
zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

L = [] #create a list

for num in zipcode:
    L.append(codes[int(num)]) #append the values to a list

print ''.join(L) #join them together and then print

rawDescription: How to test each specific digit or character

description: Test each specific digit or character


code:
for potential_key in range(10):
    print x[potential_key]

rawDescription: (python) How do I obtain specific entries from a dictionary (using keys) as I do with an array?


code: print "\"A word that needs quotation marks\""

rawDescription: Python - Using quotation marks inside quotation marks


code:
from selenium.webdriver.firefox import webdriver

driver = webdriver.WebDriver()
driver.get('http://www.walmart.com/ip/29701960?page=seeAllReviews')

for review in driver.find_elements_by_class_name('BVRRReviewDisplayStyle3Main'):
    title = review.find_element_by_class_name('BVRRReviewTitle').text
    rating = review.find_element_by_xpath('.//div[@class="BVRRRatingNormalImage"]//img').get_attribute('title')
    print title, rating

driver.close()

rawDescription: Pin down exact content location in html for web scraping urllib2 Beautiful Soup


code:
def foo(bar=1, baz=[2,3]):
    print bar, baz

foo()
foo(baz=4)
foo(10, 20)

rawDescription: Automatically remove named parameters from a Python function call


code:
from itertools import groupby
from operator import itemgetter

table = '''\
field1      field2  field3                  field4  field5
3/16/2012   a       KONDRA I KOMANG         1       TERAPI OZON 60 MENIT
3/16/2012   b       WARTI NI WAYAN          1       TERAPI OZON 60 MENIT
3/16/2012   c       MARDIKA I GUSTI PUTU    1       TERAPI OZON 60 MENIT
3/16/2012   d       DARMIASIH NI KOMANG     1       TERAPI OZON 60 MENIT
3/19/2012   e       DARMIASIH NI KOMANG     0.5     Orbitalized 240T
3/19/2012   e       DARMIASIH NI KOMANG     0.5     Octogenarian 240T
3/19/2012   e       DARMIASIH NI KOMANG     1       TERAPI AKUPUNKTUR
3/29/2012   f       ARNI NI MADE            3       Lingzhi 60C
3/29/2012   f       ARNI NI MADE            1       Octogenarian 240T
'''

# Setup bounds for fields in fixed width table.
fields = (0,12,20,44,52,None)
bounds = zip(fields[:-1],fields[1:])

# Parse the data
data = []
for line in table.splitlines():
    data.append([line[a:b].rstrip() for a,b in bounds])

# Sort the data by field3,field2 for groupby.
data.pop(0) # remove header line
data.sort(key=itemgetter(2,1))

# Output result header
print '{:24}{}'.format('field1','field2')

# Output result lines
for field3,items in groupby(data,key=itemgetter(2)):
    result = []
    for field1,field1_items in groupby(items,key=itemgetter(0)):
        result.append(';{} ({})'.format(field1,' + '.join(' '.join(item[3:]) for item in field1_items)))
    print '{:24}{}'.format(field3,' '.join(result))

rawDescription: Python list to insert to other table or list


code:
for n in result:
    print n

(4, array(['n', 'sn'], dtype=object))
(3, array(['sp', 'p'], dtype=object))

rawDescription: Modifying Python DataFrame rows with duplicates


code: print e.get()

rawDescription: Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label?


code:
def inv_range(start, stop, step):
  for val in range(start, stop):
    if (val - start) % step != 0:
      yield val

print list(inv_range(0,50,3))

rawDescription: Generate inverse sequence

description: Generate inverse sequence


code:
class B:
    def __init__(self): print "__init__"

def k(self):
    print "Instantiating"
    p(self)

p, B.__init__ = B.__init__, k
s = B()

rawDescription: get notified whenever a certain class I don't control is instantiated in python

description: Get notified whenever a certain class I don't control is instantiated in python


code: print "a %s %d"

rawDescription: Escaping % in MySQLdb

description: Escaping % in MySQLdb


code:
if 'myremote' in local('git remote', capture=True):
    print 'Remote \'myremote\' already exists.'
else:
    local('git remote add myremote git@myremote.com:myrepo.git')

rawDescription: Interpret (and use) the output from Fabric local command


code:
def display(var):
    import inspect, re
    callingframe = inspect.currentframe().f_back
    cntext = "".join(inspect.getframeinfo(callingframe, 5)[3]) #gets 5 lines
    m = re.search("display\s+\(\s+(\w+)\s+\)", cntext, re.MULTILINE)
    print m.group(1), type(var), var

rawDescription: retrieving a variable's name in python at runtime?

description: Retrieving a variable's name in python at runtime


code:
if a.find('/*!*/;') > -1:
   print 'found'

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

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


code:
espf = []

for x in l:
    # Restart counters for every row
    espf_current = [] # Will store any sequences of 2
    contador = 0      # Will count consecutive 2's

    for c in x:
        celula = c
        if celula == 2:
            contador += 1    # Count number of 2
        elif celula == 1:
            if contador > 0: # Store any 2 before 1
                espf_current += [contador]
            contador = 0
    if contador > 0:         # Check if the row ends in 2
        espf_current += [contador]

    # Store results of this row in the final results
    espf += [tuple(espf_current)]
print tuple(espf)

rawDescription: Sum of specific elements in a list, if they are consecutive (python)


code:
def is_sorted(x,i):
    if i >= len(x):
        return True
    elif x[i] <= x[i-1]:
        return False
    else:
        return is_sorted(x,i+1)

x = "abcadef"
y = "aabcdef"
z = "abcdef"
print is_sorted(x, 1)
print is_sorted(y, 1)
print is_sorted(z, 1)

rawDescription: Why do I get None instead of True/False in python?


code:
import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
import numpy as np

nbins = 50
labeled_bin = 200

fig, ax = plt.subplots()

series = pd.Series(np.random.normal(0, 100, 1000))

## plot the histogram and return the bin position and values
ybins, xbins, _ = ax.hist(series, bins=nbins)

## find out in which bin belongs the position where you want the label
ind_bin = np.where(xbins >= labeled_bin)[0]
if len(ind_bin) > 0 and ind_bin[0] > 0:
    ## get position and value of the bin
    x_bin = xbins[ind_bin[0]-1]/2. + xbins[ind_bin[0]]/2.
    y_bin = ybins[ind_bin[0]-1]
    ## add the arrow
    ax.annotate("",
                xy=(x_bin, y_bin + 5), xycoords='data',
                xytext=(x_bin, y_bin + 20), textcoords='data',
                arrowprops=dict(arrowstyle="->",
                                connectionstyle="arc3"),
                                )
else:
    print "Labeled bin is outside range"

rawDescription: Labelling a matplotlib histogram bin with an arrow


code:
l2=[]
with open('list_data2.txt') as f:
  for line in f:
   l=line.strip().split(',')
   l2.append(sorted(l[:2])+sorted(l[2:],reverse=True))

print l2

rawDescription: How do I sort a numerical list with names attached?

description: Sort a numerical list with names attached


code:
from numpy import zeros, empty

def insert_dtype(x, position, new_dtype, new_column):
    if x.dtype.fields is None:
        raise ValueError, "`x' must be a structured numpy array"
    new_desc = x.dtype.descr
    new_desc.insert(position, new_dtype)
    y = empty(x.shape, dtype=new_desc)
    for name in x.dtype.names:
        y[name] = x[name]
    y[new_dtype[0]] = new_column
    return y

x = zeros((2,), dtype='i4,f4,a10')
x[:] = [(1, 2., 'Hello'), (2, 3., 'World')]

new_dt = ('my_alphabet', '|S3')
new_col = ['abc', 'def']

x = insert_dtype(x, 1, new_dt, new_col)

rawDescription: Insert field into structured array at a specific column index


code:
import scipy.sparse as sp
import numpy as np
D=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))
A=sp.csr.csr_matrix(np.diagflat(np.random.random(100)))
y=np.random.random(100)
y_next = A*(D*y)
print y_next.shape, type(y_next)
print y_next

rawDescription: Shape inconsistence in my code based on numpy/scipy package


code:
def squareEach(nums):
    answer = []
    for num in nums:
        answer.append(num*num)
    return answer

def sumList(nums):
    answer = 0
    for num in nums:
        answer += num
    return answer

def toNumbers(strList):
    answer = []
    for numStr in strList.split():
        try:
            num = int(numStr)
            answer.append(num)
        except:
            pass
    return answer

def main():
    print "** Program to find sum of squares from numbers in a file **"
    fileName = raw_input("What file are the numbers in? ")
    sum = 0
    with open(fileName, 'r') as infile:
        for line in infile:
            nums = toNumbers(line)
            squares = squareEach(nums)
            sum += sumList(squares)
        print sum

rawDescription: Computing the sum of squares from a file in Python


code:
# Obviously, this should be more interesting..
import sys
print "cd", sys.argv[1]

rawDescription: Changing prompt working directory via Python script


code:
import ruamel.yaml as yaml

# the scalar emitter from emitter.py
def process_scalar(self):
    if self.analysis is None:
        self.analysis = self.analyze_scalar(self.event.value)
    if self.style is None:
        self.style = self.choose_scalar_style()
    split = (not self.simple_key_context)
    # VVVVVVVVVVVVVVVVVVVV added
    try:
        x = int(self.event.value)  # might need to expand this
    except:
        # we have string
        if split:
            self.style = "'"
        else:
            self.style = '"'
    # ^^^^^^^^^^^^^^^^^^^^
    # if self.analysis.multiline and split    \
    #         and (not self.style or self.style in '\'\"'):
    #     self.write_indent()
    if self.style == '"':
        self.write_double_quoted(self.analysis.scalar, split)
    elif self.style == '\'':
        self.write_single_quoted(self.analysis.scalar, split)
    elif self.style == '>':
        self.write_folded(self.analysis.scalar)
    elif self.style == '|':
        self.write_literal(self.analysis.scalar)
    else:
        self.write_plain(self.analysis.scalar, split)
    self.analysis = None
    self.style = None
    if self.event.comment:
        self.write_post_comment(self.event)

infile = yaml.load(open('yamlfile'), Loader=yaml.RoundTripLoader)

for key, value in infile['main'].items():
    if key == 'keepalivetimeout':
        item = value['item']
        item['keepalivetimeout'] = 400

dd = yaml.RoundTripDumper
dd.process_scalar = process_scalar

print '---'
print yaml.dump(infile, Dumper=dd)

rawDescription: Pyyaml - Using different styles for keys and integers and strings


code:
def add(a, b):
    print "ADD %d + %d" % (a, b)
    return float(a + b) # WE RETURN A FLOAT

def subtract(a, b):
    print "SUBTRACT %d - %d" % (a, b)
    return float(a - b) # WE RETURN A FLOAT

def divide(a, b):
    print "DIVIDE %d / %d" % (a, b)
    return float(a / b) # WE RETURN A FLOAT

print "Solve 24 + 34 / 100 - 1023?"
what = subtract(add(24, divide(34.0,100)),1023)

print "The Answer is", what

rawDescription: learnpythonthehardway exercise 21 write out a formula

description: Learnpythonthehardway exercise 21 write out a formula


code:
from itertools import groupby, product
from operator import itemgetter

groups = [list(g) for _,g in groupby(data,key=itemgetter(0))]
for i in range(1,len(groups)+1):
    print list(product(*groups[:i]))

rawDescription: Split the python list of list

description: Split the python list of list


code:
with open('database.txt', 'r') as f:
    results = dict()

    for line in f.read().strip().split('--------------------'):
        if line.strip() != "":
            first_name, last_name, phone, email = line.strip().split('\n')
            results[first_name] = dict(first_name=first_name, last_name=last_name, phone=phone, email=email)

    print results

rawDescription: reading lines from file to make 2D dictionary [python]

description: Reading lines from file to make 2D dictionary [python]


code:
print 'Zoomed by 2x along the last two axes:'
print ndimage.zoom(data, (1, 2, 2))

rawDescription: Resampling a numpy array representing an image


code:
shop = ["Apple" , "orange", "mangoo" , "Tomato" , "grape"]
my_dict = {}
#List Shop

x = """
1. apple
2. orange
3. mangoo
4. tomato
5. grape """
print x
#Screen showing in monitor

while True:
    buy = int(raw_input('what do u want to buy?" (use number 1-7): '))
    buy = shop[buy-1]
    print "You want" , buy
    equal = int(raw_input('how much? : '))
    my_dict[buy] = my_dict.get(buy, 0) + equal
    print my_dict, x

rawDescription: Dictionary up to date

description: Dictionary up to date


code:
import subprocess
import glob
import os.path
import itertools

def sort_by_ending(filename):
    filename = os.path.split(filename)[1]
    if '_' in filename:
        return filename.split('_')[1:]
    else:
        return [filename]

folder = r'c:\test'

with open('output.txt', 'w') as f_output:
    # ap and sta files sorted by filename ending
    files = sorted(glob.glob(os.path.join(folder, 'ap*.hdf5')) + glob.glob(os.path.join(folder, 'sta*.hdf5')), key=sort_by_ending)

    for ap, sta in itertools.izip(*[iter(files)]*2):
        print "'{}' and '{}'".format(os.path.split(ap)[1], os.path.split(sta)[1])
        # Do we have an ap/sta pair?
        if os.path.split(ap)[1].startswith('ap') and os.path.split(sta)[1].startswith('sta'):
            # Launch the Python script with the required parameters
            p = subprocess.Popen(['python.exe', 'result.py', ap, sta], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            # Write stdout to a file and stderr to the screen
            f_output.write(out)
            print err
        else:
            print '{} is missing'.format(sta)
            break

rawDescription: Repeatedly execute python script over files in folder arranged in order of date


code:
from sys import stdout

from twisted.python.log import startLogging, err

from twisted.internet import reactor
from twisted.internet.defer import Deferred

from twisted.conch.ssh.common import NS
from twisted.conch.scripts.cftp import ClientOptions
from twisted.conch.ssh.filetransfer import FileTransferClient
from twisted.conch.client.connect import connect
from twisted.conch.client.default import SSHUserAuthClient, verifyHostKey
from twisted.conch.ssh.connection import SSHConnection
from twisted.conch.ssh.channel import SSHChannel

class SFTPSession(SSHChannel):
    name = 'session'

    def channelOpen(self, whatever):
        d = self.conn.sendRequest(
            self, 'subsystem', NS('sftp'), wantReply=True)
        d.addCallbacks(self._cbSFTP)

    def _cbSFTP(self, result):
        client = FileTransferClient()
        client.makeConnection(self)
        self.dataReceived = client.dataReceived
        self.conn._sftp.callback(client)

class SFTPConnection(SSHConnection):
    def serviceStarted(self):
        self.openChannel(SFTPSession())

def sftp(user, host, port):
    options = ClientOptions()
    options['host'] = host
    options['port'] = port
    conn = SFTPConnection()
    conn._sftp = Deferred()
    auth = SSHUserAuthClient(user, options, conn)
    connect(host, port, options, verifyHostKey, auth)
    return conn._sftp

def transfer(client):
    d = client.makeDirectory('foobarbaz', {})
    def cbDir(ignored):
        print 'Made directory'
    d.addCallback(cbDir)
    return d

def main():
    startLogging(stdout)

    user = 'exarkun'
    host = 'localhost'
    port = 22
    d = sftp(user, host, port)
    d.addCallback(transfer)
    d.addErrback(err, "Problem with SFTP transfer")
    d.addCallback(lambda ignored: reactor.stop())
    reactor.run()

if __name__ == '__main__':
    main()

rawDescription: twisted conch filetransfer

description: Twisted conch filetransfer


code:
from collections import Counter

import nltk

text = nltk.corpus.genesis.words('english-kjv.txt')
bigrams = nltk.bigrams(text)
cfd_bigrams = Counter(bigrams)
cfd_unigrams = Counter(list(text))

print "p('said','unto') =", cfd_bigrams[u'said', u'unto'] / float(sum(cfd_bigrams.values()))

print "p('said'|'unto') =", (cfd_bigrams[u'said', u'unto'] / float(sum(cfd_bigrams.values()))) / cfd_unigrams[u'unto']

print "p('beginning','the') =", cfd_bigrams[u'beginning', u'the']

rawDescription: What is the Probability of ‘begining’ given ‘the’?


code:
i = 0
while i < 7:
    try:
        if someCondition(i):
            i = 5
        elif otherCondition(i):
            continue
        print 'i = %d' % i
    finally:
        i += 1

rawDescription: How do I use a C-style for loop in Python?

description: Use a C-style for loop in Python


code:
def foobar(*args, **kwargs):
    print args
    print kwargs

foobar(1, 2, a='3', spam='eggs')

rawDescription: what does double star followed by variable name mean in python?

description: What does double star followed by variable name mean in python


code:
'université'
len('université')
'université'.decode('utf8')
print 'université'

rawDescription: Python Latin Characters and Unicode

description: Python Latin Characters and Unicode


code:
class A(object):
    def methodX(self):
        print "A.methodX"
    def methodY(self):
        print "A.methodY"
        self.methodX()

class B(A):
    def methodZ(self):
        print "B.methodZ"
        self.methodY()

b = B()
b.methodZ()

rawDescription: Calling a base class method from a derived class method in Python


code:
from sympy import *

a,b,c,d,e = symbols("a,b,c,d,e")
x = a**2 + b/(c-min(d,e))
y = c+sqrt(x*e)

values = {a:73,b:13,c:100,d:20,e:18}

print x
print y

print x.subs(values)
print y.subs(values)

print N(x.subs(values))
print N(y.subs(values))

rawDescription: Open source language/library/representation format that allows treating numeric formulas as objects?


code:
#!/usr/bin/env python
from subprocess import Popen
command = ['/usr/bin/ssh', 'localhost', '/home/gd/test/python/back.sh']
print Popen(command)
print "Python done"

rawDescription: python: os.spawn . cannot start bash process on background

description: Python: os.spawn . cannot start bash process on background


code:
class Tiger(object):
    def __init__(self, num_stripes):
        self.num_stripes = num_stripes
        self.num_black_stripes = self.get_black_stripes()
        self.num_yellow_stripes = self.get_yellow_stripes()
    def get_black_stripes(self):
        return self.num_stripes / 2
    def get_orange_stripes(self):
        return self.num_stripes / 2

big_tiger = Tiger(num_stripes=200)
little_tiger = Tiger(num_stripes=30)

# Now we can do logic without having to keep re-calculating values
if big_tiger.black_stripes > little_tiger.orange_stripes:
    print "Big tiger has more black stripes than little tiger has orange"

rawDescription: Python classes self.variables

description: Python classes self.variables


code: print np.sign(df2.f - df2.g).replace({-1:"f", 1:"g", 0:"fg"})

rawDescription: Simulating event system using pandas

description: Simulating event system using pandas


code:
print df
#   START_TIME  END_TIME  TRIAL_No itemnr
#0     2403950   2413067  Trial: 1    P14
#1     2413378   2422499  Trial: 2    P03
#2     2422814   2431931  Trial: 3    P13
#3     2432246   2441363  Trial: 4    P02
#4     2523540   2541257  Trial: 5    P11
#5     2541864   2560297  Trial: 6    P10
#6     2560916   2577249  Trial: 7    P05

#PREDPROCESSING
#helper column for matching start and end rows
df['row'] = range(len(df))

#reshape to df - every row two times repeated for each date of START_TIME and END_TIME
starts = df[['START_TIME','TRIAL_No','itemnr','row']].rename(columns={'START_TIME':'Bin_time'})
ends = df[['END_TIME','TRIAL_No','itemnr','row']].rename(columns={'END_TIME':'Bin_time'})
df = pd.concat([starts, ends])
df = df.set_index('row', drop=True)
df = df.sort_index()

#convert miliseconds to timedelta for resampling by time 100ms
df['Bin_time'] = df['Bin_time'].astype('timedelta64[ms]')

rawDescription: Resampling with start and end date columns


code:
#!/usr/local/bin/python
# by Daniel Rosengren modified by e-satis

import sys, time
stdout = sys.stdout

BAILOUT = 16
MAX_ITERATIONS = 1000

class Iterator(object) :

    def __init__(self):

        print 'Rendering...'
        for y in xrange(-39, 39):
            stdout.write('\n')
            for x in xrange(-39, 39):
                if self.mandelbrot(x/40.0, y/40.0) :
                    stdout.write(' ')
                else:
                    stdout.write('*')

    def mandelbrot(self, x, y):
        cr = y - 0.5
        ci = x
        zi = 0.0
        zr = 0.0

        for i in xrange(MAX_ITERATIONS) :
            temp = zr * zi
            zr2 = zr * zr
            zi2 = zi * zi
            zr = zr2 - zi2 + cr
            zi = temp + temp + ci

            if zi2 + zr2 > BAILOUT:
                return i

        return 0

t = time.time()
Iterator()
print '\nPython Elapsed %.02f' % (time.time() - t)

rawDescription: PyLint, PyChecker or PyFlakes?

description: PyLint, PyChecker or PyFlakes


rawDescription: Ordinal numbers replacement

description: Ordinal numbers replacement


code:
from scrapy.selector import Selector

sel = Selector(text = html_string)

for node in sel.css('a *::text'):
    print node.extract()

rawDescription: How to get innerHTML of a node using scrapy Selector?

description: Get innerHTML of a node using scrapy Selector


code:
import re

compiled = re.compile(r"""
    .*?                                # Leading junk
    (?P<ipaddress>\d+\.\d+\.\d+\.\d+)  # IP address
    .*?                                # Trailing junk
    """, re.VERBOSE)
str = "IP 1.2.3.4 is currently trusted in the white list, but it is now using a new trusted certificate."
m = compiled.match(str)
print m.group("ipaddress")

rawDescription: How to extract a word from text in Python

description: Extract a word from text in Python


code:
from StringIO import StringIO
import pandas as pd
data = StringIO("""b e a d c
b 0.0 0.1 0.3 0.2 0.5
e 0.1 0.0 0.4 0.9 0.3
a 0.3 0.4 0.0 0.7 0.6
d 0.2 0.9 0.7 0.0 0.1
c 0.5 0.3 0.6 0.1 0.0
""")
df = pd.read_csv(data, sep=" ")
print df.sort_index().sort_index(axis=1)

rawDescription: rearranging matrix with named column/rows python

description: Rearranging matrix with named column/rows python


code:
string = "-1, -2, 3, 4, 5"
print [int(el) for el in string.split(',')]

rawDescription: Convert a string into a list with integers including negative numbers


code: if options.verbose : print ""

rawDescription: My first python program: can you tell me what I'm doing wrong?


code:
class test:
    def __init__(self):
        self.a = None
        self.b = None
        self.c = None
    def prod(self):
        return self.a * self.b
trial = test()
trial.a = 4
trial.b = 5
print trial.prod()

rawDescription: How to create a simple function within a class in Python?

description: Create a simple function within a class in Python


code:
import datetime
now = datetime.datetime.now()
end_time = now + datetime.timedelta(hours=6)
print now
print end_time

rawDescription: Accessing datetime.now() values in Python


code:
import cssutils

data = """
p{font-family:"Verdana"}

p{font-family:"Comic Sans"}

p{font-family:"Times New Roman", Times, serif}
"""

sheet = cssutils.parseString(data)

for rule in sheet:
    if rule.type == rule.STYLE_RULE:
        # find property
        for property in rule.style:
            if property.name == 'font-family':
                print property.value

rawDescription: CSS parsing Python for finding font-family


code:
print 1 < x # [False, True, True]
print x < 3 # [True, True, False]
print 1 < x < 3 # [True, True, False]

rawDescription: What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)


code:
def find_if_composite_memo(s, set_, memo):
    print "ENTER", s, '\t', memo
    n = len(s)
    if len(set_) == 0 or n == 0:
        return False
    if s in memo:
        print "Found", s, memo[s]
        return memo[s]
    if s in set_:
        return True
    for i in range(1, n+1):
        this_s = s[:i]
        if this_s in set_:
            if find_if_composite_memo(s[i:], set_, memo):
                memo[s] = True
                memo[s[i:]] = True
                print "Add", memo
                return True
    memo[s] = False
    return False

b = ["cat", "banana", "dog", "nana",
     "walk", "walker",
     "dogcat", "dogwalker",
     "dogwalkerbanana",
     "catwalkerbanana-FAIL"]
print(find_longest_word(b))

rawDescription: Time complexity of python code for finding the longest word that can be made of other words in the list (follow-up)


code:
alphabet = " abcdefghijklmnopqrstuvwxyz"
keyString ="kjihgfedcba zyxwvutsrqponml"

def mapInToOut(inChar, alphFrom, alphTo):
    index = 0
    found = 0
    for char in alphFrom:
        if(char == inChar):
            found = index #remember it in a variable found
            break
        else:
            index +=1
    outChar = alphTo[found]
    return(outChar)

def encode(inString):
    outString = ''
    for char in inString:
        outString += mapInToOut(char, alphabet, keyString)
    return(outString)

def decode(inString):
    outString = ''
    for char in inString:
        outString += mapInToOut(char, keyString, alphabet)
    return(outString)

print ("this is a crazy bit of news to share we attack at dawn")
encoded_msg = encode("this is a crazy bit of news to share we attack at dawn")
print encoded_msg
print decode(encoded_msg)

rawDescription: Generalizing two similar functions into one


code:
@decallmethods(login_testuser)
class TestCase(object):
    def setUp(self):
        pass

    def test_1(self):
        print "test_1()"

    def test_2(self):
        print "test_2()"

rawDescription: Applying python decorators to methods in a class


code:
import json
json_data = json.load(open("myfile.json"))

def walk_keys(obj, path=""):
    if isinstance(obj, dict):
        for k, v in obj.iteritems():
            for r in walk_keys(v, path + "." + k if path else k):
                yield r
    elif isinstance(obj, list):
        for i, v in enumerate(obj):
            s = "[" + str(i) + "]"
            for r in walk_keys(v, path + s if path else s):
                yield r
    else:
        yield path

for s in sorted(walk_keys(json_data)):
    print s

rawDescription: Print unique JSON keys in dot notation using Python


code:
import win32api

def getFileDescription(windows_exe):
    try:
        language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
        stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
        description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
    except:
        description = "unknown"

    return description

print getFileDescription(r"C:\Program Files\Internet Explorer\iexplore.exe")

rawDescription: Get Application Name from .exe file in python


code:
a_cols = ["col_a","col_a1"]
b_cols = ["col_b","col_b1"]
df["flag"] = df.eval(" and ".join("%s==%s" % pair for pair in zip(a_cols,b_cols)))
print df

rawDescription: Compare multiple sets of columns to produce boolean result column


code:
from math import log10

if log10(n) < -5:
    print "%e" % n
else:
    print "%f" % n

rawDescription: Python float formatting - like "g", but with more digits


code:
def print_list(l,size):
    for x in l:
        print x[2],

rawDescription: Print list as array Python

description: Print list as array Python


code:
import AgglomerativeClustering
from scipy.cluster.hierarchy import linkage
import numpy as np
import time

l = 1000; iters = 50
d = [np.random.random(100) for _ in xrange(1000)]

t = time.time()
for _ in xrange(iters):
    clustering = AgglomerativeClustering(n_clusters=l-1,
        affinity='euclidean', linkage='complete')
    clustering.fit(d)
scikit_time = (time.time() - t) / iters
print 'scikit-learn Time: {0}s'.format(scikit_time)

t = time.time()
for _ in xrange(iters):
    linkage(d, 'complete')
scipy_time = (time.time() - t) / iters
print 'SciPy Time: {0}s'.format(scipy_time)

print 'scikit-learn Speedup: {0}'.format(scipy_time / scikit_time)

rawDescription: sklearn agglomerative clustering linkage matrix

description: Sklearn agglomerative clustering linkage matrix


code:
from lxml import etree

def ucfirst(context, s):
    return s.capitalize()

ns = etree.FunctionNamespace("mynamespace")
ns['ucfirst'] = ucfirst

xml = etree.XML('''
<allusers>
 <user>
  <uid>bob</uid>
  <id>1</id>
 </user>
 <user>
  <uid>joe</uid>
  <id>2</id>
 </user>
</allusers>''')

xsl = etree.XML('''\
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:f="mynamespace" exclude-result-prefixes="f">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">

Users

    <xsl:for-each select="user">

        <xsl:value-of select="f:ucfirst(concat(string(uid), string(id)))"/>

    </xsl:for-each>

 </xsl:template>
</xsl:stylesheet>
''')

transform = etree.XSLT(xsl)
result = transform(xml)
print result

rawDescription: How to use python functions and variables inside XSLT?

description: Use python functions and variables inside XSLT


code:
import sys

def factorial(n):
    fact = 1
    while (n > 0):
        fact = float(fact) * float(n)
        n -= float(1)
        if float(fact) > sys.float_info.max:
            return "Too big"
    return str(fact)

print factorial(50e500)

rawDescription: Avoiding expensive calculations in Python IRC bot


code:
while 1:
    c = pfd.stdout.read(1)
    if not c:
        break
    elif c == '\n': # print line when found
        print repr(out)
        out = ''
    else:
        out += c
        if out.strip() == 'sh#':
            break

rawDescription: detecting end of tty output

description: Detecting end of tty output


code:
from cStringIO import StringIO
from lxml import etree

xmlfile = StringIO("""
<ThisIsMyData>
  <ITEM>
    <Number>0</Number>
    <Flag1>TRUE</Flag1>
    <Flag2>FALSE</Flag2>
    <Flag3>FALSE</Flag3>
    RED
    <Path>C:\\test\\</Path>
    <file>test.png</file>
  </ITEM>
  <ITEM>
    <Number>1</Number>
    <Flag1>TRUE</Flag1>
    <Flag2>FALSE</Flag2>
    <Flag3>FALSE</Flag3>
    Blue
    <Path>c:\\test\\test2\\</Path>
    <file>blue.png</file>
  </ITEM>
 </ThisIsMyData>
""".strip())

datafile = StringIO("""
Label;FullPath
YELLOW;C:\\test\\test.png
YELLOW;c:\\test\\test2\\blue.png
""".strip())

# Read "csv". Simple, no error checking, skip first line.
filenameToLabel = {}
for l,f in (x.strip().split(';') for x in datafile.readlines()[1:]):
  filenameToLabel[f] = l

def first(seq,default=None):
  """xpath helper function"""
  for item in seq:
    return item
  return None

doc = etree.XML(xmlfile.read())

for item in doc.xpath('//ITEM'):
  item_filename = first(item.xpath('./Path/text()'),'').strip() + first(item.xpath('./file/text()'),'').strip()
  label = filenameToLabel.get(item_filename)
  if label is not None:
    first(item.xpath('./Flag1')).text = 'TRUE'
    first(item.xpath('./Flag2')).text = 'FALSE'
    first(item.xpath('./Flag3')).text = 'FALSE'
    first(item.xpath('./Label')).text = label

print etree.tostring(doc)

rawDescription: Lost in XML and Python

description: Lost in XML and Python


code:
import sys, time

def carriage_return():
    sys.stdout.write('\r')
    sys.stdout.flush()

for i in range(100):
    if i % 10 == 0:
        print i,
        carriage_return()
        time.sleep(1)

rawDescription: Problems refreshing stdout line using print with python


code: print "\n".join(['{0:04}'.format(num) for num in xrange(0, 10000)])

rawDescription: how to loop from 0000 to 9999 and convert the number to the relative string?

description: Loop from 0000 to 9999 and convert the number to the relative string


code:
def line_pre_adder(filename, line_to_prepend):
    f = fileinput.input(filename, inplace=1)
    for xline in f:
        if f.isfirstline():
            print line_to_prepend.rstrip('\r\n') + '\n' + xline,
        else:
            print xline,

rawDescription: Prepend line to beginning of a file

description: Prepend line to beginning of a file


code:
soup = BeautifulSoup('''\

    Text with whitespace

No whitespace

    A nested
    tag
    is not a problem

''')
whitespace = u' \t\n\r\x0a'  # extend as needed
for p in soup.find_all('p'):
    for string in list(p.strings):  # copy so we can replace some
        left = string[:1] in whitespace
        right = string[-1:] in whitespace
        if not left and not right:
            continue  # leave be
        new = string
        if left:
            new = ' ' + new.lstrip()
        if right:
            new = new.rstrip() + ' '
        string.replace_with(new)

print str(soup)

rawDescription: Remove empty spaces inside <p> tags using BeautifulSoup


code:
import urllib2,sys
f = open("url_file.txt","a+")
urlhere = sys.argv[1]
print "for url: " + urlhere + ":"

try:
    fileHandle = urllib2.urlopen(urlhere)
    data = fileHandle.read()
    fileHandle.close()
    f.write(urlhere+ "\n")
    f.close()
    print "It exists"

except urllib2.URLError, e:
    print 'PAGE 404: It Doesnt Exist', e

rawDescription: Writing text to txt file in python on new lines?


code:
elements = translation.split(' ',2)
traditionnal = elements[0]
simplified = elements[1]
rest = elements[2]
print "Traditionnal:" + traditionnal
print "Simplified:" + simplified
elems = rest.split(']')
tr = elems[0].strip('[')
print "Pronouciation:" + tr

rawDescription: python regex unicode - extracting data from an utf-8 file

description: Python regex unicode - extracting data from an utf-8 file


code:
edges = [ (u, v, d) if u.isdigit() else (v, u, d)  for u, v, d in G.edges(data=True) ]
print edges

rawDescription: Read bipartite graph

description: Read bipartite graph


code:
def fixed_point(func, x0, args=(), xtol=1e-8, maxiter=500):
    """Find the point where func(x) == x

    Given a function of one or more variables and a starting point, find a
    fixed-point of the function: i.e. where func(x)=x.

    Uses Steffensen's Method using Aitken's Del^2 convergence acceleration.
    See Burden, Faires, "Numerical Analysis", 5th edition, pg. 80

    Example
    -------
    >>> from numpy import sqrt, array
    >>> from scipy.optimize import fixed_point
    >>> def func(x, c1, c2):
            return sqrt(c1/(x+c2))
    >>> c1 = array([10,12.])
    >>> c2 = array([3, 5.])
    >>> fixed_point(func, [1.2, 1.3], args=(c1,c2))
    array([ 1.4920333 ,  1.37228132])

    See also:

      fmin, fmin_powell, fmin_cg,
             fmin_bfgs, fmin_ncg -- multivariate local optimizers
      leastsq -- nonlinear least squares minimizer

      fmin_l_bfgs_b, fmin_tnc,
             fmin_cobyla -- constrained multivariate optimizers

      anneal, brute -- global optimizers

      fminbound, brent, golden, bracket -- local scalar minimizers

      fsolve -- n-dimenstional root-finding

      brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding

    """
    if not isscalar(x0):
        x0 = asarray(x0)
        p0 = x0
        for iter in range(maxiter):
            p1 = func(p0, *args)
            p2 = func(p1, *args)
            d = p2 - 2.0 * p1 + p0
            p = where(d == 0, p2, p0 - (p1 - p0)*(p1-p0) / d)
            relerr = where(p0 == 0, p, (p-p0)/p0)
            if all(relerr < xtol):
                return p
            p0 = p
    else:
        p0 = x0
        for iter in range(maxiter):
            p1 = func(p0, *args)
            p2 = func(p1, *args)
            d = p2 - 2.0 * p1 + p0
            if d == 0.0:
                return p2
            else:
                p = p0 - (p1 - p0)*(p1-p0) / d
            if p0 == 0:
                relerr = p
            else:
                relerr = (p-p0)/p0
            if relerr < xtol:
                return p
            p0 = p
    raise RuntimeError, "Failed to converge after %d iterations, value is %s" % (maxiter,p)

rawDescription: Solve this equation with fixed point iteration


code:
d = {'a': 1, 'b': 2}
s = shelve.open('foo.db')
s.update(d)
print s
{'a': 1, 'b': 2}

rawDescription: allow a shelve object to be pickled in python, pypar and mpich2

description: Allow a shelve object to be pickled in python, pypar and mpich2


code:
import re

dictionary_1 = { 1:'first', 2:'second'}
dictionary_2 = { 1:'January', 2:'February'}
dictionary_3 = { 1996:'asd', 1995:'asd1'}

input1 = raw_input("Enter date:")
lista = re.split(r'[.\/-]', input1)
print "lista: ", lista
day = lista[0]
month = lista[1]
year = lista[2]
everything_ok = False
if dictionary_1.get(int(day)) != None:
   day_print = dictionary_1.get(int(day))
   everything_ok = True
else:
   print "There is no such day"
if dictionary_2.get(int(month)) != None:
   month_print = dictionary_2.get(int(month))
   everything_ok = True
else:
   print "There is no such month"
   everything_ok = False
if dictionary_3.get(int(year)) != None:
   year_print = dictionary_3.get(int(year))
   everything_ok = True
else:
   print "There is no such year"
   everything_ok = False
if everything_ok == True:
   print "Date: ", day_print, "/", month_print, "/", year_print #or whatever format
else:
   pass

rawDescription: Date conversion from numbers to words in Python


code:
import re
regex = re.compile(r'(.*?)\[.*?\]\.(.*)')
groupofgrids = {}
for item in tablenames:
    matches = regex.findall(item)
    if (len(matches) > 0 and len(matches[0]) == 2):
        key = "-".join(matches[0])
        if key in groupofgrids:
            groupofgrids[key].append(item)
        else:
            groupofgrids[key] = [item]
import json
print json.dumps(groupofgrids,sort_keys=True, indent=4)
#OUTPUT
'''
{
    "SCS_q102-SCS_q102_grid": [
        "SCS_q102[{SCS_q102$_$_$SCS_q102_1}].SCS_q102_grid",
        "SCS_q102[{SCS_q102$_$_$SCS_q102_2}].SCS_q102_grid",
        "SCS_q102[{SCS_q102$_$_$SCS_q102_3}].SCS_q102_grid",
        "SCS_q102[{SCS_q102$_$_$SCS_q102_4}].SCS_q102_grid"
    ],
    "SCS_q107-SCS_q107_grid": [
        "SCS_q107[{SCS_q107$_$_$SCS_q107_1}].SCS_q107_grid",
        "SCS_q107[{SCS_q107$_$_$SCS_q107_2}].SCS_q107_grid",
        "SCS_q107[{SCS_q107$_$_$SCS_q107_3}].SCS_q107_grid"
    ]
}
'''

rawDescription: python: match and group sets of similar strings in a loop

description: Python: match and group sets of similar strings in a loop


code:
import json

import scrapy

class JustPropertySpider(scrapy.Spider):
    name = "justproperty"
    allowed_domains = ["justproperty.com"]
    start_urls = [
        "http://www.justproperty.com/search/uae/apartments/filter__cid/0/sort/score__desc/per_page/20/page/1"
    ]

    def parse(self, response):
        yield scrapy.Request('http://www.justproperty.com/search/featured-properties/?url=filter__cid/0/sort/score__desc/per_page/20/page/1&ajax=true',
                             callback=self.parse_results,
                             headers={'X-Requested-With': 'XMLHttpRequest'})

    def parse_results(self, response):
        results = json.loads(response.body)

        for result in results:
            print result['description']

rawDescription: xpath why i got empty result in this expth

description: Xpath why i got empty result in this expth


code:
test = True
seq = [42, 23]
seq[test] = 1
print seq
#result: [42, 1]

rawDescription: Switch between assignments to different variables in python? With ternary operator?


code:
with open('test.txt') as f:
    print [line.strip().split(']')[-1] for i,line in enumerate(f) if i in [1,8]]

rawDescription: If I have got content of line 2 I want to get content of line 9. How to do?


code:
import re

string = """
data.items.[0].deliveryAddress.region
data.items.[0].classification.scheme
data.items.[0].classification.id
data.items.[0].description
"""

rx = re.compile(r'^((?:.(?!classification))+)$', re.MULTILINE)

matches = rx.findall(string)
print matches
# ['data.items.[0].deliveryAddress.region', 'data.items.[0].description']

rawDescription: Regex match key except one

description: Regex match key except one


code:
import Tkinter as Tk

def countlines(event):
    (line, c) = map(int, event.widget.index("end-1c").split("."))
    print line, c

root = Tk.Tk()
root.geometry("200x200")
a = Tk.Text(root)
a.pack()
bindtags = list(a.bindtags())
bindtags.insert(2, "custom")
a.bindtags(tuple(bindtags))
a.bind_class("custom", "<Key>", countlines)

root.mainloop()

rawDescription: Get of number of lines of a Text tkinter widget


code:
class testclass(object):
    def __init__(self):
        self.testit = None
    def __repr__(self):
        return "guacamole"

def foobarme():
    assert False
    return 42

def getQuotation():
    print "Fetching quotation from the absolutely useless function."
    return ("Four score and seven years ago our fathers brought forth, "
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")

rawDescription: Python multi-line strings breaks Vim's indent folding


code:
def append_and_return_static_list(item, items=[]):
    items.append(item)
    return items

append_and_return_static_list(0)
append_and_return_static_list(1)
print append_and_return_static_list(2) #=> [0,1,2]

rawDescription: static effect on python

description: Static effect on python


code:
from itertools import tee, izip
def solve(lis, num):
    it1, it2 = tee(lis, 2)
    next(it2)  #drop first item
    for i, (x, y) in enumerate(izip(it1, it2), 1):
        print x, y
        if y - x >= num:
            yield i

print list(solve((x for x in [0,1,-1,3,7,4,5,3,2,4,8]), 2))
#[3, 4, 9, 10]

rawDescription: How to print out the indexes in a list with repetitive elements

description: Print out the indexes in a list with repetitive elements


code:
from sqlalchemy import Integer, Boolean, String, Column, create_engine, \
    ForeignKey
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm.collections import attribute_mapped_collection

Base = declarative_base()

class SomeClass(Base):
    __tablename__ = 'sometable'

    id = Column(Integer, primary_key=True)
    tuple_elements = relationship(
                "TupleAssociation",
                collection_class=attribute_mapped_collection("name"),
                cascade="all, delete-orphan"
            )
    items = association_proxy("tuple_elements", "as_tuple")

class TupleAssociation(Base):
    __tablename__ = 'tuple_association'
    parent_id = Column(Integer, ForeignKey('sometable.id'), primary_key=True)
    name_id = Column(Integer, ForeignKey("name_data.id"), primary_key=True)

    col1 = Column(Integer)
    col2 = Column(Boolean)

    name_element = relationship("NameElement")

    def __init__(self, key, tup):
        self.name_element = NameElement(name=key)
        self.col1, self.col2 = tup

    @property
    def name(self):
        return self.name_element.name

    @property
    def as_tuple(self):
        return self.col1, self.col2

class NameElement(Base):
    __tablename__ = 'name_data'

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

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

collection = SomeClass()
collection.items["item name 1"] = (3, True)
collection.items["item name 2"] = (1, False)
print collection.items

s.add(collection)
s.commit()

collection = s.query(SomeClass).first()
print collection.items

rawDescription: Using a tuples of values in sqlalchemy mapped collection


code:
import sys
print len(sys.modules.keys())
print sys.modules.keys()
import urllib
print len(sys.modules.keys())
print sys.modules.keys()
import urllib #again!
print len(sys.modules.keys()) #has not loaded any additional modules

rawDescription: what happens when i import module twice in python

description: What happens when i import module twice in python


code: print "Thanks %s for participating in the quiz." % studentName #python2

rawDescription: Text after variable print command

description: Text after variable print command


rawDescription: How to search key from key-value pair in python

description: Search key from key-value pair in python


code:
tuple_list = [('a', 1), ('b', 3), ('c', 2)]
def flatten_tuple(tuple_list):
    #Method 1
    #import itertools
    #return [item for item in itertools.chain.from_iterable(tuple_list)]

    #Method 2
    return [item for tempList in tuple_list for item in tempList]

print flatten_tuple(tuple_list)

rawDescription: Python: convert a list of tuples into a flat list


code:
l = [u'adidas\nReebok']
manufacturers = l[0].split()
manufacturers
for x in manufacturers:
    print x

rawDescription: Splitting unicode string

description: Splitting unicode string


code:
import numpy as np
from scipy.stats import ttest_ind
from scipy.special import stdtr

np.random.seed(1)

# Create sample data.
a = np.random.randn(40)
b = 4*np.random.randn(50)

# Use scipy.stats.ttest_ind.
t, p = ttest_ind(a, b, equal_var=False)
print "ttest_ind: t = %g  p = %g" % (t, p)

# The following is basically the same as the implementation in
# scipy.stats.ttest_ind.

# Compute the descriptive statistics of a and b.
abar = a.mean()
avar = a.var(ddof=1)
na = a.size
adof = na - 1

bbar = b.mean()
bvar = b.var(ddof=1)
nb = b.size
bdof = nb - 1

# Compute Welch's t-test using the descriptive statistics.
tf = (abar - bbar) / np.sqrt(avar/na + bvar/nb)
dof = (avar/na + bvar/nb)**2 / (avar**2/(na**2*adof) + bvar**2/(nb**2*bdof))
pf = 2*stdtr(dof, -np.abs(tf))

print "formula:   t = %g  p = %g" % (tf, pf)

rawDescription: Perform 2 sample t-test

description: Perform 2 sample t-test


code:
with open('/tmp/results_nslookup.txt', 'r') as f:
    for line in f:
        if line == 'Non-authoritative answer:\n':
            for i in range(8):
                print lines.next().strip()

rawDescription: Python: Print next x lines from text file when hitting string


code:
''' Fun with zip '''

numbers = [1, 2, 3, 4, 5, 6]
letters = ['a', 'b', 'c', 'd', 'e', 'f']

#Using zip to create a list of tuples containing pairs of elements of numbers & letters
print zip(numbers, letters)

#zip works on other iterable objects, including strings
print zip(range(1, 7), 'abcdef')

#zip can handle more than 2 iterables
print zip('abc', 'def', 'ghi', 'jkl')

#zip can be used in a for loop to process two (or more) iterables simultaneously
for n, l in zip(numbers, letters):
    print n, l

#Using zip in a list comprehension to make a list of lists
print [[l, n] for n, l in zip(numbers, letters)]

#zip stops if one of the iterables runs out of elements
print [[n, l] for n, l in zip((1, 2), letters)]
print [(n, l) for n, l in zip((3, 4), letters)]

#Turning an iterable into an iterator object using the iter function
iletters = iter(letters)

#When we take some elements from an iterator object it remembers where it's up to
#so when we take more elements from it, it continues from where it left off.
print [[n, l] for n, l in zip((1, 2, 3), iletters)]
print [(n, l) for n, l in zip((4, 5), iletters)]

#This list will just contain a single tuple because there's only 1 element left in iletters
print [(n, l) for n, l in zip((6, 7), iletters)]

#Rebuild the iletters iterator object
iletters = iter('abcdefghijkl')

#See what happens when we zip multiple copies of the same iterator object.
print zip(iletters, iletters, iletters)

#It can be convenient to put multiple copies of an iterator object into a list
iletters = iter('abcdefghijkl')
gang = [iletters] * 3

#The gang consists of 3 references to the same iterator object
print gang

#We can pass each iterator in the gang to zip as a separate argument
#by using the "splat" syntax
print zip(*gang)

#A more compact way of doing the same thing:
print zip(* [iter('abcdefghijkl')]*3)

rawDescription: How to get a split up a list of numbers and insert into another list

description: Get a split up a list of numbers and insert into another list


code:
from itertools import izip_longest
with open("loginsystem.txt", "r") as loginsystem:
    for line1, line2 in izip_longest(*[loginsystem]*2):
        username = line1.rstrip()
        password = line2.rstrip()
        print username, password

rawDescription: Python Login: Error reading Text file

description: Python Login: Error reading Text file


rawDescription: PyQuery: Get only text of element, not text of child elements


code:
terribledatastruct = [[k]+[[item] for item in v] for k,v in a.items()]
print terribledatastruct

rawDescription: Combining 2 lists like UNION ie keeping 1 copy of mutual items between 2 lists and appending others


code: print 'your name is %s!' %name

rawDescription: Python - Removing space in a string

description: Python - Removing space in a string


code:
a = 5     # In binary: 0101
b = 3     # In binary: 0011
c = a | b # In binary: 0111

print c   # In decimal: 7

rawDescription: How do the bitwise operations work?

description: How do the bitwise operations work


code:
import lxml.etree

root = lxml.etree.parse("example.xml")
for instance in root.xpath('//Instance[descendant::UserValue[@title = "THIRD"][@value != ""]]'):
    print instance.get('name')

rawDescription: Can findall or finditer be used in this python ElementTree example?


code:
import re
sub = '(\w*)\W*(\w*)\W*(name)\W*(\w*)\W*(\w*)'
str1 = '''My name is Avi. Name identifies who you are. It is important to have a name starting with the letter A.'''
for i in re.findall(sub, str1, re.I):
    print " ".join([x for x in i if x != ""])

rawDescription: Finding words around a substring

description: Finding words around a substring


code: print 'mco shell run -I {0} "sc qc {1} 5000"'.format(server_name, rifs.rstrip('\r'))

rawDescription: Python String Format not working as expected when ran in a script


code:
dateOfBirth = get_dateOfBirth()
birthDate = dateOfBirth[2]
birthMonth = dateOfBirth[1]
birthYear = dateOfBirth[0]

#bonus style tip: add a space between 'is' and the number that follows it
print "date is " + str(birthDate)
print "month is " + str(birthMonth)
print "year is " + str(birthYear)

rawDescription: Why is there comma in my slice

description: Why is there comma in my slice


code:
for k, v in dictionary.token2id.iteritems():
    print k, v

rawDescription: Access key value pairs in gensim dictionary


rawDescription: python expand a list to print

description: Python expand a list to print


code:
inds = np.where(np.diff(x))[0]
out = np.split(x,inds[np.diff(inds)==1][0::2]+2)
for n in out:
    print n

rawDescription: Split list based on when a pattern of consecutive numbering stops


code:
import urlparse
def seo(url):
    result = requests.get(url)
    soup = BeautifulSoup(result.text)
    links = [urlparse.urljoin(url, tag['href']) for tag in soup.findAll('a', href=True)] # get all links on the page
    lower_cased = result.text.lower()
    keywords = ['creative']
    while links: # keep going until list is empty
        for keyword in keywords:
            if keyword.lower() in lower_cased:
                print "Success we found the keyword: {}".format(keyword)
                return
        link = links.pop() # get next link to check
        result = requests.get(link)
        lower_cased = result.text.lower()

rawDescription: How to get my code to stop printing a keyword in my web crawler

description: Get my code to stop printing a keyword in my web crawler


code: print ' '.join(sorted(d, key=lambda k: len(d[k]), reverse=True))

rawDescription: python sorting dictionary by length of values

description: Python sorting dictionary by length of values


code:
# geetree.py
import xml.etree.ElementTree as ET

class GeeElem(object):
    """Wrapper around an ElementTree element. a['foo'] gets the
       attribute foo, a.foo gets the first subelement foo."""
    def __init__(self, elem):
        self.etElem = elem

    def __getitem__(self, name):
        res = self._getattr(name)
        if res is None:
            raise AttributeError, "No attribute named '%s'" % name
        return res

    def __getattr__(self, name):
        res = self._getelem(name)
        if res is None:
            raise IndexError, "No element named '%s'" % name
        return res

    def _getelem(self, name):
        res = self.etElem.find(name)
        if res is None:
            return None
        return GeeElem(res)

    def _getattr(self, name):
        return self.etElem.get(name)

class GeeTree(object):
    "Wrapper around an ElementTree."
    def __init__(self, fname):
        self.doc = ET.parse(fname)

    def __getattr__(self, name):
        if self.doc.getroot().tag != name:
            raise IndexError, "No element named '%s'" % name
        return GeeElem(self.doc.getroot())

    def getroot(self):
        return self.doc.getroot()

rawDescription: Really simple way to deal with XML in Python?


code:
s = "this    is my long     sentence"
print ' '.join(s.split(' ')[3:])

rawDescription: Pythonically remove the first X words of a string


code:
from ipywidgets import widgets, interact

# Create text widget for output
output_slider_variable = widgets.Text()

# Define function to bind value of the input to the output variable
def f(x):
    output_slider_variable.value = str(x)

# Create input slider with default value = 10
interact(f, x=10)

# Display output variable in text box
output_slider_variable

# Create and output new int variable with value of slider
new_variable = int(output_slider_variable.value)
print new_variable

# Do something with new variable, e.g. cube
new_variable_cubed = pow(new_variable, 3)
print new_variable_cubed

rawDescription: Using Ipython ipywidget to create a variable?


code:
# Get data into a dictionary
json_data = r.json()
# Now you access the 'data' by
print json_data['data']
# [{'field1': 'value1'}, {'field1': 'value2'}]

# The above is a list
# To access it,
print json_data['data'][0]['field1']
# value1
print json_data['data'][1]['field1']
# value2

rawDescription: Using python requests module to hit API, how do I filter what's returned when using 'print r.json'?


code:
# Let's suppose we have 30 observations with 5 variables each...
# The five variables are temp, pressure, x-velocity, y-velocity, and z-velocity
x = np.random.random((30, 5))

# Make a structured dtype to represent our variables...
dtype=dict(names=['temp', 'pressure', 'x_vel', 'y_vel', 'z_vel'],
           formats=5 * [np.float])

# Now view "x" as a structured array with the dtype we created...
data = x.view(dtype)

# Each measurement will now have the name fields we created...
print data[0]
print data[0]['temp']

# If we want, say, all the "temp" measurements:
print data['temp']

# Or all of the "temp" and "x_vel" measurements:
print data[['temp', 'x_vel']]

rawDescription: python numpy array / dict multiple inheritance

description: Python numpy array / dict multiple inheritance


code:
l=[i for i in range(1,100)]
for index, i in enumerate(l[:20]):
    print index, i

rawDescription: Restrict for loop in Python for specific iterations


code:
tnode = root.xpath('/p')
content = tnode.xpath('text()')
print ''.join(content)

rawDescription: How to parse text from html using lxml?

description: Parse text from html using lxml


code:
count = 0
for value in iterate(range([])):
    print value
    count += 1
else:
    if count == 0:
        print "list was empty"

rawDescription: Using for...else in Python generators

description: Using for...else in Python generators


code:
x = NP.arange(0, 10, dtype=float)
s = NP.ndarray.tostring(x)

print len(x)
print len(s)
print repr(s)
print NP.fromstring(s, dtype=float)

rawDescription: convert numpy array type: float32 to char

description: Convert numpy array type: float32 to char


code:
df1 = pd.DataFrame.from_items([('data_id1' , ['bzx_0001','bzx_0002','bzx_0003','bzx_0004'])
, ('Full_Key_1',['AAAA-BBBB-20150101-NS237890', 'BBBB-CCCC-21050101-MS18546', 'CCCC-CCCC-20150101-MS34567', 'CCCC-CCCC-20150101-MS34568'])
, ('text_field',['aaaaa', 'bbbbb', 'cccccc', 'ddddd'])])

df2 = pd.DataFrame.from_items([('data_id2',['dm_0001', 'dm_0002', 'dm_0003', 'dm_0004'])
,('Partial_key',['AAAA-BBBB-20150101-', 'AAAA-BBBB-20150101-', 'BBBB-CCCC-21050101-', 'XXXX-XXXX-20150101-'])
])

df1['key'] =1
df2['key'] =1

merged_cross_join = pd.merge(df1, df2,on='key')

# we don't need this helper column 'key' any longer
merged_cross_join.drop('key', axis=1, inplace=True)
df1.drop('key', axis=1, inplace=True)

contains_criteria = merged_cross_join[['Full_Key_1','Partial_key']].apply(lambda x: x['Partial_key'] in x['Full_Key_1'],axis=1)
print merged_cross_join[contains_criteria]

rawDescription: Left outer Join Pandas Data Frame using contains


code:
from functools import wraps

def deco(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return f(f, *args, **kwargs)
    return wrapper

@deco
def test(callee, value=None):
    if value is None:
        print 'function attribute "x" is {}'.format(callee.x)
    else:
        print 'setting function attribute "x" to {}'.format(value)
        callee.x = value

test(42)  # -> setting function attribute "x" to 42
test()    # -> function attribute "x" is 42

rawDescription: Python: Reference to the function from inside itself (like arguments.callee in JavaScript)


code:
from bs4 import BeautifulSoup
html_doc = '''

    <sh src="./citys/1.jpg" alt="boy" title="boy" />port_new_cape
    452

South

    May 09, 1997
    Jan 23, 2009 12:05 pm

    <sh src="./citys/1.jpg" alt="boy" title="boy" />port_new_cape
    452

South

    May 09, 1997
    Jan 24, 2009 12:05 pm

    <sh src="./citys/1.jpg" alt="boy" title="boy" />port_new_cape
    452

South

    May 09, 1997
    Jan 25, 2009 12:05 pm

'''
soup = BeautifulSoup(html_doc)
cities = soup.find_all('td', {'class' : 'details'})
counter = 0
while len(cities) > counter:
    datesColumn = cities[counter].find_next_siblings('td')
            # Assuming you are interested in second column of date
    if len(datesColumn) == 2:
        print datesColumn[1].string

    counter += 1

rawDescription: Picking up (next_siblings) texts

description: Picking up (next_siblings) texts


code:
# time execution
from timeit import timeit
time_order8 = timeit("butter_bandpass_filter(data,300,2000,20000,8)", "from __main__ import butter_bandpass_filter, butter_bandpass, data", number=100)
time_order2 = timeit("butter_bandpass_filter(data,300,2000,20000,2)", "from __main__ import butter_bandpass_filter, butter_bandpass, data", number=100)
print "For order 8: %.2f seconds" % time_order8
print "For order 2: %.2f seconds" % time_order2

rawDescription: Most efficient way to filter a long time series Python


code:
print 1
print (1)
print 1,2
print (1,2)

rawDescription: Should I use the print statement or function in Python 2.7?


code:
#!/usr/bin/env python

last = "0"

with open('input.csv') as csv:
    print next(csv).strip().replace(';', ';PSR;', 1)
    for line in csv:
        field = line.strip().split(';')
        if field[3] == last: field.insert(1, "0")
        else: field.insert(1, last)
        last = field[4]
        print ';'.join(field)

rawDescription: How do create new column in csv file using python by shifting one row


code:
b = [[1, 2], [3, 4]]
x = [['foo', 'bar'], ['barFoo', 'fooBar']]
for rows in zip(b, x):
    for bval, xval in zip(*rows):
        print 'bval: %d; xval: %s' % (bval, xval)

rawDescription: Python: Iterating on the lines and columns of an array


code:
tree = Node(1, Node(4, Node(4, Node(4), Node(5)), Node(6), Node(7, Node(8))))
print tree
tree.updateLeafs(10)
print tree

rawDescription: Updating all objects in a class

description: Updating all objects in a class


code:
randomTextgroup = re.compile(r'\*{2}\].*?\[\*{2}\].*?(?=\[\*{2}\])')

with open ("C:Location/test.txt", 'r') as txt:

    data=txt.read().replace('\n','\r')

    section = randomTextgroup.findall(data)

    for i in section:

        randText = re.search('\]\s(.*?)\[', i)
        test = randText.group(1)
        print test

        findMe = re.search('findMe', i)
        result2 = findMe.group()
        print result2

        numbers = re.findall('(\d{3}\-\d{3})', i)
        print numbers, '\n'

rawDescription: Iterating over data from a retrieved file to extract various values


code:
from texttable import Texttable

tab = Texttable()
for row in data.asList()[0][1:]:
    row = dict(row)
    tab.header(row.keys())
    tab.add_row(row.values())
print tab.draw()

rawDescription: Parsing a lisp file with Python

description: Parsing a lisp file with Python


code:
lst = ['food', 'dinner', 'pasta', 'white sauce', 'fetucci alfredo']
for i in range(len(lst)):
  print lst[:i+1]

rawDescription: Python: loop through a list using all previous indexes with each index


code: print data["people 1"].values()

rawDescription: Help for arrays: converting php code to python


code:
req = requests.post(url, data=data, headers=headers, stream=True, files=buff)
print req.text

rawDescription: Python equivalent to curls --form: Create multipart form-data post request with data in "form" parameter