code:
import itertools

num = [1, 2, 3, 4, 5]
combinations = []
for combination in itertools.combinations(num, 3):
    combinations.append(int("".join(str(i) for i in combination)))
# => [123, 124, 125, 134, 135, 145, 234, 235, 245, 345]
print len(combinations)
# => 10

rawDescription: How to count possibilities in python lists

description: Count possibilities in python lists


code:
import threading
import Queue
import subprocess

lock = threading.Lock()

def go(alist):
  while queueCommand.qsize() > 0:
      command = queueCommand.get(False)
      res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
      output = res.communicate()
      output = str(output)
      star = output.find("address") + 8
      en = output.find(",")
      ip = output[star:en-3]
      lock.acquire()
      alist.append(ip)  #<================== her i use the liste
      lock.release()

def foo(alist):
    alist.append("bar")

if __name__ == '__main__':
  with open ("icq.txt","r") as myfile:
    text = myfile.read()
  end = 0
  alist = []
  queueCommand = Queue.Queue()
  while True:
    start = text.find("href=") + 13
    if start == 12:
      break
    end = text.find("/",start)
    if text[start:end].find("icq.com") != -1:
      hostname="host "+ text[start:end]
      queueCommand.put(hostname)
    text = text[end:len(text)]

  threads = []
  for i in range(10):
    thread = threading.Thread(target=go,args=(alist,)) #<====== i give the list as argument)
    thread.start()
    threads.append(thread)
  for thread in threads:
    thread.join()

  print alist

rawDescription: How do I modify a list inside a thread?

description: Modify a list inside a thread


code:
import time

# Change data.txt to the name of your file
data_file = open("data.txt", "r")

# Get rid of blank lines (often the last line of the file)
vals = [i for i in data_file.read().split('\n') if i]

data_file.close()

for i in vals:
    i = i.split(',')
    i[1] = i[1][1:]
    time.sleep(float(i[0]))

    print i[1]

rawDescription: How to replay a list of event consistently

description: Replay a list of event consistently


code:
import wx

import matplotlib
matplotlib.interactive(True)
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import numpy as np

class DraggableRectangle:
    def __init__(self, rect):
        self.rect = rect
        self.press = None

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

    def on_press(self, event):
        print "on_press"
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return

        contains, attrd = self.rect.contains(event)
        if not contains: return
        print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata

    def on_motion(self, event):
        print "on_motion"
        'on motion we will move the rect if the mouse is over us'
        if self.press is None: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        #print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)

        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def on_release(self, event):
        print "on_release"
        'on release we reset the press data'
        self.press = None
        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(800,600))

        self.panel = wx.Panel(self)

        self.figure = Figure(figsize=(6, 4), dpi=100)
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel, wx.ID_ANY, self.figure)

        rects = self.axes.bar(range(10), 20*np.random.rand(10))
        self.drs = []
        for rect in rects:
            dr = DraggableRectangle(rect)
            dr.connect()
            self.drs.append(dr)

print "test"
app = wx.PySimpleApp()#redirect=False)
top = Frame("test")
top.Show()
app.MainLoop()

rawDescription: How do I use matplotlib's event handing inside wxPython?

description: Use matplotlib's event handing inside wxPython


code:
import numpy as np

###################################################
# openCL libraries
###################################################
import pyopencl as cl
import pyopencl.array as cl_array

deviceID = 0
platformID = 0
workGroup=(1,1)

N = 10
testData = np.zeros(N, dtype=cl_array.vec.float2)

dev = cl.get_platforms()[platformID].get_devices()[deviceID]

ctx = cl.Context([dev])
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
Data_In = cl.Buffer(ctx, mf.READ_WRITE, testData.nbytes)

prg = cl.Program(ctx, """

    __kernel void   Pack_Cmplx( __global float2* Data_In, int  N)
    {
      int gid = get_global_id(0);

      Data_In[gid] = 1;
    }
     """).build()

prg.Pack_Cmplx(queue, (N,1), workGroup, Data_In, np.int32(N))
cl.enqueue_copy(queue, testData, Data_In)

print testData

rawDescription: How to use float2 in pyopencl?

description: Use float2 in pyopencl


code:
import re
identifiers = "[a-zA-Z][a-zA-Z0-9]*"
invalid = re.sub(identifiers, "", "hello\nworld\n$myworld\nrandom!!(+")
print invalid

rawDescription: Regex + python27: How to get characters not matching regexes?


code:
#!/usr/bin/python

'''
This module logs python errors.
'''

import socket, os, sys, traceback

def sendError(err):
    # log the error (in my actual implementation, this sends the error to a database)
    with open('log','w') as f:
        f.write(err)

def exceptHandler(etype, value, tb):
    """An additional wrapper around our custom exception handler, to prevent errors in
       this program from being seen by end users."""
    try:
        subProgExceptHandler(etype, value, tb)
    except:
        sys.stderr.write('Sorry, but there seems to have been an error in pylog itself. Please run your program using regular python.\n')

def subProgExceptHandler(etype, value, tb):
    """A custom exception handler that both prints error and traceback information in the standard
       Python format, as well as logs it."""
    import linecache

    errorVerbatim = ''

    # The following code mimics a traceback.print_exception(etype, value, tb) call.
    if tb:
        msg = "Traceback (most recent call last):\n"
        sys.stderr.write(msg)
        errorVerbatim += msg

        # The following code is a modified version of the trackeback.print_tb implementation from
        # cypthon 2.7.3
        while tb is not None:
            f = tb.tb_frame
            lineno = tb.tb_lineno
            co = f.f_code
            filename = co.co_filename
            name = co.co_name
            # Filter out exceptions from pylog itself (eg. execfile).
            if not "pylog" in filename:
                msg = '  File "%s", line %d, in %s\n' % (filename, lineno, name)
                sys.stderr.write(msg)
                errorVerbatim += msg
                linecache.checkcache(filename)
                line = linecache.getline(filename, lineno, f.f_globals)
                if line:
                    msg = '    ' + line.strip() + '\n'
                    sys.stderr.write(msg)
                    errorVerbatim += msg
            tb = tb.tb_next

    lines = traceback.format_exception_only(etype, value)
    for line in lines:
        sys.stderr.write(line)
        errorVerbatim += line

    # Send the error data to our database handler via sendError.
    sendError(errorVerbatim)

def main():
    """Executes the program specified by the user in its own sandbox, then sends
       the error to our database for logging and analysis."""
    # Get the user's (sub)program to run.
    try:
        subProgName = sys.argv[1]
        subProgArgs = sys.argv[3:]
    except:
        print 'USAGE: ./pylog FILENAME.py *ARGS'
        sys.exit()

    # Catch exceptions by overriding the system excepthook.
    sys.excepthook = exceptHandler
    # Sandbox user code exeuction to its own global namespace to prevent malicious code injection.
    execfile(subProgName, {'__builtins__': __builtins__, '__name__': '__main__', '__file__': subProgName, '__doc__': None, '__package__': None})

if __name__ == '__main__':
    main()

rawDescription: log syntax errors and uncaught exceptions for a python subprocess and print them to the terminal

description: Log syntax errors and uncaught exceptions for a python subprocess and print them to the terminal


code:
primeCounter = 1
candidate = 3

while primeCounter < 1000:
    isPrime = True
    counter = 2
    while counter < candidate:
        if candidate%counter == 0:
            isPrime = False
            break # <<<<<<<<<<<<<<<<<   break here, or the loop will go infinite
        else:
            counter = counter + 1

    if isPrime == True:
        print candidate
        primeCounter = primeCounter + 1

    candidate = candidate + 1

rawDescription: Why is my nested while loop not working

description: Why is my nested while loop not working


code:
import string
keyset = set(open('keywords.txt').read().splitlines())
for lineno,line in  enumerate(open('textfile.txt')):
    result = [kw
              for kw in keyset
              for w in line.split()
              if kw in w and w.strip(string.punctuation) == kw]
    if result:
        print "%5s (%s): %s" % (lineno,', '.join(result), line),

rawDescription: How can I turn a list of words in a text file into a regex to filter out?

description: Turn a list of words in a text file into a regex to filter out


code:
import random

#list comprehension, but since you are new use uncommented  way
#numbers = [random.randrange(10, 30) for i in range(0, 10)]

#creates an empty list
numbers = []
#goes from 0 - 9, _ means no variable necessary, just looping a set number of times
for _ in range(0, 10):
    #generates a random number from 10 - 30 and appends it to the numbers list
    numbers.append(random.randrange(10, 30))

#creates an empty string and num of evens and odds at 0
printStr = ''
evens = 0
odds = 0
#for each number in numbers list
for num in numbers:
    #add to print string with number and two asterisks after it
    printStr += '{0}**'.format(num)
    #check if its even, if so increment even count
    if num % 2 == 0:
        evens += 1
    #if not, increment odd count
    else:
        odds += 1
#print string of all numbers and 2 asterisks after each number
print printStr
#print that the list has x evens and x odds
print 'List has {0} even numbers and {1} odd numbers'.format(evens, odds)

rawDescription: Adding to empty set

description: Adding to empty set


code:
for number in iter(lambda: int(input('Choose a number: ')), 5):
    print number # prints it if it wasn't 5...

rawDescription: Is there any way to access the result something never assigned?


code:
import nltk
from nltk.corpus import gutenberg
from random import shuffle

listOfSents = gutenberg.sents()
triggerSentence = raw_input("Please enter the trigger sentence: ")

longestWord = max(triggerSentence.split(), key=len)
longSents = [" ".join(sent) for sent in listOfSents
                 if longestWord in sent
                 and len(sent) > 40]

for sent in shuffle(longSents):
    print sent

rawDescription: How to make this random text generator more efficient in Python?

description: Make this random text generator more efficient in Python


code:
for results in electionResults(soup):
    print results

rawDescription: Why does formatting get all funky when returning more than one string?


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for m in soup.find_all('a'):
    m.replaceWithChildren()
print soup

rawDescription: Removing html hyperlinks anchor from text with regex (in python, pyqt4)


code:
def SingletonAttrs(**names):
    keys = names.keys()
    def class_decorator(cls):
        class Meta(type):
            def __getattribute__(cls, attr):
                if attr in keys:
                    return type.__getattribute__(owner_cls,  attr)
                return type.__getattribute__(cls, attr)
            def __setattr__(cls, attr, value):
                if attr in keys:
                    class Wrapper(object):
                        def __init__(self, value):
                            self.__set__(None, value)
                        __set__ = lambda self, instance, value: setattr(owner_cls,"__" +  attr, value)
                        __get__ = lambda self, instance, owner: type.__getattribute__(owner_cls, "__" + attr)
                    return type.__setattr__(owner_cls,  attr, Wrapper(value))
                return type.__setattr__(cls, attr, value)
        owner_cls = Meta(cls.__name__, cls.__bases__, cls.__dict__.copy())
        for key in keys:
            setattr(owner_cls, key, names[key])
        return owner_cls
    return class_decorator

if __name__ == "__main__":

    @SingletonAttrs(a="value 1", b="value 2")
    class Test(object):
        pass

    class TestB(Test):
        pass

    t = Test()
    print t.a
    print t.b
    tb = TestB()
    tb.a = "value 3"
    print Test.a

rawDescription: python:class attribute/variable inheritance with polymorphism?

description: Python:class attribute/variable inheritance with polymorphism


code:
answer = raw_input("Say something.")
output = []
if "ok" in answer.lower():
    output.append("Yay!")
if "poo" in answer.lower():
    output.append("That's very childish")
