code:
from sqlalchemy import create_engine, Column, Integer, String, Table, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, joinedload

engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base(bind=engine)

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

    following = relationship(
        'User', lambda: user_following,
        primaryjoin=lambda: User.id == user_following.c.user_id,
        secondaryjoin=lambda: User.id == user_following.c.following_id,
        backref='followers'
    )

    def __str__(self):
        return self.name

    def __repr__(self):
        return '<User {0}>'.format(self)

user_following = Table(
    'user_following', Base.metadata,
    Column('user_id', Integer, ForeignKey(User.id), primary_key=True),
    Column('following_id', Integer, ForeignKey(User.id), primary_key=True)
)

Base.metadata.create_all()

u2 = User(name='jacob')
u3 = User(name='james')
u4 = User(name='victor')

u1 = User(name='david', followers=[u2, u3, u4])

session.add_all([u1, u2, u3, u4])
session.commit()

print u1.followers  # [<User jacob>, <User james>, <User victor>]
print u1.followers[0].following  # [<User david>]

rawDescription: How to implement this has-many-through relatinship in SQLAlchemy

description: Implement this has-many-through relatinship in SQLAlchemy


code:
class WSHandler(tornado.websocket.WebSocketHandler):
    clients = []

    def open(self):
        self.clients.append(self)
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
        self.clients.remove(self)
        print 'closed connection'

rawDescription: Tornado: Identify / track connections of websockets?


code:
if pattern.match(lword):
   if len(lword) >= 10:
       print '%s%s%d' % (lword, "\t", 1)
   elif len(lword) in [7, 8, 9] :
       print '%s%s%d' % (lword, "\t", 1)
   elif len(lword) in [4, 5, 6] :
       print '%s%s%d' % (lword, "\t", 1)
   else: # lword is between one and three characters long
       print '%s%s%d' % (lword, "\t", 1)

rawDescription: Python String length to find character

description: Python String length to find character


code:
list  = [9,15,1,4,2,3,6]
result = 10
list.sort()
list = filter(lambda x:x < result,list)
myMap = {}

for i in list:
    if i in myMap:
        print myMap[i], i
        break
    myMap[result - i] = i

rawDescription: most efficient way to find a sum of two numbers

description: Most efficient way to find a sum of two numbers


code:
Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable" + variable + "\JonDoFoxPortable.exe", "https://www.amazon.com/gp/sign-in.html"], )

Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable%a\JonDoFoxPortable.exe"%variable, "https://www.amazon.com/gp/sign-in.html"], )

rawDescription: How to put a variable into this string in Python?

description: Put a variable into this string in Python


code:
# all subsets of k or less elements of 1..n
def subsets4(n,k):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n:
      i -= 1

# all subsets of k or less elements of 1..n with max difference <= w
def subsets5(n,k,w):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n and r-a[0] <= w:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n or a[i]+1-a[0] > w:
      i -= 1

# all subsets of 1..n having exactly k elements
def subsets(n,k):
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    # find i
    i = k-1
    while i >= 0 and a[i]+k-i >= n+1: i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

# all subsets of 1..n having exactly k elements and whose max
# difference is w
def subsets2(n,k,w):
  if k > w: return
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    i = k-1
    while i >= 0 and (a[i]+k-i >= n+1 or a[i]+k-i-a[0] > w) : i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

def test(n,k,w):
  s1 = [ s for s in subsets4(n,k) if s[-1] - s[0] <= w ]
  s2 = [ s for s in subsets5(n,k,w) ]
  if s1 == s2:
    print "OK", n,k,w
    return 0
  else:
    print "NOT OK", n, k, w
    return 1

# for s in subsets2(10,3,4): print s
# for s in subsets(10,3): print s
def megatest():
  failed = 0
  for n in xrange(10,20):
    for k in xrange(1,n+1):
      for w in xrange(k,n+1):
        failed += test(n,k,w)
  print "failed:", failed

rawDescription: Fast combinatoric generator in Python

description: Fast combinatoric generator in Python


code:
import pygtk
pygtk.require('2.0')
import gtk
import urllib2

class MainWin:

    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.image=gtk.Image()
        response=urllib2.urlopen(
            'http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')
        loader=gtk.gdk.PixbufLoader()
        loader.write(response.read())
        loader.close()
        self.image.set_from_pixbuf(loader.get_pixbuf())
        # This does the same thing, but by saving to a file
        # fname='/tmp/planet_x.jpg'
        # with open(fname,'w') as f:
        #     f.write(response.read())
        # self.image.set_from_file(fname)
        self.window.add(self.image)
        self.image.show()
        self.window.show()

    def main(self):
        gtk.main()

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

rawDescription: How to display an image from web?

description: Display an image from web


code:
#!/usr/bin/python

d = { 'case1' : [18, 17, 497, 298, 57, 82],
      'case2': [128, 184, 497,298,57,82],
      'case3':[104,2828,3881, 497, 38],
      'case4': [392, 497, 573]
      }

k = d.values()
# This sort will bring the shortest list in the front
# will loop base on the shortest list.(optimization)
k.sort(key = lambda s: len(s))

def find_first_common_number(k):
    # lets loop base on the shortest list.
    # k[0] is the first list
    for y in k[0]:
       if all([y in x for x in k[1:]]):
           return y
    return False
print find_first_common_number(k)

rawDescription: How to find the first common number from a dictionary of lists

description: Find the first common number from a dictionary of lists


code:
from PyQt4 import QtCore, QtGui

class DragButton(QtGui.QPushButton):

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()

        super(DragButton, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            # adjust offset from clicked point to origin of widget
            currPos = self.mapToGlobal(self.pos())
            globalPos = event.globalPos()
            diff = globalPos - self.__mouseMovePos
            newPos = self.mapFromGlobal(currPos + diff)
            self.move(newPos)

            self.__mouseMovePos = globalPos

        super(DragButton, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos
            if moved.manhattanLength() > 3:
                event.ignore()
                return

        super(DragButton, self).mouseReleaseEvent(event)

def clicked():
    print "click as normal!"

if __name__ == "__main__":
    app = QtGui.QApplication([])
    w = QtGui.QWidget()
    w.resize(800,600)

    button = DragButton("Drag", w)
    button.clicked.connect(clicked)

    w.show()
    app.exec_()

rawDescription: Dragging/Moving a QPushButton in PyQt

description: Dragging/Moving a QPushButton in PyQt


code:
from django.db import connection
cursor = connection.cursor()
cursor.execute("SHOW TABLE STATUS WHERE name='appname_modelname'")
print cursor.fetchone()

rawDescription: Django: get table engine from model

description: Django: get table engine from model


code:
from Queue import Queue

results = Queue()

def upload_to_s3(filepath, unique_id):
    # do something
    print s3_url # <-- Confirming that this `s3_url` variable is not None
    results.put(s3_url)

threads = []
for num, list_of_paths in enumerate(chunked_paths_as_list):
    for filepath in list_of_paths:
        t = threading.Thread(target=upload_to_s3, args=(filepath, self.unique_id))
        t.start()
        threads.append(t)
for t in threads:
    t.join()

while not results.empty():
    print results.get()

rawDescription: Map not returning anything

description: Map not returning anything


code:
words = ('hello', 'good\-bye', 'red', 'blue')
pattern = re.compile('(' + '|'.join(words) + ')', re.IGNORECASE)
sentence = 'SAY HeLLo TO reD, good-bye to Blue.'
print pattern.findall(sentence)

rawDescription: python regex question

description: Python regex question


code:
class GlobalWealth(object):
    def __init__(self):
        self._global_wealth = 10.0
        self._observers = []

    def get_wealth(self):
        return self._global_wealth

    def set_wealth(self, value):
        self._global_wealth = value
        for callback in self._observers:
            print 'anouncing change'
            callback(self._global_wealth)

    global_wealth = property(get_wealth, set_wealth)

    def bind_to(self, callback):
        print 'bound'
        self._observers.append(callback)

class Person(object):
    def __init__(self, data):
        self.wealth = 1.0
        self.data = data
        self.data.bind_to(self.update_how_happy)
        self.happiness = self.wealth / self.data.global_wealth

    def update_how_happy(self, global_wealth):
        self.happiness = self.wealth / global_wealth

if __name__ == '__main__':
    data = GlobalWealth()
    p = Person(data)
    print p.happiness
    data.global_wealth = 1.0
    print p.happiness

rawDescription: How to trigger function on value change?

description: Trigger function on value change


code:
parser = html.HTMLParser(encoding="utf-8")
t = lxml.html.fromstring(page_html, parser=parser)
print lxml.html.tostring(t)

rawDescription: How do I tell lxml which charset to use?

description: Tell lxml which charset to use


code:
import itertools

mapping = {
    'v': 'aeiou',
    'c': 'bcdfghjklmnpqrstvwxyz'
}

pattern = 'vcvcv'

for thing in itertools.product(*map(mapping.get, pattern)):
    print ''.join(thing)

rawDescription: Python: Create dynamic loop based on pattern


code:
import base64
print base64.b64encode("password")
print base64.b64decode("cGFzc3dvcmQ=")

rawDescription: Hiding a password in a (python) script

description: Hiding a password in a (python) script


code:
soup = BeautifulSoup(text, 'html.parser')
print soup.find('li', attrs={'class': 'spacer'})
soup = BeautifulSoup(text, 'html5lib')
print soup.find('li', attrs={'class': 'spacer'})
soup = BeautifulSoup(text, 'lxml')
print soup.find('li', attrs={'class': 'spacer'})

rawDescription: get data within <li> tag beautiful soup

description: Get data within <li> tag beautiful soup


code:
soup = BeautifulSoup(html)

mostwant = {'class': 'productlist_mostwanted_item '}
griditem = {'class': 'productlist_grid_item '}

divs = soup.findAll(attrs = mostwant) + soup.findAll(attrs = griditem)

for product in divs:
    item = product.h2.a.text.strip()
    price = re.search('\d+\.\d+', product.findAll('p')[1].text).group(0)
    print "%s - (%s)" % (item, price)

rawDescription: Python BeautifulSoup Loop

description: Python BeautifulSoup Loop


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: How do I prevent Python's urllib(2) from following a redirect

description: Prevent Python's urllib(2) from following a redirect


code:
globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print globvar     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

rawDescription: Using global variables in a function other than the one that created them


code:
with open(fname) as f:
    lines = f.readlines()
    for line in lines:
        if not line.strip().endswith(':'):
            print line

rawDescription: Match the last word and delete the entire line


code:
responser = requests.get(url, params=params, headers=headers)
data = responser.json()
print data[1]

rawDescription: Extracting second list from nested list returned by XHR request


code:
import dpkt, pcap, glob
for i in glob.glob("/pcap/*.pcap"):
    pc = pcap.pcap(i)
    pc.setfilter('src host 192.168.1.140 or dst host 192.168.1.140')
    for timestamp, packet in pc:
        print dpkt.ethernet.Ethernet(packet)

rawDescription: Python sniffer using pypcap and dpkt on OS X


code:
import re
import codecs

#...
#...
#extract() returns list, so you need to take first element
title = hxs.select('/html/head/title/text()').extract() [0]
content = hxs.select('//*[@id="content"]')
#instead of using BeautifulSoup for this task, you can use folowing
content = content.select('string()').extract()[0]

#simply delete duplicating spaces and newlines, maybe you need to adjust this expression
cleaned_content = re.sub(ur'(\s)\s+', ur'\1', content, flags=re.MULTILINE + re.UNICODE)

texts = "%s\n\n%s" % (title, cleaned_content)

#look's like typo in filename creation
#filename ....

#and my preferable way to write file with encoding
with codecs.open(filename, 'w', encoding='utf-8') as output:
    output.write(texts)

rawDescription: Formatting text output with Scrapy in Python


code:
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.text_ctrl = wx.TextCtrl(self)

        rows = [("Ford", "123", "1996", ""),
                ("Nissan", "432", "2010", ""),
                ("Porche", "911", "2009", "")
                ]
        self.list_ctrl = EditableListCtrl(self, style=wx.LC_REPORT, size=(-1, 150))

        self.list_ctrl.InsertColumn(0, "Something")
        self.list_ctrl.InsertColumn(1, "Price")
        self.list_ctrl.InsertColumn(2, "Qty")
        self.list_ctrl.InsertColumn(3, "Total")
        self.listitems = set()
        self.index = 0
        index = 0
        for row in rows:
            self.list_ctrl.InsertStringItem(index, row[0])
            self.list_ctrl.SetStringItem(index, 1, row[1])
            self.list_ctrl.SetStringItem(index, 2, row[2])
            self.list_ctrl.SetStringItem(index, 3, row[3])
            index += 1
        btn = wx.Button(self, label="Add Line")
        btn.Bind(wx.EVT_BUTTON, self.add_line)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.text_ctrl,0,wx.ALL|wx.EXPAND,5)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)
        self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.total)

    def add_line(self, event):
        textval = self.text_ctrl.GetValue()
        if textval not in self.listitems:
            line = str(self.index)
            self.list_ctrl.InsertStringItem(self.index, line)
            self.list_ctrl.SetStringItem(self.index, 0, self.text_ctrl.GetValue())
            self.index += 1
            self.listitems.add(textval)
        else:
            print "duplicate detected"

    def total(self,event):
        if not event.IsEditCancelled():
            count = self.list_ctrl.GetItemCount()
            totals = []
            try:
                for row in range(count):
                    itemprice = self.list_ctrl.GetItem(itemId=row, col=1)
                    itemqty = self.list_ctrl.GetItem(itemId=row, col=2)
                    itempriceval = itemprice.GetText()
                    itemqtyval = itemqty.GetText()
                    total = float(itempriceval)*float(itemqtyval)
                    totals.append(total)

                print totals

                for row, total in zip(range(count),totals):
                    self.list_ctrl.SetStringItem(row, 3, "%.2f" % total)
            except:
                return
        else:
            print "edit was cancelled"

