code:
import random

class Character:
    def __init__(self, max_hp, max_power):
        self.hp = random.randint(1, max_hp)
        self.power = random.randint(1, max_power)
    def is_dead(self):
        return self.hp <= 0
    def hit_by(self, enemy):
        self.hp -= enemy.power

class Player(Character):
    def __init__(self):
        Character.__init__(self, max_hp=10, max_power=3)
        self.xp = 0
        self.level = 0
        self.xp_thresholds = [(i + 2) ** 2 for i in range(15)]
    def battle_win(self):
        self.xp += battle_win_xp
        if self.level < len(self.xp_thresholds) and self.xp > self.xp_thresholds[self.level + 1]:
            self.level_up()
    def level_up(self):
        self.hp += 1
        self.level += 1
        print('you are now at level %s!, your health now is %s points' % (self.level + 1, self.hp))

def begin():
    game.player = Player()
    print('______________________________________')
    print('Welcome to the Battle of the dragons!')
    print("you are a fierce Warrior with %s health points and A power of %s points" %(game.player.hp, game.player.power))
    print('------------------------------------------------------------------------')
def new_dragon():
    print('While adventuring you have met a scary looking dragon')
    print('Without hesitation you jump to fight it off!')
    print('=============================================')
    game.dragon = Character(5, 5)
def fight():
    player, dragon = game.player, game.dragon
    if random.randint(0, 1):
        player.hit_by(dragon)
        print("the dragon hits you for %s points" % dragon.power)
        if player.is_dead():
            print("you're dead! Game over")
            return
        else:
            print("you have %s health left!" % player.hp)
    else:
        dragon.hit_by(player)
        print("you hit the dragon for %s points" % player.power)
        if dragon.is_dead():
            print("Horaay!you have killed the dragon!and your experience points are now %s"%player.xp)
            player.battle_win()
            game.dragon = None
            return
        else:
            print ("the dragon have %s health left!" %dragon.hp)
    print "Press enter to continue (q to quit)"
    if input() is 'q':
        game.finished = True
def play_again():
    print 'do you want to play again?(y)es or no'
    if input().lower().startswith('y'):
        game.__init__()
    else:
        game.finished = True
battle_win_xp = 5 #the default XP gain after battle win
class Game:
    def __init__(self):
        self.dragon = None
        self.player = None
        self.finished = False
game = Game()
while not game.finished:
    if not game.player:
        begin()
    elif game.player.is_dead():
        play_again()
    elif not game.dragon:
        new_dragon()
    else:
        fight()

rawDescription: how to get out of my game while game ending conditions are not met?

description: Get out of my game while game ending conditions are not met


code:
from lxml import etree as ET

data = """<?xml version="1.0"?>
<data>
<item>1</item>
<test>2</test>
</data>"""

tree = ET.fromstring(data)

for element in tree.xpath('//item|//test'):
    print element.text

rawDescription: ElementTree findall 'or' operator

description: ElementTree findall 'or' operator


code:
import re

def expand(s):

    ''' Group 1 contains the string inside the parenthesis
        Group 2 contains the digit or character `s` after the closing parenthesis

    '''
    match = re.search(r'\(([^()]*)\)(\d|s)', s)
    if match:
        group0 = match.group()
        group1 = match.group(1)
        group2 = match.group(2)
        if group2.isdigit():
            # A digit after the closing parenthesis. Repeat the string inside
            s = s.replace(group0, ((group1 + '/') * int(group2))[:-1])
        else:
            s = s.replace(group0, '/'.join(group1.split('/') + group1.split('/')[::-1]))

    if '(' in s:
        return expand(s)

    return s

li = parse('1/(15/-23)2/4')

for index, s in enumerate(li):
    if '(' in s:
        s = expand(s)
        li[index] = s.split('/')

import itertools
print list(itertools.chain(*li))

rawDescription: Python/Regex - Expansion of Parentheses and Slashes


code:
# If the PE file was loaded using the fast_load=True argument, we will need to parse the data directories:
pe.parse_data_directories()

for entry in pe.DIRECTORY_ENTRY_IMPORT:
  print entry.dll
  for imp in entry.imports:
    print '\t', hex(imp.address), imp.name

rawDescription: Getting IAT and EAT from PE

description: Getting IAT and EAT from PE


code:
print next_morris(1)
11
print next_morris(111221)
312211

rawDescription: Can anyone provide a more pythonic way of generating the morris sequence?


code:
# convert both lists to dictionaries
primers_names_dict = {
    (str(p['part number']), str(p['construct']), str(p['direction'])): p
    for p in primers_names_list
}
primers_sequence_dict = {
    (str(p['Part Number']), str(p['Construct Number']), str(p['Direction'])): p
    for p in primers_without_names
}

# now that we have two dicts, we can do a key<->key match between them, so each
# comparison op is just a dictionary key lookup, which is O(1) on average
matches = []
for key in primers_names_dict.keys():
    if key in primers_sequence_dict: # amortized O(1) lookup
        matches.append( {
            'part number': primers_names_dict[key]['part number'],
            'construct number': primers_names_dict[key]['construct'],
            'notes': primers_names_dict[key]['notes'],
            'primer sequence': primers_sequence_dict[key]['Primer Sequence']
        } )

for m in matches:
    print m
print len(matches)

rawDescription: How to check whether a subset of dictionary items exists in another list of dictionaries?

description: Check whether a subset of dictionary items exists in another list of dictionaries


code:
def pick(seq,superset,match):
    subset = []
    for item in superset:
        if len([i for i, j in zip(list(seq),list(item)) if i==j])<=match:
            subset.append(''.join(item))
    return subset

superset = [
    'GGG', 'GGC', 'GGA', 'GGT', 'GCG', 'GCC', 'GCA', 'GCT', 'GAG', 'GAC', 'GAA', 'GAT', 'GTG', 'GTC', 'GTA', 'GTT',
    'CGG', 'CGC', 'CGA', 'CGT', 'CCG', 'CCC', 'CCA', 'CCT', 'CAG', 'CAC', 'CAA', 'CAT', 'CTG', 'CTC', 'CTA', 'CTT',
    'AGG', 'AGC', 'AGA', 'AGT', 'ACG', 'ACC', 'ACA', 'ACT', 'AAG', 'AAC', 'AAA', 'AAT', 'ATG', 'ATC', 'ATA', 'ATT',
    'TGG', 'TGC', 'TGA', 'TGT', 'TCG', 'TCC', 'TCA', 'TCT', 'TAG', 'TAC', 'TAA', 'TAT', 'TTG', 'TTC', 'TTA', 'TTT'
]

seq = "AAA"

print pick(seq, superset, 1)

rawDescription: python: iterate through dynamic list

description: Python: iterate through dynamic list


code:
import time
import numpy as np
import csv

start = time.time()
def elapsed():
    return time.time() - start

# count data rows, to preallocate array
f = open('links.csv', 'rb')
def count(f):
    while 1:
        block = f.read(65536)
        if not block:
             break
        yield block.count(',')

linecount = sum(count(f))
print '\n%.3fs: file has %s rows' % (elapsed(), linecount)

# pre-allocate array and load data into array
m = np.zeros(linecount, dtype=[('a', np.uint32), ('b', np.uint32)])
f.seek(0)
f = csv.reader(open('links.csv', 'rb'))
for i, row in enumerate(f):
    m[i] = int(row[0]), int(row[1])

print '%.3fs: loaded' % elapsed()
# sort in-place
m.sort(order='b')

print '%.3fs: sorted' % elapsed()

rawDescription: Python: How to read huge text file into memory


code:
import re
from nltk.corpus import stopwords

scentence = 'El problema del matrimonio es que se acaba todas las noches despues de hacer el amor, y hay que volver a reconstruirlo todas las mananas antes del desayuno.'

#We only want to work with lowercase for the comparisons
scentence = scentence.lower()

#remove punctuation and split into seperate words
words = re.findall(r'\w+', scentence,flags = re.UNICODE | re.LOCALE)

#This is the simple way to remove stop words
important_words=[]
for word in words:
    if word not in stopwords.words('spanish'):
        important_words.append(word)

print important_words

#This is the more pythonic way
important_words = filter(lambda x: x not in stopwords.words('spanish'), words)

print important_words

rawDescription: Get rid of stopwords and punctuation

description: Get rid of stopwords and punctuation


code:
from timeit import timeit
import re

def find(string, text):
    if string.find(text) > -1:
        pass

def re_find(string, text):
    if re.match(text, string):
        pass

def best_find(string, text):
    if text in string:
       pass

print timeit("find(string, text)", "from __main__ import find; string='lookforme'; text='look'")
print timeit("re_find(string, text)", "from __main__ import re_find; string='lookforme'; text='look'")
print timeit("best_find(string, text)", "from __main__ import best_find; string='lookforme'; text='look'")

rawDescription: What's a faster operation, re.match/search or str.find?


code:
def mymatrix():
    return np.matrix('1 2; 3 4')

mat = mymatrix()
print mat
# outputs [[1 2] [3 4]]

rawDescription: Numpy Matrix class: Default constructor attributes for inherited class


code:
found = data.find("STUDENT1")
while found != -1:
    print found
    found = data.find("STUDENT1", found+1)

rawDescription: how to find a word in ASCII file using python

description: Find a word in ASCII file using python


code:
def _print_messages():
    print 'Started Printing loop'
    for log_record in log_iterator(['testlog', 'anotherlog']):
        print '%s:%s:%s' % (log_record.levelname, log_record.name, log_record.message)

def test_log_handling():

    t = threading.Thread(target = _print_messages)
    t.start()

    time.sleep(.1)
    logging.getLogger('testlog').critical('Message!')
    logging.getLogger('anotherlog').critical('Another Message right away')
    logging.getLogger('badlog').critical("Someething useless that you don't want to hear")
    time.sleep(.1)
    logging.getLogger('testlog').critical('Another Message after some wait')

rawDescription: Python: Have an iterator iterate over log messages on a different thread


code:
import argparse

class Pairs(argparse.Action):
    def __call__(self, parser, namespace, values, opts, **kwargs):
        lst = getattr(namespace, self.dest)
        if lst is None:
            lst = []
            setattr(namespace, self.dest, lst)
        lst.append(tuple(values))

parser = argparse.ArgumentParser()
parser.add_argument('-R', nargs='+', dest='libnames', action=Pairs)
print parser.parse_args("-R a b -R c d -R e f".split())

rawDescription: How can I make python's argparse accept any number of [-R a b]s, and aggregate them into a list

description: Make python's argparse accept any number of [-R a b]s, and aggregate them into a list


code:
d = MultiKeyDict(a=1, b=2)
d['c', 'd'] = 3 # two keys for one value
print d['c'], d['d']
d['c'] = 4
print d['d']
d.add_keys('d', ('e',))
d['e']
d2 = MultiKeyDict.from_dict({ ('a', 'b'): 1 })
d2['a'] = 2
d2['b']

rawDescription: Python Map(Dict) with multiple indexes

description: Python Map(Dict) with multiple indexes


code:
print np.unravel_index(np.argmax(r), r.shape)
#  (191, 86, 106)  # seems reasonable

rawDescription: How to set the origin of a volumetric spot knowing its coordinates origin?

description: Set the origin of a volumetric spot knowing its coordinates origin


code:
for item in _ih:
    print item

rawDescription: Search inside ipython history

description: Search inside ipython history


code:
from mako.template import Template
from mako.runtime import Context
from StringIO import StringIO

mytemplate = Template("""
<%def name='html_link(w)'>
${w}
</%def>
<%def name='text_link(w)'>
${w}
</%def>
<%def name="body()">
I visit ${getattr(local, format + '_link')(website)} all the time.
</%def>
""")

buf = StringIO()
ctx = Context(buf, website='stackoverflow.com', format='html')
mytemplate.render_context(ctx)
print buf.getvalue()

