code:
from datetime import datetime, timedelta
import xlrd

book = xlrd.open_workbook(filename='test.xls')
sheet = book.sheet_by_name('Sheet1')

date = sheet.cell_value(0, 0)
datetime_value = datetime(*xlrd.xldate_as_tuple(date, 0))

print datetime_value  # prints 2003-11-03 04:03:00
print datetime_value.time()  # 04:03:00
print datetime_value - timedelta(hours=1)  # prints 2003-11-03 03:03:00

rawDescription: Python Excel date/time read in issue

description: Python Excel date/time read in issue


code:
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.button = QtGui.QPushButton('Select Files', self)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.handleButton)

    def handleButton(self):
        title = self.button.text()
        for path in QtGui.QFileDialog.getOpenFileNames(self, title):
            print path

if __name__ == '__main__':

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

rawDescription: PyQt: How can I get a large list of filenames from the user?


code:
def clearscreen(numlines=100):
    """Clear the console.
    numlines is an optional argument used only as a fall-back.
    """
    import os
    if os.name == "posix":
        # Unix/Linux/MacOS/BSD/etc
        os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
        # DOS/Windows
        os.system('CLS')
    else:
        # Fallback for other operating systems.
        print '\n' * numlines

rawDescription: Deleting already printed in Python

description: Deleting already printed in Python


code:
def remove_empties(list1):
    """removes any empty lists from our list"""
    list2 = [x for x in list1 if x]
    return list2

def merge_to_couples(new_list):
    answer = []
    new_list = remove_empties(new_list)

    while True:
        answer.append([new_list[0][0], new_list[1][0]])
        del new_list[0][0], new_list[1][0]

        new_list = remove_empties(new_list) #remove empty lists every iteration

        if len(new_list) == 0: #if length is 0, our work is done
            return answer
        if len(new_list) == 1: #if length is 1, we need to append remaining numbers as lists with length 1
            for i in new_list[0]:
                answer.append([i])
            return answer

A_list = [1,2,3]
B_list = [4]
C_list = [5,6,7,8]
D_list = [9,10]

new_list = [A_list, B_list, C_list, D_list]

print merge_to_couples(new_list)

rawDescription: Making 4 lists into a 2 element nested list


code:
import sys, cmd

class Std(object):
    def __getattribute__(self, name):
        if name in ('__getattribute__', '__setattr__'):
            return object.__getattribute__(self, name)
        else:
            return getattr(sys.__stdout__, name)

    def __setattr__(self, name, value):
        setattr(sys.__stdout__, name, value)

class HelloWorld(cmd.Cmd):
    FRIENDS = [ 'Alice', 'Adam', 'Barbara', 'Bob' ]

    def do_greet(self, person):
        "Greet the person"
        if person and person in self.FRIENDS:
            greeting = 'hi, %s!' % person
        elif person:
            greeting = "hello, " + person
        else:
            greeting = 'hello'
        print greeting

    def complete_greet(self, text, line, begidx, endidx):
        if not text:
            completions = self.FRIENDS[:]
        else:
            completions = [f for f in self.FRIENDS
                           if f.startswith(text)]
        return completions

    def do_EOF(self, line):
        return True

if __name__ == '__main__':

    HelloWorld(stdout=Std()).cmdloop()

rawDescription: Python readline tab-completion in cmd.Cmd, when sys.stdout has been replaced


code:
from multiprocessing import Process
import sys

rocket = 0

def func1():
    global rocket
    print 'start func1'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func1'

def func2():
    global rocket
    print 'start func2'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func2'

if __name__=='__main__':
     p1 = Process(target = func1)
     p1.start()
     p2 = Process(target = func2)
     p2.start()

rawDescription: Python: Executing multiple functions simultaneously


code:
def cmdB():
     pass

def cmdC():
    pass

def cmdA():
    # start recording information
    print "recording started"
    #run cmdB
    cmdB()
    # run cmdC
    cmdC()
    # stop recording
    print "recording stopped"

if __name__ == '__main__':
    cmdA()

rawDescription: Python how to realize keeping cmdA executing till cmdB an cmdC finished


code:
#!/usr/bin/env python

from git import *

repo = Repo("misctools")
o = repo.remotes.origin
o.pull()

master = repo.head.reference
print master.log()

rawDescription: git-python get commit feed from a repository

description: Git-python get commit feed from a repository


code:
import urllib
print urllib.urlopen('http://www.example.com/blah').read()

rawDescription: How to get html tags from url?

description: Get html tags from url


code:
class FunctionLike(object):
    def __call__(self, a):
        print "I got called with %r!" % (a,)

fn = FunctionLike()
fn(10)

# --> I got called with 10!

rawDescription: Overload () operator in Python

description: Overload () operator in Python


code:
data = []
with open('data.txt') as f:
    for line in f:                   # loop over the rows
        fields = line.split()        # parse the columns
        rowdata = map(float, fields) # convert text to numbers
        data.extend(rowdata)         # accumulate the results
print 'Minimum:', min(data)
print 'Maximum:', max(data)

rawDescription: How to find min/max values from rows and columns in Python?

description: Find min/max values from rows and columns in Python


code:
for name in (data['name'],) if data['name'] else ():
    print name

rawDescription: Python style: more concise way to rename a variable while checking it is not null


code:
import sys
import socket
from multiprocessing import Process, Queue

UDP_ADDR = ("", 13000)

def send(m):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(m, UDP_ADDR)

def receive(q):
    buf = 1024
    Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    Sock.bind(UDP_ADDR)
    while True:
      (data, addr) = Sock.recvfrom(buf)
      q.put(data)

def client():
  q = Queue()
  r = Process(target = receive, args=(q,))
  r.start()

  print "client loop started"
  while True:
      m = q.get()
      print "got:", m
      if m == "stop":
          break
  print "loop ended"

  r.terminate()

if __name__ == '__main__':
  args = sys.argv
  if len(args) > 1:
    send(args[1])
  else:
    client()

rawDescription: How to run a thread more than once in python

description: Run a thread more than once in python


code:
from pyvisa.vpp43 import visa_library
visa_library.load_library("/Library/Frameworks/Visa.framework/VISA")
import visa
visa.get_instruments_list() # Yields: ['ASRL1', 'ASRL2', 'ASRL3', 'ASRL4', 'GPIB0::1']
temp = visa.Instrument("GPIB0::1")
print temp # Yeilds: Instrument("GPIB0::1::INSTR")

rawDescription: NI VISA + pyVisa on Mac OS X (Snow Leopard)


code:
class CountrySerializer(serializers.Serializer):
    country_geoname_id = serializers.CharField(required=True)
    country_code = serializers.CharField(source='iso', max_length=2L, required=True)
    country_name = serializers.CharField(max_length=64L, required=True)

rawDescription: Changing name of json attribute in serializer


code:
o=1
globals()
type(o)
def o():
    print 1

type(o)
globals()

rawDescription: How to choose between objects in Python

description: Choose between objects in Python


code:
choice = raw_input("Would you like to hit or stand?")
print choice
if choice == "hit":
    return hand + deck()
elif choice == "stand":
    return hand

rawDescription: python blackjack game, seems to be ignoring 'if' statement

description: Python blackjack game, seems to be ignoring 'if' statement


code:
def main():
    isTime = False
    newLists = []
    with open("time.txt", 'r') as newFile:
        result = newFile.readlines()
        for line in result:
            if "$TIME" in line:
                isTime = True
            elif isTime:
                newLists.append(line)

    for line in newLists:
        print line

if __name__ == '__main__':
    main()

rawDescription: extracting data from file python

description: Extracting data from file python


code:
import socket
import BaseHTTPServer
from oauth2client.tools import ClientRedirectServer, ClientRedirectHandler

port_number = 0
host_name = 'localhost'
try:
    httpd = ClientRedirectServer((host_name, port_number), ClientRedirectHandler)
except socket.error, e:
    print "socket error: " + str(e)
else:
    print "The server is running on: port " + str(httpd.socket.getsockname()[1])
    print "and host_name " + host_name
    httpd.serve_forever()

rawDescription: Python httpd will listen on used ports without throwing a socket error in windows but not OS X?


code:
parentdict = {100: None, 200: 100, 300: 200, 400: 100, 500: None, 600: 300}
def getParent(p_child):
    level = 1
    thisparent = parentdict[p_child]
    while thisparent is not None:
        thisparent = parentdict[thisparent]
        level += 1
    return level

levelsdict = {}
for thing in parentdict.keys():
    thinglevel = getParent(thing)
    if thinglevel in levelsdict:
        templist = levelsdict[thinglevel]
        templist.append(thing)
        levelsdict[thinglevel] = templist
    else:
        levelsdict[thinglevel] = [thing]

print levelsdict

{1: [100, 500],2: [200,400], 3: [300], 4: [600]}

rawDescription: Create number of lists based on hierarchy


code:
from subprocess import Popen
process = Popen(['sleep', '10'])
print 'Returned immediately!'

rawDescription: How to call a linux command in Python without waiting for the results (Linux)

description: Call a linux command in Python without waiting for the results (Linux)


code:
from random import randint

def RandNum(number):
    l = []
    for _ in range(number):
        l.append(randint(0,12))
    print " ".join(map(str, l))

RandNum(3)

rawDescription: Is there a better way to repeat code other than using def


code:
import re
teststring = """အေရာင္ဆန္းဆန္းေလး ေတြ ပါတဲ့ Enhancing Eyes shawdow palette ေလးပါ ။
Price - 17000 ks. Call 625555555"""

Numbers = re.findall("\d+", teststring)
Latin = re.findall("[A-Za-z]+", teststring)
Burmese = re.findall("[^A-Za-z0-9 ]+", teststring)
print Numbers, Latin,
print Burmese

rawDescription: Python parse text for latin alphabet

description: Python parse text for latin alphabet


code:
import json
import logging

from stompest.config import StompConfig
from stompest.sync import Stomp

class Handler(logging.Handler):
    def __init__(self, amq_uri, out_queue):
        logging.Handler.__init__(self)
        self.queue = queue
        self.uri = uri

    def emit(self, record):
        msg = self.format(record)
        cfg = StompConfig(self.uri)
        data = json.dumps(msg)
        client = Stomp(cfg)
        client.connect()

        try:
            client.send(self.queue, data)
        except Exception, exc:
            print "Error: ", exc
        client.disconnect()

def get_logger(uri, queue):
    logger = logging.getLogger('testlogger')
    logger.addHandler(Handler(uri, queue))

rawDescription: ActiveMQ message bus logging handler python


code:
variable = subprocess.Popen("<command>",stdout=subprocess.PIPE,shell=True)
print variable.stdout.read()

rawDescription: Store subprocess.call stdout in a variable and load it back as json


code:
a = [1,2,3,4]
b = [1,2]

print any(x in a for x in b)
True

rawDescription: Most efficient way of checking list membership of more that one value


code:
from paste.script import command

class TestCommand(command.Command):

    max_args = 1
    min_args = 1

    usage = "NAME"
    summary = "Say hello!"
    group_name = "My Package Name"

    parser = command.Command.standard_parser(verbose=True)
    parser.add_option('--goodbye',
                      action='store_true',
                      dest='goodbye',
                      help="Say 'Goodbye' instead")

    def command(self):
        name = self.args[0]
        if self.verbose:
            print "Got name: %r" % name
        if self.options.goodbye:
            print "Goodbye", name
        else:
            print "Hello", name

rawDescription: Modifying The Template For New Pylons Controllers


code:
x = np.random.randn(100, 100, 100)
print np.allclose(np.linalg.norm(x), np.sqrt(np.sum(np.square(x[:,:,:]))))
# True

rawDescription: looking for a 3D version of numpy.linalg.norm

description: Looking for a 3D version of numpy.linalg.norm


