code:
from bs4 import BeautifulSoup
import requests

html = requests.get('http://www.indiainfoline.com/Markets/Company/A.aspx').text
bs = BeautifulSoup(html)

# find the links to companies
company_menu = bs.find("div",{'style':'padding-left:5px'})
# print all companies links
companies = company_menu.find_all('a')
for company in companies:
    print company['href']

rawDescription: How can I best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV?

description: Best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV


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

class WebPage(QObject):
    finished = Signal()
    def __init__(self, data, parent=None):
        super(WebPage, self).__init__(parent)
        self.output = []
        self.data = data
        self.page = QWebPage()
        self.page.loadFinished.connect(self.process)

    def start(self):
        self.page.mainFrame().setHtml(self.data)

    @Slot(bool)
    def process(self, something=False):
        self.page.setViewportSize(self.page.mainFrame().contentsSize())
        frame = self.page.currentFrame()
        elem = frame.documentElement()
        self.gather_info(elem)
        self.finished.emit()

    def gather_info(self, elem, i=0):
        if i > 200: return
        cnt = 0
        while cnt < 100:
            s = elem.toPlainText()
            rect = elem.geometry()
            name = elem.tagName()
            dim = [rect.x(), rect.y(),
                rect.x() + rect.width(), rect.y() + rect.height()]
            if s: self.output.append(dict(pos=(i, cnt), dim=dim, tag=name, text=s))
            child = elem.firstChild()
            if not child.isNull():
                self.gather_info(child, i+1)
            elem = elem.nextSibling()
            if elem.isNull():
                break
            cnt += 1

webpage = None

def print_strings():
    for s in webpage.output:
        print s['pos'], s['dim'], s['tag'], '-',  repr(s['text'])

if __name__ == '__main__':
    app = QApplication(sys.argv)
    data = open(sys.argv[1]).read()
    webpage = WebPage(data)
    webpage.finished.connect(print_strings)
    webpage.start()

rawDescription: generate list of text content with pyqt?

description: Generate list of text content with pyqt


code:
for a in range(2):
    print "a = {:d}".format(a)
    for b in range(3, 5):
        print "b = {:d}".format(b)
        if b == 4:
            print "BREAK!"
            break
    else:
        print "Only run if no break in inner loop"

rawDescription: python break for nested loops without executing sub-sequential code

description: Python break for nested loops without executing sub-sequential code


code:
def withpushback(aniterator):
  pushback = None
  while True:
    if pushback is not None:  # last client action was `send`
      to_yield = pushback
      while pushback is not None:
        pushback = yield to_yield  # iterate until a `next`!
    else:                     # last client action was `next`
      try: to_yield = next(aniterator)
      except StopIteration: break
    pushback = yield to_yield

# an example use...:
w = withpushback(iter(range(7)))
repetitions = {2: 3, 5: 2}
for p in w:
  print p,
  if repetitions.get(p):
    w.send(p)
    repetitions[p] -= 1
print

rawDescription: Critique my prime_factors() function

description: Critique my prime_factors() function


rawDescription: extract each segment from text using regex pattern

description: Extract each segment from text using regex pattern


code:
import os
filepath = os.getcwd()
def MakeFile(file_name):
    temp_path = filepath + file_name
    with open(file_name, 'w') as f:
        f.write('''\
def print_success():
    print "sucesss"
''')
    print 'Execution completed.'

rawDescription: Whats the best way to write python code into a python file?


rawDescription: Pandas: Applying a function whilst splitting a dict into columns


code: print venue.location

rawDescription: Retrieving Venue Information with pylast and last.fm


code:
keys = [k for k in argv if k.startswith('--conf-')]
p = argparse.ArgumentParser()
for k in keys:
    p.add_argument(k, dest=foo(k))
print vars(p.parse_args(argv))

rawDescription: Create variable key/value pairs with argparse (python)


code:
primes = [2]
candidate = 3
while len(primes) < 1000:
    isprime = True
    for testprime in primes:
        if candidate % testprime == 0:
            isprime = False
            break
    if isprime:
        primes.append(candidate)
    candidate = candidate + 2
print primes

rawDescription: Please help, this loop isn't working as expected and I don't know how to use booleans in Python


code:
from lxml import html
raw = '''

12th January 2016
11:22pm
Clothing

carlos santos

10
polo

'''

root = html.fromstring(raw)
span = root.xpath('//td[@class="product"]/span/@class')[0]
print span

rawDescription: Retrieving the name of a class attribute with lxml


code:
import collections
XAndYClass = collections.namedtuple('XAndYClass', 'x y')
xandy = XAndYClass(1, 2)
print xandy.x # prints 1
x, y = xandy # x = 1, y = 2

rawDescription: python - cast a class to a tuple and "autobox" to a for loop

description: Python - cast a class to a tuple and "autobox" to a for loop


rawDescription: Combinations And Mass Set Intersections With Apache Spark (Pyspark)


code:
from itertools import islice

for anchor1 in soup.findAll('div', {"class": "listGrid-price"}):
    textcontent = u' '.join(islice(anchor1.stripped_strings, 0, 3, 2))
    if textcontent:
        print textcontent

rawDescription: Issue in printing data under different classes on html page, together using beautiful soup


code:
days = []
for s in l[0]:
    if s.startswith('DAYS'):
        days.append(s)

print days

rawDescription: Python select columns with similar names


code:
d = {}
for name in names:
    d.setdefault(name,0)
    d[name] += 1
print d
{'foobar': 2, 'foo': 3, 'bar': 1}
srt = sorted(d.items(),key=lambda x:x[1],reverse=True)
most_com,least_com = srt[0][0],srt[-1][0]

rawDescription: Calculate most common string in a wxListBox


code:
for x,y in zip(w,c):
    for j in x:
        for k in y:
           l=hamming_distance(x, y)
    print l

rawDescription: Iterative summation of output generated by a function in a for loop (python)


code:
m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer'])
# Add new key
m.new_key = 'Hello world!'
# Or
m['new_key'] = 'Hello world!'
print m.new_key
print m['new_key']
# Update values
m.new_key = 'Yay!'
# Or
m['new_key'] = 'Yay!'
# Delete key
del m.new_key
# Or
del m['new_key']

rawDescription: How to use a dot "." to access members of dictionary?

description: Use a dot "." to access members of dictionary


code:
foo = 1 #

def test(bar):
    # At this point, "bar" points to the same object as foo.
    bar = 2    # We're updating the name "bar" to point an object "int(2)".
    # 'foo' still points to its original object, "int(1)".
    print foo, bar # Therefore we're showing two different things.

test(foo)

rawDescription: Are integers in Python passed by value or reference?


code:
import MySQLdb
#connect to db
db = MySQLdb.connect("localhost","root","password","testdb" )

#setup cursor
cursor = db.cursor()

#create anooog1 table
cursor.execute("DROP TABLE IF EXISTS anooog1")

sql = """CREATE TABLE anooog1 (
         COL1 INT,
         COL2 INT )"""
cursor.execute(sql)

#insert to table
try:
    cursor.execute("""INSERT INTO anooog1 VALUES (%s,%s)""",(188,90))
    db.commit()
except:
    db.rollback()

#show table
cursor.execute("""SELECT * FROM anooog1;""")
print cursor.fetchall()

db.close()

rawDescription: How can I insert data into a MySQL database?

description: Insert data into a MySQL database


code:
bar_instance = Bar()
print issubclass(bar_instance.__class__, Foo)

rawDescription: What is the issubclass equivalent of isinstance in python?


code:
from subprocess import Popen, PIPE

process = Popen(["stdbuf", "-oL", "./main"], stdout=PIPE, bufsize=1)
for line in iter(process.stdout.readline, b''):
    print line,
process.communicate() # close process' stream, wait for it to exit

rawDescription: Python C program subprocess hangs at "for line in iter"


code:
def is_prime(number):
    amount_of_factors = 0
    for candidate_factor in range(1, number+1):
        if number % candidate_factor == 0:
            amount_of_factors += 1
    if amount_of_factors == 2:
        return True
    else:
        return False

prime_total=0
#generates a list of numbers.
for number in range(2, 101):
    if is_prime(number):
        prime_total += number
print prime_total

rawDescription: python sum of primes

description: Python sum of primes


code:
for atts in ('thing', 'owner', 'color'):
    print ' '.join('%-10s'%getattr(item, attr) for item in bin)

rawDescription: How do I print parameters of multiple Python objects in table form?

description: Print parameters of multiple Python objects in table form


code:
import xml.dom.minidom

data = """<Project attri1="build">
    <Import Project="blahblah" xmlns="someURI" />
    <ItemGroup>
        <Folder Include="Scripts" />
        <Folder Include="Scripts/1.0" />
        <Folder Include="Scripts/2.0/" />
    </ItemGroup>
    <ItemGroup/>
    <ItemGroup>
         <None Include="Scripts/1.0/001.sql" />
         <None Include="Scripts/2.0/002.sql" />
    </ItemGroup>
</Project>"""

dom = xml.dom.minidom.parseString(data)

new_folder = dom.createElement('Folder')
new_folder.setAttribute('Include', 'newpath')

folder = dom.getElementsByTagName('Folder')[0]
folder.parentNode.appendChild(new_folder)

new_script = dom.createElement('Folder')
new_script.setAttribute('Include', 'newsql')

none = dom.getElementsByTagName('None')[0]
none.parentNode.appendChild(new_script)

print dom.toprettyxml()

rawDescription: Modify XML using Python minidom

description: Modify XML using Python minidom


code:
def insert(input,inputList):
    index = search(input,inputList)                    #Get where the value should be inserted
    newInput = [input]+list(inputList[index:])         #Add the end of the list to the input
    if index:
        newInput = list(inputList[:index])+newInput    #Add the start of the list if the index isn't 0
    return newInput

print insert(-5, (1, 5, 10))
#Result: (-5, 1, 5, 10)
print insert(3, (1, 5, 10))
#Result: (1, 3, 5, 10)

rawDescription: Python-search function

description: Python-search function


code:
sample = u'some stuff: éŘ'
''.join(c if 0 < ord(c) <= 127 else '\\u{:04x}'.format(ord(c)) for c in sample)
print ''.join(c if 0 < ord(c) <= 127 else '\\u{:04x}'.format(ord(c)) for c in sample)

rawDescription: Encode non-ascii char into UTF-16

description: Encode non-ascii char into UTF-16


code:
text = "abcdef"
print "<%*s>" % (len(text)+2,text)
print "<%-*s>" % (len(text)+2,text)

rawDescription: variable length of %s with the % operator in python

description: Variable length of %s with the % operator in python


code:
import requests

url = "https://www.virustotal.com/tr/file/7b0140ef63a604abaad37538e58fc2d4f8a50228818bf12afbdbcb9d51b44b96/analysis/"

#-----

r = request.get(url)

print r # status 403

#-----

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0",
}

r = request.get(url, headers=headers)

print r # status 200

rawDescription: Python Get Data From HTML Page

description: Python Get Data From HTML Page


code:
from itertools import groupby

food = [
    {'name': 'apple', 'type': 'fruit'},
    {'name': 'pear', 'type': 'fruit'},
    {'name': 'parrot', 'type': 'vegetable'}]

for group, items in groupby(sorted(food, key=lambda x: x['type']), lambda x: x['type']):
    print group, list(items) # here is group and items' objects in the group

rawDescription: Create list with one key of list of dictionaries


code:
x=2*5
xmlTemplate="""
<personal reference="500.txt">
    <others:sequence>
        <feature:name="{name}" age="{age}" dob="{dob}"/>
    </others:sequence>
</personal>""".format(name='Michael', age=x, dob='15/10/1900')
print xmlTemplate

rawDescription: How to build an xml template with variable attributes from scratch

description: Build an xml template with variable attributes from scratch


code:
import re