rawDescription: How do I call template defs with names only known at runtime in the Python template language Mako?

description: Call template defs with names only known at runtime in the Python template language Mako


code:
import py_streamhtmlparser
parser = py_streamhtmlparser.HtmlParser()
html = """
        link
"""
cur_attr = cur_value = None
for index, character in enumerate(html):
   parser.Parse(character)
   if parser.State() == py_streamhtmlparser.HTML_STATE_VALUE:
      # we are in an attribute value. Record what we got so far
      cur_tag = parser.Tag()
      cur_attr = parser.Attribute()
      cur_value = parser.Value()
      continue
   if cur_value:
      # we are not in the value anymore, but have seen one just before
      print "%r %r %r" % (cur_tag, cur_attr, cur_value)
      cur_value = None

rawDescription: example for using streamhtmlparser

description: Example for using streamhtmlparser


code:
import sys
import numpy
from scipy.ndimage import label, morphology

def split_sprite(img, mask, bg, join_interior=True, basename='sprite_%d.png'):
    im = img.load()

    m = numpy.array(mask, dtype=numpy.uint8)
    if join_interior:
        m = morphology.binary_fill_holes(m)
    lbl, ncc = label(m, numpy.ones((3, 3)))

    for i in xrange(1, ncc + 1):
        px, py = numpy.nonzero(lbl == i)
        xmin, xmax, ymin, ymax = px.min(), px.max(), py.min(), py.max()

        sprite = Image.new(img.mode, (ymax - ymin + 1, xmax - xmin + 1), bg)
        sp = sprite.load()
        for x, y in zip(px, py):
            x, y = int(x), int(y)
            sp[y - int(ymin), x - int(xmin)] = im[y, x]

        name = basename % i
        sprite.save(name)
        print "Wrote %s" % name

sprite = Image.open(sys.argv[1])
mask, bg = sprite_mask(sprite)
split_sprite(sprite, mask, bg)

rawDescription: PySide: Separating a spritesheet / Separating an image into contiguous regions of color


code:
s1="""
from itertools import groupby
a = ['1CDABCABDA', '1CDABCABDB', '1CDABCABDD', '1BCABCCCAA', '1DDAABBBBA', '1BCABCCCAD']
key = lambda i: a[i][:-1]
indexes = sorted(range(len(a)), key=key)
result = [[x, list(y)] for x, y in groupby(indexes, key=key)]
"""
s2="""
a = ['1CDABCABDA', '1CDABCABDB', '1CDABCABDD', '1BCABCCCAA', '1DDAABBBBA', '1BCABCCCAD']
new=[i[:-1] for i in a]
d={}
for i,j in enumerate(new):
   d.setdefault(j,[]).append(i)
d.items()
    """

print ' first: ' ,timeit(stmt=s1, number=100000)
print 'second : ',timeit(stmt=s2, number=100000)

rawDescription: Python optimize how to find duplicate value and value index in a list


code:
for info in soup.find_all('div', class_='user-info'):
    print 'href:', info.find('a', href=True)['href']
    print 'user:', info.find('span', class_='usertag').text

#href: /kjrphotography
#user: @kjrphotography

rawDescription: How to do Regex for a href?

description: Do Regex for a href


code:
u = 'http://streaming.radionomy.com/Cheche-International-Radio'
print 'started...', u
r = requests.get(u, stream=True)
content_type = r.headers['Content-Type']
if content_type.startswith('text/html'):
    content = r.content
    # process the content
else:
    print 'discarded ', u

rawDescription: How do I close a request to a streaming radio stream url?

description: Close a request to a streaming radio stream url


code:
myfile = open("readnwrite.txt", "r+") #The "r+" specifies we want to read and write
for line in myfile:
    print line

rawDescription: Opening and reading txt files

description: Opening and reading txt files


code:
text = " ".join(text.split())
# -> u'2 rooms \xb7 USD 0'
print text
# -> 2 rooms · USD 0

rawDescription: beautifulsoup 4 + python: string returns 'None'

description: Beautifulsoup 4 + python: string returns 'None'


code:
import sys

file2= sys.argv[0]
print file2

rawDescription: how to pass filename to the variable in Python

description: Pass filename to the variable in Python


code:
dict = request.form
for key in dict:
    print 'form key '+dict[key]

rawDescription: Print all POST request parameters without knowing their names


code:
for key, value in b.iteritems():
    print '%s %s %s' % (key[0], key[1], value)

rawDescription: Python tuple operations and count

description: Python tuple operations and count


code:
def rand_bitstring(n):
    rand = random.SystemRandom()
    lst = []
    for j in range(1, n+1):
        r = rand.randint(1,2)
        if r == 2:
            lst.append(0)
        else:
            lst.append(1)

    return lst

def main():
    lst = rand_bitstring(50)*200000
    print time.time()
    print powercheck(lst)
    print time.time()
    powercheck2(lst)
    print time.time()

rawDescription: Detect whether sequence is a multiple of a subsequence in Python


code:
def to_ranges(seq):
    iseq = iter(seq)
    x = next(iseq)
    yield range(0,x+1)
    start = x
    for x in iseq:
        stop = start+x
        yield range(start+1,stop+1)
        start = stop

for x in to_ranges([1,2,3,4]):
    print x

rawDescription: Generate summed up ranges from a list

description: Generate summed up ranges from a list


code:
import time
now = time.time()
hunger = lambda: time.time() > now + 60
for i in range(10):
    print 'doing stuff here on loop', i
    time.sleep(10)
    print 'hunger is', hunger()

rawDescription: How could I set a boolean value to True, and make it so if a condition is met, it becomes False, but only for a certain amount of time?


code:
mydict={}
mydict['val2']=200
exec "val1=val2" in mydict
mydict.keys()
mydict['val2']
mydict['val1']

rawDescription: Given module m and code object c, what does "exec c in m.__dict__" do?


code:
from StringIO import StringIO

pdfContent = StringIO(getPDFContent("Billineg.pdf").encode("ascii", "ignore"))
for line in pdfContent:
    print line

rawDescription: How to use Python to pick up texts on PDF documents

description: Use Python to pick up texts on PDF documents


code:
sent = 'These sentences involves some horsing around'
for word, tag in pos_tag(word_tokenize(sent)):
    wntag = tag[0].lower()
    wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
    lemma = wnl.lemmatize(word, wntag) if wntag else word
    print lemma

rawDescription: WordNetLemmatizer not returning the right lemma unless POS is explicit - Python NLTK


rawDescription: Preparing a list from a dictionary in python


code:
import collections
import signal
import subprocess

class Alarm(Exception):
    pass

def alarm_handler(signum, frame):
    raise Alarm

def main():
    # start process, redirect stdout
    process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True)

    # set signal handler
    signal.signal(signal.SIGALRM, alarm_handler)
    signal.alarm(2) # produce SIGALRM in 2 seconds

    try:
        # save last `number_of_lines` lines of the process output
        number_of_lines = 200
        q = collections.deque(maxlen=number_of_lines)
        for line in iter(process.stdout.readline, ""):
            q.append(line)
        signal.alarm(0) # cancel alarm
    except Alarm:
        process.terminate()
    finally:
        # print saved lines
        print ''.join(q)

if __name__=="__main__":
    main()

rawDescription: Stop reading process output in Python without hang?


code:
from itertools import islice
with open('file.txt', 'r') as f:
    # iterate over every 3rd line, starting with the 2nd
    for line in islice(f, 1, None, 3):
        parts = line.split()
        print 'Event'
        # iterate over 3-element chunks
        for x in zip(*(iter(parts),) * 3):
            print 'Disconnect branch from bus %s to %s circuit %s' % x
        print 'end'

rawDescription: Python Changing the format of a text file to a new format


rawDescription: Best way to remove similar points in a list of points

description: Remove similar points in a list of points


code:
with open('adapt.py', 'r') as my_file:
    source = my_file.read()
    ast_tree = ast.parse(source, 'adapt.py')
    visitor = SelfAttributesVisitor()
    visitor.visit(ast_tree)
    print visitor.attributes

rawDescription: python print variables of a specific line

description: Python print variables of a specific line


code:
import urllib
import urllib2
from bs4 import BeautifulSoup
import re
gamesForDay = ['/games/20151002/DENLAC/gameinfo.html']
for game in gamesForDay:
   url =  "http://www.nba.com/"+game
   page = urllib2.urlopen(url).read()
   soup = BeautifulSoup(page, "html.parser")

   # fetch the tables you are interested in
   tables = soup.findAll(id="nbaGITeamStats")
   for table in tables:
       team_name = table.thead.tr.th.text
       # odd/even class rows (tr)
       rows = table.find_all(attrs={'class':'odd'})
       rows.extend(table.find_all(attrs={'class':'even'}))

       for player in rows:
           # search the row cols based on 'id'
           player_name = player.find('td', attrs={'id':'nbaGIBoxNme'}).text

           # search the row cols based on 'class'
           player_position = player.find('td', attrs={'class':'nbaGIPosition'}).text

           # search for all td where the class is not defined
           player_numbers = [ x.text for x in player.findAll('td', attrs={'class':None})]

           print player_name, player_position, player_numbers

rawDescription: How to scrape data from a table using a loop to get all td data using python

description: Scrape data from a table using a loop to get all td data using python


code:
import tarfile,os
import sys
os.chdir("/tmp/foo")
tar = tarfile.open("test.tar")
for member in tar.getmembers():
    f=tar.extractfile(member)
    content=f.read()
    print "%s has %d newlines" %(member, content.count("\n"))
    print "%s has %d spaces" % (member,content.count(" "))
    print "%s has %d characters" % (member, len(content))
    sys.exit()
tar.close()

rawDescription: reading tar file contents without untarring it, in python script

description: Reading tar file contents without untarring it, in python script


code:
help(open)
print file.__doc__

rawDescription: Open/write deleting txt file contents?

description: Open/write deleting txt file contents


code:
# iterate over ALL stories

allstories = Story.objects.all()
for s in allstories:
    print s.title
    print s.thetext
    print s.userstory     # this might error!
    print s.classicstory  # this might error!

rawDescription: Fetching data from parent table in Django in database hierarchy


code:
z = self.editFunktion.text()   #put the Funktion in z
z = str(z)                     #converted it into str() (sometimes needed)
exec z                      #exec takes the String and executes it

rawDescription: Is it possible to get a python IDE working inside a PYQT4 widget?


code:
class MySyntaxError(Exception):
  def __init__(self, text, index):
    self.text = text
    self.index = index
  def __str__(self):
    return "Syntax error at index " + repr(self.index) + ": " + self.text

def parse(input):
  global index
  index = 0

  def eat_char(set):
    global index
    if index < len(input) and input[index] in set:
      index += 1
    else:
      raise MySyntaxError("expected char in " + repr(set), index)

  def e():
    eat_char(['0', '1'])

  def A():
    global index
    if index == len(input): # Successfully parsed
      return
    elif input[index] in ['0', '1']:
      while (input[index] in ['0', '1']):
        e()
    elif input[index] is '(':
      eat_char(['('])
      A()
      eat_char(['U'])
      A()
      eat_char([')'])
    else:
      raise MySyntaxError("expected char '0', '1' or '('", index)

  A()
  if index != len(input): # Parsing didn't reach the end of the string
    raise MySyntaxError("parsing ended before the end of the input", index)

def test(string):
  try:
    parse(string)
    print "The string " + string + " have been successfully parsed"
  except MySyntaxError as e:
    print "The string " + string + " can't be parsed:"
    print e

test("(01U(1U0))")
test("(01U(1U0)") # Missing parenthesis, syntax error
test("(01U(1U0)))") # Too many parenthesis, syntax error
test("01U(1U0)") # No parenthesis, syntax error

rawDescription: Add parenthesis to statement

description: Add parenthesis to statement


code:
import collections

