code:
import sys

import pygame
from pygame.locals import *

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

UP   = 1
DOWN = 2

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

class Player():

    def __init__(self, screen, x, y):

        self.screen = screen

        self.rect = pygame.Rect(0,0,0,0)

        self.image = pygame.image.load('player.png')

        self.rect.size = self.image.get_size()

        self.rect.center = (x, y)

        self.speed_x = 0
        self.speed_y = 15

        self.direction = None

    def draw(self):
        self.screen.blit(self.image,(self.rect.x,self.rect.y))

    def move(self):
        if self.direction:
            if self.direction == UP :
                self.rect.y -= self.speed_y
                if self.rect.y < 0 :
                    self.rect.y = 0
            elif self.direction == DOWN :
                self.rect.y += self.speed_y
                if self.rect.y > self.screen.get_size()[1] - self.rect.height :
                    self.rect.y = self.screen.get_size()[1] - self.rect.height

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

class Ball():

    def __init__(self, screen, x, y):

        self.screen = screen

        self.rect = pygame.Rect(0,0,0,0)

        self.image = pygame.image.load('ball.png')

        self.rect.size = self.image.get_size()

        self.rect.center = (x, y)

        self.speed_x = 15
        self.speed_y = 15

    def draw(self):
        self.screen.blit(self.image,(self.rect.x,self.rect.y))

    def move(self):
        self.rect.x += self.speed_x
        self.rect.y += self.speed_y

        if self.rect.left < 0 or self.rect.right > self.screen.get_size()[0]:
            self.speed_x = -self.speed_x
        if self.rect.top < 0 or self.rect.bottom > self.screen.get_size()[1]:
            self.speed_y = -self.speed_y

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

class Game():

    def __init__(self):

        self.FPS = 25

        pygame.init()

        self.fpsClock = pygame.time.Clock()

        self.background = pygame.image.load('background.jpg')
        self.size = self.width, self.height = self.background.get_size()

        self.screen = pygame.display.set_mode(self.size,0,32)

        self.ball = Ball(self.screen, 100, 100)

        self.player1 = Player(self.screen, 35, self.height/2)
        self.player2 = Player(self.screen, self.width-35, self.height/2)

        print self.screen.get_rect().center

        font = pygame.font.SysFont("", 72)

        self.text_paused = font.render("PAUSE", True, (255, 0, 0))

        print self.text_paused.get_rect()

        screen_rect = self.screen.get_rect()
        self.text_rect = self.text_paused.get_rect()
        self.text_rect.center = screen_rect.center

        print self.text_rect

    def run(self):

        RUNNING = True

        PAUSED = False
        while RUNNING:

            # --- events ---

            for event in pygame.event.get():
                if event.type==QUIT:
                    RUNNING = False

                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        RUNNING = False
                    elif event.key == K_SPACE:
                        PAUSED = not PAUSED
                    elif not PAUSED:
                        if event.key == K_w:
                            self.player1.direction = UP
                        elif event.key == K_s:
                            self.player1.direction = DOWN
                        elif event.key == K_UP:
                            self.player2.direction = UP
                        elif event.key == K_DOWN:
                            self.player2.direction = DOWN

                if event.type == KEYUP:
                    if not PAUSED:
                        if event.key in (K_w, K_s):
                            self.player1.direction = None
                        elif event.key in (K_UP, K_DOWN):
                            self.player2.direction = None

            # --- recalculations ---

            if not PAUSED:
                self.player1.move()
                self.player2.move()
                self.ball.move()

            #~ print self.ball.get_rect()

            if pygame.sprite.collide_rect(self.ball, self.player1):
                self.ball.speed_x = -self.ball.speed_x

            if pygame.sprite.collide_rect(self.ball, self.player2):
                self.ball.speed_x = -self.ball.speed_x

            # --- drawing ---

            self.screen.blit(self.background,(0,0))
            self.player1.draw()
            self.player2.draw()
            self.ball.draw()

            if PAUSED:

                self.screen.blit(self.text_paused, self.text_rect.topleft)

            pygame.display.update()

            # --- FPS ---

            self.fpsClock.tick(self.FPS)

        # --- finish ---

        pygame.quit()
        sys.exit()

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

Game().run()

rawDescription: Pygame collision interaction

description: Pygame collision interaction


code:
def getter(name):
    def get_prop(self):
        print 'Getting {}'.format(name)
        return getattr(self, name)
    return get_prop

def setter(name):
    def set_prop(self, value):
        print 'Setting {} to {}'.format(name, value)
        return setattr(self, name, value)
    return set_prop

class User(object):
    user_id = property(getter('_user_id'), setter('_user_id'))

rawDescription: Python properties: why doesn't this code run the function?


rawDescription: For each matching element in two lists add a list and extend


code:
team_member = Team.objects.get(id=team_member_id)
if team_member.site.id == request.user.site_set.all().first().id:
   print "same site"
else:
   print "different site"

rawDescription: How to authenticate if a user "owns" a model instance

description: Authenticate if a user "owns" a model instance


code:
import subprocess
import time
import sys

class Timeout(Exception):
    pass

def run(command, timeout=10):
    proc = subprocess.Popen(command, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    poll_seconds = .250
    deadline = time.time()+timeout
    while time.time() < deadline and proc.poll() == None:
        time.sleep(poll_seconds)

    if proc.poll() == None:
        if float(sys.version[:3]) >= 2.6:
            proc.terminate()
        raise Timeout()

    stdout, stderr = proc.communicate()
    return stdout, stderr, proc.returncode

if __name__=="__main__":
    print run(["ls", "-l"])
    print run(["find", "/"], timeout=3) #should timeout

rawDescription: python: run a process with timeout and capture stdout, stderr and exit status

description: Python: run a process with timeout and capture stdout, stderr and exit status


code:
s = 'a.pdf'
print s.replace('pdf', 'jpg')

rawDescription: What is the most elegant way of converting the string 'a.pdf' to 'a.jpg'?


code:
import sys
print sys.executable

rawDescription: Finding python.exe programatically in python


code:
#!/usr/bin/env python

for line in open('data.txt'):
    company, year, number_of_employee = line.split(',')
    print "Company: %s" % company

rawDescription: Python Reading a file print several information


code:
from bs4 import BeautifulSoup
import mechanize

url = "http://www.minyanville.com/business-news/editors-pick/articles/A-Russian-Bank-Is-Sued-for/8/7/2013/id/51205"
br = mechanize.Browser()
htmltext = br.open(url).read()
soup = BeautifulSoup(htmltext)
for tag in soup.findAll('span',{'itemprop':'articleBody'}):
    print tag.text

rawDescription: Python Scraping, how to get text from this through BeautifulSoup?


code:
import re
input = 'ABC @XYZ ABC @Python ABC http://www.stackoverflow.com ABC'
output = re.sub(r'(http|@)\S*\s', '', input)
print output  # 'ABC ABC ABC ABC'

rawDescription: How can I delete regex substrings in Python?

description: Delete regex substrings in Python


code:
from itertools import product

comb = product(my_map, ['abc','123','a1'])
mapped = ((p(x),f) for (p,f),x in comb)
groups = (f(v.groups()) for v,f in mapped if v)
print next(groups), list(groups) # first match and the rest of them

rawDescription: is there a way to make this code more terse?

description: Is there a way to make this code more terse


code:
import ast
with open('mymodule.py') as f:
    tree = ast.parse(f.read())
for node in ast.walk(tree):
    if isinstance(node, ast.FunctionDef):
        print node.lineno, node.col_offset

rawDescription: How to differentiate between "a string" and "a actual code" in python?

description: Differentiate between "a string" and "a actual code" in python


code:
import sys

for line in fileinput.input(file_full_path, inplace=True):
    newline, count = re.subn(search_str, replace_str, line.rstrip())
    sys.stderr.write("your message here")
    print newline

rawDescription: Using fileinput (Python) for a search-and-replace while also sending messages to console


code:
def dixnary():
    return "Dictionary contents :\n%s\nLength = %d" % (d1, len(d1))

print dixnary()

rawDescription: Unable to understand results of Python code


code:
for k, v in d.items():
    print k, max(v)

rawDescription: How to find the maximum value for each key in a dictionary of lists?

description: Find the maximum value for each key in a dictionary of lists


code:
class Abcd:
    def __init__(self, name, number):
        self.name = name
        self.number = number

    def __str__(self):
        return "{} and {}".format(self.name, self.number)

    def __repr__(self):
        return self.__str__()

ax = Abcd("a", 20)
by = Abcd("b", 1)

lst = [ax, by]

print str(lst)
list.sort(lst, key=lambda x: x.number)
print str(lst)

rawDescription: Can you sort by subclass?

description: Sort by subclass


code:
import struct
print struct.pack('BB', 48, 51)
print '\x30\x33'
'\x30\x33' == '03'

rawDescription: Python pack string as ascii hex value

description: Python pack string as ascii hex value


code:
import numpy as np

def m(s):
    import sys
    frame = sys._getframe(1)
    return np.array([eval(item.strip(), frame.f_globals, frame.f_locals)
                         for item in s.split(";")])

def f():
    a = 1
    x = 3.14
    print m("1,2,3;np.sin(a),np.sqrt(2),x")

f()

rawDescription: When a string is used as an input into numpy.mat(), certain characters are ignored


code:
for j in xrange(windows_a.shape[0]):
    print j

    vmin, vmax = windows_b[j].min(), windows_b[j].max()

    pl.imshow(windows_a[j], vmin=vmin, vmax=vmax, interpolation='nearest')
    pl.xticks(np.arange(0,window_size_1,1))
    pl.yticks(np.arange(0,window_size_1,1))
    pl.gray()
    pl.grid( color = 'y' )
    pl.savefig('./IW_small/IW_small_' + str("%05d" %i) + "_" + str("%05d" %j) + '.png')
    pl.close()

    pl.imshow(windows_b[j], vmin=vmin, vmax=vmax, interpolation='nearest')
    pl.xticks(np.arange(0,window_size_2,1))
    pl.yticks(np.arange(0,window_size_2,1))
    pl.gray()
    pl.grid( color = 'y' )
    pl.savefig('./IW_big/IW_big_' + str("%05d" %i) + "_" + str("%05d" %j) + '.png')
    pl.close()

rawDescription: Making a subimage have the same intensity representation


code:
import pyHook
import pythoncom

def OnKeyboardEvent(event):
    print event.Ascii

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

while True:
    pythoncom.PumpMessages()

rawDescription: Python for Autohotkey style key-combination sniffing, automation?


code: print request.get_full_path()

rawDescription: processing urls with hashes like jsbin

description: Processing urls with hashes like jsbin


code:
import readline

def tab_completer(text, state):
    print "\ntext:", text

readline.parse_and_bind("tab: complete")
readline.set_completer(tab_completer)
raw_input('Prompt: ')

rawDescription: Python: bind a function to a key

description: Python: bind a function to a key


code:
import pyPdf
if pyPdf.PdfFileReader(open("file_name.pdf", 'rb')).isEncrypted:
    print "Rut ro, it's encrypted."
    # skip file? Write to a log?
else:
    print "We're clear."
    # Do stuff with the file.

rawDescription: Scan for secured pdf documents

description: Scan for secured pdf documents


code:
import numpy
import itertools as it
arr=numpy.random.rand(4,400)
transposed=arr.T
values=[numpy.dot(i,j) for i, j in it.combinations(transposed, 2)]
print values

rawDescription: apply a function to each pair of rows of an array

description: Apply a function to each pair of rows of an array


code:
closur=[]
i=6
dis_tn=distance[i]
headnode=x
dont_break = True
while dont_break:
    dont_break = False
    for k in headers[headnode]:
        if distance[k]<=dis_tn:
            if i in headers[k]:
                closur.append(x)
                closur.append(k)
                closur.append(i)
            else:
                headnode=k
                dont_break = True
                break
print "Closure of (",x,",",i,")= ",closur

rawDescription: Python for loop iteration using lists

description: Python for loop iteration using lists


code:
import time

date_time = '29.08.2011 11:05:02'
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
print epoch

rawDescription: How to convert current date to epoch timestamp?

description: Convert current date to epoch timestamp


code:
import re,sys,os
from PyQt4 import QtGui,QtCore
class projs(QtGui.QWidget):
    def __init__(self, parent=None):
        super(projs,self).__init__(parent)
        self.setWindowTitle(" V1.0 ")
        tab_widget = QtGui.QTabWidget()
        proj    = QtGui.QWidget()
        asset   = QtGui.QWidget()
        shots   = QtGui.QWidget()
        tab_widget.addTab(proj, "Tab01")
        tab_widget.addTab(asset, "Tab02")
        tab_widget.addTab(shots, "Tab03")
        self.projPathLabel = QtGui.QLabel("Project Path ",proj)
        self.projPathLabel.setGeometry(10,20,100,25)
        self.assetPathLabel = QtGui.QLabel("Project ",asset)
        self.assetPathLabel.setGeometry(20,20,100,25)
        self.shotPathLabel = QtGui.QLabel("Project ",shots)
        self.shotPathLabel.setGeometry(20,20,100,25)
        vbox = QtGui.QVBoxLayout()
        vuvupic = QtGui.QPixmap("C:\Users\name\Documents\config\Logo2.jpg")
        label = QtGui.QLabel(self)
        label.setPixmap(vuvupic)
        vbox.addWidget(label)
        vbox.addWidget(tab_widget)
        self.setLayout(vbox)
        p1_vbox = QtGui.QVBoxLayout(proj)
        proj.setLayout(p1_vbox)
        p2_vbox = QtGui.QVBoxLayout(asset)
        asset.setLayout(p2_vbox)
        p3_vbox = QtGui.QVBoxLayout(shots)
        shots.setLayout(p3_vbox)
app = QtGui.QApplication([])
win = projs()
win.resize(410,390)
win.show()
sys.exit(app.exec_())

rawDescription: load image from the Top in TabWidget from PyQt

description: Load image from the Top in TabWidget from PyQt


code:
class RequestHandler(SimpleXMLRPCRequestHandler):
    def __init__(self, request, client_address, server):
        print client_address # do what you need to do with client_address here
        SimpleXMLRPCRequestHandler.__init__(self, request, client_address, server)

rawDescription: IP address of client in Python SimpleXMLRPCServer?


code:
from threading import *
import wx
import time

EVT_RESULT_ID = wx.NewId()

def EVT_RESULT(win, func):
    win.Connect(-1, -1, EVT_RESULT_ID, func)

class MyGui(wx.Frame):
    def __init__(self):
        self.spellchkthrd = None
        #lots of stuff

        self.input = wx.TextCtrl(self.panel, -1, "", size=(200, 150), style=wx.TE_MULTILINE|wx.TE_LEFT|wx.TE_RICH)
        self.Bind(wx.EVT_TEXT, self.OnEdit, self.input)
        EVT_RESULT(self, self.OnSplCheck)

    def OnEdit(self, event):
        if not self.spellchkthrd:
            self.spellchkthrd = SpellCheckThread(self)
        else:
            self.spellchkthrd.newSig()

    def OnSplCheck(self, event):
        self.spellchkthrd = None
        #All the spell checking stuff

class ResultEvent(wx.PyEvent):
    def __init__(self):
        wx.PyEvent.__init__(self)
        self.SetEventType(EVT_RESULT_ID)

class SpellCheckThread(Thread):
    def __init__(self, panel):
        Thread.__init__(self)
        self.count = 0
        self.panel = panel
        self.start()

    def run(self):
        while self.count < 1.0:
            print self.count
            time.sleep(0.1)
            self.count += 0.1

        wx.PostEvent(self.panel, ResultEvent())

    def newSig(self):
        print "new"
        self.count = 0

rawDescription: How do I handle multiple EVT_TEXT events in wxPython?

description: Handle multiple EVT_TEXT events in wxPython


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

Base = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=False)

