code:
import os
import re
import sys
import gmpy2
import multiprocessing
from hashlib import sha1
from base64 import b64encode, b32encode
from pyasn1.codec.der import encoder
from Queue import Empty as QueueEmpty
from pyasn1.type import univ, namedtype

#### Constants stolen from the original shallot ####
EMIN = 0x10001
EMAX = 0xFFFFFFFFFF

#### Prime finding stuff for RSA ####
def random(bytez):
    '''Produces a random number thats has bytez*8 amount of bits.'''
    return gmpy2.mpz(reduce(lambda a, b: (a << 8)|ord(b), os.urandom(bytez), 0))

def good_prime(p):
    '''True if highly probably prime, else false.'''
    return gmpy2.is_prime(p, 1000) and \
           gmpy2.is_strong_bpsw_prp(p)

def find_prime(bytez=128):
    '''Checks random numbers for primality'''
    p = random(bytez)|1
    while not good_prime(p):
        p = random(bytez)|1
    return p

def good_pair(p, q):
    '''Returns p*q if p and q are a good pair, else 0.'''
    n = p*q
    k = gmpy2.ceil(gmpy2.log2(n))
    if abs(p - q) > 2**(k/2 - 100):
        return n
    return 0

##### Encoding stuffs #####
#https://tools.ietf.org/html/rfc3447#appendix-A.1
class RSAPublicKey(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('modulus', univ.Integer()),
        namedtype.NamedType('publicExponent', univ.Integer())
        )

class RSAPrivateKey(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('version', univ.Integer()),
        namedtype.NamedType('modulus', univ.Integer()),
        namedtype.NamedType('publicExponent', univ.Integer()),
        namedtype.NamedType('privateExponent', univ.Integer()),
        namedtype.NamedType('prime1', univ.Integer()),
        namedtype.NamedType('prime2', univ.Integer()),
        namedtype.NamedType('exponent1', univ.Integer()),
        namedtype.NamedType('exponent2', univ.Integer()),
        namedtype.NamedType('coefficient', univ.Integer())
        )

def public_key(n, e):
    public_key = RSAPublicKey()
    public_key.setComponentByName('modulus', n)
    public_key.setComponentByName('publicExponent', e)
    return encoder.encode(public_key)

def make_onion(n, e):
    return b32encode(sha1(public_key(n, e)).digest())[:16].lower()+'.onion'

def private_key(n, e, d, p, q):
    private_key = RSAPrivateKey()
    private_key.setComponentByName('version', 0)
    private_key.setComponentByName('modulus', n)
    private_key.setComponentByName('publicExponent', e)
    private_key.setComponentByName('privateExponent', d)
    private_key.setComponentByName('prime1', p)
    private_key.setComponentByName('prime2', q)
    private_key.setComponentByName('exponent1', d % (p - 1))
    private_key.setComponentByName('exponent2', d % (q - 1))
    private_key.setComponentByName('coefficient', gmpy2.invert(q, p))
    return encoder.encode(private_key)

def pprint_privkey(privkey):
    print '-'*5 + 'BEGIN RSA PRIVATE KEY' + '-'*5
    encoded = b64encode(privkey)
    while encoded:
        chunk, encoded = encoded[:64], encoded[64:]
        print chunk
    print '-'*5 + 'END RSA PRIVATE KEY' + '-'*5

#### Worker process generates keys, hashes, and checks for patterns ####
class Worker(multiprocessing.Process):

    def __init__(self, regex, results, trials, kill, *args, **kwds):
        multiprocessing.Process.__init__(self, *args, **kwds)
        self.regex = regex
        self.results = results
        self.trials = trials
        self.kill = kill

    def run(self):
        pattern = re.compile(self.regex)
        search = pattern.search
        i = 0
        while True:
            p = find_prime()
            q = find_prime()
            if q > p:
                p, q = q, p
            n = good_pair(p, q)
            if not n:
                continue
            tot = n - (p + q - 1)
            e = EMIN
            while e < EMAX:
                if self.kill.is_set():
                    self.trials.put(i)
                    return
                i += 1
                onion = make_onion(n, e)
                if search(onion) and gmpy2.gcd(e, tot)==1:
                    d = gmpy2.invert(e, tot)
                    priv = private_key(n, e, d, p, q)
                    self.results.put(onion+priv)
                    self.trials.put(i)
                    self.kill.set()
                    return
                e += 2

def kill_procs(processes, results, trials, kill):
    '''joins all processes, empties all queues, and returns sum of trials.'''
    if not kill.is_set():
        kill.set()
    sum_trials = 0
    while not trials.empty():
        sum_trials += trials.get()
    for proc in processes:
        proc.join()
    while not results.empty():
        results.get()
    return sum_trials

#### Main thread ####
def main(pattern):
    results = multiprocessing.Queue()
    trials = multiprocessing.Queue()
    kill = multiprocessing.Event()
    processes = []
    for i in range(multiprocessing.cpu_count()):
        processes.append(Worker(pattern, results, trials, kill))
        processes[-1].start()
    try:
        while True:
            try:
                found = results.get(True, 0.1)
            except QueueEmpty:
                pass
            else:
                break
    except KeyboardInterrupt:
        sum_trials = kill_procs(processes, results, trials, kill)
        print 'Tried', sum_trials, 'public keys before exit'
        sys.exit(1)
    sum_trials = kill_procs(processes, results, trials, kill)
    onion = found[:22]
    privkey = found[22:]
    print '-'*64
    print 'Found matching pattern after', sum_trials, 'tries:', onion
    print '-'*64
    pprint_privkey(privkey)

if __name__ == '__main__':
    try:
        main(sys.argv[1])
    except KeyboardInterrupt:
        sys.exit(1)

rawDescription: Generating private_key and hostname for Tor hidden service, in python


code:
import socks, urllib2
from sockshandler import SocksiPyHandler
proxy_opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "IP", 9050))
print proxy_opener.open("http://whatismyip.org/").read()

rawDescription: How can I unwrap a module with SocksiPy?

description: Unwrap a module with SocksiPy


code:
# establish the "rules"
bad_time = {
    'Sat': lambda h: True,    # always bad time!
    'Sun': lambda h: h < 2000,
    'Fri': lambda h: h > 1630,
}

# ... get your `weekday` and `hourmin` values

is_bad = bad_time.get(weekday, lambda h: (1630 <= h < 2000))(hourmin)
print 'bad time' if is_bad else 'good time'

rawDescription: Pythonians: Is there a better way to code this in Python?


code:
import subprocess

p = subprocess.Popen(['python', 'hello.py'])

print "over"
p.wait()

rawDescription: Running two Python 2.7 scripts simultaneously


code:
i = 0
with arcpy.da.SearchCursor("your_table", ["your_fields"], "your_query") as cursor:
    for row in cursor:
        i += 1

if i == 0:
    print "No results returned."

rawDescription: How do I check if my search cursor is empty in arcpy?

description: Check if my search cursor is empty in arcpy


code:
import re

strings = """String1_Todelete_restofstring.txt
String2_Alsotoremove_restofstring.txt
String3_2013_restofstring.txt
String4_2011_restofstring.txt
String5_restofstring_tosuppress.txt""".split()

keywords = set(("Todelete", "2013","2011", "Alsotoremove","tosuppress"))

for s in strings:
    print re.sub("_[^_.]+", lambda m: "" if m.group(0)[1:] in keywords else m.group(0), s)

rawDescription: How to clean up strings with regex from list

description: Clean up strings with regex from list


code:
import sympy
from sympy.parsing.sympy_parser import parse_expr
import re

def text_to_equations(text):
    lines = text.split('\n')
    lines = [line.split('=') for line in lines]
    eqns = []
    for lhs, rhs in lines:
        # clobber all the spaces
        lhs = lhs.replace(' ','')
        # *assume* that a number followed by a letter is an
        # implicit multiplication
        lhs = re.sub(r'(\d)([a-z])', r'\g<1>*\g<2>', lhs)
        eqns.append( (parse_expr(lhs), parse_expr(rhs)) )
    return eqns

def get_all_symbols(eqns):
    symbs = set()
    for lhs, rhs in eqns:
        for sym in lhs.atoms(sympy.Symbol):
            symbs.add(sym)
    return symbs

def text_to_eqn_matrix(text):
    eqns = text_to_equations(text)
    symbs = get_all_symbols(eqns)
    n = len(eqns)
    m = len(symbs)
    A = numpy.zeros((m, n))
    b = numpy.zeros((m, 1))
    for i, (lhs, rhs) in enumerate(eqns):
        d = lhs.as_coefficients_dict()
        b[i] = int(rhs)
        for j, s in enumerate(symbs):
            A[i, j] = d[s]
    x = sympy.Matrix([list(symbs)]).T
    return sympy.Matrix(A), x, sympy.Matrix(b)

s = '''-38 y1  +  35 y2  +  31 y3  = -3047
11 y1  + -13 y2  + -34 y3  = 784
34 y1  + -21 y2  +  19 y3  = 2949'''
A, x, b = text_to_eqn_matrix(s)
print A
print x
print b

rawDescription: Formatting a txt file of equations into the same format and then manipulating them for linear algebra calculations in Python


code:
def main():
    x = 'idle (images=green:200, inpadoc=green:60, other=green:1000, retrieval=green:200, search=green:30)'
    x= x.replace("idle (", "")
    x= x.replace(")", "")
    tmp = x.split(',')

    tmp2 = dict( i.split('=') for i in tmp )

    for item in tmp2:
        tmp2[item] = tmp2[item].split(":")

    print tmp2

if __name__ == '__main__':
    main()

rawDescription: best way to create a dict of the given string in python

description: Create a dict of the given string in python


code:
import string

table = string.maketrans(';|', ',,')
deletechars = ' \r\n<>\'"'

print "ex'a;m|ple\n".translate(table, deletechars)
# -> exa,m,ple

rawDescription: How would I search and replace multiple regexes using python re


code:
with open('test.test') as f:
    num_to_scores = {}
    for linenum, line in enumerate(f):
        splitline = line.split()
        if 'danny' in line and 'james' in line:
            store_next_vals = True
            dannyloc, jamesloc = splitline.index('danny'), splitline.index('james')
        else:
            if store_next_vals:
                num_to_scores[linenum] = {"james":splitline[jamesloc], "danny":splitline[dannyloc]}
                store_next_vals = False
with open('out.test', 'w') as f:
    f.write('line number,james,danny\n')
    for linenum in num_to_scores:
        f.write('%d,%s,%s\n' % (linenum, num_to_scores[linenum]['james'], num_to_scores[linenum]['danny']))
print open('out.test').read()

rawDescription: How can I search a specific element and prints its value beneath it in a CSV file in Python

description: Search a specific element and prints its value beneath it in a CSV file in Python


code:
for i in range(10):
    filename = 'data_%d.dat'%(i,)
    print filename

rawDescription: How do you increment file name in Python

description: Increment file name in Python


code:
import re

s='''This is a note[*] and this is another[*]
[*]: note 1
[*]: note 2'''

# text and notes are separated
[text,notes] = re.split(r'((?:\r?\n\[\*\]:[^\r\n]*)+$)', s)[:-1]

# this generator gives the next replacement string
def getnote(notes):
    for note in re.split(r'\r?\n\[\*\]: ', notes)[1:]:
        yield r'\footnote{{{}}}'.format(note)

note = getnote(notes)

res = re.sub(r'\[\*\]', lambda m: note.next(), text)
print res