code:
def round_robin(units, sets = None):
    """ Generates a schedule of "fair" pairings from a list of units """
    count = len(units)
    sets = sets or (count - 1)
    half = count / 2
    for turn in range(sets):
        left = units[:half]
        right = units[count - half - 1 + 1:][::-1]
        pairings = zip(left, right)
        if turn % 2 == 1:
            pairings = [(y, x) for (x, y) in pairings]
        units.insert(1, units.pop())
        yield pairings

teams = ['a', 'b', 'c', 'd']
print list(round_robin(teams, sets = len(teams) * 2 - 2))

rawDescription: Generating natural schedule for a sports league


code:
#!/usr/bin/python
#

from pyPdf import PdfFileWriter, PdfFileReader

input1 = PdfFileReader(file("in.pdf", "rb"))
output = PdfFileWriter()

numPages = input1.getNumPages()
print "document has %s pages." % numPages

for i in range(numPages):
    page = input1.getPage(i)
    print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
    page.trimBox.lowerLeft = (25, 25)
    page.trimBox.upperRight = (225, 225)
    page.cropBox.lowerLeft = (50, 50)
    page.cropBox.upperRight = (200, 200)
    output.addPage(page)

outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()

rawDescription: Cropping pages of a .pdf file

description: Cropping pages of a .pdf file


code:
def get_cosine(c_eqn1, c_eqn2):
    _special_symbol = float(c_eqn1.count("*"))
    cos_result = 0
    try:
        eqn1_vector = Counter(get_word(c_eqn1))
        eqn2_vector = Counter(get_word(c_eqn2))
        _special_symbol = 0
        spe_list = list()
        # Storing number of * & the value contains *
        for _val in eqn1_vector.keys():
            if _val.__contains__("*"):
                _special_symbol += eqn1_vector[_val]
                spe_list.append(_val)
        if eqn2_vector.has_key("*"):
            _special_symbol -= eqn2_vector["*"]
    except Exception as exp:
        print "Exception at converting equation to vector", exp
        traceback.print_exc()
    else:
        intersection = set(eqn1_vector.keys()) & set(eqn2_vector.keys())
        numerator = sum([eqn1_vector[x] * eqn2_vector[x]
                         for x in intersection])
        non_intersection_sum = 0
        non_intersection_value = list()
        # storing no of non_matched value
        for _val in eqn2_vector.keys():
            if _val not in intersection:
                non_intersection_sum += eqn2_vector[_val]
                non_intersection_value.append(_val)
        # Join both non intercet lists
        if non_intersection_value:
            non_intersection_value.extend(spe_list)
        # If both non intersect value are not same
        # Empty the list
        if _special_symbol != non_intersection_sum:
            non_intersection_value = list()
        # Cosine similarity formula
        _sum1 = sum([eqn1_vector[x]**2 for x in eqn1_vector.keys() if x not in non_intersection_value])
        _sum2 = sum([eqn2_vector[x]**2 for x in eqn2_vector.keys() if x not in non_intersection_value])
        denominator = math.sqrt(_sum1) * math.sqrt(_sum2)
        if not denominator:
            cos_result = 0
        else:
            cos_result = float(numerator) / denominator
    return cos_result if cos_result <= 1.0 else 1

rawDescription: cosine similarity for latex code of an equation

description: Cosine similarity for latex code of an equation


code:
my_strings = ['aaaaaa', 'bbbb', 'ccc']

for el in my_strings:
    print el[:3]

rawDescription: Python print first three characters of string in list using a for loop


code:
path = os.path.expanduser('~/Desktop/test.txt')
print os.path.exists(path)

rawDescription: Errors with path beginning with "~" in Python


code:
import re
s = '2011 Company'
removed = re.sub('(^\d{4})|(\d{4}$)','',s).strip()
print removed

rawDescription: Regular Expression to remove a copyright


code:
import os
from Bio import PDB

class ChainSplitter:
    def __init__(self, out_dir=None):
        """ Create parsing and writing objects, specify output directory. """
        self.parser = PDB.PDBParser()
        self.writer = PDB.PDBIO()
        if out_dir is None:
            out_dir = os.path.join(os.getcwd(), "chain_PDBs")
        self.out_dir = out_dir

    def make_pdb(self, pdb_path, chain_letters, overwrite=False, struct=None):
        """ Create a new PDB file containing only the specified chains.

        Returns the path to the created file.

        :param pdb_path: full path to the crystal structure
        :param chain_letters: iterable of chain characters (case insensitive)
        :param overwrite: write over the output file if it exists
        """
        chain_letters = [chain.upper() for chain in chain_letters]

        # Input/output files
        (pdb_dir, pdb_fn) = os.path.split(pdb_path)
        pdb_id = pdb_fn[3:7]
        out_name = "pdb%s_%s.ent" % (pdb_id, "".join(chain_letters))
        out_path = os.path.join(self.out_dir, out_name)
        print "OUT PATH:",out_path
        plural = "s" if (len(chain_letters) > 1) else ""  # for printing

        # Skip PDB generation if the file already exists
        if (not overwrite) and (os.path.isfile(out_path)):
            print("Chain%s %s of '%s' already extracted to '%s'." %
                    (plural, ", ".join(chain_letters), pdb_id, out_name))
            return out_path

        print("Extracting chain%s %s from %s..." % (plural,
                ", ".join(chain_letters), pdb_fn))

        # Get structure, write new file with only given chains
        if struct is None:
            struct = self.parser.get_structure(pdb_id, pdb_path)
        self.writer.set_structure(struct)
        self.writer.save(out_path, select=SelectChains(chain_letters))

        return out_path

class SelectChains(PDB.Select):
    """ Only accept the specified chains when saving. """
    def __init__(self, chain_letters):
        self.chain_letters = chain_letters

    def accept_chain(self, chain):
        return (chain.get_id() in self.chain_letters)

if __name__ == "__main__":
    """ Parses PDB id's desired chains, and creates new PDB structures. """
    import sys
    if not len(sys.argv) == 2:
        print "Usage: $ python %s 'pdb.txt'" % __file__
        sys.exit()

    pdb_textfn = sys.argv[1]

    pdbList = PDB.PDBList()
    splitter = ChainSplitter("/home/steve/chain_pdbs")  # Change me.

    with open(pdb_textfn) as pdb_textfile:
        for line in pdb_textfile:
            pdb_id = line[:4].lower()
            chain = line[4]
            pdb_fn = pdbList.retrieve_pdb_file(pdb_id)
            splitter.make_pdb(pdb_fn, chain)

rawDescription: How to extract chains from a PDB file?

description: Extract chains from a PDB file


code:
import hashlib
hash = hashlib.md5()
t = (1, 2, 3, 4, 5) # whatever
t_as_string = str(t)
hash.update(t_as_string)
print hash.hexdigest()

rawDescription: identify 5-tuple flows by hash value in Python

description: Identify 5-tuple flows by hash value in Python


code:
import re,urllib2
def find_words(each_func):
    i=0
    wordsineach_func=[]
    while len(each_func) >0:
        i=i+1
        word_found=longest_word(each_func)
        if len(word_found)>0:
            wordsineach_func.append(word_found)
            each_func=each_func.replace(word_found,"")
     #   print i,word_found,each
    return wordsineach_func

def longest_word(phrase):
    phrase_length=len(phrase)
    words_found=[];index=0
    outerstring=""
    while index < phrase_length:
        outerstring=outerstring+phrase[index]
        index=index+1
        if outerstring in words or outerstring.lower() in words:
            words_found.append(outerstring)
    if len(words_found) ==0:
        words_found.append(phrase)
    return max(words_found, key=len)

data = urllib2.urlopen('https://s3.amazonaws.com/hr-testcases/479/assets/words.txt')
words=[]
for line in data:
    words.append(line.replace("\n",""))
string="#honesthournow20"
string=string.replace("#","")
new_words=re.split(r'(\d+)',string)
output=[]
for each in new_words:
    each_words=find_words(each)
    for each_word in each_words:
        output.append(each_word)
print output

rawDescription: Checking the language of extracted trends from twitter


code:
def f(x, a):
    return x*x - a

def fp(x):
    return 2.*x

def A(a):
    xprev = 0.
    x = 1.
    n = 0
    while x != xprev:
        xprev = x
        x = (x * fp(x) - f(x,a)) / fp(x)
        n += 1
        if n >100:
            return n, x
    return n, x

def B(a):
    xprev = 0.
    x = 1.
    n = 0
    while x != xprev:
        xprev = x
        dx = - f(x,a) / fp(x)
        x = x + dx
        n += 1
        if n >100:
            return n, x
    return n, x

import numpy as np
import matplotlib.pyplot as plt

n = 100000

aa = 1. + 9. * np.random.random(n)

data_A = np.zeros((2, n))
data_B = np.zeros((2, n))

for i, a in enumerate(aa):
    data_A[:,i] = A(a)
    data_B[:,i] = B(a)

bins = np.linspace(0, 110, 12)

hist_A = np.histogram(data_A, bins=bins)
hist_B = np.histogram(data_B, bins=bins)
print "A: n<10: ", hist_A[0][0], " n>=100: ", hist_A[0][-1]
print "B: n<10: ", hist_B[0][0], " n>=100: ", hist_B[0][-1]

plt.figure()
plt.subplot(1,2,1)
plt.scatter(aa, data_A[0])
plt.subplot(1,2,2)
plt.scatter(aa, data_B[0])
plt.show()

rawDescription: Different implementations of Newton's method in floating point arithmetic


code:
h5f = h5py.File('test.h5', 'w')
h5f.create_dataset('array1', data=np.array([1,2,3,4]))
h5f.create_dataset('array2', data=np.array([5,4,3,2]))
h5f.close()

# Now open it back up and read data
h5f = h5py.File('test.h5', 'r')
a = h5f['array1'][:]
b = h5f['array2'][:]
h5f.close()
print a
print b
# [1 2 3 4]
# [5 4 3 2]

rawDescription: Numpy savez / load thousands of arrays, but not in one step


code:
import itertools

digits = [0,1,2,3,4,5,6,7,8,9]
oom = [ 10 ** i for i, digit in enumerate( digits ) ][ ::-1 ] # orders of magnitude
allperm = itertools.permutations( digits )

firstpass = ( sum( a * b for a, b in zip( perm, oom ) ) for perm in allperm if perm[ 0 ] )

print sum( i for i in firstpass if i % 7 == 0 )

rawDescription: iterating from a tuple of tuples optimizations

description: Iterating from a tuple of tuples optimizations


code:
steps = {1:"val1", 5:"val2", 2:"val3"}
for key in sorted(steps):
    print steps[key]

rawDescription: python: iterate over dictionary sorted by key

description: Python: iterate over dictionary sorted by key


code:
class Wrapper(object):
    def __init__(self, obj):
        self._wrapped_obj = obj
    def __getattr__(self, obj):
        print 'getattr', obj
        return getattr(self._wrapped_obj, attr)

dir(Wrapper({}))

rawDescription: What kind of python magic does dir() perform with __getattr__?


code:
import urllib2, BeautifulSoup

opener = urllib2.build_opener()
opener.addheaders[0] = ('User-agent', 'Mozilla/5.1')

response = opener.open('http://www.whitepages.com/carrier_lookup?carrier=other&number_0=1112223333&response=1').read()

bs = BeautifulSoup.BeautifulSoup(response)
print bs.findAll('div', attrs={'class': 'carrier_result'})[0].next.strip()

rawDescription: How do I print a line following a line containing certain text in a saved file in Python?

description: Print a line following a line containing certain text in a saved file in Python


code:
class A:
    foo = None
    foo_set_flag = False

    def __init__(self, arg_foo):
        if not A.foo_set_flag:
            A.foo = arg_foo
            A.foo_set_flag = True

a=A(12)
b=A(13)
print a.foo # -> 12
print b.foo # -> 12

rawDescription: How to initialise a class variable with __init__()?

description: Initialise a class variable with __init__()


