code:
import multiprocessing as mp
import time

class LogParserProcess(mp.Process):
    def __init__(self):
        super(LogParserProcess, self).__init__()
        self.done = mp.Event()
        self.flights_to_add = ()

    def stop(self):
        self.done.set()
        print 'Stopping: ' + str(self.done.is_set())

    def run(self):
        file_list = ()
        while not self.done.is_set() or len(file_list) > 0:
            print 'Status ' + str(self.done.is_set()) + ' ' + str(len(file_list))

files_to_copy = "file1 file2 file3"
proc = LogParserProcess()
proc.start()
print 'BEFORE COPY ' + str(proc.done.is_set())
time.sleep(1)
print 'AFTER COPY ' + str(proc.done.is_set())
proc.stop()
print 'AFTER STOP ' + str(proc.done.is_set())
proc.join()
print 'AFTER JOIN ' + str(proc.done.is_set())

rawDescription: Python: can't set Process variable from outside Process


code:
d=Decimal('3.14159')

list_d = str(d).split('.')
# Converting the decimal to string and splitting it at the decimal point

# If decimal point exists => Negative exponent
# i.e   3.14159 => "3", "14159"
# exponent = -len("14159") = -5
# integer = int("3"+"14159") = 314159

if len(list_d) == 2:
    # Exponent is the negative of length of no of digits after decimal point
    exponent = -len(list_d[1])
    integer = int(list_d[0] + list_d[1])

# If the decimal point does not exist => Positive / Zero exponent
# 3400
# exponent = len("3400") - len("34") = 2
# integer = int("34") = 34

else:
    str_dec = list_d[0].rstrip('0')
    exponent = len(list_d[0]) - len(str_dec)
    integer = int(str_dec)

print integer, exponent

rawDescription: How to Convert pythons Decimal() type into an INT and exponent


code:
import mock

def foo():
    for line in open('myfile'):
        print line

@mock.patch('__builtin__.open')
def test_foo(open_mock):
    foo()
    assert open_mock.called

rawDescription: Mocking file objects or iterables in python


code:
def findSum(nbr_list):
    total = 0
    for nbr in nbr_list:
        total += nbr
    return total

nbr_list = [1,2,3,4,100]
print 'Total: ', findSum(nbr_list)

rawDescription: I can't seem to get this function to add the list together


code:
class Klass(object):
        pass

list1 = [('method1', 'value1'), ('method2', 'value2'), ('method10', 'value10')]

def make_attr(value):
    return lambda self: value

for method_name, method_value in list1:
    setattr(Klass, method_name, make_attr(method_value))

c = Klass()
print c.method1(), c.method2(), c.method10()  # "value1, value2, value10"

rawDescription: python setattr for dynamic method creator with decorator

description: Python setattr for dynamic method creator with decorator


code:
for element in root.iter(tag='unit'):
    print element #This would print all the unit elements in the document.

rawDescription: Get attribute of complex element using lxml


code:
for word in doc.Words:
    print word

rawDescription: Extract headings from a MS Word document in Python


code:
import numpy as np
import re

def validate(seq):
    stl = "".join(np.diff(seq).astype(str))
    for x in re.findall("[1]+",stl):
        if len(x)>3:
            return False
    return True

print validate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 16, 20])
print validate([1, 2, 3, 6, 7, 8, 9, 11, 12, 16, 17, 18, 19, 22, 23])

rawDescription: Avoid chained numbers when printing lists


code:
res = [[[0 for item3 in range(2)] for item2 in range(2)] for item1 in range(10)]
print res

rawDescription: How do I create many matrices inside a loop statement with python?

description: Create many matrices inside a loop statement with python


code:
import re
import ast
with open('test.txt.') as f:
 for line in f:
  m = re.match('(.*) \w+ @ \w+ \w+', line)
  print ast.literal_eval(m.group(1))

rawDescription: Escaping quotes when isolating strings from input


code:
for section in load_sections('/tmp/test.txt'):
    print section

rawDescription: Parsing data from text file

description: Parsing data from text file


code:
def ctrl_buttons(self, button):
    if button=="PLAY":
        print "pressed play"
    elif button=="STOP":
        print "pressed stop"

rawDescription: Kivy/python. One callback function for several buttons


code:
class Chunk:
    def __init__(self, bucket_name, identifier, chunk_number):
        self.chunk_number = chunk_number
        self.filename = '/' + bucket_name + '/' + identifier + '-chunk-' + str(chunk_number)
        self.stat = None
        self.load_stat()

    def load_stat(self):
        try:
            self.stat = gcs.stat(self.filename)
        except gcs.NotFoundError:
            self.stat = None

    def exists(self):
        return not not self.stat

    def write(self, size, data):
        write_retry_params = gcs.RetryParams(backoff_factor=1.1)
        gcs_file = gcs.open(self.filename, 'w', retry_params=write_retry_params)
        for c in data.file:
            gcs_file.write(c)
        gcs_file.close()
        self.load_stat()

    def write_on(self, stream):
        gcs_file = gcs.open(self.filename)

        try:
            data = gcs_file.read()
            while data:
                stream.write(data)
                data = gcs_file.read()
        except gcs.Error, e:
            log.error('Error writing data to chunk: ' + e.message)
        finally:
            gcs_file.close()

    def delete(self):
        try:
            gcs.delete(self.filename)
            self.stat = None
        except gcs.NotFoundError:
            pass

rawDescription: Files uploaded using ng-flow to upload to gae blobstore always named 'blob'


code:
def Interrupt_Button_clicked(widget, args=()):
    def cb():
        builder.get_object('Azioni_Window').show()
        print ("pulsing2 is done")

    pulsing2(callback=cb)

def pulsing2(fraction=0.0, callback=None):
    progressbar.set_inverted(True)
    fraction += 0.2
    progressbar.set_fraction(fraction)
    print fraction # THIS WORKS CORRECTLY EVERY TIME, 5 TIMES IN TOTAL (1/0.2)
    if fraction != 1:
        gobject.add_timeout(1, pulsing2, fraction, callback)
    else:
        if callback:
            gobject.idle_add(callback)

rawDescription: Thread.join() doesn't let the thread itself to update the GUI


code:
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  def do_GET(self):
      # Your code here
      print "Client requested:", self.command, self.path

      SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

PORT = 8000

httpd = SocketServer.TCPServer(("", PORT), MyHandler)

print "Serving at port:", PORT
httpd.serve_forever()

rawDescription: SimpleHTTPServer and SocketServer

description: SimpleHTTPServer and SocketServer


code: print 2*p

rawDescription: Sparse random matrix in Python with different range than [0,1]


code:
import threading
import Queue

class Server(threading.Thread):
    def __init__(self, title, q, loop_time = 1.0/60):
        self.title = title
        self.q = q
        self.timeout = loop_time
        super(Server, self).__init__()

    def run(self):
        print "%s started" % self.title
        while True:
            try:
                function, args, kwargs = self.q.get(timeout=self.timeout)
                function(*args, **kwargs)

            except Queue.Empty:
                # print "empty"
                self.idle()

    def idle(self):
        pass
        # put the code you would have put in the `run` loop here

    # This is the top level function called by other objects
    def reboot(self):
        self.__onThread(self.__reboot)

    def __reboot(self):
        if not hasattr(self, "_down"):
            self._down = threading.Thread(target=self.__powerDown)
            self._down.start()
            up = threading.Thread(target=self.__powerUp)
            up.start()

    def __onThread(self, function, *args, **kwargs):
        self.q.put((function, args, kwargs))

    def __powerDown(self):
        # do something
        print "%s power down" % self.title
        pass

    def __powerUp(self):
        print "%s power up" % self.title

        if not hasattr(self, "_down"):
            return

        self._down.join()
        # do something
        del self._down

class ServerA(Server):
    pass

class ServerB(Server):
    pass

def main():
    serverA = ServerA("A", Queue.Queue())
    serverB = ServerB("B", Queue.Queue())
    serverA.start()
    serverB.start()
    serverA.reboot()
    serverB.reboot()

if __name__ == '__main__':
    main()

rawDescription: How do I Spawn threads from two different objects and coordinate them in python v2.7?


code:
for tags in root.iter():
    print tags.text

rawDescription: Using lxml to extract data where all elements are not known in advance


rawDescription: Python List length as a string

description: Python List length as a string


code:
for num in s.split(","):
    print float(num)

rawDescription: Sum of all numbers inside a string

description: Sum of all numbers inside a string


code:
def readRows():
    # using list comprehension
    return [ sh1.row_values(idx, 4, 6) for idx in range(sh1.nrows) ]

print readRows()

rawDescription: Help with Excel, Python and XLRD

description: Help with Excel, Python and XLRD


code:
result = []
for c in this.s:
   if c in this.d:
      result.append(this.d[c])
   else:
      result.append(c)
print "".join(result)

rawDescription: Dissecting a line of (obfuscated?) Python


code:
main_dict={
    'dictrefA':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy"},
    'dictrefB':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy1"},
    'dictrefC':{
      "operation_module":"cbs",
      "operation_group":"xxx1",
      "operation_type":"yyy1"}
      }

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}

for x,y in main_dict.items():
    if all(v in recievedDict.items() for v in y.items()):
        print x
        break

rawDescription: Determining which Dictionary matches my dictionary class in python


code:
grouped = df.reset_index().groupby(((df.index == 0)*1).cumsum())
for n,g in grouped:
    print g

rawDescription: how to groupby a DataFrame by index

description: Groupby a DataFrame by index


code: print "ID is : ", m.groups()[1]

rawDescription: Return All Matching Lines in a Logfile

description: Return All Matching Lines in a Logfile


code:
import urllib2

import poster.encode
import poster.streaminghttp

opener = poster.streaminghttp.register_openers()

params = {'filename': open(uploadfile,'rb'), 'description': 'upload test'}
datagen, headers = poster.encode.multipart_encode(params)
response = opener.open(urllib2.Request(upload_file_url, datagen, headers))
print response.read()

rawDescription: uploading files with urllib to a FileField model in Django

description: Uploading files with urllib to a FileField model in Django


code:
def get_all_substrings(string):
  length = len(string)
  alist = []
  for i in xrange(length):
    for j in xrange(i,length):
      alist.append(string[i:j + 1])
  return alist

def get_all_substrings_1(input_string):
  length = len(input_string)
  return [input_string[i:j + 1] for i in xrange(length) for j in xrange(i,length)]

from timeit import timeit
print timeit("get_all_substrings('abcde')", "from __main__ import get_all_substrings")
# 3.33308315277
print timeit("get_all_substrings_1('abcde')", "from __main__ import get_all_substrings_1")
# 2.67816185951

rawDescription: How To Get All The Contiguous Substrings Of A String In Python?


code:
class Base(object):
    cc = '' # this won't get printed
    def __init__(self):
        # print SubClass' new attribues' names ('aa' and 'bb' for example)
        print set(dir(self.__class__)) - set(dir(Base))

class SubClass(Base):
    aa = ''
    bb = ''

foo = SubClass()
# set(['aa', 'bb'])

rawDescription: Python: how to get subclass's new attributes name in base class's method?


code:
shortcuts = {}
shortcuts['name'] = tree['node1']['node11']['node111']
print shortcuts['name']['_data']

rawDescription: Python apply grid on 2D data, save non empty grid cells in tree


code:
from itertools import count
for window in incremental_window(count()):
    print window
    if 25 in window:
        break

rawDescription: Python grouping elements in a list in increasing size


code:
import Tkinter
root = Tkinter.Tk()
def unshow_me(event):
    event.widget.grid_forget()

def handle_click(text):
    print text
    a=text

for r in range(3):
    for c in range(6):
        text = 'R=%s,C=%s'%(r,c)
        label = Tkinter.Label(root, text=text, borderwidth=1 )
        label.grid(row=r,column=c)
        label.bind("<Button-1>", lambda e, text=text:(handle_click(text), unshow_me(e)))

