code:
for k,v in shortcodes.iteritems():
    for x in v:
        index = description.find('{'+k+'}')
        description[index:index+len('{'+k+'}')] = description[index:index+len('{'+k+'}')].replace('{'+k+'}', x)
print description

rawDescription: Python not returning specific list item by variable


code:
import re

for line in open(filename, 'r'):
    matchPattern = "^(\d{5})$"
    match = re.match(matchPattern, line, flags=0)
    print match.group(0) #the whole match

rawDescription: Finding Zip number from String

description: Finding Zip number from String


code:
fd = open(dFile)
lineas = fd.readlines()
fd.close()

for i in xrange(0, len(lineas), 6):
    print "CA:"+lineas[i]+"Q:"+lineas[i+1]+"A1:"+lineas[i+2]+"A2:"+lineas[i+3]+"A3:"+lineas[i+4]+"A4:"+lineas[i+5];

rawDescription: How can I increase the i in a for?

description: Increase the i in a for


code:
#!/usr/bin/env python
#http://stackoverflow.com/questions/14406562/prevent-running-concurrent-instances-of-a-python-script

import sys
import time
import fcntl

file_handle = None

def file_is_locked(file_path):
    global file_handle
    file_handle= open(file_path, 'w')
    try:
        fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
        return False
    except IOError:
        return True

file_path = '/var/lock/test.py'

if file_is_locked(file_path):
    print 'another instance is running exiting now'
    sys.exit(0)
else:
    print 'no other instance is running'
    for i in range(5):
        time.sleep(1)
        print i + 1

rawDescription: Prevent running concurrent instances of a python script


code:
import os
import sys

dir_arg=sys.argv[1]
num_arg=int(sys.argv[2])  #need to convert second argument to a number

dirs=os.listdir(dir_arg)

print dirs[:num_arg]

rawDescription: Python script command line input

description: Python script command line input


code:
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))

for row in df.itertuples():
    df.set_value(row.Index,'test',row.D)

print df.head()

print df.head()

rawDescription: How to update dataframe value

description: Update dataframe value


code:
user_choice = raw_input('Please click ENTER button to close application')
if not user_choice:
    print "ABORTED"
    quit()

rawDescription: Selenium: Quit Python script without closing browser


code:
import networkx as nx
G=nx.path_graph(4)
s='\n'.join(nx.generate_graphml(G))
print s

rawDescription: with Networkx, how to write graphml and other format into a string, instead of a file?

description: With Networkx, how to write graphml and other format into a string, instead of a file


code:
inputFileName = "sample.txt"

import itertools
import linecache

def sliding_window(window_size, step_size, lastwindow_start):
    for i in xrange(0, lastwindow_start, step_size):
        yield (i, i + window_size)
    yield (lastwindow_start, total_pi_digits)

