code:
upperBound = input("Enter the upper bound number: ")

"""Create a list named prime of booleans, all set to True, for numbers between 0 and the upper bound"""
prime = [True]*(upperBound + 1) #List of booleans all set to True

"""set prime[0] and prime[1] to False"""
prime[0] = False #0 is not prime
prime[1] = False #1 is not prime

print "Generating all primes between 2 and", upperBound

"""let p loop over all values between 2 and the upper bound (inclusive)"""
for p in range(2, upperBound + 1): #Loop through all the values between 2 and the upper bound, inclusive
    print "Checking", str(p)
    if prime[p] == True: #if the number is prime
        k = p + p #k is the first multiple of the prime number
        print " ", str(p), "is prime"
        for x in range(k, upperBound + 1, p): #Iterate through all the multiples of the prime, less than the upper bound
            print " ", str(x), "is not prime"
            prime[x] = False #Mark the multiples as composite

"""The prime list now has a True value for those numbers that are prime"""

"""Generate the result list with a loop adding numbers to the list"""
primeNumbers = []
for x in range(len(prime)): #Iterate through the boolean list
    if prime[x] == True: #if the number is prime
        primeNumbers.append(x) #add it to the list of prime numbers

print primeNumbers

rawDescription: How to find a prime number with an upperbound and store in an array?

description: Find a prime number with an upperbound and store in an array


code:
import xmltodict
from collections import OrderedDict

xmldata = """<NewDataSet>
    <Root>
        <Phonemic>and</Phonemic>
        <Phonetic>nd</Phonetic>
        <Description/>
        <Start>0</Start>
        <End>8262</End>
    </Root>
    <Root>
        <Phonemic>comfortable</Phonemic>
        <Phonetic>comfetebl</Phonetic>
        <Description>adj</Description>
        <Start>61404</Start>
        <End>72624</End>
    </Root>
</NewDataSet>"""

obj = xmltodict.parse(xmldata)
obj = obj["NewDataSet"]
root_elements = obj["Root"] if type(obj) == OrderedDict else [obj["Root"]]
# Above step ensures that root_elements is always a list
for element in root_elements:
    print element["Phonetic"]

rawDescription: Parsing an xml file with an ordered dictionary


code:
for i in range(7):
    print test[-(8-i):(-(6-i) or None)]
    print i

rawDescription: Python string slicing, special case if end of string included?


code:
from pyparsing import *

identifier = Word(alphas, alphanums).setName('identifier')
DOT = Suppress('.')

# Option 1 - fully specified options
full_database_name = identifier('user') + DOT + identifier('database')
just_database_name = identifier('database')
table_name = (full_database_name + DOT + identifier('table') |
              just_database_name + DOT + identifier('table'))

# Option 2 - use FollowedBy to explicitly lookahead when checking for leading user
table_name = (Optional(identifier('user') + FollowedBy(2*(DOT+identifier)) + DOT) +
                identifier('database') + DOT + identifier('table'))

# Option 3 - use liberally matching expression, with a parse action to assign fields
def assignTableFields(fields):
    if len(fields) == 2:
        fields['database'],fields['table'] = fields
    elif len(fields) == 3:
        fields['user'],fields['database'],fields['table'] = fields
    else:
        raise ParseException("wrong number of fields")
table_name = delimitedList(identifier, delim='.').setParseAction(assignTableFields)

for test in ("a.b.c", "b.c"):
    print test
    print table_name.parseString(test).dump()
    print

rawDescription: Optional string segment in pyparsing

description: Optional string segment in pyparsing


rawDescription: Numpy array [? 5 5] to five arrays with [? 5]


code:
import zmq
from multiprocessing import Process
import time

class ServerPubSub(Process):
    def __init__(self, port, n):
        Process.__init__(self)
        self.port = port
        self.n = n

    def run(self):
        self.context = zmq.Context()
        self.pub = self.context.socket(zmq.PUB)
        self.pub.bind('tcp://127.0.0.1:%d' % self.port)
        self.pub.setsockopt(zmq.HWM, 1000)

        time.sleep(1)

        end = False
        for i in range(self.n):
            print 'SRV: sending message %d' % i
            self.pub.send('Message %d' % i)
            print 'SRV: message %d sent' % i
            time.sleep(0.2)

        self.pub.close()

class ClientPubSub(Process):
    def __init__(self, port, n):
        Process.__init__(self)
        self.port = port
        self.n = n

    def run(self):
        self.context = zmq.Context()
        self.sub = self.context.socket(zmq.SUB)
        self.sub.connect('tcp://127.0.0.1:%d' % self.port)
        self.sub.setsockopt(zmq.SUBSCRIBE, '')
        self.poller = zmq.Poller()
        self.poller.register(self.sub, zmq.POLLIN)

        end = False
        count = 0
        while count < self.n:
            ready = dict(self.poller.poll(0))
            if self.sub in ready and ready[self.sub] == zmq.POLLIN:
                msg = self.sub.recv()
                print 'CLI: received message "%s"' % msg
                count += 1

        self.sub.close()

if __name__ == "__main__":
    port = 5000
    n = 10
    server = ServerPubSub(port, n)
    client = ClientPubSub(port, n)

    server.start()
    client.start()

    server.join()
    client.join()

rawDescription: ZMQ Poller not working within Class Instance


code:
for msj in (level_3,level_2,level_1):
    if msj:
        print msj
        break

rawDescription: Python: Shorten If / Elif control flow tree (printing highest order string that is not None)


code:
tags = Tags.objects.filter(id__in=[ids])
for tag in tags:
    print tag.name

rawDescription: Getting a specific tag object using django-taggit


code: print map(lambda ll: min([len(set(l)) for l in zip(*ll)]), lst)

rawDescription: Python: List Comparison to find unique element count of lists


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

    <meta property="og:country-name" content="South Africa"/>
    <title>Foo</title>
''')
country_meta = soup.find('meta', attrs={'property': 'og:country-name', 'content': True})
country_meta
print country_meta['content']

rawDescription: Python beautiful soup issue

description: Python beautiful soup issue


rawDescription: How to split a matrix into 4 blocks using numpy?

description: Split a matrix into 4 blocks using numpy


code:
a=irc.recv (4096)
print a

rawDescription: Python IRC Bot won't join channels on some servers, and I don't know how to make it respond to user input


code:
import webcolors

def closest_colour(requested_colour):
    min_colours = {}
    for key, name in webcolors.css3_hex_to_names.items():
        r_c, g_c, b_c = webcolors.hex_to_rgb(key)
        rd = (r_c - requested_colour[0]) ** 2
        gd = (g_c - requested_colour[1]) ** 2
        bd = (b_c - requested_colour[2]) ** 2
        min_colours[(rd + gd + bd)] = name
    return min_colours[min(min_colours.keys())]

def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name

requested_colour = (119, 172, 152)
actual_name, closest_name = get_colour_name(requested_colour)

print "Actual colour name:", actual_name, ", closest colour name:", closest_name

rawDescription: Convert rgb color to english color name, like 'green'


code:
from itertools import permutations
from collections import defaultdict

dd = defaultdict(dict)
for row in correspondingFunctionDatabase:
    for fst, snd in permutations(row, 2):
        dd[tuple(fst)][snd[0]] = snd[1]

print dd['Java', 'System.out.println']['Python']
# print
print dd['JavaScript', 'eval(statement)']['Python']
# eval(statement)
print dd['JavaScript', 'eval(statement)'].get('FakeLanguage', 'No Matching Function')
# No Matching Function

rawDescription: How can I keep track of equivalent functions in different programming languages?

description: Keep track of equivalent functions in different programming languages


code:
mapped = map(offline_optimize, *inputs)
print len(mapped), mapped[0]

rawDescription: Apply a function to a list of dictionaries in python


code:
import pyqtgraph as pg
import numpy as np
app = pg.mkQApp()
data = np.random.normal(size=(120,20000), scale=0.2) + \
       np.arange(120)[:,np.newaxis]
view = pg.GraphicsLayoutWidget()
view.show()
w1 = view.addPlot()
now = pg.ptime.time()
for n in data:
    w1.plot(n)
print "Plot time: %0.2f sec" % (pg.ptime.time()-now)
app.exec_()

rawDescription: Plotting large arrays in pyqtgraph

description: Plotting large arrays in pyqtgraph


code:
template_tuple = (0, 0, 'X', 0, 'Y', 'Z')
template_tuple_len = len(template_tuple)

def extract_normalized_fields_from_row(row):
    split_row = row.strip().split('\t')
    return [v if len(v) else template_tuple[i] for (i, v) in enumerate(
            split_row + [''] * (template_tuple_len - len(split_row)))]

for row in (
    '\t'.join(('1', '2', '3', '4', '5', '6')),
    '\t'.join(('1', '', '3', '4', '5', '6')),
    '\t'.join(('1', '2', '3', '4'))
    ):
    print extract_normalized_fields_from_row(row)

rawDescription: Template Python list with default element values


code:
os.system("sudo /home/pi/433Utils/RPi_utils/RFSniffer >output.txt & pkill --signal SIGINT RFSniffer")
f = open("output.txt","r")
readf = f.read()
for line in readf:
    print line,
#close file
if f.closed == "False":
    f.close()

rawDescription: Raspberry RFSniffer read output with python


code:
from collections import OrderedDict
print OrderedDict(get_entries( infile ))

rawDescription: Python: Build a dictionary from a file's contents


code:
import os
import time
import signal
import subprocess

# Map child PID to Popen object
SUBPROCESSES = {}

# Define handler
def handle_sigchld(signum, frame):
    pid = os.wait()[0]
    print 'Subprocess PID=%d ended' % pid
    del SUBPROCESSES[pid]

# Handle SIGCHLD
signal.signal(signal.SIGCHLD, handle_sigchld)

# Spawn a couple of subprocesses
p1 = subprocess.Popen(['sleep', '1'])
SUBPROCESSES[p1.pid] = p1
p2 = subprocess.Popen(['sleep', '2'])
SUBPROCESSES[p2.pid] = p2

# Wait for all subprocesses to die
while SUBPROCESSES:
    print 'tick'
    time.sleep(1)

# Done
print 'All subprocesses died'

rawDescription: How to Clean Up subprocess.Popen Instances Upon Process Termination


code:
new_dict = dict((x.split(' ') for x in list_1))
set_interest = set(list_interest)
for item in new_dict:
    if item in set_interest:
        print "yes"
    else:
        print "\t", item

rawDescription: Comparing Lists and Dictionaries correctly


code:
import json
print json.dumps([foo, bar])

rawDescription: php `exec` calls python script. how return strings from python?

description: Php `exec` calls python script. how return strings from python


code:
print len("ã")
print ord("ã"[0])
print ord("ã"[1])

rawDescription: How to convert a unicode character representation from string to unicode in python?

description: Convert a unicode character representation from string to unicode in python


code:
from lxml.cssselect import CSSSelector
from lxml.etree import fromstring
# Document with table, containing header, three data rows and two footer rows:
doc = fromstring('''

header

Data 1

Data 2

Data 3

footer1

footer2

