rawDescription: Exclude matching the period character in a [\W\d]+ regex


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


code:
import sys

def JListPrcessor(javalist):
    print "pass the processor"
    jlist = [float(num) for num in javalist.split(",")]
    print jlist

try:
    inputList = sys.argv[1]
except:
    print "No input str given"
    sys.exit(1)

JListPrcessor(inputList)

rawDescription: How to process a Java List (sent from Java programs) in a Python function?

description: Process a Java List (sent from Java programs) in a Python function


code:
import datetime
iYear = datetime.datetime.strptime('05/01/2014', '%m/%d/%Y').year
print type(iYear), iYear

rawDescription: month/day/year conversion to just year with python or VB Script

description: Month/day/year conversion to just year with python or VB Script


code:
import numpy as np
data = np.zeros((6,18))
data[2:5, 9:14] = 1
print data

rawDescription: How to create a 2D "rect" array (square block of 1's, else 0's) in numpy?

description: Create a 2D "rect" array (square block of 1's, else 0's) in numpy


code:
with open("hello.txt") as f:
    for line in f:
        print line,

rawDescription: Conditionally writing to different output files


code:
default = None
for university, sport, url_name in cat_list:
    if university == x:
        if sport == y:
            print url_name
            break # stop looping
        elif sport == 'default':
            default = url_name
else: # triggered only if the loop reached the end without hitting a break statement
    print default

rawDescription: python for loop logic to check variables against

description: Python for loop logic to check variables against


code:
for code, (name, size, calories, cents) in foods.iteritems():
    print "%s \t %s \t %s \t %s \t %s " % (code, name, size, calories, cents)

rawDescription: python multiplication {how to do it with a dic ?}

description: Python multiplication {how to do it with a dic ?}


rawDescription: Selenium returns a page source in which all tags names have "a0:" prefixed to them


code:
from traits.api import HasTraits, List, on_trait_change
from traitsui.api import Item, View, CheckListEditor

class Foo(HasTraits):
    stuff = List()

    traits_view = View(Item('stuff', style='custom', label='stuff',
        editor=CheckListEditor(values=['.state.First', '.state.Last', '.state.Any'],
                               format_func=lambda x: x)))

    @on_trait_change('stuff[]')
    def show_stuff(self):
        print "stuff =", self.stuff

if __name__ == "__main__":
    f = Foo()
    f.configure_traits()

rawDescription: TraitsUI CheckListEditor changing the case of values


rawDescription: Python parse filenames by custom field pattern


code:
hex_list = "7F 9D AA 3E F7 0E 9C 75 7C 37"

# Convert your string list to a list: e.g. ['7F', '9D', 'AA'...
hex_list = hex_list.split()

for hex in hex_list:
    print int(hex, 16) #hex is base 16

rawDescription: process a string of ascii Hex values into a numerical hex value

description: Process a string of ascii Hex values into a numerical hex value


code:
import zlib

crc_table = None

def make_crc_table():
  global crc_table
  crc_table = [0] * 256
  for n in xrange(256):
    c = n
    for k in xrange(8):
        if c & 1:
            c = 0xedb88320L ^ (c >> 1)
        else:
            c = c >> 1
    crc_table[n] = c

make_crc_table()

"""
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
should be initialized to all 1's, and the transmitted value
is the 1's complement of the final running CRC (see the
crc() routine below)). */
"""
def update_crc(crc, buf):
  c = crc
  for byte in buf:
    c = crc_table[int((c ^ ord(byte)) & 0xff)] ^ (c >> 8)
  return c

# /* Return the CRC of the bytes buf[0..len-1]. */
def crc(buf):
  return update_crc(0xffffffffL, buf) ^ 0xffffffffL

if __name__ == "__main__":
    tests = [
        "",
        "\x00",
        "\x01",
        "Twas brillig and the slithy toves did gyre and gimble in the wabe",
        ]

    for test in tests:
        model = crc(test) & 0xFFFFFFFFL
        zlib_result = zlib.crc32(test) & 0xFFFFFFFFL
        print (model, zlib_result, model == zlib_result)

rawDescription: How to set parameters in Python zlib module

description: Set parameters in Python zlib module


rawDescription: check if element in values also in key in dict

description: Check if element in values also in key in dict


code:
from oscar.core.loading import get_class
Partner = get_class('partner.models', 'Partner')
print Partner
StockAlert = get_class('partner.models', 'StockAlert')
print StockAlert

rawDescription: How to alter Fields in Django and Oscar

description: Alter Fields in Django and Oscar


code:
#! /usr/bin/env python
import time
import vardata

while True:
    time.sleep(1)
    reload(vardata)
    print vardata.pyserv

rawDescription: how can you Import an os variable into PYTHON and have it update?


rawDescription: Data loading using arrays in Python

description: Data loading using arrays in Python


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

def testAddOne(data):
    return data + 1

def testAddN(dataN):
    data,N=dataN
    for x in xrange(N):
        data = testAddOne(data)
    return data

if __name__ == '__main__':
    num_matrices = 4
    matrix_size = 1000
    num_adds_per_matrix = 2500

    num_processes = 4

    inputs = [(random((matrix_size,matrix_size)), num_adds_per_matrix)
              for i in range(num_matrices)]
    #print inputs # test using, e.g., matrix_size = 2

    start = time.clock()

    if num_processes > 1:
        proc_pool = Pool(processes=num_processes)
        outputs = proc_pool.map(testAddN, inputs)
    else:
        outputs = map(testAddN, inputs)

    stop = time.clock()
    #print outputs # test using, e.g., matrix_size = 2
    print "Elapsed", stop - start

rawDescription: Python array sum vs MATLAB

description: Python array sum vs MATLAB


code:
import numpy as np
data = np.loadtxt('C:\Python27\Fake1.txt')
print data.mean(0)

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


code:
#!/usr/bin/python

def main():
    for line in sys.stdin:
        print line

if __name__ == "__main__":
    main()

rawDescription: Map Reduce multiple outputs in python boto


code:
class Vertex(object):

    def __init__(self, resource):
        if not hasattr(self.__class__, 'resource'):
            self.__class__.resource = resource

    @classmethod
    def foo(cls):
        print cls.resource

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

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


code:
import sys
from time import sleep

for c in "Hello":
    print c,        # note the comma
    sleep(0.1)
print               # the final newline

rawDescription: Python code, Time delay on console text

description: Python code, Time delay on console text


code:
from inspect import getmembers

for name, attr in getmembers(foo):
    print '{}: {}'.format(name, type(attr))

rawDescription: Finding out type within dir()

description: Finding out type within dir()


code:
g = Graph.Full(3)
vseq = g.vs
print type(vseq)
# <class 'igraph.VertexSeq'>

rawDescription: Getting vertex list from python-igraph

description: Getting vertex list from python-igraph


code:
businesses = response.get('businesses')

if not businesses:
    print u'No businesses for {0} in {1} found.'.format(term, location)
    return

business_id = businesses[0]['id']

rawDescription: Grabbing all results for a certain query Yelp API using Python


code:
from bs4 import BeautifulSoup

data = """

paragraph1

paragraph2

paragraph3

"""

soup = BeautifulSoup(data, 'html.parser')
for p in soup.find_all('p'):
    if 'style' in p.attrs:
        del p.attrs['style']

print soup.prettify()

rawDescription: Removing style from specific tags BeautifulSoup/Python


code: print '%d  %.2f' % (count, conv)

rawDescription: How do I add space between two variables after a print in Python

description: Add space between two variables after a print in Python


code:
class RedisTools:
    '''
    A set of utility tools for interacting with a redis cache
    '''

    def __init__(self):
        self._queues = ["default", "high", "low", "failed"]
        self.get_redis_connection()

    def get_redis_connection(self):
        redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
        self.redis = redis.from_url(redis_url)

    def get_queues(self):
        return self._queues

    def get_queue_count(self, queue):
        return Queue(name=queue, connection=self.redis).count

    def msg_print_log(self, msg):
        print msg
        logger.info(msg)

    def get_key_count(self):
        return len(self.redis.keys('rq:job:*'))

    def get_queue_job_counts(self):
        queues = self.get_queues()
        queue_counts = [self.get_queue_count(queue) for queue in queues]
        return zip(queues, queue_counts)

    def has_orphanes(self):
        job_count = sum([count[1] for count in self.get_queue_job_counts()])
        return job_count < self.get_key_count()

    def print_failed_jobs(self):
        q = django_rq.get_failed_queue()
        while True:
            job = q.dequeue()
            if not job:
                break
            print job

    def print_job_counts(self):
        for queue in self.get_queue_job_counts():
            print "{:.<20}{}".format(queue[0], queue[1])
        print "{:.<20}{}".format('Redis Keys:', self.get_key_count())

    def delete_failed_jobs(self):
        q = django_rq.get_failed_queue()
        count = 0
        while True:
            job = q.dequeue()
            if not job:
                self.msg_print_log("{} Jobs deleted.".format(count))
                break
            job.delete()
            count += 1

    def delete_orphaned_jobs(self):
        if not self.has_orphanes():
            return self.msg_print_log("No orphan jobs to delete.")

        for i, key in enumerate(self.redis.keys('rq:job:*')):
            job_number = key.split("rq:job:")[1]
            job = Job.fetch(job_number, connection=self.redis)
            job.delete()
            self.msg_print_log("[{}] Deleted job {}.".format(i, job_number))

rawDescription: Redis still fills up when results_ttl=0, Why?


code:
data = range(8112) # replace with input from file

col_size = 52
col_count = 6
batch_size = (col_size*col_count)
# split input into batches of 6 columns of 52 entries each
for batch in range(0,len(data),batch_size):
    # rearrange line data into 6 column format
    cols = zip(*[data[x:x+col_size] for x in range(batch,batch+batch_size,col_size)])
    for c in cols:
        print c

rawDescription: Reading a large amount of numbers in python


code:
def Prop(cls):
    f = cls.__dict__
    return property(f['get'], f['set'])

#### test ####

class Example(object):

    @Prop
    class myattr():

        def get(self):
            print 'getting', self._value
            return self._value

        def set(self, value):
            print 'setting', value
            self._value = value

e = Example()
e.myattr = 'somevalue'
print e.myattr

rawDescription: C# style properties in python

description: C# style properties in python


code:
def int_str_to_chr_str(int_str):
    res = []
    for pair in zip(int_str[::2], int_str[1::2]):
        number = int("".join(pair))

        if number <= 26:
            number += 96

        res.append(chr(number))

    return "".join(res)

print int_str_to_chr_str("1920012532082114071825463219200125320615151209190846")

rawDescription: convert string of numbers to letters python

description: Convert string of numbers to letters python


code:
import json

some_dict = {'id': 0123, 'text': 'A dummy text'}

dummy_json = json.dumps(some_dict)

rawDescription: Trying to use a JSON object as a argument (Python)


code:
p = re.compile(r'\b(?!(?:18|19|20)\d{2}\b)\d+\b')
test_str = "Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, 4 5 6 created in 2008"
print p.sub("", test_str)

rawDescription: Delete numbers not dates in Python (regex)


code:
import pandas as pd

def compare_extract(extract_name, reference='gold_std.csv'):

    gold = pd.read_csv(reference)
    ext = pd.read_csv(extract_name)

    gc = set(gold.columns)
    header = ext.columns
    extc = set(header)

    if gc != extc:
        missing = ", ".join(list(gc - extc))
        print "Extract has the following missing columns: {}".format(missing)
    else:
        print "Extract has the same column as standard. Checking for abberant rows..."
        gold_list = gold.values.tolist()
        ext_list = ext.values.tolist()
        # Somewhat non-pandaic approach because possible no same IDs so we're relying
        # on set operations instead. A bit hackish, actually.
        diff = list(set(map(tuple, gold_list)) - set(map(tuple, ext_list)))
        df = pd.DataFrame(diff, columns=header)
        print "The following rows are not in the extract: "
        print df

