code:
class DataStream(object):
    def __init__(self, iterable):
        self.iterable = iter(iterable)
        self.next_item = (None, 0)
        self.next_x = None
        self.current_y = 0
        self.next()

    def next(self):
        if self.next_item is None:
            raise StopIteration()
        self.current_y = self.next_item[1]
        try:
            self.next_item = self.iterable.next()
            self.next_x = self.next_item[0]
        except StopIteration:
            self.next_item = None
            self.next_x = None
        return self.next_item

    def __iter__(self):
        return self

class MergedDataStream(object):
    def __init__(self, *iterables):
        self.streams = [DataStream(i) for i in iterables]
        self.outseq = []

    def next(self):
        xs = [stream.next_x for stream in self.streams if stream.next_x is not None]
        if not xs:
            raise StopIteration()
        next_x = min(xs)
        current_y = 0
        for stream in self.streams:
            if stream.next_x == next_x:
                stream.next()
            current_y += stream.current_y
        self.outseq.append((next_x, current_y))
        return self.outseq[-1]

    def __iter__(self):
        return self

if __name__ == '__main__':
    seqs = [
        [(1, 100), (5, 100), (10, 100)],
        [(4, 150), (5, 100), (18, 150)],
        ]

    sm = MergedDataStream(*seqs)
    for x, y in sm:
        print "%02s: %s" % (x, y)

    print sm.outseq

rawDescription: Algorithm to sum/stack values from a time series graph where data points don't match on time


code:
import ast

x = r'''import sys
sys.stdout.write('foo\n')
sys.stdout.write('bar\n')'''

y = r'''import   sys
sys.stdout.\
    write('foo\n'); sys.stdout.\
    write(

    'bar\n') # This is an unnecessary comment'''

xd = ast.dump(ast.parse(x))
yd = ast.dump(ast.parse(y))
print xd == yd

rawDescription: comparing Python code for equivalence

description: Comparing Python code for equivalence


rawDescription: Pandas TimeSeries resample produces NaNs


code:
from BeautifulSoup import BeautifulSoup
from selenium import webdriver
import re

url="https://www.lotto.de/de/ergebnisse/lotto-6aus49/archiv.html"
driver = webdriver.PhantomJS(executable_path="C://Users//Royskatt//Downloads//phantomjs-2.0.0-windows//bin//phantomjs.exe")
#driver = webdriver.Firefox()
driver.get(url)
soup = BeautifulSoup(driver.page_source)

lottonumbers = []

for ul in soup.findAll("div", {"class": "winning_numbers boxRow clearfix"}):
    for i in re.findall(r'(?<=zahl\([1-6]\)">)\d{1,2}|(?<="last">)\d', str(ul)):
        lottonumbers.append(i)

print lottonumbers

rawDescription: Python XML string reassembly

description: Python XML string reassembly


rawDescription: How to avoid the L in Python

description: Avoid the L in Python


code:
from functools import wraps
from flask import request

def webLog(func):
    @wraps(func)
    def newFunc(*args, **kwargs):
        print request.url + " : " + str(request.remote_addr)
        return func(*args, **kwargs)
    return newFunc

rawDescription: how to get request of flask in decorator logger?

description: Get request of flask in decorator logger


code:
from signal import pause, signal, SIGINT, SIGTERM, SIGQUIT

class A:
    def __init__(self):
        self._signum = 0

    def _signal_handler(self, signum, frame):
        print "Received terminate request - signal = {0}".format(signum)
        self._signum = signum
        return

    def wait_for_signal(self):
        signal(SIGINT, self._signal_handler)
        signal(SIGTERM, self._signal_handler)
        signal(SIGQUIT, self._signal_handler)
        print "Pause until signaled"
        while self._signum == 0:
            pause()

def main():
    a = A()
    a.wait_for_signal()

main()

rawDescription: threading.Condition.wait() is not catching SIGTERM

description: Threading.Condition.wait() is not catching SIGTERM


code:
pygame.init()
screen = pygame.display.set_mode((720, 480))
pygame.display.set_caption('My game')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))

charRect = pygame.Rect((0,0),(10, 10))
print os.path.abspath("airbender.png")
charImage = pygame.image.load(os.path.abspath("ImageName.png"))
charImage = pygame.transform.scale(charImage, charRect.size)
charImage = charImage.convert()

background.blit(charImage, charRect) #This just makes it in the same location
                                     #and prints it the same size as the image

screen.blit(background,(0,0))
pygame.display.flip()

rawDescription: How do I blit an image to a surface in pygame that's the same size as a Rectangle

description: Blit an image to a surface in pygame that's the same size as a Rectangle


code:
import click
import cmd

class REPL(cmd.Cmd):
    def __init__(self, ctx):
        cmd.Cmd.__init__(self)
        self.ctx = ctx

    def default(self, line):
        subcommand = cli.commands.get(line)
        if subcommand:
            self.ctx.invoke(subcommand)
        else:
            return cmd.Cmd.default(self, line)

@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
    if ctx.invoked_subcommand is None:
        repl = REPL(ctx)
        repl.cmdloop()

@cli.command()
def a():
    """The `a` command prints an 'a'."""
    print "a"

@cli.command()
def b():
    """The `b` command prints a 'b'."""
    print "b"

if __name__ == "__main__":
    cli()

rawDescription: Creating a shell command line application with Python and Click


code:
just_before_midnight = datetime(2014, 7, 16, 23, 59, 59)
just_after_midnight_the_next_day = just_before_midnight + timedelta(seconds=2)
print just_before_midnight, just_after_midnight_the_next_day

rawDescription: Adding timedelta to datetime not moving to next day


code:
for k1 in sorted(a.keys()):
   sub = a[k1]
   for k2 in sorted(sub.keys()):
     print k1, k2, sub[k2] # or do whatever else

rawDescription: python sort a two-level defaultdict and return defaultdict or dict

description: Python sort a two-level defaultdict and return defaultdict or dict


code:
a_list = range(4)
print '[{}]'.format(', '.join(hex(x) for x in a_list))

rawDescription: Printing an list with hex elements

description: Printing an list with hex elements


code:
age = .01
print 'test%s' % str(age)[1:] if 0<age<1 else str(age)

rawDescription: Using strings of decimals in python

description: Using strings of decimals in python


code:
from inspect import isfunction

for obj in globals().values():
    if isfunction(obj) and obj.__module__ == __name__:
        print obj()

rawDescription: How to automatically execute all functions in script?

description: Automatically execute all functions in script


code:
import re
from collections import Counter

count = Counter()

for line in open ('data.txt', 'r'):
  for word in re.split('(?<!>)\s+', line):
    count[word] += 1

print count['=> donor']

rawDescription: How to include white space when searching for a string in text file

description: Include white space when searching for a string in text file


code:
import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')
nm.command_line()
nm.scaninfo()
nm.all_hosts()
nm['127.0.0.1'].hostname()
nm['127.0.0.1'].state()
nm['127.0.0.1'].all_protocols()
nm['127.0.0.1']['tcp'].keys()
nm['127.0.0.1'].has_tcp(22)
nm['127.0.0.1'].has_tcp(23)
nm['127.0.0.1']['tcp'][22]
nm['127.0.0.1'].tcp(22)
nm['127.0.0.1']['tcp'][22]['state']
for host in nm.all_hosts():
    print('----------------------------------------------------')
    print('Host : %s (%s)' % (host, nm[host].hostname()))
    print('State : %s' % nm[host].state())
    for proto in nm[host].all_protocols():
        print('----------')
        print('Protocol : %s' % proto)

        lport = nm[host][proto].keys()
        lport.sort()
        for port in lport:
            print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
print(nm.csv())
nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')
hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
for host, status in hosts_list:
    print('{0}:{1}'.host)
nma = nmap.PortScannerAsync()
def callback_result(host, scan_result):
    print '------------------'
    print host, scan_result

nma.scan(hosts='192.168.1.0/30', arguments='-sP', callback=callback_result)
while nma.still_scanning():
    print("Waiting >>>")
    nma.wait(2)   # you can do whatever you want but I choose to wait after the end of the scan

nm = nmap.PortScannerYield()
for progressive_result in nm.scan('127.0.0.1/24', '22-25'): print(progressive_result)

rawDescription: parsing an nmap result

description: Parsing an nmap result


code:
class ChainList(list):
    def __getattribute__(self, name):
        try:
            return object.__getattribute__(self, name)
        except:
            pass

        return ChainList([getattr(child, name)
            for child in self if name in dir(child)])

    def __call__(self, *args, **kwargs):
        return ChainList([child(*args, **kwargs)
            for child in self if callable(child)])

class GrandParent(object):
    def __init__(self,name='',age=0,is_retired=True,children=[]):
        self.name = name
        self.age = age
        self.is_retired = is_retired

        if children:
            self.children = ChainList(children)

    def print_mychildren(self):
       for child in self.children:
            print "Child: ",child

    def mychildren(self):
        return self.children

    def __str__(self):
        return "name: %s age: %s is_retired:%s" %(self.name,self.age,self.is_retired)

class Parent(object):
    def __init__(self,name='',age=0,has_mortgage=True,children=[]):
        self.name = name
        self.age = age
        self.has_mortgage = has_mortgage

        if children:
            self.children = ChainList(children)

    def print_mychildren(self):
       for child in self.children:
            print "Child: ",child

    def __str__(self):
        return "name: %s age: %s has_mortgage: %s" %(self.name,self.age,self.has_mortgage)

    def mychild(self, name):
        for child in self.children:
            if child.name == name:
                return child

class Child(object):
    def __init__(self,name='',age=0,has_toys=True):

        self.name = name
        self.age = age
        self.has_toys = has_toys

    def __str__(self):
        return "name: %s age: %s has_toys:%s" %(self.name,self.age,self.has_toys)

if __name__ == '__main__':

    beaver = Child('Beaver',12,True)
    ward = Child('Ward',16,False)

    june = Parent('June',38,True,[beaver,ward])

    grandpa = GrandParent('Grandpa',61,True,[june])

    print grandpa

    grandpa.print_mychildren() # print June

    # Doesn't work
    grandpa.mychildren().print_mychildren() #  I want it to print Ward and Beaver
    # Doesn't work
    print grandpa.mychildren().mychild('Beaver').age # I want it to return an age

rawDescription: Python: How do you chain methods on lists of lists of objects?


code:
import requests

URL = 'https://www.yourlibrary.ca/account/index.cfm'
payload = {
    'barcode': 'your user name/login',
    'telephone_primary': 'your password',
    'persistent': '1'  # remember me
}

session = requests.session()
r = requests.post(URL, data=payload)
print r.cookies

rawDescription: How to simulate HTTP post request using Python Requests module?

description: Simulate HTTP post request using Python Requests module


code:
def isnumeric(obj):
    try:
        obj + 0
        return True
    except TypeError:
        return False

print isnumeric([1,2,3]) # False
print isnumeric(2.5)     # True
print isnumeric('25')    # False

rawDescription: What is the best way to check if a variable is of numeric type in python


code:
import time
x = 100

while x > 0:
    print x
    x-=1
    time.sleep(1)

rawDescription: Python - How to Subtract a Variable By 1 Every Second?


code:
#!/usr/bin/python

from Tkinter import *
from tkFileDialog import askopenfilename

root = Tk()
root.withdraw()
print askopenfilename()

rawDescription: JFileChooser for Python?

description: JFileChooser for Python


code:
#Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.

balance = 999999
annualInterestRate = 0.18

monthlyPayment = 0.0
monthlyInterestRate = annualInterestRate/12
low = balance / 12.0
high = ((balance * ((1.0 + monthlyInterestRate)**12.0))/12.0)
monthlyPayment = (high+low)/2
while balance != 0:
    newBalance = balance
    for x in range(1, 13):
        lastMonthBalance = newBalance - monthlyPayment
        newBalance = lastMonthBalance + ((monthlyInterestRate) * lastMonthBalance)
        if -0.05 <= newBalance <= 0.05:
            balance = 0

    if newBalance < 0.0:
        high = monthlyPayment
    elif newBalance > 0.0:
        low = monthlyPayment

    monthlyPayment = (low + high) / 2

