code:
while True:
    value = raw_input('Value between 0 and 100:')
    try:
       value = int(value)
    except ValueError:
       print 'Valid number, please'
       continue
    if 0 <= value <= 100:
       break
    else:
       print 'Valid range, please: 0-100'

rawDescription: How do I use try .. except or if ...else to validate user input?

description: Use try .. except or if ...else to validate user input


code:
feedquery = gdata.contacts.service.ContactsQuery()
feedquery.query.max_results = 1000
gmlf = gd_client.GetContactsFeed(feedquery.ToUri())
for index,gmc in enumerate(gmlf.entry):
        print str(index) +":"+ gmc .title.text

rawDescription: Google Apps shared contacts API get a contact for python


code:
import re
import urllib2

# Import Custom libraries
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup

def render_google_uri(idict):
    '''
    Render the appropriate Google Map Api request uri
    '''
    base_url = "http://maps.googleapis.com/maps/api/geocode/xml?"
    options = [(key,re.sub("\s+", "+", value)) for (key, value) in idict.items()]

    options = map(lambda x: "=".join(x), options)
    options = "&".join(options)
    url = base_url + options
    return url

def get_street_position(*args):
    '''
    Longitude and Latitude from Street Address
    '''
    def is_result(itag):
        if itag.name == "result":
            if itag.type.text == "locality":
                return True
        return False

    ret_list = []
    for address in args:
        google_api_dict = \
        {
            "address" : address,
            "sensor"  : "false",
        }
        request_uri = render_google_uri(google_api_dict)
        request = urllib2.Request(request_uri, None, {})

        try:
            response = urllib2.urlopen(request)
            the_page = response.read()
        except Exception:
            the_page = ""

        if the_page:
            pool = BeautifulStoneSoup(the_page)
            result = pool.find(is_result)

            if result:
                bounds = result.find("bounds")

                if bounds:

                    cur_dict = \
                    {
                        "Address"        : address,
                        "Google Address" : result.formatted_address.text,
                        "Bounds"         : \
                        {
                            "SW" :
                            {
                                "Longitude" : bounds.southwest.lng.text,
                                "Latitude"  : bounds.southwest.lat.text
                            },
                            "NE" :
                            {
                                "Longitude" : bounds.northeast.lng.text,
                                "Latitude"  : bounds.northeast.lat.text
                            }
                        }
                    }
                    ret_list += [cur_dict]

    return ret_list

if __name__ == "__main__":
    print get_street_position("Sydney", "Marrakech")

rawDescription: Finding pair of longitude and latitude for a city or country


code:
def integrate(fs):
    print fs[0](1)

f1 = lambda x: x**2
f2 = lambda x: x**3

fs = (f1, f2)

integrate(fs)

rawDescription: Pass set of functions as arguments to another function in Python


code:
XMAX = 800
YMAX = 570

NEIGHBOURS = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]

matrix = range(XMAX * YMAX)

def all_neighbours(m):
    for i in xrange(len(m)):
        ns = []
        y, x = divmod(i, XMAX)
        for u, v in NEIGHBOURS:
            ux = u + x
            vy = v + y
            if 0 <= ux < XMAX and 0 <= vy < YMAX:
                ns.append(ux + vy * YMAX)
        yield i, ns

if __name__ == '__main__':

    for field, neighbours in all_neighbours(matrix):
        print field, neighbours

rawDescription: Listing adjacent cells

description: Listing adjacent cells


code:
from random import randint

dice = [0 for i in range(6)]

for rolls in range(0, 10000):
    rand = randint(1, 6)
    dice[rand-1] += 1

print 'the result was'
print dice

rawDescription: What better ways could i have written this python code


code:
d = {1:2, 3:4}
if 4 in d.itervalues():
    print 'Aha!'

rawDescription: If x is a value in this dict... Python

description: If x is a value in this dict... Python


code:
import os, sys

f = open(sys.argv[1], 'r')

T = int(f.readline())

def next(ary, start):
    j = start
    l = len(ary)
    ret = start - 1
    while j < l and ary[j]:
        ret = j
        j += 1
    return ret

for t in range(T):
    n, k = map(int, f.readline().strip().split(' '))
    a, b, c, r = map(int, f.readline().strip().split(' '))

    m = [0] * (4 * k)
    s = [0] * (k+1)
    m[0] = a
    if m[0] <= k:
        s[m[0]] = 1
    for i in xrange(1, k):
        m[i] = (b * m[i-1] + c) % r
        if m[i] < k+1:
            s[m[i]] += 1

    p = next(s, 0)
    m[k] = p + 1
    p = next(s, p+2)

    for i in xrange(k+1, n):
        if m[i-k-1] > p or s[m[i-k-1]] > 1:
            m[i] = p + 1
            if m[i-k-1] <= k:
                s[m[i-k-1]] -= 1
            s[m[i]] += 1
            p = next(s, p+2)
        else:
            m[i] = m[i-k-1]
        if p == k:
            break

    if p != k:
        print 'Case #%d: %d' % (t+1, m[n-1])
    else:
        print 'Case #%d: %d' % (t+1, m[i-k + (n-i+k+k) % (k+1)])

rawDescription: My FB HackerCup code too slow of large inputs


code:
def f():
  f = sys._getframe()
  i = f.f_lasti + 3   # capture current point of execution, advance to expected store
  print dis.disco(f.f_code, i)

rawDescription: Accessing the name that an object being created is assigned to


code:
import numpy as np
arr1 = np.array([1.4, 3.0, 4.0, 4.0, 7.0, 9.0, 9.0, 9.0])
arr2 = np.array([2.3, 5.0, 2.3, 2.3, 4.0, 6.0, 5.0, 6.0])
data = np.array([5.4, 7.1, 9.5, 1.9, 8.7, 1.8, 6.1, 7.4])
from collections import defaultdict
dd = defaultdict(list)
for x1, x2, d in zip(arr1, arr2, data):
  dd[x1, x2].append(d)

arr1unique = np.array([x[0] for x in dd.iterkeys()])
arr2unique = np.array([x[1] for x in dd.iterkeys()])
dataunique = np.array([np.mean(x) for x in dd.itervalues()])
print arr1unique
print arr2unique
print dataunique

rawDescription: Computing mean for non-unique elements of numpy array pairs


code:
import boto
from boto.s3.connection import S3Connection
s3 = boto.connect_s3()
buckets = s3.get_all_buckets()
for key in buckets:
    print key.name

rawDescription: get all available buckets and print but only with bucket name

description: Get all available buckets and print but only with bucket name


code:
import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        main_layout = QtGui.QVBoxLayout()

        self.browser = QtGui.QTextBrowser()
        self.browser.setHtml('''some text
click me to call a function

        Click me to scroll down
foo
foo
foo
foo
foo
foo

        foo
bar
bar
bar
bar
bar
bar
hello!
hello!
hello!
hello!
hello!
hello!
hello!
hello!</html''')

        self.browser.anchorClicked.connect(self.on_anchor_clicked)

        main_layout.addWidget(self.browser)

        self.setLayout(main_layout)

    def on_anchor_clicked(self,url):
        text = str(url.toString())
        if text.startswith('some_special_identifier://'):
            self.browser.setSource(QtCore.QUrl()) #stops the page from changing
            function = text.replace('some_special_identifier://','')
            if hasattr(self,function):
                getattr(self,function)()

    def a_function(self):
        print 'you called?'

app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()

sys.exit(app.exec_())

rawDescription: PySide/PyQt: Is it possible to make strings that you attach to the QTextBrowser separate clickable units


code:
root = etree.XML(open("lx.xml").read())
span = root.xpath("//span[@id='identifiedid']")[0]
print span.getparent().getnext().xpath('li/a/@href')

rawDescription: HTML getnext using lxml parser

description: HTML getnext using lxml parser


code:
with open('predictions2.txt') as f:
     tot_sum = 0
     for i,x in enumerate(f, 1):
         val = float(x)
         #do something with val
         tot_sum += val           #add val to tot_sum
     print tot_sum/i              #print average

#prints -0.32322842

rawDescription: python program to add all values from each line

description: Python program to add all values from each line


code:
url = 'http://httpbin.org/cookies/set/requests-is/awesome'
r = requests.get(url)
print r.cookies

rawDescription: Scrape a web page that requires they give you a session cookie first


code:
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))
rect = pygame.Rect(180, 180, 20, 20)
clock = pygame.time.Clock()
d=1
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            raise

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 255, 255), rect)
    rect.move_ip(d, 0)
    if not screen.get_rect().contains(rect):
        d *= -1

    pos = pygame.mouse.get_pos()

    # print the 'absolute' mouse position (relative to the screen)
    print 'absoulte:', pos

    # print the mouse position relative to rect
    print 'to rect:', pos[0] - rect.x, pos[1] - rect.y

    clock.tick(100)
    pygame.display.flip()

rawDescription: Python surface real position coordinates of pygame.mouse.get_pos and Rect.collidepoint


code:
import numpy
import theano
import theano.tensor as tt
import theano.tensor.shared_randomstreams

n = 6
alpha = [1] * n
seed = 1
w = theano.shared(numpy.random.randn(n, 2, 2).astype(theano.config.floatX))
p = theano.shared(numpy.random.dirichlet(alpha).astype(theano.config.floatX))
rng = tt.shared_randomstreams.RandomStreams(seed=seed)
i = rng.choice(size=(1,), a=n, p=p)
f = theano.function([], [p, i, w[i]])
print f()
print f()
print f()
print f()

rawDescription: Choose a number from a a given pmf in theano


code:
import os
# This is the path where all the files are stored.
folder_path = 'c:\\'
# Open one of the files,
for data_file in sorted(os.listdir(folder_path)):
    print data_file

rawDescription: Read files sequentially in order

description: Read files sequentially in order


code:
def printTotals(transferred, toBeTransferred):
    print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred)

sftp.put("myfile","myRemoteFile",callback=printTotals)

rawDescription: How to see (log) file transfer progress using paramiko?

description: See (log) file transfer progress using paramiko


code:
f = open('Bevolking_per_maand__100214211711.sps','r')
#lines = f.readlines()
spss_keys = list()
data = list()
begin_data_step= False
end_data_step = False

for l in f:
    # first look for TITLE
    if l.find('TITLE') <> -1:
        start_pos=l.find('"')+1
        end_pos = l.find('"',start_pos+1)
        title = l[start_pos:end_pos]
        print "title:" ,title

    if l.find('DATA LIST') <> -1:
        data_list = True
        start_pos=l.find('=')+1
        end_pos=len(l)
        num_records= l[start_pos:end_pos].strip()
        print "number of records =", num_records

    if num_records=='1':
        if ((l.find("Key") <> -1) and (not begin_data_step) and (not end_data_step)):
            spss_keys.append([l[15:22].strip(),int(l[23:29].strip()),int(l[32:36].strip()),l[37:].strip()])

    if l.find('END DATA.') <> -1:
        end_data_step=True

    if ((begin_data_step) and (not end_data_step)):
        values = list()
        for key in spss_keys:
            values.append(l[key[1]-1:key[2]])
        data.append(values)
        if l[-1]=="." :
            begin_data=False

    if l.find('BEGIN DATA') <> -1:
        begin_data_step=True

    if end_data_step:
        print ""
        # more to follow

data

rawDescription: Importing data in SPSS syntax incl. 'value labels' and 'var labels'


code:
import itertools

def three_vs_three(xs):
    xset = set(xs)
    for i1, i2 in itertools.combinations(xs[1:], 2):
        triple = set([xs[0], i1, i2])
        yield sorted(triple), sorted(xset - triple)

things = ['CM%d' % x for x in 19, 20, 21, 23, 25, 26]
print list(three_vs_three(things))

rawDescription: Generate all possible comparisons 3 v 3 in a group of 6


code:
import sys
print sys.argv

rawDescription: How to get input of arguments from the terminal along with the filename in python?

