code:
import sqlite3

conn = sqlite3.connect('catalog.db')
cursor = conn.cursor()
result = cursor.execute('CREATE TABLE ru (msgid TEXT, msgstr TEXT)')

# bulk insert the messages
messages = [(msg.id, msg.string) for msg in cat if msg.id]
result = cursor.executemany('INSERT INTO ru (msgid, msgstr) VALUES (?, ?)',  messages)
assert(result.rowcount == len(messages))
conn.commit()

result = cursor.execute("SELECT * from ru where msgid = '11 inches/sec.'")
msgid, msgstr = result.fetchone()
# .encode('utf8') can be removed for Python 3
print '"{}" translates to "{}"'.format(msgid, msgstr.encode('utf8'))

msgid = 'A Samba password is required to export printer drivers'
result = cursor.execute("SELECT * from ru where msgid = ?", (msgid,))
msgid, msgstr = result.fetchone()
print '"{}" translates to "{}"'.format(msgid, msgstr.encode('utf8'))

rawDescription: Storing a text file into SQLite3 database using python


code:
def choose_random(list1,n):
    import random
    i = 0
    set_list=[]
    major_numbers=range(1,50) + list1
    print(major_numbers)
    while i <n:
        set1 =random.sample(major_numbers,5)
        set2 =random.sample(major_numbers,2)
        while len(set(set1)) != len(set1):
            print("Duplicate found at %i"%i)
            print set1
            print("Changing to:")
            set1 =random.sample(major_numbers,5)
            print set1
        set_list.append([set1,set2])
        i +=1
    return set_list

rawDescription: Python failure to find all duplicates

description: Python failure to find all duplicates


code:
instance = getattr(rsyslog, 'instance{}'.format(this_instance_number))
print getattr(instance, attribute)

rawDescription: How to use string formatting to dynamically assign variables

description: Use string formatting to dynamically assign variables


rawDescription: re.split on multiple characters (and maintaining the characters) produces a list containing also empty strings

description: Re.split on multiple characters (and maintaining the characters) produces a list containing also empty strings


code:
import os

# path is your starting point - everything under it will be searched
path = os.getcwd()
fname = 'file1.txt'
my_files = []

# Start iterating, and anytime we see a file that matches fname,
# add to our list
for root, dirs, files in os.walk(path):
  for name in files:
    if name == fname:
      # root here is the path to the file
      my_files.append(os.path.join(root, name))

print my_files

rawDescription: return text file path

description: Return text file path


code:
rdflib.plugin.register('sparql', rdflib.query.Processor,
                       'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
                       'rdfextras.sparql.query', 'SPARQLQueryResult')

query = """
SELECT ?seq_index ?name ?surname WHERE {
     <http://www.ncbi.nlm.nih.gov/pubmed/18273724> bib:authors ?seq .
     ?seq ?seq_index ?seq_bnode .
     ?seq_bnode foaf:givenname ?name .
     ?seq_bnode foaf:surname ?surname .
}
"""
for row in sorted(g.query(query, initNs=dict(rdf=RDF,foaf=FOAF,bib=BIB)),
                                                  key=lambda x:int(x[0][44:])):
    print "Author(%s) %s %s"%(row[0][44:],row[1],row[2])

rawDescription: How to access members of an rdf list with rdflib (or plain sparql)

description: Access members of an rdf list with rdflib (or plain sparql)


code:
template_string = """
{% for i in [1,2,3,4,5,6,7] %}
  {% if loop.index %3 == 0%}3{%else%}0{%endif%}\n
{% endfor %}
"""

from jinja2 import Template

print Template(template_string).render()

rawDescription: How to check whether a number is divisible by another in Jinja Template (Django Framework)

description: Check whether a number is divisible by another in Jinja Template (Django Framework)


code:
from collections import defaultdict

d = defaultdict(int)

for item in batting:
  d[item['teamID']] += item.get('R', 0) or 0

for team, r_sum in sorted(d.items(), key=lambda x: x[0]):
  print team, r_sum

rawDescription: Grouping in Python

description: Grouping in Python


rawDescription: Python-How to find length of the longest run of monotonically increasing numbers?


code:
def my_function(fn_variable_name):
    return fn_variable_name + 1

# calling directly
print my_function(1) # outputs 2

# calling with variable
another_variable_name = 45
print my_function(another_variable_name) # outputs 46

# Storing result in variable
yet_another_variable_name = my_function(32)
print yet_another_variable_name # outputs 33

# Combining the 2
var_1 = 23
var_2 = my_function(var_1)
print var_1 # outputs 23
print var_2 # outputs 24

rawDescription: About num1 and num2 in python calculator


code:
from collections import Counter, defaultdict
import csv

counts = defaultdict(Counter)

with open(somefilename, 'rb') as f:
    reader = csv.reader(f)
    next(reader)  # skip the header
    for row in reader:
        key, time_bin, count = row[0], int(row[1]), int(row[2])
        counts[time_bin][key] += count

for time_bin in counts:
    print '{}=> {}'.format(time_bin, counts[time_bin].most_common(2))

rawDescription: find the largest value in 2d dictionary wrt to the key

description: Find the largest value in 2d dictionary wrt to the key


code:
import urllib
import re
##NL East stats.
teamstate = ["wsh","phi","nym","mia","atl"]
teamnamelist = ["washington-nationals","philadelphia-phillies","new-york-mets","miami-marlins","atlanta-braves"]
teamlist = ["Washington Nationals","Philadelphia Phillies","New York Mets","Miami Marlins","Atlanta Braves"]

for x, y, z in zip(teamstate, teamnamelist, teamlist):
    url = "http://espn.go.com/mlb/team/_/name/%s/%s" % (x, y)
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '(.+?)'
    pattern = re.compile(regex)
    val = re.findall(pattern,htmltext)[1]
    print "The batting average of the %s is %s." % (z, str(val))

rawDescription: Python producing two instances of data set


code:
file_a = open("file_a.txt", "r")
file_b = open("file_b.txt", "r")

for a_line in file_a:
    # This result value will be written into your new file
    result = ""
    # This is what we will search with
    search_val = a_line.strip("\n")
    print "---- Using " + search_val + " from file_a to search. ----"
    for b_line in file_b:
        print "Searching file_b using " + b_line.strip("\n")
        if b_line.strip("\n").find(search_val) == 0:
            result += (b_line)
    print "---- Search ended ----"
    # Set the read pointer to the start of the file again
    file_b.seek(0, 0)

    if result:
        # Write the contents of "results" into a file with the name of "search_val"
        with open(search_val + ".txt", "a") as f:
            f.write(result)

file_a.close()
file_b.close()

rawDescription: Read lines in one file and find all strings starting with 4-letter strings listed in another txt file


code:
#!/usr/bin/python

import struct
import numpy as np

infile = open("lake_example.hds","rb")

blockdata = []

while infile.read(1):
    infile.seek(-1,1)
    data = infile.read(56)
    n = struct.unpack('<3i4', data[0:12])
#    print n[0], n[1], n[2]
    n = struct.unpack('<2f4', data[12:20])
#    print n[0], n[1]
#    print data[20:36]
    n = struct.unpack('<5i4', data[36:56])
#    print n[0], n[1], n[2], n[3], n[4]
    ncol = n[0]
    nrow = n[1]
    a = np.fromfile(infile,dtype='f4',count=ncol*nrow).reshape((ncol,nrow))
    blockdata.append(a)
    data = infile.read(4)
    n = struct.unpack('<i4', data)
#    print n[0]

for block in blockdata:
    print block

rawDescription: How to convert a unformatted fortran file(modflow output ) to numpy array

description: Convert a unformatted fortran file(modflow output ) to numpy array


code:
import Tkinter

class Application(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, master)
        self.master.minsize(width=100, height=100)
        self.master.config()

        self.master.bind('<Left>', self.left_key)
        self.master.bind('<Right>', self.right_key)

        self.main_frame = Tkinter.Frame()
        self.main_frame.pack(fill='both', expand=True)
        self.pack()

    @staticmethod
    def left_key(event):
        print event + " key pressed"

    @staticmethod
    def right_key(event):
        print event + " key pressed"

root = Tkinter.Tk()
app = Application(root)
app.mainloop()

rawDescription: Tkinter - Can't bind arrow key events

description: Tkinter - Can't bind arrow key events


code:
import inspect

def PrintFrame():
  callerframerecord = inspect.stack()[1]    # 0 represents this line
                                            # 1 represents line at caller
  frame = callerframerecord[0]
  info = inspect.getframeinfo(frame)
  print info.filename                       # __FILE__     -> Test.py
  print info.function                       # __FUNCTION__ -> Main
  print info.lineno                         # __LINE__     -> 13

def Main():
  PrintFrame()                              # for this line

Main()

rawDescription: How to determine file, function and line number?

description: Determine file, function and line number


code:
for player in soup1.select('ul.team1 li.player'):
    text = player.get_text().strip()
    print text

rawDescription: Parser returns "\n" instead of desired output.


code:
labels = set('DET NN VB IN JJ'.split())

true_positives = Counter()
false_negatives = Counter()
false_positives = Counter()

for i in labels:
    for j in labels:
        if i == j:
            true_positives[i] += cm[i,j]
        else:
            false_negatives[i] += cm[i,j]
            false_positives[j] += cm[i,j]

print "TP:", sum(true_positives.values()), true_positives
print "FN:", sum(false_negatives.values()), false_negatives
print "FP:", sum(false_positives.values()), false_positives

rawDescription: How to use the confusion matrix module in NLTK? - python

description: Use the confusion matrix module in NLTK? - python


code: print "Tmin=", ", ".join(Tmin)

rawDescription: Writing to a CSV with python without brackets


rawDescription: Saving list from python to text file in format


code:
import subprocess
ps = subprocess.Popen("ps -U 0", shell=True, stdout=subprocess.PIPE)
print ps.stdout.read()
ps.stdout.close()
ps.wait()

rawDescription: Which is the best way to get a list of running processes in unix with python?


code:
# Get power status of the system using ctypes to call GetSystemPowerStatus

import ctypes
from ctypes import wintypes

class SYSTEM_POWER_STATUS(ctypes.Structure):
    _fields_ = [
        ('ACLineStatus', wintypes.BYTE),
        ('BatteryFlag', wintypes.BYTE),
        ('BatteryLifePercent', wintypes.BYTE),
        ('Reserved1', wintypes.BYTE),
        ('BatteryLifeTime', wintypes.DWORD),
        ('BatteryFullLifeTime', wintypes.DWORD),
    ]

SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)

GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL

status = SYSTEM_POWER_STATUS()
if not GetSystemPowerStatus(ctypes.pointer(status)):
    raise ctypes.WinError()
print 'ACLineStatus', status.ACLineStatus
print 'BatteryFlag', status.BatteryFlag
print 'BatteryLifePercent', status.BatteryLifePercent
print 'BatteryLifeTime', status.BatteryLifeTime
print 'BatteryFullLifeTime', status.BatteryFullLifeTime

rawDescription: In Python, how can I detect whether the computer is on battery power?


code:
from itertools import combinations

def com_subset_sum(seq, target):
    if target == 0 or target in seq:
        return True

    for r in range(len(seq),1,-1):
        for subset in combinations(seq, r):
            if sum(subset) == target:
                print subset
                return True
    return False

print com_subset_sum([1,3,5,1,6,8,7],10)

rawDescription: How to get s subset sum to print out the integers it used (python)

description: Get s subset sum to print out the integers it used (python)


code:
def find_leafes( root ):
    """ finds folders with no subfolders """
    for root, dirs, files in os.walk(root):
        if not dirs: # can't go deeper
            yield root

print longest_path(find_leafes( root ))

rawDescription: Find deepest nested path?

description: Find deepest nested path


code: print score

rawDescription: dictionaries in python: summing the values returned from searching the keys

description: Dictionaries in python: summing the values returned from searching the keys


code:
#in templatetags/user_list_tags.py
from django import template
from django import forms
register = template.Library()