rawDescription: Comparing gold standard csv file and extracted values csv files in Python


code:
import numpy as np
print np.unique([50,50,30,10,50,50,30], return_inverse=True)[1]

rawDescription: Unique zero-based id for values in pandas


code:
from timeit import timeit

n = 2
N = "10**7"
mod = "9999999"
gc = "False"
print "N: %s; mod: %s; garbage collection: %s" % (N, mod, gc)

setup ="""
N = %s
mod = %s
d = {x:1 for x in xrange(N)}
if %s:
    gc.enable()""" % (N, mod, gc)

t = timeit(
'd = {k:v for k, v in d.iteritems() if not k % mod}',
setup=setup,
number=n)
print "%s times method 1 (dict comp): %.3f s" % (n, t)

t = timeit(
'''
for k, v in d.items():
    if k % mod:
        del d[k]
''',
setup=setup,
number=n)
print "%s times method 2 (key deletion within for loop over d.items()): %.3f s" % (n, t)

t = timeit('''
removekeys = [k for k, v in d.iteritems() if k % mod]
for k in removekeys:
    del d[k]
''',
setup=setup,
number=n)
print "%s times method 3 (key deletion after list comp): %.3f s" %(n, t)

rawDescription: Fastest way to remove a lot of keys from a dictionary


code:
import types

methods = ['foo', 'bar']

def metaMethod(self, m):
    def f(self):
        print "method", m
    return f

class Egg:
    def __init__(self):
        for m in methods:
            self.__dict__[m] = types.MethodType(metaMethod(self, m), self)

e = Egg()
e.foo()
e.bar()

rawDescription: How do I find the name of metamethod?

description: Find the name of metamethod


code: print nt.Point.__doc__

rawDescription: Adding docstrings to namedtuples?

description: Adding docstrings to namedtuples


code:
import numpy as np
import timeit

use_numba = False
try:
    import numba as nb
except ImportError, e:
    use_numba = False

def _run_it(a, N):
    s = 0.0
    for k in xrange(N):
        s += k / np.sin(a)

    return s

# define wrapper call function with optimizer
if use_numba:
    print 'Using numba'
    run_it = nb.jit()(_run_it)
else:
    print 'Falling back to python'
    run_it = _run_it

if __name__ == '__main__':
    print timeit.repeat('run_it(50.0, 100000)', setup='from __main__ import run_it', repeat=3, number=100)

rawDescription: Python Numba/jit conditional and recursive (stack) use


code:
for i, e in reversed(list(enumerate(a))):
    print i, e

rawDescription: Traverse a list in reverse order in Python


rawDescription: Django filter queryset __in for *every* item in list


code:
var = int(raw_input("Enter 1,2,3 or 4 for add,subtract,multiplication,division      respectively: "))
if var == 1:
    print "You chose to add.Lets add!! :)"

def main ():
    total = 0.0
    while True:
        number = float(raw_input('enter a number: '))
        total+=number
        if number == 0:
            break
    print 'the total is', total

main()

rawDescription: Python-Multiple user inputs

description: Python-Multiple user inputs


code:
from ctypes import *
libc = cdll.LoadLibrary("libc.dylib")
libc.time.__doc__ = 'time -- get time of day'
print libc.time.__doc__

rawDescription: Adding docstrings to python functions pulled from dll using ctypes


code:
dist[dist > 3.0] = 0
print dist

rawDescription: numpy 2D matrix- How to improve performance in this case?

description: Numpy 2D matrix- How to improve performance in this case


code:
import cgi
import urlparse
url = "http://example.com/?a=text&q2=text2&q3=text3&q2=text4"
qs = cgi.parse_qs(urlparse.urlparse(url)[4])
del(qs['q2'])
print qs

rawDescription: How do I remove a query string from URL using Python

description: Remove a query string from URL using Python


code:
l= sorted(cList)
for i, c1 in enumerate(l[:-1]):
    for c2 in l[i + 1:]:
        o= overlap(c1,c2)
        if o is None:
            break
        print "left: %s, right: %s, length: %s" % o

rawDescription: Python: optimizing pairwise overlaps between intervals


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

with open("somefile") as f:
  for line in f:
    if not marking:
      index = line.find(startmarker)
      if index != -1:
        marking = True
        result.append(line[index:])
    else:
      index = line.rfind(endmarker)
      if index != -1:
        marking = False
        result.append(line[:index + len(endmarker)])
      else:
        result.append(line)

print "".join(result)

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

description: Grab a chunk of data from a file


code:
def p(x):
 print "Hello World"
say=lambda x:p

rawDescription: Interesting "Hello World" Interview

description: Interesting "Hello World" Interview


code:
exit = False
while exit != True:
  print "Hello!"
  exit = True

rawDescription: How to change the value of a local variable (python) each time the code loops?

description: Change the value of a local variable (python) each time the code loops


code:
try:
    commmanHandler(botuser, botmsg)
except Exception, e:
    print e
    irc.send('PRIVMSG' + channel + ' :' + e)

rawDescription: Identify sending user, Python IRC

description: Identify sending user, Python IRC


code:
class MyClass:
    class_attribute = None

    @classmethod
    def set(cls, value):
        cls.class_attribute = value

    def do_computation(self):
        self.set(10)

a = MyClass()
b = MyClass()
print a.class_attribute
print b.class_attribute

a.do_computation()
print a.class_attribute
print b.class_attribute

rawDescription: Super and child class sharing a variable


code:
import requests

url = 'http://bioportal.bioontology.org/annotator'
params = {
    'text': 'Sample text',  # this is the contents of the text area
    'longest_only': 'false',
    'raw': 'true'
}

# start a web-scraping session (mostly, for maintaining cookies)
session = requests.Session()
session.get(url)

# submit the "form"
response = session.post(url, data=params)
data = response.json()

# get the annotations
for annotation in data['annotations']:
    print annotation['annotatedClass']['prefLabel']

rawDescription: Enter data in textarea using mechanize Python


code:
import re

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

a = '111 = dw11qdwq\n111 = aaaaa'

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

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

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


code:
from pyparsing import *
# elements of the makefile are delimited by line, so we must
# define skippable whitespace to include just spaces and tabs
ParserElement.setDefaultWhitespaceChars(' \t')
NL = LineEnd().suppress()

EQ,COLON,LBRACK,RBRACK = map(Suppress, "=:[]")
identifier = Word(alphas+'_', alphanums)

symbol_assignment = Group(identifier("name") + EQ + empty +
                          restOfLine("value"))("symbol_assignment")
symbol_ref = Word("$",alphanums+"_.")

def only_column_one(s,l,t):
    if col(l,s) != 1:
        raise ParseException(s,l,"not in column 1")
# task identifiers have to start in column 1
task_identifier = identifier.copy().setParseAction(only_column_one)

task_description = "des:" + empty + restOfLine("des")
task_path = "path:" + empty + restOfLine("path")
task_para_body = delimitedList(symbol_ref)
task_para = "para:" + LBRACK + task_para_body("para") + RBRACK
task_para.ignore(NL)
task_definition = Group(task_identifier("target") + COLON +
        Optional(delimitedList(identifier))("deps") + NL +
        (
        Optional(task_description + NL) &
        Optional(task_path + NL) &
        Optional(task_para + NL)
        )
    )("task_definition")

makefile_parser = ZeroOrMore(
    symbol_assignment |
    task_definition |
    NL
    )

if __name__ == "__main__":
    test = """\
samtools=/path/to/samtools
picard=/path/to/picard

task1:
    des: description
    path: /path/to/task1
    para: [$global.samtools,
           $args.input,
           $path
          ]

task2: task1
"""

# dump out what we parsed, including results names
for element in makefile_parser.parseString(test):
    print element.getName()
    print element.dump()
    print

rawDescription: Any python module for customized BNF parser?


code:
from numpy import array
l = [[2, 3], [4, 2], [3, 2]]
a = array(l)
print a.shape

rawDescription: Find the dimensions of a multidimensional Python array


code:
import json
respStr = """
{
   "movies_search":{
      "@xmlns":"http://www.ourmoviest.com/mv",
      "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
      "@xsi:schemaLocation":"http://www.ourmoviest.com/mv mv.xsd ",
      "error":{
         "errorMessage":"We cannot proceed search",
         "statusCode":"00004"
      }
   }
}
"""
respObj = json.loads(respStr)
print respObj['movies_search']['error']['errorMessage']

rawDescription: How to read field from JSON encoding of XML message

description: Read field from JSON encoding of XML message


code:
import string
import struct
xlate = string.maketrans('abcdef', 'ABCDEF')

print [hex(ord(b)).translate(xlate) for b in struct.pack('>L',777007543)]
# ['0x2E', '0x50', '0x31', '0xB7']

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

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


code:
hay = "The fox jumped up the tree."
needle = "umpe"

print hay.replace(needle, "<strong>%s<strong>" % needle)

rawDescription: How do I do this regex in Python?

description: Do this regex in Python


code:
import contextlib
import dis

@contextlib.contextmanager
def silly():
    yield

def foo():
    print "Hello"
    with silly():
        print "Inside"
    print "Goodbye"

rawDescription: Is it possible to access the context object (code block) inside the __exit__() method of a context manager?


code:
missing=[]
numbers.insert(0, 0) # add the minimum value on begining of the list
numbers.append(41)  # add the maximum value at the end of the list
for rank in xrange(0, len(numbers)-1):
   if numbers[rank+1] - numbers[rank] > 2:
      missing.append("%s-%s"%(numbers[rank] +1 , numbers[rank+1] - 1))
   elif numbers[rank+1] - numbers[rank] == 2:
      missing.append(str(numbers[rank]+1))

print missing

rawDescription: Determine sequential numbers in a list (Python)


code:
s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')
s=set(s)
qs="""R De Me Li
Fi Colla
bow ODE
Car
Do Car
ral lar
R De Me 4
R Do Carp""".split('\n')

for q in sorted(qs):
    print "results for '{}':".format(q)
    pattern = r'.*' + re.sub(r'\W', '.*', q.strip()) + '.*'
    matches=[item for item in s if re.match(pattern, item, re.I)]
    for e in difflib.get_close_matches(q,s,3,0.33):
        if e not in matches:
            matches.append(e)

    for i,e in enumerate(matches,1):
        print "\t{}. {}".format(i,e)
    else:
        if len(matches)==0:
            print "\tNo matches"

rawDescription: regular expression: search multiple words with multiple parts

description: Regular expression: search multiple words with multiple parts


code:
def Name_Organizer(FASTA,output):

    import os
    import re

    in_file=open(FASTA,'r')
    dir,file=os.path.split(FASTA)
    temp = os.path.join(dir,output)
    out_file=open(temp,'w')

    data=''
    name_list=[]
    seq_list=[]

    for line in in_file:

        line=line.strip()
        for i in line:
            if i=='>':
                name_list.append(line)
                if data:
                    seq_list.append(data)
                    data=''
                break
            else:
                line=line.upper()
        if all([k==k.upper() for k in line]):
            data=data+line

    print seq_list

rawDescription: Add multiple sequences from a FASTA file to a list in python


code:
def caller(self):
    def _callback(response):
        print "Doing something cool"
    getUrlRes("google.com", params, "GET", callback=_callback)

@tornado.web.asynchronous
def getUrlRes(self, url, params, req_type, callback=None):
    def urlCallBack(response):
        print "Got response {}".format(response)
        if callback:
            _callback(response)  # call the passed callback here
        self.finish()
    self.client.fetch("{}?{}".format(url, urllib2.urlencode(params)),
                      urlCallBack, method=req_type)