if not output:
    output.append("I'm not very talkative.")
for o in output:
  print o

rawDescription: Is there a way to detect if the user last entered text, or the program, in Python?


code:
#!/usr/bin/env python2.6
import time
import multiprocessing

class Model:
    name = ""
    def __init__(self,name):
        self.name = name

def run_experiment(id, model):
    print "Experiment %d is starting" % id
    for _ in range(0,60):
        print "Hey %s from experiment %d" % (model.name, id)
        time.sleep(1)
    print "Experiment %d is done" % id
    return "Result for %d" % id

if __name__ == "__main__":
    model = Model("statictistical model")
    experiments = ((i, model) for i in range(0, 5))
    pool = multiprocessing.Pool(3)

    results = [pool.apply_async(run_experiment, experiment) for experiment in experiments]
    for result in results:
        r = result.get()
        # do something with r
        # or nothing, i suppose...

rawDescription: Python multiprocessing/threading with shared variables that only will be read


code:
import urllib
import re

fbhandle = urllib.urlopen('http://www.facebook.com/Microsoft')
pattern = "6 of(.*)fans" #this wild card doesnt appear to work?
compiled = re.compile(pattern)

ms = compiled.search(fbhandle.read())
print ms.group(1).strip()
fbhandle.close()

rawDescription: Wildcard matching a string in Python regex search


code:
def key_length_match(x):
    # only number of values is important
    xs = x.split()
    return len(xs)

def key_unorderd_match(x):
    # only the occurence of values is important
    xs = x.split()
    return (len(xs), set(xs[1:]))

def key_exact_match(x):
    # exact ordering of values is important
    xs = x.split()
    return (len(xs), xs[1:])

lines = (l.strip() for l in open('input.txt', 'r'))
lines = sorted(lines, key=key_unorderd_match, reverse=True)
for l in lines:
    print l

rawDescription: Sorting a file in Python by columnar patterns


code:
import StringIO

buff = StringIO.StringIO(2048)          # Some decent size, to avoid mid-run expansion
while True:
    data = sock.recv()                  # Pull what it can
    buff.write(data)                    # Append that segment to the buffer
    if '\n' in data: break              # If that segment had '\n', break

# Get the buffer data, split it over newlines, print the first line
print buff.getvalue().splitlines()[0]

rawDescription: python socket readline without socket.makefile()

description: Python socket readline without socket.makefile()


code:
v1 = 0.982769777778
v2 = 0.985980444444
from decimal import Decimal
d1 = Decimal(str(v1)).quantize(Decimal('0.01'))
d2 = Decimal(str(v2)).quantize(Decimal('0.01'))
diff = d2 - d1
print d1, d2, diff

rawDescription: Doing arithmetic with up to two decimal places in Python?


code:
import csv
from collections import namedtuple

def csv_rows(infile, header):
    reader = csv.reader(infile, delimiter="\t")
    if header: next(reader)
    return reader

def data_sets(infile, header):
    gene_data = namedtuple("Data", 'id, name, q, start, end, sym')
    for row in csv_rows(infile, header):
        yield gene_data(*row)

def read_file_type1(infile, header=True):
    # for this file type, we only need to pass the caller the raw
    # data objects
    return data_sets(infile, header)

def read_file_type2(infile, header=True):
    # for this file type, we have to pre-process the data sets
    # before yielding them. A good way to express this is using a
    # generator expression (we could also add a filtering condition here)
    return (transform_data_set(x) for x in data_sets(infile, header))

# Usage sample:
with open("...", "r") as f:
  for obj in read_file_type1(f):
    print obj

rawDescription: What is the most Pythonic way to modify the function of a function?


code:
d = c.decode("windows-1252")
print d

rawDescription: Reading a web page with python but question marks appear instead of apostrophes


code:
import numpy as np
arr = np.fromfunction(lambda i,j: i+j, (6,6))
print arr

rawDescription: Python: how to define a 2D array by comprehension by using function?


code:
def findLargest(l):
    listLength = len(l)
    if listLength == 1:
        return l[0]
    if listLength == 2:
        if l[0] > l[1]:
            print "OMG I Found it: {0}".format(l[0])
            return l[0]
        return l[1]

    halfway = int(listLength/2)
    firsthalf = l[:int(halfway)]
    secondhalf = l[int(halfway):]
    if listIsSorted(firsthalf) and listIsSorted(secondhalf):
        return max(l[halfway - 1], l[-1])
    elif listIsSorted(firsthalf):
        return findLargest(secondhalf)
    else:
        return findLargest(firsthalf)

rawDescription: Value prints correctly in function, but when returned is None


code:
class Media(models.Model):
    pass

class Something(models.Model):
    media = models.ForeignKey(Media)

class Picture(Media):
    foo = models.CharField(max_length=100)

    def get_tiny_object(self):
        return self.foo

class Video(Media):
    bar = models.CharField(max_length=100)

    def get_tiny_object(self):
        return self.bar

picture = Picture.objects.create(foo='some picture')
video = Video.objects.create(bar='some video')
something1 = Something.objects.create(media=picture)
something2 = Something.objects.create(media=video)
print something1.media.get_tiny_object() # this is a picture remember?
print something2.media.get_tiny_object() # and lo, here is a video

rawDescription: Database modeling in django

description: Database modeling in django


code:
import re

def repeated_re_sub(pattern, sub, s, flags=re.U):
    """Match-and-replace repeatedly until we run out of matches..."""
    patc = re.compile(pattern, flags)

    sold = ''
    while sold != s:
        sold = s
        print "patc=>%s<    sold=>%s<   s=>%s<" % (patc,sold,s)
        s = patc.sub(sub, sold)
        #print help(patc.sub)

    return s

print repeated_re_sub('[^\b]\b', '', 'abc\b\x08de\b\bfg')
#print repeated_re_sub('.\b', '', 'abcd\b\x08e\b\bfg')

rawDescription: Apply formatting control characters (backspace and carriage return) to string, without needing recursion


code:
# Always prints 0-9 in order
a_list = [0,1,2,3,4,5,6,7,8,9]
for x in a_list:
    print x

# May or may not print 0-9 in order. Implementation dependent.
a_dict = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
for x in a_dict:
    print x

rawDescription: Is 'for x in array' always result in sorted x? [Python/NumPy]


code:
from multiprocessing import Process, Event, Lock
from time import sleep

def wait(stop_event, lock):
    with lock:
        print "Waiting 2 s..."
    sleep(2)
    with lock:
        print "Stopping processes..."
    stop_event.set()

def loop(stop_event, lock):
    while not stop_event.is_set():
        with lock:
            print "Looping every 0.5 s..."
        sleep(0.5)

if __name__ == '__main__':
    stop_event, lock = Event(), Lock()
    wait_process = Process(target=wait, args=(stop_event, lock))
    loop_process = Process(target=loop, args=(stop_event, lock))
    wait_process.start(), loop_process.start()
    wait_process.join(), loop_process.join()

rawDescription: how to stop one process from another in Python

description: Stop one process from another in Python


code:
import time

tenminutes = 600

while true:
  time.sleep(tenminutes)
  print "This is a reminder"

rawDescription: How can I use the time library in python to do something every ten minutes?

description: Use the time library in python to do something every ten minutes


code:
s = 3
print df
#   Sales  col1
#0      1     3
#1      2     4
#2      3     7
#3      4     6

df1 = df[df['Sales'] >= s]
print df1
#   Sales  col1
#2      3     7
#3      4     6
df2 = df[df['Sales'] < s]
print df2
#   Sales  col1
#0      1     3
#1      2     4

rawDescription: Pandas split DataFrame by column value

description: Pandas split DataFrame by column value


code:
import itertools
import operator
foo = [("a", 1), ("a", 2), ("b", 1), ("c", 1), ("c", 2)]
for group in itertools.groupby(foo, operator.itemgetter(0)):
    print group[0], list(map(operator.itemgetter(1), group[1]))

rawDescription: Combiner function in python hadoop streaming


code:
i = sel.is_text_present("Schedule")    #text that the test case is 'waiting' for
while i != True:
    print i
    print "Waiting for job..."
    sel.refresh()
    i = sel.is_text_present("Schedule")    #text that the test case is 'waiting' for

print "Job found..."

rawDescription: Selenium RC / Python / JavaScript .aspx / Loop Statement


rawDescription: Finding an exact position of a smaller list inside a list (python)


code:
if request.method == 'POST':
        array = simplejson.loads(request.POST['result'])
        response += 'success'
        data = array['array']
        lab.services = data[0]
        lab.states = data[1]
        lab.node_set = data[2]
        lab.selected_nodes = data[3]
        lab.is_slided = data[4]
        lab.is_select_enabled = data[5]
        print lab.is_select_enabled
        lab.save()

rawDescription: django - unexpected behavior when "updating" property in instance of model

description: Django - unexpected behavior when "updating" property in instance of model


code:
import pandas as pd
import multiprocessing as mp

LARGE_FILE = "D:\\my_large_file.txt"
CHUNKSIZE = 100000 # processing 100,000 rows at a time

def process_frame(df):
    # process data frame
    return len(df)

if __name__ == '__main__':
    reader = pd.read_table(LARGE_FILE, chunksize=CHUNKSIZE)
    pool = mp.Pool(4) # use 4 processes

    funclist = []
    for df in reader:
        # process each data frame
        f = pool.apply_async(process_frame,[df])
        funclist.append(f)

    result = 0
    for f in funclist:
        result += f.get(timeout=10) # timeout in 10 seconds

    print "There are %d rows of data"%(result)

rawDescription: parallel processing in pandas python

description: Parallel processing in pandas python


code:
import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.status = tk.Label(anchor="w", text="woot")
        self.text = tk.Text(wrap="word", width=60, height=10)
        self.status.pack(side="bottom", fill="x")
        self.text.pack(side="top", fill="both", expand=True)

        self.text.insert("1.0", "Move your cursor around to see what " +
                         "index is under the cursor, and what " +
                         "text is selected\n")
        self.text.tag_add("sel", "1.10", "1.16")

        # when the cursor moves, show the index of the character
        # under the cursor
        self.text.bind("<Any-Motion>", self.on_mouse_move)

    def on_mouse_move(self, event):
        index = self.text.index("@%s,%s" % (event.x, event.y))
        ch = self.text.get(index)
        pos = "%s/%s %s '%s'" % (event.x, event.y, index, ch)
        try:
            sel = "%s-%s" % (self.text.index("sel.first"), self.text.index("sel.last"))
        except Exception, e:
            sel = "<none>"
        self.status.configure(text="cursor: %s selection: %s" % (pos, sel))

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: How do I return word under cursor in tkinter?

description: Return word under cursor in tkinter


code:
#!/usr/bin/env python
f = open('log.txt','r')

columns = {}

for line in f.readlines():
    if line.startswith('#Fields:'):
        for index, substring in enumerate(line.split(' ')):
            columns[substring] = index
    elif line.startswith('#'):
        continue
    else:
        print line.split(' ')[columns['cs(User-Agent)']]

rawDescription: get all values in cs(Referer)

description: Get all values in cs(Referer)


rawDescription: python regular expression with re.split()

description: Python regular expression with re.split()


code:
from collections import defaultdict
d = defaultdict(list)
for x in myList:
   d[type(x)].append(x)

print d[int]
print d[str]

rawDescription: Python - Splitting List That Contains Strings and Integers


code:
for x in my_list:
    doSomething()
if not my_list:
    print "my_list is empty"

rawDescription: for/empty loop condition in python