#this is djangp template tag for user selection form
@register.filter
def user_select_field(form, userid):
    """
    returns UserSelectionForm field for a user with userid
    """
    key = str(userid)
    if key not in form.fields.keys():
        print 'Key %s not found in dict' % key
        return None
    #here i use BoundField:
    boundField = forms.forms.BoundField(form, form.fields[key], key)
    return unicode(boundField)

rawDescription: How to render django form field in template

description: Render django form field in template


code:
data = root.findall('data/METAR')
for sky in data.findall('sky_condition'):
    print "Sky Condition: {0} at {1} ft AGL".format(
        sky.attrib['sky_cover'],
        sky.attrib['cloud_base_ft_agl']
      )

rawDescription: python parse xml

description: Python parse xml


code:
import os
print os.stat('/path/to/filename').st_size

rawDescription: How to get CherryPy to follow symbolic links?

description: Get CherryPy to follow symbolic links


code:
def prisms(V):
    for a in range(1, V+1):
        for b in range(1, V+1):
            if V % (a*b) == 0:
                c = V / (a*b)
                print a,b,c

rawDescription: Generate all cubes with a given volume

description: Generate all cubes with a given volume


code:
a = range(5)
b = range(5, 7)
a[1::2] = b
print a

rawDescription: Assignment to discontinuous slices in python


code:
for (key, value) in aa.viewitems() & bb.viewitems():
    print '%s: %s is present in both aa and bb' % (key, value)

rawDescription: python dictionary match key values in two dictionaries

description: Python dictionary match key values in two dictionaries


code:
nsmap = {'atom': 'http://www.w3.org/2005/Atom'}
for x in root.iterfind('atom:entry', namespaces=nsmap):
    print x

rawDescription: Python lxml iterfind w/ namespace but prefix=None


code:
def deco(func):
    def wrapper(self, *args, **kwargs):
        print "I am the decorator, I know that self is", self, "and I can do whatever I want with it!"
        print "I also got other args:", args, kwargs
        func(self)
    return wrapper

class Foo(object):
    @deco
    def meth(self):
        print "I am the method, my self is", self

rawDescription: Python Decorator with arguments and accessing class instance


code:
def upanddown(n):
    return range(1, n) + range(n, 0, -1)

def triangle(rows):
    for num in xrange(1, rows + 1):
        print ' '.join(map(str, upanddown(num))).center(rows * 4 - 3)

rows = input("Number of rows: ")
triangle(rows)

rawDescription: patterns in python-complex

description: Patterns in python-complex


code:
@app.task(serializer='json', name='task_name')
def task1(arg1):
    print arg1

rawDescription: How to execute celery task when new message arrives to queue?

description: Execute celery task when new message arrives to queue


code:
y = [1, 2, 3]
z = [4, 5, 6]
for x in y or z:
    print x
y = [] # Empty lists evaluate to False in Python
for x in y or z:
    print x

rawDescription: What does "for x in y or z:" do in Python?


code:
from datetime import timedelta
for t in all_times_in_us:
    print timedelta(seconds=t/1000000.0)

rawDescription: Converting time efficiently in Python

description: Converting time efficiently in Python


code:
flags = dict.fromkeys(["a", "b", "c"], True)
flags.update(dict.fromkeys(["d", "e"], False))
print flags

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


rawDescription: Formatting a tuple of floats

description: Formatting a tuple of floats


code:
#guess_999.py
import sys
import os
import fcntl
import time

fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)

time_started = time.time()
while True:
    try:
        stdin = sys.stdin.read()
        if "999" in stdin:
            print "It took %f seconds" % (time.time() - time_started)
            break
    except IOError:
        pass

rawDescription: Is it possible to implement a break statement using a user input in python


code:
df.loc[:,'1Y':'3Y'] = df.loc[:,'1Y':'3Y'].applymap(lambda x: float(x.replace(",",".")))
df2 = df.set_index('Date').groupby('Currency').apply(lambda x: x.loc[:,'1Y':'3Y'].diff()).fillna(0)
print df2

rawDescription: Subtracting rows in panda having different column types


code:
import random
print "WELCOME TO LOTTERY HACK MACHINE!!!\n"
today = int(raw_input( "Please enter today's date: " ))
if today<=31:
    print "Please enter the 4-digit prize winning lottery number for the last 7 days"
    y = raw_input( "Enter 7 numbers separated by commas: " )
    input_list = y.split(',')
    numbers = [list(x.strip()) for x in input_list]
    choice = random.choice(numbers)
    random.shuffle(choice)
    print ''.join(choice)

elif today>31:
    print "A month has only 31 days ;P"

rawDescription: How to choose a random input from 7 given input?

description: Choose a random input from 7 given input


code:
movie['title'] = hxs.xpath('//*[@id="overview-top"]/h1/span[1]/text()')[0].strip()
print movie['title']

rawDescription: crawling imdb database using python and lxml

description: Crawling imdb database using python and lxml


code:
cleaned = [item.strip() for item in eg.split(',')]
print cleaned  # ['item 1', 'item 2', 'item 3']

rawDescription: New lines with Python's ConfigParser?

description: New lines with Python's ConfigParser


code:
line = "83,  44.257790830341,  58.187003960661,  50.0"
first_char = line.split(',')[0].strip() #first char is 83
# lets check whether to replace it or not
if first_char in replacement_data.keys():
    # if key exists, we have to replace
    line = replacement_data[first_char]
    print line # so that it writes to file

rawDescription: replace lines in a text file from another text file

description: Replace lines in a text file from another text file


code:
n = int(raw_input())
left = int(raw_input())

memo = {}
def dp(n, left): # returns tuple (cost, [factors])
    if (n, left) in memo: return memo[(n, left)]

    if left == 1:
        return (n, [n])

    i = 2
    best = n
    bestTuple = [n]
    while i * i <= n:
        if n % i == 0:
            rem = dp(n / i, left - 1)
            if rem[0] + i < best:
                best = rem[0] + i
                bestTuple = [i] + rem[1]
        i += 1

    memo[(n, left)] = (best, bestTuple)
    return memo[(n, left)]

print dp(n, left)[1]

rawDescription: Factoring a number into roughly equal factors


code:
for line in tree.xpath('.//s:sp[@who="Barnardo"]/s:l/text()', namespaces=nsmap):
    print line.strip()

rawDescription: What is an ElementTree object exactly, and how can I get data from it?


code:
#Additional import
from collections import OrderedDict
channels = OrderedDict()
for elem in tv_elem.getchildren():
    if elem.tag == 'channel':
       channels[elem.attrib['id']] = self.load_channel(elem)
       for channel_value in channels.items():
           print channel_value.get_display_name()

rawDescription: Print dict items in alphabet order

description: Print dict items in alphabet order


code: print "The number is {number} and the text is {text}".format(number=a, text=b)

rawDescription: printing variables in python using % and variable name

description: Printing variables in python using % and variable name


code:
f = foo()
f.x.extend([1,2,3])

print f.get_x[0] # 1
print f.get_x(1) # 2
print f.x(2)     # 3

rawDescription: Same name regular and property functions under the same class in python


code:
for row in parse(dic):
    row = map(str, row)
    print '.'.join(row[:-1]) + '->' + row[-1]

rawDescription: Python 2.7 how should I parse a dict object?


code:
with open('output.txt', 'w') as f:
    def my_print(str):
        print str
        f.write(str + '\n')

    for filename in glob.glob('*.txt'):
        my_print(filename)

rawDescription: Writing a print statement to a text file


code:
def scale(self, image):
    images = []

    image.seek(0)

    try:
        im1 = PIL.open(image)
        icc_profile = im1.info.get("icc_profile")
    except IOError, e:
        logger.error(unicode(e), exc_info=True)

    images.append({"file": image, "url": self.url, "size": "original"})

    for size in IMAGE_WEB_SIZES:
        d = cStringIO.StringIO()
        try:
            im = crop(image, size["width"], size["height"])

            im.save(d, "JPEG", icc_profile=icc_profile)
            images.append({"file": d, "url": self.scale_url(size["name"]), "size": size})
        except IOError, e:
            logger.error(unicode(e), exc_info=True)
            pass

    return images

rawDescription: Python PIL - changing colour profile to untagged RGB on crop, scale and save


code:
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)

rawDescription: Python Request HTTP Page and Use The Source Information


code:
for name in sorted(glob.glob('/home/myfiles/*_customer_records_2323_*.zip'))[-5:]:
    print '\t', name

rawDescription: python have glob return only the 5 most recent file matches

description: Python have glob return only the 5 most recent file matches


code: print c,

rawDescription: Python: Print to one line with time delay between prints


code:
logindb = {row[1]:row[2] for row in prsnobj.result}

if login in logindb and logindb[login] == password:
    print "WELCOME !!"
else:
    print "
Invalid Login
"

rawDescription: Unable to execute the else part of an if statment when conditon does not match in python


code:
import grequests
from bs4 import BeautifulSoup

def get_urls_from_response(r):
    soup = BeautifulSoup(r.text)
    urls = [link.get('href') for link in soup.find_all('a')]
    return urls

def print_url(args):
    print args['url']

def recursive_urls(urls):
    """
    Given a list of starting urls, recursively finds all descendant urls
    recursively
    """
    if len(urls) == 0:
        return
    rs = [grequests.get(url, hooks=dict(args=print_url)) for url in urls]
    responses = grequests.map(rs)
    url_lists = [get_urls_from_response(response) for response in responses]
    urls = sum(url_lists, [])  # flatten list of lists into a list
    recursive_urls(urls)

rawDescription: How to use python-requests and event hooks to write a web crawler with a callback function?

description: Use python-requests and event hooks to write a web crawler with a callback function


code:
import datetime
day = datetime.datetime.strptime('Tue Feb 19 00:09:28 +1100 2013', '%a %b %d %H:%M:%S +1100 %Y')
delta = day - datetime.datetime.now()
print delta.days

rawDescription: Calculating days using string dates in Python


code:
import collections

d = collections.OrderedDict(
    [
            ('key1', 'value1'),
            ('key2', 'value2'),
            ('key3', 'value3')
    ]
)

table_name = "entries"

sql_query = "INSERT INTO {} {} VALUES {}".format(table_name, str(tuple(d.keys())).replace("'", ""), str(tuple(d.values())).encode('utf-8'))
print sql_query

rawDescription: How should I unpack dict keys, values as columns, values in an embedded MySQL statement?


code:
from  bisect import bisect
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
        i = bisect(breakpoints, score)
        return grades[i]

while True:
    try:
       grad = float(raw_input("what is ur grade")) *100
       print grade(grad)
       break
    except:
         print 'Enter Correct value'

rawDescription: My code is not executing properly in Python


code:
import inquirer
confirm = {
    inquirer.Confirm('confirmed',
                     message="Do you want to enter the door ?" ,
                     default=True),
}
confirmation = inquirer.prompt(confirm)
print confirmation["confirmed"]

rawDescription: How to let the user select an input from a finite list?

description: Let the user select an input from a finite list


code:
c = Contact("me", "123-123-123", "foo@example.com")
c.update_phone("456-456-456")
print c
for i, hist in enumerate(c.view_history()):
    print "{}. {}".format(i, hist)

rawDescription: Python inheritance best practice for 'object histories'


code:
def dataReceived(self, data):
    print "Received: %s" % data
    if self.factory._logic_callback:
        ret = self.factory._logic_callback.process(data)
        print "Sent: %s" % ret
        self.transport.write(ret)

rawDescription: Python Twisted: separate protocol from processing


rawDescription: Nicest way to pad zeroes to string

description: Nicest way to pad zeroes to string


code:
from lxml.html import parse
from urllib2 import urlopen

SID_URL = 'http://www.cfilt.iitb.ac.in/indowordnet/ajax/onto.jsp?sid=%s'

url = 'http://www.cfilt.iitb.ac.in/indowordnet/first?langno=3&queryword=%E0%A6%97%E0%A6%BE%E0%A6%A7%E0%A6%BE'
tree = parse(urlopen(url))

sid = tree.find('.//label[@id="sid"]').text