def PiCrop(window_size, step_size):

    f = open(inputFileName, 'r')

    first_line = f.readline().split()

    total_pi_digits = int(first_line[0])

    lastwindow_start = total_pi_digits-(total_pi_digits%window_size)

    lastcounter = (total_pi_digits//window_size)*(window_size/step_size)

    flags = [False for i in range(lastcounter)]

    first_line[0] = str(window_size)
    second_line = f.readline().split()
    offset = int(round(float(second_line[0].strip('\n'))))
    first_line = " ".join(first_line)

    f. close()

    with open(inputFileName, 'r') as f:
        header = f.readline()
        data = [line.strip().split(',') for line in f.readlines()]

        for counter, window in enumerate(sliding_window(window_size,step_size,lastwindow_start)):
            chunk = data[window[0]:window[1]]

            print window

            with open('PiCrop_{}.txt'.format(counter), 'w') as output:

                if (flags[counter] == False):
                    flags[counter] = True

                    headerline = float(linecache.getline(inputFileName, window[1]+1)) - offset
                    output.write(str(window_size) + " " + str("{0:.4f}".format(headerline)) + " " + 'L' + '\n')

                for item in chunk:
                    newline = str("{0:.4f}".format(float(str(item).translate(None, "[]'"))-offset))
                    output.write(str(newline) + '\n')

rawDescription: How to write a sliding window algorithm that accounts for a step_size of 0?

description: Write a sliding window algorithm that accounts for a step_size of 0


code:
lower_valid_temp, upper_valid_temp = valid['Temp']
if lower_valid_temp <= Temp <= upper_valid_temp:
    print "..."

rawDescription: Checking the range from dictionary values


code:
with open("/Users/[username]/Downloads/email_original.csv",'rb')  as file:
    with open("/Users/[username]/Downloads/email_hashed.csv",'w')  as output:
        for line in file:
           line=line.strip()
           print hashlib.md5(line).hexdigest()
           output.write(hashlib.md5(line).hexdigest() +'\n')

rawDescription: MD5 Hashing a CSV with Python

description: MD5 Hashing a CSV with Python


code:
print "Hello World!"
end_command = raw_input("Press any key to exit...")

rawDescription: Can't open .exe generated after converting .py to .exe with py2exe


code:
import wmi, time
ip = 'xx.xx.xx.xxx'
username = "administrator"
password = "CLARiiON!"
SW_SHOWNORMAL = 1
from socket import *
print "Establishing connection to %s" %ip
c = wmi.WMI(ip, user=username, password=password)
process_startup = c.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = c.Win32_Process.Create(CommandLine="C:\User\Administrator\Desktop\runIOX_auto.bat",ProcessStartupInformation=process_startup)
if result == 0:
  print "Process started successfully: %d" % process_id
else:
  raise RuntimeError, "Problem creating process: %d" % result

rawDescription: Python: Using wmi to start executable remotely


code:
x=re.findall(r"\bcourse: (\w+)(.*?)(?=(?:\bcourse:|$))",x,flags=re.DOTALL)

print [[i[0]]+re.findall(r"TestName: (\w+)(?:(?!\bTestName\b).)*MyName\s*(\d+)",i[1],flags=re.DOTALL) for i in x]

rawDescription: Python: regex findall for subcategories?

description: Python: regex findall for subcategories


code:
import pandas as pd

fp = '/Users/USERNAME/Development/file.csv'

usecols = ['VIN', 'Reg City','First Name','Last Name','MGVW','Nat Flt Ind','MGVW',
                                'Reg Name','Phone', 'Unnamed: 8','ZIP','VC','VType',
                                'Reg Voc', 'Make','Veh Model', 'E Mfr','Engine Model',
                                'CY2010', 'CY2011', 'CY2012', 'CY2013', 'CY2014', 'CY2015',
                                'Std Cnt']

df = pd.read_csv(fp, usecols=usecols, sep=',')

print df.head()

rawDescription: Python Pandas not reading DataFrame

description: Python Pandas not reading DataFrame


code:
def makeExpr(ch):
    expr = Word(ch).setParseAction(lambda tokens: [ch,len(tokens[0])])
    return expr

expr = Each([Optional(makeExpr(ch)) for ch in "abc"])

for t in tests:
    print t,expr.parseString(t).asList()

rawDescription: Pyparsing - where order of tokens in unpredictable


code:
from pybrain.structure import FeedForwardNetwork
n = FeedForwardNetwork()
from pybrain.structure import LinearLayer, SigmoidLayer
inLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outLayer = SigmoidLayer(1)

n.addInputModule(inLayer)
n.addModule(hiddenLayer)
n.addOutputModule(outLayer)

from pybrain.structure import FullConnection
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
in_to_out = FullConnection(inLayer, outLayer)

n.addConnection(in_to_hidden)
n.addConnection(hidden_to_out)
n.addConnection(in_to_out)

n.sortModules()

print n

rawDescription: cascade-forward neural network

description: Cascade-forward neural network


code:
from xml.etree import ElementTree as et

raw = '''xml string as posted in the question'''
root = et.fromstring('<root>'+raw+'</root>')

target_elements = root.findall('.//body/*')

result = [t.tag for t in target_elements]
print result
# output :
# ['c', 'winforms', 'type-conversion', 'opacity', 'css', 'css3', 'internet-explorer-7', 'c', 'code-generation', 'j', 'visualj']

rawDescription: Getting all nested children within xml tag in python


code:
import sys
print sys.stdin

rawDescription: Pass output of a script to another script using sys.argv


code:
INPUT_txt = raw_input("Input File(s): ")
element = [ss for ss in (s.strip() for s in INPUT_txt.split(",")) if ss]
for filename in element:
    print os.path.abspath(filename)
    print os.path.isfile(filename)

rawDescription: How to import a list of file in Python using raw_input

description: Import a list of file in Python using raw_input


code:
import json

resp = '{"Info": "8\u6298", "Name": "\u5bb6\u4e50\u798f"}'
print json.loads(resp)

## {u'Info': u'8\u6298', u'Name': u'\u5bb6\u4e50\u798f'}

rawDescription: Unicode in python

description: Unicode in python


rawDescription: can't get correct python regex with this string that contains unicode

description: Can't get correct python regex with this string that contains unicode


code:
def foo():
    yield 1
    yield 2
    yield 3

s = set(foo())
l = list(foo())
results1 = [i*2 for i in foo()]
results2 = (i*2 for i in foo())
for r in foo():
    print r

rawDescription: Why are collections not handled uniformly in Python?


code:
import re
my_str = '123hello 456world'
output = re.sub(r'\d+', '', my_str)
print output  # 'hello world'

rawDescription: Strip Numbers From String in Python

description: Strip Numbers From String in Python


rawDescription: Python: fsolve near asymptotic region

description: Python: fsolve near asymptotic region


code:
#!/usr/bin/env python
from PyQt4.QtCore import pyqtSlot, QObject, QSignalMapper, QTimer
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView

class bookie(QObject):
    def __init__(self, parent=None):
        super(bookie, self).__init__(parent)

        self.count  = 0
        self.url    = 'http://some website.com/search?param={0}'
        self.params = range(100) # instead of [1,2,3,4,5,6,7,8,9,10,11,12] and so on...

        Grabber = QWebView # This would be your Grabber class

        self.mapper = QSignalMapper(self)
        self.mapper.mapped.connect(self.on_mapper_mapped)

        for grabberNumber in range(10): # Create 10 Grabber instances
            grabber = Grabber()
            grabber.loadFinished.connect(self.mapper.map)

            self.mapper.setMapping(grabber, grabberNumber)

            grabber.loadFinished.emit(True) # Initialize the grabber by emitting loadFinished

    def on_mapper_mapped(self, gNumber):
        self.count += 1
        if self.count < len(self.params):
            gParam  = self.params[self.count]
            grabber = self.mapper.mapping(gNumber)
            #grabber.load(QUrl(self.url.format(gParam)))

            # Next 2 lines for testing purposes, remove & uncomment the previous line

            print "GRABBER:", gNumber, "PARAMETER:", gParam
            QTimer.singleShot(1, lambda:grabber.loadFinished.emit(True))

if __name__ == "__main__":
    import  sys

    app = QApplication(sys.argv)
    main = bookie()

rawDescription: multiple threads of QWebView in PyQt4

description: Multiple threads of QWebView in PyQt4


code:
#settings.py
var = 'this is global'

# a.py
import settings
import b
b.foo()

# b.py
import settings
def foo():
    print settings.var

rawDescription: How to share globals between imported modules?

description: Share globals between imported modules


code:
import boto

s3 = boto.connect_s3()
bucket = s3.lookup('example-bucket')
for key in bucket.list(prefix='photos/2006/', delimiter='/'):
    print k.name

rawDescription: How i use amazon s3 in python to use REST examples


code:
f = open("test.jpg", "rb")
ima = f.read(16)

print "%s" % (binascii.hexlify(ima))

rawDescription: Why can I not read more than 16 bytes of a JPEG file in Python?


code:
import subprocess, os, sys

# Unbuffer output
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

tee = subprocess.Popen(["tee", "log.txt"], stdin=subprocess.PIPE)
os.dup2(tee.stdin.fileno(), sys.stdout.fileno())
os.dup2(tee.stdin.fileno(), sys.stderr.fileno())

print "\nstdout"
print >>sys.stderr, "stderr"
os.spawnve("P_WAIT", "/bin/ls", ["/bin/ls"], {})
os.execve("/bin/ls", ["/bin/ls"], os.environ)

rawDescription: How do I duplicate sys.stdout to a log file in python?

description: Duplicate sys.stdout to a log file in python


code:
class MyDict(dict):
  def __init__(self, *args, **kwargs):
    super(MyDict, self).__init__(*args, **kwargs)
    self['name'] = 'james'
d = MyDict()
print d['name']

rawDescription: How do I properly subclass a "dict"?

description: Properly subclass a "dict"


code:
def function1():
    print 'function1'

def function2():
    print 'function2'

functions = [function1, function2]

for function in functions:
    function()

rawDescription: How do I iterate through a series of functions in Django?

description: Iterate through a series of functions in Django


code:
from music21 import *
s = converter.parse(filepath)
# assuming key changes are the same in all parts, just get the first part
p = s.parts[0]
pFlat = p.flat
keySigs = pFlat.getElementsByClass('KeySignature')
for k in keySigs:
    print k.measureNumber

rawDescription: Extracting measure number of key change in MusicXML


code:
def divisors(n):
     i = 1
     a = []
     while (i<=n):
         if n % i == 0:
             p = a.append(i)
             print p
         i += 1
     return a

rawDescription: How can i make self updating list in python?

description: Make self updating list in python


code:
result = SDSS.query_crossid(co, photoobj_fields=['modelMag_g', 'modelMag_i'])
print result

rawDescription: How to use Astropy/Astroquery to get Sloan magnitudes

description: Use Astropy/Astroquery to get Sloan magnitudes


code:
import os
import pandas as pd
import glob
os.chdir("E:/") # change this to the directory where your csv files are stored
csv_files = {} # we store the dataframes in a dictionary
for file in glob.glob("*.csv"):
    csv_files[file] = pd.read_csv(file)

for dataframe in csv_files.values():
    print dataframe

rawDescription: Reading different tables from a directory into an array of separate dataframes


code:
tree = (5, (8, (12, None, None), (2, None, None)),(9, (7, (1, None, None), None), (4, (3, None, None), None)))

def tree_depth(node):

    if not isinstance(node, tuple):
        return 1
    else:
        return max(tree_depth(subnode) for subnode in node) + 1

print tree_depth(tree)

rawDescription: recursively parse all of the levels of tree

description: Recursively parse all of the levels of tree


code:
from operator import itemgetter
from itertools import groupby
full_list = [0,1,2,3,10,11,12,59]
cont = [map(itemgetter(1), g) for k, g in groupby(enumerate(full_list), lambda (i,x):i-x)]
# [[0, 1, 2, 3], [10, 11, 12], [59]]

rawDescription: python return lists of continuous integers from list

description: Python return lists of continuous integers from list


code:
class foo:
    id = 1
    def __init__(self):
        self.attr = 10

class bar:
    id = 2
    def __init__(self):
        self.attr = 20

def create_foo():
    return foo()

def create_bar():
    return bar()

class_dict = {1:create_foo,2:create_bar}

class_index = [1,2,1,2,1,1,1,2,2,2,1] #data read from file

class_list = [] #output list containing the newly instanciated bar or foo

for index in class_index:
    c = class_dict[index]()
    class_list.append(c)

for classe in class_list:
    print str(classe.attr),

print "\n-------------"
class_list[0].attr = 15

for classe in class_list:
    print str(classe.attr),

rawDescription: Read from file and instanciate new class based on file input


code:
def main(): #global
    n = [1]
    z = None

    def addone():  #local
        if not z:
            n[0] += 1
    addone()
    print n[0]

main()

rawDescription: Accessing global function's variables in a local function, Python2


code:
def square_root():
    sqrt = int(raw_input("What number do you want the square root for? ")) ** 0.5
    if sqrt == int(sqrt):  # i.e. 'sqrt' is an integer
        print "Result is", int(sqrt)
    else:
        print "That is not a perfect square!"

rawDescription: Prevent certain numbers from printing

description: Prevent certain numbers from printing


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

data = {'memfn':'hikaru', 'memln':'nakamura', 'mode':'Search'}
url = r'http://www.uschess.org/msa/thin2.php'
s = urllib.urlopen(url, bytes(urllib.urlencode(data)))
print s.read()

rawDescription: Retrieving data via POST request

description: Retrieving data via POST request


rawDescription: returning different time frames from datetime

description: Returning different time frames from datetime


code:
from threading import Thread
import hashlib

def async(gen):
    def func(*args, **kwargs):
        it = gen(*args, **kwargs)
        result = it.next()
        Thread(target=lambda: list(it)).start()
        return result
    return func

@async
def test(text):
    # synchronous part (empty in this example)
    yield # Use "yield value" if you need to return meaningful value
    # asynchronous part[s]
    digest = hashlib.md5(text).hexdigest()
    print digest

rawDescription: Easy Python ASync. Precompiler?

description: Easy Python ASync. Precompiler


code:
def RungeK():
    # ...
    # a lot of code which create the vectors A and B and calculates
    # one Kunge-Kutta step for them
    # ...

    pool = Pool(processes=2)
    n = 20                         # Just something for the example
    A = np.arange(50000)
    B = np.ones_like(A)

    for i in xrange(n):  # loop over the time steps
        A *= np.mean(B)*B - A
        B *= np.sqrt(A)
        results = pool.map(Splitter, [(A, 3), (B, 2)])
        A = results[0]
        B = results[1]
    pool.close()
    print np.mean(A)  # Some output
    print np.max(B)

rawDescription: How to use python multiprocessing Pool.map within loop

description: Use python multiprocessing Pool.map within loop


code:
x = set([1, 2, 3])
y = x

y |= set([4, 5, 6])
print x
print y

rawDescription: Are Python sets mutable?

description: Are Python sets mutable


code:
#!/usr/bin/env python

import urllib2
import threading

class MyHandler(urllib2.HTTPHandler):
    def http_response(self, req, response):
        print "url: %s" % (response.geturl(),)
        print "info: %s" % (response.info(),)
        for l in response:
            print l
        return response

o = urllib2.build_opener(MyHandler())
t = threading.Thread(target=o.open, args=('http://www.google.com/',))
t.start()
print "I'm asynchronous!"

rawDescription: Using urllib2.urlopen() to load a URL without waiting for a response


code:
import time, resource, os

import requests
from awsauth import S3Auth

with open(os.path.expanduser("~/.boto")) as f:
  lines = f.read().splitlines()
ACCESS_KEY = lines[1].split(' = ')[1]
SECRET_KEY = lines[2].split(' = ')[1]

for i in range(20):
  print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
  url = 'http://{}.s3.amazonaws.com/{}'.format(
    'jca-screenshots-' + ('thumbs' if i % 2 == 0 else 'normal'), 'test.png')
  with open('test.png', 'rb') as f:
    resp = requests.put(url, data=f, auth=S3Auth(ACCESS_KEY, SECRET_KEY))
  print 'resp:', resp
  time.sleep(5)

rawDescription: boto set_contents_from_filename memory leak

description: Boto set_contents_from_filename memory leak


code:
"""
This function evaluates the guess to see if it'll solve the problem.
It returns the balance so that you can use it.
"""
def evaluatePayment(balance, guess, annualInterestRate):
    annualInterestRate = annualInterestRate
    balance = balance
    minPay = guess
    for month in range(1,13):
        remainingBalance = balance - minPay
        balance = (remainingBalance + remainingBalance * annualInterestRate / 12.0)
        #print "month: " + str(month) + " balance: " + str(balance) + " minPay: " + str(minPay)
    if balance <= 0:
        return balance
    else:
        return balance

#Defines the variables needed for this to work
annualInterestRate = 0.18
balance = 999999
monthInt = annualInterestRate / 12.0
upper= evaluatePayment(balance, 0, annualInterestRate)/12
lower = balance / 12.0
guess = (lower+upper)/2

"""
This is where the "guessing" takes part. It will iterate untill it finds a solution.
It takes the original guess and tries it, if it returns false it will check if the
guess was to high or to low and try assign new values depending on the elif-statement.
"""
while True:
    if (evaluatePayment(balance, guess, annualInterestRate)) <= 0.00 and (evaluatePayment(balance, guess, annualInterestRate)) > -0.01:
        print "Lowest Payment: " + str(round(guess,2)) + " balance: "
        break
    elif (evaluatePayment(balance, guess, annualInterestRate)) > 0.00:
        lower = guess
        upper = upper
        guess = (lower+upper)/2
        #print "high " + str(guess)

    elif (evaluatePayment(balance, guess, annualInterestRate)) < 0.00:
        upper = guess
        lower = lower
        guess = (lower+upper)/2
        #print "low " + str(guess)

rawDescription: Infinite loop in my code and I don't understand where it comes from


code: print { k:v for k, v in d.items() if v['end qty'] == 0 }

rawDescription: Splitting a nested dictionary based on value of nested key


code:
import os
from subprocess import Popen, PIPE
def strip_comments(text):
    p = Popen(['gcc', '-fpreprocessed', '-dD', '-E', '-x', 'c++', '-'],
            stdin=PIPE, stdout=PIPE, stderr=PIPE)
    p.stdin.write(text)
    p.stdin.close()
    return_code = p.wait()
    print p.stdout.read()

rawDescription: piping a string to gcc with python's subprocess.Popen

description: Piping a string to gcc with python's subprocess.Popen


code:
print html_diff.make_table(previous_contents.split('\n'),
                           fetch_url.page_contents.split('\n'))

rawDescription: Using the difflib.HtmlDiff class - showing single chars


code:
with open(sys.argv[1], 'r') as f:
  for _ in xrange(0, 7, 1):
    print f.next().rstrip()

rawDescription: xrange not adhering to start, stop, step

description: Xrange not adhering to start, stop, step


code:
import os
import sys
import matplotlib
import matplotlib.pyplot as plt
import StringIO
import urllib, base64

plt.plot(range(10, 20))
fig = plt.gcf()

imgdata = StringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0)  # rewind the data

print "Content-type: image/png\n"
uri = 'data:image/png;base64,' + urllib.quote(base64.b64encode(imgdata.buf))
print '<img src = "%s"/>' % uri

rawDescription: matplot - store image in variable

description: Matplot - store image in variable


code:
import json
j = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print j['two']

rawDescription: Parse JSON in Python

description: Parse JSON in Python


code:
import lxml.etree

example_doc = """
  <root>

    text
    <sibling/>
    <sibling/>

  </root>
"""
root = lxml.etree.XML(example_doc)

new_root = lxml.etree.Element("root")
it = iter(root)
for e in it:
  if e.tag == "a" and e.get("name") == "listofplaces":
    new_root.text = e.tail
    break
else:
  assert False, "TODO: handle tag not found"
for e in it:
  if e.tag == "a" and e.get("name") == "transport":
    break
  new_root.append(e)
else:
  assert False, "TODO: handle tag not found"

print lxml.etree.tostring(new_root)

rawDescription: Is the content between anchor tags (a) in html seen as a branch in lxml?


code:
import urllib2
u = urllib2.urlopen("http://www.avcodes.co.uk/airllistres.asp", "statuslst=Y&iataairllst=&icaoairllst=B&B1=Submit")
print u.read()

rawDescription: scraping data from simple website - change 'post' to 'get'

description: Scraping data from simple website - change 'post' to 'get'


code:
from datetime import date, timedelta

def allsundays(year):
   d = date(year, 1, 1)                    # January 1st
   d += timedelta(days = 6 - d.weekday())  # First Sunday
   while d.year == year:
      yield d
      d += timedelta(days = 7)

for d in allsundays(2010):
   print d

rawDescription: How can I select all of the Sundays for a year using Python?

description: Select all of the Sundays for a year using Python


rawDescription: Python list to list of lists

description: Python list to list of lists


code:
#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT
import pty
import os