''')
sel = CSSSelector('table tr')
rows = sel(doc)[1:-2] # remove header, footer1, footer2 from result
for tr in rows:
    print tr.getchildren()[0].text

rawDescription: Use CSS to select range of elements in Python


rawDescription: How to compare multiple rows from same column in DataFrame

description: Compare multiple rows from same column in DataFrame


code:
import csv

st="""\
10004, 10005, 0,'1 ',2,2,1, 0.00000E+0, 0.00000E+0, 2, 'ALCAZAR,NMRD ',1, 129,1.0000"""

kwargs={'skipinitialspace':True, 'quotechar':"'"}

for e in csv.reader(st.splitlines(), **kwargs):
    print e

rawDescription: To split strings using Python

description: To split strings using Python


code:
l=[]
for i in s:
    l.append(i['task']['name'])

print l

rawDescription: How to make grep using Python

description: Make grep using Python


code:
print_at = 0
for i in range(userloop):
    if i >= print_at:
        print "At the top i is %d" % i
        numbers.append(i)
        userincrease = int(raw_input("Increase > "))
        print_at = i + userincrease

        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i

rawDescription: Could I use a for loop instead of a while loop?


code:
class D(dict):
    def __init__(self):
        self.changed_values = {}
        self.__initialized = True

    def __setitem__(self, key, value):
        self.changed_values[key] = value
        super(D, self).__setitem__(key, value)

    def __getattr__(self, item):
        """Maps values to attributes.
        Only called if there *isn't* an attribute with this name
        """
        try:
            return self.__getitem__(item)
        except KeyError:
            raise AttributeError(item)

    def __setattr__(self, item, value):
        """Maps attributes to values.
        Only if we are initialised
        """
        if not self.__dict__.has_key('_D__initialized'):  # this test allows attributes to be set in the __init__ method
            return dict.__setattr__(self, item, value)
        elif self.__dict__.has_key(item):       # any normal attributes are handled normally
            dict.__setattr__(self, item, value)
        else:
            self.__setitem__(item, value)

a = D()
a['hi']  = 'hello'
print a.hi
print a.changed_values

a.hi = 'wow'
print a.hi
print a.changed_values

a.test = 'test1'
print a.test
print a.changed_values

rawDescription: Exposing dict values via properties

description: Exposing dict values via properties


code:
class MyDataStructure:
    def __init__(self,a_list):
        self.my_data = a_list
    def __getitem__(self,arg):
        if isinstance(arg,slice):
            return [item for item in self.my_data if arg.start <= item[arg.step] <= arg.stop]

        raise Exception("%s is unscriptable"%self)

print MyDataStructure([[1,2,3],[5,3,6],[5,4,14],[23,5,2]])[2:4:1]

rawDescription: Python: Iterate through a list by value, not by index


code:
import jsonpickle
import os
import sys

SAVEGAME_FILENAME = 'savegame.json'

game_state = dict()

class Human(object):
    """The human player
    """
    def __init__(self, name, health, gold):
        self.name = name
        self.health = health
        self.gold = gold

class Monster(object):
    """A hostile NPC.
    """
    def __init__(self, name, health):
        self.name = name
        self.health = health

def load_game():
    """Load game state from a predefined savegame location and return the
    game state contained in that savegame.
    """
    with open(SAVEGAME_FILENAME, 'r') as savegame:
        state = jsonpickle.decode(savegame.read())
    return state

def save_game():
    """Save the current game state to a savegame in a predefined location.
    """
    global game_state
    with open(SAVEGAME_FILENAME, 'w') as savegame:
        savegame.write(jsonpickle.encode(game_state))

def initialize_game():
    """If no savegame exists, initialize the game state with some
    default values.
    """
    player = Human('Fred', 100, 10)
    imp = Monster('Imp', 50)

    state = dict()
    state['players'] = [player]
    state['npcs'] = [imp]
    return state

def attack():
    """Toy function to demonstrate attacking an NPC.
    """
    global game_state
    imp = game_state['npcs'][0]
    imp.health -= 3
    print "You attacked the imp for 3 dmg. The imp is now at %s HP." % imp.health

def spend_money(amount):
    """Toy function to demonstrate spending money.
    """
    global game_state
    player = game_state['players'][0]
    player.gold -= amount
    print "You just spent %s gold. You now have %s gold." % (amount, player.gold)

def game_loop():
    """Main game loop.
    This loop will run until the player exits the game.
    """
    global game_state

    while True:
        print "What do you want to do?"
        choice = int(raw_input("[1] Save game [2] Spend money "
                               "[3] Attack that Imp! [4] Load game "
                               "[5] Exit game\n"))
        if choice == 1:
            save_game()
        elif choice == 2:
            spend_money(5)
        elif choice == 3:
            attack()
        elif choice == 4:
            game_state = load_game()
        else:
            print "Goodbye!"
            sys.exit(0)

def main():
    """Main function. Check if a savegame exists, and if so, load it. Otherwise
    initialize the game state with defaults. Finally, start the game.
    """
    global game_state

    if not os.path.isfile(SAVEGAME_FILENAME):
        game_state = initialize_game()
    else:
        game_state = load_game()
    game_loop()

if __name__ == '__main__':
    main()

rawDescription: How to make a save/load game for a text based python rpg?

description: Make a save/load game for a text based python rpg


code:
def match_string(input, pattern, valid_words, date_format):
    print '\n> match_string(\'%s\', \'%s\', %s, \'%s\')' % (input, pattern, valid_words, date_format)

    digits = '0123456789'
    inputIndex = 0
    patternIndex = 0

    while inputIndex < len(input) and patternIndex < len(pattern):
        if pattern[patternIndex] == '#':
            patternIndex += 1
            if pattern[patternIndex] == '#':
                patternIndex += 1
                if pattern[patternIndex] == '#':

                    # validate date
                    date_formatIndex = 0
                    while inputIndex < len(input) and date_formatIndex < len(date_format):

                        if input[inputIndex] == date_format[date_formatIndex]:
                            inputIndex += 1
                            date_formatIndex += 1

                        elif input[inputIndex] in digits:

                            startIndex = inputIndex
                            while inputIndex < len(input) and input[inputIndex] in digits:
                                inputIndex += 1
                            number = int(input[startIndex:inputIndex])

                            if date_format[date_formatIndex:].startswith('%y'):
                                placeholder = True
                            elif date_format[date_formatIndex:].startswith('%m'):
                                if number < 1 or 12 < number:
                                    return 'Error: expected a month between 1 and 12\n input   %d -> "...%s"\n pattern %d -> "...%s"\n date format %d -> "...%s"' % (startIndex, input[startIndex:], patternIndex - 2, pattern[patternIndex - 2:], date_formatIndex, date_format[date_formatIndex:])

                            elif date_format[date_formatIndex:].startswith('%d'):
                                if number < 1 or 31 < number:
                                    return 'Error: expected a day between 1 and 31\n input   %d -> "...%s"\n pattern %d -> "...%s"\n date format %d -> "...%s"' % (startIndex, input[startIndex:], patternIndex - 2, pattern[patternIndex - 2:], date_formatIndex, date_format[date_formatIndex:])

                            else:
                                return 'Error: input doesn\'t match date format\n input   %d -> "...%s"\n pattern %d -> "...%s"\n date format %d -> "...%s"' % (startIndex, input[startIndex:], patternIndex - 2, pattern[patternIndex - 2:], date_formatIndex, date_format[date_formatIndex:])

                            date_formatIndex += 2

                        else:
                            return 'Error: input doesn\'t match date format\n input   %d -> "...%s"\n pattern %d -> "...%s"\n date format %d -> "...%s"' % (inputIndex, input[inputIndex:], patternIndex - 2, pattern[patternIndex - 2:], date_formatIndex, date_format[date_formatIndex:])

                    patternIndex += 1

                else:
                    # validate word
                    valid = False
                    for word in valid_words:
                        if input[inputIndex:].startswith(word):
                            valid = True
                            inputIndex += len(word)
                            break
                    if not valid:
                        return 'Error: expected a valid word\n input   %d -> "...%s"\n pattern %d -> "...%s"' % (inputIndex, input[inputIndex:], patternIndex - 2, pattern[patternIndex - 2:])

            else:
                # validate number
                if not input[inputIndex] in digits:
                    return 'Error: expected a number\n input   %d -> "...%s"\n pattern %d -> "...%s"' % (inputIndex, input[inputIndex:], patternIndex - 1, pattern[patternIndex - 1:])
                while inputIndex < len(input) and input[inputIndex] in digits:
                    inputIndex += 1

        elif input[inputIndex] != pattern[patternIndex]:
            return 'Error: input and pattern do not match\n input   %d -> "...%s"\n pattern %d -> "...%s"' % (inputIndex, input[inputIndex:], patternIndex, pattern[patternIndex:])
        else:
            inputIndex += 1
            patternIndex += 1
    return True

print match_string('1 and 2 are numbers foo and bar are strings 12-1-2013 is a date', '# and # are numbers ## and ## are strings ### is a date', ['foo', 'bar'], '%m-%d-%y')
print match_string('1 is a number foo is a string 12-1-2013 is a date', '# is a number ## is a string ### is a date', ['foo'], '%m-%d-%y')
print match_string('foo is a number bar is a string 12-1-2013 is a date', '# is a number ## is a string ### is a date', ['foo'], '%m-%d-%y')
print match_string('1 is a number bar is a string 12-1-2013 is a date', '# is a number ## is a string ### is a date', ['foo'], '%m-%d-%y')
print match_string('1 is a number foo is a string January is a date', '# is a number ## is a string ### is a date', ['foo'], '%m-%d-%y')
print match_string('1 and 2 are numbers foo and bar are strings 15-1-2013 is a date', '# and # are numbers ## and ## are strings ### is a date', ['foo', 'bar'], '%m-%d-%y')
print match_string('1 and 2 are numbers foo and bar are strings 08-42-2013 is a date', '# and # are numbers ## and ## are strings ### is a date', ['foo', 'bar'], '%m-%d-%y')
print match_string('1 and 2 are numbers foo and bar are strings 08;4;2013 is a date', '# and # are numbers ## and ## are strings ### is a date', ['foo', 'bar'], '%m-%d-%y')
print match_string('1 and 2 are numbers foo and bar are strings 08-4-2013 is a date', '# and # are numbers ## and ## are strings ### is a date', ['foo', 'bar'], '~%m-%d-%y')

rawDescription: Python: matching strings against pattern strings with number, string, and date placeholders with good error catching


code:
import random

class Variables(object):
    numbers = {
        "num{}".format(i): "Text {}".format(random.randrange(100))
        for i in range(100)
    }

v = Variables()
print v.numbers["num50"]

rawDescription: Python: Assigning Random Integer to Variables in a Class


code:
from numpy import *

eps = 0.1
#ASSUMING the second arrow is sorted (otherwise sort it first)
a= array(
    [[1, 1.2, 3],
    [2, 2.2, 3],
    [3, 2.25, 1],
    [4, 2.28, 4],
    [5, 3.2, 8],
    [6, 4.2, 10],
    [7, 4.21, 3],
    [8, 4.25, 4],
    [9, 4.28, 1],
    [10, 5.2, 10],
    ])
# expected result
# a= [[1, 1.2, 3],
#     [3, 2.25, 1],
#     [5, 3.2, 8],
#     [9, 4.28, 1],
#     [10, 5.2, 10],
#     ]

n = shape(a)[0]
b = a[:,1]

a1 = a[ (diff(b)<eps) ]
#indexes of some False that could be True.
#these indexes should be checked backwards
#and evtl. added to a1
indexes = where((diff(b)<eps)==False)[0][1:]
for index in indexes:
    if b[index] - b[index-1]<eps:
        a1 = vstack( (a1,a[index,:]) )

#sort array
a1 = a1[lexsort( (a1[:,1],a1[:,1]))]

groups = where(diff(a1[:,1])>eps)[0]
i = 0
# get min of groups
for g in groups:
    ag = a1[i:g+1,2]
    Ag = a1[i:g+1,:]
    if i == 0:
        a2 = Ag [ ag == min(ag) ]
    else:
        a2 = vstack( (a2, Ag [ ag == min(ag) ] ) )

    i = g+1
#add last group
ag = a1[g+1:,2]
Ag = a1[g+1:,:]
a2 = vstack( (a2, Ag [ ag == min(ag) ]) )

#the elements that build no groups
result = a[ in1d(a[:,0], [ int(i) for i in a[:,0] if i not in a1[:,0] ])  ]
# add the elements of a2, these are the minimal elements of each group
result = vstack( (result, a2) )
# sort the result (optional)
result = result[lexsort( (result[:,0], result[:,0]))]
print "final result\n", result

rawDescription: how to delete reduplicate element in an array in python

description: Delete reduplicate element in an array in python


code:
import glob
import os
files = []
for x in os.listdir(path_to_top_folder):
    if os.path.isdir(x):
        for fil in glob.glob("{0}/*.txt".format(x)):
            files += [os.path.split(fil)[-1]]
print files

rawDescription: Access files with wildcard filename in directory structure


code:
def func1():
    print 'inside func1 in first.py'

if __name__ == '__main__':
    # first.py executed as a script
    func1()

rawDescription: Equivalent of source() of R in Python

description: Equivalent of source() of R in Python


code:
from lxml import etree

SVGNS = u"http://www.w3.org/2000/svg"
svg = '''<!--Square 2" Tile Template -->
<svg xmlns="http://www.w3.org/2000/svg" width="181" height="181">
    <text id="tile_text" y="90" width="100%"
          style="text-align:center;font-family:Verdana;font-size:20">
        TEXT TO REPLACE
    </text>