tree = parse(urlopen(SID_URL % sid))
for record in tree.xpath('//ontorecord'):
    print record.find('onto_label').text

rawDescription: How to get a data miner to do specific search on web page?

description: Get a data miner to do specific search on web page


code:
for k, v in Reaction.__dict__.iteritems():
    if not k.startswith('__'):
        print v

rawDescription: Iterate of a Class´ attributes

description: Iterate of a Class´ attributes


code:
for i in range(500):
    for j in range (16):
        print '%8.1f' % (0),
    print
    for j in range (16):
        print '%8.1f' % (0),
    print
    for j in range (4):
        print '%8.1f' % (0),
    print

rawDescription: python writing txt file 500* 36 zero,then wrap lines in every 16

description: Python writing txt file 500* 36 zero,then wrap lines in every 16


code:
def new_panel(self, nm):
        pnl = wx.Panel(self)
        pnl.identifierTag = nm
        self.nb.AddPage(pnl, nm, select=True)
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        #self.nb.SetSelection(self.nb.GetPageCount()-1)
        pnl.SetFocus() # Have focused the last panel.
        print self.nb.GetSelection()

rawDescription: wxPython auinotebook.GetSelection() return index to the first page

description: WxPython auinotebook.GetSelection() return index to the first page


code:
def get_str_between(s, before, after):
  # gets a substring between two strings in a string in python
  # by: Cody Kochmann
  unique="~~~~the~obvious~way~to~do~it~~~~"
  return(s.replace(before, unique).replace(after, unique).split(unique)[1])

def get_every_str_between(s, before, after):
  # returns an array of substrings between "before" and "after"
  # by: Cody Kochmann
  unique="~~~~this~is~the~obvious~way~to~do~it~~~~"
  second_unique="~~~~i~think~you'll~like~this~~~~~"
  s=(s.replace(before, unique).replace(after, second_unique).split(unique))
  out=[]
  while len(s):
    tmp=s.pop()
    if second_unique in tmp:
      out.append(tmp.split(second_unique)[0])
  return(out)

target = '<span style="color: blue; font-size: 36px; font-weight: 600;"> string </span>'

print get_every_str_between(target, ">", "</")

rawDescription: How to tree-search an html file tag with no specific type

description: Tree-search an html file tag with no specific type


code:
import re

def checkio(text):

    text = re.sub('[,\.\?\!]', ' ', text).lower()
    words = [w for w in text.split() if not re.search(r'\d', w)]
    print words ## prints [] in this case

rawDescription: Removing words containing digits from a given string


code:
name = motif[1] ### using column index
print name
for value in column[1]: ### using column index
    if value == "1":
        print coordinates
        out = open("%s.bed" %name, "a")
        out.write(str(coordinates)+"\n")
    elif value == "0":
        pass

rawDescription: Using index in Python to iterate through columns


code:
from xml.dom import minidom
import pdb

def get_e(dom, tag):
    l=[]
    els=dom.getElementsByTagName(tag)
    for e in els:
        for child_el in e.childNodes:
            # if this was an element node get its children
            if child_el.nodeType == e.ELEMENT_NODE:
                l.extend(get_e(e, child_el.tagName))
            else:
                l.append(child_el)
    return l

def get_text(els):
    l=[]
    for e in els:
        if e.nodeType == e.TEXT_NODE:
            l.append(e.data)
    return l

dom = minidom.parse('s.xml')
print get_text(get_e(dom, 'td'))

rawDescription: Python Dom Parsing

description: Python Dom Parsing


code:
from itertools import combinations
a = [5,2,8,14,6,13]

print [sum(i) for i in list(combinations(a, 2))]

rawDescription: Perform operation on every array element pair quickly


rawDescription: multiple replacement values in python3

description: Multiple replacement values in python3


code:
from itertools import product
for word, letter, hours in product(["AAA", "BBB", "CCC"], ["M", "Q", "S", "K", "B"], ["00:00", "01:00", "02:00", "03:00"]):
    print word, letter, hours

rawDescription: How to iterate in a cartesian product of lists

description: Iterate in a cartesian product of lists


code:
def core_function():
    print "core function"

rawDescription: python module organisation

description: Python module organisation


code:
from multiprocessing import Pool

def f(x):
    return x*x

pool = Pool(processes=4)
print pool.map(f, range(10))

rawDescription: Python multiprocessing for expensive operation with 2D array


code:
from operator import mul
from itertools import starmap, izip, imap

def matmul(a, b):
     c = zip(*b)
     def element(row):
         # or   [sum(map(mul, row, col)) for col in c]
         # or   [sum(imap(mul, row, col)) for col in c]
         return [sum(starmap(mul, izip(row, col))) for col in c]
     print [element(row) for row in a]

rawDescription: How to pack the elements of product of matrix multiplication back

description: Pack the elements of product of matrix multiplication back


code:
from timeit import timeit

methods = [['Vroomfondel1', '''from collections import defaultdict
import itertools
dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = defaultdict(list)
for k,v in itertools.chain.from_iterable([itertools.product(vals,key) for key,vals in dict1.items()]):
    dict2[k].append(v)'''],

['Vroomfondel2', '''from collections import defaultdict
import itertools
dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = defaultdict(list)
[dict2[k].append(v) for k,v in itertools.chain.from_iterable([itertools.product(vals,key) for key,vals in dict1.items()])]'''],

['***Vroomfondel2 mod', '''dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = {item: [key for key in dict1 if item in dict1[key]] for value in dict1.values() for item in value}'''],

['mhlester1', '''dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = {}
for key, values in dict1.items():
    for value in values:
        dict2.setdefault(value, []).append(key)'''],

['mhlester1 mod', '''from collections import defaultdict
dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = defaultdict(list)
for key, values in dict1.items():
    for value in values:
        dict2[value].append(key)'''],

['mhlester2', '''from collections import defaultdict
dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = defaultdict(list)
for key, values in dict1.items():
    for value in values:
        dict2[value].append(key)'''],

['initial', '''from collections import defaultdict
dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
dict2 = defaultdict(list)
for i in dict1:
    for j in dict1[i]:
        dict2[j].append(i)''']

]

for method in methods:
    print "% 15s" % (method[0]), '\t', timeit(method[1], number=10000)

rawDescription: Pythonic inverse dict non-unique mappings


code:
#!/usr/bin/env python
import execnet

gw = execnet.makegateway("ssh=user@host")
channel = gw.remote_exec("""
import dbus, sys

bus = dbus.SystemBus()
remote_object = bus.get_object('daemon.location', '/daemon')
controller = dbus.Interface(remote_object, 'daemon.path')

channel.send(controller.getValue())
""")
tuple_ = channel.receive()
print tuple_
print tuple_[0]

rawDescription: How to get a variable from the shell in python?

description: Get a variable from the shell in python


code:
# sample custom action from docs
class FooAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        print('Setting: %r %r %r' % (namespace, values, option_string))
        setattr(namespace, self.dest, 'action:'+values)
p = argparse.ArgumentParser()
a1 = p.add_argument('--foo', action=FooAction, default='default')
print 'action:',a1
print p.parse_args([])

ns = argparse.Namespace()
a1(p, ns, a1.default, 'no string') # call action
print p.parse_args([],ns)
print p.parse_args(['--foo','1'],ns)

rawDescription: Cause Python's argparse to execute action for default


code:
for elem in tree.xpath("//div[@id='slider1']//div[@id='gridshow']"):
   for elem_a in elem.xpath("./a"):
      for elem_img in elem_a.xpath("./img"):
         print '<A> HREF=%s'%(elem_a.attrib['href'])
         print '<IMG> ALT="%s"'%(elem_img.attrib['alt'])

rawDescription: Python iterate through section using lxml


code:
import scripttwo
def test():
    print "Hello World"

scripttwo.test(test)

rawDescription: Running function from script it was imported by


code:
import shlex, functools, sys, StringIO

def bin_numeric_op(func):
    @functools.wraps(func)
    def execute(self):
        n2, n1 = self._stack.pop(), self._stack.pop()
        n1 = float(n1)
        n2 = float(n2)
        self._stack.append(func(n1, n2))
    return execute

def relational_op(func):
    @functools.wraps(func)
    def execute(self):
        n2, n1 = self._stack.pop(), self._stack.pop()
        self._stack.append(bool(func(n1, n2)))
    return execute

def bin_bool_op(func):
    @functools.wraps(func)
    def execute(self):
        n2, n1 = self._stack.pop(), self._stack.pop()
        n1 = bool(n1)
        n2 = bool(n2)
        self._stack.append(bool(func(n1, n2)))
    return execute

class Interpreter(object):
    def __init__(self):
        self._stack = []
        self._vars = {}
        self._squarestack = []

    def processToken(self, token):
        if token == '[':
            self._squarestack.append(len(self._stack))
        # Currently inside square brackets, don't execute
        elif len(self._squarestack) > 0:
            if token == ']':
                startlist = self._squarestack.pop()
                lst = self._stack[startlist:]
                self._stack[startlist:] = [tuple(lst)]
            else:
                self._stack.append(token)
        # Not current inside list and close square token, something's wrong.
        elif token == ']':
            raise ValueError("Unmatched ']'")
        elif token in self.builtin_ops:
            self.builtin_ops[token](self)
        else:
            self._stack.append(token)
    def get_stack(self):
        return self._stack
    def get_vars(self):
        return self._vars
    @bin_numeric_op
    def add(n1, n2):
        return n1 + n2
    @bin_numeric_op
    def mul(n1, n2):
        return n1 * n2
    @bin_numeric_op
    def div(n1, n2):
        return n1 / n2
    @bin_numeric_op
    def sub(n1, n2):
        return n1 - n2
    @bin_numeric_op
    def mod(n1, n2):
        return n1 % n2
    @bin_numeric_op
    def Pow(n1, n2):
        return n1**n2
    @relational_op
    def less(v1, v2):
        return v1 < v2
    @relational_op
    def lesseq(v1, v2):
        return v1 <= v2
    @relational_op
    def greater(v1, v2):
        return v1 > v2
    @relational_op
    def greatereq(v1, v2):
        return v1 > v2
    @relational_op
    def isequal(v1, v2):
        return v1 == v2
    @relational_op
    def isnotequal(v1, v2):
        return v1 != v2
    @bin_bool_op
    def bool_and(v1, v2):
        return v1 and v2
    @bin_bool_op
    def bool_or(v1, v2):
        return v1 or v2
    def bool_not(self):
        stack = self._stack
        v1 = stack.pop()
        stack.append(not v1)
    def if_func(self):
        stack = self._stack
        pred = stack.pop()
        code = stack.pop()
        if pred:
            self.run(code)
    def ifelse_func(self):
        stack = self._stack
        pred = stack.pop()
        nocode = stack.pop()
        yescode = stack.pop()
        code = yescode if pred else nocode
        self.run(code)
    def store(self):
        stack = self._stack
        value = stack.pop()
        varname = stack.pop()
        self._vars[varname] = value
    def fetch(self):
        stack = self._stack
        varname = stack.pop()
        stack.append(self._vars[varname])
    def remove(self):
        varname = self._stack.pop()
        del self._vars[varname]
    # The default argument is because this is used internally as well.
    def run(self, code=None):
        if code is None:
            code = self._stack.pop()
        for tok in code:
            self.processToken(tok)
    def dup(self):
        self._stack.append(self._stack[-1])
    def swap(self):
        self._stack[-2:] = self._stack[-1:-3:-1]
    def pop(self):
        self._stack.pop()
    def showstack(self):
        print"%r" % (self._stack,)
    def showvars(self):
        print "%r" % (self._vars,)
    builtin_ops = {
        '+': add,
        '*': mul,
        '/': div,
        '-': sub,
        '%': mod,
        '^': Pow,
        '<': less,
        '<=': lesseq,
        '>': greater,
        '>=': greatereq,
        '==': isequal,
        '!=': isnotequal,
        '&&': bool_and,
        '||': bool_or,
        'not': bool_not,
        'if': if_func,
        'ifelse': ifelse_func,
        '!': store,
        '@': fetch,
        'del': remove,
        'call': run,
        'dup': dup,
        'swap': swap,
        'pop': pop,
        'stack': showstack,
        'vars': showvars
        }