cmd = 'socat -d -d PTY: PTY:'

master, slave = pty.openpty()

p = Popen(cmd, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True)
stdout = os.fdopen(master)
print stdout.readline()
print stdout.readline()

rawDescription: Python Run a daemon sub-process & read stdout


code:
import re
from nltk import word_tokenize
sents = """The price of it is $5.00.
The price of it is RM5.00.
The price of it is €5.00.""".split('\n')
for sent in sents:
    numbers_in_sent = re.findall("[-+]?\d+[\.]?\d*", sent)
    for num in numbers_in_sent:
            sent = sent.replace(num, ' '+num+' ')
    print word_tokenize(sent)

rawDescription: How to tokenize all currency symbols using Regex in python?

description: Tokenize all currency symbols using Regex in python


code:
import socket
from multiprocessing import Process, Queue, Value
from ctypes import c_bool

HOST = '198.51.100.0'
PORT = 8080

# This function will be run in a child process
def update_proc(data_queue, update_queue, quit_flag):
    while not quit_flag.value:
        data = data_queue.get()
        # do something with the data...
        update_queue.put(data)
    print "Closing child update process"

# This function will be run in a child process
def activate_proc(update_queue, quit_flag):
    while not quit_flag.value:
        data = update_queue.get()
        # do something with the data...
    print "Closing child activate process"

# main process begins execution here, if module is run from the terminal
if __name__ == "__main__":
    # Connect to remote host over TCP
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((HOST,PORT))

    # Set up a Queue to pass data to the update process, and another one
    # for the two children to communicate
    data_queue = Queue()
    update_queue = Queue()

    # The quit_flag Value is a *very* primitive way to signal the child
    # processes to quit. I'm sure there are better ways to do this, but I'm
    # tired and can't think of any right now.
    quit_flag = Value(c_bool, False)

    # Create two child processes, pass a reference to the Queue to each
    update = Process(target=update_proc, args=(data_queue, update_queue, quit_flag))
    activate = Process(target=activate_proc, args=(update_queue, quit_flag))

    update.start()
    activate.start()

    # Read data from the TCP socket, push it onto the data_queue
    while True:
        client.sendall("loc\n")
        data = client.recv(8192)
        if not data:
            print "network connection closed by client"
            break
        data_queue.put(data)

    # Join with child processes before closing
    print "All done, closing child processes"
    update.join()
    activate.join()

rawDescription: Running two threads which are dependent on each other in a loop


code:
class Test(db.Model):
  link = db.LinkProperty()

t = Test()
t.link = 'http://google.com'

print str(t.link)

rawDescription: Google App Engine: LinkProperty to string


code:
def func():
    a = 5
    print 'done'
    return a

# version 1: if print doesn't have to be outside of the function
def main():
    temp = raw_input('')
    if temp == '':
        local_a = func()
    else:
        # use else to avoid UnboundLocalError: local variable 'a' referenced
        # before assignment
        local_a = None
    print local_a

if __name__ == "__main__":
    main()

# # version 2: if print have to be outside of the function, then I can only
# # think of using global variable, but it's bad.
# global_a = None
# def main():
#     temp = raw_input('')
#     if temp == '':
#         global global_a
#         global_a = func()

# if __name__ == "__main__":
#     main()
#     print global_a

rawDescription: Python: Print a function which returned a value without running the function


code:
from django import template
from django.utils.safestring import mark_safe

register = template.Library()
@register.filter(name='formattedtext')

def formattedtext(value):
    try:
        scoringTemplate = "" + value + ""
        print scoringTemplate #return string with "text"
        return mark_safe(scoringTemplate)   # unescaped, raw html
    except ValueError:
        return value
    except:
        return value

rawDescription: Automatic addslashes while returing my own template tag in Django


code:
from Spotfire.Dxp.Application import DocumentMetadata
dmd = Application.DocumentMetadata #Get MetaData
path = str(dmd.LoadedFromLibraryPath) #Get Path
if path == "None": #If this isn't a library file get the local file path
    path = str(dmd.LoadedFromFileName)
    sub = path.rfind("\\") + 1 #Find "\" from the right and grab the position of the character past it
else:
    sub = path.rfind("/") + 1 #Find "/" from the right and grab the position of the character past it
length = len(path) #get length of path
analysis = path[sub:length] #The specific analysis is the slice of the path after the farthest right '/' character.

print path #for testing
print analysis #for testing

rawDescription: Is it possible to use IronPython to return the path of the current *.dxp project as a string?


code:
from pyPdf import PdfFileWriter, PdfFileReader
import os, sys, glob, shutil

path = glob.glob ('p:\letter2pdf\processing\*.pdf')
template = 'p:/letter2pdf/processing/template/template.pdf'
dest1 = 'p:\letter2pdf\output'
dest2 = 'p:\letter2pdf\processing'

for files in sorted(path):

    output = PdfFileWriter()
    input1 = PdfFileReader(file(files,"rb"))

    print "title = %s" % (input1.getDocumentInfo().title)

# add page 1 from input1 to output document, unchanged
    output.addPage(input1.getPage(0))

#add a watermark from another pdf:
    page2 = input1.getPage(0)
    watermark = PdfFileReader(file (template, "rb"))
    page2.mergePage(watermark.getPage(0))

# finally, write "output" to document-output.pdf
    outputStream = file(input1.getDocumentInfo().title, "wb")
    output.write(outputStream)
    outputStream.close()

rawDescription: Allowing for possible unknown file names to be processed in a python script


code:
f = open(file_name)
for line in f:
  s = line.split(':')
  print s[0],s[2]

rawDescription: How could I execut this code in python (awk example provided) file seperator


code:
#!/usr/bin/env python
""" Searches subdirectories of the current directory looking for .pyc files which
    do not have matching .py files, and deletes them.

    This is useful as a hook for version control when Python files are moved.
    It is dangerous for projects that deliberately include Python
    binaries without source.
"""
import os
import os.path
for root, dirs, files in os.walk("."):
    pyc_files = filter(lambda filename: filename.endswith(".pyc"), files)
    py_files = set(filter(lambda filename: filename.endswith(".py"), files))
    excess_pyc_files = filter(lambda pyc_filename: pyc_filename[:-1] not in py_files, pyc_files)
    for excess_pyc_file in excess_pyc_files:
        full_path = os.path.join(root, excess_pyc_file)
        print "Removing old PYC file:", full_path
        os.remove(full_path)

rawDescription: Automatically deleting pyc files when corresponding py is moved (Mercurial)


code:
import requests
s = requests.Session()

s.get("https://entp.hud.gov/idapp/html/condlook.cfm")

s.headers["Referer"] = "https://entp.hud.gov/idapp/html/condlook.cfm"

payload = {
    'fapproval_method':'NEW',
    'fsorted_by':'condo_name',
    'fstate':'WA',
    'fcounty':'',
    'fcondo_id':'',
    'fcondo_name':'',
    'fcity':'',
    'fzip':'',
    'fstatus_code':'A',
    'fsearch_type':'B',
    'fbegin_mo':'',
    'fbegin_dy':'',
    'fbegin_yr':'',
    'fend_mo':'',
    'fend_dy':'',
    'fend_yr':'',
    'came_from':'oth',
    'in_fhac':'true'
}
r = s.post("https://entp.hud.gov/idapp/html/condo1.cfm", data=payload)
print r.text

rawDescription: Python requests not working

description: Python requests not working


code:
"abc\n123"
print "abc\n123"

rawDescription: Python newline display in console

description: Python newline display in console


code:
userRows=int(input("Enter a positive number less than or equal to 20!"));
for i in range(userRows):
   print ' '*(userRows-i)+'0'*i

rawDescription: I need to reverse the output format of a printed triangle using O's


rawDescription: Print shape in Python

description: Print shape in Python


code:
def f(x) :
    if x == 0:
        print 'yes'
    else:
        print 'no'

f(2)

rawDescription: how can I end a function in python without using (return)?

description: End a function in python without using (return)


code:
import os,sys
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
elements={'Animals':{1:'Bison',2:'Panther',3:'Elephant'},'Birds':{1:'Duck',2:'Hawk',3:'Pigeon'},'Fish':{1:'Shark',2:'Salmon',3:'Piranha'}}

class Model(QtCore.QAbstractListModel):
    def __init__(self):
        QtCore.QAbstractListModel.__init__(self)
        self.items=[]
        self.modelDict={}

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

    def data(self, index, role):
        if not index.isValid():
            return QtCore.QVariant()
        if not (0 <= index.row() < len(self.items)):
            return QtCore.QVariant()

        if not index.isValid():
            return QtCore.QVariant()
        if role==QtCore.Qt.ItemDataRole:
            key=str(index.data().toString())
            returnedValue=self.modelDict.get(key)
            return QtCore.QVariant(returnedValue)

        elif role==QtCore.Qt.DisplayRole:
            row=index.row()
            itemTitle=self.items[row]
            return QtCore.QVariant(itemTitle)

    def addItems(self):
        for key in self.modelDict:
            index=QtCore.QModelIndex()
            self.beginInsertRows(index, 0, 0)
            self.items.append(key)

            inst=self.modelDict.get(key)
            self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)

        self.endInsertRows()

    def removeByIndex(self, index):
        if not index.isValid(): return
        row=index.row()
        self.beginRemoveRows(QtCore.QModelIndex(), row, 0)
        self.items=self.items[:row]+self.items[row+1:]
        key=str(index.data().toString())
        if self.modelDict.get(key): self.modelDict.pop(key,None)
        self.endRemoveRows()

class ListView(QtGui.QListView):
    def __init__(self):
        super(ListView, self).__init__()
        self.model= Model()
        self.model.modelDict=elements
        self.model.addItems()
        self.setModel(self.model)
        self.clicked.connect(self.itemClicked)
        self.show()

    def itemClicked(self, index):
        print 'NUMBER OF ITEMS BEFORE DELETE: %s'%self.model.rowCount()
        self.model.removeByIndex(index)
        print 'NUMBER OF ITEMS AFTER DELETE: %s'%self.model.rowCount()

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

rawDescription: How to clean the mess after items deletion using model()

description: Clean the mess after items deletion using model()


code:
import os, re, sys, time, datetime, collections, thread, threading, atexit, traceback

u'''

debug_live.start(seconds_float) starts a monitor thread which print
the stacktrace of all threads into a logfile.
You can report which lines are executed the most with this script:

app_foo_d@server:~$ python djangotools/utils/debug_live.py -h
usage: debug_live.py [-h] [--most-common N] {sum-all-frames,sum-last-frame}

Read stacktrace log

positional arguments:
  {sum-all-frames,sum-last-frame}

optional arguments:
  -h, --help            show this help message and exit
  --most-common N       Display the N most common lines in the stacktraces

---------------------------------

You can start the watching thread your django middleware like this:

class FOOMiddleware:
    def __init__(self):
        u'This code gets executed once after the start of the wsgi worker process. Not for every request!'
        seconds=getattr(settings, 'debug_live_interval', None)
        if seconds:
            seconds=float(seconds)
            from djangotools.utils import debug_live
            debug_live.start(seconds)

# settings.py
debug_live_interval=0.3 # ever 0.3 second

# Inspired by http://code.google.com/p/modwsgi/wiki/DebuggingTechniques

You can get a simple report of the log file of stacktraces like below. The lines
which are not from django are marked with "<====". That's most likely your code
and this could be a bottle neck.

python ..../debug_live.py read
 1971 File: "/home/foo_bar_p/django/core/handlers/wsgi.py", line 272, in __call__
      response = self.get_response(request)
 1812 File: "/home/foo_bar_p/django/core/handlers/base.py", line 111, in get_response
      response = callback(request, *callback_args, **callback_kwargs)
 1725 File: "/home/foo_bar_p/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
      return self.cursor.execute(query, args)
 1724 File: "/home/foo_bar_p/django/db/models/sql/compiler.py", line 735, in execute_sql
      cursor.execute(sql, params)
 1007 File: "/home/foo_bar_p/django/db/models/sql/compiler.py", line 680, in results_iter
      for rows in self.execute_sql(MULTI):
  796 File: "/home/foo_bar_p/django/db/models/query.py", line 273, in iterator
      for row in compiler.results_iter():
  763 File: "/home/foo_bar_p/foo/utils/ticketutils.py", line 135, in __init__      <====
      filter=type_filter(root_node=self.root_node)
  684 File: "/home/foo_bar_p/django/db/models/query.py", line 334, in count
      return self.query.get_count(using=self.db)
  679 File: "/home/foo_bar_p/django/db/models/sql/query.py", line 367, in get_aggregation
      result = query.get_compiler(using).execute_sql(SINGLE)
  677 File: "/home/foo_bar_p/django/db/models/sql/query.py", line 401, in get_count
      number = obj.get_aggregation(using=using)[None]

'''