d = { 'gardai-plan-crackdown-on-troublemakers-at-protest-2438316.html':
      {'dail': 1,
       'focus': 1,
       'actions': 1,
       'trade': 2,
       'protest': 1,
       'identify': 1 }}

for original_filename in d.keys():
    m = re.search('^(.*)\.html$',original_filename,)
    if not m:
        print "Ignoring the file:", original_filename
        continue
    output_filename = m.group(1)+'.arff'
    with open(output_filename,"w") as fp:
        fp.write('''@RELATION wordcounts

@ATTRIBUTE word string
@ATTRIBUTE count numeric

@DATA
''')
        for word_and_count in d[original_filename].items():
            fp.write("%s,%d\n" % word_and_count)

rawDescription: Creating an ARFF file from python output


code:
from itertools import izip_longest
with open('file1.txt') as f1, open('file2.txt') as f2:

     args = [iter(f2)] * 3
     z = izip_longest(f1, izip_longest(*args), fillvalue='-')
     for line, tup in z:
           print '{:11}'.format(line.strip()), '{:5}{:5}{:>5}'.format(*map(str.strip, tup))

rawDescription: Python - combine text files (specific lines)


code:
def f(s, c, start):
    i = s.find(c, start)
    if i < 0:
        return set([s])
    else:
        return set.union(f(s, c, i+1), f(s[:i]+s[i+1:], c, i))

s = 'aAabbAA'
print f(s, 'A', 0)
# set(['aAabbA', 'aabbAA', 'aAabbAA', 'aabb', 'aAabb', 'aabbA'])

rawDescription: Getting a list of strings with character removed in permutation


code:
def a(b): #Line 1
  print "a",b
def c(b):
  a2 = a
  if b is None:
    def a2(b):
      print 'local',b
  a2(b) #Line 8

rawDescription: What is the pythonic way to have conditional shadowing?


code:
#! /usr/local/bin/python
import sys
import os

# return a set of selected values when a string in the form:
# 1-4,6
# would return:
# 1,2,3,4,6
# as expected...

def parseIntSet(nputstr=""):
    selection = set()
    invalid = set()
    # tokens are comma seperated values
    tokens = [x.strip() for x in nputstr.split(',')]
    for i in tokens:
        if len(i) > 0:
            if i[:1] == "<":
                i = "1-%s"%(i[1:])
        try:
            # typically tokens are plain old integers
            selection.add(int(i))
        except:
            # if not, then it might be a range
            try:
                token = [int(k.strip()) for k in i.split('-')]
                if len(token) > 1:
                    token.sort()
                    # we have items seperated by a dash
                    # try to build a valid range
                    first = token[0]
                    last = token[len(token)-1]
                    for x in range(first, last+1):
                        selection.add(x)
            except:
                # not an int and not a range...
                invalid.add(i)
    # Report invalid tokens before returning valid selection
    if len(invalid) > 0:
        print "Invalid set: " + str(invalid)
    return selection
# end parseIntSet

print 'Generate a list of selected items!'
nputstr = raw_input('Enter a list of items: ')

selection = parseIntSet(nputstr)
print 'Your selection is: '
print str(selection)

rawDescription: Interpreting Number Ranges in Python

description: Interpreting Number Ranges in Python


code:
import csv
import random
from sys import exit

with open('heights.csv','rb') as f:
    names,a = {},{}
    for k,row in enumerate(csv.reader(f)):
        names[k] = row[0]
        a[k]     = int(row[1].replace('.',''))
    print names
    print 'a==',a
    L = len(a)

compute = input(">")

if compute == 1:

    if a[1] - a[0] <= 1000 and all(a[i] - a[i-1] <= 2300 for i in xrange(2,L)):

        read_b = {L-1 : random.randint( max(500,a[1]-a[0]), 1000 ) }
        read_a = {1   : a[0] + read_b[L-1] - a[1] }

        for i in xrange(2,L):
            read_b[L-i] = random.randint( max(500,a[i]-a[i-1]), 2300 )
            read_a[i] = a[i-1] + read_b[L-i] - a[i]

    else:
        exit("The file can't be treated because of the relative values of "
             "the following couples of lines : "+\
             ('(0,1) ' if 1000 < a[1] - a[0] else "")+\
             ' '.join(repr((i-1,i)) for i in xrange(2,L) if 2300 < a[i] - a[i-1]))

    with open('output.txt','w') as fw:
        fw.write('[1]\n1={}\n4={}\n\n[2]\n1={}\n4={}\n\n'.\
                 format(names[0],read_b[L-1],names[1],read_a[1]))

        fw.writelines('[{}]\n1=\n4={}\n\n[{}]\n1={}\n4={}\n\n'.\
                      format(str(2*k-1),read_b[L-k],str(2*k),names[k],read_a[k])
                      for k in xrange(2,L))

    # display of the execution

    print 'read_a== %s\nread_b== %s\n\n' % (read_a,read_b)+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_b[%s] == %s' % (str(L-y-1), read_b[L-y-1])+\
                      '\n    -read_a[%s] == %s' % (str(y+1)  ,-read_a[y+1]  ) )
                     for y in xrange(L-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (L-1,a[L-1])+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_a[%s] == %s' % (y  , read_a[y]  )+\
                      '\n    -read_b[%s] == %s' % (L-y,-read_b[L-y]) )
                     for y in xrange(L-1,0,-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (0,a[0])+\
          ''+\
          'sum(read_a.values())== %s\nsum(read_b.values())== %s' %\
          (sum(read_a.values()),sum(read_b.values()))

rawDescription: How to index and automate processing of list using input csv file

description: Index and automate processing of list using input csv file


code:
for url in urls:
    link_list = []
    r  = requests.get('http://www.' +url)
    data = r.text
    soup = BeautifulSoup(data)

    for link in soup.find_all('a'):
        links = (link.get('href'))
        link_list.append(links)

    if domain in ', '.join(link_list):
        print url +status

rawDescription: live link checking with Python can't get it to run from file

description: Live link checking with Python can't get it to run from file


code:
class DefaultNASubdict(dict):
    class NADict(object):
        def __getitem__(self, k):
            return 'NA'

    NA = NADict()

    def __missing__(self, k):
        return self.NA

nadict = DefaultNASubdict({
                'a': {'j':1,'k':2},
                'b': {'j':2,'k':3},
                'd': {'j':1,'k':3}
            })

print nadict['a']['j']  # 1
print nadict['b']['j']  # 2
print nadict['c']['j']  # NA

rawDescription: Python dict.get() with multidimensional dict


code:
res = function()
print '{} {}'.format(res[0], res[1])
# In python 3 also with unpacking; print('{} {}'.format(*res))

rawDescription: Print to stdout with space's between result?


code:
from django.db import models

class ContactCSVModel(models.Model):
    pass

for field_name in ('first_name', 'mobile', 'last_name'):
    model_field = models.CharField()
    model_field.contribute_to_class(ContactCSVModel, field_name)

print [f.name for f in ContactCSVModel._meta.fields]

# prints:
[u'id', 'first_name', 'mobile', 'last_name']

rawDescription: Django ordering code in class

description: Django ordering code in class


code:
def test2():
    import sys
    root = Tkinter.Tk()
    root.title('Ttk Calendar')
    ttkcal = Calendar(firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')

    if 'win' not in sys.platform:
        style = ttk.Style()
        style.theme_use('clam')

    root.mainloop()

    x = ttkcal.selection
    print 'x is: ', x

rawDescription: python calendar widget - return the user-selected date

description: Python calendar widget - return the user-selected date


code:
def bold_partial_rep(matchobj):
    return '<b>' + matchobj.group(0) + '</b>'

def bold_partial(long_string, partial):
    replacer = re.compile(partial, re.IGNORECASE)
    new_long_string = replacer.sub(bold_partial_rep, long_string)
    print new_long_string

rawDescription: How to preserve case using re.IGNORECASE and .sub?


code:
def typecheck(value):
    try:
        return len(value) == 8 and bool(int(value, 2))
    except TypeError, ValueError:
        return False

rawDescription: Function that returns true or false if certain conditions are met?


code: print namez,add,phone,email

rawDescription: Print function attribute

description: Print function attribute


code: print 0x10929df10

rawDescription: What is the second number in a python weak reference's repr?


code:
@gen.coroutine
def get(self):
    print 'a'
    self.process('data') # I don't want to wait here
    print 'b'
    self.write('page')

@gen.coroutine
def process(self, arg):
    print 'c'
    d = yield gen.Task(self.otherFunc, arg)
    print 'd'
    raise gen.Return(None)

rawDescription: Don't wait for an async function to finish


rawDescription: Combined list and dict comprehension

description: Combined list and dict comprehension


code:
result = set(nested_remainder
    for remainder in all_remainders(34, 5)
    for nested_remainder in all_remainders(remainder, 7)
)

print result

rawDescription: How do i get Python to show all possible remainders instead of just the one that occurs with the largest multiple of the number?

description: Get Python to show all possible remainders instead of just the one that occurs with the largest multiple of the number


code:
#!/usr/bin/python
import os
PROCNAME = "file1.py"
pids = []
for proc_data in os.popen('/bin/ps -eo pid,comm,args'):
    bits = proc_data.strip().split()
    (pid, comm ) = bits[0:2]
    args = " ".join( bits[3:] )
    if args == PROCNAME:
        pids.append( pid )

print pids

rawDescription: how to find multiple PID of same script

description: Find multiple PID of same script


code:
from pyquery import PyQuery as pq
import requests

response = requests.get('http://www.ncbi.nlm.nih.gov/pubmed/?term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care')

namespaces = {'xi': 'http://www.w3.org/2001/XInclude', 'test': 'http://www.w3.org/1999/xhtml'}
links = pq('#maincontent .linkoutlist test|a', response.content, namespaces=namespaces)
for link in links:
    print link.attrib.get("title", "No title")

rawDescription: Using lxml to parse namepaced HTML?

description: Using lxml to parse namepaced HTML


code:
import numpy as np
import pandas

df = pandas.DataFrame({"a": np.random.random(100),
                       "b": np.random.random(100),
                       "id": np.arange(100)})

# Bin the data frame by "a" with 10 bins...
bins = np.linspace(df.a.min(), df.a.max(), 10)
groups = df.groupby(np.digitize(df.a, bins))

# Get the mean of each bin:
print groups.mean() # Also could do "groups.aggregate(np.mean)"

# Similarly, the median:
print groups.median()

# Apply some arbitrary function to aggregate binned data
print groups.aggregate(lambda x: np.mean(x[x > 0.5]))

rawDescription: binning a dataframe in pandas in Python

description: Binning a dataframe in pandas in Python


code:
for key, value in var.items():
    print "{} -> {{{}}}".format(key, ", ".join(value))

rawDescription: Making a dictionary from a list of strings (creating keys from the elements of the list)


code:
from bs4 import BeautifulSoup

source = """

"""
soup = BeautifulSoup(source, 'lxml')
for div in soup.find_all('div'):
    print div.get('style')

rawDescription: Python - How to get total no of style attributes of a specific div


code:
csv.DictWriter and csv.DictReader
import csv

with open("my_file.csv", "rb") as f:
    reader = csv.DictReader(f, ["one", "two", "three"])
    for line in reader:
        print line["one"] # prints the 1st column
        print line["two"] # etc.
        print line["three"] # etc., etc.

rawDescription: make a class __init__ from a list

description: Make a class __init__ from a list


code:
import fileinput
for line in fileinput.input(paths):
    line = line.strip()
    if line.startswith('def '):
        print '%s: %s' % (fileinput.filename(), line.partition(':')[0])

rawDescription: Making Python code Pythonic

description: Making Python code Pythonic


code:
@task
def show_roles():
    for key, value in sorted(env.roledefs.items()):
        print key, value

rawDescription: How can I list the roles defined in a fabfile?

description: List the roles defined in a fabfile


code:
from datetime import datetime
def print_progress(iterable, percent_step=1):
    total = float(len(iterable))
    # or 1 means that iterable has < 100 elems
    abs_step = int((total * percent_step)/100) or 1
    for i, obj in enumerate(iterable):
        if i and not i % abs_step:
             print "{0:.2%} processed, {1:%H:%M:%S}".format(i/total, datetime.now())
        yield obj

rawDescription: Python/Django print loop progress along the way


code:
for k1 in dictionary:
    for k2 in dictionary[k1]:
        print k1, k2, dictionary[k1][k2]

rawDescription: Python reading complex nested json values


code:
import regex

template='{}=>{} with {} substitutions, {} insertions, {} deletions'
tgt='ca bke clss'
for word in ['car','bike','van','class']:
    pat=r'((?:\b{}\b){{e<=2}})'.format(word)
    m=regex.search(pat, tgt, regex.BESTMATCH)
    if m:
        print template.format(m.group(1),word,*m.fuzzy_counts)

rawDescription: python related word search and replace with list words

description: Python related word search and replace with list words


code:
def main():
    app = QtGui.QApplication([])
    window = Widget()
    print window.getData()  # window is value from edit field

rawDescription: How to use QtGui in PyQt4 to collect input variables to a python program?

description: Use QtGui in PyQt4 to collect input variables to a python program


code:
from collections import Counter

def sort_str(s, sort_fmt):
    counter = Counter(s)
    d = set(sort_fmt)
    result = ''.join(c * counter[c] for c in sort_fmt)
    result += ''.join(c for c in s if c not in d)
    return result

if __name__ == '__main__':
    print sort_str('abdcdfs', 'dacg')

rawDescription: String Sort based on some Format

description: String Sort based on some Format


code:
list_vars = []
var = ""

def print_vars():
    print "list_vars = " + str(Shared.list_vars)
    print "var = " + Shared.var

rawDescription: Static variables in Python that are shared across files


code:
for j in M.retr(i+1)[1]:
    print j

rawDescription: Open mail in python

description: Open mail in python


code:
txt='''\
Line 1
line 2
line 3
line 4
last line'''

print txt.rpartition('\n')[0]

rawDescription: How to not print the last line

description: Not print the last line


code:
{0: [{'distance': 2.1, 'home_id': 2L, 'home_number': 1, 'road_id': 0},
     {'distance': 4.1, 'home_id': 1L, 'home_number': 2, 'road_id': 0}],
 1: [{'distance': 4, 'home_id': 3L, 'home_number': 1, 'road_id': 1}],
 2: [{'distance': 2.1, 'home_id': 4L, 'home_number': 1, 'road_id': 2},
     {'distance': 6, 'home_id': 5L, 'home_number': 2, 'road_id': 2}]}

rawDescription: Sorting and re-numbering values in Python dictionary


code:
def sum_digits(n):
    n = str(n)
    if int(n) < 10:
        return n
    else:
        count = 0
        for c in n:
            count += int(c)
    return sum_digits(count)

print sum_digits(123456789) # --> 9  # a string

rawDescription: How can I Keep the following program running until it becomes a single digit number?


code:
import itertools
import operator

seq1 = [[1, [1,2,3,4]], [2, [5,6,7]]]
seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]]

def index_reads(seq):
    return {k: [i[1] for i in g] for k, g in itertools.groupby(seq, operator.itemgetter(0))}

print index_reads(seq1)
print index_reads(seq2)

rawDescription: Dictionary comprehension with nested list, prevent overwrite


code:
import numpy as np

col1 = np.random.random_integers(1, 5, 100)
col2 = np.random.random_integers(1, 10, 100)
bin1 = np.arange(0.5, 6.5, 1)
bin2 = np.arange(0.5, 11.5, 1)

mat = np.histogram2d(col1, col2, bins=[bin1, bin2])[0]
print mat.shape
print mat

rawDescription: Creating a matrix of joint number of hits from two columns using numpy/pandas


code:
from itertools import groupby
from datetime import datetime
def solve(item):
    dt = datetime.strptime(item[0], '%H:%M:%S.%f')
    return dt.hour, dt.minute, dt.second

lis = [['10:09:56.033', 'ComponentB', 2, 'DEBUG', 'description'], ['10:09:56.034', 'ComponentB', 1, 'DEBUG', 'description'], ['10:09:57.034', 'ComponentB', 2, 'DEBUG', 'description'], ['10:09:57.045', 'ComponentB', 2, 'DEBUG', 'description']]

for k, g in groupby(lis, key=solve):
    print '\n'.join(x[0] for x in g) + '\n'

rawDescription: Selecting some elements in python lists

description: Selecting some elements in python lists


code:
def recursiveCounter(message):  # when the decorator is defined
    def decorater(func):  # when the decorator is created - @recursiveCounter("hello I was called once")
        def wrapper(count):  # when the function is decorated - def counter(count): print count
            func(count)  # when the function is called - counter(0)
            if count < 10:  # when the function is called
                count += 1  # when the function is called
                wrapper(count)  # when the function is called

        print message  # **when the function is decorated**
        return wrapper  # when the function is decorated
    return decorater  # when the decorator is created

rawDescription: Decorator is run without being called

description: Decorator is run without being called


code:
def locate_columns(a, b, offset=0):
    """Locate `a` as a sublist of `b`.

    Yields all possible lists of `len(a)` indices such that `a` can be read
    from `b` at those indices.
    """
    if not a:
        yield []
    else:
        positions = (offset + i for (i, e) in enumerate(b[offset:])
                     if e == a[0])
        for position in positions:
            for tail_cols in locate_columns(a[1:], b, position + 1):
                yield [position] + tail_cols

def locate_submatrix(a, b, offset=0, cols=None):
    """Locate `a` as a submatrix of `b`.

    Yields all possible pairs of (row_indices, column_indices) such that
    `a` is the projection of `b` on those indices.
    """
    if not a:
        yield [], cols
    else:
        for j, row in enumerate(b[offset:]):
            if cols:
                if all(e == f for e, f in zip(a[0], [row[c] for c in cols])):
                    for r, c in locate_submatrix(a[1:], b, offset + j + 1, cols):
                        yield [offset + j] + r, c
            else:
                for cols in locate_columns(a[0], row):
                    for r, c in locate_submatrix(a[1:], b, offset + j + 1, cols):
                        yield [offset + j] + r, c

B = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]]
A = [[6,8], [16,18]]