description: For/empty loop condition in python


code:
class ApplicationError(Fault):

    def __init__(self, exc_info):
        Fault.__init__(self, xmlrpclib.APPLICATION_ERROR,
                       u'Application internal error')

class NotWellformedError(Fault):

    def __init__(self, exc):
        Fault.__init__(self, xmlrpclib.NOT_WELLFORMED_ERROR, str(exc))

class UnsupportedEncoding(Fault):

    def __init__(self, exc):
        Fault.__init__(self, xmlrpclib.UNSUPPORTED_ENCODING, str(exc))

# XXX INVALID_ENCODING_CHAR is masked by xmlrpclib, so the error code will be
# INVALID_XMLRPC.
class InvalidRequest(Fault):

    def __init__(self, message):
        ault.__init__(self, xmlrpclib.INVALID_XMLRPC, message)

class MethodNotFound(Fault):

    def __init__(self, name):
        Fault.__init__(self, xmlrpclib.METHOD_NOT_FOUND,
                       u'Method %r is not supported' % name)

class WrongMethodUsage(Fault):

    def __init__(self, message):
        Fault.__init__(self, xmlrpclib.INVALID_METHOD_PARAMS, message)

class WrongType(Fault):

    def __init__(self, arg_name, type_name):
        Fault.__init__(self, xmlrpclib.INVALID_METHOD_PARAMS,
                       u'Parameter %s must be %s' % (arg_name, type_name))

class XMLRPCDispatcher(SimpleXMLRPCDispatcher, XMLRPCDocGenerator):

    server_name = server_title = 'Personalization center RPC interface'
    server_documentation = 'Available methods'

    def __init__(self, methods):
        SimpleXMLRPCDispatcher.__init__(self, allow_none=True, encoding=None)
        self.register_instance(methods)
        self.register_multicall_functions()
        #self.register_introspection_functions()

    def _dispatch(self, method_name, args):
        if self.funcs.has_key(method_name):
            method = self.funcs[method_name]
        else:
            method = self.instance._getMethod(method_name)
        arg_names, args_name, kwargs_name, defaults = \
                                                inspect.getargspec(method)
        assert arg_names[0]=='self'
        arg_names = arg_names[1:]
        n_args = len(args)
        if not (args_name or defaults):
            if n_args!=len(arg_names):
                raise WrongMethodUsage(
                    u'Method %s takes exactly %d parameters (%d given)' % \
                                (method_name, len(arg_names), n_args))
        else:
            min_args = len(arg_names)-len(defaults)
            if len(args)<min_args:
                raise WrongMethodUsage(
                    u'Method %s requires at least %d parameters (%d given)' % \
                                (method_name, min_args, n_args))
            if not args_name and n_args>len(arg_names):
                raise WrongMethodUsage(
                    u'Method %s requires at most %d parameters (%d given)' % \
                                (method_name, len(arg_names), n_args))
        try:
            return method(*args)
        except Fault:
            raise
        except:
            logger.exception('Application internal error for %s%r',
                             method_name, args)
            raise ApplicationError(sys.exc_info())

    def dispatch(self, data):
        try:
            try:
                args, method_name = xmlrpclib.loads(data)
            except ExpatError, exc:
                raise NotWellformedError(exc)
            except LookupError, exc:
                raise UnsupportedEncoding(exc)
            except xmlrpclib.ResponseError:
                raise InvalidRequest('Request structure is invalid')
            method_name = method_name.encode('ascii', 'replace')
            result = self._dispatch(method_name, args)
        except Fault, exc:
            logger.warning('Fault %s: %s', exc.faultCode, exc.faultString)
            return xmlrpclib.dumps(exc)
        else:
            try:
                return xmlrpclib.dumps((result,), methodresponse=1)
            except:
                logger.exception('Application internal error when marshalling'\
                                 ' result for %s%r', method_name, args)
                return xmlrpclib.dumps(ApplicationError(sys.exc_info()))

class InterfaceMethods:

    def _getMethod(self, name):
        if name.startswith('_'):
            raise MethodNotFound(name)
        try:
            method = getattr(self, name)
        except AttributeError:
            raise MethodNotFound(name)
        if not inspect.ismethod(method):
            raise MethodNotFound(name)
        return method

rawDescription: XML-RPC server with better error reporting


code:
import requests
from bs4 import BeautifulSoup

my_headers={"Host": "www.google.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,am;q=0.7,zh-HK;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Cookie": "PREF=ID=1111111111111111:FF=0:LD=en:TM=1439993585:LM=1444815129:V=1:S=Zjbb3gK_m_n69Hqv; NID=72=F6UyD0Fr18smDLJe1NzTReJn_5pwZz-PtXM4orYW43oRk2D3vjb0Sy6Bs_Do4J_EjeOulugs_x2P1BZneufegpNxzv7rkY9BPHcfdx9vGOHtJqv2r46UuFI2f5nIZ1Cu4RcT9yS5fZ1SUhel5fHTLbyZWhX-yiPXvZCiQoW4FjZd-3Bwxq8yrpdgmPmf4ufvFNlmTd3y; OGP=-5061451:; OGPC=5061713-3:",
"Connection": "keep-alive"}

googtrends = requests.get("https://www.google.com/trends/",headers=my_headers)
my_content = googtrends.text.encode('utf-8')
soup = BeautifulSoup(my_content,'html.parser')
links = soup.find_all("a", {"class": "trending-story ng-isolate-scope"},href=True)

#Lets try if we are getting correct content from the site
# That site contains "Apple Inc., App Store" so let's check it in the got response

print 'Apple Inc., App Store' in my_content

# It prints false so website is being rendered by JS even header change does not affect

rawDescription: Beautiful Soup won't return results

description: Beautiful Soup won't return results


code:
v1 = {x2-x1, y2-y1}   # Vector 1
v2 = {x2-xA, y2-yA}   # Vector 1
xp = v1.x*v2.y - v1.y*v2.x  # Cross product
if xp > 0:
    print 'on one side'
elif xp < 0:
    print 'on the other'
else:
    print 'on the same line!'

rawDescription: How can I check if a point is below a line or not?

description: Check if a point is below a line or not


code:
files = {}
for fpath in glob.glob("*.txt"):
    with open(fpath) as f:
         fixed_text = re.sub("[^a-zA-Z'-]"," ",f.read())
    words = fixed_text.split()
    total_words = len(words)
    total_unique = len(set(words))
    files[fpath] = (total_words, total_unique)
    print "Total words:", total_words
    print "Total unique:", total_unique

with open("some_csv.csv", "w") as f:
    for fname in files:
        print >> f, "%s,%s,%s" % (fname, files[fname][0], files[fname][1])

rawDescription: Count two values (total words & uniques) in multiple files (texts) and output a csv in Python


code:
result, _ = zip(*find_phrase())
print result

rawDescription: Accessing a range of the first element of a tuple in a list of tuples


code:
for msg_num in M.search(None, "UNDELETED")[1][0].split():
    msg = M.fetch('1', '(BODY.PEEK[TEXT])')
    a = msg[1][0][1][139:161]
    print a

# later..
if a == 'this is just a test...':
   # your code here
   pass

rawDescription: Python: How do I save something that printed in a terminal as a variable to compare later?


code:
class Foo(object):
    def __init__(self, value):
        self._a = [value]

    def __geta(self):
        return self._a[0]

    def __seta(self, obj):
        self._a.insert(0, obj)
        self._a.pop(1)

    a = property(__geta, __seta)

var1 = Foo(5)
print var1.a
var1.a = 3
print(var1.a)

rawDescription: change pointed instance inside a function in python

description: Change pointed instance inside a function in python


code:
from ggplot import *

p = ggplot(aes(x='C1', y='C2', color='Population'), data=mds)+ geom_point(alpha=0.6) + ggtitle('My plot')

print p

rawDescription: Plot different color for different categorical levels in ggplot


code:
import cProfile
import pstats
import glob
import math

def gen():
    for i in range(1, 10):
        yield math.factorial(i)

class index(object):
    def GET(self):
        p = cProfile.Profile()

        it = gen()
        while True:
            try:
                nxt = p.runcall(next, it)
            except StopIteration:
                break
            print nxt

        p.print_stats()

index().GET()

rawDescription: Profiling Python generators

description: Profiling Python generators


code:
import sys
import numpy as np

try:
    ifile = sys.argv[1]
   #; ofile = sys.argv[2]
except:
    print "Usage:", sys.argv[0], "ifile"; sys.exit(1)

# Open and read file from std, and assign first four (orbit, time, lat, lon)
# columns to four lists, and last 128 columns (waveforms) to an array.

def remove_bracket(line):
    return float(line.strip("][ '"))

data = np.genfromtxt(ifile, delimiter = ',',
                     dtype = 'float',
                     converters = {i:remove_bracket for i in range(132)}
                     )

orbit = data[:,0]
time = data[:,1]
lat = data[:,2]
lon = data[:,3]
wvf = data[:,4:128]
print(wvf)

rawDescription: Python: Reading cvs file into lists and an array


code:
string1 = 'Hello'
string2 = 'hello'

if string1.lower() == string2.lower():
    print "The strings are the same (case insensitive)"
else:
    print "The strings are not the same (case insensitive)"

rawDescription: How do I do a case insensitive string comparison in Python?

description: Do a case insensitive string comparison in Python


code:
for row in The_cursor:
    print row
    if row[0] == 'United States' and row[1] < 8000000:
        The_cursor.deleteRow()
    elif row [0] == 'Mexico' and row[1] < 8000000:
        The_cursor.deleteRow()
    elif row[0] == 'Canada' and row[1] < 3000000:
        The_cursor.deleteRow()

rawDescription: Printng row in Python

description: Printng row in Python


code:
def compareDate (dataType, actualResult, rIndex, rLen):
    hyphenCount , result , count = 0 , 0 , 0
    dateString = ""
    while (rIndex < rLen):
        ch = actualResult[rIndex]
        if (ch == '.'):
            break
        if(ch >= '0' and ch <= '9'):
            hyphenCount = 0
            count += 1
            if(count > 2):
                result = -1
                print " d = "
                break;
            dateString += str(ch)
        if(ch == '-'):
            count = 0
            hyphenCount += 1
            if(hyphenCount > 1):
                print " h = "
                result = -1
                break

        #print ("Char %c" % ch)
        rIndex += 1
    print "dateString is = %s" % dateString

    if result != -1:
        msg = ""
        mt = dateString[0] + dateString[1]
        dt = dateString[2] + dateString[3]
        yr = dateString[4] + dateString[5]
        leap = '20'+yr
        print mt, dt, yr
        mt = int(mt)
        dt = int(dt)
        yr = int (yr)
        leap = int(yr)
        #create and call function which verify date
        result,msg = verifyDate(mt,dt,yr,leap)
        print msg

    return (result, rIndex)