description: Get input of arguments from the terminal along with the filename in python


code:
import os.path

p = '/var/www/RH/HV/FILE.doc'
print os.path.basename(p)

rawDescription: How to change the attachment name?

description: Change the attachment name


code:
def handle(self):
    while 1:
        self.data = self.request.recv(1024).strip()
        print self.data
        if self.data == 'quit':
            break
        # echo back data in all caps
        self.request.sendall(self.data.upper())

rawDescription: How to only allow one client to connect to my TCPSocketServer?

description: Only allow one client to connect to my TCPSocketServer


code:
def reload_start(self):
    self.enable()
    self.conn.set_prompt(r'Save\? \[yes/no\]\:')
    self.conn.execute('reload')
    self.conn.set_prompt(r'Proceed with reload\? \[confirm\]')
    self.conn.execute('no')
    self.conn.set_prompt()
    self.conn.execute('confirm')
    print self.conn.response

rawDescription: Exscript control cisco execute 'reload' command


code:
import threading
import subprocess

class RepeatingTimer(threading._Timer):
    def run(self):
        while True:
            self.finished.wait(self.interval)
            if self.finished.is_set():
                return
            else:
                self.function(*self.args, **self.kwargs)

def status():
    print "I'm alive"
timer = RepeatingTimer(1.0, status)
timer.daemon = True # Allows program to exit if only the thread is alive
timer.start()

proc = subprocess.Popen([ '/bin/sleep', "5" ])
proc.wait()

timer.cancel()

rawDescription: showing progress while spawning and running subprocess

description: Showing progress while spawning and running subprocess


code:
import random

def playGame():
    chance = random.randrange(1, 11)

    print "\nI am thinking of a number between 1-10."
    print "Can you guess my number?"
    guess = int( raw_input( "Enter your guess. " ) )

    while guess != chance:
        if guess > chance:
            print "Too high. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )

        if guess < chance:
            print "Too low. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )

    print "Congratulations! You guessed my number!"

playGame()

keep_playing = True
while keep_playing:
    answer = raw_input( "\nWould you like to try again? (y or n) " )
    if answer == 'n':
        print "\nThank you for playing!"
        keep_playing = False

    if answer == 'y':
        playGame()

rawDescription: Python function not working when called

description: Python function not working when called


code:
import serial

ser = serial.Serial ( 2 , baudrate = 57600 , timeout = 1 )
while True:

    line = ser.readline().strip('^\n\r')
    print line
    AngleText = open ("data.txt" , "w")
    AngleText.write (str(line))
    AngleText.close()

ser.close()

rawDescription: continuous writing to a text file and ensuring that a new file is create each time

description: Continuous writing to a text file and ensuring that a new file is create each time


code:
def ops_list():
    "calculate the list of overloadable operators"
    #<type 'object'> has functions but no operations
    not_ops = dir(object)

    #calculate the list of operation names
    ops = set()
    for mytype in (int, float, str):
        for op in dir(mytype):
            if op.endswith("__") and op not in not_ops:
                ops.add(op)
    return sorted(ops)

class MetaCastable(type):
    __ops = ops_list()

    def __new__(mcs, name, bases, dict):
        #pass any undefined ops to self.__op__
        def add_op(op):
            if op in dict:
                return
            fn = lambda self, *args: self.__op__(op, args)
            fn.__name__ = op
            dict[op] = fn

        for op in mcs.__ops:
            add_op( op )
        return type.__new__(mcs, name, bases, dict)

class Castable(object):
    __metaclass__ = MetaCastable
    def __str__(self):
        print "str!"
        return "<Castable>"
    def __int__(self):
        print "int!"
        return 42
    def __float__(self):
        print "float!"
        return 2.718281828459045

    def __op__(self, op, args):
        try:
            other = args[0]
        except IndexError:
            other = None
        print "%s %s %s" % (self, op, other)
        self, other = coerce(self, other)
        return getattr(self, op)(*args)

    def __coerce__(self, other):
        print "coercing like %r!" % other
        if other is None: other = 0.0
        return (type(other)(self), other)

rawDescription: Python: coerce new-style class

description: Python: coerce new-style class


code:
class MultiDict(dict):
    def __getitem__(self, key):
        if key in self:
            return dict.__getitem__(self, key)
        result = []
        for complete_key in sorted(self.keys()):
            if complete_key.startswith(key):
                result.extend(self[complete_key])
        return result

#example
a = MultiDict()
a["a0"] = [1]
a["a1"] = [2]
print  a["a"]
[1, 2]

rawDescription: How can I read to dictionary keys in a way that make sense?

description: Read to dictionary keys in a way that make sense


code:
import lxml.html as html
page = html.parse("http://www.insiderpages.com/b/3721895833/central-kia-of-irving-irving")
print page.xpath("//div[@class='rating_box']/abbr/@title")

rawDescription: Scrapping: How to fetch an attribute in a <abbr> tag


code:
for address in sess.query(Address).join(Address.association).filter_by(type='users'):
    print "Street", address.street, "Member", address.member

rawDescription: In SQLAlchemy, can you filter a polymorphically associated model by the type of its associated record?


code:
for x in xrange(1, 10):
    print x,

rawDescription: Print range of numbers on same line

description: Print range of numbers on same line


code:
#!/usr/bin/env python

import sys, os, time, termios, tty, signal

# Define some custom exceptions we can raise in signal handlers
class SkipYear(Exception):
    pass

class SkipMonth(Exception):
    pass

# Process one month
def process_month(year, month):

    # Fake up whatever the processing actually is
    print 'Processing %04d-%02d' % (year, month)
    time.sleep(1)

# Process one year
def process_year(year):

    # Iterate months 1-12
    for month in range(1, 13):

        try:
            process_month(year, month)
        except SkipMonth:
            print 'Skipping month %d' % month

# Do all processing
def process_all(args):

    # Help
    print 'Started processing - args = %r' % args

    try:

        # Iterate years 2010-2015
        for year in range(2010, 2016):

            try:
                process_year(year)
            except SkipYear:
                print 'Skipping year %d' % year

    # Handle SIGINT from parent process
    except KeyboardInterrupt:
        print 'Child caught SIGINT'

    # Return success
    print 'Child terminated normally'
    return 0

# Main entry point
def main(args):

    # Help
    print 'Press Y to skip current year, M to skip current month, or CTRL-C to abort'

    # Get file descriptor for stdin. This is almost always zero.
    stdin_fd = sys.stdin.fileno()

    # Fork here
    pid = os.fork()

    # If we're the child
    if not pid:

        # Detach child from controlling TTY, so it can't be the foreground
        # process, and therefore can't get any signals from the TTY.
        os.setsid()

        # Define signal handler for SIGUSR1 and SIGUSR2
        def on_signal(signum, frame):
            if signum == signal.SIGUSR1:
                raise SkipYear
            elif signum == signal.SIGUSR2:
                raise SkipMonth

        # We want to catch SIGUSR1 and SIGUSR2
        signal.signal(signal.SIGUSR1, on_signal)
        signal.signal(signal.SIGUSR2, on_signal)

        # Now do the thing
        return process_all(args[1:])

    # If we get this far, we're the parent

    # Define a signal handler for when the child terminates
    def on_sigchld(signum, frame):
        assert signum == signal.SIGCHLD
        print 'Child terminated - terminating parent'
        sys.exit(0)

    # We want to catch SIGCHLD
    signal.signal(signal.SIGCHLD, on_sigchld)

    # Remember the original terminal attributes
    stdin_attrs = termios.tcgetattr(stdin_fd)

    # Change to cbreak mode, so we can detect single keypresses
    tty.setcbreak(stdin_fd)

    try:

        # Loop until we get a signal. Typically one of...
        #
        # a) SIGCHLD, when the child process terminates
        # b) SIGINT, when the user presses CTRL-C
        while 1:

            # Wait for a keypress
            char = os.read(stdin_fd, 1)

            # If it was 'Y', send SIGUSR1 to the child
            if char.lower() == 'y':
                os.kill(pid, signal.SIGUSR1)

            # If it was 'M', send SIGUSR2 to the child
            if char.lower() == 'm':
                os.kill(pid, signal.SIGUSR2)

    # Parent caught SIGINT - send SIGINT to child process
    except KeyboardInterrupt:
        print 'Forwarding SIGINT to child process'
        os.kill(pid, signal.SIGINT)

    # Catch system exit
    except SystemExit:
        print 'Caught SystemExit'

    # Ensure we reset terminal attributes to original settings
    finally:
        termios.tcsetattr(stdin_fd, termios.TCSADRAIN, stdin_attrs)

    # Return success
    print 'Parent terminated normally'
    return 0

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

rawDescription: How handle asynchronous keystroke with Python?


code:
import sys, os, msvcrt #
line = sys.stdin.readline() #
line #
_ = msvcrt.setmode(sys.stdin.fileno(), os.O_TEXT) #
line = sys.stdin.readline()
line
print "Hi"

rawDescription: Python Unbuffered Mode Causes Problems in Windows


code:
subscriber = Subscriber.objects.get(subId=my_subscriber_id)
print subscriber.sms_set.all()  # all SMSs for that subscriber
print subscriber.call_set.all() # all Calls for that subscriber

rawDescription: How to use Django join

description: Use Django join


code:
def extract(string1):
    string1=string1+" "
    #added a spaces at the end of the string so that last number is also    extracted
    l=len(string1)
    pos=0
    num=[]
    continuity=0
    for i in range(l):

        if string1[i].isdigit()==True:
            continuity+=1

        else:

            if pos!=continuity:
                ''' This condition prevents consecutive execution
                of else part'''

                num=num+ [int(string1[pos:continuity])]

            continuity+=1
            pos=continuity
    return num

string="ab73t9+-*/182"

num1=[]

num1=extract(string)

print num1

rawDescription: Extraction of 2 or more digit number into a list from string like 12+13


code:
import re
import urllib2

page = urllib2.urlopen("http://www.m.funtweets.com/random").read()
umatch = re.search(r"@(\w+)", page)
user = umatch.group()
utext = re.search(r" (\w.*)", page)
text = utext.group()
print '@{0}\n{1}\n'.format(user,text)

rawDescription: Using regular expressions to parse HTML

description: Using regular expressions to parse HTML


rawDescription: change end of string after dash

description: Change end of string after dash


code:
n,m = map(int,raw_input().split())
s = n
aux = 0
while n>=m:
    n, aux = divmod(n, m)
    s +=n
    n +=aux
print s

rawDescription: Speed up code in python

description: Speed up code in python