root.mainloop()

rawDescription: Hide function not called

description: Hide function not called


code:
from pyparsing import nestedExpr

data = "( (a ( ( c ) b ) ) ( d ) e )"
print nestedExpr().parseString(data).asList()

rawDescription: Matching Nested Structures With Regular Expressions in Python


code:
from numpy import array

print array(data).reshape(*length)  # optionally add .tolist() to convert to list

rawDescription: Python list to XML and vice versa

description: Python list to XML and vice versa


code:
import json
array = '[1, 2, 3]'
for item in json.loads(array):
    print item
# Prints 1, then 2, then 3

rawDescription: Exclude Char from Python For

description: Exclude Char from Python For


code:
import datetime
from itertools import groupby

def get_week(date):
    return date.isocalendar()[1]

def group_by_month(weeks, year):
    """
    Group a list containing one item per week, starting with week 1, by month.

    If there are too few items to fill a year, stop after last item.
    If there are more items than weeks in the year, stop before new year.
    """
    day = datetime.timedelta(days=1)
    week = datetime.timedelta(days=7)

    # Find first Thursday (it's in week 1 by ISO 8601)
    date = datetime.date(year, 1, 1)
    while date.weekday() != 3:
        date += day

    # Create list of one day from each week
    thursdays = []
    while date.year == year:
        thursdays.append(date)
        date += week

    # Check if the last day is in the last week and if not,
    # add the week of the last day
    last = tursdays[-1]
    if get_week(last.replace(day=31)) != get_week(last):
        # this will not be a Thursday, but what the hey
        thursdays.append(last.replace(day=31))

    # The thursdays are already sorted by month, so
    # it's OK to use groupby without sorting first
    for k, g in groupby(zip(weeks, thursdays), key=lambda x: x[1].month):
        yield [x[0] for x in g]

list_1 = [500] * 52

print map(sum, group_by_month(list_1, 2012))

rawDescription: Python: Nested If Looping

description: Python: Nested If Looping


code:
import re
line = "service_description     M:\ Drive Space"

pattern = "\s*(service_description)\s*([A-Z]:\\\s*Drive\s*Space)"
print "your pattern:   ", pattern

pattern = r"\s*(service_description)\s*([A-Z]:\\\s*Drive\s*Space)"
print "what you wanted:", pattern

p1 = re.compile(pattern)
match = p1.match(line)
print match
if match:
    print match.group(2)

rawDescription: Python regex wont match or escape backslashes


code:
from functools import partial

# call this on every button selection
def test(filepath, ignore):
    # maya will send "test(name, False)"; we just ignore the 'False'
    print "Here's where I would reload", filepath

example = cmds.window(title = 'example')
menubar = cmds.menuBarLayout()

for name in folder_names:
    menuname = name
    if menuname:
        menuname = menuname.split("/")[-2] # we used trailing slashes
    else:
        menuname = "root"
    cmds.menu(label = menuname)
    file_names = root_dict[name]
    file_names.sort()
    for fn in file_names:
        mi = cmds.menuItem(label = fn, command = partial(test, fn))
    cmds.setParent(menubar)

rawDescription: Multiple Paths Traversed and Displayed Filed type in Maya Menu with Python


code:
from itertools import imap,islice

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'nut','banana','apple',
      'heeeeeeeeeeen','tiger','snake'
      'red','purple','white',
      'atlantic','pacific','antarctic',
      'Bellini']

print 'len of li == %d\n' % len(li)
def cols_print(li,nc):
    maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2
                  for st in xrange(nc))

    nblines,tail = divmod(len(li),nc)
    stakes = nc*['%%-%ds']
    form = ''.join(stakes) % maxel

    itn = iter(li).next

    print '\n'.join(form % tuple(itn() for g in xrange(nc))
                  for i in xrange(nblines))
    if tail:
        print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
    else:
        print

for nc in xrange(3,8):
    cols_print(li,nc)
    print 'mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm'

rawDescription: Print python list in groups of 3

description: Print python list in groups of 3


code:
data="""? (192.168.30.4) at 00:10:60:0a:70:26 [ether]  on vlan20
? (192.168.30.1) at 70:ca:9b:99:6a:82 [ether]  on vlan20"""

print "#Protocol  Address          Age (min)   Hardware Addr   Type   Interface"
for line in data.splitlines():
    _,ip,_,har_ad,_,_,interface = line.split()
    ip = ip.strip('()')
    it = iter(har_ad.split(':'))
    har_ad = ".".join([x+y for x,y in zip(it,it)])
    print "#Internet  {} {:>11s} {:>18s} {:>5s} {:>8s}".format(ip,'-', har_ad,'ARPA' ,interface)

rawDescription: Make lists into further sublists

description: Make lists into further sublists


code:
import numpy as np
import pandas as pd
idx =["%07d" % x for x in range(int(2e6))]
a = pd.Series(np.arange(2e6, dtype=np.double), index=idx)
new_index = ["0000003", "0000020", "000002a"]

print a.index._engine.mapping # None
print a.reindex(new_index)
print a.index._engine.mapping # <pandas.hashtable.PyObjectHashTable object at ...>

a = pd.Series(np.arange(2e6, dtype=np.double), index=idx)
print a.asof(new_index)
print a.index._engine.mapping # None

rawDescription: pandas memory usage when reindexing

description: Pandas memory usage when reindexing


code:
for s in (
    '"java jobs in delhi" it software "pune" hello',
    'foo "bar"',
):
    result = s.split('"')
    print 'text between quotes: %s' % (result[1::2],)
    print 'text outside quotes: %s' % (result[::2],)

rawDescription: Extract unquoted text from a string

description: Extract unquoted text from a string


code:
import subprocess
# Create a Popen object that captures the output.
p=subprocess.Popen(['look','data'],stdout=subprocess.PIPE)
# Call 'look data' and wait for it to finish.
p.wait()
# Now read the output.
print p.stdout.read()

rawDescription: How do I write to a file using a terminal command in python?

description: Write to a file using a terminal command in python


code:
class FormPostSerializer(Serializer):
    formats = ['form']
    content_types = {
        'form': 'multipart/form-data',
    }

    def from_form(self, content):
        try:
            dict = cgi.parse_multipart(StringIO(content), self.form_boundary)
        except Exception, e:
            raise e
        for key, value in dict.iteritems():
            dict[key] = value[0] if len(value) > 0 else None
        return dict

rawDescription: TastyPie: is it possible to post multiple files in a bulk request


code:
class IndexHandler(webapp.RequestHandler):
    """Your site"""
    def get(self):
        self.response.out.write("hi")

if __name__ == '__main__':
    application = webapp.WSGIApplication([
        ('.*', IndexHandler),
    ], debug=True)

    # Start fake-request/profiling bit
    urls = [
        "/",
        "/blog/view/hello",
        "/admin/post/edit/hello",
        "/makeanerror404",
        "/makeanerror500"
    ]

    def fake_wsgi_callback(response, headers):
        """Prints heads to stdout"""
        print("\n".join(["%s: %s" % (n, v) for n, v in headers]))
        print("\n")

    for request_url in urls:
        html = application({
        'REQUEST_METHOD': 'GET',
        'PATH_INFO': request_url},
        fake_wsgi_callback
        )
        print html

rawDescription: Best way to profile/optimize a website on google's appengine

description: Profile/optimize a website on google's appengine


code:
class bla(object):
 def __init__(self):
  self.a = 1
 def foo(self):
  print self.a
d = Wrapper(bla())
d.a
d.foo()
d.a = 2
d.a
d.foo()

rawDescription: Python: How to extend a huge class with minimum lines of code?


code:
a = ['hi','helllo']
for i in a:
    print i,

rawDescription: How to use this for loop to get expected result in python

description: Use this for loop to get expected result in python


code:
import pkgutil
# this is the package we are inspecting -- for example 'email' from stdlib
import email
package = email
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
    print "Found submodule %s (is a package: %s)" % (modname, ispkg)

rawDescription: Python Finding all packages inside a package, even when in an egg


code:
for k, v in obj[0].items():
    print k, j

rawDescription: How to change this json like string to json with python

description: Change this json like string to json with python


code:
step.reverse()
for key, group in groupby(time, lambda _: step.pop()):
    group = tuple(group)
    print 'Step:', key, 'Min:', min(group), 'Max:', max(group)

rawDescription: Best way in Python to combine 2 lists and return a min / max of a set


rawDescription: How to get elements from a list, by groups of 10? (or 5, or 3, etc)

description: Get elements from a list, by groups of 10? (or 5, or 3, etc)


code:
string1 = 'IIXIIXIIXIIX'
substring = 'X'
replacement='B'
ss = input("Which substring would you like to replace: ")
list1= string1.split(substring)
print  substring.join(list1[:ss])+replacement+substring.join(list1[ss:])

rawDescription: user selects substring in python

description: User selects substring in python


code:
# print all clusters and their sets
for cluster in union_find:
    print cluster, union_find[cluster]

rawDescription: Implementing Disjoint Set Data Structure in Python


code:
def solution3():
    return z[:,ycond, :][...,xcond]

N = 10
print timeit.timeit("solution1()", setup="from __main__ import solution1, solution2, solution3, z, xcond, ycond, xx, yy", number=N)
print timeit.timeit("solution2()", setup="from __main__ import solution1, solution2, solution3, z, xcond, ycond, xx, yy", number=N)
print timeit.timeit("solution3()", setup="from __main__ import solution1, solution2, solution3, z, xcond, ycond, xx, yy", number=N)

# 0.439269065857   # solution1
# 0.752536058426   # solution2
# 0.340197086334   # solution3

rawDescription: Conditioning and operation on multidimensional (3D) array in Python-NumPy


code:
from itertools import groupby as g
def most_common_oneliner(L):
  return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]

rawDescription: Python most common element in a list

description: Python most common element in a list


code: print 'yes' if list(val) == list(reversed(val)) else 'no'

rawDescription: Testing palindrome with nested loops

description: Testing palindrome with nested loops


code:
# This will assume, if no page selected, it is on the first page
actual_page = request.GET.get('page', 1)
if actual_page:
    print actual_page

rawDescription: Get current page of pagination in view and manually set count of page


code:
columns = [x for x in header if x != 'ID']
print columns
['Age', 'Income', 'Deductions', 'Hours', 'Adjustment', 'Adjusted', 'Employment_Consultant', 'Employment_PSFederal', 'Employment_PSLocal', 'Employment_PSState', 'Employment_Private', 'Employment_SelfEmp', 'Employment_Unemployed', 'Employment_Volunteer', 'Education_Associate', 'Education_Bachelor', 'Education_College', 'Education_Doctorate', 'Education_HSgrad', 'Education_Master', 'Education_Preschool', 'Education_Professional', 'Education_Vocational', 'Education_Yr10', 'Education_Yr11', 'Education_Yr12', 'Education_Yr5t6', 'Education_Yr7t8', 'Education_Yr9', 'Marital_Absent', 'Marital_Divorced', 'Marital_Married', 'Marital_Married-spouse-absent', 'Marital_Unmarried', 'Marital_Widowed', 'Occupation_Cleaner', 'Occupation_Clerical', 'Occupation_Executive', 'Occupation_Farming', 'Occupation_Machinist', 'Occupation_Professional', 'Occupation_Repair', 'Occupation_Sales', 'Occupation_Service', 'Occupation_Support', 'Occupation_Transport', 'Sex_Female', 'Sex_Male', 'Accounts_Cuba', 'Accounts_England', 'Accounts_Germany', 'Accounts_India', 'Accounts_Indonesia', 'Accounts_Iran', 'Accounts_Ireland', 'Accounts_Jamaica', 'Accounts_Malaysia', 'Accounts_Mexico', 'Accounts_Philippines', 'Accounts_Portugal', 'Accounts_UnitedStates', 'Accounts_Vietnam']
#if you need filter df by columns
df = df[columns]