rawDescription: Auto compute in a wxPython ListCtrl

description: Auto compute in a wxPython ListCtrl


code:
#a.py
a = a + 5

#b.py
a = 10
execfile("a.py")
print a

rawDescription: Python: read and execute lines from other script (or copy them in)?


code:
def test_derived():
    patcher = mock.patch.object(Derived, '__bases__', (mock.Mock,))
    with patcher:
        patcher.is_local = True
        d = Derived()
        print d.foo()

rawDescription: How to mock a base class with python mock library

description: Mock a base class with python mock library


code:
import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")

go = False

while(go is True):
    user=int(float(user))
    computer=random.randrange(0,101)
    computer=int(float(computer))
    while user!=computer:
        if user<computer:
            user=raw_input("This number is too low! Please try again: ")
            user=int(float(user))
            count+=1
        if user>computer:
            user=raw_input("This number is too high! Please try again: ")
            user=int(float(user))
            count+=1
    else:
        count+=1
        print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
        user1=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        while user!="play" and user1!="stop":
            user1=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="play":
                count=0
                computer=random.randrange(0,101)
                computer=int(float(computer))
                while user!=computer:
                    if user<computer:
                        user=raw_input("This number is too low! Please try again: ")
                        user=int(float(user))
                        count+=1
                    if user>computer:
                        user=raw_input("This number is too high! Please try again: ")
                        user=int(float(user))
                        count+=1
                else:
                    count+=1
                    print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
                    user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="stop":
                #print ""
                #Change it so that you change go to False
                #The loop will not execute again
                go = False

rawDescription: How do I replay a game?

description: Replay a game


code:
import requests
import json
from bs4 import BeautifulSoup as soup

data = requests.get("http://www.charitystars.com/ajax/getbidshistory?oid=9315")
data_dict = json.loads(data.text)

auction_table = soup(data_dict["html"])

for td in auction_table.find_all("td"):
    print td.get_text()

"""
28/04/2016
 andy.shirt.5

€ 120

27/04/2016
 simone.vanin

€ 60

"""

rawDescription: Access code in Firefox Inspector with Python


code:
class T:
    def __init__(self,x):
    	self.x = x
    def __radd__(self, other):
    	return other + self.x

test = (T(1),T(2),T(3),200)
print sum(test)

rawDescription: python's sum() and non-integer values

description: Python's sum() and non-integer values


code:
inp1 = "agucagcguagcugaucguga"
inp2 = "(..(((...(()).)...)))"

bkts = []
nomatch = []
pairs = []

for idx in range(len(inp2)):
    c = inp2[idx];
    if c == '(':
        bkts.append(inp1[idx])
    elif c == '.':
        nomatch.append(inp1[idx])
    elif c == ')':
        if len(bkts) > 0:
            pair = [bkts.pop(), inp1[idx]]
            pairs.append('-'.join(pair))
        else :
            print "error: Too many closing bkts"

if len(bkts) > 0 :
    print "Unmatched brackets! Too many open"

print "unmatched: " + ','.join(nomatch)
print "matches: " + ','.join(pairs)

rawDescription: Parsing through balanced paranthesis and obtaining corresponding elements


code:
counter = df["ItemID"].value_counts() #df is your dataframe
print counter[1] #prints how many times 1 occurred

rawDescription: Pandas framework: determining the count of a column data


code:
for index in range(len(col)):
    lat, lon = col[index].split(",")
    print "lat=%s, lon=%s" % (lat, lon)

rawDescription: Extract latitude and longitude from coordinates


code:
import re
import sys

class Pin:
    formatDict = {'%d': r'(\d+)', '%f': r'(\d+\.?\d*)'}
    def __init__(self, input=sys.stdin):
        self.input = input

    def scanf(self, format):
        # change the C style format to python regex
        for a, b in self.formatDict.iteritems():
            format = format.replace(a, b)
        patt = re.compile('^\\s*%s$'%format, re.M)
        buf = ''
        matched = 0
        while 1:
            c = self.input.read(1)
            if not c: break
            buf += c
            g = patt.match(buf)
            if g:
                # matched, but there may be more to match, so don't break now
                matched = 1
                matchedGroup = g
            elif matched:
                # the first unmatch after a match, seek back one char and break now
                self.input.seek(-1, 1)
                break
        if matched:
            return tuple(eval(x) for x in matchedGroup.groups())

scanf = Pin(open('in', 'r')).scanf
print scanf('%d %d')
print scanf('%f %f')

rawDescription: Python Input Read Formatting

description: Python Input Read Formatting


code:
aa = {1:'a', 3:'c', 2:'b', 4:'d'}
bb = {'apple':'a', 'pear':'b', 'mango': 'g'}

bb_rev = dict((value, key)
    for key, value in bb.iteritems()) # bb.items() in python3
dd = dict((key, bb_rev[value])
    for key, value in aa.iteritems() # aa.items() in python3
    if value in bb_rev)

print dd

rawDescription: How do I map values to values with a common key in Python

description: Map values to values with a common key in Python


code:
class Foo(object):
    def __init__(self, arg=None):
        self.arg = arg
        print dir(self.__class__) + dir()

    def __dir__(self):
        return [self.arg()]

def dirfoo():
    return ["new thing"]

foo = Foo(dirfoo)

print dir(foo)

rawDescription: dynamically assigning function to __dir__ of an instance

description: Dynamically assigning function to __dir__ of an instance


code:
response = ''
while not isinstance(response, int):
    try:
        response = int(raw_input('MSG :'))
    except:
        print 'Not int'
        continue

rawDescription: How to validate user enters only an integer number

description: Validate user enters only an integer number


code:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import multiprocessing
import time
import random
from Tkinter import *

#Create a window
window=Tk()

def main():
    #Create a queue to share data between process
    q = multiprocessing.Queue()

    #Create and start the simulation process
    simulate=multiprocessing.Process(None,simulation,args=(q,))
    simulate.start()

    #Create the base plot
    plot()

    #Call a function to update the plot when there is new data
    updateplot(q)

    window.mainloop()
    print 'Done'

def plot():    #Function to create the base plot, make sure to make global the lines, axes, canvas and any part that you would want to update later

    global line,ax,canvas
    fig = matplotlib.figure.Figure()
    ax = fig.add_subplot(1,1,1)
    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.show()
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
    canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
    line, = ax.plot([1,2,3], [1,2,10])

def updateplot(q):
    try:       #Try to check if there is data in the queue
        result=q.get_nowait()

        if result !='Q':
             print result
                 #here get crazy with the plotting, you have access to all the global variables that you defined in the plot function, and have the data that the simulation sent.
             line.set_ydata([1,result,10])
             ax.draw_artist(line)
             canvas.draw()
             window.after(500,updateplot,q)
        else:
             print 'done'
    except:
        print "empty"
        window.after(500,updateplot,q)

def simulation(q):
    iterations = xrange(100)
    for i in iterations:
        if not i % 10:
            time.sleep(1)
                #here send any data you want to send to the other process, can be any pickable object
            q.put(random.randint(1,10))
    q.put('Q')

if __name__ == '__main__':
    main()

rawDescription: Why can't matplotlib plot in a different thread?


rawDescription: How to walk through a python array/object similar to PHP's foreach function

description: Walk through a python array/object similar to PHP's foreach function


code:
#!/usr/bin/python
import ftplib
import os
filename = "filename"
ftp = ftplib.FTP("xx.xxx.xxx.xxx")
ftp.login("uid", "psw")
ftp.cwd("/my/location")
os.chdir(r"c:\somewhere")
print filename
ftp.retrbinary('RETR %s' % filename, open(filename, 'w').write)