code:
import re
text='''Service ( ndfdXML ) tns="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl" Prefixes (2) ns0 = "http://schemas.xmlsoap.org/soap/encoding/" ns1 = "http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd" Ports (1): (ndfdXMLPort) Methods (12): CornerPoints(ns1:sectorType sector, ) GmlLatLonList(ns1:listLatLonType listLatLon, xs:dateTime requestedTime, ns1:featureTypeType featureType, ns1:weatherParametersType weatherParameters, ) GmlTimeSeries(ns1:listLatLonType listLatLon, xs:dateTime startTime, xs:dateTime endTime, ns1:compTypeType compType, ns1:featureTypeType featureType, xs:string propertyName, ) LatLonListCityNames(ns1:displayLevelType displayLevel, ) LatLonListLine(xs:decimal endPoint1Lat, xs:decimal endPoint1Lon, xs:decimal endPoint2Lat, xs:decimal endPoint2Lon, ) LatLonListSquare(xs:decimal centerPointLat, xs:decimal centerPointLon, xs:decimal distanceLat, xs:decimal distanceLon, xs:decimal resolution, ) LatLonListSubgrid(xs:decimal lowerLeftLatitude, xs:decimal lowerLeftLongitude, xs:decimal upperRightLatitude, xs:decimal upperRightLongitude, xs:decimal resolution, ) LatLonListZipCode(ns1:zipCodeListType zipCodeList, ) NDFDgen(xs:decimal latitude, xs:decimal longitude, ns1:productType product, xs:dateTime startTime, xs:dateTime endTime, ns1:weatherParametersType weatherParameters, ) NDFDgenByDay(xs:decimal latitude, xs:decimal longitude, xs:date startDate, xs:integer numDays, ns1:formatType format, ) NDFDgenByDayLatLonList(ns1:listLatLonType listLatLon, xs:date startDate, xs:integer numDays, ns1:formatType format, ) NDFDgenLatLonList(ns1:listLatLonType listLatLon, ns1:productType product, xs:dateTime startTime, xs:dateTime endTime, ns1:weatherParametersType weatherParameters, ) Types (60): ns0:Array ns0:ENTITIES ns0:ENTITY ns0:ID ns0:IDREF ns0:IDREFS ns0:NCName ns0:NMTOKEN ns0:NMTOKENS ns0:NOTATION ns0:Name ns0:QName ns0:Struct ns0:anyURI ns0:arrayCoordinate ns0:base64 ns0:base64Binary ns0:boolean ns0:byte ns1:compTypeType ns0:date ns0:dateTime ns0:decimal ns1:displayLevelType ns0:double ns0:duration ns1:featureTypeType ns0:float ns1:formatType ns0:gDay ns0:gMonth ns0:gMonthDay ns0:gYear ns0:gYearMonth ns0:hexBinary ns0:int ns0:integer ns0:language ns1:latLonPairType ns1:listCityNamesType ns1:listLatLonType ns0:long ns0:negativeInteger ns0:nonNegativeInteger ns0:nonPositiveInteger ns0:normalizedString ns0:positiveInteger ns1:productType ns1:sectorType ns0:short ns0:string ns0:time ns0:token ns0:unsignedByte ns0:unsignedInt ns0:unsignedLong ns0:unsignedShort ns1:weatherParametersType ns1:zipCodeListType'''

for match in re.findall(r"[a-zA-Z]+?\s*\([a-z]*?[0-9]*?:",text):
    print match.split("(")[0]

rawDescription: I am unable to find a reg-exp to find the items (marked below in bold italics) in a txt file


code:
def GetMimeMessage(service, user_id, msg_id):
  """Get a Message and use it to create a MIME Message.
  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    msg_id: The ID of the Message required.
  Returns:
    A MIME Message, consisting of data from Message.
  """
  try:
    message = service.users().messages().get(userId=user_id, id=msg_id,
                                             format='raw').execute()
    #print 'Message snippet: %s' % message['snippet']
    msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
    mime_msg = email.message_from_string(msg_str)
    for part in mime_msg.walk():
        if part.get_content_type() == 'text/plain':
            print part.get_payload()
    return mime_msg
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

rawDescription: Gmail API: message snippet to ignore reply parts of the body


code:
from fbx import *

for i in range(myscene.GetCharacterCount()):
    character = myscene.GetCharacter(i)
    node = character.eHips
    link = FbxCharacterLink()
    while (character.GetCharacterLink(node, link)):
        print node, link.mNode.GetName()
        node = character.ENodeId(int(node+1))

rawDescription: FBX SDK - Get the FbxNode from an ENodeId of a Character


code:
import lxml.etree

TEST="<html><h1>...</html>"

def insert_section(root):
    tables = root.findall(".//table")
    for table in tables:
        section = ET.Element("section")
        table.addprevious(section)
        section.insert(0, table)   # this moves the table

root = ET.fromstring(TEST)
insert_section(root)
print ET.tostring(root)

rawDescription: Python lxml wrapping elements

description: Python lxml wrapping elements


code:
d ={'idx1':{'tokenA': 0.1,
            'tokenB': 1.3,
            'tokenD': 2.3},
      'idx2':{'tokenC': 0.9,
            'tokenE': 3.4},
      'idxn':{'tokenA': 0.3,
            'tokenF': 0.4,
            'tokenZ': 7.4}
      }

from collections import Counter

token_sums   = sum((Counter(v       ) for k,v in d.iteritems()), Counter())
token_counts = sum((Counter(v.keys()) for k,v in d.iteritems()), Counter())
token_mean   = {k:token_sums[k]/token_counts[k] for k in token_sums}
print token_mean

rawDescription: How to get average value from a dict like below?

description: Get average value from a dict like below


code:
from nltk.corpus import wordnet as wn
from nltk.stem import PorterStemmer
from itertools import chain

bank_sents = ['I went to the bank to deposit my money',
'The river bank was full of dead fishes']

plant_sents = ['The workers at the industrial plant were overworked',
'The plant was no longer bearing flowers']

ps = PorterStemmer()

def lesk(context_sentence, ambiguous_word, pos=None, stem=True, hyperhypo=True):
    max_overlaps = 0; lesk_sense = None
    context_sentence = context_sentence.split()
    for ss in wn.synsets(ambiguous_word):
        # If POS is specified.
        if pos and ss.pos is not pos:
            continue

        lesk_dictionary = []

        # Includes definition.
        lesk_dictionary+= ss.definition.split()
        # Includes lemma_names.
        lesk_dictionary+= ss.lemma_names

        # Optional: includes lemma_names of hypernyms and hyponyms.
        if hyperhypo == True:
            lesk_dictionary+= list(chain(*[i.lemma_names for i in ss.hypernyms()+ss.hyponyms()]))

        if stem == True: # Matching exact words causes sparsity, so lets match stems.
            lesk_dictionary = [ps.stem(i) for i in lesk_dictionary]
            context_sentence = [ps.stem(i) for i in context_sentence]

        overlaps = set(lesk_dictionary).intersection(context_sentence)

        if len(overlaps) > max_overlaps:
            lesk_sense = ss
            max_overlaps = len(overlaps)
    return lesk_sense

print "Context:", bank_sents[0]
answer = lesk(bank_sents[0],'bank')
print "Sense:", answer
print "Definition:",answer.definition
print

print "Context:", bank_sents[1]
answer = lesk(bank_sents[1],'bank','n')
print "Sense:", answer
print "Definition:",answer.definition
print

print "Context:", plant_sents[0]
answer = lesk(plant_sents[0],'plant','n', True)
print "Sense:", answer
print "Definition:",answer.definition
print

rawDescription: Word sense disambiguation algorithm in Python


code:
from Tkinter import *

class takeInput(object):

    def __init__(self,requestMessage):
        self.root = Tk()
        self.string = ''
        self.frame = Frame(self.root)
        self.frame.pack()
        self.acceptInput(requestMessage)

    def acceptInput(self,requestMessage):
        r = self.frame

        k = Label(r,text=requestMessage)
        k.pack(side='left')
        self.e = Entry(r,text='Name')
        self.e.pack(side='left')
        self.e.focus_set()
        b = Button(r,text='okay',command=self.gettext)
        b.pack(side='right')

    def gettext(self):
        self.string = self.e.get()
        self.root.destroy()

    def getString(self):
        return self.string

    def waitForInput(self):
        self.root.mainloop()

def getText(requestMessage):
    msgBox = takeInput(requestMessage)
    #loop until the user makes a decision and the window is destroyed
    msgBox.waitForInput()
    return msgBox.getString()

var = getText('enter your name')
print "Var:", var

rawDescription: text input in tkinter

description: Text input in tkinter


code:
class Reciever:
    def themethod(self):
        frame = sys._getframe(1)
        arguments = frame.f_code.co_argcount
        if arguments == 0:
            print "Not called from a method"
            return
        caller_calls_self = frame.f_code.co_varnames[0]
        thecaller = frame.f_locals[caller_calls_self]
        print "Called from a", thecaller.__class__.__name__, "instance"

rawDescription: Can Python determine the class of a object accessing a method


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

# Input: Two 3D points of the form (x,y,z).
# Output: Euclidean distance between the points.
def getDistance((x1, y1, z1), (x2, y2, z2)):
   d = math.sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2) + pow((z1 - z2), 2))
   return round(d, 3)

# Purpose: Prints a list of 3-tuples (r1,r2,d). R1 and r2 are
# residue numbers, and d is the distance between their respective
# gamma sulfur atoms.
def prettyPrint(dsBonds):
   print "Residue 1\tResidue 2\tDistance"
   for r1, r2, d in dsBonds:
       print " {0}\t\t {1}\t\t {2}".format(r1, r2, d)

# Purpose: Find all pairs of cysteine residues whose gamma sulfur atoms
# are within maxBondDist of each other.
def main():
   pdbFile = open('2v5t.pdb','r')
   #Max distance to consider a disulfide bond.
   maxBondDist = 2.5

   # Anonymous function to check if a line from the PDB file is a gamma
   # sulfur atom from a cysteine residue.
   isCysLine = lambda line: (line[0:4] == "ATOM" and line[13:15] == "SG")
   cysLines = [line for line in pdbFile if isCysLine(line)]
   pdbFile.close()

   # Anonymous function to get (x,y,z) coordinates in angstroms for
   # the location of a cysteine residue's gamma sulfur atom.
   getCoords = lambda line:(float(line[31:38]),
                            float(line[39:46]), float(line[47:54]))
   cysCoords = map(getCoords, cysLines)
   # Make a list of all residue pairs classified as disulfide bonds.
   dsBonds = []

   for i in range(len(cysCoords)-1):
      for j in range(i+1, len(cysCoords)):
         dist = getDistance(cysCoords[i], cysCoords[j])
         residue1 = int(cysLines[i][23:27])
         residue2 = int(cysLines[j][23:27])
         if dist < maxBondDist:
            dsBonds.append((residue1,residue2,dist))

   prettyPrint(dsBonds)

if __name__ == "__main__":
    main()

rawDescription: Indentation in python script

description: Indentation in python script


code:
def addInterest(balance, rate):
    print (balance)
    newBalance = balance * (1 + rate)
    balance = newBalance

amount = 1000
rate = 0.05
print id(amount)
addInterest(amount, rate)

rawDescription: easy functions with multiple parameters Python 2.7

description: Easy functions with multiple parameters Python 2.7


code:
def open_spider(self, spider):
   print spider.name

def close_spider(self, spider):
   print spider.name

def item_scraped(self, item, spider):
   print spider.name

rawDescription: Get spider name in scrapy feed exporter

description: Get spider name in scrapy feed exporter


code:
for cell in cell_list:
    print cell.value
    print "Row:", cell.row, "Column:", cell.col

rawDescription: gspread or such: help me get cell coordinates (not value)

description: Gspread or such: help me get cell coordinates (not value)


code:
boys = ['Kedrick','Jonathan','Tim','Philip','John','Quincy'];
girls  = ['Annabel','Janet','Jocelyn','Pamela','Priscilla','Viviana'];

matchesBoys = {i:{'to':''} for i in boys}
matchesGirls = {i:{'to':''} for i in girls}

for name in boys:
    giveTo = girls[random.randint(0, len(girls)-1)]
    girls.remove(giveTo)
    matchesBoys[name]['to']=giveTo

for name in matchesGirls:
    giveTo = boys[random.randint(0, len(boys)-1)]
    boys.remove(giveTo)
    matchesGirls[name]['to']=giveTo

del boys, girls
for i in matchesBoys:
    print "%s matched with %s"%(i, matchesBoys[i]['to'])
for i in matchesGirls:
    print '%s matched with %s'%(i, matchesGirls[i]['to'])

rawDescription: Two way matching for Secret Santa game

description: Two way matching for Secret Santa game


code:
import numpy as np
angle = np.arange(-5., 5.)
norm = np.arange(10.)
with np.errstate(divide='ignore'):
    print np.where(norm != 0., angle / norm, -2)
# or:
with np.errstate(divide='ignore'):
    res = angle/norm
res[np.isnan(res)] = -2

rawDescription: RuntimeWarning: Divide by Zero error: How to avoid? PYTHON, NUMPY