</svg>'''

xml_data = etree.fromstring(svg)
# We search for element 'text' with id='tile_text' in SVG namespace
find_text = etree.ETXPath("//{%s}text[@id='tile_text']" % (SVGNS))
# find_text(xml_data) returns a list
# [<Element {http://www.w3.org/2000/svg}text at 0x106185ab8>]
# take the 1st element from the list, replace the text
find_text(xml_data)[0].text = 'BLAHBLAH'
new_svg = etree.tostring(xml_data)
print new_svg

rawDescription: Replacing inner contents of an SVG in Python


code:
test_str = "some string\n# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V\n         , 0 iput-object v1, v0, Lorg/cyanogenmod/audiofx/ActivityMusic$11;->this$0 Lorg/cyanogenmod/audiofx/ActivityMusic;\n         , 4 invoke-direct v0, Ljava/lang/Object;-><init>()V\n         , a return-void\n# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V "
ss = test_str.split('\n')
for s in ss:
    if s.strip()[0] == "#":
        print s
        break

rawDescription: Python Regex starting with hashtag

description: Python Regex starting with hashtag


code:
def defineAList():
    local_list = ['1','2','3']
    print "For checking purposes: in defineAList, list is", local_list
    return local_list

def useTheList(passed_list):
    print "For checking purposes: in useTheList, list is", passed_list

def main():
    # returned list is ignored
    returned_list = defineAList()

    # passed_list inside useTheList is set to what is returned from defineAList
    useTheList(returned_list)

main()

rawDescription: Python: Passing variables between functions


code:
class MyTime(datetime.time):
    def __new__(cls, t):
        return datetime.time.__new__(cls, t.hour, t.minute, t.second, t.microsecond)

print MyTime(datetime.time(10,20,30,400000))

rawDescription: subclassing Python's datetime.time

description: Subclassing Python's datetime.time


code: print sql.decode('string_escape')

rawDescription: How Can I have newlines in multiline string print new lines in Windows?

description: Have newlines in multiline string print new lines in Windows


code:
while assigned_layers <= len(self.layers):
    for element_tuple in tuple_of_tuples:
        for item in element_tuple:
            if item: # in python, 0 is treated as boolean False, 1 or any other value is treated as boolean True.
                dimension = [layerbuff + str(assigned_layers)] #what is layerbuff?
                modules_dict = {
                    1: pybrain.GaussianLayer(dimension),
                    2: pybrain.LinearLayer(dimension),
                    3: pybrain.LSTMLayer(dimension),
                    4: pybrain.SigmoidLayer(dimension),
                    5: pybrain.TanhLayer(dimension)
                    } # Notice how this dict is much easier to read.

                vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff]  #modules_dict is a dict and not a callable object
                c_buff = 0
                assigned_layers +=1
                #No need for continue here, since that's what the if...else does here.
            else:
                element_tuple = element_tuple[1:] #what is this for?
                print 'Invalid: ', tuple_of_tuples, element_tuple

rawDescription: Matching Binary operators in Tuples to Dictionary Items


code:
from sklearn.cluster import AffinityPropagation
import numpy as np

data = [[1200, 1210], [1212, 1222], [1190, 1200], [300, 310], [800, 810]]
centers = np.array([[sum(x) / len(x)] for x in data])

clf = AffinityPropagation()

for (points, cluster) in zip(data, clf.fit_predict(centers)):
    center = sum(points) / len(points)
    print points, center, cluster

rawDescription: Finding "hotspots" in unidimensional list in python


code:
from calendar import TimeEncoding, month_name

def get_month_name(month_no, locale):
    with TimeEncoding(locale) as encoding:
        s = month_name[month_no]
        if encoding is not None:
            s = s.decode(encoding)
        return s

print get_month_name(3, "nb_NO.UTF-8")

rawDescription: Python calendar: day/month names in specific locale


code:
n = 2
L = []

for i in range(N):
    L.append([])

    for LL in reversed(L):
        LL.append(n)
        n += 1

for i, LL in enumerate(L):
    print i + 1, LL

rawDescription: Newbie algorithm design

description: Newbie algorithm design


code:
def dump(data, path=''):
    if isinstance(data, defaultdict):
        for k, v in data.items():
            dump(v, path + k + ":")
    else:
        print "%s%s" % (path, ':'.join("%s=%r" % pair for pair in d.items()))

dump(data)

rawDescription: How to os.walk deep defaultdict for values?

description: Os.walk deep defaultdict for values


code:
import glob
import os
os.chdir("/mydir")
for files in glob.glob("*.csv"):
    print files

rawDescription: Merge CSV Files in Python with Different file names


code:
import pandas as pd
from StringIO import StringIO

d1 = '''Country Name  Year  Population Age 0 - 14
        Aruba  1960              43.847771
      Andorra  1960                    NaN
  Afghanistan  1960              43.712284
       Angola  1960              43.759289
      Albania  1960              41.757282'''

d2 = '''Country Name  Population Age 15 - 64
        Aruba               53.667355
      Andorra                     NaN
  Afghanistan               53.834637
       Angola               53.587101
      Albania               52.941044'''

t3 = pd.DataFrame.from_csv( StringIO(d1), sep='\s{2,}', index_col=None )
print '\nt3:\n',t3

t4 = pd.DataFrame.from_csv( StringIO(d2), sep='\s{2,}', index_col=None )
print '\nt4:\n',t3

print '\n--- merge ---\n'

print pd.merge( t4, t3, on='Country Name')

print pd.merge( t4, t3[ ['Country Name', 'Population Age 0 - 14'] ], on='Country Name')

print '\n--- concat ---\n'

print pd.concat( (t4, t3['Population Age 0 - 14']), axis=1)

print '\n--- [xxx] = [xxx] ---\n'

t4['Population Age 0 - 14'] = t3['Population Age 0 - 14']

print t4

rawDescription: Do I want to join/merge, concat, or append these two Pandas DataFrames?


code:
X_data = []
X_row, X_col = [], []
targets_array = []

for row_idx, opportunity in enumerate(opp_data):
    targets_array.append(int(opportunity[-1]))
    row = np.array(map(int, opportunity[:-1]))
    col_inds, = np.nonzero(row)
    X_col.extend(col_inds)
    X_row.extend([row_idx]*len(col_inds))
    X_data.extend(row[col_inds])

print " Starting to transform to a sparse matrix" + str(time.now())
X = coo_matrix((X_data, (X_row, X_col)), dtype=int)
print "Finished transform to a sparse matrix " + str(time.now())

rawDescription: Python - The best way to read a sparse file into a sparse matrix


code:
for elem in dis:
    print max(dis[elem])

rawDescription: PYTHON - creating multiple lists in a loop


code:
import time
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://stackoverflow.com/users/login')
username = browser.find_element_by_id("email")
password = browser.find_element_by_id("password")
username.send_keys("emailID")
password.send_keys("password")
browser.find_element_by_name("submit-button").click()

for i in range(5):
    print browser.current_url, " - loop ", i
    time.sleep(1)
print browser.current_url

rawDescription: Extracting user data from stackoverflow using Selenium


code:
import time

cache = {}
lastTime = time.time()

def timedCacheDecorator(func):

    def wrap(*args, **kwargs):

        key = str(args)+str(kwargs)

        # cache for 5 seconds
        global lastTime
        if key not in cache or time.time() - lastTime > 5:
            lastTime = time.time()
            cache[key] = func(*args, **kwargs)

        return cache[key]

    return wrap

# lets test it

@timedCacheDecorator
def myquery():
    return time.time()

print myquery()
time.sleep(1)
print myquery()
time.sleep(5)
print myquery()
time.sleep(1)
print myquery()

rawDescription: Cache the result of a MySQLdb database query in memory


code:
# insert 30 random-valued rows
t.insert().execute([{'number': randrange(100)} for x in xrange(30)])

for row in select([func.avg(t.c.number), func.np_var(t.c.number)]).execute():
    print 'RESULT ROW: ', row

rawDescription: How can i write my own aggregate functions with sqlalchemy?

description: Write my own aggregate functions with sqlalchemy


code:
import json

yourData = []
with open("fileName.txt") as inputData:
    for line in inputData:
        try:
            yourData.append(json.loads(line.rstrip(';\n')))
        except ValueError:
            print "Skipping invalid line {0}".format(repr(line))

print yourData
import pprint
pprint.pprint(yourData
)

rawDescription: Load multiple dictionaries from a file

description: Load multiple dictionaries from a file


rawDescription: How can I "count from the back" in python lists?

description: "count from the back" in python lists


code:
from itertools import permutations
yourdictionary = {(1,3,2):'text',(3,1,2):'text'}
yourlist = [1, 2, 3]
print any(tuple(perm) in yourdictionary for perm in permutations(yourlist))
yourdictionary = {frozenset([1, 2, 3]): 'text', frozenset([4, 5, 6]): 'othertext'}
frozenset(yourlist) in yourdictionary
frozenset([2, 3]) in yourdictionary

rawDescription: How to find combinations of a list in a Dictionary?

description: Find combinations of a list in a Dictionary


code:
from itertools import combinations

t = [(1,2), (3,4), (5,6)]

for i in combinations(t, 2):
    print i

rawDescription: Choose two tuples from a list and calculate every possible pair in Python


code:
import fileinput

counter = 0
for line in fileinput.input('inp.txt', inplace=True):
    if not counter:
        if line.startswith('# my comment'):
            counter = 4
        else:
            print line,
    else:
        counter -= 1

rawDescription: Deleting n number of lines after specific line of file in python


rawDescription: lxml: Converting XML to HTML through XSLT and get HtmlElements

description: Lxml: Converting XML to HTML through XSLT and get HtmlElements


code:
def frob(x):
    if str(x) == str(x)[::-1]:
        return x
    else:
        return frob(x + int(str(x)[::-1]))

print frob(29)

rawDescription: Generating list until certain condition is met using a single line


code: print msg

rawDescription: Multipart email PDF attachment blank

description: Multipart email PDF attachment blank


code: print " ".join([wordclass for word, wordclass in your_list if word == 'federal'])

rawDescription: List with tuples in python

description: List with tuples in python


code:
list = node.getElementsByTagName('Item')
items = dict((item.getAttribute('id'), item) for item in list)
for id in items:
  print "Item[%d] -> %s" % (id, items[id]) # Only single item per id left.

rawDescription: Python minidom parser: Ignore tag with identical attribute values


code:
def OnConnect(self, event):
    #print [event.IsChecked()]
    if not hasattr(self, "mon"):
        self.mon = Monitor("", "", "", self.path, "60", self.StatusBar)
    if event.IsChecked():
        print "Set Menu Label Disconnected"
        self._Connect.SetItemLabel("Disconnect")
        print "Start Monitor"
        self.mon.Start()
        print "Start Finished"
    else:
        print "Set Menu Label Connected"
        self._Connect.SetItemLabel("Connect")
        print "Stop Monitor"
        self.mon.Stop()
        print "Stop Finished"

rawDescription: Problems stopping a thread when signaled from a menu event?


code:
class bob:
    def __init__(self, n):
        self.n=n
        self.i=[0]

bob1=bob(2)

i = bob1.i
print i #>>0
bob1.i[0] += 1
print(bob1.i)
print i #>> now both are [1]

rawDescription: Instance attribute mutability (very basic)


code:
from multiprocessing.managers import SyncManager

HOST = ''
PORT0 = 5011
PORT1 = 5012
PORT2 = 5013
AUTHKEY = 'authkey'

def QueueServerClient(HOST, PORT, AUTHKEY):
    class QueueManager(SyncManager):
        pass
    QueueManager.register('get_queue')
    QueueManager.register('get_name')
    QueueManager.register('get_description')
    manager = QueueManager(address = (HOST, PORT), authkey = AUTHKEY)
    manager.connect() # This starts the connected client
    return manager

# create three connected managers
qc0 = QueueServerClient(HOST, PORT0, AUTHKEY)
qc1 = QueueServerClient(HOST, PORT1, AUTHKEY)
qc2 = QueueServerClient(HOST, PORT2, AUTHKEY)
# Get the queue objects from the clients
q0 = qc0.get_queue()
q1 = qc1.get_queue()
q2 = qc2.get_queue()
# put stuff in the queues
q0.put('some stuff')
q1.put('other stuff')
q2.put({1:123, 2:'abc'})
# check their sizes
print 'q0 size', q0.qsize()
print 'q1 size', q1.qsize()
print 'q2 size', q2.qsize()

# pull some stuff and print it
print q0.get()
print q1.get()
print q2.get()

rawDescription: Python multiprocessing RemoteManager under a multiprocessing.Process


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

fname = 'groupby.h5'

# create a frame
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'foo',
                         'bar', 'bar', 'bar', 'bar',
                         'foo', 'foo', 'foo'],
                   'B': [1,1,1,2,
                         1,1,1,2,
                         2,2,1],
                   'C': ['dull', 'dull', 'shiny', 'dull',
                         'dull', 'shiny', 'shiny', 'dull',
                         'shiny', 'shiny', 'shiny'],
                   'D': np.random.randn(11),
                   'E': np.random.randn(11),
                   'F': np.random.randn(11)})

# create the store and append, using data_columns where I possibily
# could aggregate
with pd.get_store(fname,mode='w') as store:
    store.append('df',df,data_columns=['A','B','C'])

    print "\ndf:\n%s" % store['df']

    # get the groups
    A = store.select_column('df','A')
    B = store.select_column('df','B')
    idx = pd.MultiIndex.from_arrays([A,B])
    groups = idx.unique()

    # iterate over the groups and apply my operations
    l = []
    for (a,b) in groups:

        grp = store.select('df',where = [ 'A=%s and B=%s' % (a,b) ])

        # this is a regular frame, aggregate however you would like
        l.append(grp[['D','E','F']].sum())

print "\nresult:\n%s" % pd.concat(l, keys = groups)

os.remove(fname)

rawDescription: "Group By" multiple columns on Large Data in HDFStore


rawDescription: Print a python array on a specific format


code:
array1 = arr1.split(',')
array2 = arr2.split(',')
for i,value in enumerate(array1):
   print value, array2[i]

rawDescription: get array values in python

description: Get array values in python


code:
# Libraries
import re

# Example filenames. Use glob as described below to grab your pdf filenames
file_list = ['name_ID_123.pdf','name2_ID_456.pdf'] # glob.glob("*.pdf")

for fname in file_list:
    res = re.findall("ID_(\d+).pdf", fname)
    if not res: continue
    print res[0] # You can append the result to a list

rawDescription: Grab part of filename with Python

description: Grab part of filename with Python


code:
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
subject = article = rdflib.term.URIRef("http://www.someuri.org/for/your/subject")

# (subject,None,None) represents a constrain to iterate over the graph. By setting
# any of the three elements in the triple you constrain by any combination of subject,
# predicate or object. In this case we only  constrain by subject.
for triple in g.triples((subject,None,None)):
    print triple

rawDescription: how to look-up rdf object knowing subject or other way around?

description: Look-up rdf object knowing subject or other way around


code:
from BeautifulSoup import BeautifulSoup as bs
html = '''