class OrderedDefaultdict(collections.OrderedDict):
    def __init__(self, *args, **kwargs):
        if not args:
            self.default_factory = None
        else:
            if not (args[0] is None or callable(args[0])):
                raise TypeError('first argument must be callable or None')
            self.default_factory = args[0]
            args = args[1:]
        super(OrderedDefaultdict, self).__init__(*args, **kwargs)

    def __missing__ (self, key):
        if self.default_factory is None:
            raise KeyError(key)
        self[key] = default = self.default_factory()
        return default

    def __reduce__(self):  # optional, for pickle support
        args = (self.default_factory,) if self.default_factory else ()
        return self.__class__, args, None, None, self.iteritems()

def simplexml_load_file(file):
    from lxml import etree

    tree = etree.parse(file)
    root = tree.getroot()

    def xml_to_item(el):
        item = el.text or None
        child_dicts = OrderedDefaultdict(list)
        for child in el.getchildren():
            child_dicts[child.tag].append(xml_to_item(child))
        return collections.OrderedDict(child_dicts) or item

    def xml_to_dict(el):
        return {el.tag: xml_to_item(el)}

    return xml_to_dict(root)

x = simplexml_load_file('routines/test.xml')

print x

for y in x['root']:
    print y

rawDescription: how do i rewrite this function to implement OrderedDict?

description: Rewrite this function to implement OrderedDict


code:
matrix = [[4,5,6,7],[2,4,9,3]]
for row in matrix:
  for element in row:
    print element

rawDescription: Nested loops with matrix - why does this second matrix at all?


code: print dir("")

rawDescription: Convert string with thousands separator to integer in Python


code:
# UNTESTED
import requests
import csv

# Connect to the web server.
response = requests.get("https:://domain.com/file_maker.pl?param1=12345", stream=True)
# Read the data as CSV
data = csv.reader(response.raw)

# Use the data
for line in data:
  print line

rawDescription: How to download a file (perl cgi backend) using python requests

description: Download a file (perl cgi backend) using python requests


code:
from lxml import etree

file = "file://path/to/file.xml"
doc = etree.parse(file)

print doc.xpath('//cat[id=2]/name/text()')[0]

rawDescription: How to print a specific xml value using python?

description: Print a specific xml value using python


code:
def static_var(varname, value):
    print "1"

    def decorate(func):
        print "2"
        setattr(func, varname, value)
        return func
    print "3"
    return decorate

@static_var("counter", 0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

foo()
foo()

rawDescription: static variable in a function with Python Decorator

description: Static variable in a function with Python Decorator


code: print parser.parse_args(["all"])

rawDescription: argparse: identify which subparser was used

description: Argparse: identify which subparser was used


code:
def test(*args, **kwargs):
    for i in kwargs:
        exec('%s = %s' % (i, repr(kwargs[i])))
    print target

rawDescription: why doesn't ** unpack kwargs in function calls?

description: Why doesn't ** unpack kwargs in function calls


code:
import urllib2
from bs4 import BeautifulSoup

url2 = urllib2.urlopen('http://www.airport-data.com/airport/HJO/#location')
soup = BeautifulSoup(url2)

print soup.find('td', class_='tc1', text='Elevation:').next_sibling.text

rawDescription: Scraping from webpage - python

description: Scraping from webpage - python


code:
import pandas as pd
import io

temp=u"""Prediction;MAPE;Score
2015-11-01;7.93;1.83;1"""
df = pd.read_csv(io.StringIO(temp), sep=";", index_col=None)
df.index.name = ''
print df

#            Prediction  MAPE  Score
#
#2015-11-01        7.93  1.83      1

print df.index
#Index([u'2015-11-01'], dtype='object', name=u'')

#convert index to datetime
df.index = pd.to_datetime(df.index)

df = df.to_period('M')
print df

#         Prediction  MAPE  Score
#
#2015-11        7.93  1.83      1

rawDescription: Python/Datetime/Pandas - Changing datetime format


code: print f.read(1)

rawDescription: Extract first 20 bytes in a binary header


code:
form = web.input()
print form.username

rawDescription: How are POST and GET variables handled in Python?


code:
data = ['alabama', 'alaska', 'arizona', 'arkansas', 'california', 'georgia']
for state in data:
    for letter in ['b', 'c']:
        if letter in state: # or whatever condition would be a match
            break
    else:
        print state

rawDescription: Sorting out rows in a csv not working as planned


code:
from itertools import groupby, izip
words = (''.join(g) for k, g in groupby(words, ' '.__ne__) if k)
for g in izip(*[iter(words)] * 4):
    print ' '.join(g)

rawDescription: Pythonic way to split a line into groups of four words?


code:
dic = {}
for line in x:
    m = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b",line).group().split()
    c = re.search(r"\[(.+)\]",line).group().split()
    for i in range(len(m)):
        dic.setdefault(m[i], []).append(c)

for k,v in dic.iteritems(): #or items if Python 3.X
    print k, len(v), v

rawDescription: Counting number of unique items from a dictionary


code:
import numpy as np
from scipy.signal import butter, lfilter

##############################################################
# For generating sample-data
##############################################################
from scipy.integrate import odeint

def roessler_ode(y,t,omega=1,a=0.165,b=0.2,c=10):
    dy = np.zeros((3))
    dy[0] = -1.0*(omega*y[1] + y[2]) #+ e1*(y[3]-y[0])
    dy[1] = omega * y[0] + a * y[1]
    dy[2] = b + y[2] * (y[0] - c)
    return dy

class Roessler(object):
    """A single coupled Roessler oscillators"""
    def __init__(self, y=None, omega=1.0, a=0.165,b=0.2,c=10):
        self.omega = omega
        self.a = a
        self.b = b
        self.c = c
        if y==None:
            self.y = np.random.random((3))+0.5
        else:
            self.y = y

    def ode(self,y,t):
        dy = roessler_ode(y[:],t,self.omega,self.a,self.b,self.c)
        return dy

    def integrate(self,ts):
        rv = odeint(self.ode,self.y,ts)
        self.y = rv[-1,:]
        return rv
###############################################################

def butter_bandpass(lowcut,highcut,fs,order=8):
    nyq = 0.5*fs
    low = lowcut/nyq
    high = highcut/nyq

    b,a = butter(order, [low, high], btype='band')
    return b,a

def butter_bandpass_filter(data,lowcut,highcut,fs,order=8):
    b,a = butter_bandpass(lowcut,highcut,fs,order=order)
    return lfilter(b,a,data)

# generate sample data
data = Roessler().integrate(np.arange(0,1000,0.1))[:,2]
data += np.random.normal(size=data.shape)

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

data_bp8 = butter_bandpass_filter(data,300,2000,20000,8)
data_bp2 = butter_bandpass_filter(data,300,2000,20000,2)

# plot signals
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot(data, label="raw")
plt.plot(data_bp8, label="order 8")
plt.plot(data_bp2, label="order 2")
plt.legend()

# plot power spectral densities
plt.figure(2)
plt.psd(data, Fs=200000, label="raw")
plt.psd(data_bp8, Fs=20000, label="order 8")
plt.psd(data_bp2, Fs=20000, label="order 2")
plt.legend()

plt.show()

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


code:
import pymel.core.uitypes as pmui
print pmui.ModelEditor().getRendererListUI()

rawDescription: How to query current viewport renderer in maya

description: Query current viewport renderer in maya


code:
df = []
result = []

for one_file in folder:
        with open(one_file) as f:
            df.append( pd.read_csv(f) )
            result.append( function(df[-1]) )
            print result[-1]

print df[0]
print df[1]
# etc.
print df[-1] # last df

print result[0]
print result[1]
# etc.
print result[-1] # last result

rawDescription: Python: Multiple CSVs

description: Python: Multiple CSVs


code:
print "All UIDs:",values.keys()
print "Seen UIDs:",[uid for uid,flags in values.iteritems() if r"\Seen" in flags]
print "Deleted UIDs:",[uid for uid,flags in values.iteritems() if r"\Deleted" in flags]

rawDescription: pythonic way to optimize the logic to filter/extract data from list

description: Pythonic way to optimize the logic to filter/extract data from list


code: print "\"%s\""%word

rawDescription: printing double quotes around a variable

description: Printing double quotes around a variable


code:
import random

def count_function(sequence, item):
    index = 0
    count = 0

    while index < len(sequence):
        if sequence[index] == item:
            count = count + 1
        index = index + 1

    return count

random_list = []
list_length = 20

while len(random_list) < list_length:
    random_list.append(random.randint(0,10))

count = count_function(random_list, 9)

print random_list
print count

rawDescription: If there any way to add this code to a function?


code:
mydict = {
        'test1': {
                'turned_on': True,
                'other_data': 'foo',
            },
        'test2': {
            'turned_on': False,
            'other_data': 'bar',
            },
        }
delete_if_not('turned_on', mydict)
print mydict

rawDescription: How to delete entries in a dictionary with a given flag in python?

description: Delete entries in a dictionary with a given flag in python


code:
def get_text_before(element):
    for item in element.xpath("preceding-sibling::*/text()|preceding-sibling::text()"):
        item = item.strip()
        if item:
            yield item

def get_text_after(element):
    for item in element.xpath("following-sibling::*/text()|following-sibling::text()"):
        item = item.strip()
        if item:
            yield item

for el in tree.findall('.//dd'):
    before = " ".join(get_text_before(el))
    after = " ".join(get_text_after(el))

    print {
        "el": el,
        "before": before,
        "after": after
    }

rawDescription: lxml etree get all text before element

description: Lxml etree get all text before element


code:
from bs4 import BeautifulSoup as bs

html = """

This section on C interview questions and answers focuses on “Variable Names”. One shall practice these interview questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Interview questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:

1. C99 standard guarantees uniqueness of ____ characters for internal names.

a) 31

b) 63

c) 12

d) 14

View Answer
Answer:b

Explanation:ISO C99 compiler may consider only first 63 characters for internal.

2. C99 standard guarantess uniqueness of _____ characters for external names.

a) 31

b) 6

c) 12

d) 14

View Answer
Answer:a

Explanation:ISO C99 compiler may consider only first 31 characters for external

variables having 31 characters due to which it may not be unique.

3. Which of the following is not a valid variable name declaration?

a) int __a3;

b) int __3a;

c) int __A3;

d) None of the mentioned

View Answer
Answer:d

Explanation:None.

4. Which of the following is not a valid variable name declaration?

a) int _a3;

b) int a_3;

c) int 3_a;

d) int _3a
"""

soup = bs(html,'html.parser')

p = soup.select('div.entry-content p')
for i in p[2:]:
    print i.text.encode('utf-8')
    print '\n'*3 # just print three newlines

rawDescription: How to extract text in between <br> python

description: Extract text in between <br> python


code:
try:
  si = iter(string)
  for c in subStr:
    while c != si.next():
      pass
except StopIteration:
  print "no match"
else:
  print "match"

rawDescription: python string match time complexity

description: Python string match time complexity


code:
import numpy as np
x = np.arange(10, dtype=np.int)

print 'An integer array:', x
print 'But if we view it as a float:', x.view(np.float)
print "...It's probably not what we expected..."

rawDescription: Numpy: Permanent changes to using numpy.ndarray.view?


rawDescription: Neighbourhood of Scipy Labels

description: Neighbourhood of Scipy Labels


code:
words = set(open("wordlist.txt").read().split())
for line in open_file_handle:
    if words.intersection(line.split()):
       print "Match Found"
    else:
       print "No Match Found"

rawDescription: BRO IDS searches

description: BRO IDS searches


code:
import itertools
import operator

a = {'name' : 'a', 'items': 3}
b = {'name' : 'b', 'items': 4}
c = {'name' : 'c', 'items': 5}

dcts = [a,b,c]

x = itertools.product(range(a['items']), range(b['items']), range(c['items']))
y = itertools.ifilter(lambda i: sum(i)==7, x)
z = (tuple([[dct, operator.setitem(dct, 'picked', vval)][0] \
       for dct,vval in zip(dcts, val)]) for val in y)
for zz in z:
    print zz

rawDescription: all possible combinations of dicts based on values inside dicts

description: All possible combinations of dicts based on values inside dicts


code: print 'My name is {}.'.format(name)

rawDescription: options when formatting strings

description: Options when formatting strings


code:
import Tkinter

class Monitor(object):

  def __init__(self):
    self.mw = Tkinter.Tk()
    self.mw.title("Messages by NeuronSimulation")
    self.text = Tkinter.Text(self.mw, width = 80, height = 30)
    self.text.pack()
    self.mw.protocol(name="WM_DELETE_WINDOW", func=self.handler)
    self.is_mw = True

  def write(self, s):
    if self.is_mw:
      self.text.insert(Tkinter.END, str(s) + "\n")
    else:
      print str(s)

  def handler(self):
    self.is_mw = False
    self.mw.quit()
    self.mw.destroy()

monitor = Monitor()

rawDescription: classmethod for Tkinter-Monitor-Window

description: Classmethod for Tkinter-Monitor-Window


code:
tmp = ar.flatten()[sorted_ind].compressed() # compressed() will delete all invalid data.
idx = np.searchsorted(tmp, 51.0)
print sorted_ind[:idx]
print sorted_ind[idx:len(tmp)]

rawDescription: NumPy: Find sorted indices from a masked 2D array above and below a threshold


code:
for row in sorted(split_processes[1:], key=itemgetter(10)):
    print " ".join(row)

rawDescription: Python sort string array with subtring

description: Python sort string array with subtring


code:
query = "SELECT * from %s where id = %d"
completeQuery=query% ('myTable', 21)
print completeQuery

rawDescription: Read txt file and add a variable to the text - Python


code:
class AutoRegister(type):
  def __new__(mcs, name, bases, D):
    self = type.__new__(mcs, name, bases, D)
    if "ID" in D:  # only register if has ID attribute directly
      if self.ID in self._by_id:
        raise ValueError("duplicate ID: %r" % self.ID)
      self._by_id[self.ID] = self
    return self

class Base(object):
  __metaclass__ = AutoRegister
  _by_id = {}
  ID = "base"

  @classmethod
  def from_id(cls, id):
    return cls._by_id[id]()

class A(Base):
  ID = "A"

class B(Base):
  ID = "B"

print Base.from_id("A")
print Base.from_id("B")

rawDescription: How to get a reference to the current class from class body?

description: Get a reference to the current class from class body


code: print '*' * n

rawDescription: How to print a horizontal line that is as long as the user inputs it

description: Print a horizontal line that is as long as the user inputs it


code:
import re

a = open("file", "r")
g = a.read()
ma = re.finditer('test', g)
for t in ma:
    print t.start(), t.end()

rawDescription: Find all string positions in file

description: Find all string positions in file


code:
import pandas as pn
file_name = './exmp.txt'
names = ['class', 'value']
df = pn.read_csv(file_name, sep=': ', names=names, engine='python')
print 'Max values'
print df.groupby('class').max()
print 'Min values'
print df.groupby('class').min()
print 'Avr values'
print df.groupby('class').mean()

rawDescription: Python process data statistics runs very slow while using tuple in dictionary


code:
for abc in list_of_lists:
    for s in takewhile(lambda x: x.count(" ") < 3, abc):
        print s

rawDescription: Python: printing elements from a list till a specific element


code:
defaults = {'foo':0, 'bar':1, 'baz':2}

@defaultArgs(defaults)
def func(**kwargs):
    print kwargs  # args accessible via the kwargs dict

rawDescription: Dynamically define functions with varying signature


code:
from bs4 import BeautifulSoup, SoupStrainer
import urllib2
import re

def main():
    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    url = 'http://www.cnn.com/2013/10/29/us/florida-shooting-cell-phone-blocks-bullet/index.html?hpt=ju_c2'
    soup = BeautifulSoup(opener.open(url))
    #1) Link to the website

    #2) Date article published
    date = soup.find("div", {"class":"cnn_strytmstmp"}).text.encode('utf-8')
    #3) title of article
    title = soup.find("div", {"id":"cnnContentContainer"}).find('h1').text.encode('utf-8')
    #4) Text of the article
    paragraphs = soup.find('div', {"class":"cnn_strycntntlft"}).find_all('p')
    text = " ".join([ paragraph.text.encode('utf-8') for paragraph in paragraphs])

    print url
    print date
    print title
    print text

if __name__ == '__main__':
     main()

rawDescription: Web Scraping news article and exporting to csv file


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

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

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


code:
import numpy as np
from sklearn import preprocessing
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout

# Create some random data
np.random.seed(42)
X = np.random.random((10, 50))

# Similar labels
labels = ['good', 'bad', 'soso', 'amazeballs', 'good']
labels += labels
labels = np.array(labels)
np.random.shuffle(labels)

# Change the labels to the required format
numericalLabels = preprocessing.LabelEncoder().fit_transform(labels)
numericalLabels = numericalLabels.reshape(-1, 1)
y = preprocessing.OneHotEncoder(sparse=False).fit_transform(numericalLabels)

# Simple Keras model builder
def buildModel(nFeatures, nClasses, nLayers=3, nNeurons=10, dropout=0.2):
    model = Sequential()
    model.add(Dense(nNeurons, input_dim=nFeatures))
    model.add(Activation('sigmoid'))
    model.add(Dropout(dropout))
    for i in xrange(nLayers-1):
        model.add(Dense(nNeurons))
        model.add(Activation('sigmoid'))
        model.add(Dropout(dropout))
    model.add(Dense(nClasses))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='sgd')

    return model