rawDescription: Python Regular expression only matches once


code:
twitter = Twython(consumer_key,consumer_secret, access_token, access_secret)
search_results = twitter.search(q='Australia', count=10)
try:
    for tweet in search_results["statuses"]:
        try:
            twitter.retweet(id = tweet["id_str"])
            twitter.create_favorite(id = tweet["id_str"])
            st=tweet["entities"]["user_mentions"]
            if st != []:
                twitter.create_friendship(screen_name=st[0]["screen_name"])
        except TwythonError as e:
            print e
except TwythonError as e:
        print e

rawDescription: How to auto follow someone with Twython

description: Auto follow someone with Twython


code:
from collections import defaultdict
d = defaultdict(int)
for record in input:
    d[record[-1]] += 1
print d

rawDescription: Python "how many elements have"

description: Python "how many elements have"


code:
from Crypto.Cipher import AES,DES
from Crypto import Random

iv_AES = Random.new().read(AES.block_size)
iv_DES = Random.get_random_bytes(8)

key_AES = 'abcdefghijklmnop'
key_DES = 'abcdefgh'

aese = AES.new(key_AES,AES.MODE_CFB,iv_AES)
aesd = AES.new(key_AES,AES.MODE_CFB,iv_AES)
dese = DES.new(key_DES,DES.MODE_CFB,iv_DES)
desd = DES.new(key_DES,DES.MODE_CFB,iv_DES)

plaintext = 'Hello! World'

print plaintext == aesd.decrypt(aese.encrypt(plaintext))
print plaintext == desd.decrypt(dese.encrypt(plaintext))

rawDescription: PyCrypto Decrypting Cipher text does no yield plaintext


code:
import re

file = open ('data.txt', 'r')

allowed = False

for line in file:
    line= line.strip()
    if line == 'et 1027':
        allowed = True
    if line ==  't 0':
        allowed = False
        break
    if allowed:
        y= re.findall('[0-9]+', line)
        print y

rawDescription: Extracting numeric data lines between lines or two markers


code:
uv = api_result['data']['40332895']['user_values']
for doc in uv:
    print uv[doc]

rawDescription: printing JSON values in Python

description: Printing JSON values in Python


code:
#########################################################
## customize Title bar
## dotpy.ir
## iraj.jelo@gmail.com
#########################################################
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import Qt

class TitleBar(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowFlags(Qt.FramelessWindowHint);
        css = """
        QWidget{
            Background: #AA00AA;
            color:white;
            font:12px bold;
            font-weight:bold;
            border-radius: 1px;
            height: 11px;
        }
        QDialog{
            Background-image:url('img/titlebar bg.png');
            font-size:12px;
            color: black;

        }
        QToolButton{
            Background:#AA00AA;
            font-size:11px;
        }
        QToolButton:hover{
            Background: #FF00FF;
            font-size:11px;
        }
        """
        self.setAutoFillBackground(True)
        self.setBackgroundRole(QtGui.QPalette.Highlight)
        self.setStyleSheet(css)
        self.minimize=QtGui.QToolButton(self);
        self.minimize.setIcon(QtGui.QIcon('img/min.png'));
        self.maximize=QtGui.QToolButton(self);
        self.maximize.setIcon(QtGui.QIcon('img/max.png'));
        close=QtGui.QToolButton(self);
        close.setIcon(QtGui.QIcon('img/close.png'));
        self.minimize.setMinimumHeight(10);
        close.setMinimumHeight(10);
        self.maximize.setMinimumHeight(10);
        label=QtGui.QLabel(self);
        label.setText("Window Title");
        self.setWindowTitle("Window Title");
        hbox=QtGui.QHBoxLayout(self);
        hbox.addWidget(label);
        hbox.addWidget(self.minimize);
        hbox.addWidget(self.maximize);
        hbox.addWidget(close);
        hbox.insertStretch(1,500);
        hbox.setSpacing(0);
        self.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Fixed);
        self.maxNormal=False;
        close.clicked.connect(self.close);
        self.minimize.clicked.connect(self.showSmall);
        self.maximize.clicked.connect(self.showMaxRestore);

    def showSmall(self):
        box.showMinimized();

    def showMaxRestore(self):
        if(self.maxNormal):
            box.showNormal();
            self.maxNormal= False;
            self.maximize.setIcon(QtGui.QIcon('img/max.png'));
            print '1'
        else:
            box.showMaximized();
            self.maxNormal=  True;
            print '2'
            self.maximize.setIcon(QtGui.QIcon('img/max2.png'));

    def close(self):
        box.close()

    def mousePressEvent(self,event):
        if event.button() == Qt.LeftButton:
            box.moving = True; box.offset = event.pos()

    def mouseMoveEvent(self,event):
        if box.moving: box.move(event.globalPos()-box.offset)

class Frame(QtGui.QFrame):
    def __init__(self, parent=None):
        QtGui.QFrame.__init__(self, parent)
        self.m_mouse_down= False;
        self.setFrameShape(QtGui.QFrame.StyledPanel)
        css = """
        QFrame{
            Background:  #D700D7;
            color:white;
            font:13px ;
            font-weight:bold;
            }
        """
        self.setStyleSheet(css)
        self.setWindowFlags(Qt.FramelessWindowHint);
        self.setMouseTracking(True);
        self.m_titleBar= TitleBar(self);
        self.m_content= QtGui.QWidget(self);
        vbox=QtGui.QVBoxLayout(self);
        vbox.addWidget(self.m_titleBar);
        vbox.setMargin(0);
        vbox.setSpacing(0);
        layout=QtGui.QVBoxLayout(self);
        layout.addWidget(self.m_content);
        layout.setMargin(5);
        layout.setSpacing(0);
        vbox.addLayout(layout);
        # Allows you to access the content area of the frame
        # where widgets and layouts can be added

    def contentWidget(self):
        return self.m_content

    def titleBar(self):
        return self.m_titleBar

    def mousePressEvent(self,event):
        self.m_old_pos = event.pos();
        self.m_mouse_down = event.button()== Qt.LeftButton;

    def mouseMoveEvent(self,event):
        x=event.x();
        y=event.y();

    def mouseReleaseEvent(self,event):
        m_mouse_down=False;

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv);
    box = Frame()
    box.move(60,60);
    l=QtGui.QVBoxLayout(box.contentWidget());
    l.setMargin(0);
    edit=QtGui.QLabel("""I would've did anything for you to show you how much I adored you
But it's over now, it's too late to save our loveJust promise me you'll think of me
Every time you look up in the sky and see a star 'cuz I'm  your star.""");
    l.addWidget(edit)
    box.show()
    app.exec_()

rawDescription: How to customize title bar and window

description: Customize title bar and window


code:
# Define a new signal called 'trigger' that has no arguments.
trigger = QtCore.pyqtSignal()

def connect_and_emit_trigger(self):
    # Connect the trigger signal to a slot.
    self.trigger.connect(self.handle_trigger)

    # Emit the signal.
    self.trigger.emit()

def handle_trigger(self):
    # Show that the slot has been called.

    print "trigger signal received"

rawDescription: I extend QApplication and calling a method after exec_ does not work


code:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

class IdNameMixin(object):
    id = Column(Integer, primary_key=True)
    name = Column(String)

class C1(declarative_base(), IdNameMixin):
    __tablename__ = 'C1'

class C2(declarative_base(), IdNameMixin):
    __tablename__ = 'C2'

print C1.__dict__['id'] is C2.__dict__['id']
print C1.__dict__['name'] is C2.__dict__['name']

rawDescription: SQLAlchemy: avoiding repetition in declarative style class definition


code:
import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip

parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()

print args.url

url = args.url
temp_kmz="temp_kmz"

def extractAll(zipName):
    z = zip(zipName)
    for f in z.namelist():
        if f.endswith('/'):
            os.makedirs(f)
        else:
            z.extract(f)

def download(url, fileName=None):
    def getFileName(url,openUrl):
        if 'Content-Disposition' in openUrl.info():
            # If the response has Content-Disposition, try to get filename from it
            cd = dict(map(
                lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
                openUrl.info()['Content-Disposition'].split(';')))
            if 'filename' in cd:
                filename = cd['filename'].strip("\"'")
                if filename: return filename
        # if no filename was found above, parse it out of the final URL.
        return os.path.basename(urlparse.urlsplit(openUrl.url)[2])

    r = urllib2.urlopen(urllib2.Request(url))
    try:
        fileName = fileName or getFileName(url,r)
        with open(fileName, 'wb') as f:
            shutil.copyfileobj(r,f)
    finally:
        r.close()

def main():
    download(url,temp_kmz)
    extractAll(temp_kmz)
main()

rawDescription: Python error extracting some zip files

description: Python error extracting some zip files


code:
import inspect, sys

print inspect.getsourcefile(sys, sys._getframe(1))

rawDescription: How can I get the name of the script calling the function in python?

description: Get the name of the script calling the function in python


code:
import csv
from tabulate import tabulate
open_file = open('items.csv', 'r+')
all_lines = []
for line in open_file:
    splitline = line.strip().split(',')
    all_lines.append((splitline[0], splitline[1], splitline[2], splitline[3]))

#print all_lines
count = 0
new_lines=[]
for splitline in all_lines:
    in_out = splitline[3]
    dollar_sign = "= $"
    daily_price = "{0:.2f}".format(float(splitline[2]))
    if in_out == "out":
        in_out = str("*")
    else:
        in_out = str("")
    str2='('+splitline[1]+')'
    print count, "- {:<30} {:<30} {}{:<30} {:<10}".format(splitline[0], str2, dollar_sign, daily_price, in_out)
    new_lines.append([splitline[0], str2, dollar_sign, daily_price, in_out])
    count += 1

print tabulate(new_lines, tablefmt="plain")
print
print tabulate(new_lines, tablefmt="plain", numalign="left")

rawDescription: Python - Formatting a print to align a specific column


code:
from bs4 import BeautifulSoup
from requests import get

url = "http://www.berliner-ensemble.de/spielplan"

soup = BeautifulSoup(get(url).content)

iframes = [i.find('iframe') for i in soup('td', {'class': 'Bleft'})]

for i in iframes:
    iframe = BeautifulSoup(get(i.attrs['src']).content)
    columns = [i.text for i in iframe.findAll('td')]
    print columns

rawDescription: data scaping beautifulsoup - How to get?

description: Data scaping beautifulsoup - How to get


code:
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align.Applications import MuscleCommandline

sequences = ["AGGGGC",
             "AGGGC",
             "AGGGGGC",
             "AGGAGC",
             "AGGGGG"]

longest_length = max(len(s) for s in sequences)
padded_sequences = [s.ljust(longest_length, '-') for s in sequences]
records = (SeqRecord(Seq(s)) for s in padded_sequences)

SeqIO.write(records, "msa_example.fasta", "fasta")

from Bio.Align.Applications import MuscleCommandline
cline = MuscleCommandline(input="msa_example.fasta", out="msa.txt")
print cline

rawDescription: Multiple Sequence Alignment with Unequal String Length


code:
from collections import namedtuple
#describe a record
record = namedtuple("record","account password total left active")
#create a list of records
with open("userdb.txt") as f:
    accounts = map(record._make,map(str.split,f))
#convert it to a dict so it is easy to access by account number
user_db = dict((acct.account,acct) for acct in accounts)

