code:
#!/usr/bin/env python
import math
import ephem
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt

def main():
    # find a shadow length for a unit-length stick
    o = ephem.Observer()
    o.lat, o.long = '37.0625', '-95.677068'
    now = o.date
    sun = ephem.Sun(o) #NOTE: use observer; it provides coordinates and time
    A = sun.alt
    shadow_len = 1 / math.tan(A)

    # find the next time when the sun will cast a shadow of the same length
    t = ephem.Date(find_next_time(shadow_len, o, sun))
    print "current time:", now, "next time:", t # UTC time
    ####print ephem.localtime(t) # print "next time" in a local timezone

def update(time, sun, observer):
    """Update Sun and observer using given `time`."""
    observer.date = time
    sun.compute(observer) # computes `sun.alt` implicitly.
    # return nothing to remember that it modifies objects inplace

def find_next_time(shadow_len, observer, sun, dt=1e-3):
    """Solve `sun_altitude(time) = known_altitude` equation w.r.t. time."""
    def f(t):
        """Convert the equation to `f(t) = 0` form for the Brent's method.

        where f(t) = sun_altitude(t) - known_altitude
        """
        A = math.atan(1./shadow_len) # len -> altitude
        update(t, sun, observer)
        return sun.alt - A

    # find a, b such as f(a), f(b) have opposite signs
    now = observer.date # time in days
    x = np.arange(now, now + 1, dt) # consider 1 day
    plt.plot(x, map(f, x))
    plt.grid(True)
    ####plt.show()
    # use a, b from the plot (uncomment previous line to see it)
    a, b = now+0.2, now+0.8

    return opt.brentq(f, a, b) # solve f(t) = 0 equation using Brent's method

if __name__=="__main__":
    main()

rawDescription: Compute shadow length using PyEphem

description: Compute shadow length using PyEphem


code:
import csv

# key function for sorting later
def second_and_first(x):
    return (x[1],x[0])

# dictionary to store key_fields and their counts
counter = {}
# list to store rows from the csv file
data = []

with open('report.csv','rb') as infile:
    for row in csv.reader(infile):
        # For convenience, assign the value of row[0] to key_field
        key_field = row[0]
        # if key_field is not in the dictionary counter. Add it with a value of 1
        if key_field not in counter:
            counter[key_field] = 1
        # otherwise, it is there, increment the value by one.
        else:
            counter[key_field] += 1
        # insert the value associated with key_field in the counter into the start of
        # the row
        row.insert(0,counter[key_field])
        # Append the row to
        data.append(row)

for row in sorted(data,key=second_and_first):
    print row

rawDescription: python read csv file as dictionary and sort and increment a counter

description: Python read csv file as dictionary and sort and increment a counter


code:
s = u'Zåìôèðà'
print s.encode('latin1').decode('cp1251')
# Zемфира

rawDescription: How to decode cp1252 string?

description: Decode cp1252 string


code:
# Cauchy numbers generator
def cauchy_numbers():
  while True:
    yield cauchy(3,1)

# running sum utility function
def running_sum(iterable):
  sum = 0
  for x in iterable:
    sum += x
    yield sum

# Running averages generator expression (** the neat part **)
running_avgs = (sum/(i+1) for (i,sum) in enumerate(running_sum(cauchy_numbers())))

# goes on forever
for avg in running_avgs:
  print avg

# alternatively, take just the first 10
import itertools
for avg in itertools.islice(running_avgs, 10):
  print avg

rawDescription: Running average in Python

description: Running average in Python


code:
import re
pattern = re.compile(r'^http://example.com/category/\?.*?(?=page=\d+)')
should_match = [
    'http://example.com/category/?sort=a-z&page=1',
    'http://example.com/category/?page=1&sort=a-z&cache=1',
    'http://example.com/category/?page=1&sort=a-z#'
]
for url in should_match:
    print "Matches" if pattern.search(url) else "Doesn't match"

rawDescription: How to create LinkExtractor rule which based on href in Scrapy

description: Create LinkExtractor rule which based on href in Scrapy


code:
while running:
     event = pygame.event.poll()
     if event.type == pygame.QUIT:
         running = 0
     elif event.type == pygame.MOUSEMOTION:
         print "mouse at (%d, %d)" % event.pos

rawDescription: Python/Pygame mouse position not updating and program losing focus


code:
p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE)
# Grab stdout line by line as it becomes available.  This will loop until
# p terminates.
while p.poll() is None:
    l = p.stdout.readline() # This blocks until it receives a newline.
    print l
# When the subprocess terminates there might be unconsumed output
# that still needs to be processed.
print p.stdout.read()

rawDescription: printing stdout in realtime from a subprocess that requires stdin

description: Printing stdout in realtime from a subprocess that requires stdin


rawDescription: Search and filter pandas dataframe with regular expressions


code:
a = find_position("Lots of foxes", 'o')
print a

rawDescription: Find a character in multiple places python


code:
a = 1
def add(b):
    print a + b

add(2)

rawDescription: Django persistent API connections between requests


code:
a = A()
a.class_method()
print a.__dict__
# {'instance_attribute': 'I am instance attribute'}

rawDescription: Difference between Class Attributes, Instance Attributes, and Instance Methods in Python


code:
r = requests.get("http://orlando.craigslist.org/search/apa?sort=pricedsc")
from bs4 import BeautifulSoup

html = r.content

soup = BeautifulSoup(html)
print "Highest price: ${}".format(prices[0])
print "Lowest price: ${}".format(prices[-1])
print "Average price: ${}".format(sum(prices, 0.0)/len(prices))

rawDescription: web scrapping craigslist appartment prices in python not showing highest cost appartment

description: Web scrapping craigslist appartment prices in python not showing highest cost appartment


code:
from bs4 import BeautifulSoup, Tag

data = """
Main Section

Bla bla bla

Subsection

Some more info

Subsection 2

Even more info!

Main Section 2

bla

Subsection

Some more info

Subsection 2

Even more info!
"""

soup = BeautifulSoup(data)
for main_section in soup.find_all('h2'):
    for sibling in main_section.next_siblings:
        if not isinstance(sibling, Tag):
            continue
        if sibling.name == 'h2':
            break
        print sibling.text
    print "-------"

rawDescription: Iterating through a DOM with BeautifulSoup/Python


code:
d, w = "hello&^uevfehello!`.<hellohow*howdhAreyou", ["hello","how","are","you"]
import re, collections
pattern = re.compile("|".join(w), flags = re.IGNORECASE)
print collections.Counter(pattern.findall(d))

rawDescription: Word frequency in a string without spaces and with special characters?


code:
a = A()
b = B()

print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {}

a.att = b

print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {'att': [<__main__.A object at 0x7f731c8fc910>]}

rawDescription: Python - how to get class instance reference from an attribute class?


rawDescription: How to split my strings with re.split?

description: Split my strings with re.split


code:
class Test(object):
    def __init__(self, x):
        self.x = x

    def printX(self):
        print self.x

rawDescription: Dynamically creating a class from file in Python


code:
import urllib
from bs4 import BeautifulSoup

url = "http://www.microsoft.com/en-us/download/confirmation.aspx?id=17851"
pageurl = urllib.urlopen(url)
soup = BeautifulSoup(pageurl)

urls = []
for d in soup.select("p.start-download [href]"):
    urls.append(d.attrs['href'])

print urls[0]

rawDescription: How to filter and take only one download link?

description: Filter and take only one download link


code:
class BigClass:
    static_data = 42

    def foo(self):
        return "frog"

bc = eval("BigClass" + "()")
print bc.foo()
print bc.static_data

rawDescription: Python - How to call static member using class string?


code:
for group in split_by(L, '@@'):
    print group

rawDescription: How can I split a List (in python) in different parts (after a certain element)?

description: Split a List (in python) in different parts (after a certain element)


code:
class tweet(object):
    def __init__(self, text):
        self.text = text
        self.retweets = 0
    def retweet(self):
        self.retweets += 1
    def __repr__(self):
        return "(%i)" % (self.retweets)
    def __hash__(self):
        return hash(self.text)

class person(object):
    def __init__(self, name):
        self.name = name
        self.tweets = dict()

    def __repr__(self):
        return "%s : %s" % (self.name, self.tweets)

    def new_tweet(self, text):
        self.tweets[text] = tweet(text)

    def retweet(self, text):
        self.tweets[text].retweet()

M = person("mac389")
M.new_tweet('foo')
M.new_tweet('bar')
M.retweet('foo')
M.retweet('foo')

print M

rawDescription: Generate nested dictionary with list and dict comprehensions


code:
import requests as rq
import threading

urls = ["...={}".format(x) for x in range(100)]

def get_status(url):
    if rq.get(url, verify=False).status_code != 404:
        print "Successful: {}\n".format(url)
    else:
        print "Failed: {}".format(url)

for url in urls:
    t = threading.Thread(None, get_status, url, (url,))
    t.start()

rawDescription: For-loop calling urllib.urlopen().getcode() is slow


code:
from datetime import datetime
from babel.dates import format_datetime

now = datetime.utcnow()
format = 'EEE, dd LLL yyyy hh:mm:ss'
print format_datetime(now, format, locale='en') + ' GMT'

rawDescription: RFC 1123 Date Representation in Python?

description: RFC 1123 Date Representation in Python


code:
import urllib
url='example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
urllib.unquote(url).decode('utf8')
print urllib.unquote(url).decode('utf8')

rawDescription: Url decode UTF-8 in Python

description: Url decode UTF-8 in Python


code:
import os, sys, pygst
pygst.require('0.10')
import gst, gobject
gobject.threads_init()

def get_peaks(filename):
    global do_run

    pipeline_txt = (
        'filesrc location="%s" ! decodebin ! audioconvert ! '
        'audio/x-raw-int,channels=1,rate=44100,endianness=1234,'
        'width=32,depth=32,signed=(bool)True !'
        'level name=level interval=1000000000 !'
        'fakesink' % filename)
    pipeline = gst.parse_launch(pipeline_txt)

    level = pipeline.get_by_name('level')
    bus = pipeline.get_bus()
    bus.add_signal_watch()

    peaks = []
    do_run = True

    def show_peak(bus, message):
        global do_run
        if message.type == gst.MESSAGE_EOS:
            pipeline.set_state(gst.STATE_NULL)
            do_run = False
            return
        # filter only on level messages
        if message.src is not level or \
           not message.structure.has_key('peak'):
            return
        peaks.append(message.structure['peak'][0])

    # connect the callback
    bus.connect('message', show_peak)

    # run the pipeline until we got eos
    pipeline.set_state(gst.STATE_PLAYING)
    ctx = gobject.gobject.main_context_default()
    while ctx and do_run:
        ctx.iteration()

    return peaks

def normalize(peaks):
    _min = min(peaks)
    _max = max(peaks)
    d = _max - _min
    return [(x - _min) / d for x in peaks]

if __name__ == '__main__':
    filename = os.path.realpath(sys.argv[1])
    peaks = get_peaks(filename)

    print 'Sample is %d seconds' % len(peaks)
    print 'Minimum is', min(peaks)
    print 'Maximum is', max(peaks)

    peaks = normalize(peaks)
    print peaks

rawDescription: Getting max amplitude for an audio file per second


code:
both_tags = soup.find_all("tr", class_="viewLicense")
for tag in both_tags:
    if 'viewLicenseDetails' not in tag.attrs['class']:
            print tag

rawDescription: Beautiful Soup - Class contains 'a' and not contains 'b'


code:
def to_rna(dna_input):
    dna_rna = {'A':'U', 'C':'G', 'G':'C', 'T':'A'}
    rna = []
    for char in dna_input:
        rna.append(dna_rna.get(char, ''))
    print "".join(rna)

to_rna("ACGTGGTCTTAA") #the string input