code:
td = "0:01:23.415000"
time = td.split(':')
time_string = ''
if int(time[0]):
    time_string += time[0]
    if int(time[0]) > 1:
        time_string += " hours, "
    else:
        time_string += " hour, "
if int(time[1]):
    time_string += str(time[1])
    if int(time[1]) > 1:
        time_string += " minute, "
    else:
        time_string += " minutes, "
if float(time[2]):
    time_string += str(int(float(time[3])))
    if int(float(time[2])) > 1:
        time_string += " second"
    else:
        time_string += " seconds"
print time_string

rawDescription: Django deltatime formatting

description: Django deltatime formatting


code:
for item in d.values():
    if 'the' in item:
        print "value in dictionary"

rawDescription: Key with multiple values - check if value is in dictionary python


code:
def on_next2_clicked(self,button):
    cmd = "My Command"
    proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    while True:
        line = proc.stdout.read(2)
        if not line:
            break
        self.fper = float(line)/100.0
        self.ui.progressbar1.set_fraction(self.fper)
        while Gtk.events_pending():
            Gtk.main_iteration()  # runs the GTK main loop as needed
    print "Done"

rawDescription: Window freezes after clicking of button in python GTK3


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

driver = webdriver.Firefox()
driver.get("https://ar.aeromexico.com/search/flight/RoundTrip/EZE/MEX/2014-12-10/2015-01-25/1/0/0?")

results = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'results')))
for price in results.find_elements_by_class_name('total-price'):
    print price.text.strip()

driver.close()

rawDescription: urllib downloads a different file than what I do manually

description: Urllib downloads a different file than what I do manually


code:
import csv
with open("propertyOutput.csv", "rb") as fp:
    reader = csv.DictReader(fp, skipinitialspace=True)
    for row in reader:
        print row

rawDescription: how to get data out of `dictreader` in python

description: Get data out of `dictreader` in python


code:
import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for", infile

rawDescription: Create thumbnail images for jpegs with python


code:
user_input = raw_input("prompt>")
print 'admin {'
for line in user_input.split('|'):
    print '"{0}";'.format(line.strip())
print '}'

rawDescription: split input into lines instead of limiting lines

description: Split input into lines instead of limiting lines


code:
import requests

payload = {'user' : 'username',
          'pass' : 'PaSSwoRd'}

r = requests.get(r'http://www.URL.com/home', data=payload)

print r.text

rawDescription: send post request python

description: Send post request python


code:
import gobject
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

OPATH = "/temp/Test"
IFACE = "temp.Test"
BUS_NAME = "temp.Test"

class Implementation(object):
    # This is the implementation shared by the methods exported on the bus
    def theX(self, arg):
        print arg

class MyService(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus.request_name(BUS_NAME, dbus.bus.NAME_FLAG_REPLACE_EXISTING)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)

class IfaceOne(MyService, Implementation):
    def __init__(self):
        MyService.__init__(self)

    @dbus.service.method(dbus_interface=IFACE + ".IfaceOne", in_signature='s')
    def X(self, arg):
        super(IfaceOne, self).theX(arg)

class IfaceTwo(IfaceOne, Implementation):
    def __init__(self):
        MyService.__init__(self)

    @dbus.service.method(dbus_interface=IFACE + ".IfaceTwo", in_signature='s')
    def X(self, arg):
        super(IfaceTwo, self).theX(arg)

if __name__ == "__main__":
    iface_two = IfaceTwo()
    loop = gobject.MainLoop()
    loop.run()

rawDescription: Python D-Bus: custom d-bus method name with decorator


code:
a = 'ATGTATTATTAA'

codons = (a[n:n+3] for n in xrange(0,len(a),3)) # creates generator

dict_codons = {}

for codon in codons:
    if dict_codons.has_key(codon):
        dict_codons[codon] += 1
    else:
        dict_codons[codon] = 1

print dict_codons

rawDescription: How to count the number of codons in a frame using python (biopython)

description: Count the number of codons in a frame using python (biopython)


code:
import threading
import subprocess

class MyClass(threading.Thread):
    def __init__(self):
        self.stdout = None
        self.stderr = None
        threading.Thread.__init__(self)

    def run(self):
        p = subprocess.Popen('rsync -av /etc/passwd /tmp'.split(),
                             shell=False,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)

        self.stdout, self.stderr = p.communicate()

myclass = MyClass()
myclass.start()
myclass.join()
print myclass.stdout

rawDescription: Python Subprocess.Popen from a thread

description: Python Subprocess.Popen from a thread


code:
def shift_jis2unicode(charcode): # charcode is an integer
    if charcode <= 0xFF:
        shift_jis_string = chr(charcode)
    else:
        shift_jis_string = chr(charcode >> 8) + chr(charcode & 0xFF)

    unicode_string = shift_jis_string.decode('shift-jis')

    assert len(unicode_string) == 1
    return ord(unicode_string)

print "U+%04X" % shift_jis2unicode(0x8144)
print "U+%04X" % shift_jis2unicode(0x51)

rawDescription: get utf-8 character code given a shift-jis character code?

description: Get utf-8 character code given a shift-jis character code


code:
os.listdir("somedirectory")

filenames = next(os.walk(path))[2]

# Return everything under C:\Users\admin that contains a folder called wlp.
from glob import glob
glob('C:\Users\admin\*\wlp')

rawDescription: ReNaming files with python script

description: ReNaming files with python script


code:
import datetime
import collections
from time import strptime

date = datetime.date.today()
print "E K A D A S I  2 0 1 3 "
print "Today: {day} - {month} - {year}".format(
    day   = date.day,
    month = date.month,
    year  = date.year
)

dates = collections.OrderedDict()
with open('ekadasi.txt', 'r') as file_data:
    for line in file_data.readlines():
        if line:  # if line is not empty
            day, month, name1, name2 = line.split()
            temp = '{day}-{month}'.format(
                day = day,
                month = strptime(month, '%B').tm_mon
            )
            dates[temp] = str(name1 + name2)
print dates

rawDescription: python list of strings readen from file to dictionary

description: Python list of strings readen from file to dictionary


code:
from datetime import datetime
filetoread = "/home/scripts/gt/"
s = raw_input("Enter date please e.g: 130415: ")
date = datetime.strptime(s, '%y%m%d')
print date.strftime("%Y/%m/%d")
path = filetoread + s + ".txt"

rawDescription: Extract date from filename

description: Extract date from filename


code:
fun = 'test_fun'
arg = 'hi'
ulen = len(fun)
arglen = len(arg)
buflen = (2 + ulen) + (2 + arglen)
buf = pack('!B', 173)
buf += pack('<H', buflen)
buf += pack('!B', 0)
c.send(buf)

buf = pack('!B', ulen & 0xff)
buf += pack('!B', (ulen>>8) & 0xff)
buf += fun
buf += pack('!B', arglen & 0xff)
buf += pack('!B', (arglen>>8) & 0xff)
buf += arg
sent = c.send(buf)
print "sent", sent

rawDescription: Talk to a uwsgi RPC app from a non uwsgi process


code:
class TreeNode:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def find_max(node):
    if (node is None): return ""
    return max([find_max(node.left), node.val, find_max(node.right)], key=len)

tree = TreeNode("test", TreeNode("asdasjkdnaskdjasd", TreeNode("")), TreeNode("a"))
print find_max(tree) # asdasjkdnaskdjasd

rawDescription: Find the longest string in a binary string tree python


code:
# This is the first modification - importing the library
import datetime
import gviz_api

# page_template stays the same
# ...

def main():
  # Creating the data
  # Here we change the type of column "year" to "date"
  description = {"year": ("date", "Year"),
                 "sales": ("number", "Sales"),
                 "expenses": ("number", "Expenses")}

  # Here we switch out the string dates with an actual Python datetime.date
  # The conversion happens in the the subsequent functions, giving you a
  # date that is usable in the JS
  data = [{"year": datetime.date(2007,3,7), "sales": 1000, "expenses": 300},
          {"year": datetime.date(2009,6,11), "sales": 1200, "expenses": 400},
          {"year": datetime.date(2009,3,1), "sales": 1300, "expenses": 500},
          {"year": datetime.date(2010,8,6), "sales": 1401, "expenses": 600},
          {"year": datetime.date(2011,7,13), "sales": 1500, "expenses": 800}]
  # Loading it into gviz_api.DataTable
  data_table = gviz_api.DataTable(description)
  data_table.LoadData(data)

  # Creating a JavaScript code string
  jscode = data_table.ToJSCode("jscode_data",
                               columns_order=("year", "sales", "expenses"),
                               order_by="year")
  # Creating a JSon string
  json = data_table.ToJSon(columns_order=("year", "sales", "expenses"),
                           order_by="year")

  # Putting the JS code and JSon string into the template
  print "Content-type: text/html"
  print
  print page_template % vars()

if __name__ == '__main__':
  main()

rawDescription: Date format using gviz_api.py

description: Date format using gviz_api.py


code:
arr = [1,2,3,4,5,6]
i = 0
while i < len(arr):
    print arr[i]
    i = i + 1

rawDescription: while loop in django

description: While loop in django


code:
class Foo(int):
    def __init__(self, value=0):
        print self, type(self)
        self = 'foobar'
        print type(self)

foo = Foo(10)
print foo, type(foo)

rawDescription: Python class methods changing self

description: Python class methods changing self


code:
name = frozenset(string1.split(','))

if name.issubset(string.split(',')):
    print 'yes'
else:
    print 'no'

rawDescription: How to check issubset for the whole words separated by commas?

description: Check issubset for the whole words separated by commas


code:
for i in all_neighbors(graph, node):
    print i

rawDescription: What does "returns iterator" mean in python?


code:
def windowEnumerationHandler(self, hwnd, resultList):
    '''
    This is a handler to be passed to win32gui.EnumWindows() to generate
    a list of (window handle, window text) tuples.
    '''

    resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def bringToFront(self, windowText):
    '''
    Method to look for an open window that has a title that
    matches the passed in text. If found, it will proceed to
    attempt to make that window the Foreground Window.
    '''
    secondsPassed = 0
    while secondsPassed <= 5:
        # sleep one second to give the window time to appear
        wx.Sleep(1)

        print 'bringing to front'
        topWindows = []
        # pass in an empty list to be filled
        # somehow this call returns the list with the same variable name
        win32gui.EnumWindows(self.windowEnumerationHandler, topWindows)
        print len(topWindows)
        # loop through windows and find the one we want
        for i in topWindows:
            if windowText in i[1]:
                print i[1]
                win32gui.ShowWindow(i[0],5)
                win32gui.SetForegroundWindow(i[0])
        # loop for 5-10 seconds, then break or raise
        handle = win32gui.GetForegroundWindow()
        if windowText in win32gui.GetWindowText(handle):
            break
        else:
            # increment counter and loop again
            secondsPassed += 1

rawDescription: Simple Hotkey Script in Python - How to set a global hotkey to send a string of text?


code:
new_data_list_splitted = []
for i in data_list_splitted:
  if len(i) >= 40:
    ii = i.split(";")
    txt_line = "%s;%s;%s;%s;%s"%(ii[0],ii[1],ii[2],ii[3],ii[4])
    new_data_list_splitted.append(txt_line)
    txt_line = "%s;%s;%s;%s;%s"%(ii[4],ii[5],ii[6],ii[7],ii[8])
    new_data_list_splitted.append(txt_line)
  else:
    new_data_list_splitted.append(i)

print new_data_list_splitted #this will have a new row for measurement value

rawDescription: Split string and append parts in running list [Python]