rawDescription: Tornado asynchttpclient results in None

description: Tornado asynchttpclient results in None


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

    tot_sum = 2

    for num in xrange( 3, 2000000, 2 ):
            isPrime = True
            for j in xrange(3, int( num ** 0.5 ) + 1, 2 ):
                if( num % j == 0 ):
                    isPrime = False
                    break

            if isPrime:
                tot_sum = tot_sum + num

    print "total sum is %d" % tot_sum

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

prime()

rawDescription: how to increase the efficiency of this program?

description: Increase the efficiency of this program


code:
user_id,user_name,code
0001,user_a,e-5
0001,user_a,s-N
0001,user_a,n
0001,user_a,t
0001,user_a,a
0002,user_b,e-N
0002,user_b,t-5
0002,user_b,s
0002,user_b,n
0002,user_b,a

rawDescription: Python iterate over csv row by row while keeping track of content of next row


code:
user = Auth.objects.using('app_data').get(user=user)
print user.database

rawDescription: How to change database in django in runtime

description: Change database in django in runtime


code:
import pandas as pd
s = pd.Index(A)
pos = s.get_indexer(B)
print A[pos]
print B

rawDescription: Different results using numpy.in1d() with an array and with its single elements


code:
class Foo(object):

    def bar(self):
        print self
        print self.bar
        print self.bar.im_self # prints the same as self

rawDescription: Python Suite, Package, Module, TestCase and TestSuite differences


code:
#!/usr/bin/env python

import re

prefix = re.compile('^(.*?)"')
quotes = re.compile('".*?(.*).*?"')
pattern = re.compile('[A-Z]{1}[0-9]{7}')
suffix = re.compile('".*"(.*?)$')

for (i,line) in enumerate(open('myfile.txt')):
    pre = prefix.search(line).group(1)
    data = quotes.search(line).group(1)
    suf = suffix.search(line).group(1)
    for match in re.finditer(pattern,data):
        print pre+match.group(0)+suf

rawDescription: Create new line based on each regex match in python


code:
from Tkinter import *

def count_monkeypatch(self, index1, index2, *args):
    args = [self._w, "count"] + ["-" + arg for arg in args] + [index1, index2]

    result = self.tk.call(*args)
    return result

Text.count = count_monkeypatch

root = Tk()
text = Text(root, width = 12, height = 5, wrap = WORD)
text.insert(END, 'This is an example text.')
text.pack()

def test(event):
    print "displaylines:", text.count("1.0", "end", "displaylines")
    print "lines:", text.count("1.0", "end", "lines")

text.bind('<Map>', test)

root.mainloop()

rawDescription: Tkinter - Counting lines in a Text widget with word wrapping


code:
import subprocess