rawDescription: How do I replace characters in a string using a character map?

description: Replace characters in a string using a character map


code:
if len(ranking_list) > 0:
    print ranking_list

rawDescription: Scrapy:newbie attempts to pass the null value


code:
from collections import Counter
from nltk.corpus import brown

# Split the words and POS tags
words, poss = zip(*brown.tagged_words())
# Put them into a Counter object
pos_freq = Counter(poss)

for pos in pos_freq:
    print pos, pos_freq[pos]

rawDescription: How to find adjective frequency from a specifique categories in brown corpus in NLTK

description: Find adjective frequency from a specifique categories in brown corpus in NLTK


code:
symbolslist = ["100","200","300","400","500","600","700","800","900","1000","1500","2000","3000","4000","5000","7000","10000"]
pricelist = [] #empty list

i=0
while i<len(symbolslist):
    htmltext = urllib.urlopen("http://www.fifacoinszone.com/default/quick/getpricedetail?platform_id=7&coins="+symbolslist[i] +"&cur=GBP")
    data = json.load(htmltext)
    pricelist.append(data["single_price_just"]) #appends your result to end of the list
    print pricelist[i] #prints the most recently added member of pricelist
    i+=1

rawDescription: Only last iteration of while loop saves

description: Only last iteration of while loop saves


code:
from datetime import datetime as dt

def suffix(d):
    return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')

def custom_strftime(format, t):
    return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))

print custom_strftime('%B {S}, %Y', dt.now())

rawDescription: Display the date, like "May 5th", using pythons strftime?


code:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import io