def verifyDate(mt,dt,yr,leap):
    res = 0
    msg = ""
    leapyr = checkForLeapYear(leap)
    if(yr >= 00 and yr <= 99):
        if(leapyr): # for leap year
            if(mt == 2):
                if(dt >= 01 and dt <= 29):
                    res = 0
                else:
                    msg = "should not exceed 29"
            elif(mt == 1 or mt == 3 or mt == 5 or mt == 7 or mt == 8 or mt == 10 or mt == 12):
                    if(dt >= 01 and dt <= 31):
                        res = 0
                    else:
                        msg = "should not exceed 31"
                        res = -1
            elif(mt == 4 or mt == 6 or mt == 9 or mt == 11):
                    if(dt >= 01 and dt <= 30):
                        res = 0
                    else:
                        msg  = "should not exceed 30"
                        res = -1
            else:
                    msg  = "month should in btwn 01 to 12"
                    res = -1
            # for leap year ...Ends Here

        else:
            if((mt >= 1) and (mt <= 12)):
                if(mt == 2):
                    if(dt >= 01 and dt <= 28):
                        res = 0
                    else:
                        msg = "should not exceed 28"
                        res = -1
                elif(mt == 1 or mt == 3 or mt == 5 or mt == 7 or mt == 8 or mt == 10 or mt == 12):
                    if(dt >= 01 and dt <= 31):
                        res = 0
                    else:
                        msg = "should not exceed 31"
                        res = -1
                elif(mt == 4 or mt == 6 or mt == 9 or mt == 11):
                    if(dt >= 01 and dt <= 30):
                        res = 0
                    else:
                        msg  = "should not exceed 30"
                        res = -1
            else:
                    msg  = "month should in btwn 01 to 12"
                    res = -1

    else:
        msg  = "year should in btwn 00 to 99"
        res = -1
    return (res,msg)

def checkForLeapYear(yr):
    if((yr %100) == 0 and (yr % 400 == 0 )):
        return True
    elif(yr%4 == 0):
        return True
    else:
        return False

rawDescription: Fetch and validate date by reading char by char in python


code:
from multiprocessing import Pool
import time
import random

class DATA:
    def __init__(self, obj, new_id):
        self.obj = obj
        self.new_id = new_id

def collect_id(data):
    print 'collecting data for obj {0} with id {1}'.format(data.obj, data.new_id)

def create(obj):
    pool = Pool()
    # "do something in database"
    # "return ID for newly created data in DB"
    newly_created_id = int(random.random()*10)
    data = DATA(obj, newly_created_id)
    r  = pool.map_async(collect_id, [data])
    return newly_created_id

new_id = create({'some':'object'})
print "new id: {0}".format(new_id)

rawDescription: call a method Asynchronously from another method

description: Call a method Asynchronously from another method


code:
import os
import hashlib

location = '/Users/jeff/desktop/typflashdrive'
doc_count = 0
dup_doc_count = 0

hash_vs_file = {}

for (dirname, dirs, files) in os.walk(location):
    for filename in files:
        file_path = os.path.join(dirname, filename)
        file_hash = hashlib.md5(open(file_path).read()).hexdigest()
        if filename.endswith('.doc'):
            doc_count = doc_count + 1
            if file_hash not in hash_vs_file:
                hash_vs_file[file_hash] = [file_path]
            else:
                dup_doc_count += 1
                hash_vs_file[file_hash].append(file_path)

print 'doc_count = ', doc_count
print 'dup_doc_count = ', dup_doc_count

for file_hash in hash_vs_file:
    print file_hash
    for file_path in hash_vs_file[file_hash]:
        print file_path
    print "\n\n\n"

rawDescription: Checking duplicate files against a dictionary of filesizes and names


code:
def measure_memory( process ):
    w = wmi.WMI('.')
    result = w.query("SELECT WorkingSetSize FROM Win32_Process WHERE ProcessID="+str(process.pid))
    print result
    subset = result[0]
    return "%.2f" % (float(subset.WorkingSetSize)/1024./1024.)

rawDescription: How to get the memory working set of a 64bit process in python 2.7 32bit?

description: Get the memory working set of a 64bit process in python 2.7 32bit


code:
   #Create a Dictionary to represent the possible
   #exits from a location in an adventure game

def get_dir():
    good_answers = ["north", "south", "east", "west", "quit"]
    direction = raw_input("Enter your direction: ").lower()
    while direction not in good_answers:
        direction = raw_input("Bad direction, try again: ").lower()
    return direction

game = {"north" : "North leads to garden.",
    "south" : "South leads to the kitchen.",
    "east" : "East leads to the dining room.",
    "west" : "West leads to the living room."}

print "Press quit to exit"
direction = get_dir()
while direction != "quit":
    print game[direction]
    direction = get_dir()

print "Quitting..."

rawDescription: Python while statment doesnt work

description: Python while statment doesnt work


code:
def foo():
    spam = 'eggs'
    def bar():
        print spam
foo()

rawDescription: python yield generator variable scope

description: Python yield generator variable scope


code:
print A
#  key1 key2  C1  C2
#0    A    X   5   2
#1    A    Y   3   2
#2    B    X   6   1
#3    B    Y   1   3
#4    C    Y   1   4
#5    D    X   2   3
#6    D    Y   1   3

def f(df):
    df['RESULT'] = df['C2'].sum() + df['C1'].loc[df['key2'] == 'Y'].sum()
    df['RESULT'].loc[df['key2'] == 'X'] = 0
    return df

df = A.groupby('key1', sort = False).apply(f)
print df
#  key1 key2  C1  C2  RESULT
#0    A    X   5   2       0
#1    A    Y   3   2       7
#2    B    X   6   1       0
#3    B    Y   1   3       5
#4    C    Y   1   4       5
#5    D    X   2   3       0
#6    D    Y   1   3       7

rawDescription: conditional dataframe operations using Pandas

description: Conditional dataframe operations using Pandas


code:
characters = {
'1': 'Stark',
'2': 'Greyjoy',
'3': 'California',
'4': 'Whitewalkers',
'5': 'Knights Watch',
'6': 'Dalthraki',
}

print 'Type 1 for Stark'
print 'Type 2 for Greyjoy'
print 'Type 3 for Lannister'
print 'Type 4 for Whitewalkers'
print 'Type 5 for Knights Watch'
print 'Type 6 for Dalthraki'
choice = raw_input("> ")
if choice in characters:
    print 'You\'ve selected', characters[choice]
else:
    pass

rawDescription: Accessing Dictionary with Raw Input

description: Accessing Dictionary with Raw Input


code:
#first we need to create mappings in a dict for your reflections
reflect = {
    'you': 'I',
    'are': 'am'
}

# After we read user input
user_input = 'hello, how are you ?'

#Now look how we can replace all words in user_input from reflect with one line
reflected = [word for word in [reflect.get(key, key) for key in user_input.split()]]

print ' '.join(reflected)

rawDescription: finding strings in one list and based on what it says, replacing it with strings from another list

description: Finding strings in one list and based on what it says, replacing it with strings from another list


code:
for i, (start, end) in enumerate(searchPFAM(fname)):
    print start, end
    if start <= end:
        with open('newfile-%s.txt' % i,'w') as fileinput:
            text=''.join(makeList[(start-1):(end-1)])
            fileinput.write(text)

rawDescription: Is there a way to count how many values I have in yield?


rawDescription: Split long conditional expressions to lines


code:
from ctypes import cdll, py_object
lib = cdll.LoadLibrary('./lib1.so')

d = lib.foo('hello')
print d                     # prints address of object

lib.foo.restype = py_object # change the result type
d = lib.foo('hello')
print d                     # prints (2, 2) as expected

rawDescription: Python, handling Py_buildValue in python code


code:
import datetime
datestring = "1277722499.82"
dt = datetime.datetime.fromtimestamp(float(datestring))
print dt

rawDescription: Convert Unix Timestamp to human format in Django with Python


code:
from bs4 import BeautifulSoup
import urllib2
url = 'http://www.snapdeal.com/'
data = urllib2.urlopen(url).read()

page = BeautifulSoup(data,'html.parser')

for link in page.findAll('a'):
       l = link.get('href')
       print l

rawDescription: scraping all links using beautifulSoup/lxml

description: Scraping all links using beautifulSoup/lxml


code:
import time
import calendar
stra ="Sat Nov 30 20:48:48 +0000 2013"
formata = "%a %b %d %H:%M:%S +0000 %Y"
print calendar.timegm(time.strptime(stra, formata))

rawDescription: How to convert the following time format to epoch time

description: Convert the following time format to epoch time


code:
content = r'\xe87\x00\x10LOL'
print content
content
content.decode('string-escape')
map(hex, map(ord, content.decode('string-escape')))
bytes(map(ord, content.decode('string-escape')))
bytearray(map(ord, content.decode('string-escape')))

rawDescription: Python: Convert a string to its binary representation


code:
pairs = [("a", 1), ("b", 2), ("c", 3)]
for a, b in pairs:
   print a, b

rawDescription: How can I access each element of a pair in a pair list?

description: Access each element of a pair in a pair list


code:
with open('u.txt') as uf, open('v.txt') as vf:
    for u,v in zip(uf,vf):
        print uv2sd(float(u),float(v))

rawDescription: How to extract data in *.txt file and use it in Python?

description: Extract data in *.txt file and use it in Python


code:
import re

print bool(re.search("domain.com(?!/user/)", "domain.com"))
print bool(re.search("domain.com(?!/user/)", "domain.com/user/test"))
print bool(re.search("domain.com(?!/user/)", "domain.com/test"))

rawDescription: Python Regex Search Ignore String

description: Python Regex Search Ignore String


code:
import requests
import time
import json
import urllib
import urllib2
import datetime
import tweepy

ct = "INSERT CONSUMER TOKEN"
cs = "INSERT CONSUMER SECRET"
at = "INSERT ACCESS TOKEN"
acs = "INSERT CONSUMER SECRET"
count = 1

twitter = "transportgovuk"
consumer_token = ct
consumer_secret = cs
access_token = at
access_secret = acs
auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
auth.set_access_token(access_token,access_secret)

api  = tweepy.API(auth)

status_ids= "724916468975046656"

for status in tweepy.Cursor(api.user_timeline,twitter,max_id=status_ids).items(count):
    print status

rawDescription: How do I add max_id to my twitter (tweepy) request? (Python 2.7)

description: Add max_id to my twitter (tweepy) request? (Python 2.7)


rawDescription: Extend a python list to contain all the numbers in between


code:
def LeID(Xv,Yv,Dv):
    import uuid
    import string
    import arcpy
    MyNS = uuid.UUID('f82aa75f-56a8-442b-89d2-eb7aaae6f9c3')
    X_val = "%.6f" % Xv
    Y_val  = "%.6f" % Yv
    date = Dv
    xarr = string.split(X_val, '.')
    yarr = string.split(Y_val, '.')
    date = string.split(date , '/')
    val =  str(xarr[0] + xarr[1] + yarr[0] + yarr[1] + date[0]  + date[1] + date[2] )
    print MyNS, repr(val) # for debugging
    return '{' + str(uuid.uuid5(MyNS, val)) + '}'

rawDescription: Python UUID badly formed hexadecimal string


code:
from itertools import groupby
data = ['a','s','s','s','d','e','e','s','s','a','a']
output = [(key, len(list(group))) for key, group in groupby(data)]
print output

rawDescription: Python: group list items by adjacent same value


code:
from selenium import webdriver
from bs4 import BeautifulSoup

link = "https://www.iaai.com/Vehicles/VehicleDetails.aspx?auctionID=14712591&itemID=15775059&RowNumber=0"
browser = webdriver.Firefox()
browser.get(link)
page = browser.page_source

soup = BeautifulSoup(page)

table = soup.find('dd', attrs = {'class': 'tip_vehicleStats'})
vin = table.contents.span.contents[0]
print vin

rawDescription: Open a page programatically in python

description: Open a page programatically in python


code:
@classmethod
def parse(cls, api, raw):
        status = cls.first_parse(api, raw)
        setattr(status, 'json', json.dumps(raw))
        return status

tweepy.models.Status.first_parse = tweepy.models.Status.parse
tweepy.models.Status.parse = parse