proc = subprocess.Popen(['/bin/bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = proc.communicate('ls -lash')

print stdout

rawDescription: Interacting with bash from python

description: Interacting with bash from python


code:
import random

iterations = 100000

doors = ["goat"] * 2 + ["car"]
change_wins = 0
change_loses = 0

for i in xrange(iterations):
    random.shuffle(doors)
    # you pick door n:
    n = random.randrange(3)
    # monty picks door k, k!=n and doors[k]!="car"
    sequence = range(3)
    random.shuffle(sequence)
    for k in sequence:
        if k == n or doors[k] == "car":
            continue
    # now if you change, you lose iff doors[n]=="car"
    if doors[n] == "car":
        change_loses += 1
    else:
        change_wins += 1

print "Changing has %s wins and %s losses" % (change_wins, change_loses)
perc = (100.0 * change_wins) / (change_wins + change_loses)
print "IOW, by changing you win %.1f%% of the time" % perc

rawDescription: Is this a good or bad 'simulation' for Monty Hall? How come?


code:
from django.db import connection

@shared_task
def my_task(track):
    print 'in the task', track.id, track.points.all().count()

def some_method():
    t = Track()
    t.save()
    t = fill_with_points(t)  # creating points, attaching them to a Track
    t.save()
    print 'before the task', track.id, track.points.all().count()
    connection.on_commit(lambda: my_task.delay(t))

rawDescription: Django related objects are missing from celery task (race condition?)


code:
import sys
import os.path
import urllib2

url = 'http://www.iana.org/domains/example/'
saved_time_file = 'last time check.txt'

request = urllib2.Request(url)
if os.path.exists(saved_time_file):
    """ If we've previously stored a time, get it and add it to the request"""
    last_time = open(saved_time_file, 'r').read()
    request.add_header("If-Modified-Since", last_time)

try:
    response = urllib2.urlopen(request) # Make the request
except urllib2.HTTPError, err:
    if err.code == 304:
        print "Nothing new."
        sys.exit(0)
    raise   # some other http error (like 404 not found etc); re-raise it.

last_modified = response.info().get('Last-Modified', False)
if last_modified:
    open(saved_time_file, 'w').write(last_modified)
else:
    print("Server did not provide a last-modified property. Continuing...")
    """
    Alternately, you could save the current time in HTTP-date format here:
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
    This might work for some servers that don't provide Last-Modified, but do
    respect If-Modified-Since.
    """

"""
You should get here if the server won't confirm the content is old.
Hopefully that means it's new.
HTML should be in response.read().
"""

rawDescription: How to check if the value on a website has changed

description: Check if the value on a website has changed


code:
from bs4 import BeautifulSoup
import requests

BASE_URL = 'http://www.scoresway.com/?sport={sport}&page=match&id={match_id}'
ADVANCED_URL = "http://www.scoresway.com/b/block.teama_people_match_stat.advanced?has_wrapper=true&match_id={match_id}&sport={sport}&localization_id=www"

def get_match(sport, match_id):
    # basic
    r = requests.get(BASE_URL.format(sport=sport, match_id=match_id))
    soup = BeautifulSoup(r.content)

    for x in soup.findAll('table')[1].findAll('tr')[-1].findAll('td'):
        print ''.join(x.findAll(text=True))

    # advanced
    response = requests.get(ADVANCED_URL.format(sport=sport, match_id=match_id))
    soup = BeautifulSoup(response.text)
    print [td.text.strip() for td in soup('td', class_='name')]

get_match('basketball', 45926)

rawDescription: Python Beautiful Soup scape page containing Java Script


code:
import bs4
import imdb
import requests

src = requests.get('http://www.imdb.com/title/tt2024544/').text
bs = bs4.BeautifulSoup(src)
recs = [rec['data-tconst'][2:] for rec in bs.findAll('div', 'rec_item')]
print recs

rawDescription: IMDB suggestions

description: IMDB suggestions


code:
from collections import defaultdict
import string
text ='intalks is an organization comprised of passionate students'.lower().translate(None,string.punctuation+' ')
c = defaultdict(int)
c.update({letter:0 for letter in string.lowercase[:26]})
for letter in text:
        c[letter] += 1

def getbin(foo):
    if len(set(a))==1 and a[0] == '1':
        return '0'*(len(a)+1)
    else:
        return (("{0:0"+str(len(a))+"b}").format(int (a,2)+1)).zfill(len(a))

a = '0'

for letter,freq in sorted(c.iteritems(),key = lambda (l,f) : f, reverse = True):
    print letter,a,freq
    a = getbin(a)

rawDescription: Encoding of alphabets with binary digits in descending order


code:
with open('somefile.csv') as fin:
    csvin = csv.DictReader(fin)
    for row in csvin:
        print 'Person {name} had a balance of {Open Balance}'.format(**row)

rawDescription: Reformatting a CSV using Python and bucketing the data


code:
from lxml import etree

doc = etree.HTML("""

    Citibank Credit Card - Save over 5% on fuel | Citibank.co.in

Get the IndianOil Citibank Card. Apply Now!

   Get 10X Rewards On Shopping -
   Save Over 5% On Fuel

   www.citibank.co.in/CreditCards

""")

etree.strip_tags(doc,'a','b')

for element in doc.xpath('//*[following-sibling::*[name()="br"]]'):
  print repr(element.tail.strip())

rawDescription: How to split the tags from html tree

description: Split the tags from html tree


code:
a = np.char.strip(a, '"').astype(float)
print a

rawDescription: How to setup string to float converters for numpy genfromtxt

description: Setup string to float converters for numpy genfromtxt


code:
tags = soup('a', limit=3)[2]

print tags.get('href', None)

rawDescription: Not able to retrieve the HREF from the tag


code:
class MyClassWithAQueue(object):
    q = multiprocessing.Queue()
    def __init__(self):
        pass

MyClassWithAQueue.q = Queue.Queue()
myclass = MyClassWithAQueue()
print myclass.q

rawDescription: Monkey Patching an attribute within a class?


code:
class DeferredEval(object):
    def __init__(self,func):
        self.func = func

    def __call__(self):
        return self.func()

    def __add__(self,other):
        return self.func() + other

    def __radd__(self,other):
        return other + self.func()

x = None
y = None
r = DeferredEval(lambda:x*y)

try:
    a = 1 + r
except TypeError as err:
    print "Oops, can't calculate r yet -- Reason:",err

x = 1
y = 2
print 1 + r
print r + 1

rawDescription: Passing arithmatic expressions in python without evaluating them


code:
import numpy as np

# Make some data...
n = 10
x = np.arange(n)
y = 3 * x + 5 + np.random.random(n)

# Note that polyfit is an easier way to do this...
# It would just be "model, resid = np.polyfit(x,y,1,full=True)[:2]"
A = np.vstack((x, np.ones(n))).T
model, resid = np.linalg.lstsq(A, y)[:2]

r2 = 1 - resid / (y.size * y.var())
print r2

rawDescription: Converting Numpy Lstsq residual value to R^2


code:
def gcdRecur(a, b, _indent=''):
    global level
    print '{}entering gcdRecur({}, {})'.format(_indent, a, b)
    if b == 0:
        print '{}returning a as b is 0: {}'.format(_indent, a)
        return a
    else:
        recursive_result = gcdRecur(b, a%b, _indent + ' ')
        print '{}Recursive call returned, passing on {}'.format(_indent, recursive_result)
        return recursive_result

gcdRecur(60,100)

rawDescription: python recursion (why won't program exit once if condition is met?)

description: Python recursion (why won't program exit once if condition is met?)


code: print db.ips.distinct("ip")

rawDescription: Pymongo: Select field into list without adding field name to list


code:
import os
print os.path.expandvars("My home directory is $HOME")

rawDescription: Python Environment variable within environment variable


code:
from sqlalchemy.orm.evaluator import EvaluatorCompiler
print EvaluatorCompiler().process(bool_clause)(a_foo)
print EvaluatorCompiler().process(int_clause)(a_foo)

rawDescription: Can sqlalchemy clauses be evaluated without a database?


code:
data={}
exec( compile(config,'config.py','exec'), globals(), data )
print data['animals']

rawDescription: Python text processing and parsing

description: Python text processing and parsing


code:
from bs4 import BeautifulSoup
sample = '''\

English

Open-content textbooks

51 000+ pages

'''
soup = BeautifulSoup(sample)
for item in soup.find_all('div', lang=True):
    if not (item.strong and item.small):
        continue
    language = item.strong.string
    pages = item.small.string
    print '{}: {}'.format(language, pages)

rawDescription: How to get objects from div with BeautifulSoup in Python?

description: Get objects from div with BeautifulSoup in Python


code:
y = df.groupby([pd.Grouper(freq='1s', key='Date'), 'Path']).count()
print(y)
y.columns = ['Count']

print y[y.Count < 3]

rawDescription: Pandas filter counts

description: Pandas filter counts


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

for res in combos(a, b):
    print res

rawDescription: Merging 2 Lists In Multiple Ways - Python


rawDescription: Cannot fetch javascript element

description: Cannot fetch javascript element


code:
from bisect import bisect_left
def solve(matrix, param):
    param.sort()             #Sort the params passed by user
    maxx = param[-1]         #Find max
    for row in matrix:
        # If item in the row is greater than `maxx` then use Nan
        # else use bisect_right to get the next highest item from
        # params in O(log N) time.
        yield [float('nan') if item > maxx else
                                 param[bisect_left(param, item)] for item in row]

grid_z1 = [[1,2,3],
           [4,5,6],
           [7,8,9]]
print list(solve(grid_z1, [2, 7, 4]))

rawDescription: Efficient algorithm for matrix calculation


code:
import numpy as np
from scipy import stats

def lognfit(x, ddof=0):
    x = np.asarray(x)
    logx = np.log(x)
    mu = logx.mean()
    sig = logx.std(ddof=ddof)
    return mu, sig

# A simple data set for easy reproducibility
x = np.array([50., 50, 100, 200, 200, 300, 500])

# Explicit formula
my_mu, my_sig = lognfit(x)

# Fit a normal distribution to log(x)
norm_mu, norm_sig = stats.norm.fit(np.log(x))

# Fit the lognormal distribution
lognorm_sig, _, lognorm_expmu = stats.lognorm.fit(x, floc=0)

print "Explicit formula:   mu = %10.8f, sig = %10.8f" % (my_mu, my_sig)
print "Fit log(x) to norm: mu = %10.8f, sig = %10.8f" % (norm_mu, norm_sig)
print "Fit x to lognorm:   mu = %10.8f, sig = %10.8f" % (np.log(lognorm_expmu), lognorm_sig)

rawDescription: Fitting lognormal distribution using Scipy vs Matlab


code:
nb_tabs = (max_size - len(a))/8 + 1
print "%s%sLOCKED" % (a, "\t" * nb_tabs)

rawDescription: How to align output in consistent format

description: Align output in consistent format


code:
variable = "/users/me/folder/"
the_list = ["/users/me/folder/file1_001_134543_X5_6MGFS.txt", "/users/me/folder/file2_231_234233_Y5_6MGFFAS.txt"]

results_list = list()

for full_path in the_list:
    _, file_name = full_path.split(variable) # This splits "/users/me/folder/file1_001_134543_X5_6MGFS.txt" into "/users/me/folder/" and "file1_001_134543_X5_6MGFS.txt" so we take the last one as 'file_name'
    file_name = file_name.split('_')[0]      # This splits e.g. "file1_001_134543_X5_6MGFS.txt" in ["file1", "001", "134543", "X5", "6MGFS.txt"] so we take only the first one, which is in index 0
    results_list.append(file_name)           # Adding e.g. "file1" to our 'results_list'

print results_list

rawDescription: Edit a list to remove a defined variable and everything after and including a character


code:
prior = []    # list of prior input sets (only those that weren't redundant)

with open("permission.txt") as in_file:
  for in_line in in_file:
    master = set([])
    perm_line = set([int(s) for s in in_line.split('P') if len(s)>0])

    # Check each of the shorter (prior) permission strings
    for short in prior:
      # If the prior string has no superfluous permissions, add it to the master set.
      extra = short - perm_line
      if len(extra) == 0:
        master = master.union(short)

    # Did the combination of prior strings cover all of the input line permissions?
    print in_line,
    if len(perm_line - master) == 0:
      print "\tPermissions included in prior strings"
    else:
      print "\tFound New permission combinations; adding to reference list"
      prior.append(perm_line)

rawDescription: Generating different combinations of string in python


code:
for k,v in status.__dict__.items():  #same thing as `vars(status)`
    print k,v

rawDescription: How to "see" the structure of an object in python


code:
count = 0
for pos in motion_plan(increaser(0, -1), alternator(0, 1)): #location 4 motion plan
    print "%d %r" % (count, pos)
    count += 1

rawDescription: Robot Motion in Python

description: Robot Motion in Python


code:
import numpy as np
summed = 0

dim1 = 2  # this is 81 in your case
dim2 = 4  # this is 990000 in your case
array = np.random.random(size=(dim1, dim2, 3))

Nt = dim2
i = Nt // 2

for k in xrange(dim1):
    summed = 0
    for j in xrange(dim2-i):
        vec1 = array[k][j]
        vec2 = array[k][j+i]
        summed += np.dot(vec1,vec2)
    print summed

print '='*70

for k in xrange(dim1):
    summed = np.einsum('ij,ij', array[k][:Nt//2], array[k][Nt//2:])
    print summed

rawDescription: Take dot product of first and middle entry, second and middle+1 entries until middle-1 and last entry python/numpy


code:
import functions

def main():
    a, b, c = (1, 2, 3)
    functions.function1(a)
    functions.function2(b)
    functions.function3(c)
    d = functions.function4()
    print d

if __name__ == "__main__":
    main()

rawDescription: splitting one program into several smaller and their binding in python?

description: Splitting one program into several smaller and their binding in python


rawDescription: Regex - Expression for matching international characters doesn't work when I add ^ $


code:
import collections

def canonical(cards):
    """
    Rules for a canonical hand:
    1. The cards are in sorted order

    2. The i-th suit must have at least many cards as all later suits.  If a
       suit isn't present, it counts as having 0 cards.

    3. If two suits have the same number of cards, the ranks in the first suit
       must be lower or equal lexicographically (e.g., [1, 3] <= [2, 4]).

    4. Must be a valid hand (no duplicate cards)
    """

    if sorted(cards) != cards:
        return False
    by_suits = collections.defaultdict(list)
    for suit in range(0, 52, 13):
        by_suits[suit] = [card%13 for card in cards if suit <= card < suit+13]
        if len(set(by_suits[suit])) != len(by_suits[suit]):
            return False
    for suit in range(13, 52, 13):
        suit1 = by_suits[suit-13]
        suit2 = by_suits[suit]
        if not suit2: continue
        if len(suit1) < len(suit2):
            return False
        if len(suit1) == len(suit2) and suit1 > suit2:
            return False
    return True

def deal_cards(permutations, n, cards):
    if len(cards) == n:
        permutations.append(list(cards))
        return
    start = 0
    if cards:
        start = max(cards) + 1
    for card in range(start, 52):
        cards.append(card)
        if canonical(cards):
            deal_cards(permutations, n, cards)
        del cards[-1]

def generate_permutations(n):
    permutations = []
    deal_cards(permutations, n, [])
    return permutations

for cards in generate_permutations(5):
    print cards

rawDescription: Generating all 5 card poker hands

description: Generating all 5 card poker hands


code:
import collections

node = collections.namedtuple('node', 'a b c d')

# Tom = ...

v = node(Tom, 100, 3.14, {'x':1, 'y':2, 'z':3})
print node  # node(a=…, b=100, c=3.14, d={'x':1, 'y':2, 'z':3})
print node.c  # 3.14
print node[2]  # 3.14 (works too, but is less meaningful and robust than something like node.last_name)

rawDescription: pack multiple variables of different datatypes in list/array python

description: Pack multiple variables of different datatypes in list/array python


code:
# POS that starts with JJ are adjectives, sum the counts up
print sum(pos_freq[i] for i in pos_freq if i.startswith('JJ'))

rawDescription: How to find adjective frequency from a specifique categories in brown corpus in NLTK

description: Find adjective frequency from a specifique categories in brown corpus in NLTK


code:
a = np.matrix(255, dtype= np.uint8)
b = np.matrix(255, dtype= np.uint8)

p = NumpyOverflowPreventer(a)
p+=1

p = NumpyOverflowPreventer(b)
p.__iadd__(1)

print a,b
# [[255]] [[255]]

rawDescription: substituting __iadd__ doesn't work as expected for += operator

description: Substituting __iadd__ doesn't work as expected for += operator


code:
from operator import itemgetter
print sorted(res.items(),key=itemgetter(1))

rawDescription: Positional Rankings and Dealing with Ties in Python


code:
import os, json
import pandas as pd

# this finds our json files
path_to_json = 'json/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

# here I define my pandas Dataframe with the columns I want to get from the json
jsons_data = pd.DataFrame(columns=['country', 'city', 'long/lat'])

# we need both the json and an index number so use enumerate()
for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)

        # here you need to know the layout of your json and each json has to have
        # the same structure (obviously not the structure I have here)
        country = json_text['features'][0]['properties']['country']
        city = json_text['features'][0]['properties']['name']
        lonlat = json_text['features'][0]['geometry']['coordinates']
        # here I push a list of data into a pandas DataFrame at row given by 'index'
        jsons_data.loc[index] = [country, city, lonlat]

# now that we have the pertinent json data in our DataFrame let's look at it
print jsons_data

rawDescription: Python: Read several json files from a folder


code:
import re
string = [
    'adverb with text verb text from here on',
    'adverb with text noun text from here on'
]
print [re.findall(r'adverb(.*?)(?:verb|noun)', s) for s in string]

rawDescription: Get pattern text that is within search string


code:
from PyQt4 import QtCore, QtGui
import sys
import Queue as queue

class ResultObj(QtCore.QObject):
    def __init__(self, val):
        self.val = val

class SimpleThread(QtCore.QThread):
    finished = QtCore.pyqtSignal(object)

    def __init__(self, queue, callback, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.queue = queue
        self.finished.connect(callback)

    def run(self):
        while True:
            arg = self.queue.get()
            if arg is None: # None means exit
                print("Shutting down")
                return
            self.fun(arg)

    def fun(self, arg):
        for i in range(3):
            print 'fun: %s' % i
            self.sleep(1)
        self.finished.emit(ResultObj(arg+1))

class AppWindow(QtGui.QMainWindow):
    def __init__(self):
        super(AppWindow, self).__init__()
        mainWidget = QtGui.QWidget()
        self.setCentralWidget(mainWidget)
        mainLayout = QtGui.QVBoxLayout()
        mainWidget.setLayout(mainLayout)
        button = QtGui.QPushButton('Process')
        button.clicked.connect(self.process)
        mainLayout.addWidget(button)

    def handle_result(self, result):
        val = result.val
        print("got val {}".format(val))
        # You can update the UI from here.

    def process(self):
        MAX_CORES=2
        self.queue = queue.Queue()
        self.threads = []
        for i in range(MAX_CORES):
            thread = SimpleThread(self.queue, self.handle_result)
            self.threads.append(thread)
            thread.start()

        for arg in [1,2,3]:
            self.queue.put(arg)

        for _ in range(MAX_CORES): # Tell the workers to shut down
            self.queue.put(None)

app = QtGui.QApplication([])
window = AppWindow()
window.show()
sys.exit(app.exec_())

rawDescription: How to return value from function running by QThread and Queue

description: Return value from function running by QThread and Queue


code:
strID = raw_input("Username : ")
strPassWd = getpass.getpass("Password : ")

with open('studentsusers.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')

    for row in spamreader:
        strLoginID, strLoginPasswd = row

        while strLoginID != "":
            if strID == strLoginID and strLoginPasswd == strPassWd:
                print strID, "Logged In"
                PupilMenu()
                strContinue = sys.stdin.readline()
                return
            else:
                break   # goes on to the next row in spamreader

#if we get here there is no such login and id
print "Login ID or password incorrect, press enter to continue"
strContinue = sys.stdin

rawDescription: Reading pairs of rows from a file

description: Reading pairs of rows from a file


code:
objs = StoreVideoEventSummary.objects.filter(Timestamp__range=(start_time, end_time),
                                                   Customer__id=customer_id,
                                                   Store__StoreName=store)\
    .order_by("Timestamp")

def date_hour(timestamp):
   return datetime.datetime.fromtimestamp(timestamp).strftime("%x %H")

groups = itertools.groupby(objs, lambda x:date_hour(x.Timestamp))
#since groups is an iterator and not a list you have not yet traversed the list
for group,matches in groups: #now you are traversing the list ...
    print group,"TTL:",sum(1 for _ in matches)

rawDescription: Django group by hour

description: Django group by hour


code:
for row in soup('table')[6].findAll('tr'):
    tds = row('td')
    if len(tds) < 2:
        continue
    print tds[0].string, '\n'.join(filter(unicode.strip, tds[1].strings))

rawDescription: Python: html table content

description: Python: html table content


code:
def get_value():
    while True:
        z = input("Insert value: ") # DON'T DO THIS, INPUT() IS BADDDDD IN PYTHON2
        # instead do:
        ## try: z = int(raw_input("Insert value: "))
        ## except ValueError:
        ##     print("Please provide an integer")
        ##     continue
        # this also FORCES z to be an integer, so isinstance(z,int) is ALWAYS TRUE
        is_int = isinstance(z,int)
        is_pos = is_int and z>0 # this will keep you from throwing an exception if
                                # you can't compare z>0 because z isn't int
        if is_int and is_pos:
            print z**2
        elif is_int:
            print "Please provide an integer"
            continue
        else:
            print "Integer must be positive"
            continue
        # nothing can ever get to that last else block you had.

rawDescription: Python: How do I assign 2 values I return from a function with 1 input as values outside the function?


code:
sort_of_dict = [('ALice', 1), ('in', 2), ('Wonderland', 3)]

for key, value in sort_of_dict:
  print key

rawDescription: Enumerate Keys in a dictionary?

description: Enumerate Keys in a dictionary


code:
def partition(lst, item):
    if item in lst:
        n = lst.index(item)
        return lst[:n], [item], lst[n+1:]
    else:
        return lst, [], []

print partition(range(10), 7)

rawDescription: More compact way to skip initial list items? (or partition list based on an item)


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print soup.get_text()

rawDescription: Converting html to text with Python

description: Converting html to text with Python


code:
from fastavro import load

buf.seek(0)
print load(buf, schema) # {'age': 23, 'name': 'Ann'}

rawDescription: Brutally slow Apache Avro performance in Python, different results when encoding to messages vs. files


code:
from datetime import timedelta, date

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2013, 1, 1)
end_date = date(2015, 6, 2)
for single_date in daterange(start_date, end_date):
    print single_date.strftime("%Y-%m-%d")

rawDescription: Iterating through a range of dates in Python


code:
def message_to_bits(msg):
    result = 0
    for c in msg:
        result = (result << 8) | ord(c)
    return result

def get_bit(num, position):
    return num & (1 << position)

def set_bit_on(num, position):
    return num | (1 << position)

def set_bit_off(num, position):
    return num & ~(1 << position)

def set_bit(num, value, position):
    if value:
        return num | (1 << position)
    else:
        return num & ~(1 << position)

def encode(nested_list_thing, key):
    key_bits = message_to_bits(key)
    key_length = 8 * len(key)
    print ('key: {:0%db}' % key_length).format(key_bits)

    # Mask keeps track of which bit in
    # key_bits we are using to set the value of
    mask = 1 << (key_length - 1)

    result = []
    for list_ in nested_list_thing:
        encoded_list = []

        for tuple_ in list_:
            encoded_tuple = []

            for num in tuple_:
                # Encode the number
                set_to_bit = key_bits & mask
                encoded_num = set_bit(num, set_to_bit, 0)
                encoded_tuple.append(encoded_num)
                # Move to next position in key_bits
                mask = mask >> 1
            encoded_list.append(tuple(encoded_tuple))
        result.append(encoded_list)
    return result

image = [[(15, 103, 225), (0, 3, 19)],
        [(22, 200, 1), (8, 8, 8)],
        [(0, 0, 0), (5, 123, 19)]]
key = 'hello'
print encode(image, key)

rawDescription: how to change values in a nested list depending on elements of two other different lists and function is difficult to read

description: Change values in a nested list depending on elements of two other different lists and function is difficult to read


code:
import glob

import numpy as np

flistC11 = glob.glob('ES-s*')
for fname in flistC11:
    strains, stresses = np.loadtxt(fname, unpack=True)
    strain = np.mean(strains)
    strain_percent = strain/10
    print fname, strain_percent
    es_avg = np.mean(stresses)
    es_std = np.std(stresses)
    print fname, es_avg, es_std
    np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent, es_avg, es_std])

rawDescription: How to make file name a variable using np.savetxt in python?

description: Make file name a variable using np.savetxt in python


code: print remainder(15,3)

rawDescription: how to compute a%b recursively?

description: Compute a%b recursively


rawDescription: How to improve the speed of my selection process, python

description: Improve the speed of my selection process, python


rawDescription: Get pairs of positive values efficiently with numpy


code:
from itertools import cycle
for i in cycle([2, 4]):
    print i

rawDescription: Oscillating Values in Python

description: Oscillating Values in Python


rawDescription: python string formatting fixed width

description: Python string formatting fixed width


code:
# requires numpy and PyMix (matplotlib is just for making a histogram)
import random
import numpy as np
from matplotlib import pyplot as plt
import mixture

random.seed(010713)  # to make it reproducible

# create a mixture of normals:
#  1000 from N(0, 1)
#  2000 from N(6, 2)
mix = np.concatenate([np.random.normal(0, 1, [1000]),
                      np.random.normal(6, 2, [2000])])

# histogram:
plt.hist(mix, bins=20)
plt.savefig("mixture.pdf")

rawDescription: Separate mixture of gaussians in Python

description: Separate mixture of gaussians in Python


code:
p = multiprocessing.Pool()
rs = p.map_async(do_work, xrange(num_tasks))
p.close() # No more work
while (True):
  if (rs.ready()): break
  remaining = rs._number_left
  print "Waiting for", remaining, "tasks to complete..."
  time.sleep(0.5)

rawDescription: Show the progress of a Python multiprocessing pool map call?


rawDescription: Why `float` function is slower than multiplying by 1.0?


code:
class A:
    @classmethod
    def foo(kls, param):
        print 'A.foo called, param is ' + param

def foo_wrapper(wrapped_func):
    wrapped_func = wrapped_func.__func__
    def _w(*args, **kwargs):
        print '<foo_wrap>'
        wrapped_func(*args, **kwargs)
        print '</foo_wrap>'
    return classmethod(_w)

A.foo = foo_wrapper(A.foo)
A.foo('bar')

rawDescription: How to wrap (monkey patch) @classmethod

description: Wrap (monkey patch) @classmethod


code:
import sys

argument = sys.argv[1]
attr_map = dict(item.strip().split('=', 1) for item in argument.split(','))
print attr_map['sender-ip']

rawDescription: How to read arguments of Python script into dictionary

description: Read arguments of Python script into dictionary


code:
from lxml import etree

NS_MAP = {
    "p" : "http://parent-space.com/",
    "c" : "http://child-space.com/"
}
NS_PARENT = "{%s}" % NS_MAP["parent"]
NS_CHILD = "{%s}" % NS_MAP["child"]

parent = etree.Element(NS_PARENT + "parent", nsmap=NS_MAP)
child  = etree.SubElement(parent, NS_CHILD + "child")
child.text = "Some Text"

print etree.tostring(parent, pretty_print=True)

rawDescription: Clashing namespaces with lxml etree

description: Clashing namespaces with lxml etree


code:
class B(object):
   def c(self):
     pass
   print c

print B.c
b = B()
b.c

rawDescription: Python Data Model Document : an unbound user-defined method and a class method object


code:
def commonprefix(l):
    # this unlike the os.path.commonprefix version
    # always returns path prefixes as it compares
    # path component wise
    cp = []
    ls = [p.split('/') for p in l]
    ml = min( len(p) for p in ls )

    for i in range(ml):

        s = set( p[i] for p in ls )
        if len(s) != 1:
            break

        cp.append(s.pop())

    return '/'.join(cp+['{'+' => '.join('/'.join(p[i:]) for p in ls)+'}'])

print commonprefix(['wallpapers/8.jpg','wallpapers/1.jpg'])
print commonprefix(['/home/user/doc.pdf', '/home/user/Desktop/doc.pdf'])

rawDescription: Common path between filenames (like in git)


code:
# Style 1
abc = lambda *args, **kwargs: myFunction(*args, **kwargs)

# Style 2
def abc(*args, **kwargs):
    return myFunction(*args, **kwargs)

def myFunction(x):
    print x

rawDescription: Is it possible to use functions before declaring their body in python?


code:
html = '

&abc is my input text

'
print strip_tags(html)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print soup.text
soup = BeautifulSoup('=&abc= is my input text')
soup.text

rawDescription: python remove html tags including html entities but not normal text with a '&' prefix

description: Python remove html tags including html entities but not normal text with a '&' prefix


code:
for num,name in name_dict.iteritems():
    print str(num) + '\t' + name

rawDescription: Create a dictionary from two lists and then print the data in python


code:
import itertools
import copy
stacks = [["A", "C"], ["B" , "D" ], ["E"]]
children = []
for i in range(len(stacks)):
    for j in range(len(stacks)):
        if i == j:
            continue
        cur_stack = copy.deepcopy(stacks)
        cur_stack[j].append(cur_stack[i].pop())
        print cur_stack
        children.append(cur_stack)

rawDescription: Pythonic way to shuffle stacks

description: Pythonic way to shuffle stacks


code:
with open("data_file") as f:
    for line in f:
        if len(line.strip())==6:
            print line.strip()

rawDescription: How to extract specific lines from a textfile and store in a list(python)

description: Extract specific lines from a textfile and store in a list(python)


code:
#!/usr/bin/python

import os
import time

print "Parent started"
os.system("./child.py &")
print "(child started, sleeping)"

time.sleep(5)

print "Parent finished"

rawDescription: "Fire and forget" a process from a Python script


code:
from itertools import islice

with open("test_file1.txt", "r") as f:
   for line in islice(f, 1, None, 2):
       nums = [int(n) for n in line.split()]
       print 'Sample size: {}  Results: {}'.format(len(nums), sum(nums))

rawDescription: Pythonic method to sum all the odd-numbered lines in a file


code:
somefile.py
import example
from util import PropertyVoodoo

class Math(example.Math):
    __properties__ = ['pi']
    __metaclass__  = PropertyVoodoo

m = Math()
print m.pi
m.pi = 1024
print m.pi
m.pi = 10000
print m.pi

rawDescription: Python Properties & Swig

description: Python Properties & Swig


code:
string1 = "Sony Xperia Z2 m/Smartwatch 2"
print re.split(" m/| \+ ", string1)[0]
# output: Sony Xperia Z2

string2 = "Sony Xperia Z2 + headphones"
print re.split(" m/| \+ ", string2)[0]
# output: Sony Xperia Z2

rawDescription: Python Regex to match and exclude certain content


code:
from bs4 import BeautifulSoup
import requests

url = "http://www.sportstats.com/soccer/italy/serie-a-2013-2014/sampdoria-napoli-zZAT2c14/?block=3"
soup = BeautifulSoup(requests.get(url).content)

id_ = "betType_1_2"
for item in soup.select('div#{id} table.oddsTable tr'.format(id=id_))[1:-1]:
    print [td.text for td in item('td')]

rawDescription: scraping different table with same classes with beautifulsoup,python

description: Scraping different table with same classes with beautifulsoup,python


code:
from pyparsing import *

integer = Regex(r"-?\d+").setName("integer")
integer.setParseAction(lambda tokens: int(tokens[0]))
floatnum = Regex(r"-?\d+\.\d*").setName("float")
floatnum.setParseAction(lambda tokens: float(tokens[0]))
dblQuotedString.setParseAction(removeQuotes)
COMMA = Suppress(',')
validLine = dblQuotedString + COMMA + dblQuotedString + COMMA + \
        integer + COMMA + floatnum + LineEnd()

tests = """\
"good data","good2",100,3.14
"good data" , "good2", 100, 3.14
bad, "good","good2",100,3.14
"bad","good2",100,3
"bad","good2",100.5,3
""".splitlines()

for t in tests:
    print t
    try:
        print validLine.parseString(t).asList()
    except ParseException, pe:
        print pe.markInputline('?')
        print pe.msg
    print

rawDescription: Verify CSV against given format

description: Verify CSV against given format


code:
for link in br.links():
    print link.url, link.text

rawDescription: How can I log into a website using python?

description: Log into a website using python


code:
output
print output[0]

rawDescription: How to make a list from several nested lists

description: Make a list from several nested lists


code:
with open('file2') as fd:
    for line in fd:
        data = line.strip().split()
        print ' '.join(data), data1[data[0]], data1[data[1]]

rawDescription: Python to search values in one text file, compare them with values in another text file, then replace values if there is a match


code:
import csv

with open('csv_data.csv') as fin:
    csvin = csv.DictReader(fin)
    headers = csvin.fieldnames
    by_hotel = {row['Hotel']: row for row in csvin}

print by_hotel['sutton']['excellent']
# 489

rawDescription: parsing this csv file in python(pylab) and converting it into a dictionary

description: Parsing this csv file in python(pylab) and converting it into a dictionary


code:
data = get_ticket_available_product(self, date_select, client_id, quantity)
for d in data:
    print d

rawDescription: how to return the looped values in a method in python/django that no using array

description: Return the looped values in a method in python/django that no using array


code:
def hashes(n):
    for i in range(n):
        print '#' + ' '*i + '#'

rawDescription: python nested loop print spaces between two characters every repeat

description: Python nested loop print spaces between two characters every repeat


code:
import inspect
def lt_dec(func):
    def _lt_lower(*args, **kwargs):
        kwargs = inspect.getcallargs(func, *args, **kwargs)
        if 'lane_type' in kwargs:
            kwargs['lane_type'] = kwargs['lane_type'].lower()
        return func(**kwargs)
    return _lt_lower
@lt_dec
def test1(blah, lane_type):
    print lane_type
test1('foo', 'Solid')
test1('foo', lane_type='Solid')

rawDescription: Decorator for functions with positional arguments, with commonly named argument


code:
import csv
with open('datafile', 'rb') as f:
    reader = csv.DictReader(f)
    dic = {row['name'].lower(): row for row in reader}
print dic

rawDescription: python, how to update the dictionaries by mutating its format?

description: Python, how to update the dictionaries by mutating its format


code:
import xml.etree.ElementTree as etree

data = """
Hello\rworld\rI love you.
"""
data = '<bar>%s</bar>' % data.replace('\r', '<br/>')

bar = etree.fromstring(data)
foo = etree.Element('foo')
foo.append(bar)

print etree.tostring(foo)

rawDescription: Python xml creating SubElements with multiple <br />


code:
import pkgutil

# this is the package we are inspecting -- for example 'email' from stdlib
import email

package = email
prefix = package.__name__ + "."
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix):
    print "Found submodule %s (is a package: %s)" % (modname, ispkg)
    module = __import__(modname, fromlist="dummy")
    print "Imported", module

rawDescription: List all the modules that are part of a python package?


rawDescription: Networkx Random Geometric Graph limit nodes within radius r


code:
def unique_items(L):
    found = set()
    for item in L:
        if item[0] not in found:
            yield item
            found.add(item[0])

print list(unique_items(L))

rawDescription: Removing duplicates from list of lists in Python


code:
data = {"Address": "Home sweet home", "Admin": "James Bond", "City": "London"}

template = """
I live at
Address: {Address}
in City of: {City}
and my admin is: {Admin}
"""
print template.format(**data)

rawDescription: Python 2.7 reading template and returning new file with substitutions


code:
from struct import unpack
from collections import namedtuple
b = "\x00\x00\x00\x05\x00\x00\x00\x2a"

Point = namedtuple('Point', 'x, y')

p = Point._make(unpack("!ii", b))
print p.x
print p.y

rawDescription: Python buffer decode

description: Python buffer decode


code: print '  -- Checking connectivity from {} to {}'.format(env.hosts, dbHostList)

rawDescription: How can I print each list item, in a single line of code?

description: Print each list item, in a single line of code


code:
cases=[
    ["The", "sailor", "is", "sick", "."],
    ["The", "U.S.", "is", "big", "."],
    ["round", "up", "the", "''", "blonde", "bombshell", "\\",
    "a", "all", "-lrb-", "well", "almost", "all", "-rrb-"],
]

import re

for li in cases:
    print '{}\n\t->{}'.format(li, [w for w in li if re.search(r'^[a-zA-Z]', w)])

rawDescription: regex to remove non alpha-words A-Z a-z from a list (exceptions)

description: Regex to remove non alpha-words A-Z a-z from a list (exceptions)


code: print map(int, str(number))

rawDescription: Write the digits of a number into an array


code:
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.http import Request
from PIL import Image
from cStringIO import StringIO
import re

class jellyImagesPipeline(ImagesPipeline):

    CONVERTED_ORIGINAL = re.compile('^full/[0-9,a-f]+.jpg$')

    # name information coming from the spider, in each item
    # add this information to Requests() for individual images downloads
    # through "meta" dict
    def get_media_requests(self, item, info):
        print "get_media_requests"
        return [Request(x, meta={'image_names': item["image_name"]})
                for x in item.get('image_urls', [])]

    # this is where the image is extracted from the HTTP response
    def get_images(self, response, request, info):
        print "get_images"

        for key, image, buf, in super(jellyImagesPipeline, self).get_images(response, request, info):
            if self.CONVERTED_ORIGINAL.match(key):
                key = self.change_filename(key, response)
            yield key, image, buf

    def change_filename(self, key, response):
        return "full/%s.jpg" % response.meta['image_name'][0]

rawDescription: Scrapy: customize Image pipeline with renaming defualt image name


code:
data = [
[],
[3,5],
[1]
]

pairs = []
for row_idx, row in enumerate(data):
    for item in row:
        pairs.append((row_idx, item))

print pairs

rawDescription: quick way to convert a list of lists (ie from scipy.spatial.KDTree.query_ball_tree) to an array of pairs?

description: Quick way to convert a list of lists (ie from scipy.spatial.KDTree.query_ball_tree) to an array of pairs


code:
for d in xrange(1,68):
    exec 'x%s = data[:,%s]' %(d,d-1)

rawDescription: Creating new array in for loop (Python)

description: Creating new array in for loop (Python)


code:
class v(ast.NodeVisitor):
  def generic_visit(self, node):
    print type(node).__name__
    ast.NodeVisitor.generic_visit(self, node)

rawDescription: Simple example of how to use ast.NodeVisitor?


code:
import logging

import Queue
from apscheduler import events
from apscheduler.scheduler import Scheduler
import time

#logging.basicConfig(level=logging.INFO,format='%(asctime)s : %(name)s : %(levelname)s : %(module)s.%(funcName)s(%(lineno)d) : %(thread)d %(threadName)s: %(message)s')

#enable logger to see exceptions caught by apscheduler
logging.basicConfig()

q = Queue.Queue()

sched = Scheduler()

@sched.interval_schedule(seconds=1)
def some_job():
    msg = "Decorated job : %s" % time.time()
    print msg
    logging.info(msg)
    q.put(msg)
    q.put("message 2")

def job_listener(event):
    #print str(event)
    while not q.empty():
        get_ = "msg from job '%s': '%s'" % (event.job, q.get())
        print get_
        logging.info(get_)

sched.add_listener(job_listener,
                   events.EVENT_JOB_EXECUTED |
                   events.EVENT_JOB_MISSED |
                   events.EVENT_JOB_ERROR)

config = {'apscheduler.jobstores.file.class': 'apscheduler.jobstores.shelve_store:ShelveJobStore',
          'apscheduler.jobstores.file.path': '/temp/dbfile'}

sched.configure(config)

sched.start()

q.join()
while True:
    pass

rawDescription: Capture logs in apscheduler

description: Capture logs in apscheduler


code:
class Land:
    def __init__(self):
        print "a new Land"
        self.farms = []

    def addfarm(self):
        self.farms.append(Farm())

class Farm:
    def __init__(self):
        print "a new farm"
        self.animals = {}

    def addanimal(self,name):
        if not name in self.animals:
            self.animals[name] = Animal(name)
            return True
        return False

class Animal:
    def __init__(self, name):
        print "hi, I am %s" % name
        self.name = name

USA = Land()
USA.addfarm()
USA.farms[0].addanimal('George')
USA.farms[0].addanimal('Martin')
USA.addfarm()
USA.farms[1].addanimal('Polly')
USA.farms[1].addanimal('Ralph')

rawDescription: python multiple nested classes

description: Python multiple nested classes


code:
from nltk.stem import WordNetLemmatizer

s = """
 You all are so beautiful soooo beautiful
 Thought that was a really awesome quote
 Beautiful things don't ask for attention
 """

wnl = WordNetLemmatizer()
print " ".join([wnl.lemmatize(i) for i in s.split()]) #You all are so beautiful soooo beautiful Thought that wa a really awesome quote Beautiful thing don't ask for attention

rawDescription: Remove word extension in python

description: Remove word extension in python


code:
def g6(func):
    def g7(n):
        if n > 0:
            return n*func(n-1)
        else:
            return 1

    return g7

print f1(g6)(5) # 120
print f1(g6)(6) # 720

rawDescription: Decoding a Cryptic Python statement

description: Decoding a Cryptic Python statement


code:
with open("data.csv", "r") as f_input:
    lmin_col = []
    lmin_row = []

    for row in csv.reader(f_input):
        row = map(float, row)
        lmin_row.append(min(row))

        if lmin_col:
            lmin_col = map(min, lmin_col, row)
        else:
            lmin_col = row

    print "Min per row:", lmin_row
    print "Min per col:", lmin_col

rawDescription: Finding the minimum of each column of a CSV file using python


code:
def func(**args):
   class BindArgs(object):
       foo = args['foo']
       print 'foo is ', foo
       def __init__(self,args):
          print " hello i am here"
   return BindArgs(args) #return an instance of the class

f = func(foo=3)

rawDescription: Writing a class inside a function in python


code:
inreader = [{'PERSON1': '1', 'PERSON2': '1', 'GENE': 'TEST1', 'PERSON4': '1', 'PERSON3': '1'}, {'PERSON1': '1', 'PERSON2': '0', 'GENE': 'TEST2', 'PERSON4': '0', 'PERSON3': '0'}, {'PERSON1': '1', 'PERSON2': '0', 'GENE': 'TEST3', 'PERSON4': '0', 'PERSON3': '1'}, {'PERSON1': '0', 'PERSON2': '0', 'GENE': 'TEST4', 'PERSON4': '0', 'PERSON3': '0'}, {'PERSON1': '-', 'PERSON2': '0', 'GENE': 'TEST5', 'PERSON4': '-', 'PERSON3': '0'}, {'PERSON1': '2', 'PERSON2': '1', 'GENE': 'TEST9', 'PERSON4': '2', 'PERSON3': '1'}]
result = []
for line in inreader:
    newline = dict([(k,v.replace('1', '0')) for k,v in line.items() ])
    result.append(newline)

print result

rawDescription: Replace key values in a python csv Dictreader object


code:
def process_item(self, item, spider):
    try:
        self.cursor.execute("""INSERT INTO scraped(score, count, url) VALUES (%s, %s, %s)""", (item['reviewScore'], item['reviewCount'], item['reviewURL']))
        self.conn.commit()
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])

        return item