print "Lowest Payment: %.2f" % monthlyPayment

rawDescription: Python - Finding monthly NPV of Credit Card


code:
bank = {'bob':[1122,0],'fred':[2211,0]}
values =bank.values()
print values[0]
print values[0][1]

rawDescription: Changing and adding to lists within dictionaries in python2.7


code:
import win32api, win32con

print "start"
win32api.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
print "end"

rawDescription: How to make SendMessage unblocking?

description: Make SendMessage unblocking


code:
def find( x, string, backward = False, ignore_case = False ):
    x      = str(x)
    string = str(string)
    if ignore_case:
        x = x.lower()
        string = string.lower()
    str_list = [ i for i in string ]
    x_list   = [ i for i in x      ]
    if backward:
        x_list.reverse()
        str_list.reverse()
    x = ''.join(x_list)
    string = ''.join(str_list)
    lenx = len(x)
    ans = []
    for i in range( len(str_list) - lenx ):
        if x == string[i:i+lenx]:
            ans.append( i )
    return ans

def rfind( x, string, ignore_case = False):
    return find( x, string, backward = True, ignore_case = ignore_case )

print  find('f','abcdefgacdfh')
# [5, 10]
print rfind('f','abcdefgacdfh')
# [1, 6]
print  find(12,'aaa3331222aa12a')
# [6, 12]
print rfind(12,'aaa3331222aa12a')
# [1, 7]

rawDescription: How does find procedure work in python

description: How does find procedure work in python


code:
import os

sep = " "

with open("inline-t.txt", "rb+") as fd:
    seekpos = fd.tell()
    line = fd.readline()
    while line:
        print line
        next = raw_input(">>> ")
        if next == ":q":
            break
        if next:
            values = line.split(sep)
            newval = values[0] + sep + next + '\n'
            if len(newval) == len(line):
                fd.seek(seekpos)
                fd.write(newval)
                fd.flush()
                os.fsync(fd)
            else:
                remaining = fd.read()
                fd.seek(seekpos)
                fd.write(newval + remaining)
                fd.flush()
                os.fsync(fd)
                fd.seek(seekpos)
                line = fd.readline()
        seekpos = fd.tell()
        line = fd.readline()

rawDescription: edit a file line by line interactively from user input in python

description: Edit a file line by line interactively from user input in python


code:
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os,sys
import json

ckey = '***'
consumer_secret = '***'
access_token_key = '***'
access_token_secret = '***'

start_time = time.time()

class listener(StreamListener):

    def __init__(self, start_time, time_limit=300):

        self.time = start_time
        self.limit = time_limit

    def on_data(self, data):

        while (time.time() - self.time) < self.limit:
            try:
                tweet = json.loads(data)
                user_name = tweet['user']['name']
                tweet_count = tweet['user']['statuses_count']
                text = tweet['text']
                saveFile = open('user_tweets29.json', 'a')
                saveFile.write(text.encode('utf8'))
                saveFile.write('\n')
                saveFile.close()

                return True
            except BaseException, e:
                print 'failed ondata,', str(e)
                time.sleep(5)
                pass
        exit()

    def on_error(self, status):
        print statuses

auth = OAuthHandler(ckey, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
twitterStream = Stream(auth, listener(start_time, time_limit=60))
twitterStream.filter(track=['twitter'])

rawDescription: save twitter user information into a file using StreamListener

description: Save twitter user information into a file using StreamListener


code:
df = df_final[(df_final['time'] > datetime.time(16,30)) & (df_final['time'] < datetime.time(16,35))].groupby('dish_name').sum()

df['dish_selling_rate'] = df['dish_quantity'] / df['dish_quant_bought']

print df.head(5)

rawDescription: Pandas percentage calculation with groupby operation


code:
import datetime

todayDate = datetime.date.today()
if todayDate.day > 25:
    todayDate += datetime.timedelta(7)
print todayDate.replace(day=1)

rawDescription: finding first day of the month in python

description: Finding first day of the month in python


code:
def merge(x, key='IP'):
    tmp = []
    for i in x:
        if (i[0:len(key)] == key) and len(tmp):
            yield ' '.join(tmp)
            tmp = []
        tmp.append(i)
    if len(tmp):
        yield ' '.join(tmp)

a = ['IP 123 84','apple','mercury','IP 543 65','killer','parser','goat','IP 549 54 pineapple','django','python']
print list(merge(a))

['IP 123 84 apple mercury', 'IP 543 65 killer parser goat', 'IP 549 54 pineapple django python']

rawDescription: Merge list items based on condition within the list


code:
#!/usr/bin/python

import subprocess
import re
import sys
import time
import datetime
import plotly.plotly as py # plotly library
from plotly.graph_objs import Scatter, Layout, Figure, Data, Stream, YAxis

# Plot.ly credentials and stream tokens
username                 = 'plotly_username'
api_key                  = 'plotly_api_key'
stream_token_temperature = 'stream_token_1'
stream_token_humidity    = 'stream_token_2'

py.sign_in(username, api_key)

trace_temperature = Scatter(
    x=[],
    y=[],
   stream=Stream(
        token=stream_token_temperature
    ),
    yaxis='y'
)

trace_humidity = Scatter(
    x=[],
    y=[],
    stream=Stream(
        token=stream_token_humidity
    ),
    yaxis='y2'
)

layout = Layout(
    title='Raspberry Pi - Temperature and humidity',
    yaxis=YAxis(
        title='Celcius'
    ),
    yaxis2=YAxis(
        title='%',
        side='right',
        overlaying="y"
    )
)

data = Data([trace_temperature, trace_humidity])
fig = Figure(data=data, layout=layout)

print py.plot(fig, filename='Raspberry Pi - Temperature and humidity')

stream_temperature = py.Stream(stream_token_temperature)
stream_temperature.open()

stream_humidity = py.Stream(stream_token_humidity)
stream_humidity.open()

while(True):
  # Run the DHT program to get the humidity and temperature readings!
  output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]);
  print output

  # search for temperature printout
  matches = re.search("Temp =\s+([0-9.]+)", output)
  if (not matches):
        time.sleep(3)
        continue
  temp = float(matches.group(1))

  # search for humidity printout
  matches = re.search("Hum =\s+([0-9.]+)", output)
  if (not matches):
        time.sleep(3)
        continue
  humidity = float(matches.group(1))

  print "Temperature: %.1f C" % temp
  print "Humidity:    %.1f %%" % humidity

  # Append the data to the streams, including a timestamp
  now = datetime.datetime.now()
  stream_temperature.write({'x': now, 'y': temp })
  stream_humidity.write({'x': now, 'y': humidity })

  # Wait 30 seconds before continuing
  time.sleep(30)

stream_temperature.close()
stream_humidity.close()

rawDescription: How to create two y-axes streaming plotly

description: Create two y-axes streaming plotly


code:
emp_names = []
num_to_enter = 10
while num_to_enter > 0:
    emp_names.append(raw_input("enter name"))
    num_to_enter -= 1

print emp_names[0]

#do what you need to do

rawDescription: Python 2.7.5 how to accumulate and declare values in an array/ list from user input?


code:
print "The number sequence is",
for bin_tens in bin_range:
    print group_counter[bin_tens],
print

rawDescription: Display Integers in a row format in python


code:
code_string = '''
@staticmethod
def test():
    return 'returning blah'
'''
exec(code_string)
test
class ClassName(object):
    def instancemethod(self):
            print "instancemethod!"
setattr(ClassName, 'teststaticmethod', test)
ClassName.teststaticmethod()

rawDescription: python exec, add staticmethod to class

description: Python exec, add staticmethod to class


code:
import numpy as np
full = [line for line in open("foo4",'r').readlines() if not line.startswith("#")]
datablocks = np.split(np.array(full), len(full)/4081)
for block in datablocks:
    #lines is a dataset, sorted by first column
    lines = sorted(block, key= lambda line : int(line.split()[0]))
    print lines

rawDescription: Python sort data every nth line?

description: Python sort data every nth line


code:
A = ["40", "60", "80"]

B = ["10", "42", "100"]

newA = filter(lambda a: all([abs(int(a) - int(b)) >= 5 for b in B]), A)
newB = filter(lambda b: all([abs(int(a) - int(b)) >= 5 for a in A]), B)

print newA
print newB

rawDescription: How to mathematically subtract items in two lists in Python and only output those items which meet a condition?

description: Mathematically subtract items in two lists in Python and only output those items which meet a condition


code:
concat = "".join(info)
names = concat.split()
print names

rawDescription: Concatenating certain parts of a list. Removing spaces.


code:
# The following two classes are toys, both have a playWith
# as you wanted, each playWith do different things
class Ball:
    def __init__(self):
        self.name = "ball"

    def playWith(self):
        print "the ball bounces"

class Car:
    def __init__(self):
        self.name = "car"

    def playWith(self):
        print "the car is fast"

# This is a Python generator, every time .next() is called on it,
# the next "yield-value" is returned
def generator():
    while True:
        yield Ball()
        yield Car()

# This is the creator, it has to be a class rather than a function
# since you wanted a new toy each time getNewToy is called
# and as such the generator needs to be tracked
class ToyCreator:
    def __init__(self):
        self.generator = generator()

    def getNewToy(self):
        return self.generator.next()

# Create five toys, print their name and play with them
# Do note here that even though we ask for five toys but only have
# two "yields" in the generator, the generator "wraps around" (since,
# internally, its just an endless loop)
toyCreator = ToyCreator()
for i in range(5):
    toy = toyCreator.getNewToy()
    print "Toy",i,toy.name,"\t:",
    toy.playWith()

rawDescription: Creating an Instance of a Class with a variable in Python


code:
import pycurl
import cStringIO
import re

curl = pycurl.Curl()

buff = cStringIO.StringIO()
hdr = cStringIO.StringIO()

curl.setopt(pycurl.URL, 'http://example.org')
curl.setopt(pycurl.WRITEFUNCTION, buff.write)
curl.setopt(pycurl.HEADERFUNCTION, hdr.write)
curl.perform()

print "status code: %s" % curl.getinfo(pycurl.HTTP_CODE)
# -> 200

status_line = hdr.getvalue().splitlines()[0]
m = re.match(r'HTTP\/\S*\s*\d+\s*(.*?)\s*$', status_line)
if m:
    status_message = m.groups(1)
else:
    status_message = ''

print "status message: %s" % status_message
# -> "OK"

rawDescription: How to get HTTP status message in (py)curl?

description: Get HTTP status message in (py)curl


code:
b = self.a.b
if b.c:
    b.c = 'foo'
print b.c

rawDescription: python shorten nested unmutable object variable names

description: Python shorten nested unmutable object variable names


code:
import win32api

drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print drives

rawDescription: Is there a way to list all the available drive letters in python?


code:
result = {}
for item in listOfHeroes:
    if item['user__user__username'] not in result:
        result[item['user__user__username']] = []
    result[item['user__user__username']].append({'skill': item['skill'], 'level': item['level']})

for key, value in result.items():
    print 'User: {0} / Skills: {{ {1} }}'.format(key, ' / '.join(['{0} level {1}'.format(skill['skill'], skill['level']) for skill in value]))

rawDescription: Group Values in a data structure

description: Group Values in a data structure


code:
from nltk.corpus import wordnet as wn
from nltk.corpus.reader import NOUN

#this gives a synsets list of empty length, since there is no noun corresponding to 'propose'
synsets = wn.synsets('propose', NOUN)

if synsets.length == 0 :
    print ' We found a pure NOUN'

 #this will give you a non empty synset list since 'iron' can be a NOUN too.