from django.conf import settings

outfile = os.path.expanduser('~/tmp/debug_live.log')

other_code=re.compile(r'/(django|python...)/')

def stacktraces():
    code=[]
    now=datetime.datetime.now()
    pid=os.getpid()
    my_thread_id=thread.get_ident()
    for thread_id, stack in sys._current_frames().items():
        if thread_id==my_thread_id:
            continue # Don't print this monitor thread
        code.append("\n\n#START date: %s\n# ProcessId: %s\n# ThreadID: %s" % (now, pid, thread_id))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
        code.append('#END')
    if not code:
        return
    fd=open(outfile, 'at')
    fd.write('\n'.join(code))
    fd.close()

def monitor(interval):
    while monitor_thread:
        stacktraces()
        time.sleep(interval)

monitor_thread=None

def exiting():
    global monitor_thread
    monitor_thread=None

def start(interval):
    global monitor_thread
    if monitor_thread:
        return
    assert not os.path.islink(outfile), outfile # well known temporary name.... symlink attack...
    monitor_thread = threading.Thread(target=monitor, args=[interval])
    monitor_thread.setDaemon(True)
    atexit.register(exiting)
    monitor_thread.start()

def read_logs(args):
    # The outfile can be huge, don't read the whole file into memory.
    counter=collections.Counter()
    cur_stack=[]
    py_line=''
    code_line=''
    if args.action=='sum-all-frames':
        sum_all_frames=True
    else:
        sum_all_frames=False
    for line in open(outfile):
        if line.startswith('#END'):
            if sum_all_frames:
                frames=cur_stack
            else:
                frames=cur_stack[-1:]
            counter.update(frames)
            cur_stack=[]
            continue
        if line[0] in '\n#':
            continue
        if line.startswith('File:'):
            py_line=line.rstrip()
            continue
        if line.startswith(' '):
            code_line=line.rstrip()
            if not (py_line, code_line) in cur_stack:
                # If there is a recursion, count the line only once per stacktrace
                cur_stack.append((py_line, code_line))
            continue
        print 'ERROR unparsed', line
    for (py, code), c in counter.most_common(args.most_common):
        if not other_code.search(py):
            py='%s      <====' % py
        print '% 5d %s\n    %s' % (c, py, code)

def main():
    import argparse
    parser=argparse.ArgumentParser(description='Read stacktrace log')
    parser.add_argument('action', choices=['sum-all-frames', 'sum-last-frame'])
    parser.add_argument('--most-common', metavar='N', default=30, type=int, help='Display the N most common lines in the stacktraces')
    args=parser.parse_args()
    return read_logs(args)

if __name__=='__main__':
    main()

rawDescription: Live Profiling of Python Server

description: Live Profiling of Python Server


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's inspect unstable behaviour

description: Celery's inspect unstable behaviour


code:
import csv
import sqlite3

#Give the table a name and use it for the file as well
table_name = 'Example'
a = open(table_name + '.csv', 'r')

#Use a dict reader
my_reader = csv.DictReader(a)
print my_reader.fieldnames # Use this to create table and get number of field values,etc.

# create statement
create_sql = 'CREATE TABLE ' + table_name + '(' + ','.join(my_reader.fieldnames) + ')'
print create_sql

#open the db
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Create table using field names
c.execute(create_sql)
insert_sql = 'insert into ' + table_name + ' (' + ','.join(my_reader.fieldnames) + ') VALUES (' + ','.join(['?'] * len(my_reader.fieldnames))+ ')'
print insert_sql

values = []
for row in my_reader:
    row_values = []
    for field in my_reader.fieldnames:
        row_values.append(row[field])
    values.append(row_values)

c.executemany(insert_sql, values)

rawDescription: Dynamic INSERT Statement in Python

description: Dynamic INSERT Statement in Python


code:
string_line = '
this is real crm and for real CRM amd and Read Crm.
\r\n'
keyword = 'Real CRM'

if keyword.lower() in string_line.lower():
    print "Keyword found"
else:
    print "Keyword not found"

rawDescription: Python check word exist in a string contents in any cases


code:
# Writer
import sys

while True:
    print '0123456789'
    sys.stdout.flush()

rawDescription: Preventing write interrupts in python script


code:
strings = raw_input("Please enter words divided by comma and white space: ")
x = strings.split()
y = []
for i in x:
    if len(i) > 10:
        y.append(i)
print len(y)

rawDescription: How to write a program that returns a count of strings longer than 10 characters in a list of strings

description: Write a program that returns a count of strings longer than 10 characters in a list of strings


code:
root = Tkinter.Tk()
label = Tkinter.Label(root)
label.pack()
img = None
tkimg = [None]  # This, or something like it, is necessary because if you do not keep a reference to PhotoImage instances, they get garbage collected.

delay = 500   # in milliseconds
def loopCapture():
    print "capturing"
#    img = fetch_image(URL,USERNAME,PASSWORD)
    img = Image.new('1', (100, 100), 0)
    tkimg[0] = ImageTk.PhotoImage(img)
    label.config(image=tkimg[0])
    root.update_idletasks()
    root.after(delay, loopCapture)

loopCapture()
root.mainloop()

rawDescription: Refresh image in Tkinter window

description: Refresh image in Tkinter window


code: print RSelement.text

rawDescription: BeautifulSoup: stripping HTML tags from findAll ResultSet


code:
from random import randint

def number_of_tries_before_hitting_jackpot(user_number):
    n_tries = 0
    while True:
        random_number = set([randint(1,10) for i in range(1,7)])
        if user_number == random_number:
              return n_tries
        else:
              n_tries+=1

def ask_user_his_number():
   userlist = []
   for y in range(6):
        user = input("Enter your entry no.{} lotto number: ".format(y+1))
        userlist.append(user)
   return set(userlist)

n_tries_list = []
for x in range(1,1001):
       user_number =  ask_user_his_number()
       print user_number
       tmp = number_of_tries_before_hitting_jackpot(user_number)
       print tmp
       n_tries_list.append(tmp)
avg_number_drawings = reduce(lambda x, y: x + y, n_tries_list) / len(n_tries_list)
print avg_number_drawings

rawDescription: Python: Average number of drawings (Randomly generated numbers)


code:
import email

email_message = email.message_from_string(raw_email)

print email_message['To']

print email.utils.parseaddr(email_message['From']) # for parsing "Yuji Tomita" <yuji@grovemade.com>

print email_message.items() # print all headers

# note that if you want to get text content (body) and the email contains
# multiple payloads (plaintext/ html), you must parse each message separately.
# use something like the following: (taken from a stackoverflow post)
def get_first_text_block(self, email_message_instance):
    maintype = email_message_instance.get_content_maintype()
    if maintype == 'multipart':
        for part in email_message_instance.get_payload():
            if part.get_content_maintype() == 'text':
                return part.get_payload()
    elif maintype == 'text':
        return email_message_instance.get_payload()

rawDescription: Read the body (only the text)of unseen email using imap python


code:
import os

def find_folder( path, folder_name):
    head, tail = os.path.split(path)
    if folder_name == os.path.split(head)[1]:
        return path
    else:
        return find_folder(head, folder_name)

path = 'C:\\Folder1\\Folder2\\Folder3\\Folder4'
print find_folder(path, 'Folder2')

rawDescription: Get subset of path with specified folder


code:
if dict['key']:
    print dict['key']
else:
    #Other stuff
    None

rawDescription: A dictionary with in dictionary that only gets created when the current dataset requires it


code:
import time
# target rate: 100 ops / 1 second
target = 100.0
round_time = 1.0
# at first, assume the writes are immediate
sleepTime = round_time/target

ops = 0
t_start = time.time()

while True:
    #doYourIOoperationHere()
    ops += 1
    time.sleep(sleepTime)

    # adjust sleep time periodically
    if ops == target:
        t_end = time.time()
        elapsed = t_end - t_start
        difference = round_time - elapsed
        # print out the vars here to debug adjustment
        print "%d ops done, elapsed %.3f, difference %.3f" % (ops, elapsed, difference)
        # increase or decrease the sleep time, approach the target time slowly
        sleepTime += difference/target/2
        t_start = time.time()
        ops = 0

rawDescription: Programmatically Set File Write Rate

description: Programmatically Set File Write Rate


code:
#!/usr/bin/env python
import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

rawDescription: Writing a telnet client

description: Writing a telnet client


code:
import os, time

checkdir="./"

for item in os.listdir(checkdir):
    item_path = "%s%s"%(checkdir,item)
    mtime = os.path.getmtime(item_path)
    print "%s: %s" %(item_path,mtime)

rawDescription: Python ftplib: Best way to check if a file has changed?


code:
import os.path
print os.path.dirname("C:\Users\myFile.txt")
print os.path.dirname(os.path.dirname("C:\Users\myFile.txt"))

rawDescription: Remove Last Path Component In a String

description: Remove Last Path Component In a String


code:
brands = ["google", "apple", "intel", "qualcomm"]
otherBrands = ["nike", "reebok", "puma"]

print ":".join(brands + otherBrands)

rawDescription: Python - Unpack lists and join into a string


code:
def UploadResourceSample(filename, filepath, fullpath):
  """Upload a document, and convert to Google Docs."""
  client = CreateClient()
  doc = gdata.docs.data.Resource(type='document', title=filename)

  path = fullpath
  print 'Selected file at: %s' % path

  # Create a MediaSource, pointing to the file
  media = gdata.data.MediaSource()
  media.SetFileHandle(path, 'application/pdf')

  # Pass the MediaSource when creating the new Resource
  create_uri = gdata.docs.client.RESOURCE_UPLOAD_URI + '?ocr=true&ocr-language=de'
  doc = client.CreateResource(doc, create_uri=create_uri, media=media)
  print 'Created, and uploaded:', doc.title.text, doc.resource_id.text

rawDescription: Upload pdf file with gdata docs python v3.0 with ocr


code:
import sys
sys.stdin.encoding
print urllib.urlencode({'foo':raw_input('> ').decode('cp437').encode('utf8')})

rawDescription: Problem encoding accented characters with python


code:
# get desired posts (add your filters etc)
posts = session.query(Post).order_by(Post.time)

# create a dictionary, where key is the date(-only), and values are the posts of that day
from collections import defaultdict
grouped = defaultdict(list)
for post in posts:
    dat = post.time.date()
    grouped[dat].append(post)

# iterate over new grouped structure
for dat, posts in sorted(grouped.items()):
    print dat
    for post in posts:
        print '  ', post

rawDescription: sqlalchemy + flask , getting all post by day

description: Sqlalchemy + flask , getting all post by day


code:
import functools
def need_two_arguments(a,b):
    print a + b

no_arguments_needed = functools.partial(need_two_arguments, "Hello, ", "World!")
no_arguments_needed()
no_arguments_needed = (lambda: need_two_arguments("Hello, ", "World!"))
no_arguments_needed()

rawDescription: python subprocess.popen use preexec_fn with arguments

description: Python subprocess.popen use preexec_fn with arguments


code:
for key in mydictionary:
   print "key: %s , value: %s" % (key, mydictionary[key])

rawDescription: How can I get dictionary key as variable directly in Python (not by searching from value)?