code:
import boto

s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')

# Copy the key onto itself, preserving the ACL but changing the content-type
key.copy(key.bucket, key.name, preserve_acl=True,
    metadata={'Content-Type': 'text/plain'})

key = bucket.lookup('mykey')
print key.content_type

rawDescription: Using boto, set content_type on files which are already present on s3


code:
# sender
import message
message.result = someCalculationResult

# receiver
import message
print message.result

rawDescription: Strange way to pass data between modules in Python: How does it work?


code:
import os
print os.environ['BUILD_NUMBER']

rawDescription: How to use jenkins plugin in python script?

description: Use jenkins plugin in python script


code:
import csv

fields = ('category', 'code', 'price', 'description', 'link', 'picture', 'plans')
table_row = '''\

      {description} ({price:.2f})

'''

with open(filename, 'rb') as infile:
    reader = csv.DictReader(infile, fieldnames=fields, delimiter='\t')
    for row in reader:
        row['price'] = float(row['price'])  # needed to make `.2f` formatting work
        print table_row.format(**row)

rawDescription: Whats the python equivalent of a `while read ... do; ... done < file` for tab delimited file?


code:
def gen_user_random():
    from Fourganizical import pack8
    import time,sys
    print 'Hey there user, start a-bashing that keyboard to make some randomness.'
    keystimes = []
    lasttime = None
    while len(keystimes) < 20:
            key = getch()
            timenow = (time.time() + time.clock())
            if lasttime:
                    timesince = timenow-lasttime
                    keystimes.append(int(timesince*100000000000000000))
            lasttime = timenow
    print 'Check out this *nasty* random number you made!'
    rnum = int(''.join([str(x) for x in keystimes]))
    print rnum
    print 'And OMG here is that *nasty* set of bytes it made!'
    rbytes = pack8(rnum)
    print
    sys.stdout.write(''.join(rbytes))
    print
    print
    return keystimes

rawDescription: Best Python way to harvest user entropy from keystrokes a la PGP?


code:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

handshake = "9601aa2a2768b58af647e03f23e3bdaa5d9ab12d648d85755aaaee430ea273946dec5348aaa61dc261138d50d2c80966b7113a659ad5a3998263e2dc0ce4bba5dbd22d2c6bc5c4c75802b8b130d1b0b39558d5516bbe1a1ce2852a6495e52fda5259626480ec1aede49a085b5f6005a1317cfabf72ff740cd038808e01fb3ac2".decode('hex')

privkey = open('server.key', 'r').read()
rsakey = RSA.importKey(privkey)
rsa = PKCS1_v1_5.new(rsakey)
sentinel = 'Failure'
decrypted = rsa.decrypt(handshake, sentinel)

print decrypted.encode('hex')

rawDescription: Decrypt SSL Premaster Key

description: Decrypt SSL Premaster Key


code:
import os.path
i = 3
print os.path.join("C:\Test", str(i) + ".jpg")

rawDescription: Understanding Python's backslash escape character


code:
edges = {}
with open("Input_1.txt", "r") as edge_data:
    for row in edge_data:
        k, v = row.strip().split(")") # split as key, value
        k = " ".join(i.strip("'") for i in k.strip("(").split(", ")) # clean unwanted symbol and merge together
        v = v.strip(" []").split(", ") # get list value
        edges[k] = v

with open("Input_2", "r") as classified_data:
    for row in classified_data:
        k = row.strip();
        for v in edges.get(k, []):
            print k, v

rawDescription: Dict and List Manipulation Python

description: Dict and List Manipulation Python


code:
import os
import math

"""
A simple backprop neural network. It has 3 layers:
    Input layer: 2 neurons
    Hidden layer: 2 neurons
    Output layer: 1 neuron
"""

class Weight:
    """
    Class representing a weight between two neurons
    """
    def __init__(self, value, from_neuron, to_neuron):
        self.value = value
        self.from_neuron = from_neuron
        from_neuron.outputWeights.append(self)
        self.to_neuron = to_neuron
        to_neuron.inputWeights.append(self)

        # delta value, this will accumulate and after each training cycle
        # will be used to adjust the weight value
        self.delta = 0.0

class Neuron:
    """
    Class representing a neuron.
    """
    def __init__(self):
        self.value = 0.0        # the output
        self.idealValue = 0.0   # the ideal output
        self.error = 0.0        # error between output and ideal output
        self.inputWeights = []    # weights that end in the neuron
        self.outputWeights = []  # weights that starts in the neuron

    def activate(self):
        """
        Calculate an activation function of a neuron which is
        a sum of all input weights * neurons where those weights start
        """
        x = 0.0;
        for weight in self.inputWeights:
            x += weight.value * weight.from_neuron.value
        # sigmoid function
        self.value = 1 / (1 + math.exp(-x))

class Network:
    """
    Class representing a whole neural network. Contains layers.
    """
    def __init__(self, layers, learningRate, weights):
        self.layers = layers
        self.learningRate = learningRate    # the rate at which the network learns
        self.weights = weights

    def training(self, entries, expectedOutput):
        for i in range(len(entries)):
            self.layers[0][i].value = entries[i]
        for i in range(len(expectedOutput)):
            self.layers[2][i].idealValue = expectedOutput[i]
        for layer in self.layers[1:]:
            for n in layer:
                n.activate()
        for n in self.layers[2]:
            error = (n.idealValue - n.value) * n.value * (1 - n.value)
            n.error = error
        for n in self.layers[1]:
            error = 0.0
            for w in n.outputWeights:
                error += w.to_neuron.error * w.value
            n.error = error
        for w in self.weights:
            w.delta += w.from_neuron.value * w.to_neuron.error

    def updateWeights(self):
        for w in self.weights:
            w.value += self.learningRate * w.delta

    def calculateSingleOutput(self, entries):
        """
        Calculate a single output for input values.
        This will be used to debug the already learned network after training.
        """
        for i in range(len(entries)):
            self.layers[0][i].value = entries[i]
        # activation function for output layer
        for layer in self.layers[1:]:
            for n in layer:
                n.activate()
        print self.layers[2][0].value

#------------------------------ initialize objects etc

neurons = [Neuron() for n in range(5)]

w1 = Weight(-0.79, neurons[0], neurons[2])
w2 = Weight( 0.51, neurons[0], neurons[3])
w3 = Weight( 0.27, neurons[1], neurons[2])
w4 = Weight(-0.48, neurons[1], neurons[3])
w5 = Weight(-0.33, neurons[2], neurons[4])
w6 = Weight( 0.09, neurons[3], neurons[4])

weights = [w1, w2, w3, w4, w5, w6]
inputLayer  = [neurons[0], neurons[1]]
hiddenLayer = [neurons[2], neurons[3]]
outputLayer = [neurons[4]]
learningRate = 0.3
network = Network([inputLayer, hiddenLayer, outputLayer], learningRate, weights)

# just for debugging, the real training set is much larger
trainingSet = [([0.0,0.0],[0.0]),
               ([1.0,0.0],[1.0]),
               ([2.0,0.0],[1.0]),
               ([0.0,1.0],[0.0]),
               ([1.0,1.0],[1.0]),
               ([2.0,1.0],[0.0]),
               ([0.0,2.0],[0.0]),
               ([1.0,2.0],[0.0]),
               ([2.0,2.0],[1.0])]

#------------------------------ let's train
for i in range(100): # training iterations
    for entries, expectedOutput in trainingSet:
        network.training(entries, expectedOutput)
    network.updateWeights()

#network has learned, let's check
network.calculateSingleOutput((1, 0)) # this should be close to 1
network.calculateSingleOutput((0, 0)) # this should be close to 0

rawDescription: Help me with my backprop implementation in Python


code:
import time
run = raw_input("Start? > ")
mins = 0
# Only run if the user types in "start"
if run == "start":
    # Loop until we reach 20 minutes running
    while mins != 20:
        print ">>>>>>>>>>>>>>>>>>>>>", mins
        # Sleep for a minute
        time.sleep(60)
        # Increment the minute total
        mins += 1
    # Bring up the dialog box here

rawDescription: Creating a timer in python

description: Creating a timer in python


code:
# Create soup from the html. (Here I am assuming that you have already read the file into
# the variable "html" as a string).
soup = BeautifulSoup(html)
# Find all divs with class="address"
address_class = soup.find_all('div', {"class": "address"})
# Loop over the results
for row in address_class:
  # Each result has one <a> tag, and we need to get the href property from it.
  print row.find('a').get('href')

rawDescription: Parse JavaScript href with Python

description: Parse JavaScript href with Python


code:
with open(__file__) as f:
    print f.read()

rawDescription: Python script that prints its source

description: Python script that prints its source


code:
x = ['AU_TEST_PUBLIC_BRANCH.05.01.01.151.005\r\n']
print x[0].rstrip()

rawDescription: Parsing the text in a file

description: Parsing the text in a file


code:
mime = {"html":"text/html", "css":"text/css", "png":"image/png"}
if RequestedFileType in mime.keys():
    self.send_response(200)
    self.send_header('Content-type', mime[RequestedFileType])
    self.end_headers()
    print RequestedFileType
    f = open(curdir + sep + self.path)
    self.wfile.write(f.read())
    f.close()
    return

rawDescription: Creating a Python webserver - layout and setup


code:
def disabled(msg):
    def _decorator(f):
        def _wrapper():
            print f.__name__ + ' has been disabled ' + msg
        return _wrapper
    return _decorator

@disabled("I want to skip it")
def testFoo():
    '''Foo test case'''
    print 'this is foo test case'

testFoo()

rawDescription: Disable a python unit test with a message


code:
FOO_SYNTAX = (file, float, int)
def foo_cmd(infile, infloat, inint):
    pass

def parse_args(rule, args):
    if len(rule) != len(args):
        raise ValueError, "Wrong number of arguments"
    return [rule_item(arg) for rule_item, arg in zip(rule, args)]

COMMANDS = {'foo': (FOO_SYNTAX, foo_cmd)}

def dispatch(line):
    cmd, rest = line.split(None, 1)
    args = rest.split()
    syntax, cmd_func = COMMANDS[cmd]
    cmd_func(*parse_args(syntax, args))

rawDescription: How should I convert a Python tuple of strings into dynamically-specified types?


code:
import datetime
import pytest

FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 05, 55)

@pytest.fixture
def patch_datetime_now(monkeypatch):

    class mydatetime:
        @classmethod
        def now(cls):
            return FAKE_TIME

    monkeypatch.setattr(datetime, 'datetime', mydatetime)

def test_patch_datetime(patch_datetime_now):
    assert datetime.datetime.now() == FAKE_TIME

rawDescription: How to monkeypatch python's datetime.datetime.now with py.test?

description: Monkeypatch python's datetime.datetime.now with py.test


code:
import wikipedia

PAGES = ['New York', 'Mercury_(planet)', 'Tucana']

for page in PAGES:
    wikipage = wikipedia.page(page)
    print "Page Title: %s" % wikipage.title
    print "Page URL: %s" % wikipage.url
    print "Nr. of images on page: %d" % len(wikipage.images)
    print " - Main Image: %s" % wikipage.images[0]
    print ""

rawDescription: is there any API to get image from wiki page

description: Is there any API to get image from wiki page


code:
class A(object):
    pass

class B(A):
    def __add__(self, other):
        return self.value + other

a = A()
a.value = 5

a.__class__ = B

print a + 10

rawDescription: Python object conversion

description: Python object conversion


code:
(r'^view/(?P<post_number>\d+)/$', 'foofunc'),

# we're capturing a very simple regular expression \d+ (any digits) as post_number
# to be passed on to foofunc

def foofunc(request, post_number):
    print post_number

# visiting /view/3 would print 3.

rawDescription: what is this url mean in django

description: What is this url mean in django


code:
from numpy import*
import matplotlib.pyplot as plt
import matplotlib.animation as animation