code:
class SafeUncompressor(object):
    """Small proxy class that enables external file object
    support for uncompressed, bzip2 and gzip files. Works transparently, and
    supports a maximum size to avoid zipbombs.
    """
    blocksize = 16 * 1024

    class FileTooLarge(Exception):
        pass

    def __init__(self, fileobj, maxsize=10*1024*1024):
        self.fileobj = fileobj
        self.name = getattr(self.fileobj, "name", None)
        self.maxsize = maxsize
        self.init()

    def init(self):
        import bz2
        import gzip
        self.pos = 0
        self.fileobj.seek(0)
        self.buf = ""
        self.format = "plain"

        magic = self.fileobj.read(2)
        if magic == '\037\213':
            self.format = "gzip"
            self.gzipobj = gzip.GzipFile(fileobj = self.fileobj, mode = 'r')
        elif magic == 'BZ':
            raise IOError, "bzip2 support in SafeUncompressor disabled, as self.bz2obj.decompress is not safe"
            self.format = "bz2"
            self.bz2obj = bz2.BZ2Decompressor()
        self.fileobj.seek(0)

    def read(self, size):
        b = [self.buf]
        x = len(self.buf)
        while x < size:
            if self.format == 'gzip':
                data = self.gzipobj.read(self.blocksize)
                if not data:
                    break
            elif self.format == 'bz2':
                raw = self.fileobj.read(self.blocksize)
                if not raw:
                    break
                # this can already bomb here, to some extend.
                # so disable bzip support until resolved.
                # Also monitor http://stackoverflow.com/questions/13622706/how-to-protect-myself-from-a-gzip-or-bzip2-bomb for ideas
                data = self.bz2obj.decompress(raw)
            else:
                data = self.fileobj.read(self.blocksize)
                if not data:
                    break
            b.append(data)
            x += len(data)

            if self.pos + x > self.maxsize:
                self.buf = ""
                self.pos = 0
                raise SafeUncompressor.FileTooLarge, "Compressed file too large"
        self.buf = "".join(b)

        buf = self.buf[:size]
        self.buf = self.buf[size:]
        self.pos += len(buf)
        return buf

    def seek(self, pos, whence=0):
        if whence != 0:
            raise IOError, "SafeUncompressor only supports whence=0"
        if pos < self.pos:
            self.init()
        self.read(pos - self.pos)

    def tell(self):
        return self.pos

rawDescription: How to protect myself from a gzip or bzip2 bomb?

description: Protect myself from a gzip or bzip2 bomb


code:
mydict = {'spam': "Hello, world!"}
mydict['eggs'] = "Good-bye!"
variable_name = 'spam'
print mydict[variable_name]  # ==> Hello, world!
mydict[variable_name] = "some new value"
print mydict['spam']  # ==> "some new value"
print mydict['eggs']  # ==> "Good-bye!"

rawDescription: Alternatives to exec/eval?

description: Alternatives to exec/eval


rawDescription: accessing values from a nested dictionary

description: Accessing values from a nested dictionary


code:
#!/usr/bin/python

import sys
import subprocess

p = subprocess.Popen("""python -c '
from time import sleep ; import sys
for i in range(3):
    sleep(1)
    print "Hello", i
    sys.stdout.flush()
'""", shell = True, stdout = subprocess.PIPE)

while True:
    inline = p.stdout.readline()
    if not inline:
        break
    sys.stdout.write(inline)
    sys.stdout.flush()

print "Done"

rawDescription: Python: How to read stdout non blocking from another process?


code: print timeit.timeit("counter += 1", "counter = 0", number=100000000)

rawDescription: Algorithm timing in Python

description: Algorithm timing in Python


code:
print re.sub(r'\B@\b', r'', '@Alice @ home')
print re.sub(r'\B@\b', r'', 'foo @Alice @ home')

rawDescription: Regex to replace substring (if not the start of a word)


rawDescription: How can i split a list of lists, so that every list is on an own row?

description: Split a list of lists, so that every list is on an own row


code:
import re

swars = """Name:  Chewie  Race:  Wookie    Name:  Han Solo  Race: Human
Name:  Jabba the Hutt  Race: Hutt"""

print re.split('\s*(?=Name)', swars)
# prints: ['Name:  Chewie  Race:  Wookie', 'Name:  Han Solo  Race: Human', 'Name:  Jabba the Hutt  Race: Hutt']

rawDescription: re.split - Leave matched string in place?

description: Re.split - Leave matched string in place


code:
from sqlalchemy import select
from sqlalchemy.sql import and_

results = select([orm_obj.c.poi_id, orm_obj.c.poi_name])\
    .where(and_(orm_obj.c.id > 1, orm_obj.c.id < 100)).execute()
for id, name in results:
    print id, name

rawDescription: how to select only some columns in SQLAlchemy?

description: Select only some columns in SQLAlchemy


code:
import bisect
loi = [1, 8, 11, 20, 37, 66, 99, 120, 56000, 59001]
idx = bisect.bisect_right(loi,100)-1

if idx%2 == 0:
    print loi[idx]
else:
    print "not in a range"

rawDescription: python, find a range where a number belongs to, ranges were formed up from list of integers

description: Python, find a range where a number belongs to, ranges were formed up from list of integers


code:
from BeautifulSoup import BeautifulSoup

def removeTags(html, *tags):
    soup = BeautifulSoup(html)
    for tag in tags:
        for tag in soup.findAll(tag):
            tag.replaceWith("")

    return soup

testhtml = '''

        <title>Page title</title>

    text here
This is paragraph one.

This is paragraph two.

'''

print removeTags(testhtml, 'b', 'p')

rawDescription: Python: strip html from text data

description: Python: strip html from text data


code:
from multiprocessing import Pool

pool = Pool(processes=4)
result = pool.map_async(work, ("%d.txt"%n for n in xrange(1,21)))
print sum(result.get())

rawDescription: Batch processing on multiple cores

description: Batch processing on multiple cores


code:
import inspect, nosetests
print inspect.getsourcefile(nosetests)

rawDescription: Python importing a module: how to track its orgin PYTHONPATH, sys, os


code:
import numpy as np
import networkx as nx

array1 = np.arange(256)
array2 = np.arange(256) * 10

g = nx.Graph()

valdict = {}

for x in array1:
    g.add_node(x)
    valdict[x] = array2[x]

nx.set_node_attributes(g, 'value', valdict)

for i in array1:
    print g.nodes()[i], g.node[i]['value']

rawDescription: Creating key:attribute pairs in networkx for Python


code:
import string
import os
import re

def pdf_find_str(pdfname, str):
  f = open(pdfname, "rb")

  # read the file CHUNK_SIZE chars at a time, keeping last KEEP_SIZE chars
  CHUNK_SIZE = 2*1024*1024
  KEEP_SIZE = 3 * len(str) # each char might be in #ff form
  hexvals = "0123456789abcdef"

  ichunk = removed = 0
  chunk = f.read(CHUNK_SIZE)
  while len(chunk) > 0:

    # Loop to find all #'s and replace them with the character they represent.
    hpos = chunk.find('#')
    while hpos != -1:
      if len(chunk)-hpos >= 3 and chunk[hpos+1] in hexvals and chunk[hpos+2] in hexvals:
        hex = int(chunk[hpos+1:hpos+3], 16)  # next two characters are int value
        ch = chr(hex).lower()
        if ch in str: # avoid doing this if ch is not in str
          chunk = chunk[:hpos] + ch + chunk[hpos+3:]
          removed += 2
      hpos = chunk.find('#', hpos+1)

    m = re.search(str, chunk, re.I)
    if m:
      return ichunk * (CHUNK_SIZE-KEEP_SIZE) + m.start()

    # Transfer last KEEP_SIZE characters to beginning for next round of
    # testing since our string may span chunks.
    next_chunk = f.read(CHUNK_SIZE - KEEP_SIZE)
    if len(next_chunk) == 0: break
    chunk = chunk[-KEEP_SIZE:] + next_chunk

    ichunk += 1

  f.close()
  return -1

# On one file:
#if pdf_find_str("Consciousness Explained.pdf", "javascript") != -1:
#  print 'Contains "javascript"'

# Recursively on a directory:
for root, dirs, files in os.walk("Books"):
  for file in files:
    if file.endswith(".pdf"):
      position = pdf_find_str(root + "/" + file, "javascript")
      if position != -1:
        print file, "(", position, ")"
# Note: position returned by pdf_find_str does not account for removed
# characters from #ff representations (if any).

rawDescription: python to search within pdf file

description: Python to search within pdf file


code:
with open("food.txt") as f:
    res = []
    s=[]
    for line in f:
        line=line.rstrip()
        x=len(line)
        line=line.lstrip()
        indent = x-len(line)
        s=s[:indent/2]+[line]
        res.append("/".join(s))
    print res

rawDescription: Parsing a file with hierarchical structure in Python


code:
#Important assumption here... wordlist is sorted

base=wordlist[0]                      #consider the first word in the list
for word in wordlist:                 #loop through the entire list checking if
    if not word.startswith(base):     # the word we're considering starts with the base
        print base                    #If not... we have a new base, print the current
        base=word                     #  one and move to this new one
    #else word starts with base
        #don't output word, and go on to the next item in the list
print base                            #finish by printing the last base

rawDescription: Python- Remove all words that contain other words in a list


code:
class house:
    house_size = 100

class room:
    def __init__(self, house_object):
        self.house_object = house_object
        self.room_size = self.house_object.house_size / 10

    def __setattr__(self, key, value):
        self.__dict__[key] = value
        if key == "room_size":
            self.house_object.house_size = self.room_size * 10

house1 = house()
room1 = room(house1)

room1.room_size = 50
print house1.house_size

rawDescription: Object variable dependency in Python

description: Object variable dependency in Python


code:
#!/usr/bin/env python
import sys
import termios
import contextlib

@contextlib.contextmanager
def raw_mode(file):
    old_attrs = termios.tcgetattr(file.fileno())
    new_attrs = old_attrs[:]
    new_attrs[3] = new_attrs[3] & ~(termios.ECHO | termios.ICANON)
    try:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, new_attrs)
        yield
    finally:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, old_attrs)

def main():
    print 'exit with ^C or ^D'
    with raw_mode(sys.stdin):
        try:
            while True:
                ch = sys.stdin.read(1)
                if not ch or ch == chr(4):
                    break
                print '%02x' % ord(ch),
        except (KeyboardInterrupt, EOFError):
            pass

if __name__ == '__main__':
    main()

rawDescription: Key Listeners in python?

description: Key Listeners in python


code:
winw = 60
winh = 40
stepsize = 10
for i in range(num_scale_steps):
    if (winh + stepsize > 360) or(winw + stepsize > 240) :
        break
    print "in iteration %d " % i
    winw += stepsize
    winh += stepsize
    print "new width:%d"   % winw

rawDescription: break from for loop in python

description: Break from for loop in python


code:
def foo():
    print 'I am foo!'
def bar():
    pass
def baz():
    pass

funcs = {'func_a':foo, 'func_b':bar, 'func_c':baz}

funcs['func_a']()

rawDescription: Passing a list to eval()

description: Passing a list to eval()


code:
def ToSeoFriendly(s, maxlen):
    '''Join with dashes, eliminate punction, clip to maxlen, lowercase.

        >>> ToSeoFriendly("The quick. brown4 fox jumped", 14)
        'the-quick-brow'

    '''
    t = '-'.join(s.split())                                # join words with dashes
    u = ''.join([c for c in t if c.isalnum() or c=='-'])   # remove punctation
    return u[:maxlen].rstrip('-').lower()                  # clip to maxlen

if __name__ == '__main__':
    import doctest
    print doctest.testmod()

rawDescription: How can I create a SEO friendly dash-delimited url from a string in PYTHON?

description: Create a SEO friendly dash-delimited url from a string in PYTHON


code:
from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE

# assume delayed autostart isn't set
delayed = False

# registry key to query
key = OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\services\wuauserv')
try:
    delayed = bool(QueryValueEx(key, 'DelayedAutoStart')[0])
except WindowsError, e:
    print 'Error querying DelayedAutoStart key: {0}'.format(e)

print delayed

rawDescription: Python WMI query for Windows services with StartMode of "Automatic (Delayed Start)"