description: Get dictionary key as variable directly in Python (not by searching from value)


code:
def nesting():
    count = [0]
    def nested():
        count[0] += 1

    for i in range(10):
        nested()
    print count[0]

rawDescription: How do I change nesting function's variable in the nested function

description: Change nesting function's variable in the nested function


code:
for k,g in df.groupby(df['A'] - np.arange(df.shape[0])):
    print g

rawDescription: Slice pandas dataframe in groups of consecutive values


code:
for row in Alist:
    print row

rawDescription: python list of lists insert newline

description: Python list of lists insert newline


code:
def deranged(n):
    for i in xrange(100, n, 100):
            yield i
    yield n

for i in deranged(435):
    print i

rawDescription: Specify loops in python using an arithematic operation


code:
#!/usr/bin/env python
import threading, time

class A(threading.Thread):
    def __init__(self, name):
            threading.Thread.__init__(self)
            self.name=name
            self.count=0
    def run(self):
            while self.count<10:
                    print self.name, "Running!"
                    time.sleep(1)
                    self.count+=1

a=A("first")
a.start()
time.sleep(5)
a=A("second")
a.start()

rawDescription: Python threads garbage collection

description: Python threads garbage collection


code:
import requests
import urlparse

url = "http://192.168.1.2/map/ShowPage.ashx?="
names = ["admin","backup","contact","index","logs","news","reboot",
              "register","test","users"]

with open("/root/Desktop/urls.txt", 'a') as urls:
    for name in names:
       newUrl = url + name
       r = requests.get(newUrl)
       c = r.content
       if r.status_code == 200 and (("Unknown" in c) <>1):
            urls.write(url + '\n')

rawDescription: Using Python to append a list of names to a single URL during a crawl of a URL


code:
#!/usr/bin/env python

from ctypes import *
import msvcrt
import os
import subprocess
import time

TEST_EXECUTABLE = 'C:\\dev\\test.exe'
TEST_INPUT = 'C:\\dev\\input.txt'

# input event types
KEY_EVENT = 0x0001

# constants, flags
MAPVK_VK_TO_VSC = 0

# structures
class CHAR_UNION(Union):
    _fields_ = [("UnicodeChar", c_wchar),
                ("AsciiChar", c_char)]

    def to_str(self):
        return ''

class KEY_EVENT_RECORD(Structure):
    _fields_ = [("bKeyDown", c_byte),
                ("pad2", c_byte),
                ("pad1", c_short),
                ("wRepeatCount", c_short),
                ("wVirtualKeyCode", c_short),
                ("wVirtualScanCode", c_short),
                ("uChar", CHAR_UNION),
                ("dwControlKeyState", c_int)]

    def to_str(self):
        return ''

class INPUT_UNION(Union):
    _fields_ = [("KeyEvent", KEY_EVENT_RECORD)]

    def to_str(self):
        return ''

class INPUT_RECORD(Structure):
    _fields_ = [("EventType", c_short),
                ("Event", INPUT_UNION)]

    def to_str(self):
        return ''

def write_key_to_console(hcon, key):
    li = INPUT_RECORD * 2
    list_input = li()

    ke = KEY_EVENT_RECORD()
    ke.bKeyDown = c_byte(1)
    ke.wRepeatCount = c_short(1)

    cnum = ord(key)
    ke.wVirtualKeyCode = windll.user32.VkKeyScanW(cnum)
    ke.wVirtualScanCode = c_short(windll.user32.MapVirtualKeyW(int(cnum),
                                                               MAPVK_VK_TO_VSC))
    ke.uChar.UnicodeChar = unichr(cnum)
    kc = INPUT_RECORD(KEY_EVENT)
    kc.Event.KeyEvent = ke
    list_input[0] = kc

    list_input[1] = list_input[0]
    list_input[1].Event.KeyEvent.bKeyDown = c_byte(0)

    events_written = c_int()
    ret = windll.kernel32.WriteConsoleInputW(hcon,
                                             list_input,
                                             2,
                                             byref(events_written))

    return ret

def main():
    with open(TEST_INPUT, mode='r') as test_input:
        fdcon = os.open('CONIN$', os.O_RDWR | os.O_BINARY)
        hconin = msvcrt.get_osfhandle(fdcon)

        test_exec = subprocess.Popen([TEST_EXECUTABLE])

        for cmd in test_input:
            cmd = cmd.strip('\r\n')
            write_key_to_console(hconin, cmd)
            time.sleep(1)

        os.close(fdcon)
        ret = test_exec.wait()

        print '%s (%d): %d' % (TEST_EXECUTABLE, test_exec.pid, ret)

    pass

if __name__ == "__main__":
    main()

rawDescription: How to control interactive console input/output from Python on Windows?

description: Control interactive console input/output from Python on Windows


code:
import threading

def after_2_minutes():
   if process_still_running():
       print "ALERT!!"

# start a timer in the background that waits 2 minutes
threading.Timer(2 * 60, after_2_minutes).start()

# start the process
foo.bar()

rawDescription: Python watch long running process, alert after so many minutes?


code:
reflace=0
inputData = "55,1,1,1".split(",")
# Now find the first field from comma
inputData[reflace] = round( float( inputData[reflace] ) , 2) + 0.01

print inputData[reflace] ### Will print 55.01

ranges = [(-1,1)]
if any(lower <= inputData[reflace] <= upper for (lower, upper) in ranges):
  print "+ " + inputData

rawDescription: Python - how to check if the value is within given range of float/integer?


code:
for i, j in enumerate(my_list):
    for elem in (v for k, v in enumerate(my_list) if k != i):
        print elem,

rawDescription: During an iteration print the whole list but the current index


code: print json.dumps(x, ensure_ascii=False)

rawDescription: Python Saving JSON Files as UTF-8

description: Python Saving JSON Files as UTF-8


code:
import scipy
X = scipy.rand(3,3)
Y = scipy.rand(3,3)
print X+Y
print scipy.delete(X, 1, 0)
print scipy.delete(X, 1, 1)
X[1,:] = [1,2,3]
print X

rawDescription: numpy.matrix manipulations

description: Numpy.matrix manipulations


code:
import networkx as nx

G = nx.DiGraph()
G.add_edges_from([('a', 'c', {'weight': 1}),
                  ('a', 'b', {'weight': 3}),
                  ('c', 'a', {'weight': 1}),
                  ('c', 'd', {'weight': 2}),
                  ('b', 'a', {'weight': 1}),
                  ('d', 'c', {'weight': 1})])

print G.edges(data=True)

g = nx.Graph()
g.add_edges_from(G.edges_iter(), weight=0)

print g.edges(data=True)

for u, v, d in G.edges_iter(data=True):
    g[u][v]['weight'] += d['weight']

print g.edges(data=True)

rawDescription: NetworkX DiGraph() to Graph() with edge weights not summed, how to sum weights?


code:
import wx
from functools import partial

class MenuCallback(object):

    def __init__(self, parent, id):
        self.frame=wx.Frame(None)
        menu_bar = wx.MenuBar()
        menu1 = wx.Menu()
        menu_item_1 = menu1.Append(-1, "&Option #1")
        menu_item_2 = menu1.Append(-1, "&Option #2")
        menu_item_3 = menu1.Append(-1, "&Exit...")

        menu_bar.Append(menu1, "&File")
        self.frame.SetMenuBar(menu_bar)
        self.frame.Bind(wx.EVT_MENU, partial(self.option_chosen, 1),
                        menu_item_1)
        self.frame.Bind(wx.EVT_MENU, partial(self.option_chosen, 2),
                        menu_item_2)
        self.frame.Bind(wx.EVT_MENU, self.on_close, menu_item_3)

        self.frame.Show(True)

    def option_chosen(self, number, event):
        print "option chosen =", number

    def on_close(self, event):
        self.frame.Close(True)

app = wx.PySimpleApp()
MC=MenuCallback(parent=None, id=-1)
app.MainLoop()

rawDescription: How to get selected menu item in wxPython?

description: Get selected menu item in wxPython


code:
from twisted.internet import protocol, reactor, task
import twisted.internet.error

class peer(protocol.DatagramProtocol):

    def sendJoin(self):
        self.transport.write('Join',("127.0.0.1", 8001))

    def startProtocol(self):
        self.sendJoin()

# just to make sure ports 8002 and 8003 will be in use
reactor.listenUDP(8002, peer())
reactor.listenUDP(8003, peer())

# try all ports between [8002..8999]
for port in range(8002, 9000):
    try:
        reactor.listenUDP(port, peer())
        print "Listening on port %d" % port
        break
    except twisted.internet.error.CannotListenError, ex:
        print "Port is %d busy: %s" % (port, ex)
        continue
reactor.run()

rawDescription: How to listen on another port if busy in twisted

description: Listen on another port if busy in twisted


code:
from croniter import croniter
from datetime import datetime
base = datetime(2010, 1, 25, 4, 46)
iter = croniter('*/5 * * * *', base)  # every 5 minites
print iter.get_next(datetime)   # 2010-01-25 04:50:00
print iter.get_next(datetime)   # 2010-01-25 04:55:00
print iter.get_next(datetime)   # 2010-01-25 05:00:00
iter = croniter('2 4 * * mon,fri', base)  # 04:02 on every Monday and Friday
print iter.get_next(datetime)   # 2010-01-26 04:02:00
print iter.get_next(datetime)   # 2010-01-30 04:02:00
print iter.get_next(datetime)   # 2010-02-02 04:02:00

rawDescription: Cron parser and validation in python

description: Cron parser and validation in python


rawDescription: Printing Excel sheets with COM interface


code:
def chequeouser(self):
    passwdcheck = str(txt_password.text())
    usuariover =  str(txt_usuario.text())
   # datosLogin = "SELECT * FROM t_usuarios WHERE id_usuario = 'usuario' AND pasword = 'password'
    cursor = dbase.cursor()
    cursor.execute("SELECT id_usuario, password FROM t_usuarios WHERE id_usuario = %s AND password = %s", (usuariover, passwdcheck))
    row = cursor.fetchone()
    if not row:
       print "no rows!"
       return
    if passwdcheck == row[1]:
       print 'MATCH for user', row[0]
    else:
       print 'uhoh'

rawDescription: MysqlDb and Pyqt4 comparing variables

description: MysqlDb and Pyqt4 comparing variables


code:
from django.db import connection
print connection.queries
u'SELECT DISTINCT "market_basket"."id", "market_basket"."weight" FROM "market_basket" INNER JOIN "market_fruit" ON ("market_basket"."id" = "market_fruit"."inbasket_id") INNER JOIN "market_fruitname" ON ("market_fruit"."ofkind_id" = "market_fruitname"."id") WHERE ("market_fruitname"."name" IN (\'apple\') AND "market_fruitname"."name" IN (\'banana\')) LIMIT 21'

rawDescription: Query Q() is not working as 'set contain' for related field in Django?


code:
import multiprocessing as mp
import multiprocessing.managers