_dir = os.path.dirname(os.path.abspath(__file__))

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print status.user.screen_name
        with codecs.open(os.path.join(_dir, 'tweets.json'), "a", 'utf-8') as textFile:
                textFile.write(status.json)
                textFile.write('\n')

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

rawDescription: StremListener does't return data in JSON in tweepy


code:
#these values set how sensitive the bounding box detection is
threshold = 200     #the average of the darkest values must be _below_ this to count (0 is darkest, 255 is lightest)
obviousness = 50    #how many of the darkest pixels to include (1 would mean a single dark pixel triggers it)

from PIL import Image

def find_line(vals):
    #implement edge detection once, use many times
    for i,tmp in enumerate(vals):
        tmp.sort()
        average = float(sum(tmp[:obviousness]))/len(tmp[:obviousness])
        if average <= threshold:
            return i
    return i    #i is left over from failed threshold finding, it is the bounds

def getbox(img):
    #get the bounding box of the interesting part of a PIL image object
    #this is done by getting the darekest of the R, G or B value of each pixel
    #and finding were the edge gest dark/colored enough
    #returns a tuple of (left,upper,right,lower)

    width, height = img.size    #for making a 2d array
    retval = [0,0,width,height] #values will be disposed of, but this is a black image's box

    pixels = list(img.getdata())
    vals = []                   #store the value of the darkest color
    for pixel in pixels:
        vals.append(min(pixel)) #the darkest of the R,G or B values

    #make 2d array
    vals = np.array([vals[i * width:(i + 1) * width] for i in xrange(height)])

    #start with upper bounds
    forupper = vals.copy()
    retval[1] = find_line(forupper)

    #next, do lower bounds
    forlower = vals.copy()
    forlower = np.flipud(forlower)
    retval[3] = height - find_line(forlower)

    #left edge, same as before but roatate the data so left edge is top edge
    forleft = vals.copy()
    forleft = np.swapaxes(forleft,0,1)
    retval[0] = find_line(forleft)

    #and right edge is bottom edge of rotated array
    forright = vals.copy()
    forright = np.swapaxes(forright,0,1)
    forright = np.flipud(forright)
    retval[2] = width - find_line(forright)

    if retval[0] >= retval[2] or retval[1] >= retval[3]:
        print "error, bounding box is not legit"
        return None
    return tuple(retval)

if __name__ == '__main__':
    image = Image.open('cat.jpg')
    box = getbox(image)
    print "result is: ",box
    result = image.crop(box)
    result.show()

rawDescription: Trim scanned images with PIL?

description: Trim scanned images with PIL


code: print " ".join(word.rstrip("!") for word in text.split())

rawDescription: How to strip characters from the right side of every word in Python?

description: Strip characters from the right side of every word in Python


code:
def main(path, filename):
# path to where your .csv lives, and the name of the csv.
    import geopy
    from geopy.geocoders import ArcGIS
    import pandas as pd

    Target_Addresses = pd.read_csv(path+'\\'+filename)
    Target_Addresses['Lat'] = np.nan
    Target_Addresses['Long'] = np.nan
    Indexed_Targets = Target_Addresses.set_index('UniqueID')

    geolocator = ArcGIS() #some parameters here
    Fails = []
    for index, row in Indexed_Targets.iterrows():
        Address = row['Address']
        Result = geolocator.geocode(Address)
        if Result == None:
            Result = geolocator.geocode(Address[:-7])
            if Result == None:
                Fails.append[Address]
            else:
                Indexed_Targets.set_value(index, 'Lat', Result.latitude)
                Indexed_Targets.set_value(index, 'Long', Result.longitude)
        else:
            Indexed_Targets.set_value(index, 'Lat', Result.latitude)
            Indexed_Targets.set_value(index, 'Long', Result.longitude)
    for address in Fails:
        print address
    Indexed_Targets.to_csv(filename[:-4]+"_RESULTS.csv")

if __name__ == '__main__':
    main(path, filename) # whatever these are for you...

rawDescription: Geocoding using Geopy and Python

description: Geocoding using Geopy and Python


code:
for a, b in enumerate(blob, 1):
    print '{} {}'.format(a, b)

rawDescription: How to print out a numbered list in Python 3

description: Print out a numbered list in Python 3


code:
from win32com.shell import shell, shellcon
print shell.SHGetFolderPath(0, shellcon.CSIDL_MYPICTURES, None, 0)
# prints something like C:\Documents and Settings\Username\My Documents\My Pictures
# (Unicode object)

rawDescription: Python, get windows special folders for currently logged-in user


code:
from datetime import datetime

now = datetime.now()
print now
# >>> 2013-11-28 15:34:47.223000
now = now.replace(microsecond=0) # Remove milliseconds
print now
# >>> 2013-11-28 15:34:47
print now.strftime('%Y-%m-%d %H:%M:%S.%f')
# >>> 2013-11-28 15:34:47.000000

rawDescription: cx_Oracle not returning milliseconds for 1900-01-01 00:00:00.000000

description: Cx_Oracle not returning milliseconds for 1900-01-01 00:00:00.000000


code:
import sys
from PIL import Image

im=Image.open("test.jpg")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux >(200,200,200):
            im.putpixel((i,j),(255,0,0))
        else:
            im.putpixel((i,j),(0,0,0))

im.save("test_copy.jpg")
im.save("test_copy.png")

im=Image.open("test_copy.jpg")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux not in color:
            color[aux]=1
        else:
            color[aux]+=1

print "Compressed file stat"
print color

im=Image.open("test_copy.png")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux not in color:
            color[aux]=1
        else:
            color[aux]+=1

print "Uncompressed file stat"
print color

rawDescription: pil related script : picture not getting polarized

description: Pil related script : picture not getting polarized


code:
sin,sout,serr = ssh.exec_command("while true; do uptime; done")

def line_buffered(f):
    line_buf = ""
    while not f.channel.exit_status_ready():
        line_buf += f.read(1)
        if line_buf.endswith('\n'):
            yield line_buf
            line_buf = ''

for l in line_buffered(sout):   # or serr
    print l

rawDescription: MapReduce with paramiko how to print stdout as it streams


code:
import os

os.system("batch <<< '/home/some_user/do_the_due.py'")
# or if you don't want to wait for system idle,
#   os.system("at now <<< '/home/some_user/do_the_due.py'")

print 'Content-type: text/html\n'
print 'Done!'

rawDescription: Start background process/daemon from CGI script


code:
import re
str = 'finding exstention from string on http://domain.coms/index/page/2'
pattern = re.compile(r'([^\/]*\.(?:com|en|org))')
m = pattern.search(str)
print m.group(1)

rawDescription: Find a regular extension of domains on a string python Regex


code:
from multiprocessing import Pool
import time
import random

def work(i):
    time.sleep(0.1*i)
    return i

def main():
    p = Pool(4)
    nums = range(50)
    random.shuffle(nums)

    start = time.time()
    print 'Using imap'
    for i in p.imap(work, nums):
        print i
    print 'Time elapsed: %s' % (time.time() - start)

    start = time.time()
    print 'Using imap_unordered'
    for i in p.imap_unordered(work, nums):
        print i
    print 'Time elapsed: %s' % (time.time() - start)

if __name__ == "__main__":
    main()

rawDescription: In what situation do we need to use `multiprocessing.Pool.imap_unordered`?


code:
from django.db import connection
cursor = connection.cursor()
code = 'statin'
query = 'SELECT * FROM chemical WHERE name ~* {}'
print query.format(code)
cursor.execute(query.format(code))
print cursor.fetchall()

rawDescription: Regex search from django.db.connection?

description: Regex search from django.db.connection


code:
with open("qwer.txt", 'r') as my_file:
    for line in my_file:
        print line.split('=')[0].split('.')[1]

rawDescription: python parse and print text before .(dot)

description: Python parse and print text before .(dot)


code:
a = sc.parallelize([1,2,3]).distinct()
print a.toDebugString()

rawDescription: Spark toDebugString not nice in python

description: Spark toDebugString not nice in python


code:
from datetime import datetime, tzinfo, timedelta

timestamp = "1322485986.672901000"

class UTC(tzinfo):
    def utcoffset(self, dt):
         return timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return timedelta(0)

d1 = datetime.fromtimestamp(float(timestamp), UTC())
print d1

rawDescription: Converting string timestamp to Python datetime (was: how to properly convert char into float or double in Python)


code:
import serial

def serialConnect():
    serlocations=['/dev/ttyACM', '/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10','/dev/ttyS0', '/dev/ttyS1', '/dev/ttyS2', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1', 'end']
    for device in serlocations:
        try:
            ser = serial.Serial(
                port=device,
                baudrate=9600,
                parity=serial.PARITY_ODD,
                stopbits=serial.STOPBITS_TWO,
                bytesize=serial.SEVENBITS
            )
            print device
            return ser
        except:
            x=0
    if device == 'end':
        print "No Device Found"

ser = serialConnect()
if ser:
    ser.write("TEST")
    ser.timeout=5
    for i in ser.readlines():
        print i

rawDescription: Can I use the USB port of the raspberry pi (model B+) for serial communication (RS232)

description: Use the USB port of the raspberry pi (model B+) for serial communication (RS232)


code:
mySet = {"hello", "lo"}
for item in mySet:
    for other_item in mySet.difference([item]):
        if item in other_item:
            print item, other_item

rawDescription: Python, sets, Comparing two elements in the same set


code:
def filters():
    mypatterns=[]
    for LogLine in Log:
        flag = True
        for key,ConfLine in Conf.items():
            for patterns in ConfLine:
                print patterns
                mypatterns.append(patterns)
                if re.match((DateString + patterns), LogLine):
                    flag = False
                    break
            if(flag == False):
                break
        if(flag):
            print LogLine

print mypatterns

rawDescription: Print all in once

description: Print all in once


code:
n_user = 3
n_channel = 2
n_pixels = 3
A = np.zeros(shape=(n_user, n_channel, n_pixels))

for i in range(n_user):
    A[i, 0, :] = np.arange(n_pixels)
    A[i, 1, :] = np.arange(n_pixels) + n_pixels
print A

mu_f    = np.zeros(shape=n_channel)
sigma_f = np.zeros(shape=n_channel)

for i in range(n_channel):
    mu_f[i]    = np.mean(A[:,i,:])
    sigma_f[i] = np.std(A[:,i,:])

print mu_f
print sigma_f

for i in range(n_channel):
    A[:, i, :] -= mu_f[i]
    A[:, i, :] /= sigma_f[i]

print A

print np.mean(A[:,0,:])
print np.std(A[:,0,:])

rawDescription: Standardization/preprocessing for 4-dimensional array


code:
from itertools import chain
d1={'A': [], 'C': ['SUV'], 'B': []}
print list(chain.from_iterable(d1.itervalues()))

rawDescription: Concatenate lists together

description: Concatenate lists together


code:
import json
import urllib2
import MultipartPostHandler

import httplib, urllib

data2 = ""

conn = httplib.HTTPConnection("localhost", 8080)
conn.request("GET", "/get_blobstore_url/")
r1 = conn.getresponse()
print r1.status, r1.reason

data2 = r1.read()

params = {'file':open('test_mat.jpg', 'rb')}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)

print data2
req = urllib2.Request(data2, params)

response = urllib2.urlopen(req).read().strip()

rawDescription: Upload files in blobstore without the form