temp=u"""tweetcricscore 51 highactive
tweetcricscore 46 eventbased
tweetcricscore 12 eventbased
tweetcricscore 46 eventbased
tweetcricscore 1 viewers
tweetcricscore 178 viewers
tweetcricscore 46 situational
tweetcricscore 23 situational
tweetcricscore 1 situational
tweetcricscore 8 situational
tweetcricscore 56 situational"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),
                 sep="\s+", #separator is arbitrary whitespace
                 names=['keyword','freq','class']) #set new col names
print df

rawDescription: Pandas Seaborn Swarmplot doesn't plot

description: Pandas Seaborn Swarmplot doesn't plot


code:
mystring="walk walked walking talk talking talks talked fly flying"
list_of_words=["walk","talk","fly"]

sp = mystring.split()
for x in list_of_words:
    li = [y for y in get_close_matches(x,sp,cutoff=0.5)
          if x in y]
    print '%-7s %d  in %-10s' % (x,len(li),li)

rawDescription: find variations of a word in a string on python

description: Find variations of a word in a string on python


code:
index = 0
coef_features_c1_c2 = []

for feat, c1, c2 in zip(word_vectorizer.get_feature_names(), mnb.feature_count_[0], mnb.feature_count_[1]):
    coef_features_c1_c2.append(tuple([mnb.coef_[0][index], feat, c1, c2]))
    index+=1

for i in sorted(coef_features_c1_c2):
    print i

rawDescription: How to get most informative features for scikit-learn classifier for different class?

description: Get most informative features for scikit-learn classifier for different class


code:
from socket import *

ip="192.168."
print("Searching for IP...")
s=socket(AF_INET,SOCK_STREAM) # create the socket in loop
for i in range(0,256):
    for j in range(0,256):
        host=ip+str(i)+"."+str(j)
        add=(host,1060)
        print host

        #s.settimeout(4) # set the timeout value
        try:
            s.connect(add)
            print("Server Found:",add)
        except Exception, error:
            print(host,"failed")
            print error
        #s.close() # close it
        #del s

rawDescription: How to flood IP addresses to find server

description: Flood IP addresses to find server


code:
for count, group in enumerate(n_split(dblist, 5), 1):
    print 'env{}: {}'.format(count, ' '.join(group))

rawDescription: Splitting a Python list into groups of 5 and then printing


code:
with open("test.txt") as f:
    print f.read() # => prints entire file, sets file pointer to end of file
    f.seek(0) # => reset file pointer to beginning of file
    print f.read() # => prints entire file

rawDescription: Reading and Writing files in Python

description: Reading and Writing files in Python


code:
import collections
c = collections.Counter()
for item in r:
    c[item] += 1
print c
Counter({'there are no': 1, 'are no builtins': 1, 'no builtins for': 1, 'builtins for that': 1})

rawDescription: Is there a built-in function in Python or MySQL which returns a set of 3 frequently occuring words?


code:
from multiprocessing import Process

# called by each thread
def do_task(f, l) :
  with open(f,'r') as fi:
     lines = fi.read().splitlines()
     l.append(lines)

lengths = []
processes = []
files = glob.glob('*.txt') # 5 files
for f in files:
    p = Process(target=do_task, args = (f,lengths,))
    processes.append(p)
    p.start()

for p in processes :
    p.join()

for l in lengths:
    print l

rawDescription: Running multiple programs from the for loop


code:
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
        print row

rawDescription: sort names in alphabitical order in mysql using python

description: Sort names in alphabitical order in mysql using python


code:
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup('''

   <title>Testing</title>

     foo
     Bar

''')

for link in soup.findAll('a'): # find all links
    link['href'] = link['href'] + '?foo'

print soup

rawDescription: Where can I find some "hello world"-simple Beautiful Soup examples?


code:
#/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer
from sqlalchemy.sql import and_, or_
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///')
session = sessionmaker(bind=engine)()
Base = declarative_base()

class Foo(Base):
    __tablename__ = 'foo'

    id = Column(Integer, primary_key=True)
    a = Column(Integer)
    b = Column(Integer)
    c = Column(Integer)

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __repr__(self):
        return '(%d %d %d)' % (self.a, self.b, self.c)

Base.metadata.create_all(engine)

session.add_all([Foo(1, 2, 3), Foo(3, 2, 1), Foo(3, 3, 3), Foo(1, 3, 4)])
session.commit()
items = ((1, 2, 3), (3, 3, 3))
conditions = (and_(Foo.a==x, Foo.b==y, Foo.c==z) for (x, y, z) in items)
q = session.query(Foo)
print q.all()
q = q.filter(or_(*conditions))
print q
print q.all()

rawDescription: How to get rows which match a list of 3-tuples conditions with SQLAlchemy

description: Get rows which match a list of 3-tuples conditions with SQLAlchemy


code:
import multiprocessing
import time

# Your foo function
def foo(n):
    for i in range(10000 * n):
        print "Tick"
        time.sleep(1)

if __name__ == '__main__':
    # Start foo as a process
    p = multiprocessing.Process(target=foo, name="Foo", args=(10,))
    p.start()

    # Wait 10 seconds for foo
    time.sleep(10)

    # Terminate foo
    p.terminate()

    # Cleanup
    p.join()

rawDescription: Stop code after time period

description: Stop code after time period


code:
from fractions import gcd

ratio = '1875:5625:625'

def solve(ratio):
    numbers = [int(i) for i in ratio.split(':')]
    denominater = reduce(gcd,numbers)
    solved = [i/denominater for i in numbers]
    return ':'.join(str(i) for i in solved)

ratio_2_solve =  solve(ratio)
print ratio_2_solve
#3:9:1

rawDescription: How does one simplify a complicated ratio?

description: Simplify a complicated ratio


code:
from itertools import groupby
from operator import itemgetter

for value, group in groupby(list_of_dicts, key=itemgetter('value')):
    print 'Group for value {}'.format(value)
    for d in group:
        print d

rawDescription: Group dict values into chunks with python


rawDescription: Using Python to create a new Numpy array from an existing one


code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
print type(ax)
b = type(ax)
import matplotlib.axes
issubclass(b, matplotlib.axes.SubplotBase)

rawDescription: There is a class matplotlib.axes.AxesSubplot, but the module matplotlib.axes has no attribute AxesSubplot


code:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
    print row.user_id, row.user_name

rawDescription: MSSQL in python 2.7

description: MSSQL in python 2.7


code:
from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

rawDescription: Python multiprocessing an enourmous amount of data


rawDescription: Merge two arrays by collections of two elements


code:
heads = itertools.imap( lambda l: l[0], enumerate(lists))

for head in heads:
   print head

rawDescription: manipulating lists to find different variables

description: Manipulating lists to find different variables


code:
data = [('Apple',      'Coles',      1.50),
        ('Apple',      'Woolworths', 1.60),
        ('Apple',      'IGA',        1.70),
        ('Banana',     'Coles',      0.50),
        ('Banana',     'Woolworths', 0.60),
        ('Banana',     'IGA',        0.70),
        ('Cherry',     'Coles',      5.00),
        ('Date',       'Coles',      2.00),
        ('Date',       'Woolworths', 2.10),
        ('Elderberry', 'IGA',        10.00)]

from itertools import groupby, islice
from operator import itemgetter
from collections import defaultdict

stores = sorted(set(row[1] for row in data))
# probably splitting this up in multiple lines would be more readable
pivot = ((fruit, defaultdict(lambda: None, (islice(d, 1, None) for d in data))) for fruit, data in groupby(sorted(data), itemgetter(0)))

print 'Fruit'.ljust(12), '\t'.join(stores)
for fruit, prices in pivot:
    print fruit.ljust(12), '\t'.join(str(prices[s]) for s in stores)

rawDescription: Produce a summary ("pivot"?) table

description: Produce a summary ("pivot"?) table


code:
import csv
fi = open('xgroup.csv')
fo = open('xgroup3.csv', 'w')
w = csv.writer(fo)
r = csv.reader(fi)
li = 0
print "Opened reader and writer"
for row in r:
    gname = '/'.join(row[1:])
    sname = '/'.join(sorted(row[1:]))
    tname = '/'.join([row[1]]+sorted(row[2:]))
    w.writerow([row[0], gname, sname, tname]+row[1:])
    li += 1
fi.close()
fo.close()
print "Closed reader and writer after",li,"lines"

rawDescription: Extracting Groups

description: Extracting Groups


code:
from pyparsing import *

my_expr = Literal("The") + "quick" + "brown" + "fox" + "jumps" + "over" + "the" + "lazy" + "dog"

string_1 = """The quick brown
fox jumps over the lazy dog.
"""

string_2 = """The quick brown fox jumps
over the lazy dog.
"""

for test in (string_1, string_2):
    print '-'*40
    print test
    print my_expr.parseString(test)
    print

rawDescription: Pyparsing - literal text with line breaks in varying locations


code:
import sys

def bits(byte):
    """Convert byte (byte string of length 1) to bits (int of value,
    0 or 1), most significant bit first.
    """
    byte_as_int = ord(byte)
    for i in reversed(xrange(8)):
        yield (byte_as_int >> i) & 1

# Read byte string from first argument.
bytes = sys.argv[1]

# For each byte in the byte string, print all bit numbers and
# the corresponding bit value, in the format as asked.
for b in bytes:
    for i, bit in enumerate(bits(b)):
        print i, bit

rawDescription: Hex string manipulation in Python

description: Hex string manipulation in Python


code: print [i for i in re.split(r'[\s\[\]]*',s) if i]

rawDescription: Extracting float numbers from file using python


code:
import markup
url_= ('href1.com','href2.com')
mycxml=markup.page(mode='xml', case='given')
mycxml.init(encoding='utf-8')
mycxml.Collection(**{'xmlns:p': 'somelines'})
mycxml.Items(url_)
mycxml.collection.close()
print mycxml

rawDescription: markup.py how to use ':' in a tag

description: Markup.py how to use ':' in a tag


rawDescription: converters option in numpy genfromtxt not accepting -ve indexing of columns

description: Converters option in numpy genfromtxt not accepting -ve indexing of columns


code:
import os

#Define decorator, takes the function to execute as an argument
def dir_or_file_decorator(func):
    def newFunc(path):
        if os.path.isdir(path):
            filenames = os.listdir(path)
            for filename in filenames:
                filepath = os.path.join(path,filename)
                func(filepath)
        else:
            func(path)
    return newFunc

#Define the function we want to decorate
@dir_or_file_decorator
def print_file_name(filepath):
    print filepath

#Run some tests
print 'Testing file'
print_file_name(r'c:\testdir\testfile1.txt')
print 'Testing dir'
print_file_name(r'c:\testdir')

#The @decorator is just syntactic sugar. The code below shows what actually happens
def print_file_name2(filepath):
    print filepath

decorated_func = dir_or_file_decorator(print_file_name2)
print 'Testing file'
decorated_func(r'c:\testdir\testfile1.txt')
print 'Testing dir'
decorated_func(r'c:\testdir')

#Output:
# Testing file
# c:\testdir\testfile1.txt
# Testing dir
# c:\testdir\testfile1.txt
# c:\testdir\testfile2.txt

rawDescription: python decorator for directory

description: Python decorator for directory


code:
def __connect_pop3__(self):
    """\brief Method for connecting to POP3 server
       \return True   If connection to POP3 succeeds or if POP3 is already connected
       \return False  If connection to POP3 fails
    """
    #------Check that POP3 is not already connected-----------------------
    if not self.pop3_connected:
        #------Connect POP3-----------------------------------------------
        self.debug(100, 'Connecting POP3 with: ', self.host_name, self.user_name, self.pass_name)
        self.conn_pop3 = POP3(self.host_name)
        res1 = self.conn_pop3.user(self.user_name)
        string1 = str(res1)
        self.debug(100, 'User identification result:', string1)
        res2 = self.conn_pop3.pass_(self.pass_name)
        string2 = str(res2)
        self.debug(100, 'Pass identification result:', string2)
        #------Check if connection resulted in success--------------------
        #------Server on DavMail returns 'User successfully logged on'----
        if  string2.find('User successfully logged on')<>-1 or string1.find('User successfully logged on')<>-1 :
            self.pop3_connected = True
            return True
        else:
            return False
        #endif
    else:
        self.debug(255, 'POP3 already connected')
        return True
    #endif

rawDescription: Get emails with Python and pop lib

description: Get emails with Python and pop lib


rawDescription: Manipulating csv files from long format using numpy or pandas


rawDescription: Convert dataFrame to list

description: Convert dataFrame to list


code: print dict(first_plus_second)

rawDescription: sum value of two different dictionaries which is having same key

description: Sum value of two different dictionaries which is having same key


code:
c = Clock()
print c
print "Starting..."; c.start_stop()
for i in range(4):
    time.sleep(2)
    print c
print "Stopping..."; c.start_stop()
for i in range(4):
    time.sleep(2)
    print c
print "Starting..."; c.start_stop()
for i in range(4):
    time.sleep(2)
    print c
print "Resetting..."; c.reset()
print c

rawDescription: How should a clock object be made in Python?


code:
from io import StringIO
import pandas as pd

data="tp,desc,L,L,D,D\n,,1,2,3,4\n3001, foo, 23.1, 35.3, 52.0, 11.9\n3010, bar, 31.l, 25.9, 13.9, 134.8"
df= pd.read_csv(StringIO(data.decode('UTF-8')),sep=',', index_col=[0,1])
print df

rawDescription: how to assign hierarchical column names in pandas dataframe

description: Assign hierarchical column names in pandas dataframe


code:
import sqlite3
import time
from numpy import genfromtxt

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

def Create_DB(db):
    #Create DB and format it as needed
    with sqlite3.connect(db) as conn:
        conn.row_factory = dict_factory
        conn.text_factory = str

        cursor = conn.cursor()

        cursor.execute("CREATE TABLE [Price_History] ([id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [date] DATE, [opn] FLOAT, [hi] FLOAT, [lo] FLOAT, [close] FLOAT, [vol] INTEGER);")

def Add_Record(db, data):
    #Insert record into table
    with sqlite3.connect(db) as conn:
        conn.row_factory = dict_factory
        conn.text_factory = str

        cursor = conn.cursor()

        cursor.execute("INSERT INTO Price_History({cols}) VALUES({vals});".format(cols = str(data.keys()).strip('[]'),
                    vals=str([data[i] for i in data]).strip('[]')
                    ))

def Load_Data(file_name):
    data = genfromtxt(file_name, delimiter=',', skiprows=1, converters={0: lambda s: str(s)})
    return data.tolist()

if __name__ == "__main__":
    t = time.time()

    db = 'csv_test_sql.db' #Database filename
    file_name = "t.csv" #sample CSV file used:  http://www.google.com/finance/historical?q=NYSE%3AT&ei=W4ikVam8LYWjmAGjhoHACw&output=csv

    data = Load_Data(file_name) #Get data from CSV

    Create_DB(db) #Create DB

    #For every record, format and insert to table
    for i in data:
        record = {
                'date' : i[0],
                'opn' : i[1],
                'hi' : i[2],
                'lo' : i[3],
                'close' : i[4],
                'vol' : i[5]
            }
        Add_Record(db, record)

    print "Time elapsed: " + str(time.time() - t) + " s." #3.604s

rawDescription: using sqlalchemy to load csv file into a database

description: Using sqlalchemy to load csv file into a database


code:
listone = [5,5,5,6,7,8,9]
listtwo = [4,2,3,4,5,6,7]
for x, y in zip(listone, listtwo):
    value = y/float(x)*100
    print "In a survey %.2f%% of dentists recommend this toothpaste" % value

rawDescription: Average out two list Python3

description: Average out two list Python3


code:
def function(beta):
    beta = beta + 'aei'
    print beta

rawDescription: Give a function an operation in python (JES)


code:
data_dict = {
    'extra': ['ex1', 'ex2', 'ex3', 'ex4'],
    'produktcode': ['ACE001', 'ACE002', 'ACE003', 'ACE004'],
    'typ': ['A', 'B', 'C', 'D'],
    'hotelname': ['foo01','foo02','foo03','foo04'],
    'kategorie': [3, 4, 5, 6],
    'size': ['big', 'small', 'medium', 'big']
}

heads = data_dict.keys()

insert_statement = 'insert into stamm_data (%s) values %s'

for t in zip(*(data_dict[head] for head in heads)):
    print cursor.mogrify(insert_statement, (
        AsIs(','.join(heads)), t
    ))

rawDescription: Passing a list of column names and values to Psycopg


code:
for pair in partition([1,2,3,4]):
    left, right = map(list, sorted(pair, key=len))
    print left, right

rawDescription: All ways of partitioning a list into two non-empty lists


code:
start_time = datetime.datetime.now()

# Start the transaction time
QtSql.QSqlDatabase.transaction()

query = QtSql.QSqlQuery("files.sqlite")
for id_bdd, percentage in zip(list_id, list_percentages):
    request = "UPDATE papers SET percentage_match = ? WHERE id = ?"
    params = (percentage, id_bdd)

    query.prepare(request)

    for value in params:
        query.addBindValue(value)

    query.exec_()

# commit changues
if QtSql.QSqlDatabase.commit():
   print "updates ok"

elsapsed_time = datetime.datetime.now() - start_time
print(elsapsed_time.total_seconds())

rawDescription: Improve sqlite query speed

description: Improve sqlite query speed


code:
import datetime
template = "{0:25}|{1:25}|{2!s:25}|{3:25}|{4:25}"
rows = (('DL2C1683', 'in', datetime.datetime(2013, 4, 28, 15, 53, 27), 'hyundai i10', '11'),
        ('UP1S1783', 'in', datetime.datetime(2013, 4, 28, 15, 53, 57), 'honda kinetic', '11'))
print template.format("Registration No.", "State", "Time", "Make", "Sector") # header
for row in rows:
    print template.format(*row)

rawDescription: datetime object in tuple not printing properly with template

description: Datetime object in tuple not printing properly with template


code:
min(stats.items(), key=lambda (k,v):float(v['load_1min']))
max(stats.items(), key=lambda (k,v):float(v['load_1min']))

rawDescription: find max , min of values in multidimensional dict

description: Find max , min of values in multidimensional dict


code:
def mul2x2(a, b, modulo):
    result = [[0, 0], [0, 0]]
    for i in xrange(2):
        for j in xrange(2):
            for k in xrange(2):
                result[i][j] += a[i][k] * b[k][j]
                result[i][j] %= modulo
    return result

def pow(m, n, modulo):
    result = [[1, 0], [0, 1]]
    while n:
        if n % 2: result = mul2x2(result, m, modulo)
        m = mul2x2(m, m, modulo)
        n //= 2
    return result

def subsets(n):
    m = pow([[0, 1], [1, 1]], n, 524287)
    return (m[0][0] + 2 * m[0][1]) % 524287

for i in xrange(1, 10):
    print i, subsets(i)
for i in xrange(1, 20):
    print i, subsets(10 ** i)

rawDescription: OverFlowError while solving "Number of subsets without consecutive numbers"


code:
import feedparser

url = feedparser.parse('http://ellywonderland.blogspot.com/feeds/posts/default?alt=rss')
for x in url.entries:
    print str(x.link)

rawDescription: Extract links for certain section only from blogspot using BeautifulSoup


rawDescription: Resource.getrusage() always returns 0

description: Resource.getrusage() always returns 0


code: print words

rawDescription: Store value of "if any(" search

description: Store value of "if any(" search


code: print  re.sub(r'\d', '8', string)

rawDescription: Substituting all numbers in a string with a different character in python


code:
#!/usr/bin/python
#function invest defines initial amount invested, interest, and over how many years
import sys

def invest(amount, rate, time):
    growth = (amount * rate)
    # range generates a list, thus using memory
    # xrange is a generator so it doesn't use all memory
    for i in xrange(1, time + 1):
        print 'Year {0}: {1}'.format(i, amount + (growth * i))

    return growth

def compound(amount, rate, time, additional=0):
    # range generates a list, thus using memory
    # xrange is a generator so it doesn't use all memory
    for i in xrange(1, time + 1):
        amount += additional
        amount = int(amount + (amount * rate))
        print 'Year {0}: {1}'.format(i, amount)

if __name__ == '__main__':
    args = sys.argv
    time = 5
    if len(args) > 1:
        amount = int(args[1])
        rate = float(args[2])
        time = int(args[3])
        additional = int(args[4])

        compound(amount, rate, time=time, additional=additional)
        #growth = invest(2000, .025, time=time)

rawDescription: Understanding for loops and functions

description: Understanding for loops and functions


code:
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')

import csv
for line in csv.reader(open("employees.csv", "rb")):
    emp = EmployeeRecord._make(line)
    print emp.name, emp.title

rawDescription: Python - Import file into NamedTuple

description: Python - Import file into NamedTuple


code:
import numpy as np
from scipy.stats import rankdata

def get_order_array(a):
    b = np.empty(a.shape, dtype=int)
    for k, row in enumerate(a):
        b[k] = rankdata(-row, method='dense') - 1
    return b

if __name__ == "__main__":
    a = np.array([[9,0,1,0,3,0],
                  [0,1,2,3,4,5],
                  [0.01,0.3,2,100,1,1],
                  [0,0,0,0,1,1],
                  [4,4,4,4,4,4]])
    print get_order_array(a)

rawDescription: Convert numpy.array to order of elements when duplicate values are present


code:
import csv
import datetime
import itertools

with open("file.csv", "rb") as quotes:
    csvReader = csv.DictReader(quotes)

    lmb = lambda d: datetime.datetime.strptime(d["Date"], "%d-%b-%Y").date()
    for k, g in itertools.groupby(csvReader, key = lmb):
        # do stuff per day
        counts = (int(data["Count"]) for data in g)
        print "On {0} the total count was {1}".format(k, sum(counts))

rawDescription: Dealing with subsets of data using csv.Dictreader


code:
decompose = lambda number: divmod(number, m)

class Phi_m(list):
    def __init__(self, items):
        list.__init__(self)
        # you need to know at least m numbers.
        assert len(items) >= m, 'Not enough data'
        list.extend(self, items)
    # this is a sparse list
    # http://stackoverflow.com/questions/1857780/sparse-assignment-list-in-python
    def __setitem__(self, index, value):
        missing = index - len(self) + 1
        if missing > 0:
            self.extend([None] * missing)
            list.__setitem__(self, index, value)
    def __getitem__(self, index):
        try:
            value = list.__getitem__(self, index)
            if value is not None:
                # the item is in the list, yeah!
                return value
            # the item is in the list because it was resized
            # but it is None, so go on and calculate it.
        except IndexError:
            # the item is not in the list, calculate it.
            pass
        print 'calculating Fm[%s]' % index
        A, B = decompose(index)
        value1 = self.__getitem__(A)
        value2 = self.__getitem__(A + 1)
        print 'Fm[A=%s] = %s, Fm[A+1=%s] = %s' % (A, value1, A+1, value2)
        print 'back to calculating Fm[%s]' % index
        # m * x[n1] + r * (x[n1 + 1] - x[n1]) = (m - r) * x[n1] + r * x[n1 + 1]
        # A = n1 ; B = r ; value1 = x[n1] ; value2 = x[n+1]
        value = (m - B) * value1 + B * value2
        self.__setitem__(index, value)
        return value

x = Phi_m([0, 1, 1])

x[5]

rawDescription: Decomposition function

description: Decomposition function


code:
from toolz.itertoolz import sliding_window, iterate, map, zip
from itertools import product
from numpy import uint8

def inc(x):
    return x + uint8(1)

# Create dictionary mapping from codon to integer
mapping = dict(zip(product('ATCG', repeat=3), iterate(inc, uint8(0))))

seq = sc.parallelize(["ATCGATGCATGCATGC"])

(seq
  # Generate pairs (start-position, 3-gram)
  .flatMap(lambda s: zip(iterate(inc, 0), sliding_window(3, s)))
  # Map 3-grams to respective integers
  .map(lambda (pos, seq): (pos, mapping.get(seq)))
  .collect())

description: Conditionally Combining/Reducing key-pairs


rawDescription: Calculate mean of elements in array with same "identifier"


code:
#Python 2.6
from selenium import selenium
import unittest, time, re, csv, logging

class Untitled(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://www.SourceDomain.com")
        self.selenium.start()

    def test_untitled(self):
        sel = self.selenium
        spamReader = csv.reader(open('your_file.csv'))
        for row in spamReader:
            sel.open(row[0])
            sel.wait_for_page_to_load("30000")
            print sel.get_html_source()

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

rawDescription: How to apply Loop to working Python Selenium Script?

description: Apply Loop to working Python Selenium Script


code:
m = max(sortDict.values())
print [v for v in sortDict.values() if len(v) == m]

rawDescription: Find all values with maximum length in a dictionary


code:
import random

newgame = True
choosing = False

playerchoice = ''
computerchoice = ''

options = ['rock', 'scissors', 'paper', 'lizard', 'spock']

def play():
    global choosing, computerchoice, computerchoice, newgame
    choosing = True

    while choosing == True: # or while choosing
        choose()

    result = get_result(playerchoice, computerchoice)

    if result[0] == 'Draw':
        print "It's a draw, want to try again?", "Please type YES or NO:"

        # convert input into uppercase string, use dictionary to convert to boolean
        # default to False
        newgame = {'YES' : True, 'NO' : False}.get(str(input()).upper(), False)
        return # leave function back into 'while newgame' loop
    else:
        # fun code that converts the first value into the win/lose string, and combines it with the outcome of the round
        # join uses the string ', ' in between the two things we print
        values = [{True : 'You win!', False : 'You lose!'}[result[0]], result[1]]
        string = ', '.join(values)
        print string

def choose():
    global playerchoice, choosing, computerchoice

    print "rock, paper, scissors, lizard, spock. What do you choose?"
    playerchoice = input()

    if playerchoice in options: # if playerchoice == any option
        print 'you chose', playerchoice

        computerchoice = options[random.randint(0,4)] # take advantage of list indexes
        print 'computer chose', computerchoice

        choosing = False
    else:
        print "this is not an option, please try again"

def get_result(playerchoice, computerchoice):
    results = {
               'rock' : {
                         'rock' : ('Draw',),
                         'paper' : (False, 'paper covers rock!'),
                         'scissors' : (True, 'rock crushes scissors!'),
                         'lizard' : (True, 'rock crushes lizard!'),
                         'spock' : (False, 'spock vaporizes rock!')
                        },
               'paper' : {
                          'rock' : (True, 'paper covers rock!'),
                          'paper' : ('Draw',),
                          'scissors' : (False, 'scissors cut paper!'),
                          'lizard' : (False, 'lizard eats paper!'),
                          'spock' : (True, 'paper disproves spock!')
                         },
               'scissors' : {
                             'rock' : (False, 'rock crushes scissors!'),
                             'paper' : (True, 'scissors cut paper!'),
                             'scissors' : ('Draw',),
                             'lizard' : (True, 'scissors kills lizard!'),
                             'spock' : (False, 'spock smashes scissors!')
                            },
               'lizard' : {
                           'rock' : (False, 'rock crushes lizard!'),
                           'paper' : (True, 'lizard eats paper!'),
                           'scissors' : (False, 'scissors kills lizard!'),
                           'lizard' : ('Draw',),
                           'spock' : (True, 'lizard poisons spock!')
                          },
               'spock' : {
                          'rock' : (True, 'spock vaporizes rock!'),
                          'paper' : (False, 'paper disproves spock!'),
                          'scissors' : (True, 'spock smashes scissors!'),
                          'lizard' : (False, 'lizard poisons spock!'),
                          'spock' : ('Draw',)
                         },
               }
    return results[playerchoice][computerchoice]

while newgame == True:
    play()

rawDescription: Rock, Paper, Scissors - How to start a new game when there's a draw


code:
class Animal:
    __hungry = "yes"
    __name = "no name"
    __owner = "no owner"

    def __init__(self):
        pass

    def set_owner(self,newOwner):
        self.__owner= newOwner
        return

    def get_owner(self):
        return self.__owner

    def set_name(self,newName):
        self.__name= newName
        return

    def get_name(self):
        return self.__name

    def noise(self):
        print('errr')
        return

    def __hiddenmethod(self):
        print("hard to find")

def main():
    dog = Animal()
    dog.set_owner('Sue')
    print dog.get_owner()
    dog.noise()

if  __name__ =='__main__':
    main()

rawDescription: main method in Python

description: Main method in Python


code:
from bs4 import BeautifulSoup

html_doc = """
Total Cash (mrq):
8.71B
"""

soup = BeautifulSoup(html_doc, 'html.parser')

td_head = soup.find(text="Total Cash (mrq):").parent
td_desired = td_head.find_next('td')

print td_desired.contents[0]

rawDescription: parsing with expression including a newline in python

description: Parsing with expression including a newline in python


code:
class Foo(object):
    def __init__(self, name):
        self.name = name

    def __setattr__(self, attrname, value):
        print "Intercepted: " + attrname + " attribute of Foo set to " + value
        super(Foo, self).__setattr__(attrname, value)

rawDescription: Protecting a mutable object from changing in Python


code:
import pulp

# Create variables and model
x = pulp.LpVariable.dicts("x", df.index, lowBound=0)
mod = pulp.LpProblem("Budget", pulp.LpMaximize)

# Objective function
objvals = {idx: (1.0/(df['30-day Cost'][idx]/df['Trials'][idx]))*(df['Success'][idx]/float(df['Trials'][idx])) for idx in df.index}
mod += sum([x[idx]*objvals[idx] for idx in df.index])

# Lower and upper bounds:
for idx in df.index:
    mod += x[idx] >= df['Cost Min'][idx]
    mod += x[idx] <= df['Cost Max'][idx]

# Budget sum
mod += sum([x[idx] for idx in df.index]) == 5000.0

# Solve model
mod.solve()

# Output solution
for idx in df.index:
    print idx, x[idx].value()
# 0 2570.0
# 1 1350.0
# 2 1080.0

print 'Objective', pulp.value(mod.objective)
# Objective 1798.70495012

rawDescription: Linear Programming (Simplex LP) PuLP?

description: Linear Programming (Simplex LP) PuLP


code:
def fixImgurLinks(url):
    if url.lower().startswith("http://imgur.com"):
        url = url.replace("http://imgur", "http://i.imgur",1) # Only replace the first instance.
        if not url.endswith(".jpg"):
            url +=".jpg"
    return url

for u in ["http://imgur.com/Cuv9oau","http://www.google.com/search?q=http://imgur"]:
    print fixImgurLinks(u)

rawDescription: how do I modify a url that I pick at random in python

description: Modify a url that I pick at random in python


code:
s='''<?xml version="1.0" encoding="UTF-8"?>
<drum>
  <cmts>
    <Pre_EQ>
      <success field_name="success">1</success>
      <coefficient field_name="coefficient">080118000000ffd00000ffe00000fff0001000200020fff000000010002000003fa0000000000020003000000020ffd00010001000000000000000000010000000000020ffc0000000000020000000500010ffe0fff0ffe0ffd0fff0fff0ffd0ffd0fff0</coefficient>
    </Pre_EQ>
  </cmts>
</drum>'''
tree = ET.fromstring(s)
for elem in tree.findall('.//Pre_EQ/success'):
    print elem.tag, elem.attrib, elem.text

for elem in tree.findall('.//success'):
    if elem.attrib.get('field_name') == "success":
        print elem.tag, elem.attrib, elem.text

rawDescription: What is python 2.6 equivalent of iterfind()


code:
def _one_child_number(s, child_count, digits_count):
    print s, child_count
    if s >= 10**(digits_count-1): # if the length of s is digits_count
        return child_count # child_count is 0 or 1 here, 1 means we found one one-child-number.
    else:
        if s == 0:
            start = 1 #if the length of s is 0, we choose from 123456789 for the most left digit.
        else:
            start = 0 #otherwise we choose from 0123456789
        count = 0 # init the one-child-number count
        for c in range(start, 10): # loop for every digit
            s2 = s*10 + c  # add digit c to the right of s

            # following code calculates the child count of s2
            child_count2 = child_count
            i = 10
            while True:
                part = s2 % i
                if part % digits_count == 0:
                    child_count2 += 1
                    if child_count2 > 1: # when child count > 1, it's not a one-child-number, break
                        break
                if part == s2:
                    break
                i *= 10

            # if the child count by far is less than or equal 1,
            # call _one_child_number recursively to add next digit.
            if child_count2 <= 1:
                count += _one_child_number(s2, child_count2, digits_count)
        return count

rawDescription: Optimizing Brute Force process in python / pypy for finding One-Child numbers


rawDescription: Slicing pandas groupby groups into equal lengths


code:
import re
p = re.compile(r'ORIG\s?:\s?/\s?([A-Z0-9]+)', re.IGNORECASE)
test_str = "ORIG:/texthere"
print re.search(p, test_str).group(1)

rawDescription: Alternatives to variable-width lookbehind in Python regex


code:
import csv
s = csv.Sniffer()
print s.sniff("a,b,c").delimiter
print s.sniff("a;b;c").delimiter
print s.sniff("a#b#c").delimiter

rawDescription: Pandas guess delimiter with sep=None

description: Pandas guess delimiter with sep=None


code:
myset = set([3,4,5,6,2])
while myset:
    myset.pop()
    print myset

rawDescription: delete items from a set while iterating over it

description: Delete items from a set while iterating over it


code:
import re
samples = ['tt-5d10h', 'tt-5d10h30m', 'tt-5d30m', 'tt-10h30m', 'tt-5d', 'tt-10h', 'tt-30m',]

def parse(text):
    match = re.match('tt-(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?', text)
    values = [int(x) for x in match.groups(0)]
    return values

for sample in samples:
    print parse(sample)

rawDescription: Extracting tokens where some are optional


code:
import BeautifulSoup
txt = '''<description><![CDATA[Voice of America is an international news and broadcast organization serving Central and Eastern Europe, the Caucasus, Central Asia, Russia, the Middle East and Balkan countries]]></description>'''
soup = BeautifulSoup.BeautifulSoup(txt)
for cd in soup.findAll(text=True):
  if isinstance(cd, BeautifulSoup.CData):
    print 'CData value: %r' % cd

rawDescription: how to get content inside CDATA of XML tag django

description: Get content inside CDATA of XML tag django


code:
pid = getpid()

f = open('./trashme.txt', 'w')
f.write('This is a test\n')

lsof = (check_output(['/usr/sbin/lsof', '-p', str(pid)], stderr=STDOUT)).split("\n")
print lsof[0]
for line in lsof[1:]:
    if (re.search('trashme', line)): print line

f.close

rawDescription: How to get the path to the opened file from a script inside a OS X .app bundle?

description: Get the path to the opened file from a script inside a OS X .app bundle


rawDescription: How to check whether two lists are circularly identical in Python

description: Check whether two lists are circularly identical in Python


code:
import re

pattern = re.compile(r"\[\[([\w ]+)(\|[\w ]+)?\]\]")
text = "[[a|b]] fdkjf [[c|d]] fjdsj [[efg]]"
results = pattern.findall(text)

# outputs [('a', '|b'), ('c', '|d'), ('efg', '')]

print [link[0] for link in results]

# outputs ['a', 'c', 'efg']

rawDescription: Python regex for finding contents of MediaWiki markup links


code:
alldiv = soup.findAll('div', { "class" : "content_page" })
for div in alldiv: print div.a

for div in alldiv: print div.a['href']

rawDescription: How to retrieve these elements from a webpage?

description: Retrieve these elements from a webpage


code:
import re

in_dict = {
    '1': 'active_rate',
    '2': 'purchase_per_active'
}

formula = {
    'total_purchase': 'B1*B2'
}

def lookup(number):
    return in_dict[number.group(1)]

for key, value in formula.items():
    formula[key] = re.sub("[a-zA-Z](\d+)", lookup, value)

print formula

rawDescription: Search and replace characters in string from the list


code:
from os.path import join

def square(x):
    return x*x

def cube(x):
    return x**3

l = []
for key, value in locals().items():
    if callable(value) and value.__module__ == __name__:
        l.append(key)
print l

rawDescription: Getting a list of locally-defined functions in python


code:
msg = email.message_from_string(msgtxt)
print msg['Subject']

rawDescription: Parsing email with Python

description: Parsing email with Python


code:
version_action = next((action for action in parser._get_optional_actions()
                       if isinstance(action, argparse._VersionAction)), None)
print version_action.version if version_action else 'unknown'

rawDescription: Get version string from argparse

description: Get version string from argparse


code: print listmap

rawDescription: Find product of any subset of elements from a list


code:
subject_dic = {}
inputFile = open(filename)

# Turn "line1\nline2\n" into ['line1', 'line2']
inputData = inputFile.read().splitlines()

#for line in inputFile:
for line in inputData:
    #split_line = string.split(line, ',')
    #subject_dic[split_line[0]] = tuple(split_line[1:3])
    mykey, myval1, myval2 = line.split(',') # Strings always have .split()
    subject_dic[mykey] = (myval1, myval2) # Explicit tuple assignment

print subject_dic

rawDescription: trying to create a dictionary but do not know how to deal with \n

description: Trying to create a dictionary but do not know how to deal with \n


code:
class StdOutListener(StreamListener):
    def __init__(self, api=None):
        super(StdOutListener, self).__init__()
        self.num_tweets = 0

    def on_data(self, data):
    #first check the location is not None
        if status.author.location and 'New York' in status.author.location:
            self.num_tweets += 1
            print data
        if self.num_tweets < 5: #Remove the limit of no. of tweets to 5
            return True
        else:
            return False
    def on_error(self, status):
        print status

rawDescription: Scrape tweets by tweet location and user location


code:
one = [(1, 2), (3, 4)]
for o in one:
    print o,      #added the ,
print      #and an optional print() to ensure that anything afterwords prints on a separate line

rawDescription: Python print a list of two element in one line in a loop


code:
import numpy as np

train_labels = train_dataframe.class_label
labels = list(set(train_labels))
train_labels = np.array([labels.index(x) for x in train_labels])
train_features = train_dataframe.iloc[:,1:]
train_features = np.array(train_features)

print "train labels: "
print train_labels
print
print "train features:"
print train_features

rawDescription: Support vector machine in Python using libsvm example of features


code:
import re
import json

from bs4 import BeautifulSoup
import requests

response = requests.get("https://www.kaggle.com/titericz")
soup = BeautifulSoup(response.content, "html.parser")

pattern = re.compile(r"profile: ({.*}),", re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)

profile_text = pattern.search(script.text).group(1)
profile = json.loads(profile_text)

print profile["ranking"], profile["rankingText"]

rawDescription: Python:Getting text from html using Beatifulsoup


code:
import csv
with open('some.csv') as fin:
    for row in csv.reader(fin):
        print int(row[0])

rawDescription: Effective way to get part of string until token


code:
import urllib2
req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',
                      data='This data is passed to stdin of the CGI')
f = urllib2.urlopen(req)
print f.read()

rawDescription: Python send https data to website

description: Python send https data to website


code:
load_profile = open('users/file.txt', "r")
read_it = load_profile.read()
myLine = ""
for line in read_it.splitlines():
    if line == "This is the line I am looking for":
        myLine = line
        break
print myLine

rawDescription: How do you read a specific line of a text file in Python?

description: Read a specific line of a text file in Python


code:
print 'inside sub.py',
print '-'*50
print 'importing main.py'
import main
print 'imported main.py'
import logging
print 'getting logger instance in sub'
sub_logger = main.logger()
print 'got logger instance in sub'
sub_logger.info("utilizing sub_logger")
print 'exiting sub.py',
print '-'*50

rawDescription: python logging ensure a handler is added only once

description: Python logging ensure a handler is added only once


code:
currency_values = {
    'twenties' : 20,
    'tens' : 10,
    'fives' : 5,
    'ones' : 1,
    'quarters' : 0.25,
    'dimes' : 0.10,
    'nickels' : 0.05,
    'pennies' : 0.01,
}

currency_order = ['twenties','tens','fives','ones','quarters','dimes','nickels','pennies']

def user_change(balance):
    user_change_results = []
    for currency in currency_order:
        #print balance
        if balance == 0:
            break
        currency_amount = currency_values[currency]
        currency_change_amount = balance//currency_amount
        user_change_results.append((currency,currency_change_amount))
        balance-=(currency_change_amount*currency_amount)

    return user_change_results

if __name__ == '__main__':
    print user_change(34.36)

rawDescription: Can this be done by using a loop? Or maybe by using lists?


code: print lemma.name()

rawDescription: transforming wordnet txt into lists in python nltk

description: Transforming wordnet txt into lists in python nltk


code:
import sys
with open(sys.argv[1], 'r') as f:
    contents = f.read()
print contents

rawDescription: I want to read in a file from the command line in python


code:
global_array1 = []
global_array2 = []

def myfunc(aResult, anArray):
  global global_array1
  global global_array2

  for a in anArray:
      if a:
          result = global_array1
      else:
          result = global_array2

      # do something hhere
      result.append(aResult)

anArray = [True, True, False]

myfunc(1, anArray)
myfunc(2, anArray)
myfunc(3, anArray)

print 'global_array1:', global_array1
print 'global_array2:', global_array2

rawDescription: How can I recreate a pointer to a global array in python

description: Recreate a pointer to a global array in python


rawDescription: Replacing multiple occurences of a character in a string with python


code:
import sys

# If the Current Working directory is skeleton
# C:\Users\kumarvivek\Desktop\skeleton>python C:\Users\kumarvivek\Desktop\skeleton\tests\ex47_tests.py
#
# sys.path.append(r"..\skeleton")

# If the Current Working directory is any of these "tests" or "ex47"
# C:\Users\kumarvivek\Desktop\skeleton\tests>python C:\Users\kumarvivek\Desktop\skeleton\tests\ex47_tests.py
# C:\Users\kumarvivek\Desktop\skeleton\ex47>
#
# sys.path.append(r"..\..\skeleton")

sys.path.append(r"..\..\skeleton")

from ex47 import mod

print mod.x , mod.directoryPath, mod.fileName

rawDescription: Appending Pythonpath, importing modules from different directories on windows


code:
def func1(n_samples, noise, factor, random_state):
    print locals()

def func2(n_samples, ratio, noise):
    print locals()

def func3(my_key, default_key='world!'):
    print locals()

funclist = [func1, func2, func3]
paramlist = [
        dict(
            n_samples=1000,
            noise=0.3,
            factor=0.5,
            random_state=1
        ),
        dict(
            n_samples=1000,
            ratio=0.5,
            noise=0.1
        ),
        dict(
            my_key='hello',
        )
]

for index, func in enumerate(funclist):
    func(**paramlist[index])

rawDescription: Python function pointer with different argument size


code:
print ssh.data_content('etc/init.d/ssh')
u'#! /bin/sh\n\n### BEGIN INIT INFO\n#......'

rawDescription: Get a List of conffiles in a deb package in python


code:
import pymel.core.uitypes as pmui
print pmui.ModelEditor().getRendererList()

rawDescription: How to query current viewport renderer in maya

description: Query current viewport renderer in maya


code:
import os
L = [
"/path1/path2/path3/example_1.edf",
"/path1/path2/path3/example_1.v",
"/path1/path2/path3/exa_4mple_1.sv",
"/path1/path2/path3/example_1.vh" ]
for mypath in L:
    if mypath.split('.')[-1] in ('v', 'sv', 'vh'):
            print os.path.split(mypath)[1]

rawDescription: chaining regular expressing in python

description: Chaining regular expressing in python


code:
#!/usr/bin/python
import re
reg4 = re.compile(r'''
        ^                     # Start of a line (due to re.MULTILINE, this may match at the start of any line)
        (?:                   # Non capturing group with multiple options, first option:
            PMID-\s           # Literal "PMID-" followed by a space
            (?P<pmid>[0-9]+)  # Then a string of one or more digits, group as 'pmid'
        |                     # Next option:
            TI\s{2}-\s        # "TI", two spaces, a hyphen and a space
            (?P<title>.*?)    # The title, a non greedy match that will capture everything up to...
            ^PG               # The characters PG at the start of a line
        |                     # Next option
            AB\s{2}-\s        # "AB  - "
            (?P<abstract>.*?) # The abstract, a non greedy match that will capture everything up to...
            ^AD               # "AD" at the start of a line
        )
        ''', re.MULTILINE | re.DOTALL | re.VERBOSE)
for i in reg4.finditer(data):
    print i.groupdict()

rawDescription: Match multiple patterns in a multiline string


code: print sum(d.values())

rawDescription: How to add all the elements inside loop in Python?

description: Add all the elements inside loop in Python


code:
class SymNDArray(numpy.ndarray):
    def __setitem__(self, (i, j), value):
        super(SymNDArray, self).__setitem__((i, j), value)
        super(SymNDArray, self).__setitem__((j, i), value)

def symarray(input_array):
    """
    Returns a symmetrized version of the array-like input_array.
    Further assignments to the array are automatically symmetrized.
    """
    return symmetrize(numpy.asarray(input_array)).view(SymNDArray)

# Example:
a = symarray(numpy.zeros((3, 3)))
a[0, 1] = 42
print a  # a[1, 0] == 42 too!

rawDescription: Numpy ‘smart’ symmetric matrix

description: Numpy ‘smart’ symmetric matrix


code:
from functools import partial
from sys import maxint

def get_friends_followers_ids(twitter_api, screen_name=None, user_id=None,
friends_limit=maxint, followers_limit=maxint):
    # Must have either screen_name or user_id (logical xor)
    assert (screen_name != None) != (user_id != None),\
    "Must have screen_name or user_id, but not both"
    # See https://dev.twitter.com/docs/api/1.1/get/friends/ids and
    # https://dev.twitter.com/docs/api/1.1/get/followers/ids for details
    # on API parameters
    get_friends_ids = partial(make_twitter_request, twitter_api.friends.ids,
                             count=5000)
    get_followers_ids = partial(make_twitter_request,twitter_api.followers.ids,
                               count=5000)
    friends_ids, followers_ids = [], []
    for twitter_api_func, limit, ids, label in [
                    [get_friends_ids, friends_limit, friends_ids, "friends"],
                    [get_followers_ids, followers_limit, followers_ids, "followers"]
                ]:
        if limit == 0: continue
        cursor = -1
        while cursor != 0:

            # Use make_twitter_request via the partially bound callable...
            if screen_name:
                response = twitter_api_func(screen_name=screen_name, cursor=cursor)
            else: # user_id
                response = twitter_api_func(user_id=user_id, cursor=cursor)
            if response is not None:
                ids += response['ids']
                cursor = response['next_cursor']
            print >> sys.stderr, 'Fetched {0} total {1} ids for{2}'.format(len(ids),
                                                    label, (user_id or screen_name))
            # XXX: You may want to store data during each iteration to provide
            # an additional layer of protection from exceptional circumstances
            if len(ids) >= limit or response is None:
                break
    # Do something useful with the IDs, like store them to disk...
    return friends_ids[:friends_limit], followers_ids[:followers_limit]

# Sample usage
twitter_api = oauth_login()
friends_ids, followers_ids =get_friends_followers_ids(twitter_api,
                                                      screen_name="SocialWebMining",
                                                      friends_limit=10,
                                                      followers_limit=10)
print friends_ids
print followers_ids

rawDescription: How to get follower count using tweepy

description: Get follower count using tweepy


code:
import dns
import dns.name
import dns.query
import dns.resolver

def get_authoritative_nameserver(domain, log=lambda msg: None):
    n = dns.name.from_text(domain)

    depth = 2
    default = dns.resolver.get_default_resolver()
    nameserver = default.nameservers[0]

    last = False
    while not last:
        s = n.split(depth)

        last = s[0].to_unicode() == u'@'
        sub = s[1]

        log('Looking up %s on %s' % (sub, nameserver))
        query = dns.message.make_query(sub, dns.rdatatype.NS)
        response = dns.query.udp(query, nameserver)

        rcode = response.rcode()
        if rcode != dns.rcode.NOERROR:
            if rcode == dns.rcode.NXDOMAIN:
                raise Exception('%s does not exist.' % sub)
            else:
                raise Exception('Error %s' % dns.rcode.to_text(rcode))

        rrset = None
        if len(response.authority) > 0:
            rrset = response.authority[0]
        else:
            rrset = response.answer[0]

        rr = rrset[0]
        if rr.rdtype == dns.rdatatype.SOA:
            log('Same server is authoritative for %s' % sub)
        else:
            authority = rr.target
            log('%s is authoritative for %s' % (authority, sub))
            nameserver = default.query(authority).rrset[0].to_text()

        depth += 1

    return nameserver

import sys

def log(msg):
    print msg

print get_authoritative_nameserver(sys.argv[1], log)

rawDescription: How can I find the authoritative DNS server for a domain using dnspython?

description: Find the authoritative DNS server for a domain using dnspython


code:
def do_this():
    print url
    text_file = open("Output.txt", "a")
    text_file.write("Purchase Amount: %s\n" % url)

rawDescription: How To Print Variable To A Text File In Python?


code:
import gobject
import dbus
import dbus.service

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

OPATH = "/com/example/StopLoop"
IFACE = "com.example.StopLoop"
BUS_NAME = "com.example.StopLoop"
TIMEOUT = 30 * 1000

class Example(dbus.service.Object):
    def __init__(self, loop):
        self.loop = loop
        bus = dbus.SessionBus()
        bus.request_name(BUS_NAME)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)
        # Add a timeout for how long to run mainloop
        # if no signal is received
        self.setup_timeout(TIMEOUT)
        # Listen to the "Stop" signal
        self.listen_for_signal(bus)

    def setup_timeout(self, timeout):
        gobject.timeout_add(timeout, self.handler)

    def listen_for_signal(self, bus):
        bus.add_signal_receiver(self.handler, "Stop")

    def handler(self):
        # This handler is used for both timeout and signal
        self.loop.quit()

if __name__ == "__main__":
    loop = gobject.MainLoop()
    a = Example(loop)
    loop.run()
    print "Exited mainloop, continuing program..."

rawDescription: How to stop a dbus gobject loop

description: Stop a dbus gobject loop


code:
def get_widget_value(widget):
    if isinstance(widget,wx.TextCtrl):
       return widget.GetLabel()
    if hasattr(widget,"GetValue"):
       return widget.GetValue()
    raise Exception,"Unknown Widget {0}".format(widget)

def set_widget_value(widget,value):
    if isinstance(widget,wx.TextCtrl):
       return widget.SetLabel(value)
    if hasattr(widget,"SetValue"):
       return widget.SetValue(value)
    raise Exception,"Unknown Widget {0}".format(widget)

rawDescription: WxPython Panel Refresh

description: WxPython Panel Refresh


rawDescription: Creating a diff array using lambda functions in python


code:
from Tkinter import Tk, Button

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.machines_off = []
        a = Button(self, text="do the first thing", command=self.run_command)
        b = Button(self, text="do the second thing", command=self.wol_machines)
        a.pack()
        b.pack()
    def run_command(self):
        #reset machines_off to the empty list.
        #delete this next line if you want to retain old values.
        self.machines_off = []
        # Some stuff .....
        machineName = "foo"
        self.machines_off.append(machineName)
        # Some stuff ....

    def wol_machines(self):
        print "wol_machines was called"
        print "contents of machines_off: ", self.machines_off
        # Some stuff ....

root = App()
root.mainloop()

rawDescription: Sharing list between functions

description: Sharing list between functions


code:
for line in data:
    print line[2] #prints the values in the third column

rawDescription: Read file into dictionary and manipulate data


code:
import enchant
print enchant.list_languages()

rawDescription: PyEnchant without German dictionary

description: PyEnchant without German dictionary


code:
a = (1, 2, 3, 4, 5, 6)
print "z\tx\ty"  # Print top line separated by tab characters
for i in a:  # For each element in the list
    if i % 2 == 0:  # If the element is even
        print i, "\t", i
    else:  # If it is odd
        print i, "\t\t", i

rawDescription: Print values from several array using Python


code:
for item in l:
  print item[0], ', '.join(map(str, item[1:]))

rawDescription: Printing a list of lists, without brackets


code:
import csv

with open(fn) as f:
    reader=csv.reader(f, delimiter='\t')
    a, b, c = zip(*reader)

    print a
    ('Classification Type', 'Commercial Homes', 'Residential Homes')
    print b
    ('A', '12', '10')
    print c
    ('B', '15', '14')
    # trim the tuples as you wish, such as b=list(b[1:])...

rawDescription: Read columns into separate lists

description: Read columns into separate lists


code:
from BeautifulSoup import BeautifulSoup

# Note: I have added the <movielist> root element
xml = """<movielist>
<movie id = 0>
  <Movie_name>The Shawshank Redemption   </Movie_name>
  <Address>http://www.imdb.com/title/tt0111161/
  </Address>
  <year>1994  </year>
  <stars>Tim Robbins  Morgan Freeman  Bob Gunton    </stars>
  <plot> plot...
  </plot>
  <keywords>Reviews, Showtimes</keywords>