rawDescription: Python FTP from UNIX to Windows

description: Python FTP from UNIX to Windows


code:
from scrapy.http import Request
from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider

class ExampleSpider(BaseSpider):
    name = 'example'
    allowed_domains = ['jadopado.com']

    def start_requests(self):
        yield Request(url='http://jadopado.com/', cookies={'customer_country_code': 'AE'})

    def parse(self, response):
        hxs = HtmlXPathSelector(response)

        print hxs.select("//div[@class='country_code']/text()").extract()[0]

rawDescription: Scrapy Post request isn't working

description: Scrapy Post request isn't working


code:
import json

server_response = '{"searchInformation": {"formattedSearchTime": "0.26", "formattedTotalResults": "489", "searchTime": 0.25836300000000001, "totalResults": "489"} }'
myjson = json.loads(server_response)
print myjson["searchInformation"]["totalResults"]

rawDescription: Python interpreting JSON values

description: Python interpreting JSON values


code:
#!/usr/bin/env python
import subprocess

login = 'username'
password = 'somepassword'

# OpenSSL doesn't support stronger hash functions, mkpasswd is preferred
#p = subprocess.Popen(('openssl', 'passwd', '-1', password), stdout=subprocess.PIPE)
p = subprocess.Popen(('mkpasswd', '-m', 'sha-512', password), stdout=subprocess.PIPE)
shadow_password = p.communicate()[0].strip()

if p.returncode != 0:
    print 'Error creating hash for ' + login

r = subprocess.call(('usermod', '-p', shadow_password, login))

if r != 0:
    print 'Error changing password for ' + login

rawDescription: is there a way to script in Python to change user passwords in Linux? if so, how?

description: Is there a way to script in Python to change user passwords in Linux? if so, how


code:
import Tkinter as tk

class DateEntry(tk.Frame):
    def __init__(self, master, frame_look={}, **look):
        args = dict(relief=tk.SUNKEN, border=1)
        args.update(frame_look)
        tk.Frame.__init__(self, master, **args)

        args = {'relief': tk.FLAT}
        args.update(look)

        self.entry_1 = tk.Entry(self, width=2, **args)
        self.label_1 = tk.Label(self, text='/', **args)
        self.entry_2 = tk.Entry(self, width=2, **args)
        self.label_2 = tk.Label(self, text='/', **args)
        self.entry_3 = tk.Entry(self, width=4, **args)

        self.entry_1.pack(side=tk.LEFT)
        self.label_1.pack(side=tk.LEFT)
        self.entry_2.pack(side=tk.LEFT)
        self.label_2.pack(side=tk.LEFT)
        self.entry_3.pack(side=tk.LEFT)

        self.entry_1.bind('<KeyRelease>', self._e1_check)
        self.entry_2.bind('<KeyRelease>', self._e2_check)
        self.entry_3.bind('<KeyRelease>', self._e3_check)

    def _backspace(self, entry):
        cont = entry.get()
        entry.delete(0, tk.END)
        entry.insert(0, cont[:-1])

    def _e1_check(self, e):
        cont = self.entry_1.get()
        if len(cont) >= 2:
            self.entry_2.focus()
        if len(cont) > 2 or not cont[-1].isdigit():
            self._backspace(self.entry_1)
            self.entry_1.focus()

    def _e2_check(self, e):
        cont = self.entry_2.get()
        if len(cont) >= 2:
            self.entry_3.focus()
        if len(cont) > 2 or not cont[-1].isdigit():
            self._backspace(self.entry_2)
            self.entry_2.focus()

    def _e3_check(self, e):
        cont = self.entry_2.get()
        if len(cont) > 4 or not cont[-1].isdigit():
            self._backspace(self.entry_3)

    def get(self):
        return self.entry_1.get(), self.entry_2.get(), self.entry_3.get()

if __name__ == '__main__':
    def show_contents(e):
        print dentry.get()

    win = tk.Tk()
    win.title('DateEntry demo')

    dentry = DateEntry(win, font=('Helvetica', 40, tk.NORMAL), border=0)
    dentry.pack()

    win.bind('<Return>', show_contents)
    win.mainloop()

rawDescription: Tkinter - Entry box - formatted for date


code:
import xlrd

book = xlrd.open_workbook('trend.xls')
sheet = book.sheet_by_index(0)

unique_combinations = {}

for row_index in range(sheet.nrows):
    cell_1 = sheet.cell(row_index, 0)
    cell_2 = sheet.cell(row_index, 1)
    cell_3 = sheet.cell(row_index, 2)
    unique_combo = (cell_1.value, int(cell_2.value))
    if unique_combinations.has_key(unique_combo):
        unique_combinations[unique_combo].append(cell_3.value)
    else:
        unique_combinations[unique_combo] = [cell_3.value]

for k in unique_combinations.keys():
    values = unique_combinations[k]
    average = sum(values ) / len(values )
    print '%s - %s = %s' % (k[0], k[1], average)

rawDescription: Read Excel Files and Only Use Specific Files


code:
import urllib
proxies = {"http": "http://localhost:8888"}
params = urllib.urlencode({'search':"August Rush"})
f = urllib.urlopen("http://www.thepiratebay.org/search/query", params, proxies)
print len(f.read())

rawDescription: Unexpected behaviour with Urllib in Python


code:
@login_required(login_url='/accounts/login/', template_name='market/postad.html')
def newProduct(request):
    product_form = ProductForm(request.POST or None, request.FILES or None)
    if(request.method =='POST'):
        if product_form.is_valid():
            product = product_form.save(commit=False)
            product.userid = Account.objects.get_or_create(user=request.user)
            product.save()
        else:
            print product_form.errors
    return render(request, template_name, {'product_form':product_form} )

rawDescription: Updating fields of model using forms and views on Django 1.7


code:
a = [0xE3, 0x81, 0x82]
print "".join([chr(c) for c in a]).decode('UTF-8')

rawDescription: In Jython, how can I create unicode string from UTF-8 byte sequence?


code:
import socket

address = ('', 7777)
server_socket = socket.socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
try:
    server_socket.bind(address)
except Exception, e:
    print colored("Address already in use", 'red')

server_socket.listen(2)

print colored("Socket ready", 'blue')
while True:
    client_socket, addr = server_socket.accept()
    hostIP = addr[0]
    port = addr[1]

    try:
        host = gethostbyaddr(hostIP)[0]
    except:
        host = hostIP
    print colored("Got connection from: " + host, 'blue')
    while True:
        try:
            recv_data = client_socket.recv(2048)
            if not recv_data:
                break
            print("Got: " + recv_data)
        except socket.error, e:
            print "nothing"
            recv_data = "" # this is because I test what it is later, but that's irrevlevant.

rawDescription: Python socket server - continue to receive client data


code: print inp[1::2]

rawDescription: How do print the odd letters of an input in python


code:
import urllib2
from BeautifulSoup import BeautifulSoup

#fetch html
source = urllib2.urlopen(url)

#parse with BeautifulSoup
BS = BeautifulSoup(source)

#search for title element, print element.text
print BS.find('title').text

rawDescription: Getting url title with python

description: Getting url title with python


rawDescription: Converting multi-line script output to dictionary using regex


code:
def testfunc():
    """ My Docstrings """
    print "test"

testfunc.__doc__

rawDescription: Can I make IDLE shell show me the description of a function?

description: Make IDLE shell show me the description of a function


code:
import json
import urllib2
#Read file and print a line
webFD = urllib2.urlopen("http://rasinsrv07.cstcis.cti.depaul.edu/CSC455/assignment4.txt")
tweets = webFD.readlines()

for tweet in tweets:
    print tweet

    #create dictionary
    try:
        dictt = json.loads(tweet)
    except ValueError:
        continue

    #print dictionary
    print dictt.keys()

    #print values
    print dictt.values()

    #loop through tweets
    for (key, value) in dictt.items():
        print key, '->', value

    #Created the DB
    import sqlite3

    conn = sqlite3.connect('twitter.db')
    c = conn.cursor()

    #Created the table for the tweets
    c.execute("CREATE TABLE IF NOT EXISTS Tweet(created_at, id, text, source,    in_reply_to_user_ID,retweet_Count)")

    #*** Here is a possible solution ***
    c.execute('INSERT INTO Tweet VALUES (?, ?, ?, ?, ?, ?)',
          (dictt['created_at'], dictt["id"], dictt["text"], dictt['source'], dictt['in_reply_to_user_id'],
           dictt['retweet_count']))
    conn.commit()
    conn.close()

rawDescription: python dictionary into sqlite

description: Python dictionary into sqlite


code:
def recurse_directory(dictionary):
    for dirname, dir_content in dictionary.iteritems():
        for filename in dir_content:
            if type(filename) == dict: # It's a directory
                recurse_directory(filename)
            else:
                print "{} => {}".format(dirname, filename)

rawDescription: Python: file search using dictionary

description: Python: file search using dictionary


code:
def rec_mult(x, y, tot=0, inc=0):
    if int(x) ==  0:
        return tot
    else:
        x = str(x)
        tot += int(x[(-1+inc):]) * y
        x = x[:(-1+inc)] + "0"; inc -= 1
        return rec_mult(x, y, tot, inc)  # <-- return here

print rec_mult(2, 10)  # 20

rawDescription: Unexpected python recursive multiplication return


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

pat = re.compile(r'''
    (?P<open> \b [^\W_]+ ) ,, |
    ,, (?P<close> [^\W_]+ ) \b |
    (?P<attrName> \S+ ) [ ] (?P<attrValue> [^,\n]+ ) [ ] ,, |
    (?P<textContent> [^,\s] (?: [^,] | , (?!,) )*? ) \s* (?=[^\W_]*,,)''',
      re.X)

txt = '''html,,
    head,,        profile http://gmpg.org/xfn/11 ,,
                  lang en-US ,,

        title,,   Some markright page.
        ,,title
    ,,head'''

result = ''
opened = False
for m in pat.finditer(txt):
    if m.group('attrName'):
        result += ' ' + m.group('attrName') + '="' + m.group('attrValue') + '"'
    else:
        if opened:
            opened = False
            result += '>'
        if m.group('open'):
            result += '<' + m.group('open')
            opened = True
        elif m.group('close'):
            result += '</' + m.group('close') + '>'
        else:
            result += m.group('textContent')
print result

rawDescription: Some issues with Python regex findall

description: Some issues with Python regex findall


code:
n = 5 # total length
n2 = 2 # number of "2"s
for idx in itertools.combinations( xrange(n), n2 ):
    print [ 2 if i in idx else 1 for i in xrange(n) ]