rawDescription: Python pandas dataframe- remove columns from header


code:
names = []
for s in nuke.selectedNodes():
    n = s['name'].value()
    names.append(n)
print names

rawDescription: Get selected node names into a list or tuple in Nuke with Python


code:
n = 5
W = np.random.randint(10,size=5)
L = [chr(ord('A') + i) for i in W]

L # => ['A', 'C', 'H', 'G', 'C']

srt = np.argsort(W)
result = [(L[i], W[i]) for i in srt]

print result
[('A', 0), ('C', 2), ('C', 2), ('G', 6), ('H', 7)]

rawDescription: 1d list indexing python: enhance MaskableList


code:
for i in get_all_sub_mat(Matrix):
  print i

rawDescription: Find all submatrices of a given matrix

description: Find all submatrices of a given matrix


code:
a = 1

def fn():
    print a  # This is "referencing a variable" == "reading its value"

# Prints: 1

rawDescription: Python : Why is it said that variables that are only referenced are implicitly global?


code:
sam = '/Users/me/Documents/Tools/samtools-1.2/samtools'
home = os.environ['HOME']
inpath = "{}/Documents/Pythonstuff/Bam_count_tags".format(home)

stream_in = "{s} view -h {ip}/test.bam".format(s=sam, ip=inpath)
pyscript = "python {ip}/bam_tags.py".format(ip=inpath)
stream_out = "{s} view -bh - > {ip}/small.bam".format(s=sam, ip=inpath)

# Absolute paths, witten as a pipe
fullPipe = "{inS} | {py} | {outS}".format(inS=stream_in,
                                              py=pyscript,
                                              outS=stream_out)

print fullPipe
# Translates to >>>
# samtools view -h test.bam | python ./bam_tags.py | samtools view -bh - > small.bam

rawDescription: Stream stdout from subprocess to python function, and back to subprocess


code:
fone = set(['apple', 'orange', 'ice', 'icecream'])
ftwo = set(['apple' ,'pear' ,'ice'])
# transforming to sets saves to check twice for the same combination

result = []
for wone in fone:
    for wtwo in ftwo:
        if wone.find(wtwo) != -1 or wtwo.find(wone) != -1:
            result.append(wone)
            result.append(wtwo)
for w in set(result):
    print w

rawDescription: Comparing two .txt files in Python and saving exact and similar matches to .txt file


code:
def nestList(f,x,c):
    result = [x]
    for i in range(c):
        x = f(x)
        result.append(x)
    return result

import math
print nestList(math.cos, 1.0, 10)

rawDescription: how to write a function like Mma's NestList in Python

description: Write a function like Mma's NestList in Python


code:
class MyClass(object):
    data = {}

    def __setitem__(self, key, value):
        self.data[key] = value
        print "__setitem__ called"

    def __getitem__(self, item):
        print "__getitem__ called"
        return self.data[item]

c = MyClass()
c['list'] = ['hello']   # __setitem__ called
c['list'].append('world')   # __getitem__ called

rawDescription: Why is __setitem__ not invoked when changing list type data?


code: print url_key

rawDescription: How do I access a dictionary value for use with the urllib module in python?

description: Access a dictionary value for use with the urllib module in python


code:
def F(x,fie,dfie):
    y = eval(fie)
    return y

def DF(x,fie,dfie):
    dy = eval(dfie)
    return dy

print newton(F,x0,DF,(fie,dfie))

rawDescription: How use the newton function for root finding of the Scipy's optimize package


code:
import inspect

class Tester( object ):
    def method( self ):
        print 'I am a Tester'

print inspect.getsource(Tester)

rawDescription: How to get the function definition from an object?

description: Get the function definition from an object


code:
mylist1 = ['test.alpha', 'test.green']
mylist2 = ['asdf.blue', 'asdf.alpha', 'asdf.red']
sort_order = ['red', 'blue', 'green', 'alpha']
mylist2.sort(key=lambda x: sort_order.index(x.split(".")[1]))
mylist1.sort(key=lambda x: sort_order.index(x.split(".")[1]))
print mylist2
print mylist1

rawDescription: Custom order list python

description: Custom order list python


code: print sorted(availletters)

rawDescription: letter/word guessing game in python

description: Letter/word guessing game in python


code:
def static_vars(**kwargs):
    def decorate(func):
        for k in kwargs:
            setattr(func, k, kwargs[k])
        return func
    return decorate

@static_vars(counter=0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

rawDescription: What is the Python equivalent of static variables inside a function?


code:
for item in variable['result']['data']:
    if item['id'] == 'mike':
        print item['licenses']
        break

rawDescription: Python: Retrieve result from inner dictionary


code:
my_string ='   0   0 -16-50.6123 115.393   2'
widths=[4,4,4,8,8,4]
my_start=0
my_end=0
for w in widths:
     my_end+=w
     print my_string[my_start:my_end] #or do whatever you want
     my_start+=w

rawDescription: Uneven character line split

description: Uneven character line split


code:
import myModule
pValue = myModule.doSomething()
print pValue

rawDescription: run a simple python script in ios

description: Run a simple python script in ios


rawDescription: super simple cipher function in python. comparing a list to keys in a dictionary

description: Super simple cipher function in python. comparing a list to keys in a dictionary


code:
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

from twisted.web.client import FileBodyProducer
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from pprint import pformat

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            print 'Some data received:'
            print display
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        self.finished.callback(None)

class SaveContents(Protocol):
    def __init__(self, finished, filesize, filename):
        self.finished = finished
        self.remaining = filesize
        self.outfile = open(filename, 'wb')

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            self.outfile.write(display)
            self.remaining -= len(display)
        else:
            self.outfile.close()

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        self.outfile.close()
        self.finished.callback(None)

agent = Agent(reactor)
f = open('70935-new_barcode.pdf', 'rb')
body = FileBodyProducer(f)
d = agent.request(
    'POST',
    'http://127.0.0.1:8080?filename=test.pdf',
    Headers({'User-Agent': ['Twisted Web Client Example'],
             'Content-Type': ['multipart/form-data; boundary=1024'.format()]}),
    body)

def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print 'Response length:', response.length
    print pformat(list(response.headers.getAllRawHeaders()))
    finished = Deferred()
    response.deliverBody(SaveContents(finished, response.length, 'test2.pdf'))
    return finished
d.addCallback(cbRequest)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

rawDescription: twisted get body of POST request

description: Twisted get body of POST request


code:
class A(object):
    def __enter__(self):
        print 'enter'
        # create database object here (or in __init__)
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        print 'exit'
        # save database object here

    # other methods

rawDescription: python : tracking change in class to save it at the end

description: Python : tracking change in class to save it at the end


code:
from nltk import trigrams

tgs = trigrams(text)
lake_tgs = filter(lambda item: item[1] == 'lake', tgs)

from collections import Counter

before_lake = map(lambda item: item[0], lake_tgs)
after_lake = map(lambda item: item[2], lake_tgs)

c = Counter(before_lake + after_lake)
print c.most_common()

rawDescription: How to find the most frequent words before and after a given word in a given text in python?

description: Find the most frequent words before and after a given word in a given text in python


code:
def pre_init_callback(sender, **kwargs):
    print 'pre_init', sender, kwargs
pre_init.connect(pre_init_callback)

def post_init_callback(sender, **kwargs):
    print 'post_init', sender, kwargs
post_init.connect(post_init_callback)

rawDescription: Does Django send the signal pre_init/post_init when instances are loaded by a QuerySet?


code:
def multiprocess(function, argslist, ncpu):
    total = len(argslist)
    done = 0
    result_queue = mp.Queue()
    jobs = []
    while argslist != [] and done<10 :
        if len(mp.active_children()) < ncpu:
            p = mp.Process(target=function,args=(result_queue, argslist.pop(),))
            jobs.append(p)
            p.start()
            done+=1
            print "\r",float(done)/total,"%",
    #get results here
    res = [result_queue.get() for p in jobs]
    print res

rawDescription: Python 2.7 multiprocessing get process result whithout using pool


code:
PROCESS_ID = 14766
cmdline = open("/proc/" + str(pid) + "/cmdline").read()
print cmdline.split("\0")

rawDescription: In Python, command line args without import?


code:
for span in soup.find('div', class_='views-row-1').find_all('span', recursive=False):
    print span.text.strip()

rawDescription: Extra newline character for children of Beautiful Soup


code:
a = np.array([1,2,3,1,2,1,1,1,3,2,2,1])
counts = np.bincount(a)
print np.argmax(counts)

rawDescription: find the most frequent number in a numpy vector

description: Find the most frequent number in a numpy vector


code:
import ast
def myFunc(myString, myList1, myList2):
    print myString, myList1, myList2

myFunc(*ast.literal_eval("('item 1', [1,2,3,4] , [4,3,2,1])"))

rawDescription: Read in tuple of lists from text file as tuple, not string - Python


code:
def func(a):
    a.append(1)
    a.append(2)
    a.append(3)

L=[]
func(L)
print L

rawDescription: Is there a way in Python to return value via output parameter?


code:
from lxml import etree
data = open('result.html','r').read()
doc = etree.HTML(data)

for tr in doc.xpath('//table/tr[@class="trmenu1"]'):
  print tr.xpath('./td/text()')

rawDescription: HTML Parsing using Python

description: HTML Parsing using Python


code:
import re
x = re.search('> *([^>]+) *</a>', country)
print x.group(1)

rawDescription: Splitting a html line to obtain an output


code:
import os, os.path, sys
import glob
from xml.etree import ElementTree

def run(files):
    xml_files = glob.glob(files +"/*.xml")
    xml_element_tree = None
    for xml_file in xml_files:
        data = ElementTree.parse(xml_file).getroot()
        # print ElementTree.tostring(data)
        for result in data.iter('results'):
            if xml_element_tree is None:
                xml_element_tree = data
                insertion_point = xml_element_tree.findall("./results")[0]
            else:
                insertion_point.extend(result)
    if xml_element_tree is not None:
        print ElementTree.tostring(xml_element_tree)

rawDescription: merging xml files using python's ElementTree

description: Merging xml files using python's ElementTree


code:
import itertools

for i in itertools.product(['a1','a2','a3'], ['b1','b2'],['c1','c2','c3','c4']):
    print '_'.join(i)

rawDescription: Find all combinations of N items in N groups without duplicates of item combos (python)?


code:
from mutagen.mp3 import MP3
audio = MP3("example.mp3")
print audio.info.length

rawDescription: python ringtone maker : how to split files

description: Python ringtone maker : how to split files


code:
for line in iter(sys.stdin.readline, ''):
    print "Echo:", line.decode('utf-8').strip()

rawDescription: Python read unicode stdin without batching


code:
import csv

st='''\
stuff, not, needed
#more stuff which is not needed
# even more stuff not needed
label,path,value
a,/path,1
b,/path,2'''

data=[]
tgt='label,path,value'
start=False
for line in csv.reader(st.splitlines()):
    if start:
        data.append(line)
    elif ','.join(e.strip() for e in line)==tgt:
        start=True
        data.append(line)

print data
# [['label', 'path', 'value'], ['a', '/path', '1'], ['b', '/path', '2']]

rawDescription: Use DictReader in Python to determine table header in CSV file


code:
args = ['firstargument', 'secondargument', '-s', 'thirdargument', '-s', 'fourth', 'fifth', '-s', 'sixth', 'seventh']
args1 = ['firstargument', 'secondargument', '-s', 'thirdargument', '-s', 'fourth', 'fifth', '-s', 'sixth', 'seventh', '-s']

marker = "-s"
def parser(arglist):
    scraplist, result = [], []
    arglist_range = range(len(arglist))
    #Remember candidates for exclude. It's your marker and previous element.
    for arg in arglist_range:
        if arglist[arg] == marker:
            scraplist.append(arg - 1)
            scraplist.append(arg)
    #Excluding marked elements
    for arg in arglist_range:
        if (not arg in scraplist):
            result.append(arglist[arg])
    return result

print parser(args)
print parser(args1)

rawDescription: Python - Need to get argument on command line


code:
from bs4 import BeautifulSoup
import urllib2
from lxml.html import fromstring
import re
import csv
import pandas as pd

wiki = "http://en.wikipedia.org/wiki/List_of_England_Test_cricket_records"
header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)