code:
phrases = [['Lionhearted', 'Baby Preach'], ['Lionhearted Baby', 'Preach'], ['Lionhearted', 'Baby', 'Preach']]
artist_info = {u'Baby': u'Baby by Justin Bieber', u'Lionhearted': u'Lionhearted by Porter Robinson', u'Preach': u'Preach by Drake', u'Baby Preach': u'Baby Preach by some pop singer'}

lst = filter(lambda x: all(y in artist_info for y in x), phrases)
phrases2 = map(lambda x: [artist_info[y] for y in x], lst)
print phrases2

rawDescription: Conditionally replacing items in nested list based on values in dictionary


code:
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener

from myproject.myapp.utils import do_something_with_tweet

class StdOutListener(StreamListener):

    def on_data(self, data):
        do_something_with_tweet(data)
        return True

def main():
    listener = StdOutListener()

    auth = OAuthHandler(
        TWITTER_CONSUMER_KEY,
        TWITTER_CONSUMER_SECRET)

    auth.set_access_token(
        TWITTER_ACCESS_TOKEN,
        TWITTER_ACCESS_SECRET)

    try:
        stream = Stream(auth, listener)
        stream.filter(track=['#something', ])
    except (KeyboardInterrupt, SystemExit):
        print 'Stopping Twitter Streaming Client'

if __name__ == '__main__':
    main()

rawDescription: Python, handle persistent http connection in web application


code:
import random

def interleave(*args):
  iters = map(iter, args)
  while iters:
    it = random.choice(iters)
    try:
      yield next(it)
    except StopIteration:
      iters.remove(it)

print list(interleave(xrange(1, 5), xrange(5, 10), xrange(10, 15)))

rawDescription: Interleaving multiple iterables randomly while preserving their order in python


code:
import subprocess
cmd="phantomjs --ignore-ssl-errors=yes rasterize.js input output"
proc = subprocess.Popen([cmd])
print "PID of running process::", proc.pid

rawDescription: Get process ID of Phantomjs through python


code:
from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    return getattr(app.control.inspect(), method)()

print inspect('scheduled')
print inspect('active')

rawDescription: Celery scheduled list returns None

description: Celery scheduled list returns None


code:
task = subprocess.Popen(["python", "test2.py"], stdout=subprocess.PIPE)
print task.communicate()

rawDescription: Redirecting subprocess stdout

description: Redirecting subprocess stdout


code:
def identify(event):
    item = w.find_closest(event.x, event.y)[0]
    tags = w.gettags(item)
    print tags

rawDescription: how to find tags near to mouse click in tkinter python gui

description: Find tags near to mouse click in tkinter python gui


code:
import urllib
params = [('name1','value1'), ('name2','value2 with spaces & stuff')]
pairs = urllib.urlencode(params)
fullurl = 'http://status.dummy.com/status.json' + '?' + pairs
print fullurl

rawDescription: passing multiple parameters for the url using pycurl

description: Passing multiple parameters for the url using pycurl


code:
import json

input_json = """
[
    {
        "type": "1",
        "name": "name 1"
    },
    {
        "type": "2",
        "name": "name 2"
    },
    {
        "type": "1",
        "name": "name 3"
    }
]"""

# Transform json input to python objects
input_dict = json.loads(input_json)

# Filter python objects with list comprehensions
output_dict = [x for x in input_dict if x['type'] == '1']

# Transform python object back into json
output_json = json.dumps(output_dict)

# Show json
print output_json

rawDescription: how to filter json array in python

description: Filter json array in python


code:
import threading
import sys, time
from PyQt4 import QtCore, QtGui
import psutil
import os

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.main_layout = QtGui.QVBoxLayout()

        ok_button = QtGui.QPushButton("Run")
        ok_button.clicked.connect(self.OK)
        self.main_layout.addWidget(ok_button)

        cancel_button = QtGui.QPushButton("Cancel")
        cancel_button.clicked.connect(self.cancel)
        self.main_layout.addWidget(cancel_button)

        central_widget = QtGui.QWidget()
        central_widget.setLayout(self.main_layout)
        self.setCentralWidget(central_widget)

    def myEvenListener(self,stop_event):
        state=True
        while state and not stop_event.isSet():
            for i in range(10,100):
                time.sleep(i*0.01)
                print '.'*i

    def OK(self):
        self.stop_event=threading.Event()
        self.c_thread=threading.Thread(target=self.myEvenListener, args=(self.stop_event,))
        self.c_thread.start()

    def cancel(self):
        self.stop_event.set()
        self.close()

def main():
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(480, 320)
    window.show()
    app.exec_()
main()

def kill_proc_tree(pid, including_parent=True):
    parent = psutil.Process(pid)
    if including_parent:
        parent.kill()

me = os.getpid()
kill_proc_tree(me)

rawDescription: Python PyQt: How Run While Loop Without Locking Main Dialog Window


code:
import random
import collections

def maxvote(nlabels):
  cnt = collections.defaultdict(int)
  for i in nlabels:
    cnt[i] += 1
  maxv = max(cnt.itervalues())
  return random.choice([k for k,v in cnt.iteritems() if v == maxv])

print maxvote([1,3,4,5,5,5,3,3,11])

rawDescription: How to optimize this Python code?

description: Optimize this Python code


code:
#!/usr/bin/env python
# src_dimensions.py
# requires mediainfo package
import subprocess, glob
globpattern = 'path/to/*.*'
cmd = ['mediainfo', '--Inform=Video;%Width%x%Height%\\n']
cmd.extend(glob.glob(globpattern))
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE)
outputlines = filter(lambda x:len(x)>0,(line.strip() for line in proc.stdout))
print outputlines

rawDescription: python subprocess store each line of output in a list

description: Python subprocess store each line of output in a list


code:
top = ET.fromstring(myXml)
for a in top.iter('a'):
  s = ET.tostring(a, method='text')
  print s

rawDescription: Python XML parser doesn't get all text

description: Python XML parser doesn't get all text


rawDescription: For loop with Regex not behaving as desired


code:
import re
print re.findall('(.(foo))', "Xfoo")   # [('Xfoo', 'foo')]
print re.findall('(.(?:foo))', "Xfoo") # ['Xfoo']

rawDescription: Python regex - Ignore parenthesis as indexing?


code:
new_subjects = request.POST.getlist('subjects')
print new_subjects

rawDescription: python dictionary question

description: Python dictionary question


code:
file2=set(open("file2").read().split())
for line in open("file1"):
    if line.split(":")[0].strip("'") in file2:
        print line

rawDescription: Python, loop through lines in a file; if line equals line in another file, return original line


code:
def use_string(wrap):
  print wrap.s           # Prints 'olle'
  wrap.s = None          # Resets the reference
  print wrap.s           # Prints 'None'

class wrapper(object):
  pass

a = wrapper()
a.s = "olle"
print a.s                # Prints 'olle'
use_string(a)
print a.s                # Prints 'None'

rawDescription: Python Free String Passed to Function

description: Python Free String Passed to Function


code:
import xml.etree.cElementTree as etree # or: from lxml import etree

body = etree.parse('test.html').find('body')

for aname in body.iterfind('*[@class="aname"]'):
    cname = aname.find('*[@class="bname"]//a[@class="cname"]')
    print cname.text # title
    print cname.get('href') # url

    for div in aname.iterfind('div[@class=""]/div'):
        strong = div.find('strong')
        if strong is not None:
            print strong.text # label
            text = div[0].tail.strip() # http://stackoverflow.com/a/9674097/4323
            if text:
                print text
            else:
                for box in div.iterfind('*[@class="box-span"]'):
                    print box.text

    print

rawDescription: How to find text in specific nested tag wih lxml and pyhthon?

description: Find text in specific nested tag wih lxml and pyhthon


code:
import csv
import gnupg
import os
import StringIO

gpg = gnupg.GPG(gnupghome="/home/chefgit/Desktop/.gnupg")
file = open("/home/chefgit/Desktop/csv/dest/file_100.gpg")
decrypt = gpg.decrypt_file(file)
csv_input = csv.reader(StringIO.StringIO(decrypt))

for row in csv_input:
    print row

rawDescription: How do I convert python crypt object to csv so I can call csv methods on the contents

description: Convert python crypt object to csv so I can call csv methods on the contents


code:
import random
unordered = list(range(10))
ordered = []
i = 0

random.shuffle(unordered)

print unordered
lowest = unordered[0]

while len(unordered) > 0:
    if  unordered[i] < lowest:
        lowest = unordered[i]
    i += 1
    if i == len(unordered):
        ordered.append(lowest)
        unordered.remove(lowest)
        if unordered:
          lowest = unordered[0]
        i = 0

print(ordered)

rawDescription: Manually sort a list of 10 integers in python


code:
import copy

class Base(dict):
    global_class_cache = {}

    def __init__(self, **data):
        self.local_data = data

    def __add__(self, other):
        new_instance = self._new_type((type(self), type(other)))()
        new_instance.update(copy.deepcopy(self).__dict__)
        new_instance.update(copy.deepcopy(other).__dict__)
        return new_instance

    def _new_type(self, parents):
        parents = tuple(parents)
        if parents not in Base.global_class_cache:
            name = '_'.join(cls.__name__ for cls in parents)
            Base.global_class_cache[parents] = type(name, parents, {})
        return Base.global_class_cache[parents]

class A(Base):
    def a(self):
        print "test a"

class B(Base):
    def b(self):
        print "test b"

if __name__ == '__main__':
    a = A(a=1, b=2)
    b = B(c=1)
    c = a + b
    c.b() # should work
    c.a() # should work
    print c.__class__.__name__

rawDescription: python: multiple inheritance and __add__() in base class

description: Python: multiple inheritance and __add__() in base class


code:
def stemming_text_1():
    with open('test.txt', 'r') as f:
        for line in f:
            print line
            singles = []

            stemmer = PorterStemmer() #problem from HERE
            for plural in line.split():
                singles.append(stemmer.stem(plural))
            print ' '.join(singles)

rawDescription: Python Stemming words in a File

description: Python Stemming words in a File


code:
import numpy as np
from scipy import sparse
A = np.array([[1,2,0],[0,0,3],[1,0,4]])
B = np.matrix([[1,2,0],[0,0,3],[1,0,4]])
A
sA = sparse.csr_matrix(A)   # Here's the initialization of the sparse matrix.
sB = sparse.csr_matrix(B)
sA
print sA

rawDescription: How to transform numpy.matrix or array to scipy sparse matrix

description: Transform numpy.matrix or array to scipy sparse matrix


code:
import random

rows = 3
cols = 3
virus = [0,0,0,0,0,1,2]
virus_level = random.choice(virus)
first_gen = []
for rownum in range(rows):
    row = []
    for colnum in range(cols):
        virus_level = random.choice(virus)
        row.append(virus_level)
    first_gen.append(row)

print first_gen  # [[2, 0, 0], [0, 0, 0], [1, 0, 0]]

rawDescription: Creating 2-D list in python using random numbers.


code:
l = [rel1, rel2, rel3, rel4]
if any(item > 0.05 for item in l) and sum(item > 0.01 for item in l) >= 2:
    print "Yay"

rawDescription: Elegant way of writing nested if statements (containing different conditions)


code:
# empty set of already seen ips:
seen_ips = set()

with open("access_log.txt") as f:
    for line in lines:
        arr = line.split(' ')
        date = arr[3]

        # if the ip still not seen, then print and add it to the seen_ips set:
        if arr[0] not in seen_ips:
            print arr[0], "- - ", date[1:], " ",arr[6]
            seen_ips.add(arr[0])
        # else (i.e. ip already seen) ignore and go on with the next line