for loc in locate_submatrix(A, B):
    print loc

rawDescription: Is there a way to find out if A is a submatrix of B?


code: print string1.decode('string-escape')

rawDescription: Decoding escaped characters

description: Decoding escaped characters


code:
from lxml import etree
from StringIO import StringIO

tree = etree.parse(StringIO('<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">411,00</string>'))
print tree.xpath('//text()')[0]

rawDescription: Xml with python

description: Xml with python


code:
s = Serializer('sekrit', expires_in=3600)
token1 = s.dumps({'id': 'foobar'})
token2 = s.dumps({'id': 'foobar'})
from difflib import ndiff
print '\n'.join(ndiff(token1.split('.'), token2.split('.')))

rawDescription: python: API token generation with itsdangerous

description: Python: API token generation with itsdangerous


code:
import datetime
import logging
import socket
import webapp2

class TestHandler(webapp2.RequestHandler):

    def get(self):
        size = 1024 * 4
        start = datetime.datetime.now()
        for _ in range(10):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(('216.58.216.4', 80))
            sock.send('GET / HTTP/1.0\n\r\n\r')
            data = ''
            newData = 'dummy'
            while newData != '' and not newData.endswith('\r\n'):
                newData = sock.recv(size)
                #print newData
                data += newData
            print '.'
            sock.close()
        msg = 'timespent:{} for:{}'.format(
            datetime.datetime.now() - start, len(data))
        logging.info(msg)
        self.response.write('<p>{}</p>'.format(msg))

app = webapp2.WSGIApplication([
    ('/timit', TestHandler),
], debug=True)

rawDescription: App Engine Socket API factor 8 slower than native python


code:
from elixir import *

metadata.bind = 'sqlite://'
metadata.bind.echo = False

class Book(Entity):
    PrintTitle = Field(String(50))

setup_all()
create_all()

params = {'PrintTitle':'Ethyl the Aardvark goes Quantity Surveying'}

bk = Book()

print "Title in database (before): {}".format(bk.PrintTitle)

for k, v in params.items():
    setattr(bk, k, v)

print "Title in database (after): {}".format(bk.PrintTitle)

rawDescription: How to reference Python elixir object items

description: Reference Python elixir object items


code:
import inspect

def class_printer(fn):
    cls = inspect.getouterframes(inspect.currentframe())[1][3]
    def decorated_fn(self, msg):
        fn(self,cls+" says: "+msg)
    return decorated_fn

class MyClass():
    @class_printer
    def say(self, msg):
        print msg

rawDescription: Accessing function arguments from decorator


code:
import subprocess