def shell(interp):
    try:
        while True:
            x = raw_input("star>   ")
            msg = None
            try:
                interp.run(shlex.split(x))
            except KeyError:
                msg = "does not exist"
            except:
                sys.excepthook(*sys.exc_info())
                msg = "parse error!"
            if msg != None:
                print "   =>",msg,"\n"
            else:
                print "   => %r\n" % (interp.get_stack(),)
    except (EOFError, KeyboardInterrupt):
        print

interp = Interpreter()
if len(sys.argv) > 1:
    lex = shlex.shlex(open(sys.argv[1], 'r'), sys.argv[1])
    tok = shlex.get_token()
    while tok is not None:
        interp.processToken(tok)
        tok = lex.get_token()
shell(interp)

rawDescription: small language in python

description: Small language in python


code:
return regex.sub(ur"((?!-)\p{P})+", " ", text)

return regex.sub(ur"(\p{P}(?<!-))+", " ", text)

rawDescription: Remove selected punctuation from unicode strings


code:
bites = u'Ç'.encode('utf-8')
unikode = u'Ç'
print bites
print unikode
bites == unikode

rawDescription: How could python treat unicode and non-unicode tuple as equal?


code:
n=5;count=2;i=3;j=2;
while (count <= n):
    for j in range (2,i):
        if(i%j == 0):
            break
    if(j==i-1):
        print i
        count = count +1
    i = i+1

rawDescription: Unable to find the mistake in prime number code in python


code:
import re
data = open('c:/users/metolone/util/windiff.exe','rb').read()

# Search for printable ASCII characters encoded as UTF-16LE.
pat = re.compile(ur'(?:[\x20-\x7E][\x00]){3,}')
words = [w.decode('utf-16le') for w in pat.findall(data)]
for w in words:
    print w

rawDescription: How do I extract Unicode character sequences from an MZ executable file?

description: Extract Unicode character sequences from an MZ executable file


code:
s = r'some \\ doubles'
print s
print s.replace('\\\\', '\\')

rawDescription: Python regex to replace double backslash with single backslash


code:
x = 0
bool = False

def functionA(x):
    global bool
    if x is 0:
        bool = True

def functionB():
    print bool
    if bool is True:
        print "Halleluhja"

functionA(x)
functionB()

print x, bool

rawDescription: Check boolean from another function

description: Check boolean from another function


code:
add1 = make_adder(1)
add5 = make_adder(5)

print add1(10)   # 11
print add5(10)   # 15

rawDescription: In python, when you pass internally defined functions into other functions, how does it keep the variables?


code:
def masterMind():
    number = random.randint(10000, 99999)
    print number # [Debug]
    tries = 1
    userGuess = raw_input("Incorrect! Guess my 5 digit password:")

    while tries < 10:
        if number != userGuess:
            tries += 1
            userGuess = input("Guess my 5 digit password:")
        else:
            print "Win: ", userGuess
            break

rawDescription: Troubles with my MasterMind game code

description: Troubles with my MasterMind game code


code:
l = ['start of paragraph 1','start of paragraph 2','para 3']
first_words = [p.split()[0] for p in l]
print first_words

rawDescription: Find the first word of a paragraph inside a list


code:
import Image
im = Image.load("image.png")
if all(rgb == (0,0,0) for rgb in im.getdata()):
    print "all black"

rawDescription: Check if image is black

description: Check if image is black


code:
import re

myString = "This is my tweet check it out http://tinyurl.com/blah"

r = re.compile(r"(http://[^ ]+)")
print r.sub(r'\1', myString)

rawDescription: Find Hyperlinks in Text using Python (twitter related)


code:
def adder(n):
    def add_func(x):
        return x + n
    return add_func

add3 = adder(3)
print map(add3, [1,2,3])  # [4, 5, 6]

rawDescription: Write pure and functional code in Python using the concept higher-order functions like in JavaScript


code:
import re
file = open('1.txt','r')
for line in file.readlines():
    sequence = re.findall(r"A\d\d", line)
    if sequence:
        for i in sequence:
            print i

rawDescription: Read a file, and print a specific line of it in Python


code:
s = '''that's my     string, "   keep these spaces     "    but reduce these '''

normal, quoted, eating = 0,1,2
state = eating
result = ''
for ch in s:
  if (state, ch) == (eating, ' '):
    continue
  elif (state,ch) == (eating, '"'):
    result += ch
    state = quoted
  elif state == eating:
    result += ch
    state = normal
  elif (state, ch) == (quoted, '"'):
    result += ch
    state = normal
  elif state == quoted:
    result += ch
  elif (state,ch) == (normal, '"'):
    result += ch
    state = quoted
  elif (state,ch) == (normal, ' '):
    result += ch
    state = eating
  else: # state == normal
    result += ch

print result

rawDescription: Python loop through string in nested for loops


code:
import collections

def make_ngram_ipatterns(n):
    """Make all needed patterns used by *gramCollocationFinder up to n words"""

    i_patterns = []

    for i in xrange(1, n+1):
        if i <= 2:
            i_patterns.append('i' * i)

        else:
            for j in xrange(2**(i-2)):
                 bin_str = '{0:0{1}b}'.format(j, i-2)
                 ix_pattern = bin_str.replace('0', 'x').replace('1', 'i')
                 i_patterns.append('i{}i'.format(ix_pattern))

    return i_patterns

def make_ngram_npatterns(n):
    """Make all needed n-patterings used by *gramCollocationFinder up to n words"""
    all_ipatterns = make_ngram_ipatterns(n)

    npatterns = []

    for ipattern in all_ipatterns:
         i_order = sum(c == 'i' for c in ipattern)
         i_length = len(ipattern)
         for j in xrange(n - i_length+1):
             npattern = 'n_{}{}{}'.format('x'* j,
                                           ipattern ,
                                           'x'* (n - i_length - j))

             npatterns.append((i_order, ipattern, npattern))

    return sorted(npatterns)

def main():

    n = 5

    all_ipatterns = make_ngram_ipatterns(n)

    print '\n'.join(make_ngram_ipatterns(n))

    for order, ipattern, npattern in make_ngram_npatterns(n):
         wparams = ', '.join('w{}'.format(i+1)
                                for i, c in enumerate(npattern[2:])
                                if c == 'i'
                            )
         print('order: {1:2}   ipattern: {2:{0}s}   npattern: {3}'
               ' ->  {3} = self.{2}({4})'.format(
                   n, order, ipattern, npattern, wparams))

if __name__ == '__main__':
    main()

rawDescription: Transform QuadgramCollationFinder into PentagramCollationFinder


code:
from xml.etree import cElementTree as etree

root = etree.Element('root')
for i in range(1,3):
    item = etree.SubElement(root, 'item')
    item.attrib['class']='memory'
    for j in range(1,3):
        p = etree.SubElement(item, 'p')
        p.text = 'DIMM Size %s'%j

print etree.tostring(root)

rawDescription: Issue in generating the xml file from python minidom


code:
import pandas as pd

#files = ['university1.csv','university2.csv','university3.csv']
for i in range(1,4):
    print i
    print 'university'+ str(i) + '.csv'

    #Importing the csv file into df
    df = pd.read_csv('university'+ str(i) + '.csv', sep=";", skiprows=1)

    #Changing datetime
    df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'],
                                                   format='%Y-%m-%d %H:%M:%S:%f')

    #Set index from column
    df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')

    #Add Magnetic Magnitude Column
    df['magnetic_mag'] = np.sqrt(df['MAGNETIC FIELD X (μT)']**2 + df['MAGNETIC FIELD Y (μT)']**2 + df['MAGNETIC FIELD Z (μT)']**2)

    #Copy interesting values
    df2 = df[[ 'ATMOSPHERIC PRESSURE (hPa)',
              'TEMPERATURE (C)', 'magnetic_mag']].copy()

    #Hourly Average and Standard Deviation for interesting values
    df3 = df2.resample('H').agg(['mean','std'])
    df3.columns = [' '.join(col) for col in df3.columns]

    #Daily Average and Standard Deviation for interesting values
    df4 = df2.resample('D').agg(['mean','std'])
    df4.columns = [' '.join(col) for col in df4.columns]

    #Write to new csv
    df3.to_csv('Hours'+ str(i) + '.csv')
    df4.to_csv('Day'+ str(i) + 's.csv')

    #New df3 with stacked hour averages
    df5 = df3.reset_index()
    hour = pd.to_timedelta(df5['YYYY-MO-DD HH-MI-SS_SSS'].dt.hour, unit='H')
    df6 = df5.groupby(hour).mean()
    df6.to_csv('Hours - stacked'+ str(i) + '.csv')

rawDescription: Python: Loop acting on several files and writing new ones


code:
info = ['processor_1_info', 'processor_2_info', 'processor_3_info']
#info = machine_info['processors'] for your code.
print "Processor(s):{:>20s}".format(info[0])
for item in info[1:]:
    print "{:>33s}".format(item)

rawDescription: Python printing Formatted text with lists nested in dict


code:
def key_function(x):
    return len(x)

a = ['green egg','snail and lettuce','bacon','dorse naga','rutabaga ripple','cheese']
a.sort(key=key_function)
print a

rawDescription: Arranging number of strings using Python's lambda function


code:
import re
def finder(st,past_ind=0,l=[]):
   m=re.search(r'(.+)\1+',st)
   if m:
      i,j=m.span()
      sub=st[i:j]
      ind = (sub+sub).find(sub, 1)
      sub=sub[:ind]
      if len(sub)>1:
        l.append([sub,(i+past_ind+1,j+past_ind+1)])
      past_ind+=j
      return finder(st[j:],past_ind)
   else:
      return l

s='AAACACGTACGTAATTCCGTGTGTCCCCTATACGTATACGTTT'
print finder(s)

rawDescription: A more complex version of "How can I tell if a string repeats itself in Python?"


code:
import sys
print sys.stdin.read()

rawDescription: Making use of piped data in python

description: Making use of piped data in python


code:
import csv
with open('GeoIPCountryWhois.csv', mode='r') as f:
    reader = csv.reader(f)
    for num, row in enumerate(reader):
        if '66.35' in row[0]:
            print num, row

rawDescription: Searching and Matching a part of a string in a CSV file using Python


code:
if 'link' not in item:
    print "link has not been given a value"

rawDescription: Scrapy: If key exists, why do I get a KeyError?


code:
import re
p = re.compile(ur'\b(yest(?:e(?:r(?:d(?:ay?)?)?)?)?|tod(?:ay?)?|tom(?:o(?:r(?:r(?:ow?)?)?)?)?)\b', re.IGNORECASE)
test_str = u"yest\nyeste\nyester\nyesterd\nyesterda\nyesterday\ntod\ntoda\ntoday\ntom\ntomo\ntomor\ntomorr\ntomorro\ntomorrow\n\nyesteray\ntomorow\ntommorrow\ntody\nyesteday"
print(p.findall(test_str))
# => [u'yest', u'yeste', u'yester', u'yesterd', u'yesterda', u'yesterday', u'tod', u'toda', u'today', u'tom', u'tomo', u'tomor', u'tomorr', u'tomorro', u'tomorrow']

rawDescription: Python regexp to match full or partial word