# Do an exhaustive search over a given parameter space
for nLayers in xrange(2, 4):
    for nNeurons in xrange(5, 8):
        model = buildModel(X.shape[1], y.shape[1], nLayers, nNeurons)
        modelHist = model.fit(X, y, batch_size=32, nb_epoch=10,
                              validation_split=0.3, shuffle=True, verbose=0)
        minLoss = min(modelHist.history['val_loss'])
        epochNum = modelHist.history['val_loss'].index(minLoss)
        print '{0} layers, {1} neurons best validation at'.format(nLayers, nNeurons),
        print 'epoch {0} loss = {1:.2f}'.format(epochNum, minLoss)

rawDescription: Appropriate Deep Learning Structure for multi-class classification


code:
class MySequence(object):
    def __iter__(self):
        yield 1
        yield 2
        yield 3

for x in MySequence():
    print x # prints 1, then 2, then 3

rawDescription: How to implement a minimal class that behaves like a sequence in Python?

description: Implement a minimal class that behaves like a sequence in Python


rawDescription: Lagrange interpolation in Python: as a result matematical formula


code:
class StateHolder:
    def set_state(self, state):
        self.state = state

def with_state_object(state_object):
    def decorator(f):
        def helper(*args, **kwargs):
            print "Current state is: {}.".format(state_object.state)
            return f(*args, **kwargs)
        return helper
    return decorator

global_state = StateHolder()
global_state.set_state("some state")

@with_state_object(global_state)
def foo():
    return "something"

rawDescription: variables as decorator arguments

description: Variables as decorator arguments


code:
from collections import defaultdict
tally = defaultdict(int)
text = "one two two three three three"
for i in text.split():
    tally[i] += 1
print tally  # defaultdict(<type 'int'>, {'three': 3, 'two': 2, 'one': 1})

rawDescription: Python - counting duplicate strings

description: Python - counting duplicate strings


code:
from nltk.corpus import wordnet as wn
from nltk.tag import pos_tag
sent = "dog is barking at tree"
for i in [i[0] for i in pos_tag(sent.split()) if i[1].lower()[0] == 'n']:
    print wn.synsets(i); print

rawDescription: How to get all the meanings of a word using python NLTK?

description: Get all the meanings of a word using python NLTK


code:
def mylog(text):
    logging.info(text)
    print '\x1b[80D' + '\x1b[K'+ text,
    sys.stdout.flush()

rawDescription: display only one logging line

description: Display only one logging line


rawDescription: BS4: Getting text in tag

description: BS4: Getting text in tag


code:
import re

postalCode = " 123 4th Street, Toronto, Ontario, M1A 1A1 "
#read First Line
line = postalCode

if re.search("[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]", line , re.IGNORECASE | re.DOTALL):
    print 'Match found - valid Canadian address: ', line
else:
    print 'Error - no match - invalid Canadian address:', line

rawDescription: Python, Canadian Address RegEx validation


code:
def time() :
    tlist = [0.0, 0.12, 0.16, 0.2, 0.31, 0.34, 0.38, 0.46, 0.51] # list of start time for the phonemes
    plist = [0.5, 0.1, 0.4, 0.35, 0.8, 0.6, 1.0, 0.2, 0.7, 0.9] # list of parameter values of the phonemes

    total_frames = math.floor(tlist[-1] / 0.04)
    t_u = 0.0
    i = 0
    while i < len(tlist) - 1:
        # if the number is in the range
        # do the calculations and move to the next number
        if t_u > tlist[i] and t_u < tlist[i + 1] :
            print "\n The t_u value:", t_u, 'is between',
            print "start:", tlist[i], " and end: ", tlist[i+1]
            poly = poly_coeff(tlist[i], tlist[i + 1], t_u)
            Newton(poly, plist[i], plist[i+1])
            t_u = t_u + 0.04 # regular time interval

        # if the number is at the lower boundary of the range no need of calculation as u = 0
        elif t_u == tlist[i] :
            print "\n The t_u value:", t_u, 'is on the boundary of',
            print "start:", tlist[i], " and end: ", tlist[i+1]
            print "u : 0"
            Bezier(0, plist[i], plist[i+1])
            t_u = t_u + 0.04 # regular time interval

        # if the number is at the upper boundary of the range no need of calculation as u = 1
        elif t_u == tlist[i + 1] :
            print "\n The t_u value:", t_u, 'is on the boundary of',
            print "start:", tlist[i], " and end: ", tlist[i+1]
            print " u : 1"
            Bezier(1, plist[i], plist[i+1])
            t_u = t_u + 0.04 # regular time interval

        # if the number isn't in the range, move to the next range
        else :
            i += 1

rawDescription: using two consecutive values from a list in each iteration

description: Using two consecutive values from a list in each iteration


code:
import re
pieces = 'Ka4Qb3Td7b4c4'
m = re.compile('[A-Za-z]{1,2}\d+')
print m.findall(pieces)

rawDescription: Splitting a string with multiple separators


code:
from itertools import izip_longest
def zip_plus(first, second):
    return [(a or []) + (b or []) for a, b in izip_longest(first, second)]

print zip_plus([], [[1, 2], [3, 4]])
print zip_plus([[1, 2], [3, 4]], [[11, 12], [13, 14]])
print zip_plus([[1, 2], [3, 4]], [[11, 12]])
print zip_plus([[1, 2]], [[11, 12], [13, 14]])

rawDescription: Is there a better way to build lists like this?


code:
import re

lines = ["@16 @ties-2", "5=3+2"]

for line in lines:
    print [x for x in re.split("(?<=@)([-a-z0-9]+)|([0-9=+-])|( )", line) if x]

rawDescription: Readline into Tuples

description: Readline into Tuples


rawDescription: I cannot scrape anything with BeautifulSoup


code:
PARAMS = 'TEST = xy; TEST2= klklk '
from timeit import timeit
print timeit('dict((i.strip() for i in item.split("=")) for item in PARAMS.split(";"))', "from __main__ import PARAMS")
print timeit('dict([i.strip() for i in item.split("=")] for item in PARAMS.split(";"))', "from __main__ import PARAMS")

rawDescription: Python split twice with strip in one line


code:
#!/usr/bin/env python

