code:
def hist(inputstr):
    lowlet = inputstr.upper()
    alphas = 'abcdefghijklmnopqrstuvwxyz'.upper()
    occurrences = dict( (letter, 0) for letter in alphas)
    total = 0
    for letter in lowlet:
        if letter in occurrences:
            total += 1
            occurrences[letter] += 1
    letcount = sorted(occurrences.iteritems(),key = lambda x:-x[1])
    for letter, count in letcount:
        if count>0:
             print letter*count

rawDescription: python - Letter Frequency

description: Python - Letter Frequency


rawDescription: How to reshape square numpy matrix into "diamond" of diagonals structure most efficiently

description: Reshape square numpy matrix into "diamond" of diagonals structure most efficiently


code:
from lxml import etree

name = '12345'

tree =   etree.parse('C:/Users/file.xml')
root = tree.getroot()

query = root.xpath('//ns:akey[text()="12345"]/..', namespaces={'ns': 'URN:XXX:XXXX:XXX:ADP:0001'})
for parent in query:
    print "" + '\n' + parent.tag
    child = parent.getchildren()
    for kid in child:
        p = kid.tag + " " + kid.text

rawDescription: How to search for text in XML and extract it's entire node it belongs to, with python ElementTree

description: Search for text in XML and extract it's entire node it belongs to, with python ElementTree


code:
import imaplib
from email.parser import HeaderParser

conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('username@gmail.com', 'password')

# Select the mail box
status, messages = conn.select('INBOX')

if status != "OK":
    print "Incorrect mail box"
    exit()

if int(messages[0]) > 0:

    for message_number in range(1,int(messages[0])+1):
        data = conn.fetch(message_number, '(BODY[HEADER])')
        parser = HeaderParser()
        msg = parser.parsestr(data[1][0][1])
        print "Subject: %s" % msg['subject']
        print "From: %s" % msg['from']

rawDescription: How do you check an email server (gmail) for a certain address and run something based on that address?

description: Check an email server (gmail) for a certain address and run something based on that address


code:
with open(user_input_path) as f1, open('/tmp/ARNE/blt') as f2:
    not_found = set(f1.read().split())
    for line in f2:
        _, found = line.strip().split(',')
        not_found.discard(found)  # remove found word
    print not_found
    # for x in not_found:
    #     print x

rawDescription: python, compare two files and get difference

description: Python, compare two files and get difference


code:
import PyQt4.QtGui as gui, PyQt4.QtCore as core

app = gui.QApplication([])

cb = gui.QComboBox()

cb.addItem('int 1',1)
cb.addItem('int 2',2)
cb.addItem('int 3',3)
cb.addItem('int 4',4)

print cb.itemData(0).toInt()[0]

core.pyqtSlot('int')
def f(index):
    data,can_convert =  cb.itemData(index).toInt()
    if can_convert:
        print 'integer:',data

cb.currentIndexChanged.connect(f)

cb.show()

app.exec_()

rawDescription: How to setData to QComboBox in PyQt/PySide?

description: SetData to QComboBox in PyQt/PySide


code:
def string_to_c(s, max_length = 140, unicode=False):
    ret = []

    # Try to split on whitespace, not in the middle of a word.
    split_at_space_pos = max_length - 10
    if split_at_space_pos < 10:
        split_at_space_pos = None

    position = 0
    if unicode:
        position += 1
        ret.append('L')

    ret.append('"')
    position += 1
    for c in s:
        newline = False
        if c == "\n":
            to_add = "\\\n"
            newline = True
        elif ord(c) < 32 or 0x80 <= ord(c) <= 0xff:
            to_add = "\\x%02x" % ord(c)
        elif ord(c) > 0xff:
            if not unicode:
                raise ValueError, "string contains unicode character but unicode=False"
            to_add = "\\u%04x" % ord(c)
        elif "\\\"".find(c) != -1:
            to_add = "\\%c" % c
        else:
            to_add = c

        ret.append(to_add)
        position += len(to_add)
        if newline:
            position = 0

        if split_at_space_pos is not None and position >= split_at_space_pos and " \t".find(c) != -1:
            ret.append("\\\n")
            position = 0
        elif position >= max_length:
            ret.append("\\\n")
            position = 0

    ret.append('"')

    return "".join(ret)

print string_to_c("testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing", max_length = 20)
print string_to_c("Escapes: \"quote\" \\backslash\\ \x00 \x1f testing \x80 \xff")
print string_to_c(u"Unicode: \u1234", unicode=True)
print string_to_c("""New
lines""")

rawDescription: Get str repr with double quotes Python

description: Get str repr with double quotes Python


code:
content = [u'\u4e00\u5927\u5b66\u751f\u88ab\u654c\u4eba\u6293\u4e86\uff0c\u654c\u4eba\u628a\u4ed6\u7ed1\u5728\u4e86\u7535\u7ebf\u6746\u4e0a\uff0c\u7136\u540e...']
print content[0]
content_utf8 = content[0].encode('utf8')
content_utf8[:10]
print content_utf8

rawDescription: using scrapy crawl non-unicode asian language (chinese, for example) website encoding

description: Using scrapy crawl non-unicode asian language (chinese, for example) website encoding


code:
for l in x:
    print ', '.join(map(str, l))

rawDescription: Python: Indexing a list of lists

description: Python: Indexing a list of lists


code:
def convert_to_float(frac_str):
    try:
        return float(frac_str)
    except ValueError:
        num, denom = frac_str.split('/')
        try:
            leading, num = num.split(' ')
            whole = float(leading)
        except ValueError:
            whole = 0
        frac = float(num) / float(denom)
        return whole - frac if whole < 0 else whole + frac

print convert_to_float('3') # 3.0
print convert_to_float('3/2') # 1.5
print convert_to_float('1 1/2') # 1.5
print convert_to_float('-1 1/2') # -1.5

rawDescription: Convert fraction to float?

description: Convert fraction to float


code:
from collections import Counter
d = Counter(a)

n, m = d.keys(), d.values()
print n, m

rawDescription: Counting frequency of string or float in a list


code: max(d.items(), key=lambda(k,v):v[1])[0]

rawDescription: Getting the key to the maximum component of a tuple in a dictionary of tuples


code:
# create dictionary, defaulting to new random positions
randpos = lambda: (random.randrange(0, width), random.randrange(0, height))
d = collections.defaultdict(randpos)

for i in range(10):             # corresponds to your outer 'while' loop
    print "outer loop", i
    for k in range(5):          # your 'for characters...' loop
        x, y = d[k]             # get x, y from dict, default to random position
        print x, y              # use position

rawDescription: random.randrang constant image blitting

description: Random.randrang constant image blitting


code:
for field in User._meta.get_sorted_fields():
    print field.name

rawDescription: Python: Dumping Database Data with Peewee


code:
import os
import pygame
from pygame.locals import *

def load_sliced_sprites(w, h, filename):
    '''
    Specs :
        Master can be any height.
        Sprites frames width must be the same width
        Master width must be len(frames)*frame.width
    Assuming you ressources directory is named "resources"
    '''
    images = []
    master_image = pygame.image.load(os.path.join('resources', filename)).convert_alpha()

    master_width, master_height = master_image.get_size()
    for i in xrange(int(master_width/w)):
        images.append(master_image.subsurface((i*w,0,w,h)))
    return images

class AnimatedSprite(pygame.sprite.Sprite):
    def __init__(self, images, fps = 10):
        pygame.sprite.Sprite.__init__(self)
        self._images = images

        # Track the time we started, and the time between updates.
        # Then we can figure out when we have to switch the image.
        self._start = pygame.time.get_ticks()
        self._delay = 1000 / fps
        self._last_update = 0
        self._frame = 0

        # Call update to set our first image.
        self.update(pygame.time.get_ticks())

    def update(self, t):
        # Note that this doesn't work if it's been more that self._delay
        # time between calls to update(); we only update the image once
        # then, but it really should be updated twice.

        if t - self._last_update > self._delay:
            self._frame += 1
            #if self._frame >= len(self._images): self._frame = 0
            if self._frame < len(self._images):
                self.image = self._images[self._frame]
                self._last_update = t

    def getimage(self):
        return self.image

    def isfinished(self):
        if self._frame == len(self._images):
            return True
        else:
            return False

    def reset(self):
        if self._frame >= len(self._images): self._frame = 0

if __name__ == "__main__":
    print "the game"
    pygame.init()
    screen = pygame.display.set_mode((800,600))

    images = load_sliced_sprites(100, 71, "spinning_roundhouse_kick.png")
    sprite = AnimatedSprite(images, 20)
    #while pygame.event.poll().type != KEYDOWN:
    while True:
        screen.fill((255,255,255))
        event = pygame.event.poll()
        if event.type == KEYDOWN and event.key == K_a:
            #print "keydown"
            #for image in images:
            while True:
                #print "frame"
                #print sprite._frame
                #print sprite.isfinished()
                time = pygame.time.get_ticks()
                sprite.update(time)
                screen.blit(sprite.getimage(), (0,0))
                pygame.display.update()
                #sprite.reset()
                if sprite.isfinished() == True:
                    sprite.reset()
                    break

        #time = pygame.time.get_ticks()
        #sprite.update(time)
        #screen.blit(sprite.image, (0,0))

        pygame.display.update()

rawDescription: sprite animation on key press

description: Sprite animation on key press


code:
import os
subpath ="filedir/filename.txt"
print os.path.join(os.getenv("LOCALAPPDATA"), subpath)

rawDescription: How can I open a file from a location that everyone has but under a different name?

description: Open a file from a location that everyone has but under a different name


rawDescription: Python pandas convert (yy/mm) date format and select certain time range


code:
import sys

fileName = sys.argv[1]
dataFile = open(fileName, 'r')

def calcDir(p0, p1, p2):
    v1x = float(p1[0]) - float(p0[0])
    v1y = float(p1[1]) - float(p0[1])
    v2x = float(p2[0]) - float(p1[0])
    v2y = float(p2[1]) - float(p1[1])
    if v1x * v2y - v1y * v2x > 0.0:
        return 'Left'
    else:
        return 'Right'

lineIdx = 0
for line in dataFile:
    line = line.rstrip()
    lineIdx += 1
    if lineIdx == 1:
        print line
    elif lineIdx == 2:
        line0 = line
        print line0
    elif lineIdx == 3:
        line1 = line
    else:
        line2 = line
        dir = calcDir(line0.split()[1:], line1.split()[1:], line2.split()[1:])
        print line1 + ' ' + dir
        line0 = line1
        line1 = line2

print line2

rawDescription: Algorithm to detect left or right turn from x,y co-ordinates


code:
#!/bin/sh
''''which python2 >/dev/null 2>&1 && exec python2 "$0" "$@" # '''
''''which python  >/dev/null 2>&1 && exec python  "$0" "$@" # '''
''''exec echo "Error: I can't find python anywhere"         # '''

import sys
print sys.argv

rawDescription: shebang env preferred python version

description: Shebang env preferred python version


code:
#!/usr/bin/env python

from evdev import *
import signal, sys, time