rawDescription: Saving Scrapy data to corresponding URL in MySQL


code:
import json

import scrapy
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

LOGIN = 'email'
PASSWORD = 'password'

class CourseraSpider(scrapy.Spider):
    name = "courseraSpider"
    allowed_domains = ["coursera.org"]

    def start_requests(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('https://www.coursera.org/login')

        form = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@data-js='login-body']//div[@data-js='facebook-button-divider']/following-sibling::form")))
        email = WebDriverWait(form, 10).until(EC.visibility_of_element_located((By.ID, 'user-modal-email')))
        email.send_keys(LOGIN)

        password = form.find_element_by_name('password')
        password.send_keys(PASSWORD)

        login = form.find_element_by_xpath('//button[. = "Log In"]')
        login.click()

        WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[. = 'My Courses']")))

        self.driver.get('https://www.coursera.org/')
        cookies = self.driver.get_cookies()

        self.driver.close()

        courses_url = 'https://www.coursera.org/api/memberships.v1'
        params = {
            'fields': 'courseId,enrolledTimestamp,grade,id,lastAccessedTimestamp,role,v1SessionId,vc,vcMembershipId,courses.v1(display,partnerIds,photoUrl,specializations,startDate,v1Details),partners.v1(homeLink,name),v1Details.v1(sessionIds),v1Sessions.v1(active,dbEndDate,durationString,hasSigTrack,startDay,startMonth,startYear),specializations.v1(logo,name,partnerIds,shortName)&includes=courseId,vcMembershipId,courses.v1(partnerIds,specializations,v1Details),v1Details.v1(sessionIds),specializations.v1(partnerIds)',
            'q': 'me',
            'showHidden': 'false',
            'filter': 'archived'
        }

        params = '&'.join(key + '=' + value for key, value in params.iteritems())
        yield scrapy.Request(courses_url + '?' + params, cookies=cookies)

    def parse(self, response):
        data = json.loads(response.body)

        for course in data['linked']['courses.v1']:
            print course['name']