import gc
import os
import subprocess

# GC thresholds (http://docs.python.org/3/library/gc.html#gc.set_threshold)
print "Garbage collection thresholds: {}".format(gc.get_threshold())

if __name__ == '__main__':

    pid = os.getpid()

    print('------- No file descriptor ...')
    subprocess.call(['lsof -p %s' % pid], shell=True)

    x = open('/tmp/test', 'w')
    print('------- Reference to a file ...')
    subprocess.call(['lsof -p %s' % pid], shell=True)

    x = 2
    print('------- FD is freed automatically w/o GC')
    subprocess.call(['lsof -p %s' % pid], shell=True)

rawDescription: Python csv read file, what if I don't close the file?


code: print toSimpleXml(data, 'root')

rawDescription: How to handle recursion in python?

description: Handle recursion in python


code:
import re

# convert unicode or string to raw
def raw(s):
    if isinstance(s, str):
        s = s.encode('string-escape')
    elif isinstance(s, unicode):
        s = s.encode('unicode-escape')
    return s

s = '\x1e\x00\x00\x00\xf0\x0f184203308373388492761797873987'

print [ re.sub(r'\\', r'0', raw(i)) for i in s]

rawDescription: Convert String of Hex and Ints to List in Python


code:
import numpy as np
a = np.array(((3,4),(5,6)))
print np.sum(a) # 18
print np.sum(a, axis=0) # [8, 10]
print np.sum(a, axis=1) # [7, 11]

rawDescription: Walk through each column in a numpy matrix efficiently in Python


code:
from datetime import datetime
import itertools

data = """00:00:01.43
00:00:01.97
00:00:02.50
00:00:03.04
00:00:03.54
00:00:04.04
00:00:04.57
00:00:05.11
00:00:05.61
00:00:06.14
00:00:06.64
00:00:07.14
00:00:07.64
00:00:08.18
00:00:08.68
00:00:09.18
00:00:09.21"""

def pairs(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)

format = "%H:%M:%S.%f"
for before, after in pairs(data.split()):
    before_dt = datetime.strptime(before, format)
    after_dt = datetime.strptime(after, format)
    difference = (after_dt - before_dt).total_seconds()

    print "%s vs %s: %s" % (before, after, difference)

rawDescription: How to calculate time difference easily in hour-minute-second format

description: Calculate time difference easily in hour-minute-second format


code:
v = x.data
r, c = x.nonzero()

print np.all(x.todense()[r, c] == v)
# True

rawDescription: Scipy Handling of Large COO matrix

description: Scipy Handling of Large COO matrix


code:
def updateCache(name, new_data):
    global cache
    info = cache[name]
    datarows = info['datarows']
    datarows.append(new_data)

cache = {'foo': {'datarows': []}, 'bar': {'datarows': []}}

print cache
updateCache('foo', 'bar')
print cache

rawDescription: Python elements retrieved from container (list, dict, tuple etc) passed by reference or value?


code:
from bs4 import BeautifulSoup
import mechanize

url = "http://web2.nyrrc.org/cgi-bin/start.cgi/mar-programs/archive/archive_search.html"

browser = mechanize.Browser()
browser.addheaders = [
    ('user-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'),
    ('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
]
browser.set_handle_robots(False)

# get years
browser.open(url)
browser.form = list(browser.forms())[0]
select = browser.form.controls[3]
years = [item.name for item in select.get_items()]

# process year by year
for year in years:
    print "Processing year=" + year

    # open up search form again, fill out an appropriate year and submit
    browser.open(url)
    browser.form = list(browser.forms())[0]
    select = browser.form.controls[3]

    browser.form['input.searchyear'] = [year]
    browser.form['search.method'] = ['search.flat']
    browser.submit()

    # get overall winners
    soup = BeautifulSoup(browser.response())
    print soup.find(text='Overall Winners').parent.parent.parent.find_next_sibling('tr').find('pre').text

rawDescription: Webscraping with BeautifulSoup - NYC Marathon Times


code:
import re

data = """
DataStore.prime('ws-stage-stat',
{ against: 0, field: 2, stageId: 9155, teamId: 26, type: 8 },
[[['goal','fastbreak','leftfoot',[1]],['goal','openplay','leftfoot',[2]],
['goal','openplay','rightfoot',[1]],['goal','owngoal','leftfoot',[1]],
['goal','penalty','rightfoot',[1]],['miss','corner','header',[6]],
['miss','corner','leftfoot',[2]],['miss','corner','rightfoot',[2]],
['miss','crossedfreekick','header',[1]],['miss','openplay','header',[4]],
['miss','openplay','leftfoot',[11]],['miss','openplay','rightfoot',[27]]]]
"""

# parse js
pattern = re.compile("\[([^\[]+?),\[(\d+)\]\]")

print pattern.findall(data)

rawDescription: How to parse this JSON array of arrays (I think)

description: Parse this JSON array of arrays (I think)


code: print [f for f in files2 if f not in files1]

rawDescription: Python: Compare two directories with different structure and list new files


code:
# main_program.py
import globals

def modify_klaatu():
    print globals.klaatu
    globals.klaatu = "It was the best of times, it was the worst of times."

rawDescription: Truly Global Variable in python

description: Truly Global Variable in python


code:
class A(object):
    clsVar = 'a'
    @classmethod
    def clsMeth(cls):
        print 'changing clsVar'
        cls.clsVar = 'b'
    A.clsMeth()

rawDescription: Python Class Variable Initialization

description: Python Class Variable Initialization


code:
first_tuple = out[0]
print first_tuple

rawDescription: I need to read in a text file with data into python and assign the data the variables


code:
a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much http and com too']
b = ['com', 'http', 'net']

print set(a)-set([i for i in a for j in b if j not in i])

rawDescription: match any of string in list b with list a. a is a long sentences

description: Match any of string in list b with list a. a is a long sentences


code:
import gobject, glib, gtk

counter = 0

def callback(*args):
    global counter
    counter += 1
    print 'callback called', counter
    if counter > 10:
        print 'last call'
        return False

    return True

def terminate(*args):
    print 'Bye bye'
    loop.quit()

gobject.timeout_add(100, callback)
gobject.timeout_add(3000, terminate)
loop = glib.MainLoop()
loop.run()

rawDescription: GTK main blocks -- Python

description: GTK main blocks -- Python


code:
import operator

print "1) Add"
print "2) Substract"
print "3) Multiply"
print "4) Divide"
print "5) Exit"
while True:
    x=input("Choose an operation: ")
    if x==5:
        break
    y=input("How many numbers do you need to operate: ")
    operands=[input('Value {}'.format(i+1)) for i in range(count)]
    if x==1:
        op, value = operator.add, 0
    elif x==2:
        op, value = operator.sub, 0
    elif x==3:
        op, value = operator.mul, 1
    elif x==4:
        op, value = operator.truediv, 1
    for operand in operands:
        value = op(value, operand)
    print value

rawDescription: How to go back to a specific line

description: Go back to a specific line


code:
for i in threading.enumerate():
   if i.name == "MainThread":
       print i.is_alive()

rawDescription: Check if the Main Thread is still alive from another thread


code:
import h5py
import numpy as np

data = []
with h5py.File("data.mat") as f:
    for column in f['test']:
        row_data = []
        for row_number in range(len(column)):
            row_data.append(''.join(map(unichr, f[column[row_number]][:])))
        data.append(row_data)

print data
print np.transpose(data)

rawDescription: Reading a Matlab's cell array saved as a v7.3 .mat file with H5py


code:
from multiprocessing import Process, Manager
import time
import itertools

def do_work(in_queue, out_list):
    while True:
        item = in_queue.get()
        line_no, line = item

        # exit signal
        if line == None:
            return

        # fake work
        time.sleep(.5)
        result = (line_no, line)

        out_list.append(result)

if __name__ == "__main__":
    num_workers = 4

    manager = Manager()
    results = manager.list()
    work = manager.Queue(num_workers)

    # start for workers
    pool = []
    for i in xrange(num_workers):
        p = Process(target=do_work, args=(work, results))
        p.start()
        pool.append(p)

    # produce data
    with open("source.txt") as f:
        iters = itertools.chain(f, (None,)*num_workers)
        for num_and_line in enumerate(iters):
            work.put(num_and_line)

    for p in pool:
        p.join()

    # get the results
    # example:  [(1, "foo"), (10, "bar"), (0, "start")]
    print sorted(results)

rawDescription: Processing single file from multiple processes in python


code:
df['Tenant'].replace('', np.nan, inplace=True)
print df

rawDescription: Python Pandas DataFrame remove Empty Cells


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('

', "html5")
new_tag = soup.new_tag('img', src='/some/url/here')
new_tag
soup.div.append(new_tag)
print soup.prettify()

rawDescription: BeautifulSoup create a <img /> tag

description: BeautifulSoup create a <img /> tag


code:
if any(data.n == 5 for data in myList):
    print "Found it"

rawDescription: Difference between two "contains" operations for python lists


code:
for line in iter(process.stdout.readline,""):
   print line

rawDescription: Read stdout from subprocess until there is nothing left


code:
for line in r.splitlines():
    if "REQUIRES" in line:
          print line,

rawDescription: How get line from web site with Python

description: How get line from web site with Python


code:
if product == 512:
    #use `map` to turn the factors into strings, first
    print "factors: " + ",".join(map(str, factors))

rawDescription: Simplify Python iterations

description: Simplify Python iterations


code: print re.match(pattern, "10\xbd")

rawDescription: The same regular expression doesn't work on my local environment


code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Abhishek Bhatia's data & scatter plot.
x = np.array([  29.,   36.,    8.,   32.,   11.,   60.,   16.,  242.,   36.,
               115.,    5.,  102.,    3.,   16.,   71.,    0.,    0.,   21.,
               347.,   19.,   12.,  162.,   11.,  224.,   20.,    1.,   14.,
                 6.,    3.,  346.,   73.,   51.,   42.,   37.,  251.,   21.,
               100.,   11.,   53.,  118.,   82.,  113.,   21.,    0.,   42.,
                42.,  105.,    9.,   96.,   93.,   39.,   66.,   66.,   33.,
               354.,   16.,  602.])
y = np.array([ 30,  47, 115,  50,  40, 200, 120, 168,  39, 100,   2, 100,  14,
               50, 200,  63,  15, 510, 755, 135,  13,  47,  36, 425,  50,   4,
               41,  34,  30, 289, 392, 200,  37,  15, 200,  50, 200, 247, 150,
              180, 147, 500,  48,  73,  50,  55, 108,  28,  55, 100, 500,  61,
              145, 400, 500,  40, 250])
fig = plt.figure()
ax=plt.gca()
ax.scatter(x,y,c="blue",alpha=0.95,edgecolors='none', label='data')
ax.set_yscale('log')
ax.set_xscale('log')

newX = np.logspace(0, 3, base=10)  # Makes a nice domain for the fitted curves.
                                   # Goes from 10^0 to 10^3
                                   # This avoids the sorting and the swarm of lines.

# Let's fit an exponential function.
# This looks like a line on a lof-log plot.
def myExpFunc(x, a, b):
    return a * np.power(x, b)
popt, pcov = curve_fit(myExpFunc, x, y)
plt.plot(newX, myExpFunc(newX, *popt), 'r-',
         label="({0:.3f}*x**{1:.3f})".format(*popt))
print "Exponential Fit: y = (a*(x**b))"
print "\ta = popt[0] = {0}\n\tb = popt[1] = {1}".format(*popt)

# Let's fit a more complicated function.
# This won't look like a line.
def myComplexFunc(x, a, b, c):
    return a * np.power(x, b) + c
popt, pcov = curve_fit(myComplexFunc, x, y)
plt.plot(newX, myComplexFunc(newX, *popt), 'g-',
         label="({0:.3f}*x**{1:.3f}) + {2:.3f}".format(*popt))
print "Modified Exponential Fit: y = (a*(x**b)) + c"
print "\ta = popt[0] = {0}\n\tb = popt[1] = {1}\n\tc = popt[2] = {2}".format(*popt)

ax.grid(b='on')
plt.legend(loc='lower right')
plt.show()

rawDescription: Log log plot linear regression

description: Log log plot linear regression


code: print fooPy()

rawDescription: assigning value to shell variable using a function return value from Python

description: Assigning value to shell variable using a function return value from Python


rawDescription: Filter special chars such as color codes from shell output


code:
s1 = 'DOMA A\r\nName: Ryan\r\nBest: 1\r\nAlias: 3K\r\nLocation: Eng\r\nGame Wins: 51\r\nTime: 09:10:50'
s2 = 'DOMA A\r\nName: Ryan\r\nBest: 1\r\nAlias: 3K\r\nLocation: Eng\r\nGame Wins: 51\r\nTime: 09:10:50\r\nREF: Yes'

print "s1: ", pattern6.findall( re.sub('\r\n', '\n', s1) )
print "s2: ", pattern6.findall( re.sub('\r\n', '\n', s2) )

rawDescription: Regex pattern to stop only accept what's left on that line


rawDescription: Python - sort lists based on their sum

description: Python - sort lists based on their sum


code:
for retval in quux():
    print retval.foo, retval.bar

rawDescription: Return iterable of unpackable values that won't break client code if I start returning more data


code:
L = [1, 1, 3, 5, 7, 5, 1, 2, 1, 1, 1, 8, 9, 1, 1, 1]

def peak_groups(l):
    start_i = 0
    peak_i = None
    for i,x in enumerate(l):
        if peak_i is None:
            # look for start of peak group, or peak itself
            if x < 2:
                start_i = i
            elif x > 6:
                peak_i = i
        else:
            # look for end of peak group
            if x < 2:
                yield (start_i, peak_i, i)
                start_i = i
                peak_i = None

    # finally check edge condition if we reached the end of the list
    if peak_i is not None:
        yield (start_i, peak_i, i)

for group in peak_groups(L):
    print group

rawDescription: Python - finding next and previous values in list with different criteria


code:
import re
re.match(ur'^(?P<search_word>[a-zA-Z]*)/?$', 'testString/')
re.match(ur'^(?P<search_word>[a-zA-Z]*)/?$', 'test-String/')  # does not match 'test-String/' because of the hyphen
re.match(ur'^(?P<search_word>[a-zA-Z]*)/?$', '')  # also matches empty string ''

rawDescription: url dispatcher detect all and nothing with regexp

description: Url dispatcher detect all and nothing with regexp


code:
for out in gen_group(l0):
    print out

rawDescription: How can I group a list of objects by continuity?

description: Group a list of objects by continuity


code: print methods_in_order(Foo())

rawDescription: Is there a way to programatically determine the order in which functions are declared in a python module?


code:
def peek(f, length=1):
    pos = f.tell()
    data = f.read(length) # Might try/except this line, and finally: f.seek(pos)
    f.seek(pos)
    return data

print peek(f, 4) # cat1
print peek(f, 4) # cat1

rawDescription: Reading a Line From File Without Advancing [Pythonic Approach]


code:
import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.pow(2,3)  # Returns 2**3 = 8
print s.add(2,3)  # Returns 5
print s.div(5,2)  # Returns 5//2 = 2

# Print list of available methods
print s.system.listMethods()

rawDescription: Receiving XML document with python via HTTP


code:
for section in load_sections(filename):
    print section

rawDescription: Parsing data from text file

description: Parsing data from text file


code:
def reshape(t):
    out = []
    out.append(t[0])
    out.append(t[1])
    for v in brc.value:
        if t[2] == v:
            out.append(t[3])
        else:
            out.append(0)
    return (out[0],out[1]),(out[2],out[3],out[4],out[5])
def cntryFilter(t):
    if t[2] in brc.value:
        return t
    else:
        pass

def addtup(t1,t2):
    j=()
    for k,v in enumerate(t1):
        j=j+(t1[k]+t2[k],)
    return j

def seq(tIntrm,tNext):
    return addtup(tIntrm,tNext)

def comb(tP,tF):
    return addtup(tP,tF)

countries = ['CA', 'UK', 'US', 'XX']
brc = sc.broadcast(countries)
reshaped = calls.filter(cntryFilter).map(reshape)
pivot = reshaped.aggregateByKey((0,0,0,0),seq,comb,1)
for i in pivot.collect():
    print i

rawDescription: Reshaping/Pivoting data in Spark RDD and/or Spark DataFrames


rawDescription: Write a column next to list in csv_python


code: print "\n".join(fullText.splitlines())

rawDescription: Preserve line breaks when converting string to list and then back again with join


code:
from collections import Counter
import csv

def read_csv(fn):
    with open(fn, 'r') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        reader.next()    # Skip header
        for row in reader:
            ip, bytes = row
            yield ip, int(bytes)

totals = Counter()
for ip, bytes in read_csv('data.txt'):
    totals[ip] += bytes

print totals

rawDescription: Python dictionary sum

description: Python dictionary sum


code:
command = "lst = [i**2 for i in range({})]"
exec command.format(10)  #Performs identically to lst = [i**2 for i in range(10)]
lst
exec command.format(4)   #Performs identically to lst = [i**2 for i in range(4)]
lst

rawDescription: Is there a good standard for adding data to a string if already saved as a str object


code: print np.convolve(a, np.ones(3) / 3, mode='valid')

rawDescription: Fast 1D convolution with finite filter and sum of dirac deltas in python


code:
from bs4 import BeautifulSoup

html="""

headline 1

text

text

headline 2

text

"""

soup = BeautifulSoup(html)

for h2 in soup.findAll("h2"):
    node = h2.next_sibling

    print "This h2", h2

    while node is not None and node.name != "h2":
        if node.name == "p":
            print node
        node = node.next_sibling

rawDescription: How to determine a logic part with python's beautiful soup when crawling

description: Determine a logic part with python's beautiful soup when crawling


code:
count = len(l)
def countdownList(l):  # 3. prints number of files left to process
    global count
    count = count - 1
    print count, " files left to go."
    if count == 0:
        print "Finished!"

rawDescription: trying to count down from a number, not working

description: Trying to count down from a number, not working


code:
from subprocess import Popen, PIPE, STDOUT
cmd = 'rsync --rsh=ssh -rv thisdir/ servername:folder/'
p = Popen(cmd.split(), stdout=PIPE, stderr=STDOUT)
for line in p.stdout:
    print line

rawDescription: printing stdout in realtime from subprocess

description: Printing stdout in realtime from subprocess


code:
import re

t = "http://www.egg1.com http://egg2.com egg3 egg4"

p = re.compile('(http://\S+)|(egg\d)')
for url, egg in p.findall(t):
  if egg:
    print egg

rawDescription: Python regex not to match http://

description: Python regex not to match http://


code:
import time
if __name__ == "__main__":
    url = "http://ftp.postgresql.org/pub/source/v9.2.3/postgresql-9.2.3.tar.gz"
    down = Downloader()
    down.download(url, "file")
    print "Download started..."
    time.sleep(5)
    down.cancel()
    print "Download canceled"

rawDescription: downloading a file in python and cancel

description: Downloading a file in python and cancel


code:
memdata.execute("select * from tabletwo where tabletwo.Gene_ID == 'NFYA'")
nfya_row = {desc[0]: column for desc, column in zip(memdata.getdescription(), memdata.next())}

memdata.execute("SELECT tableone.SampleID from tableone WHERE tableone.Diagnos=='RH'")
for row in memdata:
    sampleID = row[0]
    print sampleID, nfya_row[sampleID]

rawDescription: sql, matching row values obtained from first table to the column names of second table without loop

description: Sql, matching row values obtained from first table to the column names of second table without loop


code:
y = x.reshape(3,3)
print y.strides

rawDescription: Efficient Numpy 2D array construction from 1D array


code:
def hamming(s1, s2):
    #Return the Hamming distance between equal-length sequences
    if len(s1) != len(s2):
        raise ValueError("Undefined for sequences of unequal length")
    return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))