class mydict(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self.iters = {}

    def iteritems(self):
        print "iteritems", mp.current_process()
        return dict.iteritems(self)

    def _iteritems_start(self):
        print "_iteritems_start", mp.current_process()
        i = dict.iteritems(self)
        self.iters[id(i)] = i
        return id(i)

    def _iteritems_next(self, iter_id):
        try:
            return self.iters[iter_id].next()
        except StopIteration:
            del self.iters[iter_id]
            return None

class mydict_proxy(mp.managers.DictProxy):
    def iteritems(self):
        print "iteritems proxy", mp.current_process()
        return self._callmethod("iteritems")

    def iteritems2(self):
        print "iteritems2 proxy", mp.current_process()
        iter_id = self._callmethod("_iteritems_start")
        def generator():
            while True:
                a = self._callmethod("_iteritems_next",
                             (iter_id,))
                if a == None:
                    return
                yield a
        return generator()

    _method_to_typeid_ = { "iteritems": "Iterator" }
    _exposed_ = mp.managers.DictProxy._exposed_
    _exposed_ += ("iteritems", "_iteritems_start", "_iteritems_next")

class mymanager(mp.managers.BaseManager):
    pass
mymanager.register("mydict", mydict, mydict_proxy)
mymanager.register("Iterator", proxytype = mp.managers.IteratorProxy,
           create_method = False)

def other(d):
    for k, v in d.iteritems2():
        d[k] = v.lower()
    for k, v in d.iteritems():
        d[k] = ord(v)

def main():
    manager = mymanager()
    manager.start()
    d = manager.mydict(list(enumerate("ABCDEFGHIJKLMNOP")))
    for (k, v) in d.iteritems():
        print k, v
    proc = mp.Process(target = other, args = (d,))
    proc.start()
    proc.join()
    for (k, v) in d.iteritems():
        print k, v

if __name__ == "__main__":
    main()

rawDescription: How to iterate over a dict proxy in Python?

description: Iterate over a dict proxy in Python


code:
from multiprocessing import Process, Queue

def load_image(img_file, output_q):
    with open(img_file, 'rb') as f:
        img_data = f.read()
        # perform processing on img_data, then queue results
        output_q.put((img_file, img_data))

result_q = Queue()
images = ['/tmp/p1.png', '/tmp/p2.jpg', '/tmp/p3.gif', '/tmp/p4.jpg']

for img in images:
    Process(target=load_image, args=(img, result_q)).start()

for i in range(len(images)):
    img, data = result_q.get()
    # do something with the image data
    print "processing of image file %s complete" % img

rawDescription: Parallel processing / threading in Python


code:
import lxml.etree as et
import csv

xml="""<?xml version="1.0" encoding="UTF-8"?>
<OASISReport>
    <MessageHeader>
        <TimeDate>2015-11-05T07:50:48-00:00</TimeDate>
        <Source>OASIS</Source>
        <Version>v20131201</Version>
    </MessageHeader>
    <MessagePayload>
        <RTO>
            <name>CAISO</name>
            <REPORT_ITEM>
                <REPORT_HEADER>
                    <SYSTEM>OASIS</SYSTEM>
                    <TZ>PPT</TZ>
                    <REPORT>PRC_LMP</REPORT>
                    <MKT_TYPE>DAM</MKT_TYPE>
                    <UOM>US$/MWh</UOM>
                    <INTERVAL>ENDING</INTERVAL>
                    <SEC_PER_INTERVAL>3600</SEC_PER_INTERVAL>
                </REPORT_HEADER>
                <REPORT_DATA>
                    <DATA_ITEM>LMP_PRC</DATA_ITEM>
                    <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
                    <OPR_DATE>2015-10-12</OPR_DATE>
                    <INTERVAL_NUM>2</INTERVAL_NUM>
                    <INTERVAL_START_GMT>2015-10-12T08:00:00-00:00</INTERVAL_START_GMT>
                    <INTERVAL_END_GMT>2015-10-12T09:00:00-00:00</INTERVAL_END_GMT>
                    <VALUE>29</VALUE>
                </REPORT_DATA>
                <REPORT_DATA>
                    <DATA_ITEM>LMP_PRC</DATA_ITEM>
                    <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
                    <OPR_DATE>2015-10-12</OPR_DATE>
                    <INTERVAL_NUM>12</INTERVAL_NUM>
                    <INTERVAL_START_GMT>2015-10-12T18:00:00-00:00</INTERVAL_START_GMT>
                    <INTERVAL_END_GMT>2015-10-12T19:00:00-00:00</INTERVAL_END_GMT>
                    <VALUE>35.67227</VALUE>
                </REPORT_DATA>
</REPORT_ITEM>
<REPORT_ITEM>
    <REPORT_HEADER>
        <SYSTEM>OASIS</SYSTEM>
        <TZ>PPT</TZ>
        <REPORT>PRC_LMP</REPORT>
        <MKT_TYPE>DAM</MKT_TYPE>
        <UOM>US$/MWh</UOM>
        <INTERVAL>ENDING</INTERVAL>
        <SEC_PER_INTERVAL>3600</SEC_PER_INTERVAL>
    </REPORT_HEADER>
    <REPORT_DATA>
        <DATA_ITEM>LMP_PRC</DATA_ITEM>
        <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
        <OPR_DATE>2015-10-12</OPR_DATE>
        <INTERVAL_NUM>2</INTERVAL_NUM>
        <INTERVAL_START_GMT>2015-10-12T08:00:00-00:00</INTERVAL_START_GMT>
        <INTERVAL_END_GMT>2015-10-12T09:00:00-00:00</INTERVAL_END_GMT>
        <VALUE>29</VALUE>
    </REPORT_DATA>
    <REPORT_DATA>
        <DATA_ITEM>LMP_PRC</DATA_ITEM>
        <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
        <OPR_DATE>2015-10-12</OPR_DATE>
        <INTERVAL_NUM>12</INTERVAL_NUM>
        <INTERVAL_START_GMT>2015-10-12T18:00:00-00:00</INTERVAL_START_GMT>
        <INTERVAL_END_GMT>2015-10-12T19:00:00-00:00</INTERVAL_END_GMT>
        <VALUE>35.67227</VALUE>
    </REPORT_DATA>
</REPORT_ITEM>
<REPORT_ITEM>
    <REPORT_HEADER>
        <SYSTEM>OASIS</SYSTEM>
        <TZ>PPT</TZ>
        <REPORT>PRC_LMP</REPORT>
        <MKT_TYPE>DAM</MKT_TYPE>
        <UOM>US$/MWh</UOM>
        <INTERVAL>ENDING</INTERVAL>
        <SEC_PER_INTERVAL>3600</SEC_PER_INTERVAL>
    </REPORT_HEADER>
    <REPORT_DATA>
        <DATA_ITEM>LMP_PRC</DATA_ITEM>
        <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
        <OPR_DATE>2015-10-12</OPR_DATE>
        <INTERVAL_NUM>2</INTERVAL_NUM>
        <INTERVAL_START_GMT>2015-10-12T08:00:00-00:00</INTERVAL_START_GMT>
        <INTERVAL_END_GMT>2015-10-12T09:00:00-00:00</INTERVAL_END_GMT>
        <VALUE>29</VALUE>
    </REPORT_DATA>
    <REPORT_DATA>
        <DATA_ITEM>LMP_PRC</DATA_ITEM>
        <RESOURCE_NAME>TH_SP15_GEN-APND</RESOURCE_NAME>
        <OPR_DATE>2015-10-12</OPR_DATE>
        <INTERVAL_NUM>12</INTERVAL_NUM>
        <INTERVAL_START_GMT>2015-10-12T18:00:00-00:00</INTERVAL_START_GMT>
        <INTERVAL_END_GMT>2015-10-12T19:00:00-00:00</INTERVAL_END_GMT>
        <VALUE>35.67227</VALUE>
    </REPORT_DATA>
</REPORT_ITEM>
<DISCLAIMER_ITEM>
    <DISCLAIMER>The contents of these pages are subject to change without notice.  Decisions based on information contained within the California ISO's web site are the visitor's sole responsibility.</DISCLAIMER>
    </DISCLAIMER_ITEM>
    </RTO>
    </MessagePayload>
</OASISReport>"""

tree = et.fromstring(xml)

itms = tree.xpath("//REPORT_ITEM")

data = []
for i in itms:
    d= [[k.text for k in j] for j in i]
    print d
    data.append(d)

for item in data:
    for i in range(len(data)):
        with open("D:\\_" + str(i) + ".csv", 'w') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerows(item)

rawDescription: Write xml response to multiple csv files in Python


code:
import time
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from xhtml2pdf import pisa
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

url = 'http://www.yahoo.com'
tem_pdf = "c:\\tem_pdf.pdf"
final_file = "c:\\younameit.pdf"

app = QApplication(sys.argv)
web = QWebView()
#Read the URL given
web.load(QUrl(url))
printer = QPrinter()
#setting format
printer.setPageSize(QPrinter.A4)
printer.setOrientation(QPrinter.Landscape)
printer.setOutputFormat(QPrinter.PdfFormat)
#export file as c:\tem_pdf.pdf
printer.setOutputFileName(tem_pdf)

def convertIt():
    web.print_(printer)
    QApplication.exit()

QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)

app.exec_()
sys.exit

# Below is to add on the weblink as text and present date&time on PDF generated

outputPDF = PdfFileWriter()
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Helvetica", 9)
# Writting the new line
oknow = time.strftime("%a, %d %b %Y %H:%M")
can.drawString(5, 2, url)
can.drawString(605, 2, oknow)
can.save()

#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file(tem_pdf, "rb"))
pages = existing_pdf.getNumPages()
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
for x in range(0,pages):
    page = existing_pdf.getPage(x)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
# finally, write "output" to a real file
outputStream = file(final_file, "wb")
output.write(outputStream)
outputStream.close()

print final_file, 'is ready.'

rawDescription: How to convert webpage into PDF by using Python

description: Convert webpage into PDF by using Python


code:
f = open("csv.txt")
c = csv.reader(f)
for row in c: print row
f.seek(0)
for row in c: print row   # again

rawDescription: Python csv.reader: How do I return to the top of the file?


code:
ints = [1, 2, 3, 4]
widths = [(4, w), (2, x), (2, y), (8, z)]
for i in ints:
    print "%s|%s|%s|%s" % tuple(((("%0." + str(padding) + "d") % (i)).center(width)) for padding, width in widths)

rawDescription: Center a formatted row of integers in Python?


code:
class foo(object):
    def __nonzero__( self) :
        return self.bar % 2 == 0

def a(foo):
    if foo:
        print "spam"
    else:
        print "eggs"

def main():
    myfoo = foo()
    myfoo.bar = 3
    a(myfoo)

if __name__ == "__main__":
    main()

rawDescription: Boolean value of objects in Python

description: Boolean value of objects in Python


code:
import nfqueue, socket
from threading import Thread
from scapy.all import *
import os

os.system('iptables -t mangle -A PREROUTING -j NFQUEUE --queue-num 1')
os.system('iptables -t mangle -A POSTROUTING -j NFQUEUE --queue-num 2')

count = 0

def cb(payload):
    global count
    count +=1
    data = payload.get_data()
    p = IP(data)

    print str(count) + ": TOS     = " + str(p.tos)
    payload.set_verdict(nfqueue.NF_ACCEPT)

def run_queue(queue_num):
    print "Preparing the queue"
    q = nfqueue.queue()
    q.open()
    q.unbind(socket.AF_INET)
    q.bind(socket.AF_INET)
    q.set_callback(cb)
    q.create_queue(queue_num)

    try:
        print "Running the queue"
        q.try_run()

    except KeyboardInterrupt, e:
        print "interruption"
        q.unbind(socket.AF_INET)
        q.close()

if __name__ == "__main__":
    t1 = Thread(target=run_queue, args=(1,))
    t1.start()
    t2 = Thread(target=run_queue, args=(2,))
    t2.start()
    t1.join()
    t2.join()

rawDescription: Run 2 blocking loops in the same program


code:
regex.findall(ur'\p{L}\p{M}*', name)
for c in regex.findall(ur'\p{L}\p{M}*', name):
  print c

rawDescription: How to split unicode strings character by character in python?

description: Split unicode strings character by character in python


code:
while True:
    readable, writable, exceptional = select.select([sock], [], [], 1.0)
    if readable:
        data = sock.recv(1024)
    else:
        print "nothing yet"

rawDescription: continue by sock.recv() when empty python

description: Continue by sock.recv() when empty python


code:
import sys
def myScript():
        # Rate our Love!!
    ###   Press F5
    ## then input a rating for our relationship so far
    print "Type a rating for our relationship"
    love_rate = raw_input("Type a rating for our relationship:")
    ### word answers
    idk = 'idk'
    no = 'no'
    yes = 'yes'
    lol = 'lol'
    smh = 'smh'
    love(love_rate)