link1 contents

link2 contents

'''
soup = bs(html)
for anchor_tag in soup.findAll('a'):
  print anchor_tag.contents[0]

rawDescription: Python: Fetching and parsing text from html files


code:
from testconfig import config
import sys
class testModule_1:
    def test_M1_1(self):
        print 'Module 1 Test 1 Config: ' + config['rc_configuration']['command']

rawDescription: python nose tests: specifying which node the test should be run in Selenium Grid

description: Python nose tests: specifying which node the test should be run in Selenium Grid


code:
import numpy
utils = numpy.random.uniform(0, 300, 10)
print repr(utils[0])
type(utils[0])

rawDescription: Increasing speed of python code

description: Increasing speed of python code


code:
class UserType( models.Model ):
    TYPES = (( 'tenant', 'Tenant'),
             ( 'propman', 'Property Manager'),
             ( 'propown', 'Property Owner'),
             ( 'vendman', 'Vendor Manager'),
             ( 'vendor', 'Vendor'),
             ( 'viewer', 'Viewer'),
             ( 'moderator', 'Moderator'),
             ( 'admin', 'Administrator'))

    name = models.CharField( max_length = 135, choices=TYPES )

def has_role(user, role):
    return user.profile.user_types.filter(name=role).count() == 1

def has_roles(user, roles):
    return user.profile.user_types.filter(name__in=roles).count() == len(roles)

print has_roles(user, ['viewer','moderator','admin'])

rawDescription: Check for multiple values in a M2M relationship in Django


rawDescription: averaging list of lists python

description: Averaging list of lists python


code:
class P(object):
    var = "original"

    def func(self):
        if self.var != P.var:
            print "DO A"
        else:
            print "DO B"

class C1(P):
    var = "changed"

class C2(P):
    pass

c1 = C1()
c2 = C2()

c1.func()  # Do A
c2.func()  # Do B

rawDescription: Python method be aware the self.variable changed


code:
Students = ["student1", "student2", "student3", "student4", "student5"]
Scores = [12,13,8,15,16,19,18,14,3,15,6,3,9,11,13,5,2,18,3,7]

Averages = []
s = 0
for i in range(20):
    if ((i) % 4 == 0 and i > 0):
        Averages.append(float(s) / 4)
        s = 0
    s = s + Scores[i]

print Averages

rawDescription: How to use only a specific amount of elements in a for loop

description: Use only a specific amount of elements in a for loop


code:
import re
add = u"埼玉県川口市金山町１２丁目１－１０４番地"
test = re.search(ur'番地|丁目', add)
test.group(0)
print test.group(0)

rawDescription: Python - regex with Japanese letters matches only one character


code:
for i in list_of_arrays:
    print total(i)

rawDescription: Iterate through a list of numpy arrays

description: Iterate through a list of numpy arrays


code:
from random import randint
from itertools import islice, dropwhile

file = "/etc/passwd"
f = open(file, "r")

f.seek(randint(10,250))
pos = f.tell()

print "pos=%d" % pos

def countbytes(iterable):
    bytes = 0
    for item in iterable:
        bytes += len(item)
        yield bytes

print list(
        islice(
            dropwhile(lambda x: x[1] <= pos, enumerate(countbytes(open(file, "r"))))
            , 1))[0][0]+1

rawDescription: How to get the current open file line in python?

description: Get the current open file line in python


code:
s = 'col555'
print s[3:]

rawDescription: Python left() equivalent?

description: Python left() equivalent


code: print "Python"

rawDescription: How can I get return value ( string ) from called Python by BatchFile

description: Get return value ( string ) from called Python by BatchFile


code:
import logging
import sys

# DATABASE
import pymysql
import sqlalchemy
from sqlalchemy.sql import table, column, exists
from sqlalchemy import *

sys.path.append("/Volumes/Orange-1a/^datamine/^scripts/^foundation/")
import music_tables
from music_tables import *

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

logger = logging.getLogger(__name__)

class FuriousmePipeline(object):

    def __init__(self):
        self.seen = []

    def process_item(self, item, spider):
        logger.info(item)

        some_engine = create_engine('mysql+pymysql://root@127.0.0.1/music_marketing?charset=utf8&use_unicode=0', pool_recycle=3600)

        # create a configured "Session" class
        Session = sessionmaker(bind=some_engine)

        # create a Session
        session = Session()

        thelist_id = item.get('entity_id')

        for email in item.get('emails'):
            if not email in self.seen:
                self.seen.append(email)
                try:
                    ins = data.insert().values(thelist=thelist_id, tag=22, value=email)
                    conn.execute(ins)  # remember this to write to database!
                except Exception, e:
                    print 'INSERT ERROR: ', thelist_id

        return item

rawDescription: Scrapy Passing Arguments and Writing to MySQL


code:
if folders:
    print "has folders!"

rawDescription: Python Script greping directories

description: Python Script greping directories


code:
address = ('http://www.somesite.com/article.php?page=' +numb)
html = urllib2.urlopen(address).read()
regex = re.findall(r"([a-f\d]{12})", html)
print "\n".join(regex)

rawDescription: Python re.findall prints list instead of string


code:
a = 1

def fn():
   global a  # <<< We're adding this
   print a
   a = 2

fn()
print a

# Prints: 1
# Then, prints: 2 (a changed in the global scope too)

rawDescription: Python : Why is it said that variables that are only referenced are implicitly global?


code:
with open('file.txt') as fp:
    text = fp.read()           # read the entire file content into `text`
lines = text.splitlines()      # split the text into a list of lines
output = ','.join(lines)       # use comma to join the lines into a string
print output                   # and print it.

rawDescription: python file iteration to change the formatting of output (going from a column to line)

description: Python file iteration to change the formatting of output (going from a column to line)


code:
import re
import itertools as it

text = "<h> This is a string. </H><p> This is another part. </P>"

cnt = it.count()
d = {}
def replace(tag, d, cnt):
    if tag not in d:
        d[tag] = '{{{}}}'.format(next(cnt))
    return d[tag]
print re.sub(r"(</?\w+>)", lambda x: replace(x.group(1), d, cnt), text)
print d

rawDescription: replace pattern with a sequential number string in python

description: Replace pattern with a sequential number string in python


code:
import locale
locale.setlocale(locale.LC_NUMERIC, 'en_US')
print locale.atof('1,234,456.23')  # Prints 1234456.23

rawDescription: Flexible numeric string parsing in Python


code:
import re
data = """part 1;"this is ; part 2;";'this is ; part 3';part 4;this "is ; part" 5"""
PATTERN = re.compile(r'''((?:[^;"']|"[^"]*"|'[^']*')+)''')
print PATTERN.split(data)[1::2]

rawDescription: How to split but ignore separators in quoted strings, in python?

description: Split but ignore separators in quoted strings, in python


code:
import io
with io.open('d:\\DRA_ACTORDEF_T0.x', 'r', encoding='your_encoding') as f:
    for line in f:
        print line

rawDescription: Reading file stops at halfway

description: Reading file stops at halfway


code:
class OffsetDict(dict):
    def __init__(self, lst, offset):
        self.lst = lst
        self.offset = offset
    def __nonzero__(self):
        return bool(self.lst)
    def iteritems(self):
        return enumerate(self.lst, self.offset)

A = ["a", "b", "c"]
d = OffsetDict(A, 5)
print json.dumps(d)

rawDescription: make a increaseKey for list in python

description: Make a increaseKey for list in python


code:
#!/usr/bin/env python

# Helper script when freezing Pmw applications.  It concatenates all
# Pmw megawidget files into a single file, 'Pmw.py', in the current
# directory.  The script must be called with one argument, being the
# path to the 'lib' directory of the required version of Pmw.
# To freeze a Pmw application, you will also need to copy the
# following files to the application directory before freezing:
#
#    PmwBlt.py PmwColor.py

import os
import re
import sys

# The order of these files is significant.  Files which reference
# other files must appear later.  Files may be deleted if they are not
# used.
files = [
    'Dialog', 'TimeFuncs', 'Balloon', 'ButtonBox', 'EntryField',
    'Group', 'LabeledWidget', 'MainMenuBar', 'MenuBar', 'MessageBar',
    'MessageDialog', 'NoteBook', 'OptionMenu', 'PanedWidget', 'PromptDialog',
    'RadioSelect', 'ScrolledCanvas', 'ScrolledField', 'ScrolledFrame',
    'ScrolledListBox', 'ScrolledText', 'HistoryText', 'SelectionDialog',
    'TextDialog', 'TimeCounter', 'AboutDialog', 'ComboBox', 'ComboBoxDialog',
    'Counter', 'CounterDialog',
]

# Set this to 0 if you do not use any of the Pmw.Color functions:
needColor = 1

# Set this to 0 if you do not use any of the Pmw.Blt functions:
needBlt = 1

def expandLinks(path):
    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)
    while 1:
        if not os.path.islink(path):
            break
        dir = os.path.dirname(path)
        path = os.path.join(dir, os.readlink(path))

    return path

def mungeFile(file):
    # Read the file and modify it so that it can be bundled with the
    # other Pmw files.
    file = 'Pmw' + file + '.py'
    text = open(os.path.join(srcdir, file)).read()
    text = re.sub('import Pmw\>', '', text)
    text = re.sub('INITOPT = Pmw.INITOPT', '', text)
    text = re.sub('\<Pmw\.', '', text)
    text = '\n' + ('#' * 70) + '\n' + '### File: ' + file + '\n' + text
    return text

# Work out which version is being bundled.
file = sys.argv[0]
file = os.path.normpath(file)
file = expandLinks(file)

dir = os.path.dirname(file)
dir = expandLinks(dir)
dir = os.path.dirname(dir)
dir = expandLinks(dir)
dir = os.path.basename(dir)

version = dir[4:].replace('_', '.')

# Code to import the Color module.
colorCode = """
import PmwColor
Color = PmwColor
del PmwColor
"""
# Code to import the Blt module.
bltCode = """
import PmwBlt
Blt = PmwBlt
del PmwBlt
"""
# Code used when not linking with PmwBlt.py.
ignoreBltCode = """
_bltImported = 1
_bltbusyOK = 0
"""
# Code to define the functions normally supplied by the dynamic loader.
extraCode = """

### Loader functions:

_VERSION = '%s'

def setversion(version):
    if version != _VERSION:
        raise ValueError, 'Dynamic versioning not available'

def setalphaversions(*alpha_versions):
    if alpha_versions != ():
        raise ValueError, 'Dynamic versioning not available'

def version(alpha = 0):
    if alpha:
        return ()
    else:
        return _VERSION

def installedversions(alpha = 0):
    if alpha:
        return ()
    else:
        return (_VERSION,)

"""
if '-noblt' in sys.argv:
    sys.argv.remove('-noblt')
    needBlt = 0

if '-nocolor' in sys.argv:
    sys.argv.remove('-nocolor')
    needColor = 0

if len(sys.argv) != 2:
    print 'usage: bundlepmw.py [-noblt] [-nocolor] /path/to/Pmw/Pmw_X_X_X/lib'
    sys.exit()

srcdir = sys.argv[1]

if os.path.exists('Pmw.py'):
    print 'Pmw.py already exists. Remove it and try again.'
    sys.exit()

outfile = open('Pmw.py', 'w')

if needColor:
    outfile.write(colorCode)

if needBlt:
    outfile.write(bltCode)

outfile.write(extraCode % version)

# Specially handle PmwBase.py file:
text = mungeFile('Base')
text = re.sub('import PmwLogicalFont', '', text)
text = re.sub('PmwLogicalFont._font_initialise', '_font_initialise', text)
outfile.write(text)
if not needBlt:
    outfile.write(ignoreBltCode)

files.append('LogicalFont')
for file in files:
    text = mungeFile(file)
    outfile.write(text)

print ''
print '   Pmw.py has been created.'

if needColor or needBlt:
    print '   Before running freeze, also copy the following file(s):'
    if needBlt:
        print '   ' + os.path.join(srcdir, 'PmwBlt.py')
    if needColor:
        print '   ' + os.path.join(srcdir, 'PmwColor.py')

rawDescription: Python Pmw and cx_Freeze?

description: Python Pmw and cx_Freeze


code:
file2list=sorted(file2list,key=operator.itemgetter(1,2,3,4,5,6))
for entry1 in file1list:
    for entry2 in file2list:
        d=np.abs(entry1[1:7]-entry2[1:7])
        if(np.all(d < tol)):
            print entry1,entry2
        elif(d[0] > tol):
            break

rawDescription: efficient string comparison in python including numerical evaluation

description: Efficient string comparison in python including numerical evaluation


code:
from datetime import datetime

def is_dated_csv(filename):
    """
    Return True if filename matches format YY-MM-DD.csv, otherwise False.
    """
    date_format = '%y-%m-%d.csv'

    try:
        datetime.strptime(filename, date_format)
        return True
    except ValueError:
        # filename did not match pattern
        pass

    return False

# Tests
print is_dated_csv('15-01-01.CSV') # True
print is_dated_csv('99-12-31.csv') # True
print is_dated_csv('15-13-32.csv') # False
print is_dated_csv('15-02-30.csv') # False

rawDescription: Using Python, I want to parse each file in one directory with format YY-MM-DD.CSV but ignore all others


code:
#!/usr/bin/python

import mmap
import os
import struct
import sys

FILE = "/opt/tmp/random"  # dd if=/dev/random of=/tmp/random bs=1024k count=1024
SIZE = os.stat(FILE).st_size
BYTES = 2
SKIP = 10

def byfile():
    sum = 0
    with open(FILE, "r") as fd:
        for offset in range(0, SIZE/BYTES, SKIP*BYTES):
            fd.seek(offset)
            data = fd.read(BYTES)
            sum += struct.unpack('h', data)[0]
    return sum

def bymmap():
    sum = 0
    with open(FILE, "r") as fd:
        mm = mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ)
        for offset in range(0, SIZE/BYTES, SKIP*BYTES):
            data = mm[offset:offset+BYTES]
            sum += struct.unpack('h', data)[0]
    return sum