for x in Bio.SeqIO.parse("reads.fastq","fastq"):
        reads_array.append(x)

nmer = 7

l_chosen = ['gttattt','attattt','tgctagt']
chosen_reads = []

for x in reads_array:
        s2 = str(x.seq)
        breakFlag = False
        for s in [s2[i:i+nmer] for i in range(len(s2)-nmer-1)]:
                for ds in l_chosen:
                        dist = hamming(ds,s)
                        if dist == 0:
                                print s2, s,ds,dist
                                chosen_reads.append(x)
                                breakFlag = True
                                break;
                if breakFlag:
                        break;

rawDescription: choosing reads with Hamming distance zero

description: Choosing reads with Hamming distance zero


code:
import subprocess
null = open(os.devnull, "w")
grep = subprocess.Popen(shlex.split("grep -i oleg /etc/passwd"), stderr = null, stdout = null)
grep.communicate()
print grep.returncode
grep = subprocess.Popen(shlex.split("grep -i igor /etc/passwd"), stderr = null, stdout = null)
grep.communicate()
print grep.returncode

rawDescription: How to skip printing the command output and just get the return value from a os.system command?

description: Skip printing the command output and just get the return value from a os.system command


code: print getattr(args, 'lambda')

rawDescription: Python: How to escape 'lambda'

description: Python: How to escape 'lambda'


code:
from urlparse import urljoin
from bs4 import BeautifulSoup
import requests

base_url = 'https://malwr.com/'
url = 'https://malwr.com/account/login/'
username = 'username'
password = 'password'

session = requests.Session()

# getting csrf value
response = session.get(url)
soup = BeautifulSoup(response.content)

form = soup.form
csrf = form.find('input', attrs={'name': 'csrfmiddlewaretoken'}).get('value')

# logging in
data = {
    'username': username,
    'password': password,
    'csrfmiddlewaretoken': csrf
}
session.post(url, data=data)

# getting analysis data
response = session.get('https://malwr.com/analysis/MDMxMmY0NjMzNjYyNDIyNDkzZTllOGVkOTc5ZTQ5NWU/')
soup = BeautifulSoup(response.content)

link = soup.find('section', id='file').find('table')('tr')[-1].a.get('href')
link = urljoin(base_url, link)
print link