soup = BeautifulSoup(page)

table = soup.find_all('table')[6]

tmp = table.find_all('tr')

first = tmp[0]
allRows = tmp[1:-1]
#table.find_all('tr')[1:-1]

headers = [header.get_text() for header in first.find_all('th')]

results = [[data.get_text() for data in row.find_all('td')] for row in allRows]

#2=
# list of tuple (Level of tr, Level of td, total Count, Text Value)
#e.g.
#[(1, 0, 2, u'2=')]
# (
 is 1 , td sequence in tr is 0, reapted 2 times , value is 2=)
rowspan = []

for no, tr in enumerate(allRows):
    tmp = []
    for td_no, data in enumerate(tr.find_all('td')):
        print  data.has_key("rowspan")
        if data.has_key("rowspan"):
            rowspan.append((no, td_no, int(data["rowspan"]), data.get_text()))

if rowspan:
    for i in rowspan:
        # tr value of rowspan in present in 1th place in results
        for j in xrange(1, i[2]):
            #- Add value in next tr.
            results[i[0]+j].insert(i[1], i[3])

df = pd.DataFrame(data=results, columns=headers)
print df

rawDescription: What should I do when <tr> has rowspan

description: What should I do when <tr> has rowspan


code:
import numpy as np
from scipy.sparse import *

def method1():
    return np.array([np.bincount(ii, r) for r in a.T]).T

def method2():
    ares = a.reshape((-1,),order='F')
    col = np.tile(ii,(a.shape[1],))
    row = np.tile(np.arange(a.shape[1]),(a.shape[0],1)).reshape((-1,),order='F')

    return coo_matrix((ares,(col,row)),shape=(np.unique(ii).size,a.shape[1])).todense()

if __name__ == '__main__':
    from timeit import Timer

    a = np.random.randint(0,1000,(1000000,3))
    ii = np.random.randint(0,10,(a.shape[0],))

    N = 100
    t1 = Timer("method1()", "from __main__ import method1")
    t2 = Timer("method2()", "from __main__ import method2")
    print 't2/t1: %f' % (t2.timeit(N)/t1.timeit(N))

rawDescription: Using numpy.bincount with array weights

description: Using numpy.bincount with array weights


code: __builtin__.True = False

rawDescription: Why were True and False changed to keywords in Python 3


code:
from collections import defaultdict
with open('abc') as f:
    dic = defaultdict(dict)
    for line in f:
        cmp, val, col = line.split()
        dic[cmp][col] = val
print dic
# defaultdict(<type 'dict'>,
 #{'cmp1': {'val_5': '0.127', 'val_4': '0.809', 'val_1': '0.277', 'val_3': '0.795', 'val_2': '0.097'},
 # 'cmp2': {'val_5': '0.148', 'val_4': '0.909', 'val_7': '0.599', 'val_6': '0.938', 'val_3': '0.839'}})

#get a sroted list of all val_i from the dic
vals = sorted(set(y for x in dic.itervalues() for y in x))

keys = sorted(dic)
print "name    {}".format("\t".join(vals))
for key in keys:
    print "{}    {}".format(key, "\t".join(dic[key].get(v,'0')  for v in vals)  )

rawDescription: Creating fingerprint file from flat file in python


code:
def get(x):
    return (x * 10) % 10
x = - 15.6
print get(abs(x))

rawDescription: How to get the first digit of the fractional part of a decimal number?

description: Get the first digit of the fractional part of a decimal number


code:
with open("test.csv") as f:
    records = csv.DictReader(f)
    for row in records:
         print row

rawDescription: Python writing a csv to dictionary with headers as keys and rows as values


code:
import ctypes, _ctypes

def castArgToCtype(arg, ctyp):
    if issubclass(ctyp, _ctypes._Pointer): return createPtr(arg, ctyp)
    return ctyp(arg)

def createPtr(arg, ctyp):
    assert isinstance(arg, (list,tuple))
    assert issubclass(ctyp, _ctypes._Pointer)
    o = (ctyp._type_ * (len(arg) + 1))()
    for i in xrange(len(arg)):
        o[i] = castArgToCtype(arg[i], ctyp._type_)
    op = ctypes.pointer(o)
    op = ctypes.cast(op, ctyp)
    return op

a = createPtr((1,2,3), ctypes.POINTER(ctypes.c_int))
print a, a[0], a[1], a[2], a._objects

rawDescription: Python: ctypes pointer to local variables; how to ensure they don't get freed?


code:
# configuration dict, written by the user
categories = {
    'apple'  : [1, 2],
    'banana' : [3],
    'melon'  : [4, 5, 6]
}

# dynamically generate lookup table
lookup = {}
for cat, nums in categories.iteritems():
    for n in nums: lookup[n] = cat

# ... later:
from collections import defaultdict
categorized = defaultdict(list)
for n in numbers:
    cat = lookup.get(n)
    if not cat:
        continue
    categorized[cat].append(n)
    print "put %d in category %s" % (n, cat)

rawDescription: Categorize a number

description: Categorize a number


code:
for f in ["/tmp/foo.txt", "./foo.txt"]:
    try:
        foo = open(f)
    except:
        pass
    else:
        print foo.read()

rawDescription: Idiom in python for opening second file when the first file does not exist


code:
import ctypes
import sys
from sys import platform as _platform

if _platform == "linux" or _platform == "linux2":
    _libm = ctypes.cdll.LoadLibrary('libm.so.6')
    _funcname = 'nextafter'
elif _platform == "darwin":
    _libm = ctypes.cdll.LoadLibrary('libSystem.dylib')
    _funcname = 'nextafter'
elif _platform == "win32":
    _libm = ctypes.cdll.LoadLibrary('msvcrt.dll')
    _funcname = '_nextafter'
else:
    # these are the ones I have access to...
    # fill in library and function name for your system math dll
    print "Platform", repr(_platform), "is not supported"
    sys.exit(0)

_nextafter = getattr(_libm, _funcname)
_nextafter.restype = ctypes.c_double
_nextafter.argtypes = [ctypes.c_double, ctypes.c_double]

def nextafter(x, y):
    "Returns the next floating-point number after x in the direction of y."
    return _nextafter(x, y)

assert nextafter(0, 1) - nextafter(0, 1) == 0
assert 0.0 + nextafter(0, 1) > 0.0

rawDescription: Increment a python floating point value by the smallest possible amount


code:
import sys
import tempfile
new_list = sorted(sys.argv[1:])
with tempfile.NamedTemporaryFile('w', delete=False) as f:
    f.write('\n'.join(new_list))
    print f.name

rawDescription: Transfering arguments in Automator's workflow . Python Shells


code:
import os, time
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print "last modified: %s" % time.ctime(mtime)

rawDescription: How to get file creation & modification date/times in Python?

description: Get file creation & modification date/times in Python


code:
from collections import namedtuple
Security = namedtuple('Security', ['read','write','execute'])
policy_string = 'YYN'   # read = Yes, write = Yes, execute = No
policy = Security(*[c for c in policy_string])
print policy

rawDescription: Python - Get Multiple Characters From String


rawDescription: Python Regex don't match if word in line


code:
print re.findall(r"^(?!\w+-\s+[^\n]+$).+$", text, re.MULTILINE)
['hello bye']

rawDescription: How to find lines that do not match pattern in a multiline block?

description: Find lines that do not match pattern in a multiline block


code:
def onclick(event):
    print event.__class__
    wedge = event.artist
    label = wedge.get_label()
    print label
    fig.canvas.figure.clf()
    ax=fig.add_subplot(111)
    wedges, plt_labels = ax.pie([50, 100, 60],labels=labels)
    fig.canvas.draw()

rawDescription: Unusual behaviour of pick_event in a matplotlib


code:
def part1():
    print "part1 expensive computation running"
    return "This was expensive to compute"

def part2(value):
    print "part2 running with %s" % value

rawDescription: Keep persistent variables in memory between runs of Python script


rawDescription: Depth First Search in Python (including cycles)


code:
att = {
    "roll": attRoll,
    "hit": hit,
    "dmg": dmg
}
print att["dmg"]

rawDescription: python list of variables, retrieve value of variable by variable name

description: Python list of variables, retrieve value of variable by variable name


code:
import numpy as np, time

start = time.clock()

count = 0
iters = 1000000 # 1million

l=10
k=10
l0=l+k-1

N = 4 # number of millions

for n in range(N):
    t = np.random.choice([-1,1], size=l0 * iters)
    v = np.random.choice([-1,1], size = l * iters)

    for i in xrange(iters):
        if (not np.convolve(v[(l*i):(l*(i+1))], t[(l0*i):(l0*(i+1))], 'valid').any()):
            count += 1

print (time.clock() - start)

print count

rawDescription: How to get the hardest part to take longest in python

description: Get the hardest part to take longest in python


code:
while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(20) & 0xFF
    if k == 27:
        break
    elif k == ord('a'):
        print mouseX,mouseY

rawDescription: Opencv: detect mouse position clicking over a picture


code:
# check if the file does not already exist
if not os.path.isfile(savepath+"/"+fname):
    # open a file to save the data to ("wb" means write binary mode)
    outfile = open(savepath+"/"+fname, "wb");
    # download the data from the url specified above
    infile = urllib2.urlopen(url);
    # read the downloaded data and write it to our output file
    outfile.write(infile.read());
    # close the output file once we're done
    outfile.close();
else:
    print "'"+fname+"' ALREADY EXISTS in the save directory '"+savepath+"'.";

rawDescription: How to download and rewrite a file in Python?

description: Download and rewrite a file in Python


code:
if not lis.endswith('.zip'):
   print lis

rawDescription: I have a list of strings that end with different extensions how would I tell Python to not print the .zip strings


rawDescription: Every possible combination of a multi-list


code:
mylist = ['var1', 'var2', 'var3_something', 'var1_text', 'var1var1']
import re
r = re.compile(r'^var1$')
matches = [item for item in mylist if r.match(item)]
print matches

rawDescription: Regular expression in python - help needed


code:
#!/usr/bin/env python
from ftplib import FTP
from StringIO import StringIO

ftp = FTP('ftp.kernel.org')
ftp.login()
r = StringIO()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)

print r.getvalue()

rawDescription: Is it possible to read FTP files without writing them using Python?


code:
import operator
ranges=[[0,1,2,3,4], [1,2], [0,1], [2,3,4], [3,4,5], [3,4,5,6], [4,5], [6,7], [5,6]]
sorted_ranges = sorted(ranges,key=operator.itemgetter(-1),reverse=True)
sorted_ranges = sorted(sorted_ranges,key=operator.itemgetter(0))

filtered = []
i,j = 0,0

while i < len(sorted_ranges):
    filtered.append(sorted_ranges[i])
    j = i+1
    while j < len(sorted_ranges) and sorted_ranges[i][-1] >= sorted_ranges[j][-1]:
        print "Remove " , sorted_ranges.pop(j) , "dominated by",sorted_ranges[i]
    i+=1

print "RESULT",filtered

rawDescription: Filtering a list of integer in range, to exclude the subsets in python


code:
show = {
        'episodes': [
                    {'episode': 0, 'season': 1}, {'episode': 1, 'season': 1}, {'episode': 2, 'season': 1},
                    {'episode': 0, 'season': 2}, {'episode': 1, 'season': 2}, {'episode': 2, 'season': 2},
                ],
            'imdbnumber': '260449', 'title': 'Vikings'
        }

trakt_show = {'ids':{}}

if 'imdbnumber' in show:
    if show['imdbnumber'].startswith('tt'):
        trakt_show['ids']['imdb'] = show['imdbnumber']
    else:
        trakt_show['ids']['tvdb'] = show['imdbnumber']