Session = sessionmaker(bind=engine)

class TestAuditLog(Base):
    __tablename__ = 'TEST_AUDIT_LOG'
    id = Column(Integer, primary_key=True)
    comment = Column(String)

    entityId = Column('TEST_AUDIT_LOG', Integer, nullable=False,
                     # POINT-1
                     #ForeignKey('TEST.TS_TEST_ID', ondelete="CASCADE"),
                     )

    def __init__(self, comment):
        self.comment = comment

    def __repr__(self):
        return "<TestAuditLog(id=%s entityId=%s, comment=%s)>" % (self.id, self.entityId, self.comment)

class Test(Base):
    __tablename__ = 'TEST'
    id = Column('TS_TEST_ID', Integer, primary_key=True)
    name = Column(String)

    audits = relationship(TestAuditLog, backref='test',
                # POINT-2
                primaryjoin="Test.id==TestAuditLog.entityId",
                foreign_keys=[TestAuditLog.__table__.c.TEST_AUDIT_LOG],
                # POINT-3
                passive_deletes='all',
            )

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return "<Test(id=%s, name=%s)>" % (self.id, self.name)

Base.metadata.create_all(engine)

###################
## tests
session = Session()

# create test data
tests = [Test("test-" + str(i)) for i in range(3)]
_cnt = 0
for _t in tests:
    for __ in range(2):
        _t.audits.append(TestAuditLog("comment-" + str(_cnt)))
        _cnt += 1
session.add_all(tests)
session.commit()
session.expunge_all()
print '-'*80

# check test data, delete one Test
t1 = session.query(Test).get(1)
print "t: ", t1
print "t.a: ", t1.audits
session.delete(t1)
session.commit()
session.expunge_all()
print '-'*80

# check that audits are still in the DB for deleted Test
t1 = session.query(Test).get(1)
assert t1 is None
_q = session.query(TestAuditLog).filter(TestAuditLog.entityId == 1)
_r = _q.all()
assert len(_r) == 2
for _a in _r:
    print _a

rawDescription: SQLAlchemy - don't enforce foreign key constraint on a relationship


code:
class Fruit:
    Apple = 0
    Banana = 0
    Grape = 0

    def __repr__(self):
         return "You have {self.Apple} apples, {self.Banana} bananas, and {self.Grape} grapes".format(**locals())

    def update(self):
       while 1:
         choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>')
         if (int(choice) == 1):
            self.Add()
         elif (int(choice) == 2):
            self.Clear()
         else:
             print "Input not valid"

    def Add(self):
       self.Apple += int(raw_input('How many apples do you want to add?\n>>'))
       self.Banana += int(raw_input('How many bananas do you want to add?\n>>'))
       self.Grape += int(raw_input('How many grapes do you want to add?\n>>'))
       print self

    def Clear(self):
       self.Apple = 0
       self.Banana = 0
       self.Grape = 0
       print self

if __name__ == "__main__":
    fruit = Fruit()
    fruit.update()

rawDescription: Updating and clearing dictionaries in Python


code:
class Connection:
    def __init__(self, socket, protocol):
        self.socket = socket
        self.protocol = protocol

    def doRead(self):
        try:
            data = self.socket.recv(self.bufferSize)
        except socket.error, se:
            if se.args[0] == EWOULDBLOCK:
                return
            else:
                return main.CONNECTION_LOST
        if not data:
            return main.CONNECTION_DONE
        return self.protocol.dataReceived(data)

rawDescription: adding a doRead() method to an existing socket object

description: Adding a doRead() method to an existing socket object


code:
from pyVim.connect import SmartConnect
from pyVmomi import vim
si = SmartConnect(host='10.12.254.137', user='administrator@vsphere.local', pwd='password')
content = si.RetrieveContent()
children = content.rootFolder.childEntity
for child in children:
    print child

'vim.Datacenter:datacenter-33'
'vim.Datacenter:datacenter-2'
children[0].name
'1000110'
dc = vim.Datacenter('datacenter-33')
dc._stub = si._stub
dc.name
'1000110'

rawDescription: get vmId via pyVmomi

description: Get vmId via pyVmomi


code:
class Red(object):
    def x(self):
        print '#F00'

class Blue(object):
    def x(self):
        print '#00F'

class Shape(object):
    def __init__(self,color):
        self._color=color
    def x(self):
        return self._color.x()

class Circle(Shape):
    def __init__(self, color):
        Shape.__init__(self,color)
        self.x()

class Square(Shape):
    def __init__(self, color):
        Shape.__init__(self,color)
        self.x()
        self.sides = 4

red_circle = Circle(color=Red())
blue_circle = Circle(color=Blue())
blue_square = Square(color=Blue())

rawDescription: Set an object's superclass at __init__?

description: Set an object's superclass at __init__


code:
import time                             # import time library
import datetime                         # this is the way to import a module named datetime...
import pytz                             # imports Python time zone (needed to first do: sudo easy_install --pytz upgrade)
now = datetime.datetime.now(pytz.timezone('US/Pacific'))
timenow = now.strftime("%I:%M %p")
print timenow
if timenow > "08:30 PM" and timenow < "08:45 PM":
   print "Programmed Pause"
   time.sleep(999999)

rawDescription: How To Use The Current Local Time of Day To Control A Process Without Dates


code:
from bottle import route, run, template
import bottle

@route('/hello/:name')
def index(name='World'):
    print "Debug Print Statement"
    return template('<b>Hello {{name}}</b>!', name=name)

class WSGIRefServer(bottle.ServerAdapter):
    def run(self, handler): # pragma: no cover
        from wsgiref.simple_server import make_server, WSGIRequestHandler

        class LogHandler(WSGIRequestHandler):
            def log_request(self, code='-', size='-'):
                """Log an accepted request.

                This is called by send_response().

                """
                if code not in  ["200", "304"]:
                    self.log_message('"%s" %s %s',
                                     self.requestline, str(code), str(size))

        self.options['handler_class'] = LogHandler
        srv = make_server(self.host, self.port, handler, **self.options)
        srv.serve_forever()

run(host='localhost', server=WSGIRefServer, port=8080)

rawDescription: Is there any way to make the Bottle servers less verbose?