def love(n):
        if n < 0 :
            print "Why would it be negative?!"
        elif n == 'yes' :
            print " Well if that's the case, then I think we're gonna be just fine."
        elif n == 'no' :
            print 'well then... this is awkward'
        elif n == 'lol' :
            print '''THATS NOT EVEN A NUMBER    ......sniff'''
        elif n == 'smh' :
            print "I'm kinda mad that's an answer you thought of putting here"
        ## numbered entries
        elif n == '0' :
            print " *gasps profusely* YOU DON'T DESERVE THIS PROGRAM"
        elif n == '1' :
            print "Wow that is kinda hurtful, not gonna lie"
        elif n == '2' :
            print "You make me smile at least once, each and every day"
        elif n == '3' :
            print"you wouldn't believe how annoying it was to get this program to run properly!" + " Thats all i get?"
        elif n == '4' :
            print "let's " + "shoot a little higher than that"
        elif n == '5' :
            print "you're unforgettable, that's what you are"
        elif n == '6' :
            print "always have, always '____' *hint* fill in the blank "
        elif n == '7' :
            print "i could never leave you, I love you too much"
        elif n == '8' :
            print "an 8/10 is still only a B, maybe I'm not trying hard enough"
        elif n == '9' :
            print " well, I'm not perfect yet, could have seen that one coming. Guess I just have to keep trying :)"
        elif n == '10' :
            print " i think you're exaggerating, i really am not that good yet"
        elif n == '11' :
            print """I can be a little immature sometimes and i'm sorry for that, i promise I can get better though. But i need you. I need you to help me out. Can you do that?"""
        elif n == '12' :
            print "I don't think the scale is supposed to go this high"
        elif n == '13' :
            print "alright now you're pushing it."
        elif n == '14' :
            print "alright, THE SCALE GOES UP TO AROUND 10. CEASE"
        elif n == '15' :
            print " go up one more number. I DARE YOU"
        elif n == '16' :
            print " go up one more number. see what happens"
        elif n == '17' :
            print "one more number"
        elif n == '18' :
            print "one more"
        elif n == '19' :
            print "STOP"
        elif n == '92412' :
            print " I think that is one fantastic answer, can't wait for our anniversary"
        else:
            print "I still really hope that we could get married someday."

        print """Want to try again?"""
        yes = set(['yes','y', 'ye', ''])
        no = set(['no','n'])

        choice = raw_input().lower()
        if choice in yes:
           myScript()
        elif choice in no:
           sys.exit(0)
        else:
           sys.stdout.write("Please respond with 'yes' or 'no'")
        myScript()

myScript()

rawDescription: I there a python function to reset the script after it's run?


code:
def selector(seqs, picks):
  iters = [iter(x) for x in seqs]
  for choice in picks:
    yield next(iters[choice])

print list(selector((news, post), (0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0)))

rawDescription: Combine two Django queries in a certain order


code:
def duplicate_names(self, cr, uid, context=None):
    # Note: context not propagated for brevity of example
    groups = self.read_group(cr, uid, [], ['name'], ['name'])
    duplicate_names = [g['name'] for g in groups if g['name_count'] > 1]
    print "Duplicates names:", duplicate_names

    if duplicate_names:
        # It's easy to find out the IDs of duplicate records for each name,
        # here is for the first one
        duplicate_ids = self.search(cr, uid, [('name', '=', duplicate_names[0])])
        print "Duplicate IDs for %r: %s" % (duplicate_names[0], duplicate_ids)

rawDescription: OpenERP : fetch record with same name

description: OpenERP : fetch record with same name


code: print dt.strftime("%d-%m-%Y %H:%M:%S")

rawDescription: Python DateUtil Converting string to a date and time


code:
#!/usr/bin/env python

import subprocess
import sys

if len(sys.argv) == 8:
    # assume this was passed to git; we can of course do
    # some parsing to check if we got valid git style args
    args = [sys.argv[2], sys.argv[5]]
elif len(sys.argv) == 3:
    args = sys.argv[1:]
else:
    print "Not a valid number of args (2 or 7) to this diff program"
    sys.exit(1)
print "Files: ", args
for filename in args:
    filetype = subprocess.check_output(['file', filename])
    if filetype.find('text') == -1:
        args.insert(0, 'diff')
        print "A binary file was found: " + filename + ", deferring to diff"
        #sys.stdout.flush()
        subprocess.call(args)
        sys.exit(0)

rawDescription: Git diff is complaining, "external diff died, stopping at ... " with my python diff program


code:
d6 = Die() #create die 1 with the default side of 6
d4 = Die(4) #create die 2 with 4 sides
d12 = Die(12) #create die 3 with 12 sides

roll1 = d6.roll_die()
roll2 = d4.roll_die()
roll3 = d12.roll_die()

print "%d\n%d\n%d\nsum = %d" % (roll1, roll2, roll3, roll1 + roll2 + roll3)

rawDescription: Understanding Python Class instances

description: Understanding Python Class instances


code:
p=[2,]
number=3
while len(p)<35:
    if all(number%i!=0 for i in p):
        p.append(number)
    number += 1
print p

rawDescription: Prime numbers: Why is this code giving me some composite numbers?


code:
import bisect

def get_interval(x):
    intvals = [5000, 7500, 10000, 20000, 30000, 40000, 50000]
    i = bisect.bisect_right(intvals,x)
    return intvals[i-1:i+1]

print get_interval(5500)

"""
>>>
[5000, 7500]
"""

rawDescription: Python: Round to next predefined integer in list


code:
result = regex.search(line)
print result.group()

rawDescription: Can I print what string format was matched from a multiple-string, Regex search

description: Print what string format was matched from a multiple-string, Regex search


code:
import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get(url).text)

tables = soup.find_all('table') # this returns 8 tables
print len(tables)

for table in tables:
    for tr in table.find_all('tr'):
        print tr.text
        print

rawDescription: How to not repeat yourself by finding all tables in HTML

description: Not repeat yourself by finding all tables in HTML


code:
import arcpy
from arcpy import env
import numpy

# Setup workspace
arcpy.env.workspace=r"C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\Minimize_DEM_Analysis\PolyConvert\out_prj.gdb"
# Enable overwriting
arcpy.env.overwriteOutput=True

# final output text file to be created
filename=r"C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\Minimize_DEM_Analysis\PolyConvert\areatable_Sorted.txt"

areaColumn=[] #create an empty list where area data can be stored (ends up being 1x336 after completing the first for loop)
areaset=set()

#===============================================================================
# get a list of all feature classes in env.workspace
fcs=arcpy.ListFeatureClasses("*")
fcCount=len(fcs)  #print a count of the number of feature classes in the gdb
print fcCount
# feature class column where area in km2 is stored
areafield="Area_km"

fcs.sort()
for fc in fcs:                       # loop through each feature class
    print fc
    rows = arcpy.SearchCursor(fc)    # list all rows in the feature class
    for row in rows:                 # loop through each row in the feature class
        area=row.getValue(areafield) # get the value stored in the field, "Area_km"
        print area
        print 'appending area'
        if area not in areaset:
            areaColumn.append(area)  # add the value to AreaColumn array
            print'adding area'
            areaset.add(area)
            print areaColumn

print'convert areaColumn list to an array'
initial=numpy.array(areaColumn,dtype=float) # convert the 1x336 list to a 1x336 array
print initial                               # print the new array, "initial"
initial.shape

print'resizing to 16x21 array'
finalArea=numpy.resize(initial,(16,21))     # resize the 1x336 array to a 16x21 array
print finalArea                             # print the resized array, "finalarea"

print 'saving finalarea array'
numpy.savetxt(filename,finalArea)           # save "finalarea" array to a text file

rawDescription: Turn a 1x336 array into a 16x21 array

description: Turn a 1x336 array into a 16x21 array


code:
def clean_dir(directory):

    os.chdir(directory)

    for filename in os.listdir(directory):
        clean_file(filename)

def clean_file(filename):

    tag_black_list = ['iframe', 'script']
    tag_white_list = ['p','div']
    attr_white_list = {'*': ['title']}

    with open(filename, 'r') as fhandle:
        text = BeautifulSoup(fhandle)
        text.encode("utf-8")

        # Step one, with BeautifulSoup: Remove tags in tag_black_list, destroy contents.
        [s.decompose() for s in text(tag_black_list)]
        pretty = (text.prettify())

        # Step two, with Bleach: Remove tags and attributes not in whitelists, leave tag contents.
        cleaned = bleach.clean(pretty, strip="TRUE", attributes=attr_white_list, tags=tag_white_list)

        # this appends -cleaned to the file;
        # relies on the file having a '.'
        dot_pos = filename.rfind('.')
        cleaned_filename = '{0}-cleaned{1}'.format(filename[:dot_pos], filename[dot_pos:])

        with open(cleaned_filename, 'w') as fout:
            fout.write(cleaned.encode("utf-8"))

    print "Done"

rawDescription: Parse each file in a directory with BeautifulSoup/Python, save out as new file


code:
liPos = [(2,5),(8,9),(18,22)]
s = "I hope that I will find an answer to my question!"

exclusions = set().union(* (set(range(t[0], t[1]+1)) for t in liPos) )
pruned = ''.join(c for i,c in enumerate(s) if i not in exclusions)

print pruned

rawDescription: python string slicing with a list

description: Python string slicing with a list


code:
#!/usr/bin/python
import operator
import matplotlib.pyplot as plt
import numpy as np

l=[]
with open("testdata") as f:
    line = f.next()
    f.next()# skip headers
    nat = int(line.split()[0])
    print nat

    for line in f:
        # store striped line and only store if there is data on the line.
        cleaned = line.strip()
        if cleaned:
            # convert to float and remove characters in first index
            l.append(map(float,cleaned.split()[1:]))

b = 0
a = 1

# create a list to store our calculations in
distances = []
num_vects = len(l)

for b in range(num_vects-1):
    for a in range(b+1,num_vects):
        vector1 = (l[b][0],l[b][1],l[b][2])
        vector2 = (l[a][0],l[a][1],l[a][2])

        x = vector1
        y = vector2
        vector3 = list(np.array(x) - np.array(y))

        dotProduct = reduce( operator.add, map( operator.mul, vector3, vector3))

        dp = dotProduct**.5

        # store individual data point into the list of calculated distances
        distances.append(dp)

# plot histogram
num_bins = 200 # <- number of bins for the histogram

# store useful data returned by the histogram function
(n, bins, patches) = plt.hist(distances, num_bins)
plt.show()

rawDescription: How to make bins and histograms

description: Make bins and histograms


code:
query = prefetch(Dungeon.select(), Monster.select())
for dungeon in query:
    print dungeon.name
    for monster in dungeon.monsters_prefetch:  # This is a list of monsters
        print '    *', monster.name

rawDescription: How to add a list of joined data in peewee?

description: Add a list of joined data in peewee


code:
def write_config(project_abs_path):
    global WWW_DOCUMENT_ROOT
    print WWW_DOCUMENT_ROOT

    WWW_DOCUMENT_ROOT = 'A'

rawDescription: strange visibility scope behaviour in python

description: Strange visibility scope behaviour in python


code:
while len(pkt) > 0:

    print "Waiting.."
    pkt = sock.recv(2048)
    print "received"

    if(len(pkt)) > 54:
        ethHeader = pkt[0:14]
        ipHeader = pkt[14:34]
        tcpHeader = pkt[34:38]

        ethH = struct.unpack("!6s6s2s",ethHeader)
        ethdata = processEth(ethH)

        ipH = struct.unpack("!12s4s4s",ipHeader)
        ipdata = processIP(ipH)

        tcpH = struct.unpack("!HH16", tcpHeader)
        tcpdata = processTCP(tcpH)

        print "S.mac "+ethdata[0]+" D.mac "+ethdata[1]+"     from:  "+ipdata[0]+":"+tcpdata[0]+"    to:  "+ipdata[1]+":"+tcpdata[1]
        #time.sleep(1);

    else:
        continue

rawDescription: Raw socket python packet sniffer