code:
class BitFields(object):
    """ A bit field class

        fieldwidth is a tuple or list containing the
        bit width of each field, from least significant
        to most significant.
    """
    def __init__(self, totalwidth, fieldwidths, value=0):
        if sum(fieldwidths) != totalwidth:
            raise ValueError, "Field width error"

        self.fieldwidths = fieldwidths
        self.num_fields = len(fieldwidths)

        #Calculate field offsets
        self.offsets = []
        pos = 0
        for w in fieldwidths:
            self.offsets.append(pos)
            pos += w

        #Set up bitfield attribute names
        self.field_names = ['b' + str(i) for i in range(self.num_fields)]

        #Initialize
        self.setvalue(value)

    #Set all fields to zero
    def clear(self):
        for f in self.field_names:
            setattr(self, f, 0)

    #A generator expression of all the field values
    def _all_fields(self):
        return (getattr(self, f) for f in self.field_names)

    def __str__(self):
        return ', '.join(['%s: 0x%x' % (f, v)
            for f, v in zip(self.field_names, self._all_fields())])

    #Get the register value as an int
    @property
    def value(self):
       return sum(v<<p for v, p in zip(self._all_fields(), self.offsets))

    #Set field values
    def regset(self, **kwargs):
        for f, v in kwargs.items():
            setattr(self, f, v)

    #Set the register from an int
    def setvalue(self, value):
        for f, w in zip(self.field_names, self.fieldwidths):
            #print f, w
            mask = (1<<w) - 1
            v = value & mask
            value >>= w
            setattr(self, f, v)

#Test
fields = (16, 8, 12, 12, 6, 4, 6)
reg = BitFields(64, fields)

#Set some fields by attribute
reg.b0 = 10
reg.b1 = 1
reg.b2 = 3

#Print the register using its __str__ method
print reg

#Print a single field
print reg.b1

#Print the current value of the register in decimal and as a hex string
v = reg.value
print v, hex(v)

#Reset all fields to zero
reg.clear()
print reg

#Set some fields by keyword
reg.regset(b0=7, b1=3, b2=1)
print reg

#Set some fields using a dict
field_dict = {'b0':5, 'b3':0xa, 'b4':0xf}
reg.regset(**field_dict)
print reg

#Set the register from an int or long
n = 0x111133337777ffff
reg = BitFields(64, fields, n)
print reg

v = reg.value
print v, hex(v), n == v

n = 0x123456789abcdef0
reg.setvalue(n)
print reg

v = reg.value
print v, hex(v), n == v

import random

print 'Testing .setvalue()...'
for _ in xrange(50000):
    n = random.randint(0, (1<<64) - 1)
    reg.setvalue(n)
    v = reg.value
    assert v == n, (n, v)
print 'OK'

rawDescription: How to do bit manipulation of 64 bit data and output to a C function in a dll

description: Do bit manipulation of 64 bit data and output to a C function in a dll


code:
import sys

for line in sys.stdin:
    try:
        city, temperature = line.rsplit(None, 1)
        print '%s\t%s' % (city ,temperature)
    except ValueError:
        continue

rawDescription: get city and temperature values into variables to use in reducer file

description: Get city and temperature values into variables to use in reducer file


code:
seen.append(str)
print "--------------"
crawl(str, seen)

rawDescription: Why is this recursion stopping

description: Why is this recursion stopping


rawDescription: Common elements between two lists of lists (intersection of nested lists)


code:
L = []
with open('in.txt','r') as f:
    for line in f:
        l = line.strip().split(',')
        L.append(l)
M = {}
for key in L[0]:
    M[key] = []
for i in range(1,len(L)):
    for j in range(len(L[i])):
        M[ L[0][j] ].append(L[i][j])
print M

rawDescription: Given a text file, how do I make it into a dictionary in Python?


code:
all_compare_tables = []
for Book in rootNode.getBooks():
    CompareTables=[]
    CompareTables.append(Book.getName())
    for Table in Book.getTables():
        CompareTables.append(Table.getName())
    all_compare_tables.append(CompareTables)

print all_compare_tables[0]
print all_compare_tables[1]

# better yet, loop:

for compare_tables in all_compare_tables:
    print compare_tables

rawDescription: Python list Merging

description: Python list Merging


code:
import os

for subdir, dirs, files in os.walk("your root directory"):
    for file in files:
        print os.path.join(subdir, file)
        #processing the files

rawDescription: (Python) Program to read specific data from many output files


code:
import lxml.etree as et
html=\
"""
<!DOCTYPE html>

    <title>Hello HTML</title>

Hello 1

Hello 2

Hello 3

Hello 4

"""
doc = et.fromstring(html)
for i in doc.xpath('.//p[contains(.,"Hello") and not(contains(.,"4"))]'):
    i.text='replaced'
print et.tostring(doc,pretty_print=True)

rawDescription: Search and replace HTML Text, not tags

description: Search and replace HTML Text, not tags


code:
# file foo.py

from multiprocessing import Value, Process
import time

__all__ = ['start', 'stop', 'pause', 'resume']

_statement = None
_process = None

def _target(run_statement):
    """ Target of the foo's process """
    while True:
        if run_statement.value:
            print "I'm running !"
            time.sleep(1)

def start():
    global _process, _statement
    _statement = Value("i", 1)
    _process = Process(target=_target,  args=(_statement,))
    _process.start()

def stop():
    global _process, _statement
    _process.terminate()
    _statement, _process = None, _process

def enable():
    _statement.value = True

def disable():
    _statement.value = False

rawDescription: Control running Python Process (multiprocessing)


code:
import re

def find_between(s, word):
    # Search for your (word followed by ((:a_digit) repeated three times))
    x = re.search("(%s(:\d+){3})" % word, s)
    return x.groups()[0]

for word in l:
    print find_between(s, word).split(':', 1)[-1].split(':')

rawDescription: python - looping through values in a string

description: Python - looping through values in a string


code:
for w, c in tp[1].iteritems():
    print w, c

rawDescription: Removing 'Counter' from Dictionary python


code:
wanted = "fish chips steak"
cnt = Counter()
words = re.findall('\w+', open('1976.03.txt').read().lower())
for word in words:
    if word in wanted:
        cnt[word] += 1
print cnt

rawDescription: Python - Finding word frequencies of list of words in text file


code:
from Tkinter import *
import os
import time

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)    # initializes window
        self.Button1 =  Button(self, text="Blue Buff", command=self.my_time)
        self.Button1.pack()
        self.pack()

    def my_time(self):  # creates a timer starting at 5 min , counts down to 0 then repeats
        min = 4
        sec = 59

        while sec <=60:
            os.system('cls')
            print min, "Minutes", sec, "Seconds"
            self.Button1.configure(text="{}:{}".format(min,sec))
            self.Button1.update()
            time.sleep(1)
            sec -= 1
            if sec == 0:
                 min -= 1
                 sec = 59
            elif min == 0:
                 min = 1

root = Tk()
app = Application(master=root)
app.mainloop()#

rawDescription: Error when trying to display a timer into button text


code:
import paramiko
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname='localhost', username='haiprasan86', password='secret')
print ssh

rawDescription: SSH a linux box using subprocess module

description: SSH a linux box using subprocess module


code:
import numpy as np

def log_bounded(x, verylow=-10**10):
    if np.isscalar(x):               # handle scalars as well
        if x > 0:
            y = np.log(x)
        else:
            y = verylow
    else:
        y = np.empty_like(x)
        y[x > 0] = np.log(x[x > 0])  # compute log only where needed
        y[x <= 0] = verylow
    return y

print log_bounded(-3), log_bounded(np.arange(-2, 3)), log_bounded(3)

rawDescription: Define boudned ufunc for log

description: Define boudned ufunc for log


code:
import os
import os.path

grabmeName = 'blah-grabme-12.csh'

with os.fdopen(os.open(grabmeName, os.O_WRONLY | os.O_CREAT, 0700), 'w') as f:
    f.write("#!/bin/csh -f\n")
    f.write("echo 'hello'\n")
os.system(os.path.abspath(grabmeName))

rawDescription: Creating and executing a tcsh script from within Python


code:
cols[1] = "No_Match"
print '\t'.join(cols)

rawDescription: Python: Elegantly print all *remaining* elements from list in one line


code:
import logging, sys
from StringIO import StringIO

def get_stderr():
    saved_stderr = sys.stderr
    stderr_string_io = StringIO()

    for handler in logging.root.handlers:
        if handler.stream is sys.stderr:
            handler.stream = stderr_string_io

    sys.stderr = stderr_string_io

    try:
        logging.error("Foobar!!!")

    finally:
        # set the stdout and stderr back to their original values
        for handler in logging.root.handlers:
            if handler.stream is sys.stderr:
                handler.stream = saved_stderr

        sys.stderr = saved_stderr

    err_output = stderr_string_io.getvalue()
    return err_output

for x in [1, 2]:
    err_output = get_stderr()
    print  "Run %d: %s" % (x, err_output)

rawDescription: Python StringIO is not correctly capturing data from stderr


rawDescription: Arranging a list by given order in python for dump into json


code: print ''.join([myVariable[i] for i in [0, 3, 4, 6, 7]])

rawDescription: printing parts of a string while skipping others in Python

description: Printing parts of a string while skipping others in Python


code:
def genDocText(func):
    func.__doc__ = r'''
    >>> conn = httplib.HTTPConnection(...)
    >>> %(func_name)s(None, conn)
    Traceback (most recent call last):
    NoneAsInputError: `mystring` should be a string and not `None`!

    >>> %(func_name)s("", conn)
    Traceback (most recent call last):
    EmptyStringError: `mystring` should not be an empty string!
    ''' % {'func_name': func.__name__}
    return func

@genDocText
def function_1(mystring, conn):
    pass

@genDocText
def function_2(mystring, conn):
    pass

@genDocText
def function_n(mystring, conn):
    pass

if __name__ == '__main__':
    print function_1.__doc__
    print function_2.__doc__
    print function_n.__doc__

rawDescription: Better way to test several functions for the same thing in python


code:
with open(r'd:\output1.doc', 'r') as inputfile:
    inputstring = inputfile.read()

data = ast.literal_eval(inputstring)
for key, sublist in data.items():
    print '{}:'.format(key)
    for subdict in sublist:
        for key, value in subdict.items():
            print('{}:{}'.format(key, value))

rawDescription: How to remove commas, brackets in python using regular expression?

description: Remove commas, brackets in python using regular expression


code:
import pandas as pd
import json
import urllib

js = json.loads(urllib.urlopen("test.json").read())
data = js["data"]
rows = [row["row"] for row in data] # Transform the 'row' keys to list of lists.
df = pd.DataFrame(rows, columns=js["columns"])
print df

rawDescription: JSON to Pandas: is there a more elegant solution?


code:
def auto_api(api_classes, glob_dict):
    for key, value in glob_dict.items():
        if isinstance(value, type) and hasattr(value, "auto_api"):
            for api_class in api_classes:
                setattr(value, api_class.__name__, APIProperty(api_class))

class X(object):
    auto_api = True

class Y(object):
    @classmethod
    def method(cls, x, *args):
        print cls, x, args

auto_api((Y,), globals())

#Example

x = X()
x.Y.method(1,2,3)

rawDescription: Partially Evaluating Python Classmethod Based on Where It's Accessed From


code:
#!/usr/bin/env python
import json
import urllib, urllib2

def detect_language(text,
    userip=None,
    referrer="http://stackoverflow.com/q/4545977/4279",
    api_key=None):

    query = {'q': text.encode('utf-8') if isinstance(text, unicode) else text}
    if userip: query.update(userip=userip)
    if api_key: query.update(key=api_key)

    url = 'https://ajax.googleapis.com/ajax/services/language/detect?v=1.0&%s'%(
        urllib.urlencode(query))

    request = urllib2.Request(url, None, headers=dict(Referer=referrer))
    d = json.load(urllib2.urlopen(request))

    if d['responseStatus'] != 200 or u'error' in d['responseData']:
        raise IOError(d)

    return d['responseData']['language']

print detect_language("Python - can I detect unicode string language code?")


code:
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
import re
ar = "("+ar                      #Add the ( with your data
ar = re.sub(r",","+",ar)         #Substitute with + instead of ,
ar = re.sub(r"(?=\:|$)",")",ar)  #look ahead for add `)` after colon
ar = re.sub(r"(?<=)\:","*(",ar)  #Replace the color with *

#NOw you data look likes(0.212+1.231+0.112)*(1.001+3.212+0.002)*(0.002+0.0001+1.1)
#Finally evaluvate the string as a expression
print eval(ar)

rawDescription: for each gives different result?

description: For each gives different result


code:
import threading
from time import sleep