rawDescription: Generating all lists that satisfy certain constraints in Python


code:
import datetime
now = datetime.datetime.now()
delay = float (raw_input ("enter delay (s): "))
dt = datetime.timedelta (seconds=delay)
then = now + dt
print now
print then

rawDescription: Manipulating time in python

description: Manipulating time in python


code:
from bs4 import BeautifulSoup
html_doc = """<ul data-bin="Sdafdo39">"""
bs = BeautifulSoup(html_doc)
print [item["data-bin"] for item in bs.find_all() if "data-bin" in item.attrs]
# ['Sdafdo39']

rawDescription: How find specific data attribute from html tag in BeautifulSoup4?


code:
result = 0
for i in range(a,b+1):
  if i%2 == 1:
    result += i
print result

rawDescription: Sum a sequence of numbers

description: Sum a sequence of numbers


code:
not_empty_ones = []

for el in all_data_array:
    if all(el):
        not_empty_ones.append(el)
print not_empty_ones

rawDescription: Removing empty elements from an array/list in Python


code:
def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            print "invalid input, try again!"

rawDescription: Check if user input is an integer

description: Check if user input is an integer


code:
from PySide.QtCore import QByteArray, QUrl
from PySide.QtGui import QApplication
from PySide.QtWebKit import QWebView, QWebPage
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply

class Browser(object):

    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.network_manager.createRequest = self._create_request
        self.network_manager.finished.connect(self._request_finished)

        self.web_page = QWebPage()
        self.web_page.setNetworkAccessManager(self.network_manager)

        self.web_view = QWebView()
        self.web_view.setPage(self.web_page)

    def _create_request(self, operation, request, data):
        print data.readAll()
        reply = QNetworkAccessManager.createRequest(self.network_manager,
                                                    operation,
                                                    request,
                                                    data)
        return reply

    def _request_finished(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print reply.error()
            print reply.errorString()

    def _make_request(self, url):
        request = QNetworkRequest()
        request.setUrl(QUrl(url))
        return request

    def _urlencode_post_data(self, post_data):
        post_params = QUrl()
        for (key, value) in post_data.items():
            post_params.addQueryItem(key, unicode(value))

        return post_params.encodedQuery()

    def perform(self, url, method='GET', post_data=dict()):
        request = self._make_request(url)

        if method == 'GET':
            self.web_view.load(request)
        else:
            encoded_data = self._urlencode_post_data(post_data)
            request.setRawHeader('Content-Type',
                                 QByteArray('application/x-www-form-urlencoded'))
            self.web_view.load(request,
                               QNetworkAccessManager.PostOperation,
                               encoded_data)

if __name__ == '__main__':
    app = QApplication([])
    browser = Browser()
    browser.perform('http://www.python.org', 'POST', {'test': 'value', 'anothername': 'gfdgfd'})
    app.exec_()

rawDescription: POST request using QWebView request

description: POST request using QWebView request


code:
values_list = [{'end_date': u'2014-08-25','link': u'http://www.tt.tw/1','start_date':    u'2014-08-01'},
{'end_date': u'2014-08-26','link': u'http://www.tt.tw/2','start_date': u'2014-08-03'},
{'end_date': u'2014-08-23','link': u'http://www.tt.tw/3','start_date': u'2014-08-01'}]

for dictionary in values_list:
    print dictionary['link']

rawDescription: python: for loop dictionary print out item

description: Python: for loop dictionary print out item


code:
resultDict = {}
for startCity in places:
    for endCity in places:
        startLocation = startCity[0] + ' ' + startCity[1]
        endLocation = endCity[0] + ' ' + endCity[1]
        resultLocations = [startLocation, endLocation]
        resultId = ','.join(resultLocations.sort())

        if startLocation == endLocation or resultId in resultDict:
            continue

        dlat = math.radians(float(startCity[2])-float(endCity[2]))
        dlon = math.radians(float(startCity[3])-float(endCity[3]))
        a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(float(endCity[2]))) \
            * math.cos(math.radians(float(startCity[2]))) * math.sin(dlon/2) * math.sin(dlon/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        d = radius * c
        resultDict[resultId] = d * 0.621371

print resultDict

rawDescription: Iteration of a 2d list in Python, distance between two locations


code:
while True:
    print "Hello world!"

rawDescription: Python on windows - conhost.exe CPU usage


code:
import os
import json
from csv import DictWriter
import codecs

def get_columns():
    columns = []
    with open("columns.txt") as f:
        columns = [line.split()[0] for line in f if line.strip()]
    return columns

if __name__ == "__main__":
    in_path = "./fb-data_input/"
    out_path = "./fb-data_output/"
    columns = get_columns()
    bad_keys = ("has_added_app", "is_community_page")
    for filename in os.listdir(in_path):
        json_filename = os.path.join(in_path, filename)
        csv_filename = os.path.join(out_path, "%s.csv" % (os.path.basename(filename)))
        with open(json_filename) as f, open(csv_filename, "wb") as csv_file:
            csv_file.write(codecs.BOM_UTF8)
            csv = DictWriter(csv_file, columns)
            csv.writeheader()
            for line_number, line in enumerate(f, start=1):
                try:
                    data = json.loads("{%s}" % (line.strip().strip('^')))
                    # fix parking column
                    if "parking" in data:
                        data['parking'] = ", ".join("%s: %s" % (k, str(v)) for k, v in data['parking'].items())
                    data = {k.upper(): unicode(v).encode('utf8') for k, v in data.items() if k not in bad_keys}
                except Exception, e:
                    import traceback
                    traceback.print_exc()
                    data = {columns[0]: "Error on line %s of %s: %s" % (line_number, json_filename, e)}
                csv.writerow(data)

rawDescription: Converting a modified JSON to CSV using python


code:
from PIL import Image
from numpy import *
size=25,25
im=(Image.open('...'))
im = im.resize(size, Image.ANTIALIAS)
im.save("Thumbnail.png","PNG")
imgArr = array(im)
print imgArr.shape

rawDescription: Want to create a thumbnail(25,25) of an Image of size (181,256) using python


code:
for p in pkts:
    if TCP in p:
        print len(p[TCP].payload)

rawDescription: How to get the length of http response python scapy

description: Get the length of http response python scapy


code:
import os, sys

def main():
    HEADER = '''# Author: Rob
# Company: MadeupOne
# Copyright Owner: Rob
'''

    filelist = []
    for path, dir, files in os.walk(sys.argv[1]):
        for file in files:
            if file.endswith('.py'):
                filelist.append(path + os.sep + file)

        for filename in filelist:
            try:
                inbuffer = open(filename, 'U').readlines()
                outbuffer = [HEADER] + inbuffer

                open(filename, 'wb').writelines(outbuffer)
            except IOError:
                print 'Please check the files, there was an error when trying to open %s...' % filename
            except:
                print 'Unexpected error ocurred while processing files...'

if __name__ == '__main__': main()

rawDescription: I need to append standard header contents to the top of all of my python files


code:
l = ["whiff", "dog", "axe"]
answer = l[0][4] + l[1][1] + l[2][1]
print answer

rawDescription: Take elements from items in a list of strings


code:
def main():
    total = 0.0
    print ' this is the accumulator test run '
    # Loop continuously.
    while True:
        # Get the input using raw_input because you are on Python 2.7.
        number = raw_input('enter a number: ')
        # If the user typed in "done"...
        if number == 'done':
            # ...break the loop.
            break
        # Else, try to add the number to the total.
        try:
            # This is the same as total = total + float(number)
            total += float(number)
        # But if it can't (meaning input was not valid)...
        except ValueError:
            # ...continue the loop.
            # You can actually do anything in here.  I just chose to continue.
            continue
    print ' the total is', total
main()

rawDescription: Python 2.7.5, making loop with unlimited user input, accumulate totals, applying sentinel?


rawDescription: datetime from timeseries data in multiple columns

description: Datetime from timeseries data in multiple columns


code:
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read().split("\r\n")

# If you want to add '/32' to each IP
ips = [x + "/32" for x in ips if x]

rawDescription: data from url open in list

description: Data from url open in list


code:
import pandas as pd

df = pd.DataFrame({
    'sec_Id': ["to", "ti", "tu", "ta", "ty", "te"],
    'sec_Orga': ['CNP', 'COF', 'COF', 'POS', 'POS', 'POS'],
    'sec_Etat': ['Sorti(e)', 'Valide', 'Suspendu(e)', 'Valide', 'Suspendu(e)', 'Suspendu(e)']
})

pivot = df.pivot_table(index='sec_Orga', columns='sec_Etat', aggfunc=len)
pivot["total"] = pivot.sum(axis=1)

print pivot

# pivot.to_csv("p.csv") # Export to CSV file. Uncomment to use.

rawDescription: python pandas - groupby unstack pivot - count len - mixed values and export

description: Python pandas - groupby unstack pivot - count len - mixed values and export


code:
Parents={-1:"Root"}
def add_dir(level, parent, index, k):
    print "Directory"
    print "Level=%d, Parent=%s, Index=%d, value=%s" % (level, Parents[parent], index, k)
def add_file(parent, index, k):
    print "File"
    print "Parent=%s, Index=%d, value=%s" %  (Parents[parent], index, k)
def f(level=0, parent=-1, index=0, di={}):
    for k in di:
        index +=1
        if di[k]:
            Parents[index]=k
            add_dir(level, parent, index, k)
            f(level+1, index, index, di[k])
        else:
            add_file(parent, index, k)

a={
    'SomeRootDirectory': {
        'foo.txt': None,
        'bar.txt': None,
        'Stories': {
            'Horror': {
                'rickscott.txt' : None,
                'Trash' : {
                    'notscary.txt' : None,
                    },
                },
            'Cyberpunk' : None
            },
        'Poems' : {
            'doyoureadme.txt' : None
        }
    }
}

f(di=a)

rawDescription: Walking/iterating over a nested dictionary of arbitrary depth (the dictionary represents a directory tree)


code:
from multiprocessing import Process
import os

def info(title):
    print title
    print 'module name:', __name__
    print 'parent process:', os.getppid()
    print 'process id:', os.getpid()

def f(name):
    os.setuid(1000)
    info('function f')
    print 'hello', name
    os.system('id')

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

rawDescription: In Python how to call subprocesses under a different user?


code:
for acc in account["data"]:
    if acc["id"] == "345632575539519":
        print acc["access_token"]
        self.facebook_request("/345632575539519/feed", post_args={"message":"hello", "link": "http://www.example.com"}, access_token=acc["access_token"], callback=self.async_callback(self._on_page_post))

rawDescription: Application page cant know that am the admin?


code:
def parse(self, response):
    print "URL: " + response.url

rawDescription: Scrapy get request url in parse

description: Scrapy get request url in parse


code:
import urllib
import sys

url = 'http://www.srh.noaa.gov/data/PHI/SRFPHI'

try:
    response = urllib.urlopen(url)
except:
    print 'Invalid url.'
    sys.exit(1)

data = response.readlines()

for i in range(0, len(data)):
    if '.FOR THE BEACHES OF NEW JERSEY AND DELAWARE' in data[i]:
        date = data[i - 2]
        print date
        line = data[i]
        while line.strip('\n') != 'TIDE INFORMATION...':
            print line
            i = i + 1
            line = data[i]

rawDescription: python web page parsing when no HTML tags exist

description: Python web page parsing when no HTML tags exist


code:
for i in range (0,1000):
   if data['statuses'][i][u'lang']==u'en':
      print data['statuses'][i]['text']
      print '\n'

rawDescription: how to get English tweets alone using python?

description: Get English tweets alone using python


code:
class MyList(list):
    def __iter__(self):
        return (x for x in list.__iter__(self) if x is not None)

ml = MyList(["cat", "dog", None, "fox"])
for item in ml:
    print item

[x for x in ml]
list(ml)

rawDescription: Overloading List Comprehension Behavior?

description: Overloading List Comprehension Behavior


code:
import Tkinter as tk
import sys

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        toolbar = tk.Frame(self)
        toolbar.pack(side="top", fill="x")
        b1 = tk.Button(self, text="print to stdout", command=self.print_stdout)
        b2 = tk.Button(self, text="print to stderr", command=self.print_stderr)
        b1.pack(in_=toolbar, side="left")
        b2.pack(in_=toolbar, side="left")
        self.text = tk.Text(self, wrap="word")
        self.text.pack(side="top", fill="both", expand=True)
        self.text.tag_configure("stderr", foreground="#b22222")

        sys.stdout = TextRedirector(self.text, "stdout")
        sys.stderr = TextRedirector(self.text, "stderr")

    def print_stdout(self):
        '''Illustrate that using 'print' writes to stdout'''
        print "this is stdout"

    def print_stderr(self):
        '''Illustrate that we can write directly to stderr'''
        sys.stderr.write("this is stderr\n")

class TextRedirector(object):
    def __init__(self, widget, tag="stdout"):
        self.widget = widget
        self.tag = tag

    def write(self, str):
        self.widget.configure(state="normal")
        self.widget.insert("end", str, (self.tag,))
        self.widget.configure(state="disabled")

app = ExampleApp()
app.mainloop()

rawDescription: Python : Converting CLI to GUI

description: Python : Converting CLI to GUI


code:
import xmlrpclib
import multiprocessing

def init():
    global server
    server = xmlrpclib.ServerProxy('http://myserver:6060/RPC2',)

def func(argument):
    result = server.some_function(argument)
    return result

def run_parallel(line,num_procesos):
    pool = multiprocessing.Pool(processes=num_procesos, initializer=init)
    async_results = [pool.apply_async(func, ("dummy",q))
                     for i in range (0, num_procesos)]
    for r in async_results:
        print r.get()

    pool.close()
    pool.join()

if __name__ == '__main__':
  line="test"
  run_parallel(line,4)

rawDescription: Python Multiprocessing. How to enqueue XMLRPC ServerProxy objects


code:
q = A.objects.select_related().annotate(num_B=Count('B'))
print A[0].num_B

rawDescription: Django count related objects

description: Django count related objects


code:
for row in data:
    if all(row[key] == value for key, value in indexdict.items()):
        print row

rawDescription: Iterating through dictionary and printing matching rows from tsv file


code:
from multiprocessing import Process
import time

def method(namelog):
    while True:
        with open(namelog,'a') as filelog:
            time.sleep(0.1)
            filelog.write('test log anything \n')
if __name__ == '__main__':
    p1 = Process(target=method, args=('log1.log',))
    print "start process1"
    p1.start()
    p2 = Process(target=method, args=('log2.log',))
    print "start process2"
    p2.start()

rawDescription: how create process child run method while True in python use multiprocessing?

description: How create process child run method while True in python use multiprocessing


code:
import ctypes
import ctypes.wintypes

winHttp = ctypes.windll.LoadLibrary("Winhttp.dll")

# http://msdn.microsoft.com/en-us/library/aa384098(VS.85).aspx
# first get a handle to HTTP session
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY=0
WINHTTP_NO_PROXY_NAME=WINHTTP_NO_PROXY_BYPASS=0
WINHTTP_FLAG_ASYNC=0x10000000
HINTERNET = winHttp.WinHttpOpen("PyWin32", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC)
print HINTERNET

# now get proxy using HTTP session
# http://msdn.microsoft.com/en-us/library/aa384097(VS.85).aspx
"""
BOOL WinHttpGetProxyForUrl(
  __in   HINTERNET hSession,
  __in   LPCWSTR lpcwszUrl,
  __in   WINHTTP_AUTOPROXY_OPTIONS *pAutoProxyOptions,
  __out  WINHTTP_PROXY_INFO *pProxyInfo
);
"""
# create C structure for WINHTTP_AUTOPROXY_OPTIONS
#http://msdn.microsoft.com/en-us/library/aa384123(VS.85).aspx
"""
typedef struct {
  DWORD   dwFlags;
  DWORD   dwAutoDetectFlags;
  LPCWSTR lpszAutoConfigUrl;
  LPVOID  lpvReserved;
  DWORD   dwReserved;
  BOOL    fAutoLogonIfChallenged;
} WINHTTP_AUTOPROXY_OPTIONS;
"""
class WINHTTP_AUTOPROXY_OPTIONS(ctypes.Structure):
    _fields_ = [("dwFlags", ctypes.wintypes.DWORD),
                ("dwAutoDetectFlags", ctypes.wintypes.DWORD),
                ("lpszAutoConfigUrl", ctypes.wintypes.LPCWSTR),
                ("lpvReserved", ctypes.c_void_p ),
                ("dwReserved", ctypes.wintypes.DWORD),
                ("fAutoLogonIfChallenged",ctypes.wintypes.BOOL),]

WINHTTP_AUTOPROXY_AUTO_DETECT = 0x00000001;
WINHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001;
WINHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002;
options = WINHTTP_AUTOPROXY_OPTIONS()
options.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT
options.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A
options.lpszAutoConfigUrl = 0
options.fAutoLogonIfChallenged = False

# create C structure for WINHTTP_AUTOPROXY_OPTIONS
# http://msdn.microsoft.com/en-us/library/aa383912(VS.85).aspx
"""
struct WINHTTP_PROXY_INFO {
  DWORD  dwAccessType;
  LPWSTR lpszProxy;
  LPWSTR lpszProxyBypass;
};
"""
class WINHTTP_PROXY_INFO(ctypes.Structure):
    _fields_ = [("dwAccessType", ctypes.wintypes.DWORD),
                ("lpszProxy", ctypes.wintypes.LPCWSTR),
                ("lpszProxyBypass", ctypes.wintypes.LPCWSTR),]

info = WINHTTP_PROXY_INFO()

ret = winHttp.WinHttpGetProxyForUrl(HINTERNET, "http://www.google.com", ctypes.pointer(options), ctypes.pointer(info) )
print "proxy success?",ret
if not ret:
    # some error lets see what is that?
    import win32api
    import win32con
    errorCode = win32api.GetLastError()
    print "win32 Error:",errorCode
    s = ""
    print win32api.FormatMessage(errorCode)

print info.dwAccessType, info.lpszProxy, info.lpszProxyBypass

rawDescription: What's the correct way to use win32inet.WinHttpGetProxyForUrl


code:
with open("file1.txt", "r") as f1:
    lines1 = f1.readlines()

with open("file2.txt", "r") as f2:
    lines2 = f2.readlines()

bound = max(len(lines1), len(lines2))
for idx, line1, line2 in zip(xrange(bound), lines1, lines2):
    if line1 != line2:
        diff_line = idx
        break

bound = max(len(line1), len(line2))
for idx, c1, c2 in zip(xrange(bound), list(line1), list(line2)):
    if c1 != c2:
        diff_char = idx
        break

print "first difference is on line {0:g} and character {1:g}".format(diff_line, diff_char)

rawDescription: Compare two text files files and return what lines and columns the 1st difference is on


code:
def content_file_name(instance, filename):
    print instance
    print filename
    file = os.path.exists("media/file/"+str(filename))
    print file
    if file:
        os.remove("media/file/"+str(filename))
    return "file/"+str(filename)

rawDescription: changing file name in django

description: Changing file name in django


code:
import re
#the regular express .*? means match any characters,
#util it comes up with string "bad"
print re.sub('not (.*?) bad', 'good', s)

rawDescription: Python Replace() function using an interval between two words


code:
idx = pd.MultiIndex.from_product([['no', 'yes']] * 3, names=['A', 'B', 'C'])
print df1.groupby(['A', 'B', 'C']).size().reindex(idx).fillna(0).reset_index().rename(columns={0: 'count'})
print df2.groupby(['A', 'B', 'C']).size().reindex(idx).fillna(0).reset_index().rename(columns={0: 'count'})

rawDescription: Creating a counting table from data that is not cover all possible combinations and missing value is available in pandas data frame


code:
import re
string = "-32000 ... 0 [foo1] some string not intresting"
match = re.match(r'(?P<Pmin>-?\d+)\D+(?P<Pmax>-?\d+).*?\[(?P<Punits>[^]]+)\]', string)
# captures a dash which may or may not be there
# captures digits, minimum 1 time into group 1
# looks for anything that is not a digit (\D+)
# captures digits, minimum 1 time into group 2
# look for square brackets - the unit at the end

print match.group('Pmin')
# output: -32000

rawDescription: parsing value range with units using regex

description: Parsing value range with units using regex


code:
import datetime
currenttime = datetime.datetime.now()
finaltime = currenttime + datetime.timedelta(milliseconds=milliseconds)
print finaltime.strftime('%H:%M:%S.%f')

rawDescription: convert or parse milliseconds to timeframe [Python 3]

description: Convert or parse milliseconds to timeframe [Python 3]


code:
for a in path:
    print a.xpath('name()')  # prints a:rPr

rawDescription: conversion of namespace to nodename in lxml - python

description: Conversion of namespace to nodename in lxml - python


code:
import re
s = 'blah&match=Z-300&'
print re.search('&match=(.*)',s).group(1) #Z-300&
print re.search('&match=(.*)&',s).group(1) #Z-300

rawDescription: re.search greedy matching all combinations (letters, special characters, numbers)

description: Re.search greedy matching all combinations (letters, special characters, numbers)


code:
class Test(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = None
    def compute(self, x):
        print 'calculating c...'
        self.c = x * 2

if __name__ == "__main__":
    import cPickle as pickle

    test = Test(10, 'hello')
    test.compute(6)

    # I have computed c and I want to store it, so I don't have to recompute it again:

    pickle.dump(test, open('test_file.pkl', 'wb'))

rawDescription: Python pickle instance variables

description: Python pickle instance variables


code:
import mechanize
from bs4 import BeautifulSoup

url = "http://www.thehindu.com/archive/web/2010/06/19/"

br =  mechanize.Browser()
htmltext = br.open(url).read()

articletext = ""
for tag_li in soup.findAll('li', attrs={"data-section":"Op-Ed"}):
    for link in tag_li.findAll('a'):
        urlnew = urlnew = link.get('href')
        brnew =  mechanize.Browser()
        htmltextnew = brnew.open(urlnew).read()
        articletext = ""
        soupnew = BeautifulSoup(htmltextnew)
        for tag in soupnew.findAll('p'):
            articletext += tag.text
        print re.sub('\s+', ' ', articletext, flags=re.M)

driver.close()

rawDescription: Web Scraping To Form A Database of News

description: Web Scraping To Form A Database of News


code:
with open(lobFileName, "w+") as LOBs: #read/write, truncates file!
        for item in reader:
            for key in item: # iterate through the dictionary
                if key == 'cost_center':
                    print "%s\n" % (item[key]) # TODO: Replace this with however I display it on the webpage.
                    LOBs.write("%s\n" % (item[key])) # write the LOBs to the file, one LOB per line

rawDescription: Print works fine, but when I write() the same thing to file, I get "Expected a character buffer object"?


code:
import unittest
from mock import patch

def a(n):
    if n > 10:
        b()

def b():
    print "test"

class MyTestCase(unittest.TestCase):
    @patch('__main__.b')
    def test_b_called(self, mock):
        a(11)
        self.assertTrue(mock.called)

    @patch('__main__.b')
    def test_b_not_called(self, mock):
        a(10)
        self.assertFalse(mock.called)

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

rawDescription: How to test that a function is called within a function with nosetests

description: Test that a function is called within a function with nosetests


code:
with open(file, 'r') as temp:
    for num, line in enumerate(temp, 1):
        for string in lookup:

            # Regex
            searchObj = re.finditer(string, line, re.M | re.I)

            if searchObj:
                for match in searchObj:

                    # Find the start index of the keyword
                    start = match.span()[0]

                    # Find the end index of the keyword
                    end = match.span()[1]

                    # Truncate line to get only 'n' characters before and after the keyword
                    tmp = line[start-n:end+n] + '\n'
                    print tmp

rawDescription: Python regex to get n characters before and after a keyword in a line of text


code:
def discover(self, *args):
    devList = discover_devices()
    for device in devList:
        name = str(lookup_name(device))
        if str(lookup_name(device)) == "":
            name = "Unknown device"
        deviceinfo = "[color=1f358e][font=tahoma]Device name: [color=1f358e][font=tahoma]" + str(name) + "\n[color=1f358e][font=tahoma]MAC address: [color=1f358e][font=tahoma]" + str(device)
        btnDevice = Button(text=deviceinfo, markup = True, font_size='17sp')
        btnDevice.bind(on_release=self.optionmenu)
        box.add_widget(btnDevice)

    self.boxes.add_widget(box)
    layout.clear_widgets()

def optionmenu(self, instance):
    print instance.text

rawDescription: Creating distinct buttons in a loop in Python/Kivy?


code:
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        a = data.split(':')
        print a
        if len(a) > 1:
           command = a[0]
           content = a[1]

           msg = ""
           if command == "msg":
               msg = content
               self.message(msg)
               print msg

    def message(self, message):
        for c in self.factory.clients:
            c.transport.write(message + '\n')

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(650, factory)
print "Iphone Chat server started"
reactor.run()

rawDescription: Not able to calling NSStreamEventHasBytesAvailable


code:
import requests
import grequests

def exception_handler(request, exception):
    print "Request failed"

def chop(seq,size):
    """Chop a sequence into chunks of the given size."""
    chunk = lambda i: seq[i:i+size]
    return map(chunk,xrange(0,len(seq),size))

def get_chunk(chunk):
    reqs = (grequests.get(u) for u in chunk)
    foo = grequests.map(reqs)
    for r in foo:
        player_id = r.request.url.split('=')[-1]
        print r.status_code, player_id, r.request.url, len(r.content)
        open('data/%s.html' %player_id, 'w').write(r.content)

urls = [a.strip() for a in open('temp/urls.txt').read().split('\n') if a]

chunks = chop(urls, 150)

for chunk in chunks:
    get_chunk(chunk)

rawDescription: download list of urls with scrapy, to a list of filenames, with rate-limiting

description: Download list of urls with scrapy, to a list of filenames, with rate-limiting


code:
class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master, relief="sunken", border=1)
        # Various initialization code here
        self.box_dict = {}

    def create_widgets(self):
        for i in range(9):
            box = ttk.Combobox(self, state="readonly")
            # Do various things with your box object here
            self.box_dict[i] = box
            # Only complication is registering the callback
            box.bind("<<ComboboxSelected>>",
                         lambda event, i=i: self.change_icon(event, i))

    def change_icon(self, event, i):
        self.var_Selected = self.box_dict[i].current()
        print "The user selected value now is:"
        print self.var_Selected

rawDescription: python tkinter reference in comboboxes created in for loop

description: Python tkinter reference in comboboxes created in for loop


code:
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import pkgutil
import twisted

for importer, modname, ispkg in pkgutil.walk_packages(
    path=twisted.__path__,
    prefix=twisted.__name__+'.',
    onerror=lambda x: None):
        # skip tests
        if modname.find('test') > -1:
            continue
        print(modname)
        # gloss over import errors
        try:
            __import__(modname)
        except:
            print 'Failed importing', modname
            pass

# show that we actually imported all these, by showing one subpackage is imported
print twisted.python

rawDescription: Python: Importing everything from a python namespace / package


code:
import BeautifulSoup

negative_keywords = ['basement', 'unfinished', 'hardwood']

html = '''

    Looking for a corporate rental, this beautiful decorated 5 BR,
    4.5 BA two story house is in a desirable location, 7 minutes off
    I 85. Beautiful solid cherry cabinets in kitchen and laundry room.
    All stainless steel appliances. Hardwood floors in kitchen and foyer,
    Ceramic tile floors in all bathrooms, laundry room, dining room and sunroom.

'''

soup = BeautifulSoup.BeautifulSoup(html)
bodyContents = soup.find(attrs={'id' : 'postingbody'})

if any([k in bodyContents.getText().lower() for k in negative_keywords]):
    print "keyword was found"

rawDescription: Searching against a negative set of words


code:
from BeautifulSoup import BeautifulSoup

text = """

text1

test

one more

text2

"""

soup = BeautifulSoup(text)

# to get the contents of all
 tags:
for tag in soup.findAll('ul'):
    print tag.contents[0]

# to get the contents of
 tags w/ attribute class="descShort bullet":
for tag in soup.findAll('ul', {'class': 'descShort bullet'}):
    print tag.contents[0]

rawDescription: python re.compile Beautiful soup

description: Python re.compile Beautiful soup


code:
mystring =""" I like to wash clothes on %s
I like to clean dishes %s
"""
wash_clothes = 'tuesdays'
clean_dishes = 'never'

print mystring % (wash_clothes, clean_dishes)

rawDescription: In Python, can you have variables within triple quotes? If so, how?


code:
torrent1 = decode(open("test1.torrent", "rb").read())
torrent2 = decode(open("test2.torrent", "rb").read())

m = hashlib.sha1()
m.update(str(torrent1["info"]))
print m.hexdigest()

m = hashlib.sha1()
m.update(str(torrent2["info"]))
print m.hexdigest()

rawDescription: Detect identical torrent files

description: Detect identical torrent files


code:
from xml.etree import ElementTree

tree = ElementTree.parse('filename.html')
for elem in tree.findall('img'):
    print elem['src']

rawDescription: Detect image src from html using python and regular expressions?


code:
k=0
yterms = []
while k<=SEG:
    yterm = H + aa*m.cosh(xterms[k]/aa) -aa*m.cosh(DIS/aa)
    yterms.append(yterm)
    k+=1
print yterms

rawDescription: How to used a previously generated tuple to generate a new tuple

description: Used a previously generated tuple to generate a new tuple


code:
import os
import fileinput

if __name__ == '__main__':
    # Menu should not have any marker, just pure contents
    with open('menu.txt') as f:
        menu_contents = f.read()

    # Initialize a few items
    start_marker = '<!--123-->'
    end_marker   = '<!--321-->'
    file_list = ['index.html', 'foo.html']
    found_old_contents = False

    # Loop to replace text in place
    for line in fileinput.input(file_list, inplace=True):
        line = line.rstrip()

        if line == start_marker:
            found_old_contents = True
            print line
            print menu_contents
        elif line == end_marker:
            found_old_contents = False

        if not found_old_contents:
            print line

rawDescription: Replacing a section of text in one file with text from another using python


code:
class Foo(object):
    def __init__(self):
        self.bar = 'test'

im = Foo()
print im.bar.upper()

rawDescription: Can I treat an instance of one class as an attribute of another

description: Treat an instance of one class as an attribute of another


code:
import re
my_text = 'address ae fae daq ad, 1231 asdas  landline 213121233 -123    mobile 513121233 cell (132) -142-3127  email sdasdas@gmail.com , sdasdas@yahoo.com - ewqas@gmail.com'

split_words = ['address', 'landline', 'mobile', 'cell', 'email']
resultant_dict = {'addresses': [], 'phones': [], 'emails': []}

for sw in split_words:

    text = filter(None, my_text.split(sw))
    text = text[0].strip() if len(text) < 2 else text[1].strip()
    next_split = [x.strip() for x in text.split() if x.strip() in split_words]

    if next_split:
        text = text.split(next_split[0])[0].strip()

    if sw in ['address']:
        text = text.split(',')
        for t in text:
            resultant_dict['addresses'].append({'address': t.strip()})

    elif sw in ['landline', 'mobile', 'cell']:
        resultant_dict['phones'].append({'number': text, 'kind': sw})

    elif sw in ['email']:

        connectors = [',', '-']
        emails = re.split('|'.join(connectors), text)
        text = filter(None, [x.strip() for x in text.split()])

        for email in emails:

            email = email.strip()
            connector = ''
            index = text.index(email) if email in text else 0

            if index > 0:
                connector = text[index - 1]

            resultant_dict['emails'].append({'email': email, 'connector': connector})

print resultant_dict

rawDescription: Text Scraping using Python: Regex

description: Text Scraping using Python: Regex


code:
import tempfile, subprocess

def pdf_to_string(file_object):
    pdfData = file_object.read()
    tf = tempfile.NamedTemporaryFile()
    tf.write(pdfData)
    tf.seek(0)
    outputTf = tempfile.NamedTemporaryFile()

    if (len(pdfData) > 0) :
        out, err = subprocess.Popen(["pdftotext", "-layout", tf.name, outputTf.name ]).communicate()
        return outputTf.read()
    else :
        return None

pdf_file="files/2014_1.pdf"
file_object = file(pdf_file, 'rb')
print pdf_to_string(file_object)

rawDescription: read pdf file horizontally with pdfminer

description: Read pdf file horizontally with pdfminer


code:
def blocks(files, size=65536):
    while True:
        b = files.read(size)
        if not b: break
        yield b

with open("file", "r") as f:
    print sum(bl.count("\n") for bl in blocks(f))

rawDescription: (Python) Counting lines in a huge (>10GB) file as fast as possible


code:
sql="select value from table where name='aaaa'"
cursor.execute(sql)
results=cursor.fetchall() # changed to results to better reflect the list structure
count = len(results)
for i in range(0, count):
  for j in range (i+1, count):
    print results[i][0], results[j][0]

rawDescription: how to compare the rows of a table using python

description: Compare the rows of a table using python


code:
import ctypes
from ctypes import *

# Defining the python type that represents the C SmartString
# It must extend the 'Structure' class
# Structure, c_uint, c_char_p, etc. were imported from ctypes
class SmartString(Structure):
    _fields_=[("string_len",c_uint),
              ("alloc_len",c_uint),
              ("str",c_char_p),
              ("str_terminator", c_char_p)]

# Loading the C shared lib I've just compiled
smartstring_lib = ctypes.CDLL('SmartString.so')

# Defining pointer to the SmartString_new() function
SmartString_new = smartstring_lib.SmartString_new

# Declaring the function return type - a pointer to a SmartString object - just like in the C code
SmartString_new.restype = POINTER(SmartString)

# Declaring list of parameter types. In this case, the list contains only one item,
# as the function has only one parameter
SmartString_new.argtypes = [c_char_p]

# Calling the SmartString_new() function. Expecting to get a pointer to SmartString object into 'my_str'
# The API requires a MUTABLE string, so create_string_buffer() is used here
# The reference to this string is not saved, as I don't care if it is modified by the C code
my_str = SmartString_new(create_string_buffer('my nice string'))

# Printing fields of the dereferenced returned value (dereferencing is done using '.contents')
print my_str.contents.string_len
print my_str.contents.alloc_len
print my_str.contents.str
print my_str.contents.str_terminator

rawDescription: Python - ctypes - How to call functions and access struct fields?


code:
import os

root = "/Users/Siddhartha/Desktop/py scripts"
for item in os.listdir(root):
    if not item.startswith('.') and os.path.isfile(os.path.join(root, item)):
        print item

rawDescription: How to ignore hidden files in python functions?

description: Ignore hidden files in python functions


code:
input_is_valid = False
while not input_is_valid:
    comma_separated_numbers = raw_input("Please enter a list of 5 numbers,separated by commas: ")
    numbers = [int(x.strip()) for x in comma_separated_numbers.split(",")]
    if len(numbers) != 5:
        print "Please enter exactly 5 numbers"
    else:
        input_is_valid = True

rawDescription: Python: Select Only Parts of an input?

description: Python: Select Only Parts of an input


code:
@process
def send_msg(chan, msg):
    chan.write(msg)

@process
def alt_example(chan1, chan2):
    alt = Alt(chan1, chan2)
    print alt.select()
    print alt.select()

c1, c2 = Channel(), Channel()
Par(send_msg(c1, 'yes'), send_msg(c2, 'no'), alt_example(c1, c2)).start()

rawDescription: CSP Channels with native threading in Python


code:
import itertools as it
ls = ['a', 'b', 'c', 'd']
ii=it.groupby( it.combinations(ls, 2), lambda x: x[0] )
for key, iterator in ii:
    print key, list(iterator)

rawDescription: From a combination, get subsets with members containing the same first element, python


code:
def letterbox(f,aspect_ratio=1):
    try:
        im = Image.open(StringIO(f))
        imagex = int(im.size[0])
        imagey = int(im.size[1])
        width = min(imagex, imagey*aspect_ratio)
        height = min(imagex/aspect_ratio, imagey)
        left =(imagex - width)/2
        top = (imagey - height)/2
        box = (left,top,left+width,top+height)
        im = im.crop(box)
        new_file = StringIO()
        im.save(new_file,'JPEG')
        new_file.seek(0)
    except Exception, e:
        pass
    return new_file

rawDescription: How can I take an image and get a 3x2 ratio image cropped from the center?

description: Take an image and get a 3x2 ratio image cropped from the center


code:
# function that will be run through the pool
def process_request(req):
    try:
            dr = urllib2.urlopen(req, timeout=30)

    except Exception, e:
            logging.error(traceback.print_exc())

# process killer
def sig_end(r):
    sys.exit()

# globals
MP_CONCURRENT = int(multiprocessing.cpu_count()) * 2
if MP_CONCURRENT < 2: MP_CONCURRENT = 2
CHUNK_SIZE = 20
POOL = multiprocessing.Pool(MP_CONCURRENT)

# pool initiator
def request_manager(req_list):
    try:
            resp = POOL.map_async(process_request, req_list, CHUNK_SIZE, callback=sig_end)

    except Exception, e:
            logging.error(traceback.print_exc())

rawDescription: Python multiprocess Process never terminates


rawDescription: I want to convert a string into list

description: I want to convert a string into list


code:
class Meta(type):
    # There is *NO* @classmethod decorator here
    def my_class_method(cls):
        print "I have a class {}".format(cls)

class MyThing(object):
    __metaclass__ = Meta
    def __init__(self, this=None):
        self.this = this

rawDescription: Don't want instance to see Python @classmethod


code:
from bs4 import BeautifulSoup
import requests

apmc = 'JALGAON'

url = 'http://www.msamb.com/apmcpri_rpt.aspx'
with requests.Session() as session:
    session.headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
        'X-Requested-With': 'XMLHttpRequest'
    }
    response = session.get(url)
    soup = BeautifulSoup(response.content)

    # build an options mapping
    options = {option.get_text(strip=True): option['value'] for option in soup.select("select#cpMainContent_cmb_comm option")[1:]}

    # parse form parameters
    form = soup.find("form", id="form1")
    params = {
        'ctl00$cpMainContent$cmb_comm': options.get(apmc),
        '__ASYNCPOST': 'true',
        'ctl00$cpMainContent$ScriptManager1': 'ctl00$cpMainContent$UpdatePanel1|ctl00$cpMainContent$cmb_comm',
        '__EVENTTARGET': 'ctl00$cpMainContent$cmb_comm',
        '__EVENTARGUMENT': form.find('input', {'name': '__EVENTARGUMENT'})['value'],
        '__LASTFOCUS': '',
        '__VIEWSTATE': form.find('input', {'name': '__VIEWSTATE'})['value'],
        '__VIEWSTATEGENERATOR': form.find('input', {'name': '__VIEWSTATEGENERATOR'})['value'],
        '__VIEWSTATEENCRYPTED': '',
        '__EVENTVALIDATION': form.find('input', {'name': '__EVENTVALIDATION'})['value']
    }

    response = session.post(url, data=params)

    # parse the results
    soup = BeautifulSoup(response.content)
    for row in soup.select("table#cpMainContent_GridView1_tab5 tr")[1:]:
        print row.find_all("td")[1].text