L=1         # lattice constant = sigma*2**0.5 (Let)

x=array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.1,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18])
#x=zeros(18,float)
y=zeros(18,float)

#~~~~~~~~~~~~~~~~~ ANIMATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def simData():
    t = 1
    while t<=20:
        for i in range(18):
            x[i]=x[i]+L/2.0
            y[i]=y[i]+L/2.0
            if x[i]>L*3:                    # translate back the particle if it goes out of window limit 0 to L*cell
                x[i]=x[i]-L*3
            elif x[i]<0:
                x[i]=L*3-x[i]
            if y[i]>L*3:                    # translate back the particle if it goes out of window limit 0 to L*cell
                y[i]=y[i]-L*3
            elif y[i]<0:
                y[i]=L*3-y[i]
        t=t+1

def simPoints():
    k=0
    for i in range(0,6,1):
        for j in range(0,6,1):
            if (i+j)%2==0:
                x[k]=i*L*.5+.25*L
                y[k]=j*L*.5+.25*L
                k=k+1

fig = plt.figure()
ax = plt.axes()
#simPoints()
line, = ax.plot(x, y,'bo', ms=8)
ax.set_ylim(0, L*3)
ax.set_xlim(0, L*3)

def animate(i):
    simData()
    print x
    line.set_data(x, y)
    return line,

ani = animation.FuncAnimation(fig, animate, blit=False, interval=100, frames=200)

plt.show()

rawDescription: Multi body animation in python returns static picture


code:
import lxml.html as html
import mechanize

br = mechanize.Browser()
page = br.open("http://www.urbandictionary.com/")

br.select_form(nr=0)
br["term"] = "skid"
br.submit()

fragment = html.fromstring(br.response().read())

print fragment.find_class('definition')[0].text_content()

rawDescription: How do I display sentences from a website?

description: Display sentences from a website


code:
english_list = ["fire","apple","morning","river","wind"]
spanish_list = ["fuego","manzana","mañana","río","viento"]
english_to_spanish = dict(zip(english_list, spanish_list))
spanish_to_english = dict(zip(spanish_list, english_list))

def translate(word):
    translation = english_to_spanish.get(word)
    if translation:
        return translation

    translation = spanish_to_english.get(word)
    if translation:
        return translation

    raise Exception('Word {0} does not exists'.format(word))

print("Welcome to the English <--> Spanish Dictionary")
while True:
    word = input("> ")
    if word == 'show':
        wordlist = input("Would you like to see the English or Spanish wordlist?")
        if wordlist == 'english':
            print ','.join(english_list)
        elif wordlist == 'spanish':
            print ','.join(spanish_list)
    else:
        try:
            translate(word)
        except Exception as e:
            print str(e)

rawDescription: Python making a dictionary

description: Python making a dictionary


code:
class Iter(object):
    ' a custom iterator which returns a timestep and corresponding data '

    def __init__(self, fd):
        self.__fd = fd
        self.__timestep = None
        self.__next_timestep = None
        self.__finish = False
        for _ in self.to_next_timestep(): pass # skip header

    def to_next_timestep(self):
        ' iterate until next timestep '
        for line in self.__fd:
            if 'TIMESTEP' in line:
                self.__timestep = self.__next_timestep
                self.__next_timestep = float(line.split('=')[1])
                return
            yield line
        self.__timestep = self.__next_timestep
        self.__finish = True

    def __iter__(self): return self

    def next(self):
        if self.__finish:
            raise StopIteration
        data = genfromtxt(self.to_next_timestep(), delimiter=',')
        return self.__timestep, data

with open('myfile.txt') as fd:
    iter = Iter(fd)
    for timestep, data in iter:
        print timestep, data # data can be selected upon highest timestep

rawDescription: Filtering whilst using numpy.genfromtxt

description: Filtering whilst using numpy.genfromtxt


code:
if True:
    print "long test long test long test long test long"\
    "test long test long test long test long test long test"

rawDescription: Lengthy single line strings in Python without going over maximum line length


code:
import apt

apt_cache = apt.Cache() #High level

apt_cache.update()
apt_cache.open()

list_pkgs = []

for package_name in apt_cache.keys():
    selected_package = apt_cache[package_name]

    #Verify that the package can be upgraded
    if selected_package.isUpgradable:
        pkg = dict(
            name=selected_package.name,
            version= selected_package.installedVersion,
            desc= selected_package.description,
            homepage= selected_package.homepage,
            severity= selected_package.priority)
        list_pkgs.append(pkg)

print list_pkgs

rawDescription: python apt_pkg to obtain individual pkg details?

description: Python apt_pkg to obtain individual pkg details


code:
import re
str = "3249dsf 2013-02-10T06:44:30.666821+00:00 domain constant 66666 sync:[127.0.0.1] Request: pubvalue=kjiduensofksidoposiw&change=09872534&value2=jdmcnhj&counter=232&value3=2&nonce=7896089hujoiuhiuh098h"
print re.search("\[(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]", str).groupdict()

rawDescription: How to parse with one regular expression this string in Python

description: Parse with one regular expression this string in Python


code:
with open("input.txt") as f:
    lastmarkedline = None
    for line in f:
        if line.startswith('>'):
            lastmarkedline = line
        elif lastmarkedline is not None:
            field1 = lastmarkedline.split()[1]
            field2 = line.split()[1]
            print "{0}\t{1}".format(field1, field2)

rawDescription: How to process information from multiple lines in python?

description: Process information from multiple lines in python


code:
parsed = lxml.html.fromstring(html)

for bad in parsed.xpath('//script|//style'):
    bad.getparent().remove(bad)

print lxml.html.tostring(parsed)

rawDescription: Python cache html file

description: Python cache html file


code:
from string import ascii_uppercase, ascii_lowercase, digits, maketrans
s = "Testing123"

intab = ascii_uppercase + ascii_lowercase + digits
outtab = ('L' * 26) + ('l' * 26) + ('n' * 10)
trantab = maketrans(intab, outtab)

print s.translate(trantab)

rawDescription: Replace values in iterable with values of another iterable to the same value


rawDescription: python: function parameter updating issues

description: Python: function parameter updating issues


code:
s = "<</Type/Pages/Kids[4 0 R]/Count 1>>endobj/Count 5"

print re.findall("(Count \d+)",s) # Count followed by  1 or more digits
['Count 1', 'Count 5']

rawDescription: searching for keywords in a file using python

description: Searching for keywords in a file using python


rawDescription: How to check if list of int in the list of string?

description: Check if list of int in the list of string


code:
class glx(object):
    '''Borg pattern singleton, used to pass around refs to objs. Class
    attrs will NOT be reset on new instance calls (instance attrs will).
    '''
    x = ''
    __sharedState = {}
    def __init__(self):
        self.__dict__ = self.__sharedState
        #will be reset on new instance
        self.y = ''

if __name__ == '__main__':
    gl = glx()
    gl.x = ['red', 'green', 'blue']
    gl2 = glx()
    print gl2.x[0]

rawDescription: read & write global variables and lists

description: Read & write global variables and lists


code:
import tkSnack
import thread
import Tkinter as tk

class my_gui(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.grid(row=8)

    def on_button(self):
        thread1=thread.start_new_thread(self.run,(PATH_TO_WAVE_FILE,))

    def run(self, path):
        for k in range(10):
            PITCH_VALUES = self.snack_work(path)
            print PITCH_VALUES

    def snack_work(self, INPUT_WAVE_FILE):
        ## initializing the snack tool
        tkSnack.initializeSnack(self) # use self instead of the separate root object
        # self.withdraw()
        mysound=tkSnack.Sound()

        ## processing original wave file
        mysound.read(INPUT_WAVE_FILE)

        PITCH_VALUES= mysound.pitch()
        return PITCH_VALUES

app = my_gui()
app.mainloop()

rawDescription: Tkinter windows popping up and not able to close them


code:
data='''atl001 I
atl001 I
cms017 H
atl001 R
lhc003 H
cms017 R
cms017 I
lhc003 H
lhc003 R
cms017 R'''

stats={}
for i in data.split('\n'):
    user, irh = i.split()
    u = stats.setdefault(user, {})
    u[irh] = u.setdefault(irh, 0) + 1

print 'user  I  R  H'
for user in sorted(stats):
    stat = stats[user]
    print user, stat.get('I', 0), stat.get('R', 0), stat.get('H', 0)

rawDescription: how to calculate number of items in per user groupby item

description: Calculate number of items in per user groupby item


code:
from PySide import QtGui, QtCore
import sys

class View(QtGui.QGraphicsView):
    def __init__(self, parent = None):
        super(View, self).__init__(parent)
        self.setScene(QtGui.QGraphicsScene(self) )
        self.setSceneRect( 0, 0, 1000, 1000 )
        self.setFixedSize(500, 500)

    def mouseMoveEvent(self, event):
        print
        print self.mapToScene(event.pos()).toTuple()
        # prints (1000, 1000) at lower-right corner
        print event.pos().toTuple()
        # prints (500, 500) at lower-right corner

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

        self.view = View(self)

        hLayout = QtGui.QHBoxLayout()
        hLayout.addWidget(self.view)

        buttonsLayout = QtGui.QVBoxLayout()
        buttonsLayout.setSpacing(0)
        for i in range(10):
            newButton = QtGui.QPushButton()
            buttonsLayout.addWidget(newButton)
        hLayout.addLayout(buttonsLayout)

        self.tempButton = QtGui.QPushButton()

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(hLayout)
        mainLayout.addWidget(self.tempButton)
        self.setLayout(mainLayout)

    def run(self):
        self.show()
        print
        print self.view.viewport().width() # outputs 485
        print self.view.width() # outputs 500
        print self.view.rect() # outputs QRect(0, 0, 500, 500)
        print self.view.size() # outputs QSize(500, 500)
        print self.view.sceneRect() #outputs QRect(0, 0, 1000, 1000)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    win = MainWindow()
    win.run()

    sys.exit(app.exec_())

rawDescription: PySide QGraphicsView size

description: PySide QGraphicsView size


code:
def remove_dup(string):
   ans_str = string[0] # sets "ans_str" equal to the first character of string

   for s in string: # loops through all of the characters in string
      if ans_str.find(s) == -1:
          # this checks if the current letter can be found in ans_strin
          # if the current letter is found it will return 0 if not it returns -1
          ans_str += s # this appends the current character to ans_str if ans_string.find() returns -1
   return ans_str

print remove_dup("aabbacc")

rawDescription: Removing Duplicates in String -Python

description: Removing Duplicates in String -Python


code:
data = """ Foo; Bar; Aoicsdeadwcwewrw; owierwicowmwoemow; aoweirwoer
 ODIFUWE
 acowierwe; asodicjwoer; s; ow; w; w""".split("\n")

for row in data:
    row = row.split(";")[:5]
    res = []
    for item in row:
        if len(";".join(res + [item])) > 45: break
        res.append(item)
    print ";".join(res)

rawDescription: Slicing list by characters and elements, Python


code:
import datetime
now = datetime.datetime.now()
print now.year, now.month, now.day, now.hour, now.minute, now.second
# 2015 5 6 8 53 40

rawDescription: How to get current time in python and break up into year, month, day, hour, minute?

description: Get current time in python and break up into year, month, day, hour, minute


code:
from scrapy.contrib.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy import Item, Field
from scrapy.http import Request
import urlparse

class Eb5Item(Item):

    name = Field()
    email = Field()
    name = Field()
    description = Field()
    phone = Field()
    url = Field()

class Eb5Spider(CrawlSpider):

    name = 'eb5'
    allowed_domains = ["eb5info.com"]
    start_urls = ["http://eb5info.com/regional-centers/"]

    def parse(self, response):
        hxs = Selector(response)
        sites = hxs.xpath('//ul/li/a/@href')
        for site in sites:
            yield Request(urlparse.urljoin("http://www.eb5info.com", site.extract()), callback=self.parse_item)

    def parse_item(self, response):
        hxs = Selector(response)
        item = Eb5Item()
        item['url'] = hxs.xpath("//dd/a/@href").extract()
        # item[blah]...
        print item['url']
        return item

rawDescription: contact info extraction by web crawling with scrapy / python

description: Contact info extraction by web crawling with scrapy / python


rawDescription: Why is Python 2.X division not working as intended even after using float


code:
import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.text = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
        self.text.Bind(wx.EVT_KEY_DOWN, self.onEnter)
        btn = wx.Button(self, label="Do something")
        self.text.SetFocus()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.text, 0, wx.EXPAND|wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onEnter(self, event):
        """"""
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER or keycode == wx.WXK_TAB:
            self.process_text(event=None)
            event.EventObject.Navigate()
        event.Skip()

    #----------------------------------------------------------------------
    def process_text(self, event):
        """
        Do something with the text
        """
        text = self.text.GetValue()
        print text.upper()
        for word in text.split():
            print word

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="TextCtrl Demo")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