def main():
    t = Test()
    t.go()
    try:
        join_threads(t.threads)
    except KeyboardInterrupt:
        print "\nKeyboardInterrupt catched."
        print "Terminate main thread."
        print "If only daemonic threads are left, terminate whole program."

class Test(object):
    def __init__(self):
        self.running = True
        self.threads = []

    def foo(self):
        while(self.running):
            print '\nHello\n'
            sleep(2)

    def get_user_input(self):
        while True:
            x = raw_input("Enter 'e' for exit: ")
            if x.lower() == 'e':
               self.running = False
               break

    def go(self):
        t1 = threading.Thread(target=self.foo)
        t2 = threading.Thread(target=self.get_user_input)
        # Make threads daemonic, i.e. terminate them when main thread
        # terminates. From: http://stackoverflow.com/a/3788243/145400
        t1.daemon = True
        t2.daemon = True
        t1.start()
        t2.start()
        self.threads.append(t1)
        self.threads.append(t2)

def join_threads(threads):
    """
    Join threads in interruptable fashion.
    From http://stackoverflow.com/a/9790882/145400
    """
    for t in threads:
        while t.isAlive():
            t.join(5)

if __name__ == "__main__":
    main()

rawDescription: Python Threading: Running 2 different functions simultaneously


code: my_dictionary = dict(map(lambda (k,v): (k, f(v)), my_dictionary.iteritems()))

rawDescription: Mapping over values in a python dictionary


rawDescription: regex for ordering/numbering

description: Regex for ordering/numbering


code:
from lxml import html

xml_content = """<Classes>
    <German>
        <Student>
            John
        </Student>
        <Student>
            Adam
        </Student>
    </German>
    <English>
        <Student>
            Mary
        </Student>
    </English>
    <French>
        <Student>
            Anil
        </Student>
        <Student>
            Jack
        </Student>
    </French>
    <Spanish>
        <Student>
            Mary
        </Student>
        <Student>
            Jack
        </Student>
    </Spanish>
</Classes>"""

tree = html.fromstring(xml_content)
classes = tree.xpath('//classes/*')
for language_class in classes:
    print language_class.tag.capitalize()
    for student in language_class.xpath('.//student/span//text()'):
        print "    {}".format(student)

rawDescription: Reading tricky XML with XPATH

description: Reading tricky XML with XPATH


code:
import argparse
p = argparse.ArgumentParser()
p.add_argument('a', nargs='*')
res = p.parse_args()
print res.a

rawDescription: Python Argparse without command keyword

description: Python Argparse without command keyword


code:
import re
exp = re.compile(r'<.*?>')
with open('file.xml') as f:
    lines = ''.join(line for line in f.readlines())
    text_only = exp.sub('',lines).strip()
if 'Harry Potter' in text_only:
   print 'It exists'
else:
   print 'It does not'

rawDescription: extract text from xml documents in python

description: Extract text from xml documents in python


code:
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe

class Foo(object):
    def __unicode__(self):
        return mark_safe("foo")

foo = Foo()
print "foo =", type(foo)

ufoo = unicode(foo)
print "ufoo =", type(ufoo)

forced_foo = force_unicode(foo)
print "forced_foo =", type(forced_foo)

bar = mark_safe("bar")
print "bar =", type(bar)

forced_bar = force_unicode(bar)
print "forced_bar =", type(forced_bar)

rawDescription: Django: Odd mark_safe behaviour?

description: Django: Odd mark_safe behaviour


code:
def changeText(s,l,t):
    return "boo" + t[0]

expr = Literal("A").setParseAction(changeText) + "B"
print expr.parseString("A B").asList()

rawDescription: change string during pyparsing

description: Change string during pyparsing


code:
from lxml import objectify

obj2 =  lxml.objectify.parse('get_subscribers_result.xml')
root = obj2.getroot()

tag = '{http://example.com/123/ss/base/v1_0}subscriberDetail'

for subscriberDetail in (root.xpath('//ns2:subscriberDetail', namespaces={'ns2': 'http://example.com/123/ss/base/v1_0'})):
               print subscriberDetail.key
               print subscriberDetail.value
               print "*********************************"

rawDescription: Python: How can I use lxml objectify's iterchildren to get details of siblings which are in a different namespace


code:
import threading, time, msvcrt, sys

class ValueGetter(object):
    def __init__(self, wait_time_sec = 5.0):
        self.stop_flag = True
        self.wait_time_sec = wait_time_sec
        self.tprint = self.tnow = time.clock()

    def get_value(self):
        self.stop_flag = False
        p = threading.Thread(target=self.print_uthere)
        p.start()
        print 'enter something:'
        retval = ''
        ch = ''
        while not ch == '\r':
            retval += ch
            ch = msvcrt.getch()
            sys.stdout.write(ch)
            self.tprint = time.clock()
        print
        self.stop_flag = True
        p.join()
        return retval

    def print_uthere(self):
        self.tprint = self.tnow = time.clock()
        while not self.stop_flag:
            if self.tnow > (self.tprint + self.wait_time_sec):
                print 'Are you there???'
                self.tprint = time.clock()
            time.sleep(0.01)
            self.tnow = time.clock()

v = ValueGetter()
print v.get_value()

rawDescription: Time issue in Python

description: Time issue in Python


code:
print re.sub(r"\b(hell)\b",r"\1",x,flags=re.I)

print re.sub(r"\b(hell\S*)",r"\1",x,flags=re.I)

print re.sub(r"\b(\S*hell\S*)",r"\1",x,flags=re.I)

print re.sub(r"(hell)",r"\1",x,flags=re.I)

rawDescription: Python case insensitive find and replace with the same found word


code:
def world_tick(self):
    if __name__ == '__main__':
        print "World tick"
        actor_chunk = len(self.actors)/self.numThreads
        if len(self.processes)==0:
            for _ in range(self.numThreads):
                new_process = multiprocessing.Process(WorldThread.WorldProcess.work, args=(_, self.actors[_*actor_chunk,(_+1)*actor_chunk]))

rawDescription: Anything behind 'if __name__ == "__main__":' not executing


code:
import timeit
import re

FILE_NAME = r'input.txt'

def re_approach():
    return len(set(re.findall(r'(?=(.{5}))', input[:-1])))

def iter_approach():
    return len(set([input[i:i+5] for i in xrange(len(input[:-6]))]))

with open(FILE_NAME, 'r') as fh:
    input = fh.read()

# verify that the output of both approaches is identicle
assert set(re.findall(r'(?=(.{5}))', input[:-1])) == set([input[i:i+5] for i in xrange(len(input[:-6]))])
print timeit.repeat(stmt = re_approach, number = 500)
print timeit.repeat(stmt = iter_approach, number = 500)

rawDescription: Faster operation reading file

description: Faster operation reading file


code: for i in range(10, 0, -2): print i

rawDescription: Countdown function with two parameters

description: Countdown function with two parameters


code:
import numpy as np
a = np.array([ 8.08970226,  np.NAN,  np.NAN,  8.30043545,  np.NAN,  np.NAN,   np.NAN,  np.NAN])
np.ma.fix_invalid(a)
print _

rawDescription: Python: change numpy array with NaNs to array with numbers and '--'


code:
import sys
import xml.etree.ElementTree as ElementTree

tree = ElementTree.parse("test.xml")
root = tree.getroot()

#The keyword to find and remove
keyword = "GENTEST05"

for element in list(root):

    for sub in element.iter():

        if sub.text and keyword in sub.text:
            root.remove(element)
            print ElementTree.tostring(root)
            sys.exit()

rawDescription: Modify XML node using Python

description: Modify XML node using Python


code:
dict1 = {item[0]: item for item in list1}
dict2 = {item[0]: item for item in list2}
print [dict2.get(item, dict1.get(item)) for item in dict1.viewkeys() | dict2]

rawDescription: Match lists and write result to new list


code:
from collections import defaultdict
genes_dict = defaultdict(list)
for line in open("filename",'r'):
    _,val,key = line[::-1].split(" ",2)
    genes_dict[key[::-1]].append(val[::-1])

for key in genes_dict:
    vals = ""
    for val in genes_dict[key]:
        vals +=","+val
    print key,vals.lstrip(",")

rawDescription: Adding different genes for same chromosomal regions to single line in Python


code: print 'elapsed %.5f seconds' % evaluate(334863)

rawDescription: What is most efficient way of setting row to zeros for a sparse scipy matrix?


code:
listOfItems = [['A','A','A'],['A','B','C'],['A','A','A'],['A','B','B'],['A','B','C']]
setOfItems = []
newListOfItems = []
for item in listOfItems:
    if item in setOfItems:
        continue
    setOfItems.append(item)
    temp = list(item)
    occurence = listOfItems.count(item)
    temp.append(occurence)
    newListOfItems.append(temp)

print newListOfItems

rawDescription: How to count frequency of multiple combination of value in Python

description: Count frequency of multiple combination of value in Python


code:
test_string = 'A177T'
to_search = 'COADREAD ZNF271 Missense_Mutation MAAA-AA-3947 Q14591 A177T'
if test_string in to_search.split(' '):
    print "found", test_string

rawDescription: creating and match token between two dictionaries

description: Creating and match token between two dictionaries


code:
# function which returns avg mark given the name
avg_mark = lambda name:sum(s[name])/len(s[name])
for i in sorted(s.keys(),key=avg_mark,reverse=True):
    print i,avg_mark(i)

rawDescription: Select and sort integers/strings from a text file in Python


code:
def main():
    win= GraphWin("lab12",500,500)
    score=0
    c,x,y = draw_circle(win)
    while score < 10:
        mouseClick2=win.getMouse()
        if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
            score=score + random.randint(0,5)
        c,x,y = draw_circle(win, c)

    print "you won"
    print "your final score is, {0}".format(score)

rawDescription: Circle Clicking game simple python

description: Circle Clicking game simple python


code:
class ImportWrapper(object):
    def __init__(self, real_import):
        self.real_import = real_import

    def wrapper(self, wantedModules):
        def inner(moduleName, *args, **kwargs):
            if moduleName in wantedModules:
                print "IMPORTING MODULE", moduleName
                self.real_import(*args, **kwargs)
            else:
                print "NOT IMPORTING MODULE", moduleName
        return inner

    def mock_import(self, moduleName, wantedModules):
        __builtins__.__import__ = self.wrapper(wantedModules)
        try:
            __import__(moduleName, globals(), locals(), [], -1)
        finally:
            __builtins__.__import__ = self.real_import

rawDescription: Python, unit-testing and mocking imports


code:
def f(*args, **kwargs):
    a = args[0]
    b = args[1]
    c = kwargs.get('c', 3)
    d = kwargs.get('d', 4)
    print a, b, c, d

rawDescription: Remove *args and **kwargs from method signatures using rope


code:
dict1= {0:'One', 1:'Two', 2:'Three'}
print {value:{} for key,value in dict1.items()}

rawDescription: Set the name of dictionary in Python

description: Set the name of dictionary in Python


code:
while current_index <=top_number:
    url="http://www.imdb.com/search/title?at=0&sort=user_rating&start="+str(current_index)+"&title_type=feature&year=2005,2014"
    print url
    if top_number==55:
      lis=read_m_from_url(url,top_number-current_index+1)
    else:
     lis=read_m_from_url(url,50)

rawDescription: Infinite loop in while

description: Infinite loop in while


code:
def convert(stuff):
    it = iter(stuff)
    return zip(it, it, it)

for date, type, amount in convert(data):
    print date, type, amount

rawDescription: Getting 2nd Next Value in For Loop Python


code:
for c in range(10,-1,-1):
    print c

rawDescription: Counting backward from an integer using Python range built-in function, related to parse trees


code:
import collections
d=collections.defaultdict(list)
for link in mysite.getallLinks():
    d[link[0]].append(link)             # Dict of first letter -> list of links

# Print all links starting with firstletter
for link in d[firstletter]:
    print link

rawDescription: A question on python sorting efficiency

description: A question on python sorting efficiency


code:
emails = 'john45@email.com bob@email.com joe2@email.com'.split()