rawDescription: Not able to scrape data from dropdown box


code:
from matplotlib import pyplot as plt

plt.scatter(range(3), range(1,4), 200, marker=ur'$\u2640$')
plt.scatter(range(3), range(2,5), 200, marker=ur'$\u2642$')

rawDescription: Men=mars and female=venus symbols in python


code:
print df
print df.applymap(lambda x: x>1)

rawDescription: Pandas DataFrame: apply function to all columns


code:
for i in range(1,10) + [chr(x) for x in range(ord('a'), ord('z')+1)]:
    print i

rawDescription: How to create a loop from 1-9 and from a-z?

description: Create a loop from 1-9 and from a-z


code:
signal = [0,0,0,0,1,2,3,4,0,0,0,0,1,1,1,1,1,0,0,0,0]
segments = []
segmentsCoords = []

inSegment = False
for i in xrange(len(signal)):
    if inSegment and signal[i] == 0:
        continue
    elif not inSegment and signal[i] == 0:
        inSegment = True
        begin = i
    elif inSegment and signal[i] != 0:
        inSegment = False
        end = i
        segments.append((end-begin)/2+begin)
        segmentsCoords.append([begin, end])
    elif not inSegment and signal[i]:
        continue
if signal[-1] == 0:
    end = len(signal)
    segments.append((end-begin)/2+begin)
    segmentsCoords.append([begin, end])