code:
import re
seq=">Bounded_RNA_of:1DDL:Elength : 1"
match = re.search(r'>Bounded_RNA_of:(\w+):(\w)length : 1',seq)
print match.group(1), match.group(2)

rawDescription: Capturing subset of a string using Python's regex


rawDescription: Print output in a single line

description: Print output in a single line


code:
print '''
This uses the "here document" syntax to output
multiple lines
'''

rawDescription: Python - print until marker (a la PHP)?

description: Python - print until marker (a la PHP)


code:
from bs4 import BeautifulSoup
from bs4 import NavigableString

s = """your HTML here"""

soup = BeautifulSoup(s)
headers = soup.find_all("h2")
for h in headers:
   print h.text
   for s in h.next_siblings:
      if s.name == "h2":
         break
      elif isinstance(s, NavigableString):
         print s.string

rawDescription: How to parse items which don't have class/id using BeautifulSoup

description: Parse items which don't have class/id using BeautifulSoup


code:
import re
f=open('test.file.here', 'r')

pattern = re.compile("^[^\s]*ello[^\s]*\sWorld[^\s]*$")

for line in f:
  if pattern.match(line):
    print line,

f.close()

rawDescription: How can I create search terms with wildcards in Python?

description: Create search terms with wildcards in Python


code:
'Gen\xc3\xa8ve'.decode('latin1')
print 'Gen\xc3\xa8ve'.decode('latin1')
'Gen\xc3\xa8ve'.decode('utf8')
print 'Gen\xc3\xa8ve'.decode('utf8')

rawDescription: Python who to write to file encoding 'latin-1'


code:
def Print(n, s):
    i=0
    while i<n:
        print s
        i+=1

n=3
while True:
    Print(n, 'Yes')
    Print(n, 'No')

rawDescription: Repeat print after every n prints

description: Repeat print after every n prints


code:
from collections import OrderedDict
positions = OrderedDict()
with open('test.txt') as f:
    for line in f:
        key, ele = line.strip().split()
        positions[key] = positions.get(key, []) + [ele]
print positions

rawDescription: Defaultdict appending trick

description: Defaultdict appending trick


code:
def analizarRT():
    timeline = []
    fileRT = openFile('rt.txt')
    for status in api.user_timeline('cnn', count='6'):
        timeline.append(status)
    for i in range(6):
        for b in range(6):
            if timeline[i].id == fileRT[b].id:
                pass
            else:
                newlist.append(timeline[i].id)
    print newlist

rawDescription: Compare two lists in python and obtain non-equality


code:
t=[i.replace("\'","").replace("?","").replace(":","").replace("\"","").replace("#","").strip()
  for i in file.read().split() if i not in stop]
#blog_posts.append((t,))
p=' '.join(t)
blog_posts.append((p,))
print blog_posts

rawDescription: How to remove quotes after removing stopwords from nltk?

description: Remove quotes after removing stopwords from nltk


rawDescription: Make sorted representation of dict of lists


rawDescription: Code object and purpose of co_name attribute


code:
import matplotlib.pyplot as plt

def on_pick(event):
    artist = event.artist
    xmouse, ymouse = event.mouseevent.xdata, event.mouseevent.ydata
    x, y = artist.get_xdata(), artist.get_ydata()
    ind = event.ind
    print 'Artist picked:', event.artist
    print '{} vertices picked'.format(len(ind))
    print 'Pick between vertices {} and {}'.format(min(ind), max(ind)+1)
    print 'x, y of mouse: {:.2f},{:.2f}'.format(xmouse, ymouse)
    print 'Data point:', x[ind[0]], y[ind[0]]
    print

fig, ax = plt.subplots()

tolerance = 10 # points
ax.plot(range(10), 'ro-', picker=tolerance)

fig.canvas.callbacks.connect('pick_event', on_pick)

plt.show()

rawDescription: Matplotlib / python clickable points

description: Matplotlib / python clickable points


code:
import multiprocessing
import os

def getAllFiles():
    my_files = list()
    for root, dirs, files in os.walk("C:/Users/test"):
        for file in files:
            if file.endswith(".txt"):
                file_path= os.path.join(root, file)
                my_files.append(file_path)
    return my_files

def calc(file):
    with open(file, 'r') as f:
        return sum(map(int, f.readlines()))

if __name__ == '__main__':
    my_files = getAllFiles()
    num_fetch_threads = len(my_files)
    pool = multiprocessing.Pool(processes=num_fetch_threads)
    results = pool.map(calc, my_files)
    result = sum(results)
    print result

rawDescription: Sum of values in a function called from thread


code:
from array import array
from datetime import datetime

def WriteInts(theArray,filename):
    f = file(filename,"wb")
    theArray.tofile(f)
    f.close()

def ReadInts(filename):
    d = datetime.utcnow()
    theArray = array('i')
    f = file(filename,"rb")
    try:
        theArray.fromfile(f,1000000000)
    except EOFError:
        pass
    print "Read %d ints in %s" % (len(theArray),datetime.utcnow() - d)
    return theArray

if __name__ == "__main__":
    a = array('i')
    a.extend(range(0,1000000))
    filename = "a_million_ints.dat"
    WriteInts(a,filename)
    r = ReadInts(filename)
    print "The 5th element is %d" % (r[4])

rawDescription: Python list serialization - fastest method


code:
import autopy
from time import sleep

def color_checker():
    color_grabber = hex(autopy.bitmap.Bitmap.open("screen1.png").get_color(292,115))
    return color_grabber

def mouse_move_click():
    autopy.mouse.smooth_move(433,320)
    autopy.mouse.click()

def screen_grab():
    autopy.bitmap.capture_screen().save("screen1.png")

def the_ifs(color):
    if color == "0xffcb05":
        mouse_move_click()

while 1==1:
    sleep(1)
    screen_grab()
    color = color_checker()
    the_ifs(color)
    print color

rawDescription: python autopy problems/confusion

description: Python autopy problems/confusion


code:
def start2():
    t= int (raw_input())
    t0 = time()

    while(True):
        if(is_palindrome(t)):
            apple=is_prime(t)
            if(apple):
                print t
                print "Execution time:", round(time()-t0, 3), "s"
                break
        t=t+1

rawDescription: Difference in execution time for a query in Python


code:
from BeautifulSoup import BeautifulSoup, NavigableString, Tag

input = '''

Important Text 1

Not Important Text

Important Text 2

Important Text 3

Non Important Text

Important Text 4

'''

soup = BeautifulSoup(input)

for br in soup.findAll('br'):
    next = br.nextSibling
    if not (next and isinstance(next,NavigableString)):
        continue
    next2 = next.nextSibling
    if next2 and isinstance(next2,Tag) and next2.name == 'br':
        text = str(next).strip()
        if text:
            print "Found:", next

rawDescription: Using beautifulsoup to extract text between line breaks (e.g. <br /> tags)


code:
import gtk.gdk
import sys

def PixelAt(x, y):
    w = gtk.gdk.get_default_root_window()
    sz = w.get_size()
    pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
    pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
    pixel_array = pb.get_pixels_array()
    return pixel_array[y][x]

print PixelAt(int(sys.argv[1]), int(sys.argv[2]))

rawDescription: How can I grab the color of a pixel on my desktop? (Linux)

description: Grab the color of a pixel on my desktop? (Linux)


code:
#!/usr/bin/env python

d = {1:2, 3:4, 5:6, 7:8, 9:0}

# If you want an ordered dictionary (and have python 2.7/3.2),
# uncomment the next lines:

# from collections import OrderedDict
# d = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

last = len(d) - 1

for i, x in enumerate(d):
    if i == last:
        print i, x, 'last'
    else:
        print i, x

# Output:
# 0 1
# 1 3
# 2 9
# 3 5
# 4 7 last

rawDescription: Identify which iteration you are on in a loop in python


code:
words_list = ["Bluebird, 4005", "ABCD, 1", "EFGH, 2677", "IJKL, 2"]
print sorted(words_list, key = lambda x: int(x.split(",")[1]))
# ['ABCD, 1', 'IJKL, 2', 'EFGH, 2677', 'Bluebird, 4005']

rawDescription: Sort list after numerical value in string


code:
class RPCConnection(object, Telnet):
  def __init__(self, host = 'localhost', port = 9198, auto = True):
    Telnet.__init__(self)

    self.host, self.port, self.connected = host, port, False

    if auto:
      self._connect()

  def _connect(self):
    self.open(self.host, self.port)

    self.connected = True

  def _disconnect(self):
    self.close()

    self.connected = False

  def _send(self, dict):
    self.write(json.dumps(dict))

  def _recv(self):
    resp = self.read_until('\n')

    try:
      return json.loads(resp)
    except Exception, e:
      print e
      print resp

rawDescription: Push messages to clients in Python

description: Push messages to clients in Python


code:
from pyquery import PyQuery as pq

doc = pq(url='https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')

for heading in doc(".mw-headline:contains('S&P 500 Component Stocks')").parent("h2"):
    rows = pq(heading).next("table tr")
    for row in rows:
        tds = pq(row).find("td")
        print tds.eq(0).text()

rawDescription: Python 3.4 : Regex list grouping

description: Python 3.4 : Regex list grouping


code:
from javax.xml.stream import XMLStreamConstants, XMLInputFactory, XMLStreamReader
from java.io import ByteArrayInputStream;
from java.lang import String

xml = String(
"""<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
  <employee id="111">
    <firstName>Rakesh</firstName>
    <lastName>Mishra</lastName>
    <location>Bangalore</location>
  </employee>
  <employee id="112">
    <firstName>John</firstName>
    <lastName>Davis</lastName>
    <location>Chennai</location>
  </employee>
  <employee id="113">
    <firstName>Rajesh</firstName>
    <lastName>Sharma</lastName>
    <location>Pune</location>
  </employee>
</employees>
""")

class Employee:
    id = None
    firstName = None
    lastName = None
    location = None

    def __str__(self):
        return self.firstName + " " + self.lastName + "(" + self.id + ") " + self.location

factory = XMLInputFactory.newInstance();
reader = factory.createXMLStreamReader(ByteArrayInputStream(xml.getBytes()))
employees = []
employee = None
tagContent = None

while reader.hasNext():
    event = reader.next();

    if event == XMLStreamConstants.START_ELEMENT:
        if "employee" == reader.getLocalName():
            employee = Employee()
            employee.id = reader.getAttributeValue(0)
    elif event == XMLStreamConstants.CHARACTERS:
        tagContent = reader.getText()
    elif event == XMLStreamConstants.END_ELEMENT:
        if "employee" == reader.getLocalName():
            employees.append(employee)
        elif "firstName" == reader.getLocalName():
            employee.firstName = tagContent
        elif "lastName" == reader.getLocalName():
            employee.lastName = tagContent
        elif "location" == reader.getLocalName():
            employee.location = tagContent

for employee in employees:
    print employee

rawDescription: SAX IncrementalParser in Jython

description: SAX IncrementalParser in Jython


code:
f = open('randomthing.py', 'rU')
fdata = f.read()
newlines = f.newlines
print repr(newlines)

rawDescription: Preserve end-of-line style when working with files in python