if sys.argv[1] == 'mmap':
    print bymmap()

if sys.argv[1] == 'file':
    print byfile()

rawDescription: Extract specific bytes from a binary file in Python


code:
#! /usr/bin/python2.4
import sys
h = set()
for line in sys.stdin.xreadlines():
  for term in line.split():
    h.add(term)
print len(h)

rawDescription: How can I count unique terms in a plaintext file case-insensitively?

description: Count unique terms in a plaintext file case-insensitively


code:
d = {}
f = open("tennis.txt")
for i, l in enumerate(f):
    # cut the '0b' chars, so you will get your dict keys just like you want
    bin_num = bin(i)[2:]
    # if the key is shorter than 3 chars, add 0 to the beginning
    while len(bin_num) < 3:
        bin_num = '0' + bin_num
    d[bin_num] = l[:-1]

f.close()
for i in sorted(d.items()):
    print i

rawDescription: Python: from a text file to dictionary with keys


code:
words = ['mention', 'detention', 'attention', 'iteraction', 'interception', 'solution', 'iteraction','reiteration', 'determination', 'tension', 'tentative', 'intention', 'solution', 'tentative', 'concatenation', 'alternative', 'bitter', 'asterisk']

subs = ['ter','tion','ten']
edges = []

for word in words:
    for sub in subs:
        if sub in word:
            edges.append( (word, sub) )

print edges[0:6]

# prints [('mention', 'tion'), ('detention', 'tion'), ('detention', 'ten'), ('attention', 'tion'), ('attention', 'ten'), ('iteraction', 'ter')]

rawDescription: How to represent this as a Network?

description: Represent this as a Network


code:
import ConfigParser

cp = ConfigParser.ConfigParser()
cp.read('conds.ini')

conds = []
conds.append(cp.get('mysettings', 'cond1'))
conds.append(cp.get('mysettings', 'cond2'))
conds.append(cp.get('mysettings', 'cond3'))

# alternatively you could do the following to read them all at once
# conds = [expr for name, expr in cp.items('mysettings')
#          if name.startswith('cond')]

x = 8
y = 3
for cond in conds:
    truthiness = eval('bool({})'.format(cond))
    print '{}: {}'.format(cond, truthiness)

rawDescription: reading boolean condition from config file in Python

description: Reading boolean condition from config file in Python


code:
for line in fin.readlines():
    words = line.split()    # list of words
    new_words = []
    unique_words = set()
    for word in words:
        if (word not in unique_words and
                  (not word.isdigit() or int(word) <= 65000)):
            new_words.append(word)
            unique_words.add(word)
    new_line = ' '.join(new_words)
    print new_line

rawDescription: Remove duplicate, remove certain letters from line if found


code:
monster     = Monster()
magician = Magician()

print magician.hp
print monster.hp

monster.punch(magician)
magician.sword_attack(monster)

print magician.hp
print monster.hp

monster.punch(magician)
magician.magic_attack(monster)

print magician.hp
print monster.hp

monster.punch(magician)
magician.magic_attack(monster)

print magician.hp
print monster.hp

monster.punch(magician)
magician.sword_attack(monster)

print magician.hp
print monster.hp

rawDescription: Sharing Class Variables

description: Sharing Class Variables


code:
codes = {chr(n): '[%d]' % n for n in range(255)}

def encode1(text):
    return ''.join(codes[c] for c in text)

def encode2(text):
    import re
    return re.sub(r'.', lambda m: codes[m.group()], text)

def encode3(text):
    return ''.join(map(codes.get, text))

import timeit

a = 'foobarbaz' * 1000

print timeit.timeit(lambda: encode1(a), number=100)
print timeit.timeit(lambda: encode2(a), number=100)
print timeit.timeit(lambda: encode3(a), number=100)

# 0.113456964493
# 0.445501089096
# 0.0811159610748

rawDescription: What's an efficient way to encode a (very long) string from a dictionary? (Python)


code:
import re
import pandas

df = pandas.DataFrame([['adamant', 'Adamant Home Network', 86], ['adamant', 'ADAMANT, Ltd.', 86],
                       ['adamant bild', "TOV Adamant-Bild", 86], ['360works', '360WORKS', 94],
                       ['360works ', "360works.com ", 94]], columns=['match', 'name', 'group'])

def my_function(group):
    for i, row in group.iterrows():
        if ''.join(re.findall("[a-zA-Z]+", row['match'])).lower() not in ''.join(
                re.findall("[a-zA-Z]+", row['name'])).lower():
            # parsing the names in each columns and looking for an inclusion
            # if one of the inclusion fails, we return 'FN'
            return 'FN'
    # if all inclusions succeed, we return 'TP'
    return 'TP'

res_series = df.groupby('group').apply(my_function)
res_series.name = 'count'
res_df = res_series.reset_index()
print res_df

rawDescription: Python Pandas: How to groupby and compare columns


code:
for name, values in groupby(data, itemgetter(0)):
    first, second = zip(*values)[1:3]
    print name, max(first), min(second)

rawDescription: arrange values in a python 2D array

description: Arrange values in a python 2D array