synsets = wn.synsets('iron',NOUN)

if synsets.length > 0 :
   print 'Iron is also a noun other than verb'

rawDescription: Extracting nouns alone from NLTK WordNet


code:
import operator
LL = ['ACCCACCCGTGG','AATCCC','CCCTGAGG']

def createLenList(n,LL):
    stubs = {}
    for l in LL:
      for i,e in enumerate(l):
          stub = l[i:i+n]
          if len(stub) == n:
             if stub not in stubs: stubs[stub]  = 1
             else:                 stubs[stub] += 1

    maxKey =   max(stubs.iteritems(), key=operator.itemgetter(1))[0]
    return [maxKey,stubs[maxKey]]

maxStub =  createLenList(3,LL)
print maxStub

rawDescription: List of strings, get common substring of n elements, Python


code:
def create_file(self):

    modal = ModalView(title="Just a moment", size_hint=(0.5, 0.5))
    btn_ok = Button(text="Save & continue", on_press=self.save_file)
    btn_no = Button(text="Discard changes", on_press=modal.dismiss)

    box = BoxLayout()
    box.add_widget(btn_ok)
    box.add_widget(btn_no)

    modal.add_widget(box)
    modal.open()
    modal.bind(on_dismiss=self._continue_create_file)

def _continue_create_file(self, *args):
    print "back now!"
    self.editor_main.text = ""

    new = CreateView()
    new.open()

rawDescription: Kivy: How to create a 'blocking' popup/modalview?


code:
data = [('#example', 65544),('#Example', 92002)]

hashtable = {}

for i in data:

    # See if this thing exists regardless of casing
    if i[0].lower() not in hashtable:

        # Create a dictionary
        hashtable[i[0].lower()] = {
            'meta':'',
            'value':[]
        }

        # Copy the relevant information
        hashtable[i[0].lower()]['value'].append(i[1])
        hashtable[i[0].lower()]['meta'] = i[0]

    # If the value exists
    else:

        # Check if the number it holds is the max against
        # what was collected so far. If so, change meta
        if i[1] > max(hashtable[i[0].lower()]['value']):
            hashtable[i[0].lower()]['meta'] = i[0]

        # Append the value regardless
        hashtable[i[0].lower()]['value'].append(i[1])

# For output purposes
myList = []

# Build the tuples
for node in hashtable:
    myList.append((hashtable[node]['meta'],sum(hashtable[node]['value'])))

# Voila!
print myList
# [('#Example', 157546)]

rawDescription: Python: Sum entries in list of tuples entries with case sensitive keys?


code:
from collections import defaultdict

x=['a','3','4','b','1','2','c','4','5']

key='<unknown>' # needed if the first value of x is a number
category = defaultdict(list)  # defaultdict with list
for i in x:
    if i.isalpha():
        key = i;
    else:
        category[key].append(i) # no need to initialize with an empty list

print category

rawDescription: How to add values in keys in a dictionary inside a loop?

description: Add values in keys in a dictionary inside a loop


code:
import pandas as pd

df = pd.read_csv('example.csv', names=['label', 'value'])
# >>> df
#   label  value
# 0     B      1
# 1     A      1
# 2     A      1
# 3     B      1
# 4     A      3
# 5     A      2
# 6     B      1
# 7     B      2
# 8     B      2

s = df.groupby(['label', 'value']).size()
# >>> s
# label  value
# A      1        2
#        2        1
#        3        1
# B      1        3
#        2        2
# dtype: int64

# ref1: http://stackoverflow.com/questions/15751283/converting-a-pandas-multiindex-dataframe-from-rows-wise-to-column-wise
# ref2: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.unstack.html
m = s.unstack()
# >>> m
# value  1  2   3
# label
# A      2  1   1
# B      3  2 NaN

# Below are optional: just to make it look more like what you want
m.columns.name = None
m.index.name = None
m = m.fillna(0)
print m
#    1  2  3
# A  2  1  1
# B  3  2  0

rawDescription: Create Matrix from a csv file - Python

description: Create Matrix from a csv file - Python


code:
import urllib
import httplib

server='myserver.com'
get_data='/link/with/get/data.php?test=1'

data = urllib.urlencode({'a': 2})
h = httplib.HTTPConnection('enfenion.com')
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
h.request('POST', get_data, data, headers)
r = h.getresponse()
print r.read()

rawDescription: Submiting form with python

description: Submiting form with python


rawDescription: How to extract coordinates(lat, lan) from a URL in Python?

description: Extract coordinates(lat, lan) from a URL in Python


code:
import numpy as np
from Tkinter import *
from ttk import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

def on_key_event(event):
   print('you pressed %s'%event.key)
   key_press_handler(event, canvas, toolbar)

def mOpen():
   global t
   global a
   global canvas_3

   var = IntVar()
   slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, variable=var, command=updateScale)
   slider_1.place(x = 765,y=390)
   t = np.random.random_sample(100000).reshape(100,100,10)
   f = Figure(figsize=(5,4),dpi=100)
   a = f.add_subplot(111)
   a.imshow(t[:,:,var.get()])
   print var.get()
   canvas_3 = FigureCanvasTkAgg(f,master = mGui)
   canvas_3.show()
   canvas_3.get_tk_widget().place(x=5,y=5)
   toolbar_3 = NavigationToolbar2TkAgg( canvas_3, mGui )
   toolbar_3.update()
   toolbar_3.place(x=10,y=15)
   canvas_3._tkcanvas.place(x=7,y=7)
   canvas_3.mpl_connect('key_press_event', on_key_event)

def updateScale(var):
   print "scale is now %s" % (var)
   a.imshow(t[:,:,int(float(var))])
   canvas_3.show()

mGui = Tk()
mOpen()
mGui.geometry('900x900+300+10')
mGui.title('Plot')
mGui.mainloop()

rawDescription: How to update matplotlib with Scale widget in Tkinter

description: Update matplotlib with Scale widget in Tkinter


code:
print [ type(x) for x in htmldata ]
Series([ str(x) for x in htmldata ])

rawDescription: Converting a list in a dict to a Series

description: Converting a list in a dict to a Series


code:
from itertools import product

with open('testing2.txt') as fin:
    for line in fin:
        items = line.split()
        first, rest = items[0], items[1:]
        for i, j in product(first, rest):
            print i, j

rawDescription: python file read with different size of lines

description: Python file read with different size of lines


code:
for i in range(500):
  var_name = "pr%i" % i
  var_value = getattr(self.ui, "pr%i" % i).isChecked()
  print "%s has value: %s" % (var_name, var_value)

rawDescription: From string to pyqt object as variable

description: From string to pyqt object as variable


code:
from string import punctuation, whitespace
import urllib2
import datetime
import re
from bs4 import BeautifulSoup as Soup

today = datetime.date.today()
html = urllib2.urlopen("http://www.99acres.com/property-in-velachery-chennai-south-ffid").read()

soup = Soup(html)
print "INSERT INTO Property (URL,Rooms, Place, Phonenumber1,Phonenumber2,Phonenumber3,Typeofperson, Name)"
print "VALUES ("
re_digit = re.compile('(\d+)')
properties = soup.findAll('a', title=re.compile('Bedroom'))

for eachproperty in soup.findAll('div', {'class':'sT'}):
  a      = eachproperty.find('a', title=re.compile('Bedroom'))
  pdate  = eachproperty.find('i', {'class':'pdate'})
  pdates = re.sub('(\s{2,})', ' ', pdate.text)
  div    = eachproperty.find('div', {'class': 'sT_disc grey'})
  try:
    project = div.find('span').find('b').text.strip()
  except:
    project = 'No project'
  area = re.findall(re_digit, div.find('i', {'class': 'blk'}).text.strip())
  print today,","+"http:/"+ (a['href'] if a else '')+",", (a.string if a else '')+ "," +",".join(re.findall("'([a-zA-Z0-9,\s]*)'", (a['onclick'] if a else '')))+","+ ", ".join([project] + area),","+pdates

rawDescription: Getting repeated data - screenscraping using bs4


code:
import numpy as np
import matplotlib.pyplot as plt
xs, ys = np.loadtxt('/tmp/example.txt', delimiter=',').T
print xs
print ys
plt.bar(xs, ys)
plt.show()

rawDescription: plotting histogram using matplotlib in python

description: Plotting histogram using matplotlib in python


code:
for dictionary in data:
    for key in dictionary:
        print dictionary[key]

rawDescription: accesing data in a dictionary python

description: Accesing data in a dictionary python


code:
import datetime
date = datetime.datetime.strptime("111609", "%m%d%y")
print date.strftime("%m/%d/%Y")

rawDescription: Convert date Python

description: Convert date Python


code:
from models import *
from piston.handler import BaseHandler
from django.http import Http404

class ZipCodeHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request, zip_code):
        try:
            points = DeliveryPoint.objects.filter(zip_code=zip_code).order_by("name")
            dps = []
            for p in points:
                name = p.name if (len(p.name)<=16) else p.name[:16]+"..."
                dps.append({'name': name, 'zone': p.zone, 'price': p.price})
            return {'length':len(dps), 'dps':dps}
        except Exception, e:
            return {'length':0, "error":e}

rawDescription: Piston customize response representation


code: print string.replace("\\n", "\n")

rawDescription: Passing newline within string into a python script from the command line


code:
urlstring = "members_booking.php?operation=member_day&course_id=1&d_date=%s-%s-%s&comp=1"

year = raw_input("Enter the YEAR: ")
month = raw_input("Enter the MONTH: ")
day = raw_input("Enter the DAY: ")

print urlstring % (year, month, day)

rawDescription: Editing the URL with user data Python Mechanize


rawDescription: Escaping characters in python just once (single backlash)


code:
'"Isn\'t," she said.'
print '"Isn\'t," she said.'

rawDescription: Escape quote in Python

description: Escape quote in Python


code:
import ProtoText
print person_obj['name']       # print out the person_obj.name
person_obj['name'] = 'David'   # set the attribute 'name' to 'David'
# again set the attribute 'name' to 'David' but in batch mode
person_obj.update({'name': 'David'})
print ('name' in person_obj)  # print whether the 'name' attribute is set in person_obj
# the 'in' operator is better than the google implementation HasField function
# in the sense that it won't raise Exception even if the field is not defined

rawDescription: creating a python dictionary like object from protocol buffers for use in pandas

description: Creating a python dictionary like object from protocol buffers for use in pandas


code:
def create_table():
    curname = None
    for amount in xrange(2500):
        name = best(amount)
        if name != curname:
            if curname is not None:
                print "%d-%d %s" % (minamt, amount-1, curname)
            curname = name
            minamt = amount
    print "%d-%d %s" % (minamt, amount-1, curname)

rawDescription: Create a table from a function's return values


code:
J = multiprocessing.Queue()
R = multiprocessing.Queue()
S = multiprocessing.Semaphore(NUMWORKERS)

def intermediate_worker(jobs, results, sem):
    with sem: #context manager handles incrementing/decrementing the semaphore
        while True:
            task = jobs.get()
            if task is None:
                jobs.put(None)
                break
            print 'working', task
            results.put(task)

def collector(result_queue, sem) :
    total = 0
    while sem.get_value() < NUMWORKERS or not result_queue.empty():
        result = result_queue.get()
        total += result
        print 'collection', total

[multiprocessing.Process(target=intermediate_worker, args=(J,R,S)).start()
 for i in xrange(NUMWORKERS)]

multiprocessing.Process(target=collector, args=(R,S)).start()

rawDescription: Tell the consumer of multiple producers that there are no more results


code:
import re

data_file = open("data.txt", 'r')
data = data_file.read()

search = re.search("DATA\s\=\s((.*\d.+\,?\n)+)", data)
print search.group(1)

rawDescription: How to copy lines from one file and write them in other file using python?

description: Copy lines from one file and write them in other file using python