keys = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
dev = InputDevice('/dev/input/event0')
def scanBarcode():
  barcode = ''
  while True:
      event = dev.read_one()
      if event is None and barcode == '':
        #There are blank events in between characters,
        #so we don't want to break if we've started
        #reading them
        break #nothing of importance, start a new read.
      try:
        if event is not None:
          if event.type == ecodes.EV_KEY:
            data = categorize(event)
            if data.keystate == 0 and data.scancode != 42: # Catch only keyup, and not Enter
              if data.scancode == 28: #looking return key to be pressed
                return barcode
              else:
                  barcode += keys[data.scancode] # add the new char to the barcode
      except AttributeError:
          print "error parsing stream"
          return 'SOMETHING WENT WRONG'
      if lcd.buttonPressed(lcd.LEFT):
        return

rawDescription: Read from USB barcode scanner only if data is available


code:
import mimetypes
import string
import random
import time
import md5
import httplib

def upload(fields,files):
    boundaryChars = list(string.lowercase) + list(string.uppercase) + \
                    [str(x) for x in range(10)] + ['_'*10]
    random.shuffle(boundaryChars)

    boundary = '----------RaNdOm_crAPP'+''.join(boundaryChars[:20])
    CRLF = '\r\n'
    elem = []
    for key in fields:
        elem.append('--' + boundary)
        elem.append('Content-Disposition: form-data; name="%s"' % key)
        elem.append('')
        elem.append(fields[key])
    for (key, filename,value) in files:
        elem.append('--' + boundary)
        elem.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        elem.append('Content-Type: %s' % mimetypes.guess_type(filename)[0] or \
                        'application/octet-stream')
        elem.append('')
        elem.append(value)
    elem.append('--' + boundary + '--')
    elem.append('')
    body = CRLF.join(elem)
    content_type = 'multipart/form-data; boundary=%s' % boundary
    return content_type, body

def search_img(item, image_name):
    try:
        f = open(image_name, 'rb')
        img = f.read()
    except IOError, e:
        print 'fail to open %s' % image_name
        print e
        return None

    ts = str(time.time())

    m = md5.new('testsearch_by_image' + item)
    m.update(ts)
    m.update('0123456789')
    sign = m.hexdigest()

    #params = urllib.urlencode( )

    contentType,body = upload({
        'item': item,
        'app_key': 'test',
        'cmd':'search_by_image',
        'sign':sign,
        #'img_file':img,
        'extra':'',
        'time_stamp':ts,
        },
        [('img_file', image_name, img)]
    )
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Content-type': contentType,
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5',
        'Host':'i.joyton.com:2010',
        'Origin':'http://i.joyton.com:2010',
        'Referer':'http://i.joyton.com:2010/'
    }
    #print c
    #print body

    conn = httplib.HTTPConnection('i.joyton.com', 2010)
    conn.request('POST', '/', body, headers)
    response = conn.getresponse()

    print response.status, response.reason
    print response.read()
    conn.close()
    return response.read()

if __name__ == '__main__':
    search_img('book', 'iMgXS.jpg') #the same image.

rawDescription: How to upload, using python, a form that contains an image?

description: Upload, using python, a form that contains an image


code:
from bs4 import BeautifulSoup as BS
import urllib2
seed = "http://web.archive.org/web/20100412111652/http://app.singaporeedu.gov.sg/asp/index.asp"
soup = BS(urllib2.urlopen(seed))
print soup.find_all('a')

rawDescription: How to re.findall() using `daringfireball`'s regex?

description: Re.findall() using `daringfireball`'s regex


code:
with open(infile,'r') as r:
    start = set([line.strip().split()[3] for line in r])
    r.seek(0, 0)
    genes = set([line.split('"')[1] for line in r])
    print len(start)
    print len(genes)

rawDescription: splitting lines with " from an infile in python

description: Splitting lines with " from an infile in python


code:
from lxml import etree as ET
import gzip
import sqlite3

conn = sqlite3.connect('/path/to/database')