code:
import unittest

from mock import MagicMock
from MyClass import MyClass

class FirstTest(unittest.TestCase):

    def setUp(self):
        self.A = MyClass.A
        MyClass.A = MagicMock(name='mocked A', return_value='CPU')

    def tearDown(self):
        MyClass.A = self.A

    def test_mocked_static_method(self):
        print 'First Test'
        print MyClass.check
        print MyClass.A

class SecondTest(unittest.TestCase):

    def setUp(self):
        MyClass.check = MagicMock(name='mocked check', return_value=object)

    def test_check_mocked_check_method(self):
        print 'Second Test'
        print MyClass.check
        print MyClass.A

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

rawDescription: Anyway to reset a mocked method to its original state? - Python Mock - mock 1.0b1


code:
import pandas as pd
from cStringIO import StringIO
from scipy.optimize import curve_fit

df = pd.read_table(StringIO('''
                neg       neu       pos       avg
    0           NaN       NaN       NaN       NaN
    250    0.508475  0.527027  0.641292  0.558931
    500         NaN       NaN       NaN       NaN
    1000   0.650000  0.571429  0.653983  0.625137
    2000        NaN       NaN       NaN       NaN
    3000   0.619718  0.663158  0.665468  0.649448
    4000        NaN       NaN       NaN       NaN
    6000        NaN       NaN       NaN       NaN
    8000        NaN       NaN       NaN       NaN
    10000       NaN       NaN       NaN       NaN
    20000       NaN       NaN       NaN       NaN
    30000       NaN       NaN       NaN       NaN
    50000       NaN       NaN       NaN       NaN'''), sep='\s+')

# Do the original interpolation
df.interpolate(method='nearest', xis=0, inplace=True)

# Display result
print 'Interpolated data:'
print df
print

# Function to curve fit to the data
def func(x, a, b, c, d):
    return a * (x ** 3) + b * (x ** 2) + c * x + d

# Initial parameter guess, just to kick off the optimization
guess = (0.5, 0.5, 0.5, 0.5)

# Create copy of data to remove NaNs for curve fitting
fit_df = df.dropna()

# Place to store function parameters for each column
col_params = {}

# Curve fit each column
for col in fit_df.columns:
    # Get x & y
    x = fit_df.index.astype(float).values
    y = fit_df[col].values
    # Curve fit column and get curve parameters
    params = curve_fit(func, x, y, guess)
    # Store optimized parameters
    col_params[col] = params[0]

# Extrapolate each column
for col in df.columns:
    # Get the index values for NaNs in the column
    x = df[pd.isnull(df[col])].index.astype(float).values
    # Extrapolate those points with the fitted function
    df[col][x] = func(x, *col_params[col])

# Display result
print 'Extrapolated data:'
print df
print

print 'Data was extrapolated with these column functions:'
for col in col_params:
    print 'f_{}(x) = {:0.3e} x^3 + {:0.3e} x^2 + {:0.4f} x + {:0.4f}'.format(col, *col_params[col])

rawDescription: Extrapolate values in Pandas DataFrame

description: Extrapolate values in Pandas DataFrame


code:
#!/usr/bin/env python

import sys, os, time, atexit
from signal import SIGTERM

class Daemon:
        """
        A generic daemon class.

        Usage: subclass the Daemon class and override the run() method
        """
        def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
                self.stdin = stdin
                self.stdout = stdout
                self.stderr = stderr
                self.pidfile = pidfile

        def daemonize(self):
                """
                do the UNIX double-fork magic, see Stevens' "Advanced
                Programming in the UNIX Environment" for details (ISBN 0201563177)
                http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
                """
                try:
                        pid = os.fork()
                        if pid > 0:
                                # exit first parent
                                sys.exit(0)
                except OSError, e:
                        sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
                        sys.exit(1)

                # decouple from parent environment
                os.chdir("/")
                os.setsid()
                os.umask(0)

                # do second fork
                try:
                        pid = os.fork()
                        if pid > 0:
                                # exit from second parent
                                sys.exit(0)
                except OSError, e:
                        sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
                        sys.exit(1)

                # redirect standard file descriptors
                sys.stdout.flush()
                sys.stderr.flush()
                si = file(self.stdin, 'r')
                so = file(self.stdout, 'a+')
                se = file(self.stderr, 'a+', 0)
                os.dup2(si.fileno(), sys.stdin.fileno())
                os.dup2(so.fileno(), sys.stdout.fileno())
                os.dup2(se.fileno(), sys.stderr.fileno())

                # write pidfile
                atexit.register(self.delpid)
                pid = str(os.getpid())
                file(self.pidfile,'w+').write("%s\n" % pid)

        def delpid(self):
                os.remove(self.pidfile)

        def start(self):
                """
                Start the daemon
                """
                # Check for a pidfile to see if the daemon already runs
                try:
                        pf = file(self.pidfile,'r')
                        pid = int(pf.read().strip())
                        pf.close()
                except IOError:
                        pid = None

                if pid:
                        message = "pidfile %s already exist. Daemon already running?\n"
                        sys.stderr.write(message % self.pidfile)
                        sys.exit(1)

                # Start the daemon
                self.daemonize()
                self.run()

        def stop(self):
                """
                Stop the daemon
                """
                # Get the pid from the pidfile
                try:
                        pf = file(self.pidfile,'r')
                        pid = int(pf.read().strip())
                        pf.close()
                except IOError:
                        pid = None

                if not pid:
                        message = "pidfile %s does not exist. Daemon not running?\n"
                        sys.stderr.write(message % self.pidfile)
                        return # not an error in a restart

                # Try killing the daemon process
                try:
                        while 1:
                                os.kill(pid, SIGTERM)
                                time.sleep(0.1)
                except OSError, err:
                        err = str(err)
                        if err.find("No such process") > 0:
                                if os.path.exists(self.pidfile):
                                        os.remove(self.pidfile)
                        else:
                                print str(err)
                                sys.exit(1)

        def restart(self):
                """
                Restart the daemon
                """
                self.stop()
                self.start()

        def run(self):
                """
                You should override this method when you subclass Daemon. It will be called after the process has been
            daemonized by start() or restart().
                """

rawDescription: Implementing a full Python Unix-style daemon process


code:
def altera(L1, L2):
    for elemento in L2:
        # Call method on L1 that changes it
        L1.append(elemento)
    # Call method on L2 that changes it
    L2.append(4)
    # Change object pointed to by name L1 -- Lista1
    L1[-1] = 10
    # Change object pointed to by name L2 -- Lista2
    del L2[0]
    return L2[:]

Lista1 = [1,2,3]
Lista2 = [1,2,3]

Lista3 = altera(Lista1, Lista2)

print Lista1
print Lista2
print Lista3

rawDescription: Difference between local and global variables in python


code:
latitudes = []
counts = []
blocks = []
current_block = []
for line in test:
    print line
    if line.startswith("latitude"):
        # New block: add the previous one to `blocks` and reset
        blocks.append(current_block)
        current_block = []
        latitudes.append(float(line.split()[-1]))
    elif line.startswith("number_of_data"):
        # Just append the current count to the list
        counts.append(int(line.split()[-1]))
    else:
        # Update the current block
        current_block += [float(f) for f in line.strip().split()]
# Make sure to add the last block...
blocks.append(current_block)
# And to remove the first (empty) one
blocks.pop(0)

rawDescription: How to parse block data from a text file into an 2D array in Python?

description: Parse block data from a text file into an 2D array in Python


code:
class Widget:
    def __init__(self, value):
        self.value = value

a = [Widget(4), Widget(8), Widget(15)]
b = a[0:1]

print a[0] is b[0]

rawDescription: How to build a new list from a list in Python?

description: Build a new list from a list in Python


code:
class Base(object):
    __model__ = None
    def __init__(self):
        self.headers = []

    def add_header(self, *args):
        for h in args:
            self.headers.append(h)

    def set_header_values(self):
        for h in self.headers:
            setattr(self, h, getattr(self.__model__, h))

class ChildOne(Base):
    def __init__(self, model):
        super(ChildOne, self).__init__()
        self.__model__ = model

class ChildTwo(Base):
    def __init__(self, model):
        super(ChildTwo, self).__init__()
        self.__model__ = model

class Model(object):
    foo = 'bar'

m = Model()
a = ChildOne(m)
b = ChildTwo(m)
a.add_header('tralala')
print a.headers
print b.headers

rawDescription: Python inherited base class variables

description: Python inherited base class variables


code:
import random

def logic(start, limit):
    while True:
        guess = random.randint(start, limit)
        print 'Is your number %i?' % guess
        answer = raw_input('Too high (h), too low (l) or true (t)?\n')
        if answer.lower() == 'h':
            limit = guess - 1
            if limit < start:
                print "I give up, I've run out of options"
                return
        elif answer.lower() == 'l':
            start = guess + 1
            if start > limit:
                print "I give up, I've run out of options"
                return
        elif answer.lower() == 't':
            return 'Your number was %i' % guess

rawDescription: List index out of range even though I'm asking for [0]?


code:
import threading,time,serial

#subclass threading.Thread
class RFIDSerialThread(threading.Thread):
    SLEEP_TIME = 0.001 #setup sleep time (update speed)
    #constructor with 3 parameters: serial port, baud and a label for this reader so it's easy to spot
    def __init__(self,port,baud,name):
        threading.Thread.__init__(self) #initialize this instance as a thread
        self.isAlive = False #keep track if the thread needs to run(is setup and able to go) or not
        self.name = name     #potentially handy for debugging
        self.data = ""       #a placeholder for the data read via serial
        self.station = None  #the serial object property/variable
        try:
            self.station = serial.Serial(port,baud,timeout=0) #attempt to initialize serial
            self.isAlive = True #and flag the thread as running
        except Exception,e:
            print name + " is Down",e #in case of errors print the message, including the station/serial port

    def run(self): #this gets executed when the thread is started
        while self.isAlive:
            if self.station != None:    self.data = self.station.read(12) #read serial data
            time.sleep(RFIDSerialThread.SLEEP_TIME)

if __name__ == '__main__':
    #initialize the two readers
    station1 = RFIDSerialThread('/dev/ttyUSB0',9600,"station #1")
    station2 = RFIDSerialThread('/dev/ttyUSB1',9600,"station #2")
    #start the threads
    station1.start()
    station2.start()

    while True:#continously print the data (if any)
        if len(station1.data) > 0:  print station1.name,station1.data
        if len(station2.data) > 0:  print station2.name,station2.data

rawDescription: Jumbled out put of RFID Tags

description: Jumbled out put of RFID Tags


code:
from collections import OrderedDict

_currency_values = [
    ('twenties',20),
    ('tens',10),
    ('fives',5),
    ('ones',1),
    ('quarters',0.25),
    ('dimes',0.10),
    ('nickels',0.05),
    ('pennies',0.01),
]
currency_values = OrderedDict(_currency_values)

def user_change(balance):
    user_change_results = []
    for currency in currency_values.keys():
        #print balance
        if balance == 0:
            break
        currency_amount = currency_values[currency]
        currency_change_amount = balance//currency_amount
        user_change_results.append((currency,currency_change_amount))
        balance-=(currency_change_amount*currency_amount)

    return user_change_results

if __name__ == '__main__':
    print user_change(34.36)

rawDescription: Can this be done by using a loop? Or maybe by using lists?


code:
print df.columns.str[0].unique()
['a' 'b' 'c']

rawDescription: Working in Pandas with variable names with a common suffix


code:
try:
    most_recent = max(list_user_files(join(ROOT, userdir)), key=itemgetter(1))
except ValueError as e:
    print '***DEBUG***', list(list_user_files(join(ROOT, userdir)))

rawDescription: How to find the most recently modified folder in a directory of directories (python)?

description: Find the most recently modified folder in a directory of directories (python)