print segmentsCoords
print segments

rawDescription: finding a regions in python list

description: Finding a regions in python list


code:
import sys

# ...
print i
sys.stdout.flush()  # force a buffer flush

rawDescription: "pass" statement in datetime not working


code:
output = {}
for value in data:
    output.setdefault(value['exercise_id'], {})[value['student_id']] = value

print output

rawDescription: Python: Convert a list into a bidimensional mapping dictionary


code:
from sys import argv
import os

class MyPath(object):
    def __init__(self, newpath):
        self.newpath = newpath

    def __enter__(self):
        self.path = os.environ['PATH']
        os.environ['PATH'] = self.newpath
        return self.newpath

    def __exit__(self, type, value, traceback):
        os.environ['PATH'] = self.path

if __name__ == "__main__":
    if len(argv) < 2:
        print "usage: %s path" % (argv[0],)
        exit(1)
    print os.environ['PATH']

    with MyPath(argv[1]) as newpath:
        print newpath
        print os.environ['PATH']

    print os.environ['PATH']

rawDescription: python override environment variable in local scope

description: Python override environment variable in local scope


code:
import os
import multiprocessing as mp

results = []

def testFunc(file):
    result = []
    print "Working in Process #%d" % (os.getpid())
    # This is just an illustration of some logic. This is not what I'm
    # actually doing.
    with open(file, 'r') as f:
        for line in f:
            if 'dog' in line:
                result.append(line)
    return result

def collect_results(result):
    results.extend(result)

if __name__ == "__main__":
    p = mp.Pool(processes=2)
    files = ['/path/to/file1.txt', '/path/to/file2.txt']
    for f in files:
        p.apply_async(testFunc, args=(f, ), callback=collect_results)
    p.close()
    p.join()
    print(results)