# create table
with conn:
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS odp_urls
        (url text primary key, title text, description text)''')

count = 0
nsmap = {'d': 'http://purl.org/dc/elements/1.0/'}
with gzip.open('content.rdf.u8.gz', 'rb') as content, conn:
    cursor = conn.cursor()
    for event, element in ET.iterparse(content, tag='{http://dmoz.org/rdf/}ExternalPage'):
        url = element.attrib['about']
        title = element.xpath('d:Title/text()', namespaces=nsmap)
        description = element.xpath('d:Description/text()', namespaces=nsmap)
        title, description = title and title[0] or '', description and description[0] or ''

        # no longer need this, remove from memory again, as well as any preceding siblings
        elem.clear()
        while elem.getprevious() is not None:
            del elem.getparent()[0]

        cursor.execute('INSERT OR REPLACE INTO odp_urls VALUES (?, ?, ?)',
            (url, title, description))
        count += 1
        if count % 1000 == 0:
            print 'Processed {} items'.format(count)

rawDescription: How to extract information from ODP accurately?

description: Extract information from ODP accurately


code:
# locate the table containing a cell with the given text
owner = re.compile('Owner Name')
cell = soup.find(text=owner).parent
while cell.name != 'table': cell = cell.parent
# print all non-empty strings in the table (except for the given text)
for x in cell.findAll(text=lambda x: x.strip() and not owner.match(x)):
  print x

rawDescription: Beautifulsoup get value in table

description: Beautifulsoup get value in table


code:
aList = range(10)
anIterator = iter(aList)

for item in anIterator:
    print item
    if item > 4: break

for item in anIterator:
    print item

rawDescription: How to iterate over a string using a buffer (python)

description: Iterate over a string using a buffer (python)


code:
with open('input.txt') as input_file:
  for row in input_file:
    if '1' in row.split():
      print 1
    else:
      print -1

rawDescription: How to read through a text file in row and column wise manner

description: Read through a text file in row and column wise manner


code:
import requests
s = requests.Session()
r = s.get('http://www.THEWEBSITE.com/custserv/locate_store.cmd')
print r.status_code
for c in s.cookies:
    print c
payload = { 'form_state' : 'selectThisStoreForm', 'storeId' : '62012' }
r = s.post('http://www.THEWEBSITE.com/custserv/save_user_store.cmd', data=payload)
print r.status_code
for c in s.cookies:
   print c

rawDescription: Cookies, JavaScript, Python, Browsing-but-not-really


code:
for i, line in enumerate(sys.stdin, -4):
    if i>=0: print line,

rawDescription: Strip the First Four Rows of a CSV in Python?


code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

time1 = np.arange(0,5,0.0001)
time = np.arange(0,15,0.0001)
data1=np.sin(2*np.pi*300*time1)
data2=np.sin(2*np.pi*600*time1)
data3=np.sin(2*np.pi*900*time1)
data=np.append(data1,data2 )
data=np.append(data,data3)
print len(time)
print len(data)

NFFT = 200     # the length of the windowing segments
Fs = 500  # the sampling rate

# plot signal and spectrogram

ax1 = plt.subplot(211)
plt.plot(time,data)   # for this one has to either undersample or zoom in
plt.xlim([0,15])
plt.subplot(212 )  # don't share the axis
Pxx, freqs, bins, im = plt.specgram(data, NFFT=NFFT,   Fs=Fs,noverlap=100, cmap=plt.cm.gist_heat)
plt.show()

rawDescription: Plotting with matplotlib specgram?

description: Plotting with matplotlib specgram


code:
from reportlab.lib.units import inch
from reportlab.lib.colors import magenta, red
from reportlab.pdfgen import canvas

lyrics = ['This is first line', 'This is second line', 'This is third line', 'This is the fourth line', 'This is the fifth line']

def textsize(canvas):
    canvas.setFont('Times-Roman', 20)
    canvas.setFillColor(red)
    canvas.drawCentredString(2.75*inch, 2.5*inch, "Font Size examples")
    canvas.setFillColor(magenta)

    size = 7
    y = 2.3 * inch
    x = 1.3 * inch
    for line in lyrics:
        canvas.setFont('Helvetica', size)
        canvas.drawString(x, y, '%s points' % size)
        print x, y, line
        canvas.drawString(x + 2*inch, y, line)
        y = y - (size * 1.2)
        size = size + 1.5

c = canvas.Canvas('font.pdf')
textsize( c )
c.showPage()
c.save()

rawDescription: Python reportlab not generating pdf properly


code:
array = ['foo', 'bar', 'foobar']
for index, item in enumerate(array):
    print item
    array[index] = None

# at the end of the loop, `item` still points to the final item
# so you might want to do the same to make sure it gets released
item = None

rawDescription: Release memory of an item within an array


code:
for i, line in enumerate(grid):
   print i,
   for char in line:
      print char,
   print

rawDescription: Printing numbered nested lists on separate lines using enumerate in python


code:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT name, category FROM animal")
result_set = cursor.fetchall()
for row in result_set:
    print "%s, %s" % (row["name"], row["category"])

rawDescription: How to retrieve SQL result column value using column name in Python?

description: Retrieve SQL result column value using column name in Python


code:
merged = [zip(fieldnames, row.split(",")) for row in my_list]
print merged

rawDescription: Python 2.6 How can I reiterate over a DictReader object that took in a list object?


code:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score

documents = ["Human machine interface for lab abc computer applications",
             "A survey of user opinion of computer system response time",
             "The EPS user interface management system",
             "System and human system engineering testing of EPS",
             "Relation of user perceived response time to error measurement",
             "The generation of random binary unordered trees",
             "The intersection graph of paths in trees",
             "Graph minors IV Widths of trees and well quasi ordering",
             "Graph minors A survey"]
true_k = 2
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(documents)
model = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1)
model.fit(X)
print("Top terms per cluster:")
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
    print "Cluster %d:" % i,
    for ind in order_centroids[i, :10]:
        print ' %s' % terms[ind],
    print

rawDescription: Clustering text documents using scikit-learn kmeans in Python


code:
import requests
with open('obama.txt','r') as f:
    text = f.read()

r=requests.post('http://www.sentiment140.com/api/bulkClassify?query=obama',data=text)
print r.text

rawDescription: Convert Linux's curl program to a python code


code:
from selenium import webdriver
my_driver = webdriver.PhantomJS()
my_driver.get(response.url)
results = my_driver.find_elements_by_xpath('//table[contains(@class, "modular")]//tr[.//a]')
for row in results:
    print row.find_element_by_xpath('./td[@class="title"]/a').text

rawDescription: scraping text hidden (?) behind javascript object

description: Scraping text hidden (?) behind javascript object


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
baseurl="https://www.google.co.in/?gws_rd=ssl"
driver = webdriver.Firefox()
driver.get(baseurl)
driver.find_element_by_id("lst-ib").click()
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("test")
driver.find_element_by_id("lst-ib").send_keys(Keys.RETURN)
driver.implicitly_wait(2)
link_name=driver.find_elements_by_xpath(".//*[@id='rso']/div/li/div/h3/a")
for link in link_name:
    print link.get_attribute('href')

rawDescription: How to get all links on a web page using python and selenium IDE

description: Get all links on a web page using python and selenium IDE


code:
my_list = [1, 3, 5, 9, 2, 6]
filtered_list = [item for item in my_list if item > 3]
print filtered_list
len(filtered_list)
# compare to generator expression

filtered_gen = (item for item in my_list if item > 3)
print filtered_gen  # notice it's a generator object
len(filtered_gen) # So technically, it has no length
# We extract each item out individually. We'll do it manually first.

filtered_gen.next()
filtered_gen.next()
filtered_gen.next()
filtered_gen.next() # Should be all out of items and give an error
# Yup, the generator is spent. No values for you!

# Let's prove it gives the same results as our list comprehension

filtered_gen = (item for item in my_list if item > 3)
gen_to_list = list(filtered_gen)
print gen_to_list
filtered_list == gen_to_list

rawDescription: generator comprehension

description: Generator comprehension


code:
import urllib2
print urllib2.unquote("%CE%B1%CE%BB%20")

rawDescription: Transform URL string into normal string in python (%20 to space etc)


code:
def wrapper(n,k):
    list=[]
    for i in range(1,n+1):
        list.append(str(i))

    rec_seq(list,k,0,'')

def rec_seq(list,k,i,prefix):
    #base case
    if k==0:
        print prefix
        return

    else:
        for c in list[i:None]:
            if prefix=='':
                newPrefix=prefix+c
                rec_seq(list,k-1,i+1,newPrefix)
            else:
                if int(c)>int(prefix[-1]):
                    newPrefix=prefix+c
                    rec_seq(list,k-1,i+1,newPrefix)
        return

if __name__=='__main__':
    wrapper (5,3)

rawDescription: Recursion : multiple instances of function for each element in for is not created


code:
def dict_path(path,my_dict):
    for k,v in my_dict.iteritems():
        if isinstance(v,dict):
            dict_path(path+"_"+k,v)
        else:
            print path+"_"+k,"=>",v
dict_path("",my_dict)

rawDescription: Print complete key path for all the values of a python nested dictionary


code:
print "\nReading the entire file into a list."
text_file = open("read_it.txt", "r")
lines = text_file.readlines()
print lines
print len(lines)
for line in lines:
    print line
text_file.close()

rawDescription: How to read the entire file into a list in python?

description: Read the entire file into a list in python


code:
who = raw_input("Give a name")
for name, number in zip(a, b):
    if name == who:
        print name, number

rawDescription: How to get two values from two seperate lists to print (python)

description: Get two values from two seperate lists to print (python)


code:
from threading import Thread
from Queue import Queue

def produce(queue, n_items):
    for d in range(n_items):
        queue.put(d)
        print "put {0} in queue".format(d)

def consume(queue, n_items):
    d = 0
    while d != n_items -1: # You need some sort of stop condition
        d = queue.get()
        print "got {0} from queue".format(d)

def start_producer_and_consumer(wait):
    q = Queue()
    consumer_thread = Thread(target = consume, args = (q, 10))
    producer_thread = Thread(target = produce, args = (q, 10))
    producer_thread.start()
    consumer_thread.start()
    if wait:
        producer_thread.join()
        consumer_thread.join()

if __name__ == '__main__':
    start_producer_and_consumer(True)

rawDescription: python asynchronization

description: Python asynchronization


code:
string_lst = ['fun', 'dum', 'sun', 'gum']
x="I love to have fun."

print re.findall(r"(?=("+'|'.join(string_lst)+r"))",x)

rawDescription: How to match any string from a list of strings in regular expressions in python?

description: Match any string from a list of strings in regular expressions in python


code:
sequence = "LTSDAKAARFSDPQFWKGRFSDPQFWKGRSAAKGRFARTSSGAAEKREPQAAYWKGRF "
occurrenceAA = str(sequence.count("AA"))   # counting occurence of n-aminos
percent_occurrenceAA = float(occurrenceAA)/len(sequence)*100   # calculate percent total of protein
print occurrenceAA, " Double-alanines in your sequence"
print round(percent_occurrenceAA,2), " % of total"   # rounding off % to 2 decimal places

rawDescription: Counting di-Amino Acid frequencies (Bigram frequencies) from FASTA files


code:
from datetime import datetime
import csv
import time
with open('abc1') as f:
    reader = csv.reader(f, delimiter=',')
    for line in reader:
        pass
    class_name = ''.join(line[:2]) #save the name on last line
    print class_name
    f.seek(0)           # Rest the file pointer t o the start of the file
    for line in reader:
        cls_name = ''.join(line[:2])
        if cls_name == class_name:
            if line[-2]:
                current_dtime = datetime.now()
                fetched_time = datetime.strptime(line[2], '%H:%M')
                fetched_time = datetime(year=current_dtime.year,
                                        month = current_dtime.month,
                                        day = current_dtime.day,
                                        hour = fetched_time.hour,
                                        minute = fetched_time.minute
                                        )
                if ((current_dtime - fetched_time).seconds/ 60.0) > 30.0:
                    print "late"

rawDescription: Finding the last unique identifier and checking for a number then comparing time to current time


code:
class Foo(object):
    def eggs(self):
        print eggs()

class Bar(Foo):
    def ham(self):
        print eggs()

rawDescription: Why python use different scope mechanisms in function and class?


rawDescription: Getting file extension using pattern matching in python


code:
print("Type the filename")
file_again = raw_input("> ")

with open(file_again, 'r') as f:
    lines = f.readlines()

for num, line in enumerate(lines, 1):
    print "%s: %s" % (num, line)

rawDescription: Print one line at a time from text file using python


code:
import re
def foo():
    import csv
    fubar = 0
    print dir()
foo()

rawDescription: Python: what modules have been imported in my process?


code:
class Foo(object):
    classVar = 0 #this is a class variable. It is shared between all instances
    def __init__(self, instanceVar):
        self.someVar = instanceVar

obj1 = Foo(10)
obj2 = Foo(42)

print obj1.classVar # prints 0
print obj2.classVar # prints 0

print obj1.someVar #prints 10
print obj2.someVar #prints 42

rawDescription: In python. class variable is static..is I want to use different instance. is there any solution?


code:
import datetime

class Art(db.Model):
    title = db.StringProperty(required=True)
    art = db.TextProperty(required=True)
    created = db.DateTimeProperty(auto_now=True)
art = Art(title='title', art='art').put()
seconds = (datetime.datetime.now() - art.created).seconds

print 'Posted %s seconds ago.' % seconds

rawDescription: Time difference between DateTimeProperty of Google App Engine


code:
def height_generator(height):
    while height > 0:
        a = yield height
        if a == 1:
            height -= 15
        else:
            height -= 20

g = height_generator(100)
g.next()
print g.send(1) # 85
print g.send(2) # 65
print g.send(1) # 50

rawDescription: python generator with different next() conditions

description: Python generator with different next() conditions


code:
import matplotlib.pyplot as plt

# this is just a helper class to keep things clean
class MyAxis(object):
    def __init__(self,ax,fig):
        # this flag tells me if there is a plot in these axes
        self.empty = False
        self.ax = ax
        self.fig = fig
        self.pos = self.ax.get_position()

    def del_ax(self):
        # delete the axes
        self.empty = True
        self.fig.delaxes(self.ax)

    def swap(self,other):
        # swap the positions of two axes
        #
        # THIS IS THE IMPORTANT BIT!
        #
        new_pos = other.ax.get_position()
        self.ax.set_position(new_pos)
        other.ax.set_position(self.pos)
        self.pos = new_pos

def main():
    # generate a figure and 10 subplots in a grid
    fig, axes = plt.subplots(ncols=5,nrows=2)

    # get these as a list of MyAxis objects
    my_axes = [MyAxis(ax,fig) for ax in axes.ravel()]

    for ax in my_axes:
        # plot some random stuff
        ax.ax.plot(range(10))

    # delete a couple of axes
    my_axes[0].del_ax()
    my_axes[6].del_ax()

    # count how many axes are dead
    dead = sum([ax.empty for ax in my_axes])

    # swap the dead plots for full plots in a row wise fashion
    for kk in range(dead):
        for ii,ax1 in enumerate(my_axes[kk:]):
            if ax1.empty:
                print ii,"dead"
                for jj,ax2 in enumerate(my_axes[::-1][kk:]):
                    if not ax2.empty:
                        print "replace with",jj
                        ax1.swap(ax2)
                        break
                break

    plt.draw()
    plt.show()

if __name__ == "__main__":
    main()

rawDescription: In Python with Matplotlib how to check if a subplot is empty in the figure


code:
output = '
\n'
output += '%s %s%s%s\n' % ('Rk'.rjust(3), 'Team'.ljust(50), 'W'.rjust(2), 'L'.rjust(2))
output += '%s %s%s%s\n' % ('--'.rjust(3), '----'.ljust(50), '-'.rjust(2), '-'.rjust(2))

for team in teams:
    link = '%s' % (team.slug, team.name)
    link = link.ljust(50 + len(team.slug) + 15)
    rank, won, lost = str(team.rank).rjust(3), str(team.won).rjust(2), str(team.lost).rjust(2)
    output += '%s %s%s%s\n' % (rank, link, won, lost)
output += '
'
print output

rawDescription: Python ljust not displaying properly when including a lin


code:
import os

def read_log(findme):
  f = open('/var/tmp/a.ini', "r")
  for line in f:
    if "=" in line:
      key,val = line.split("=",1)
      if findme in key:
        return val.replace("\\n", "").rstrip()

def replace_preset(findme):
  a = os.popen("grep %s /var/tmp/a.ini" % findme).read()
  a=  a.replace("%s=" % findme, "").replace("\\n", "").rstrip()
  os.system('sed -i s/"^preset0=.*"/"preset0=' + a + '"/ /var/tmp/a.ini')

def replace_preset_byfield(input, reflace, reflaceby):
  try:
    input = input.replace("\\n", "").rstrip()
    input = input.split(",")
    input[reflace] = reflaceby.replace("\\n", "").rstrip()
    print input
    a  = "%s,%s,%s,%s" % (input[0],input[1],input[2],input[3])
    os.system('sed -i s/"^preset0=.*"/"preset0=' + a + '"/ /var/tmp/a.ini')
  except Exception, err:
    print err
    pass

# 1) Read
print read_log("preset5")

# 2) Copy preset5 and paste to preset0
replace_preset("preset5")
print read_log("preset0")

# 3) Edit preset0 values
# replace_preset_byfield(read_log("preset0"), 0, 55 )
replace_preset_byfield(read_log("preset0"), 0, "55" )
print read_log("preset0")

rawDescription: Python - how to copy value from a line and then paste to other line values as it is, or modify it later


code:
lst = [1, 3, 4, 5, 1]
new_lst = lst[:3]
del lst[:3]
print lst
print new_lst

rawDescription: Remove and return adjacent elements from a list


code:
import threading
END = False
def run(x=0):
    x += 5
    print x
    if not END:
        threading.Timer(1.0, run, [x]).start()

threading.Timer(1.0, run, [x]).start()
5

rawDescription: Activate Python program every 5 minutes?

description: Activate Python program every 5 minutes


code:
lis =  ['a', 'b', 'c', 'd', 'e']
for x,y in zip(lis,lis[1:]):
    print x,y

rawDescription: getting two values using comprehension

description: Getting two values using comprehension


code:
class Animal(object):
    def speak(self):
        raise NotImplementedError()

    def speak_twice(self):
        self.speak()
        self.speak()

class Dog(Animal):
    def speak(self):
        print "woff!"

class Cat(Animal):
    def speak(self):
        print "meow"

rawDescription: What’s the point of inheritance in Python?


rawDescription: searching and adding a python list

description: Searching and adding a python list


code:
words = [ "happy", "sad", "good", "bad"]
for word in words:
    if ("happy" in word.lower()):
        print "yes"

rawDescription: How do i check if a string in a set is part of another string in python 2.7?

description: Check if a string in a set is part of another string in python 2.7


code:
qs = qs.filter(reduce(operator.or_, or_queries)).as_pymongo()
print qs

rawDescription: How to print contents of mongoengine's QuerySet

description: Print contents of mongoengine's QuerySet


code:
class a_daemon():
    def __init__(self,last_line):
        print "initilizing.."
        self.last_line=last_line

    def run_me(self):
        print "running.."
        #while true, process lines, set last_line to current line being processesed.
        self.last_line='blah'

from contextlib import contextmanager

@contextmanager
def run_new_daemon():
    print "getting last line"
    last_line=open("last_line.txt").read() #you should get a "file does not exist" error the first time running this unless you created the file already

    my_daemon=a_daemon(last_line)
    yield my_daemon

    print "shutting down, writing last line to file."
    with open("last_line.txt",'w') as last_line_file:
        last_line_file.write(my_daemon.last_line)

with run_new_daemon() as my_daemon:
    my_daemon.run_me()

rawDescription: Best way to keep track of the last line read in a text file

description: Keep track of the last line read in a text file


rawDescription: How can I add small 2D array to larger array?

description: Add small 2D array to larger array


code:
import re
pat = re.compile("[A-Za-z]+")
for sentence in text:
    sent_tokens = []
    prev_end = 0
    for match in re.finditer(pat, sentence):
        print match.start(0), match.end(0), match.group(0)
        chinese_part = sentence[prev_end:match.start(0)]
        sent_tokens += tokenize(chinese_part)
        sent_tokens.append(match.group(0))
        prev_end = match.end(0)
    last_chinese_part = sentence[prev_end:]
    sent_tokens += tokenize(last_chinese_part)
    print sent_tokens

rawDescription: How to not split English into separate letters in the Stanford Chinese Parser

description: Not split English into separate letters in the Stanford Chinese Parser


code:
import gtk.gdk

w = gtk.gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
if (pb != None):
    pb.save("screenshot.png","png")
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

rawDescription: Take a screenshot via a python script. [Linux]


code:
goldah = []

i=0
while i< len(titles):
    titles = map(float, titles)
    goldahtemp = titles[i] * exchange
    goldahtemp = "%.2f" % goldahtemp
    print goldahtemp
    goldah.append(goldahtemp)
    i+=1

rawDescription: Last iteration of loop does not save

description: Last iteration of loop does not save


rawDescription: How to convert hex string to hex number?

description: Convert hex string to hex number


rawDescription: Multiply each number of an array with its aligned row of a matrix in numpy?


code:
import urllib2

#redirect_handler = urllib2.HTTPRedirectHandler()

class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        print "Cookie Manip Right Here"
        return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)

    http_error_301 = http_error_303 = http_error_307 = http_error_302

cookieprocessor = urllib2.HTTPCookieProcessor()

opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)
urllib2.install_opener(opener)

response =urllib2.urlopen("WHEREEVER")
print response.read()

print cookieprocessor.cookiejar

rawDescription: Python HTTP HEAD - dealing with redirects properly?


code:
# from Ned Batchfelder
# http://nedbatchelder.com/blog/201310/range_overlap_in_two_compares.html
def overlap(start1, end1, start2, end2):
    """
    Does the range (start1, end1) overlap with (start2, end2)?
    """
    return end1 >= start2 and end2 >= start1

def compare_rows(group):
    winners = []
    skip = []
    if len(group) == 1:
        return group[['start', 'end', 'score']]
    for i in group.index:
        if i in skip:
            continue
        for j in group.index:
            last = j == group.index[-1]
            istart = group.loc[i, 'start']
            iend = group.loc[i, 'end']
            jstart = group.loc[j, 'start']
            jend = group.loc[j, 'end']
            if overlap(istart, iend, jstart, jend):
                winner = group.loc[[i, j], 'score'].idxmax()
                if winner == j:
                    winners.append(winner)
                    skip.append(i)
                    break
            if last:
                winners.append(i)
    return group.loc[winners, ['start', 'end', 'score']].drop_duplicates()

grouped = df.groupby('id')
print grouped.apply(compare_rows)

rawDescription: Pandas - non overlapping group members

description: Pandas - non overlapping group members


code:
import os
import csv

from itertools import izip_longest

def writeCSV(path, filename, d):
    def flatten(l):
        return [t[i] for t in l for i in range(len(t))]

    filename = os.path.join(path, filename)

    with open(filename, 'wb') as outfile:
        writer = csv.writer(outfile, delimiter='~')
        keys = d.keys()
        writer.writerow(flatten(zip(keys, ['id'] * len(keys))))
        # Keep the values in the same order as keys
        values = [d[key] for key in keys]
        writer.writerows(flatten(row) for row in izip_longest(*values, fillvalue=('', '')))
        print "write file complete"

writeCSV("C:\\Projects\\Output", "output.csv", allData)

rawDescription: writing list of tuples within a dictionary to csv

description: Writing list of tuples within a dictionary to csv


code:
import tweepy

CONSUMER_KEY = 'yourconsumerkey'
CONSUMER_SECRET = 'yourconsumersecret'
ACCESS_KEY = 'youraccesskey'
ACCESS_SECRET = 'yoursecret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

search_text = "#gamedev"
search_number = 2
search_result = api.search(search_text, rpp=search_number)
for i in search_result:
    print i.text

rawDescription: Tweepy Python, retrieving latest 5 instances of a hashtag


code:
def _get_logger(pdir,ldir,lname,level,fmt):
    try:
        logs_dir = os.path.join(pdir, ldir)
        if not os.path.exists(logs_dir):
            os.makedirs(logs_dir)
    except Exception as e:
        print e
        exit(1)

    logging.basicConfig(
        filename=os.path.join(logs_dir, lname),
        level=level,
        format=fmt
    )
    return logging.getLogger('cli.log')

exec_file = os.path.abspath(argv[0])
exec_dir = os.path.dirname(exec_file)
#build a dict with default configuration values for the logger
default_logger = dict(pdir=exec_dir,ldir='logs',lname='cli.log',level='ERROR',
                fmt='%(asctime)s %(levelname)s: %(message)s')

try:
    log_filename = args.logfile if args.logfile else config.get('Logging', 'filename')
    level = config.get('Logging', 'log_level')
    format = config.get('Logging', 'format')
except (NoOptionError, NoSectionError) as e:
    #unpack the configuration dict and call _get_logger
    logger = _get_logger(**default_logger)
    logger.error(repr(e))
    exit(1)
else:
    # we have successfully read configuration from file, so update
    # configuration dict to reflect new settings
    default_logger.update(fmt=format,level=level,lname=log_filename)
if os.path.isabs(log_filename):
    # log_filename is an absolute one. split it to get filename and dirname
    # and update configuration dict
    bdir, log_filename = os.path.split(log_filename)
    default_logger.update(pdir='',ldir=bdir,lname=log_filename)
logger = _get_logger(**default_logger)

rawDescription: Logging errors to two different files

description: Logging errors to two different files


code:
#
# The following is an example of an application I have running using the
# serial port.  Although the baud is lower, it should not matter.  This application
# will capture each byte and append them into a buffer before processing.  All bytes
# are captured including non-printable characters.  The terminator in my application
# is the RETURN character (0x0A) or the (0x0D) denoting end of line.
# I think your port is hung waiting on the full twelve (12) bytes that you are
# expecting on the read.  The hang condition is because only a portion of
# the 12 bytes have occurred.  I suggest you add a timeout value of 1 second
# to the serial read, then test for the length of what was read to validate
# what you think you just read.  If zero, you have a serial cable or problem
# at the other end of the cable.  If greater than zero, then the device is
# not sending what you think it should.

import serial
import string
import binascii
import threading
import thread
import fileinput
import datetime
import time
import sys

from configobj import ConfigObj

# Insert following code at top in the import area of your application
#
hdrFile  = 'AppName.ini'
config   = ConfigObj(hdrFile)
hdrVal   = 'COM'
comport  = int(config[hdrVal])
hdrBaud  = 'Baudrate'
baudrate = config[hdrBaud]

print "Com Config = ",config
print "Com Port   = ",comport

boolSerOnline = False
bool_IsALIVE  = False      # thread has active serial port

class myThread( threading.Thread ):
    def __init__(self, intThreadID, strName, intCountVal ):
        # --------------------------------------- Constructor
        threading.Thread.__init__(self)    # <<-- MUST be first in Init
        self.threadID = intThreadID
        self.threadName = strName
        self.intStartCounter = intCountVal
    def run(self):
        # --------------------------------------- Tooltalk
        # Thread to Listen to the RS-232 RX Port
        # ------------------------------- TOOLTALK ()
        # ------------------------------- THREAD
        global bool_IsALIVE, boolThreadRun
        #
        #
        inbuf  = []          # Clear Buffer
        buffer = ""          # Clear Buffer
        strFirstByte = ""
        boolWriteCRLF = 0
        #
        print "\n*** Starting %s: %s" % ( self.threadName, time.ctime(time.time()))
        print "\n"
        #
        bool_IsALIVE = False
        #
        # ----------------------------------- Start of Thread Loop
        while ( boolThreadRun ):
            # ----------------------------- THREAD is Running

            try:
                # ------------ Read 1 byte at a time in thread
                # Note: reads all characters, Even non-printable characters
                strByte = str(ser.read( 1 ))
                #
                intLength = len( strByte )
                #
                if ( intLength > 0 ):
                    bool_IsALIVE = True          # Indicate connected
                    # Process byte
                    #
                    if (strFirstByte == ""):
                        boolWriteCRLF  = 0
                        #
                        if ( strByte == '\r' ):
                            strByte = ''          # Skip until we have first byte
                        elif ( strByte == '\n' ):
                            strByte = ''          # Skip until we have first byte
                        elif ( strByte == '0' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte
                            boolWriteCRLF  = 1
                        elif ( strByte == 'S' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte
                            boolWriteCRLF  = 1
                        elif ( strByte == 'C' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte
                            boolWriteCRLF  = 1
                        elif ( strByte == '*' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte     # save first byte of string
                            boolWriteCRLF  = 1
                        elif ( strByte == '>' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte     # save first byte of string
                            boolWriteCRLF  = 1
                        elif ( strByte == 'M' ):
                            inbuf = []                     # Clear buffer first
                            inbuf.append( strByte )        # Add to inbuf
                            buffer = ''.join(inbuf)        # join the two buffers
                            strFirstByte = strByte     # save first byte of string
                            boolWriteCRLF  = 1
                        else:
                            pass
                    else:
                        inbuf.append( strByte )        # Add to inbuf
                        buffer = ''.join(inbuf)        # join the two buffers
                else:
                    # serial driver timed out producing No byte
                    # print "SKIP - NO BYTE (NO POWER?)"
                    pass
            except:
                bool_IsALIVE = False
                print "[Tooltalk] Error reading Serial Port"

if __name__ == "__main__":
    try:
        # *** +++++++++++++ Notice the 1 second timeout ...
        ser = serial.Serial(comport,baudrate,8,'N',1,timeout=1)
        boolSerOnline = True
    except:
        print "[Main] Serial Port NOT Connected\n"
        boolSerOnline = False

    boolThreadRun = True        # Flag indicating "Tooltalk" to Run
    #
    thread1 = myThread(1, "Tooltalk-1", 1)   # Init Thread
    thread1.start()        # Start Tooltalk Thread

rawDescription: pyserial and wxpython matplotlib the read method failed in thread

description: Pyserial and wxpython matplotlib the read method failed in thread


code:
import re

# Store your string
my_str = 'red blue green * hello* pink orange 4pgp42g4jg42 * world* violet black'

# Find matches
match = re.findall(r'\*([^\*]*)\*', my_str)

# Print everything
print match

# Iterate
for item in match:
    print item

rawDescription: Python Extract Multiple Strings Many Times Between Asterisks


code:
import zipfile

def main(archive_list=[],zfilename='default.zip'):
    print zfilename
    zout = zipfile.ZipFile(zfilename, "w", zipfile.ZIP_DEFLATED) # <--- this is the change you need to make
    for fname in archive_list:
        print "writing: ", fname
        zout.write(fname)
    zout.close()

if __name__ == '__main__':
    main()

rawDescription: python zipfile module doesn't seem to be compressing my files

description: Python zipfile module doesn't seem to be compressing my files


code:
for line in reversed(open("filename").readlines()):
    cond = line.startswith("\"submitTime\"")
    if cond and line.startswith("},"):
        new = line.replace("},","],")
        print new
        cond = False
    else:
        print line

rawDescription: Can Python perform the 'replace' function across lines?


code:
for line in open(os.path.expanduser("~/my_file")):
    print line

rawDescription: Is there a way I can use ~ to point to the home directory in file paths in Python?


code:
import networkx as nx
g = nx.DiGraph()
g.add_edges_from( (y,x) for x,y in your_list )
print list(nx.dfs_postorder_nodes(g, '3'))
[u'11', u'10', u'5', u'7', u'6', u'9', u'8', u'4', '3']

rawDescription: need to find child element from python list

description: Need to find child element from python list


code:
from ctypes import *

NSLibraryDirectory = 5
NSUserDomainMask = 1

def NSSearchPathForDirectoriesInDomains(directory, domainMask, expand = True):
    # If library path looks like framework, OS X will search $DYLD_FRAMEWORK_PATHs automatically
    # There's no need to specify full path (/System/Library/Frameworks/...)
    Foundation = cdll.LoadLibrary("Foundation.framework/Foundation")
    CoreFoundation = cdll.LoadLibrary("CoreFoundation.framework/CoreFoundation");

    _NSSearchPathForDirectoriesInDomains = Foundation.NSSearchPathForDirectoriesInDomains
    _NSSearchPathForDirectoriesInDomains.argtypes = [ c_uint, c_uint, c_bool ]
    _NSSearchPathForDirectoriesInDomains.restype = c_void_p

    _CFRelease = CoreFoundation.CFRelease
    _CFRelease.argtypes = [ c_void_p ]

    _CFArrayGetCount = CoreFoundation.CFArrayGetCount
    _CFArrayGetCount.argtypes = [ c_void_p ]
    _CFArrayGetCount.restype = c_uint

    _CFArrayGetValueAtIndex = CoreFoundation.CFArrayGetValueAtIndex
    _CFArrayGetValueAtIndex.argtypes = [ c_void_p, c_uint ]
    _CFArrayGetValueAtIndex.restype = c_void_p

    _CFStringGetCString = CoreFoundation.CFStringGetCString
    _CFStringGetCString.argtypes = [ c_void_p, c_char_p, c_uint, c_uint ]
    _CFStringGetCString.restype = c_bool

    kCFStringEncodingUTF8 = 0x08000100

    # MAX_PATH on POSIX is usually 4096, so it should be enough
    # It might be determined dynamically, but don't bother for now
    MAX_PATH = 4096

    result = []

    paths = _NSSearchPathForDirectoriesInDomains(directory, domainMask, expand)

    # CFArrayGetCount will crash if argument is NULL
    # Even though NSSearchPathForDirectoriesInDomains never returns null, we'd better check it
    if paths:
        for i in range(0, _CFArrayGetCount(paths)):
            path = _CFArrayGetValueAtIndex(paths, i)

            buff = create_string_buffer(MAX_PATH)
            if _CFStringGetCString(path, buff, sizeof(buff), kCFStringEncodingUTF8):
                result.append(buff.raw.decode('utf-8').rstrip('\0'))
            del buff

        _CFRelease(paths)

    return result

print NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask)

rawDescription: On OS X, is it possible to get the user library directory from pure Python?


code:
while True:
     if a==0:
         print "ok"
     elif a==1:
         continue
         # goes back to top of loop
     else:
          print "ok"

rawDescription: How to break loop and then start over

description: Break loop and then start over


code:
import numpy as np
import pandas

df = pandas.DataFrame(dict(x=['1', 'a5', 'cde9', '1', 'cde9']))

unique_vals, df['keys'] = np.unique(df.x, return_inverse=True)

print df

rawDescription: python strings to int/float in an efficient way

description: Python strings to int/float in an efficient way


code:
import libxml2
from urllib2 import urlopen
data = urlopen('http://pastie.org/pastes/1333021/download').read()
doc = libxml2.parseMemory(data,len(data))
context = doc.xpathNewContext()
context.xpathRegisterNs("db", "http://examplenamespace.com")
for res in context.xpathEval("//db:food"):
    context.setContextNode(res)
    print "Got Food Node:"
    desc = context.xpathEval('./db:description')[0]
    print desc

rawDescription: Python libxml2 XPath/Namespace Help

description: Python libxml2 XPath/Namespace Help


code:
import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
  pickle.dump(a, handle)

with open('filename.pickle', 'rb') as handle:
  b = pickle.load(handle)

print a == b

rawDescription: Python json of a list

description: Python json of a list


code:
result = session.getbulk(0, 48, vars)
for i in range(0, len(result), 3):
    print "ifind: "+result[i]+" ifdesc: "+result[i+1]+" status: "+result[i+2]

rawDescription: Printing net-snmp getbulk results with newlines at each index


code:
class DependentProperty(object):
    def __init__(self, calculate=None, default=None, depends_on=()):
        # "name" and "dependence_tree" properties are attributes
        # set up by the metaclass of the owner class
        if calculate:
            self.calculate = calculate
        else:
            self.default = default
        self.depends_on = set(depends_on)

    def __get__(self, instance, owner):
        if hasattr(self, "default"):
            return self.default
        if not hasattr(instance, "_" + self.name):
            setattr(instance, "_" + self.name,
                self.calculate(instance, getattr(instance, "_" + self.name + "_last_value")))
        return getattr(instance, "_" + self.name)

    def __set__(self, instance, value):
        setattr(instance, "_" + self.name + "_last_value", value)
        setattr(instance, "_" + self.name, self.calculate(instance, value))
        for attr in self.dependence_tree[self.name]:
            delattr(instance, attr)

    def __delete__(self, instance):
        try:
            delattr(instance, "_" + self.name)
        except AttributeError:
            pass

def assemble_tree(name,  dict_, all_deps = None):
    if all_deps is None:
        all_deps = set()
    for dependance in dict_[name].depends_on:
        all_deps.add(dependance)
        assemble_tree(dependance, dict_, all_deps)
    return all_deps

def invert_tree(tree):
    new_tree = {}
    for key, val in tree.items():
        for dependence in val:
            if dependence not in new_tree:
                new_tree[dependence] = set()
            new_tree[dependence].add(key)
    return new_tree

class DependenceMeta(type):
    def __new__(cls, name, bases, dict_):
        dependence_tree = {}
        properties = []
        for key, val in dict_.items():
            if not isinstance(val, DependentProperty):
                continue
            val.name = key
            val.dependence_tree = dependence_tree
            dependence_tree[key] = set()
            properties.append(val)
        inverted_tree = {}
        for property in properties:
            inverted_tree[property.name] = assemble_tree(property.name, dict_)
        dependence_tree.update(invert_tree(inverted_tree))
        return type.__new__(cls, name, bases, dict_)

if __name__ == "__main__":
    # Example and visual test:

    class Bla:
        __metaclass__ = DependenceMeta

        def calc_b(self, x):
            print "Calculating b"
            return x + self.a

        def calc_c(self, x):
            print "Calculating c"
            return x + self.b

        a = DependentProperty(default=10)
        b = DependentProperty(depends_on=("a",), calculate=calc_b)
        c = DependentProperty(depends_on=("b",), calculate=calc_c)

    bla = Bla()
    bla.b = 5
    bla.c = 10

    print bla.a, bla.b, bla.c
    bla.b = 10
    print bla.b
    print bla.c

rawDescription: Lazy data-flow (spreadsheet like) properties with dependencies in Python


code:
import re
prices = ['400.00 USD', '120.00 $', '237.00 euro']
template = re.compile('^([0-9\.]+)\s+(.*)$')
for price in prices:
    print template.findall(price)

rawDescription: Extracting the part of the string

description: Extracting the part of the string


code:
counter = Counter(tuple(sorted(tup)) for tup in your_list)
print counter

rawDescription: Counting tuples in a list, no matter the order of each element within the tuples


code:
out = []
sentences = corpus.findall('.//sentence')
for sentence in sentences:
    out.append(sentence.text)
    instances = sentence.findall('instance')
    for instance in instances:
        out.append(instance.text)
        out.append(instance.tail)
    out.append(sentence.tail)

filterout = []
for i in out:
    txt = i.replace('\n', ' ').strip()
    if len(txt):
        filterout.append(txt)
print ' '.join(filterout)

rawDescription: Parsing multiple instance within a sentence in XML - Python


code:
wsd = WSDiscovery()
wsd.start()

ttype = QName("abc", "def")

ttype1 = QName("namespace", "myTestService")
scope1 = Scope("http://myscope")
ttype2 = QName("namespace", "myOtherTestService_type1")
scope2 = Scope("http://other_scope")

xAddr = "localhost:8080/abc"
wsd.publishService(types=[ttype], scopes=[scope2], xAddrs=[xAddr])

ret = wsd.searchServices()

for service in ret:
    print service.getEPR() + ":" + service.getXAddrs()[0]

wsd.stop()

rawDescription: Discovery of web services using Python

description: Discovery of web services using Python


code:
def copy_goal(g):
  return Tactic('skip')(g)[0]

x = Int('x')
g = Goal()
g.add(x < 10)
g.add(x > 0)
g1 = copy_goal(g)
g1.add(x != 5)
print g
print g1

rawDescription: z3py: apply tactics on a solver

description: Z3py: apply tactics on a solver


code:
import ConfigParser

class acc(ConfigParser.RawConfigParser, object):
    def __init__(self, acc_file):
        super(acc, self).__init__()
        self.lol = 1
        print self.has_section(self.lol)

a=acc(1)

rawDescription: Subclass of ConfigParser has no attribute _sections


code:
def __init__(self,*args,**kwargs):

    default_values = {a:{},b:34,c:"generic string"}
    for k,v in kwargs.iteritems():
        try:
            if not v is False:
                setattr(self,k,default_values[k])
        except Exception, e:
            print "Argument has no default value.",e

rawDescription: Choosing variables to create when initializing classes


code:
import numpy as np
from sklearn.metrics import  pairwise

main_object = [0.8,0.8]

X = np.matrix([
    [12345,0.8,0.9],
    [11111,0.9,0.1],
    [22222,0.7,.8]])

# you create a dictionary of <key=id, value=position(row in samples)>
dict_ids = {idx:int(id.item(0,0)) for idx,id in enumerate(X[:,0:1])}
print dict_ids

# calculating distances in one shot, X[:,1:] this syntax omits ids col
dist = pairwise.pairwise_distances(X[:,1:], main_object, metric='cosine')
print dist

# in dist now you have all distances to 'main_object'. Now you can play with it, for example if you want min dist:
print 'id:',dict_ids[np.argmin(dist)], 'dist:',min(dist)
# output id: 12345 dist: [ 0.00172563]

rawDescription: How to properly use sklearn cosine_similarity with a dictionary

description: Properly use sklearn cosine_similarity with a dictionary


code:
#!/usr/bin/env python

from collections import deque
from threading import Thread,Condition

QUEUE = deque()

def an_item_is_available():
    return bool(QUEUE)

def get_an_available_item():
    return QUEUE.popleft()

def make_an_item_available(item):
    QUEUE.append(item)

def consume(cv):
    cv.acquire()
    while not an_item_is_available():
        cv.wait()
    print 'We got an available item', get_an_available_item()
    cv.release()

def produce(cv):
    cv.acquire()
    make_an_item_available('an item to be processed')
    cv.notify()
    cv.release()

def main():
    cv = Condition()
    Thread(target=consume, args=(cv,)).start()
    Thread(target=produce, args=(cv,)).start()

if __name__ == '__main__':
    main()

rawDescription: Is there a python library for notification and waiting?


code:
#import itertools
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import random

#Generate a list of 5000 int between 1200,5500
M = 5000
myList = [random.randrange(1200,5500)  for i in xrange(0,M)]

#Convert to 50 x 100 list
n = 50
newList = [myList[i:i+n] for i in range(0, len(myList), n)]

#Convert to 50 x 100 numpy array
nArray = array(newList)
print nArray

a11=nArray.reshape(50,100)
plt.imshow(a11, cmap='hot')
plt.colorbar()
plt.show()

rawDescription: Matplotlib,how to represent array as image?


code:
import maya.cmds as cmds
import pymel.core as pm

class WindowWatcher():
    """ A class to watch for a particular window in Maya """

    def __init__(self, window_name, on_open_callback, on_close_callback=None):
        self.window_name = window_name
        self.on_open_callback = on_open_callback
        self.on_close_callback = on_close_callback
        self.window_opened = False

    def check_for_window_open(self):
        if not self.window_opened:
            if self.window_name in cmds.lsUI(windows=True):
                self.on_open_callback.__call__()
                self.window_opened = True
        else:
            if not self.window_name in cmds.lsUI(windows=True):
                self.window_opened = False
                if self.on_close_callback:
                    self.on_close_callback.__call__()

if __name__ == "__main__":
    # demo

    render_window_name = "renderViewWindow"
    def on_open_render_window(arg1, arg2):
        # your on_window_open code here
        print "Render Window opened!"
        print "Arg1: %s   Arg2: %s" % (arg1, arg2)

    script_editor_name = "scriptEditorPanel1Window"
    def on_open_script_editor():
        # your on_window_open code here
        print "Script Editor opened!"

    render_window_watcher = WindowWatcher(render_window_name,
                                          pm.windows.Callback(on_open_render_window, "Hello", "World")
                                          )
    script_editor_watcher = WindowWatcher(script_editor_name, on_open_script_editor)

    cmds.scriptJob(event=["idle",
                          pm.windows.Callback(render_window_watcher.check_for_window_open)])
    cmds.scriptJob(event=["idle",
                          pm.windows.Callback(script_editor_watcher.check_for_window_open)])

rawDescription: Maya Window UI create Event?

description: Maya Window UI create Event


code:
import weakref

def foo():
    print "It Works!"

def func():
    return weakref.ref(foo)

ref = func() # func returns the reference to the foo() function
original_func = ref() # calling the reference returns the referenced object
original_func() # prints "It Works!"

rawDescription: Calling Python functions passed by weakref


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import collections
from sqlalchemy.orm.properties import RelationshipProperty

class memoized_classproperty(object):
    """A decorator that evaluates once at the class level,
       assigns the new value to the class.
    """

    def __init__(self, fget, doc=None):
        self.fget = fget
        self.__doc__ = doc or fget.__doc__
        self.__name__ = fget.__name__

    def __get__(desc, self, cls):
        result = desc.fget(cls)
        setattr(cls, desc.__name__, result)
        return result

class Lens(object):
    @memoized_classproperty
    def pk_columns(cls):
        return class_mapper(cls).primary_key

    @memoized_classproperty
    def relations(cls):
        props = collections.namedtuple('RelationshipItem', 'one many')(set(), set())
        # 0.8 will have "inspect(cls).relationships" here
        mapper = class_mapper(cls)
        for item in mapper.iterate_properties:
            if isinstance(item, RelationshipProperty):
                if item.direction.name == ('MANYTOONE' or 'ONETOONE'):
                    local_column = mapper.get_property_by_column(item.local_side[0]).key
                    props.one.add((local_column, item.key))
                else:
                    props.many.add(item.key)
        return props

Base= declarative_base(cls=Lens)

meetings_topics = Table("meetings_topics", Base.metadata,
    Column('topic_id', Integer, ForeignKey('topic.id')),
    Column('meetings_id', Integer, ForeignKey('meetings.id')),
)
class Meeting(Base):
    __tablename__ = 'meetings'
    def __init__(self, memo):
        self.memo = memo
    id = Column(Integer, primary_key=True)
    date = Column(Date)
    memo = Column('note', String(60), nullable=True)
    category_name = Column('category', String(60), ForeignKey('categories.name'))
    category = relationship("Category", backref=backref('meetings'))
    topics = relationship("Topic",
        secondary=meetings_topics,
        backref="meetings")

class Category(Base):
    __tablename__ = 'categories'
    name = Column(String(50), primary_key=True)

class Topic(Base):
    __tablename__ = 'topic'
    id = Column(Integer, primary_key=True)

print Meeting.pk_columns
print Meeting.relations.one

# assignment is OK, since prop is memoized
Meeting.relations.one.add("FOO")

print Meeting.relations.one

rawDescription: How can I 'index' SQLAlchemy model attributes that are primary keys and relationships


code:
import nltk
# Define the cfg grammar.
grammar = nltk.parse_cfg("""
S -> NP VP
NP -> 'DT' 'NN'
VP -> 'VB'
VP -> 'VB' 'NN'
""")

# Make your POS sentence into a list of tokens.
sentence = "DT NN VB NN".split(" ")

# Load the grammar into the ChartParser.
cp = nltk.ChartParser(grammar)

# Generate and print the nbest_parse from the grammar given the sentence tokens.
for tree in cp.nbest_parse(sentence):
    print tree

rawDescription: CFG using POS tags in NLTK

description: CFG using POS tags in NLTK


code:
for k, v in dict.iteritems():
        for item in v:
            print item, k

rawDescription: Python dict print One per line with Key in each line


code:
from dateutil import parser

def reduce_string(string):
    i = len(string) - 1
    while string[i] >= '0' and string[i] < '9':
        i -= 1
    while string[i] < '0' or string[i] > '9':
        i -= 1
    return string[:i + 1]

def find_date(string):
    while string:
        try:
            dateString = parser.parse(string, fuzzy=True)
            year = dateString.year
            month = dateString.month
            day = dateString.day
            return (year, month, day)
        except ValueError:
            pass

        string = reduce_string(string)

    return None

date = find_date('2007-09-10_00005')
if date:
    print date
else:
    print "can't decode"

rawDescription: Python dateutil parser, ignore non-date part of string


code:
raw = """{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:'xxxx\r\n:xxxx'}"""

def parse(raw):
    # split into chunks by '}{' and remove the outer '{}'
    parts = raw[1:-1].split('}{')
    for part in parts:
        # split by the first ':'
        num, data = part.split(':', 1)
        # yield each entry found
        yield int(num), data

# make a dict from it
print dict(parse(raw))

rawDescription: splitting a braces grouped string in python

description: Splitting a braces grouped string in python


code:
import numpy as np
from scipy.spatial import distance

m = 100
n = 200
X = np.random.randn(m, n)

d = distance.pdist(X, metric='jaccard')
print(d.shape)
# (4950,)

D = distance.squareform(d)
print D.shape
# (100, 100)

rawDescription: Get indices of results from scipy.pdist(myArray,metric="jaccard") to map back to original array?


code:
value = 1e-7
print value
print '%f' % (value)
print '%s' % (value)

rawDescription: Python insert values <= 1e-7 in a MySQL database


code:
class Parent(object):
    def __init__(self):
        pass
    def Create(self):
        return 'Password'

class Child(Parent):
    def __init__(self):
        self.Create()
    def Create(self):
        return 'The ' + super(Child, self).Create()

print Child().Create() # prints "The Password"

rawDescription: Overriding in Python

description: Overriding in Python


code:
from bs4 import BeautifulSoup
xml = "<TestTag>a string</TestTag>"
soup = BeautifulSoup(xml, "xml")
print soup

rawDescription: How to maintain case-sensitive tags in BeautifulSoup.BeautifulStoneSoup?

description: Maintain case-sensitive tags in BeautifulSoup.BeautifulStoneSoup


code:
html_list = self.driver.find_element_by_id("myId")
items = html_list.find_elements_by_tag_name("li")
for item in items:
    text = item.text
    print text

rawDescription: How to get a list of the <li> elements in an <ul> with Selenium in Python?

description: Get a list of the <li> elements in an <ul> with Selenium in Python


code:
import module_1

class Card(object):
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.index = [suit, number]

if __name__ == '__main__':
    # Keep reference to the original Card class.
    original_card = module_1.Card
    # Replace with my custom Card class.
    module_1.Card = Card
    new_deck = module_1.Deck()
    print new_deck.cards[0]
    # Restore.
    module_1.Card = original_card

rawDescription: How can I override a class definition in python?

description: Override a class definition in python


code:
import uu

uu.encode("Bar.txt", "Hello.txt")

import StringIO

output = StringIO.StringIO()
uu.encode("Bar.txt", output)
print output.getvalue()

rawDescription: Python - how to save uu.encode() output to a variable


code:
s = 'CRIT   :  [2013-07-26T14:40:50-0400] msg=<POLICY ADDED>,'
import re
print re.match(r'(\w+).*?msg=<(.*?)>', s).groups()
# ('CRIT', 'POLICY ADDED')

rawDescription: Trouble with parsing a .txt file for specific strings in Python


code:
MyLocation = 'DE'
location_score = { MyLocation : 1 }
that_other_field_score = {'PRF1' : 3, 'PRF2' : 2, 'PRF3' : 1}

def score( row ):
    # returns a tuple of item score
    # items not in the score dicts get score 0 for that field
    return ( that_other_field_score.get(row[3], 0),
                  location_score.get(row[4], 0))

data = [
('Pencils', 'Artists Pencils', 18.95, 'PVT', 'DE'),
('Pencils', '', 19.95, 'PVT', 'IT'),
('Pencils', '', 23.50, 'PRF1', 'US'),
('Pencils', 'Wooden Pencils', 23.50, 'PRF2', 'DE'),
('Pencils', '', 12.50, 'NON', 'DE')]

# sort data, highest score first
data.sort(key=score, reverse=True)
print data

rawDescription: Sorting iterables based on preferred order of strings


code:
#!/usr/bin/python
import rpy2.robjects as robjects

l = [1,2,3,4,5]

# get ks.test via execute string as R statement
test = robjects.r('ks.test')
# get a built-in functions variables directly
pexp = robjects.r.pexp

l_vector = robjects.IntVector(l)
result = test(l_vector, pexp)
print result[result.names.index('p.value')]

rawDescription: How to pass a list to R in rpy2 and get result back

description: Pass a list to R in rpy2 and get result back


code:
import csv

records = []
reader = csv.reader(open('source.csv', 'rb'))

for record in reader:
    print record
    records.append(record)

rawDescription: Python New Line confusion in a CSV file

description: Python New Line confusion in a CSV file


code:
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
from email.mime.image import MIMEImage

mime_root = MIMEMultipart('related', '===============xxxxxxxxxxxxx==')
headers= {'Content-Type': 'multipart/related; '
          'boundary="%s"' % mime_root.get_boundary(),
          'Authorization':'Bearer %s' % access_token}
setattr(mime_root, '_write_headers', lambda self: None)
#Create the metadata part of the MIME
mime_text = MIMENonMultipart(*['application','json'])
mime_text.set_payload("{'text':'waddup doe!'}")
print "Attaching the json"
mime_root.attach(mime_text)

if method == 'Image':
    #DO Image
    file_upload = open('555.jpg', 'rb')
    mime_image = MIMENonMultipart(*['image', 'jpeg'])
    #add the required header
    mime_image['Content-Transfer-Encoding'] = 'binary'
    #read the file as binary
    mime_image.set_payload(file_upload.read())
    print "attaching the jpeg"
    mime_root.attach(mime_image)

elif method == 'Video':
    mime_video = MIMENonMultipart(*['video', 'vnd.google-glass.stream-url'])
    #add the payload
    mime_video.set_payload('https://dl.dropboxusercontent.com/u/6562706/sweetie-wobbly-cat-720p.mp4')
    mime_root.attach(mime_video)

rawDescription: Multipart POST request Google Glass

description: Multipart POST request Google Glass


code:
import re
p = re.compile("<br>\((\d+%)\)<\/TD>", re.IGNORECASE)
for m in p.finditer(html_code):
    print m.start(), m.group()

rawDescription: How can I only take the regex without using re.split()?

description: Only take the regex without using re.split()


code:
def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

f_dic = {'a':'d'}
s_dic = {'1':'x'}

with open('file1.csv','r') as f:
    text=f.read()
    with open('file2.csv','w') as w:
        text=replace_all(text,f_dic)
        text=replace_all(text,s_dic)
        print text
        w.write(text)

rawDescription: Replace multiple strings in csv file

description: Replace multiple strings in csv file


rawDescription: For each "sub item" in json array, get name, then the value for a sub item


code:
import re
import subprocess
import cv2
import os

device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)
df = subprocess.check_output("lsusb", shell=True)
for i in df.split('\n'):
    if i:
        info = device_re.match(i)
        if info:
            dinfo = info.groupdict()
            if "Logitech, Inc. Webcam C270" in dinfo['tag']:
                print "Camera found."
                bus = dinfo['bus']
                device = dinfo['device']
                break

device_index = None
for file in os.listdir("/sys/class/video4linux"):
    real_file = os.path.realpath("/sys/class/video4linux/" + file)
    print real_file
    print "/" + str(bus[-1]) + "-" + str(device[-1]) + "/"
    if "/" + str(bus[-1]) + "-" + str(device[-1]) + "/" in real_file:
        device_index = real_file[-1]
        print "Hurray, device index is " + str(device_index)

camera = cv2.VideoCapture(int(device_index))

while True:
    (grabbed, frame) = camera.read() # Grab the first frame
    cv2.imshow("Camera", frame)
    key = cv2.waitKey(1) & 0xFF

rawDescription: Create openCV VideoCapture from interface name instead of camera numbers


code:
import numpy as np

def calculate_angle(point_a, point_b):
    """ Calculate angle between two points """
    ang_a = np.arctan2(*point_a[::-1])
    ang_b = np.arctan2(*point_b[::-1])
    return np.rad2deg((ang_a - ang_b) % (2 * np.pi))

a = np.array([14, 140])
b = np.array([13, 120])
c = np.array([12, 130])
d = np.array([11, 110])

# create vectors
ba = a - b
bc = c - b
cd = d - c

# calculate angle
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))

angle = np.arccos(cosine_angle)
inner_angle = np.degrees(angle)

print inner_angle  # 8.57299836361

# see how changing the direction changes the angle
print calculate_angle(bc, cd) # 188.572998364
print calculate_angle(cd, bc) # 171.427001636

rawDescription: calculating angle between three points but only anticlockwise in python

description: Calculating angle between three points but only anticlockwise in python


code:
from operator import itemgetter
print "Original List"
print '\n'.join(str(l) for l in phonemelist)
grp = itertools.groupby(phonemelist, itemgetter(1))
res = [tuple(v) for k, v in grp if k]
print '\n'.join(str(l) for l in res)
newlists = []
# for each part between the markers
for item in res:
    # update the ranges and add it to the overall list
    newlists.append(update_ranges(item))
print "\n after applying co-articulation rules"
print '\n\n'.join('\n'.join(str(i) for i in l) for l in newlists)

rawDescription: working inside the boundaries denoted by 'None' inside a list

description: Working inside the boundaries denoted by 'None' inside a list


code:
import csv
import re

sample = """stop_id,on_street,cross_street,routes,boardings
49,HARRISON,PAULINA,"126, 755",1.6
50,ASHLAND,CONGRESS,"9,126",14.8
51,ASHLAND,VAN BUREN,"9,126",100.9
52,JACKSON,1900 W.(MALCOLM X COLL.),126,82.8"""

open('sample.csv','w').write(sample)

with open('sample.csv') as sample, open('output.csv','w') as output:
    reader = csv.reader(sample)
    writer = csv.writer(output)
    # discard input header
    next(reader)
    # write output header
    writer.writerow(['stop_id', 'route'])
    # process rows
    for row in reader:
        if row:
            for route in re.split(r', *', row[3].replace('"', '')):
                writer.writerow([row[0], route])

print open('output.csv').read()

rawDescription: Data normalization with Python

description: Data normalization with Python


code:
def reformat_html_id_table(line):
    htmlTable = '
\n'
    mod = 9
    for i in range(0, len(line)):
        if (i > mod and i%mod == 5):
            htmlTable = htmlTable + '\t\t 	\n'
        if (i-9) % mod == 0:
            if i == 0:
                htmlTable = htmlTable + '\t
\n'
            else:
                htmlTable = htmlTable + '\t
\n\t
\n'
            htmlTable = htmlTable + '\t\t{0}	\n'.format(line[i])
        else:
            htmlTable = htmlTable + '\t\t{0}	\n'.format(line[i])
        if i==9:
            mod=8
    htmlTable = htmlTable + '\t
\n
'
    print htmlTable

rawDescription: Make HTML Table from List

description: Make HTML Table from List


rawDescription: Sort list of lists based on a dict values as key


code:
n = 0
with open('command.txt', 'r') as file:
    for line in file:
        word = line.split()
        print word
        n += 1
print n

rawDescription: Getting the number of "lists" returned by split() in python


code:
s = u"Hey I\u2019m Bob"
print s
print s.encode('utf8')
import sys
sys.stdout.encoding
print s.encode('cp1252')

rawDescription: Python 2.7 Unicode/IDLE confusion

description: Python 2.7 Unicode/IDLE confusion


code:
def myRandom():
    myOutput = input("How many suggestions would you like?")
    myDict = {
    "The First":1988,
    "The Second:": 1992,
    "The Third": 1974,
    "The Fourth": 1935}
    outList = random.sample(myDict, myOutput)
    print "These are your suggestions: {0}".format(outList)

rawDescription: Python random.sample same as random.choice with IF Statement


code:
import win32com.client

word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open(r"C:\temp\foo.doc")
if doc.SpellingErrors.Count:
    for err in doc.SpellingErrors:
        print err.Text
else:
    print "No errors"
word.Quit()
word = None

rawDescription: Get Incorrect Spelled words from microsoft word


code:
import re

line = [u'x', u'y', u'z', u'쭌', u'a']

if any([re.search(u'[\u3131-\ucb4c]', x) for x in line[3:]]):
    print "found character"

rawDescription: Use search with regex to find Korean characters using Python


code:
from collections import defaultdict

datasets = [{'foo': 3}, {'bar': 4}], [{'bar': 1}, {'foo': 8}]

result = defaultdict(list)

for dataset in datasets:
    for eachdict in dataset:
        for key, value in eachdict.iteritems():
            result[key].append(value)

# the nested loops as a one-liner (utterly unnecessary):
# any(result[key].append(value) for dataset in datasets
#                               for eachdict in dataset
#                               for key, value in eachdict.iteritems())

# if you really need your output in exactly that format:
# result = [(key,) + tuple(values) for key, values in result.iteritems()]
print result

rawDescription: Create (k, v1, ..., vn) tuples based on matching dict keys


code:
i = child.expect([".*\$", "Password Incorrect"])
if i != 0:
    print "Incorrect credentials"
else:
    print "Command executed correctly"

rawDescription: how to check if ssh command ran through pexpect spawn command ran successfully or not .

description: Check if ssh command ran through pexpect spawn command ran successfully or not .


code:
import urllib
from HTMLParser import HTMLParser

base_url = 'http://127.0.0.1/'

class ImgParser(HTMLParser):
    def __init__(self, *args, **kwargs):
        self.downloads = []
        HTMLParser.__init__(self, *args, **kwargs)

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            for attr in attrs:
                if attr[0] == 'src':
                    self.downloads.append( attr[1] )

imgp = ImgParser()
with open('test.html') as f:
    # instead you could feed it the original url obj directly
    imgp.feed(f.read())

for path in imgp.downloads:
    url = base_url + path
    print url
    urllib.urlretrieve(url, path)

rawDescription: How to retrieve a webpage in python, including any images

description: Retrieve a webpage in python, including any images


code:
import zipfile
z = zipfile.ZipFile('c:/a_zip_file')
print 'total files size=', sum(e.file_size for e in z.infolist())
z.close()

rawDescription: How can I protect myself from a zip bomb?

description: Protect myself from a zip bomb


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

# Define some test data which is close to Gaussian
data = numpy.random.normal(size=10000)

hist, bin_edges = numpy.histogram(data, density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2

# Define model function to be used to fit to the data above:
def gauss(x, *p):
    A, mu, sigma = p
    return A*numpy.exp(-(x-mu)**2/(2.*sigma**2))

# p0 is the initial guess for the fitting coefficients (A, mu and sigma above)
p0 = [1., 0., 1.]

coeff, var_matrix = curve_fit(gauss, bin_centres, hist, p0=p0)

# Get the fitted curve
hist_fit = gauss(bin_centres, *coeff)

plt.plot(bin_centres, hist, label='Test data')
plt.plot(bin_centres, hist_fit, label='Fitted data')

# Finally, lets get the fitting parameters, i.e. the mean and standard deviation:
print 'Fitted mean = ', coeff[1]
print 'Fitted standard deviation = ', coeff[2]

plt.show()

rawDescription: Fit a gaussian function

description: Fit a gaussian function


code:
def __getattr__(self, name):
    print "before"
    attr = getattr(self.tf, name)
    print "after"
    return attr

rawDescription: XMLRPC server functions wrapper

description: XMLRPC server functions wrapper


code:
import sys
import StringIO
inputfile=sys.argv[1]

buffer = StringIO.StringIO()

# buffering
with open(inputfile, 'r') as f:
    buffer.write(f.read())

# use it
buffer.seek(0)
for line in buffer:
    print line

# use it again
buffer.seek(0)
for line in buffer:
    print line

rawDescription: Use process substitution as input file to Python twice


rawDescription: python statistic top 10

description: Python statistic top 10


code:
from subprocess import PIPE
import subprocess
pro = subprocess.Popen("ifconfig", stdout=PIPE, stderr=PIPE)
data = pro.communicate()[0].split()
for line in data:
    print "THIS IS A LINE"
    print line
    print "**************"

rawDescription: Need to get output as lists rather than strings in Popen or any other system commands


code: print '{0} times {1} is equal to {2}'.format(a, b, iterMul(a,b))

rawDescription: is the print statement correct?

description: Is the print statement correct


code:
import threading

def poll_position(fobj, seconds=0.5):
    """Call once to repeatedly get statistics every N seconds."""
    position = getMotorPosition()

    # Do something with the position.
    # Could store it in a (global) variable or log it to file.
    print position
    fobj.write(position + '\n')

    # Set a timer to run this function again.
    t = threading.Timer(seconds, poll_position, args=[fobj, seconds])
    t.daemon = True
    t.start()

def control_loop(positions, velocity):
    """Repeatedly moves the motor through a list of positions at a given velocity."""
    while True:
        for position in positions:
            moveMotorToPosition(position, velocity)

if __name__ == '__main__':
    # Start the position gathering thread.
    poll_position()
    # Define `position` and `velocity` as it relates to `moveMotorToPosition()`.
    control_loop([first_position, second_position], velocity)

rawDescription: Parallel while loops in python for motor control and data acquisition


code:
import urllib2

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp,
                                                                 code, msg,
                                                                 headers)
        result.status = code
        result.headers = headers
        return result

request = urllib2.Request("http://wp.me")
opener = urllib2.build_opener(SmartRedirectHandler())
obj = opener.open(request)
print 'The original headers where', obj.headers
print 'The Redirect Code was', obj.status

rawDescription: How do I access the original response headers that contain a redirect when using urllib2.urlopen

description: Access the original response headers that contain a redirect when using urllib2.urlopen


code:
import networkx as nx

G = nx.Graph()
G.add_node(1,color='red')
G.add_node(2,color='red')
G.add_node(3,color='blue')
G.add_node(4,color='blue')

G.add_edges_from([(1,2),(1,3),(3,4)])

for (u,v) in G.edges_iter():
    if G.node[u]['color'] != G.node[v]['color']:
        print u,v

rawDescription: finding edges in networkx that meet a certain criteria

description: Finding edges in networkx that meet a certain criteria


code:
B=[0,1,0]

def check(ll,callback):
    """
    This function only works if you
    increment the last element in the list.
    All other incrementing is done in this function.
    """
    for idx in reversed(range(len(ll))):
        if(callback(ll)):
            ll[idx]=0
            ll[idx-1]+=1
        else:
            break  #this index wasn't updated, so the next one won't be either.

    #a check to see if every element is 1
    return all(map(lambda x: x==1,ll))

def checksum(ll):
    return True if sum(ll)>100 else False

count=0
while True:
    count+=1
    B[-1]+=1
    if(check(B,checksum)): break
    print B

print B   # [1,1,1]
print count

rawDescription: Creating a conditional iterative list

description: Creating a conditional iterative list


code:
def try_login(username, password):
    params = urllib.urlencode(
            {'username': username,
             'password': password })
    f = urllib.urlopen("http://ninitelist.yolasite.com/passprotected.php", params)
    return "Invalid login details" not in f.read()

if __name__ == '__main__':
    print try_login("testuser", "testdata")

rawDescription: Python to fill out web forum

description: Python to fill out web forum


code:
import math

example_triangle = [[0,0], [5,5], [0,5], [0,0]]
high_tide_line = [[0, 0], [5., 1.5], [10., 3.2], [20., 1.], [30., 4.5], [80.,2.], [80,0]]
low_tide_line = [[0, 10.], [5., 11.5], [10., 13.2], [20., 11.], [30., 14.5], [80., 12.], [80, 10]]

def points_from_geom(geom):
    idx = 0
    line_lengths = []
    unit_vectors = []
    interpolated_points = []
    while idx < (len(geom) - 1):
        dy, dx = ((geom[idx+1][1] - geom[idx][1]), (geom[idx+1][0] - geom[idx][0]))
        line_lengths.append(math.sqrt(dy**2 + dx**2))
        try:
            angle = math.atan(dy/dx)
            unit_vectors.append([math.cos(angle)*cmp(dx, 0),
                math.sin(angle)*cmp(dy, 0)])
        except ZeroDivisionError:
            if geom[idx+1][1] < geom[idx][1]:
                direction = [0, -1]
            else:
                direction = [0, 1]
            unit_vectors.append(direction)
        idx += 1

    for i, length in enumerate(line_lengths):
        inter = 0
        while inter <= length:
            interpolated_points.append([geom[i][0] + unit_vectors[i][0]*inter,\
                geom[i][1] + unit_vectors[i][1]*inter])
            inter += .3048 # a ft in proper units ;)

    return interpolated_points

ln1 = points_from_geom(example_triangle)
ln2 = points_from_geom(high_tide_line)
ln3 = points_from_geom(low_tide_line)

print ln1, ln2, ln3

rawDescription: Python, GIS and Fortran: Trying to create multiple polygons from xy point data


code:
workdir = '/Volumes/place/sub place'

def myFunc(_, dir, flist):
    for f in flist:
        fpath = os.path.join(dir, f) # need to make a full path first
        print f, fpath, os.path.isfile(fpath)

os.path.walk(workdir,myFunc,None)

rawDescription: why does python os.path.isfile seem to ignore certain file types?

description: Why does python os.path.isfile seem to ignore certain file types


code:
def in_string(digits, number):
    if not number:    # no more digits in number?
        return False  # all done
    digit = number[0]     # get the leading digit
    number = number[1:]   # leave the remaining digits in number
    if digit == digits[0]:    # found a digit in the sequence?
        digits = digits[1:]   # leave the remaining digits
        if not digits:        # no more digits in sequence?
            return True       # all done
    return in_string(digits, number)    # recursion

c = 0
for i in range(10000):
    if in_string("34", str(i)):
        c += 1
print c

rawDescription: Occurances of Num2 after Num1 in range(1,n) in Python with RECURSION


rawDescription: accessing a set of values from a dictionary

description: Accessing a set of values from a dictionary


code:
class MyIterator:
    cur_word = ''
    def parse(self):
        data = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
        for index in range(1,3):
            (word, num) = data[index]
            self.cur_word = word
            yield self.unique_str(num)

    def unique_str(self, num):
        data = ['a', 'b']
        for d in data:
            yield "%s-%d-%s" % (self.cur_word, num, d)

miter = MyIterator()
parse = miter.parse()
for ustrs in parse:
    for ustr in ustrs:
        print ustr

rawDescription: Instance member data is not passed to instance method


code:
# instead of if float1==float2:, use this:
if abs(float1-float2) <= sys.float_info.epsilon:
    print "equal"

rawDescription: Python: read mixed float and string csv file


code:
p = "0(532) 222 22 22"
print ''.join([x for x in p if x.isdigit()])

rawDescription: Python, a smarter way of string to integer conversion


code:
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='hostname', username='username', password='password')

channel = client.get_transport().open_session()
command = "import sys; sys.stdout.write('stdout message'); sys.stderr.write(\'stderr message\'); sys.exit(22)"
channel.exec_command('/usr/bin/python -c "%s"' % command)
channel.shutdown_write()

stdout = channel.makefile().read()
stderr = channel.makefile_stderr().read()
exit_code = channel.recv_exit_status()

channel.close()
client.close()

print 'stdout:', stdout
print 'stderr:', stderr
print 'exit_code:', exit_code

rawDescription: how to execute python or bash script through ssh connection and get the return code

description: Execute python or bash script through ssh connection and get the return code


code:
B = [5,-4,5,-6]
y = [
        [0,1,0,1],
        [0,0,0,0],
        [0,0,0,1],
        [0,0,0,0],
    ]
x = []
for i, row in enumerate(y):
    temp = []
    for j, col in enumerate(row):
        if col != 0:
            temp.append(str(col) + '*x' + str(i) + str(j))
        else:
            temp.append(col)
    x.append(temp)

#for one in x:
#    print one

equ = []
for i in xrange(4):
    temp1 = []
    temp2 = []
    for j in xrange(4):
        temp1.append(x[i][j])
        temp2.append(x[j][i])
    temp2.append(B[i])
    equ.append(tuple(temp1 + temp2))

equtions = []
for one in equ:
    s = '%s + %s + %s + %s - %s - %s - %s - %s = %s' % one
    equtions.append(s)

for one in equtions:
    print one

import re
from copy import deepcopy

equ_bak = deepcopy(equtions)

p_var = re.compile(r'x\d\d')
vars = set([])
for one in equ_bak:
    m = p_var.findall(one)
    vars |= set(m)
vars = sorted(list(vars))

p_ef = re.compile(r'([+-]* *\d*)\*(x\d\d)')
effs = []
for one in equ_bak:
    m = p_ef.findall(one)
    #print m
    temp = [0] * len(vars)
    for num, var in m:
        try:
            temp[vars.index(var)] = float(num.replace(' ', ''))
        except:
            pass
    effs.append(tuple(temp))

#for one in effs:
#    print one

import numpy as np
A = np.array(effs)
x = np.linalg.lstsq(A,B)
print vars
print x[0]

rawDescription: How to write program run matrix as below in python?

description: Write program run matrix as below in python


code:
from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
import itertools

def Synonym_Checker(word1, word2):
    """Checks if word1 and word2 and synonyms. Returns True if they are, otherwise False"""
    equivalence = WordNetLemmatizer()
    word1 = equivalence.lemmatize(word1)
    word2 = equivalence.lemmatize(word2)

    word1_synonyms = wordnet.synsets(word1)
    word2_synonyms = wordnet.synsets(word2)

    scores = [i.wup_similarity(j) for i, j in list(itertools.product(word1_synonyms, word2_synonyms))]
    max_index = scores.index(max(scores))
    best_match = (max_index/len(word1_synonyms), max_index % len(word1_synonyms)-1)

    word1_set = word1_synonyms[best_match[0]].lemma_names
    word2_set = word2_synonyms[best_match[1]].lemma_names
    match = False
    match = [match or word in word2_set for word in word1_set][0]

    return match

print Synonym_Checker("tomato", "Lycopersicon_esculentum")

rawDescription: General synonym and part of speech processing using nltk


code:
import wx

########################################################################
class MyApp(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Example")
        panel = wx.Panel(self)

        lbl = wx.StaticText(panel, label="I'm a label!")
        txt = wx.TextCtrl(panel, value="blah blah")
        btn = wx.Button(panel, label="Clear")
        btn.Bind(wx.EVT_BUTTON, self.onClear)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(lbl, 0, wx.ALL, 5)
        self.sizer.Add(txt, 0, wx.ALL, 5)
        self.sizer.Add(btn, 0, wx.ALL, 5)

        panel.SetSizer(self.sizer)

    #----------------------------------------------------------------------
    def onClear(self, event):
        """"""
        children = self.sizer.GetChildren()

        for child in children:
            widget = child.GetWindow()
            print widget
            if isinstance(widget, wx.TextCtrl):
                widget.Clear()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyApp()
    frame.Show()
    app.MainLoop()

rawDescription: wxPython: How to get sizer from wx.StaticText?

description: WxPython: How to get sizer from wx.StaticText