code:
ifile = open('example.csv', 'r')
reader = csv.reader(ifile, delimiter = ';')
print reader.next()

rawDescription: undestanding csv module delimiter

description: Undestanding csv module delimiter


code: print ''.join(sorted(open('infile'), key=lambda l: -int(ast.literal_eval(l)[1]))),

rawDescription: How to order these lines for a highscore table in Python

description: Order these lines for a highscore table in Python


code:
data = """
   SP : STRUCT
    Spare : STRUCT  //Spare
     Val : INT  := 100;
     UpLim : INT  := 100;
     LoLim : INT ;
     Def : INT  := 100;
     Prot : INT  := 2;
    END_STRUCT ;
   END_STRUCT ;
"""

print parse(data)
# {'SP': {'Spare': {'LoLim': 0, 'Prot': 2, 'Def': 100, 'UpLim': 100, 'Val': 100}}}

rawDescription: Efficiently extract blocks of text from a space-indent structured file


code:
for name, value in root.attrib.items():
    print '{0}="{1}"'.format(name, value)

rawDescription: Get root's attributes - elementtree

description: Get root's attributes - elementtree


code:
class Test(C.Structure):
    _fields_ = [("x", C.POINTER(C.c_double)),
                ("y", C.POINTER(C.c_int))]

test = Test()
print test.x
# outputs: <__main__.LP_c_double object at 0x1014aa320>

print test.y
# outputs: <__main__.LP_c_int object at 0x101429320>

print test.x[0]
# raises ValueError: NULL pointer access

print test.y[0]
# raises ValueError: NULL pointer access

rawDescription: Access np.array in ctypes struct

description: Access np.array in ctypes struct


code:
print lst[0].decode('utf8')
print lst[1].decode('utf8')

rawDescription: List with hex escaped values to readable string in Python


code:
import mechanize, fileinput

urls = open('F:\Python\url_list2.txt')
content = [x.strip() for x in urls.readlines()]
print content

rawDescription: why am I getting a 'closed file' message when I run my python script?

description: Why am I getting a 'closed file' message when I run my python script


code:
#!/usr/bin/env python
from ftplib import FTP

def writeFunc(s):
  print "Read: " + s

ftp = FTP('ftp.kernel.org')
ftp.login()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', writeFunc)

rawDescription: Is it possible to read FTP files without writing them using Python?


code:
in_file = open(fileName, 'r')
file = in_file.readlines()

new_list = [''.join(file[i*4:(i+1)*4]) for i in range(int(len(file)/4))]
list_no_n = [item.replace('\n','') for item in new_list]

print new_list
print list_no_n

rawDescription: Read a text file into python by splitting the file into list items according to a set of characters


code:
a = [1,2,3]
b = a
print a
print b
b.append(4)
print a
print b

rawDescription: Removing current element from a list

description: Removing current element from a list


code:
a=chr(3)
print repr(a)

rawDescription: storing chr(3) to variable in python output showing junk values

description: Storing chr(3) to variable in python output showing junk values


code:
with open(inputfilename) as inputfile:
    words = []
    for line in inputfile:
        line = line.strip()
        if not line or line.startswith('#'):
            continue
        word, positions = line.split(':')
        words.extend((int(p), word) for p in positions.split(','))

print ' '.join([w for p, w in sorted(words)])

rawDescription: Turning a text file with words and their positions into a sentence


rawDescription: Printing X-type pattern in Python 2.7

description: Printing X-type pattern in Python 2.7


code:
def foo(x):
    if x == 6:
        print 'six'
    elif x == 3:
        print 'three'
    yield
    if x > 4:
        print 'greater than four'
    else:
        print 'not greater than four'
    yield

rawDescription: Is it possible to call a function, process some of its conditionals, break out, call it again and have it pickup where it left off?


code:
str = u"BBC \xe2 abc - Here is the text"
m = re.search(ur'^(.*? [-\xe2] )?(.*)', str, re.UNICODE)

# or equivalent
# m = re.match(ur'(.*? [-\xe2] )?(.*)', str, re.UNICODE)

# You don't really need re.UNICODE, but if you want to use unicode
# characters, it's better you conside à to be a letter :-) , so re.UNICODE

# group(1) contains the part before the hypen
if m.group(1) is not None:
    print m.group(1)

# group(2) contains the part after the hypen or all the string
# if there is no hypen
print m.group(2)

rawDescription: Regex: Matching a string starting with anything, then a hyphen


code:
text = "cow cat mouse cat tiger"

fDist = FreqDist(word for word in word_tokenize(text))

for word in fDist:
    print "Frequency of", word, fDist.freq(word)

rawDescription: New to nltk, having trouble with conditional frequency


code:
for i in sel_list[0]:
    print opt_list[int(i)]

rawDescription: Python: How to get multiple entries from listbox?


code:
import BaseHTTPServer

PORT = 8000

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def send_response(self, code, message=None):
        """Send the response header and log the response code.

        In contrast to base class, do not send two standard headers
        with the server software version and the current date.
        """
        self.log_request(code)
        if message is None:
            if code in self.responses:
                message = self.responses[code][0]
            else:
                message = ''
        if self.request_version != 'HTTP/0.9':
            self.wfile.write("%s %d %s\r\n" %
                             (self.protocol_version, code, message))

    def do_GET(self):
        self.send_response(500)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Internal Server Error\n")

httpd = BaseHTTPServer.HTTPServer(("", PORT), MyHandler)
print "serving at port", PORT
httpd.serve_forever()

rawDescription: Minimal HTTP server with Werkzeug - Internal Server Error


code: print df2.to_string()

rawDescription: Using Python to Combine CSV Elements From Columns to Rows


code:
myvariable = 5
def func():
    print myvariable  #prints 5

def func2():
    x = 3
    def func3():
        print x  #will print 3 because it picks it up from `func2`'s scope

    func3()

rawDescription: In Python what is a global statement?

description: In Python what is a global statement


code:
d2=item.find("div",{"class":"panel-body standard-p"})
d3=d2.text
print d3
p_id = re.findall(r'[0-9]{9}',d3)
idICareAbout = p_id[0]

rawDescription: Unicode Web Scraping

description: Unicode Web Scraping


code:
for para in soup.find_all('p'):
    if para.strong is not None:
        print para.strong.get_text()
        lines = filter(None, (t.strip() for t in para.find_all(text=True)))[1:]
        print '\n'.join(lines)
        print

rawDescription: Beautiful Soup (Python)

description: Beautiful Soup (Python)


code:
import sys
from colors import *

sys.stdout.write(RED)
print "All following prints rendered in red, until changed"

sys.stdout.write(REVERSE + CYAN)
print "From now on change to cyan, in reverse mode"
print "NOTE: 'CYAN + REVERSE' wouldn't work"

sys.stdout.write(RESET)
print "'REVERSE' and similar modes need be reset explicitly"
print "For color alone this is not needed; just change to new color"
print "All normal prints after 'RESET' above."

rawDescription: How do I print colored output to the terminal in Python?

description: Print colored output to the terminal in Python


code:
@register.tag
def my_tag(parser, token):
    # This version uses a regular expression to parse tag contents.
    try:
        # Splitting by None == splitting by spaces.
        tag_name, var_name = token.contents.split(None, 1)
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
    #this will "bind" the variable in the template to the actual_var object
    actual_var = template.Variable(var_name)
    return MyNode(template_variable)

class MyNode(template.Node):
    def __init__(self, actual_var):
        self.actual_var = actual_var

    def render(self, context):
        actual_var_value = self.actual_var.resolve(context)
        #do something with it
        return result

rawDescription: What django resolve_variable do? (template.Variable)


code:
from datetime import datetime, timedelta

from delorean import stops
import delorean

start = datetime.today()
stop = datetime.today() + timedelta(7)

for day in stops(freq=delorean.DAILY, start=start, stop=stop):
    start = day.naive().replace(hour=8, minute=0)
    stop = day.naive().replace(hour=12, minute=0)

    for t in stops(freq=delorean.MINUTELY, interval=30, start=start, stop=stop):
        print t.datetime.strftime("%A %H:%M")

rawDescription: Using datetime module to set time intervals


code:
#!/usr/bin/env python
import os
import re
import sys

def files_with_matched_lines(topdir, matched):
    for root, dirs, files in os.walk(topdir, topdown=True):
        dirs[:] = [d for d in dirs if not d.startswith('.')] # skip "hidden" dirs
        for filename in files:
            if filename.endswith(".txt"):
                path = os.path.join(root, filename)
                try:
                    with open(path) as file:
                        for line in file:
                            if matched(line):
                                yield path
                                break
                except EnvironmentError as e:
                    print >>sys.stderr, e

with open('/path/list') as file:
    hosts = file.read().splitlines()
matched = re.compile(r"\b(?:%s)\b" % "|".join(map(re.escape, hosts))).search
for path in files_with_matched_lines("/path/somefolder/", matched):
    print path

rawDescription: How to check to see if the contents in file A exist in the contents files in a directory

description: Check to see if the contents in file A exist in the contents files in a directory


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import inspect

Base = declarative_base()

class BaseClass(Base):
    __tablename__ = 'base'

    id = Column(Integer, primary_key=True)
    type = Column(String)
    a = Column(Integer)
    b = Column(Integer)
    __mapper_args__ = {"polymorphic_on": type}

class Mixin(object):
    c = Column(Integer)

class ItemA(BaseClass):
    __tablename__ = 'a'
    id = Column(ForeignKey('base.id'), primary_key=True)
    d = Column(Integer)
    __mapper_args__ = {"polymorphic_identity": 'a'}

class ItemB(Mixin, BaseClass):
    __tablename__ = 'b'
    id = Column(ForeignKey('base.id'), primary_key=True)
    __mapper_args__ = {"polymorphic_identity": 'b'}

class ItemC(Mixin, BaseClass):
    __tablename__ = 'c'
    id = Column(ForeignKey('base.id'), primary_key=True)
    e = Column(Integer)
    __mapper_args__ = {"polymorphic_identity": 'c'}

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

def magic_w_poly(base):
    insp = inspect(base)

    w_poly = []
    crit = []

    for mapper in insp.self_and_descendants:
        if "c" in mapper.c:
            w_poly.append(mapper)
            crit.append(mapper.c.c)
    w_col_c = with_polymorphic(base, w_poly)

    def comparator(value):
        return or_(
                    crit_elem == value
                    for crit_elem in crit
                )

    return w_col_c, comparator

s = Session(e)

w_col, comp = magic_w_poly(BaseClass)

print s.query(w_col).filter(comp(35))

rawDescription: SQLalchemy Joined Inheritance Query Based on Mixin Column


code:
from pytz import timezone
from datetime import datetime

zonename = "Pacific/Wallis"
now = datetime.now(tz=timezone(zonename))
dst_timedelta = now.dst()
### dst_timedelta is offset to the winter time,
### thus timedelta(0) for winter time and timedelta(0, 3600) for DST;
### it returns None if timezone is not set

print "DST" if dst_timedelta else "no DST"

rawDescription: Is a specific timezone using DST right now?


code:
from operator import itemgetter
from itertools import groupby
getter, d = itemgetter(0, 4, 5), {}
for key, grp in groupby(sorted(a, key=lambda x: x[3]), key=lambda x: x[3]):
    for item in grp:
        d.setdefault(item[1], {}).setdefault(
            item[2], {})[item[3]] = list(getter(item))
print d

rawDescription: How to make a nested dictionary from a list's items?

description: Make a nested dictionary from a list's items


code:
first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

in_first = set(first_list)
in_second = set(second_list)

in_second_but_not_in_first = in_second - in_first

result = first_list + list(in_second_but_not_in_first)
print result  # Prints [1, 2, 2, 5, 9, 7]

rawDescription: Combining two lists and removing duplicates, without removing duplicates in original list