#handle the login
def login(user,password):
    user_account = user_db.get(user,None)
    if user_account is None:
        return #failed login user does not exist
    if user_account.password != password:
        return #failed password does not match
    if user_account.active == "1":
        return user_db[user]

#get the user within N retries or fail
def get_user(retries=3):
    for i in range(retries): #retry N times
        selected_user = login(raw_input("Enter Account #:"),raw_input("Enter Pin:"))
        if not selected_user:
            print "Invalid Login Retry!!!"
            continue
        return selected_user

if __name__ == "__main__":
    user = get_user(5)
    if not user:
        print "Sorry Too Many Retries... try again later."
    else:
        print "Logged In As:",user

rawDescription: Simulate ATM login function

description: Simulate ATM login function


code:
result = re.search(your_stuff_here)
if result:
    print result.group(0)

rawDescription: Convert SRE_Match object to string

description: Convert SRE_Match object to string


code:
class Base(object):
    def __init__(self, arg1, arg2, karg1=None, karg2=None):
            print arg1, arg2, karg1, karg2

b = Base(1, 2, karg2='a')
class Derived(Base):
    def __init__(self, *args, **kwargs):
            super(Derived, self).__init__(*args, **kwargs)

d = Derived(1, 2, karg1='Hello, world!')
class Derived2(Base):
    def __init__(self):
            super(Derived2, self).__init__()        # this will fail because Base.__init__ does not have a no-argument signature

d2 = Derived2()

rawDescription: What happens when __init__() gets some arguments


code:
for i in range(256):
    try:
       s = serial.Serial(i)
       print "Found A Serial Port Available At COM%d"%i
    except serial.serialutil.SerialException:
       print "Nothing On COM%d"%i

rawDescription: Python: Determine assigned serial port my hardware connected to


code:
import ctypes
testlib = ctypes.CDLL('/home/user/dir/libfoo.so')
testlib.getText.restype = testlib.getText2.restype = ctypes.c_char_p
print testlib.getText()
print testlib.getText2()

rawDescription: Returning a String from C to Python

description: Returning a String from C to Python


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

class MyTableModel(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = [i for i in range(90)]

    def rowCount(self, parent):
        return len(self.items)
    def columnCount(self, parent):
        return 1

    def data(self, index, role):
        if not index.isValid():
            return QVariant()

        row=index.row()
        column=index.column()

        if role == Qt.DisplayRole:
            if row<len(self.items):
                return QVariant(self.items[row])
            else:
                return QVariant()

        # if role==Qt.BackgroundColorRole:
        #     if row%2: bgColor=QColor(Qt.green)
        #     else: bgColor=QColor(Qt.blue)
        #     return QVariant(QColor(bgColor))

class Proxy01(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy01, self).__init__()

    def filterAcceptsRow(self, row, parent):
        if row%3: return True
        else: return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        self.tablemodel=MyTableModel(self)

        self.proxy1=Proxy01()
        self.proxy1.setSourceModel(self.tablemodel)

        tableviewA=QTableView(self)
        tableviewA.setModel(self.proxy1)
        tableviewA.setSortingEnabled(True)
        tableviewA.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)
        tableviewA.horizontalHeader().setStretchLastSection(True)
        tableviewA.setAlternatingRowColors(True)
        tableviewA.setStyleSheet("alternate-background-color: yellow; background-color: red;");

        layout = QVBoxLayout(self)
        layout.addWidget(tableviewA)

        self.setLayout(layout)

    def test(self, arg):
        print arg

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

rawDescription: How to control QTableView Items Backgound color

description: Control QTableView Items Backgound color


code:
import re

def func1(s):
    print s, "is a nice string"

def func2(s):
    print s, "is a bad string"

funcs = {
    r".*pat1.*": func1,
    r".*pat2.*": func2
}
s = "Some string with both pat1 and pat2"

for pat, func in funcs.items():
    if re.search(pat, s):
        func(s)

rawDescription: Is there a way in python to apply a list of regex patterns that are stored in a list to a single string?


code:
with open('sunspot_data.txt') as sun_file:
    for _ in range(3): # skip first three lines
        sun_file.readline()

    for line in sun_file:
        numbers = line.rstrip().split()
        new_list = map(float, numbers)

        print new_list

rawDescription: Getting rid of quote marks and spaces in a list from a read file


rawDescription: Checking if a list of tuples are the same


code:
x = '0.57 10 0'

#separate the space delimited string into a list containing substrings
y = x.split(' ')

#you can access an element from your list
print y[0]

#or all of the elements
for i in y:
    print i

rawDescription: Remove certain numbers python

description: Remove certain numbers python


code:
import gzip
import io

with io.TextIOWrapper(io.BufferedReader(gzip.open(filename))) as file:
    for line in file:
        print line,

rawDescription: How do you iterate through a gzipped carriage-return file using python 2.7?

description: Iterate through a gzipped carriage-return file using python 2.7


code:
numbers = input("Enter some numbers: ").split()
print set(numbers)

rawDescription: Python checking for number in input

description: Python checking for number in input


code:
class _BaseA(object):
    _intern = object.__dict__.keys()

class A(_BaseA):
    myattribute = 1

print filter(lambda x: x not in A._intern+['__module__'], A.__dict__.keys())

rawDescription: Get class object __dict__ without special attributes


code:
x = '123 Main St'
y = '123 MAIN ST'

new_x = x.lower()
new_y = y.lower()

if new_x == new_y :
   print "correct"
else:
   print "incorrect"

rawDescription: Compare addresses pulled from an XML document in different formats


code:
def sublist(list1, list2):
    n = len(list1)
    return any((list1 == list2[i:i + n]) for i in range(len(list2) - n + 1))

print sublist([2, 3], [1, 2, 3, 4, 5])
print sublist([1, 3], [1, 2, 3, 4, 5])
print sublist([1, 2, 3], [1, 2, 1, 2, 3, 4])

rawDescription: How to check if first list appear in the same order somewhere in the second list

description: Check if first list appear in the same order somewhere in the second list


code:
list = entry.split('.');
print list[0], list[2]

rawDescription: Find Nth item in comma separated list in Python


code:
def list_file():
    return sys.argv[1:]

print list_file()

rawDescription: Expanding asterisk in glob

description: Expanding asterisk in glob


code:
def no_name():
    # return the line of code that generates this outcome instead of printing it
    #to get rid of the None.

    return "Goodbye! Total score: 9 points.\n"

print no_name()

rawDescription: Let a function return nothing, not even "None" in Python 2.7.10 - possible?


code:
with open('abc.txt') as f:
    a = ' '
    while(a):
        a = f.readline()
        l = a.find(':abc_efg') #Gives a non-negative value when there is a match
        if ( l >= 0 ):
            print f.readline()

rawDescription: Match the pattern and print next line

description: Match the pattern and print next line


code:
import sys
from collections import Counter
lines = open(sys.argv[1], 'r').readlines()
c = Counter()
for line in lines:
    for work in line.strip().split():
        c.update(work)
for ind in c:
    print ind, c[ind]

rawDescription: Python - Counting Words In A Text File

description: Python - Counting Words In A Text File


code:
import csv
import re

class Score(object):
    def __init__(self, *args):
        self.lst = args

    def __repr__(self):
        return str(tuple(self.lst))

    def __iadd__(self, other):
        new = [self.lst[i] + other.lst[i] for i in range(3)]
        return Score(*new)

lis__ = [
    'Hi this is an example, this site is nice!.',
    'I hope someone can help',
]

# Build word_scores dictionary, keyed by word
word_scores = {}
with open('yourcsv.csv') as f:
    reader = csv.reader(f)
    for line in reader:
        word_scores[line[0].lower()] = Score(*map(int, line[1:]))

# Loop over lis__, computing the total score for each element (elem_score),
#    append it to line_scores
line_scores = []
for elem in lis__:
    elem_score = Score(0,0,0)
    for word in re.split(r'[^\w]+', elem):
        try:
            score = word_scores[word.lower()]
            print("  Found: %s %s" % (word.lower(), score))
            elem_score += score
        except KeyError:
            pass
    print("%s : %s" % (elem_score, elem))
    line_scores.append(elem_score)

print
print "Line Scores:"
print line_scores

rawDescription: How to return the sum of the values for a given .csv file?

description: Return the sum of the values for a given .csv file


code:
import pandas as pd
from itertools import groupby
from collections import OrderedDict
import json

df = pd.read_csv('SampleCsvFile.csv', dtype={
            "zipcode" : str,
            "date" : str,
            "state" : str,
            "val1" : str,
            "val2" : str,
            "val3" : str,
            "val4" : str,
            "val5" : str
        })

results = []

for (zipcode, state), bag in df.groupby(["zipcode", "state"]):
    contents_df = bag.drop(["zipcode", "state"], axis=1)
    subset = [OrderedDict(row) for i,row in contents_df.iterrows()]
    results.append(OrderedDict([("zipcode", zipcode),
                                ("state", state),
                                ("subset", subset)]))

print json.dumps(results[0], indent=4)
#with open('ExpectedJsonFile.json', 'w') as outfile:
#    outfile.write(json.dumps(results[0], indent=4))

rawDescription: Convert CSV to JSON (in specific format) using Python


code:
for a in range(2, 3000):
    if all(a % k == 0 for k in range(1,11)):
        print a

rawDescription: elegant way of using a range using an if statement?

description: Elegant way of using a range using an if statement


rawDescription: Force python to not output a float in standard form / scientific notation / exponential form


code:
import numpy as np
import pandas as pd

dates = np.asarray(pd.date_range('1/1/2000', periods=8))
df1 = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])
df2 = df1.copy()
df3 = df1.copy()
df = df1.append([df2, df3])

print df

rawDescription: append multiple pandas data frames at once

description: Append multiple pandas data frames at once


code:
import csv, glob, sys

from collections import defaultdict

files = [open(f) for f in glob.glob('user./[1-29].csv')]
#files = [open('input1','r')]
counts = defaultdict(int)

for f in files:
    r = csv.reader(f)
    for line in r:
        for num in line:
            counts[int(num)] += 1

for key,val in sorted(counts.items()):
    print key, val

rawDescription: Multiple CSV Python sort

description: Multiple CSV Python sort


code:
with open('test.txt') as input_data:
    # Skips text before the beginning of the interesting block:
    for line in input_data:
        if line.strip() == 'Start':  # Or whatever test is needed
            break
    # Reads text until the end of the block:
    for line in input_data:  # This keeps reading the file
        if line.strip() == 'End':
            break
        print line  # Line is extracted (or block_of_lines.append(line), etc.)

rawDescription: python - Read file from and to specific lines of text

description: Python - Read file from and to specific lines of text


code:
from itertools import permutations
values = graph['foobar']
result = {x[0]:x[1:] for x in permutations(values)}
print result

rawDescription: Selectively printing values from a python dictionary


code:
def g(t):
    res = []
    for i,x in enumerate(t[:-1]):
        res.append((x, t[-1][i]))
        res.append((x, t[-1][i+1]))
    return res

def h(t):
    if isinstance((t[-1]), tuple):
        return g(t) + h(t[-1])
    else:
        return []

t = (4, (3, 5, (2, 4, 6, (1, 3, 5, 7))))
print h(t)

rawDescription: From nested tuples to edges of a binomial tree, in Python


code:
s = ["a1","a2","a1","a3","a2"]
duplicates = [x for x in s if s.count(x) > 1]
myl = list(set(duplicates))
print myl

rawDescription: Check for two identical string elements in a Python array