rawDescription: Sorting a txt file with Python

description: Sorting a txt file with Python


code:
import multiprocessing
import threading
import time

def good_worker():
    print "[GoodWorker] Starting"
    time.sleep(4)
    print "[GoodWorker] all good"

def bad_worker():
    print "[BadWorker] Starting"
    time.sleep(2)
    raise Exception("ups!")

class MyProcManager(object):
    def __init__(self):
        self.procs = []
        self.errors_flag = False
        self._threads = []
        self._lock = threading.Lock()

    def terminate_all(self):
        with self._lock:
            for p in self.procs:
                if p.is_alive():
                    print "Terminating %s" % p
                    p.terminate()

    def launch_proc(self, func, args=(), kwargs= {}):
        t = threading.Thread(target=self._proc_thread_runner,
                             args=(func, args, kwargs))
        self._threads.append(t)
        t.start()

    def _proc_thread_runner(self, func, args, kwargs):
        p = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
        self.procs.append(p)
        p.start()
        while p.exitcode is None:
            p.join()
        if p.exitcode > 0:
            self.errors_flag = True
            self.terminate_all()

    def wait(self):
        for t in self._threads:
            t.join()

if __name__ == '__main__':
    proc_manager = MyProcManager()
    proc_manager.launch_proc(good_worker)
    proc_manager.launch_proc(good_worker)
    proc_manager.launch_proc(bad_worker)
    proc_manager.wait()
    if proc_manager.errors_flag:
        print "Errors flag is set: some process crashed"
    else:
        print "Everything closed cleanly"

rawDescription: Clean Python multiprocess termination dependant on an exit flag


code:
for i, j in itertools.product([1, 2, 3], [4, 5, 6]):
    print i, j

rawDescription: Automatically nested for loops in python


code:
from ctypes import *

Val_GetStatus= CDLL('x').Val_GetStatus
Val_GetStatus.argtypes = [c_int,POINTER(c_unit8)]
Val_GetStatus.restype = c_int

deviceStatus = ctypes.c_uint8()

print Val_GetStatus(0, byref(deviceStatus))
print deviceStatus.value

rawDescription: passing a pointer value to a C function from python

description: Passing a pointer value to a C function from python


code:
from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('file.ini')

config_dict = {}

for element in parser.sections():
    print 'Section:', element
    config_dict[element] = {}
    print '  Options:', parser.options(element)
    for name, value in parser.items(element):
        print '  %s = %s' % (name, value)
        config_dict[element][name] = value

print config_dict

rawDescription: using configparser in python

description: Using configparser in python


code:
import os
import sys
import fnmatch

if len(sys.argv) < 2:
    sys.exit('Usage: check_copyright.py <build directory>')

userstring1="Copyright (c) 2012 Company, Inc\nAll Rights Reserved.\nCompany Confidential and Proprietary."
userstring2="Copyright (c) 2011-2013 Company, Inc\nAll Rights Reserved.\nCompany Confidential and Proprietary."

for path,dirs,files in os.walk(sys.argv[1]):
    for fname in files:
        for userstring in [userstring1, userstring2]:
            test(userstring, path, dirs, fname)

def test(userstring, path, dirs, fname):
    userlines = userstring.split('\n')
    for pat in ['*.cpp','*.c','*.h']:
        if fnmatch.fnmatch(fname,pat):
            fullname = os.path.join(path,fname)
            with open(fullname) as file:
                match = 0
                for line in file:
                    if userlines[match] in line: # Check if the line at index `m` is in the user lines
                        match += 1 # Next time check the following line
                    elif match > 0: # If there was no match, reset the counter
                        match = 0
                    if match >= len(userlines): # If 3 consecutive lines match, then you found a match
                        break
                if match == len(userlines): # print if you found a match
                    print "MATCH"
                    print match
                    print "LENGTH userlines"
                    print len(userlines)
                    print fullname

rawDescription: looping over markings in files based on userstrings

description: Looping over markings in files based on userstrings


code:
class Foo(object):
  def __init__(self, val=2):
     self.val = val
  def __getstate__(self):
     print "I'm being pickled"
     self.val *= 2
     return self.__dict__
  def __setstate__(self, d):
     print "I'm being unpickled with these values:", d
     self.__dict__ = d
     self.val *= 3

import pickle
f = Foo()
f_string = pickle.dumps(f)
f_new = pickle.loads(f_string)

rawDescription: Simple example of use of __setstate__ and __getstate__


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("http://www.flipkart.com/mobiles/pr?   p%5B%5D=sort%3Dfeatured&sid=tyy%2C4io&ref=659eb948-c365-492c-99ef-59bd9f0427c6")
time.sleep(3)
for i in range(5):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # scroll to bottom of page
    time.sleep(2)
driver.find_element_by_xpath('//*[@id="show-more-results"]').click() # click load more  button, needs to be done until you reach the end.
elem=[]
elem=driver.find_elements_by_xpath('.//div[@class="pu-title fk-font-13"]')
for e in elem:
   print e.text

rawDescription: extracting more information from webdriver

description: Extracting more information from webdriver


code:
import re
import pyparsing

class BoolRegEx(object):

  def Match(self, tags=[], query=""):
    self.tags = tags
    if ' ' not in query:
      return self.Search(query)
    else:
      return pyparsing.operatorPrecedence(
        pyparsing.Word(pyparsing.printables, excludeChars="()"), [
          (pyparsing.Literal("NOT"), 1, pyparsing.opAssoc.RIGHT, self.Not),
          (pyparsing.Literal("OR"),  2, pyparsing.opAssoc.LEFT,  self.Or),
          (pyparsing.Literal("AND"), 2, pyparsing.opAssoc.LEFT,  self.And),
        ]
      ).parseString(query, parseAll=True)[0]

  def Search(self, a):
    try:
      regex = re.compile(a.replace("<<", "#~").replace(">>", "~#").replace(">", ")").replace("<", "(").replace("#~", "<").replace("~#", ">"))
      for tag in self.tags:
        match = regex.match(tag)
        if match and len(match.group(0)) == len(tag):
          return True
      return False
    except:
      raise

  def And(self, t):
    for a in t[0][0::2]:
      if isinstance(a, basestring):
        v = self.Search(a)
      else:
        v = bool(a)
      if not v:
        return False
    return True

  def Or(self, t):
    for a in t[0][0::2]:
      if isinstance(a, basestring):
        v = self.Search(a)
      else:
        v = bool(a)
      if v:
        return True
    return False

  def Not(self, t):
    a = t[0][1]
    if isinstance(a, basestring):
      return not self.Search(a)
    else:
      return not bool(a)

print BoolRegEx().Match(['client/chat', 'user_a', 'limited', 'extraspecial'], "client/.+ AND user_a AND NOT ( limited AND NOT ( .+<r|i>special OR godmode ) )")
# False

print BoolRegEx().Match(['client/chat', 'user_a', 'limited', 'superspecial'], "client/.+ AND user_a AND NOT ( limited AND NOT ( .+<r|i>special OR godmode ) )")
# True

rawDescription: In Python, how can I query a list of words to match a certain query criteria?


code:
class MyClass(object):
    def getData(self):
        print "I am not able to call after every interval"

def main():
    while "foo" != "bar":
        m = MyClass()
        Timer(2.0, m.getData()).start()

main()

rawDescription: Calling method after every interval using Python Timer


code:
print "a\n\tb"
print """a\n\tb"""

rawDescription: Multi-line Strings

description: Multi-line Strings


code:
import sys, inspect

def is_mod_function(mod, func):
    return inspect.isfunction(func) and inspect.getmodule(func) == mod

def list_functions(mod):
    return [func.__name__ for func in mod.__dict__.itervalues()
            if is_mod_function(mod, func)]

print 'functions in current module:\n', list_functions(sys.modules[__name__])
print 'functions in inspect module:\n', list_functions(inspect)

rawDescription: Find functions explicitly defined in a module (python)


code:
@as_declarative()
class Base(object):
    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()
    id = Column(Integer, primary_key=True)

class CompanyInterface(Base):
    discriminator = Column(String)

    __mapper_args__ = {"polymorphic_on": discriminator}

class Address(Base):
    street = Column(String)
    city = Column(String)
    zip = Column(String)
    company_id = Column(Integer, ForeignKey(CompanyInterface.id))
    company = relationship(CompanyInterface)

    def __repr__(self):
        return ("%s(street=%r, city=%r, zip=%r, company=%r)" %
                (self.__class__.__name__, self.street, self.city, self.zip, self.company))

class Customer(CompanyInterface):
    id = Column(Integer, ForeignKey(CompanyInterface.id), primary_key=True)
    name = Column(String)
    __mapper_args__ = {"polymorphic_identity": "Customer"}

class Supplier(CompanyInterface):
    id = Column(Integer, ForeignKey(CompanyInterface.id), primary_key=True)
    company_name = Column(String)
    __mapper_args__ = {"polymorphic_identity": "Supplier"}

engine = create_engine('sqlite:///test/test_me.db', echo=False)
Base.metadata.create_all(engine)

session = Session(engine)

address1 = Address(street='test-1', city="Detroit", zip="56785")
address2 = Address(street='test-2', city="Phoenix", zip="110322")
address3 = Address(street='test-3', city="Washington", zip="432414")

supl1 = Supplier(company_name="Supplier-1 TEST")
supl2 = Supplier(company_name="Supplier-2 TEST")
cust1 = Customer(name="Customer-1 TEST")
cust2 = Customer(name="Customer-2 TEST")

address1.company = supl1
address2.company = cust1
address3.company = cust1

session.add_all([address1, address2, address3])

session.commit()

address3.company = supl2

session.commit()

print "PRINTING, TOTAL = %s" % session.query(Address).count()
for address in session.query(Address):
    print "ADDRESS = %s" % address

rawDescription: SQLAlchemy polymorphic association

description: SQLAlchemy polymorphic association


code:
#!/usr/bin/env python

import re
s = 'aug 12, 2010 abc $123'

print re.findall(r'\$(\d+)', s)
# => ['123']

print re.search(r'\$(\d+)', s).group()
# => $123

print re.search(r'\$(\d+)', s).group(1)
# => 123

rawDescription: python re: r'\b \$ \d+ \b' won't match 'aug 12, 2010 abc $123'

description: Python re: r'\b \$ \d+ \b' won't match 'aug 12, 2010 abc $123'


code:
import Tkinter

def oval_mouse_click(event):
    print "in oval"
    event.widget.tag_click = True

def canvas_mouse_click(event):
    if event.widget.tag_click:
        event.widget.tag_click = False
        return
    print "in canvas"

root = Tkinter.Tk()
canvas = Tkinter.Canvas(width=400, height=300)
oid = canvas.create_oval(400/2-10, 300/2-10, 400/2+10, 300/2+10, fill='blue')
canvas.tag_click = False
canvas.tag_bind(oid, "<Button-1>", oval_mouse_click)
canvas.bind("<Button-1>" , canvas_mouse_click)
canvas.pack()
root.mainloop()

rawDescription: How can I stop raising event in Tkinter?

description: Stop raising event in Tkinter


code:
if rounds == 5:
    if playr_wins > computer_wins:
        console.set_color(0.00, 0.00, 1.00)
        print '\nYOU WON THE GAME'
        console.set_color(0.00, 0.00, 0.00)
        break
    elif computer_wins > playr_wins:
        console.set_color(1.00, 0.00, 0.00)
        print '\nYOU LOST THE GAME'
        console.set_color(0.00, 0.00, 0.00)
        break
    else:
       print "Wrong counts"