import re
print sorted(emails, key=lambda L: (map(int, re.findall('(\d+)@', L)), L))
# ['bob@email.com', 'joe2@email.com', 'john45@email.com']

rawDescription: Sorting a list using a regex in Python

description: Sorting a list using a regex in Python


code:
import itertools

data = []

extra = itertools.cycle(['fL', 'fD', 'dep', 'AOV'])
for line in fileLines:
    for word in line.split():
        text = next(extra)
        print word, text
        data.append([word, text])

rawDescription: Add in words after line split and loop it?


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector

def main():
    x, y = np.random.random((2, 100))
    fig, ax = plt.subplots()
    ax.scatter(x, y, color='black')
    highlighter = Highlighter(ax, x, y)
    plt.show()

    selected_regions = highlighter.mask
    # Print the points _not_ selected
    print x[~selected_regions], y[~selected_regions]

class Highlighter(object):
    def __init__(self, ax, x, y):
        self.ax = ax
        self.canvas = ax.figure.canvas
        self.x, self.y = x, y
        self.mask = np.zeros(x.shape, dtype=bool)

        self._highlight = ax.scatter([], [], s=200, color='yellow', zorder=10)

        self.selector = RectangleSelector(ax, self, useblit=True)

    def __call__(self, event1, event2):
        self.mask |= self.inside(event1, event2)
        xy = np.column_stack([self.x[self.mask], self.y[self.mask]])
        self._highlight.set_offsets(xy)
        self.canvas.draw()

    def inside(self, event1, event2):
        """Returns a boolean mask of the points inside the rectangle defined by
        event1 and event2."""
        # Note: Could use points_inside_poly, as well
        x0, x1 = sorted([event1.xdata, event2.xdata])
        y0, y1 = sorted([event1.ydata, event2.ydata])
        mask = ((self.x > x0) & (self.x < x1) &
                (self.y > y0) & (self.y < y1))
        return mask

main()

rawDescription: Choosing a box of data points from a plot


code:
while True:
    print inst.ask('channel')
    time.sleep(1)

rawDescription: PyVisa and Printing New Data

description: PyVisa and Printing New Data


code:
import re, mmap, os

def conv(s):
    try:
        return float(s)
    except ValueError:
        return s

data_dict={}
with open(fn, 'r') as fin:
    size = os.stat(fn).st_size
    data = mmap.mmap(fin.fileno(), size, access=mmap.ACCESS_READ)
    for m in re.finditer(r'^(\w+)$([\d\s,.]+)', data, re.M):
        data_dict[m.group(1)]=[[conv(e) for e in line.split(',')]
                        for line in m.group(2).splitlines() if line.strip()]

print data_dict

rawDescription: Python - Parse text file and create lists based on some criteria


code:
from unittest import TestCase

from nose.tools import istest

def my_verification_method(param):
    """ description """
    print "this is param=", param
    assert param > 0, "something bad..."

def method_name(param):
    """ this is how you name the tests from param values """
    return "test_with_param(%i)" % param

def doc_name(param):
    """ this is how you generate doc strings from param values """
    return "test with param=%i" % param

def _create(param):
    """ Helper method to make functions on the fly """

    @istest
    def func_name(self):
        my_verification_method(param)

    return func_name

def populate(cls, params):
    """ Helper method that injects tests to the TestCase class """

    for param in params:
        _method = _create(param)
        _method.__name__ = method_name(param)
        _method.__doc__ = doc_name(param)
        setattr(cls, _method.__name__, _method)

class AppsTest(TestCase):
    """ TestCase Container """

    pass

test_params = [-1, 1, 2]
populate(AppsTest, test_params)

rawDescription: python nosetests setting the test description

description: Python nosetests setting the test description


code:
import _md5

def md5_getstate(md):
    return (md.A, md.B, md.C, md.D, md.count + [], md.input + [], md.length)

def md5_continue(state):
    md = _md5.new()
    (md.A, md.B, md.C, md.D, md.count, md.input, md.length) = state
    return md

m1 = _md5.new()
m1.update("hello, ")
state = md5_getstate(m1)
m2 = md5_continue(state)
m2.update("world!")
print m2.hexdigest()

m = _md5.new()
m.update("hello, world!")
print m.hexdigest()

rawDescription: Hash algorithm for dynamic growing/streaming data?


code: print sniff.__doc__

rawDescription: Python stop sniffing upon specific condition


code:
import datetime

tz = "+10:00"

def tz_to_timedelta(tz):
    min = datetime.datetime.strptime('', '')
    try:
        return -(datetime.datetime.strptime(tz,"-%H:%M") - min)
    except ValueError:
        return datetime.datetime.strptime(tz,"+%H:%M") - min

print tz_to_timedelta(tz)

rawDescription: Is there a wildcard format directive for strptime?


code:
eastern = pytz.timezone('US/Eastern')
d = datetime.datetime(2014,03,21,7)
dateeastern = eastern.localize(d)
dateeastern
dateutc = dateeastern.astimezone(pytz.utc)
dateutc

rawDescription: Incorrectly converting between Eastern and GMT time


code:
# in Python, it's common to operate a line at a time on a file instead of
# reading the entire thing into memory

my_file = open("test100.html")
for line in my_file:
    print defang_named(line)
my_file.close()

rawDescription: Some Basic Python Questions

description: Some Basic Python Questions


code:
import copy
import inspect

class MetaFunction(type):
    def __new__(metacls, cls_name, cls_bases, cls_dict):
        result_class = type.__new__(metacls, cls_name, cls_bases, cls_dict)
        if '__call__' in cls_dict:
            original_call = cls_dict['__call__']
            args, varargs, kwargs, defaults = inspect.getargspec(original_call)
            args = args[1:]
            if defaults is None:
                defaults = [''] * len(args)
            else:
                defaults = [''] * (len(args) - len(defaults)) + list(defaults)
            signature = []
            for arg, default in zip(args, defaults):
                if default:
                    signature.append('%s=%s' % (arg, default))
                else:
                    signature.append(arg)
            if varargs is not None:
                signature.append(varargs)
            if kwargs is not None:
                signature.append(kwargs)
            signature = ', '.join(signature)
            passed_args = ', '.join(args)
            new_call = (
                    """def __call__(self, %(signature)s):
                           result = self._call(%(passed_args)s)
                           if self._%(cls_name)s__negate:
                               result = -result
                           return result"""
                           % {
                               'cls_name':cls_name,
                               'signature':signature,
                               'passed_args':passed_args,
                              })
            eval_dict = {}
            exec new_call in eval_dict
            new_call = eval_dict['__call__']
            new_call.__doc__ = original_call.__doc__
            new_call.__module__ = original_call.__module__
            new_call.__dict__ = original_call.__dict__
            setattr(result_class, '__call__', new_call)
            setattr(result_class, '_call', original_call)
            setattr(result_class, '_%s__negate' % cls_name, False)
            negate = """def __neg__(self):
                            "returns an instance of the same class that returns the negation of __call__"
                            negated = copy.copy(self)
                            negated._%(cls_name)s__negate = not self._%(cls_name)s__negate
                            return negated""" % {'cls_name':cls_name}
            eval_dict = {'copy':copy}
            exec negate in eval_dict
            negate = eval_dict['__neg__']
            negate.__module__ = new_call.__module__
            setattr(result_class, '__neg__', eval_dict['__neg__'])
        return result_class

class Base(object):
    __metaclass__ = MetaFunction

class Power(Base):
    def __init__(self, power):
        "power = the power to raise to"
        self.power = power
    def __call__(self, number):
        "raises number to power"
        return number ** self.power

rawDescription: Implementing __neg__ in a generic way for all subclasses in Python


code:
from BeautifulSoup import BeautifulSoup
import urllib
import re

site = urllib.urlopen('http://duckduckgo.com/?q=example')
data = site.read()

parsed = BeautifulSoup(data)
topics = parsed.findAll('div', {'id': 'zero_click_topics'})[0]
results = topics.findAll('div', {'class': re.compile('results_*')})

print results[0].text

rawDescription: duckduckgo API not returning results

description: Duckduckgo API not returning results


code:
import itertools
print freq_sorted(itertools.chain.from_iterable(data))
print freq_sorted(itertools.chain.from_iterable(data), include_freq=True)

rawDescription: Ranking Elements of multiple Lists by their count in Python


code:
import time
import netaddr

ipset1 = netaddr.IPSet(open('iplist1.txt','r').readlines())
ipset2 = netaddr.IPSet(open('iplist2.txt','r').readlines())

print "Set 1:", len(ipset1), "IP addresses"
print "Set 2:", len(ipset2), "IP addresses"

start = time.time()
ipset = ipset1 & ipset2
print "Elapsed:", time.time() - start
print "Intersection:",len(ipset),"IP addresses"

rawDescription: Intersections between two lists of thousands of networks


code:
def daterange(d1, d2):
    return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1))

for d in daterange(date1, date2):
    print d.strftime('%Y.%m.%d')

rawDescription: Creating a loop for two dates

description: Creating a loop for two dates


code:
print varBinds
print varBinds[0]
print varBinds[0][0]
print varBinds[0][1]

rawDescription: Getting Data from an SNMP port in Python


rawDescription: beautifulsoup configure autoclosing tags

description: Beautifulsoup configure autoclosing tags


code:
import xml.etree.cElementTree as etree
from pprint import pprint
tree = etree.parse('find_title.xml')
for value in tree.getiterator(tag='title'):
    print value.text
for value in tree.getiterator(tag='page'):
    pprint(value.attrib)

rawDescription: Print XML tag attributes using element tree


code:
for i, j in dict.iteritems():
    newNumber = phoneNum.replace(i, j)
    print "Number: ",newNumber

rawDescription: Trouble with Changing Characters in a String Then Returning the String


code:
import re

string = 'I have values "1" and "1.5" also "12.555"'
m = re.findall(r'\"(\d+|\d+\.\d+)\"', string)
print ", " . join(m)

rawDescription: Perl regex vs. Python regex non-capture seems to work differently


code:
from collections import deque

class Buffer(deque):
    def put(self, iterable):
        for i in iterable:
            self.append(i)

    def peek(self, how_many):
        return ''.join([self[i] for i in xrange(how_many)])

    def get(self, how_many):
        return ''.join([self.popleft() for _ in xrange(how_many)])

buf = Buffer()
buf.put('abcdefg')
print buf
print buf.peek(4)
print buf
print buf.get(5)
print buf

rawDescription: Python string FIFO

description: Python string FIFO


code:
import numpy as np
import timeit

def meth1(pts):
   pts = np.array(pts)  #Skip if pts is a numpy array already
   lp = len(pts)
   arr = np.zeros((lp,lp,lp,3))
   arr[:,:,:,0] = pts[:,None,None]  #None is the same as np.newaxis
   arr[:,:,:,1] = pts[None,:,None]
   arr[:,:,:,2] = pts[None,None,:]
   return arr

def meth2(pts):
   lp = len(pts)
   N = lp
   arr = np.zeros((lp,lp,lp,3))
   for i in xrange(0, N):
      for j in xrange(0, N):
         for k in xrange(0, N):
            arr[i,j,k,0] = pts[i]
            arr[i,j,k,1] = pts[j]
            arr[i,j,k,2] = pts[k]

   return arr

pts = range(10)
a1 = meth1(pts)
a2 = meth2(pts)

print np.all(a1 == a2)

NREPEAT = 10000
print timeit.timeit('meth1(pts)','from __main__ import meth1,pts',number=NREPEAT)
print timeit.timeit('meth2(pts)','from __main__ import meth2,pts',number=NREPEAT)

rawDescription: Constructing a 3D cube of points from a list


rawDescription: Converting a List of Tuples into a Dict in Python


code:
from Tkconstants import DISABLED, NORMAL
from Tkinter import Tk, Canvas
import Tkinter
import base64

import ImageTk