rawDescription: Making a wxPython TextCtrl respond to cr or tab


code:
from PySide import QtGui

class Window(QtGui.QWidget):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        self.updateFromSpinbox = False # Used so it slider doesn't run function twice
        self.spinbox = QtGui.QDoubleSpinBox()
        self.spinbox.setRange(0.0, 1.0)
        self.spinbox.setDecimals(3)
        self.spinbox.setSingleStep(0.1)

        self.sld = QtGui.QSlider()
        self.sld.setRange(0, 1000)

        self.checkButton = QtGui.QPushButton('Set interactive mode')
        self.checkButton.setCheckable(True)

        self.sld.valueChanged[int].connect(self.update_spinbox)
        self.sld.sliderReleased.connect(self.slider_released)
        self.spinbox.editingFinished.connect(self.update_slider_position)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.spinbox)
        mainLayout.addWidget(self.sld)
        mainLayout.addWidget(self.checkButton)
        self.setLayout(mainLayout)

        self.resize(200, 200)
        self.show()

    # Runs when interactive mode is disabled
    def slider_released(self):
        if not self.checkButton.isChecked():
            self.myFunction()

    def update_spinbox(self, value):
        if not self.updateFromSpinbox:
            self.spinbox.setValue(float(value)/1000)
            if self.checkButton.isChecked():
                self.myFunction()

    def update_slider_position(self):
        self.updateFromSpinbox = True
        self.sld.setSliderPosition(self.spinbox.value()*1000)
        self.updateFromSpinbox = False
        self.myFunction()

    # Main function to run when value updates
    def myFunction(self):
        print self.sld.value()

win = Window()

rawDescription: Getting a QDoubleDoubleSpinBox to display the (converted) value of a QSlider and still use setTracking(False)


code:
def linetype(line):
    if line.startswith("a"):
        return 1
    if line.startswith("b"):
        return 2
    try:
        parts = [int(x) for x in line.split()]
        if len(parts) >=3 and parts[-1] == 0:
            return 3
    except:
        pass
    raise Exception("Unknown Line Type")

maxtype = 0

for line in open("filename","r"):  #iterate over each line in the file
    line = line.strip() # strip any whitespace
    if line == "":      # if we're left with a blank line
        continue        # continue to the next iteration

    lt = linetype(line) # get the line type of the line
                        # or raise an exception if unknown type
    if lt >= maxtype:   # as long as our type is increasing
        maxtype = lt    # note the current type
    else:               # otherwise line type decreased
        raise Exception("Out of Order")  # so raise exception

print "Validates"  # if we made it here, we validated

rawDescription: Python - Check Order of Lines in File

description: Python - Check Order of Lines in File


code:
import re
EXAMPLE_REGEX = re.compile("(.*):(.*)(?:e.?g.?|(?:for )?example)(.*)")
print EXAMPLE_REGEX.findall('Foo: The companion of bar e.g. I have class called FooBar')

rawDescription: Regex For Parsing a Definition In Plain English with Example


code:
import urllib, urllib2, cookielib

username = 'myuser'
password = 'mypassword'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
opener.open('http://www.example.com/login.php', login_data)
resp = opener.open('http://www.example.com/hiddenpage.php')
print resp.read()

rawDescription: How to use Python to login to a webpage and retrieve cookies for later usage?

description: Use Python to login to a webpage and retrieve cookies for later usage


code:
from PyQt4 import QtCore, QtGui
class ShowEventFilter(QtCore.QObject):
    def eventFilter(self, filteredObj, event):
        if event.type() == QtCore.QEvent.Show:
            print "Popup Showed !"
            # do whatever you want
        return QtCore.QObject.eventFilter(self, filteredObj, event)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    cb = QtGui.QComboBox()
    cb.addItems(['a', 'b', 'c'])

    eventFilter = ShowEventFilter()
    cb.view().installEventFilter(eventFilter)
    cb.show()
    app.exec_()

rawDescription: dropdown event/callback in combo-box in pyqt4

description: Dropdown event/callback in combo-box in pyqt4


code: print os.getcwd()

rawDescription: Configure PyCharm IDE to organize the imports the way Eclipse does (Django development)


code:
import numpy as np
import scipy.stats as st

data = np.random.random(10000)
distributions = [st.laplace, st.norm]
mles = []

for distribution in distributions:
    pars = distribution.fit(data)
    mle = distribution.nnlf(pars, data)
    mles.append(mle)

results = [(distribution.name, mle) for distribution, mle in zip(distributions, mles)]
best_fit = sorted(zip(distributions, mles), key=lambda d: d[1])[0]
print 'Best fit reached using {}, MLE value: {}'.format(best_fit[0].name, best_fit[1])

rawDescription: Fit data to all possible distributions and return the best fit


code:
import pandas as pd
df = pd.read_csv('filename.txt', sep = "\t")
g2 = df.drop_duplicates(['col3', 'col5'])
g3 = g2.groupby(['col3', 'col5']).size().sum(level=0)
print g3

rawDescription: pandas group by sum of column size

description: Pandas group by sum of column size


code:
def override(p, methods):
    oldType = type(p)
    newType = type(oldType.__name__ + "_Override", (oldType,), methods)
    p.__class__ = newType

class Test(object):
    def __str__(self):
        return "Test"

def p(self):
    print(str(self))

def monkey(x):
    override(x, {"__del__": p})

a=Test()
b=Test()
monkey(a)
print "Deleting a:"
del a
print "Deleting b:"
del b

rawDescription: Monkey patch __del__ to new function

description: Monkey patch __del__ to new function


code:
def find(s):
  try:
    sub=re.search(r"\[(.*?)\]",s).group(1)
    return sub,s.split().index('['+sub.split()[0])
  except AttributeError:
    return '[]'

print find('He is a very good man. He has a [good wife].')
print find('He is a very good man. He has a good wife.')

rawDescription: Get index of some expressions in a file

description: Get index of some expressions in a file


code:
import tensorflow as tf

a = tf.constant([8, 7, 8, 1, 3, 4, 5, 9, 5, 0, 5])
init = tf.initialize_all_variables()

unique_a_vals, unique_idx = tf.unique(a)
count_a_unique = tf.unsorted_segment_sum(tf.ones_like(a),
                                         unique_idx,
                                         tf.shape(a)[0])

more_than_one = tf.greater(count_a_unique, 1)
more_than_one_idx = tf.squeeze(tf.where(more_than_one))
more_than_one_vals = tf.squeeze(tf.gather(unique_a_vals, more_than_one_idx))

# If you want the original indexes:
not_duplicated, _ = tf.listdiff(a, more_than_one_vals)
dups_in_a, indexes_in_a = tf.listdiff(a, not_duplicated)

with tf.Session() as s:
    s.run(init)
    a, dupvals, dupidxes, dia = s.run([a, more_than_one_vals,
                                  indexes_in_a, dups_in_a])
    print "Input: ", a
    print "Duplicate values: ", dupvals
    print "Indexes of duplicates in a: ", dupidxes
    print "Dup vals with dups: ", dia

rawDescription: How to find duplicated elements in a 1D Tensor

description: Find duplicated elements in a 1D Tensor


rawDescription: For string with lower then UPPER . Give position when UPPER starts . Python


code:
for status in api.user_timeline():
    print status.id

rawDescription: How to get tweet IDs (since_id, max_id) in tweepy (python)?

description: Get tweet IDs (since_id, max_id) in tweepy (python)


code:
import re

p = re.compile('\d+(\.\d+)?')

a = raw_input('How much is 1 share in that company? ')

while p.match(a) == None:
    print "You need to write a number!\n"
    a = raw_input('How much is 1 share in that company? ')

rawDescription: Python - Using isdigit for floats?

description: Python - Using isdigit for floats


code:
from scrapy import log
from scrapy.contrib.spiders import XMLFeedSpider
from myproject.items import TestItem
from scrapy.selector import XmlXPathSelector
from scrapy.http import Request

class MySpider(XMLFeedSpider):
    name = 'example.com'
    allowed_domains = ['example.com']
    start_urls = ['http://www.example.com/feed.xml']
    iterator = 'iternodes' # This is actually unnecessary, since it's the default value
    itertag = 'item'

    def parse_node(self, response, node):
        log.msg('Hi, this is a <%s> node!: %s' % (self.itertag, ''.join(node.extract())))

        item = Item()
        item['id'] = node.select('@id').extract()
        item['name'] = node.select('name').extract()
        item['description'] = node.select('description').extract()
        return item

    def parse_nodes(self, response, nodes):
        # call built-in method that itself calls parse_node()
        # and yield whatever it returns
        for n in super(MySpider, self).parse_nodes(response, nodes):
            yield n

        # once you're done with item/nodes
        # look for the next page link using XPath
        # these lines are borrowed form
        # https://github.com/scrapy/scrapy/blob/master/scrapy/contrib/spiders/feed.py#L73
        selector = XmlXPathSelector(response)
        self._register_namespaces(selector)
        for link_url in selector.select('//pageSuivante/text()').extract():
            print "link_url", link_url
            yield Request(link_url)

rawDescription: How to add new requests for my Scrapy Spider during crawling

description: Add new requests for my Scrapy Spider during crawling


code:
endofprogram=False
try:
    filename=str(input('Enter file name: '))
    filename2=str(input('Enter file to write: '))
    infile_temp=open(filename,'r')
    infile=infilea.readlines()
    infile_temp.close()
    outfile=open(filename2,'w')

    #IOError if file is not found
except IOError:
    print('End reading file--end of program')
    endofprogram=True

if(endofprogram==False):
    total=0
    alist=[]

    for line in infile:
        line=line.strip('\n')

        if(len(line)!=0)and line[0]!='#':
            name,grade=line.split()
            total=total+float(grade)
            record=(name,float(grade))
            alist.append(record)

            outfile.write(name+" "+str(grade)+'\n')

    average=total/len(alist)
    for item in alist:
        if item[1]<average:
            print item

rawDescription: file program reading and writing python

description: File program reading and writing python


code: print np.einsum('fij,fjk->fik', V, V)

rawDescription: Matrix multiply a numpy array of matrices


code:
for sheet in xlwb.Worksheets:
     xlApp = win32com.client.Dispatch("Excel.Application")
     nwb = xlApp.WorkbookAdd()
     sheet.Copy(Before=nwb.Sheet(1))
     nwb.SaveAs("C:\Users\user\AppData\Local\Temp\\" +sheet.Name+ ".xlsx")
     nwb.Close(True)

rawDescription: Python - Win32com - Open Workbook & Create a New Excel File for Each Tab


code:
import csv
import json

class Node(object):
    def __init__(self, name, size=None):
        self.name = name
        self.children = []
        self.size = size

    def child(self, cname, size=None):
        child_found = [c for c in self.children if c.name == cname]
        if not child_found:
            _child = Node(cname, size)
            self.children.append(_child)
        else:
            _child = child_found[0]
        return _child

    def as_dict(self):
        res = {'name': self.name}
        if self.size is None:
            res['children'] = [c.as_dict() for c in self.children]
        else:
            res['size'] = self.size
        return res