rawDescription: Submit form that renders dynamically with Scrapy?


code:
for index, keyValues in dict0.items():
    print index
    for key, value in keyValues.items():
        print "\t {}: {}".format(key, value)

rawDescription: Unpack uncertain number of attributes from iterator object in Boto DynamoDB Scan


code:
x = 'http://test.com/lalala-134'
print x.rsplit('-', 1)[0]
'something-with-a-lot-of-dashes'.rsplit('-', 1)[0]

rawDescription: how to get the last part of a string before a certain character?

description: Get the last part of a string before a certain character


code:
x = {"a": "stuff"}
_setattr(x, "f[0].a", "test")
print x
print _getattr(x, "f[0].a")
x = ["one", "two"]
_setattr(x, "3[0].a", "test")
print x
print _getattr(x, "3[0].a")

rawDescription: Python recursive setattr()-like function for working with nested dictionaries


code:
def printer(f):
    print f
    return f

class Foo(object):
    __metaclass__ = decorate_all(printer)
    def bar(self):
        pass
    @dont_decorate
    def baz(self):
        pass
    @classmethod
    def test(self):
        pass
# prints
# <function bar at 0x04EB59B0>

class AnotherName(Foo):
    def blah(self):
        pass
# prints
# <function blah at 0x04EB5930>