description: Raw socket python packet sniffer


code:
import urllib2
import time

first = 1
last = 2622

for i in range(first, last+1):
    time.sleep(5) # Be nice to the server! And avoid being blocked.
    for ext in ['png', 'gif']:
        # Make sure that the img dir exists! If not, the script will throw an
        # IOError
        with open('img/{}.{}'.format(i, ext), 'wb') as ifile:
            try:
                req = urllib2.Request('http://www.questionablecontent.net/comics/{}.{}'.format(i, ext))
                req.add_header('user-agent', 'Mozilla/5.0')
                ifile.write(urllib2.urlopen(req).read())
                break
            except urllib2.HTTPError:
                continue
    else:
        print 'Could not find image {}'.format(i)
        continue
    print 'Downloaded image {}'.format(i)

rawDescription: Downloading Webcomic Saving Blank Files

description: Downloading Webcomic Saving Blank Files


code:
from itertools import chain
from collections import Counter
l =  [[1, 2, 7], [1, 2, 3], [1, 2, 3, 7], [1, 2, 3, 5, 6, 7]]
#use chain(*l) to flatten the lists into a single list
c = Counter(chain(*l))
print c
#sort keys in order of descending frequency
sortedValues = sorted(c.keys(), key=lambda x: c[x], reverse=True)
#show the four most common values
print sortedValues[:4]
#alternatively, show the values that appear in more than 50% of all lists
print [value for value, freq in c.iteritems() if float(freq) / len(l) > 0.50]

rawDescription: extracting item with most common probability in python list

description: Extracting item with most common probability in python list


code:
from PyQt4 import QtCore, QtGui, QtWebKit

class WebViewCreator(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.view = QtWebKit.QWebView(self)
        self.view.loadFinished.connect(self.load_finished)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)
        self.view.load(QtCore.QUrl('http://stackoverflow.com/'))

    def load_finished(self, ok):
        print ok

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = WebViewCreator()
    window.show()
    sys.exit(app.exec_())

rawDescription: PyQt QtWebKit loadFinished not called

description: PyQt QtWebKit loadFinished not called


code:
def enter(self):
    print "What's up? Get in the Bran Muffin car!"

    action = raw_input(">  ")

    if(action == "shut up chip" or action == "go away dude" or action == "screw you"):
        print "Screw you!"
        print action
        return next_scene('Your_death')
        #return 'Death'
    elif action == "hi chip":
        print "What's up loser?!?! Let's go to O&A..."
        return next_scene('Chip_in_studio')
    else:
        print "what's wrong with you? Let's go to my mother's house, I think Lamar's there..."
        return 'Chip_mom_house'

rawDescription: Efficient approach for multiple 'or' statements in an conditional statement


rawDescription: Pybrain how to predict on new data

description: Pybrain how to predict on new data


code:
import praw

r = praw.Reddit(user_agent='top_comment_test')
subreddit = r.get_subreddit('opensource')
top = subreddit.get_top(params={'t': 'all'}, limit=25) # For a more potentially accurate set of top comments, increase the limit (but it'll take longer)
all_comments = []
for submission in top:
    submission_comments = praw.helpers.flatten_tree(submission.comments)
    #don't include non comment objects such as "morecomments"
    real_comments = [comment for comment in submission_comments if isinstance(comment, praw.objects.Comment)]
    all_comments += real_comments

all_comments.sort(key=lambda comment: comment.score, reverse=True)

top_comments = all_comments[:25] #top 25 comments

print top_comments

rawDescription: How can I make a list of the top comments in a subreddit with PRAW?

description: Make a list of the top comments in a subreddit with PRAW


code: print [procinfo for procinfo in procinfos]

rawDescription: I have a simple python dict why is a generator reference printed instead of the dict


code:
import cv, numpy                       # Comments added by me
mat = cv.CreateMat(3, 5, cv.CV_32FC1)  # make a 3x5 mat
cv.Set(mat, 7)                         # set the cells to 7
a = numpy.asarray(mat)                 # convert it to a numpy array
print a

rawDescription: How to convert CvMat to Numpy array?

description: Convert CvMat to Numpy array


code:
from zipfile import ZipFile

archive= ZipFile('path/to/zipfile.zip')
print [info.filename for info in archive.filelist if info.filename.endswith('.inf')][0]

rawDescription: Find first "inf" (fixed extension) file in zip


code:
import urllib2
from bs4 import BeautifulSoup

html = urllib2.urlopen('http://www.dtemaharashtra.gov.in/approvedinstitues/StaticPages/frmInstituteSummary.aspx?InstituteCode=1002').read()
soup = BeautifulSoup(html)
print soup.find(id='ctl00_rightContainer_ContentBox1_lblEMailAddress').text

rawDescription: getting email ids using beautifulsoup

description: Getting email ids using beautifulsoup


rawDescription: How do I un-escape a backslash-escaped string in python?

description: Un-escape a backslash-escaped string in python


rawDescription: Flatten a nested array but also append information from another array in python


code:
with open('file') as f:
    for line in f.readlines():
        l = line.split()
        if l and l[4] == 'Q9C0F0':
            print l[5][1:-1] # or parse int in a more inteligent way

rawDescription: parsing line capture value in the ttoken

description: Parsing line capture value in the ttoken


code:
import datetime
lastplus = q.get()
if lastplus.date < datetime.datetime.now()-datetime.timedelta(seconds=20):
    print "Go"

rawDescription: How to check if datetime is older than 20 seconds

description: Check if datetime is older than 20 seconds


code:
import random

def die_roller(throws):
    """generates throws rolls of a die"""
    for _ in xrange(throws):
        yield random.randint(1, 6)

roller = die_roller(10)
print type(roller)      # => <type 'generator'>
print list(roller)      # => [6, 6, 3, 1, 6, 3, 1, 5, 4, 4]
print sum(roller)       # roller was exhausted, generates null list thus 0 sum

big_roller = die_roller(10**5)
print sum(big_roller)   # => 3500238

rawDescription: reuse generators

description: Reuse generators


code:
from lxml import etree
from io import StringIO, BytesIO

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

def node_location(node):
    position = len(node.xpath('./preceding-sibling::' + node.tag)) + 1
    return '/' + node.tag + '[' + str(position) + ']'

def node_path(node):
    nodes = mynode.xpath('./ancestor-or-self::*')
    return ''.join( map(node_location, nodes) )

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

xml = """
<a1>
    <b1>
        <c1>content1</c1>
    </b1>
    <b1>
        <c1>content2</c1>
    </b1>
</a1>"""

root = etree.fromstring(xml)

for mynode in root.xpath('//c1'):
    print node_path(mynode)

rawDescription: XQuery absolute path in python lxml

description: XQuery absolute path in python lxml


code:
for instance in session.query(User).order_by(User.id):
    print instance.name, instance.fullname

rawDescription: Retrieving the results from a table using SQLAlchemy


code:
import sys
import os
from qgis.core import *
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

LATITUDE = 1.29306
LONGITUDE = 103.856

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

layer=QgsVectorLayer("/home/shubham/SMART/zones/mtz1092p.shp", "mtz1092p", "ogr")
if not layer.isValid():
    print "Layer failed to load!"
provider = layer.dataProvider()

def findFeatureId(point):
    feat = QgsFeature()
    allAttrs = provider.attributeIndexes()
    provider.select(allAttrs)
    while provider.nextFeature(feat):
        geom = feat.geometry()
        x = geom.asPolygon()
        if len(x) == 0:
            print "Feature ID %d has no ring" % feat.id()
        else:
            codes = []
            codes.append(Path.MOVETO)
            for i in range (0, len(x[0]) - 2):
                codes.append(Path.LINETO)
            codes.append(Path.CLOSEPOLY)
            path = Path(x[0], codes)
            if (path.contains_point(point, None, 0.0)):
                print "Point contained in feature ID %d" %feat.id()

if __name__ == "__main__":
    crsSrc = QgsCoordinateReferenceSystem(4326) # WGS84
    crsDest = QgsCoordinateReferenceSystem(3414)# SVY21
    xform = QgsCoordinateTransform(crsSrc, crsDest)
    pt = xform.transform(QgsPoint(LONGITUDE, LATITUDE))
    findFeatureId(pt)

rawDescription: QGis: Finding the polygon inside which a point lies


code:
set1=set(line[:6] for line in open('file1.txt'))
set2=set(line[:6] for line in open('file2.txt'))
not_found = set1 - set2
if not_found:
    print "Some keys not found: " + ', '.join(not_found)

rawDescription: Search a file for strings from a second file


rawDescription: how to split sentences with multiple splitting parameters and including split elements

description: Split sentences with multiple splitting parameters and including split elements


code:
from matplotlib import pyplot as pp

class FigEventLoopDemo(object):

    def __init__(self):

        self.fig,self.ax = pp.subplots(1,1,num='Event loop demo')
        self.clickme = self.ax.text(0.5,0.5,'click me',ha='center',va='center',color='r',fontsize=20,picker=10)

        canvas = self.fig.canvas

        # add a callback that triggers when the text is clicked
        self.cid = canvas.mpl_connect('pick_event',self.on_pick)

        print "entering a blocking loop"

        # start a blocking event loop
        self.fig.canvas.start_event_loop(timeout=-1)

    def on_pick(self,event):

        if event.artist is self.clickme:

            # exit the blocking event loop
            self.fig.canvas.stop_event_loop()

            print "now we're unblocked"

rawDescription: matplotlib: deliberately block code execution pending a GUI event

description: Matplotlib: deliberately block code execution pending a GUI event


code:
from multiprocessing import Process
import time

def f(name):
    time.sleep(1)
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.terminate() # <---
    p.join()

rawDescription: Difference between join and terminate

description: Difference between join and terminate


code:
def d(msg='my default message'):
    def decorator(func):
        def newfn(request, **kwargs):
            print msg
            return func(request, **kwargs)
        return newfn
    return decorator

rawDescription: Django decorators with arguments for multiple views


code:
from fabric.api import *
print "Hello"

def deploy():
    with settings(host_string="Remote", user = "ubuntu", key_filename="/home/ubuntu/key.pem"):
        put('/home/localuser/sample.sh', '/home/ubuntu/')
        run('bash /home/ubuntu/sample.sh')

if __name__ == '__main__':
   deploy()

rawDescription: Running Fabric with Python script together


rawDescription: script that counts the fraction of Alphabet (a, b, c) every 5 lines

description: Script that counts the fraction of Alphabet (a, b, c) every 5 lines


code:
import sys
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.button = QtGui.QPushButton('Test')
        self.edit = QtGui.QTextEdit()
        layout.addWidget(self.edit)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.handleTest)

    def handleTest(self):
        tab = "\t"
        cursor = self.edit.textCursor()

        start = cursor.selectionStart()
        end = cursor.selectionEnd()

        cursor.setPosition(end)
        cursor.movePosition(cursor.EndOfLine)
        end = cursor.position()

        cursor.setPosition(start)
        cursor.movePosition(cursor.StartOfLine)
        start = cursor.position()
        print cursor.position(), end

        while cursor.position() < end:
            cursor.movePosition(cursor.StartOfLine)
            cursor.insertText(tab)
            end += tab.count()
            cursor.movePosition(cursor.EndOfLine)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

rawDescription: How to indent region in a PyQt TextEdit

description: Indent region in a PyQt TextEdit


code:
A = [1, 4, 5, 8, 10, 13]    # in sorted order
Aset = set(A)

for d in range(1, 13):
    already_seen = set()
    for a in A:
        if a not in already_seen:
            b = a
            count = 1
            while b + d in Aset:
                b += d
                count += 1
                already_seen.add(b)
            # if there is a hole to jump over:
            if b + 2 * d in Aset:
                b += 2 * d
                count += 1
                while b + d in Aset:
                    b += d
                    count += 1
                    # don't record in already_seen here
            print "found %d items in %d .. %d" % (count, a, b)
            # collect here the largest 'count'

rawDescription: Longest arithmetic progression with a hole