try:
    trakt_show['ids']['tvdb'] = show['tvdb']
except:
    pass
try:
    trakt_show['ids']['imdb'] = show['imdb']
except:
    pass
try:
    trakt_show['title'] = show['title']
except:
    pass

if 'episodes' in show and show['episodes']:
    ep = {}
    for episode in show['episodes']:
        try:
            ep[episode["season"]].append(episode["episode"])
        except:
            ep[episode["season"]] = [episode["episode"]]

    ep1 = {"seasons":[]}
    for i in ep:
        tmp = dict()
        tmp["number"] = i
        tmp["episodes"] = []
        for j in ep[i]:
            tmp["episodes"].append({"number":j})
        ep1["seasons"].append(tmp)
    trakt_show.update(ep1)
print trakt_show

rawDescription: Update dict with extra values

description: Update dict with extra values


code:
def function_writer():
    print "I am a function writer"

    def new_function():
        print "I am a new function"

    return new_function

fn_dynamic = function_writer()
fn_dynamic() #call new_function

rawDescription: Python: defining a function inside of a function


code:
import re

log = 'loss of gene'
phrases = ['aquaporin protein', 'another protein']

with open('input.txt') as f_input:
    for number, line in enumerate(f_input, start=1):
        # Sanitise the line
        text = ' '.join(re.findall(r'\b(\w+)\b', line.lower()))

        # Only process lines containing 'loss of gene'
        log_index = text.find(log)

        if log_index != -1:
            for phrase in phrases:
                phrase_index = text.find(phrase)

                if phrase_index != -1:
                    if log_index < phrase_index:
                        start, end = (log_index + len(log), phrase_index)
                    else:
                        start, end = (phrase_index + len(phrase), log_index)

                    if len(text[start:end].split()) <= 5:
                        print "line {} matched - {}".format(number, phrase)
                        break

rawDescription: 'difficult' Determine proximity between 2 strings in python


code:
for l in open("/proc/stat"):
  l = l.split()
  if l[0] == 'procs_running':
    result = int(l[1])

print result

rawDescription: Get load at a moment in time or getloadavg() for a smaller time period in Python in Linux (CentOS)


code:
def compare_json(x,y):
    for x_key in x:
        if x_key in y and x[x_key] == y[x_key]:
            print 'Match'
        else:
            print 'Not a match'
    if any(k not in x for k in y):
        print 'Not a match'

rawDescription: Comparing python dictionaries and find diffrence of the two


code:
for i in range(5):
    print i

rawDescription: How does the Python's range function work?


code:
import  numpy as np
import ctypes as C
import pefile

def unprotect( address):
    crap = C.byref(C.create_string_buffer("\x00"*4))
    res = C.windll.kernel32.VirtualProtect(address, 0x1000, 0x40, crap)

lib = C.cdll.LoadLibrary('pycon.exe')
k32 = C.windll.kernel32
pe =  pefile.PE('pycon.exe')

for entry in pe.DIRECTORY_ENTRY_IMPORT:
  #print entry.dll
  #assuming that the exe only has dependency on kernel32
  for imp in entry.imports:
    diff = imp.address-0x400000
    memptr = k32.GetProcAddress(k32._handle,imp.name)
    unprotect(lib._handle+diff)
    fptr = (C.c_uint).from_address(lib._handle+diff)
    fptr.value = memptr

d = np.ones((5)).astype('double')
lib.init()
lib.fun(d.ctypes.data_as(C.POINTER(C.c_double)), C.c_size_t(5))
print d

rawDescription: How to initialize CRT when doing LoadLibrary("*.exe")

description: Initialize CRT when doing LoadLibrary("*.exe")


code:
func_list=[locals()[key] for key in locals().keys()
           if callable(locals()[key]) and key.startswith('time')]

alist=range(1000000)
times=[]
for f in func_list:
    n = 10
    times.append( min(  t for t,_,_ in (f(alist,31) for i in range(n))))

for (time,func_name) in zip(times, func_list):
    print '%s took %0.3fms.' % (func_name, time*1000.)

rawDescription: timeit versus timing decorator

description: Timeit versus timing decorator


code:
x = full_name.index(" ")
new_string = full_name[x+1:] + " " + full_name[0:x]
print new_string

rawDescription: Python & Strings/Loops help please

description: Python & Strings/Loops help please


code:
#daterange_filter/filter.py
class DateBookFilter(admin.filters.FieldListFilter):
    template = 'daterange_filter/filter2.html'

    def __init__(self, field, request, params, model, model_admin, field_path):
        self.parameter_name = field.name
        self.lookup_kwarg_since = '%s__gte' % self.parameter_name
        self.lookup_kwarg_upto = '%s__lte' % self.parameter_name
        super(DateBookFilter, self).__init__(
            field, request, params, model, model_admin, self.parameter_name)
        self.form = self.get_form(request)

    def choices(self, cl):
        return []

    def expected_parameters(self):
        return [self.lookup_kwarg_since, self.lookup_kwarg_upto]

    def get_form(self, request):
        print self.used_parameters
        return DateRangeForm(data=self.used_parameters,
                             field_name=self.parameter_name)

    def queryset(self, request, queryset):
        if self.form.is_valid():
            # get no null params
            filter_params = dict(filter(lambda x: bool(x[1]),
                                        self.form.cleaned_data.items()))
            from_date = self.form.cleaned_data['%s__gte' % self.parameter_name]
            to_date = self.form.cleaned_data['%s__lte' % self.parameter_name]
            #If none are selected, we return the queryset without filtering
            if from_date is None and to_date is None:
                return queryset
            #if "From" is empty we will look for records before "To"
            if from_date is None:
                return queryset.filter(shelf__book__date__lt=to_date).order_by('-shelf__book__date')
            #if "To" is empty we will look for records after "From"
            if to_date is None:
                return queryset.filter(shelf__book__date__gt=from_date).order_by('-shelf__book__date')
            #if none of the above then both fields are set so we look between "From" and "To"
            else:
                return queryset.filter(shelf__book__date__range=(from_date,to_date)).order_by('-shelf__book__date')
        else:
            return queryset

admin.filters.FieldListFilter.register(
   lambda f: isinstance(f, models.DateField), DateCalibrationFilter)

rawDescription: Filter range date from custom property using SimpleListFilter


code:
import numpy.random as nprnd
from pandas import DataFrame
import pandas as pd

n = 10 # Number of samples
# Starting at 8:00 AM, set some (n) random prices between 4-5
df = DataFrame({'minute_id': range(480,480+n), 'price':(5-4) * nprnd.random(n) + 4 })
df['change'] = df.price - df.price.shift(1)
df['direction'] = df.change.map(lambda x: 0 if x == 0 else x/abs(x))
df = df.dropna()
#------------------------------------------
# Col 1, rolling Avg over the entire DF
df['rolling_avg'] = pd.rolling_mean(df.price, n, 1)
#------------------------------------------
# Col 4, rolling Avg windows size of 4
df['RA_wnd_4'] = pd.rolling_mean(df.price, 4, 1)
#------------------------------------------
# Helper code for cols 2, 3
# Adding Helper column that shows when direction have been changed
df['dir_change'] = (df.direction.shift(1) != df.direction).astype(int)
# Identify the DF "blocks" for every direction change
df['block'] = df.dir_change.cumsum()
# Split the DF based on those bolcks
grouped = df.groupby('block')
# Add Function that will cumsum() for a block, and call it
def f1(group):
     return DataFrame({'rolling_count' : group.cumsum()})
df['one'] = 1
#------------------------------------------
# Col 2, CumSum() of the 'change' column while in the current "blcok" (direction)
df['rolling_count'] = grouped.change.apply(f1)
#------------------------------------------
# Col 3, Count in the current "block" (Direction)
df['rolling_count'] = grouped.one.apply(f1)
df = df.drop('one', axis=1)

print df

rawDescription: Python and Pandas - column that "count direction" and show "avarage until now"


code:
#module "foo"
SANITIZE='foo'
def parse():
    print SANITIZE

class Parser(object):
    def __init__(self):
        print SANITIZE * 2

rawDescription: Alter module variables in one module while it doesn't change in the original


code:
import sys
import re
import csv

input = 'foo blah 38902462986.328946239846'

reader = csv.reader(open('test.csv','rb'), delimiter=',', quotechar="\"")
for row in reader:
    value = row[0]
    if value in input:
        regex = row[2]
        test = re.search(regex, input)
        print input[test.start():test.end()]

rawDescription: Python: Using regex stored in CSV

description: Python: Using regex stored in CSV


code:
tt = defaultdict(set)
for e, s in [i.split('-') for i in trans.split(' ')]:
    tt[int(e)].add(int(s))

query = 'this'
for i in tt[eng_words.index(query)]:
    print i

rawDescription: How to extract matching strings into a defaultdict(set)? Python

description: Extract matching strings into a defaultdict(set)? Python


code:
import collections

data = [
  [1, 2, 3, 4, 5],
  [1, 9, 3, 4, 5],
  [1, 10, 8, 4, 5],
  [1, 12, 13, 7, 5],
  [1, 14, 13, 13, 6],
]

def sorted_by_count(lists):
  counts = collections.defaultdict(int)
  for L in lists:
    for n in L:
      counts[n] += 1

  return [num for num, count in
          sorted(counts.items(),
                 key=lambda k_v: (k_v[1], k_v[0]),
                 reverse=True)]

print sorted_by_count(data)

rawDescription: Ranking Elements of multiple Lists by their count in Python


code:
links = soup.find_all('a', href=re.compile('^/on/demandware.store/Sites-BNY-Site/default/Product-Variation'))
for link in links[1:]:
    print link['href']

rawDescription: Extract single href from a web page

description: Extract single href from a web page


code:
# changed set to list
result = list()
for ticket in tickets:
# to get rid of the "None" priorities
    if ticket.priority != '<pirority range>':
        print ""
    else:
        #notice the change below
        result.append(ticket)

# Oscar's solution:
minTicket = min(result, key=lambda val : val.priority)

print minTicket.priority
print minTicket.code

rawDescription: Keeping Values connected when sorting results of a for loop


rawDescription: date and time to just time in python

description: Date and time to just time in python


code:
def overunder(n):
  over_count = 0
  under_count = 0
  push_count = 0
  overodds = 0
  underodds = 0
  for i in range(n):
    if round(np.random.normal(pjTotal3s,pjGame3STD)) > threes_over:
      over_count = over_count + 1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) < threes_over:
      under_count = under_count +1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) == threes_over:
      push_count = push_count + 1

  over = 1/(over_count / float(over_count + under_count))
  under = 1/ (under_count / float(over_count + under_count))
  over_odds_given = 1.77
  under_odds_given = 2.05
  overedge = 1/over * over_odds_given - 1
  underedge = 1/under * under_odds_given - 1

  print overedge, underedge

  return over_count, under_count, push_count

rawDescription: improving a function in python

description: Improving a function in python


code:
x = 5
while True:
  if (some-condition):
    x = 3
  print x

rawDescription: change value when python is in infinite loop

description: Change value when python is in infinite loop


code:
sam2_identifiers = set()
for line in reader2:
    sam2_identifiers.add(line[0])

print sam1 - sam2

rawDescription: Python: Fastest way of parsing first column of large table in array


code:
class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = name
    def talk(self):              # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")

class Cat(Animal):
    def talk(self):
        return 'Meow!'

class Dog(Animal):
    def talk(self):
        return 'Woof! Woof!'

animals = [Cat('Missy'),
           Cat('Mr. Mistoffelees'),
           Dog('Lassie')]

for animal in animals:
    print animal.name + ': ' + animal.talk()

# prints the following:
#
# Missy: Meow!
# Mr. Mistoffelees: Meow!
# Lassie: Woof! Woof!

rawDescription: Practical example of Polymorphism