root = Node('Flare')

with open('/tmp/test.csv', 'r') as f:
    reader = csv.reader(f)
    reader.next()
    for row in reader:
        grp1, grp2, grp3, size = row
        root.child(grp1).child(grp2).child(grp3, size)

print json.dumps(root.as_dict(), indent=4)

rawDescription: Python to parent/child JSON

description: Python to parent/child JSON


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

class MyWindow(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)

        self.hbox = QHBoxLayout(self)
        self.myButtons = QDialogButtonBox(self)
        self.hbox.addWidget(self.myButtons)
        button = self.myButtons.addButton(QDialogButtonBox.Open)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    translator = QTranslator()
    print translator.load("qt_ru", QLibraryInfo.location(QLibraryInfo.TranslationsPath))
    app.installTranslator(translator)
    ui = MyWindow()
    ui.show()
    sys.exit(app.exec_())

rawDescription: Pyqt English to Finnish Translation

description: Pyqt English to Finnish Translation


code:
from itertools import cycle, chain, repeat
def process(setup):
    # to use a dictionary, just change the end to in setup.items()
    return cycle(chain.from_iterable(repeat(c, n) for c, n in setup))

# A namedtuple would also be appropriate here
setup = [('A', 2), ('B', 1), ('C', 3)]

it = process(setup) # global variables are of the devil
print next(it) # repeat ad nauseam

rawDescription: Rotating return value by ratio function the pythonista way


code:
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))

def on_focus(event):
    print ax.hitlist(event)
fig.canvas.mpl_connect("motion_notify_event", on_focus)
plt.show()

rawDescription: matplotlib detect object upon mouse event

description: Matplotlib detect object upon mouse event


code:
def a():
    x = 5 # x is within scope while the function is being executed
    print x

a()
# x is now out of scope, has no references and can now be deleted

rawDescription: when does python delete variables?

description: When does python delete variables


code:
import threading

response = None
def user_input():
    global response
    response = raw_input("Do you wish to reconnect? ")

user = threading.Thread(target=user_input)
user.daemon = True
user.start()
user.join(2)
if response is None:
    print
    print 'Exiting'
else:
    print 'As you wish'

rawDescription: Python cancel raw_input/input via writing to stdin?


code:
entries = []
duplicate_entries = []
with open('in.txt', 'r') as my_file:
    for line in my_file:
        columns = line.strip().split(',')
        if columns[2] not in entries:
            entries.append(columns[2])
        else:
            duplicate_entries.append(columns[2])

if len(duplicate_entries) > 0:
    with open('out.txt', 'w') as out_file:
        with open('in.txt', 'r') as my_file:
            for line in my_file:
                columns = line.strip().split(',')
                if columns[2] in duplicate_entries:
                    print line.strip()
                    out_file.write(line)
else:
    print "No repetitions"

rawDescription: Python - Display rows with repeated values in csv files


rawDescription: Slicing in python similar to MATLAB

description: Slicing in python similar to MATLAB


code:
from itertools import permutations
from random import randint
from pprint import pprint

def generate_genes():
    """
    Generates a boilerplate list of genes
    @rtype : list
    """
    tuple_list = []

    for i in range(16):

        binary_var = bin(i)[2:]

        if len(binary_var) != 4:
            binary_var = "0" * (4 - len(binary_var)) + binary_var

        tuple_list.append([('A', (1 if binary_var[0] == '1' else 0)),
                           ('B', (1 if binary_var[1] == '1' else 0)),
                           ('C', (1 if binary_var[2] == '1' else 0)),
                           ('D', (1 if binary_var[3] == '1' else 0))])

    return tuple_list

def all_possible_genes():
    """ Generates all possible combinations of ABCD genes
    @return: returns a list of combinations
    @rtype: tuple
    """
    gene_list = generate_genes()
    all_possible_permutations = []
    for gene in gene_list:
        all_possible_permutations.append([var for var in permutations(gene)])

    return all_possible_permutations

def gene_stringify(gene_tuple):
    """
    @type gene_tuple : tuple
    @param gene_tuple: The gene tuple generated
    """

    return "".join(str(var[0]) for var in gene_tuple if var[1])

def dameraulevenshtein(seq1, seq2):
    """Calculate the Damerau-Levenshtein distance between sequences.

    This distance is the number of additions, deletions, substitutions,
    and transpositions needed to transform the first sequence into the
    second. Although generally used with strings, any sequences of
    comparable objects will work.

    Transpositions are exchanges of *consecutive* characters; all other
    operations are self-explanatory.

    This implementation is O(N*M) time and O(M) space, for N and M the
    lengths of the two sequences.

    >>> dameraulevenshtein('ba', 'abc')
    2
    >>> dameraulevenshtein('fee', 'deed')
    2

    It works with arbitrary sequences too:
    >>> dameraulevenshtein('abcd', ['b', 'a', 'c', 'd', 'e'])
    2
    """
    # codesnippet:D0DE4716-B6E6-4161-9219-2903BF8F547F
    # Conceptually, this is based on a len(seq1) + 1 * len(seq2) + 1 matrix.
    # However, only the current and two previous rows are needed at once,
    # so we only store those.
    oneago = None
    thisrow = range(1, len(seq2) + 1) + [0]
    for x in xrange(len(seq1)):
        # Python lists wrap around for negative indices, so put the
        # leftmost column at the *end* of the list. This matches with
        # the zero-indexed strings and saves extra calculation.
        twoago, oneago, thisrow = oneago, thisrow, [0] * len(seq2) + [x + 1]
        for y in xrange(len(seq2)):
            delcost = oneago[y] + 1
            addcost = thisrow[y - 1] + 1
            subcost = oneago[y - 1] + (seq1[x] != seq2[y])
            thisrow[y] = min(delcost, addcost, subcost)
            # This block deals with transpositions
            if (x > 0 and y > 0 and seq1[x] == seq2[y - 1]
                and seq1[x - 1] == seq2[y] and seq1[x] != seq2[y]):
                thisrow[y] = min(thisrow[y], twoago[y - 2] + 1)
    return thisrow[len(seq2) - 1]

if __name__ == '__main__':
    genes = all_possible_genes()
    list1 = genes[randint(0, 15)][randint(0, 23)]
    list2 = genes[randint(0, 15)][randint(0, 23)]

    print gene_stringify(list1)
    pprint(list1)
    print gene_stringify(list2)
    pprint(list2)
    print dameraulevenshtein(gene_stringify(list1), gene_stringify(list2))

rawDescription: Multiple sequence comparison of arbitrary string with oriented characters


code:
import random, math, time
from Tkinter import * # our UI

def sqr(x):
    return x*x

class Point:
    def __init__(self,x,y):
        self.x = float(x)
        self.y = float(y)
        self.left = 0
        self.right = []
    def __repr__(self):
        return "("+str(self.x)+","+str(self.y)+")"
    def distance(self,other):
        return math.sqrt(sqr(self.x-other.x)+sqr(self.y-other.y))

def equidist(left,right,dist):
    u = (right.x-left.x)
    v = (right.y-left.y)
    if 0 != u:
        r = math.sqrt(sqr(dist)-((sqr(u)+sqr(v))/4.))
        theta = math.atan(v/u)
        x = left.x+(u/2)-(r*math.sin(theta))
        if x < left.x:
            x = left.x+(u/2)+(r*math.sin(theta))
            y = left.y+(v/2)-(r*math.cos(theta))
        else:
            y = left.y+(v/2)+(r*math.cos(theta))
    else:
        theta = math.asin(v/(2*dist))
        x = left.x-(dist*math.cos(theta))
        y = left.y + (v/2)
    return Point(x,y)

class Vis:
    def __init__(self):
        self.frame = Frame(root)
        self.canvas = Canvas(self.frame,bg="white",width=width,height=height)
        self.canvas.pack()
        self.frame.pack()
        self.run()
    def run(self):
        self.count_calc0 = 0
        self.count_calc1 = 0
        self.count_calc2 = 0
        self.count_calc3 = 0
        self.count_calc4 = 0
        self.count_calc5 = 0
        self.prev_x = 0
        self.best = -1
        self.best_centre = []
        for self.sweep in xrange(0,len(points)):
            self.count_calc0 += 1
            if len(points[self.sweep].right) <= self.best:
                break
            self.calc(points[self.sweep])
        self.sweep = len(points) # so that draw() stops highlighting it
        print "BEST",self.best+1, self.best_centre # count left-most point too
        print "counts",self.count_calc0, self.count_calc1,self.count_calc2,self.count_calc3,self.count_calc4,self.count_calc5
        self.draw()
    def calc(self,p):
        for self.right in p.right:
            self.count_calc1 += 1
            if (self.right.left + len(self.right.right)) < self.best:
                # this can never help us
                continue
            self.count_calc2 += 1
            self.centre = equidist(p,self.right,radius)
            assert abs(self.centre.distance(p)-self.centre.distance(self.right)) < 1
            count = 0
            for p2 in p.right:
                self.count_calc3 += 1
                if self.centre.distance(p2) <= radius:
                    count += 1
            if self.best < count:
                self.count_calc4 += 4
                self.best = count
                self.best_centre = [self.centre]
            elif self.best == count:
                self.count_calc5 += 5
                self.best_centre.append(self.centre)
            self.draw()
            self.frame.update()
            time.sleep(0.1)
    def draw(self):
        self.canvas.delete(ALL)
        # draw best circle
        for best in self.best_centre:
            self.canvas.create_oval(best.x-radius,best.y-radius,\
                best.x+radius+1,best.y+radius+1,fill="red",\
                outline="red")
        # draw current circle
        if self.sweep < len(points):
            self.canvas.create_oval(self.centre.x-radius,self.centre.y-radius,\
                self.centre.x+radius+1,self.centre.y+radius+1,fill="pink",\
                outline="pink")
        # draw all the connections
        for p in points:
            for p2 in p.right:
                self.canvas.create_line(p.x,p.y,p2.x,p2.y,fill="lightGray")
        # plot visited points
        for i in xrange(0,self.sweep):
            p = points[i]
            self.canvas.create_line(p.x-2,p.y,p.x+3,p.y,fill="blue")
            self.canvas.create_line(p.x,p.y-2,p.x,p.y+3,fill="blue")
        # plot current point
        if self.sweep < len(points):
            p = points[self.sweep]
            self.canvas.create_line(p.x-2,p.y,p.x+3,p.y,fill="red")
            self.canvas.create_line(p.x,p.y-2,p.x,p.y+3,fill="red")
            self.canvas.create_line(p.x,p.y,self.right.x,self.right.y,fill="red")
            self.canvas.create_line(p.x,p.y,self.centre.x,self.centre.y,fill="cyan")
            self.canvas.create_line(self.right.x,self.right.y,self.centre.x,self.centre.y,fill="cyan")
        # plot unvisited points
        for i in xrange(self.sweep+1,len(points)):
            p = points[i]
            self.canvas.create_line(p.x-2,p.y,p.x+3,p.y,fill="green")
            self.canvas.create_line(p.x,p.y-2,p.x,p.y+3,fill="green")

radius = 60
diameter = radius*2
width = 800
height = 600

points = []

# make some points
for i in xrange(0,100):
    points.append(Point(random.randrange(width),random.randrange(height)))

# sort points for find-the-right sweep
points.sort(lambda a, b: int(a.x)-int(b.x))

# work out those points to the right of each point
for i in xrange(0,len(points)):
    p = points[i]
    for j in xrange(i+1,len(points)):
        p2 = points[j]
        if p2.x > (p.x+diameter):
            break
        if (abs(p.y-p2.y) <= diameter) and \
            p.distance(p2) < diameter:
            p.right.append(p2)
            p2.left += 1