code:
import pscript2

print sample_list

rawDescription: How to share a list between two files

description: Share a list between two files


code:
text = """\
12 items - Ironing Service    11 Mar 2009 to 10 Apr 2009
Washing service (3 Shirt)  23 Mar 2009"""

# using tok_period as defined in the OP

# parse each line separately
for tx in text.splitlines():
    print SkipTo(tok_period).parseString(tx)[0]

# or have pyparsing search through the whole input string using searchString
for [[td,_]] in SkipTo(tok_period,include=True).searchString(text):
    print td

rawDescription: How to write the grammar for this in pyparsing: match a set of words but not containing a given pattern

description: Write the grammar for this in pyparsing: match a set of words but not containing a given pattern


code:
a
a = np.resize(a, (5,4)); print a

rawDescription: Reshaping Arrays in Numpy with variables


code:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE, STDOUT

script_path = os.path.join(get_script_dir(), 'parent.py')
p = Popen([sys.executable, '-u', script_path],
          stdout=PIPE, stderr=STDOUT, bufsize=1)
with p.stdout:
    for line in iter(p.stdout.readline, b''):
        print line,
p.wait()

rawDescription: Read console output of another program in Python


code:
while True:
    line = proc.stdout.readline()
    if not line:
        break
    print ">>> " + line.rstrip()

rawDescription: Real-time intercepting of stdout from another process in Python


code:
import requests

xml = """<?xml version='1.0' encoding='utf-8'?>
б"""

data = requests.post('http://httpbin.org/post', data=xml, headers={'Authorization': 'a', 'developerToken': 'b', 'clientCostumerID': 'c'}).json

for headername, headervalue in data['headers'].iteritems():
    print '%s: %s' % (headername, headervalue)

rawDescription: Headers are not set correctly

description: Headers are not set correctly


code:
nLTest, items = ['asda','asda','test','ada','test','yuil','test'], {}
for idx, item in enumerate(nLTest):
    nLTest[idx] += str(items.setdefault(item, 0) or "")
    items[item] += 1
print nLTest

rawDescription: vectorizing a for loop in python

description: Vectorizing a for loop in python


code:
import csv

maximum_value = None
with open("vic_visitors.csv", 'rb') as visitors:
    reader = csv.DictReader(visitors)
    for row in reader:
        count, year = max((int(row[year]), year) for year in reader.fieldnames[1:])  # skip the first column
        if not maximum_value or count > maximum_value[0]:
            maximum_value = (count, row[reader.fieldnames[0]], year)

print "The greatest visitornumber was {} in {} in the year {}.".format(
    *maximum_value)

rawDescription: How to find maximum number from csv-read data in Python 2.7?

description: Find maximum number from csv-read data in Python 2.7


code:
from itertools import groupby
from pprint import pprint

def month_number(date):
    month, year = date.split('/')
    return int(year) * 12 + int(month)

L = [[date for _, date in run]
     for _, run in groupby(enumerate(sorted(months, key=month_number)),
                           key=lambda (i, date): (i - month_number(date)))]
pprint(L)

rawDescription: What is the most pythonic way to sort dates sequences?


code:
import numpy as np

s1 =[100,200,400,800]

var = []
stddev = []
mn = []
ds = []
for j in range(0,len(s1)):
    ns = s1[j]
    print ns
    data = np.random.normal(loc=0.5, scale=0.10, size=ns) #loc  mean, scale standard deviation
    v=0
    z=0
    for i in range(0, ns):
        v = v+ data[i]
        z = z + data[i]*data[i]
    mean = v/ns
    meansq = z/ns

    ds.append(data)
    mn.append(mean)
    var.append(meansq - mean*mean)
    stddev.append(np.sqrt(meansq - mean*mean))

print mn
print var
print stddev

# random data samples are within the ds list

rawDescription: need to create a list which appends previous and current stddev calculation to it

description: Need to create a list which appends previous and current stddev calculation to it


code:
from xml.etree import ElementTree as et
xmlstr="""\
<?xml version="1.0"?>
<area>
  <media>
    <options>
         <name>Jaipur</name>
    </options>
 </media>
</area>"""
doc=et.fromstring(xmlstr)
doc.find('.//name').text='Mumbai'
print et.tostring(doc)

rawDescription: how to change a node value in python

description: Change a node value in python


code: print today.replace(year=today.year + 1)

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


code:
from sympy import exp, N, S
from sympy.matrices import Matrix

data = [[S("-800.21"),S("-600.00")],[S("-600.00"),S("-1000.48")]]
m = Matrix(data)
ex = m.applyfunc(exp).applyfunc(lambda x:N(x, 100))
vecs = ex.eigenvects()
print vecs[0][0] # eigen value
print vecs[1][0] # eigen value
print vecs[0][2] # eigen vect
print vecs[1][2] # eigen vect

rawDescription: numpy arbitrary precision linear algebra

description: Numpy arbitrary precision linear algebra


code:
with open('text.txt', 'r') as handle:
    numbers = []

    for line in handle:
        field = line.split()[0]

        try:
            numbers.append(float(field))  # Or `int()`
        except ValueError:
            print field, "isn't a number"
            continue

    highest = max(numbers)

rawDescription: Simple Python headache with number comparison


code:
with open('file.txt') as fp:
    first, second = zip(*[line.rstrip().split(',') for line in fp])
    print first
    print second

rawDescription: Print first element or 2nd element from text file


code:
def main(argv):
    seen = set() # only use if you actually need  a set of all names
    with open("test.txt", 'r') as infile:
        r = csv.reader(infile, delimiter="|")
        for row in r:
            v = row[1]
            if v:
                filename = "bby_" + v + ".dat"
                existingFile = open(filename, 'a')
                existingFile.write(row)
                existingFile.close()
                seen.add(v)
            else:
                print "Empty"

rawDescription: Get data between 1st and 2nd pipe in python


code:
def foo(data, name=''):
    print type(data), data.value, name
    for j in range(1000):
        data.value += 1

rawDescription: Shared variable in python's multiprocessing


code:
for t in range(0,150):
    QRAIN1 = []
    for z in range(0,279):
        QC1 = QC[t,z,:,:]
        SUMQ =1000*np.mean(QC1)
        QRAIN1.append(SUMQ)
    print len(QRAIN1)
    QRAIN.append(QRAIN1)
QR = np.array(QRAIN)

rawDescription: Separate the first loop append and the second loop append while doing double loop in Python


code:
print np.concatenate((a[..., np.newaxis], b[..., np.newaxis]), axis=2).shape
# (3, 2, 2)

rawDescription: understanding numpy's dstack function

description: Understanding numpy's dstack function


code:
rows = ['Date,Open,High,Low,Close,Volume,Adj Close', '2012-11-30,691.31,699.22,685.69,698.37,3163600,698.37', '2012-11-29,687.78,693.90,682.00,691.89,2776500,691.89','2012-11-28,668.01,684.91,663.89,683.67,3042000,683.67', '2012-11-27,660.17,675.00,658.00,670.71,2508700,670.71']

def format_rows(rows, gap):
    split_rows = [row.split(',') for row in rows]
    # Splits each row up, by comma
    column_lengths = [max(col_len) for col_len in zip(*[map(len, row) for row in split_rows])]
    # Finds the maximum size of each column

    for row in split_rows:
        col_lengths = zip(row, column_lengths)
        print ''.join(col.ljust(col_length + gap, ' ') for (col, col_length) in col_lengths)
        # Prints out the data, making sure there's a minimum of "gap" spaces
        # between each column

rawDescription: python yahoo finance format

description: Python yahoo finance format


code:
orig_list = [
   { 'cpu': 4, 'mem': '4', 'name': 'server1', 'drives': '4', 'nics': '1' },
   { 'cpu': 1, 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' },
   { 'cpu': 2, 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' }
   ]

print orig_list

for item in orig_list :
    if item['cpu'] < 4:
        item['cpu'] = 4

print orig_list

rawDescription: How to updating dict items inside a list of dicts

description: Updating dict items inside a list of dicts


rawDescription: How to print string array in reverse order in python

description: Print string array in reverse order in python


code:
import numpy as np
import time

A = np.random.randint(0, 10, (3200, 1400))

t = time.time()
S = A.cumsum(axis=0).cumsum(axis=1)
print np.round_(time.time() - t, 3), 'sec elapsed'

rawDescription: Efficient summed Area Table Calculation with Numpy


code:
import os

# Print current working directory
print "Current working dir : %s" % os.getcwd()

rawDescription: Python Script not creating file when run through Task Scheduler


rawDescription: NumPy - Set values in structured array based on other values in structured array


code:
import itertools
import string
others = ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z']
# this dict is fixed
dict1 = {k : [k] for k in string.uppercase if k not in others}
# iterate all possibles in others, then merge two dicts into one
for i in itertools.permutations(others):
    dict2 = dict(zip(others, i))
    print dict(dict1.items() + dict2.items())

rawDescription: PYTHON: How do I create a list of every possible letter mapping using a dictionary that stores every possible letter mapping combination?


code:
def foo(**kwargs):
    print kwargs

params = {'arg1':1, 'arg2':2}

foo(**params)         # Version 1
foo(arg1=3,arg2=4)    # Version 2

rawDescription: order of parameters of function call of python

description: Order of parameters of function call of python


code:
import re

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

driver = webdriver.Chrome()
driver.get('https://www.google.com/?gws_rd=ssl#q=plumbers%2BAvondale%2BAZ')

# waiting for results to load
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "lclbox")))

phone_re = re.compile(r"\(\d{3}\) \d{3}-\d{4}")

for result in box.find_elements_by_class_name("intrlu"):
    for span in result.find_elements_by_tag_name("span"):
        if phone_re.search(span.text):
            parent = span.find_element_by_xpath("../..")
            print parent.text
            break

    try:
        reviews = result.find_element_by_partial_link_text("review").text
    except NoSuchElementException:
        reviews = "0 Google reviews"

    print reviews
    print "-----"

rawDescription: Webdriver: How to find elements when class name contains space?


code:
def foo():
    print "foo"

def bar():
    print "bar"

first = foo

print (first == bar) # prints "False"
print (first == foo) # prints "True"

rawDescription: Testing for functions called from a list in python


code:
import urlparse

parsed = urlparse.parse_qs(referer_url)
print parsed['start'][0]

rawDescription: Retrieving arguments from HTTP_REFERER string


code:
from xml.etree.ElementTree import fromstring, ElementTree

data = """<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to> Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
"""

tree = ElementTree(fromstring(data))
print [item.tag for item in tree.getiterator()]

rawDescription: List of distinct XML element names using BeautifulSoup


code:
def Process(records, per_record_hook=None):
    "adds all records to XYZ system"
    for record in records:
        if per_record_hook:
            per_record_hook(record)
        XYZ.append(record)

def print_record(record):
    "print a '.' for each record (primitive counter)"
    print '.'

rawDescription: Can API hooking in python be OS agnostic?


code:
sentence = thestring.split(" ")
from collections import Counter
for fruit, num in sorted(Counter(sentence).items()):
    print "{:10}|{:5}".format(fruit.ljust(10, "."), str(num).rjust(5, "."))

rawDescription: Order a sentence alphabetically and count the number of times each words appears and print in a table


code:
import cgi
form = cgi.FieldStorage()

import json

import serial
ser = serial.Serial('/dev/ttyUSB0', 57600)
ser.write("Hello, this is a command for value %s from slider id %s!\n" % (form["id"], form["value"]))      # write a string
ser.close()

print "Content-type: application/json"
print
print(json.JSONEncoder().encode({"status":"ok"}))

rawDescription: Running Python CGI Scripts from Javascript and JQuery Mobile UI


code: exec 'f(' + ','.join(str(i) for i in range(5000)) + ')'

rawDescription: What is a maximum number of arguments in a Python function?