_FONTCOLOR = "#FFFFFF"
_BGCOLOR = "#787878"
_ICONDATA = '''iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAGXRFWHRTb2Z0d2FyZQBBZ
 G9iZSBJbWFnZVJlYWR5ccllPAAAAUJJREFUeNrsVc1qhDAQHq20FnRP7kF68iB9D+99Dp/J5+jdR9Bz8VwU
 PChbQbdo00yYLLuSrW4x9NKBjy+RL5nMZ34MxhjoDBM0h/YExoy3DCYnRtxx3BMbV8QTxxdVvaT7JGYWiV2
 OPbE1GywH9RwDh83xqEiCupHjg6Mmnixa+T5JkrelmuM4fimK4nVJF4bhM6cjLkqWu1vp69P8Q9u2MI7j/P
 OO5hV+yn9wEVEUqRI8yAZOmuc5NE0DWZbBMAznupN95o276KQryxI8z4MgCMD3fajrWqn79Tnoug5c1xVtZ
 LRq04Om8H3bBI7jQN/3oo2M/U0ToO9VVQlrkLH/UwJ2y/HHsG0b97vYRcjYV+mss5N6EWmaqhIc5zZdsWaS
 SUzqHFZW8L5Sd5CLNqgKbXeR9ttU/3vw/yb/eYJvAQYA4v5708p9noAAAAAASUVORK5CYII='''

class DesktopBtn(Tkinter.Button):

    def __init__(self, parent, buttonName, connector=None, **options):
        '''
        @param buttonName: Name of the button

        '''
        Tkinter.Button.__init__(self, parent, **options)
        self._imagePath = 'button.gif'
        self._BtnPresspath = 'buttonP.gif'
        self._BtnPressImage = Tkinter.PhotoImage(file=self._BtnPresspath)
        self._image = Tkinter.PhotoImage(file=self._imagePath)
        self.bind('<ButtonPress-1>', self._on_pressed)
        self.bind('<ButtonRelease-1>', self._on_release)
        self._parent = parent
        self._btnName = buttonName
        self._connector = connector
        self.config(width=70,
                    height=65,
                    borderwidth=0,
                    compound=Tkinter.CENTER,
                    font=("Arial", 9, "bold"),
                    foreground=_FONTCOLOR,
                    activebackground=_BGCOLOR,
                    text=buttonName,
                    wraplength=64,
                    image=self._image,
                    command=self._onClickSwitch
                    )

    def _on_pressed(self, event):
        if self.cget("state") != "disabled":
            self.config(relief="flat")
            self.config(image=self._BtnPressImage)

    def _on_release(self, event):
        if self.cget("state") != "disabled":
            self.config(image=self._image)

    def _onClickSwitch(self):
        self.config(relief="flat")
        if self._connector:
            self._connector.switchDesktop(self._btnName,
                                          "test")

    def getButtonName(self):
        return self._btnName

    def setConnector(self, connector):
        self._connector = connector

class CustomButton(Tkinter.Canvas):
    def __init__(self, parent, buttonname=None, icon=None, command=None, **options):
        Tkinter.Canvas.__init__(self, parent, borderwidth=0, highlightthickness=0,**options)
        self.command = command
        self._imagePath = 'button.gif'
        self._BtnPresspath = 'buttonP.gif'
        self._icon = icon
        self._BtnPressImage = Tkinter.PhotoImage(file=self._BtnPresspath)
        self._image = Tkinter.PhotoImage(file=self._imagePath)
        self.bgimage = self.create_image(35, 35, image=self._image)
        self.text = buttonname
        if self._icon:
            self._icondata = base64.b64decode(self._icon)
            self._iconimage = ImageTk.PhotoImage(data=self._icondata)
            self.create_image(35, 35, image=self._iconimage)
        if self.text and self._icon:
            self.create_text(35, 63, anchor="s",
                             text=self.text,
                             font=("arial", 8, "bold"),
                             fill=_FONTCOLOR)
        elif not self._icon:
            self.create_text(35, 45, anchor="s",
                             text=self.text,
                             font=("arial", 8, "bold"),
                             fill=_FONTCOLOR)
        self.configure(width=70, height=70)
        self._activation()

    def disable(self):
        self.config(state=DISABLED)
        self._activation()

    def enable(self):
        self.config(state=NORMAL)
        self._activation()

    def _activation(self):
        if self.cget("state") == "disabled":
            self.create_rectangle(-1,-1,70,70, stipple="gray50", fill="white")
            self.unbind("<ButtonPress-1>")
            self.unbind("<ButtonRelease-1>")
        else:
            self.bind("<ButtonPress-1>", self._on_press)
            self.bind("<ButtonRelease-1>", self._on_release)

    def _on_press(self, event):
        self.itemconfig(self.bgimage, image=self._BtnPressImage)
        print "pressed"

    def _on_release(self, event):
        self.itemconfig(self.bgimage, image=self._image)
        if self.command is not None:
            self.command()

tk = Tk()
but = DesktopBtn(tk, "test")
but.config(state="disabled")
but.pack()
butt_blank = CustomButton(tk)
butt_text = CustomButton(tk, buttonname="test", state=DISABLED)
butt_icon = CustomButton(tk, icon=_ICONDATA)
butt_icon_text = CustomButton(tk, icon=_ICONDATA, buttonname="test")
butt_blank.pack()
butt_text.pack()
butt_icon.pack()
butt_icon_text.disable()
butt_icon_text.pack()
tk.mainloop()

rawDescription: How to disable a Tkinter canvas object

description: Disable a Tkinter canvas object


code:
from Bio import SeqIO
temp = {}
for line in open("test.txt","r"):
    i, c = line.strip().split()
    temp[i] = c

for rec in SeqIO.parse("mymodified_transcript.fa","fasta"):
    if str('>'+rec.id) in temp.keys():
        print str('>'+rec.id), temp['>'+rec.id]
        print str(rec.seq)

rawDescription: Extract multiple sequences from fasta but with different name


code:
import getpass
print getpass.getuser()

rawDescription: How do I get the username in Python?

description: Get the username in Python


code:
from subprocess import check_output
user_name = check_output('whoami').strip()
print user_name
#out: userX

rawDescription: how to hide or delete the 0 when call to os.system in pyhon?

description: Hide or delete the 0 when call to os.system in pyhon


code:
files = ''
for filename in os.listdir(dir):
      if filename.endswith('.bat'):
         files += filename + ' READY \n'
         time.sleep(1)
         os.system('cls')
print files

rawDescription: I need to display the specific format file in real time scenario


code:
from twython import Twython

twitter = Twython()
followers = twitter.get_followers_ids(screen_name = "ryanmcgrath")

for follower_id in followers:
    print "User with ID %d is following ryanmcgrath" % follower_id

rawDescription: How to get twitter followers using Twython?

description: Get twitter followers using Twython


code:
#!/usr/bin/python

import sys

def main():
    print  "hell0\n"

main()

rawDescription: Why won't my python code print anything?

description: Why won't my python code print anything


code:
import pycountry

input_countries = ['American Samoa', 'Canada', 'France']

countries = {}
for country in pycountry.countries:
    countries[country.name] = country.alpha2

codes = [countries.get(country, 'Unknown code') for country in input_countries]

print codes  # prints [u'AS', u'CA', u'FR']

rawDescription: How to convert country names to ISO 3166-1 alpha-2 values, using python

description: Convert country names to ISO 3166-1 alpha-2 values, using python


code:
import pandas as pd
import numpy as np
import talib

df  = pd.DataFrame(np.random.randn(100,4),
    index=pd.date_range(pd.datetime(2000,3,30), freq='B', periods=100),
    columns =['PX_OPEN', 'PX_LAST', 'PX_HIGH', 'PX_LOW'] )

lista3 = ['PX_OPEN', 'PX_LAST', 'PX_HIGH', 'PX_LOW']
lista4 = ['W-MON','W-TUE','W-WED','W-THU','W-FRI']
i0=[]
i1=[]
i2=[]
i3=[]
i4=[]

adxs_list = [([talib.ADX(df['PX_HIGH'].resample(w).values,
                      df['PX_LOW'].resample(w).values, df['PX_LAST'].resample(w).values
                      , timeperiod=3)]) for w in lista4]

    # transposing the arrays and assigning them the original index of that week day
for u,v in [(u,v) for u,v in zip(range(5),lista4)]:
    r = "i{0} = pd.DataFrame(adxs_list[{0}]).transpose().set_index(df.PX_OPEN.resample('{1}').index)".format(u,v)
    exec r

    # combining all the new dataframes into a single dataframe (respecting their indexes)
y0 = [i0, i1, i2, i3, i4]
i_frame = reduce(pd.DataFrame.combine_first, y0)

    # merging this new dataframe into the original df
df = df.merge(i_frame, left_index=True, right_index=True)

    # for some strange reason new column is named 0, so renaming it
names = df.columns.values
names[-1] = 'ADX_w'
df.columns = names

rawDescription: Rolling Weekly Technical Indicator using talib, python and pandas.


code:
from itertools import izip

with open('test.log') as lines:
    for input_line, output_line in izip(lines):
        if output_line.startswith('b'):
            print input_line
            print output_line

rawDescription: Match characters in a line and print the line before python


code:
import os

path = 'Root'
for root, dirnames, filenames in os.walk(path):
    print root
    for filename in filenames:
        print filename
    if not filenames:
        print 'No File Found'
    print

rawDescription: Listing files in a directory recursively - python


code:
from publicsuffix import PublicSuffixList
psl = PublicSuffixList()
print psl.get_public_suffix('mail.yahoo.com')
print psl.get_public_suffix('account.hotmail.co.uk')

rawDescription: How to get the domainname (name+TLD) from a URL in python

description: Get the domainname (name+TLD) from a URL in python


code:
import json
import requests
import time

from datetime import date, timedelta

year_tournament_map = {
    2013: 8273,
    2012: 6978,
    2011: 5861,
    2010: 4940,
    2009: 3419,
    2008: 2689,
    2007: 2175,
    2006: 1645,
    2005: 1291,
    2004: 903,
    2003: 579,
    2002: 421,
    2001: 243,
    2000: 114,
    1999: 26,
}

years = sorted(year_tournament_map.keys())
url = 'http://www.whoscored.com/tournamentsfeed/%s/Fixtures/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}

for year in years:
    start_date = date(year, 11, 1)
    end_date = date(year + 1, 5, 31)
    delta = end_date - start_date

    for days  in range(delta.days + 1):
        time.sleep(0.5)

        test_date = start_date + timedelta(days=days)

        params = {'d': str(test_date).replace('-', ''), 'isAggregate': 'false'}
        response = requests.get(url % year_tournament_map[year], params=params, headers=headers)

        try:
            json_data = response.content.replace("'", '"').replace(',,', ',null,')
            fixtures = json.loads(json_data)

        except ValueError:
            print "Error!!!"

        else:

            if fixtures:  # If there are fixtures
                print ",\n".join([", ".join(str(x) for x in fixture) for fixture in fixtures])  # `fixtures` is a nested list

            else:
               print "No Fixtures Today: %s" %  test_date

rawDescription: XHR Requests not returning all data from website


code:
from mechanize import Browser

b = Browser()
b.open("http://utahcritseries.com/RawResults.aspx")
b.select_form(nr=0)

year = b.form.find_control(type='select')
year.get(label='2005').selected = True

src = b.submit().read()
print src

rawDescription: Python-getting data from an asp.net AJAX application


code:
import itertools
lst = ['a', 'b', 'c', 'd']
combinatorics = itertools.product([True, False], repeat=len(lst) - 1)

solution = []
for combination in combinatorics:
    i = 0
    one_such_combination = [lst[i]]
    for slab in combination:
        i += 1
        if not slab: # there is a join
            one_such_combination[-1] += lst[i]
        else:
            one_such_combination += [lst[i]]
    solution.append(one_such_combination)

print solution

rawDescription: Python: find all possible word combinations with a sequence of characters (word segmentation)


code:
num = [1,2,3,4,5,6]

def rotate(lst,x):
    copy = list(lst)
    for i in range(len(lst)):
        if x<0:
            lst[i+x] = copy[i]
        else:
            lst[i] = copy[i-x]

rotate(num, 2)

print num

rawDescription: Rotating values in a list [Python]

description: Rotating values in a list [Python]