code:
import numpy as np

def asobjectarray(alist):
    r = np.ndarray(len(alist), object)
    r[:] = alist
    return r

a = np.asarray([[1]])
b = np.asarray([[1,2]])
r = asobjectarray([a,b])
f=lambda x,y:x[:,0]+y
print f(*r)

rawDescription: Correct way of passing arguments which is an array of arrays


code:
from itertools import product, combinations

otheroptions = ['A','B',   'D','E','F','G','H','I','J']
c = "C"
l = 10
nc = 3

for t in product(otheroptions, repeat=l-nc):
    for p in combinations(range(l), nc):
        r = list(t)
        for i in p:
            r[i:i] = [c]
        print "".join(r)

rawDescription: Permutation method in Python with some fixed elements


code:
with open(sys.argv[1], 'rb') as f:
    contents = f.read()
    for chunk in contents.split('\xaa\xaa')[1:]:
        length = ord(chunk[0])
        data = chunk[10:10+length]
        print data

rawDescription: What is the Pythonic way to implement a simple FSM?


code: print var1[BOOT][0]

rawDescription: python, read inside dict value as single value without "[" and "]"

description: Python, read inside dict value as single value without "[" and "]"


code:
print sql.execute('SELECT * FROM testTable WHERE testColumn IN ("{}")'.format(u'", "'.join(tests).encode('utf-8')), engine).fetchall()
print pd.read_sql_query('SELECT * FROM testTable WHERE testColumn IN ("{}")'.format(u'", "'.join(tests).encode('utf-8')), engine)

rawDescription: pandas.io sql.execute() variable-unicode-query-param with IN operator

description: Pandas.io sql.execute() variable-unicode-query-param with IN operator


code:
import collections

def make_constants(variables):
    n = collections.namedtuple('Constants', variables.keys())
    return n(**variables)

c = make_constants({"a": 2, "b": 76})
print c
# the output is:
# Constants(a=2, b=76)

rawDescription: Request for comments: python class factory for group of constant values


code:
import cgi
form = cgi.FieldStorage()
v = form.getvalue('id_text','')

print """Content-type: text/html

<html>
<body> submit value:%s </body>
</html>""" % (v)

rawDescription: How can i do python form post request?

description: Do python form post request


code:
import inspect
def f1(): f2()

def f2():
  curframe = inspect.currentframe()
  calframe = inspect.getouterframes(curframe, 2)
  print 'caller name:', calframe[1][3]

f1()

rawDescription: Python: How to get the caller's method name in the called method?


rawDescription: BottlyPy - How to read UWSGI_SCHEME?

description: BottlyPy - How to read UWSGI_SCHEME


code:
for key, grp in itertools.groupby(sorted(raw), key=operator.itemgetter(0)):
    print key, map(operator.itemgetter(1), grp)

# 1 ['one', 'one']
# 2 ['two', 'two']
# 3 ['three']

rawDescription: How to use itertools.groupby when the key value is in the elements of the iterable?

description: Use itertools.groupby when the key value is in the elements of the iterable


code:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt

x,y,z = zip((0,1,3),(0,8,9),(1,5,24))

print x
# (0, 0, 1)

print y
# (1, 8, 5)

print z
# (3, 9, 24)

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.2)
plt.show()

rawDescription: How can I plot surface on mplot3d for data like (x,y,z)=(0,0,1),(0,0,2)

description: Plot surface on mplot3d for data like (x,y,z)=(0,0,1),(0,0,2)


code:
import urllib
import urllib2
import shutil
import urlparse
import os
import sys
import re
import string
import unicodedata
import traceback
from titlecase import titlecase