description: Practical example of Polymorphism


code: print dict(customers)

rawDescription: Populating a Python dictionary

description: Populating a Python dictionary


code:
#! /usr/bin/env python

''' Merge lists in a dict

    Lists are merged if they have any element in common,
    so that in the resulting dict no list element will be
    associated with more than one key.

    Written by PM 2Ring 2014.11.18

    From http://stackoverflow.com/q/26972204/4014959
'''

#Some test data
groups = {
    'g01': ['a', 'b', 'c', 'd'],
    'g02': ['a', 'b', 'c', 'd', 'e'],
    'g03': ['f', 'g', 'h'],
    'g04': ['g', 'j'],
    #'g05': ['g', 'a'],
    'g06': ['k', 'l'],
    'g07': ['l', 'm'],
    'g08': ['m', 'n'],
    'g09': ['o', 'p'],
    'g10': ['p', 'q'],
    'g11': ['q', 'o'],
    #'g12': ['l', 'q'],
}

def merge_lists(d):
    src = dict((k, set(v)) for k, v in d.iteritems())

    while True:
        dest = {}
        count = 0
        while src:
            k1, temp = src.popitem()
            if temp is None:
                continue
            for k2, v in src.iteritems():
                if v is None:
                    continue
                if temp & v:
                    temp |= v
                    src[k2] = None
                    count += 1
                    k1 = min(k1, k2)
            dest[k1] = temp

        if count > 0:
            #print count
            #print_dict(dest)
            src = dest
        else:
            dest = dict((k, sorted(list(v))) for k, v in dest.iteritems())
            return dest

def print_dict(d):
    for k in sorted(d.keys()):
        print "%s: %s" % (k, d[k])
    print

def main():
    print_dict(groups)
    print 20*'-'

    dest = merge_lists(groups)
    print_dict(dest)

if __name__ == '__main__':
    main()

rawDescription: Combine dictionary entries by common elements


code: print [parse_date(date) for key in My_Dict for data in My_Dict[key]["Dates"] for date in data]

rawDescription: Loop through all items in a list of list embedded in dictionary


code:
import keyword
for keyword in keyword.kwlist:
    print keyword

rawDescription: Python, different types of keywords

description: Python, different types of keywords


code:
count = np.bincount(labels)
x = np.bincount(labels, X[:, 0])
y = np.bincount(labels, X[:, 1])

centroids = np.c_[x, y] / count[:, None]
print centroids

rawDescription: Is there a way to vectorize this loop

description: Is there a way to vectorize this loop


code:
import array
print array.array('B', "ABCD")

rawDescription: Python: convert string to byte array

description: Python: convert string to byte array


code:
import urllib, collections
txt = urllib.urlopen("http://www.blahblahblah.com").read()

tally = collections.Counter(txt)

def freq_threshold(char, n):
    if tally[char] > n:
        return tally[char]

print freq_threshold('b', 3)

rawDescription: What is my function missing? (python)

description: What is my function missing? (python)


code:
df[4] = df[4].str.split(':').str.get(0)
df[6] = df[6].str.split(':').str.get(0)
print df.to_string()

rawDescription: Calcuate Duration and Average of CSV file data using python


code: print "Estimated Time: %s" % datetime.timedelta(seconds = est)

rawDescription: Data and time conversion - PyMathematics


code:
str1 = 'abc'
str2 = 'def'
print str1 + str2
print repr(str1 + str2)

rawDescription: How can I concatenate ' in a string using python

description: Concatenate ' in a string using python


code:
from time import sleep
import sys
print "sub3 output: start, sleeping 15 sec"
sys.stdout.flush()
sleep(15)
print "sub3 output: finish"

rawDescription: Custom Scheduler to have sequential + semi-sequential scripts with timeouts/kill switches?


code:
RNA = 'AGCCAUGUAGCUAACUCAGGUUACAUGGGGAUGACCCCGCGACUUGGAUUAGAGUCUCUUUUGGAAUAAGCCUGAAUGAUCCGAGUAGCAUCUCAG'
candidates = []
start = 0

while (RNA.find('AUG', start) > -1):
    start = RNA.find('AUG', start) #Confound python and its lack of assignment returns
    candidates.append((RNA[start+3:], start+3))
    start += 1

matches = []

for candidate in candidates:
    for terminator in ['UAG', 'UGA', 'UAA']:
        end = 1;
        while(candidate[0].find(terminator, end) > -1):
            end = candidate[0].find(terminator, end)
            matches.append((candidate[1], candidate[1] + end, candidate[0][:end]))
            end += 1

for match in matches:
    print "%d - %d: %s" % match

rawDescription: Overlapping regex matches

description: Overlapping regex matches


code:
import re
s = "The quick, brown fox jumps over the lazy dog."

triplets = re.findall(r"\b(?=((?:\w+(?:\W+|$)){3}))", s)

print triplets
# ['The quick, brown ', 'quick, brown fox ', 'brown fox jumps ',
#  'fox jumps over ', 'jumps over the ', 'over the lazy ', 'the lazy dog.']

print triplets[3]
# fox jumps over

rawDescription: string mask and offset with regex

description: String mask and offset with regex


code:
@memoize
def foo(bar):
    return bar ** 3

print memoize.decorated

rawDescription: How to register within a decorator all functions it decorates?

description: Register within a decorator all functions it decorates


code:
class memoize_by_bar(key_memoized):
    def key(self, args, kwargs):
        return self.normalize_args(args, kwargs)['bar']

@memoize_by_bar
def foo(bar, baz, spam):
    print 'calling foo: bar=%r baz=%r spam=%r' % (bar, baz, spam)
    return bar

print foo('x', 'ignore1', 'ignore2')
print foo('x', 'ignore3', 'ignore4')

rawDescription: Is there a pythonic way to support keyword arguments for a memoize decorator in Python?


code:
def from_redis(query, meth):
    for server in ['r1', 'r2']:
        try:
            return meth(q.options(FromCache(server)))
        except Exception, e:
            continue
    else:
        raise e

rawDescription: Abstracting away boiler plate sqlalchemy call to allow different options in a try/except block


code:
import subprocess

def runProcess(exe):
    p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while(True):
      retcode = p.poll() #returns None while subprocess is running
      line = p.stdout.readline()
      yield line
      if(retcode is not None):
        break

for line in runProcess(['./test.sh']):
  if line.startswith('GARYFIXLER:'):
      print line,

rawDescription: how can I silence all of the output from a particular python command?

description: Silence all of the output from a particular python command


code:
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()

rawDescription: subprocess.Popen in different console

description: Subprocess.Popen in different console


rawDescription: How to access key in Python dictionary

description: Access key in Python dictionary


code:
class Foo(object):
    @hybridmethod
    def bar(cls_or_self):
        print 'Called with cls_or_self={!r}'.format(cls_or_self)

Foo.bar()
Foo().bar()

rawDescription: python: hybrid between regular method and classmethod

description: Python: hybrid between regular method and classmethod


code:
#!/usr/bin/env python
"""It is a `filename -> filename.ext` filter.

   `ext` is mime-based.

"""
import fileinput
import mimetypes
import os
import sys
from subprocess import Popen, PIPE

if len(sys.argv) > 1 and sys.argv[1] == '--rename':
    do_rename = True
    del sys.argv[1]
else:
    do_rename = False

for filename in (line.rstrip() for line in fileinput.input()):
    output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate()
    mime = output.split(';', 1)[0].lower().strip()
    ext = mimetypes.guess_extension(mime, strict=False)
    if ext is None:
        ext = os.path.extsep + 'undefined'
    filename_ext = filename + ext
    print filename_ext
    if do_rename:
       os.rename(filename, filename_ext)

rawDescription: How to add file extensions based on file type on Linux/Unix?

description: Add file extensions based on file type on Linux/Unix


code:
with open("test.txt") as fp:
    lines = fp.readlines()
    print dict((x.split(';')[1][:-1], x.split(';')[0]) for x in lines)

rawDescription: Create array from text file using the input as keys