code:
from xml.dom.minidom import parseString

xmlDoc = parseString("""<document goes here>""")
lNodesWithLevel2 = [lNode for lNode in xmlDoc.getElementsByTagName("l")
                    if lNode.getAttribute("k") == "Level2"]

matchingVNodes = map(lambda lNode: lNode.getElementsByTagName("v"), lNodesWithLevel2)

print map(lambda vNode: vNode.firstChild.nodeValue, matchingVNodes)
# Prints [u'Intermediate Level']

rawDescription: how do I filter values from XML file in python

description: Filter values from XML file in python


code:
from sqlalchemy.orm.collections import column_mapped_collection, attribute_mapped_collection, mapped_collection

mapper(Item, items_table, properties={
    # key by column
    'notes': relation(Note, collection_class=column_mapped_collection(notes_table.c.keyword)),
    # or named attribute
    'notes2': relation(Note, collection_class=attribute_mapped_collection('keyword')),
    # or any callable
    'notes3': relation(Note, collection_class=mapped_collection(lambda entity: entity.a + entity.b))
})

# ...
item = Item()
item.notes['color'] = Note('color', 'blue')
print item.notes['color']

rawDescription: Setting up relations/mappings for a SQLAlchemy many-to-many database


code:
import numpy as np
data  = np.array([[0,1,2,3,4,7,6,7,8,9,10],
     [3,3,3,4,7,7,7,8,11,12,11],
     [3,3,3,5,7,7,7,9,11,11,11],
     [3,4,3,6,7,7,7,10,11,17,11],
     [4,5,6,7,7,9,10,11,11,11,11]])
required = np.where(data==11)

coords = zip(required[0], required[1]) #Create pairs of indices as tuples
for i in np.random.choice(len(coords), 3, replace=False): #Pick random index values for coords
    print coords[i] #May want to do something other than printing here.

rawDescription: Randomly select index positions in numpy


code:
import sqlite3

conn = sqlite3.connect(':memory:')
conn.execute("""CREATE TABLE Widget (id INTEGER PRIMARY KEY,
                                     serial_number INTEGER,
                                     description TEXT);""")

my_data = [ [143, 800, 'Ask'],
            [213, 457, 'Comment'],
            [424, 800, 'Hi'] ]

for row in my_data:
    conn.execute("INSERT INTO Widget (id, serial_number, description) VALUES (?,?,?);" , row )

conn.commit() # save changes

res = conn.execute("SELECT * FROM Widget WHERE id=143")
row = res.next()
print row #prints (143, 800, u'Ask')

rawDescription: File operation is slower is there a faster look up method in Python?


code: print "a.py exited with code {0}".format(proc.returncode)

rawDescription: how to catch the sys.exit value in one program from other program in python

description: Catch the sys.exit value in one program from other program in python


code:
from numpy import matrix
from numpy import linalg
A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix.
x = matrix( [[1],[2],[3]] )                  # Creates a matrix (like a column vector).
y = matrix( [[1,2,3]] )                      # Creates a matrix (like a row vector).
print A.T                                    # Transpose of A.
print A*x                                    # Matrix multiplication of A and x.
print A.I                                    # Inverse of A.
print linalg.solve(A, x)     # Solve the linear equation system.

rawDescription: Python Inverse of a Matrix

description: Python Inverse of a Matrix


code:
from curses.ascii import ctrl
print ctrl(']')

rawDescription: How to enter the escape characters for telnet programmatically?

description: Enter the escape characters for telnet programmatically


code:
from django.shortcuts import render
from multiprocessing import Process
import time

def do_job(seconds):
    """
    This is the function that will run your three-hour job.
    """
    time.sleep(seconds)                     # just sleep to imitate a long job
    print 'done!'                           # will go to stdout, so you will see this
                                            # most easily in test on local server

def test(request):
    """
    This is your view.
    """
    # In place of this comment, check the database.
    # If job already running, return appropriate template.
    p = Process(target=do_job, args=(15,))  # sleep for 15 seconds
    p.start()                               # but do not join
    message = 'Process started.'
    return render(request, 'test.html',
                  {'message': message})

rawDescription: Running an long-running Python function from Django


code:
# ahead
def ahead():
    if 'theObject' not in globals():
        print "You need an object before you can proceed."
        opening()

    else:
        print "You walk for a while."
        print "Eventually you find a door to exit the Labrynthe with a code above it"
        print "You must translate the code using the code translator."
        print theObject
        if theObject == "map":
            print "You gotta get the code."
            dead("You walked too far to return. You die of exhaustion")
        elif theObject == "code":
            print "You say out aloud the magic open words 'open sesame!'"
            complete()
        else:
            print "Grab the code from the left room."
            opening()

rawDescription: Using logic to check if a variable is set


code:
jars, ops = map(int, input().split())
candies = 0
for i in range(ops):
    a, b, k = map(int, input().split())
    candies += k*(b-a+1)
print candies / jars

rawDescription: Optimization of a program in python

description: Optimization of a program in python


code:
for thing in (Thing1,Thing2,Thing3):
    try:
       thing()
       break  #break out of loop, don't execute else clause
    except:   #BARE EXCEPT IS USUALLY A BAD IDEA!
       pass
else:
    print "nothing worked"

rawDescription: Try until something works

description: Try until something works


code:
class SMSStore(object):
    def __init__(self):
        self.store = []
        self.message_count = 0

    def add_new_arrival(self,number,time,text):
        self.store.append(("From: "+number, "Recieved: "+time,"Msg: "+text))
        self.message_count += 1

    def delete(self, i):
        if i >= len(store):
            raise IndexError
        else:
            del self.store[i]
            self.message_count -= 1

sms_store = SMSStore()
sms_store.add_new_arrival("1234", "now", "lorem ipsum")
try:
    sms_store.delete(20)
except IndexError:
    print("Index does not exist")

print sms_store.store

# multiple separate stores
sms_store2 = SMSStore()
sms_store2.add_new_arrival("4321", "then", "lorem ipsum")
print sms_store2.store

rawDescription: Python global variable and class functionality


code:
import os

if not os.path.exists('readfile.csv'):
    with open("readfile.csv", "wb") as read_f:
        with open("position.dat", "wb") as pos_fi:

                read_f.write('aaa,111\n')
                read_f.write('bbb,222\n')
                read_f.write('ccc,333\n')
                read_f.write('ddd,444\n')

                pos_fi.write('0')

data = {}

with open('position.dat', 'rb') as pos_f:
    ps = pos_f.read()
    print 'Position is : ', ps
    p = int(ps)

# open your data file and the position file
with open('readfile.csv', 'rb') as read_f:

        # read the offset position and seek to that location
        read_f.seek(p)
        for line in iter(read_f.readline, ''):
                num,name = line.strip().split(',')
                print num, name
                data[num] = name

        position = str(read_f.tell())

# store your new offset position
with open("position.dat", "wb") as pos:
        pos.write(position)

rawDescription: Python CSV file read and write with incremental number


code:
def wrap(f):
    def wrapped(*args, **kwargs):
        print "f was called"
        return f(*args, **kwargs)

    return wrapped

@wrap
def foo(x):
    return foo(x - 1) if x > 0 else 1

rawDescription: Python: wrapping recursive functions

description: Python: wrapping recursive functions


code:
from scipy.optimize import fsolve
import math

def equations(p):
    x, y = p
    return (x+y**2-4, math.exp(x) + x*y - 3)

x, y =  fsolve(equations, (1, 1))

print equations((x, y))

rawDescription: How to solve a pair of nonlinear equations using Python?

description: Solve a pair of nonlinear equations using Python


code:
def convert_mp3_to_wav(input_filename, output_filename):

    if sys.platform.startswith("win"):
        # Don't display the Windows GPF dialog if the invoked program dies.
        # See comp.os.ms-windows.programmer.win32
        # How to suppress crash notification dialog?, Jan 14,2004 -
        # Raymond Chen's response [1]

        import ctypes
        SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN
        ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX);
        subprocess_flags = 0x8000000 #win32con.CREATE_NO_WINDOW?
    else:
        subprocess_flags = 0

    """
    converts the incoming mp3 file to wave file
    """
    if not os.path.exists(input_filename):
        raise AudioProcessingException, "file %s does not exist" % input_filename

    #exec("lame {$tmpname}_o.mp3 -f {$tmpname}.mp3 && lame --decode {$tmpname}.mp3 {$tmpname}.wav");
    command = ["lame", "--silent", "--decode", input_filename, output_filename]

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
    (stdout, stderr) = process.communicate()

    if process.returncode != 0 or not os.path.exists(output_filename):
        raise AudioProcessingException, stdout

    return output_filename

rawDescription: Handling subprocess crash in Windows

description: Handling subprocess crash in Windows


code:
print d
def whichHalf(t):
    t['Div'] = 'Train'
    t[:len(t)/2]['Div'] = 'Test'
    return t
d.groupby('Cat').apply(whichHalf)

rawDescription: How can I make a DataFrame containing half of the data from another DataFrame, distributed evenly across values in a column?

description: Make a DataFrame containing half of the data from another DataFrame, distributed evenly across values in a column


code:
import re

p = re.compile('(?P<name>[^|]*)\|(?P<desc>[^|]*)')
m = p.match('jack|a good boy')
name = m.group('name')
desc = m.group('desc')
print name  # jack
print desc  # a good boy

rawDescription: python regular expression group matching with anti '|'

description: Python regular expression group matching with anti '|'


code:
#!/usr/bin/env python
"""Usage:
./extract.py test.zip
"""

from zipfile import ZipFile
import os
import sys
import tempfile
import shutil

ROOT_PATH = 'xl/media/'

zip_name = sys.argv[1]
zip_path = os.path.abspath(zip_name)
extraction_dir = os.path.join(os.getcwd(), os.path.splitext(zip_name)[0])
temp_dir = tempfile.mkdtemp()

with ZipFile(zip_path, 'r') as zip_file:
    # Build a list of only the members below ROOT_PATH
    members = zip_file.namelist()
    members_to_extract = [m for m in members if m.startswith(ROOT_PATH)]
    # Extract only those members to the temp directory
    zip_file.extractall(temp_dir, members_to_extract)
    # Move the extracted ROOT_PATH directory to its final location
    shutil.move(os.path.join(temp_dir, ROOT_PATH), extraction_dir)

# Uncomment if you want to delete the original zip file
# os.remove(zip_path)

print "Sucessfully extracted '%s' to '%s'" % (zip_path, extraction_dir)

rawDescription: Extract and rename zip file folder

description: Extract and rename zip file folder


code:
import time

tt = time.strptime("19/Jun/2014:00:03:09","%d/%b/%Y:%H:%M:%S")
print time.mktime(tt)

rawDescription: python convert human to timestamp

description: Python convert human to timestamp


code:
from flask import Flask, render_template_string
from flask.ext.wtf import Form
from wtforms import FieldList, StringField

app = Flask(__name__)
app.secret_key = 'TEST'

class TestForm(Form):
    person = FieldList(StringField('Person'), min_entries=3, max_entries=10)
    foo = StringField('Test')

@app.route('/', methods=['POST', 'GET'])
def example():
    form = TestForm()
    if form.validate_on_submit():
        print form.person.data
        ## [u'One', u'Two', u'Three']

    return render_template_string(
        """
            <form method="post" action="#">
            {{ form.hidden_tag() }}
            {{ form.person }}
            <input type="submit"/>
            </form>
        """, form=form)

