code:
processes = []

with open("file.txt", "r") as f:
    lines = f.readlines()

    # Loop through all lines, ignoring header.
    # Add last element to list (i.e. the process name)
    for l in lines[1:]:
        processes.append(l.split()[-1])

print processes

rawDescription: extracting specific data from a text file in python

description: Extracting specific data from a text file in python


code:
builder = DTreeBuilder()
builder.build([], [1,2,3])
print builder.sigma

rawDescription: Python: Substituting for global variables


code:
from itertools import product

for key in dd[0]:
    for combination in product(*(d[key] for d in dd)):
        print ' '.join(combination)

rawDescription: Getting the values of many defaultdicts that contains a certain key


code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyTableModel(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = [i for i in range(10000)]

    def rowCount(self, parent):
        return len(self.items)
    def columnCount(self, parent):
        return 1

    def data(self, index, role):
        if not index.isValid():
            return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        column=index.column()
        if row<len(self.items):
            return QVariant(self.items[row])
        else:
            return QVariant()

class Proxy01(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy01, self).__init__()
        self.keyword=None

    def setKeyword(self, arg):
        if arg: self.keyword=str(arg)
        self.reset()

    def filterAcceptsRow(self, row, parent):
        sourceModel=self.sourceModel()
        index=sourceModel.index(row, 0, parent)
        name=sourceModel.data(index, Qt.DisplayRole).toString()

        if self.keyword and name and not self.keyword.lower() in str(name).lower():
            return False
        return True

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        self.tablemodel=MyTableModel(self)

        self.proxy1=Proxy01()
        self.proxy1.setSourceModel(self.tablemodel)

        tableviewA=QTableView()
        tableviewA.setModel(self.proxy1)

        searchEdit=QLineEdit()
        searchEdit.textChanged.connect(self.proxy1.setKeyword)

        layout = QVBoxLayout(self)
        layout.addWidget(tableviewA)
        layout.addWidget(searchEdit)
        self.setLayout(layout)

    def test(self, arg):
        print arg

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

rawDescription: Chained QSortFilterProxyModels

description: Chained QSortFilterProxyModels


code:
import numpy as np

a = np.array([1,2,3])
a.resize(2,3)
print a
#array([[1, 2, 3],
#       [0, 0, 0]])

rawDescription: Python equivalent code to MATLABs vec2mat


code:
print '\n'
print r'\n'

rawDescription: I am trying to print some list containing '\t' and '\n' . But as python is not printing it because it is thinking it as a special character


code:
for arg in sys.argv[1:]:
    print arg

rawDescription: How to not display script name when looping on command line argument?

description: Not display script name when looping on command line argument


code:
import re
obj = re.search(r'@CAD_DTA\\">(.+?)@G@H@CAD_LBL', 'Some text here...@CAD_DTA\\">I WANT THIS@G@H@CAD_LBL')
print obj.start(1)
print obj.group(1)

rawDescription: Extracting strings from HTML with Python wont work with regex or BeautifulSoup


code:
import random
import pygame

from Player import *
from Enemy import *

WHITE = (255,255,255)
BLACK = (0  ,0  ,0  )
RED   = (255,0  ,0  )

#----------------------------------------------------------------------

class Game():

    def __init__(self):

        pygame.init()

        self.screen = pygame.display.set_mode((1400,500))

        self.background = pygame.image.load("GameField1.png").convert()

        self.enemies = []
        #self.players = []
        self.player = Player(self.screen.get_rect())

        # create 3 enemies 0...2

        for i in range(3):
            enemy = Enemy("enemy_"+str(i)+".png")
            enemy.update() # set random position on start
            self.enemies.append(enemy)

        self.font = pygame.font.SysFont("", 32)

        self.gameover_text = self.font.render("GAME OVER", -1, RED)
        self.gameover_rect = self.gameover_text.get_rect(center=self.screen.get_rect().center)

        self.restart()

    #------------

    def restart(self):

        self.player_score = 0
        self.enemies_score = 0

        #self.play_time = 2*60 # 2minutes * 60 seconds
        self.play_time = 30 # 30 seconds for fast test
        self.change_time = pygame.time.get_ticks() + 1000 # 1s

        self.player.restart()

    #------------

    def update_time(self):
        print "(debug): time:", self.change_time, pygame.time.get_ticks()

        if pygame.time.get_ticks() >= self.change_time:
            self.change_time += 1000 # 1s
            self.play_time -= 1

        return self.play_time <= 0 # GAME OVER ?

    #------------

    def draw_score(self, surface):
        surface_rect = surface.get_rect()

        self.player_score_text = self.font.render(str(self.player_score) + " :Player", -1, WHITE)
        self.player_score_rect = self.player_score_text.get_rect(right=surface_rect.right-10, top=10)

        surface.blit(self.player_score_text, self.player_score_rect)

        self.enemies_score_text = self.font.render("Enemy: " + str(self.enemies_score), -1, WHITE)
        self.enemies_score_rect = self.enemies_score_text.get_rect(left=surface_rect.left+10, top=10)

        surface.blit(self.enemies_score_text, self.enemies_score_rect)

        print "(debug): render scores:", self.player_score, self.player_score_rect, self.enemies_score, self.enemies_score_rect

    #------------

    def draw_time(self, surface):
        surface_rect = surface.get_rect()

        time_str = "%02d:%02d" % (self.play_time/60, self.play_time%60)

        self.time_text = self.font.render(time_str, -1, RED )
        self.time_rect = self.time_text.get_rect(centerx=surface_rect.centerx, top=10)

        surface.blit(self.time_text, self.time_rect)

        print "(debug): render time:", self.play_time, self.time_rect, (self.play_time/60, self.play_time%60), time_str

    #------------

    def run(self):

        clock = pygame.time.Clock()

        RUNNING = True
        GAME_OVER = False

        while RUNNING:

            # --- events ---

            PLAYER_MOVES = False

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = False

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

                    if event.key == pygame.K_BACKSPACE:
                        if GAME_OVER:
                            GAME_OVER = False
                            self.restart()

                # player moves

                if not GAME_OVER:
                    PLAYER_MOVES = self.player.handle_events(event)

            # --- updates ----

            if PLAYER_MOVES and not GAME_OVER:

                # changes position when key is pressed

                for enemy in self.enemies:
                    enemy.update()

                # check collisions

                collision = False
                for enemy in self.enemies:
                    if pygame.sprite.collide_rect(self.player, enemy):
                        collision = True
                        break # first collision and I don't check rest enemies

                if collision:
                    self.enemies_score += 7
                    print "(debug): game: collision:", self.player_score, self.enemies_score
                    self.player.restart()

                # check touchdown

                if self.player.rect.left <= 100:
                    self.player_score += 7
                    print "(debug): game: touchdown:", self.player_score, self.enemies_score
                    self.player.restart()

            if not GAME_OVER:
                GAME_OVER = self.update_time()

            # --- draws ---

            self.screen.fill(BLACK)

            self.screen.blit(self.background, self.background.get_rect())

            self.player.draw(self.screen)

            for enemy in self.enemies:
                enemy.draw(self.screen)

            self.draw_time(self.screen)
            self.draw_score(self.screen)

            if GAME_OVER:
                self.screen.blit(self.gameover_text, self.gameover_rect)

            pygame.display.update()

            # --- FPS ---

            clock.tick(20)

#----------------------------------------------------------------------

Game().run()

rawDescription: Pygame enemy display random when player moves by keyboard


code:
keyList=entryDir.keys()
if keyList[0] != keyList[1]:
    if entryDir[keyList[0]][0] == entryDir[keyList[1]][0]:
        print entryDir[keyList[0]][0], "match found"
    else:
        print entryDir[keyList[0]][0],entryDir[keyList[1]][0], "no match"

rawDescription: iterate over dict of lists and return keys with matching values

description: Iterate over dict of lists and return keys with matching values


code: print text,

rawDescription: Print without line feed

description: Print without line feed


code: print outstanding_amount[1] # Since we start from 0, the second item is at index 1

rawDescription: Is there a way to add up integers which i have retrieved from a .txt file


code: print '/'.join(map(str, [now.month, now.day, now.year]))

rawDescription: Formatting datetime in python

description: Formatting datetime in python


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

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


code:
def readFile1(file1):
    cList1 = [line.rstrip('\n') for line in file1]
    print cList1

rawDescription: readlines not reading the last line of the file in python

description: Readlines not reading the last line of the file in python


code:
def wrapper(thelist):
    for item in thelist:
        yield(item[0], item[1], item[2], item[3:])

mylist = [(1,2,3,4), (5,6,7,8)]

for a, b, c, d in wrapper(mylist):
    print a, b, c, d

rawDescription: Extended tuple unpacking in Python 2

description: Extended tuple unpacking in Python 2


code:
class DimNames(object):
    def __init__(self, dimnames):
        self.dd = [{n:i for i,n in enumerate(names)} for names in dimnames]
    def __getitem__(self,key):
        # print key
        if isinstance(key, tuple):
            return tuple([self.parse_key(key, self.dd[i]) for i,key in enumerate(key)])
        else:
            return self.parse_key(key, self.dd[0])
    def parse_key(self,key, dd):
        if key is None:
            return key
        if isinstance(key,int):
            return key
        if isinstance(key,str):
            return dd[key]
        if isinstance(key,tuple):
            return tuple([self.parse_key(k, dd) for k in key])
        if isinstance(key,list):
            return [self.parse_key(k, dd) for k in key]
        if isinstance(key,slice):
            return slice(self.parse_key(key.start, dd),
                         self.parse_key(key.stop, dd),
                         self.parse_key(key.step, dd))
        raise KeyError

dd = DimNames([['x','y','z'], ['a','b','c']])

print A[dd['x']]              # A[0]
print A[dd['x','c']]          # A[0,2]
print A[dd['x':'z':2]]        # A[0:2:2]
print A[dd[['x','z'],:]]      # A[[0,2],:]
print A[dd[['x','y'],'b':]]   # A[[0,1], 1:]
print A[dd[:'z', :2]]         # A[:2,:2]

rawDescription: python tensors with named field access

description: Python tensors with named field access


code:
class Foo(int):
    def __new__(cls, some_argument=None, value=0):
        i = int.__new__(cls, value)
        i._some_argument = some_argument
        return i

    def print_some_argument(self):
        print self._some_argument

rawDescription: Python Class with integer emulation

description: Python Class with integer emulation


code:
S1, S2 = {}, {} # dicts are slightly faster than `set()`
with open(path) as f:
     for i, line in enumerate(f, 1):
         id, sep, type = line.partition(" ")
         if type == "1" or type == "2":
            S1[id] = True
         elif type == "3" or type == "4":
            S2[id] = True
         else:
            print "WARNING: unknown type: %r in line %d: %r" % (type, i, line)
print len(S1), len(S2)

rawDescription: How to improve performance of this counting program?

description: Improve performance of this counting program


code:
from bs4 import BeautifulSoup

html_doc = "<b class='whb'>This is the text I want to retrieve</b> This is additional text that I don't want."

soup = BeautifulSoup(html_doc, 'html.parser')

print soup.b.text

rawDescription: Find section of a string from keyword to keyword


code:
import wmi

w = wmi.WMI()
for battery in w.query('select * from Win32_Battery'):
    print battery.EstimatedChargeRemaining

rawDescription: Get battery status using wmi in python?

description: Get battery status using wmi in python


code:
import argparse

def main():
    arger = argparse.ArgumentParser()

    # Arguments for top-level, e.g "subcmds.py -v"
    arger.add_argument("-v", "--verbose", action="count", default=0)

    subparsers = arger.add_subparsers(dest="command")

    # Make parser for "subcmds.py info ..."
    info_parser = subparsers.add_parser("info")
    info_parser.add_argument("-m", "--moo", dest="moo")

    # Make parser for "subcmds.py create ..."
    create_parser = subparsers.add_parser("create")
    create_parser.add_argument("name")
    create_parser.add_argument("additional", nargs="*")

    # Parse
    opts = arger.parse_args()

    # Print option object for debug
    print opts

    if opts.command == "info":
        print "Info command"
        print "--moo was %s" % opts.moo

    elif opts.command == "create":
        print "Creating %s" % opts.name
        print "Additional: %s" % opts.additional

    else:
        # argparse will error on unexpected commands, but
        # in case we mistype one of the elif statements...
        raise ValueError("Unhandled command %s" % opts.command)

if __name__ == '__main__':
    main()

rawDescription: Implementing a "[command] [action] [parameter]" style command-line interfaces?


code:
import json

class Light:
    def __init__(self, id, name):
        self.id = id
        self.name = name

response = '{"1": {"name": "bedroom"}, "2": {"name": "kitchen"}}'

class Decoder(json.JSONDecoder):
    def decode(self, s):
        obj = super(Decoder, self).decode(s)
        return [Light(id=k, name=v['name']) for k, v in obj.iteritems()]

lights = json.loads(response, cls=Decoder)
print lights  # prints [<__main__.Light instance at 0x9c3b50c>, <__main__.Light instance at 0x9c3b56c>]
print [light.__dict__ for light in lights]  # prints [{'id': u'1', 'name': u'bedroom'}, {'id': u'2', 'name': u'kitchen'}]

rawDescription: Deserialize JSON to Object

description: Deserialize JSON to Object


code: print '{0}  {1}'.format(count, conv)

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

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


code:
import sys, ast, getopt, types

def main(argv):
    arg_dict={}
    switches={'li':list,'di':dict,'tu':tuple}
    singles=''.join([x[0]+':' for x in switches])
    long_form=[x+'=' for x in switches]
    d={x[0]+':':'--'+x for x in switches}
    try:
        opts, args = getopt.getopt(argv, singles, long_form)
    except getopt.GetoptError:
        print "bad arg"
        sys.exit(2)

    for opt, arg in opts:
        if opt[1]+':' in d: o=d[opt[1]+':'][2:]
        elif opt in d.values(): o=opt[2:]
        else: o =''
        print opt, arg,o
        if o and arg:
            arg_dict[o]=ast.literal_eval(arg)

        if not o or not isinstance(arg_dict[o], switches[o]):
            print opt, arg, " Error: bad arg"
            sys.exit(2)

    for e in arg_dict:
        print e, arg_dict[e], type(arg_dict[e])

if __name__ == '__main__':
    main(sys.argv[1:])

rawDescription: Passing a List to Python From Command Line


code:
def equivalent(a, b):
    return is_function(zip(a, b))

print equivalent('foo', 'baa') # yes
print equivalent('foo', 'bar') # no

rawDescription: Find equivalent words in a list through arbitrary mapping


code:
import re

with open('myLargeFile.txt', 'r') as myFile:
    numbersList = re.findall('{"number":(\d{9})', myFile.read(), re.DOTALL)
print numbersList

rawDescription: python, regex: What RegEx do I need?

description: Python, regex: What RegEx do I need


code:
class hexNumber:
   def __init__(self,x):
      if isinstance(x,str):
         self.val = int(x,16)
      elif isinstance(x,int):
         self.val = int(str(x),16)
   def __str__(self):
          return hex(self.val)

y = hexNumber(12345)
print "%s"%y
print isinstance(y,hexNumber)

y = hexNumber("1234abc")
print "%s"%y
print isinstance(y,hexNumber)

rawDescription: How to convince python that an int type is actually a hex type?

description: Convince python that an int type is actually a hex type


code:
import textwrap
txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
print '\n'.join(textwrap.wrap(txt, 20, break_long_words=False))

rawDescription: How to limit the number of characters per line without affecting a word?

description: Limit the number of characters per line without affecting a word


code:
def convert_to_min(o_clock):
    h, m = o_clock.split(':')
    return int(h) * 60 + int(m)
# using this function create a list times in minutes for each time in col 4
min_col = [convert_to_min(t) for t in data[4]]
data[5] = min_col  # inserts this list as a new column '5'
print data

rawDescription: Fixed y axis in Python plotting times in 12 hr format


rawDescription: How to split datetime object in csv file using Python into 30 minute arrays and export each array as new csv file

description: Split datetime object in csv file using Python into 30 minute arrays and export each array as new csv file


code:
import diff_match_patch

dmp = diff_match_patch.diff_match_patch()
diffs = dmp.diff_main('This is my original paragraph.', 'My paragraph is much better now.')
print diffs  # pre-cleanup

dmp.diff_cleanupSemantic(diffs)
print diffs  # post cleanup

rawDescription: Paragraph Matching Python

description: Paragraph Matching Python


code:
from BeautifulSoup import BeautifulSoup

html = ('<title>Page title</title>'
       ''
       '

'
       'Slackware Linux 13.0 [x86 DVD ISO]	'
       'Slackware Linux 14.0 [x86 DVD ISO]	'
       'Slackware Linux 15.0 [x86 DVD ISO]	'
       '

'
       'body'
       '')
soup = BeautifulSoup(html)
links = [td.find('a') for td in soup.findAll('td', { "class" : "name" })]
for link in links:
    print link.string

rawDescription: Extracting Text from Parsed HTML with Python


code:
active = False

def main(app):
    global active
    active = True
    while active:
        print "infinite loop"
        app.processEvents()

rawDescription: how can i stop module in python?

description: Stop module in python


code:
class Book(models.Model):
    name        = models.CharField(max_length=140)

class UserProfile(models.Model):
    favorites    = models.ManyToManyField(Book, null=True, blank=True)
    user         = models.OneToOneField(User)

favorite_books = this_user_profile.favorites.all()
for b in Book.objects.all():
    if b in favorite_books:
        print "Book", b.name, "is a favorite of this user!"
    else:
        print "Book", b.name, "is not a favorite of this user!"

rawDescription: Complex django query in a many to many relationship


code:
re_sections=re.compile(r"(?P<section>Section\d)\s*(?P<section_data>.+?)(?=(?:Section\d|$))", re.DOTALL)
for m in re_sections.finditer(text): print m.groupdict()

rawDescription: Python Regular Expression matching multiple lines (re.DOTALL)


code:
import ScrolledText
import sys
import tkFileDialog
import Tkinter

########################################################################
class RedirectText(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, text_ctrl):
        """Constructor"""
        self.output = text_ctrl

    #----------------------------------------------------------------------
    def write(self, string):
        """"""
        self.output.insert(Tkinter.END, string)

########################################################################
class MyApp(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        self.root = parent
        self.root.title("Redirect")
        self.frame = Tkinter.Frame(parent)
        self.frame.pack()

        self.text = ScrolledText.ScrolledText(self.frame)
        self.text.pack()

        # redirect stdout
        redir = RedirectText(self.text)
        sys.stdout = redir

        btn = Tkinter.Button(self.frame, text="Open file", command=self.open_file)
        btn.pack()

    #----------------------------------------------------------------------
    def open_file(self):
        """
        Open a file, read it line-by-line and print out each line to
        the text control widget
        """
        options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
        options['initialdir'] = '/home'
        options['parent'] = self.root
        options['title'] = "Open a file"

        with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
            for line in f_handle:
                print line

#----------------------------------------------------------------------
if __name__ == "__main__":
    root = Tkinter.Tk()
    root.geometry("800x600")
    app = MyApp(root)
    root.mainloop()

rawDescription: Get command window output to display in widget with tkinter


code:
import zmq
import time
from multiprocessing import Pipe, current_process
from multiprocessing.reduction import reduce_connection

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

time.sleep(1)

a, b = Pipe()
reduced_a = reduce_connection(a)
socket.send_pyobj(reduced_a)
socket.send(current_process().authkey)

print b.recv()

rawDescription: Send python multiprocessing connection via ØMQ


code:
from collections import defaultdict

d = defaultdict(list)
for i in 'a=b,a=c,a=d,b=e'.split(","):
    k,v = i.split("=")
    d[k].append(v)
print d

rawDescription: create a dict of lists from a string

description: Create a dict of lists from a string


code:
a,b = (1, 2)  #The RHS here is a tuple, but it could be a list, generator, etc.
print a #1
print b #2

rawDescription: "variable, variable =" syntax in python?

description: "variable, variable =" syntax in python


code:
def five():
   print 'returning 5'
   return 5

1 < five() <= 5

rawDescription: What does "evaluated only once" mean for chained comparisons in Python?


code:
import sys
from os.path import splitext
try:
    print os
except NameError:
    print "os not found"
try:
    print os.path
except NameError:
    print "os.path is not found"

print sys.modules['os']

rawDescription: Why does it take longer to import a function from a module than the entire module itself?


code:
def percentLetters(s):
        print s.count('A')
        print s.count('B')
percentLetters('AABB')
2
2

rawDescription: Counting occurence within a string Python


code:
got_model = rpc_srv.getmodel('f21e4e0b-493a-460b-982a-d2bb31c45864')
m, method = xmlrpclib.loads(got_model)
for item in m:
    print item

rawDescription: Output Django Object into XML-RPC response


code:
def f(x, a, b):
    return a+b*x

def f_r(x, a_r, a_i, b_r, b_i):
    return f(x, a_r + 1J*a_i, b_r+1J*b_i)

f_u = complex_unroll(f, [False, True, True])

print f(1,2+3J,4+5J)
print f_r(1,2,3,4,5)
print f_u(1,2,3,4,5)

f_u2 = complex_unroll(f, [True, True, True])

print f_u2(1,0,2,3,4,5)

rawDescription: Replacing complex-valued arguments of generic function object by two real-valued arguments


code:
import config
import MySQLdb
import multiprocessing as mp
import random
import string
import time

def random_string(n):
    return ''.join(random.choice(string.letters) for _ in range(n))

def generate_data():
    conn=MySQLdb.connect(
        host=config.HOST,user=config.USER,
        passwd=config.PASS,db='test')
    cursor=conn.cursor()
    while True:
        with open('/tmp/test.dat','w') as f:
            for _ in range(20):
                f.write('{b}\n'.format(b=random_string(10)))
        # sql='LOCK TABLES foo WRITE'
        # cursor.execute(sql)
        sql="LOAD DATA INFILE '/tmp/test.dat' INTO TABLE test.foo"
        cursor.execute(sql)
        conn.commit()
        # sql='UNLOCK TABLES'
        # cursor.execute(sql)
        time.sleep(0.05)

def setup_innodb(connection):
    cursor=connection.cursor()
    sql='DROP TABLE IF EXISTS foo'
    cursor.execute(sql)
    sql='''\
        CREATE TABLE `foo` (
          `bar` varchar(10) NOT NULL
        ) ENGINE=InnoDB
        '''
    cursor.execute(sql)
    connection.commit()

if __name__ == '__main__':
    NUMBER_OF_ITERATIONS = 20
    conn=MySQLdb.connect(
        host=config.HOST,user=config.USER,
        passwd=config.PASS,db='test')
    setup_innodb(conn)

    # Start a process which is "simultaneously" calling LOAD DATA INFILE
    proc=mp.Process(target=generate_data)
    proc.daemon=True
    proc.start()

    for i in range( NUMBER_OF_ITERATIONS ):
        cursor = conn.cursor()
        # sql='''SELECT field
        #        FROM table
        #        WHERE other_field = 0
        #        LIMIT 10 LOCK IN SHARE MODE'''
        # sql='LOCK TABLES foo READ'
        # cursor.execute(sql)
        sql='''SELECT *
               FROM foo
               LOCK IN SHARE MODE
               '''
        res = cursor.execute(sql)
        print '# of selected rows: ' + str(res)
        values = cursor.fetchall()
        # http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
        # Locks set by LOCK IN SHARE MODE and FOR UPDATE reads are released when
        # the transaction is committed or rolled back.
        conn.commit()
        time.sleep(0.1)

    conn.close()
    print 'DONE'

rawDescription: Strange behavior of MySQL SELECT

description: Strange behavior of MySQL SELECT


code:
l = [1, 2, 3, 4, 5, 6, 7, 8]
l1,l2 = l[:len(l)/2], l[len(l)/2:]
l = [ [x,y] for x,y in zip (l1, l2[::-1])]
for i in range(len(l)):
        if isinstance(l[i][0], list):
                l[i] = [x for item in l[i] for x in item]
print l

l1,l2 = l[:len(l)/2], l[len(l)/2:]
l = [ [x,y] for x,y in zip (l1, l2[::-1])]
for i in range(len(l)):
        if isinstance(l[i][0], list):
                l[i] = [x for item in l[i] for x in item]
print l

rawDescription: Solve folding paper challenge

description: Solve folding paper challenge


code:
s = raw_input("enter the word you want to find in text file : ")

f = False
for line in open("pro.txt").readlines():
    if s == line.split()[3].strip():
        if not f:
            print "word found !!"
            f = True
        print ' '.join(line.split()[:3])

rawDescription: how to retrieve value from text file using python?

description: Retrieve value from text file using python


code:
a = 'l'
b = '5'
c = 'o'

x = input("Enter your message: ")
values = [globals().get(var, '') for var in list(x)]

print "".join(values)

rawDescription: How to use input string as variable call in python?

description: Use input string as variable call in python


code:
fp = open(file_path, "rb")
while 1:
    prefix, = struct.unpack( '>i', fp.read(4) )
    fp.seek(abs(prefix), 1)    #or read |prefix| bytes data as you want
    suffix, = struct.unpack( '>i', fp.read(4) )
    print prefix, suffix
    if abs(suffix) - abs(prefix):
        print "suffix != prefix!"
        break
    if prefix > 0: break

rawDescription: inconsistent record marker while reading fortran unformatted file

description: Inconsistent record marker while reading fortran unformatted file


code: print ElementTree.tostring(favs)

rawDescription: flickAPI: How do I list the sets for my user account?

description: FlickAPI: How do I list the sets for my user account


code:
import csv
with open('English.csv', 'rb') as csvfile1:
    with open ("Dictionary.csv", "rb") as csvfile2:
        reader1 = csv.reader(csvfile1)
        reader2 = csv.reader(csvfile2)
        rows1_col_a = [row[0] for row in reader1]
        rows2 = [row for row in reader2]
        only_b = []
        for row in rows2:
            if row[0] not in rows1_col_a:
                only_b.append(row)
        print only_b

rawDescription: Compare two CSV files and print the rows that are different Python


code:
def test():
    print 'hello world'
func = test
func() # this will call test()

rawDescription: what is the difference between eval and exec in python?

description: What is the difference between eval and exec in python


code:
Grouped = people2.groupby(key)

def f(a, b, c, **kw):
    return (a.mean() - b.mean())*c

people2["f"] = Grouped.apply(lambda df:f(**df))
print people2

rawDescription: Pass Multiple Columns to groupby.transform


code:
class ProcessStuff(Thread):
    def __init__(self, queue):
        super(ProcessStuff, self).__init__()
        self.queue = queue
        self._do_queue = Queue()  # internal Queue for DoStuffClass
        self._do_stuff = dostuff.DoStuffClass(self._do_queue)

    def run(self):
        self._do_stuff.startthread(True)
        self._do_stuff.start()
        while True:
            val = self._do_queue.get()  # Grab value from DoStuffClass
            # process it
            processed_val = "processed {}".format(val)
            self.queue.put(processed_val)

q = Queue(maxsize=1)
letsprocessstuff = ProcessStuff(q)

letsprocessstuff.start()
while (True):
    val = q.get()
    print "Outputting: %s" % val

rawDescription: How to inherit a thread class with infinite loop

description: Inherit a thread class with infinite loop


code:
dictA = {'key1':'Joe', 'key2':'Bob'}
dictB = {'key1':'Smith', 'key2':'Johnson'}
dictAB = {k: [dictA[k], dictB[k]] for k in dictA.viewkeys() & dictB.viewkeys()}
print dictAB

rawDescription: Refactoring with python dictionary comprehension


code:
def some_func(a,b='myDefaultValue'):
    print a, b

def wrapper(a,b):
    b = some_func.func_defaults[0] if b is None else b
    some_func(a,b)

print "b is 'there'"
a = "hello"
b = "there"
wrapper(a,b)

print "b is 'None'"
b = None
wrapper(a,b)

rawDescription: Function arguments inheriting defaults from other function


code:
def formula(points, n):
  if points[n] is None:
    return str(n)
  a, b, operator = points[n]
  return '(%s %s %s)' % (formula(points, a), operator, formula(points, b))

print formula(points, 10)

rawDescription: Creating formula based on binary tree leaves


code:
def submit_data():
    username = username_entry.get()
    print username

username_entry = Tk.Entry(root)
submit = Tk.Button(root,text='Enter',command=submit_data)

rawDescription: How do I store a value entered on a Tk entry widget in a variable

description: Store a value entered on a Tk entry widget in a variable


rawDescription: How to index a DataFrame based on the number of consecutive days

description: Index a DataFrame based on the number of consecutive days


code:
from collections import defaultdict

print "%(name)s, %(age)s" % defaultdict(lambda: '', {"name":'Bob'})
print "%(name)s, %(age)s" % defaultdict(lambda: '', name='Bob')

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


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

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/bandwidth_counter.pid'
        self.pidfile_timeout = 5
    def run(self):
        rx_megabits_new = 0
        tx_megabits_new = 0
        try:
                while True:
                        output = subprocess.Popen(['ifconfig', "wlan0"], stdout=subprocess.PIPE).communicate()[0]
                        rx_bytes = re.findall('RX bytes:([0-9]*) ', output)[0]
                        tx_bytes = re.findall('TX bytes:([0-9]*) ', output)[0]
                        rx_megabits = (((int(rx_bytes) * 8) / 1024) / 1024)
                        tx_megabits = (((int(tx_bytes) * 8) / 1024) / 1024)
                        current_rx_usage = rx_megabits - rx_megabits_new
                        current_tx_usage = tx_megabits - tx_megabits_new
                        rx_megabits_new = rx_megabits
                        tx_megabits_new = tx_megabits
                        print 'average megabits received', current_rx_usage / 60
                        print 'average kilobits received', (current_rx_usage * 1024) / 60
                        print 'average megabits sent', current_tx_usage / 60
                        print 'average kilobits sent', (current_tx_usage * 1024) / 60
                        time.sleep(60)

        except Exception, e:
            raise

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

rawDescription: Average network statistics in Python

description: Average network statistics in Python


code:
def iterXOR(hexString, otherString):

    #Convert otherString to a hex (the 16 is for base 16)
    otherNum = int(otherString, 16)

    #Iterate over the hexString
    for i in range(0, len(hexString) - len(otherString) + 1):

        #Grab the substring of length N beginning at index i
        #Where N is the length of otherString and i is the index
        subString = hexString[i:i+len(otherString)]

        #Convert the substring to a hex number
        hexNum = int(subString, 16)

        #Print the product of the XOR (or do whatever you want here)
        print hex(hexNum ^ otherNum)

#Usage
iterXOR("12104c06134e5709", "74484520")

rawDescription: Split encrypted string and xor it with a shorter string at every point


code:
l = [1, 1, 3, 2, 5, 7, 8, 3, 2, 1]
result = 0 # accumulator for result
for e in l:
  if e in a:
    result += 1
  elif e in b:
    result -= 1

print result # prints "2"

rawDescription: How can I quickly compare a list and a set?

description: Quickly compare a list and a set


code:
for row in soup.select('table#table_prefixes4 tr'):
    cells = row.find_all('td')
    if not cells:
        continue
    print [cell.get_text(strip=True) for cell in cells]

rawDescription: How to extract td HTML tags with Python BeautifulSoup?

description: Extract td HTML tags with Python BeautifulSoup


code:
if not any(k == 2 for k in a):
    print 'no match'

rawDescription: Is there an inverse behavior to the "else" in a for-else loop?


code:
s = "key1|key2|key3=value"

keys, value = s.split('=')
keys = keys.split('|')

base = {}

reduce(
    lambda d, k: d.setdefault(k, {}),
    keys[:-1],
    base
)[keys[-1]] = value

print base # {'key1': {'key2': {'key3': 'value'}}}

rawDescription: Python parse a string into n-depth dictionary


rawDescription: python numpy roll with padding

description: Python numpy roll with padding


code: print map(list, itertools.combinations(range(1, y+1), x))

rawDescription: List All Combinations (Python)

description: List All Combinations (Python)


code:
def some_generator(iterator ,sum_limit, index_value):
    """
    >>> list(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11], [3, 5, 15], [4, 5, 9]]

    """
    for i in iterator:
        if (sum_limit and (sum(i) <= sum_limit)) or  (index_value and (i[0] < index_value)):
            yield i
        else:
            break

def foo(_generator, custom_key = None):
    """
    >>> foo(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 5, 15], [3, 7, 10], [4, 5, 9], [4, 5, 11]]
    """
    x = list(_generator)
    return sorted(x,key= custom_key)

iterator = [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11],
         [3, 5, 15], [4, 5, 9], [4, 6, 12], [1, 5, 19]]

print foo(some_generator(iterator,20, 4), custom_key=sum)

rawDescription: Python Arguments

description: Python Arguments


code:
list_ = []
for i in range(something):
    dict_ = return_values_as_dict()
    list_.append(dict_)
print list_

rawDescription: Python The appended element in the list changes as its original variable changes


code:
from pytrie import StringTrie

url_list = [
    'http://www.google.com/mail',
    'http://www.google.com/document',
    'http://www.facebook.com',
]

url_trie = StringTrie()

for url in url_list:
    url_trie[url] = url

searches = ("http", "http://www.go", "http://www.fa", "http://fail")

for search in searches:
    print "'%s' ->" % search, url_trie.values(prefix=search)

rawDescription: Longest Prefix Matches for URLs

description: Longest Prefix Matches for URLs


code:
while file_size > 0:
    work_file = server.recv(1024)
    f.write(work_file)
    # subtract the actual number of bytes received
    file_size -= len(work_file)
    print file_size

rawDescription: Python sending a file over sockets

description: Python sending a file over sockets


code:
from lxml import etree

root = etree.Element('root')
etree.SubElement(root, 'child1')
etree.SubElement(root, 'child2')

print etree.tostring(root, encoding='UTF-8', xml_declaration=True, doctype='''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">''')

rawDescription: how to write the opening of an xml doc in lxml?

description: Write the opening of an xml doc in lxml


code:
freq = raw_input("Enter freq values separated by spaces ").strip().split(" ")
flux = raw_input("Enter flux values separated by spaces ").strip().split(" ")

print freq, flux

rawDescription: Enter many numbers using raw_input()

description: Enter many numbers using raw_input()


code:
data = '''\
234 127 34 23 45567
23 12 4 4 45
23456 2 1 444 567'''

# Split input data by row and then on spaces
rows = [ line.strip().split(' ') for line in data.split('\n') ]

# Reorganize data by columns
cols = zip(*rows)

# Compute column widths by taking maximum length of values per column
col_widths = [ max(len(value) for value in col) for col in cols ]

# Create a suitable format string
format = ' '.join(['%%%ds' % width for width in col_widths ])

# Print each row using the computed format
for row in rows:
  print format % tuple(row)

rawDescription: Line up columns of numbers (print output in table format)


code:
from bs4 import BeautifulSoup

data = '
Masala films of Indian cinema are those that mix genres in one work. Typically these films freely mix action, comedy, romance, and drama or melodrama.[1] They tend to be musicals that include songs filmed in picturesque locations. The genre is named after the masala, a mixture of spices in Indian cuisine.[2] According to The Hindu, masala is the most popular genre of Indian cinema.[3]
'

soup = BeautifulSoup(data)
print soup.get_text()

rawDescription: HTML Stripper causing error

description: HTML Stripper causing error


code:
#!/usr/bin/env python

import os
import sys
import inspect

def load_modules_from_path(path):
    """
    Import all modules from the given directory
    """
    # Check and fix the path
    if path[-1:] != '/':
        path += '/'

    # Get a list of files in the directory, if the directory exists
    if not os.path.exists(path):
         raise OSError("Directory does not exist: %s" % path)

    # Add path to the system path
    sys.path.append(path)
    # Load all the files in path
    for f in os.listdir(path):
        # Ignore anything that isn't a .py file
        if len(f) > 3 and f[-3:] == '.py':
            modname = f[:-3]
            # Import the module
            __import__(modname, globals(), locals(), ['*'])

def load_class_from_name(fqcn):

    # fqcn = fully qualified classname
    # Break apart fqcn to get module and classname

    paths = fqcn.split('.')
    modulename = '.'.join(paths[:-1])
    classname = paths[-1]

    # Import the module
    __import__(modulename, globals(), locals(), ['*'])

    # Get the class
    cls = getattr(sys.modules[modulename], classname)
    # Check cls
    if not inspect.isclass(cls):
        raise TypeError("%s is not a class" % fqcn)

    # Return class
    return cls

def main():

    food_list = ['fruit', 'chocolate']
    subfood_list = [['strawberry', 'orange'],['cadbury', 'kitkat']]

    # show food list for users to select.
    for i in xrange(len(food_list)):
        print '%d: %s' % (i, food_list[i])

    food_index = int(raw_input('Choose one: '))

    for i in xrange(len(subfood_list[food_index])):
        print '%d: %s' % (i, subfood_list[food_index][i])

    subfood_index = int(raw_input('Chose one: '))
    pkg = food_list[food_index]
    module = subfood_list[food_index][subfood_index]
    class_name = module.title()

    load_modules_from_path(pkg)
    new_class = load_class_from_name('%s.%s' % (module, class_name))

    # instantiation
    obj = new_class()
    print obj.say()

if __name__ == '__main__': main()

rawDescription: Make a dynamic import which is still explicit in Python


code:
import time
start = time.time()
do_long_code()
print "it took", time.time() - start, "seconds."

rawDescription: Python question about time spent

description: Python question about time spent


code:
for fruit_name in fruit_stand:
    print fruit_name

# Lemon
# Apple
# Cherry
# NOTE: Order not guaranteed
# Cherry, Lemon, Apple are equally likely

rawDescription: Breaking down instances of classes into lists, or: a better way to sum class variables?


code:
print repr(open)
print open.__module__
import __builtin__
print __builtin__.open

rawDescription: what exactly are built-in types in Python?

description: What exactly are built-in types in Python


code:
for group in n_split(dblist, 5):
    print ' '.join(group)

rawDescription: Splitting a Python list into groups of 5 and then printing


code:
import os
import pickle
from time import time

Internals={} #generator's internal data
STOP=False # flag for stopping the generator

Max=10639 # No. primes to generate per segment
segments=83 # No. segments to unload, reload the data from the generator

            # name  : generator
generators={'1 sieveL':sieveL,
            '2 postponed_sieveL - two level form':postponed_sieveL,
            '3 postponed_sieveR - recursive form':postponed_sieveR,
            }

print 'Doing {:,} segment{}, each generating {:,} prime{} ({:,} prime{})\n'.format(segments,''if segments==1 else's',
                                                                      Max,''if Max==1 else's',Max*segments,''if Max*segments==1 else's')
#find sum of primes of a non paused generator for comparison
Pcom=0
t1=time()
for i,k in enumerate(postponed_sieveR()):
    Pcom+=k
    if i==(Max*segments)-1:
        break
del_t=time()-t1

NamCen=max([len(i)for i in generators.keys()])
col_1='Generator Name'.center(NamCen)
col_2='Sum of all generated primes'
col_3='Data size (Bytes)'
col_4='Time (Seconds)'
Size='N/A'

# table and non paused generator
print(' | '.join([col_1,col_2,col_3,col_4]))
print(' | '.join(['-'*len(col_1),'-'*len(col_2),'-'*len(col_3),'-'*len(col_4)]))
print(' | '.join(['0 Non-paused sieve'.ljust(len(col_1)),'{:,}'.format(Pcom).center(len(col_2)),
                  Size.center(len(col_3)),'{:06.03f}'.format(del_t).center(len(col_4))]))

for name,gen in sorted(generators.items()):
    Psum=0#reset the sum and the data storage for the next sieve
    Internals={}
    t1=time()

    #print('\nstarting {}'.format(name))
    for segs in range(segments):
        for i,j in enumerate(gen(0,Internals)):
            Psum+=j
            if i==Max-1:
                STOP=True
        STOP=False
        #print('\tsegment {}; stopped at {}'.format(segs,j))
    del_t=time()-t1

    # dump data (this section can be commented out without issues)
    DataPath="C:\\data.prime"
    Data=open(DataPath,'wb')
    pickle.dump(Internals,Data)
    Data.close()
    Size=os.path.getsize(DataPath)# find size of data dump after last segment
    os.remove(DataPath)# then remove it, data was only dumped to find file size

    # show stats
    print(' | '.join([name.ljust(len(col_1)),'{:,}'.format(Psum).center(len(col_2)),
                     (Size if type(Size)==str else '{:,}'.format(Size)).center(len(col_3)),
                      '{:06.03f}'.format(del_t).center(len(col_4))]))

rawDescription: Pausable prime generator

description: Pausable prime generator


code:
# decorator for logging
def logging(func):
    def wrapper(*args, **kwargs):
        print func.__name__, args, kwargs
        res = func(*args, **kwargs)
        return res
    return wrapper

# this is some example class you do not want to/can not modify
class Working:
    def Do(c):
        print("I am working")
    def pr(c,printit):   # other example method
        print(printit)
    def bla(c):          # other example method
        c.pr("saybla")

# this is how to make a new class with some methods logged:
class MutantWorking(Working):
    pr=logging(Working.pr)
    bla=logging(Working.bla)
    Do=logging(Working.Do)

h=MutantWorking()
h.bla()
h.pr("Working")
h.Do()

rawDescription: How to cast object in Python

description: Cast object in Python


code:
result = node.text + ''.join(le.tostring(e) for e in node)
print result

rawDescription: Xpath extract current node content including all child node


rawDescription: Extracting number from string in Python with regex


code:
#!/usr/bin/python
from xml.dom.minidom import parseString
import sys

def get_XML_doc_stdin(f):
    return xml.dom.minidom.parse(f)

def get_tagged_data2(tag, index=0):
    xmlData = dom.getElementsByTagName(tag)[index].firstChild.data
    return xmlData

data=sys.stdin.read()
dom = parseString(data)

ele2=get_tagged_data2('title')
print ele2

count=int(get_tagged_data2('fullcount'))
print count,"New Messages:"

for i in range(0,count):
    nam=get_tagged_data2('name',i)
    email=get_tagged_data2('email',i)
    print "  {0}: {1} <{2}>".format(i+1,nam,email)

rawDescription: What does this Perl XML filter look like in Python?


code:
#!/usr/bin/env python
from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time

# path to the directory (relative or absolute)
dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'

# get all entries in the directory w/ stats
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
entries = ((os.stat(path), path) for path in entries)

# leave only regular files, insert creation date
entries = ((stat[ST_CTIME], path)
           for stat, path in entries if S_ISREG(stat[ST_MODE]))
#NOTE: on Windows `ST_CTIME` is a creation date
#  but on Unix it could be something else
#NOTE: use `ST_MTIME` to sort by a modification date

for cdate, path in sorted(entries):
    print time.ctime(cdate), os.path.basename(path)

rawDescription: How do you get a directory listing sorted by creation date in python?

description: Get a directory listing sorted by creation date in python


code:
import os

def process(file_name):
    with open(file_name) as readonly_file:
        print "Do something with %s ,size %d" % (file_name, len(readonly_file.read()))

def traverse(directory, callback=process):
    for dirpath, dirnames, filenames in os.walk(directory):
        for f in filenames:
            path = os.path.abspath(os.path.join(dirpath, f))
            callback(path)

print traverse('./')

rawDescription: How can I make this python script walk through a directory tree?

description: Make this python script walk through a directory tree


code:
import requests
from bs4 import BeautifulSoup
url = "http://search.yahoo.com/search?p=%s"
query = "python"
r = requests.get(url % query)
soup = BeautifulSoup(r.text)
soup.find_all(attrs={"class": "yschttl"})

for link in soup.find_all(attrs={"class": "yschttl"}):
    print "%s (%s)" %(link.text, link.get('href'))

rawDescription: How to do a basic query on yahoo search engine using Python without using any yahoo api?

description: Do a basic query on yahoo search engine using Python without using any yahoo api


code:
with open(filename) as f:
    for line in f:
            if '0,0,0' not in line:
                    print line

rawDescription: finding a pattern in text file in python

description: Finding a pattern in text file in python


rawDescription: Numpy array comparison using nditer

description: Numpy array comparison using nditer


code:
from itertools import ifilterfalse

# Build an example list of sections, e.g. sections = [' tax 1', ' tax 2' ....., ' tax 79']
sections = [" tax {}".format(i) for i in range(1, 80)]

# Entries to skip over
skip = [12, 13, 55, 67, 70]

for index, value in ifilterfalse(lambda (i, v): i in skip, enumerate(sections, start=1)):
    print value

rawDescription: Loop except for certain values using Python


code:
books = [
    [1],
    [3, 2],
    [5, 1, 6],
    [7, 4, 18],
]

max_len = len(max(books, key=len)) # max(map(len, books))
names = [str(i+1) for i, bs in enumerate(books) if len(bs) == max_len]
print 'Book {} contain the longest list(s).'.format(', '.join(names))

# => Book 3, 4 contain the longest list(s).

rawDescription: Comparing Sizes of Multiple Lists

description: Comparing Sizes of Multiple Lists


code:
def paditers(*args):
    iters = [iter(x) for x in args]

    this = [next(i) for i in iters]

    while True:
        try:
            key = min(i for i in this if i != None)
        except ValueError:
            break
        for i, val in enumerate(this):
            if val == key:
                yield val
                this[i] = next(iters[i], None)
            else:
                yield None

padded = list(paditers(a, b, c))
next_item = iter(padded).next
print zip(*((next_item(), next_item(), next_item())
         for _ in range(len(padded) // 3)))

rawDescription: Stretch columns to pair matched cells in rows


code:
import time

class trafficlight_timer:
    def __init__(self, side1):
        self.side = side1

    def trafficlight(self):
        s = 0
        b = 5
        t_max = 10
        print self.side,  "is green"
        if b > t_max:
            b = t_max
        while s <= 60:
            time.sleep(1)
            s += 1
            if s == b:
                print self.side, "is red"
                print self.side, "was green for %d seconds." % s
                print ""
                s = 65

instance_left = trafficlight_timer('left')
instance_left.trafficlight()

rawDescription: Printing a string and executing functions inside a class [Python]


code: print u', '.join([str(value) for value in unicode_values])

rawDescription: Taking input containing " or ' with argparse


code:
def load_on_show(self):
    idx = self.tabs.currentIndex()
    if idx == 1:
        url = "http://www.google.com"
        print url
        self.tab2.load_url(url)

rawDescription: Python PyQt QWebView load site in clicked tab


code:
import unittest

class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print 'setUpClass'
        cls.data = 123

    def test_one(self):
        print 'test_one'
        print self.data

    def test_two(self):
        print 'test_two'

if __name__ == "__main__":
    unittest.main()

rawDescription: How to set up a resource shared by several unit tests?

description: Set up a resource shared by several unit tests


code:
import csv
import pymssql

db = pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython')
cursor = db.cursor()

filename = "ttest.txt"
mynumbers = []

cursor.execute('truncate table tbl_contact')

with open(filename, 'r') as f:
    for line in f:
         try:
             values = line.strip().split('|')
             x,y,z,a = int(values[0]), values[1], values[2], values[3]
             cursor.execute('INSERT INTO tbl_contact VALUES (%s, %s, %s, %s)', (x,y,z,a))
             print "Record Inserted"
         except IndexError:
             print "Some error occurred."

db.commit()
db.close()

rawDescription: Only the last row is inserted to my sql database from a text file after splitting it with PYTHON


code:
import pandas as pd

mylist = [('t1', 0, 23),
          ('t1', 1, 24),
          ('t1', 2, 25),
          ('t2', 0, 22),
          ('t2', 1, 25),
          ('t2', 2, 26)]

df = pd.DataFrame(columns=['t1','t2'], index=[0,1,2])

print df
#    t1   t2
#0  NaN  NaN
#1  NaN  NaN
#2  NaN  NaN

for col, row, val in mylist:
    df[col][row] = val

print df
#   t1  t2
#0  23  22
#1  24  25
#2  25  26

#df.to_csv('filename.csv') # comma separated values
df.to_csv('filename.csv', sep=' ')

rawDescription: Re-arrange List of Tuple in Python

description: Re-arrange List of Tuple in Python


code:
import riak

client = riak.RiakClient()

test_bucket = client.bucket('test')

data1 = test_bucket.new('key1',data={'field1':'1data1','field2':'1data2','field3':1})
data1.store()

data2 = test_bucket.new('key2',data={'field1':'2data1','field2':'2data2','field3':2})
data2.store()

data3 = test_bucket.new('key3',data={'field1':'3data1','field2':'3data2','field3':3})
data3.store()

query = riak.RiakMapReduce(client).add('testbucket')
query.map(['custom_mr','mapcount'])

for result in query.run():
    print "%s" % (result)

rawDescription: What is the exact way to send erlang module and erlang function to the mapreduce face in python-riak client l


code:
import inspect
import django
print inspect.getabsfile(django)

rawDescription: where does django install in ubuntu

description: Where does django install in ubuntu


rawDescription: Get lists by reading file in Python

description: Get lists by reading file in Python


code: print df2.stack()

rawDescription: Python pandas: How do I drop specific levels in a hierarchical index if any column values are NaN?


code:
import numpy as np

x = np.random.rand(50)
k = np.random.rand(1000)
b = np.random.rand(1000)

y = np.outer(x, k) + b + np.random.normal(size=(50, 1000), scale=1e-10)

r = np.linalg.lstsq(np.c_[x, np.ones_like(x)], y)[0]

print np.allclose(r[0], k)
print np.allclose(r[1], b)

rawDescription: Efficient 1D linear regression for each element of 3D numpy array


code:
def no_recursion(inp_value):
    for i, val in enumerate(inp_value):
        # you probably have a good reason to test the index
        if i == 0:
            if val == 'valid':
                yield 'valid inp_value: %s' % val
            else:
                yield 'invalid inp: %s' % val
        else:
            yield 'index %s is %s valid inp_value' % (
                i,
                'a' if val == 'valid' else 'never'
            )

print tuple(no_recursion(inp_value))

rawDescription: Recursive function that return result from 'if' statement instead of 'else' statement


code:
import threading

def do_every (interval, worker_func, iterations = 0):
  if iterations != 1:
    threading.Timer (
      interval,
      do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
    ).start ()

  worker_func ()

def print_hw ():
  print "hello world"

def print_so ():
  print "stackoverflow"

# call print_so every second, 5 times total
do_every (1, print_so, 5)

# call print_hw two times per second, forever
do_every (0.5, print_hw)

rawDescription: Periodically execute function in thread in real time, every N seconds


code:
persons = [('id1', "Peter parker"), ('id2' ,"Richard Parker"), ('id3' ,"Parker Richard"), ('id4' ,"Aunt May")]
similarity = [[p1, p2, string_similarity(p1[1], p2[1])]
                for p1, p2 in itertools.combinations(persons, 2)]

similarity = sorted(similarity, key=lambda x: x[2], reverse=True)
for p1, p2, sim in similarity:
    print "{} - {}: {}".format(p1, p2, sim)  # p1[0], p2[0] to show ids only

rawDescription: Sorting a python dictionary after running an itertools function


code:
lod = []
populate_lod(lod, csv_fp)

pprint(lookup_lod(lod, Row=1))
pprint(lookup_lod(lod, Name='Aardvark'))
pprint(query_lod(lod, sort_keys=('Priority', 'Year')))
pprint(query_lod(lod, sort_keys=('Year', 'Priority')))
print len(query_lod(lod, lambda r:1997 <= int(r['Year']) <= 2002))
print len(query_lod(lod, lambda r:int(r['Year'])==1998 and int(r['Priority']) > 2))

rawDescription: Python: Data Structure for Maintaing Tabular Data in Memory?


code:
import pytest

@pytest.fixture
def make_1():
    return 1

@pytest.fixture
def make_2():
    return 2

@pytest.fixture
def all_makes(make_1, make_2):
    return (make_1, make_2)

def _test_thing(make):
    # Run your test
    pass

def test_main(all_makes):
    for make in all_makes:
        try:
            _test_thing(make)
        except AssertionError:
            print "Failed for make {}".format(make)
            raise

rawDescription: output of fixture in params in pytest

description: Output of fixture in params in pytest


code:
# test.py
from collections import OrderedDict
print {3: 4, "a": 2}
dict = OrderedDict
print {3: 4, "a": 2}

rawDescription: Override the {...} notation so i get an OrderedDict() instead of a dict()?


code:
import time
import sys
import urllib2
import urllib
from json import loads
import cookielib

base_url = "https://graph.facebook.com/search?q="
post_id = None
post_type = None
user_id = None
message = None
created_time = None

jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [
    ('Accept-Language', 'en-US,en;q=0.8'),]

def doit(hour):
    page = 1
    search_term = "\"Plastic Planet\""
    encoded_search_term = urllib.quote(search_term)
    print encoded_search_term
    type="&type=post"
    url = "%s%s%s" % (base_url,encoded_search_term,type)

    print url

    data = True
    while data:
        response = opener.open(url)
        opener.addheaders += [
            ('Referer', url) ]

        content = response.read()
        content = loads(content)

        print "=================================="
        for c in content["data"]:
            print c.keys()
        print "****************************************"

        if "paging" in content:
            print "current URL"
            print url
            print "next page!------------"
            url = content["paging"]["next"]
            print url
        else:
            print content
            print url
            data = False

doit(1)

rawDescription: Unable to retrieve search results from server side : Facebook Graph API usig Python


code:
answers = [["answer1a", "answer1b", "answer1c", "answer1d"],["answerna", "answernb", "answernc", "answernd"]]
questions = ['question1','questionn']
def validate(a,q):
 if a in answers[questions.index('question1')]:
  print 'Correct'
 else:
  print 'Try again'
validate('foobar','question1')
validate('answer1a','question1')
validate('answer1b','question1')

rawDescription: Python: How to use a similar operator to "or"/"in" in a list


code:
def find_matches():
    """
        Basic version: reads urls, but does not consider the semantic information of
        HTML header, body, etc. while computing duplicates.
    """

    from urllib2 import urlopen
    import hashlib

    urls = [ 'http://www.google.com', 'http://www.google.com/search']

    d = {}
    url_contents = {}
    matches = []
    for url in urls:
        c = urlopen(url)
        url_contents[url] = []
        while 1:
            r = c.read(4096)
            if not r: break
            md5 = hashlib.md5(r).hexdigest()
            url_contents[url].append(md5)
            if md5 in d:
                url2 = d[md5]
                matches.append((md5, url, url2))
            else:
                d[md5] = []
            d[md5].append(url)
    #print url_contents
    print matches

if __name__ == '__main__':
    find_matches()

rawDescription: The best way to check duplicated url in Python?

description: Check duplicated url in Python


code:
markup = 'Is this model too thin for Yves Saint Laurent? '
soup = BeautifulSoup(markup)
headline = soup.find(class_='cd__headline-text')
headline.string is None
print list(headline.strings)
print list(headline.stripped_strings)
print headline.get_text()
print headline.get_text(' - ', strip=True)
headline.contents
from bs4 import NavigableString
[el for el in headline.children if isinstance(el, NavigableString)]

rawDescription: How to obtain the content between a tag and it's ending in HTML using python' beautiful soup?

description: Obtain the content between a tag and it's ending in HTML using python' beautiful soup


rawDescription: How to print Radio Control information

description: Print Radio Control information


code:
from Tkinter import *

def run():
    # create the root and the canvas
    root = Tk()
    canvas = Canvas(root, width=300, height=200)
    canvas.pack()
    class Struct: pass
    canvas.data = Struct()
    # canvas.data.givenIngredients = set()

    prefInput(canvas)
    # and launch the app
    root.mainloop()

def var_states(varss):
    print "var_states", varss
    for var in varss:
        print var, var.get()

def prefInput(canvas):
    var1, var2 = IntVar(canvas), IntVar(canvas)
    Checkbutton(canvas, text="male", variable=var1).grid(row=1, sticky=W)
    varss = [var1, var2]
    Checkbutton(canvas, text="female", variable=var2).grid(row=2, sticky=W)
    Button(canvas, text='Show', command=lambda:var_states(varss)).grid(row=4, sticky=W, pady=4)

run()

rawDescription: Python Tkinter Checkbutton Issue

description: Python Tkinter Checkbutton Issue


code:
def foo():
    print "foo() called"

def bar():
    return foo()

bar()
result = bar()

rawDescription: Do python functions care whether or not you use their output?


code:
base_url = "https://www.webaddress.com/url/urls.do?filename={filename}&filetype=l"
soup = BeautifulSoup(c)

print [base_url.format(filename=urlparse.parse_qs(urlparse.urlparse(img['src']).query)['filename'][0])
       for img in soup.find_all('img')]

rawDescription: How do you split in beautiful soup based on two parameters - python

description: Split in beautiful soup based on two parameters - python


code:
import re

def reducer(text):  # Catch statements and add them to a dictionary
    catched = dict()
    for v, k in re.findall(r'The\s(?P<variable>\w+)\sis\s(?P<value>\w+)', text):
        try:
            catched[k].append(v)
        except KeyError:
            catched[k] = [v]
    return catched

def comma_and(ls):  # Create human-like enumeration with words
    output = [ls[0]]
    for i in xrange(1, len(ls)):
        output.append('%s%s' % (' and ' if i == len(ls) - 1 else ', ', ls[i]))
    return ''.join(output)

def rephrase(text):  # Rephrase separated statements into one sentence
    stmnts = reducer(text)
    part1 = str()
    part2 = str()
    for key in stmnts:
        if len(stmnts[key]) <= 1:
            part2 = 'but the {variable} is {value}.'.format(
                variable=stmnts[key][0], value=key)
        else:
            part1 = 'The {variables} are {value}'.format(
                variables=comma_and(stmnts[key]), value=key)
    print part1 + ' ' + part2

rawDescription: Reduce redundant block of text

description: Reduce redundant block of text


code:
results = [('object%d' % i, '111.111.5.%d' % i) for i in range(1,8)]

key = '111.111.5.4'
result = None
for t in results:
    if t[1] == key:
        result = t

print result

rawDescription: Get item with value from tuple in python


code:
def fun():
    print "in fun"

eval("fun()")

x="fun()"
eval(x)

rawDescription: Execute a string as a command

description: Execute a string as a command


code:
def SubFib(startNumber, endNumber):
    n = 0
    cur = f(n)
    while cur <= endNumber:
        if startNumber <= cur:
            print cur
        n += 1
        cur = f(n)

rawDescription: How to write the Fibonacci Sequence in Python

description: Write the Fibonacci Sequence in Python


code:
from pyparsing import *
ParserElement.enablePackrat()
import string

name = Word(alphanums)

filter = (Combine(oneOf(list(string.ascii_lowercase)) + Optional('~')) +
          dblQuotedString.setParseAction(removeQuotes))
# convert parsed filter to dict
filter.setParseAction(lambda t:{t[0]:t[1]})

expr = operatorPrecedence(filter | name,
            [
            (oneOf('. % -'), 2, opAssoc.LEFT),
            ('+', 2, opAssoc.LEFT),
            ])

test = r'foo . bar - (a"hello \"world\"" % (b~"foo" + bar) . (fooZ.(bar2+(c"foo bar".d"bar foo"))))'

print expr.parseString(test, parseAll=True).asList()

rawDescription: How to write a simple lexer/parser for user input commands in Python?

description: Write a simple lexer/parser for user input commands in Python


code:
x_val = 45
points = polygon_intersect_x(p, x_val)
minmax = [points[0], points[-1]]  # The values are already sorted
print minmax
# [-12.5, 88.5714286]

rawDescription: Python - Return y coordinates of polygon path given x


code:
from collections import Counter
from nltk.metrics import ConfusionMatrix

ref  = 'DET NN VB DET JJ NN NN IN DET NN'.split()
tagged = 'DET VB VB DET NN NN NN IN DET NN'.split()
cm = ConfusionMatrix(ref, tagged)

print cm

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

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

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

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

for i in sorted(labels):
    if true_positives[i] == 0:
        fscore = 0
    else:
        precision = true_positives[i] / float(true_positives[i]+false_positives[i])
        recall = true_positives[i] / float(true_positives[i]+false_negatives[i])
        fscore = 2 * (precision * recall) / float(precision + recall)
    print i, fscore

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

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


code:
import re
p = re.compile(ur'^r\s(?P<nickname>\b\S+\b).*\n^s\b.*?(?P<type>\bGuard\b).*\n^v.*\n^w\s.*?(?P<bandwidth>\b[0-9]+\b)', re.MULTILINE | re.VERBOSE)
test_str = u"r Tor4ever AAcif1htILdru0BO0qX7OwGVhAU oHlbWBdaN3+QSleqBVL9/yAdcRs 2014-07-31 21:42:43  \ns Exit Fast Guard HSDir Running V2Dir Valid  \nv Tor 0.2.4.21  \nw Bandwidth=231  \np reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999  \nr Tornin AA8YrCza5McQugiY3J4h5y4BF9g vNRd1kyQ0i9UsVwYq5YFPHJi3jw 2014-08-01 00:26:18  \ns Fast Guard HSDir Named Running Stable V2Dir Valid  \nv Tor 0.2.4.23  \nw Bandwidth=713  \np reject 1-65535"

re.findall(p, test_str)

rawDescription: Use of re.compile and groups in a regular expression in python


rawDescription: How do I parse a CSV list enclosed in brackets in Python?

description: Parse a CSV list enclosed in brackets in Python


code:
import lxml.etree as ET

root = ET.fromstring(xml)

print root.xpath('//alt[lang("fr")]')

rawDescription: How do I find an xml node that does not have an attribute

description: Find an xml node that does not have an attribute


code:
import re

with open('/tmp/test.txt','r') as f:
    data=''.join(f.readlines())

m=re.search(r'^(&.*!)\s*?\n',data,re.S | re.M)
if m: print m.group(1)

rawDescription: Extract string between characters from a txt file in python


code:
a='\x05\x03\x87'
print a
print repr(a)

rawDescription: Python - string of binary escape sequences as literal


code:
from sklearn.utils.extmath import cartesian

# Your data:
df1 = pd.DataFrame({'quality1':list('ABC'), 'value':[1,2,3]})
df2 = pd.DataFrame({'quality2':list('DEF'), 'value':[1,10,100]})

# Make the matrix of labels:
dfm = pd.DataFrame(cartesian((df1.quality1.values, df2.quality2.values)),
                   columns=['quality1', 'quality2'])

# Multiply values:
dfm['value'] = df1.value.values.repeat(df2.value.size) * pd.np.tile(df2.value.values, df1.value.size)

print dfm.set_index(['quality1', 'quality2'])

rawDescription: How to "multiply" python pandas dataframes (as if they were vectors)?


code:
from Bio import SeqIO

with open("fasta") as fh:
    fasta = fh.read().splitlines()

seen = set()

for record in SeqIO.parse(open('fastq'), 'fastq'):
    seq = str(record.seq)
    if seq in fasta and seq not in seen:
        seen.add(seq)
        print record.format('fastq')

rawDescription: find unique first top and bottom lines of fastq file from fasta file

description: Find unique first top and bottom lines of fastq file from fasta file


code: print [td.span.text for td in soup.find_all('td', {'class': 'size-price'})]

rawDescription: Using REGEX to match elements between lines in Python


code: print "".join(["(" + str(r) + "," + str(c) + ")\n" for (r,c) in skel_coords])

rawDescription: How can I find endpoints of binary skeleton image in OpenCV?

description: Find endpoints of binary skeleton image in OpenCV


code:
def get_side_effect(self, key):
    return [cdict for cdict in my_list if cdict["task"] == key]

print obj.get_side_effect("send-email")

rawDescription: Python: Find dict in array based on value of key


rawDescription: How do I turn a tuple into a tuple of tuples?

description: Turn a tuple into a tuple of tuples


code:
print u'\u2022'
print len(u'\u2022')
map(unicodedata.name, u'\u2022')

rawDescription: How do I split a unicode string on code points in python? (eg. \u00B7 or \u2022)?

description: Split a unicode string on code points in python? (eg. \u00B7 or \u2022)


code:
f=f_lrc.split('\n')
lyrics = f[10:]
first_part = f[:10]
yrics = re.sub(ur'<\d{1,2}:\d{2}\.\d{1,2}>', '', '\n'.join(lyrics))
last = '\n'.join(first_part) + yrics

rawDescription: How to replace all except first 10 elements matched with regex?

description: Replace all except first 10 elements matched with regex


code:
from multiprocessing.managers import BaseManager, NamespaceProxy

class TestClass(object):
    def __init__(self, a):
        self.a = a

    def b(self):
        print self.a

class MyManager(BaseManager): pass

class TestProxy(NamespaceProxy):
    # We need to expose the same __dunder__ methods as NamespaceProxy,
    # in addition to the b method.
    _exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'b')

    def b(self):
        callmethod = object.__getattribute__(self, '_callmethod')
        return callmethod('b')

MyManager.register('test', TestClass, TestProxy)

if __name__ == '__main__':
    manager = MyManager()
    manager.start()
    t = TestClass(1)
    print t.a
    mt = manager.test(2)
    print mt.a
    mt.a = 5
    mt.b()

rawDescription: Accessing an attribute of a multiprocessing Proxy of a class


code:
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

class MyApp (object):
    def __init__(self):
        self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", gtk.main_quit )
        self.entry = gtk.Entry()
        self.window.selection_convert("PRIMARY", "STRING")
        self.window.connect("selection_received", self.selection_received)
        self.window.add(self.entry)
        self.window.show_all()
    # Signal handler called when the selections owner returns the data
    def selection_received(self, widget, selection_data, data):
        print 'selection_data.type=%r'%selection_data.type
        # Make sure we got the data in the expected form
        if str(selection_data.type) == "STRING":
            self.entry.set_text("Selected Text is : %s"  % selection_data.get_text())

        elif str(selection_data.type) == "ATOM":
            # Print out the target list we received
            targets = selection_data.get_targets()
            for target in targets:
                name = str(target)
                if name != None:
                    self.entry.set_text("%s" % name)
                else:
                    self.entry.set_text("(bad target)")
        else:
            self.entry.set_text("Selection was not returned as \"STRING\" or \"ATOM\"!")

        return False
    def main(self):
        gtk.main()

app=MyApp()
app.main()

rawDescription: Getting selected text (python-gtk)

description: Getting selected text (python-gtk)


code:
import re

a = open('test.txt').read()

a = a.replace('\n', '').replace('\t', ' ')
a = a.replace('{', ':{').replace('}', '},\n')

b =  re.sub('(\".*?\") *(\".*?\")', r'\1:\2,', a)

b = "{%s}" % b

dictName = eval(b)
for key in dictName['items_game']['community_market_item_remaps']['Decoder Ring']:
    print key

rawDescription: How do I convert a text file received via a web call to a dictionary?

description: Convert a text file received via a web call to a dictionary


code:
import numpy as np

def cluster_indices(cluster_assignments):
    n = cluster_assignments.max()
    indices = []
    for cluster_number in range(1, n + 1):
        indices.append(np.where(cluster_assignments == cluster_number)[0])
    return indices

if __name__ == "__main__":
    from scipy.cluster.hierarchy import fclusterdata

    # Make some test data.
    data = np.random.rand(15,2)

    # Compute the clusters.
    cutoff = 1.0
    cluster_assignments = fclusterdata(data, cutoff)

    # Print the indices of the data points in each cluster.
    num_clusters = cluster_assignments.max()
    print "%d clusters" % num_clusters
    indices = cluster_indices(cluster_assignments)
    for k, ind in enumerate(indices):
        print "cluster", k + 1, "is", ind

rawDescription: How do I correlate my original data with clustered data

description: Correlate my original data with clustered data


code:
num, mydict = 10, {"Words":['hi', 'bye', 'goodbye']}
print {k: [i for i in v for j in range(num)] for k, v in mydict.items()}

rawDescription: how to repeatedly append a string?

description: Repeatedly append a string


code:
class Compensation:
    def bonus(self, a):
        return 10000 + a
    def salary(self):
        return 20000
    def stack(self, a=2, b=3):
        return a+b

class employee:
    def __init__(self):
        self.comp = Compensation()

e = employee()

for name in "bonus", "salary", "stack":
    def outer(name):
        def f(self, *args, **kw):
            return getattr(self.comp, name)(*args, **kw)
        f.__name__ = name
        return f
    setattr(employee, name, outer(name))

print e.bonus(9)
print e.salary()
print e.stack(b="def", a="abc")

rawDescription: Python: Dynamically adding function to a class, whose name is contained in a string


code:
with open('a.x') as f:
     vals=list(map(int, f))
     res=[vals[i: i+9] for i in range(0, 81, 9)]
     print res

rawDescription: How to read a text file into a 9x9 matrix?

description: Read a text file into a 9x9 matrix


code:
import zmq
import time
import sys
import random
from  multiprocessing import Process

def server_push(port="5556"):
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.bind("tcp://127.0.0.1:%s" % port)
    print "Running server on port: ", port
    time.sleep(1.0)
    # serves only 5 request and dies
    for reqnum in range(10):
        if reqnum < 6:
            socket.send("Continue")
        else:
            socket.send("Exit")
            print 'Push server sent "Exit" signal'
            break
        time.sleep(0.4)

def server_pub(port="5558"):
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://127.0.0.1:%s" % port)
    socket.setsockopt(zmq.HWM, 1000)
    publisher_id = random.randrange(0,9999)
    print "Running server on port: ", port
    time.sleep(1.0)
    # serves only 5 request and dies
    for reqnum in range(10):
        # Wait for next request from client
        topic = random.randrange(8,10)
        messagedata = "server#%s" % publisher_id
        print "%s %s" % (topic, messagedata)
        socket.send("%d %s" % (topic, messagedata))
        time.sleep(0.4)

class Client:
    def __init__(self,port_push, port_sub):
        self.port_push = port_push
        self.port_sub = port_sub
        # Initialize poll set

    def __call__(self):
        time.sleep(0.5)
        print 'hello from class client!'
        context = zmq.Context()
        self.socket_pull = context.socket(zmq.PULL)
        self.socket_pull.connect ("tcp://127.0.0.1:%s" % self.port_push)
        print "Connected to server with port %s" % self.port_push
        self.socket_sub = context.socket(zmq.SUB)
        self.socket_sub.connect ("tcp://127.0.0.1:%s" % self.port_sub)
        self.socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
        print "Connected to publisher with port %s" % self.port_sub

        poller = zmq.Poller()
        poller.register(self.socket_pull, zmq.POLLIN)
        poller.register(self.socket_sub, zmq.POLLIN)
        # Work on requests from both server and publisher
        should_continue = True
        print "listening"
        while should_continue:
            # print "hello"
            socks = dict(poller.poll())
            # print poller
            if self.socket_pull in socks and socks[self.socket_pull] == zmq.POLLIN:
                message = self.socket_pull.recv()
                print "Recieved control command: %s" % message
                if message == "Exit":
                    print "Recieved exit command, client will stop recieving messages"
                    should_continue = False

            if self.socket_sub in socks and socks[self.socket_sub] == zmq.POLLIN:
                string = self.socket_sub.recv()
                topic, messagedata = string.split()
                print "Processing ... ", topic, messagedata

def client(port_push, port_sub):
    print 'hello from function client!'
    context = zmq.Context()
    socket_pull = context.socket(zmq.PULL)
    socket_pull.connect ("tcp://127.0.0.1:%s" % port_push)
    print "Connected to server with port %s" % port_push
    socket_sub = context.socket(zmq.SUB)
    socket_sub.connect ("tcp://127.0.0.1:%s" % port_sub)
    socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
    print "Connected to publisher with port %s" % port_sub
    # Initialize poll set
    poller = zmq.Poller()
    poller.register(socket_pull, zmq.POLLIN)
    poller.register(socket_sub, zmq.POLLIN)
    # Work on requests from both server and publisher
    should_continue = True
    while should_continue:
        socks = dict(poller.poll(1000))
        if socket_pull in socks and socks[socket_pull] == zmq.POLLIN:
            message = socket_pull.recv()
            print "Recieved control command: %s" % message
            if message == "Exit":
                print "Recieved exit command, client will stop recieving messages"
                should_continue = False

        if socket_sub in socks and socks[socket_sub] == zmq.POLLIN:
            string = socket_sub.recv()
            topic, messagedata = string.split()
            print "Processing ... ", topic, messagedata

if __name__ == "__main__":
    # Now we can run a few servers
    server_push_port = "5556"
    server_pub_port = "5558"
    Process(target=server_push, args=(server_push_port,)).start()
    Process(target=server_pub, args=(server_pub_port,)).start()
    # Process(target=client,args=(server_push_port,server_pub_port)).start()
    Process(target=Client(server_push_port,server_pub_port)).start()

rawDescription: ZMQ Poller not working within Class Instance


code:
called = False

def g1(func):
    def g2(*args):
        print args
        return None

    global called

    if not called:
        called = True
        func(5)
    else:
        return g2

f1(g1) # prints "(5,)"

rawDescription: Decoding a Cryptic Python statement

description: Decoding a Cryptic Python statement


code:
import string

length = random.randint(7, 14)

set1 = string.ascii_uppercase                   # A-Z
set2 = string.digits                            # 0-9
set3 = string.digits + string.ascii_letters     # a-z A-Z 0-9
build = [set1, set2] + [set3] * (length - 2)    # 1 uppercase, one digit, reset from full set
random.shuffle(build)                           # shuffle the order

password = []
index = 0
last = ''

while len(password) < length:                   # keep appending until length is reached
    choice = random.choice(build[index])
    if choice != last:
        password.append(choice)
        index += 1
        last = choice

print ''.join(password)

rawDescription: Generating a password with case sensitivity criteria


code:
if True:
    varlst = [addTen(itm) for itm in varlst]
print varlst

rawDescription: Most Pythonic way to update variables if condition is met


code:
try:
    import json
except:
    import simplejson as json
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false"
contents = urllib2.urlopen(url).read()
json_array = json.loads(contents)
print repr(json_array)

rawDescription: how can i use a json output in python

description: Use a json output in python


code:
import httplib2

header = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

fullurl = "http://ws.parlament.ch/votes/councillors?councillorNumberFilter=2565&format=json"
http = httplib2.Http(".cache")

response, content = http.request(fullurl, "GET", headers=header)
print content

rawDescription: How to download webpage in python, url includes GET parameters

description: Download webpage in python, url includes GET parameters


code:
#! /usr/bin/python2.7

import datetime as dt
import random

dict_size = 1000000
num_iterations = 100

d = {i: i for i in xrange(dict_size)}

def f():
    k = random.randint(0,dict_size-1)
    return (k in d)

def g():
    k = random.randint(0,dict_size-1)
    return (k in d.keys())

def test(func):
    t = dt.datetime.utcnow()
    for i in xrange(num_iterations):
        func()
    print "%s --> %1.6f s" % (func, (dt.datetime.utcnow()-t).total_seconds())

test(f)
test(g)

rawDescription: Python is 'key in dict' different/faster than 'key in dict.keys()'


code:
l = [("first","foo"),("second","bar"),("anything","you get the point")]
bar = Foo(l)

print bar.first
#foo
print bar.second
#bar
print bar.anything
#you get the point

rawDescription: Python extremely dynamic class properties


code:
z = {'data':{'id':1,'title':'home','address':{'street':'some road','city':'anycity','postal':'somepostal'}}}

def printItems(dictObj, indent):
    print '  '*indent + '
\n'
    for k,v in dictObj.iteritems():
        if isinstance(v, dict):
            print '  '*indent , '
', k, ':', '
'
            printItems(v, indent+1)
        else:
            print ' '*indent , '
', k, ':', v, '
'
    print '  '*indent + '
\n'

printItems(z,0)

rawDescription: python: serialize a dictionary into a simple html output

description: Python: serialize a dictionary into a simple html output


code:
# get all of the users UserStories:
user = request.user
stories = UserStory.objects.filter(user=user).select_related('story')

# print all of the stories:

for s in stories:
    print s.story.title
    print s.story.thetext

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


code:
import httplib2, os, sys, tempfile, urllib, urlparse
import oauth2 as oauth
#httplib2.debuglevel=1

# this php script writes oauth_verifier to /tmp/verifier.txt
oauth_callback = 'http://localhost/api.php'

scope = 'https://docs.google.com/feeds/'
xoauth_displayname = 'Adam\'s API Test'
url = 'https://www.google.com/accounts/OAuthGetRequestToken?scope=%s&oauth_callback=%s&xoauth_displayname=%s' % (scope, oauth_callback, xoauth_displayname)

######## OAUTH: GET REQUEST TOKEN #############
consumer = oauth.Consumer('anonymous','anonymous')
client = oauth.Client(consumer)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetRequestToken OK'
else:
    print 'OAuthGetRequestToken status: %s' % resp['status']
    print content
    sys.exit(1)

######## OAUTH: AUTHORIZE TOKEN ###############
oauth_token = urlparse.parse_qs(content)['oauth_token'][0]
oauth_token_secret = urlparse.parse_qs(content)['oauth_token_secret'][0]
url = 'https://www.google.com/accounts/OAuthAuthorizeToken?hd=default&oauth_token=%s' % urllib.quote_plus(oauth_token)
print 'Visit this URL in your browser: %s' % url
raw_input('Press ENTER once you have granted access...')

######## OAUTH: GET ACCESS TOKEN ##############
verifier = file('/tmp/verifier.txt').read().rstrip()
url = 'https://www.google.com/accounts/OAuthGetAccessToken?oauth_token=%s&oauth_verifier=%s' % (oauth_token, verifier)
client.token = oauth.Token(oauth_token, oauth_token_secret)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetAccessToken OK'
    print content
else:
    print 'OAuthGetAccessToken status: %s' % resp['status']
    print content
    sys.exit(1)

rawDescription: Google OAuth problem: invalid_signature

description: Google OAuth problem: invalid_signature


code:
for test in tests:
    print test, no_curly_brackets(test)

rawDescription: Verifying that a string does not contain curly brackets other than those used for formatting


code:
from collections import Counter
from operator import add
data = {
    'level1a': {'level2a':[1,2,3]},
    'level1b': {'level2b':[4,5,6]},
    'level1c': {'level2a':[7,8,9]}
}
c = Counter()
dic = {}
for k,v in data.iteritems():
    for k1, v1 in v.iteritems():
        c[k1] += 1
        val = dic.setdefault(k1, [0]*len(v1))
        dic[k1] = map(add, v1, val)

for k,v  in c.iteritems():
    if v > 1:
        print dic[k]

rawDescription: Find matching keywords in nested dictionary


code:
import itertools
first = 'Mineral'
for k, v in itertools.chain([(first, layersListDict[first])],
                            ((k,v) for (k,v) in layerListDict.iteritems() if k != first)):
    print k, v

rawDescription: How to Re-arrange items in a Python Dictionary during For Loop?


code:
class Node(object):
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next
    def __str__(self):
        return str(self.data)

def create_linked_list(n):
    """Creating linked list for the given
       size"""
    linked_list = Node(1)
    head = linked_list
    for i in range(2, n):
        head.next = Node(i)
        head = head.next
    return linked_list

def print_linked_list(node):
    """To print the linked list in forward"""
    while node:
        print '[',node,']','[ref] ->',
        node = node.next
    print '-> None'

def find_middle1(node):
    tick = False
    half = node
    while node:
        node = node.next
        if tick:
            half = half.next
        tick = not tick
    return "Middle node is %s" % str(half)

def find_middle2(node):
    list = []
    while node:
        list.append(node)
        node = node.next
    return "Middle node is %s" % str(list[len(list)/2])

node = create_linked_list(10)
print_linked_list(node)

print find_middle1(node)
print find_middle2(node)

rawDescription: How to find middle element of linked list in one pass in python?

description: Find middle element of linked list in one pass in python


code:
from datetime import datetime, date
A = datetime.strptime("2013-11-09 18:17:53.196126", "%Y-%m-%d %H:%M:%S.%f")
B = date(*map(int, "2013-11-01".split("-")))
print A
print B
print A.date() - B

rawDescription: finding difference in days between two dates

description: Finding difference in days between two dates


code:
from collections import defaultdict

matrix = defaultdict(int)
for edge in edges:
    matrix[edge] += 1

print matrix['a', 'b']

rawDescription: Adjacency List and Adjacency Matrix in Python


code:
import socket

sock = socket.socket()
sock.bind(('', 8080))
sock.listen(5)
client, adress = sock.accept()

print "Incoming:", adress
print client.recv(1024)
print

client.send('HTTP/1.0 200 OK\r\n')
client.send("Content-Type: text/html\r\n\r\n")
client.send('
Hello World')
client.close()

print "Answering ..."
print "Finished."

sock.close()

rawDescription: Sending http headers with python

description: Sending http headers with python


code:
def get_node(x, y, z, side):
    return [(x+side,y,z), (x,y+side,z), (x,y,z+side),
            (x-side,y,z), (x,y-side,z), (x,y,z-side)]

seen_nodes = set()

def goto_next_nodes(x, y, z, cube_side, next_nodes, boundary_x, boundary_y, boundary_z):
    for node in next_nodes:
        if node not in seen_nodes:
            seen_nodes.add(node)
            if 0<=node[0]<=boundary_x and 0<=node[1]<=boundary_y and 0<=node[2]<=boundary_z:
                 print node
                 x,y,z=node[0],node[1],node[2]
                 next_nodes=get_node(x,y,z,cube_side)
                 goto_next_nodes(x,y,z,cube_side,next_nodes,boundary_x,boundary_y,boundary_z)
    return

def display_fcc(cube_side,boundary_x,boundary_y,boundary_z):
     x=y=z=0
     next_nodes=get_node(x,y,z,cube_side)
     goto_next_nodes(x,y,z,cube_side,next_nodes,boundary_x,boundary_y,boundary_z)

display_fcc(5,10,10,10)

rawDescription: Infinite loop in path finding

description: Infinite loop in path finding


code:
import validictory
schema = {"type":"object","properties":{"field1": {"type":"string", "minLength":3},"field2":{"type":"integer"}}}
try:
    validictory.validate({'field1':"ab",'field2':123},schema)
except validictory.FieldValidationError as e:
    print "field which failed validation:",e.fieldname

rawDescription: Python Validictory- Field Validation

description: Python Validictory- Field Validation


code:
import decorator

@decorator.decorator
def args_as_ints(f, *args, **kwargs):
    args = [int(x) for x in args]
    kwargs = dict((k, int(v)) for k, v in kwargs.items())
    return f(*args, **kwargs)

@args_as_ints
def funny_function(x, y, z=3):
    """Computes x*y + 2*z"""
    return x*y + 2*z

print funny_function("3", 4.0, z="5")
# 22
help(funny_function)
# Help on function funny_function in module __main__:
#
# funny_function(x, y, z=3)
#     Computes x*y + 2*z

rawDescription: Preserving signatures of decorated functions


code:
import re
# match characters from ¿ to the end of the JSON-encodable range
exclude = re.compile(ur'[\u00bf-\uffff]')

def isprintable(s):
    return not bool(exclude.search(s))

rawDescription: Check printable for Unicode

description: Check printable for Unicode


code:
from pprint import pprint
from collections import OrderedDict
import re

data = """Police Response: 11/6/2012 1:34:06 AM   Incident Desc: Traffic Stop OFC:    Received: 11/6/2012 1:34:06 AM
Disp: PCHK  Location: CLEAR LAKE RD&GREEN HILL RD
Event Number: LLS121106060941   ID: 60941   Priority: 6 Case No:
Police Response:    Incident Desc: Theft    OFC:    Received: 11/6/2012 1:43:35 AM
Disp: CSR   Location: SCH BLACHLY
Event Number: LLS121106060943   ID: 60943   Priority: 4 Case No:
Police Response: 11/6/2012 1:47:47 AM   Incident Desc: Suspicious Vehicle(s)    OFC:        Received: 11/6/2012 1:47:47 AM
Disp: FI    Location: KIRK RD&CLEAR LAKE RD
Event Number: LLS121106060944   ID: 60944   Priority: 6 Case No: """

lines = data.splitlines()
cases = ['\n'.join(lines[i:i+3]) for i in range(0, len(lines), 3)]
pattern = '(Police Response|Incident Desc|OFC|Received|Disp|Location|Event Number|ID|Priority|Case No):'
rows = []
for case in cases:
    pairs =  re.split(pattern, case)[1:]
    rows.append(OrderedDict((pairs[i*2], pairs[i*2+1]) for i in range(10)))

for i, row in enumerate(rows):
    print '============== {} =============='.format(i)
    pprint(row.items())

rawDescription: Parsing a multi-line data file with Python


code:
d={'a':6, 'b':"hello", 'c':set()}
exec '\n'.join("%s=%r"%i for i in d.items())
a
b
c

rawDescription: Is there a way to extract a dict in Python into the local namespace?


code:
with open("999A.txt") as f:
    for line in f:
        line = line.strip()
        if line.startswith("|"):
            print line

rawDescription: grabbing info from a table with loops (python)

description: Grabbing info from a table with loops (python)


code:
from itertools import islice
for line in islice(f, 17, 34):
    print line

rawDescription: Memory-efficent way to iterate over part of a large file


code:
import operator
from timeit import timeit

data = [
    'Option A',
    'Option B',
    'Blabla',
    'Some text',
]

desired_output = [1, 2, 0, 3]

class Item:
    def __init__(self, label):
        self.label = label

col = [Item(d) for d in data]

def orig():
    flt_neworder = [
        x[1] for x in sorted(
            zip(
                [x[0] for x in sorted(enumerate(col), key=lambda x: x[1].label)],
                range(len(col))
            )
        )
    ]

    assert flt_neworder == desired_output

def origmod():
    flt_neworder = [
        x[1] for x in sorted(
            zip(
                [x[0] for x in sorted(enumerate(data), key=lambda x: x[1])],
                range(len(data))
            )
        )
    ]

    assert flt_neworder == desired_output

def orignew():
    flt_neworder = list({k: i for i, (k, v) in enumerate(sorted(enumerate(data), key=operator.itemgetter(1)))}.values())
    assert flt_neworder == desired_output

def orig3():
    flt_neworder = [None] * len(col)
    for j, (_, i) in enumerate(sorted(zip((item.label for item in col), range(len(col))))): flt_neworder[i] = j

    assert flt_neworder == desired_output

def rewrite():
    idata = list(enumerate(data))
    sdata = sorted(idata, key=lambda x: x[1])
    flt_neworder = [sdata.index(x) for x in idata]

    assert flt_neworder == desired_output

def rewriteop():
    idata = list(enumerate(data))
    sdata = sorted(idata, key=operator.itemgetter(1))
    flt_neworder = [sdata.index(x) for x in idata]

    assert flt_neworder == desired_output

def rewriteuniq():
    sdata = sorted(data)
    flt_neworder = [sdata.index(x) for x in data]

    assert flt_neworder == desired_output

for fn in (orig, origmod, orignew, orig3, rewrite, rewriteop, rewriteuniq):
    print fn.__name__ + ':', timeit(fn)

rawDescription: Order mapping - more efficient Python solution for sorting?


code:
def split_list(a_list, number_of_splits):
    step = len(a_list) / number_of_splits + (1 if len(a_list) % number_of_splits else 0)
    return [a_list[i*step:(i+1)*step] for i in range(number_of_splits)]

l = [1, 8, 2, 3, 4, 5, 6, 7, 1, 5, 3, 1, 2, 5]
print l
print split_list(l, 3)
print split_list(l, 2)

rawDescription: Python split a list into smaller lists depending on original list length


code:
import os
if not os.listdir('C:/Test'):
    print "empty"

rawDescription: How to test for an empty directory, no matter what its size, in Python?

description: Test for an empty directory, no matter what its size, in Python


code:
#!/usr/bin/env python

# python script which needs an environment variable and runs as a cron job
import datetime
import os

test_environ = os.environ["TEST_ENV"]
print "Cron job has run at %s with environment variable '%s'" %(datetime.datetime.now(), test_environ)

rawDescription: Running cron python jobs within docker

description: Running cron python jobs within docker


code:
from testapp.views import XForm; f = XForm(initial={'genders': ['male', 'female']}); print f.selected_genders_labels()
from testapp.views import XForm; f = XForm(initial={'genders': ['female']}); print f.selected_genders_labels()
from testapp.views import XForm; f = XForm(initial={'genders': []}); print f.selected_genders_labels()
from testapp.views import XForm; f = XForm(); print f.selected_genders_labels()

rawDescription: Django: Display values of the selected multiple choice field in a template


code: print map(lambda x: x[0], sorted(nd.items(), key=lambda x: x[1]))

rawDescription: dictionary help for python writing a code

description: Dictionary help for python writing a code


code:
df_dict = df.groupby('State').agg('sum')['Count'].to_dict()
print df_dict

rawDescription: Dictionary Creation from DataFrame, Pandas, Python


code: print "%06d" % number

rawDescription: How to add enough zeros before a number in order to have six digits for this number using Python?

description: Add enough zeros before a number in order to have six digits for this number using Python


code:
#!/usr/bin/env python
import antlr3
from antlr3 import *
from TLexer import *
from TParser import *

input = 'a + (1.0 + 2) * 3'
lexer = TLexer(antlr3.ANTLRStringStream(input))
parser = TParser(antlr3.CommonTokenStream(lexer))
print '{0} = {1}'.format(input, parser.parse_with({'a':42}))

rawDescription: Two basic ANTLR questions

description: Two basic ANTLR questions


code:
import csv
from datetime import datetime

file1reader = csv.reader(open("file1.csv"), delimiter=",")
file2reader = csv.reader(open("file2.csv"), delimiter=",")

header1 = file1reader.next() #header
header2 = file2reader.next() #header

for Code, DTime in file1reader:
    DTime = datetime.strptime(DTime, "%Y-%m-%d %H:%M")
    for id_, D, Sym, DateTime, Bid, Ask in file2reader:
        DateTime = datetime.strptime(DateTime, "%Y-%m-%d %H:%M:%S")
        if DateTime>=DTime: # found it
            print DateTime, Bid, Ask   # output data
            break                      # break and continue where we left next time

rawDescription: loop through rows of one csv file to find corresponding data in another

description: Loop through rows of one csv file to find corresponding data in another


code:
# enumerate the days in the week
for i, day in enumerate(week):
    print "day %d" % i
    # show values in sorted order
    for key in sorted(day):
        print "  - %s\t: %s" % (key, day[key])

rawDescription: Extract resulted list data to a xml file in python


rawDescription: Repeat a scipy csr sparse matrix along axis 0


code:
import dis

# Here are the things we might want to do
def do_something_a():
    print 'I did a'

def do_something_b():
    print 'I did b'

def do_something_c():
    print 'I did c'

# Case 1
def f1(x):
    if x == 1:
        do_something_a()
    elif x == 2:
        do_something_b()
    elif x == 3:
        do_something_c()

# Case 2
FUNC_MAP = {1: do_something_a, 2: do_something_b, 3: do_something_c}
def f2(x):
    FUNC_MAP[x]()

# Show how the functions execute
print 'Case 1'
dis.dis(f1)
print '\n\nCase 2'
dis.dis(f2)

rawDescription: Why is dict faster than if-else in python?


code:
import requests
def hmm(self):
    return 'ok'

requests.models.Response.hmm = hmm
r = requests.get('http://bbc.co.uk')
print r
r.hmm()
requests.__version__

rawDescription: Python monkey patching

description: Python monkey patching


code:
tree = ('b', ('a', None, None), ('z', ('c', None, None), ('zz', None, None)))

def breadth_first(tree):
    root, left_sub_tree, right_sub_tree = tree
    yield root
    for subtree in (left_sub_tree, right_sub_tree):
        if subtree is not None:
            for element in breadth_first(subtree):
                yield element

    print list(breadth_first(tree))

rawDescription: how can i return the nodes in pre-order from encoded tuple

description: Return the nodes in pre-order from encoded tuple