# sort points in potential order for sweep, point with most right first
points.sort(lambda a, b: len(b.right)-len(a.right))

# debug
for p in points:
    print p, p.left, p.right

# show it
root = Tk()
vis = Vis()
root.mainloop()

rawDescription: Python: Visualization tool for graphs

description: Python: Visualization tool for graphs


code:
non_blank_count = 0

with open('data.txt') as infp:
    for line in infp:
       if line.strip():
          non_blank_count += 1

print 'number of non-blank lines found %d' % non_blank_count

rawDescription: Count number of lines in a txt file with Python - but exclude blank lines


code:
import fnmatch
import os
import shutil
import datetime
import time

ROOT_PATH = r'D:/Dropbox'
DEST_DIR = r'D:/Conflicted'

def cerr(message, log):
    date = str(datetime.datetime.now())
    msg = "%s : %s\n" % (date[0:19], message)
    log.write(msg)

def removeConflicted(log):
    for root, dirnames, filenames in os.walk(ROOT_PATH):
        for filename in fnmatch.filter(filenames, '*conflicted*.*'):
            # 1: comment out this line and check for leak
            cerr(os.path.join(root, filename), log)
            # 2: then comment out this line instead and check
            shutil.move(
                os.path.join(root, filename),
                os.path.join(DEST_DIR, filename))

def main():
    with open('./LOG.txt', 'a') as log:
        while True:
            print "loop"
            removeConflicted(log)
            time.sleep(10)

if __name__ == "__main__":
    main()

rawDescription: Python programming memory leak? Maybe? File search & destroy script


code:
import time
import glob
import os
import hashlib

def md5(filename):
    with open(filename, 'rb') as inputfile:
        data = inputfile.read()
        print filename, hashlib.md5(data).hexdigest()

path_to_watch = "."
before = os.listdir(path_to_watch)
while 1:
    time.sleep(3)
    after = os.listdir(path_to_watch)
    added = [f for f in after if not f in before]
    removed = [f for f in before if not f in after]
    if added:
        print "Added: ", ", ".join(added)
        for filename in added:
            md5(filename)
    if removed:
        print "Removed: ", ", ".join(removed)
    before = after

rawDescription: How to combine 2 scripts together?

description: Combine 2 scripts together


code:
letters = set(["a","b","c","d"])
inpt = raw_input()
if set(inpt).issubset(letters):
    print "letters are in list"
else:
    print "letters are not in list"

rawDescription: How To check if entered letters are in a list?


code:
import time
import select
import logging
import sys

def stdinWait(interval):
    start = time.time()
    while True:
        time_left = interval - (time.time() - start)
        if time_left <= 0:
            break
        r, w, x = select.select([sys.stdin], [], [], time_left)
        if r:
            line = r[0].readline()
            try:
                print(eval(line));
            except Exception, e:
                logging.exception(e)

def runJobs(comps, jobQueue, numRunning, limit, lock):
  while len(jobQueue) >= 0:
      print(len(jobQueue));
      if len(jobQueue) > 0:
          comp, tasks = find_computer(comps, 0);
          #do something
          stdinWait(5) # wait 5 seconds while watching stdin

rawDescription: Sleeping a thread is blocking stdin

description: Sleeping a thread is blocking stdin


code:
import gtk.gdk

w = gtk.gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
if (pb != None):
    pb.save("screenshot.png","png")
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

rawDescription: Take a screenshot and use OCR on it

description: Take a screenshot and use OCR on it


code:
f = open(fileName,'r')
content = f.readlines()
content.reverse()
for line in content:
    if line.strip() == "*  ============================== INERTIA ==============================":
        index = content.index(line)
        break
for line in content[index::-1]:
    print line

rawDescription: finding the latest modified table in a .txt file

description: Finding the latest modified table in a .txt file


code:
import datetime

if __name__ == '__main__':
    from_raw = raw_input('\nEnter FROM Date (e.g. 2013-11-29) :')
    from_date = datetime.date(*map(int, from_raw.split('-')))
    print 'From date: = ' + str(from_date)
    to_raw = raw_input('\nEnter TO Date (e.g. 2013-11-30) :')
    to_date = datetime.date(*map(int, to_raw.split('-')))

    in_file = './file.csv'
    for line in in_file:
        fields = line.split(',')
        found_date = datetime.date(*map(int, fields[0].split(' ')[0].split('-')))
        if from_date <= found_date <= to_date:
            print line

    in_file.close()

rawDescription: Read specific date range from csv file using Python


code:
def get_adjacency(graph):
    graph = {node: set(children) for node, children in graph.items()}

    for node, children in graph.items():
        for child in children:
            children = children - graph[child]
        graph[node] = children

    return {node: list(children) for node, children in graph.items()}

c = {
    'a': ['b','c','d','e'],
    'b': ['d'],
    'c': ['d','e'],
    'd': [],
    'e': [],
    'f': ['i','j','c','e','d'],
    'i': ['c','e','d'],
    'j': ['e']
}

print get_adjacency(c)

rawDescription: Graph challenge: Get adjacency list from list of children nodes


code:
import cgi
import cgitb     #this...
cgitb.enable()   #..and this are not really necessary but helps debuging

data= cgi.FieldStorage()

#remove this commant to take a look in the data received by python
#print data

#here you can retrieve the value passed by ajax
print data['testdata'].value

rawDescription: Passing JSON from jQuery .getJSON to Python


code:
z = np.zeros([N, N, 3])
z[:,:,0] = x.reshape(-1,1)
z[:,:,1] = x
fast_grid = z.reshape(N*N, 3)

print np.all( grid == fast_grid )
True

rawDescription: Creating Grid with Numpy Performance

description: Creating Grid with Numpy Performance


code:
list1 = [0, 2, 1, 3, 4]
list2 = [0, 2, 3]
minimum = min(list2,key=lambda x: list1[::-1].index(x))
print minimum
3

rawDescription: Python: Finding the minimum value given a list of order


code:
print max((l[1], l[0], k) for k in d for l in d[k])
# (5.0, 'C', 'A')

rawDescription: Finding longest path in a dictionary in python


code:
import math
import cv2
import numpy as np

def rotate_image(image, angle):
    """
    Rotates an OpenCV 2 / NumPy image about it's centre by the given angle
    (in radians). The returned image will be large enough to hold the entire
    new image, with a black background
    """

    # Get the image size
    # No that's not an error - NumPy stores image matricies backwards
    image_size = (image.shape[1], image.shape[0])
    image_center = tuple(np.array(image_size) / 2)

    # Convert the OpenCV 3x2 rotation matrix to 3x3
    rot_mat = np.vstack(
        [cv2.getRotationMatrix2D(image_center, angle, 1.0), [0, 0, 1]]
    )

    rot_mat_notranslate = np.matrix(rot_mat[0:2, 0:2])

    # Shorthand for below calcs
    image_w2 = image_size[0] * 0.5
    image_h2 = image_size[1] * 0.5

    # Obtain the rotated coordinates of the image corners
    rotated_coords = [
        (np.array([-image_w2,  image_h2]) * rot_mat_notranslate).A[0],
        (np.array([ image_w2,  image_h2]) * rot_mat_notranslate).A[0],
        (np.array([-image_w2, -image_h2]) * rot_mat_notranslate).A[0],
        (np.array([ image_w2, -image_h2]) * rot_mat_notranslate).A[0]
    ]

    # Find the size of the new image
    x_coords = [pt[0] for pt in rotated_coords]
    x_pos = [x for x in x_coords if x > 0]
    x_neg = [x for x in x_coords if x < 0]

    y_coords = [pt[1] for pt in rotated_coords]
    y_pos = [y for y in y_coords if y > 0]
    y_neg = [y for y in y_coords if y < 0]

    right_bound = max(x_pos)
    left_bound = min(x_neg)
    top_bound = max(y_pos)
    bot_bound = min(y_neg)

    new_w = int(abs(right_bound - left_bound))
    new_h = int(abs(top_bound - bot_bound))

    # We require a translation matrix to keep the image centred
    trans_mat = np.matrix([
        [1, 0, int(new_w * 0.5 - image_w2)],
        [0, 1, int(new_h * 0.5 - image_h2)],
        [0, 0, 1]
    ])

    # Compute the tranform for the combined rotation and translation
    affine_mat = (np.matrix(trans_mat) * np.matrix(rot_mat))[0:2, :]

    # Apply the transform
    result = cv2.warpAffine(
        image,
        affine_mat,
        (new_w, new_h),
        flags=cv2.INTER_LINEAR
    )

    return result

def largest_rotated_rect(w, h, angle):
    """
    Given a rectangle of size wxh that has been rotated by 'angle' (in
    radians), computes the width and height of the largest possible
    axis-aligned rectangle within the rotated rectangle.

    Original JS code by 'Andri' and Magnus Hoff from Stack Overflow

    Converted to Python by Aaron Snoswell
    """

    quadrant = int(math.floor(angle / (math.pi / 2))) & 3
    sign_alpha = angle if ((quadrant & 1) == 0) else math.pi - angle
    alpha = (sign_alpha % math.pi + math.pi) % math.pi

    bb_w = w * math.cos(alpha) + h * math.sin(alpha)
    bb_h = w * math.sin(alpha) + h * math.cos(alpha)

    gamma = math.atan2(bb_w, bb_w) if (w < h) else math.atan2(bb_w, bb_w)

    delta = math.pi - alpha - gamma

    length = h if (w < h) else w

    d = length * math.cos(alpha)
    a = d * math.sin(alpha) / math.sin(delta)

    y = a * math.cos(gamma)
    x = y * math.tan(gamma)

    return (
        bb_w - 2 * x,
        bb_h - 2 * y
    )

def crop_around_center(image, width, height):
    """
    Given a NumPy / OpenCV 2 image, crops it to the given width and height,
    around it's centre point
    """

    image_size = (image.shape[1], image.shape[0])
    image_center = (int(image_size[0] * 0.5), int(image_size[1] * 0.5))

    if(width > image_size[0]):
        width = image_size[0]

    if(height > image_size[1]):
        height = image_size[1]

    x1 = int(image_center[0] - width * 0.5)
    x2 = int(image_center[0] + width * 0.5)
    y1 = int(image_center[1] - height * 0.5)
    y2 = int(image_center[1] + height * 0.5)

    return image[y1:y2, x1:x2]

def demo():
    """
    Demos the largest_rotated_rect function
    """

    image = cv2.imread("lenna_rectangle.png")
    image_height, image_width = image.shape[0:2]

    cv2.imshow("Original Image", image)

    print "Press [enter] to begin the demo"
    print "Press [q] or Escape to quit"

    key = cv2.waitKey(0)
    if key == ord("q") or key == 27:
        exit()

    for i in np.arange(0, 360, 0.5):
        image_orig = np.copy(image)
        image_rotated = rotate_image(image, i)
        image_rotated_cropped = crop_around_center(
            image_rotated,
            *largest_rotated_rect(
                image_width,
                image_height,
                math.radians(i)
            )
        )

        key = cv2.waitKey(2)
        if(key == ord("q") or key == 27):
            exit()

        cv2.imshow("Original Image", image_orig)
        cv2.imshow("Rotated Image", image_rotated)
        cv2.imshow("Cropped Image", image_rotated_cropped)

    print "Done"

if __name__ == "__main__":
    demo()

rawDescription: Rotate image and crop out black borders

description: Rotate image and crop out black borders


code:
from xml.dom.minidom import parseString
dom = parseString('''\
<message>
  <text>
    Hello!
  </text>
</message>''')
dom.childNodes[0].childNodes
text = dom.getElementsByTagName('text')[0].childNodes[0]
text.data = text.data.replace(u'Hello', u'Hello world')
print dom.toxml()

rawDescription: Alter XML while preserving layout

description: Alter XML while preserving layout