if __name__ == '__main__':
    app.run(debug=True)

rawDescription: Multiple instances of the same form field


code:
names = ['Agrajag', 'Colin', 'Deep Thought', 'Ford Prefect' , 'Zaphod Beeblebrox', 'Zarquon']
num_pages = 3

def group_names(names, num_pages):
    letter_groups = defaultdict(list)
    for name in names: letter_groups[name[0].upper()].append(name)
    letter_groups = [letter_groups[key] for key in sorted(letter_groups.keys())]
    current_group = []
    page_groups = []
    group_size = len(names) / num_pages
    for group in letter_groups:
        current_group.extend(group)
        if len(current_group) > group_size:
            page_groups.append(current_group)
            current_group = []
    if current_group: page_groups.append(current_group)

    return page_groups

print group_names(names, num_pages)

rawDescription: Python: partition list of names into equally sized sublists


code:
dates = [datetime.date(2015, 11, 22), datetime.date(2015, 11, 23), datetime.date(2015, 11, 24)]
points = [3L, 1L, 2L]
x = [x for x in range(0,len(points))]
plt.plot(points, 'r-o')
plt.xticks(rotation=70)
plt.xticks(x,dates)
plt.tight_layout()
plt.savefig('test.pdf')

rawDescription: Show x-axis on Matplotlib - Python

description: Show x-axis on Matplotlib - Python


code:
class MyClass(object):
    def __init__(self, val):
        self.val = val
    def __nonzero__(self):
        return self.val != 0  #This is an example, you can use any condition

x = MyClass(0)
if not x:
    print 'x is false'

rawDescription: Assign class boolean value in Python

description: Assign class boolean value in Python


code:
cursor = db.cursor()
count = cursor.execute("SELECT COUNT(*) FROM " + str(table))
db.commit()
print cursor.fetchone()[0]
cursor.close()

rawDescription: how print count table sql in python

description: How print count table sql in python


code:
from collections import Counter
from collections import defaultdict

tag_corpus = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat']]

co_occurences = defaultdict(Counter)
for tags in tag_corpus:
    for key in tags:
        co_occurences[key].update(tags)
unique_tags = sorted(co_occurences)

print co_occurences
print unique_tags

rawDescription: How to efficiently tally co-occurrences in python list of lists

description: Efficiently tally co-occurrences in python list of lists


code:
import xml.dom.minidom as  md

def print_node(root):
    if root.childNodes:
        for node in root.childNodes:
           if node.nodeType == node.ELEMENT_NODE:
               print node.tagName,"has value:",  node.nodeValue, "and is child of:", node.parentNode.tagName
               print_node(node)

dom = md.parse("ASL.xml")
root = dom.documentElement
print_node(root)

rawDescription: Walk through all XML nodes in an element-nested structure


code:
for a in soup.select('div.coLeft.cate.mBottom dd a[href]'):
    #                                         ^^
    print "http://www.brothersoft.com"+ a['href']

rawDescription: How to get the item inside dd?

description: Get the item inside dd


code:
class CloudContainers(object):
    def __init__(self, username, key, originContainerName, targetContainerName):
        self.username = username
        self.key = key
        self.originContainerName = originContainerName
        self.targetContainerName = targetContainerName

    def cloudConnect(self):
        print "Creating connection"
        self.connection = cloudfiles.get_connection(self.username,self.key,servicenet=True)
        print "-- [DONE]"
        print "Accessing containers"
        self.originContainer = connection.create_container(self.originContainerName)
        self.targetContainer = connection.create_container(self.targetContainerName)
        print "-- [DONE]"
        return

    def uploadImg(self, new_name):
        new_obj = self.targetContainer.create_object(new_name)
        new_obj.content_type = 'image/jpeg'
        new_obj.load_from_filename("up/"+new_name)

    def getImg(name):
        obj = self.originContainer.get_object(name)
        obj.save_to_filename("down/"+name)

rawDescription: Global "connection"-like variables in Python


code:
import re
import pprint
import sys

class Despacho(object):
    """
    Class to parse each line, applying the regexp and storing the results
    for future use
    """
    #used a dict with the keys instead of functions.
    regexp = {
        ('processo',
         'data',
         'despacho'): re.compile(r'No.([\d]{9})  ([\d]{2}/[\d]{2}/[\d]{4})  (.*)'),
        ('titular',): re.compile(r'Tit.(.*)'),
        ('procurador',): re.compile(r'Procurador: (.*)'),
        ('documento',): re.compile(r'C.N.P.J./C.I.C./N INPI :(.*)'),
        ('apresentacao',
         'natureza'): re.compile(r'Apres.: (.*) ; Nat.: (.*)'),
        ('marca',): re.compile(r'Marca: (.*)'),
        ('classe',): re.compile(r'Clas.Prod/Serv: (.*)'),
        ('complemento',): re.compile(r'\*(.*)'),
    }

    def __init__(self):
        """
        'complemento' is the only field that can be multiple in a single registry
        """
        self.complemento = []

    def read(self, line):
        for attrs, pattern in Despacho.regexp.iteritems():
            m = pattern.match(line)
            if m:
                for groupn, attr in enumerate(attrs):
                    # special case complemento:
                    if attr == 'complemento':
                        self.complemento.append(m.group(groupn + 1))
                    else:
                        # set the attribute on the object
                        setattr(self, attr, m.group(groupn + 1))

    def __repr__(self):
        # defines object printed representation
        d = {}
        for attrs in self.regexp:
            for attr in attrs:
                d[attr] = getattr(self, attr, None)
        return pprint.pformat(d)

def process(rpi):
    """
    read data and process each group
    """
    #Useless line, since you're doing a for anyway
    #rpi = (line for line in rpi)
    group = False

    for line in rpi:
        if line.startswith('No.'):
            group = True
            d = Despacho()

        if not line.strip() and group: # empty line - end of block
            yield d
            group = False

        d.read(line)

def main():
    arquivo = open('rm1972.txt') # file to process
    for desp in process(arquivo):
        print desp # can print directly here.
        print('-' * 20)
    return 0

if __name__ == '__main__':
    main()

rawDescription: Extracting info from large structured text files


code:
import random

lst = ['!','-','=','~','|']
string = 'Hello world. Hello world.'

print ''.join('%s%s' % (x, random.choice(lst) if random.random() > 0.5 else '') for x in string)

rawDescription: Python: Inserting characters between other characters at random points


code:
grid = ['ABCD','EFGH','IJKL','MNOP']
seq = ['GB','AB','CD']
for seq_elem in seq:
  print any(seq_elem[0] in row and seq_elem[1] in row for row in grid)

rawDescription: Python 3 - Find two letters in same row

description: Python 3 - Find two letters in same row


code:
#!/usr/bin/python
import pygtk, gtk, gobject

class GTK_Main:

  def __init__(self):

    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

    # if i use set_size_request, the first launch is not running
    # in correct screen (while using dual screen)
    #self.window.set_size_request(1280, 720)
    # therefore by using default size it fix it
    self.window.set_default_size(1280, 720)

    self.window.connect("destroy", gtk.main_quit, "WM destroy")

    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)
    self.hbox.pack_start(self.button3, False)
    self.hbox.add(gtk.Label())
    self.window.show_all()
    # another.anon.coward: mentioned a great way solve it
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      print "unmaximize"
      self.window.unmaximize()

    self.window.move(0,0)

  def buttononTop(self, w):
    print "Buttons on top - redraw with different shapes includeing the window"
    self.window.remove(self.vbox)
    self.window.resize(1280, 100)
    # if i use set_size_request, the first launch is not running
    # in correct screen (while using dual screen)
    #self.window.set_size_request(1280, 720)
    # therefore by using default size it fix it
    self.window.set_default_size(1280, 100)

    # another.anon.coward: mentioned a great way solve it
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      print "unmaximize"
      self.window.unmaximize()

    self.window.move(0,0)
    self.window.show_all()

    # ------------------------------------------- [FAILS STARTS]
    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)
    self.hbox.pack_start(self.button3, False)
    self.hbox.add(gtk.Label())
    # ------------------------------------------- [FAILS FIXED]
    self.vbox.show_all()

  def exit(self, widget, data=None):
    gtk.main_quit()

GTK_Main()
gtk.gdk.threads_init()
gtk.main()

rawDescription: Python - Why after removing the widget, and then adding it from another method fails to add the same widgets?


code: print time.strftime("%Z - %Y/%m/%d, %H:%M:%S", time.localtime(time.time()))

rawDescription: Convert any epoch to human readable format for system timezone


code:
f = open('test.txt', 'r')
lines = f.readlines()
f.close()
rows = []
for line in lines:
    rows.append(line.split(' '))
print rows[2][1]

rawDescription: Python - Select specific value from test file


code:
import os
import sys

def main():
    if os.getpgrp() == os.tcgetpgrp(sys.stdout.fileno()):
        print 'Running in foreground.'
    else:
        print 'Running in background.'

if __name__ == '__main__':
    main()

rawDescription: How to detect if python script is being run as a background process

description: Detect if python script is being run as a background process


code:
from biom import example_table
axis = 'sample'
print example_table.ids(axis).size

rawDescription: is it possible to get the length of an axis directly in a biom.Table object?

description: Is it possible to get the length of an axis directly in a biom.Table object


code:
my_str = "hello world"
words = my_str.split()
print words[1]

rawDescription: Indexing by word in a list

description: Indexing by word in a list


code:
def collega_GUI(widget, args=()):

    builder.get_object('Trovarete_Window').show_all()
    progressbar.set_pulse_step(0.20)

    pulse()
    t = threading.Thread(target=worker)
    t.setDaemon(True)
    t.start()
    wait_for_t(t)

def wait_for_t(t):
   if not t.is_alive():
       t.join() # This won't block, since the thread isn't alive anymore
       print 'a'
       # Do whatever else you would do when join() (or maybe collega_GUI?) returns
   else:
       Gobject.timeout_add(200, wait_for_t, t)

rawDescription: GObject.idle_add(), thread.join() and my program hangs


code:
def foo():
    foo.bar = foo.bar + 1

foo.bar = 0

foo()
foo()
foo()

print foo.bar # prints 3

rawDescription: Does Python have something like Perl 5.10's "state" variables?


code:
#!/usr/bin/env python

import gobject
import time  # This is just used for slowing down the threads
import os
import threading
import gtk