class PubMedObject:
    doi = ''
    pmid = False
    pubmed_url = ''
    external_url = ''
    local_file = ''
    html_file = ''

    journal = ''
    authors = []
    title = ''
    pages = ''
    pub_year = ''
    volume = ''

    def __init__(self, doi_or_pmid):
        doi_or_pmid = doi_or_pmid.strip()
        self.pmid = (doi_or_pmid[:4] == 'pmid')

        #initialize URL for downloading
        if self.pmid:
            self.doi = doi_or_pmid[5:]
            self.pubmed_url = 'http://www.ncbi.nlm.nih.gov/pubmed/' + self.doi
            self.external_url = 'http://www.ncbi.nlm.nih.gov/pubmed/' + self.doi
        else:
            self.doi = doi_or_pmid
            self.pubmed_url = 'http://www.ncbi.nlm.nih.gov/pubmed/?term=' + self.doi
            self.external_url = 'http://dx.doi.org/' + self.doi

        #initialize name of local file
        self.local_file = getproperfilename(urllib.quote_plus(self.doi)) + '.html'

        #delete the local file if it already exists
        if os.path.isfile(self.local_file):
            os.remove(self.local_file)

    #call this before using anything in the object
    def download(self):
        response = urllib2.urlopen(urllib2.Request(self.pubmed_url))
        try:
            with open(self.local_file, 'wb') as f:
                self.html_file = response.read()
                f.write(self.html_file)
                f.close()
                #shutil.copyfileobj(response, f)

            #self.html_file = open(self.local_file,'r').read()
        except:
            print "Tried to download " + self.pubmed_url + ", but got this error."
            print sys.exc_info()[0]
        finally:
            response.close()

    #auths is the string that has the list of authors to return
    def get_authors(self, auths):
        result = []
        authors = re.sub(r'<[^<]+?>', '', auths[:-1]).split(', ')
        for author in authors:
            lname, name = author.split(' ')
            #add periods after each letter in the first name
            fname = ''
            for c in name:
                fname += c + '.'
            result.append(lname + ', ' + fname)
        self.authors = result
        return self.authors

    #call this after downloading the object
    def fill_data(self):
        try:
            matches = re.search('

' +
                                '(?P<journal>.*) (?P<bib_year>\d{4}).*?;(?P<bib_volume>.*?):' +
                                '(?P<bib_pages>.*?)\..*

(?P<title>.*?)

(?P<auths>.*?)
' +
                                '
.*
(?P<aff>.*?)

', self.html_file)

            self.title = titlecase(matches.group('title'))
            #print "Title: " + self.title
            self.get_authors(matches.group('auths'))
            #print "Authors: %s" % ', '.join(self.authors)
            self.journal = matches.group('journal')
            #print "Journal: " + self.journal
            self.pub_year = matches.group('bib_year')
            #print "Pub_year: " + self.pub_year
            self.volume = matches.group('bib_volume')
            #print "Volume: " + self.volume
            self.pages = matches.group('bib_pages')
            #print "Pages: " + self.pages
        except AttributeError as e:
            print "Not enough information was found for\n\t" + self.pubmed_url
            pass

    def get_formatted(self):
        new_file = ''
        new_file += ('\t
\n\t\t')
        new_file +=     ('	\n') % (self.external_url)
        new_file += ('\t\t%s
\n') % (self.external_url, self.title)
        new_file += ('\t\t%s
\n') % (', '.join(self.authors))
        new_file += ('\t\t%s, %s, %s, %s.	\n') % (self.journal, self.pub_year, self.volume, self.pages)
        new_file += ('\t
\n')
        return new_file

def getproperfilename(s):
    validFilenameChars = "-_.() %s%s" % (string.ascii_letters, string.digits)
    return ''.join(c for c in s if c in validFilenameChars)

def process_doi(doi):
    doi_object = PubMedObject(doi)
    try:
        #print "Downloading " + doi_object.pubmed_url + "..."
        doi_object.download()
        doi_object.fill_data()
        return doi_object.get_formatted()
    except urllib2.HTTPError:
        print "Skipping " + doi_object.pubmed_url + "..."
        return ''

def main(args):
    try:
        # program's main code here
        # You can also enter a PubMed id using the syntax pmid:xxxxxxxx
        print "Enter the doi to process.  Leave blank to use the dois.txt file."
        print "You can also enter a PubMed id using the syntax pmid:xxxxxxxx"
        doi = raw_input()
        if doi == '':
            with open('dois.txt','r') as doi_file:
                for doi in doi_file:
                    with open('simple.html','a') as sum_file:
                        sum_file.write(process_doi(doi))
                doi_file.close()
        else:
            print process_doi(doi)

    except BaseException as e:
        print traceback.format_exc()
        print "Error: %s %s" % (sys.exc_info()[0], e.args)
        return 1
    except:
        # error handling code here
        print "Error: %s" % sys.exc_info()[0]
        return 1  # exit on error
    else:
        return 0  # exit errorlessly

if __name__ == '__main__':
    sys.exit(main(sys.argv))

rawDescription: Obtaining data from PubMed using python

description: Obtaining data from PubMed using python


code:
for i in range(len(df.columns)):
    for index, row in df.iterrows():
        print row.ix[i]

rawDescription: How to access column via index when using iterrows()

description: Access column via index when using iterrows()


code:
import random
bingo=["H", "He", "C", "O"]
random.shuffle(bingo)
for item in bingo:
    if input("Bingo?") == "no":
        print item
    else:
        break

rawDescription: How can I display and then delete a random string from a list?

description: Display and then delete a random string from a list


code:
import pandas as pd
print pd.DataFrame(yourArray)

rawDescription: Numpy 2D array to Table

description: Numpy 2D array to Table


code:
import csv
import shutil

def createReferenceIdToPartNumberMap(csvToReadPath):
    result = {}
    print 'read part numbers to update from', csvToReadPath
    with open(csvToReadPath, 'rb') as csvInFile:
        csvReader = csv.DictReader(csvInFile)
        for row in csvReader:
            result[row['ReferenceID']] = row['PartNumber']
    return result

def updatePartNumbers(csvToUpdatePath, referenceIdToPartNumberMap):
    tempCsvPath = 'temp.csv'
    print 'update part numbers in', csvToUpdatePath
    with open(csvToUpdatePath, 'rb') as csvInFile:
        csvReader = csv.reader(csvInFile)

        # Figure out which columns contain the reference ID and part number.
        titleRow = csvReader.next()
        referenceIdColumn = titleRow.index('ReferenceID')
        partNumberColumn = titleRow.index('PartNumber')

        # Write tempoary CSV file with updated part numbers.
        with open(tempCsvPath, 'wb') as tempCsvFile:
            csvWriter = csv.writer(tempCsvFile)
            csvWriter.writerow(titleRow)
            for row in csvReader:
                # Check if there is an updated part number.
                referenceId = row[referenceIdColumn]
                newPartNumber = referenceIdToPartNumberMap.get(referenceId)
                # If so, update the row just read accordingly.
                if newPartNumber is not None:
                    row[partNumberColumn] = newPartNumber
                    print '  update part number for %s to %s' % (referenceId, newPartNumber)
                csvWriter.writerow(row)

        # TODO: Move the temporary CSV file over the initial CSV file.
        # shutil.move(tempCsvPath, csvToUpdatePath)

if __name__ == '__main__':
    referenceIdToPartNumberMap = createReferenceIdToPartNumberMap('Sheet1.csv')
    updatePartNumbers('Sheet2.csv', referenceIdToPartNumberMap)

rawDescription: How can I write to an existing csv file from a dictionary to a specific column?

description: Write to an existing csv file from a dictionary to a specific column


code:
for key in diff:
    if key in first:
        print 'First IP'
    elif key in second:
        print 'Second IP'

rawDescription: script to find which key belongs which IP address after finding all non matching elements in python?

description: Script to find which key belongs which IP address after finding all non matching elements in python


code:
import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.le = QLineEdit()
        self.le.setObjectName("host")
        self.le.setText("Host")

        self.pb = QPushButton()
        self.pb.setObjectName("connect")
        self.pb.setText("Connect")

        layout = QFormLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)

        self.setLayout(layout)
        self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
        self.setWindowTitle("Learning")

    def button_click(self):
        # shost is a QString object
        shost = self.le.text()
        print shost

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

rawDescription: How to get text in QlineEdit when QpushButton is pressed in a string?

description: Get text in QlineEdit when QpushButton is pressed in a string


code:
import re

details = '(2,5  cr / K / M)'
pattern = re.compile(r'^\(([0-9],[0-9]).*/\s([A-Z])\s/\s([A-Z])\)')
m = pattern.match(details)

credit = m.group(1)
state = m.group(2)
grade = m.group(3)

course = {'credit': credit, 'state': state, 'grade': grade}

print course

rawDescription: Python Regex - group match

description: Python Regex - group match


code:
answer=[]
head=[]
tails=[]
for x,y in enumerate(j1):
  if x%2==0:
    head.append(y)
  else:
    tails.append(y)

stillmore = True;
while stillmore == True:
  for i,z in enumerate(tails):
    temp = []
    if len(z)>0:
      temp.append(z.pop(0))
    if len(z)>0:
      temp.append(z.pop(0))
    if len(z)>0:
      temp.append(z.pop(0))
    if len(z)>0:
      temp.append(z.pop(0))
    if len(temp)>0:
      doub=[]
      doub.append(head[i])
      doub.append(temp)
      answer.append(doub)
  left=0
  for z in tails:
    left=left+len(z)
  if left == 0:
    stillmore = False
print answer

rawDescription: How to convert this list in python to the given form

description: Convert this list in python to the given form


code:
fh = file('/dev/input/mice')
while True:
    fh.read(3)
    print 'Mouse moved!'

rawDescription: How to detect mouse and keyboard inactivity in linux

description: Detect mouse and keyboard inactivity in linux


code:
import tweepy

consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

id_list = ['tweetidexample', 'tweetidexample',"....etc" ]

def get_retweet_count(tweet_id):
    tweet = api.get_status(tweet_id)
    return tweet.retweet_count

for id in id_list:
   print get_retweet_count(id), id

rawDescription: Tweet details from ID

description: Tweet details from ID


code:
import gtk

def callback(window, event):
    assert event.type == gtk.gdk.BUTTON_PRESS
    print 'Clicked at x={0}, y={0}'.format(event.x, event.y)

window = gtk.Window()
window.add_events(gtk.gdk.BUTTON_PRESS_MASK)
window.connect('button-press-event', callback)
window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()

gtk.main()

rawDescription: Click event on any part of the window with pygtk.Window


code:
import threading

statistics = 0

def load_from_file(some_arguments, filename):
    global statistics
    data_list = json.loads(open(filename).read())
    url = address + getUrl(filename, config)
    for data in data_list.get("results"):
        statistics += 1
        r = requests.post(url, data=json.dumps(data), headers=headers,
                        auth=HTTPBasicAuth(username, password))

def load_from_directory(some_arguments, directory):
    threads = []
    func = partial(load_from_file, some_arguments)

    file_list = [f for f in listdir(directory) if isfile(join(directory, f))]

    for f in file_list:
        t = threading.Thread(target=func, args=(join(directory, f)))
        t.start()
        threads.append(t)

    #Wait for threads to finish
    for thread in threads:
        thread.join()

    print "total post requests", statistics

rawDescription: Shared variable among various threads in Python


code:
found = False
foundPQRST = False
f = open('f1', 'r')
for line in f:
   if 'P QRST' in line:
      foundPQRST= True
      PQRSTline = line
   if 'abracadabra' in line:
      print 'Found'
      abraline = line
      found = True
   if found and foundPQRST:
      dosomething(PQRSTline, abraline)
      break
if not found:
   print "wasn't found"
f.close()

rawDescription: File handling in python for large amount of data


code:
obj = {'content' : 'something goes here'}
json_obj = json.dumps(obj)
json_size = len(json_obj)

serialized_size = len(serialized_object)

if json_size < serialized_size:
  print "I'd use the JSON with this..."

rawDescription: python. get size of an object

description: Python. get size of an object


code:
class Foo:
    def bar(hello=None):
         return hello
    def baz(world=None):
         return baz
import inspect
members = inspect.getmembers(Foo)
print members
inspect.getargspec(members[2][1])
inspect.getargspec(members[3][1])

rawDescription: How can I report the API of a class programmatically?

description: Report the API of a class programmatically


code:
import socket
import threading
import SocketServer

data = ""

class UDPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        global data
        data = self.request[0].strip()

class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
    pass

if __name__ == "__main__":

    ReceiverSocket = ThreadedUDPServer(("localhost",11111), UDPHandler)
    ServerThread = threading.Thread(target=ReceiverSocket.serve_forever)
    ServerThread.start()

    while 1:
        if data:
            print data
            data = ""

rawDescription: Python SocketServer passing data to the main thread via Thread local storage


code:
import networkx
import itertools

def complete_graph_from_list(L, create_using=None):
    G = networkx.empty_graph(len(L),create_using)
    if len(L)>1:
        if G.is_directed():
            edges = itertools.permutations(L,2)
        else:
            edges = itertools.combinations(L,2)
        G.add_edges_from(edges)
    return G

S = complete_graph_from_list(["a", "b", "c", "d"])
print S.edges()

rawDescription: How to generate a fully connected subgraph from node list using python's networkx module

description: Generate a fully connected subgraph from node list using python's networkx module


code:
import socket
import time
from Queue import Queue
from threading import Thread

HOST = "127.0.0.1"
PORT = 8008

def iterate_socket(sock):
    while True:
        data = sock.recv(1024)
        yield data
        if not data: # End of the stream
            return

def consume(q, s):
    for i in s:
        q.put(i)

def merge(xs, ys):
    q = Queue()
    iters = [xs, ys]
    for it in iters:
        t = Thread(target=consume, args=(q, it))
        t.start()

    done = 0
    while True:
        out = q.get()
        if out == '':  # End of the stream.
            done += 1
            if done == len(iters): # When all iters are done, break out.
                return
        else:
            yield out

sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock1.connect((HOST, PORT))
time.sleep(1)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.connect((HOST, PORT))

iter1 = iterate_socket(sock1)
iter2 = iterate_socket(sock2)

for msg in merge(iter1, iter2):
    print msg,

rawDescription: Merging multiple blocking generator functions in python


code:
def batch(iterable, n=1):
    l = len(iterable)
    for ndx in range(0, l, n):
        yield iterable[ndx:min(ndx + n, l)]

for x in batch(range(0, 10), 3):
    print x

rawDescription: how to split an iterable in constant-size chunks

description: Split an iterable in constant-size chunks


code:
from types import ClassType

import classes  # Your module containing the classes

out = []

# or you can also iterate over just vars(classes).values() as you only need the
# objects not their name.
for k, v in  vars(classes).items():
    if isinstance(v, ClassType):
        if getattr(v, 'addToList', None) or getattr(v(), 'addToList', None):
            out.append(v)
print out
#[<class classes.A at 0x0000000002B7CCA8>, <class classes.B at 0x0000000002B7CD68>]

rawDescription: Make a list of certain classes within a module


code:
def updateCount(x):
    count = 0
    for i in xrange(x):
        count = (count+1) % 10
        yield count

for i in updateCount(15):
    print i

rawDescription: How to handle variable scope elegantly in Python?

description: Handle variable scope elegantly in Python


code:
def post(self):
   #[...]
   if isvalid(username) == False:
       print "Invalid Username"
       self.redirect("/badusername")
       return

   print "User Is OK"

rawDescription: Stopping a request in Tornado

description: Stopping a request in Tornado


code:
def download(bucket, filename):
    key = Key(bucket, filename)
    headers = {}
    mode = 'wb'
    updating = False
    if os.path.isfile(filename):
        mode = 'r+b'
        updating = True
        print "File exists, adding If-Modified-Since header"
        modified_since = os.path.getmtime(filename)
        timestamp = datetime.datetime.utcfromtimestamp(modified_since)
        headers['If-Modified-Since'] = timestamp.strftime("%a, %d %b %Y %H:%M:%S GMT")
    try:
        with open(filename, mode) as f:
            key.get_contents_to_file(f, headers)
            f.truncate()
    except boto.exception.S3ResponseError as e:
        if not updating:
            # got an error and we are not updating an existing file
            # delete the file that was created due to mode = 'wb'
            os.remove(filename)
        return e.status
    return 200

rawDescription: How do I download a file from S3 using boto only if the remote file is newer than a local copy?

description: Download a file from S3 using boto only if the remote file is newer than a local copy


code:
import sys
import select

if select.select([sys.stdin,],[],[],0.0)[0]:
    print "Have data!"
else:
    print "No data"

rawDescription: How do I check if stdin has some data?

description: Check if stdin has some data


code:
while s:
    print s[:117]
    s = s[117:]

rawDescription: Python, splitting strings

description: Python, splitting strings


code: print eval('self.post.id') # Prints the value of self.post.id

rawDescription: Convert a string to preexisting variable names


code:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

country = "albanie"
zooverWeb = "http://www.zoover.nl/"

url = zooverWeb + country

driver = webdriver.Firefox()
driver.get(url)
driver.implicitly_wait(10)

links = []
for page in xrange(1, 4):
    # tricky part - waiting for the page number on the top to appear
    if page > 1:
        WebDriverWait(driver, 60).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'div.entityPagingTop strong'), str(page)))
    else:
        WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CLASS_NAME, 'next')))

    divList = driver.find_elements_by_class_name('blue2')
    for div in divList:
        hrefTag = div.find_element_by_css_selector('a').get_attribute('href')
        newLink = zooverWeb + hrefTag
        links.append(newLink)

    driver.find_element_by_class_name("next").click()

print links

rawDescription: iterate over result pages using selenium and python: StaleElementReferenceException

description: Iterate over result pages using selenium and python: StaleElementReferenceException


code:
from multiprocessing import Pool

class subcl:
    def __init__(self):
        self.counter=1
        return
    def increment(self):
        self.counter+=1
        return

def wrapper(targ):
    targ.increment()
    return targ                                        # <<<<< change #1

class sim:
    def __init__(self):
        self.world=[subcl(),subcl(),subcl(),subcl()]
    def run(self):
        if __name__=='__main__':
            p=Pool()
            self.world = p.map(wrapper,self.world)     # <<<<< change #2

a=sim()
a.run()
print a.world[1].counter # now prints 2