code:
import pycurl, json

STREAM_URL = "http://chirpstream.twitter.com/2b/user.json"

USER = "segphault"
PASS = "XXXXXXXXX"

class LimitError(Exception): pass

counter = 0
limit = 10
def on_receive(data):
    global counter
    if counter < 10:
        print data
        counter += 1
    else:
        raise LimitError
conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_receive)

try:
    conn.perform()
    print "Exited Normally"
except LimitError:
    print "Reached limit, exiting"
except pycurl.error:
    if counter == limit:
        print "pycurl expected error, nothing to worry about"
    else:
        raise
finally:
    conn.close()

print "All done"

rawDescription: How can I limit the number of curl calls to the write function?

description: Limit the number of curl calls to the write function


code:
#!/usr/bin/env python

from svm import *

# a three-class problem
labels = [0, 1, 1, 2]
samples = [[0, 0], [0, 1], [1, 0], [1, 1]]
problem = svm_problem(labels, samples);
size = len(samples)

kernels = [LINEAR, POLY, RBF]
kname = ['linear','polynomial','rbf']

param = svm_parameter(C = 10,nr_weight = 2,weight_label = [1,0],weight = [10,1])
for k in kernels:
    param.kernel_type = k;
    model = svm_model(problem,param)
    errors = 0
    for i in range(size):
    	prediction = model.predict(samples[i])
    	probability = model.predict_probability
    	if (labels[i] != prediction):
    		errors = errors + 1
    print "##########################################"
    print " kernel %s: error rate = %d / %d" % (kname[param.kernel_type], errors, size)
    print "##########################################"

param = svm_parameter(kernel_type = RBF, C=10)
model = svm_model(problem, param)
print "##########################################"
print " Decision values of predicting %s" % (samples[0])
print "##########################################"

print "Numer of Classes:", model.get_nr_class()
d = model.predict_values(samples[0])
for i in model.get_labels():
    for j in model.get_labels():
    	if j>i:
    		print "{%d, %d} = %9.5f" % (i, j, d[i,j])

param = svm_parameter(kernel_type = RBF, C=10, probability = 1)
model = svm_model(problem, param)
pred_label, pred_probability = model.predict_probability(samples[1])
print "##########################################"
print " Probability estimate of predicting %s" % (samples[1])
print "##########################################"
print "predicted class: %d" % (pred_label)
for i in model.get_labels():
    print "prob(label=%d) = %f" % (i, pred_probability[i])

print "##########################################"
print " Precomputed kernels"
print "##########################################"
samples = [[1, 0, 0, 0, 0], [2, 0, 1, 0, 1], [3, 0, 0, 1, 1], [4, 0, 1, 1, 2]]
problem = svm_problem(labels, samples);
param = svm_parameter(kernel_type=PRECOMPUTED,C = 10,nr_weight = 2,weight_label = [1,0],weight = [10,1])
model = svm_model(problem, param)
pred_label = model.predict(samples[0])

rawDescription: Any python Support Vector Machine library around that allows online learning?


code:
from Tkinter import *
import ttk

def plus(*args):
    value = float(a.get())
    value1 = float(b.get())
    result.set(value + value1)
    print "the result is " + str(result.get())
    root.destroy()             ####### look here !!!#######

root = Tk()

root.title("Plus them")

mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

a = StringVar()
b = StringVar()
result = StringVar()

feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))

feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))

ttk.Label(mainframe, text="the result is").grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=2, sticky=(W, E))

ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=3, sticky=W)

# for child in mainframe.winfo_children():
#     child.grid_configure(padx=5, pady=5)

feet_entry.focus()
root.bind('<Return>', plus)

root.mainloop()

rawDescription: Jump out in Tkinter loop

description: Jump out in Tkinter loop


code:
def days(t):
    oxford_comma = False
    a = [" day", " hour", " minute"]
    s = []
    if len(t) != 4:
        return "incorrect format"
    else:
        for i in range(3):
            if t[i] != 0:
                plural = "s" if t[i] > 1 else ""
                s.append(str(t[i]) + a[i] + plural + ", ")

        if len(s) > 1:
            if not oxford_comma:
                s[-2] = s[-2].replace(",","")
            s.insert(len(s)-1,"and ")
        return "".join(s)[:-2]

    print days((1,2,3,11))      #1 day, 2 hours, and 3 minutes
    print days((0,2,3,1))       #2 hours, and 3 minutes
    print days((0,0,3,11))      #3 minutes
    print days((1,1,1,11))      #1 day, 1 hour, and 1 minute

rawDescription: How to convert this duration into days/hours/minutes/seconds in Python/Django?

description: Convert this duration into days/hours/minutes/seconds in Python/Django


code: print '\n'.join(sorted(s.split('\n'), key=lambda x: int(x.split()[1])))

rawDescription: Best way to sort two results from string

description: Sort two results from string


code:
for news in soup.findAll('news'):
    posttitle = news['post_title']
    postdate = news['post_date']
    postcontent = news['post_content']
    print "<item>"
    print "<pubDate>"
    print postdate
    print "</pubDate>"
    print "<title>"
    print posttitle
    print "</title>"
    print "<content:encoded>"
    print postcontent
    print "</content:encoded>"
    print "</item>"

rawDescription: BeautifulSoup print multiple tag / attr

description: BeautifulSoup print multiple tag / attr


code:
for i in discoverItems(conn, jid.getDomain()):
    (ids, features) = discoverInfo(conn, i.jid)
    if NS_MUC in features:
        print i.jid

rawDescription: How can i get a list of all MUC 's in XMPP server using python xmpppy?

description: Get a list of all MUC 's in XMPP server using python xmpppy


code:
def callDuff():
    print "Call back works"
    proc = subprocess.Popen("python duff.duplicateFileFinder\duff.py", shell=True)
    #^^^^^^
    kill_window()
    proc.wait() # <-----

rawDescription: Python: Script not exiting when using Tkinter


code:
def output(output_a, output_lat, ouput_dir):

    # First create the hole directory structure.
    # -------------------------------------------
    ouput_path = os.path.join(ouput_dir, "geo{0}".format(index))
    # Check ouput directory existence.
    if not os.path.exists(os.path.join(ouput_dir, "geo{0}".format(index))):
        os.mkdir(ouput_path)

    for i in range(len(output_lat)):
        folder_name = os.path.join(ouput_path, "geo{0}".format(index))
        try:
            os.mkdir(folder_name)
        except OSError, e:
            print("Error creating output dir: {0}".format(e))

    # Then create output files.
    # --------------------------------------------
    for index, (o_lat,o_a) in enumerate(zip(output_lat, output_a)):
        folder_name = os.path.join(ouput_path, "geo{0}".format(index))
        with open(os.path.join(folder_name, "geo{0}.dat".format(index)), "w+") as f:
            for line in o_lat:
                f.write(" ".join(str(x) for x in line) + "\n")
            for line in o_a:
                f.write(" ".join(str(x) for x in line) + "\n")

rawDescription: Python: Create folders and recursively store data from array


code:
from bs4 import BeautifulSoup

data = '''

1562375XXXX	2 minutes ago	1234567: hi honney, trust trying how to use globfone. glad u told me about this site. it will be

useful to me in the future. /check globfone.com

'''

def grab_smss(soup):   # soup = the web page, parsed after applying
    """                # html_doc = html_doc.replace("\n", "")
    Extracts SMSs from page, in form [From, Ago, Msg]
    """
    sms_list = []
    in_smss = False
    [s.extract() for s in soup('br')]
    for row in soup.body.table.find_all('tr'):
        sms_row = [' '.join(unicode(subchild.string) for subchild in child) for child in row.children]
        sms_list.append(sms_row)
        if "From Number" in sms_row:
            in_smss = True
    return sms_list

print grab_smss(BeautifulSoup(data))

rawDescription: BeautifulSoup4 missing elements?

description: BeautifulSoup4 missing elements


code:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

#signal handler
def myCellChanged(row, col):
    print row, col

#just a helper function to setup the table
def createCheckItem(table, row, col):
    check = QTableWidgetItem("Test")
    check.setCheckState(Qt.Checked)
    table.setItem(row,col,check)

app = QApplication(sys.argv)

#create the 5x5 table...
table = QTableWidget(5,5)
map(lambda (row,col): createCheckItem(table, row, col),
   [(row, col) for row in range(0, 5) for col in range(0, 5)])
table.show()

#...and connect our signal handler to the cellChanged(int, int) signal
QObject.connect(table, SIGNAL("cellChanged(int, int)"), myCellChanged)
app.exec_()

rawDescription: How do I get some slot/function to be executed when a certain QTableWidgetItem is checked / unchecked in PyQt

description: Get some slot/function to be executed when a certain QTableWidgetItem is checked / unchecked in PyQt


code:
mqttc = mqtt.Client(machine_id, clean_session=False)
mqttc.username_pw_set(mqtt_user, mqtt_pwd)
mqttc.connect(mqtt_host, mqtt_port)
mqttc.subscribe(mqtt_topic, qos=1)

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print "Unexpected MQTT disconnection. Will auto-reconnect"

mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_disconnect = on_disconnect
mqttc.loop_forever()

rawDescription: MQTT Paho Python reliable reconnect

description: MQTT Paho Python reliable reconnect


code:
#!/usr/bin/env python
"""Find executables in %PATH% that match PATTERN.

"""
#XXX: remove --use-pathext option

import fnmatch, itertools, os, re, sys, warnings
from optparse import OptionParser
from stat import S_IMODE, S_ISREG, ST_MODE
from subprocess import PIPE, Popen

def warn_import(*args):
    """pass '-Wd' option to python interpreter to see these warnings."""
    warnings.warn("%r" % (args,), ImportWarning, stacklevel=2)

class samefile_win:
    """
http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html
"""
    @staticmethod
    def get_read_handle (filename):
        return win32file.CreateFile (
            filename,
            win32file.GENERIC_READ,
            win32file.FILE_SHARE_READ,
            None,
            win32file.OPEN_EXISTING,
            0,
            None
            )

    @staticmethod
    def get_unique_id (hFile):
        (attributes,
         created_at, accessed_at, written_at,
         volume,
         file_hi, file_lo,
         n_links,
         index_hi, index_lo
         ) = win32file.GetFileInformationByHandle (hFile)
        return volume, index_hi, index_lo

    @staticmethod
    def samefile_win(filename1, filename2):
        """Whether filename1 and filename2 represent the same file.

It works for subst, ntfs hardlinks, junction points.
It works unreliably for network drives.

Based on GetFileInformationByHandle() Win32 API call.
http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html
"""
        if samefile_generic(filename1, filename2): return True
        try:
            hFile1 = samefile_win.get_read_handle (filename1)
            hFile2 = samefile_win.get_read_handle (filename2)
            are_equal = (samefile_win.get_unique_id (hFile1)
                         == samefile_win.get_unique_id (hFile2))
            hFile2.Close ()
            hFile1.Close ()
            return are_equal
        except win32file.error:
            return None

def canonical_path(path):
    """NOTE: it might return wrong path for paths with symbolic links."""
    return os.path.realpath(os.path.normcase(path))

def samefile_generic(path1, path2):
    return canonical_path(path1) == canonical_path(path2)