rawDescription: Looping in Guessing Game in Python

description: Looping in Guessing Game in Python


code: print c.intersection(a)[0].x

rawDescription: separate output element in Sympy

description: Separate output element in Sympy


code:
for c in range(0,int(count)):
    print newList[c*50:(c+1)*50]

rawDescription: How to fetch n no of elements from a list in a loop.?

description: Fetch n no of elements from a list in a loop.


code:
from random import randint

x = 1.0   # 1 euro
pct = .25 # 1/4

for i in range(1000): # 0-999 = 1000
    bet = x * pct
    if randint(0,1) == 1: # assume 0 = tails 1 = heads
        x += bet*2
    else:
        x -= bet

    if x > 1000000000.0:
        break

print i,x

rawDescription: Coin toss/Who wants to be a Billionaire

description: Coin toss/Who wants to be a Billionaire


code:
import imp
print imp.reload.__doc__

rawDescription: Run code from a Python module, modify module, then run again without exiting interpeter


code:
import amara.bindery
doc = amara.bindery.parse(
    '''<iq xmlns="jabber:client"
         to="__anonymous__admin@localhost/8978528613056092673206"
         from="conference.localhost" id="disco" type="result">
         <query xmlns="http://jabber.org/protocol/disco#items">
           <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
           <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
         </query>
       </iq>''')
for item in doc.iq.query.item:
  print item.jid, item.name

rawDescription: Extract items list from XML in python

description: Extract items list from XML in python


code:
import string

class Piece(object):
    def __init__(self, type, colour):
        self.type = type
        self.colour = colour
    def __str__(self):
        return 'Piece(%(type)s, %(colour)s)' % self.__dict__

class Board(dict):
    def __init__(self, width, height):
        self.update(
            dict(((x,y), None)
                for x in string.ascii_lowercase[:width] # alphabetic axis
                for y in xrange(1, height+1)            # numeric axis
            )
        )

    def setup(self, position_seq):
        for type, colour, position in position_seq:
            self[position] = Piece(type, colour)

initial_positions = [
    ('rook', 'W', ('a', 1)),
    ('knight', 'W', ('b', 1)),
    ('bishop', 'W', ('c', 1)),
    ('queen', 'W', ('d', 1)),
    ('king', 'W', ('e', 1)),
    ('pawn', 'W', ('a', 2)),
]

b = Board( 8,8 )
b.setup( initial_positions )
print b['a',1] # returns Piece(rook, W)

rawDescription: How to recursively create individually accessible objects

description: Recursively create individually accessible objects


code:
import sys
value = sys.argv[1]
print value

existing_dlls  = []
with open("final.txt", "r") as outputd:         #if this file does not exists initially make(an empty file) it else File not find Error will be thrown
    for line in outputd:
        if line.lower().find(".dll")> -1:
            existing_dlls.append(line.strip().lower())

dontwrite = False # Flag to keep track of address to be written

with open("final.txt", "a") as outputd:         #dont open this file for every write, rather keep it open for write operations
    with open(value) as inputd: # Parse file
        for line in inputd:                         #dont iterate again and again for similar checks, if statements are mutually exclusive
            if line.strip() == 'Imports':
                continue
            elif line.strip() == 'Exports':
                continue
            elif "none" not in line:
                if line.strip().lower().endswith(".dll"):
                    if line.strip().lower() in existing_dlls:
                        dontwrite = True
                    else :
                        dontwrite = False
                if not dontwrite:
                    outputd.write(line)

rawDescription: How to group text files based on content?

description: Group text files based on content


code:
p = subprocess.Popen("svn info svn://xx.xx.xx.xx/project/trunk | grep \"Revision\" | awk '{print $2}'", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Revision is", output

rawDescription: How to run SVN commands from a python script?

description: Run SVN commands from a python script


code:
theBoard = {'top-L': ' ',
    'top-M': ' ',
    'top-R': ' ',
    'mid-L': ' ',
    'mid-M': ' ',
    'mid-R': ' ',
    'low-L': ' ',
    'low-M': ' ',
    'low-R': ' '
}                                             # <--- Close your dictionary

                                              # <--- remove random string 'Check for c...'
def openMoves():                              # <--- add parenthesis to function
    for k, v in theBoard.items():             # <--- loop over the key, value pairs
        if v == ' ':
            print "the move %s is open" % k
        else:
            print "the move %s is taken" % k

openMoves()                                   # <-- remove the print statement

rawDescription: Looping through dictionary and getting keys


code:
from sqlalchemy.sql import expression, compiler
from sqlalchemy import schema, types
import csv

# example for mssql
from sqlalchemy.dialects.mssql import base
dialect = base.dialect()
compiler_cls = dialect.statement_compiler
class NonBindingSQLCompiler(compiler_cls):
    def _create_crud_bind_param(self, col, value, required=False):
        # Don't do what we're called; return a literal value rather than binding
        return self.render_literal_value(value, col.type)

recipe_table = schema.Table("recipe", schema.MetaData(), schema.Column("name", types.String(50), primary_key=True), schema.Column("culture", types.String(50)))

for row in [{"name": "fudge", "culture": "america"}]: # csv.DictReader(open("x.csv", "r")):
    insert = expression.insert(recipe_table, row, inline=True)
    c = NonBindingSQLCompiler(dialect, insert)
    c.compile()
    sql = str(c)
    print sql

rawDescription: how to generate various database dumps

description: Generate various database dumps


code:
import os
import json
import time
import shutil
import requests

def add_coordinates(source, dest):
    with open(source, 'r') as file_object:
        json_data = json.load(file_object)

    for item in json_data:
        address = item['address']['address']
        latitude, longitude = google_call(address)

        # add latitude and longitude as address properties
        item['address']['latitude'] = latitude
        item['address']['longitude'] = longitude

        # save updated json_data
        with open(dest, 'w') as file_object:
            json.dump(json_data, file_object)

        time.sleep(10)

def google_call(address):
    api_key = "XXXXXXXXXXXXXXXXXXXXX"
    api_response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
    api_response_dict = api_response.json()

    latitude = None
    longitude = None

    if api_response_dict['status'] == 'OK':
        latitude = api_response_dict['results'][0]['geometry']['location']['lat']
        longitude = api_response_dict['results'][0]['geometry']['location']['lng']

        print 'Latitude:', latitude
        print 'Longitude:', longitude

    return latitude, longitude

if __name__ == '__main__':
    source = "/home/nishant/Documents/Python/Gnitin/testDehil.json"
    dest = "/home/nishant/Documents/Python/Gnitin/testDehil_clean.json"
    shutil.copyfile(source, dest)
    add_coordinates(source, dest)

rawDescription: How to write content back to a json file using python?

description: Write content back to a json file using python


code:
from xml.etree import ElementTree as etree

xml_content = """<eo-gateway>
    <interface-code>AAA</interface-code>
    <supplier-code>XXX</supplier-code>
    <authors>
    <author type="first">
    <forename>John</forename>
    <surname>Smith</surname>
    </author>
    </authors>
    </eo-gateway>"""

document_root = etree.ElementTree(etree.fromstring(xml_content))
for element in document_root.getiterator():
    element.tag = "art:" + element.tag
print etree.tostring(document_root.getroot())

rawDescription: How to Add Prefix in XML in Python

description: Add Prefix in XML in Python


code:
class example:
    def GET(self):
        print web.ctx.ip

rawDescription: Extracting IP from request in Python

description: Extracting IP from request in Python


code:
from nltk.corpora import wordnet as wn

for synset in list(wn.all_synsets('n')):
    print synset

# Or, equivalently
for synset in list(wn.all_synsets(wn.NOUN)):
    print synset

rawDescription: Getting a Large List of Nouns (or Adjectives) in Python with NLTK; or Python Mad Libs


code:
from gi.repository import Gtk

class CursorSample(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self, application_id="org.app.CursorSample")

        self.buffer = Gtk.TextBuffer()
        self.buffer.connect("notify::cursor-position",
                            self.on_cursor_position_changed)

        self.tw = Gtk.TextView()
        self.tw.set_buffer(self.buffer)
        self.tw.props.wrap_mode = Gtk.WrapMode.CHAR

    def do_activate(self):
        main_window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        main_window.add(self.tw)
        self.add_window(main_window)
        main_window.set_position(Gtk.WindowPosition.CENTER)
        main_window.show_all()

    def on_cursor_position_changed(self, buffer, data=None):
        print buffer.props.cursor_position

if __name__ == "__main__":
    cursorsample = CursorSample()
    cursorsample.run(None)

rawDescription: Cursor position changed in textview

description: Cursor position changed in textview


code:
def start(event):
    ax.axvspan(100, 200, facecolor='0.5', alpha=0.5)
    print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
        event.button, event.x, event.y, event.xdata, event.ydata)
    fig.canvas.draw()

rawDescription: Accessing a matplotlib plot within a def


code:
class Sudoku:
    def __init__(self):
        grid=[]
        for i in xrange(9):
            grid.append([])

        print("Enter elements, values 0 (blank) to 9:")
        for i in xrange(9):
            for x in raw_input().split():
                grid[i].append(int(x))

        self.grid = grid
        print self.grid

    def check_row(self, row, num):
        for col in xrange(9):
            if self.grid[row][col] == num:
                return False
        return True

    def check_col(self, col, num):
        for row in xrange(9):
            if self.grid[row][col] == num:
                return False
        return True

    def check_grid(self, row, col, num):
        row = (row / 3) * 3
        col = (col / 3) * 3

        for r in xrange(3):
            for c in xrange(3):
                if self.grid[row + r][col + c] == num:
                    return False

        return True

    def check(self, row, col, num):
        return (self.check_row(row, num)
            and self.check_col(col, num)
            and self.check_grid(row, col, num));

    def navigate(self, row, col):
        if col < 8:
            self.solve(row, col + 1)
        else:
            self.solve(row + 1, 0)

    def solve(self, row, col):
        if row > 8:
            self.display()
        else:
            if self.grid[row][col]:
                self.navigate(row, col)
            else:
                for num in xrange(1, 10):
                    if self.check(row, col, num):
                        self.grid[row][col] = num
                        self.navigate(row, col)
                        self.grid[row][col] = 0

    def display(self):
        print "solution"
        for i in xrange(9):
            for j in xrange(9):
                print self.grid[i][j],
            print
        print

sudoku = Sudoku()
sudoku.solve(0, 0)

rawDescription: backtracking Suduko solving, working in C, error in Python

description: Backtracking Suduko solving, working in C, error in Python


code:
import numpy as np, numpy.random as nprand, time, bisect

bigN = 5e6
smallN = 1000
maxn = 1e7
nprand.seed(1)
bigArr = nprand.randint(0, maxn, size=bigN)
smallArr = nprand.randint(0, maxn, size=smallN)

# brute force
t1 = time.time()
for i in range(len(smallArr)):
    inds = np.where(bigArr == smallArr[i])[0]
t2 = time.time()
print "Brute", t2-t1

# not brute force (like nested loop with index scan)
t1 = time.time()
sortedind = np.argsort(bigArr)
sortedbigArr = bigArr[sortedind]
for i in range(len(smallArr)):
    i1 = bisect.bisect_left(sortedbigArr, smallArr[i])
    i2 = bisect.bisect_right(sortedbigArr, smallArr[i])
    inds = sortedind[i1:i2]
t2=time.time()
print "Non-brute", t2-t1

rawDescription: Numpy Array: Efficiently find matching indices