class FileBrowser:

    def __init__(self):
        self.window = gtk.Window()
        self.window.show_all()
        self.window.connect("destroy", self._destroy)

        box = gtk.VBox()
        box.show()

        self.scrolled_w = gtk.ScrolledWindow()
        self.scrolled_w.show()
        self.window.add(box)

        self.status = gtk.Label("...")  # I added a status label to see when scan is done
        self.status.show()
        box.pack_start(self.status, False, False, 2)

        self.button = gtk.Button("start")
        self.button.show()
        self.button.connect("clicked",self.start_scanning)
        self.button.set_size_request(30,50)
        box.pack_start(self.button,False,False,4)

        self.model=gtk.TreeStore(str)
        self.treeview = gtk.TreeView(self.model)
        self.treeview.show()

        col = gtk.TreeViewColumn("FileName")
        cell = gtk.CellRendererText()

        self.treeview.append_column(col)
        col.pack_start(cell,0)
        col.set_attributes(cell,text=0)

        box.pack_start(self.scrolled_w,10)
        self.scrolled_w.add(self.treeview)
        self.window.set_size_request(600,300)

        self.threads = list()  # I made this be part of the full application
        """ That allows us to wait for all threads on close-down, don't know how
        necessary it is."""

    def start_scanning(self,w):

        self.button.set_sensitive(False)  # To disallow stacking scan-stats
        self.status.set_text("Scanning...")  # Let user know it is started
        self.model.clear()
        main_dir= os.path.expanduser("~")  # I just wanted my home-dir instead
        list_dir = os.listdir(main_dir)
        no_of_threads = len(list_dir)
        for i in range(no_of_threads):
            t = threading.Thread(target=self._thread_scanning,args=(main_dir,list_dir[i],))
            self.threads.append(t)
            t.start()

        gobject.timeout_add(200, self._callback)  # This will cause the main app to
        #check every 200 ms if the threads are done.

    def _callback(self):

        if threading.active_count() == 1:  # If only one left, scanning is done
            self.status.set_text("Done!")
            self.button.set_sensitive(True)  # Allow button being pressed again
            return False  # False make callback stop

        print threading.active_count()
        return True

    def _thread_scanning(self,main_d,list_d):
        path = os.sep.join((main_d, list_d))  # Made use of os's sep instead...
        if os.path.isdir(path):
            list_subd = os.listdir(path)
            par = self.model.append(None,[list_d])
            for sub in list_subd:
                self.model.append(par,[sub])

        time.sleep(3)  # Useless other than to delay finish of thread.

    def main(self):

        gtk.main()

    def _destroy(self, *args, **kwargs):

        #Own destroy that waits for all threads...
        for t in self.threads:
            t.join()

        gtk.main_quit(*args, **kwargs)

if __name__=="__main__":
    gobject.threads_init()
    fb=FileBrowser()
    fb.main()

rawDescription: How to refresh the tree view using python?

description: Refresh the tree view using python


rawDescription: Split list of tuples based on value within tuple


code:
#!/usr/bin/env python
from google import gsearch
import math,sys
import json
import urllib

def gsearch(searchfor):
  query = urllib.urlencode({'q': searchfor})
  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  search_response = urllib.urlopen(url)
  search_results = search_response.read()
  results = json.loads(search_results)
  data = results['responseData']
  return data

args = sys.argv[1:]
m = 45000000000
if len(args) != 2:
        print "need two words as arguments"
        exit
n0 = int(gsearch(args[0])['cursor']['estimatedResultCount'])
n1 = int(gsearch(args[1])['cursor']['estimatedResultCount'])
n2 = int(gsearch(args[0]+" "+args[1])['cursor']['estimatedResultCount'])
l1 = max(math.log10(n0),math.log10(n1))-math.log10(n2)
l2 = math.log10(m)-min(math.log10(n0),math.log10(n1))
distance = l1/l2
print distance

rawDescription: Python help in ImportError: No module named google


code:
#get minimum and maximum stock options

result = min(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('inf'))
print result

result = max(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('-inf'))
print result

rawDescription: python getting min/max values from nested dictionary

description: Python getting min/max values from nested dictionary


code:
with open(fname, 'r') as fin:
    print fin.read()

rawDescription: Python: printing a file to stdout

description: Python: printing a file to stdout


code:
from collections import Counter
from glob import iglob
import re
import os

def removegarbage(text):
    # Replace one or more non-word (non-alphanumeric) chars with a space
    text = re.sub(r'\W+', ' ', text)
    text = text.lower()
    return text

topwords = 100
folderpath = 'path/to/directory'
counter = Counter()
for filepath in iglob(os.path.join(folderpath, '*.txt')):
    with open(filepath) as filehandle:
        counter.update( removegarbage(filehandle.read()).split() )

for word, count in counter.most_common(topwords):
    print '{}: {}'.format(count, word)

rawDescription: unique word frequency in multiple files

description: Unique word frequency in multiple files


code:
import urllib
from multiprocessing import Process, Queue

def worker(input, output):
  for func, args in iter(input.get, 'STOP'):
    result = func(*args)
    output.put(result)

def find_on_page(num):
  uri = 'http://www.example.com/index.php?id=%d' % num
  f = urllib.urlopen(uri)
  data = f.read()
  f.close()
  index = data.find('datahere:') # obviously use your own methods
  if index < 0:
    return None
  else:
    return data[index:index+20]

def main():
  NUM_PROCESSES = 4
  tasks = [(find_on_page, (i,)) for i in range(99999)]
  task_queue = Queue()
  done_queue = Queue()
  for task in tasks:
    task_queue.put(task)
  for i in range(NUM_PROCESSES):
    Process(target=worker, args=(task_queue, done_queue)).start()
  for i in range(99999):
    print done_queue.get()
  for i in range(NUM_PROCESSES):
    task_queue.put('STOP')

if __name__ == "__main__":
  main()

rawDescription: C++ or Python: Bruteforcing to find text in many webpages


code:
class BaseFoo(object):
    def __init__(self):
        raise NotImplementedError('No instances of BaseFoo please.')
    def _foo(self):
        return 'Foo.'
    def _bar(self):
        return 'Bar.'

class HiddenFoo(BaseFoo):
    def __init__(self): pass

class PublicFoo(BaseFoo):
    def __init__(self): pass
    foo = BaseFoo._foo
    bar = BaseFoo._bar

def try_foobar(instance):
    print 'Trying ' + instance.__class__.__name__
    try:
        print 'foo: ' + instance.foo
        print 'bar: ' + instance.bar
    except AttributeError, e:
        print e

foo_1 = HiddenFoo()
foo_2 = PublicFoo()
try_foobar(foo_1)
try_foobar(foo_2)

rawDescription: Python Classes: turn all inherited methods private


code:
class DownloadJobResults(VirtualDownloadView):

    def get_file(self):

        print "Job is {}".format(self.kwargs.get("job",None))

        file = StringIO.StringIO()
        file.write("test 1,2,3,4\n")
        file.write("test 5,6,7,8\n")
        file.seek(0)
        return VirtualFile(file, name='sweet.txt')

rawDescription: In Django, how do you pass parameters from the URL to get_file() in VirtualDownloadView?


code: [v[0] for v in sorted(d.iteritems(), key=lambda(k, v): (-v, k))]

rawDescription: Sorting a dictionary by value then key

description: Sorting a dictionary by value then key


code:
import collections

words = ['the', 'cat','chased', 'the', 'dog', 'fled']
result = collections.defaultdict(dict)

for i in range(len(words) - 1):   # loop till second to last word
    occurs = result[words[i]]    # get the dict containing the words that follow and their freqs
    new_freq = occurs.get(words[i+1], 0) + 1  # update the freqs
    occurs[words[i+1]] = new_freq

print list(result.items())

rawDescription: Defaultdict(defaultdict) for text analysis


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

class DataModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.modelDict={}
        self.items=[]
    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)

    def columnCount(self, index=QtCore.QModelIndex()):
        return 4

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)): return QtCore.QVariant()
        key=str(self.items[index.row()])
        column=index.column()

        if role==QtCore.Qt.ItemDataRole: return self.modelDict.get(str(index.data().toString()))

        if role==QtCore.Qt.DisplayRole:
            if column==0:
                return key
            else:
                return self.modelDict.get(key).get(column)

    def addItem(self, itemName=None, column=0):
        totalItems=self.rowCount()+1
        self.beginInsertRows(QtCore.QModelIndex(), totalItems, column)
        if not itemName:            itemName='Item %s'%self.rowCount()
        self.items.append(itemName)
        self.endInsertRows()

    def buildItems(self):
        for key in self.modelDict:
            index=QtCore.QModelIndex()
            self.addItem(key)

class ProxyTableModel(QtGui.QSortFilterProxyModel):
    def __init__(self, parent=None):
        super(ProxyTableModel, self).__init__(parent)

    def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.TextAlignmentRole:
            if orientation == QtCore.Qt.Horizontal:
                return QtCore.QVariant(int(QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter))
            return QtCore.QVariant(int(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter))
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if orientation==QtCore.Qt.Horizontal:
            if column==1:
                return QtCore.QVariant("Species 1")
            elif column==2:
                return QtCore.QVariant("Species 2")
            elif column==3:
                return QtCore.QVariant("Species 3")

        return QtCore.QVariant(int(column + 1))

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        mainLayout=QtGui.QHBoxLayout()
        self.setLayout(mainLayout)

        self.dataModel=DataModel()
        self.dataModel.modelDict=elements
        self.dataModel.buildItems()

        self.proxyModel=ProxyTableModel()
        self.proxyModel.setFilterKeyColumn(0)
        self.proxyModel.setSourceModel(self.dataModel)

        self.viewA=QtGui.QListView()
        self.viewA.setModel(self.dataModel)
        self.viewA.clicked.connect(self.onClick)

        self.viewB=QtGui.QTableView()
        self.viewB.setModel(self.proxyModel)
        self.viewB.setColumnHidden(0,True)

        mainLayout.addWidget(self.viewA)
        mainLayout.addWidget(self.viewB)
        self.show()

    def onClick(self):
        index=self.viewA.currentIndex()
        key=self.dataModel.data(index, QtCore.Qt.DisplayRole)
        value=self.dataModel.data(index, QtCore.Qt.ItemDataRole)
        self.proxyModel.setFilterRegExp('%s'%key)
        print 'onClick(): key: %s'%type('%s'%key)

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

rawDescription: What design logic to use with linked ViewLists


code:
import csv
total = "C:\\...total.txt" #set path to your file!
unique = "C:\\...unique.txt"
newfile = "C:\\...match.csv"

a = []
b = []
towrite = []

with open(total, "r") as rcursor1: #read the document
    for trow in rcursor1: #read each row
        row1 = trow.split("\t") #split it by your seperator
        a.append(row1[1:]) #we are only interested in everything from column b onwards

with open(unique, "r") as rcursor2:
    for urow in rcursor2:
        row2 = urow.split("\t")
        b.append(row2)

print "This is a", a
print len(a)
print "This is b", b
print len(b)

a1 = set(map(tuple, a)) #lists are hashable, but we need unhasable object to work with set
b1 = set(map(tuple, b)) #that why change list to tuples, tuples are not hashable

matches = set(a1).intersection(b1) #find the matches, best is to take shorter list as first argument for better perfomance!
print "Our matches, unsorted!", matches

with open(newfile, 'wb') as wcursor: #write to file
    for i in matches:
        c = list(i)
        d = ",".join(c)
        print d
        wcursor.write(str(d)+"\n")

rawDescription: how to fetch complete row while comparing two files by using python or shell

description: Fetch complete row while comparing two files by using python or shell


code:
list1 = [1,2,3,4] # some list
a = list1.append(5) # append '5' to the list and assign return value to 'a'
print a
list1
list1.append(6)
list1

rawDescription: why I can't append directly after a split in python

description: Why I can't append directly after a split in python


code:
class createedditConvertorpage(QtGui.QMainWindow):
    def __init__(self,parent = None):
        QtGui.QWidget.__init__(self, parent)

    @staticmethod
    def browseFilecsvtoxml():
        directoryPath = QtGui.QFileDialog.getExistingDirectory(caption="Pick a folder", directory=QtCore.QDir.currentPath())
        return directoryPath

    def selectFilecsvtoxml(self):
        directoryPath = self.browseFilecsvtoxml()
        self.listDirPath.setText(directoryPath)

        for file_name in os.listdir(directoryPath):
            if not file_name.startswith("."):

                print (file_name) +  "   this is selectFilcestoxml"

        return directoryPath

    def convertDirectory(self):

        import longXmlEditor
        longXmlEditor.Main()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dirpath = createedditConvertorpage.browseFilecsvtoxml()
    print dirpath
    sys.exit(app.exec_())