class is_executable_destructive:
    @staticmethod
    def error_message(barename):
        r"""
"'%(barename)s' is not recognized as an internal or external\r\n
command, operable program or batch file.\r\n"

in Russian:
"""
        return '"%(barename)s" \xad\xa5 \xef\xa2\xab\xef\xa5\xe2\xe1\xef \xa2\xad\xe3\xe2\xe0\xa5\xad\xad\xa5\xa9 \xa8\xab\xa8 \xa2\xad\xa5\xe8\xad\xa5\xa9\r\n\xaa\xae\xac\xa0\xad\xa4\xae\xa9, \xa8\xe1\xaf\xae\xab\xad\xef\xa5\xac\xae\xa9 \xaf\xe0\xae\xa3\xe0\xa0\xac\xac\xae\xa9 \xa8\xab\xa8 \xaf\xa0\xaa\xa5\xe2\xad\xeb\xac \xe4\xa0\xa9\xab\xae\xac.\r\n' % dict(barename=barename)

    @staticmethod
    def is_executable_win_destructive(path):
        # assume path <-> barename that is false in general
        barename = os.path.splitext(os.path.basename(path))[0]
        p = Popen(barename, stdout=PIPE, stderr=PIPE, shell=True)
        stdout, stderr = p.communicate()
        return p.poll() != 1 or stdout != '' or stderr != error_message(barename)

def is_executable_win(path):
    """Based on:
http://timgolden.me.uk/python/win32_how_do_i/tell-if-a-file-is-executable.html

Known bugs: treat some "*~" files as executable, e.g. some "*.bat~" files
"""
    try:
        _, executable = FindExecutable(path)
        return bool(samefile(GetLongPathName(executable), path))
    except error:
        return None # not an exe or a document with assoc.

def is_executable_posix(path):
    """Whether the file is executable.

Based on which.py from stdlib
"""
    #XXX it ignores effective uid, guid?
    try: st = os.stat(path)
    except os.error:
        return None

    isregfile = S_ISREG(st[ST_MODE])
    isexemode = (S_IMODE(st[ST_MODE]) & 0111)
    return bool(isregfile and isexemode)

try:
    #XXX replace with ctypes?
    from win32api import FindExecutable, GetLongPathName, error
    is_executable = is_executable_win
except ImportError, e:
    warn_import("is_executable: fall back on posix variant", e)
    is_executable = is_executable_posix

try: samefile = os.path.samefile
except AttributeError, e:
    warn_import("samefile: fallback to samefile_win", e)
    try:
        import win32file
        samefile = samefile_win.samefile_win
    except ImportError, e:
        warn_import("samefile: fallback to generic", e)
        samefile = samefile_generic

def main():
    parser = OptionParser(usage="""
%prog [options] PATTERN
%prog [options] -e PATTERN""", description=__doc__)
    opt = parser.add_option
    opt("-e", "--regex", metavar="PATTERN",
        help="use PATTERN as a regular expression")
    opt("--ignore-case", action="store_true", default=True,
        help="""[default] ignore case when --regex is present; for \
non-regex PATTERN both FILENAME and PATTERN are first \
case-normalized if the operating system requires it otherwise \
unchanged.""")
    opt("--no-ignore-case", dest="ignore_case", action="store_false")
    opt("--use-pathext", action="store_true", default=True,
        help="[default] whether to use %PATHEXT% environment variable")
    opt("--no-use-pathext", dest="use_pathext", action="store_false")
    opt("--show-non-executable", action="store_true", default=False,
        help="show non executable files")

    (options, args) = parser.parse_args()

    if len(args) != 1 and not options.regex:
       parser.error("incorrect number of arguments")
    if not options.regex:
       pattern = args[0]
    del args

    if options.regex:
       filepred = re.compile(options.regex, options.ignore_case and re.I).search
    else:
       fnmatch_ = fnmatch.fnmatch if options.ignore_case else fnmatch.fnmatchcase
       for file_pattern_symbol in "*?":
           if file_pattern_symbol in pattern:
               break
       else: # match in any place if no explicit file pattern symbols supplied
           pattern = "*" + pattern + "*"
       filepred = lambda fn: fnmatch_(fn, pattern)

    if not options.regex and options.ignore_case:
       filter_files = lambda files: fnmatch.filter(files, pattern)
    else:
       filter_files = lambda files: itertools.ifilter(filepred, files)

    if options.use_pathext:
       pathexts = frozenset(map(str.upper,
            os.environ.get('PATHEXT', '').split(os.pathsep)))

    seen = set()
    for dirpath in os.environ.get('PATH', '').split(os.pathsep):
        if os.path.isdir(dirpath): # assume no expansion needed
           # visit "each" directory only once
           # it is unaware of subst drives, junction points, symlinks, etc
           rp = canonical_path(dirpath)
           if rp in seen: continue
           seen.add(rp); del rp

           for filename in filter_files(os.listdir(dirpath)):
               path = os.path.join(dirpath, filename)
               isexe = is_executable(path)

               if isexe == False and is_executable == is_executable_win:
                  # path is a document with associated program
                  # check whether it is a script (.pl, .rb, .py, etc)
                  if not isexe and options.use_pathext:
                     ext = os.path.splitext(path)[1]
                     isexe = ext.upper() in pathexts

               if isexe:
                  print path
               elif options.show_non_executable:
                  print "non-executable:", path

if __name__=="__main__":
   main()

rawDescription: How to tell whether a file is executable on Windows in Python?

description: Tell whether a file is executable on Windows in Python


code:
import inspect
def log_var(name):
    f = inspect.currentframe().f_back
    if name in f.f_locals:
        print "local `%s` = %r" % (name, f.f_locals[name])
    elif name in f.f_globals:
        print "global `%s` = %r" % (name, f.f_globals[name])
    elif name in f.f_builtins:
        print "builtin `%s` = %r" % (name, f.f_builtins[name])
    else:
        print "`%s` not found" % name

rawDescription: Scope of "library" methods

description: Scope of "library" methods


code:
from pyparsing import (
    Word, nums, Forward, ZeroOrMore, alphas, alphanums, Group, Combine, Suppress
)

def test_grammar():
    number = Word(nums).setParseAction(lambda toks: int(toks[0]))
    itemlist = Forward()
    items = number | itemlist
    itemlist << Group(
        Suppress('[')
        + items
        + ZeroOrMore(Suppress(',') + items)
        + Suppress(']')
    ).setParseAction(lambda toks: list(toks))
    variable = Word(alphas, alphanums + '_')('variable')
    data = itemlist('data')
    assignment = Group(variable + '=' + data)('assignment')
    indexes = ZeroOrMore(Suppress('[') + number + Suppress(']'))('indexes')
    query = Combine(variable + indexes)('query')
    action = assignment | query
    return action

def main():
    variables = dict()

    test_input_a = 'nl = [1, [2, 3]]'
    test_input_b = 'nl[1][1]'

    grammar = test_grammar()
    tokens = grammar.parseString(test_input_a)
    data = tokens['assignment']['data'][0]
    variables[tokens['assignment']['variable']] = data
    print variables['nl']

    tokens = grammar.parseString(test_input_b)
    data = variables[tokens['query']['variable']]
    for index in tokens['query']['indexes']:
        data = data[index]
    print data

if __name__ == '__main__':
    main()

rawDescription: Handling parsed variables stored in dictionary


code:
bucket_uri = boto.storage_uri(base_bucket_name, GOOGLE_STORAGE)
for object_uri in bucket_uri.list_bucket(prefix='/%s' % userid, delimiter='/'):
    print object_uri

rawDescription: get_all_keys from folder within bucket on Google Cloud Storage

description: Get_all_keys from folder within bucket on Google Cloud Storage


code:
input = "<html><sometag attr1=dont_quote_me attr2='but this one is quoted'>Text</sometag></html>"

bs = BeautifulSoup(input)

bs2 = bs.find('sometag')
for a in bs2.attrs:
    (attr,aval) = a
    print "%s='%s'" % (attr,aval),

rawDescription: Beautiful Soup adding quotations of HTML attributes


code:
from bs4 import BeautifulSoup
from selenium import webdriver

# Create your driver
driver = webdriver.Firefox()

# Get a page
driver.get('http://news.ycombinator.com')

# Feed the source to BeautifulSoup
soup = BeautifulSoup(driver.page_source)

print soup.title  # <title>Hacker News</title>

rawDescription: Can beautiful soup also hit webpage events?


code:
import os
os.system('sort -u -n s1.num > s1.ns')
os.system('sort -u -n s2.num > s2.ns')
i1 = open('s1.ns', 'r')
i2 = open('s2.ns', 'r')
try:
    d1 = i1.next()
    d2 = i2.next()
    while True:
        if (d1 < d2):
            d1 = i1.next()
        elif (d2 < d1):
            d2 = i2.next()
        else:
            print d1,
            d1 = i1.next()
            d2 = i2.next()
except StopIteration:
    pass

rawDescription: How can I find intersection of two large file efficiently using python?

description: Find intersection of two large file efficiently using python


code:
import sys
import matplotlib.pyplot as plt
from pprint import pprint

#dict to label points
mylabel = {1:'A',2:'B',3:'C',4:'D',5:'E',6:'F',7:'G',8:'H'}

#dict to derive coordinates
mylabel2 = {
         'A':(0,5),
         'B':(1,0),
         'C':(5,1),
         'D':(2,4),
         'E':(3,6),
         'F':(0,7),
         'G':(4,8),
         'H':(6,2)
           }
#Adjancency and State Matrix
Adj_Matrix = [[0, 20, 0, 0, 0, 0, 15, 0],
             [20, 0, 8, 9, 0, 0, 0, 0],
             [0,  8,  0,  6, 15, 0, 0, 10],
             [0, 9, 6, 0, 7, 0, 0, 0],
             [0, 0, 15, 7, 0, 22, 18, 0],
             [0, 0, 0, 0, 22, 0, 0, 0],
             [15, 0, 0, 0, 18, 0, 0, 0],
             [0, 0, 10, 0, 0, 0, 0, 0]]

xCoord=[mylabel2[k][0] for k in sorted(mylabel2)]
yCoord=[mylabel2[k][1] for k in sorted(mylabel2)]
plt.plot(xCoord, yCoord, 'bo')
plt.axis([-1, 7, -1, 9])
for i in xrange(8):
    plt.text(xCoord[i]-0.5, yCoord[i], mylabel[i+1])
for i in xrange(8):
    for j in xrange(8):
        if Adj_Matrix[i][j]:
            plt.plot([xCoord[i], xCoord[j]],[yCoord[i], yCoord[j]], 'b')
#Dijkstra Algorithm
def dijkstra(graph,start,target):
    inf = reduce(lambda x,y: x+y,(i[1] for u in graph for i in graph[u]))
    dist = dict.fromkeys(graph,inf)
    prev = dict.fromkeys(graph)
    q = graph.keys()
    dist[start] = 0
    while q:
        u = min(q, key=lambda x: dist[x])
        q.remove(u)
        for v,w in graph[u]:
            alt = dist[u] + w
            if alt < dist[v]:
                dist[v] = alt
                prev[v] = u
    #”way”
    trav = []
    temp = target
    while temp != start:
        trav.append(prev[temp])
        temp = prev[temp]
    trav.reverse()
    trav.append(target)
    return " -> ".join(trav),dist[target]

graph = {
    'A' : [('B',20), ('G', 15)],
    'B' : [('A', 20),('C', 8), ('D', 9)],
    'C' : [('B', 8),('D', 6), ('E', 15), ('H', 10)],
    'D' : [('B', 9),('C', 6),('E', 7)],
    'E' : [('C', 15),('D', 7),('F', 22),('G', 18)],
    'F' : [('E', 22)],
    'G' : [('A', 15),('E', 18)],
    'H' : [('C', 10)]
    }
traverse, dist = dijkstra(graph,'F','H')
print traverse
#Drawing of coordinates
mydrawing = traverse.split('-> ')
plt.plot([ mylabel2[n.rstrip()][0] for n in mydrawing ],[ mylabel2[n.rstrip()][1] for n in mydrawing])
print "Distance:",dist
plt.show()

rawDescription: Dijkstra's algorithm in Python plotting path


code:
import numpy as np
x = np.arange(10)
y = np.zeros(10)
print y
y[x>3] = 10
print y

rawDescription: Python Boolean in Brackets?

description: Python Boolean in Brackets


code:
import math

d = [0] * 9
offset = 10