rawDescription: How can I parallelize method calls on an array of objects?

description: Parallelize method calls on an array of objects


code:
a = open("file1.txt").readlines()
b = open("file2.txt").readlines()
count = 0
pos = 0

while 1:
    count += 1
    try:
        al = a.pop(0)
        bl = b.pop(0)
        if al != bl:
            print "files differ on line %d, byte %d" % (count,pos)
        pos += len(al)
    except IndexError:
        break

rawDescription: Determine where documents differ with Python


code:
import sys
import string
import nltk
from nltk.corpus import gutenberg

def longest_element(p):
    """return the first element of p which has the greatest len()"""
    max_len = 0
    elem = None
    for e in p:
        if len(e) > max_len:
            elem = e
            max_len = len(e)
    return elem

def downcase(p):
    """returns a list of words in p shifted to lower case"""
    return map(string.lower, p)

def unique_words():
    """it turns out unique_words was never referenced so this is here
       for pedagogy"""
    # there are 2.6 million words in the gutenburg corpus but only ~42k unique
    # ignoring case, let's pare that down a bit
    for word in gutenberg.words():
        words.add(word.lower())
    print 'gutenberg.words() has', len(words), 'unique caseless words'
    return words

print 'loading gutenburg corpus...'
sentences = []
for sentence in gutenberg.sents():
    sentences.append(downcase(sentence))

trigger = sys.argv[1:]
target = longest_element(trigger).lower()
last_target = None

while target != last_target:
    matched_sentences = []
    for sentence in sentences:
        if target in sentence:
            matched_sentences.append(sentence)

    print '===', target, 'matched', len(matched_sentences), 'sentences'
    longestSentence = longest_element(matched_sentences)
    print ' '.join(longestSentence)

    trigger = longestSentence
    last_target = target
    target = longest_element(trigger).lower()

rawDescription: Python code flow does not work as expected?


code:
from Tkinter import *
from PIL import Image, ImageTk
import numpy as np

class Test():
    def __init__(self, master):
        self.parent = master
        self.frame = Frame(self.parent)
        self.frame.pack(fill=BOTH, expand=1)
        self.mainPanel = Label(self.frame)
        self.mainPanel.pack(fill=BOTH, expand=1)
        self.parent.wm_protocol("WM_DELETE_WINDOW", self.on_closing)
        self.close = 0

    def on_closing(self):
        print "Over"
        self.close = 1

    def task(self):
        print "ok"
        im = Image.fromarray(np.zeros((500, 500, 3)), 'RGB')
        self.tkim = ImageTk.PhotoImage(im)
        self.mainPanel['image'] = self.tkim

root = Tk()
tool = Test(root)

while(tool.close != 1):
    tool.task()
    root.update()
root.destroy()

rawDescription: Tkinter exit command with infinite loop

description: Tkinter exit command with infinite loop


code:
for i in range(len(year_list)):
    datai=set(pd.read_csv('filename_'+year_list[i]+'.csv')['word'])
    tocompare=[]
    for j in range(i+1,len(year_list)):
        dataj=set(pd.read_csv('filename_'+year_list[j]+'.csv')['word'])
        print "Two files:",i,j
        print datai.intersection(dataj)
        tocompare.append(dataj)
    print "All compare:"
    print datai.intersection(*tocompare)
    break

rawDescription: extract common elements in several lists

description: Extract common elements in several lists


code:
link_dst1 = "E:\Music\#02.Genre_Electronic_Bass Music\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00"
link_dst2 = r"E:\Music\#02.Genre_Electronic_Bass Music\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00"
link_dst1 == link_dst2
print link_dst1

rawDescription: How to create symlinks in windows using Python?

description: Create symlinks in windows using Python


code:
init_serial()
while True:
    ser.close()
    ser.open()
    time.sleep(2)  #at least wait for 2s
    cmd = raw_input('Type what you want to send:\r\n')
    ser.write(cmd.encode('ascii')+'\r\n')
    if cmd =='q':
        ser.close()
        print ser.portstr +' is closed'
        exit()
    else:
        bytes = ser.readline()
        print ('Return: ' + bytes)

rawDescription: pySerial: port only work the first command

description: PySerial: port only work the first command


code:
def g1():
        print 'g1'

def g2():
        print 'g2'

def subfunc(fn):
        fn()

def caller(a):
        f = g1 if a else g2
        for i in range(2):
                subfunc(f)

caller(True)
caller(False)

rawDescription: Optimizing redundant logical tests on constants in Python


code:
from collections import defaultdict
items = defaultdict(list)
with open("myfile.txt") as infile:
    for line in file:
        id, text = line.rstrip().split("\t")
        items[id].append(text)
for id in items:
    print id + "\t" + ", ".join(items[id])

rawDescription: python: combine values in one column on the basis of ids in another column

description: Python: combine values in one column on the basis of ids in another column


code:
for col in collection.find({"_id.uid": 702993}):
    print col

rawDescription: PyMongo - Match by one nested field

description: PyMongo - Match by one nested field


code:
import csv
import sys

from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment, tostring

from xml.dom import minidom

def prettify(doctype, elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = doctype + ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ", encoding = 'utf-8')

doctype = '<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">'

video_data = ((256, 336000),
              (512, 592000),
              (768, 848000),
              (1128, 1208000))

with open(sys.argv[1], 'rU') as f:
    reader = csv.DictReader(f)
    for row in reader:
        root = Element('smil')
        root.set('xmlns', 'http://www.w3.org/2001/SMIL20/Language')
        head = SubElement(root, 'head')
        meta = SubElement(head, 'meta base="rtmp://cp23636.edgefcs.net/ondemand"')
        body = SubElement(root, 'body')

        switch_tag = ElementTree.SubElement(body, 'switch')

        for suffix, bitrate in video_data:
            attrs = {'src': ("mp4:soundcheck/{year}/{id}/{file_root_name}_{suffix}.mp4"
                             .format(suffix=str(suffix), **row)),
                     'system-bitrate': str(bitrate),
                     }
            ElementTree.SubElement(switch_tag, 'video', attrs)

        print prettify(doctype, root)

rawDescription: Split up SubElements in `for` loop using Python and XML


code:
import urllib2

url = 'http://api.justin.tv/api/stream/list.json?channel=FollowGrubby'
contents = urllib2.urlopen(url)

print contents.read()

rawDescription: Is There Any Way To Check if a Twitch Stream Is Live Using Python?


code:
patterns = ['abc','xyz','gfk']
with open('filer.csv') as fh:
    reader = csv.reader(fh, skipinitialspace=True)
    for line in reader:
        print '---'
        content = dict(zip(line[::2], line[1::2]))
        for pattern in patterns:
            print "{}: {}".format(pattern, content.get(pattern, 'no match'))

rawDescription: Python Script for printing contents on adjacent cell from a csv file based on matching criteria


code:
import xml.etree.ElementTree as ET
from urllib2 import urlopen

url = 'http://nexus:8080/nexus/service/local/lucene/search?g=com.xxx.yyy&a=zzz'
tree = ET.parse(urlopen(url))
print tree.findtext('.//latestRelease')

rawDescription: Pythonic way to strip a string

description: Pythonic way to strip a string


code:
from collections import Counter
s = 'fdhdhjduvduvfbvhufbvufvhifbusdbjhkbueigvuerafvguavgugvg'
for item in Counter(s[i:i+3] for i in range(len(s))).most_common():
    print item

rawDescription: How can I split and rank text/character?

description: Split and rank text/character


code:
import re

#In python functions/methods usually are lowercase
#and words are seperated by _ while classes use CamelCasing
def eval_step_by_step(expression):
    """Evaluates math expression. Doesn't do any error checking.
        expression (string) - math expression"""

    print expression
    #For pretty formating.
    expr_len = len(expression)
    #While there's parentheses in the expression.
    while True:
        #re.match checks for a match only at the beginning of the string,
        #while re.search checks for a match anywhere in the string.

        #Matches all numbers, +, -, *, / and whitespace within parentheses
        #lazily (innermost first).
        contents = re.search("\(([0-9|\*|/|\+|\-|\s]*?)\)", expression)
        #If we didn't find anything, print result and break out of loop.
        if not contents:
            #string.format() is the Python 3 way of formating strings
            #(Also works in Python 2.6).

            #Print eval(expression) aligned right in a "field" with width
            #of expr_len characters.
            print "{0:{1}}".format(eval(expression), expr_len)
            break

        #group(0) [match] is everything matching our search,
        #group(1) [parentheses_text] is just epression withing parentheses.
        match, parentheses_text = contents.group(0), contents.group(1)
        expression = expression.replace(match, str(eval(parentheses_text)))
        #Aligns text to the right. Have to use ">" here
        #because expression is not a number.
        print "{0:>{1}}".format(expression, expr_len)

#For example try: (4+3+(32-1)*3)*3
problem = raw_input("Input math problem: ")

eval_step_by_step(problem)

rawDescription: How Can I Make This Python Code More Usable And Readable?


code:
import gevent
import gevent.monkey
gevent.monkey.patch_all()

import requests

from gevent.pywsgi import WSGIServer

# app = YourBottleApp

def alarm():
    '''
    Run this service every X duration
    '''
    ALARM = 21
    while 1:
        #checking time and doing something. Then finding INTERVAL
        gevent.sleep(INTERVAL)

if __name__ == '__main__':
    http_server = WSGIServer(('', 8080), app)
    srv_greenlet = gevent.spawn(http_server.serve_forever)
    alarm_greenlet = gevent.spawn(alarm)

    try:
        gevent.joinall([srv_greenlet, alarm_greenlet])
    except KeyboardInterrupt:
        http_server.stop()
        print 'Quitting'

rawDescription: Run gevent processes and server concurrently


rawDescription: get_threshold() use other than grabage collection

description: Get_threshold() use other than grabage collection


code:
# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]

def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

def getProteinSequence(residue):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    #handle = open(filepath, "w")
    #handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
    pass

print 'Sequence Count : '
print ranSeqCount

records = []
for i in range(0,ranSeqCount,1):
    records.append(randomProteinSeqRecord(i+1))

if(filepathProvided):
    SeqIO.write(records, filepath, "fasta")
    print 'File created at : ' + filepath

else:
    print 'Writing to console is actually not supported!  :/'

print
raw_input('Press any key to exit ...')
print

rawDescription: ImportError: No module named writers.SeqRecord.fasta


code:
import json

#jstr = "... that thing above ..."
# This line only decodes the JSON into a structure in memory:
obj = json.loads(jstr)
# obj, in this case, is a dictionary, a built-in Python type.

# These lines just iterate over that structure.
for ka, va in obj.iteritems():
    print ka
    for kb, vb in va.iteritems():
        print '  ' + kb
        for key, string in vb.iteritems():
            print '    ' + repr((key, string))

rawDescription: Python decodes JSON

description: Python decodes JSON


code:
import xlrd

fname = "sample.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
    sh = bk.sheet_by_name("Sheet1")
except:
    print "no sheet in %s named Sheet1" % fname
    return None
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)

cell_value = sh.cell_value(1,1)
print cell_value