code:
import Tkinter as Tk
from PIL import Image, ImageTk

root = Tk.Tk()
root.geometry("600x360+100+50")
canvas = Tk.Canvas(root)
canvas.grid(sticky=Tk.N+Tk.E+Tk.S+Tk.W)

width = 300
height = 180

image_file = Image.open("above_image.png")
PIL_image = ImageTk.PhotoImage(image_file.convert("RGBA"))
canvas_img = canvas.create_image(width,height,image=PIL_image)

def callback(event):
    """ This function is called whenever a event occurs """
    if width-9 < event.x < width+9 and height-24 < event.y < height-12:
        print "clicked up arrow!"
    elif width-9 < event.x < width+9 and height+12 < event.y < height+24:
        print "clicked down arrow!"
    elif width+12 < event.x < width+25 and height-8 < event.y < height+8:
        print "clicked right arrow!"
    elif width-25 < event.x < width-12 and height-8 < event.y < height+8:
        print "clicked left arrow!"

canvas.bind("<Button-1>", callback)
root.mainloop()

rawDescription: Using GoogleMaps-like buttons in tkinter


code:
print result.pvalue
print result.conf_int()
print result.rsquared

rawDescription: How to save in a variable significance of a coefficient estimated by OLS using statsmodels in python?

description: Save in a variable significance of a coefficient estimated by OLS using statsmodels in python


code:
xml = """<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-02-25T23:03:41">
 <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
  <BookData book_id="somebook" isbn="0123456789">
   <Title>Interesting Book</Title>
   <TitleLong>Interesting Book: Read it or else..</TitleLong>
   <AuthorsText>John Doe</AuthorsText>
   <PublisherText>Acme Publishing</PublisherText>
  </BookData>
 </BookList>
</ISBNdb>"""
for book in tree.iterfind('BookList/BookData'):
    print 'isbn:', book.attrib['isbn']
    for child in book.getchildren():
            print '%s :' % child.tag, child.text

rawDescription: Best way to parse xml in Appengine with Python

description: Parse xml in Appengine with Python


code:
import imaplib

conn= imaplib.IMAP4_SSL('imap.googlemail.com')
conn.login('your_account@gmail.com', 'your_password')
status, dummy= conn.select('INBOX')
if status != 'OK':
    raise RuntimeError, "Failed to select inbox"

code, data= conn.search(None, CRITERION)
if code == 'OK':
    msgid_list= data[0].split()
else:
    raise RuntimeError, "Failed to get message IDs"

for msgid in msgid_list:
    code, data= conn.fetch(msgid, '(RFC822)')

    if code == 'OK':
        PROCESS(data)
    else:
        raise RuntimeError, "could not retrieve msgid %r" % msgid

conn.close()
conn.logout()

rawDescription: having problems with poplib

description: Having problems with poplib


code:
from Bio import SeqIO
with open("example.fasta", "rU") as handle:
    print list(SeqIO.parse(handle, "fasta"))

rawDescription: making table from fasta sequences, python

description: Making table from fasta sequences, python


code:
data = obj.recv(1024)
if not data:
    return
print data

rawDescription: Server prints endless newlines when client disconnects


code:
def my_dec(fn):
    print dir(fn) # Has "func_code" and "func_name"
    return fn

class A(object):
    @my_dec
    def test(self):
        pass

print dir(A.test) # Has "im_class" and "im_self"

rawDescription: Is it possible to determine which class a function is a method of?


code:
from itertools import chain

d1 = {10: [123, 244, 222], 13: [109, 125]}
d2 = {125: [[120, 244, 222], [177, 134, 111], [123, 11, 344]], 123: [[125, 109, 180], [123, 156, 290]]}

# Flatten out the list using a list comprehension
key_values = [values for list in d1.values() for values in list]

new_dict = dict()
for d2_list in chain.from_iterable(d2.itervalues()):
    key_to_check = d2_list[0]
    if key_to_check in key_values:
        if key_to_check in new_dict:
            new_dict[key_to_check].append(d2_list)
        else:
            new_dict[key_to_check] = [d2_list]
print new_dict

rawDescription: How to efficiently compare the items in a list within a dictionary to the 0th item within another list within a second dictionary

description: Efficiently compare the items in a list within a dictionary to the 0th item within another list within a second dictionary


code:
#!/bin/python

import urllib2, sys, logging, time, errno, unittest

# Variables
s = time.strftime('%Y%m%d%H%M%S')
f = open("list.txt",'r')

# Create file text file for importing into database
class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("assets_"+s+".txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

# Start capture of screen output for database file
sys.stdout = Logger()
print "UID, ASSET, STATUS, REDIRECT, DATE/TIME"

# Loop to capture URL status and redirect information
for assets in f.readlines():
    url = assets.split()
    if len(url) > 1:
        try:
            http = 'http://' + url[1]
            http_connection = urllib2.urlopen(http, timeout=5)
        except IOError, e:
            if e.errno == None:
                try:
                    www = 'http://www.' + url[1]
                    http_connection = urllib2.urlopen(www, timeout=5)
                except IOError, e:
                    print url[0], ",", url[1].rstrip("\n"), ",", "", ",", e.errno, ",", (s)
                else:
                    print url[0], ",", url[1].rstrip("\n"), ",", http_connection.getcode(), ",", http_connection.geturl(), ",", (s)
        else:
            print url[0], ",", url[1].rstrip("\n"), ",", http_connection.getcode(), ",", http_connection.geturl(), ",", (s)

rawDescription: Python conflicting [Errno] messages

description: Python conflicting [Errno] messages


code:
server=socket.socket()
server.settimeout(0.003)
host="192.168.0.1"
port=12321
try:
    server.connect(host,port)
    server.send('data')
except:
    print"server vot connected"

rawDescription: How to assign minimum time wait to connect socket

description: Assign minimum time wait to connect socket


code:
import urllib
import lxml.html

page = urllib.urlopen('http://www.toponymic-dictionary.in.ua/index.php?option=com_content&view=section&layout=blog&id=1&Itemid=2')

# use "page" as a file-like object
xmldata = lxml.html.parse(page).getroot()

ptexts = xmldata.xpath('//p[@class="MsoNormal"]//text()')
joined_text = u'\n'.join(ptexts)

print joined_text.encode('cp1251')

rawDescription: xpath for parsing in Python

description: Xpath for parsing in Python


code:
import sys
import re

x = sys.argv[1]
y = sys.argv[2]

f = open("formula.txt" ,'r')
line = f.read()
match1 = re.search(x + r"\s*=\s*'([^']*)'" , line, re.M|re.I).group(1)
match2 = re.search(y + r"\s*=\s*'([^']*)'" , line, re.M|re.I).group(1)
f.close()
print match1
print match2

rawDescription: can anybody help me to correct this "argv" program?

description: Can anybody help me to correct this "argv" program


code:
class Calculator():
    def sumatory(self):
        return float(operator1) + float(operator2)
    def substract(self):
        return float(operator1) - float(operator2)

if __name__ == '__main__':
    import sys
    operator1= sys.argv[1]
    operation= sys.argv[2]
    operator2= sys.argv[3]
    try:
        operator1 = float(sys.argv[1])
        operator2 = float(sys.argv[3])
    except ValueError:
        sys.exit("Error: Non numerical Parameters")

    if operation == "sum":
        print Calculator().sumatory()
    elif operation == "substract":
         print Calculator().substract()
    else:
        print "Error, operation not supported."

rawDescription: Duplicated results when importing methods from a class of another py file


code:
f = open('test')
json_obj = ijson.items(f,'').next() # '' loads everything as only one object.
for (key, value) in json_obj.items():
    print key + " -> " + str(value)

rawDescription: No access to top level elements with ijson?


code:
buffer = "path/filename.zip"  # zip filename to write (or file-like object)
name = "folder/data.txt"      # name of file inside zip
bytes = "blah blah blah"      # contents of file inside zip

zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED)
info = zipfile.ZipInfo(name)
info.external_attr = 0777 << 16L # give full access to included file
zip.writestr(info, bytes)
zip.close()

rawDescription: How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module?

description: Set permissions (attributes) on a file in a ZIP file using Python's zipfile module


code:
class aiRespond:
    def generateResponse( self, external_input ):
        return external_input

    def giveResponse( self, external_input ):
        return self.generateResponse( external_input) #remove self as parameter,add return

aiResponder = aiRespond()
retrieve_ai_response = aiResponder.giveResponse #remove "()"

while 1:
    external_input = raw_input("> ")
    print retrieve_ai_response(external_input)

rawDescription: TypeError referencing class method

description: TypeError referencing class method


rawDescription: Format string with regex in args in Python


code:
import itertools

places = [
    "Pp",
    "Aa@",
    "Ss5",
    "Ss5",
    "Ww",
    "Oo0",
    "Rr",
    "Dd",
]

for letters in itertools.product(*places):
    print "".join(letters)

rawDescription: Find all combinations (upper and lower and symbols) of a word in python


code:
import ast

def parse(s):
  tree = ast.parse(s)
  print ast.dump(tree)

parse('f(a,b)')

rawDescription: Getting Function name and arguments

description: Getting Function name and arguments


rawDescription: how to get the shortest matching with python (complex non-greedy pattern)

description: Get the shortest matching with python (complex non-greedy pattern)


code:
while p < num_drivers:
    f = open("Driver"+str(p)+".txt","w")
    for x in driver_routes[p]:
        to_write = ','.join(map(str, lat_long[x]))
        print to_write   # for testing
        f.write(to_write)
        f.write("\n")
    f.close()
    p += 1

rawDescription: Writing sublists in a list of lists to separate text files


code:
def describe_symtable(st, recursive=True, indent=0):
    def print_d(s, *args):
            prefix = ' ' *indent
            print prefix + s + ' ' + ' '.join(args)

    print_d('Symtable: type=%s, id=%s, name=%s' % (
            st.get_type(), st.get_id(), st.get_name()))
    print_d('  nested:', str(st.is_nested()))
    print_d('  has children:', str(st.has_children()))
    print_d('  identifiers:', str(list(st.get_identifiers())))

    if recursive:
            for child_st in st.get_children():
                    describe_symtable(child_st, recursive, indent + 5)

rawDescription: Symbol Table in Python

description: Symbol Table in Python


code:
p = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE, shell=True)
print p.communicate()[0]

rawDescription: Calling a python script that returns a value from perl


code:
from datetime import datetime
x = datetime.now()
print x
x.date()

print x.date()

rawDescription: Google app engine datastore datetime to date in Python?


code:
def a():
    treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all'])
    print "\n"
    print "Your machine type is ",platform.machine()
    print "Compiling using default compiler\n"
    print treeinfo

def b():
    subprocess.call('helloworld.exe')
    print"Build success"

def c():
    log64 =  subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"])
    if arch64 in log64:
        print "Architecture of the compiled file is 64-bit "
    elif arch32 in log64:
        print "Architecture of the compiled file is 32-bit"

def try_these(funs, catch):
    for fun in funs:
        try:
            fun()
        except catch:
            print 'ERROR!'

try_these([a, b, c], catch=(IOError, OSError))

rawDescription: reducing the number of try,except in python

description: Reducing the number of try,except in python


rawDescription: Python Regex: Group Backref

description: Python Regex: Group Backref


rawDescription: High-performance way to eliminate semi-duplicate items from a list


code:
import re

import nltk
from nltk.grammar import Nonterminal, Production, ContextFreeGrammar

grammar = nltk.parse_cfg ("""
S -> TEXT
TEXT -> WORD | WORD TEXT | NUMBER | NUMBER TEXT
""")

productions = grammar.productions()

def literal_production(key, rhs):
    """ Return a production <key> -> n

    :param key: symbol for lhs:
    :param rhs: string literal:
    """
    lhs = Nonterminal(key)
    return Production(lhs, [rhs])