dlen = len(d)
r = [d[i] + offset * math.sin(i * math.pi / (dlen - 1))
     for i in range(dlen)]

print d
print r

rawDescription: Applying a value offset through a list in a way that resembles a sine wave (eases in and out)


code:
if len(x) == len(set(x)):
    print "all elements are unique"
else:
    print "elements are not unique"

rawDescription: What's the most pythonic way to ensure that all elements of a list are different?


code:
class YourProtocol(protocol.Protocol):

    def connectionMade(self):
        self._peer = self.transport.getPeer()

    def connectionLost(self):
        print 'Lost connection from', self._peer

rawDescription: How to get the client IP address after got connection lost in twisted

description: Get the client IP address after got connection lost in twisted


code:
soup = BeautifulSoup(myXml)
for a in soup.findAll(a,{'class' : 'sqq'}):
  # this is your quote
  print a.contents

rawDescription: Screen scraping in LXML with python-- extract specific data


code:
#!/usr/bin/python

import sys
from itertools import cycle

def xor_file_with_cycling_strkey(filelocation,outfile,key):
  print filelocation
  f=open(filelocation,'r')
  f2=open(outfile,'w')
  lines=[]
  text=f.read()
  if text != '':
    for c,k in zip(text,cycle(key)):
      r=chr(ord(c)^ord(k))
      f2.write(r)
  f2.close()
  f.close()

# not used here, see compute_distance_char based on same idea.
def compute_distance(str1,str2,keysize):
  count=0
  print '%s %s' % (str1,str2)
  str1=str1.replace("\n", "")
  str2=str2.replace("\n", "")
  keysize=str(keysize*8)
  sbin1=format(int(str1,16),'0'+keysize+'b')
  sbin2=format(int(str2,16),'0'+keysize+'b')
  return hamming_distance_str(sbin1,sbin2)

#do preferer hamming_distance_bin which quicker.
def compute_distance_char(str1,str2,keysize):
  count=0
  str1=str1.replace("\n", "")
  str2=str2.replace("\n", "")
  keysize=str(keysize*8)
  sbin1=''
  sbin2=''
  for c in str1:
    sbin1=sbin1 + format(ord(c),'0'+keysize+'b')
  for c in str2:
    sbin2=sbin2 + format(ord(c),'0'+keysize+'b')
  return hamming_distance_str(sbin1,sbin2)

def hamming_distance_str(str1,str2):
  count=0
  for c1,c2 in zip(str1, str2):
    if  c1!=c2:
      count+=1
  return count

def hamming_distance_bin(str1,str2):
  count=0
  for c1,c2 in zip(str1, str2):
    if  c1!=c2:
      # quick hamming distance, counting number of differing bits.
      s=ord(c1)^ord(c2)
      # count number of bits sets using Wegner algorithm
      while s !=0:
        s&=(s-1);
        count+=1
  return count

def keysize_dist(filelocation):
  potential_keysize=0
  min_dist=40.0
  f=open(filelocation,'r')
  lines=[]
  for line in f.readlines():
    line=line.strip('\n')
    lines.append(line)
  lines=''.join(lines).strip('\n')
  normalized=[]
  for keysize in range(2,40):
# should first create base16 entries for that one , then don't use it : count_bin1=compute_distance(lines[0:keysize*2],lines[keysize*2:keysize*4],keysize)
    # proof that both functions compute same value :
    count_bin1=compute_distance_char(lines[0:keysize*2],lines[keysize*2:keysize*4],keysize)
    count_bin2=hamming_distance_bin(lines[0:keysize*2],lines[keysize*2:keysize*4])
    if ( count_bin1 != count_bin2 ):
      print 'Discrepency between compute_distance_char->%i and hamming_distance_bin->%i' % (count_bin1,count_bin2)
    count=hamming_distance_str(lines[0:keysize*2],lines[keysize*2:keysize*4])

    normalized_distance=float(count)/keysize
    print '%s %f' % (keysize,normalized_distance)
    if ( normalized_distance < min_dist ):
      potential_keysize=keysize
      min_dist=normalized_distance
#  we are more interested in keysize corresponding to minimal distance, tha n to minimal distance itself.
  return potential_keysize,min_dist

def main(args=sys.argv):
 if ( len(args) < 2 ):
   print 'Please enter cleartext origin file to be ciphered then checked an optionaly a key string ( max length 40 )'
   return 1
 if ( len(args) > 2):
   key=args[2]
 else:
   # on purpose default to key with a KEYSIZE char length 5.
   key='12345'
 xor_file_with_cycling_strkey(args[1],args[1]+'.ciphered',key)
 xor_file_with_cycling_strkey(args[1]+'.ciphered',args[1] + '.cleartext',key)

 # raw non base64 encoded.
 print keysize_dist(args[1] + '.ciphered')

if __name__ == "__main__":
    main()

rawDescription: Breaking XOR repeated key

description: Breaking XOR repeated key


code:
a=input('input:')
while a.strip()!='':

    b=a.split()
    ans=''
    while len(b) >0 :
       word=b[0][::-1]
       ans+=word+" "
       b.pop(0)
    print ans
    a=input('input:')

#output i ma hsejar !aloH

rawDescription: How can i use a while loop on this program?

description: Use a while loop on this program


code:
from datetime import datetime
excel_date = 42139
dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + excel_date - 2)
tt = dt.timetuple()
print dt
print tt

rawDescription: Convert date from excel in number format to date format python


code:
def download_urls(url_and_path_list, num_concurrent, skip_existing):
    # prepare the queue
    queue = Queue.Queue()
    for url_and_path in url_and_path_list:
        queue.put(url_and_path)

    # start the requested number of download threads to download the files
    threads = []
    for _ in range(num_concurrent):
        t = DownloadThread(queue, skip_existing)
        t.daemon = True
        t.start()

    queue.join()

class DownloadThread(threading.Thread):
    def __init__(self, queue, skip_existing):
        super(DownloadThread, self).__init__()
        self.queue = queue
        self.skip_existing = skip_existing

    def run(self):
        while True:
            #grabs url from queue
            url, path = self.queue.get()

            if self.skip_existing and exists(path):
                # skip if requested
                self.queue.task_done()
                continue

            try:
                urllib.urlretrieve(url, path)
            except IOError:
                print "Error downloading url '%s'." % url

            #signals to queue job is done
            self.queue.task_done()

rawDescription: urllib2 urlopen read timeout/block

description: Urllib2 urlopen read timeout/block


code:
import Tkinter as tk
import ttk

cmb_values = ["item1","item2","item3","item4"]

root = tk.Tk()

cmb = ttk.Combobox(root, values = cmb_values)
cmb.pack()

#if you use cmb["values"] you will get the list of values which is cmb_values
#so to get number of items in that list, you only need len()

print len(cmb["values"])

root.mainloop()

rawDescription: How to find a count of a combobox values

description: Find a count of a combobox values


code:
#!/usr/bin/python
from lxml import etree
import sys
import re

tree=etree.parse('file.xml')
tag = '//' + sys.argv[1]
find_text = etree.XPath(tag)
for j in  [tree.getpath(text) for text in find_text(tree)]:
    print re.sub(r'[\/\[\]]+', '.', j)[1:] # this will change [ / and ] to a dot.

rawDescription: Print index of XML element

description: Print index of XML element


code:
i, I = 0, 4
while i < I:
    i+=1
    print i,

rawDescription: Output: In the same line

description: Output: In the same line


code:
DNA = open('DNAGCex.txt')
DNAID = []
DNASEQ = []
for line in DNA:
    line = line.strip()
    if line.startswith('>'):
        DNAID.append(line)
        DNASEQ.append('')
    else:
        DNASEQ[-1] += line
print DNAID
print DNASEQ

rawDescription: Python: List not correct after appending lines


code:
delta = min(date_1_end,date_base_end)-max(date_1_start,date_base_start)
#
# Check if delta is negative,
#
if delta.seconds < 0:
         print 0
else:
         print delta.seconds/60.0

rawDescription: Python: Get minutes overlap between time range


code:
# create a background image on a wxPython panel
# and show a button on top of the image

import wx

class Panel1(wx.Panel):
    """class Panel1 creates a panel with an image on it, inherits wx.Panel"""
    def __init__(self, parent, id):
        # create the panel
        wx.Panel.__init__(self, parent, id)
        try:
            # pick an image file you have in the working folder
            # you can load .jpg  .png  .bmp  or .gif files
            image_file = 'roses.jpg'
            bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            # image's upper left corner anchors at panel coordinates (0, 0)
            self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
            # show some image details
            str1 = "%s  %dx%d" % (image_file, bmp1.GetWidth(), bmp1.GetHeight())
            parent.SetTitle(str1)
        except IOError:
            print "Image file %s not found" % imageFile
            raise SystemExit

        # button goes on the image --> self.bitmap1 is the parent
        self.button1 = wx.Button(self.bitmap1, id=-1, label='Button1', pos=(8, 8))

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# change the size of the frame to fit the backgound images
frame1 = wx.Frame(None, -1, "An image on a panel", size=(350, 400))
# create the class instance
panel1 = Panel1(frame1, -1)
frame1.Show(True)
app.MainLoop()

rawDescription: how to put a image as a background in wxpython

description: Put a image as a background in wxpython


code:
if a.split(None, 1)[0].lower() in {'will', 'do'}:
    print "Determining Your Future..."
    time.sleep(2)
    magic8(a) # function names are usually not capitalized

rawDescription: Basic Python Script If Statment

description: Basic Python Script If Statment


code:
def Reclass( Elevation ):
    r_min, r_max = (float(s.strip()) for s in Elevation.split('-'))
    print "r_min: {0}, r_max: {1}".format(r_min,r_max)
    if r_min < 1 and r_max < 1:
        return 0.333
    elif r_min >= 1 and r_max >= 1 and r_min <= 3 and r_max <= 3:
        return 0.666
    elif r_min > 3 and r_max > 3:
        return 1
    else:
        return 999

print Reclass("0 - 1.1")
print Reclass("5.2 - 10")
print Reclass("2 - 3")
print Reclass("0 - 0")

rawDescription: Python If Function for Ranged Field

description: Python If Function for Ranged Field


code:
#!/usr/bin/env python

import pygame
from threading import Thread
import serial
import json
from pygame.color import Color
from pygame.locals import *

CURHR = 0

ser = serial.Serial('/dev/pts/3', 9600, timeout=0)
def worker():

   global CURHR
   while True:
     msg = ser.readline()
     if len(msg) > 0:
       print "Message Received: %s" % msg
       current_message = json.loads(msg)
       if current_message["state"] == "running":
         CURHR=current_message['heart_rate']

t = Thread(target=worker)
t.daemon = True
t.start()

pygame.init()

#-------------------------------------------------------------------------------
# Constants
VERSION = '1.0'
WIDTH = 800
HEIGHT = 600
FRAMERATE = 60

#-------------------------------------------------------------------------------
# Define helper functions, classes, etc...
def text_objects(text, font):
    textSurface = font.render(text, True, (255,255,255))
    return textSurface, textSurface.get_rect()

def message_display(text,x_pos=(WIDTH/2),y_pos=(HEIGHT/2)):
    largeText = pygame.font.Font('freesansbold.ttf',25)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = (x_pos,y_pos)
    screen.blit(TextSurf, TextRect)

def spam():
    pos = pygame.mouse.get_pos()
    pygame.draw.circle(screen, Color('white'), pos, 32)

screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock  = pygame.time.Clock()
font   = pygame.font.SysFont("consolas", 25, True)
count  = 0
pygame.display.set_caption("Test")

done = False
while not done:
    screen.fill(Color('black'))
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                  done = True

    spam()
    hr_string = "HR: %s"  % CURHR
    hr_text = font.render(hr_string, True, Color('red'))
    screen.blit(hr_text, [25,10])
    clock.tick(20)
    pygame.display.flip()

rawDescription: Non blocking serial readline when using PyGame