rawDescription: PYQT use directory from one class on a script


code:
import pyme.core
import pyme.pygpgme

plaintext = pyme.core.Data('this is a test message')
ciphertext = pyme.core.Data()
ctx = pyme.core.Context()
ctx.set_armor(1)
name = 'me@office.com'
ctx.op_keylist_start(name, 0)
key = ctx.op_keylist_next()
# first argument is message to sign, second argument is buffer where to write
# the signature, third argument is signing mode, see
# http://www.gnupg.org/documentation/manuals/gpgme/Creating-a-Signature.html#Creating-a-Signature for more details.
ctx.op_sign(plaintext, ciphertext, pyme.pygpgme.GPGME_SIG_MODE_CLEAR)
ciphertext.seek(0, 0)
print ciphertext.read()

rawDescription: how do I sign data with pyme?

description: Sign data with pyme


code:
class CTError(Exception):
    def __init__(self, errors):
        self.errors = errors

try:
    O_BINARY = os.O_BINARY
except:
    O_BINARY = 0
READ_FLAGS = os.O_RDONLY | O_BINARY
WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
BUFFER_SIZE = 128*1024

def copyfile(src, dst):
    try:
        fin = os.open(src, READ_FLAGS)
        stat = os.fstat(fin)
        fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
            os.write(fout, x)
    finally:
        try: os.close(fin)
        except: pass
        try: os.close(fout)
        except: pass