Foo.qux = lambda: 1
# prints
# <function <lambda> at 0x04EB57F0>

rawDescription: Automatically decorating every instance method in a class


code:
from IPython.display import clear_output
import sys

while not result.ready():
    clear_output()
    for stdout in result.stdout:
        if stdout:
            lines = stdout.split('\n')
            for line in lines[-4:-1]:
                if line:
                    print line
    sys.stdout.flush()
    time.sleep(30)

rawDescription: IPython.parallel client is hanging while waiting for result of map_async


code:
import sys

for line in sys.stdin:
    print line,
    # do some slicing of string to get dd/mm/yy hh:mm:ss:ms
    # then create a marker file called file_dd/mm/yy hh:mm:ss:ms
    # and so on

rawDescription: Efficient way to find a string based on a list


code:
if row <9 and (col == 'A' or col ==  'B' or col ==  'C' or col == 'D' or col ==  'E' or col == 'F'  or col == 'G' or col == 'H'):

    print "The piece is moved to",col,row,"."
else:

    print "The position is not valid."

rawDescription: What am I doing wrong? and/or in Python

description: What am I doing wrong? and/or in Python


code:
import random
import string
import shelve
import datetime

urls=[''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(50))
          for i in range(40000)]
s=set(urls)
start=datetime.datetime.now()
sh=shelve.open('/tmp/test.shelve')
sh['urls']=urls
end=datetime.datetime.now()
print end-start

rawDescription: Very basic persistence or data storage for a Python script


code:
if any('L_' in key for key in dict) and any('R_' in key for key in dict):
    print 'has both'

rawDescription: Does dictionary contain key containing multiple partial strings?


code:
import numpy as np

im = np.array([[0,0,0,0,0,0],
               [0,0,1,1,1,0],
               [0,1,1,0,1,0],
               [0,0,0,1,1,0],
               [0,0,0,0,0,0]])

mask = im == 0
rows = np.flatnonzero((~mask).sum(axis=1))
cols = np.flatnonzero((~mask).sum(axis=0))

crop = im[rows.min():rows.max()+1, cols.min():cols.max()+1]
print crop

rawDescription: Remove empty 'rows' and 'columns' from 3D numpy pixel array


rawDescription: convert long date to strings

description: Convert long date to strings


code:
def save_step(k):
    global steps
    steps.append(k)

steps = []
scipy.optimize.fmin(rosen, x0, full_output=True, callback=save_step)
print np.array(steps)[:10]

rawDescription: How to return parameters from all iterations of spicy.optimize.minimize

description: Return parameters from all iterations of spicy.optimize.minimize


code:
import os
import subprocess
import time
from daemon import runner
import datetime
import re

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/twitter_counter.pid'
        self.pidfile_timeout = 5
    def run(self):
        rx_megabits_new = 0
        tx_megabits_new = 0
        try:
                while True:
                        output_csv = open("/tmp/netstats_counter.log", 'a')
                        output = subprocess.Popen(['ifconfig', "wlan0"], stdout=subprocess.PIPE).communicate()[0]

                        rx_bytes = re.findall('RX bytes:([0-9]*) ', output)[0]
                        tx_bytes = re.findall('TX bytes:([0-9]*) ', output)[0]
                        rx_megabits = (((int(rx_bytes) * 8) / 1024) / 1024)
                        tx_megabits = (((int(tx_bytes) * 8) / 1024) / 1024)

                        current_rx_usage = (rx_megabits - rx_megabits_new) / 60
                        current_tx_usage = (tx_megabits - tx_megabits_new) / 60
                        current_rx_usage_kb = current_rx_usage * 1024
                        current_tx_usage_kb = current_tx_usage * 1024

                        rx_megabits_new = rx_megabits
                        tx_megabits_new = tx_megabits

                        now = str(datetime.datetime.now())

                        col1 = str(current_tx_usage_kb)
                        col2 = str(current_rx_usage_kb)
                        output_csv.write(col1)
                        output_csv.write(", ")
                        output_csv.write(col2)
                        output_csv.write(", ")
                        output_csv.write(now)
                        output_csv.write("\n")

                        output_csv.close( )

#                       print 'average megabits received', current_rx_usage / 60
#                       print 'average kilobits received', (current_rx_usage * 1024) / 60
#                       print 'average megabits sent', current_tx_usage / 60
#                       print 'average kilobits sent', (current_tx_usage * 1024) / 60

                        time.sleep(60)

        except Exception, e:
            raise

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

rawDescription: Average network statistics in Python

description: Average network statistics in Python


code: print element.attrib

rawDescription: Python scraper (Scraperwiki) only getting half of the table


code:
# determine which centroid belongs to which cluster
# using Euclidean distance
dist1 = np.linalg.norm(centroids[0]-mean1)
dist2 = np.linalg.norm(centroids[1]-mean1)
if dist1 <= dist2:
    FIRST, SECOND = 0, 1
else:
    FIRST, SECOND = 1, 0

# compute accuracy by iterating through all 2N points
# note: first N points are from cloud1, second N points are from cloud2
correct = 0
for i in range(len(clusters)):
    if clusters[i] == FIRST and i < N:
        correct += 1
    elif clusters[i] == SECOND and i >= N:
        correct += 1

# output accuracy
print 'Accuracy: %.2f' % (correct*100./len(clusters))

rawDescription: Parse list of x,y coordinates and detect continious areas


code:
d = {}
for line in open("file"):
    line=line.strip()
    if line.endswith("\\"):
        directory = line.split(":")[-1].strip().replace("\\","")
        d.setdefault(directory,[])
    if line.startswith("->"):
        song=line.split(" ")[-1]
        d[directory].append(song)
print d

rawDescription: Copying files to directories as specified in a file list with python


code:
import xml.etree.ElementTree as ET

data = """<root>
Some bad text that I do not want to keep.
</root>"""

tree = ET.fromstring(data)
a = tree.find('a')
for element in a:
    if element.tag in ('b', 'u'):
        tail = element.tail
        element.clear()
        element.tail = tail

print ET.tostring(tree)

rawDescription: Removing an element, but not the text after it


code:
from scipy import *

a = ones((12,22))
a[5,10] = a[5,12] = 0

a_ = a[1:-1, 1:-1]
aE = a[1:-1, 0:-2]
aW = a[1:-1,   2:]
aN = a[0:-2, 1:-1]
aS = a[  2:, 1:-1]

a4 = dstack([aE,aW,aN,aS])
num_adjacent_zeros = sum(a4 == 0, axis=2)
print num_adjacent_zeros

ys,xs = where(num_adjacent_zeros == 1)
# account for offset of a_
xs += 1
ys += 1

print '\n hits:'
for col,row in zip(xs,ys):
  print (col,row)

rawDescription: Numpy - Getting index positions by testing adjacent indexes


code:
import multiprocessing, subprocess

def get_value(args):
    hostname = args[0]
    return subprocess.check_output(
        'ssh {} hostname'.format(hostname),
        shell=True,
        )

pool = multiprocessing.Pool()
print list( pool.imap_unordered(
    get_value,
    [ ('localhost',) ],
    ) )

rawDescription: Running many python threads and grabbing the returns values of each


code:
import random
lst = [0, 1, 2, 8, 2, 9, 2]
lst.pop(random.choice([i for i, j in enumerate(lst) if j == 2]))
print lst

rawDescription: Removing element in a list

description: Removing element in a list


code:
import collections

p = ['a', 'b', 'c']
h = ['a', 'c', 'd', 'c']

c = collections.Counter(h)
print c

for i in p:
  print i, c[i]

rawDescription: Counting using a pre-defined list

description: Counting using a pre-defined list


code:
import os
try:
    filename = "test123.pdf"
    if not os.path.isfile(filename):
        raise OSError("File not found")
    folder = "someFolder"
    os.mkdir (folder)
    os.rename(filename,"someFolder\\test123.pdf")
except:
    print "File not found"

rawDescription: Skip all lines in try if one line fails

description: Skip all lines in try if one line fails


code:
def f(x):
    return x**3

x[...] = f(x)
print y

rawDescription: repeated numpy subarrays

description: Repeated numpy subarrays


code:
import datetime as DT
import pytz
import calendar
eastern = pytz.timezone("US/Eastern")
utc = pytz.utc
now = DT.datetime(2013, 6, 16, 10, 0, 0)
local_now = eastern.localize(now)
utc_now = local_now.astimezone(utc)
seconds = calendar.timegm(utc_now.utctimetuple())

print(seconds)
# 1371391200

utc_then = utc.localize(DT.datetime.utcfromtimestamp(seconds))
local_then = utc_then.astimezone(eastern)