code:
import time
import ctypes

user32 = ctypes.windll.User32
OpenDesktop = user32.OpenDesktopA
SwitchDesktop = user32.SwitchDesktop
DESKTOP_SWITCHDESKTOP = 0x0100

user32.LockWorkStation ()
#
# Slight pause to overcome what appears to
# be a grace period during which a switch
# *will* succeed.
#
time.sleep (1.0)

while 1:
  hDesktop = OpenDesktop ("default", 0, False, DESKTOP_SWITCHDESKTOP)
  result = SwitchDesktop (hDesktop)
  if result:
    print "Unlocked"
    break
  else:
    print time.asctime (), "still locked"
    time.sleep (2)

rawDescription: Detect when computer is unlocked Windows


code:
def ascii_lines(iterable):
    for line in iterable:
        if all(ord(ch) < 128 for ch in line):
            yield line

f = open('somefile.txt')
for line in ascii_lines(f):
    print line

rawDescription: Deleting Non-ASCII *lines* from a file?

description: Deleting Non-ASCII *lines* from a file


code:
from copy import copy

def multidict(*args):
    if len(args) == 1:
        return copy(args[0])
    out = {}
    for x in args[0]:
        out[x] = multidict(*args[1:])
    return out

print multidict(['a', 'b'], ['A', 'B'], ['1', '2'], {})

rawDescription: Is there any elegant way to build a multi-level dictionary in python?


code:
x = "#DataCGSConversionFactor[0] = 1.0051900924519e-29"

f = float(x.split()[-1])    # split at spaces, take last item, and cast to a float

print f, type(f)
# 1.0051900924519e-29, float

rawDescription: Extracting scientific notation from a string


code:
br = mechanize.Browser()
resp = br.open('your_url_here')
print resp.get_data() # if you want to see what's returned
# if you want to see the forms, so you can find the index of the
# form you want and check that is has all the fields, if it doesn't
# you should should parse the response with BeautifulSoup
for form in br.forms():
    print '---------------'
    print form
br.select_form(nr=0) # to select the first form
br['field_name'] = 'field_value'
br['select_field_name'] = ['select_field_value']
br.submit()

rawDescription: Problems with Forms, using POST method with Mechanize in Python


code: print br.form.get_value('q')

rawDescription: how to find the POST or GET variables posted by mechanize (python)

description: Find the POST or GET variables posted by mechanize (python)


code:
import json
import urllib

import scrapy

class DmozSpider(scrapy.Spider):
    name = "arbal"
    allowed_domains = ["bigbasket.com"]
    start_urls = [
        "http://bigbasket.com/pc/bread-dairy-eggs/bread-bakery/?nc=cs"
    ]

    def parse(self, response):
        # follow pagination
        num_pages = int(response.xpath('//div[@class="noItems"]/span[@class="bFont"][last()]/text()').re(r'(\d+)')[0])
        sid = response.xpath('//a[contains(@href, "sid")]/@href').re(r'sid=(\w+)(?!&|\z)')[0]

        base_url = 'http://bigbasket.com/product/facet/get-page/?'
        for page in range(1, num_pages/20 + 1):
            yield scrapy.Request(base_url + urllib.urlencode({'sid': sid, 'page': str(page)}), dont_filter=True, callback=self.parse_page)

    def parse_page(self, response):
        data = json.loads(response.body)

        selector = scrapy.Selector(text=data['products'])
        for product in selector.xpath('//li[starts-with(@id, "product")]'):
            title = product.xpath('.//div[@class="muiv2-product-container"]//img/@title').extract()[0]
            print title

rawDescription: how to get scrapy go to the next page?

description: Get scrapy go to the next page


code:
# from: http://www.cs.princeton.edu/introcs/21function/ErrorFunction.java.html
# Implements the Gauss error function.
#   erf(z) = 2 / sqrt(pi) * integral(exp(-t*t), t = 0..z)
#
# fractional error in math formula less than 1.2 * 10 ^ -7.
# although subject to catastrophic cancellation when z in very close to 0
# from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2
def erf(z):
	t = 1.0 / (1.0 + 0.5 * abs(z))
    	# use Horner's method
        ans = 1 - t * math.exp( -z*z -  1.26551223 +
        					t * ( 1.00002368 +
        					t * ( 0.37409196 +
        					t * ( 0.09678418 +
        					t * (-0.18628806 +
        					t * ( 0.27886807 +
        					t * (-1.13520398 +
        					t * ( 1.48851587 +
        					t * (-0.82215223 +
        					t * ( 0.17087277))))))))))
        if z >= 0.0:
        	return ans
        else:
        	return -ans

rawDescription: Is there an easily available implementation of erf() for Python?


code:
import string as st

trans = st.maketrans(st.punctuation, ' '*len(st.punctuation))
with open('question5.txt', 'r') as f:
    for line in f:
        print line.translate(trans)

rawDescription: replace letters using string.punctuation in a for loop

description: Replace letters using string.punctuation in a for loop


code:
for item in request.POST.keys():
  print item + ' - ' + request.POST[item]

rawDescription: getting data from dynamically generated html form

description: Getting data from dynamically generated html form


code:
from copy import copy
import csv

class Part_Base(object):
    pass

class MultiPart_Base(list):
    pass

class part_meta(type):
    part_names = {}
    parts      = []
    def __init__(cls, cls_name, cls_bases, cls_dict):
        super(part_meta, cls).__init__(cls_name, cls_bases, cls_dict)
        if(not Part_Base in cls_bases):
            part_meta.part_names[cls_name] = cls

    def __call__(self, *args, **kwargs):
        name = kwargs.get("name", "")
        if(part_meta.part_names.has_key(name) and not (self is part_meta.part_names[name])):
            obj = part_meta.part_names[name].__call__(*args, **kwargs)
        else:
            obj = None
            if(not part_meta.part_names.has_key(self.__name__)):
                new_class = part_meta(name, (Generic_Part,), {})
                globals()[name] = new_class
                obj = new_class(*args, **kwargs)
            else:
                obj = super(part_meta, self).__call__(*args, **kwargs)
        if not obj in part_meta.parts:
            part_meta.parts.append(obj)
        return obj

    @classmethod
    def save(cls):
        all_fields = list(reduce(lambda x, y: x | set(y.fields), cls.parts, set([])))
        with open("parts.csv", "w") as file_h:
            writer = csv.DictWriter\
            (
                file_h,
                all_fields,
                restval        = "",
                extrasaction   = "ignore",
                dialect        = "excel",
                lineterminator = "\n",

            )
            writer.writeheader()
            for part in cls.parts:
                writer.writerow({field : getattr(part, field) for field in part.fields})

    @classmethod
    def load(cls):
        with open("parts.csv", "r") as file_h:
            reader = csv.DictReader(file_h)
            for row in reader:
                Part(**row)

class Part(Part_Base):
    __metaclass__ = part_meta
    fields        = []
    def __init__(self, **kwargs):
        for name, value in kwargs.items():
            setattr(self, name, value)
        self.fields += kwargs.keys()

    def __repr__(self):
        return "<%s>" % self.description

    @property
    def description(self):
        return "%s: %s %s %s %s" % (self.name, self.intype, self.outtype, self.shape, self.serialno)

    def linkwith(self, *parts):
        return Generic_MultiPart(self, *parts)

class Generic_Part(Part):
    def __init__(self, **kwargs):
        kwargs["name"] = self.__class__.__name__
        super(Generic_Part, self).__init__(**kwargs)

class Generic_MultiPart(MultiPart_Base):
    def __init__(self, *parts):
        super(Generic_MultiPart, self).__init__()
        if len(parts) >= 2:
            self.shape = parts[0].shape
            self.linkwith(*parts)
        else:
            raise ValueError("Not enough parts")

    def __repr__(self):
        return "<MultiPart: %s>" % super(Generic_MultiPart, self).__repr__()

    def linkwith(self, *parts):
        for part in parts:
            if part.shape == self.shape:
                if isinstance(part, Part):
                    self.append(part)
                elif isinstance(part, MultiPart_Base):
                    self.extend(part)
            else:
                raise ValueError("Incompatible parts")
        return self

class cooler(Generic_Part):
    intype  = "hotwater"
    outtype = "coldwater"
    fields  = ["intype", "outtype"]

class heater(Generic_Part):
    intype  = "coldwater"
    outtype = "hotwater"
    fields  = ["intype", "outtype"]

def make_some_parts():
    some_parts = \
    [
        # This is actually a cooler object
        # The metaclass uses the cooler class from above
        # to create the object
        Part
        (
            name     = "cooler",
            shape    = "1/4 inch round",
            serialno = "SN:11111"
        ),
        # Using the heater class directly
        heater
        (
            shape    = "1/4 inch round",
            serialno = "SN:22222"
        ),
        Part
        (
            name     = "purifier",
            intype   = "coldwater",
            outtype  = "hotwater",
            shape    = "1/6 inch round",
            serialno = "SN:33333"
        ),
        Part
        (
            name     = "carbon_filter",
            intype   = "coldwater",
            outtype  = "coldwater",
            shape    = "1/4 inch round",
            serialno = "SN:33333"
        )
    ]

    useless_part = some_parts[0].linkwith(some_parts[1])
    print useless_part
    filter_part  = copy(useless_part).linkwith(some_parts[3])
    print filter_part

    part_meta.save()

def load_some_parts():
    part_meta.load()
    print part_meta.parts

rawDescription: combine class instance into another instance in python?

description: Combine class instance into another instance in python


code:
from bs4 import BeautifulSoup

page = 'Elsie'
soup = BeautifulSoup(page)
Learning = soup.find_all("a", class_="sister")
print Learning

rawDescription: Examples given in the Python BeautifulSoup 4 Documentation


code:
for t in time.tzname:
    print t
datetime.strptime('2009-08-19 14:20:36 Eastern Standard Time', "%Y-%m-%d %H:%M:%S %Z")
datetime.strptime('2009-08-19 14:20:36 UTC', "%Y-%m-%d %H:%M:%S %Z")
datetime.strptime('2009-08-19 14:20:36 GMT', "%Y-%m-%d %H:%M:%S %Z")

rawDescription: What possible values does datetime.strptime() accept for %Z?


code:
import shelve

global base
global magazine_base

def load_data():
    global base
    global magazine_base
    magazine_base = shelve.open('magazine_base')
    if magazine_base.has_key('base'):
        base = magazine_base['base']
        if not base:
            base = [{'name': 'Apple', 'price': 10, 'quantity': 1}]  # example one element base
            # base = []  # it can be also just empty list at the beginning
    else:
        base = [{'name': 'Apple', 'price': 10, 'quantity': 1}]
        # base = []
        magazine_base['base'] = base

def entry():
    global base
    global magazine_base
    load_data()
    name = raw_input('Name of the product: ')
    quantity = raw_input('Quantity: ')
    price = raw_input('Price per unit: ')

    is_new_entry = True

    # entry is element of base list
    for entry in base:
        if name.lower() == entry['name'].lower():  # checking if name already exist in base list
            is_new_entry = False

    if is_new_entry:  # adding new list element
        base += [{
            'name': name,
            'quantity': int(quantity),
            'price': float(price)
        }]
        magazine_base['base'] = base
        magazine_base.close()
        print 'Saved.'  # confirmation message
    else:
        print 'Element with the name {} already exist!'.format(name)  # error message
        magazine_base.close()

def articles():
    global base
    print 'List of articles in magazine'.center(50)
    print '-'*50
    print '|'+'Name'.center(15)+'|'+'Quantity'.center(15)+'|'+'Price'.center(15)+'|'
    print '-'*50
    for entry in base:
        print "|%14s |" % entry['name'],'%13s' % entry['quantity'],'|' '%14s' % entry['price'],'|'
    print '-'*50

# testing by running functions
entry()
articles()
entry()
articles()

rawDescription: Python simple database based on shelve, adding values