rawDescription: Not able to login to https site(https://malwr.com) through python script


code:
from random import randrange
ds = [dict((randrange(1, 1000), randrange(1, 1000)) for i in xrange(500))
      for i in xrange(10000)]

# 10000 dictionaries of approx. length 500

from collections import defaultdict

def merge1(dicts, defaultdict=defaultdict, int=int):
    merged = defaultdict(int)
    for d in dicts:
        for k in d:
            merged[k] += d[k]
    return merged

def merge2(dicts):
    merged = {}
    merged_get = merged.get
    for d in dicts:
        for k in d:
            merged[k] = merged_get(k, 0) + d[k]
    return merged

def merge3(dicts):
    merged = {}
    for d in dicts:
        for k in d:
            merged[k] = merged[k] + d[k] if k in merged else 0
    return merged

from timeit import timeit
for func in ('merge1', 'merge2', 'merge3'):
    print func, timeit(stmt='{0}(ds)'.format(func),
                       setup='from __main__ import merge1, merge2, merge3, ds',
                       number=1)

rawDescription: Fastest way to merge n-dictionaries and add values on 2.6


code:
# fill missing indexes in the dictionary:
for i in range(min(sequenceDict), max(sequenceDict)):
    if i not in sequenceDict:
        sequenceDict[i] = []

# get only lists, ordered:
sortedItems = map(lambda x:x[1], sorted(sequenceDict.items(), key=lambda x:x[0]))
globalHotItems = {} # (value, startIndex): length
currentHotItems = {} # value: length

for i in range(len(sortedItems)):
    updatedHotItems = {} # updated value: length
    for item in sortedItems[i]:
        if (item - 1) in currentHotItems:
            updatedHotItems[item] = currentHotItems[item-1] + 1
        else:
            updatedHotItems[item] = 1

    deadSet = set(currentHotItems.keys()) - \
            set(updatedHotItems.keys() + [key - 1 for key in updatedHotItems.keys()])

    for item in deadSet:
        globalHotItems[ (item-currentHotItems[item]+1, i-currentHotItems[item]) ] = currentHotItems[item]

    currentHotItems = updatedHotItems

print sorted(globalHotItems.items(), key=lambda x:x[1])[-1]

rawDescription: Most efficient way to find longest incrementing subsequence in a list of lists


code:
def barFighters( self ):
    print "barFighters"
a.barFighters = barFighters
a.barFighters()

rawDescription: Adding a Method to an Existing Object Instance


code:
from nltk.model import NgramModel
from nltk.corpus import brown
from nltk.probability import LaplaceProbDist

est = lambda fdist: LaplaceProbDist(fdist)

corpus = brown.words(categories='news')[:100]
lm = NgramModel(3, corpus, estimator=est)

print lm
print (corpus[8], corpus[9], corpus[12] )
print (lm.prob(corpus[12], [corpus[8], corpus[9]]) )
print

rawDescription: How to pass in an estimator to NLTK's NgramModel?

description: Pass in an estimator to NLTK's NgramModel


code:
import re
for inputstr in ('RequestReadTimeout header=1234-40,MinRate=XXX body=XXX', 'RequestReadTimeout header=40 body=XXX'):
    print re.search(r'header=(?:\d*-)?(\d+)', inputstr).group(1)

rawDescription: Regex to find specific number using Python regex


code:
a = [(1, 5), (4, 2), (4, 3), (5, 4), (6, 3), (7, 6), (8, 9)]
# a = [(1, 5), (4, 2), (4, 3), (5, 4), (6, 3), (7, 6)]

d = {tuple(t): set(t) for t in a}  # forces keys to be unique

while True:
    for tuple_, set1 in d.items():
        try:
            match = next(k for k, set2 in d.iteritems() if k != tuple_ and set1 & set2)
        except StopIteration:
            # no match for this key - keep looking
            continue
        else:
            print 'merging', tuple(set1), match
            d[tuple_] = set1 | d.pop(match)
            break
    else:
        # no match for any key - we are done!
        break

output = sorted(tuple(s) for s in d.itervalues())
print output

rawDescription: Find sets of disjoint sets from a list of tuples or sets in python


rawDescription: Converting incorrectly encoded Chinese characters in MySQL to UTF-8


code:
def print_double(x):
    print "%s doubled is %s" % (x, x * 2)
def print_square(x):
    print "%s squared is %s" % (x, x * x)
cb = CallbackList()
cb.append(print_double)
cb.append(print_square)
cb.fire(8)

rawDescription: More pythonic way to manage a list of callbacks


code: print '%+d' % score

rawDescription: How to print +1 in Python, as +1 (with plus sign) instead of 1?

description: Print +1 in Python, as +1 (with plus sign) instead of 1


code:
import re

line = "2015/04/13.16:26:07 156.0 GB of instance data copied, dev_iosecs 1887, dev_iorate 88.8 MB/s"

if re.search("of instance data copied", line):
    m = re.search("(?P<datasize>\d[.][\d]) (?P<units>TB|GB|MB|KB) of instance data copied", line)
    print m.group('datasize'), m.group('units')
    if m.group('units') == "GB":
        print "MATCH!!!!!"

rawDescription: Having trouble with re and matching groups


code:
# Get a list of all visible files rooted in the 'path/to/the/dir' directory
print list(filterls('path/to/the/dir', lambda p: not p.startswith('.')))

rawDescription: Copying files recursively with skipping some directories in Python?


code:
from xml.parsers.expat import ParserCreate

def char_data(data):
    if data.strip(): # skip empty text if you want
        print data

parser = ParserCreate()
parser.CharacterDataHandler = char_data
parser.Parse(doc,True)

rawDescription: Remove all XML tags, only keep the text between the tags


code:
from collections import OrderedDict
import json
print json.dumps(OrderedDict(table_data))

rawDescription: Convert a HTML Table to JSON

description: Convert a HTML Table to JSON


rawDescription: String regex two mismatches Python

description: String regex two mismatches Python


code:
print next(i for i in range(10) if i**2 == 9)
print next(i for i in range(10) if i**2 == 17)
print next((i for i in range(10) if i**2 == 17), None)

rawDescription: Default value for next element in Python iterator if iterator is empty?


code:
def iset(iterable):
    for i, x in enumerate(iterable):
        def setter(value):
            iterable[i] = value
        yield setter, x
a = range(10)
for set_x, x in iset(a):
    set_x(x * 2)
print a

rawDescription: How to modify elements of iterables with iterators? I.e. how to get write-iterators in Python?

description: Modify elements of iterables with iterators? I.e. how to get write-iterators in Python


code:
from collections import Counter

with open('counts.txt') as f:
    lines = [line.strip().split() for line in f]
    wordCounts = Counter({line[1]: int(line[2]) for line in lines[1:]})
    print wordCounts.most_common(3)

rawDescription: Python Counter from txt file

description: Python Counter from txt file


code:
class SettingsForm(forms.ModelForm):

    class Meta:
        model = SettingProxy
        exclude = ('domain',)

    def save(self, domain, *args, **kwargs):
        print self.cleaned_data
        commit = kwargs.get('commit', True)
        kwargs['commit'] = False
        setting = super(SettingsForm, self).save(*args, **kwargs)
        setting.domain = domain
        if commit:
            setting.save()
        return setting

rawDescription: Store form fields as key-values / individual rows


code:
issues = ET.fromstring(data)
total = issues.find('./paging/total')
print total.text  # prints 10000

rawDescription: Python minidom Trouble obtaining value of child node


code:
def test_getWordScore():
    """
    Unit test for getWordScore
    """
    failure=False
    # dictionary of words and scores
    words = {("", 7):0, ("it", 7):4, ("was", 7):18, ("scored", 7):54, ("waybill", 7):155,
    ("outgnaw", 7):127, ("fork", 7):44, ("fork", 4):94}
    for (word, n) in words.keys():
        score = getWordScore(word, n)
        if score != words[(word, n)]:
            print "FAILURE: test_getWordScore()"
            print "\tExpected", words[(word, n)], "points but got '" \
            + str(score) + "' for word '" + word + "', n=" + str(n)
            failure=True
    if not failure:
        print "SUCCESS: test_getWordScore()"

import ps4a
ps4a.wordList = loadWords()

print "----------------------------------------------------------------------"
print "Testing getWordScore..."
test_getWordScore()

rawDescription: Why I get "global name ... is not defined"?


code:
def func(lis):
    return ''.join(lis).translate(None, ':'+whitespace)

lis = [x.split() for x in re.split(r'(?<=\w):',ph.strip(), re.DOTALL)]
dic = {}
for x, y in zip(lis[:-1], lis[1:-1]):
    dic[x[-1]] = func(y[:-1]).split(',')
dic[lis[-2][-1]] = func(lis[-1]).split(',')

print dic['List']
print dic['Members']
print dic['alias']

rawDescription: capturing the usernames after List: tag

description: Capturing the usernames after List: tag


code:
import random
def foo(x):
    print x
    return x

from functools import partial
print random.choice((partial(foo,'spam'), partial(foo,'eggs')))()

rawDescription: Avoid or delay evaluation of things which may not be used


code:
def square(x):
    return x*x

def encapsulated():
    from os.path import join

    def cube(x):
        return x**3

    l = []
    for key, value in locals().items():
        if callable(value) and value.__module__ == __name__:
            l.append(key)
    print l

encapsulated()

rawDescription: Getting a list of locally-defined functions in python


code:
from itertools import groupby

def sub(x):
    return x[1] - x[0]

a = [5, 3, 7, 4, 1, 2, 9, 10]
ranges = []
for k, iterable in groupby(enumerate(sorted(a)), sub):
     rng = list(iterable)
     if len(rng) == 1:
         s = str(rng[0][1])
     else:
         s = "%s-%s" % (rng[0][1], rng[-1][1])
     ranges.append(s)
print ranges

rawDescription: How to do an inverse `range`, i.e. create a compact range based on a set of numbers?

description: Do an inverse `range`, i.e. create a compact range based on a set of numbers


code:
contig="CCCCAAAACCCCAAAACCCCAAAACCCCTAcGAaTCCCcTCATAATTGAAAGACTTAAACTTTAAAACCCTAGAAT"
splitbase="CCCCAAAA"
halfBase="CCCC"
splittedContig=contig.split(splitbase)
cnt=len(splittedContig)-1
print cnt+sum([0.5 for e in splittedContig if e.startswith(halfBase)])

rawDescription: Counting DNA sequences with python/biopython


rawDescription: Adding numbers within object

description: Adding numbers within object


rawDescription: converting character or integer to md5 hash using python script

description: Converting character or integer to md5 hash using python script


code:
from datetime import datetime

date_object = datetime.strptime('16/07/2014 13:00:00', '%d/%m/%Y %H:%M:%S')
print date_object

rawDescription: Convert string datetime to timestamp and vice verse in python


code:
import random
import wx

########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        lbox = wx.ListBox(self, choices=colors)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbox, 0, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tab_num = 3

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)

        btn = wx.Button(panel, label="Get Color")
        btn.Bind(wx.EVT_BUTTON, self.addPage)
        sizer.Add(btn)

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def addPage(self, event):
        """"""
        page = self.notebook.GetCurrentPage()
        for widget in page.GetChildren():
            if isinstance(widget, wx.ListBox):
                selection = widget.GetString(widget.GetSelection())
                if selection:
                    print "Current color = " + selection
                else:
                    print "No color selected"

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

rawDescription: wxPython Acting on Element with Focus

description: WxPython Acting on Element with Focus


rawDescription: How to parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly?

description: Parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly


code:
import timeit
import numpy as np
import cython_bspline as CB
import numpy_bspline as NB

c = 6
n = 10**5
d = 3

fb = NB.fitpack_basis(c, n, d)
bb = NB.bspline_basis(c, n, d)
cfb = CB.cython_fitpack_basis(c,n,d)

assert np.allclose(bb, fb)
assert np.allclose(cfb, fb)
# print(NB.fitpack_basis(c,5,d,rMinOffset=-0.1,rMaxOffset=.2))

timing = dict()
timing['NB.fitpack_basis'] = timeit.timeit(
    stmt='NB.fitpack_basis(c, n, d)',
    setup='from __main__ import NB, c, n, d',
    number=10)
timing['NB.bspline_basis'] = timeit.timeit(
    stmt='NB.bspline_basis(c, n, d)',
    setup='from __main__ import NB, c, n, d',
    number=10)
timing['CB.cython_fitpack_basis'] = timeit.timeit(
    stmt='CB.cython_fitpack_basis(c, n, d)',
    setup='from __main__ import CB, c, n, d',
    number=10)

for func_name, t in timing.items():
    print "{:>25}: {:.4f}".format(func_name, t)

rawDescription: How to get the spline basis used by scipy.interpolate.splev

description: Get the spline basis used by scipy.interpolate.splev


code:
from collections import Counter

if Counter(s1) == Counter(s2):
    print "Anagram!"
else:
    print "Not Anagram!"

rawDescription: Finding anagrams in Python

description: Finding anagrams in Python


code:
myvariable = 5
def func():
    myvariable = 6    #creates a new "local" variable.
                      #Doesn't affect the global version
    print myvariable  #prints 6

func()
print myvariable   #prints 5

rawDescription: In Python what is a global statement?

description: In Python what is a global statement