</movieNum>

<movie id = 1>
  <Movie_name>Inglourious Basterds   </Movie_name>
  <Address>http://www.imdb.com/title/tt0361748/
  </Address>
  <year>2009  </year>
  <stars>Brad Pitt  Mélanie Laurent  Christoph Waltz    </stars>
  <plot>plot/...
  </plot>
  <keywords>Reviews, credits  </keywords>
</movieNum>

</movielist>"""

soup = BeautifulSoup(xml)
movies = soup.findAll('movie')

for movie in movies:
    id_tag = movie['id']
    name = movie.find("movie_name").text
    url = movie.find("address").text
    year = movie.find("year").text
    stars = movie.find("stars").text
    plot = movie.find("plot").text
    keywords = movie.find("keywords").text
    for item in (id_tag, name, url, year, stars, plot, keywords):
        print item
    print '=' * 50

rawDescription: Iterating an XML file and extracting data from it


code:
def MenuClick(self, event):
    def _menuItemSearch(menu,subMenuTree ,id):
        if not menu.FindItemById(id): return False
        # it is in this menu
        for menuItem in menu.MenuItems:
            if menuItem.GetId() == id:
                subMenuTree.append(menuItem.GetLabel())
                return True
            if menuItem.GetSubMenu():
                if _menuItemSearch(menuItem.GetSubMenu(),subMenuTree,id):
                    subMenuTree.append(menuItem.GetLabel())
                    return True
                return False
    subMenuTree = []
    for menu,name in self.GetMenuBar().GetMenus():
        _menuItemSearch(menu,subMenuTree,event.Id)
    print subMenuTree

[u'1321', u'132', u'13']

rawDescription: Figure out nested submenu selections in wxPython?


code:
def get_num(msg):
    while True:
        try:
            return int(raw_input(msg)) # number of rows should be int
        except ValueError:
            print "The value you entered isn't a valid number, please try again."

num_rows = get_num("Enter number of rows: ")
print num_rows

rawDescription: Changing global variables within a function


code:
for start, end in customFilter(s):
    print 1
    mask = np.zeros_like(s)
    mask[start: end] = 1
    ax.fill_between(x, ymin, ymax, where=mask, facecolor='green', alpha=0.5)

rawDescription: matplotlib how to fill_between step function

description: Matplotlib how to fill_between step function


code:
import theano as the
import theano.tensor as ten
import numpy as np
matrix_embedding = the.shared(np.asarray([[0.1, 0.2, 0.3],
                                          [0.4, 0.5, 0.6],
                                          [0.2, 0.3, 0.5],
                                          [0.5, 0.6, 0.7]]))
matrix_padding_embedding = ten.concatenate((matrix_embedding, ten.zeros((1, 3))))

x = ten.lvector()
y = ten.sum(matrix_padding_embedding[x])
grad = the.grad(y, matrix_embedding)
fn = the.function([x], [matrix_padding_embedding, grad])

x2 = [1, 2, -1]
p, g = fn(x2)
print p
print g

rawDescription: How to keep the weight value to zero in a particular location using theano or lasagne?

description: Keep the weight value to zero in a particular location using theano or lasagne


code:
lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for o in cycle_first_n(lst, 3):
    print o,

rawDescription: python how to efficiently cycle through few elements in a list

description: Python how to efficiently cycle through few elements in a list


code:
word = 'steam'
for i in range(len(word)):
    print word[i:i+2]

rawDescription: Create a dictionary of all letter transitions from a previous dictionary of words?


code:
import re

def parse_line(line):
    """ line -> (int, int, int), None if invalid
    """
    if not line.startswith('Xac:'):
        return None
    parts = re.split('[,Xac:YZ#?*]', line)
    parts = filter(lambda x: bool(x), parts)
    parts = map(lambda x: float(x), parts)
    return tuple(parts)

output = []
with open('input.txt') as f:
    for line in iter(f.readline, ''):
        axes = parse_line(line.strip())
        if axes:
            output.append(axes)
print output

rawDescription: Delete unusable characters or data in archive with python


code:
for line in unwrap_data("input_file.dat"):
    print line[0], min(line[1:]), max(line[1:])

rawDescription: Python-Cannot convert string to float...manipulating a text file


code:
from collections import Counter
a = Counter(0,1,2,1)
b = Counter(0,1,1)

print a - b  # ignores items in b missing in a

# check every element in a is in b
# a[key] returns 0 if key not in a, instead of raising an exception
assert all(a[key] > b[key] for key in b)

rawDescription: Subtracting two lists in Python

description: Subtracting two lists in Python


code:
import struct

type_map = {
        'string type': 's',
        'short int type': 'h',
        'long int type': 'q',
        'float': 'f'
        }

conversion = {
        's': str,
        'h': int,
        'q': int,
        'f': float
        }

def do_pack(types, data):
    if len(types) != len(data):
        raise Excpetion("wrong lengths")
    packing = '<'
    data_iter = []
    for i, struct_type in enumerate(types):
        t = type_map[struct_type]
        if t == 's':
            packing += '%ds' % len(data[i])
            data_iter.append(data[i])
        else:
            packing += t
            data_iter.append(conversion[t](data[i]))
    return struct.pack(packing, *data_iter), packing

packer = [['string type','short int type','long int type','string type','float'],['Stackoverflow','32','0','any stringgg','55.0'],['anystring','16','1654657987984','striiingg','2.5']]

types = packer[0]
for data_set in packer[1:]:
    binary, packing = do_pack(types, data_set)
    print struct.unpack(packing, binary)

rawDescription: Pack a list with string and int type use struct.pack python


code:
from timeit import Timer
a = [random.randint(0, 1000000) for _ in xrange(100000)]
##print a[:100]
def foo(a):
    a = sorted(a, reverse=True)
    return [a[i]-a[i+1] for i in xrange(len(a)-1)]

def bar(a):
    return np.diff(np.sort(a))

t = Timer('foo(a)', 'from __main__ import foo, bar, np, a')
print t.timeit(10)
# 0.86916993838

t = Timer('bar(a)', 'from __main__ import foo, bar, np, a')
print t.timeit(10)
# 0.28586356791

rawDescription: Creating a diff array using lambda functions in python


code:
def polarize(im):
    height = len(im)
    width = len(im[0])
    red = 0
    green = 0
    blue = 0
    pixels = float(width*height)
    for row in range(height):
        for col in range(width):
            red += im[row][col][0] #finds the sum of all red values
            green += im[row][col][1] #finds the sum of all green values
            blue += im[row][col][2] #finds the sum of all blue values
    print numpy.mean(im, axis=(0,1))
    print red/pixels, green/pixels, blue/pixels
    avg_value_red = red/pixels
    avg_value_green = green/pixels
    avg_value_blue = blue/pixels
    for row in range(height):
        for col in range(width):
            im[row][col][0] = 255 if im[row][col][0] >= avg_value_red else 0
            im[row][col][1] = 255 if im[row][col][1] >= avg_value_green else 0
            im[row][col][2] = 255 if im[row][col][2] >= avg_value_blue else 0
    return im

rawDescription: Polarizing image attempt--what's wrong?

description: Polarizing image attempt--what's wrong


code:
# gives you column labels, which are `True` if at least one entry has `date` in it
# have to check `kind` otherwise you get an error.
col_result = df.apply(lambda x: x.dtype.kind == "O" and (x == "date").any())

# select only columns where True (this should be one entry) and get their index (for the label)
column = col_result[col_result].index[0]
col_index = df.columns.get_loc(column)

# will be True if it contains date
row_selector = df.icol(col_index) == "date"

print df[row_selector].icol(col_index + 1).values

rawDescription: Python pandas an efficient way to see which column contains a value and use its coordinates as an offset


code:
import f2
myStorage = f2.useMyVars(0) # initialze class and properties
for i in range(0,10):
    print "Hello, "
    f2.print_world()
    myStorage.setMyVar(i)
    f2.inc_gMyVar()
print "Display class property myVar:", myStorage.getMyVar()
print "Display global variable gMyVar:", f2.get_gMyVar()

rawDescription: split python source code into multiple files?

description: Split python source code into multiple files


code:
input_file = open('filename.txt')
try:
    for i, line in enumerate(input_file):
        print line,
finally:
    input_file.close()
print "{0} line(s) printed".format(i+1)

rawDescription: python looping through input file

description: Python looping through input file


code:
class B:
    def __init__(self, master):
        """Some work here"""
    def do_that(self):
        return (1,2)

class A(B):
    def __init__(self, master):
        """Some work here"""
    def do_this(self):
        print self.do_that()[1]
mything = A('placeholder')
mything.do_this()

rawDescription: using a variable from a method of another class in python

description: Using a variable from a method of another class in python


code:
import multiprocessing
pool = multiprocessing.Pool()
print pool.map(f, range(10))

rawDescription: Is there a simple process-based parallel map for python?


code:
for line in soup.find_all('ol'):
    for item in line.find_all('li'):
        result = []
        for descendant in item.descendants:
            if isinstance(descendant, NavigableString):
                result.append(unicode(descendant).strip())
            elif descendant.name == 'dl':
                break

        print u' '.join(result)

rawDescription: Python/BeautifulSoup - How can I extract text between <li> and <dl> tags


code:
import numpy as np

def fread(fid, nelements, dtype):
     if dtype is np.str:
         dt = np.uint8  # WARNING: assuming 8-bit ASCII for np.str!
     else:
         dt = dtype

     data_array = np.fromfile(fid, dt, nelements)
     data_array.shape = (nelements, 1)

     return data_array

fid = open('test.bin', 'rb');

print fread(fid, 2, np.int16)
print fread(fid, 32, np.str)
print fread(fid, 2, np.int16)

rawDescription: Ensuring python equivalence of matlab's `fread`


code:
N = 10000
red_green_test = {'car':0.0, 'toy':0.0, 'ballon':0.0}
red_blue_test = {'car':0.0, 'toy':0.0, 'ballon':0.0}
blue_test = {'car':0.0, 'toy':0.0, 'ballon':0.0}
red_blue_green_test = {'car':0.0, 'toy':0.0, 'ballon':0.0}
default_test = {'car':0.0, 'toy':0.0, 'ballon':0.0}

for _ in xrange(N):
    red_green_test[d["red","green"]] += 1.0
    red_blue_test[d["red","blue"]] += 1.0
    blue_test[d["blue"]] += 1.0
    default_test[d["green"]] += 1.0
    red_blue_green_test[d["red","blue","green"]] += 1.0

print 'red,green test      =', ' '.join('{0}: {1:05.2f}%'.format(key, 100.0*val/N) for key, val in red_green_test.items())
print 'red,blue test       =', ' '.join('{0}: {1:05.2f}%'.format(key, 100.0*val/N) for key, val in red_blue_test.items())
print 'blue test           =', ' '.join('{0}: {1:05.2f}%'.format(key, 100.0*val/N) for key, val in blue_test.items())
print 'default test        =', ' '.join('{0}: {1:05.2f}%'.format(key, 100.0*val/N) for key, val in default_test.items())
print 'red,blue,green test =', ' '.join('{0}: {1:05.2f}%'.format(key, 100.0*val/N) for key, val in red_blue_green_test.items())

rawDescription: Multikey Multivalue Non Deterministic python dictionary


code:
it = numpy.nditer([b, lowerBounds, upperBounds],
                   op_flags=[['readwrite'], ['readonly'], ['readonly']])
for _b, _lb, _ub in it:
    multiplier = scipy.integrate.quad(f, _lb, _ub)[0]
    _b[...] *= multiplier
print b

rawDescription: Calculating new entries in array based on entries from another array in python


rawDescription: wx.TextCtrl is blank for very long strings

description: Wx.TextCtrl is blank for very long strings


code:
import re
txt="I like to eat apple. Me too. Let's go buy some apples."
words = 'go apples'.split(' ')

sentences = re.findall(r"([^.]*\.)" ,txt)
for sentence in sentences:
    if all(word in sentence for word in words):
        print sentence

rawDescription: Python extracting sentence containing 2 words


code:
import matplotlib.pyplot as plt

def get_info(obj):
    if 'get_children' in dir(obj):
        for o in obj.get_children():
            print o
            if('get_path' in dir(o)): print o.get_path()
            get_info(o)

x = range(10)
y = [c**2 for c in x]
fig,sp = plt.subplots(1,1)
plt.plot(x,y)

get_info(fig)

rawDescription: Extract path or position information about all objects in a matplotlibfigure


code:
import pandas as pd

infile = pd.read_csv("avito_trend.csv",
                      parse_dates=[2])

print infile

print infile[ infile['used_at'].dt.year.isin([2014,2015])]

rawDescription: Group csv string by Year

description: Group csv string by Year


code:
def first():
    print "In function first"
    second()

def second():
    print "In function second"

first()

rawDescription: Python Coding (call function / return values)


code:
with open('file.log') as f:
    print "".join(list(f)[2011:2220])

rawDescription: How can I get select specific lines of a text file in python?

description: Get select specific lines of a text file in python


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    roles = relationship("Role", secondary= Table(
                    'role_to_user', Base.metadata,
                    Column('role_id', Integer,
                            ForeignKey('role.id'),
                            primary_key=True),
                    Column('user_id', Integer,
                            ForeignKey('user.id'),
                            primary_key=True),
                    ),
                    lazy="subquery"
                )

    def operate_on_roles(self):
        for role in self.roles:
            role.operate(self)

class Role(Base):
    __tablename__ = 'role'
    id = Column(Integer, primary_key=True)
    type = Column(String, nullable=False)
    __mapper_args__ = {'polymorphic_on': type}

class CustomerRole(Role):
    __tablename__ = 'customer'
    id = Column(Integer, ForeignKey(Role.id), primary_key=True)
    __mapper_args__ = {'polymorphic_identity':'customer'}

    def operate(self, user):
        print "user %s getting my car fixed!" % user.name

class MechanicRole(Role):
    __tablename__ = 'mechanic'
    id = Column(Integer, ForeignKey(Role.id), primary_key=True)
    __mapper_args__ = {'polymorphic_identity':'mechanic'}

    def operate(self, user):
        print "user %s fixing cars!" % user.name

e = create_engine('sqlite://', echo=True)

Base.metadata.create_all(e)

s = Session(e)

m, c = MechanicRole(), CustomerRole()
s.add_all([m, c])
s.add_all([
        User(name='u1', roles=[m, c]),
        User(name='u2', roles=[c]),
        User(name='u3', roles=[m]),
    ])
s.commit()

for user in s.query(User):
    user.operate_on_roles()

rawDescription: How do I correctly model joined table inheritance with discriminator based on Role in SQLAlchemy

description: Correctly model joined table inheritance with discriminator based on Role in SQLAlchemy


code:
listEx = ['cat *(select: "Brown")*', 'dog', 'turtle', 'apple']
listEx2 = ['hampter',' bird', 'monkey', 'banana', 'cat']

for j in listEx2:
    for pos, i in enumerate(listEx):
        if j in i:
            print j, "found in", i, "at position", pos, "of listEx"

rawDescription: Python two lists finding index value

description: Python two lists finding index value


code:
mydict={'tom': '100','dick': '50','harry': '121','jim':'25'}
print sorted(mydict, key = lambda k: int(mydict[k]), reverse = True)

rawDescription: sorting a dictionary key based on values after they are converted to integers

description: Sorting a dictionary key based on values after they are converted to integers


code:
#!/usr/bin/env python

import timeit

from collections import defaultdict
import collections

def first():
    from collections import defaultdict
    x = defaultdict(list)

def firstwithout():
    x = defaultdict(list)

def second():
    import collections
    x = collections.defaultdict(list)

def secondwithout():
    x = collections.defaultdict(list)

print "first with import",timeit.timeit('first()', 'from __main__ import first');
print "second with import",timeit.timeit('second()', 'from __main__ import second');

print "first without import",timeit.timeit('firstwithout()', 'from __main__ import firstwithout');
print "second without import",timeit.timeit('secondwithout()', 'from __main__ import secondwithout');

rawDescription: Optimization of importing modules in Python


code:
print nameb, "met", nameg
print "they met at", place

rawDescription: Python- Simple random selector

description: Python- Simple random selector


code:
def inorder(node):
    if not isinstance(node, list):
        node = [node]
    for n in node:
        for left in inorder(getattr(n, 'lefts', [])):
            yield left
        yield n.name
        for right in inorder(getattr(n, 'rights', [])):
            yield right

print list(inorder(tree))
# ['a', 'b', 'c', 'd', 'f', 'g', 'e']

rawDescription: How to iterate this tree/graph

description: Iterate this tree/graph


code:
from bs4 import BeautifulSoup

doc = BeautifulSoup(xml, features="xml")
print doc.prettify()

rawDescription: XML node with same name in another node

description: XML node with same name in another node


code:
tralala = dict()

for group, name in [('A', 'Bob'), ('B', 'Jane'), ('A', 'Bob')]:
    tralala.setdefault(group, dict()).setdefault(name, 0)
    tralala[group][name] += 1

print tralala

rawDescription: Count the multiple occurrences in a set

description: Count the multiple occurrences in a set


code:
import subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
print process.returncode

rawDescription: how to open a cmd shell in windows and issue commands to that shell using python

description: Open a cmd shell in windows and issue commands to that shell using python


code:
def euclidean_dist2(x1, x2):
    assert(len(x1) == len(x2))

    x1 = np.array(x1)
    x2 = np.array(x2)

    norm = np.linalg.norm(x1-x2)

    return norm

print euclidean_dist2([1,2],[4,7])

rawDescription: How to vectorize array operations in Python

description: Vectorize array operations in Python


code:
import multiprocessing as mp
import time

def add(x, y):
    time.sleep(1)
    return x + y

def add_wrapper(args):
    return add(*args)

if __name__ == '__main__':
    pool = mp.Pool(4)
    for result in pool.map(add_wrapper, [(1,2), (3,4), (5,6), (7,8)]):
        print result

rawDescription: Obtain results from processes using python multiprocessing


code:
b = raw_input('enter word: ')
c = input('enter the amount of time the word will repeat: ')

for g in range (c):
    print b,

rawDescription: Print horizontal with for in Python 2.7

description: Print horizontal with for in Python 2.7


code:
hand = {'a': 1, 'q': 1, 'd': 2, 'y': 1, 'm': 1, 'o': 1}
displayHand(hand)
print 'Current Hand:', displayHand(hand)

rawDescription: Print formatting a string and function call in python


code:
from itertools import combinations
lis = [[0,0,0],[0,0,1],[0,1,0]]
for i in range(1, len(lis)+1):
    for c in combinations(lis,i):
        print c

rawDescription: How to use combinations

description: Use combinations


code:
#! /usr/bin/env python

''' Resumable SHA-256 hash for large files using the OpenSSL crypto library

    The hashing process may be interrupted by Control-C (SIGINT) or SIGTERM.
    When a signal is received, hashing continues until the end of the
    current chunk, then the current file position, total file size, and
    the sha object is saved to a file. The name of this file is formed by
    appending '.hash' to the name of the file being hashed.

    Just re-run the program to resume hashing. The '.hash' file will be deleted
    once hashing is completed.

    Written by PM 2Ring 2014.11.14
'''

import cPickle as pickle
import os
import signal
import sys

import rehash

quit = False

blocksize = 1<<16   # 64kB
blocksperchunk = 1<<8

chunksize = blocksize * blocksperchunk

def handler(signum, frame):
    global quit
    print "\nGot signal %d, cleaning up." % signum
    quit = True

def do_hash(fname, filesize):
    hashname = fname + '.hash'
    if os.path.exists(hashname):
        with open(hashname, 'rb') as f:
            pos, fsize, sha = pickle.load(f)
        if fsize != filesize:
            print "Error: file size of '%s' doesn't match size recorded in '%s'" % (fname, hashname)
            print "%d != %d. Aborting" % (fsize, filesize)
            exit(1)
    else:
        pos, fsize, sha = 0, filesize, rehash.sha256()

    finished = False
    with open(fname, 'rb') as f:
        f.seek(pos)
        while not (quit or finished):
            for _ in xrange(blocksperchunk):
                block = f.read(blocksize)
                if block == '':
                    finished = True
                    break
                sha.update(block)

            pos += chunksize
            sys.stderr.write(" %6.2f%% of %d\r" % (100.0 * pos / fsize, fsize))
            if finished or quit:
                break

    if quit:
        with open(hashname, 'wb') as f:
            pickle.dump((pos, fsize, sha), f, -1)
    elif os.path.exists(hashname):
        os.remove(hashname)

    return (not quit), pos, sha.hexdigest()

def main():
    if len(sys.argv) != 2:
        print "Resumable SHA-256 hash of a file."
        print "Usage:\npython %s filename\n" % sys.argv[0]
        exit(1)

    fname = sys.argv[1]
    filesize = os.path.getsize(fname)

    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    finished, pos, hexdigest = do_hash(fname, filesize)
    if finished:
        print "%s  %s" % (hexdigest, fname)
    else:
        print "sha-256 hash of '%s' incomplete" % fname
        print "%s" % hexdigest
        print "%d / %d bytes processed." % (pos, filesize)

if __name__ == '__main__':
    main()

rawDescription: Persisting sha256 hash objects?

description: Persisting sha256 hash objects


code:
import itertools

for elem in itertools.islice(f(), 3):
    print elem

rawDescription: Get a fixed number of items from a generator


code:
for rows in (4,6,8):
    for cols in (4,6,8):
        lr = (rows+1)*cols//2
        r = rows//2
        c = cols//2
        print rows, cols, ':\t', r,c, '\t', lr-cols-1, '\t', lr-cols, '\t', lr-1, '\t', lr, '\t', r*cols+c

rawDescription: An algorithm to calculate the square numbers for the middle four squares on a game board?


code:
import ctypes
class Conv(ctypes.Union):
  _fields_ = [ ("float", ctypes.c_double), ("int", ctypes.c_uint64)]

c = Conv()
c.float = 1.5
print hex(c.int)

rawDescription: Copying internal formats float64 uint64

description: Copying internal formats float64 uint64


code:
names = [ "Brian", "jake", "Jason", "Brad", "Tony", "jimmy", "Bobby", "Stevie"]

long = []
short = []
len_short = min([len(x) for x in names])  # this will be 4
len_long = max([len(x) for x in names])  # this will be 6

for name in names:
    if len(name) <= len_short:
        short.append(name)
        print "Short names: ", short

for name in names:
    if len(name) >= len_long:
        long.append(name)
        print "long names: ", long

rawDescription: Create new lists of the shortest and longest words in an existing list - Python


code:
found = re.findall(img_url_regex, data)
if not found: # the list is empty
    sys.stderr.write("Error finding Regex \r\n")
else:
    for imgurl in found:
        print 'Found image:', imgurl
        # whatever else you want to do with the URL.

rawDescription: Converting Script from Powershell to Python-Regex Not Working as Expected


code:
import json
import urllib2

url = 'http://otter.topsy.com/trackbacks.js?url=http%3A%2F%2Fmashable.com%2F2014%2F08%2F27%2Faustralia-retail-evolution-lab-aopen-shopping%2F&infonly=0&call_timestamp=1411090809443&apikey=09C43A9B270A470B8EB8F2946A9369F3'
data = json.load(urllib2.urlopen(url))
for tweet in data['response']['list']:
    print tweet['permalink_url']

rawDescription: Urllib Python is not providing with the html code I see with with inspect element


code:
vowels = set('aeiou')
counter = collections.Counter(v for v in sentence.lower() if v in vowels)
ranking = counter.most_common()
print ranking[0]  # most common
print ranking[-1]  # least common

rawDescription: Python string vowel counter

description: Python string vowel counter


code:
import itertools

list1 = [1,2,3]
list2 = [1,2,3]

for idx, (x, y) in enumerate(itertools.product(list1, list2), 1):
    print x, y, idx

rawDescription: How to iterate through three lists in python

description: Iterate through three lists in python


code:
import threading
import time
import random

print_lock = threading.Lock()
def atomic_print(msg):
    print_lock.acquire()
    print msg
    print_lock.release()

def loop_fn(i):
    n = random.randint(1,5)
    atomic_print("process {} started ".format(i))
    time.sleep(n)
    atomic_print("process {} finished".format(i))

threads = []
for i in range(1,5):
    t = threading.Thread(target=loop_fn, args=(i,))
    t.start()
    threads.append(t)

for t in threads:
    t.join()
print "end"

rawDescription: Python threading, weird result

description: Python threading, weird result


code:
import cmd

class MyCmd(cmd.Cmd):
    def do_cmd1(self): pass
    def do_cmd2(self): pass
    def do_cmd3(self): pass
    def do_help(self, *args):
        cmd.Cmd.do_help(self, *args)
        print 'use Ctrl+l to clear screen, Ctrl+a ...)'

d = MyCmd()
d.cmdloop()

rawDescription: Augment Python cmd module "help" printout


code: print D.dot(A.dot(y)) - (D*(A*y))

rawDescription: Shape inconsistence in my code based on numpy/scipy package


code:
with open('database.txt', 'r') as f:
    results = {first_name:dict(first_name=first_name, last_name=last_name, phone=phone, email=email) for first_name, last_name, phone, email in [line.strip().split('\n') for line in f.read().split('--------------------') if line.strip() != ""]}
    print results

rawDescription: reading lines from file to make 2D dictionary [python]

description: Reading lines from file to make 2D dictionary [python]


code:
higher_order_conversion = {
    ('apple', 'pear', 'kiwi'): 'fruit',   #the keys must be tuples, not lists
    ('X', 'Y', 'Z'): 'letter', # (because tuples are immutable and therefore hashable)
    ('loafers', 'sneakers', 'high heels'): 'shoes'
}

data_set = [[125, 'apple'], #these numbers are id numbers, or whatever extra information you might have packaged with your data
            [126, 'Y'],
            [127, 'loafers'],
            [103, 'kiwi']
            ]

print 'before', data_set

for data in data_set:
    for lower_order_list in higher_order_conversion.keys():
        if data[1] in lower_order_list:
            data[1] = higher_order_conversion[lower_order_list]

print 'after', data_set

rawDescription: dictionary-based keyword categorization

description: Dictionary-based keyword categorization


code:
c = YourAlchemyClass()
print json.dumps(c, cls=AlchemyEncoder)

rawDescription: How to serialize SqlAlchemy result to JSON?

description: Serialize SqlAlchemy result to JSON


rawDescription: Combine a list of tuples and list, removing the tuple elements from the list


code:
class Word():
  def __init__(self, name):
    print 'You have just created me and I\'m called ' + name
    self.name = name
    self.wordType = None
  def setWordType(self, type_string):
    print "Hey, and the method works!"
    self.wordType = type_string

wordArray = ["spam","ni", "is","blue"]
count = 3
if count > 0 and wordArray[count - 1] == 'is':
  title = wordArray[count]
  exec("%s = Word(title)" % (title)) #set class from list item.
  exec("%s.setWordType('verb')" % (title))

rawDescription: Can't access object even though I know it has been created?