def copytree(src, dst, symlinks=False, ignore=[]):
    names = os.listdir(src)

    if not os.path.exists(dst):
        os.makedirs(dst)
    errors = []
    for name in names:
        if name in ignore:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore)
            else:
                copyfile(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except (IOError, os.error), why:
            errors.append((srcname, dstname, str(why)))
        except CTError, err:
            errors.extend(err.errors)
    if errors:
        raise CTError(errors)

rawDescription: Python: How to Copy Files Fast

description: Python: How to Copy Files Fast


code:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=SQLSRV01;DATABASE=DATABASE;UID=USER;PWD=PASSWORD')
cursor = cnxn.cursor()

cursor.execute("SELECT WORK_ORDER.TYPE,WORK_ORDER.STATUS, WORK_ORDER.BASE_ID, WORK_ORDER.LOT_ID FROM WORK_ORDER")
for row in cursor.fetchall():
    print row

rawDescription: Retrieving Data from SQL Using pyodbc

description: Retrieving Data from SQL Using pyodbc


code:
import pyowm
from sys import exit
import time
import RPi.GPIO as GPIO, feedparser, time
from RPLCD import CharLCD, cleared, cursor
import pylirc

sockid = pylirc.init('weather')
allow = pylirc.blocking(0)
owm = pyowm.OWM('API Key')
lcd = CharLCD(pin_rs=26, pin_rw=None, pin_e=24, pins_data=[22, 18, 16, 12],
                  cols=16, rows=2)

class mail(object):
    def __init__(self):
        self.username = "email address"
        self.password = "password"
        self.newmail_offset = 0
        self.current = 0
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(15, GPIO.OUT)
        GPIO.setup(13, GPIO.OUT)
        GPIO.setup(11, GPIO.OUT)

    def buzz(self):
        self.period = 1.0 / 250
        self.delay = self.period / 2
        self.cycles = 250

        for i in range(self.cycles):
            GPIO.output(11, True)
            time.sleep(self.delay)

            GPIO.output(11, False)
            time.sleep(self.delay)

    def check(self):
        self.newmails = int(feedparser.parse("https://" + self.username + ":" +
                            self.password +"@mail.google.com/gmail/feed/atom")
                            ["feed"]["fullcount"])

        if self.newmails > self.newmail_offset:
            GPIO.output(15, True)
            GPIO.output(13, False)

            if self.newmails > self.current:
                self.buzz()
                self.current += 1
        else:
            GPIO.output(15, False)
            GPIO.output(13, True)
            self.current = 0

### will be a class
class weather(object):
    def __init__(self):
        self.w = None
        self.wind = None
        self.windkm = None
        self.humidity = None
        self.temper = None
        self.counter = 0
        self.next = 1

    def update(self):
        if self.counter == 0:
            self.counter = 180
            self.observation = owm.weather_at_place('City, Country')
            self.w = self.observation.get_weather()
            self.wind = self.w.get_wind()
            self.windkm = (self.wind['speed'] * 3600) / 1000
            self.humidity = self.w.get_humidity()
            self.temper = self.w.get_temperature('celsius')
        else:
            pass

    def display_weather(self):
        lcd.cursor_pos = (0, 4)
        lcd.write_string('Weather')
        lcd.cursor_pos = (1, 5)
        lcd.write_string(self.w.get_status())
        time.sleep(3)
        lcd.clear()

    def display_temp(self):
        lcd.cursor_pos = (0, 2)
        lcd.write_string('Temperature')
        lcd.cursor_pos = (1, 6)
        lcd.write_string(str(self.temper['temp']))
        lcd.write_string(' C')
        time.sleep(3)
        lcd.clear()

    def display_hum(self):
        lcd.cursor_pos = (0, 4)
        lcd.write_string('Humidity')
        lcd.cursor_pos = (1, 6)
        lcd.write_string(str(self.humidity))
        lcd.write_string(' %')
        time.sleep(3)
        lcd.clear()

    def display_wind(self):
        lcd.cursor_pos = (0, 3)
        lcd.write_string('Wind Speed')
        lcd.cursor_pos = (1, 4)
        lcd.write_string(str(int(self.windkm)))
        lcd.write_string('km/h')
        time.sleep(3)
        lcd.clear()

    def next_tile(self):
        self.counter -= 1
        self.codeIR = pylirc.nextcode()
        if  not self.codeIR or self.codeIR[0] == self.next:
            if self.next != 4:
                self.next += 1
            else:
                self.next -= 3
        else:
            self.next = int(self.codeIR[0])

email = mail()
weather = weather()
weather.update()
def up_next():
    weather.update()
    weather.next_tile()

while True:
    try:
        while weather.next == 1:
            weather.display_weather()
            up_next()

        while weather.next == 2:
            weather.display_temp()
            up_next()

        while weather.next == 3:
            weather.display_hum()
            up_next()

        while weather.next == 4:
            weather.display_wind()
            email.check()
            up_next()

    except(KeyboardInterrupt, SystemExit):
        print 'quitting'
        lcd.close(clear=True)
        exit()

rawDescription: Python Lirc blocks code even when blocking is off


code:
from dictclient import Connection, Database
from sys import argv

con = Connection("dict.org") #or whatever your server is
db = Database(con, "*") #replace * with ! to get only the first result
def_list = db.define(argv[1]) #list containing Definition objects
for x in def_list:
    print x.getdefstr() + '\n'

rawDescription: How to use the DICT protocol client in Python?

description: Use the DICT protocol client in Python


code:
s = raw_input("What word do you want translated?").strip()
vowel = set("aeiou")
if vowel & set(s):
    while s[0] not in vowel:
        s = s[1:] + s[0]
    print s
else:
    print "Input has no vowels"

rawDescription: Python Program to continuously move consonants to end of string until the first letter is vowel


code:
def is_valid_host(hostname):
    try:
        addr = socket.gethostbyname(hostname)
    except socket.gaierror, ex:
        return False
    return True

hosts = ['abc', 'yahoo.com', 'google.com', 'nosuchagency.gov']
filter(is_valid_host, hosts)

rawDescription: Looping through chars, generating words and checking if domain exists


code:
def print_dict(_dict, indent=""):
    for k, v in _dict.items():
        if hasattr(v, 'items'):
            print "%s(%s::" % (indent, k)
            print_dict(v, indent + "  ")
            print "%s)" % indent
        elif isinstance(v, list):
            print "%s(%s::" % (indent, k)
            for i in v:
                print_dict(dict(i), indent + "  ")
            print "%s)" % indent
        else:
            print "%s(%s::%s)" % (indent, k, v)

rawDescription: How to iterate an ordered dictionary recursively?

description: Iterate an ordered dictionary recursively


code:
# Creating the class
class Records:
    def __init__(self, firstname, lastname, age, gender):
        self.fname = firstname
        self.lname = lastname
        self.age = age
        self.gender = gender

# Using open to open the required file, and calling the file f,
# using with automatically closes the file stream, so its less
# of a hassle.
with open('object_file.txt') as f:
    list_of_records = [Records(*line.split()) for line in f]  # Adding records to a list

for record in list_of_records:
    print record.age  # Printing a sample

rawDescription: How do i create objects of data in a file ?? python

description: Create objects of data in a file ?? python


code:
rhs = data.lstrip()
print '.' * (len(data) - len(rhs)) + rhs

rawDescription: Python replace indentation with period

description: Python replace indentation with period


code:
l = [['Something', 'Something else', 'Another thing'],
     ['Other things', 'More data', 'Element'],
     ['Stuff', 'data', 'etc']]
sub1 = [
    [s.ljust(max(len(i) for i in column)) for s in column]
    for column in zip(*l)]
for p in [" ".join(row) for row in zip(*sub1)]: print p

rawDescription: data table in Python

description: Data table in Python


code:
import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()

rawDescription: Determine if current user is in Administrators group (Windows/Python)


code:
import ahocorasick

# build a match structure
print 'init empty tree'
tree = ahocorasick.KeywordTree()

cur.execute('select distinct user_id from users')

print 'add usernames to tree'
for result in cur.fetchall():
   tree.add(str(result[0]))

print 'build fsa'
tree.make()

for line in f:
     scanned += 1
     if tree.search(line) != None:
         print "got one!"

rawDescription: Searching text for a long list of substrings


code:
for index, s in enumerate(stocks_list):
    print index, s

rawDescription: How to find the position of an element in a list , in Python?

description: Find the position of an element in a list , in Python


code:
topic_tuple = lda.show_topic(0, 10)

print ','.join([str(word_tuple[1]) for word_tuple in topic_tuple])

rawDescription: how to remove numbers and symbols from output of LDA while using Gensim package?

description: Remove numbers and symbols from output of LDA while using Gensim package


code:
from datetime import date
today = date.today()
date(today.year - 1, today.month, today.day) < date(2009, 06, 05) <= today
date(today.year - 1, today.month, today.day) < date(2009, 06, 04) <= today
date(today.year - 1, today.month, today.day) < date(2010, 07, 04) <= today

rawDescription: python date appears in last year

description: Python date appears in last year


code:
import subprocess
import threading
import time

def process_tree_kill(process_pid):
    subprocess.call(['taskkill', '/F', '/T', '/PID', process_pid])

def main():
    cmd = ["gcc", "-O2", "a.c", "-o", "a"];
    p = subprocess.Popen(cmd)
    p.wait()
    print "Compiled"
    start = time.time()

    process = subprocess.Popen("a",shell=True)
    print(str(process.pid))

    # terminate process in timeout seconds
    timeout = 3 # seconds
    timer = threading.Timer(timeout, process_tree_kill,[str(process.pid)])
    timer.start()

    process.wait()
    timer.cancel()

    elapsed = (time.time() - start)
    print elapsed

if __name__=="__main__":
    main()

rawDescription: terminating a function call in python after n seconds

description: Terminating a function call in python after n seconds


code:
for t in text:
    payload = {"q": t}
    located = requests.get("http://localhost:8999/CLIFF-2.1.1/parse/text", params=payload)

    try:
        l = located.json()
    except ValueError:
        # No JSON object could be decoded - skip this item
        # and continue with the next item in `text`
        continue

    for i in l['results']['places']['focus']:
        print i['name']
        print i['lat']
        print i['lon']

rawDescription: How do I skip a requests response with no valid json?

description: Skip a requests response with no valid json


code:
data = '8/5/32'
data_list = data.split('/')

def yearCheck(data_list):
    # Years may be truncated to two digits and may in that case
    # also omit the leading 0 (if there is one), so 2000 could
    # be given as "2000", "00" or "0" (but not as an empty string).
    # Further examples:
    #   if 2099, could be given as 99
    #   if 2005, could be given as 05 or 5
    #   199 will not happen i.e. doesn't say that years may be
    #       truncated to three digits
    for index, item in enumerate(data_list):
        if len(item) > 4:
            # e.g. 30000
            print item, '- Data is invalid'
            return
        if len(item) == 4 and int(item) not in xrange(2000, 3000):
            # e.g. 3015
            print item, '- Data is invalid'
            return
        if len(item) == 3:
            # e.g. 199
            print item, '- Data is invalid'
            return
        if len(item) < 3 and int(item) in xrange(32, 100):
            data_list[index] = int(item) + 2000
    return data_list

def integer(data_list):
    int_data_list = [int(item) for item in data_list]
    return int_data_list.sort()

yearCheck(data_list)
integer(data_list)
print data_list

rawDescription: Simplifying Python program

description: Simplifying Python program


code:
import threading
import urllib
import Queue

concurrent = 5

aim = 'http://edition.cnn.com'
paths = ['2013/10/12/opinion/kazin-tea-party/index.html?hpt=hp_t5',
         '2013/10/11/opinion/opinion-hay-nobel-opcw/index.html?hpt=hp_t5',
         '2013/10/11/opinion/rosin-women-in-charge/index.html?hpt=hp_t5',
         'some invalid path',
         '2013']  # also an invalid path

def main():
    work_q = Queue.Queue()
    result_q = Queue.Queue()

    # start the scanners and the result writer
    scanners = [Scanner(work_q, result_q) for i in range(concurrent)]
    for s in scanners:
        s.start()
    results_file_path = 'results.txt'
    result_writer = ResultWriter(result_q, 'results.txt')
    result_writer.start()
    # send all the work and wait for it to be completed
    for path in paths:
        work_q.put(path.strip())
    work_q.join()
    # tell everyone to stop
    # you could just kill the threads but you writer needs to close the file
    for s in scanners:
        work_q.put(Scanner.STOP_TOKEN)
    result_q.put(ResultWriter.STOP_TOKEN)  # make sure file gets closed
    # wait for everyone to actually stop
    for s in scanners:
        s.join()
    result_writer.join()
    print 'the scan has finished and results are in {}'.format(results_file_path)

class Scanner(threading.Thread):
    STOP_TOKEN = '<<stop>>'

    def __init__(self, work_q, result_q):
        threading.Thread.__init__(self)
        self.work_q = work_q
        self.result_q = result_q

    def run(self):
        while True:
            path = status = None  # reset in case of error
            try:
                try:
                    path = self.work_q.get(timeout=0.00001)
                except Queue.Empty:
                    continue
                if path == self.STOP_TOKEN:
                    break  # stop looking for work
                get_url = urllib.urlopen(aim + '/' + path)
                status = get_url.getcode()
            except Exception as e:
                status = 'unhandled error ({})'.format(e)
            self.result_q.put((path, status))
            self.work_q.task_done()

class ResultWriter(threading.Thread):
    STOP_TOKEN = '<<stop>>'

    def __init__(self, result_q, results_file_path):
        threading.Thread.__init__(self)
        self.result_q = result_q
        self.results_file_path = results_file_path

    def run(self):
        with open(self.results_file_path, 'w') as results_file:
            while True:
                try:
                    result = self.result_q.get(timeout=0.00001)
                except Queue.Empty:
                    continue
                if result == self.STOP_TOKEN:
                    break  # stop looking for results
                path, status = result
                results_file.write('{}=>{}\n'.format(path, status))

if __name__ == '__main__':
    main()

rawDescription: How to Quit program when all the thread have been finished?


code:
def isPalindrome(inString):
    if inString[::-1] == inString:
        return True
    else:
        return False

print 'Enter a word or phrase and this program will determine if it is a palindrome or not.'
inString = raw_input()
inList = [x.lower() for x in inString if x != " "]  #if the character in inString is not a space add it to inList, this also make the letters all lower case to accept multiple case strings.
print isPalindrome(inList)

rawDescription: Palindrome Function Python

description: Palindrome Function Python


code:
import csv
S = """product,"Tom,Jerry\\n",r,0
product,Another,r,1"""
for row in csv.reader(S.splitlines()):
    print row

rawDescription: Split string by comma and escape string

description: Split string by comma and escape string


code:
import random

duplicateNumber=0

def has_duplicates(listToCheck):
    number_set = set(listToCheck)

    if len(number_set) is not len(listToCheck):
        return True
    else:
        return False

for i in range(0,1000):
    birthdayList=[]

    for j in range(0,23):
        birthday=random.randint(1,365)
        birthdayList.append(birthday)

    x = has_duplicates(birthdayList)

    if x==True:
        duplicateNumber+=1

print "after 1000 simulations with 23 students there were", duplicateNumber,"simulations with atleast one match. The approximate probibilatiy is", round(((duplicateNumber/1000.0)*100),3),"%"

rawDescription: Python Birthday paradox math not working


code:
import inspect, keyword, pprint, sys, tokenize

def value_in_frame(name, frame):
    try:
        return frame.f_locals[name]
    except KeyError:
        try:
            return frame.f_globals[name]
        except KeyError:
            raise ValueError("Couldn't find value for %s" % name)

def validate(expr, exc_class=AssertionError):
    """Evaluate `expr` in the caller's frame, raise `exc_class` if false."""
    frame = inspect.stack()[1][0]
    val = eval(expr, frame.f_globals, frame.f_locals)
    if not val:
        rl = iter([expr]).next
        for typ, tok, _, _, _ in tokenize.generate_tokens(rl):
            if typ == tokenize.NAME and not keyword.iskeyword(tok):
                try:
                    val = value_in_frame(tok, frame)
                except ValueError:
                    val = '???'
                else:
                    val = repr(val)
                print "  %s: %s" % (tok, val)
        raise exc_class("Failed to validate: %s" % expr)

if __name__ == '__main__':
    a = b = 3
    validate("a + b == 5")

rawDescription: python: automatically printing representation of each component in an expression

description: Python: automatically printing representation of each component in an expression


code:
import msvcrt
while True:
    print "Doing whatever I feel like"
    if msvcrt.kbhit():              # True if a keypress is waiting to be read.
        if msvcrt.getch()=="q":     # will not wait for Enter to be pressed
            break

rawDescription: Leave loop early based on user input without prompting each iteration


code:
sel = selected()

for i in sel:
    print i.ty.get()
    i.ty.set(i.ty.get() + 1)

rawDescription: how to get or set object's attribute from a list in maya python?

description: Get or set object's attribute from a list in maya python


code:
for i in xrange(0, len(a)):
    x = len(a[i])
    y = len(b[i])
    diff = max(x, y)
    a[i].extend([0] * (diff - x))
    b[i].extend([0] * (diff - y))

print a, b

rawDescription: How to pad multiple lists with trailing zeros?

description: Pad multiple lists with trailing zeros


code:
import string

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
comp = "this is an example"

tr = string.maketrans(alpha, key)
print string.translate(comp.upper(), tr)
# -> ULYJ YJ XW TIXBAZT

rawDescription: How to find when a string's letter matches a another's letter?

description: Find when a string's letter matches a another's letter


code:
def newbar_factory(cls):
    def newbar(self):
        super(cls, self).bar()
        # Alternately, as Sven points out you could do
        # cls.bar(self)
        print "another world!"

    return newbar

# Use
fooNew = type('fooNew', (foo,), {'bar':newbar_factory(foo)})

rawDescription: Python inheritance in dynamically created classes


code:
import os
import time
import pygame

# Center the Frame in the screen.
os.environ['SDL_VIDEO_CENTERED'] = '1'

BG_COLOR = (224, 255, 255)
TEXT_COLOR = (0, 0, 255)
SIZE = (800, 360)

def show_window(text, font, screen):
    rendered_text = font.render(text, True, TEXT_COLOR)
    screen.fill(BG_COLOR)
    screen.blit(rendered_text, ((SIZE[0] - rendered_text.get_width()) // 2,
                                (SIZE[1] - rendered_text.get_height()) // 2))
    pygame.display.flip()

pygame.init()
screen = pygame.display.set_mode(SIZE)
font = pygame.font.SysFont("comicsansms", 200)

words = ['foo', 'bar', 'baz']
for word in words:
    show_window(word, font, screen)
    # Replace the following `sleep` call with your stuff.
    time.sleep(3)

pygame.quit()
print "done"

rawDescription: Python: How to display a big wndow with custom content, no widgets and no event loop


code:
import types
class C:
    pass

def f(self):
    print self

a = C()
a.f = types.MethodType(f,a)
a.f()

rawDescription: Dynamically add member function to an instance of a class in Python


code:
totalData = [ [['Test1',5,2,1,8],['Test2',10,4,3,2]],
                  [['Test1',2,4,5,1],['Test2',4,6,10,3]] ]
for t in range(len(totalData[0])): #tests
    result = [totalData[0][t][0],]
    for i in range(1,len(totalData[0][0])): #numbers
        sum = 0.0
        for j in range(len(totalData)):
            sum += totalData[j][t][i]
        sum /= len(totalData)
        result.append(sum)
    print result

rawDescription: Averaging values in a list of a list of a list in Python


code:
from itertools import product

def horizontal():
    for x, y in product(range(20), range(17)):
        print 1 + sum(int(n) for n in grid[x][y: y + 4])

rawDescription: Alternative to nesting for loops in Python


code:
import numpy as np
import os

data = np.arange(100, dtype=np.int)
data.tofile("temp")  # save the data

f = open("temp", "rb")  # reopen the file
f.seek(256, os.SEEK_SET)  # seek

x = np.fromfile(f, dtype=np.int)  # read the data into numpy
print x
# [64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
# 89 90 91 92 93 94 95 96 97 98 99]

rawDescription: How to read part of binary file with numpy?

description: Read part of binary file with numpy


code:
import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

grape_gif='''\
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A////
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs=
'''

def getRGB(image, x, y):
    value = image.get(x, y)
    return value

master=tk.Tk()
master.geometry("300x500")

## uses data= instead of file= because the picture
## is data within this program
photo=tk.PhotoImage(data=grape_gif)
print getRGB(photo, 10, 10)

rawDescription: what exactly is the tkinter getRGB() image argument supposed to be? (python)

description: What exactly is the tkinter getRGB() image argument supposed to be? (python)


code:
import base64
import sys

encoded = base64.b64encode(sys.stdin.read())
print encoded

decoded = base64.b64decode(encoded)
print decoded

rawDescription: How does one encode and decode a string with Python for use in a URL?

description: Encode and decode a string with Python for use in a URL