code:
lcount = 1
with open("file", "r") as f:
    for line in f:
        if word in line:
            testline = line.split()
            ind = testline.index("sparrow")
            print "Word sparrow found at line %d, word %d" % (lcount, ind+1)
            break
        else:
            lcount += 1

rawDescription: How to know a position (.txt)

description: Know a position (.txt)


code:
def f():
    print "the reactor is running now"

reactor.callWhenRunning(f)

rawDescription: sound way to feed commands to twisted ssh after reactor.run()

description: Sound way to feed commands to twisted ssh after reactor.run()


code: print format_serialized(query, 0, [])

rawDescription: Converting dict to list

description: Converting dict to list


code:
a_list = [[0] * 10] * 10
for n in a_list:
    print id(n)

rawDescription: Python list confusion

description: Python list confusion


code:
l = [1, 7, 3, 5]
for first, second in zip(l, l[1:]):
    print first, second

rawDescription: Iterate over all pairs of consecutive items from a given list


code:
amount = 1000
people = 3
average = float(amount)/float(people)
print average

rawDescription: Decimal not getting while dividing

description: Decimal not getting while dividing


code:
class FTIRdataClient(object):

    def __init__(self,TCP_IP="xxx.xxx.xx.xxx",TCP_Port=xxxx,BufferSize=4024):

        #-----------------------------------
        # Configuration parameters of server
        #-----------------------------------
        self.TCP_IP          = TCP_IP
        self.TCP_Port        = int(TCP_Port)
        self.RECV_BUFFER     = int(BufferSize)

    def setParam(self,message):
        try:
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            sock.connect((self.TCP_IP,self.TCP_Port))
            sock.sendall("set "+message)

            #-------------------------
            # Loop to recieve all data
            #-------------------------
            incommingTotal = ""
            while True:
                incommingPart = sock.recv(self.RECV_BUFFER)
                if not incommingPart: break
                incommingTotal += incommingPart

            sock.close()
        except:
            print "Unable to connect to data server!!"
            incommingTotal = False

        return incommingTotal

rawDescription: Incomplete data transfer over python socket


code:
tree = ET.parse("file.xml")
root = tree.getroot()
text = ""
for n in root.findall(".//autor"):
    text += n.text
    text += n.tail
    for x in n:
        text += x.text
        text += x.tail

print text

rawDescription: Extracting text from a xml file

description: Extracting text from a xml file


code:
from itertools import product
for a, b, c, d in product(range(95), range(95), range(95), range(95)):
    print a, b, c, d
    if a == 1: break

rawDescription: How to break nested for loop in Python?

description: Break nested for loop in Python


code:
cb = numpy.zeros(size,dtype='i')
totals = numpy.zeros(size,dtype='i')

data = rank
for i in range(3):
    cb[rank] = cb[rank] + data

comm.Reduce([cb,MPI.INT], [totals,MPI.INT], op=MPI.SUM, root=0)

if rank == 0:
    print "result is", totals

rawDescription: Ensuring two messages come from the same MPI task


code:
import collections

def search_student(database=None, ID=None, **kwargs):
    if ID is None or ID == 0:
        print 'Provide the arguments properly'
        return False
    get = database.get(ID)
    if get is not None:
        print 'Student Name: {}'.format(get.name)
        for k in kwargs:
            print("Student {}: {}".format(k, getattr(get,k)))
    else:
        return "User does not exist in database"

studentDatabase = collections.namedtuple("student", "id name roll_no phone email")

DATABASE = {1: studentDatabase(1, 'Mayukh Sarkar', 9, '555-2312', 'mayukh2012@hotmail,com'),
            2: studentDatabase(2, 'Alisha Sengupta', 7, '555-1345', 'alisha@gmail.com')}

search_student(DATABASE,1,roll_no=True,email=True)

rawDescription: How can I pass the optional parameter of a namedtuple in a function

description: Pass the optional parameter of a namedtuple in a function


code:
ip="192.168.0.100"
reps= {'application.baseUrl=': """application.baseUrl="http://"""+ip+""":9000""",
       'baseUrl=': """baseUrl="http://"""+ip+""":9000""",
       'hostname': """hostname = \""""+ip+'"',
       'port': 'port = 8888'}

f = open('/opt/presentation/conf/application.conf','r').read()
lines = f.split("\n")
newConf = ""
for line in lines:
    REPLACED = False
    print "checking line: [%s]"%line
    for key in reps.keys():
        if key in line:
            print "replacing [%s] with [%s]"%(line, reps[key])
            #maintaing white spacing
            count = line.index(key[0])

            l = "%s%s\n"%(" "*count,reps[key])
            REPLACED = True

    if REPLACED == True:
        newConf += l
    else:
        newConf += "%s\n"%line

new_conf_file = open('/opt/presentation/conf/application.conf','w')
new_conf_file.write(newConf)
new_conf_file.close()

rawDescription: Python search and replace in configuration file


code:
import cv

path = 'stack.png'
mat = cv.LoadImageM(path, cv.CV_LOAD_IMAGE_UNCHANGED)
x, y = 42, 6
print type(mat)
print mat[y, x]

rawDescription: Opencv... getting at the data in an IPLImage or CvMat


code:
di = DictRange()
di[ xrange(0,10) ] = "A"
di[ xrange(11,20) ] = "B"
di[ xrange(21,30) ] = "C"

print di[24]
# C
print di[6]
# A

rawDescription: Associative array in python with keys as a fragment of array


code:
# and iterate all Property nodes, and get their attributes like this
for p in tree.findall('./Process[1]/Properties/Property'):
    print p.attrib # to get code/value, use p.attrib.get('code') etc.

rawDescription: Parsing an XML file using ElementTree

description: Parsing an XML file using ElementTree


code:
x = 10
y = 20

class dump_into_namespace:
    def __init__(self, env, *vars):
        self.vars = dict([(x, env[x]) for v in vars for x in env if v is env[x]])
    def __getattr__(self, name): return self.vars[name]

o = dump_into_namespace(locals(), x, y)
print o.x, o.y

rawDescription: Easily dumping variables from/to namespaces/dictionaries in Python


code:
for c in account:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

for c in domain:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

rawDescription: Filtering string in Python

description: Filtering string in Python


code:
import pyHook

def mouseUp(event):
    if event.Injected: return True

    print "Mouse went up"
    return True

hookManager = pyHook.HookManager()
hookManager.MouseLeftUp = mouseUp
hookManager.HookMouse()

rawDescription: python+win32: detect window drag

description: Python+win32: detect window drag


code:
import os
import rpm
import sys

ts = rpm.TransactionSet()
fd = os.open(sys.argv[1], os.O_RDONLY)
h = ts.hdrFromFdno(fd)
os.close(fd)

for dep in h[rpm.RPMTAG_REQUIRENAME]:
    print dep

rawDescription: Find rpm dependencies using python yum/rpm API?


code:
for k, v in data["clusters"].items():
    if data["clusters"][k].has_key("tag_cl"):
        print data["clusters"][k]["tag_cl"]

rawDescription: Get yaml key value in python

description: Get yaml key value in python


code:
from ctypes import *
test = cdll.LoadLibrary("test")
test.function_test.argtypes = [
    c_int, POINTER(c_double), POINTER(c_int)
]

def Py_function_test(arraysize):
    _c_readingarray = (c_double*arraysize)()
    _c_actualarraysize = c_int()
    test.function_test(arraysize, _c_readingarray, byref(_c_actualarraysize))
    return _c_readingarray, _c_actualarraysize.value

array, size = Py_function_test(10)
print list(array), size

rawDescription: How to return an array in ctyps when calling a function

description: Return an array in ctyps when calling a function


code: print tuple(max(lst) for lst in scores[1:3])

rawDescription: Max() returns highest list, not number

description: Max() returns highest list, not number


code:
import csv
import urllib2
from bs4 import BeautifulSoup
url = r"http://laguiatv.abc.es/programacion/tve-1-807.html"
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
f = csv.writer(open("prueba.csv", "w"))
for tr in soup.find_all('tr')[2:]:
    tds = tr.find_all('h2')
    td2 = tr.find_all('th')
    hora = tds[0].text.encode('utf-8')
    nombre = td2[0].text.encode('utf-8')
    print hora, nombre
    f.writerow(["Hora", "Nombre"])
    f.writerow([hora, nombre])

rawDescription: How convert print in table csv beautiful soap


code:
for indices in itertools.combinations(range(24),11):
    a = numpy.ones(24)
    a[list(indices)] = -1
    print a

rawDescription: Iterate over lists with a particular sum


code:
## file one: main.py, main program in your working directory
# this code must run directly, not inside IDLE to get right directory name
import os, mytest
curdir=os.path.dirname(__file__)
print '-'*10,'program','-'*10
print 'Program in',curdir
print 'Module is in', mytest.curdir
print 'Config contents in module directory:\n',mytest.config()
input('Push Enter')

rawDescription: How can I access relative paths in Python 2.7 when imported by different modules

description: Access relative paths in Python 2.7 when imported by different modules


code:
def do_add(s, x):
  l = len(s)
  s.add(x)
  return len(s) != l

s = set()
print do_add(s, 1)
print do_add(s, 2)
print do_add(s, 1)
print do_add(s, 2)
print do_add(s, 4)

rawDescription: Python: how to check if an item was added to a set, without 2x (hash, lookup)


code:
class Node():
    def __init__(self):
        pass

    def register(self, f):
        print f()

rawDescription: How to override a function call in Python?

description: Override a function call in Python


rawDescription: Sort a list based on a given distribution


code:
import requests
from bs4 import BeautifulSoup

url = requests.get('http://www.oddschecker.com/tennis/match-coupon')
html = url.content
soup = BeautifulSoup(html)

for row in soup.find_all("tr", {"data-market-id": True}):
    participants = [item.get_text(strip=True) for item in row.find_all('span', class_='fixtures-bet-name')]
    odds = [item.get_text(strip=True) for item in row.find_all('span', class_='odds')]

    print participants[0], odds[0], participants[1], odds[1]

rawDescription: Print table without HTML tags for use in calculations


code:
highscores = []

while True:
    try:
        highscores.append(pickle.load(f))
    except EOFError:
        break
print highscores

rawDescription: I am trying to save high scores with pickle, but how do I add to an already pickled document and then get the max?


code:
file  = open("/Users/shannonmcgregor/Desktop/npr.txt", 'r')
for line in file:
    if re.search('<a href=[^>]*(islamic|praying|marines|comets|dyslexics)', line):
        print re.findall(r'(?<=")[^"]*(?=")', line)[0]

rawDescription: using python regex to extract clean URLs

description: Using python regex to extract clean URLs


code:
class MyStore(dict):

    def __init__(self, data):
        for d in data:
            self[d] = d.upper()

    def __getattr__(self, name):
        return self[name]

    def __setattr__(self, name, value):
        self[name] = value

store = MyStore(['foo', 'bar', 'spam', 'eggs'])
print "Store items:", [item for item in store]
print "Number of items:", len(store)
print "Get item:", store['foo']
print "Get attribute:", store.foo
print "'foo' is in store:", 'foo' in store
store.qux = 'QUX'
print "Get qux item:", store['qux']
print "Get qux attribute:", store.qux
print "'qux' is in store:", 'qux' in store

rawDescription: Python class as datastore

description: Python class as datastore


code:
if __name__=="__main__":
    mydict = {}
    reader = csv.reader(open("file.csv", "rb"))
    for rows in reader:
        k = rows[0]
        v = rows[1]
        mydict[k] = v
    print mydict

rawDescription: make dictionary from csv file columns

description: Make dictionary from csv file columns


code:
# Read all except the first (header) lines
lines = file('1.txt').readlines()[1:]
lines += file('2.txt').readlines()[1:]

lines.sort(key=lambda x: int(x.split()[0]))

print ''.join(lines)

rawDescription: Merging text files in ascending order of their contents