p = subprocess.Popen("python", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("print 1+3\n")
p.stdin.write("print 2+4\n")
p.stdin.close()
print p.stdout.read()

rawDescription: How can I get return values with subprocess?

description: Get return values with subprocess


code:
def done(arg):
    print "%s is done" % arg

rawDescription: Python Process completion

description: Python Process completion


code:
seq = [x for x in names.values() if not isinstance(x, str)]
print max(seq)

rawDescription: getting max(dict_name.values()) to ignore strings

description: Getting max(dict_name.values()) to ignore strings


code:
x = ""
print list(itertools.product(range3(), mutablerange()))

rawDescription: Conjoin function made in functional style


code:
words= 'military  troop  deployment  number  need'
sent = words.split()

linenos = {}

with open("file2","r") as f1:
    for i, line in enumerate(f1):
        for word in sent:
            if word in line:
                linenos[word] = i + 1

matched2 = []
for word in sent:
    if word in linenos:
        matched2.append('%s %d' % (word, linenos[word]))
    else:
        matched2.append('%s <does not exist>' % word)
print ', '.join(matched2)

rawDescription: Get line numbers of words present in a file


code:
import timeit

def f1(aRange):
    x = [2*i for i in aRange]
    y = [3*i for i in aRange]
    return x,y

def f2(aRange):
    x, y = zip(*[(2*i, 3*i) for i in aRange])
    return x,y

def f3(aRange):
    x, y = zip(*((2*i, 3*i) for i in aRange))
    return x,y

def f4(aRange):
    x = []
    y = []
    for i in aRange:
        x.append(i*2)
        y.append(i*3)
    return x,y

print "f1: %f" %timeit.Timer("f1(range(100))", "from __main__ import f1").timeit(100000)
print "f2: %f" %timeit.Timer("f2(range(100))", "from __main__ import f2").timeit(100000)
print "f3: %f" %timeit.Timer("f3(range(100))", "from __main__ import f3").timeit(100000)
print "f4: %f" %timeit.Timer("f4(range(100))", "from __main__ import f4").timeit(100000)

rawDescription: Two assignments in single python list comprehension


code:
class Polynomial(object):
    def __init__(self, coefficients):
        self.coefficients = coefficients

    def __str__(self):
        polytostring = ' '
        for exponent, coefficient in self.coefficients.iteritems():
            if exponent == 0:
                polytostring += '%s + ' % coefficient
            else:
                polytostring += '%sx^%s + ' % (coefficient, exponent)

        polytostring = polytostring.strip(" + ")

        return polytostring

dict1 = {0:1, 1:-1}
p1 = Polynomial(dict1)

dict2 = {1:1, 4:-6, 5:-1, 3:2}
p2 = Polynomial(dict2)

print "First:-", p1
print "Second:-", p2

rawDescription: Pretty printing polynomials with dictionary python


code:
with open('data.txt', 'r') as f:
    theDict = {}
    found = -1
    for (lineno,line) in enumerate(f):
        if found < 0:
            if line.find('ArrayOf_xsd_string')>=0:
                found = lineno
                entries = []
            continue
        # Grab following 6 lines...
        if 2 <= (lineno-found) <= 6+1:
            entry = line.strip(' ""{}[]=:,')
            entries.append(entry)
        #then create a dict with the key from line 5
        if (lineno-found) == 6+1:
            key = entries.pop(4)
            theDict[key] = entries
            print key, ','.join(entries) # comma-separated, no quotes
            #break # if you want to end on first match
            found = -1 # to process multiple matches

rawDescription: Extract specific lines from file and create sections of data in python


code:
from timeit import Timer
setup = """
L = list(range(10000)) + list(range(10100, 30000))
from bisect import insort

def sort_insert(L, x):
    L.append(x)
    L.sort()

from lib.skiplist import SkipList
L2 = SkipList(allowDups=1)
for x in L:
    L2.insert(x)

from lib.sortedcontainers import SortedList
L3 = SortedList(L)
"""

# Using sortedcontainers.SortedList()
t_sortedcontainers = Timer("for i in xrange(10000, 10100): L3.add(i)", setup)
# Using bisect.insort()
t_bisect = Timer("for i in xrange(10000, 10100): insort(L, i)", setup)
# Using a Skip List
t_skiplist = Timer("for i in xrange(10000, 10100): L2.insert(i)", setup)
# Using a standard list insert and then sorting
t_sort_insert = Timer("for i in xrange(10000, 10100): sort_insert(L, i)", setup)

# Timing the results
print t_sortedcontainers.timeit(number=100)
print t_bisect.timeit(number=100)
print t_skiplist.timeit(number=100)
print t_sort_insert.timeit(number=100)

rawDescription: Alternative to python's .sort() (for inserting into a large list and keeping it sorted)


code:
day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
num = day+month+year
while len(num) > 1:
    num = str(sum(map(int, num)))
print num

rawDescription: Python Sum individual integers

description: Python Sum individual integers


code:
import os
import argparse

cases = [n[n.rindex(".")+1:] for n in os.listdir(".") if n.startswith("file.")]
cases.append("_")
parser = argparse.ArgumentParser()
parser.add_argument('case', choices = cases)

args = parser.parse_args()
print args.case

rawDescription: Specifying types and patterns using argparse choices


rawDescription: get escaped unicode code from string

description: Get escaped unicode code from string


code:
name = 'A'
print filter(lambda person: person['name'] == name, phonebook)

rawDescription: Phonebook dictionary

description: Phonebook dictionary


code:
country2 = ' United States 	'
import re
x = re.search('> *([^>]+) *', country2)
print x.group(1)

rawDescription: Splitting a html line to obtain an output


code:
def one_two_three():
    myList1 = range(0, 4)
    for i in myList1:
        yield 'number',i

def four_five_six():
    myList2 = range(3, 6)
    for i in myList2:
        yield 'other_number',i

print [{i:j,k:z} for (i,j),(k,z) in zip(one_two_three(),four_five_six())]

rawDescription: Update dictionary from two yield functions


code: print "I'm running on CPU #%s" % multiprocessing.current_process().name

rawDescription: Python multiprocessing: knowing the thread/CPU number


rawDescription: To fill the missing data lines with pandas reindex function


code:
user_input = raw_input("Enter a 4-letter word:  ")
if len(user_input) == 4:
    print obfuscate(user_input)

rawDescription: Python, obfuscating letters in a list

description: Python, obfuscating letters in a list


code:
avg = lambda nums: sum(nums)/float(len(nums))

with open("test2.xls") as w:
    w.next()  # skip over header row
    for row in w:
        cols = row.split("\t")  # split into columns
        # then split that into fields
        date, time, values, LZA, SZA, LAM = (cols[0], cols[1],
                                             map(float, cols[2:23]),
                                             cols[23], cols[24], cols[25])
        A = [avg(values[i:i+3]) for i in xrange(0, 21, 3)]
        print ('['+ ', '.join(['{:.6f}']*len(A)) + ']').format(*A)
        print avg(A)

rawDescription: Averaging down a column of averaged data


code:
from subprocess import Popen
from os import mkdir

argfile = open('commandline.txt')
for number, line in enumerate(argfile):
    newpath = 'scatter.%03i' % number
    mkdir(newpath)
    cmd = '../abc.py ' + line.strip()
    print 'Running %r in %r' % (cmd, newpath)
    Popen(cmd, shell=True, cwd=newpath)

rawDescription: Parallel running of several jobs in a python script


rawDescription: Asterisk art in python

description: Asterisk art in python


code:
for attr, value in vars(student).iteritems():
    print attr, value

rawDescription: Python: Concatenate two strings for getting a variable from a class


code: print diff.astype(bool).sum()

rawDescription: Numpy sum function returns 1.67772e+07

description: Numpy sum function returns 1.67772e+07


code:
#!/usr/bin/env python
import random
import matplotlib.pyplot as plt
import networkx as nx

def seq(n):
    return [random.gammavariate(alpha=2.0,beta=1.0) for i in range(100)]
z=nx.create_degree_sequence(100,seq)
nx.is_valid_degree_sequence(z)
G=nx.configuration_model(z)  # configuration model

degree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequence
print "Degree sequence", degree_sequence
dmax=max(degree_sequence)

plt.hist(degree_sequence,bins=dmax)
plt.title("Degree histogram")
plt.ylabel("count")
plt.xlabel("degree")

# draw graph in inset
plt.axes([0.45,0.45,0.45,0.45])
Gcc=nx.connected_component_subgraphs(G)[0]
pos=nx.spring_layout(Gcc)
plt.axis('off')
nx.draw_networkx_nodes(Gcc,pos,node_size=20)
nx.draw_networkx_edges(Gcc,pos,alpha=0.4)

plt.savefig("degree_histogram.png")
plt.show()

rawDescription: Generating a graph with certain degree distribution?


code:
for k,v in request.form.items():
    print k, v

rawDescription: Getting information from HTML Form with checkboxes with Python Flask


code:
def correct_change2(nickels, pennies, sum):
    # While you have nickels left and sum is still more than 5 cents...
    while sum > 5 and nickels:
        sum -= 5
        nickels -= 1
    # You've removed as many nickels as you had or sum is now less than 5
    # If you have enough pennies left, return True
    return pennies >= sum

def correct_change(nickels, pennies, sum):
    # Remove as many nickels as you have or as many as sum needs,
    # whichever is less
    sum -= 5 * min(nickels, sum / 5)
    # You're out of nickels or sum is less than 5
    # If you have enough pennies to equal sum, return True
    return pennies >= sum

print correct_change(5,3,11)

rawDescription: Change calculator nickels pennies

description: Change calculator nickels pennies


code:
def reverse_int(m):
    x = 0
    n = m
    if m < 0 :
      n *= -1
    while n > 0 :
        x *= 10
        x += n % 10
        n /= 10
    if m < 0:
      #concatenate a - sign at the end
      return `x` + "-"
    return x

print reverse_int(1234)
print reverse_int(-1234)

rawDescription: python-How to reverse an negative integer or non-integer

description: Python-How to reverse an negative integer or non-integer


code:
getch = _Getch()
print 'Please enter something: '
x = getch()
print x

rawDescription: How to accept input without the need to press enter Python 3

description: Accept input without the need to press enter Python 3


code:
for index in range(len(b)):
    print index, b[index]

rawDescription: How can I iterate over a list of strings with ints in python?

description: Iterate over a list of strings with ints in python


code:
try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

rawDescription: python "up-button" command completion, matlab/julia style

description: Python "up-button" command completion, matlab/julia style


code:
def palin_generator():
    """Generates palindromic numbers."""

    palindromes=[]

    for count in xrange(10000):
        n = str(count)
        if n == n[::-1]:
            palindromes.append(n)

    print palindromes

rawDescription: Palindrome generator

description: Palindrome generator


code:
g.foo = "foo"
print g.foo

rawDescription: Change storage of globals in python

description: Change storage of globals in python


code:
def somefunction(keyFunction, values):
    return dict((keyFunction(v), v) for v in values)

print somefunction(lambda a: a[0], ["hello", "world"])

rawDescription: Map list onto dictionary

description: Map list onto dictionary


code:
d = {}

try:
    gs = GoogleSearch("search query")
    gs.results_per_page = 50
    results = gs.get_results()

    for res in results:
        t = res.title.encode("utf8")
        u = res.url.encode("utf8")
        d[t] = u
except SearchError, e:
    print "Search failed: %s" % e

rawDescription: Create Dictionary out of for loops

description: Create Dictionary out of for loops


code: print cv2.getBuildInformation()

rawDescription: OpenCV built in Debug or Release mode?

description: OpenCV built in Debug or Release mode


code:
board = []

for x in range(0, 5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

rawDescription: Code academy python "Battleship" - why is "none" printing after my grid?


code:
# This is the application's front-end.
#
# The import will succeed if Python can find the `app` package, which
# will occur if the parent directory of app/ is in sys.path, either
# because the user is running the script from within that parect directory
# or because the user has included the parent directory in the PYTHONPATH
# environment variable.

from app.package_a.module_a import aaa
print aaa(123, 456)

rawDescription: Optimal file structure organization of Python module unittests?


rawDescription: Iterating over lines in a file yields wrong number of lines


code:
s = """d.complex.1
10
11
12
10
11
12"""

file_dict = file_loader(s.split("\n"))
print file_dict
print count_number(file_dict, '10')

rawDescription: Comparing two lists items in python

description: Comparing two lists items in python


code:
set1 = set(open("a.txt").read().split())
set2 = set(open("b.txt").read().split())
print filter(lambda x: x in set1, set2)

rawDescription: Search between two files in Python

description: Search between two files in Python


code:
file_name = "testdata.txt"
with open(file_name) as f:
      pre_data,post_data =[s.strip() for s in (f.read()).split("[data]")]
post_data_lines = post_data.splitlines()
headers = post_data_lines[0].split()

for line in post_data_lines[1:]:
    tmpline  = []
    pos = 0
    for itm in headers:
        tmpline.append(line[pos:pos+len(itm)])
        pos += len(itm)+1

    print dict(zip(headers,tmpline))

rawDescription: reading files python

description: Reading files python


code:
def f():
    x = {'a':'b', 'c':'d', 'e':'d'}
    for l in x:
        if x[l] != 'd': continue
        print x

def f1():
    x = {'a':'b', 'c':'d', 'e':'d'}
    for l in x:
        if x[l] == 'd':
            print x

import dis
print dis.dis(f)
print dis.dis(f1)

rawDescription: Loop control, what is more efficient

description: Loop control, what is more efficient


code:
import requests
from bs4 import BeautifulSoup
url = "https://www.facebook.com/hellomeets/events"

html = requests.get(url).text
print html

rawDescription: Python wget saves a file. how to get data in variable


code:
# if diagonal movements are accepted
neighbours = ((-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1))

# if diagonal movements are NOT accepted
neighbours = ((0, -1), (-1, 0), (1, 0), (0, 1))

for neighbour in neighbours:
    if theProblem[stateX + neighbour[0], stateY + neighbour[1]] == '|':
        print "WALL"
    else:
        print "GO"

rawDescription: Cleaner way to compare array values?

description: Cleaner way to compare array values


code:
from boto.ec2 import connect_to_region
images = (connect_to_region('us-west-1').get_all_images(owners='amazon'))
amis = {}
amis = dict([(image.id, image.creationDate) for image in images if 'ami' in image.id ])
candidates = [x for (x,y) in sorted(amis.items(), key=lambda (k,v): v)][:30]
import pdb; pdb.set_trace()
len(candidates)
for image in images:
    print image.id
    if str(image.id) in candidates:
        print "try to remove"
        image.deregister()
    else:
        print "current 30"

rawDescription: Deleting the AMI Image

description: Deleting the AMI Image


code:
fields = data.split('|',4)
result = obsList.parseString(fields[-1])

# we get back a list of objects, dump them out
for r in result:
    print r.dump()
    print

rawDescription: Pandas semi structured JSON data frame to simple Pandas dataframe


code:
mapping = {'some': 2, 'example': 3, 'words': 5}
mapping
for key in sorted(mapping.keys()):
  print key, mapping[key]

rawDescription: How to iterate over the elements of a map in python

description: Iterate over the elements of a map in python


code:
import collections
import itertools

news_db = [{'el1':2, 'el3':4, 'el12':32},
           {'el3':5, 'el4':6, 'el12':21}]

vertices = collections.Counter()
edges = collections.Counter()

for item in news_db:
    vertices.update(**item)
    edges.update(itertools.combinations(item.keys(), 2))

print vertices
print edges

rawDescription: Performance bottleneck in creating network with igraph in Python


code: print s.encode('UTF-8')

rawDescription: mutagen and id3 tags - character encoding confusion

description: Mutagen and id3 tags - character encoding confusion


code:
#!/usr/bin/env python

import numpy as np
import time
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt

plt.ion()
(x,y) = (1,2)
plt.draw()

while True:
    print "loop "
    x += 0.1
    y += 0.1
    points = np.array([[0, 0], [1, 3], [0, 2.5], [x,y], [4, 1], [6, 4]])
    plt.clf()
    vor = Voronoi(points)

####MANUAL PLOTTING

    plt.plot(points[:,0], points[:,1], 'o')
    plt.plot(vor.vertices[:,0], vor.vertices[:,1], '*')
    plt.xlim(-1, 3); plt.ylim(-1, 3)

    for simplex in vor.ridge_vertices:
        simplex = np.asarray(simplex)
        if np.all(simplex >= 0):
            plt.plot(vor.vertices[simplex,0], vor.vertices[simplex,1], 'k-')

    center = points.mean(axis=0)
    for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
        simplex = np.asarray(simplex)
        if np.any(simplex < 0):
            i = simplex[simplex >= 0][0] # finite end Voronoi vertex
            t = points[pointidx[1]] - points[pointidx[0]] # tangent
            t /= np.linalg.norm(t)
            n = np.array([-t[1], t[0]]) # normal
            midpoint = points[pointidx].mean(axis=0)
            far_point = vor.vertices[i] + np.sign(np.dot(midpoint - center, n)) * n * 100
            plt.plot([vor.vertices[i,0], far_point[0]], [vor.vertices[i,1], far_point[1]], 'k--')
    plt.draw()
    time.sleep(0.5)

rawDescription: Redraw plot in same window with scipy / voronoi_plot_2d


code:
# Sets GPIO's to HIGH = Relays OFF
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!!")

print "Setting up GPIO"
GPIO.state = 'initialized'
print "Done setting up"

import module1

print "main: %s" % GPIO.__name__
# Do stuff wth GPIO

module1.Relay(GPIO)

rawDescription: Python: How to use variables acros modules


code:
html = '''
software_0.1-0.log	17-Nov-2009 13:46  	186K
'''

import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(html)
print soup.td.next.next
print soup.td.nextSibling.next

rawDescription: How to filter data from a file using Python?

description: Filter data from a file using Python


code:
lst = ['John','Jack','Martin']
', '.join(['"{}"'.format(value) for value in lst])
print ', '.join(['"{}"'.format(value) for value in lst])

rawDescription: Join a list of strings such that each string is within quotes and comma separated


code:
import re
rr = re.compile(rr, re.VERBOSE)
print rr.findall("de Batz de Castelmore d'Artagnan, Charles Ogier W.12.345")
# [("de Batz de Castelmore d'Artagnan", ', Charles Ogier ', 'W.12.345')]

rawDescription: regular expression to split

description: Regular expression to split


code:
import xml.etree.ElementTree as etree

english_tree = etree.parse('en.xml')
french_tree = etree.parse('fr.xml')

for en, fr in zip(english_tree.iter(), french_tree.iter()):
   assert en.tag == fr.tag        # check if the structure is the same
   if en.tag == 'id':
       assert en.text == fr.text  # identification must be the same
   elif en.tag == 'string':
       if fr.text is None:
           fr.text = en.text
           print en.text          # display the inserted text

# Write the result to the output file.
with open('fr2.xml', 'w') as fout:
    fout.write(etree.tostring(french_tree.getroot()))

rawDescription: Python replace XML content with Etree

description: Python replace XML content with Etree


code:
import glob
import os
print '\n'.join(glob.iglob(os.path.join(mypath, "KRAS_P01446_3GFT_*")))

rawDescription: finding a file name from a substring

description: Finding a file name from a substring


code:
def eat_bacon():
    return 'Om nom nom.'

call_map = {'eat': eat_bacon} # here, I am using the name of method

y = raw_input('Type eat: ')
print call_map[y]()

rawDescription: Turning variable contents into line of code


code:
import PIL.ImageGrab

# pixel color at x, y
print PIL.ImageGrab.grab().load()[x, y]

rawDescription: Python find rgb

description: Python find rgb


code:
for elem, val in dis.iteritems():
    print elem    # your a, b, or c
    print val     # your [] corresponding to a, b, or c

rawDescription: PYTHON - creating multiple lists in a loop


code:
for elt1, elt13, elt2, elt14 in slicedList:
    if elt1 == elt13 and elt2 == elt14:
        print elt1

rawDescription: Loop to Match Parts of List

description: Loop to Match Parts of List


code:
import inspect

def f(s):
    print "  " * (len(inspect.stack())-2), '>>', s
    if len(s) <= 1:
        print "  " * (len(inspect.stack())-2), '<<', s
        return s
    else:
        retval = f(f(s[1:])) + s[0] #Note double recursion
        print "  " * (len(inspect.stack())-2), '<<', retval
        return retval

print f('world')

rawDescription: Can someone explain double recursion python?


code:
def findItByColor(theColor):
    array=[]
    for x in fruits:
        if theColor in x['color']:
            try:
                array.append(x['name']+" "+ str(x['weight']))
            except KeyError:
                size = ' '.join( [ '[Weight %s Mass %s]'
                                   % (str(item['weight']),str(item['mass']))
                         for item in x['size'] ])
                array.append( x['name']+" "+ size )
    return array

print findItByColor("orange")

rawDescription: adding arrays inside dictionaries, inside more arrays?

description: Adding arrays inside dictionaries, inside more arrays


code:
s = "this/is/a/test23.123456789.bin"
last_part = s.split( "/" )[-1].split( "." )
if len( last_part ) != 6:
 print "Improper file format"
else:
 print "Correct: %s" % last_format

rawDescription: string: extract first six characters after the last /

description: String: extract first six characters after the last /


code: print ppn(str(raw_input()))

rawDescription: Treat all Unicode characters as single letters


code: print textwrap.fill(splitme, width=10)

rawDescription: Break up long line of text into lines of fixed width in Python 2.7


code:
import re

regx = re.compile('(?:(?<=\A)|(?<=/)).+?(?=/|\Z)')

for ss in ('Same/Same2/Foot/Ankle/Joint/Actuator/Sensor/Temperature/Value=4.123',
           'Same/Same2/Battery/Name=SomeString',
           'Same/Same2/Home/Land/Some/More/Stuff=0.34'):

    print ss
    print regx.findall(ss)
    print

rawDescription: Regex Python / group quantifiers

description: Regex Python / group quantifiers


code:
today = date.today()
print date(today.year + 1, today.month, today.day)

rawDescription: How to Print next year from current year in Python


code:
for i, o in enumerate(obj):
    print 'obj [%s]:' % i, o.centroid

rawDescription: How to print the coordinates of all the objects in an image, in python

description: Print the coordinates of all the objects in an image, in python


code:
def many_lambdas(x):
    """many_lambdas(x)(y)(z)(q) => x + y * z^q"""
    return lambda y: lambda z: lambda q: x + y * z ** q

print many_lambdas(1)(2)(3)(4) # prints 163

rawDescription: Function Calling With 3 or More Argument Input Fields - function()()()


code:
import re, string

sentences = open("sentences.txt").read().splitlines()
data = open("data.txt").read()
pattern = re.compile(r"(^\s?\w+\b|(\b\w+)[\.?!\s]*$)")
for line in sentences:
    words = line.strip().split()
    first = words[0].lower()
    last = words[-1].translate(None, string.punctuation).lower()
    if (re.search(first, data, re.I)):
        print "Found " + first + " in data.txt"
    if (re.search(last, data, re.I)):
        print "Found " + last + " in data.txt"

rawDescription: regex match first and last word or any word

description: Regex match first and last word or any word


code:
from datetime import datetime

dates = [published.text for published in root.iterfind('.//article/journal/published')]
print max(dates, key=lambda x: datetime.strptime(x, '%d/%m/%Y'))

rawDescription: How do i get newest date through xpath?

description: Get newest date through xpath


rawDescription: Python - loop through list in specific order


rawDescription: parsing string from request data

description: Parsing string from request data


code:
from psycopg2.extensions import adapt

print adapt("Hello World'; DROP DATABASE World;")

rawDescription: How to quote a string value explicitly (Python DB API/Psycopg2)

description: Quote a string value explicitly (Python DB API/Psycopg2)


code:
import sys

def reverse(input):
    return input [::-1]

iter = int(sys.argv[1])

for i in range(iter):
    input = sys.stdin.readline()
    print reverse(input)
    sys.stdout.flush()

rawDescription: How to repeatedly pass inputs and outputs between a C# Stream and a Python Script?

description: Repeatedly pass inputs and outputs between a C# Stream and a Python Script


code:
import inspect

def log(func):
    def wrapped(*args, **kwargs):
        try:
            print "Entering: [%s] with parameters %s" % (func.__name__, args)
            try:
                return func(*args, **kwargs)
            except Exception, e:
                print 'Exception in %s : %s' % (func.__name__, e)
        finally:
            print "Exiting: [%s]" % func.__name__
    return wrapped

def trace(cls):
    for name, m in inspect.getmembers(cls, inspect.ismethod):
        setattr(cls,name,log(m))
    return cls

@trace
class X(object):
    def first_x_method(self):
        print 'doing first_x_method stuff...'
    def second_x_method(self):
        print 'doing second_x_method stuff...'

x=X()
x.first_x_method()
x.second_x_method()

rawDescription: Python dynamically add decorator to class' methods by decorating class


code:
from collections import OrderedDict
v = ['2', '3', '4', '1']
idv = sorted(v)
tabv = OrderedDict(zip(idv, v))
for key, values in tabv.items():
    print key, values

rawDescription: Two string list into dictionary: key order changing


code:
import numpy as np

def replaced(row, a, b):
    row_order = row.argsort()
    a_order = a.argsort()

    sorted_row = row[row_order]
    sorted_a = a[a_order]
    sorted_b = b[a_order]

    sorted_row[np.in1d(sorted_row, sorted_a)] = sorted_b

    # return results in original order
    return sorted_row[row_order.argsort()]

a = np.array([1, 5])
b = np.array([10, 550])

row = np.array([1,2,3,4,5])
print replaced(row, a, b)

row = np.array([5,4,3,2,1])
print replaced(row, a, b)

row = np.array([4, 5, 1, 3, 2])
print replaced(row, a, b)

rawDescription: Numpy array modifying multiple elements at once


code:
def main():
    t1offset = offset(int(t1[-4:]))
    t2offset = offset(int(t2[-4:]))

    t1 = normalize(datetime.strptime(t1[:-6],
           "%a %d %b %Y %H:%M:%S"), t1offset, t1[-5])
    t2 = normalize(datetime.strptime(t2[:-6],
                   "%a %d %b %Y %H:%M:%S"), t2offset, t2[-5])

    if t1>t2:
        print (t1-t2).total_seconds()
    elif t2>t1:
        print (t2-t1).total_seconds()
    else:
        print 0

rawDescription: Timedeltas in Python

description: Timedeltas in Python


code:
with open('data.csv') as data:
    for line in data:
        print line.strip().split('\t\t')

rawDescription: Python file delimited with double tabs

description: Python file delimited with double tabs


code:
#!/usr/bin/env python

"""
Extract all links from a web page
=================================
Author: Laszlo Szathmary, 2011 (jabba.laci@gmail.com)
Website: https://pythonadventures.wordpress.com/2011/03/10/extract-all-links-from-a-web-page/
GitHub: https://github.com/jabbalaci/Bash-Utils

Given a webpage, extract all links.

Usage:
------
./get_links.py <URL>
"""

import sys
import urllib
import urlparse
import re

from BeautifulSoup import BeautifulSoup

class MyOpener(urllib.FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15'

def process(url):
    myopener = MyOpener()
    #page = urllib.urlopen(url)
    page = myopener.open(url)

    text = page.read()
    page.close()

    soup = BeautifulSoup(text)

    urls = []

    for tag in soup.findAll('a', href=True):
        tag['href'] = urlparse.urljoin(url, tag['href'])
        urls.append(tag['href'])

    return urls

# process(url)

def main():

    # Store the urls we were given
    urls_queue = sys.argv[1:]
    urls_found = []
    urls_done = []

    site_roots = []

    # Get the domains to keep us on the same domain (don't follow external links)
    for url in urls_queue:
        mre = re.match('^https?://[^/]*',url,re.IGNORECASE)
        if mre:
            # If we've found a match, add the entire matched string to site_roots
            site_roots.append( mre.group(0) )

    while len(urls_queue) > 0:

        # Get url off the top of the queue
        url = urls_queue.pop()
        urls_done.append(url)

        found = process(url)

        for uf in found:
            # I'd suggest checking to make sure it's on the same domain here
            # any() returns true if any of the elements in the list passed are True
            # In this case, if uf starts with any of the site_root strings.
            # 'not any()' is equivalent to saying 'none'
            if not any( [ uf.startswith( site_root ) for site_root in site_roots ] ):
                continue # Next url, this is off site

            if uf not in urls_found:
                urls_found.append(uf)

            # If we don't have it in the queue, queue it up
            if uf not in urls_queue and uf not in urls_done:
                urls_queue.append(uf)

        print "Done %d; Queued %d; Found %d" % ( len(urls_done), len(urls_queue), len(urls_found) )

    print urls_found
# main()

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

if __name__ == "__main__":
    main()

rawDescription: Extracting links in a class from a webpage


code:
ROOMS = {
  'death': death,
  'central_corridor': central_corridor,
  'laser_weapon_armory': laser_weapon_armory,
  'the_bridge': the_bridge,
  'escape_pod': escape_pod
}

def runner(visit, start):
    room = start

    while True:
        action = visit[room]
        print "\n--------"
        room = action()

runner(ROOMS, 'central_corridor')

rawDescription: Having trouble with functions calling other functions through dictionaries: could someone please explain this code to me?


code: rdd.map(lambda (k, v): (frozenset(k), v)).groupByKey().collect()

rawDescription: A list as a key for PySpark's reduceByKey


code: nodesAt5 = filter(lambda (n, d): d['at'] == 5, P.nodes(data=True))

rawDescription: Select network nodes with a given attribute value


code:
"""
multi_pipe.py
"""
from multiprocessing import Process, Pipe
import time

def reader(pipe):
    output_p, input_p = pipe
    input_p.close()    # We are only reading
    while True:
        try:
            msg = output_p.recv()    # Read from the output pipe and do nothing
        except EOFError:
            break

def writer(count, input_p):
    for ii in xrange(0, count):
        input_p.send(ii)             # Write 'count' numbers into the input pipe

if __name__=='__main__':
    for count in [10**4, 10**5, 10**6]:
        output_p, input_p = Pipe()
        reader_p = Process(target=reader, args=((output_p, input_p),))
        reader_p.start()     # Launch the reader process

        output_p.close()       # We no longer need this part of the Pipe()
        _start = time.time()
        writer(count, input_p) # Send a lot of stuff to reader()
        input_p.close()        # Ask the reader to stop when it reads EOF
        reader_p.join()
        print "Sending %s numbers to Pipe() took %s seconds" % (count,
            (time.time() - _start))

rawDescription: Python multiprocessing - Pipe vs Queue

description: Python multiprocessing - Pipe vs Queue


code:
def classtree(cls, indent=0):
    print '.' * indent, cls.__name__
    for subcls in cls.__subclasses__():
        classtree(subcls, indent + 3)

classtree(BaseException)

rawDescription: Print the Python Exception/Error Hierarchy


code:
with open("/bin/ls", "rb") as fin:
  buf = fin.read()
bytes = map(ord, buf)
print bytes[:10]

rawDescription: Reading a raw binary data in python and converting it into ascii


code:
import timeit

def a():
    x = 0
    for i in xrange(1000):
        if i%2==0 and i%3==0:
            x += 3
        elif i%2==0 or i%3==0:
            x += 2
        else:
            x += 1

def b():
    x = 0
    for i in xrange(1000):
        x += 3 if i%2==0 and i%3==0 else (2 if i%2==0 or i%3==0 else 1)

test_a = timeit.Timer(a)
test_b = timeit.Timer(b)

print min(test_a.repeat(3, 1000))
print min(test_b.repeat(3, 1000))

rawDescription: Is a chained if...else expression in python faster than if..elif..else statements?


rawDescription: How to arrange three lists in such a way that the sum of corresponding elements if greater then appear first?

description: Arrange three lists in such a way that the sum of corresponding elements if greater then appear first


code:
for x in soup.body.div:
    if x.string:
        print x.string.strip()

rawDescription: Extracting content of div with BeautifulSoup


code:
def checkAge(name, a_dict):
    if name in a_dict:
        return a_dict[name]
    else:
        return "No score found for {}".format(name)

result = checkAge(str(input("What is your name?")))
print result

rawDescription: Python List within List - References

description: Python List within List - References


code:
import collections
import math

def printMyString():
  # get inputs
  myStr = raw_input("enter string: ")
  dist = int(raw_input("enter dist: "))

  #create a dict, where each key is a character from myStr and corresponding value is its frequency
  counter = collections.Counter(list(myStr))

  # create an empty list where we will fill our characters to get final string
  o = ['-']*len(myStr)

  # get the most common character
  most_common_char_freq = counter.most_common(1)[0][1]

  # sep is the maximum distance at which repeated instances of the most frequent character m can be located from each other in the final string.
  sep = int(math.ceil(len(myStr)*1.0/most_common_char_freq))

  # if sep is less than given distance, then it is not possible to have such a string.

  if(sep < dist):
    print "such a string is not possible"
    return
  #print "sep", sep

  j = 0 # this marks index at which we can write into the list

  # while we still have characters left, we will continue to fill our output list o
  while len(counter) > 0:
   current_most_common_char = counter.most_common(1)[0][0]       # get the most common character left in counter
   current_most_common_char_freq = counter.most_common(1)[0][1]

   #print "most common: ", current_most_common_char
   while o[j] != '-':  # Go to the next position in the output list where a character is yet to be written.
     j += 1
     if(j == len(o)):  # We are out of places to write, this is bad!
      # print "breaking, o = ", o
      return

   for i in range(current_most_common_char_freq): # For multiple occurences of the current most freq char, we write them one after the other, a distance of 'sep' apart
    #print o
    if (j+i*sep) >= len(o): # If we have to go beyond the length of the output list/string to write a character, then such a string is not possible
      #print "not possible, o, char is ", o, current_most_common_char
      print "such a string is not possible"
      return
    o[j+i*sep] = current_most_common_char # Write to the output list
    #print "updated o: ", o

   del counter[current_most_common_char] # remove the most common character. lets move on to next one in the loop.
   j += 1 # update before moving on

  print ''.join(o) # merge the characters in the output list to get final string

printMyString()

rawDescription: Reordering same characters such that the characters are at least distance d from each other


code:
theList = [6,3,6,8,3,2,8,3,2]

n = 11
print '---------------------', n
print sumPair(theList, n),
print timeit.timeit('sumPair(theList, n)', 'from __main__ import sumPair, theList, n', number = 1000)

print sumPair2(theList, n),
print timeit.timeit('sumPair2(theList, n)', 'from __main__ import sumPair2, theList, n', number = 1000)

print sumPair_gnibbler(theList, n),
print timeit.timeit('sumPair_gnibbler(theList, n)', 'from __main__ import sumPair_gnibbler, theList, n', number = 1000)

n = 1
print '---------------------', n
print sumPair(theList, n),
print timeit.timeit('sumPair(theList, n)', 'from __main__ import sumPair, theList, n', number = 1000)

print sumPair2(theList, n),
print timeit.timeit('sumPair2(theList, n)', 'from __main__ import sumPair2, theList, n', number = 1000)

print sumPair_gnibbler(theList, n),
print timeit.timeit('sumPair_gnibbler(theList, n)', 'from __main__ import sumPair_gnibbler, theList, n', number = 1000)

rawDescription: One loop? python

description: One loop? python


code:
from django.db import connection
class SqlPrintMiddleware(object):
    def process_response(self, request, response):
        sqltime = 0 # Variable to store execution time
        for query in connection.queries:
            sqltime += float(query["time"])  # Add the time that the query took to the total

        # len(connection.queries) = total number of queries
        print "Page render: " + unicode(sqltime) + "sec for " + unicode(len(connection.queries)) + " queries"

        return response

rawDescription: Is there a way to log the total number of queries made in a django view?


code:
class MyApp(object):
  def main(self, argv):
    self.myVar = "bar"
    self.mySub()

  def mySub(self):
    print self.myVar

MyApp().main(sys.argv[1:])

rawDescription: Global variables confusion in python

description: Global variables confusion in python


rawDescription: Pandas dataframe reduce row values between specific dates


code:
import subprocess

_cmd_lst = ['python', '-u', 'timed_printer.py']  # sudo snort -q -A console -i eth0 -c /etc/snort/snort.conf
_rpi_lst = ['python', '-u', 'rpi.py']            # python script that toggles RPi
_alert = 'TIME'                                  # The keyword you're looking for
                                                 #  in snort output

#===============================================================================

# Simple helper function that calls the RPi toggle script
def toggle_rpi():
    subprocess.call(_rpi_lst)

def try_subprocess(cmd_lst, alert, rpi_lst):
    p = subprocess.Popen(' '.join(cmd_lst), shell=True, stdout=subprocess.PIPE, bufsize=1)

    try:
        while True:
            for line in iter(p.stdout.readline, b''):
                print("try_subprocess() read: %s" % line.strip())

                if alert in line:
                    print("try_subprocess() found alert: %s" % alert)
                    toggle_rpi()

    except KeyboardInterrupt:   print(" Caught Ctrl+C -- killing subprocess...")
    except Exception as ex:     print ex
    finally:
        print("Cleaning up...")
        p.kill()
        print("Goodbye.")

def try_pexpect(cmd_lst, alert, rpi_lst):
    import pexpect # http://pexpect.sourceforge.net/pexpect.html

    p = pexpect.spawn(' '.join(cmd_lst))

    try:
        while True:
            p.expect(alert)     # This blocks until <alert> is found in the output of cmd_str
            print("try_pexpect() found alert: %s" % alert)
            toggle_rpi()

    except KeyboardInterrupt:   print(" Caught Ctrl+C -- killing subprocess...")
    except Exception as ex:     print ex
    finally:
        print("Cleaning up...")
        p.close(force=True)
        print("Goodbye.")

def try_pty(cmd_lst, alert, rpi_lst, MAX_READ=2048):
    import pty, os, select

    mfd, sfd = pty.openpty()

    p = subprocess.Popen(' '.join(cmd_lst), shell=True, stdout=sfd, bufsize=1)

    try:
        while True:
            rlist, _, _, = select.select([mfd], [], [])

            if rlist:
                data = os.read(mfd, MAX_READ)
                print("try_pty() read: %s" % data.strip())
                if not data:
                    print("try_pty() got EOF -- exiting")
                    break
                if alert in data:
                    print("try_pty() found alert: %s" % alert)
                    toggle_rpi()
            elif p.poll() is not None:
                print("try_pty() had subprocess end -- exiting")
                break

    except KeyboardInterrupt:   print(" Caught Ctrl+C -- killing subprocess...")
    except Exception as ex:     print ex
    finally:
        print("Cleaning up...")
        os.close(sfd)
        os.close(mfd)
        p.kill()
        print("Goodbye.")

#===============================================================================

try_subprocess(_cmd_lst, _alert, _rpi_lst)
#try_pexpect(_cmd_lst, _alert, _rpi_lst)
#try_pty(_cmd_lst, _alert, _rpi_lst)

rawDescription: Execute script on Snort alert

description: Execute script on Snort alert


code:
for i in test:
    print divmod(b, test[i])

rawDescription: Python: list index out of range error; certain list elements work


code:
#!/usr/bin/env python

d = {}

# open your file here, i'm using /etc/hosts as an example...
f = open("/etc/hosts","r")
for line in f:
    line = line.rstrip()
    l = len(line)+1
    for i in xrange(1,l):
        d[line[:i]] = True
f.close()

while True:
    w = raw_input('> ')
    if not w:
        break

    if w in d:
        print "match found", w

rawDescription: Fast method to search for a string in a big text file with python


code:
import numpy
a = numpy.zeros((4, 4, 3), 'int')

for n in range(4):
    for m in range(4):
        a[n, m, :] = n + m
        if (n + m) == 4:
            print n, m

c = (4, 4, 4)
indices = numpy.where(numpy.all(a == c, axis=-1))
print indices
print zip(indices[0], indices[1])

rawDescription: Finding the (x,y) indexes of specific (R,G,B) color values from images stored in NumPy ndarrays


code:
for element in selector.xpath('//element'):
    for i in range(1, 2+1):
        print element.xpath('name(@*[%d])' % i).extract_first()

rawDescription: Getting name of attributes with Scrapy XPATH


code:
class Revision(models.Model):
# Model junk...

    def send_email(self, sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision().send_email, sender=Revision)

rawDescription: How can I have a Django signal call a model method?

description: Have a Django signal call a model method


code:
# Using MAKE FEATURE LAYER with SQL QUERY
with arcpy.da.SearchCursor(fc_In1, fc_In1_FieldName) as cursor:
    for row in cursor:
        var_PolygonName = row[0]
        print "Buffering polygon " + var_PolygonName
        arcpy.MakeFeatureLayer_management(fc_In1, "Poly_lyr", '"{}" = \'{}\''.format(fc_In1_FieldName, var_PolygonName))
        arcpy.Buffer_analysis("Poly_lyr", var_PolygonName+'_Buff.shp', var_Buffer, "FULL", "ROUND", "ALL", "")

rawDescription: ArcGIS script is buffering all features each time through the loop -- why?


code: print "You have {0} coins left.".format(num_coins)

rawDescription: Python 2.6, global variable wont change, calling functions in another function


code:
import sys

branches = [i for i in xrange(10000000)]

total = 0
min = sys.maxint
max = -1
branches_count = branches.__len__()

for current in branches:
    if min > current:
        min = current
    if max < current:
        max = current

    total += current

avg = float(total) / branches_count

print total, avg, min, max

rawDescription: Unifying similar functions in one

description: Unifying similar functions in one


code:
with open("foo.txt" , 'r') as foo:
    listme = foo.read()

string =  listme.strip().split(',')
new_string = ''

for words in string:
    if words not in new_string:
        if '@hotmail.com' in words:
            new_string+=words

print new_string

rawDescription: Extracting particular type of strings from a txt file


code:
for i1, inner_l in enumerate(l):
    for i2, item in enumerate(inner_l):
        print i1, i2, item, l[i1][i2]

rawDescription: Nested lists python

description: Nested lists python


code:
def goo(a,b):
    print "{}({}, {})".format(goo.__name__, a, b)

goo(1,2)
def hoo(*args):
    print "{}({})".format(hoo.__name__, ', '.join((str(arg) for arg in args)))

hoo(2,3,4,5)

rawDescription: Automatically remove named parameters from a Python function call


code:
data = [['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks'],
['AAA', 'Some Mobile', '111', '12, 23, 34, 46','Some remarks'],
['AAA', 'Some city A', '111', '55, 56, 57, 51', 'Some more remarks'],
['BBB', 'Some city B', '222', '234, 345, 456', 'Other remarks']]

op=[data[0]]
for i in data[1:]:
   for j in i.pop(3).split(','):
      op.append([k+j.strip() if i.index(k)==2 else k for k in i])

for i in op:
   print i

rawDescription: How to match phone number prefixes?

description: Match phone number prefixes


code:
import types
import sys

src = """
def hello(who):
    print 'hello', who
"""

def module_from_text(modulename, src):
    if modulename in sys.modules:
        module = sys.modules[modulename]
    else:
        module = sys.modules[modulename] = types.ModuleType(modulename)
    exec compile(src, '<no-file>', 'exec') in module.__dict__
    return module

module_from_text('flup', src)
import flup
flup.hello('world')

rawDescription: Import module stored in a cStringIO data structure vs. physical disk file


code:
def main():
    array = ["susan", "mary", "david", "tom", "richard", "mark"]

    places_moved = int(input("How many places would you like to move?\n" + \
                             "(positive is to the right) "))

    new_list = []
    for seat in range(6):
        new_list.append(array[(seat + 6 - places_moved) % 6])

    print new_list

    return 0

if __name__ == '__main__':
    main()

rawDescription: Python 3.3.4. How to swap values within an array


code:
for (key, value) in set(aa.items()) & set(bb.items()):
    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:
f = open("test.txt", "r")

for line in f:
    line = line.strip().split(' ')
    try:
        print float(line[0]) +  25
    except:
        pass

f.close()

rawDescription: How can I read a text file and replace numbers?

description: Read a text file and replace numbers


code:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
# DON'T forget to import Label!!!
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder

Builder.load_string("""
<CustomPopup@Popup>:
    title: "My Custom Poput"

<Diction@GridLayout>:
    cols: 2
    search: id_search
    Label:
        text: "Search"
    TextInput:
        id: id_search
    Button:
        text: "click"
        on_press: root.show_popup(self)
""")

class CustomPopup(Popup):
    pass

class Diction(GridLayout):
    def show_popup(self, b):
        if self.search.text=='A':
            print 'True'
        else:
            print 'False'
    # You can send any content to the popup with the content attribute
    CustomPopup(content=Label(text=self.search.text)).open()

class MyApp(App):
    def build(self):
        return Diction()

rawDescription: Python kivy text input

description: Python kivy text input


rawDescription: How to calculate the difference of the values in a dictionary in python?

description: Calculate the difference of the values in a dictionary in python


code:
import json

class S:

    def serialize(self, obj):
        return (json.dumps(self.to_dictionary(obj)))

    def to_dictionary(self, obj):
        obj_dict = dict()
        for key in obj.__dict__.keys():
            try:
                if(type(obj.__dict__[key]) is list):
                    content = []
                    for count in range(0, obj.__dict__[key].__len__()):
                        content.append(self.to_dictionary(obj.__dict__[key][count]))
                    obj_dict[key] = content
                # elif(isinstance(obj.__dict__[key], DomainObject)):
                #    obj_dict[key] = self.to_dictionary(obj.__dict__[key])
                else:
                    obj_dict[key] = obj.__dict__[key]
            except KeyError:
                pass
            except AttributeError:
                pass
        return (obj_dict)

class A:
    def __init__(self):
        self.a = "1234"
        self.b = (1, 2, 3, 4)
        self.c = [1, 2, 3, 4]
        self.d = { 1: 2, 3: 4}

print S().serialize(A())

rawDescription: Assign array to key dictionary in python


code:
import pandas as pd
import io

df = pd.DataFrame({'school_id': ['A', 'A', 'A', 'B', 'B'],
                   'date_posted': pd.date_range('2014-01-01', '2014-01-05'),
                   'date_completed': pd.date_range('2014-01-01', '2014-01-05')})

posted = df.set_index('date_posted').groupby('school_id').cumcount()
comp = df.set_index('date_completed').groupby('school_id').cumcount()

df['posted'] = posted.values
df['comp'] = comp.values

print df

rawDescription: Iterating through pandas groupby groups

description: Iterating through pandas groupby groups


code:
import itertools
import timeit
import re
import collections

with open("/usr/share/dict/british-english") as f:
    A = [s.strip() for s in itertools.islice(f, 28000, 30000)]
    B = [s.strip() for s in itertools.islice(f, 23000, 25000)]

def f():
    result = []
    for a in A:
        for b in B:
            if a in b:
                result.append((a, b))
    return result

def g():
    return [(a, b) for a in A for b in B if a in b]

def h():
    res = [re.compile(re.escape(a)) for a in A]
    return [(a, b) for a in res for b in B if a.search(b)]

def ninjagecko():
    d = collections.defaultdict(set)
    for k, b in enumerate(B):
        for i, j in itertools.combinations(range(len(b) + 1), 2):
            d[b[i:j]].add(k)
    return [(a, B[k]) for a in A for k in d[a]]

print "Nested loop", timeit.repeat(f, number=1)
print "List comprehension", timeit.repeat(g, number=1)
print "Regular expressions", timeit.repeat(h, number=1)
print "ninjagecko", timeit.repeat(ninjagecko, number=1)

rawDescription: Efficient strings containing each other

description: Efficient strings containing each other


code:
def guess(smudged_numbers):
    from itertools import product
    num_smudges = len(smudged_numbers)
    for raw in product(smudged_numbers, repeat=4):
        if len(set(raw)) == num_smudges:
            yield raw

count = 0
for nums in guess([1, 8]):
    print nums
    count += 1
print "total", count

rawDescription: Using combinatorics in Python to list 4-digit passcodes


code:
#!/usr/bin/env python
#
#
#
# $Header: /opt/cvs/python/packages/share1.5/AutoDockTools/Utilities24/prepare_ligand4.py,v 1.5.4.1 2009/04/15 17:41:57 rhuey Exp $
#
# Modified 2016/02/07
# Hugh Bothwell  http://stackoverflow.com/users/33258
# Added -i commandline option to process multiple files
#
import os

from MolKit import Read
from AutoDockTools.MoleculePreparation import AD4LigandPreparation

# initialize command-line parameters
#-l: ligand
ligand_filename =  None
#-i: file containing ligand-filenames
ligand_listfile =  None
# optional parameters
verbose = None
add_bonds = False
#-A: repairs to make: add bonds and/or hydrogens
repairs = ""
#-C  default: add gasteiger charges
charges_to_add = 'gasteiger'
#-p preserve charges on specific atom types
preserve_charge_types=''
#-U: cleanup by merging nphs_lps, nphs, lps
cleanup  = "nphs_lps"
#-B named rotatable bond type(s) to allow to rotate
#allowed_bonds = ""
allowed_bonds = "backbone"
#-r  root
root = 'auto'
#-o outputfilename
outputfilename = None
#-F check_for_fragments
check_for_fragments = False
#-I bonds_to_inactivate
bonds_to_inactivate = ""
#-Z inactivate_all_torsions
inactivate_all_torsions = False
#-g attach_nonbonded_fragments
attach_nonbonded_fragments = False
#-m mode
mode = 'automatic'
#-d dictionary
dict = None

def process_file(fname):
    mols = Read(fname)
    if verbose: print 'read ', fname
    mol = mols[0]
    if len(mols)>1:
        if verbose:
            print "more than one molecule in file"
        #use the one molecule with the most atoms
        ctr = 1
        for m in mols[1:]:
            ctr += 1
            if len(m.allAtoms)>len(mol.allAtoms):
                mol = m
                if verbose:
                    print "mol set to ", ctr, "th molecule with", len(mol.allAtoms), "atoms"
    coord_dict = {}
    for a in mol.allAtoms: coord_dict[a] = a.coords

    mol.buildBondsByDistance()
    if charges_to_add is not None:
        preserved = {}
        preserved_types = preserve_charge_types.split(',')
        for t in preserved_types:
            if not len(t): continue
            ats = mol.allAtoms.get(lambda x: x.autodock_element==t)
            for a in ats:
                if a.chargeSet is not None:
                    preserved[a] = [a.chargeSet, a.charge]

    if verbose:
        print "setting up LPO with mode=", mode,
        print "and outputfilename= ", outputfilename
        print "and check_for_fragments=", check_for_fragments
        print "and bonds_to_inactivate=", bonds_to_inactivate
    LPO = AD4LigandPreparation(mol, mode, repairs, charges_to_add,
                            cleanup, allowed_bonds, root,
                            outputfilename=outputfilename,
                            dict=dict, check_for_fragments=check_for_fragments,
                            bonds_to_inactivate=bonds_to_inactivate,
                            inactivate_all_torsions=inactivate_all_torsions,
                            attach_nonbonded_fragments=attach_nonbonded_fragments)
    #do something about atoms with too many bonds (?)
    #FIX THIS: could be peptide ligand (???)
    #          ??use isPeptide to decide chargeSet??
    if charges_to_add is not None:
        #restore any previous charges
        for atom, chargeList in preserved.items():
            atom._charges[chargeList[0]] = chargeList[1]
            atom.chargeSet = chargeList[0]
    if verbose: print "returning ", mol.returnCode
    bad_list = []
    for a in mol.allAtoms:
        if a.coords!=coord_dict[a]: bad_list.append(a)
    if len(bad_list):
        print len(bad_list), ' atom coordinates changed!'
        for a in bad_list:
            print a.name, ":", coord_dict[a], ' -> ', a.coords
    else:
        if verbose: print "No change in atomic coordinates"
    if mol.returnCode != 0:
        sys.stderr.write(mol.returnMsg + "\n")
    # sys.exit(mol.returnCode)

if __name__ == '__main__':
    import sys
    import getopt

    def usage():
        "Print helpful, accurate usage statement to stdout."
        print "Usage: prepare_ligand4.py -l filename"
        print
        print "    Description of command..."
        print "         -l     ligand_filename       (.pdb or .mol2 or .pdbq format)"
        print "         -i     list_of_filenames.txt (.pdb or .mol2 or .pdbq format)"
        print "    Optional parameters:"
        print "        [-v]    verbose output"
        print "        [-o pdbqt_filename] (default output filename is ligand_filename_stem + .pdbqt)"
        print "        [-d]    dictionary to write types list and number of active torsions "

        print "        [-A]    type(s) of repairs to make:\n\t\t bonds_hydrogens, bonds, hydrogens (default is to do no repairs)"
        print "        [-C]    do not add charges (default is to add gasteiger charges)"
        print "        [-p]    preserve input charges on atom type, eg -p Zn"
        print "               (default is not to preserve charges on any specific atom type)"
        print "        [-U]    cleanup type:\n\t\t nphs_lps, nphs, lps, '' (default is 'nphs_lps') "
        print "        [-B]    type(s) of bonds to allow to rotate "
        print "               (default sets 'backbone' rotatable and 'amide' + 'guanidinium' non-rotatable)"
        print "        [-R]    index for root"
        print "        [-F]    check for and use largest non-bonded fragment (default is not to do this)"
        print "        [-M]    interactive (default is automatic output)"
        print "        [-I]    string of bonds to inactivate composed of "
        print "                   of zero-based atom indices eg 5_13_2_10  "
        print "                   will inactivate atoms[5]-atoms[13] bond "
        print "                               and atoms[2]-atoms[10] bond "
        print "                      (default is not to inactivate any specific bonds)"
        print "        [-Z]    inactivate all active torsions     "
        print "                      (default is leave all rotatable active except amide and guanidinium)"
        print "        [-g]    attach all nonbonded fragments "
        print "                      (default is not to do this)"

    # process command arguments
    try:
        opt_list, args = getopt.getopt(sys.argv[1:], 'l:i:vo:d:A:Cp:U:B:R:MFI:Zgh')
    except getopt.GetoptError, msg:
        print 'prepare_ligand4.py: %s' %msg
        usage()
        sys.exit(2)

    #'l:vo:d:A:CKU:B:R:MFI:Zg'
    for o, a in opt_list:
        #print "o=", o, " a=", a
        if o in ('-l', '--l'):
            ligand_filename = a
            if verbose: print 'set ligand_filename to ', a
        if o in ('-i', '--i'):
            ligand_listfile = a
            if verbose: print 'set ligand_listfile to ', a
        if o in ('-v', '--v'):
            verbose = True
            if verbose: print 'set verbose to ', True
        if o in ('-o', '--o'):
            outputfilename = a
            if verbose: print 'set outputfilename to ', a
        if o in ('-d', '--d'):
            dict = a
            if verbose: print 'set dict to ', a
        if o in ('-A', '--A'):
            repairs = a
            if verbose: print 'set repairs to ', a
        if o in ('-C', '--C'):
            charges_to_add = None
            if verbose: print 'do not add charges'
        if o in ('-p', '--p'):
            preserve_charge_types+=a
            preserve_charge_types+=','
            if verbose: print 'preserve initial charges on ', preserve_charge_types
        if o in ('-U', '--U'):
            cleanup  = a
            if verbose: print 'set cleanup to merge ', a
        if o in ('-B', '--B'):
            allowed_bonds = a
            if verbose: print 'allow ', a, 'bonds set to rotate'
        if o in ('-R', '--R'):
            root = a
            if verbose: print 'set root to ', root
        if o in ('-F', '--F'):
            check_for_fragments = True
            if verbose: print 'set check_for_fragments to True'
        if o in ('-M', '--M'):
            mode = a
            if verbose: print 'set mode to ', a
        if o in ('-I', '--I'):
            bonds_to_inactivate = a
            if verbose: print 'set bonds_to_inactivate to ', a
        if o in ('-Z', '--Z'):
            inactivate_all_torsions = True
            if verbose: print 'set inactivate_all_torsions to ', inactivate_all_torsions
        if o in ('-g', '--g'):
            attach_nonbonded_fragments = True
            if verbose: print 'set attach_nonbonded_fragments to ', attach_nonbonded_fragments
        if o in ('-h', '--'):
            usage()
            sys.exit()

    if ligand_filename:
        process_file(ligand_filename)
    elif ligand_listfile:
        # Python 2.5 does not support `with`
        # with open(ligand_listfile) as inf:
        #     for fname in inf:
        #         process_file(fname.rstrip())
        inf = open(ligand_listfile)
        for fname in inf:
            process_file(fname.rstrip())
        inf.close()
    else:
        print 'prepare_ligand4: either -l (ligand filename) or -i (ligand listfile) must be specified.'
        usage()
        sys.exit()

# To execute this command type:
# prepare_ligand4.py -l pdb_file -v

rawDescription: How to speed up a .py script refering to millions of files?

description: Speed up a .py script refering to millions of files


code:
import re

aString = "foo1(bar1(foo2bar2)) foo3(bar3)"
aList = aString.split()
newList = []
for part in aList:
    prog = re.compile(r'([a-zA-Z0-9]*)(.*)')
    newList.append(prog.split(part)[1:3])

print newList

rawDescription: Finding a string of varying length within parentheses that contain parentheses


code:
dicts = myDict.items()
dicts.sort(key=lambda (k,d): (d['site_location'], d['serial_num'],))

rawDescription: Sort Dictionary of Dictionaries on multiple child dictionary values


code:
with open("file.csv") as fin:
    headerline = fin.next()
    total = 0
    for row in csv.reader(fin):
        total += int(row[1])
    print total

rawDescription: Sum a csv column in python

description: Sum a csv column in python


code:
aN = raw_input("Enter a number with decimals")
bN = raw_input("Enter a binary number, 2 to 4 digits in length")

if (bN[-1] == "1"):
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now", aN[:aN.find(".")+3]

elif (bN[-1] == "0"):
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now", aN[:aN.find(".")+2]

rawDescription: String Slicing with If?

description: String Slicing with If


code:
raw = food(7, True)
print raw[0]
print raw[1]

rawDescription: using multiple parameters in python function

description: Using multiple parameters in python function


code:
def localhost():
    env.hosts = [ '127.0.0.1']

def remoteRun():
    print "ENV %s" %(env.hosts)
    out = run('uname -r')
    print "Output %s"%(out)

rawDescription: No hosts found: Fabric

description: No hosts found: Fabric


code:
import numpy as np
import scipy.optimize as opt

def main():
    nobservations = 4
    a, b, c = 3.0, 2.0, 1.0
    f, x, y, z = generate_data(nobservations, a, b, c)

    print 'Linear results (should be {}, {}, {}):'.format(a, b, c)
    print linear_invert(f, x, y, z)

    print 'Non-linear results (should be {}, {}, {}):'.format(a, b, c)
    print nonlinear_invert(f, x, y, z)

def generate_data(nobservations, a, b, c, noise_level=0.01):
    x, y, z = np.random.random((3, nobservations))
    noise = noise_level * np.random.normal(0, noise_level, nobservations)
    f = func(x, y, z, a, b, c) + noise
    return f, x, y, z

def func(x, y, z, a, b, c):
    f = (a**2
         + x * b**2
         + y * a * b * np.cos(c)
         + z * a * b * np.sin(c))
    return f

def linear_invert(f, x, y, z):
    G = np.vstack([np.ones_like(x), x, y, z]).T
    m, _, _, _ = np.linalg.lstsq(G, f)

    d, e, f, g = m
    a = np.sqrt(d)
    b = np.sqrt(e)
    c = np.arctan2(g, f) # Note that `c` will be in radians, not degrees
    return a, b, c

def nonlinear_invert(f, x, y, z):
    # "curve_fit" expects the function to take a slightly different form...
    def wrapped_func(observation_points, a, b, c):
        x, y, z = observation_points
        return func(x, y, z, a, b, c)

    xdata = np.vstack([x, y, z])
    model, cov = opt.curve_fit(wrapped_func, xdata, f)
    return model

main()

rawDescription: Solving non-linear equations in python

description: Solving non-linear equations in python


code:
for x in NL:
    if x < sc.NLayer:
        print 'true'
    else:
        print 'false'

rawDescription: else block never executes

description: Else block never executes


code:
import time
from collections import Counter

def fmt(data):
    return "".join("{:02x}".format(ord(c)) for c in data).upper()

def pct(v, size):
    return float(v) / size * 100

startTime = time.time()

with open("myfile.bin","rb") as f:
    counts = Counter(f.read())
    size = f.tell()

threshold = size * 0.005
err = {fmt(k):pct(v, size) for k,v in counts.items() if v > threshold }

if not err:
    print "OK"
else:
    for k,v in sorted(err.items()):
        print "    0x{} is {:.2f}%".format(k, v)

print "Done in %.2f seconds."%(time.time() - startTime)

rawDescription: Python 2.7 : How to reduce time to get data percentage of a file?


code:
import timeit
print 'From list'
print timeit.timeit('np.array(x)', setup=setup_range, number=1000)
print 'From matlab'
print timeit.timeit('np.array(x)', setup=setup_matlab, number=1000)
print 'From matlab_data'
print timeit.timeit('np.array(x._data)', setup=setup_matlab, number=1000)

rawDescription: How to efficiently convert Matlab engine arrays to numpy ndarray?

description: Efficiently convert Matlab engine arrays to numpy ndarray


code:
values=d.values() # List of values
random.shuffle(values) # Shuffles in-place
for value in values:
    print value

rawDescription: How to iterate through dict in random order in Python?

description: Iterate through dict in random order in Python