def parse(text):
    """ Parse some text.
"""

    # extract new words and numbers
    words = set([match.group(0) for match in re.finditer(r"[a-zA-Z]+", text)])
    numbers = set([match.group(0) for match in re.finditer(r"\d+", text)])

    # Make a local copy of productions
    lproductions = list(productions)

    # Add a production for every words and number
    lproductions.extend([literal_production("WORD", word) for word in words])
    lproductions.extend([literal_production("NUMBER", number) for number in numbers])

    # Make a local copy of the grammar with extra productions
    lgrammar = ContextFreeGrammar(grammar.start(), lproductions)

    # Load grammar into a parser
    parser = nltk.RecursiveDescentParser(lgrammar)

    tokens = text.split()

    return parser.parse(tokens)

print parse("foo hello world 123 foo")

rawDescription: Match alphanumeric string in nltk grammar


code: print python( Matrix([[x**2,exp(y) + x]]).jacobian([x, y]) )

rawDescription: Generate python code from a sympy expression?


code: print pd.concat(l1).to_csv('filename')

rawDescription: How to save a list of dataframes to csv

description: Save a list of dataframes to csv


code:
from cStringIO import StringIO

class Page(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        out = StringIO()
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        if self.path == '/':
            out.write("Run\n")
        elif self.path == '/run':
            self.proc = MyProc()
            print 'Starting..'
            self.proc.start()
            print 'After start.'
            out.write("
Process started
\n")
        text = out.getvalue()
        self.send_header("Content-Length", str(len(text)))
        self.end_headers()
        self.wfile.write(text)

rawDescription: Why does a background task block the response in SimpleHTTPServer?


code:
import numpy as np

# Demo data.
x = np.array([[1,2,3],
              [2,0,0],
              [1,2,3],
              [3,2,2],
              [2,0,0],
              [2,1,2],
              [3,2,1],
              [2,0,0]])

# View each row as a structure, with field names 'a', 'b' and 'c'.
dt = np.dtype([('a', x.dtype), ('b', x.dtype), ('c', x.dtype)])
y = x.view(dtype=dt).squeeze()

# Now np.unique can be used.  See the `unique` docstring for
# a description of the options.  You might not need `idx` or `inv`.
u, idx, inv = np.unique(y, return_index=True, return_inverse=True)

print "Unique vectors"
print u

rawDescription: How to find unique vectors of a 2d array over a particular axis in a vectorized manner?

description: Find unique vectors of a 2d array over a particular axis in a vectorized manner


code:
import win32com.client

conn = win32com.client.Dispatch('ADODB.Connection')
db = 'C:\\Profit\\profit_a\\ARMM'
dsn = 'Provider=VFPOLEDB.1;Data Source=%s' % db
conn.Open(dsn)

cmd = win32com.client.Dispatch('ADODB.Command')
cmd.ActiveConnection = conn
cmd.CommandText = "Select * from factura, reng_fac where factura.fact_num = reng_fac.fact_num AND factura.fact_num = 6099;"

rs = cmd.Execute() # This returns a tuple: (<RecordSet>, number_of_records)
total = rs[1]
rs = rs[0]

while total:
    for x in xrange(rs.Fields.Count):
        print '%s --> %s' % (rs.Fields.item(x).Name, rs.Fields.item(x).Value)
        rs.MoveNext()
        total = total - 1

rawDescription: Reading DBF files with pyodbc

description: Reading DBF files with pyodbc


code:
with open("txt.txt") as searchfile:
    found = False
    while not found:
        b=str(raw_input('please enter a book '))
        if b == '':
            break  # allow the search-loop to quit on no input
        for line in searchfile:
            if b in line:
                print line
                found = True
                break
        else:
            print 'Please try again'
            searchfile.seek(0)  # reset file to the beginning for next search

rawDescription: How to search txt file from python using while loop

description: Search txt file from python using while loop


code:
import io
import re

regex_oti = re.compile(ur'^.*\b(ότι|ό,τι)\b.*$')
regex_tis = re.compile(ur'^.*\b(της|τις)\b.*$')
regex_ton = re.compile(ur'^.*\b(τον|των)\b.*$')

with io.open('source.txt', 'r', encoding='utf8') as source, \
     io.open('results_oti.txt', 'w', encoding='utf8') as oti, \
     io.open('results_tis.txt', 'w', encoding='utf8') as tis, \
     io.open('results_ton.txt', 'w', encoding='utf8') as ton:

    for line in source:
        if regex_oti.match(line):
            oti.write(line)
        if regex_tis.match(line):
            tis.write(line)
        if regex_ton.match(line):
            ton.write(line)

rawDescription: Python Regex for greek words

description: Python Regex for greek words


code:
class list1(list):
    """One-based version of list."""

    def _zerobased(self, i):
        if type(i) is slice:
            return slice(self._zerobased(i.start),
                         self._zerobased(i.stop), i.step)
        else:
            if i is None or i < 0:
                return i
            elif not i:
                raise IndexError("element 0 does not exist in 1-based list")
            return i - 1

    def __getitem__(self, i):
        return list.__getitem__(self, self._zerobased(i))

    def __setitem__(self, i, value):
        list.__setitem__(self, self._zerobased(i), value)

    def __delitem__(self, i):
        list.__delitem__(self, self._zerobased(i))

    def __getslice__(self, i, j):
        print i,j
        return list.__getslice__(self, self._zerobased(i or 1),
                                 self._zerobased(j))

    def __setslice__(self, i, j, value):
        list.__setslice__(self, self._zerobased(i or 1),
                          self._zerobased(j), value)

    def index(self, value, start=1, stop=-1):
        return list.index(self, value, self._zerobased(start),
                          self._zerobased(stop)) + 1

    def pop(self, i):
        return list.pop(self, self._zerobased(i))

rawDescription: Custom wrapper for indexing python list starting at 1


rawDescription: Maximum precision on very large numbers

description: Maximum precision on very large numbers


code:
a = 'abubakar,rooney,0178222123,student abubakar,rooneyzzz,0178222164'
b = a.split(',')
print b

rawDescription: import csv file into mysql database using django web application

description: Import csv file into mysql database using django web application


code:
input_list = [['name','score1','score2''score3''score4'],
              ['Mike','5','1','6','2'],
              ['Mike','1','1','1','1'],
              ['Mike','3','0','3','0'],
              ['jose','0','1','2','3'],
              ['jose','2','3','4','5'],
              ['lisa','4','4','4','4']]
print input_list

# Remove the first element
input_list = input_list[1:]

# Initialize an empty output list
output_list = []

# Iterate through each entry in the input
for val in input_list:
    # Determine if key is already in output list
    for ent in output_list:
        if ent[0] == val[0]:
            # The value is already in the output list (so merge them)
            for i in range(1, len(ent)):
                # We convert to int and back to str
                # This could be done elsewhere (or not at all...)
                ent[i] = str(int(ent[i]) + int(val[i]))
            break
    else:
        # The value wasn't in the output list (so add it)
        # This is a useful feature of the for loop, the following
        # is only executed if the break command wasn't reached above
        output_list.append(val)

#print input_list
print output_list

rawDescription: python add specific lists within a list

description: Python add specific lists within a list


code:
from bs4 import BeautifulSoup
import requests

html = requests.get("http://www.imdb.com/title/tt1663202/reviews?ref_=tt_ov_rt")
soup = BeautifulSoup(html.text)

for div in soup.find_all('div'):
    h2 = div.find('h2')

    if h2:
        p = div.find_next_sibling('p')

        if p:
            print h2.text
            print p.text

rawDescription: How to extract text using BeautifulSoup when same tag exist which are not useful

description: Extract text using BeautifulSoup when same tag exist which are not useful


code:
for f, s, t in W:
    print b[f], b[s], b[t]

rawDescription: How does indexing a list with a tuple work?


code:
from Tkinter import *
import time

class App:
    def __init__(self, master):
        w = Label(master, text="1. Anxiety, nervousness, worry or fear")
        w.pack()

        v = IntVar()
        Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
        Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
        Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
        Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w")

        self.button = Button(master, text="BACK", fg="red", command=self.button6)
        self.button.pack(side=BOTTOM)
        self.button = Button(master, text="NEXT", fg="red", command=self.button5)
        self.button.pack(side=BOTTOM)

    def button6(self):
        print "Sam is awesome!GAJONGA"

    def button5(self):
        print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY"

master = Tk()
app = App(master)
master.mainloop()

rawDescription: Add radio button to Tkinter

description: Add radio button to Tkinter


code:
class Scope(object):
    def __getitem__(self, key):
        return 'call for %s' % key

print final.substitute(Scope())

rawDescription: Python templates and function execution

description: Python templates and function execution


code:
import requests

def user_input():
    id1 = raw_input("Enter the unique id:")
    user = raw_input("Enter your username:")
    password = raw_input("Enter corresponding password:")

    try:
        r = requests.get('http://test.com/request.pl?id='+ id1 + user + password)
        if r.status_code == 404:
            print "No such page exists.Please check the ID and try again"
        ## Ask for input again
            user_input()
        else:
            print r.text
    except requests.ConnectionError:
        print "Server is refusing connections.Please try after sometime"
        sys.exit(1)
user_input()

rawDescription: Python- Pass control flow back to top of the script


code:
with open("/home/xmp/distribution/sites.conf", "rt") as txtfile:
    for line in txtfile:
        fields = line.split(':')
        print fields[0]

rawDescription: How can python recognize : in linux command using os.popen?


code:
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Low for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm

import sys
from _winreg import *

# tweak as necessary

version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
    except EnvironmentError:
        try:
            reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return

    if (QueryValue(reg, installkey) == installpath and
            QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return

    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

if __name__ == "__main__":
    RegisterPy()

rawDescription: mysql for python 2. 7 says Python v2.7 not found

description: Mysql for python 2. 7 says Python v2.7 not found


code:
import sys
print sys.executable

rawDescription: How to get the python.exe location programmatically?

description: Get the python.exe location programmatically


code:
import os
import scriptcontext
import rhinoscriptsyntax as rs

print "//export run started/////////////"

# this function via mcneel/rhinoscriptsyntax
#https://github.com/mcneel/rhinoscriptsyntax/blob/master/Scripts/rhinoscript/layer.py
def layerNames(sort=False):
    rc = []
    for layer in scriptcontext.doc.Layers:
        if not layer.IsDeleted: rc.append(layer.FullPath)
    if sort: rc.sort()
    return rc

def GetDAESettings():
    e_str = ""
    return e_str

def GetOBJSettings():
    e_str = "_Geometry=_Mesh "
    e_str+= "_EndOfLine=CRLF "
    e_str+= "_ExportRhinoObjectNames=_ExportObjectsAsOBJGroups "
    e_str+= "_ExportMeshTextureCoordinates=_Yes "
    e_str+= "_ExportMeshVertexNormals=_No "
    e_str+= "_CreateNGons=_No "
    e_str+= "_ExportMaterialDefinitions=_No "
    e_str+= "_YUp=_No "
    e_str+= "_WrapLongLines=Yes "
    e_str+= "_VertexWelding=_Welded "
    e_str+= "_WritePrecision=4 "
    e_str+= "_Enter "

    e_str+= "_DetailedOptions "
    e_str+= "_JaggedSeams=_No "
    e_str+= "_PackTextures=_No "
    e_str+= "_Refine=_Yes "
    e_str+= "_SimplePlane=_No "

    e_str+= "_AdvancedOptions "
    e_str+= "_Angle=50 "
    e_str+= "_AspectRatio=0 "
    e_str+= "_Distance=0.0"
    e_str+= "_Density=0 "
    e_str+= "_Density=0.45 "
    e_str+= "_Grid=0 "
    e_str+= "_MaxEdgeLength=0 "
    e_str+= "_MinEdgeLength=0.0001 "

    e_str+= "_Enter _Enter"

    return e_str

def GetSTLSettings():
    eStr = "_ExportFileAs=_Binary "
    eStr+= "_ExportUnfinishedObjects=_Yes "
    eStr+= "_UseSimpleDialog=_No "
    eStr+= "_UseSimpleParameters=_No "
    eStr+= "_Enter _DetailedOptions "
    eStr+= "_JaggedSeams=_No "
    eStr+= "_PackTextures=_No "
    eStr+= "_Refine=_Yes "
    eStr+= "_SimplePlane=_No "
    eStr+= "_AdvancedOptions "
    eStr+= "_Angle=15 "
    eStr+= "_AspectRatio=0 "
    eStr+= "_Distance=0.01 "
    eStr+= "_Grid=16 "
    eStr+= "_MaxEdgeLength=0 "
    eStr+= "_MinEdgeLength=0.0001 "
    eStr+= "_Enter _Enter"
    return eStr

settingsList = {
    'GetDAESettings': GetDAESettings,
    'GetOBJSettings': GetOBJSettings,
    'GetSTLSettings': GetSTLSettings
}

fileName = rs.DocumentName()
filePath = rs.DocumentPath().rstrip(fileName)

arrLayers = layerNames(False)

def initExportByLayer(fileType="obj", visibleonly=False, byObject=False):
    for layerName in arrLayers:
        layer = scriptcontext.doc.Layers.FindByFullPath(layerName, True)
        if layer >= 0:
            layer = scriptcontext.doc.Layers[layer]
            save = True;
            if visibleonly:
                if not layer.IsVisible:
                    save = False
            if  rs.IsLayerEmpty(layerName):
                save = False
            if save:
                cutName = layerName.split("::")
                cutName = cutName[len(cutName)-1]
                objs = scriptcontext.doc.Objects.FindByLayer(cutName)
                if len(objs) > 0:
                    if byObject:
                        i=0
                        for obj in objs:
                            i= i+1
                            saveObjectsToFile(cutName+"_"+str(i), [obj], fileType)
                    else:
                        saveObjectsToFile(cutName, objs, fileType)

def saveObjectsToFile(name, objs, fileType):
    rs.EnableRedraw(False)
    if len(objs) > 0:
        settings = settingsList["Get"+fileType.upper()+"Settings"]()
        rs.UnselectAllObjects()
        for obj in objs:
            obj.Select(True)
        name = "".join(name.split(" "))
        command = '-_Export "{}{}{}" {}'.format(filePath, name, "."+fileType.lower(), settings)
        rs.Command(command, True)
        rs.EnableRedraw(True)

initExportByLayer("obj",True, False)
initExportByLayer("dae",True, False)
initExportByLayer("stl",True, False)

print "//export run ended/////////////"

rawDescription: Export layers as obj from Rhino3d

description: Export layers as obj from Rhino3d


code:
import json
json_data=open('INPUT_FLIX_DB.json')
server_details = json.load(json_data)
json_data.close()
for id in server_details:
    type=[]
    if "content-type" in server_details[id]:
         type.append("content-type")
    if "x-frame-options" in server_details[id]:
         type.append("x-frame-options")
    print id,type

rawDescription: How to Parse a Json file and search for specific set of key words?


code:
# two random arrays as an example
X = numpy.random.random(100)
Y = numpy.random.random(100)
ids = numpy.nonzero(X > Y)[0]
print ids
print ids + 2

rawDescription: Shifting Indexes in Python (Comparison to R)


code:
s = '12345'

chars = [c for c in s]

for i in range(len(s) - 1):
    print '%d:' % i,
    for el in chars[:i] + [chars[i] + chars[i + 1]] + chars[i + 2:]:
        print el,
    print

rawDescription: nested looping in python

description: Nested looping in python


code:
import pandas as pd
print [d for d in pd.date_range(start="2015-11-02", end="2015-12-14") if d.weekday() in [1,2]]

rawDescription: All tuesdays and wednesdays in a date range: is there a more pythonic way?


code:
class A:
  def __str__(self):
    return "I am an instance of A"

a_inst = A()
print a_inst

rawDescription: Class inheritance and the print/_str_() methods


code:
# list all values for a key
try:
    count = 0
    while 1:
        name, value, type = _winreg.EnumValue(t, count)
        print repr(name),
        count = count + 1
except WindowsError:
    pass

rawDescription: Loop through values or registry key.. _winreg Python


code: print sorted(data,key=lambda x: x[4])

rawDescription: Is there any way to sort nested lists without using operator.itemgetter?


code:
def my_function(a):
    a = iter(a)
    while True:
        yield 10 * next(a)
        yield next(a)
        yield "foo" + next(a)

a = [2, "foo1", "bar1", 5, "foo3", "bar2", 3, "foo2", "bar3"]
print list(my_function(a))
#prints [20, 'foo1', 'foobar1', 50, 'foo3', 'foobar2', 30, 'foo2', 'foobar3']

rawDescription: How to do something to an iterator, then something else to the next iterator in python

description: Do something to an iterator, then something else to the next iterator in python


code:
import imp
import sys

def my_import(module_name, globals):
    if module_name in sys.modules:
        return sys.modules[module_name]
    module = imp.new_module(module_name)
    vars(module).update(globals)
    f, module.__file__, options = imp.find_module(module_name)
    exec f.read() in vars(module)
    f.close()
    sys.modules[module_name] = module
    return module

rawDescription: Injecting Locals into Dynamically Loaded Modules Before Execution


code:
from ftplib import FTP

ftp = FTP('ftp.hostname.com')
ftp.login('login', 'passwrd')
ftp.cwd('www') # change into 'www' directory

if 'foo' in ftp.nlst() : #check if 'foo' exist inside 'www'
    print 'YES'
    ftp.cwd('foo')  # change into "foo" directory
    ftp.retrlines('LIST') #list directory contents

else :
    print 'NO'
    ftp.mkd('foo') #Create a new directory called foo on the server.
    ftp.cwd('foo') # change into 'foo' directory
    ftp.retrlines('LIST') #list subdirectory contents

ftp.close() #close connection

rawDescription: How to check if folder exist inside a directory if not create it using python

description: Check if folder exist inside a directory if not create it using python


rawDescription: List of Lists from a FreqDist output

description: List of Lists from a FreqDist output


code:
from multiprocessing import Pool, cpu_count

# ...
all_files = ["file%d" % i for i in range(5)]

def process_file(file_name):
    # process file
    return "finished file %s" % file_name

pool = Pool(cpu_count())

# this is a blocking call - when it's done, all files have been processed
results = pool.map(process_file, all_files)

# no more tasks can go in the pool
pool.close()

# wait for all workers to complete their task (though we used a blocking call...)
pool.join()

# ['finished file file0', 'finished file file1',  ... , 'finished file file4']
print results

rawDescription: python, subprocess: launch new process when one (in a group) has terminated

description: Python, subprocess: launch new process when one (in a group) has terminated


code:
import collections
org_month_dict = collections.defaultdict(set)

# put the data into a simple, indexed data structure
for index, row in data.iterrows():
    org_month_dict[row['org'], row['month']].add(row['person'])

orgs = set(data.org)
months = sorted(set(data.months))
for org in orgs:
    for mindex in range(len(months)-1):
        m1 = months[mindex]
        m2 = months[mindex+1]
        print org_month_dict[org, m2] & org_month_dict[org, m1] # persons in common between month 1 and 2

rawDescription: How should I structure and access a table of data so that I can compare subsets easily in Python 3.5?


code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.core.window import Window

class MyApp(App):
    def __init__(self, **kwargs):
        super(MyApp, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down = self._on_keyboard_down)
        self._keyboard.bind(on_key_up = self._on_keyboard_up)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down = self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, *args):
        print 'down', args

    def _on_keyboard_up(self, *args):
        print 'up', args

    def build(self):
        self.moveable = Scatter()
        self.moveable.add_widget( Label(text = 'Hello moving world!') )
        return self.moveable

if __name__ == '__main__':
    MyApp().run()

rawDescription: How to handle several keys pressed at the same time in Kivy?

description: Handle several keys pressed at the same time in Kivy


code:
spreadsheet_key = document.GetId().split("%3A")[1]
print "Key = %s" % spreadsheet_key

#example of using this key
w = client.AddWorksheet("Sheet 42", 5, 5, spreadsheet_key)

rawDescription: Create new spreadsheet (Google API / Python)


code:
from nltk.tree import *

np_trees = []

def traverse(t):
    try:
        t.label()
    except AttributeError:
        return

    if t.label() == "VBZ":
        current = t
        while current.parent() is not None:

            while current.left_sibling() is not None:

                if current.left_sibling().label() == "NP":
                    np_trees.append(current.left_sibling())

                current = current.left_sibling()

            current = current.parent()

    for child in t:
        traverse(child)

tree = ParentedTree.fromstring("(S (NP (NNP)) (VP (VBZ) (NP (NNP))))")
traverse(tree)
print np_trees # [ParentedTree('NP', [ParentedTree('NNP', [])])]

rawDescription: How to properly navigate an NLTK parse tree?

description: Properly navigate an NLTK parse tree


code:
my_list = []
with open('path/to/my/file.txt') as f:
    for line in f:
        words = line.split()
        my_list.append(words[1] + words[3])
print mylist

rawDescription: How to take floats from a txt to a Python list as strings

description: Take floats from a txt to a Python list as strings


code:
#The threading module allows you to subclass it's thread class to create
#and run a thread, in this case we will be starting SimpleUI in a seperate thread

import threading
from Tkinter import *

def printfunction():
    print "ButtonPress"

class NewClass:
    def __init__(self):
        self.ui = SimpleUI()
        self.ui.add_frame("frame1")
        self.ui.add_button("button1","frame1")
        self.ui.register_function("button1",printfunction)
        self.ui.start()
        #self.ui = Threader().start()
    def PrintSuccess(self):
        print "Success!"

class SimpleUI:
    def __init__(self):
        self.root = Tk()
        self.frames = {}
        self.buttons = {}
    def start(gui):
        class Threader(threading.Thread):
            def run(self):
                gui.root.mainloop()
        Threader().start()
    def hide(self):
        self.root.withdraw()
        if(raw_input("press enter to show GUI: ")==""):self.show()
    def show(self):
        self.root.update()
        self.root.deiconify()
    def add_frame(self,name):
        tmp = Frame(self.root)
        tmp.pack()
        self.frames[name] = tmp
    def add_button(self,name,frame):
        tmp = Button(self.frames[frame])
        tmp.pack()
        self.buttons[name] = tmp
    def register_function(self,button,function):
        self.buttons[button].config(command=function)

NC = NewClass()
NC.PrintSuccess()

rawDescription: Is there a way to wrap a tkinter GUI in a class that can be created and interacted with from another object? (*without* hanging in mainloop)


code:
from string import ascii_lowercase as alc

str1 = 'aBcdJohnsonzZz'
p1, sep, p2 = str1.partition('Johnson')
if sep:
    str1 = p1.translate(None, alc) + sep + p2.translate(None, alc)
print str1

rawDescription: How to remove substring from a string in python?

description: Remove substring from a string in python


code:
fig = plt.figure(figsize=(20, 222))
plt.subplots_adjust(top=.9, bottom=0.1, wspace=0.2, hspace=0.2)

for i in range(1, 65):

    print 'E', i, ':'

    plt.subplot(64, 2, i)
    plt.ylabel('E%s' % str(i))

    i += 1
    print E[0+j:127+j]
    plt.plot(E[0+j:127+j])
    j += 128
plt.savefig('foo.png', bbox_inches='tight')
plt.show()

rawDescription: Organize subplots using matplotlib

description: Organize subplots using matplotlib


code:
from bs4 import BeautifulSoup

text = """
b'Desired Text 1'
b'Desired Text 2'
b'Desired Text 3'
b'Desired Text 4'
b'Desired Text 5'
b'Desired Text 6'
"""

soup = BeautifulSoup(text, 'html.parser')
print soup.getText()

rawDescription: How can I access specific parts of a string in a txt file with python?

description: Access specific parts of a string in a txt file with python


code:
import os

def contents(filename):
    with file(filename) as f: return f.read()

words = set(["Apple", "Oranges", "Starfruit"])
path = "/opt/webserver/logs"
filenames = os.listdir(path)
print words.difference(
    reduce(lambda x, y: x.union(y), (
        # Note that the following assumes we really want to treat the file
        # as a sequence of words, and not do general substring searching.
        # For example, it will miss "apple" if the file contains "pineapples".
        set(contents(filename).split()).intersection(words)
        for filename in filenames
        # In fact, the .intersection call there is redundant, but might improve
        # performance and will probably save memory at least.
    ))
)

rawDescription: Python - Check Words not in Files in Folder


code:
import program1

# this will show program1's "x" variable
print program1.x

# this will set program1's "x" variable
program1.x = "hello again"

# this will run your program1's "run" function, which
# should create the Tkinter window for you
program1.run()

# if you REALLY want to call "Main" yourself, which will
# probably just break Tkinter since your init function
# already calls Main() once, you can do...
myclass = program1.Class(None)
myclass.Main()

rawDescription: Importation in Python

description: Importation in Python


code:
bars = [i[0]<i[1] for i in comp1]

for b in range(1,len(bars)):
    if bars[b] and bars[b-1]:
        print "yes"

rawDescription: offsetting iterator

description: Offsetting iterator


code:
class NewPiLight(object):
    global_pilnumber = 0 # Since this is on the class definition, it is static

    def __init__(self):
        print "Pilight %s created" % NewPiLight.global_pilnumber
        self.pilnumber = NewPiLight.global_pilnumber # Set the variable for this instance
        NewPiLight.global_pilnumber += 1 # This increments the static variable

rawDescription: Python OOP, using loops to number objects as they are created


code:
import time
from Queue import Empty
from multiprocessing import Process, Queue

def fakeFxn(val):
    if val == 'terminate':
        time.sleep(0.1)
        return 't'
    else:
        time.sleep(1000)
        return 'nt'

def ffWrapper(val, queue):
    retval = fakeFxn(val)
    queue.put(retval)

if __name__ == '__main__':
    q = Queue()
    p = Process(target=ffWrapper, args=('terminate', q))
    p.start()
    rc = ''
    try:
        rc = q.get(timeout=10)
    except Empty:
        print 'timeout'
    p.terminate()
    print rc

rawDescription: How to terminate an ongoing processing abruptly

description: Terminate an ongoing processing abruptly


code:
from M2Crypto import DSA, BIO
dsa = DSA.gen_params(1024)
mem = BIO.MemoryBuffer()
dsa.gen_key()
dsa.save_key_bio(mem, cipher=None)
dsa.save_pub_key_bio(mem)
print mem.getvalue()

rawDescription: Python M2Crypto - generating a DSA key pair and separating public/private components


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

f = open('list.txt', 'r')
for n, name in enumerate(f):
    original_name = 'BusinessCard_%02d_Blue.pdf' % (n + 1)
    new_name = 'BusinessCard_%02d_%s_Blue.pdf' % (
                             n, ''.join(name.title().split()))
    if os.path.isfile(original_name):
        print "Renaming %s to %s" % (original_name, new_name),
        os.rename(original_name, new_name)
        print "OK!"
    else:
        print "File %s not found." % original_name

rawDescription: Batch file renaming – inserting text from a list (in Python or Java)


code:
from collections import deque

def dequefilter(deck, condition):
    for _ in xrange(len(deck)):
        item = deck.popleft()
        if condition(item):
            deck.append(item)

deck = deque((1, 2, 3, 4, 5))
dequefilter(deck, lambda x: x > 2) # or operator.gt(2)
print deck
# deque([3, 4, 5])

rawDescription: How to modify python collections by filtering in-place?

description: Modify python collections by filtering in-place


code:
import os
out='a/b/c/'.replace(os.path.sep, '_')
print out

rawDescription: Python, make filename based on path and replace backslash


code:
from itertools import chain

full_result = {}

for blah in chain.from_iterable(d.itervalues() for d in (API1, API2, API3)):
    for d in blah:
        full_result[d['url']] = {
            'title': d['title'],
            'snippet': d['snippet'],
            'score': ScoredResults[d['url']]
        }

print full_result

rawDescription: Merging different dictionaries in Python


code:
# What to look up (Twitter screen_name:s)
ids = ["john","paul","george","ringo"]

# Create a comma separated string from the previous list
comma_separated_string = ",".join(ids)

# Query twitter with the comma separated list
output = twitter.lookup_user(screen_name=comma_separated_string)

username_list=[]

# Loop through the results (Twitter screen names)
for user in output:
    print user["id_str"]

rawDescription: Using Twython to get user ID's from a list of screen names (Twitter API)


code:
def call_with_each(*arg_tuples):
    def decorate(f):
        @functools.wraps(f)
        def decorator():
            for arg_tuple in arg_tuples:
                f(*arg_tuple)
        return decorator
    return decorate

# useage example:
@call_with_each((3,), (2,)) # note that we pass several singleton tuples
def f(x):
    print x
# calling f() prints "3\n2\n"

rawDescription: Python decorator with parameters, to run the functions multiple times?


code:
for c in re.finditer(r'^(\d+(\.\d+)+)', f, re.MULTILINE):
    print c.group(1)

rawDescription: Why doesn't this regular expression work on python


code:
from itertools import groupby
L = ["Ack","Ashley","book","channel","Charlie","David","Eli","George","Zebra"]
D ={v:k for k,v in dictmap.items() for v in v}
groups = itertools.groupby(L, key=lambda x:D.get(x[0]))

for k,g in groups:
    g=list(g)
    if len(g)>1:
        print g,
    else:
        print g[0],

rawDescription: print a list and group it by a certain mapping

description: Print a list and group it by a certain mapping


code:
from Tkinter import *

rows = ["A few lines", "of text", "for our example"]
def callback(row):
    print "you picked row # %s which has this data: %s" % (row, rows[row])

rows = ["A few lines", "of text", "for our example"]
root = Tk()
t = Text(root)
t.pack()

t.insert(END, '\n'.join(rows))
for i in range(len(rows)):
    line_num = i + 1 # Tkinter text counts from 1, not zero
    tag_name = "tag_%s" % line_num
    t.tag_add(tag_name, "%s.0" % line_num, "%s.end" % line_num)
    t.tag_bind(tag_name, "<Button-1>", lambda e, row=i: callback(row))

root.mainloop()

rawDescription: How to Show DB Query Response in Tkinter

description: Show DB Query Response in Tkinter


code:
def test_print():
    import time
    import sys
    for i in range(5):
        time.sleep(2)
        print i,  time.time()
        sys.stdout.flush()

rawDescription: How to flush the printed statements in IPython

description: Flush the printed statements in IPython


code:
import pexpect as px
import sys

def send_this(child, retcode, COMMAND):
    if retcode == 2:
        child.sendline(COMMAND)
    else:
        raise RuntimeError, "Could not complete login, due to socket error"

def expect_this(child, EXPR, timeout = 10):
    return child.expect([px.TIMEOUT, px.EOF, EXPR], timeout = timeout)

HOST = '192.168.49.49'
CMD1 = '1I'
PROMPT = '\*'     #Note: you might not need the backslash
TIMEOUT = 10

child = px.spawn('telnet %s' % HOST)
retcode = expect_this(child, PROMPT)
send_this(child, retcode, CMD1)
retcode = expect_this(child, PROMPT)
mydata1 = child.before    # mydata has the output from CMD1

rawDescription: Python: Socket: Handling TCP connections over a cell network


code:
def split_with_phrases(sentence, phrase_list):
    words = sentence.split(" ")
    phrases = set(tuple(s.split(" ")) for s in phrase_list)
    print phrases
    max_phrase_length = max(len(p) for p in phrases)

    # Find a phrase within words starting at the specified index.  Return the
    # phrase as a tuple, or None if no phrase starts at that index.
    def find_phrase(start_idx):
        # Iterate backwards, so we'll always find longer phrases before shorter ones.
        # Otherwise, if we have a phrase set like "hello world" and "hello world two",
        # we'll never match the longer phrase because we'll always match the shorter
        # one first.
        for phrase_length in xrange(max_phrase_length, 0, -1):
            test_word = tuple(words[idx:idx+phrase_length])
            if test_word in phrases:
                return test_word
        return None

    skip = 0
    for idx in xrange(len(words)):
        if skip:
            # This word was returned as part of a previous phrase; skip it.
            skip -= 1
            continue

        phrase = find_phrase(idx)
        if phrase is not None:
            skip = len(phrase)
            yield " ".join(phrase)
            continue

        yield words[idx]

print [s for s in split_with_phrases('i would like a blood orange',
    ["blood orange", "loan shark"])]

rawDescription: String splitting issue problem with multiword expressions


code:
comment = soup.find('code').contents[0]
type(comment)
BeautifulSoup(comment).findAll('a', href=re.compile('/hashtag/?'))
for link in BeautifulSoup(comment).findAll('a', href=re.compile('/hashtag/?')):
    print link.text

rawDescription: BeautifulSoup - not reading all links contained in hidden element in soup


code:
f = open('data.txt','r')
lines = [l.strip() for l in f.readlines()]
l = len(lines)
for i in range(l/2):
    print lines[i]+'\t'+ lines[i + l/2]
if l%2: #if it is odd
    print lines[-1]

rawDescription: how to adding new tab in readline python

description: Adding new tab in readline python


code:
#! /usr/bin/python

import numpy as np
import pylab

import os

# RMSE improved
def main():

    # x = 336 data points of simulated wind speed for 7 days * 24 hour * 2 (every half an hour)
    # Imagine at time t, we will get a x_t fvalue or t+48 or a 24 hours later.
    x = load_x()

    # this is a list that will contain 336 data points of our corrected data
    x_sample_predict_list = []

    # z = 336 data points for 7 days * 24 hour * 2 of actual measured wind speed (every half an hour)
    z = load_z()

    # Here is the setup of the scalar kalman filter
    # reference: http://www.swarthmore.edu/NatSci/echeeve1/Ref/Kalman/ScalarKalman.html
    # state transition matrix (we simply have a scalar)
    # what you need to multiply the last time's state to get the newest state
    # we get the x_t+1 = A * x_t, since we get the x_t+1 directly for simulation
    # we will have a = 1
    a = 1.0

    # observation matrix
    # what you need to multiply to the state, convert it to the same form as incoming measurement
    # both state and measurements are wind speed, so set h = 1
    h = 1.0

    Q = 1.0     # expected process noise of predicted Wind Speed
    R = 1.0     # expected measurement noise of Wind Speed

    p_j = Q # process covariance is equal to the initial process covariance estimate

    # Kalman gain is equal to k = hp-_j / (hp-_j + R).  With perfect measurement
    # R = 0, k reduces to k=1/h which is 1
    k = 1.0

    # one week data
    # original R2 = 0.183
    # with delay = 6, R2 = 0.295
    # with delay = 12, R2 = 0.147
    # with delay = 48, R2 = 0.075
    delay = 6

    # Kalman loop
    for t, x_sample in enumerate(x):

        if t <= delay:
            # for the first day of the forecast,
            # we don't have forecast data and measurement
            # from a day before to do correction
            x_sample_predict = x_sample
        else: # t > 48
            # for a priori estimate we take x_sample as is
            # x_sample = x^-_j = a x^-_j_1 + b u_j
            # Inside the NWP (numerical weather prediction,
            # the x_sample should be on x_sample_j-1 (assumption)

            x_sample_predict_prior = a * x_sample

            # we use the measurement from t-delay (ie. could be a day ago)
            # and forecast data from t-delay, to produce a leading residual that can be used to
            # correct the forecast.
            residual = z[t-delay] - h * x_sample_predict_list[t-delay]

            p_j_prior = a**2 * p_j + Q

            k = h * p_j_prior / (h**2 * p_j_prior + R)

            # we update our prediction based on the residual
            x_sample_predict = x_sample_predict_prior + k * residual

            p_j = p_j_prior * (1 - h * k)

            #print k
            #print p_j_prior
            #print p_j
            #raw_input()

        x_sample_predict_list.append(x_sample_predict)

    # initial goodness of fit
    R2_val_initial = calculate_regression(x,z)
    R2_string_initial = "R2 original: {0:10.3f}, ".format(R2_val_initial)
    print R2_string_initial     # R2_val_original = 0.183

    original_RMSE = (((x-z)**2).mean())**0.5
    print "original_RMSE"
    print original_RMSE
    print "\n"

    # final goodness of fit
    R2_val_final = calculate_regression(x_sample_predict_list,z)
    R2_string_final = "R2 final: {0:10.3f}, ".format(R2_val_final)
    print R2_string_final       # R2_val_final = 0.267, which is better

    final_RMSE = (((x_sample_predict-z)**2).mean())**0.5
    print "final_RMSE"
    print final_RMSE
    print "\n"

    timesteps = xrange(len(x))
    pylab.plot(timesteps,x,'r-', timesteps,z,'b:', timesteps,x_sample_predict_list,'g--')
    pylab.xlabel('Time')
    pylab.ylabel('Wind Speed')
    pylab.title('Simulated Wind Speed vs Actual Wind Speed')
    pylab.legend(('predicted','measured','kalman'))
    pylab.show()

def calculate_regression(x, y):
    R2 = 0
    A = np.array( [x, np.ones(len(x))] )
    model, resid = np.linalg.lstsq(A.T, y)[:2]
    R2_val = 1 - resid[0] / (y.size * y.var())
    return R2_val

def load_x():
    return np.array([2, 3, 3, 5, 4, 4, 4, 5, 5, 6, 5, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11,
     11, 10, 8, 8, 8, 8, 6, 3, 4, 5, 5, 5, 6, 5, 5, 5, 6, 5, 5, 6, 6, 7, 6, 8, 9, 10,
     12, 11, 10, 10, 10, 11, 11, 10, 8, 8, 9, 8, 9, 9, 9, 9, 8, 9, 8, 11, 11, 11, 12,
     12, 13, 13, 13, 13, 13, 13, 13, 14, 13, 13, 12, 13, 13, 12, 12, 13, 13, 12, 12,
     11, 12, 12, 19, 18, 17, 15, 13, 14, 14, 14, 13, 12, 12, 12, 12, 11, 10, 10, 10,
     10, 9, 9, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 7, 7, 8, 8, 8, 6, 5, 5,
     5, 5, 5, 5, 6, 4, 4, 4, 6, 7, 8, 7, 7, 9, 10, 10, 9, 9, 8, 7, 5, 5, 5, 5, 5, 5,
     5, 5, 6, 5, 5, 5, 4, 4, 6, 6, 7, 7, 7, 7, 6, 6, 5, 5, 4, 2, 2, 2, 1, 1, 1, 2, 3,
     13, 13, 12, 11, 10, 9, 10, 10, 8, 9, 8, 7, 5, 3, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6,
     7, 7, 7, 6, 6, 6, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 1, 5, 5, 3, 2, 1, 2, 6, 7,
     7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7,
     7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 11, 11, 11, 11, 10, 10, 9, 10, 10, 10, 2, 2,
     2, 3, 1, 1, 3, 4, 5, 8, 9, 9, 9, 9, 8, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7,
     7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 5, 5, 5, 5, 6, 5])

def load_z():
    return np.array([3, 2, 1, 1, 1, 1, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 2, 2, 2,
     2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 5, 4, 4, 5, 5, 5, 6, 6,
     6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 9, 10, 9, 9, 10, 10, 9,
     9, 10, 9, 9, 10, 9, 8, 9, 9, 7, 7, 6, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 7, 6, 7,
     8, 8, 7, 8, 9, 9, 9, 9, 10, 9, 9, 9, 8, 8, 10, 9, 10, 10, 9, 9, 9, 10, 9, 8, 7,
     7, 7, 7, 8, 7, 6, 5, 4, 3, 5, 3, 5, 4, 4, 4, 2, 4, 3, 2, 1, 1, 2, 1, 2, 1, 4, 4,
     4, 4, 4, 3, 3, 3, 1, 1, 1, 1, 2, 3, 3, 2, 3, 3, 3, 2, 2, 5, 4, 2, 5, 4, 1, 1, 1,
     1, 1, 1, 1, 2, 2, 1, 1, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4,
     4, 4, 5, 5, 5, 4, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 3, 3, 1, 2, 1, 1, 2, 4, 3, 1,
     1, 2, 0, 0, 0, 2, 1, 0, 0, 2, 3, 2, 4, 4, 3, 3, 4, 5, 5, 5, 4, 5, 4, 4, 4, 5, 5,
     4, 3, 3, 4, 4, 4, 3, 3, 3, 4, 4, 4, 5, 5, 5, 4, 5, 5, 5, 5, 6, 5, 5, 8, 9, 8, 9,
     9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 9, 10, 9, 8, 8, 9, 8, 9, 9, 10, 9, 9, 9,
     7, 7, 9, 8, 7, 6, 6, 5, 5, 5, 5, 3, 3, 3, 4, 6, 5, 5, 6, 5])

if __name__ == '__main__': main()  # this avoids executing main on import your_module

rawDescription: Python using Kalman Filter to improve simulation but getting worse results


code:
import pcap

pc = pcap.pcap('wlan0')
for ts, pkt in pc:
    print ts, repr(pkt)

rawDescription: How to read data from wlan0 with Python 2.7

description: Read data from wlan0 with Python 2.7


code:
c.execute(qry)
y=c.fetchone()[0]
print y

rawDescription: Saving the result of a SQLite query as a Python variable


code:
import cStringIO
import re
from xml import sax
from xml.sax import handler

relinend = re.compile(r'\n')

txt = '''<foo>
            <tit>Bar</tit>
        <baz>whatever</baz>
     </foo>'''
stm = cStringIO.StringIO(txt)

class LocatingWrapper(object):
    def __init__(self, f):
        self.f = f
        self.linelocs = []
        self.curoffs = 0

    def read(self, *a):
        data = self.f.read(*a)
        linends = (m.start() for m in relinend.finditer(data))
        self.linelocs.extend(x + self.curoffs for x in linends)
        self.curoffs += len(data)
        return data

    def where(self, loc):
        return self.linelocs[loc.getLineNumber() - 1] + loc.getColumnNumber()

locstm = LocatingWrapper(stm)

class Handler(handler.ContentHandler):
    def setDocumentLocator(self, loc):
        self.loc = loc
    def startElement(self, name, attrs):
        print '%s@%s:%s (%s)' % (name,
                                 self.loc.getLineNumber(),
                                 self.loc.getColumnNumber(),
                                 locstm.where(self.loc))

sax.parse(locstm, Handler())

rawDescription: Is there a fast XML parser in Python that allows me to get start of tag as byte offset in stream?


code:
import robotparser
import urlparse
import urllib
from BeautifulSoup import BeautifulSoup, SoupStrainer

AGENT_NAME = 'PYMOTW'
URL_BASE = 'http://www.dcs.bbk.ac.uk/~martin/sewn/ls3'
DOMAIN = urlparse.urlparse(URL_BASE).hostname
visited = ['/']  # Create a copy

parser = robotparser.RobotFileParser()
parser.set_url(URL_BASE  + '/robots.txt')
parser.read()

def process_url(url):
    # transform relative paths
    parsed_path = urlparse.urlparse(url)
    if not parsed_path.hostname:
        url = URL_BASE  + url

    # check domain
    if parsed_path.hostname != DOMAIN:
        print 'External domain ignored: %s' % parsed_path.hostname

    # ensure we are allowed to fetch url
    if not parser.can_fetch(AGENT_NAME, parsed_path.path):
        print 'Not allowed to fetch %s' % parsed_path.path
        return

    # ensure we did not already visit it
    if url in visited:
        print 'Ignoring already visited %s' % url
        return

    print 'Visiting: %s' % url
    html = urllib.urlopen(url).read()
    visited.append(url)
    links = BeautifulSoup(html, parseOnlyThese=SoupStrainer('a', href=True))

    # Retrieve all of Tags as a list
    for link in links:
        parsed_link = urlparse.urlparse(link['href'])
        if len(link['href']) is 0:
            print 'Ignoring empty link'
        elif link['href'][0] == '#':
            print 'Ignoring hash link %s' % link['href']
        elif parsed_path.hostname and parsed_link.scheme not in [None, 'http', 'https']:
            print 'Ignoring non http(s) links %s' % link['href']
        else:
            process_url(link['href'])

PATHS = [
    '/testpage.html',
    '/files/',
    '/images/',
    '/private/'
]
for path in PATHS:
    process_url(path)

rawDescription: How does obey robots.txt using Python 2.7?


code:
from lxml import etree

doc = etree.fromstring(xml)

node = doc.find('Number')
print node.text # 0

node = doc.xpath('//ConnectedTerminal/Number')[0]
print node.text # 1

rawDescription: Getting unique value when the same tag is in children's tree in XML with Python


code:
from functools import partial
def f(a,b,c):
  print a,b,c
bound_f = partial(f,1)
bound_f(2,3)

rawDescription: How to bind arguments to given values in Python functions?

description: Bind arguments to given values in Python functions


code:
@receiver(m2m_changed, sender=Blog.users.through)
def blog_users_change(sender, instance, action, **kwargs):
    if action == 'pre_remove':
         # This will give you the users BEFORE any removals have happened
         print instance.users.all()
    elif action == 'post_remove':
         # This will give you the users AFTER any removals have happened
         print instance.users.all()

rawDescription: Django: Getting m2m related objects in signals


rawDescription: Comparing two matrices in Python, and filling the missed rows


code:
import os
import re

def createEscapedPattern(path,pattern):
    newPath = os.path.normpath(path)
    newPath = newPath.replace("\\","\\\\\\\\")
    return newPath + "\\\\\\\\" +  pattern

def createEscapedPath(path):
    newPath =  os.path.normpath(path)
    return newPath.replace("\\","\\\\")

src = 'C:\\Home\\test'
path_patter = '(\S+)_(\d+)_(\d+)_(\d+)__(\d+)_(\d+)_(\d+)$'
p = re.compile(createEscapedPattern(src,path_patter))
for dir, dirs, files in os.walk(src):
    if any(f.endswith('.txt') for f in files):
        dirs[:] = []
        continue
    if any(p.match(createEscapedPath(dir)) for f in files):
        for f in files:
            print createEscapedPath(dir + "/" + f)
    p = re.compile(createEscapedPattern(dir,path_patter))

rawDescription: Python - match directories with pattern (regular expression)


code:
from subprocess import check_output
l = check_output("ping -c 1 sitename | awk -F = 'FNR==2 {print substr($4,1,length($4)-3)}'", shell=True)
print l

rawDescription: Storing value from a parsed ping

description: Storing value from a parsed ping


code:
from twisted.internet import task
from twisted.internet import reactor

def runEverySecond():
    print "a second has passed"

l = task.LoopingCall(runEverySecond)
l.start(1.0) # call every second

reactor.run()

rawDescription: Python: How do I change this code so that I can doWork() once in every 60 seconds?


code:
class MyAPIView(APIView):
    def post(self, request, *args, **kwargs):
        print self.request.META.get('HTTP_SECRET_KEY', None)

rawDescription: Handle a request header in Django rest framework to get the secret key passed in the header?