rawDescription: python multiprocessing apply_async only uses one process

description: Python multiprocessing apply_async only uses one process


code:
class continuousFun(object):
    def __init__(self,initialArray):
        self.initialArray = initialArray
        self.delta=[]
        for index in range(len(self.initialArray)):
            if index==0:self.delta.append(self.initialArray[index]-0)
            else:self.delta.append(self.initialArray[index]-self.initialArray[index-1])
    def getValue(self,pseudoIndex):
        return self.initialArray[int(pseudoIndex)]+self.delta[int(pseudoIndex)+1]*(pseudoIndex-int(pseudoIndex))

array=continuousFun([0,4,9,2,4,5,9,10])

print array.getValue(0.5)

rawDescription: Representing array in Python as a continuous function


code:
import numpy as np
import cv2
img = cv2.imread('foo.jpg')
height, width, channels = img.shape
print height, width, channels

rawDescription: Python opencv2 (cv2) wrapper get image size?


code:
stringToQueryLevel1 = ['why this', 'why this code', 'why this code is complex']
for item in stringToQueryLevel1:
    print ' ' in item

rawDescription: Using re.match() to find Substring

description: Using re.match() to find Substring


code:
def CRSTransform(Lat, Long):
    transform = osr.CoordinateTransformation(source, target)
    point = ogr.Geometry(ogr.wkbPoint)
    point.SetPoint_2D(0, float(Long), float(Lat))
    point.Transform(transform)
    print point.GetX(), "   ", point.GetY()

CRSTransform(-44.419134, 172.243651)  # 1539788.868     5081294.99354

rawDescription: Python (GDAL): projection conversion from WGS84 to NZTM2000 is not correct


code:
my_threads = []
for i in range(3):
    my_thread = MyThread()
    my_thread.name = i
    my_thread.start()
    my_threads.append(my_thread)

for t in my_threads:
    t.join()

print 'end of script'

rawDescription: Code after threads is executed early, why?


code: print "What is %d + %d?" % (int1, int2)

rawDescription: How to remove quotation marks (and other marks)?

description: Remove quotation marks (and other marks)


code: print "\n".join([p.get_text(strip=True) for p in soup.find_all(product)])

rawDescription: How to remove html tags from strings in Python using BeautifulSoup

description: Remove html tags from strings in Python using BeautifulSoup


code:
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'}
response = requests.get("http://whoscored.com/829652/Live/", headers=headers)
print response.content

rawDescription: Python - Printing an HTML page give empty response for some sites


code:
soup = BeautifulSoup(your_html)
div = soup('div',{'id':'intForecast'})[0]
tds = div.find('table').findAll('td')

for td in tds:
    day = td('span')[0].text
    forecast = td('div')[1].text
    print day, forecast

rawDescription: Python, Beautiful Soup: how to get the desired element


code:
from inspect import ismethod,getmembers

class Example:
    def __repr__(self):
        return "\n".join("%s: %s" % (k, v) for (k,v) in getmembers(self,lambda x: not ismethod(x)))

    def method(self):
        return 1

a = Example()
a.foo = 'bar'
print a

rawDescription: Python: How can I check if an attribute of an object is a method or not?


code:
qs = YourModel.objects.extra(select={
    'max_time': '''
    select * from t where (
        start_time_a in (
            select greatest(max(start_time_a), max(start_time_b)) from t
        ) or start_time_b in (
            select greatest(max(start_time_a), max(start_time_b)) from t
        )
    )'''
})

# each YourModel object in the queryset will have an extra attribute, max_time
for obj in qs:
    print obj.max_time

rawDescription: how to get latest timestamp from the two columns in django

description: Get latest timestamp from the two columns in django


code:
import urllib2
import urllib
import csv
from bs4 import BeautifulSoup  # latest version bs4
import json

week = raw_input("Which week?")
week = str(week)
data = []
url_format = "http://fantasy.nfl.com/research/projections?offset={offset}&position=O&sort=projectedPts&statCategory=projectedStats&statSeason=2015&statType=weekProjectedStats&statWeek={week}"

for page_num in range(1, 3):
    page_mult = (page_num - 1) * 25 + 1
    next = str(page_mult)
    url = url_format.format(week=week, offset=page_mult)
    print url  # I added this in order to check my output

    request = urllib2.Request(url, headers={'Ajax-Request': 'researchProjections'})
    raw_json = urllib2.urlopen(request).read()
    parsed_json = json.loads(raw_json)
    html = parsed_json['content']

    soup = BeautifulSoup(html, "html.parser")
    table = soup.find('table', attrs={'class': 'tableType-player hasGroups'})
    table_body = table.find('tbody')

    rows = table_body.find_all('tr')
    for row in rows:
        cols = row.find_all('td')
        cols = [ele.text.strip() for ele in cols]
        data.append([ele for ele in cols if ele])  # Get rid of empty values

print data

rawDescription: Scraping multiple pages in one Beautiful Soup script -- getting same result


code:
from urlparse import urlparse
parsed_uri = urlparse( 'http://stackoverflow.com/questions/1234567/blah-blah-blah-blah' )
domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print domain

# gives
'http://stackoverflow.com/'

rawDescription: Get domain name from URL

description: Get domain name from URL


code:
#!/bin/python
# Copyright Arvid Norberg 2008. Use, modification and distribution is
# subject to the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

import libtorrent as lt
import time
import sys

ses = lt.session()
ses.listen_on(6881, 6891)

info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()

while (not h.is_seed()):
    s = h.status()

    state_str = ['queued', 'checking', 'downloading metadata', 'downloading', \
        'finished', 'seeding', 'allocating', 'checking fastresume']
    print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
        (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
        s.num_peers, state_str[s.state]),
    sys.stdout.flush()

    time.sleep(1)

print h.name(), 'complete'

rawDescription: Do you know Python libs to send / receive files using Bittorent?


code:
# Split array based on where the 'nan' values are
dataSets = filter(lambda x: len(x) > 0, np.array_split(alldata,np.where(np.isnan(alldata[:,1]))[0]))

# Delete the rows with 'nan' in it.
dataSets = [np.delete(dataSet, np.where(np.isnan(dataSet)), axis=0) for dataSet in dataSets]

startYear = 1966
endYear = 1981
startMonth = 1
endMonth = 12
blank_rainfall_value = np.nan

# Insert rows of the form ['nan', year, month, 0] for all relevant years/months except where there is already a row containing that year/month
extendedDataSets = []
for dataSet in dataSets:
    missingMonths = [[dataSet[0][0], year, month, blank_rainfall_value] for year in range(startYear, endYear+1) for month in range(startMonth, endMonth+1) if [year, month] not in dataSet[:,1:3].tolist()]
    if len(missingMonths) > 0:
        extendedDataSets.append(np.vstack((dataSet, missingMonths)))

# Sort arrray by year, then month
finalDataSets = [np.array(sorted(dataSet, key=lambda row:row[1]+row[2]/12.0)) for dataSet in extendedDataSets]

for dataSet in finalDataSets:
    print dataSet
    print

rawDescription: Split array vertically, add rows of data, sort, and then apply regression for rainfall data


code:
import re
print re.findall(r'\bv\w+', thesentence)

rawDescription: Simple Python Regex Find pattern

description: Simple Python Regex Find pattern


code:
print s
s2 = re.sub(r"(?<!')\s+", " ", s)
print s2

rawDescription: Regex to match linebreaks not preceded by non-escaped quotes in text file


code:
import csv
import os

os.chdir(r"C:\Users\Robert\Documents\qwe")

output_ary = []
with open("gdweights_feh_robert_cmr.csv",'rb') as f:
    reader = csv.reader(f, delimiter= ',')
    zerovar = 0
    for row in reader:
        if zerovar==0:
            zerovar = zerovar + 1
        else:
            sn = row [11]
            zerovar = zerovar + 1
            x = float(sn)
            if x > 20:
                print x
                output_ary.append(row)

with open("output.csv",'w') as f2:
    for row in output_ary:
        for item in row:
            f2.write(item + ",")

rawDescription: How to find specific rows in csv document in python

description: Find specific rows in csv document in python


code:
import sys
from PyQt4 import QtGui, QtCore

class MyPushButton(QtGui.QPushButton):
    def __init__(self, popMenu,elementID, mainForm):
        super(MyPushButton, self).__init__()
        self.__elementID = elementID
        self.__mainForm = mainForm
        self.__popMenu = popMenu

        self.connect(self, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
        self.connect(self, QtCore.SIGNAL('clicked()'),  self,        QtCore.SLOT("triggerOutput()"))

    def on_context_menu(self, point):
        # show context menu
        self.__popMenu.exec_(self.mapToGlobal(point))

    @QtCore.pyqtSlot()
    def triggerOutput(self):
        self.__mainForm.emit(QtCore.SIGNAL("buttonXclickedSignal(PyQt_PyObject)"), self.__elementID) # send signal to MainForm class

class MainForm(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)
        self.setGeometry(300, 300, 400, 200)
        VmasterLayout = QtGui.QVBoxLayout(self)
        self.Hbox = QtGui.QHBoxLayout()

        # Custom signal
        self.connect(self, QtCore.SIGNAL("buttonXclickedSignal(PyQt_PyObject)"),         self.buttonXclicked)

        for i in range(1,4):
            # create context menu as you like
            popMenu = QtGui.QMenu(self)
            popMenu.addAction(QtGui.QAction('button %s - test0'%(i), self))
            popMenu.addAction(QtGui.QAction('button %s - test1'%(i), self))
            popMenu.addSeparator()
            popMenu.addAction(QtGui.QAction('button %s - test2'%(i), self))

            # create button
            self.button = MyPushButton(popMenu, i, self)
            self.button.setText("test button %s" %(i))
            self.button.resize(100, 30)

            # set button context menu policy
            self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

            self.Hbox.addWidget(self.button)

        VmasterLayout.addLayout(self.Hbox)

    def buttonXclicked(self, buttonID):
        if buttonID == 1:
            #do something , call some method ..
            print "button with ID ", buttonID, " is clicked"
        if buttonID == 2:
            #do something , call some method ..
            print "button with ID ", buttonID, " is clicked"
        if buttonID == 3:
            #do something , call some method ..
            print "button with ID ", buttonID, " is clicked"

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: python pyqt4 contextMenu on multiple pushButtons

description: Python pyqt4 contextMenu on multiple pushButtons


code:
def format_dict(d):
    key = d.keys()[0]
    return '--%s=%s' % (key, d[key])

ex = [{'a':'3'}, {'b':'4'}]
print ' '.join(map(format_dict, ex)) # --a=3 --b=4

rawDescription: Method of expanding list of dictionaries into list of formatted strings