print utc_now, utc_then
# 2013-06-16 14:00:00+00:00 2013-06-16 14:00:00+00:00
print local_now, local_then
# 2013-06-16 10:00:00-04:00 2013-06-16 10:00:00-04:00

rawDescription: Losing DST information using pytz and UTC conversions


code:
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
    print [item.replace(';', '') for item in row]

rawDescription: python split function

description: Python split function


code:
first = dict(a=1, b=2)
second = dict(b=0, c=3)
for i in range(1):
    diff = set(first) ^ set(second)
    key_of_first = set(first) - set(second)
    key_of_second = set(second) - set(first)
print diff
print key_of_first
print key_of_second

rawDescription: How to find which key is from which dictionary in python

description: Find which key is from which dictionary in python


code:
class Joints(list):
    """A custom list owned by another object. The owner should have
    a before_set_joints() method and an after_set_joints() method.
    """
    def __init__(self, owner, *args):
        self.owner = owner
        list.__init__(self, *args)

    def __setitem__(self, key, value):
        self.owner.before_set_joints()
        list.__setitem__(self, key, value)
        self.owner.after_set_joints()

# Define the onwer's methods cooperatively
class RobotArm(object):

    def __init__(self):
        self.__joints = Joints(self, [0] * 5)  # make self the owner

    def before_set_joints(self):
        print 'setting values'

    def after_set_joints(self):
        self.serial_transmission(self.joints)  # note a slight change here

rawDescription: When a list is used as a property in Python, the setter isn't actually called when you change an individual value in the list


code:
mylist= [{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'}, {'sort_order': 'ascending', 'chart_layout': '1', 'chart_type': 'bar'}]

print [d["sort_order"] for d in mylist if "sort_order" in d][0]

rawDescription: Find a key's value from a list of dictionaries python


code:
import threading
from itertools import repeat
from multiprocessing.pool import ThreadPool   # this is a THREAD POOL! undocumented :)

def execute_me():
    print threading.current_thread().name, 'python is fun'

tp = ThreadPool(processes=4)
print tp.map(lambda x: x(), repeat(execute_me, 4))

rawDescription: Python Threading Over a Counter

description: Python Threading Over a Counter


code:
def ifibonacci():
    a, b = 0, 1
    while b < 3000:
        yield a
        a, b = b, a + b

print list(itertools.islice(ifibonacci(), 6, 6+1))
# prints [8]

rawDescription: How to get nth result from simple function in python?

description: Get nth result from simple function in python


code:
aoaoat = client.factory.create('ArrayOfArrayOfAnyType')
lst = aoaoat.ArrayOfAnyType = []
for L in [[1,2], [5,6]]:
    aoat = client.factory.create('ArrayOfAnyType')
    aoat.anyType = L
    lst.append(aoat)
response = client.service.CalculateWeb2DObjectArray(1073757, aoaoat, 99)
print response

rawDescription: Soap Client using Suds

description: Soap Client using Suds


code:
import gevent.monkey; gevent.monkey.patch_all()
import socket

if socket.socket.__module__ == "gevent.socket":
    print "gevent monkey patch has occurred"

rawDescription: How do I detect if Gevent's monkey-patching is active?

description: Detect if Gevent's monkey-patching is active


code:
from time import clock
import urllib
import re
import BeautifulSoup
import lxml.html

start = clock()
url = 'http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx'
data = urllib.urlopen(url).read()
print "Loading took", (clock()-start), "s"

# by regex
start = clock()
x = data.find('Ingredients
')
patingr = re.compile('
\r\n +(.+?)
\r\n')
res1 = '\n'.join(patingr.findall(data,x))
print "Regex parse took", (clock()-start), "s"

# by BeautifulSoup
start = clock()
bs = BeautifulSoup.BeautifulSoup(data)
ingreds = bs.find('div', {'class': 'ingredients'})
res2 = '\n'.join(s.getText().strip() for s in ingreds.findAll('li'))
print "BeautifulSoup parse took", (clock()-start), "s  - same =", (res2==res1)

# by lxml
start = clock()
lx = lxml.html.fromstring(data)
ingreds = lx.xpath('//div[@class="ingredients"]//li/text()')
res3 = '\n'.join(s.strip() for s in ingreds)
print "lxml parse took", (clock()-start), "s  - same =", (res3==res1)

rawDescription: Python using Beautiful Soup for HTML processing on specific content


code:
for links in soup.find_all('div','moduleBody'):
    for link in links.find_all('div','feature'):
        anchor_tag = link.h2.a
        if not 'video' in anchor_tag['href']:
            print 'Name: %s' % anchor_tag.text
            print 'Link extracted: %s' % anchor_tag['href']

rawDescription: Beutifulsoup parse, get information from child tag


code: print getRoots(myGraph)

rawDescription: Python connected components

description: Python connected components


code:
import sys
class MyClass(object):
    def __init__(self, emails, delay_time=0):
        for email in emails:
            print email
        print delay_time
if __name__ == "__main__":
    if len(sys.argv)>1:
        emails = sys.argv[1:-1]
        delay = sys.argv[-1]
        try:
            delay = int(delay)
        except ValueError:
            emails.append(delay) #there's no delay
            delay=0
        a = MyClass(emails,delay_time=delay)

rawDescription: How to handle multiple values within two arguments in python class

description: Handle multiple values within two arguments in python class


code:
class Wrapper(object):
    def __init__(self, wrappee):
        self.wrappee = wrappee

    def foo(self):
        print 'foo'

    def __getattr__(self, attr):
        try:
            super(Wrapper, self).__getattr__()
        except AttributeError as e:
            return getattr(self.wrappee, attr)

class Wrappee(object):
    def bar(self):
        print 'bar'

o2 = Wrappee()
o1 = Wrapper(o2)

o1.foo()
o1.bar()

rawDescription: Proxy object in Python

description: Proxy object in Python


code:
br=mechanize.Browser()
br.set_handle_robots(False)

def open_page(url,agent,proxy):
    changeuseragent(agent.strip()) # pass br here, or move above lines out
    addproxy(proxy.strip()) # into the global scope
    changecookie()

    return br.open(url)

# if script is executed, not imported. This line below is common magic.
if __name__=="__main__":
    # TODO: open your files
    somelines = file(path,"r").readlines()
    #
    running = True
    while running:
        # TODO: select a line
        oneline = random.choice(lines)
        secondline = random.choice(otherlines)
        #
        f = open_page(your_url,agentline,proxyline)
        print f.read() #<or do whatever you wish
        f.close() #<not necessary

        running = raw_input("x and enter to exit: ").lower().startswith("x")
    # And on it goes.

rawDescription: How to make an annonimizer with random proxy/UA from files?

description: Make an annonimizer with random proxy/UA from files


code: print results

rawDescription: Dynamically creating classes in python and __repr__


code:
import functools
import operator

L = list('abcd')

def foo(indexable):
    return functools.partial(operator.__getitem__, indexable)

g = foo(L)

for i in xrange(len(L)):
    print g(i),

rawDescription: Inverse of `operator.itemgetter`

description: Inverse of `operator.itemgetter`


code:
html = """

 abc
 a.b.c.
 blah (abc)
 abc-blah
 blah-abc
 blah abc blah
 blah-abc-blah
 qweabcrty
"""

import re
print re.sub(r'\b(abc|a\.\b.\.c)\b', '@@@', html)

rawDescription: Regular expression for finding an acronym and avoid words composed by that pattern


code:
for i in range(0, max_no+1, 50):
    print i, i+49

rawDescription: Python- Most efficient way to return batches of numbers


code:
class A:
  def foo(self, x = [] ):
    x.append(1)
    self.x = x

a = A()
a.foo()
print a.x
# prints: [1]
a.foo()
print a.x
# prints: [1,1]   # !!!! Consumer would expect this to be [1]
y = [1,2,3]
a.foo(y)
print a.x
# prints: [1, 2, 3, 1]
print y
# prints: [1, 2, 3, 1]  #  !!!! My list was modified

rawDescription: Undesired python feedparser instantiation relic


code:
print "%(name)s, %(age)s" % Default('', {"name":'Bob'})
print "%(name)s, %(age)s" % Default('', name='Bob')

rawDescription: Python String template dict KeyError, how set detault value


code:
import os
#import re  -- This module can be used for more advanced processings

path = '/home/anmol/Desktop/test/testing/'#path of sample files

files = [fiile for fiile in os.listdir(path) if '~' not in fiile]
#this generates a list with the non-hidden files, hence using '~'
#os.listdir() returns a list of all the files in a given directory.

#os.listdir() = ['file 4.txt', 'file 5.txt', 'file 4.txt~', 'file 5.txt~',
#'file2.txt', 'file1.txt~', 'file1.txt', 'file3.txt', 'file3.txt~', 'file2.txt~']

# After using '~' not in fiile
# files = ['file 4.txt', 'file 5.txt', 'file2.txt', 'file1.txt', 'file3.txt']

for file_name in files:
    document = open(path+file_name).read()    #returns a string
    refined_document = " ".join(document.split("\n"))   # replaces \n with a space
    if "intermediate" in refined_document or "depot" in refined_document:
        #main working condition
        print "The required word is found under :",file_name

rawDescription: Search file for match, if not available go to next file


code:
from lxml import etree

xml = '''<root>
    <ParameterGroup name="FitItem">
            <ParameterGroup name="Affected Cross Validation Experiments">
            </ParameterGroup>
            <ParameterGroup name="Affected Experiments">
              <Parameter name="Experiment Key" type="key" value="Experiment_1"/>
              <Parameter name="Experiment Key" type="key" value="Experiment_2"/>
              <Parameter name="Experiment Key" type="key" value="Experiment_4"/>
            </ParameterGroup>
            <Parameter name="LowerBound" type="cn" value="1e-06"/>
            <Parameter name="ObjectCN" type="cn" value="CN=Root,Model=NoName,Vector=Reactions[V18],ParameterGroup=Parameters,Parameter=V,Reference=Value"/>
            <Parameter name="StartValue" type="float" value="0.1852208634119804"/>
            <Parameter name="UpperBound" type="cn" value="100"/>
          </ParameterGroup>
</root>'''
copasiML=etree.fromstring(xml)
query = "//*[@name='ObjectCN'][contains(@value,'[V18]')][contains(@value,'Parameter=V')]"
for i in copasiML.xpath(query):
    parent = i.getparent()
    parent.getparent().remove(parent)

print etree.tostring(copasiML)

rawDescription: Removing specific elements from XML using Etree


code:
import Image
from numpy import *

data = random.random((2, 2))
img1 = Image.fromarray(data)
img1.save('test.tiff')
img2 = Image.open('test.tiff')

f1 = list(img1.getdata())
f2 = list(img2.getdata())
print f1 == f2
print f1

rawDescription: How do I save a mode 'F' image? (Python/PIL)

description: Save a mode 'F' image? (Python/PIL)


code:
for item in joe:
    print item

rawDescription: How can I access and sort lists within lists in python?

description: Access and sort lists within lists in python


code:
import getpass
password = getpass.getpass()
print 'You entered:', password

rawDescription: How to get input from user when connecting to remote servers? [Python]

description: Get input from user when connecting to remote servers? [Python]


code:
def ordered_permutations(list1, list2):
    perms = []
    if len(list1) + len(list2) == 1:
        return [list1 or list2]
    if list1:
        for item in ordered_permutations(list1[1:], list2):
            perms.append([list1[0]] + item)
    if list2:
        for item in ordered_permutations(list1, list2[1:]):
            perms.append([list2[0]] + item)
    return perms

for combo in ordered_permutations(['b','a'], ['c', 'd']):
    print combo

rawDescription: Permutations of the elements of two lists with order restrictions