code:
import string
from nltk.corpus import LazyCorpusLoader, CategorizedPlaintextCorpusReader
from nltk.corpus import stopwords
my_movie_reviews = LazyCorpusLoader('my_movie_reviews', CategorizedPlaintextCorpusReader, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
mr = my_movie_reviews
stop = stopwords.words('english')
documents = [([w for w in mr.words(i) if w.lower() not in stop and w.lower() not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]
for i in documents:
    print i

rawDescription: Using my own corpus instead of movie_reviews corpus for Classification in NLTK


code: print is_prime(5003)

rawDescription: isPrime Function for Python Language

description: IsPrime Function for Python Language


code:
from StringIO import StringIO
import pandas as pd

f = StringIO("""A    B    C    D
X  5    4    3    2
Y  1    0    9    9
Z  8    7    6    5""")
x = pd.read_csv(f, delim_whitespace=True, index_col=0)

print 'The DataFrame:'
print x

print 'Selecting a column'
print x['D'] # or "x.D" if there aren't spaces in the name

print 'Selecting a row'
print x.loc['Y']

rawDescription: loading a table in numpy as in R?

description: Loading a table in numpy as in R


code:
for dirpath, dirnames, filenames in os.walk("/folder/test"):
    for file in filenames:
        if fnmatch.fnmatch(file, 'text*'):
            print file

rawDescription: Recursively find files in python

description: Recursively find files in python


rawDescription: Writing to a specific column of a text file in python


code:
for i in b:
    if i in a:
        print '%d in both sets!' %(i)
    else:
        print '%d does not match!' %(i)

rawDescription: Compare unequal lists

description: Compare unequal lists


code:
class Frame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent)
        self.Bind(wx.EVT_COLOURPICKER_CHANGED, self.on_event)
        self.Centre()
        self.Show(True)
        self.i = 0
        self.recalculating = False
        self.event_all()

    def on_event(self, event):
        self.recalculating = False
        print self.i
        self.i += 1

    def event_all(self):
        for j in range(1000):
            if not self.recalculating:
                event = wx.PyEvent()
                event.SetEventType(wx.EVT_COLOURPICKER_CHANGED.evtType[0])
                wx.PostEvent(self, event)
                self.recalculating = True

rawDescription: wxWidgets/wxPython: Do two identical events cause two handlings?

description: WxWidgets/wxPython: Do two identical events cause two handlings


code: print " ".join([ correct(i) for i in words ])

rawDescription: Python Converting output into sentence

description: Python Converting output into sentence


code: sorted(mydict.iteritems(), key=lambda (k,v): operator.itemgetter(1)(v))

rawDescription: Sorting dictionary using operator.itemgetter


rawDescription: How to format duration in Python (timedelta)?

description: Format duration in Python (timedelta)


code:
def MyDecorator(f):
    def wrapper(*args):
        print args[0].x
        return f(*args)
    return wrapper

class C(object):
    def __init__(self):
        self.x = 1
    @MyDecorator
    def do(self):
        print 'do'

c = C()
c.do()

rawDescription: passing class variables to decorator

description: Passing class variables to decorator


code:
str_display = '\n'.join(' '.join(lst) for lst in strlist)
print str_display

rawDescription: in Python, how to convert list of float numbers to string with certain format?

description: In Python, how to convert list of float numbers to string with certain format


code:
def write_cols(data):
    col_spacer = "   "      # added between columns
    widths = [0] * len(data[0])

    # Calculate the widest entry for each column
    for row in data:
        widths[:] = [max(widths[index], len(str(col))) for index, col in enumerate(row)]

    return [col_spacer.join("{:<{width}}".format(col, width=widths[index]) for index, col in enumerate(row)) for row in data]

data = [['', 'Config 1', '', 'Config 2', ''], ["Test", "Data 1", "Data 2", "Data 1", "Data 2"], ["abc", "123", "123", "123", "123"]]

for row in write_cols(data):
    print row

rawDescription: Python table with multiple header lines

description: Python table with multiple header lines


code:
bucket = s3.Bucket('bucket_name')
for obj in bucket.objects.all():
    file_contents = obj.get()["Body"].read()
    print file_contents.count('\n')

rawDescription: Is it possible to loop through Amazon S3 bucket and count the number of lines in its file/key using Python?


code: print rdata[Pdata < 0.05]

rawDescription: comparing/extracting data from matrices using python (2.6.1)

description: Comparing/extracting data from matrices using python (2.6.1)


code:
import datetime

class LogEntry(object):
    @staticmethod
    def from_str(line):
        """Converts a log line in a string, into a LogEntry."""
        # split the line by one or more spaces
        date, time, _, severity, filename, _, line, message = re.split('\s+', line, 7)

        # strip the trailing bracket on the line and cast to an int
        line = int(line[:-1])

        # combine the date and time strings and turn them into a datetime
        dt = datetime.datetime.strptime(date + time, "%Y-%m-%d%H:%M:%S,%f")

        return LogEntry(dt, severity, filename, line, message)

    def __init__(self, dt, severity, filename, line_num, message):
        self.datetime = dt
        self.severity = severity
        self.filename = filename
        self.line_num = line_num
        self.message = message

    def __str__(self):
        return '%s %s %s L%s: %s' % (self.datetime, self.severity, self.filename, self.line_num, self.message)

if __name__ == '__main__':

    log_contents = """2012-06-12 14:02:21,813 [main]  INFO  ConnectionManager.java (line 238) Initializing the ConnectionManager.
    2012-06-12 14:02:21,844 [main]  INFO  CimListener.java (line 142) Starting listener at http://127.0.0.1:7012
    2012-06-12 14:02:21,974 [main]  INFO  CimListener.java (line 158) Listening at http://127.0.0.1:7012
    2012-06-12 14:02:23,209 [main]  INFO  RmiServiceExporter.java (line 393) Looking for RMI registry at port '10099'
    2012-06-12 14:02:23,232 [main]  INFO  RmiServiceExporter.java (line 404) Could not detect RMI registry - creating new one"""

    # uncomment for real log reading
    #fh = file(filename, 'r')

    # emulate a log file by providing an iterable of lines (just like fh will do)
    fh = log_contents.split('\n')

    for line in fh:
        print LogEntry.from_str(line.strip())

rawDescription: Class Formatting Problems

description: Class Formatting Problems


code:
xml = """<Content>
  <Para>first</Para>

<Para>second</Para>

  <Para>third</Para>
</Content>"""
import xml.etree.cElementTree as et
page = et.fromstring(xml)
for p in page.getiterator():
    print "ppp", p.tag, repr(p.text)
    for c in p:
        print "ccc", c.tag, repr(c.text), p.tag

rawDescription: Iterating multiple (parent,child) nodes using Python ElementTree


code:
data = collections.defaultdict(int)
print data['5/15/2015 17:00:00']

rawDescription: Is there a way to "fill in" a series of datetime stamps?


code:
s = "classes.students.grades"
print '.'.join(s.split('.')[:-1])

rawDescription: Dot notation string manipulation

description: Dot notation string manipulation


code:
from IPython import embed

for item in range(5):
    print 'embedding', item
    embed()

rawDescription: Can I make ipython exit from the calling code?

description: Make ipython exit from the calling code


code:
python
import json
r = request.body # receives request from php
json = json.loads(r)
print json[0] # [1,2,3]

rawDescription: How to Send 2D array through php cURL

description: Send 2D array through php cURL


code:
import re
pattern = re.compile(r'\d+\/\d+')
matches = re.search(pattern, div.text)
num = matches.group(0) # num = 1/16 here
print num.split('/')[1]

rawDescription: python beautifulsoup get footer from chinese website

description: Python beautifulsoup get footer from chinese website


code:
from itertools import count, groupby
L=[1, 2, 3, 4, 6, 7, 8, 9, 12, 13, 19, 20, 22, 23, 40, 44]
G=(list(x) for _,x in groupby(L, lambda x,c=count(): next(c)-x))
print ",".join("-".join(map(str,(g[0],g[-1])[:len(g)])) for g in G)

rawDescription: Pythonic way to convert a list of integers into a string of comma-separated ranges


code:
import os
import subprocess

## test data
databaseFileSpec = r"Z:\pyTest.mdb"

vbsFileSpec =  os.environ['TEMP'] + r"\mypytemp.vbs"

scriptCode = """Option Explicit
Dim con, rst, strOut, strSeparator
Const adSchemaProviderSpecific = -1
Set con = CreateObject("ADODB.Connection")
con.Open( _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source="""
scriptCode += databaseFileSpec
scriptCode += """")
Set rst = con.OpenSchema( _
        adSchemaProviderSpecific, _
        , _
        "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
strOut = ""
strSeparator = ""
Do While Not rst.EOF
    If rst(2).Value = "True" Then
        strOut = strOut & strSeparator & Left(rst(0).Value, Len(Trim(rst(0).Value)) - 1)
        strSeparator = vbTab
    End If
    rst.MoveNext
Loop
WScript.Echo strOut
rst.Close
con.Close"""

f = open(vbsFileSpec, 'w')
f.write(scriptCode)
f.close()

tabString = subprocess.Popen(
    "cscript /nologo \"" + vbsFileSpec + "\"",
    shell=True,
    stdout=subprocess.PIPE).stdout.read()
os.remove(vbsFileSpec)

print 'The following machines are connected to "' + databaseFileSpec + '":'
for x in tabString.split("\t"):
    print x

rawDescription: ADODBAPI No. Of Open Connection with database


code: print ucontent[76:110].encode('utf-8')

rawDescription: urllib2 read to Unicode

description: Urllib2 read to Unicode


code:
from lxml import etree

xmlstr = """<html>
  <A>
    <B>
      <C>
        <D>
          <TYPE>
            <NUMBER>7297</NUMBER>
            <DATA />
          </TYPE>
          <TYPE>
            <NUMBER>7721</NUMBER>
            <DATA>A=1,B=2,C=3,</DATA>
          </TYPE>
        </D>
      </C>
    </B>
  </A>
</html>"""

html_element = etree.fromstring(xmlstr)

# find all the TYPE nodes that have NUMBER=7721 and DATA nodes
type_nodes = html_element.xpath("//TYPE[NUMBER='7721' and DATA]")

# the for loop is probably superfluous, but who knows, there might be more than one!
for t in type_nodes:
    d = t.find('DATA')
    # example: append spamandeggs to the end of the data text
    if d.text is None:
        d.text = 'spamandeggs'
    else:
        d.text += 'spamandeggs'
print etree.tostring(html_element)

rawDescription: Change specific repeating element in .xml using Python


code:
image = cv2.imread('gray.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
print image.shape

rawDescription: In OpenCV (Python), why am I getting 3 channel images from a grayscale image?


code: print '%r, %r)' % (inputs, outputs)

rawDescription: Creating and accessing list of lists in python


code:
def func(foo, bar):
    print foo, bar

from collections import namedtuple
Record = namedtuple('Record', 'foo bar')
r = Record(foo=1, bar=False)
func(**r._asdict())
func(*r)

rawDescription: How can I apply a namedtuple onto a function?

description: Apply a namedtuple onto a function


code:
import itertools

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

    def add_child(self, child_node):
        self.children.append(child_node)

    def __repr__(self):
        if not self.children:
            return '<%s/>' % self.name
        else:
            children = ''.join([repr(child) for child in self.children])
            return '<%s>%s</%s>' % (self.name, children, self.name)

# create_T(d)
def create_tree(tree_dict):
    # find which node is the top of the tree (assume it is well-formed)
    all_leaves = set(itertools.chain(*tree_dict.values()))
    top_of_the_tree = set(tree_dict.keys()) - all_leaves

    def get_node(name):
        node = Node(name)
        for child_name in tree_dict[name]:
            node.add_child(get_node(child_name))
        return node
    return get_node(top_of_the_tree.pop())

print create_tree({'a': ['b', 'c'],
                   'b': ['d'],
                   'c': ['e'],
                   'd': [],
                   'e': [],
                   'f': ['a']})

rawDescription: Python - Dictionary to NodeTree

description: Python - Dictionary to NodeTree


code:
for i in a:
  print inp+' '+i

rawDescription: finding string length and storing in dict

description: Finding string length and storing in dict


code:
import sys

data = sys.stdin.read()
print 'Data from stdin -', data

rawDescription: Reading from linux command line with Python


code:
from collections import Counter
A = [['the','big','bang'],['big','bang','theory'],['the','big','bang']]
cnt = Counter(map(tuple, A))
for k, v in cnt.iteritems():
    print list(k), v

rawDescription: How to count element frequency for a list

description: Count element frequency for a list


code:
name='a'

with open('test.txt') as inpfile:
    for num, line in enumerate(inpfile):
        if name == line.rstrip(r'\n'):
            print num

rawDescription: python regex with variable input

description: Python regex with variable input


code:
import csv
def func():
    list_of_keys = ['S_Length','S_Width','P_Length','P_Width','Predicate']
    with open('example.txt') as f:
        spamreader = csv.reader(f, delimiter=',')
        return [dict(zip(list_of_keys,row)) for row in spamreader]

print func()

rawDescription: Make a list of dynamic dictionary python


code: for i in range(10): print DoDict['do1']()

rawDescription: How can I insert a random element into a dictionary and have it randomised whenever I call it?

description: Insert a random element into a dictionary and have it randomised whenever I call it


code:
common = lists[0]
from itertools import takewhile
for l1, l2 in zip(lists, lists[1:]):
    common = [i[0] for i in takewhile(lambda i: i[0] == i[1] == i[2], zip(l1, l2, common))]
uncommon = [clist[len(common):] for clist in lists]
print common, uncommon

rawDescription: Python: return list of sequentially occuring common items from lists and also a list of uncommon ones


code:
strings = [
"abcdedf_abc_abc_XS123456789_1234567",
"abcdedf_abc_abc_AB_1234567_2014/03/17",
"abcdef_abcdf_abc_xyz_12354AB12_1234567"
]

import re
pattern = re.compile(r"(?<=_)\d+(?=_|$)")
for item in strings:
    print pattern.search(item).group()

rawDescription: python regular expression with "OR" and $ end of line

description: Python regular expression with "OR" and $ end of line


code:
from Tkinter import Tk, Canvas, Frame, BOTH

class World(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.parent.title("CliffWorld")
        self.pack(fill=BOTH, expand=1)

        canvas = Canvas(self)

        canvas.create_rectangle(4, 4, 31, 31,
            outline="#f11", fill="#1f1", width=1)
        canvas.pack(fill=BOTH, expand=1)

def drawWorld():
    root = Tk()
    ex = World(root)
    root.geometry("330x220+300+300")
    root.after(1000, lambda: root.destroy())
    root.after(1000, lambda: root.quit())
    root.mainloop()

if __name__ == "__main__":
    import tkWorld
    tkWorld.drawWorld()
    print "end"

rawDescription: Tkinter window to be shut and control flow to be returned automatically


code:
#!/usr/bin/python

import fileinput
from collections import deque
d=deque(maxlen=1)
for line in fileinput.input():
    sp=line.split()
    if len(sp)==4 and d:
        d[-1][2]=sp[2]
        d[-1][3]=sp[3]
    else:
        if d: print '\t'.join(d[-1])
        d.append(sp)
else:
    print '\t'.join(d[-1])

rawDescription: replacing some fields in a line with the fields in the same columns in the next line

description: Replacing some fields in a line with the fields in the same columns in the next line


code:
a = date(2008, 12,12)
b = date(2010, 1, 13)
delta = b - a
print delta.days
print "%d years, %d days" % divmod(delta.days, 365)

rawDescription: Finding the amount of time difference between dates in python


code:
from itertools import product

def genwords(*iterables):
    return [''.join(letters) for letters in product(*iterables)]

print genwords(firsts, seconds, thirds, fourths, fifths)
print genwords('123', 'abc')

rawDescription: Python script to generate words from letters and two letter combination


code:
class D(type):
    def __new__(cls, name, bases, dct):
        print "cls is:   ", cls
        print "name is:  ", name
        print "bases is: ", bases
        print "dct is:   ", dct
        return type.__new__(cls, name, bases, dct)

class E(object):
    __metaclass__ = D

class F(E): pass

class A(object):
    pass

rawDescription: Python metaclass behavior (not calling __new__), is there an explanation?


code:
from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
import re

register = Library()

@stringfilter
def spacify(value, autoescape=None):
    if autoescape:
	esc = conditional_escape
    else:
	esc = lambda x: x
    return mark_safe(re.sub('\s', '&'+'nbsp;', esc(value)))
spacify.needs_autoescape = True
register.filter(spacify)

rawDescription: Django templates stripping spaces?

description: Django templates stripping spaces


code:
import networkx as nx
from networkx.algorithms import bipartite

edges = [('A1','B1',3),
         ('A1','B2',7),
         ('A2','B1',2),
         ('A2','B2',4),
         ]

B = nx.Graph()
B.add_weighted_edges_from(edges)

def my_weight(G, u, v, weight='weight'):
    w = 0
    for nbr in set(G[u]) & set(G[v]):
        w += G.edge[u][nbr].get(weight, 1) + G.edge[v][nbr].get(weight,1)
    return w

G = bipartite.generic_weighted_projected_graph(B, ['A1', 'A2'], weight_function=my_weight)

print G.edges(data=True)

rawDescription: Weighted Bimodal Bipartite Graph Projection conserving original weights


code:
#!/usr/bin/env python
import sys
data = sys.stdin.readlines()
print data

rawDescription: writing to stdin from java to python

description: Writing to stdin from java to python


code:
def main():
    files = [r'C:\_local\test.txt', r'C:\_local\junk.txt']
    funcs = []
    for f in files:
        # create a new lambda and store the current `f` as default to `path`
        funcs.append(lambda path=f: os.stat(path))
    print funcs

    # calling the lambda without a parameter uses the default value
    funcs[0]()
    funcs[1]()

rawDescription: Python closure not working as expected

description: Python closure not working as expected


rawDescription: NumPy: using loadtxt or genfromtxt to read a ragged structure


code:
import csv

fOpen1=open('C:\Master.csv')
fOpen2=open('C:\Subset.csv')
fOutput1=open('C:\Untagged.csv', 'wb')

master=csv.reader(fOpen1)
subset=csv.reader(fOpen2)
untagged=csv.writer(fOutput1)

count=0

subsetCopy=set()
header1=master.next()
header2=subset.next()
untagged.writerow(header1)

for row2 in subset:
    subsetCopy.add(row2[0])

for row1 in master:
    if row1[0] not in subsetCopy:
            count=count+1
            untagged.writerow(row1)
print count

rawDescription: Comparing two CSV files and exporting non-matches using Python


code:
p = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
while True:
    line = p.stdout.readline()
    if not line:
        break
    print line

rawDescription: Getting progress message from a subprocess


code:
# Detach process
pid = os.fork()
if pid == 0:
    # Ensure that process is detached from TTY
    os.setsid()

    # call ssh from here
else:
    print "Waiting for ssh (pid %d)" % pid
    os.waitpid(pid, 0)
    print "Done"

rawDescription: How to call ssh by subprocess module so that it uses SSH_ASKPASS variable

description: Call ssh by subprocess module so that it uses SSH_ASKPASS variable


code:
reader = csv.reader(open('big.csv', 'rb'))
lines = list(reader)
print lines[:100]

rawDescription: How do you split a csv file into evenly sized chunks in Python?

description: Split a csv file into evenly sized chunks in Python


code:
import util
import binascii
hash = '62e907b15cbf27d5425399ebf6f0fb50ebb88f18'
print util.hash_to_address(binascii.unhexlify("00"), binascii.unhexlify(hash))

rawDescription: How to get Bitcoin address from pubkey_hash?

description: Get Bitcoin address from pubkey_hash


code:
i = 0
for i in xrange(len(words)):
    if words[i].startswith('@'):
        print words[i+1:len(words)]

rawDescription: Python sad/happy face machine learning (Get rid of text)


code:
import os

mydic = {'key1':['va1','va2'], 'key2':['vb1','vb2']}

BASE_DIR = os.path.join('path', 'to', 'dir')
ADD_DIR = 'dir2'
for k, v in mydic.iteritems():
    for item in v:
        print os.path.join(BASE_DIR, k, ADD_DIR, item)

rawDescription: python join key and values from a dictionary

description: Python join key and values from a dictionary


code:
def posdivisor(n):
    for i in range(1,n+1):
        if n % i == 0:
            print(i)

someValue = eval(input("Enter an integer: "))

result = posdivisor(someValue)
print result

rawDescription: Why isn't my code returning "None"?

description: Why isn't my code returning "None"


code:
class UserEnteredData(object):
    def __init__(self):
        self.a = None
        self.b = None

class TextField(pygame.sprite.Sprite):
    def __init__(self, userdata, uservar, name, x, y):
        self.userdata = userdata
        self.var = uservar
        self.value = ""
    def update(self, key):
        if key == "return":
            setattr(self.userdata, self.var, self.value)

userdata = UserEnteredData
userdata.a = "NAME"
txt = TextField(userdata, "a", "popo", 20, 20)
txt.update("return")
print userdata.a       # prints a null string

rawDescription: Changing a variable inside a class

description: Changing a variable inside a class


rawDescription: Is there a function to transpose matrices (lists of lists) in python? if rows not equal column


code:
KEYS_TO_PRINT = ["id", "channel.title"]
my_data = {"items": [
    {"id": 1, "channel": {"channelid": "channelid1", "title": "nailed_it1"}},
    {"id": 2, "channel": {"channelid": "channelid2", "title": "nailed_it2"}},
    {"id": 3}
]}
this_row = []

for item in my_data["items"]:
    for export_key in KEYS_TO_PRINT:
        value = item
        for key in export_key.split("."):
            value = value.get(key)
            if value == None: break
        this_row.append(str(value).encode('utf-8') if value != None else "")
print this_row

rawDescription: How do I serialise JSON keys in a Python list?

description: Serialise JSON keys in a Python list


code:
with open('myfile.txt', 'wb') as f:
    numpy.ndarray.tofile(myData, f)

with open('myfile.txt', 'rb') as f:
    loadedData = numpy.fromfile(f, dtype=myDtype)
    print loadedData['age']

rawDescription: Python equivalent of MATLAB's dataset array


code:
for child in data['data']['children']:
    print child['data']['title']

rawDescription: Error parsing JSON in python

description: Error parsing JSON in python


code:
x[1].sort(key = x[0].__getitem__)
print x

rawDescription: Two Dimensional Python Array: Sort second by First


code:
d = df[CONT].count().to_dict()
print d
{'age': 3, 'fnlwgt': 3}
print d['age']
3
print d['fnlwgt']
3

rawDescription: Pandas Passing Variable Names into Column Name


code:
import re
l = ['fig_1.png', 'fig_10.png', 'fig_4.png', 'fig_2.png']
l.sort(key=lambda x:int(re.findall(r"(?<=_)\d+(?=\.)",x)[0]))
print l

rawDescription: sorting python list according to given texts

description: Sorting python list according to given texts


code:
gen = sample_gen(10, [1, 2, 4, 8])
print gen.next()
print gen.next()
print gen.next()
print gen.next()

rawDescription: How to incrementally sample without replacement?

description: Incrementally sample without replacement


code:
import re
with open('data') as data:
    for line in data:
        if line.startswith('#@'):
            id_ = re.search('\s(<.*>)', line).group(1)
            fields = re.findall('(<.*?>)\s', next(data))
            fields = ['<' + re.sub(r'[^\w:/._()"]', '', f) + '>' for f in fields]
            print fields + [id_]

rawDescription: Splitting strings in python in user defined manner


code:
class Foo(object):
    def __init__(self, name):
        self.name = name
    def bar(argument):
        print argument
foo = Foo('foo')
foo.bar(1) # the 'argument' variable is bound to the instance, so you're now sending two arguments to this method

rawDescription: Making a method a @staticmethod for documentation purposes?


code:
import pyodbc

cnxn = pyodbc.connect('DRIVER={FreeTDS};SERVER=dev-sql02;PORT=1433;UID=EXAMPLE\\myusero;PWD=xxx;DATABASE=fx_staging;UseNTLMv2=yes;TDS_Version=8.0;Trusted_Domain=EXAMPLE;')
cursor = cnxn.cursor()
cursor.execute("select state, zip from addresses")
row = cursor.fetchone()
if row:
    print row

rawDescription: Python --> MSSQL

description: Python --> MSSQL


code:
import time
def proc(item):
    time.sleep(2.0)
    return item * 2

print gather_results([run_item(proc, item) for item in [1, 2, 10, 100]])

rawDescription: Threading in Python for each item in list


code:
import numpy as np
from matplotlib.nxutils import points_inside_poly

nx, ny = 10, 10
poly_verts = [(1,1), (5,1), (5,9),(3,2),(1,1)]

# Create vertex coordinates for each grid cell...
# (<0,0> is at the top left of the grid in this system)
x, y = np.meshgrid(np.arange(nx), np.arange(ny))
x, y = x.flatten(), y.flatten()

points = np.vstack((x,y)).T

grid = points_inside_poly(points, poly_verts)
grid = grid.reshape((ny,nx))

print grid

rawDescription: How to "draw" a geometric shape on a 2-D meshgrid?


code:
a = [['UK', ['http://www.uk.com']], ['COM', ['http://www.uk.com'],['http://www.michaeljackson.com']]]
b = [['Australia', ['http://www.australia.com']], ['COM', ['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']]]

# convert these to dictionaries with set values
a = {item[0]:set(s[0] for s in item[1:]) for item in a}
b = {item[0]:set(s[0] for s in item[1:]) for item in b}

# define a function to update our dictionary-of-sets data structure
def union_update_setdict(D, *setdicts):
    """Update dictionary D (with `'key':set(value)` items) with items from setdicts.

    If a new key is added to D from setdicts, a shallow copy of the value
    is added to D.
    """
    for setdict in setdicts:
        for k,v in setdict.items():
            try:
                D[k].update(v)
            except KeyError:
                D[k] = v.copy()

union_update_setdict(a, b)

# Now let's test that the code works

expected = [['UK', ['http://www.uk.com']], ['COM', ['http://www.uk.com'], ['http://www.michaeljackson.com'], ['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']], ['Australia', ['http://www.australia.com']]]

# put the "expected" results in our new data structure for comparison
expected = {item[0]:set(s[0] for s in item[1:]) for item in expected}

print a
assert expected == a

rawDescription: Combining 2 lists like UNION ie keeping 1 copy of mutual items between 2 lists and appending others


code:
from BeautifulSoup import BeautifulSoup
import re
soup = BeautifulSoup(file('playlist.html').read())
for link in soup.findAll('a', attrs={'href': re.compile("your matching re")}):
    print link.get('href')

rawDescription: How can I log into a website using python?

description: Log into a website using python


code:
my_tuple = (('What is your name?','Emma'),('What is your age',34),('What color eyes do you have','Blue'))

for i in range(3):
    print my_tuple[i][0]

rawDescription: Dictionary Query Python?

description: Dictionary Query Python


code:
def counts(filename):
    d={}
    with open(filename) as f:
        contents = f.read()

    contents = contents.split("\n")
    del contents[-1]
    contents =  map(int, contents)

    for content in contents:
        if content not in d:
            d[content] = 1
        else:
            d[content] = d[content] + 1

    return d

print counts(filename)

rawDescription: Counting how many times a number appears in a file


code:
a = ['2','3','4','5','Cherry']
print [int(x) for x in a if x.isdigit()]

rawDescription: Python: "Try" inside shortened for...loop