row_list = []
for i in range(1,nrows):
    row_data = sh.row_values(i)
    row_list.append(row_data)

rawDescription: How to copy a row of Excel sheet to another sheet using Python

description: Copy a row of Excel sheet to another sheet using Python


code:
import csv, sys
filename = 'some.csv'
with open(filename, 'rb') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            print row
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

rawDescription: parse comma separated csv file with quotes in python

description: Parse comma separated csv file with quotes in python


code:
import re
x="u'reviews': [{u'content':"
print re.sub(r"u'([^']*)'",r"\1",x)

rawDescription: Filtering a string based on some group of characters


code:
import time
from threading import Thread

def foo():
    while True:
        print "Hello Matt"
        time.sleep(5)

def bar():
    while True:
        print "Hello world"
        time.sleep(1)

a = Thread(target=foo)
b = Thread(target=bar)
a.start()
b.start()

rawDescription: Multiple loops for predefined amounts in Python


code:
import csv
f = csv.reader(open('filename1.csv', 'rb'))
g = csv.reader(open('filename2.csv', 'rb'))
output = csv.writer(open('ouputfile.csv', 'wb'))
for row_f in f:
    row_g = g.next()
    row_output = list()
    for argi, item in enumerate(row_f):
        try:
            row_output.append(int(item) + int(row_g[argi]))
        except ValueError, e:
            pass
    output.writerow(row_output)

rawDescription: Adding(mathwise) rows of several csv files in python


code:
import datetime, dateutil.parser

d = dateutil.parser.parse('2008-09-26T01:51:42.000Z')
print d.strftime('%m/%d/%Y') #==> '09/26/2008'

rawDescription: How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?

description: Convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python


code:
import numpy as np

x = np.array([[1,2],[3,4],[5,6]])

np.save("test", x)

y = np.load("test.npy")

print y

rawDescription: Jupyter Notebook - How to use a data file for code


code:
import json

v = 2.030000002
result = []
for part in json.JSONEncoder().iterencode({'x': v}):
    try:
        tmp = round(float(part), 3)
    except ValueError:
        pass
    else:
        part = '{:.3f}'.format(tmp)
    result.append(part)
result = ''.join(result)

print result  # -> {"x": 2.030}

rawDescription: Python: forcing precision on a floating point number in json?


code:
def B_producer(new_A,new_B,synch,start):
    synch.acquire()
    while True:
        with new_A:
            synch.release()
            with start:
                start.notify()
            new_A.wait()

            with new_B:

                #Do something

                synch.acquire()
                new_B.notify()

if __name__=='__main__':
    new_A = Condition()
    new_B = Condition()
    start = Condition()
    synch = Lock()

    B_producer = Process(target=B_producer, args=(new_A,new_B,synch,start))
    with start:
        B_producer.start()
        start.wait()

    count = 0

    synch.acquire()

    while True:
        new_A.acquire()
        synch.release()

        #Do something

        with new_B:
            new_A.notify()
            new_A.release()

            new_B.wait()
            synch.acquire()

        count += 1
        if count%100000 == 0:
            break

    B_producer.terminate()
    print 'end'

rawDescription: Synchronizing 2 threads in python

description: Synchronizing 2 threads in python


code:
new_keys = [row[2] + '-' + row[5] + '-' + row[6] + '-' + row[7] for row in data]
new_values = [row[3] for row in data]

aggregate_values = {} # An empty dictionary
# Iterate across the paired lists together
for key, value in zip(new_keys, new_values):
    if key not in aggregate_values:
        aggregate_values[key] = [value]
    else:
        aggregate_values[key].append(value)

# printed output
for key in aggregate_values:
    print key + " " + " ".join(aggregate_values[key])

rawDescription: Iterate and discard sequential duplicates


code:
import csv, datetime

addresses = [
    {'address': {'value': '123 road', 'count': 1}, 'delete': False},
    {'address': {'value': '456 road', 'count': 1}, 'delete': False},
    {'address': {'value': '789 road', 'count': 1}, 'delete': False},

    {'address': {'value': '1 new road', 'count': 1}, 'delete': False},
]

now = datetime.datetime.now()

### create the csv
##with open('address.csv', 'wb') as csv_file:
##    writer = csv.writer(csv_file)
##    for row in addresses:
##        writer.writerow([ row['address']['value'], row['address']['count'], now.strftime('%Y-%m-%d %H:%M:%S') ])

# make lookup keys for the dict
address_lookup = {}
for i in range(len(addresses)):
    address_row = addresses[i]
    address_lookup[address_row['address']['value']] = i

# read csv once
with open('address.csv', 'rb') as csv_file:
    reader = csv.reader(csv_file)
    for row in reader:
        print row
        # if address is found in the dict, set delete flag to true
        if row[0] in address_lookup:
            print 'flagging address as old: %s' % row[0]
            addresses[ address_lookup[row[0]] ]['delete'] = True

with open('address.csv', 'ab') as csv_file:
    # go back through addresses and add any that shouldnt be deleted to the csv
    writer = csv.writer(csv_file)
    for address_row in addresses:
        if address_row['delete'] is False:
            print 'adding row: '
            print address_row
            #writer.writerow([  row['address']['value'], row['address']['count'], now.strftime('%Y-%m-%d %H:%M:%S') ])

rawDescription: Python Dictionaries & CSV Values | Check CSV


code:
class MicroMock(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
def print_foo(x):
    print x.foo
print_foo(MicroMock(foo=3))

rawDescription: Is it possible to create anonymous objects in Python?


rawDescription: Change the color of text in python shell?


code:
import urllib2
import json
wfd = urllib2.urlopen('http://rasinsrv07.cstcis.cti.depaul.edu/CSC455/assignment5.txt')
data = wfd.read()
for line in data.split('EndOfTweet'):
    # From here, line will contain a single tweet. It appears this line is a JSON parsable structure.
    decoded_line = json.loads(line)
    # Now, lets print out the text of the tweet to show we can
    print decoded_line.get(u'text')

rawDescription: Extract indivudual tweets from a textfile with no line breaks using Python


code:
import threading
import time

def timed_output(name, delay, run_event):
    while run_event.is_set():
        time.sleep(delay)
        print name,": New Message!"

if __name__ == "__main__":
    run_event = threading.Event()
    run_event.set()
    d1 = 1
    t1 = threading.Thread(target = timed_output, args = ("bob",d1,run_event))

    d2 = 2
    t2 = threading.Thread(target = timed_output, args = ("paul",d2,run_event))

    t1.start()
    time.sleep(.5)
    t2.start()

    try:
        while 1:
            time.sleep(.1)
    except KeyboardInterrupt:
        print "attempting to close threads. Max wait =",max(d1,d2)
        run_event.clear()
        t1.join()
        t2.join()
        print "threads successfully closed"

rawDescription: Closing all threads with a keyboard interrupt


code:
print "Content-Type: text/plain"
print "Content-Disposition: attachment; filename=some-file-name.txt"
print

rawDescription: Prompt user to save my desired text file name


code:
from django.contrib.sessions.models import Session
from django.contrib.auth.models import User

session_key = '8cae76c505f15432b48c8292a7dd0e54'

session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
user = User.objects.get(pk=uid)

print user.username, user.get_full_name(), user.email

rawDescription: how to find user id from session_data from django_session table?

description: Find user id from session_data from django_session table


code:
#!/usr/bin/env python

import os
import select
import sys
import getpass
import paramiko
import socket
import logging
import Xlib.support.connect as xlib_connect
LOGGER = logging.getLogger(__name__)

# connection settings
host = '192.168.122.55'
user = 'user'
password = getpass.getpass()

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, username=user, password=password)
del password

# maintain map
# { fd: (channel, remote channel), ... }
channels = {}

poller = select.poll()
def x11_handler(channel, (src_addr, src_port)):
    '''handler for incoming x11 connections
    for each x11 incoming connection,
    - get a connection to the local display
    - maintain bidirectional map of remote x11 channel to local x11 channel
    - add the descriptors to the poller
    - queue the channel (use transport.accept())'''
    x11_chanfd = channel.fileno()
    local_x11_socket = xlib_connect.get_socket(*local_x11_display[:3])
    local_x11_socket_fileno = local_x11_socket.fileno()
    channels[x11_chanfd] = channel, local_x11_socket
    channels[local_x11_socket_fileno] = local_x11_socket, channel
    poller.register(x11_chanfd, select.POLLIN)
    poller.register(local_x11_socket, select.POLLIN)
    LOGGER.debug('x11 channel on: %s %s', src_addr, src_port)
    transport._queue_incoming_channel(channel)

def flush_out(session):
    while session.recv_ready():
        sys.stdout.write(session.recv(4096))
    while session.recv_stderr_ready():
        sys.stderr.write(session.recv_stderr(4096))

# get local disply
local_x11_display = xlib_connect.get_display(os.environ['DISPLAY'])
# start x11 session
transport = ssh_client.get_transport()
session = transport.open_session()
session.request_x11(handler=x11_handler)
session.exec_command('xterm')
session_fileno = session.fileno()
poller.register(session_fileno, select.POLLIN)
# accept first remote x11 connection
transport.accept()

# event loop
while not session.exit_status_ready():
    poll = poller.poll()
    # accept subsequent x11 connections if any
    if len(transport.server_accepts) > 0:
        transport.accept()
    if not poll: # this should not happen, as we don't have a timeout.
        break
    for fd, event in poll:
        if fd == session_fileno:
            flush_out(session)
        # data either on local/remote x11 socket
        if fd in channels.keys():
            channel, counterpart = channels[fd]
            try:
                # forward data between local/remote x11 socket.
                data = channel.recv(4096)
                counterpart.sendall(data)
            except socket.error:
                channel.close()
                counterpart.close()
                del channels[fd]

print 'Exit status:', session.recv_exit_status()
flush_out(session)
session.close()

rawDescription: x11 forwarding with paramiko

description: X11 forwarding with paramiko


code:
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class Model(QtCore.QAbstractListModel):
    def __init__(self, *args):
        QtCore.QAbstractListModel.__init__(self, *args)
        self.items=[]
        self.modelDict = None

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.ItemDataRole:
                return self.data(index)
            elif role==QtCore.Qt.DisplayRole:
                return QtCore.QVariant(self.items[index.row()])

    def getData(self, data):
        return self.modelDict[str(data)]

    def addItems(self, instDict):
        for key in instDict:
            inst=instDict.get(key)
            index=QtCore.QModelIndex()
            self.beginInsertRows(index, 0, 0)
            self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)
        self.items.extend(instDict)
        self.endInsertRows()
        self.modelDict = instDict

class ListView(QtGui.QListView):
    def __init__(self):
        super(ListView, self).__init__()
        self.model= Model()
        self.model.addItems({'Animals':['Bison','Panther','Elephant'],'Birds':['Duck','Hawk','Pigeon'],'Fish':['Shark','Salmon','Piranha']})
        self.setModel(self.model)
        self.clicked.connect(self.itemClicked)
        self.show()

    def itemClicked(self, index):
        print 'self.data(index).toString(): %s'%index.data().toString()
        print 'self.model.data(index).toString(): %s'%self.model.getData(index.data().toString())

window=ListView()
sys.exit(app.exec_())

rawDescription: How to store and get data from .model()

description: Store and get data from .model()


code:
movdir = r"C:\Scans"
basedir = r"C:\Links"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
    for filename in files:
        # I use absolute path, case you want to move several dirs.
        old_name = os.path.join( os.path.abspath(root), filename )

        # Separate base from extension
        base, extension = os.path.splitext(filename)

        # Initial new name
        new_name = os.path.join(basedir, base, filename)

        # If folder basedir/base does not exist... You don't want to create it?
        if not os.path.exists(os.path.join(basedir, base)):
            print os.path.join(basedir,base), "not found"
            continue    # Next filename
        elif not os.path.exists(new_name):  # folder exists, file does not
            shutil.copy(old_name, new_name)
        else:  # folder exists, file exists as well
            ii = 1
            while True:
                new_name = os.path.join(basedir,base, base + "_" + str(ii) + extension)
                if os.path.exists(newname):
                   shutil.copy(old_name, new_name)
                   print "Copied", old_name, "as", new_name
                   break
                ii += 1

rawDescription: Python copy files to a new directory and rename if file name already exists


code:
data = """CA, The title
CA, the title
data"""

import re
print re.split(r'(?<!\s)\n', data)
# ['CA, The title', 'CA, the title \ndata']

rawDescription: Split on newline, but not space-newline

description: Split on newline, but not space-newline


code:
class UploadPincode(models.Model):
    added_on = models.DateField(auto_now_add=True)
    pincode_file = models.FileField(
        upload_to="Pincodes/", verbose_name="Pincode Excel file (.xls,xlsx)", blank=False, null=False)

    def save(self):
        print "Before Save #######"

        super(UploadPincode, self).save()
        print " Save #######"

        pincode_process(self.pincode_file.url)

    def __unicode__(self):
        return str(self.added_on)

def pincode_process(pincode_file=None):

    print "Pincode Adding ##########################",pincode_file,pincode_file[1:]

    book = open_workbook(pincode_file[1:])
    for j in range(0, book.nsheets - 1):
        sheet = book.sheet_by_index(j)
        for i in range(sheet.nrows):
            if i != 0:
                a = sheet.row_values(i)
                int_pin = int(a[0])
                Pincode(pincode=str(int_pin)).save()

    return True

rawDescription: How to Process Uploaded file in Django Model save method


code:
from lxml import html
page = html.fromstring(open("um.htm", "r").read())
matches = page.xpath("//a[contains(@title, 'pm.') or contains(@title, 'ol')]/span")
print [elem.text for elem in matches]

rawDescription: Python pattern matching

description: Python pattern matching


rawDescription: about python list step issue when the element is long type

description: About python list step issue when the element is long type


code:
ddl = 'org_id bigint,merc_name varchar(50),deposit_day date null,amount decimal(18,3),bank_name varchar(128)'
print re.findall('(?:[^,(]|\(.*?\))+', ddl)

rawDescription: Split comma separater string while escaping the ones in parentheses


code:
from collections import defaultdict
from datetime import datetime
from functools import wraps

def store_result(method):
    @wraps(method)
    def wrapped(self, *args, **kwargs):
        result = method(self, *args, **kwargs)
        self.storeResult(method.__name__, result, str(datetime.now()))
        return result

    return wrapped

class Base(object):
    def __init__(self):
        self.results = defaultdict(dict)

    def storeResult(self, function_name, result, executed_time= None):
        self.results[function_name][result] = executed_time

class Derived(Base):
    @store_result
    def sum(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a + b

    @store_result
    def diff(self, a, b):
        print 'In derived!'
        return a - b

    @store_result
    def multiply(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a * b

    @store_result
    def divide(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a / b

rawDescription: How to pass function name of child class as parameter to parent class method in python?

description: Pass function name of child class as parameter to parent class method in python


code:
top_path = '/A/B'
for p in d.iterkeys():
    if p.startswith(top_path):
        print d[p]

rawDescription: Printing a particular subset of keys in a dictionary


code:
names = ["Rob","Mark","Steve"]
lists = {name: [] for name in names}
print lists

rawDescription: Create new lists from elements of a list


code:
temp_list = []
prev_value = original_list[0][1]

for key, value in original_list:
    if value == prev_value:
        temp_list.append(key)
    else:
        do_something(temp_list)
        print temp_list
        temp_list = [key]
    prev_value = value

do_something(temp_list)
print temp_list

# prints ['4', '3', '2']
# prints ['1', '6']

rawDescription: Iterating over a Python 2D list to find the value


code:
results = [".".join(map(str,r)) for r in result]
print ".".join(results)

rawDescription: Python - convert Tuple into String

description: Python - convert Tuple into String


code:
tags = ['alternative', 'indie', 'jam rock', 'indie', 'alternative', 'punk']
weights = [100, 20, 45, 50, 75, 50]
d = {}
for tag, weight in zip(tags, weights):
    if tag in d:
        d[tag] += weight
    else:
        d[tag] = weight

new_tags = [tag for tag in sorted(d)] #if you want it in alphabetical order
new_weights = [d[tag] for tag in new_tags]
print new_tags
print new_weights

rawDescription: Adding together the contents of a second list whenever an item appears in a first list


code:
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.patches import Rectangle, PathPatch

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

xlo = X.min()
xhi = X.max()
ylo = Y.min()
yhi = Y.max()
zlo = -2
zhi = 2

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=1, zorder=100)
cset = ax.contour(X, Y, Z, zdir='z', offset=zlo, alpha=0.0)

def highlight((xmin, xmax),(ymin, ymax)):
    # draw highlight on xz plane
    p1 = Rectangle((ymin,zlo),(ymax-ymin),(zhi-zlo), color='y', alpha=0.5, zorder=0)
    ax.add_patch(p1)
    art3d.pathpatch_2d_to_3d(p1, z=xlo, zdir='x')

    # draw highlight on yz plane
    p2 = Rectangle((xmin,zlo),(xmax-xmin),(zhi-zlo), color='y', alpha=0.5, zorder=0)
    ax.add_patch(p2)
    art3d.pathpatch_2d_to_3d(p2, z=yhi, zdir='y')

    # define a region to highlight
    highlight = (X>xmin)&(X<xmax)&(Y>ymin)&(Y<ymax)
    coll = ax.collections[0]
    # get the original color shading (if you want to keep that effect)
    colors = coll._facecolors_original
    #colors = coll.get_facecolors()
    # they are stored as a list for some reason so get the flat indicies
    for idx in np.where(highlight[:-1,:-1].flat)[0]:
        # and modifly one-by-one
        color = colors[idx][:]
        colors[idx][0] = color[2]  # swap red with blue
        colors[idx][3] = color[0]
        colors[idx][4] = .2  #change alpha
    # re-set the face colors
    coll.set_facecolors(colors)

highlight((-3,0),(-3,1))

ax.set_xlim3d(xlo, xhi)
ax.set_ylim3d(ylo, yhi)
ax.set_zlim3d(zlo, zhi)

plt.show()

rawDescription: How to annotate/highlight a 3d plot in MatPlotLib

description: Annotate/highlight a 3d plot in MatPlotLib


code:
import time

last={}

def diff(col): return counters[col] - last[iface][col]

while True:
  print "\n%10s: %10s %10s %10s %10s"%("interface","bytes recv","bytes sent", "pkts recv", "pkts sent")
  for line in open('/proc/net/dev').readlines()[2:]:
    iface, counters = line.split(':')
    counters = map(int,counters.split())
    if iface in last:
      print "%10s: %10d %10d %10d %10d"%(iface,diff(0), diff(8), diff(1), diff(9))

    last[iface] = counters

  time.sleep(1)

rawDescription: Live RX and TX rates in linux

description: Live RX and TX rates in linux


code:
import ctypes
import os.path

# duplicate the UNICODE_STRING structure from the windows API

class UNICODE_STRING(ctypes.Structure):
    _fields_ = [
      ('Length', ctypes.c_short),
      ('MaximumLength', ctypes.c_short),
      ('Buffer', ctypes.c_wchar_p)
    ]

# args

pid = 8000 # put your pid here

# define some constants; from windows API reference

MAX_TOTAL_PATH_CHARS = 32767
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_IMAGE_FILE_NAME = 27

# open handles

ntdll = ctypes.windll.LoadLibrary('ntdll.dll')
process = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION,
  False, pid)

# allocate memory

buflen = (((MAX_TOTAL_PATH_CHARS + 1) * ctypes.sizeof(ctypes.c_wchar)) +
  ctypes.sizeof(UNICODE_STRING))
buffer = ctypes.c_char_p(' ' * buflen)

# query process image filename and parse for process "name"

ntdll.NtQueryInformationProcess(process, PROCESS_IMAGE_FILE_NAME, buffer,
  buflen, None)
pustr = ctypes.cast(buffer, ctypes.POINTER(UNICODE_STRING))
print os.path.split(pustr.contents.Buffer)[-1]

# cleanup

ctypes.windll.kernel32.CloseHandle(process)
ctypes.windll.kernel32.FreeLibrary(ntdll._handle)

rawDescription: Alternative to psutil.Process(pid).name

description: Alternative to psutil.Process(pid).name


code:
import numpy as np
import cv2

img = cv2.imread('lines.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=img.shape[1]-300
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)

a,b,c = lines.shape
for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)

cv2.imshow('edges', edges)
cv2.imshow('result', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

rawDescription: How to detect lines accurately using HoughLines transform in openCV python?

description: Detect lines accurately using HoughLines transform in openCV python


code:
def foo() : print( foo.bar )
exec "foo.bar = bar\n" + "foo()" in dict( globals(), bar = 1 )

rawDescription: Python: how to use 'exec' with custom scope?


code:
import serial
com = serial.Serial('/dev/ttyUSB0',baudrate=115200)

print "Waiting for message"

char = com.read(1)
print char

com.close()

rawDescription: Serial communication between two Linux running laptops using Python


rawDescription: removing unwanted characters after image extension in a string

description: Removing unwanted characters after image extension in a string


code:
self.lineEdit1 = QtGui.QLineEdit(self)    #URL
self.lineEdit2 = QtGui.QLineEdit(self)    # API KEY
self.pushButton = QtGui.QPushButton(self)   # SEND BUTTON
QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.doIt)

def doIt(self):
    parameters = {"url": str(self.lineEdit1.text()),"apikey": str(self.lineEdit2.text())}
    data = urllib.urlencode(parameters)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    json = response.read()
    print json

rawDescription: HTTP POST request from a PyQt-GUI?

description: HTTP POST request from a PyQt-GUI


rawDescription: Right split a string into groups of 3

description: Right split a string into groups of 3


code:
result = pool.apply_async(time.sleep, (10,))
print result.get(timeout=1)           # raises TimeoutError

rawDescription: python 2.6.7 multiprocessing pool fails to shut down

description: Python 2.6.7 multiprocessing pool fails to shut down