code:
import csv
import itertools

def process(stuff):
    print ' '.join(stuff)

with open("values.csv") as f:
    rd = csv.reader(f)
    for idnum, group in itertools.groupby(rd, key=lambda x: x[0]):
        print "processing ID {}".format(idnum)
        for item in group:
            process(item)

rawDescription: Reading a File and processing elements having same ID


code:
def search(li,ur):
    for u in li:
        if u.startswith(ur):
            return li.index(u)+1
    return 0

def main():
    website = ['http://freshtutorial.com/install-xamp-ubuntu/', 'http://linuxg.net/how-to-install-xampp-on-ubuntu-13-04-12-10-12-04/', 'http://ubuntuforums.org/showthread.php?t=2149654', 'http://andyhat.co.uk/2012/07/installing-xampp-32bit-ubuntu-11-10-12-04/', 'http://askubuntu.com/questions/303068/error-with-tar-command-cannot-install-xampp-1-8-1-on-ubuntu-13-04', 'http://askubuntu.com/questions/73541/how-to-install-xampp']
    url = 'http://freshtutorial.com'
    print search(website,url)

if __name__ == '__main__':
    main()

rawDescription: How to find the position of specific element in a list?

description: Find the position of specific element in a list


code:
import re

txt="""\
This doesn't seem to work. Is findall or sub the correct approach? Or neither? Isn't it grand?
"""

first_words=set(re.findall(r'(?:^|(?:[.!?]\s))(\b[a-zA-Z\']+)', txt))

rest={word for word in re.findall(r'(\b[a-zA-Z\']+)', txt) if word not in first_words}

print first_words
# set(['This', 'Is', 'Or', "Isn't"])
print rest
# set(["doesn't", 'sub', 'grand', 'the', 'work', 'it', 'findall', 'to', 'neither', 'correct', 'seem', 'approach', 'or'])

rawDescription: Python Regular Expression help. Combinations


code:
for i in range(1,input()):
   print i * (10**i - 1) / 9

rawDescription: Python triangle quest, arithmetical operation inside print


code: print df[df.Resource.str.contains('pdf',na=False)][['IP', 'Time', 'Resource']][0:5]

rawDescription: print boolean True results of a regex match-Pandas Dataframe

description: Print boolean True results of a regex match-Pandas Dataframe


code:
import re

strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]

def split_on_letter(s):
    match = re.compile("[^\W\d]").search(s)
    return [s[:match.start()], s[match.start():]]

for s in strs:
    print split_on_letter(s)

rawDescription: How can I split a string at the first occurrence of a letter in Python?

description: Split a string at the first occurrence of a letter in Python


code:
f = open('apple.txt')
lines = f.readlines()
f.close()
things = []
printtofile = []
for i in lines:
    things.append(i)
    if i.startswith('date'):
        things.pop()
        for x in things:
            if i[:-1] == '\n':
                printtofile.append(x[:-1]+'|'+i[:-1])
            else:
                printtofile.append(x[:-1]+'|'+i)
        things = []
print printtofile
writefile = open('file.txt', 'w')
writefile.writelines(printtofile)
writefile.close()

rawDescription: Python textfile formatting

description: Python textfile formatting


code:
cpu_stats = {'time_stamp': {'hour': 22, 'month': 5, 'second': 43, 'year': 2014, 'day': 29, 'minute': 31},
             'cpus': [ [{'metric_type': 'CPU_INDEX', 'value': 1}, {'metric_type': 'CPU_TEMPERATURE', 'value': 39}, {'metric_type': 'CPU_FAN_SPEED', 'value': 12000} ] ]
            }

timestamp = ''
for stats in cpu_stats.keys():
    if stats == 'time_stamp':
        timestamp = '{year}-{month}-{day}'.format(**cpu_stats[stats])
    if stats == 'cpus':
        for cpu in cpu_stats[stats]:
            cpu_index = ''
            cpu_temperature = ''
            cpu_fan_speed = ''
            for metric in cpu:
                if metric['metric_type'] == 'CPU_INDEX':
                    cpu_index = str(metric['value'])
                elif metric['metric_type'] == 'CPU_TEMPERATURE':
                    cpu_temperature = str(metric['value'])
                elif metric['metric_type'] == 'CPU_FAN_SPEED':
                    cpu_fan_speed = str(metric['value'])
            print ','.join([timestamp, cpu_index, cpu_temperature, cpu_fan_speed])

rawDescription: recursively traverse multidimensional dictionary and export to csv

description: Recursively traverse multidimensional dictionary and export to csv


code:
def partition(array, left, right, pivotIndex):
    pivotValue = array[pivotIndex]
    swap(array, pivotIndex, right)
    storeIndex = left
    for i in range(left, right):    # range doesn't include right element already
        if array[i] <= pivotValue:  # need to check for equality (not really necessary for the sorting routine)
            swap(array, i, storeIndex)
            storeIndex = storeIndex + 1
            print array, i
    swap(array, storeIndex, right)
    return storeIndex

rawDescription: inplace quick sort implementation

description: Inplace quick sort implementation


code:
def DistanceFrom(origin):
    def f(x):
        retval = abs(x - origin)
        print "f(%s) = %s"%(x, retval)
        return retval
    return f

nums = [1, 37, 42, 101, 13, 9, -20]
nums.sort(key=DistanceFrom(10))
nums

rawDescription: Understanding __call__ and list.sort(key)


code:
def list_comments(db, limit=0):
    cur = db.cursor()
    cur.execute("SELECT id, comment FROM comments ORDER BY id DESC LIMIT %s", (str(limit)))
    results = cur.fetchall()

    print results

    return results

rawDescription: input value for LIMIT(unitest)

description: Input value for LIMIT(unitest)


code:
if re.match(ur'^((</?\d+>)|(<\d+/>))+$', str):
    print 'Matches the whole string'
else:
    print 'Doesn\'t match the whole string'

rawDescription: Is there an elegant way of seeing whether an entire string consists of nothing but matches?


code:
import subprocess
child = subprocess.Popen(["networksetup", "-setairportpower", "airport", "off"], stdout=subprocess.PIPE)
output, error = child.communicate()
print output

rawDescription: Turn on/off wifi with python on osx

description: Turn on/off wifi with python on osx


code:
import os
import pickle
from multiprocessing import Process

def scanner(root_dir, output):
    temp = set()
#if not os.path.isfile(output):  # if output does not exist create it
    with open(output, 'a'):
        pass

    files = os.walk(root_dir)  # go through all dirs and sub dirs
    for root, dirs, f in files:
        if f:
            temp.update(f)  # add all "f" files to the set
    with open(output, 'rb') as data:
        if os.path.getsize(output) > 0:  # if the file has data, it is not the first run so load and compare
            b = pickle.load(data)
            print " Deleted files {}".format(b-temp)  # if a file name has been changed been deleted
            print "Amended or New files {}".format(temp-b)  # if a file name has been changed or one added
    with open(output, 'wb') as data:  # write current files and save to output
        pickle.dump(temp, data)

if __name__ == '__main__':
    # start process for each drive
    p1 = Process(target=scanner, args=("/path1", "data.pickle"))
    p1.start()
    p2 = Process(target=scanner, args=("/path2", "data1.pickle"))
    p2.start()

rawDescription: New file creation detection using Python in Windows


code:
for u, v, p in preds:
    print '(%d, %d) -> %.8f' % (u, v, p)

rawDescription: Using adamic_adar_index in Python's Networkx


code:
for line in infline:
     #re.sub("pattern","replacement", "string")
     line =re.sub(r"([a-zA-Z]+:[0-9\.]+)",r"\1 STRING", line)
     print line

rawDescription: How can you employ multi-digit wildcards in python to parse a string using varying patterns?

description: Employ multi-digit wildcards in python to parse a string using varying patterns


code:
l = []
l.append('banana')
print l[0]
l.append('apple')
print l[0]
print l

rawDescription: simple python list problem

description: Simple python list problem


code:
import os
import time

msg =  raw_input("Enter message for child process")

x = os.fork()

if not x:
    for i in range(5):
        time.sleep(3)
        print msg

rawDescription: How to make the program relinquish control of the terminal?

description: Make the program relinquish control of the terminal


code:
from bunch import Bunch
import types
#Original class:
class A():
  y=6
  def __init__(self,num):
    self.x=num
  def funcA(self):
    print self.x

#class that wraps A using Bunch(thats what i needed .. u can use another):
class B(Bunch):
  def __init__(self, data, cls):
    self._cls = cls # notice, not an instance just the class it self
    super(B, self).__init__(data)

  def __getattr__(self, attr):
    # Handles normal Bunch, dict attributes
    if attr in self.keys():
      return self[attr]
    else:
      res = getattr(self._cls, attr)
      if isinstance(res, types.MethodType):
        # returns  the class func with self overriden
        return types.MethodType(res.im_func, self, type(self))
      else:
        # returns class attributes like y
        return res

data = {'x': 3}
ins_b = B(data, A)
print ins_b.funcA() # returns 3
print ins_b.y # returns 6

rawDescription: Python : use a class methods as static , when its implemented as instance methods


code:
def do_something(x):
  return x**2

list = []
result = [do_something(x) for x in list if list]
print result        # []

list = [1, 2, 3]
result = [do_something(x) for x in list if list]
print result       # [1, 4, 9]

rawDescription: For list unless empty in python

description: For list unless empty in python


code:
def js_to_py_re(rx):
    query, params = rx[1:].rsplit('/', 1)
    if 'g' in params:
        obj = re.findall
    else:
        obj = re.search

    # May need to make flags= smarter, but just an example...
    return lambda L: obj(query, L, flags=re.I if 'i' in params else 0)

print js_to_py_re('/o/i')('school')
# <_sre.SRE_Match object at 0x2d8fe68>

print js_to_py_re('/O/ig')('school')
# ['o', 'o']

print js_to_py_re('/O/g')('school')
# []

rawDescription: How to parse a Javascript regexp in Python?

description: Parse a Javascript regexp in Python


code:
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()

rawDescription: How can I use a SOCKS 4/5 proxy with urllib2?

description: Use a SOCKS 4/5 proxy with urllib2


code:
if args.settings_dir not in sys.path:
  sys.path.append(args.settings_dir)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

#try to query db for existing objects, e.g. user groups
#in order to detect if we are upgrading an existing installation
from django.db.utils import DatabaseError
try:
  from django.contrib.auth.models import Group
  tmp = len(Group.objects.all()) #evaluate to force db query
  updating = True
except DatabaseError as e:
  updating = False

#try to query db for South migrations in order to detect if South was already applied
#in this installation (if not then accessing MigrationHistory will throw an excepion)
try:
  from south.models import MigrationHistory
  has_south = bool(len(MigrationHistory.objects.all()))
except ImportError as e:
  print 'ERROR: Error importing South migration history: ' + str(e)
  print 'Exiting'
  exit(1)
except DatabaseError as e:
  has_south = False

#syncdb to create south_migrationhistory table, portal models will not be synced
from django.core.management import execute_from_command_line
argv = ['manage.py', 'syncdb', '--settings=settings', '--pythonpath=' + args.settings_dir]
execute_from_command_line(argv)

#if we are updating existing installation and South wasn't already applied
#then initial migration has to be 'faked' in order to sync with existing tables
if updating and not has_south:
  print 'INFO: Faking initial database migration...'
  argv = ['manage.py', 'migrate', '--all', '0001', '--fake',
          '--settings=settings', '--pythonpath=' + args.settings_dir]
  execute_from_command_line(argv)

#run normal migrations
print 'INFO: Applying database migrations...'
argv = ['manage.py', 'migrate', '--all',
        '--settings=settings', '--pythonpath=' + args.settings_dir]
execute_from_command_line(argv)

rawDescription: Django South: detecting if fake migrations were applied in production


rawDescription: Evaluation of math formula in Python (with custom variable names and subscripts)


code:
import re

content="""fox
    fox fox
 and <tag fox bar>
    small fox and
fox"""

rr = """
(fox)
(?! [^<>]*>)
(?!
    (.(?!<a))*
    </a
)
"""

p = re.compile(rr, re.IGNORECASE | re.MULTILINE | re.VERBOSE)
print p.sub(r'((\g<1>))', content)

rawDescription: Python Regular Expression: BackReference


code:
def sudo(s,password):
    rootprompt = re.compile('.*[$#]')
    s.sendline('sudo -s')
    i = s.expect([rootprompt,'assword.*: '])
    if i==0:
        print "didnt need password!"
        pass
    elif i==1:
        print "sending password"
        s.sendline(password)
        j = s.expect([rootprompt,'Sorry, try again'])
        if j == 0:
            pass
        elif j == 1:
            raise Exception("bad password")
    else:
        raise Exception("unexpected output")
    s.set_unique_promp

rawDescription: python pexpect & pxssh with sudo and EOF

description: Python pexpect & pxssh with sudo and EOF


code:
when = 0
for name in os.listdir('.'):
    if not 'foo' in name:
        continue
    # name matches *foo*
    st = os.stat(name)
    if st.mtime > when:
        when = st.mtime
        newest = name
print newest

rawDescription: python: trying to understand "subprocess" O/S calls

description: Python: trying to understand "subprocess" O/S calls


code:
#!/usr/bin/python

import select
import socket

ports_list=[7777,7778]

def make_socket(number):
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  sock.bind(('', number))
  sock.listen(5)
  return sock

read_list= map(lambda x: make_socket(x), ports_list)

print(read_list)

print "Listening on port %s" % ports_list

notAccepted = read_list[:]

while True:
    readable, writable, errored = select.select(read_list, [], [])
    for s in readable:
        if s in notAccepted:
            client_socket, address = s.accept()
            read_list.append(client_socket)
            print "Connection from", address, client_socket
        else:
            data = s.recv(1024)
            if data:
                s.send(data)
            else:
                s.close()
                read_list.remove(s)

rawDescription: Trying to implement non-blocking python udp multiple ports getting wierd exception


code:
for t in izip_longest(*x):
    print t

rawDescription: Iterating through list of list without knowing number of inner lists


code:
import pxssh
host = pxssh.pxssh
host.login('hostname','username','password')
host.sendline('command')#'ls'
print host.before

rawDescription: Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko


code:
from OpenSSL import crypto
p12 = crypto.load_pkcs12(file("push.p12", 'rb').read(), [password])

# PEM formatted private key
print crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())

# PEM formatted certificate
print crypto.dump_certificate(crypto.FILETYPE_PEM, p12.get_certificate())

rawDescription: Python p12 to pem

description: Python p12 to pem


code: print my_object.__dict__

rawDescription: print_r functionality in iPython

description: Print_r functionality in iPython


code:
x = matrix([[0.],[0],[1]])
theta = matrix(zeros([3,1]))
for i in range(5):
  grad = matrix(zeros([3,1]))
  hess = matrix(zeros([3,3]))
  [xfile, yfile] = [open('q1'+a+'.dat', 'r') for a in 'xy']
  for xline, yline in zip(xfile, yfile):
    x.transpose()[0,:2] = [map(float, xline.split("  ")[1:3])]
    y = float(yline)
    hypoth = 1 / (1 + math.exp(theta.transpose() * x))
    grad += (y - hypoth) * x
    hess -= hypoth * (1 - hypoth) * x * x.transpose()
  theta += inv(hess) * grad
print "done"
print theta

rawDescription: How do you make this code more pythonic?

description: Make this code more pythonic


code:
def parse_arguments(s):
    openers = "{[\"'("
    closers = "}]\"')"
    state = []
    current = ""
    for c in s:
        if c == "," and not state:
           yield current
           current = ""
        else:
           current += c
           if c in openers:
              state.append(c)
           elif c in closers:
              assert state, "ERROR No Opener for %s"%c
              assert state[-1] == openers[closers.index(c)],"ERROR Mismatched %s %s"%(state[-1],c)
              state.pop(-1)
    assert not state, "ERROR Unexpected End, expected %s"%state[-1]
    yield current

print list(parse_arguments("1, bar(x+17, 1), arr = 's,y'"))

rawDescription: Split string that looks like a Python function call to arguments


code:
#! /usr/bin/env python

''' Polynomial long division

From http://stackoverflow.com/questions/26173058/division-of-polynomials-in-python

A polynomial is represented by a list of its coefficients, eg
5*x**3 + 4*x**2 + 1 -> [1, 0, 4, 5]

Modified by PM 2Ring 2014.10.03
'''

def normalize(poly):
    while poly and poly[-1] == 0:
        poly.pop()
    if poly == []:
        poly.append(0)

def poly_divmod(num, den):
    #Create normalized copies of the args
    num = num[:]
    normalize(num)
    den = den[:]
    normalize(den)

    if len(num) >= len(den):
        #Shift den towards right so it's the same degree as num
        shiftlen = len(num) - len(den)
        den = [0] * shiftlen + den
    else:
        return [0], num

    quot = []
    divisor = float(den[-1])
    for i in xrange(shiftlen + 1):
        #Get the next coefficient of the quotient.
        mult = num[-1] / divisor
        quot = [mult] + quot

        #Subtract mult * den from num, but don't bother if mult == 0
        #Note that when i==0, mult!=0; so quot is automatically normalized.
        if mult != 0:
            d = [mult * u for u in den]
            num = [u - v for u, v in zip(num, d)]

        num.pop()
        den.pop(0)

    normalize(num)
    return quot, num

def test(num, den):
    print "%s / %s ->" % (num, den)
    q, r = poly_divmod(num, den)
    print "quot: %s, rem: %s\n" % (q, r)
    return q, r

def main():
    num = [1, 5, 10, 10, 5, 1]
    den = [1, 2, 1]
    test(num, den)

    num = [5, 16, 10, 22, 7, 11, 1, 3]
    den = [1, 2, 1, 3]

    quot = [5, 1, 3, 0, 1]
    rem = [0, 5]

    q, r = test(num, den)
    assert quot == q
    assert rem == r

if __name__ == '__main__':
    main()

rawDescription: Division of Polynomials in python

description: Division of Polynomials in python


code:
import readline

readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode vi')

while True:
    line = raw_input('Prompt ("stop" to quit): ')
    if line == 'stop':
        break
    print 'ENTERED: "%s"' % line

rawDescription: Python : vi mode when reading from stdin


code:
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
       return None

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

def poly_intersection(poly1, poly2):

    for i, p1_first_point in enumerate(poly1[:-1]):
        p1_second_point = poly1[i + 1]

        for j, p2_first_point in enumerate(poly2[:-1]):
            p2_second_point = poly2[j + 1]

            if line_intersection((p1_first_point, p1_second_point), (p2_first_point, p2_second_point)):
                return True

    return False

PL1 = ((-1, -1), (1, -1), (1, 2))
PL2 = ((0, 1), (2, 1))

print poly_intersection(PL1, PL2)

rawDescription: Test if polylines intersects using Python


code:
integer = 97097114103104
ascii_num = ''
while integer > 0:
    ascii_num += chr(integer % 1000)
    integer /= 1000
print ascii_num[::-1] #to Reverse the string

rawDescription: Number to ASCII String Converter

description: Number to ASCII String Converter


code:
#!/usr/bin/python

from datetime import datetime
import time

your_time = 1.28946608161e+12

print time.strftime("%a, %d %b %Y %H:%M:%S +0000",
                    datetime.fromtimestamp(your_time/1000).timetuple()
                    )

rawDescription: psycopg2 to deal with timestamp without time zone

description: Psycopg2 to deal with timestamp without time zone


code:
class Hero(object):
    def __init__(self,name):
        self.name = name #hero name
        self.health = 100 #hero health
        self.damage = 20 #hero damage
        print "My health now is ",self.health,
    def gethit (self):
        self.health = self.health - 10 #Ten is the Enemy damage value but I don't know how to get from enemy class so put constant
        print"\nOuch my health now is ",self.health

class Enemy(object):
    def __init__(self, name, hero):
        self.damage = 10 #enemy damage
        self.name = name #enemy name
        self.health = 100 #enemy name
        self.hero = hero
        print "Bwahahah I know you have " + self.hero.health
mark = Hero("Mark")
mark.gethit()
jake = Enemy("Jake", mark)

rawDescription: How do I accsess class value of an object ,vice versa?

description: Accsess class value of an object ,vice versa


code:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

from femaledaily.items import FemaledailyItem

class Femaledaily(scrapy.Spider):
    name = "femaledaily"
    allowed_domains = ["femaledaily.com"]
    BASE_URL = "http://forum.femaledaily.com/"
    start_urls = [
        "http://forum.femaledaily.com/forumdisplay.php?136-Hair-Care",
        "http://forum.femaledaily.com/forumdisplay.php?136-Hair-Care/page2",
        "http://forum.femaledaily.com/forumdisplay.php?136-Hair-Care/page3",
        "http://forum.femaledaily.com/forumdisplay.php?136-Hair-Care/page4",
    ]

    def parse(self, response):
        for thd in response.css("tbody > tr "):
            print "==========NEW THREAD======"
            url = thd.xpath('.//div[@class="threadlist-title"]/a/@href').extract()
            url = "http://forum.femaledaily.com/"+url[0]
            yield scrapy.Request(url, callback=self.parse_thread)

        # pagination
        next_page = response.xpath('//li[@class="prev_next"]/a[@rel="next"]/@href').extract()
        if next_page:
            yield Request(self.BASE_URL  + next_page[0], callback=self.parse)
        else:
            return

    def parse_thread(self, response):
        for page in response.xpath('//ol[@id="posts"]/li'):
            item = FemaledailyItem()
            item['thread_title'] = response.selector.xpath('//span[@class="threadtitle"]/a/text()').extract()
            # item['thread_starter'] = response.selector.xpath('//div[@class="username_container"]/a/text()').extract_first()
            post_creator = page.xpath('.//div[@class="username_container"]/a/text()').extract()

            if not post_creator:
                item['post_creator'] = page.xpath('.//div[@class="username_container"]/a/span/text()').extract()
            else:
                item['post_creator'] = post_creator

            item['post_content'] = ""

            cot = page.xpath(".//blockquote[@class='postcontent restore ']/text()").extract()
            for ct in cot:
                item['post_content'] += ct.replace('\t','').replace('\n','')

            yield item

        # pagination
        next_page = response.xpath('//li[@class="prev_next"]/a[@rel="next"]/@href').extract()
        if next_page:
            yield Request(self.BASE_URL  + next_page[0], callback=self.parse_thread)
        else:
            return

rawDescription: Go to next page on showthread.php with scrapy


code:
import time
import MySQLdb # also not currently used
import tweepy
from textwrap import TextWrapper # not used currently
from getpass import getpass

# define the format for messages here to avoid repetition
OUT_STR = '''
Status : %(text)s
Author : %(author)s
Date/Time : %(date)s
Source : %(source)s
Geo : %(geo)s

-----------------------------------------------------------------------------

'''

class StockTweetListener(tweepy.StreamListener):
    def __init__(self, target):
        super(StockTweetListener, self).__init__();
        self.target = target
        # status_wrapper = TextWrapper(width=60, initial_indent=' ',
        #                             subsequent_indent=' ')
        # This isn't used in the current code. But, if you were going
        # to use it, you'd need to assign it to self.status_wrapper;
        # otherwise the variable would be local to this __init__ method
        # and inaccessible from anything else.

    def on_status(self, status):
        try:
            msg = OUT_STR % {
                'text': status.text,
                'author': status.author.screen_name,
                'date': status.created_at,
                'source': status.source,
                'geo': status.geo,
            }
            print msg
            self.target.write(msg)
            # use self.target here. self is one of the paramaters to this
            # method and refers to the object; because you assigned to its
            # .target attribute before, you can use it here.

        except UnicodeDecodeError:
            # Catch any unicode errors while printing to console
            # and just ignore them to avoid breaking application.
            print "Record Skipped"

    def on_error(self, status_code):
        print 'An error has occured! Status code = %s' % status_code
        return True # keep stream alive

    def on_timeout(self):
        print 'Snoozing Zzzzzz'

def main():
    username = raw_input('Twitter username: ')
    password = getpass('Twitter password: ')
    stock = raw_input('Name of Stocks(comma seperated): ')
    stock_list = [u for u in stock.split(',')]
    follow_list = None # ??? you don't seem to define this variable

    # open results.txt here and name it f locally. once code flow leaves
    # the with statement, in this case only through an exception happening
    # that jumps you out of the while loop, the file will be closed.
    with open('results.txt', 'w') as f:
         while True:
             stream = tweepy.Stream(
                            username, password,
                            StockTweetListener(f), # passes the file to __init__
                                                   # as the "target" argument
                            timeout=None)
             stream.filter(follow_list, stock_list)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        quit()

rawDescription: Information is not written to text file from twitter in python 2.7


code:
from collections import defaultdict
bookdict = defaultdict(set)
for book, name in bookList:
    bookdict[name].add(book)
logged_in_user = 'fred'
for person, books in bookdict.items():
    if person == logged_in_user:
        continue
    common = books.intersection(bookdict[logged_in_user])
    if common:
        print '%s also has %s' % (person, ', '.join(common))

rawDescription: Nested List Comparison

description: Nested List Comparison


code:
def nodes(d):
    if d == 0:
        return 1
    else:
        return 1 + 7 * nodes(d - 1)

print [nodes(i) for i in range(5)] # [1, 8, 57, 400, 2801]

rawDescription: Using recursion to find max number of nodes in base 7 tree


code:
for name in names:
    count = "snmpbulkwalk -O qv -v 2c -c 2sc4nFB {0} OID-value ".format(name)
    bw = commands.getoutput(count)
    print name+",utilization,"+bw

rawDescription: Variable with multiple entries run in a for loop


code:
def pytest_funcarg__some_data(request):
    def create():
        # Load the test data here
        print 'Test data loaded'

    return request.cached_setup(
        setup=create,
        scope='session',
        extrakey='some_data'
    )

def uses_some_data(func):
    # The funcarg is actually requested here
    def wrapper(self, some_data):
        return func
    return wrapper

class TestSomething(object):
    @uses_some_data
    def test_something(self):
        # "Some data" is now available
        pass

rawDescription: Py.test - Session based setup

description: Py.test - Session based setup


rawDescription: RegEx Repeating Capture Group

description: RegEx Repeating Capture Group


code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create original dataframe
df = pd.DataFrame(np.random.rand(5,4), index=['art','mcf','mesa','perl','gcc'],
                        columns=['pol1','pol2','pol3','pol4'])
# Estimate average
average = df.mean()
average.name = 'average'

# Append dummy row with zeros and then average
row = pd.DataFrame([dict({p:0.0 for p in df.columns}), ])

df = df.append(row)
df = df.reindex(np.where(df.index, df.index, ''))
df = df.append(average)
print df

df.plot(kind='bar')
plt.show()

rawDescription: Visually separating bar chart clusters in pandas


code:
from sqlalchemy import Integer, String, Column, create_engine, ForeignKey, ForeignKeyConstraint
from sqlalchemy.orm import relationship, Session
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

Base = declarative_base()

class AnimalFact(Base):
    """key/value attribute whose value can be either a string or a list of strings"""
    __tablename__ = 'animalfacts'

    # use either surrogate PK id, or the composite animal_id/key - but
    # not both.   id/animal_id/key all together is not a proper key.
    # Personally I'd go for "id" here, but here's the composite version.

    animal_id = Column(Integer, ForeignKey('animal.id'), primary_key=True)
    key = Column(String, primary_key=True)

    # data
    str_value = Column(String)
    _list_value = relationship('StringEntry')

    # proxy list strings
    list_proxy = association_proxy('_list_value', 'value')

    def __init__(self, key, value):
        self.key = key
        self.value = value

    @property
    def value(self):
        if self.str_value is not None:
            return self.str_value
        else:
            return self.list_proxy

    @value.setter
    def value(self, value):
        if isinstance(value, basestring):
            self.str_value = value
        elif isinstance(value, list):
            self.list_proxy = value
        else:
            assert False

class Animal(Base):
    __tablename__ = 'animal'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    _facts = relationship(AnimalFact, backref='animal',
                          collection_class=attribute_mapped_collection('key'))
    facts = association_proxy('_facts', 'value')

    def __init__(self, name):
        self.name = name

    # dictionary interface around "facts".
    # I'd just use "animal.facts" here, but here's how to skip that.
    def __getitem__(self, key):
        return self.facts.__getitem__(key)

    def __setitem__(self, key, value):
        self.facts.__setitem__(key, value)

    def __delitem__(self, key):
        self.facts.__delitem__(key)

    def __contains__(self, key):
        return self.facts.__contains__(key)

    def keys(self):
        return self.facts.keys()

class StringEntry(Base):
    __tablename__ = 'myvalue'
    id = Column(Integer, primary_key=True)
    animal_id = Column(Integer)
    key = Column(Integer)
    value = Column(String)

    # because AnimalFact has a composite PK, we need
    # a composite FK.
    __table_args__ = (ForeignKeyConstraint(
                        ['key', 'animal_id'],
                        ['animalfacts.key', 'animalfacts.animal_id']),
                    )
    def __init__(self, value):
        self.value = value

engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)

session = Session(engine)

# create a new animal
a = Animal('aardvark')

a['eyes'] = ['left side', 'right side']

a['cute'] = 'sort of'

session.add(a)
session.commit()
session.close()

for animal in session.query(Animal):
    print animal.name, ",".join(["%s" % animal[key] for key in animal.keys()])

rawDescription: Can the same @property present both scalar and collection behavior in SQLAlchemy?


code:
if resource.rstrip('0123456789') in resources:
    if resource != resource.rstrip('0123456789'):
        print 'ok'
    else:
        print 'banned'
else:
    raise NotImplementedError()  # replace with own code

rawDescription: Python - How to include any random integer in a string


code:
from StringIO import StringIO
import os
s = StringIO()
s.write("abc")
s.seek(0, os.SEEK_END)
print s.tell()

rawDescription: In Python, how do I check the size of a StringIO object?


code:
import socket
REMOTE_SERVER = "www.google.com"
def is_connected():
  try:
    # see if we can resolve the host name -- tells us if there is
    # a DNS listening
    host = socket.gethostbyname(REMOTE_SERVER)
    # connect to the host -- tells us if the host is actually
    # reachable
    s = socket.create_connection((host, 80), 2)
    return True
  except:
     pass
  return False
print is_connected()

rawDescription: Test if an internet connection is present in python


code:
from xml.dom.minidom import parseString, getDOMImplementation

test_xml = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <out>This is a sample output with <test>default</test> text </out>
</AppName>'''

replacements = {'test':'example'}
dom = parseString(test_xml)
if (len(dom.getElementsByTagName('out'))!=0):
    xmlTag = dom.getElementsByTagName('out')[0]
    children =  xmlTag.childNodes
    text = ""
    for c in children:
        if c.nodeType == c.TEXT_NODE:
            text += c.data
        else:
            if c.nodeName in replacements.keys():
                text += replacements[c.nodeName]
            else: # not text, nor a listed tag
                text += c.toxml()
    print text

rawDescription: Parsing XML tags through Python and replace it using xml.dom.minidom


code:
a = ("12,3K", "1,84M")
numbers = {"K": 1000, "M": 1000000}

result = []

for value in a:
    if value:
        i = value[-1]
        value = float(value[:-1].replace(',', '.')) * numbers[i]
        result.append(int(value))

print max(result)

rawDescription: Compare numbers in form: 12,3K , 1,84M in python


rawDescription: How to remove preceding zeros in a string element from a Python list

description: Remove preceding zeros in a string element from a Python list


code:
trainset = [('It is too bad that our jane is just a pigeon. It would be great if it could speak. It would be able to prove my innocence.'), ('I have no other choice. Is death the only way to prove it? Loving you is really hard!'), ('These are my last words.')]

def cleanedthings(trainset):
    cleanedtrain = []
    specialch = "!@#$%^&*-=_+:;\".,/?`~][}{|)("
    for line in trainset:
        for word in line.split():
            lowword = word.lower()
            for ch in specialch:
                if ch in lowword:
                    lowword = lowword.replace(ch,"")
            if len(lowword) >= 3:
                cleanedtrain.append(lowword)
    return cleanedtrain

print " ".join(cleanedthings(trainset))

rawDescription: Removing word from string if condition is met?


code:
import re
s = '<!-- FEO DEBUG OUTPUT [TextTransAttempted:RENAME_JAVASCRIPT(19), RENAME_IMAGE(59), MINIFY_JAVASCRIPT(10), (1), EMBED_JAVASCRIPT(2), RENAME_CSS(3), (1), IMAGE_COMPRESSION(59);TextTransApplied:RENAME_JAVASCRIPT(9), RENAME_IMAGE(59), MINIFY_JAVASCRIPT(10), (1), EMBED_JAVASCRIPT(2), RENAME_CSS(3), (1), IMAGE_COMPRESSION(59);TagTransAttempted:(73);TagTransApplied:(73); ] -->'
tag_list = re.findall(r'(?:TextTransAttempted|TextTransApplied):\s*((?:(?:[A-Z]+(?:_[A-Z\d]+)+)?\(\d+\)\s*(?:,\s*|;))*)', s)
for tag in tag_list:
    result = {}
    for e in tag.split(","):
        index = e.index('(')
        if e[:index].strip():
            result[e[:index].strip()] = (e.split("(")[1].rstrip(");"))
    print result

'''
OUTPUT
>>>
{'RENAME_CSS': '3', 'IMAGE_COMPRESSION': '59', 'MINIFY_JAVASCRIPT': '10', 'RENAME_JAVASCRIPT': '19', 'RENAME_IMAGE': '59', 'EMBED_JAVASCRIPT': '2'}
{'RENAME_CSS': '3', 'IMAGE_COMPRESSION': '59', 'MINIFY_JAVASCRIPT': '10', 'RENAME_JAVASCRIPT': '9', 'RENAME_IMAGE': '59', 'EMBED_JAVASCRIPT': '2'}
'''

rawDescription: How to write a regex for the following use case

description: Write a regex for the following use case


code:
import netifaces as nif

def mac_for_ip(ip):
    'Returns a list of MACs for interfaces that have given IP, returns None if not found'
    for i in nif.interfaces():
        addrs = nif.ifaddresses(i)
        try:
            if_mac = addrs[nif.AF_LINK][0]['addr']
            if_ip = addrs[nif.AF_INET][0]['addr']
        except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
            if_mac = if_ip = None
        if if_ip == ip:
            return if_mac
    return None

rawDescription: How can I find the MAC address of a client on the same network, using Python-Flask?

description: Find the MAC address of a client on the same network, using Python-Flask


code:
class PrintHelloMiddleware(object):
    def process_response(self, request, response):
        print "Hello"
        return None

# settings.py
MIDDLEWARE_CLASSES = ( # ... usual middleware
                       'path.to.my.middleware.PrintHelloMiddleware',
                      )

rawDescription: In Django, how do I write a this for every function?


code:
import random
import itertools
import numpy
import scipy.io.wavfile

def main():
    """Read a wav file and shuffle the negative and positive pieces."""
    # Use unpacking to your advantage, and avoid using "input" as a var name
    samplerate, data = scipy.io.wavfile.read('sample.wav')

    # Note, my sample.wav is stereo, so I'm going to just work with one channel
    # If yours is mono, you'd want to just pass "data" directly in
    left, right = data.T

    wavchunks = process_wav(left)
    output = shuffle_wav(wavchunks).astype(numpy.int16)
    print get_fitness(output, samplerate)

    scipy.io.wavfile.write('output.wav', 44100, output)

def process_wav(wavdata):
    """Read a wav file data array, detect zero crossings,
    split at zero crossings, and return a list of numpy arrays"""

    # I prefer nonzero to where, but either works in this case...
    zerocrossings, = numpy.diff(numpy.sign(wavdata)).nonzero()
    zerocrossings += 1
    indicies = [0] + zerocrossings.tolist() + [None]

    # The key is that we don't need to convert everything to a list.
    # Just pass back a list of views into the array. This uses less memory.
    return [wavdata[i:j] for i, j in zip(indicies[:-1], indicies[1:])]

def shuffle_wav(partitions):
    """Accept a list as input, separate into positive and negative chunks,
    shuffle, and return a shuffled nested list."""

    # Instead of iterating through each item, just use indexing
    poschunks = partitions[::2]
    negchunks = partitions[1::2]
    if poschunks[0][0] < 0:
        # Reverse the variable names if the first chunk wasn't positive.
        negchunks, poschunks = poschunks, negchunks

    # Instead of popping a random index off, just shuffle the lists...
    random.shuffle(poschunks)
    random.shuffle(negchunks)

    # To avoid the error you were getting, use izip_longest
    chunks = itertools.izip_longest(poschunks, negchunks, fillvalue=[])

    return numpy.hstack(item for sublist in chunks for item in sublist)

def get_fitness(array, target):
    """Compares sum of square differences between the two arrays."""
    # I'm going to assume that you wanted a single sum returned here...
    # Your original code returned an array.
    return ((array - target)**2).sum()

main()

rawDescription: Mismatched dimensions on 'shuffled' NumPy array


code:
import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

rawDescription: How can I login into an email account, and check the subject of recently delivered emails using Ruby (or Python)?

description: Login into an email account, and check the subject of recently delivered emails using Ruby (or Python)


code:
for x in range(0, 9):
    if expression(random.uniform(0.0, 1.0) < 0.416667):
        print "A wins"
    else:
        print "B wins"

rawDescription: Weighted Probability in Python

description: Weighted Probability in Python


code:
import os
import time

while(1):
    os.spawnl(os.P_NOWAIT, "/path/to/php-cli twitter_clean.php")
    print "checked twitter"
    time.sleep(45)

rawDescription: Python: Auto refresh local PHP file

description: Python: Auto refresh local PHP file


code:
if cmd == "additem":
    item = raw_input("Enter item name: ")
    cart.append(item)

#at the end
for item in cart:
    print "Item: %s. Price: %s" % (item, prices[item])

rawDescription: Making a shopping cart in Python

description: Making a shopping cart in Python


code:
def countup(k,n):
    print "Let's Go"
    _countup(k,n)

def _countup(k, n):
     'counts up from k to n'
     if k == n:
         print(k)
     else:
         print(k)
         _countup(k+1,n)

rawDescription: Want to add string once at beginning

description: Want to add string once at beginning


code:
class listxer(object):
  def __init__(self, structure, paths):
    self.structure = structure
    self.paths = paths

  def _descend(self, path):
    return reduce(lambda x,y: x[y], path, self.structure)

  def __len__(self):
    return sum(len(self._descend(path)) for path in self.paths)

  def __getitem__(self, item):
    if item < 0:
      raise ValueError('negative indices not supported!')
    for path in self.paths:
      cur = self._descend(path)
      if item > len(cur):
        item -= len(cur)
        continue
      else:
        return cur[item]
    else:
      raise IndexError('list index out of range')

H = [[[1, 2], [3, 4, 5]]]

mystruct = listxer(H, ((0, 0), (0, 1)))
print len(mystruct)
print mystruct[3]

rawDescription: Python creating list C = A + B so that changes to C affect A and B


code:
for i in lis:
   exec 'driver.' + i

rawDescription: Passing selenium commands inside a list

description: Passing selenium commands inside a list


code:
import itertools

group1 = ['a', 'b']
group2 = ['c', 'd']

print list(itertools.product(group1, group2))

#==> [('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]

rawDescription: How do I build a list of all possible tuples from this table?

description: Build a list of all possible tuples from this table


code:
import subprocess
p = subprocess.Popen('pwd', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.stdout.read()
print output

rawDescription: Hiding command prompt when using python

description: Hiding command prompt when using python


code:
SQL = """select * from mytable where col1='{0}' and col2='{1}'"""

print map(lambda x :SQL.format(*x),df[['Col1','Col2']].values)

["select * from mytable where col1='1591354166' and col2='12387796'",
 "select * from mytable where col1='1596855166' and col2='8833942'",
 "select * from mytable where col1='1626196066' and col2='12584655'"]

rawDescription: using pandas df with map

description: Using pandas df with map


rawDescription: How to create a variable in Python to store a number with 500 digits after decimal?

description: Create a variable in Python to store a number with 500 digits after decimal


code:
seqA = 'ATTCGT'
matches = '||   |'
seqB = 'ATCTAT'
score = 2
print '%s\n%s\n%s\nScore: %d' % (seqA, matches, seqB, score)

rawDescription: New line when using % formatting in python


code:
import urllib #Idk if this is necessary, but I'm not gonna bother checking. Only time I used it was for urllib.urlencode, which idk if urllib2 can do
import urllib2
import cookiejar
try:
    cj = cookielib.MozillaCookieJar("./cookies.txt")
    cj.load()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    tc = opener.open("http://www.roblox.com/My/Money.aspx").read()

#    print "Loading stored cookies succeeded, automatically logging in..."

#    checksession = re.findall('My Transactions',tc)

#    if len(checksession) > 0:
#        print "Login success!"

except IOError:
    print "Loading stored cookies failed, manually logging in..."
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)
    authentication_url = 'https://www.roblox.com/newlogin'
    payload = {
        'username' : 'USERNAMEHERE',
        'password' : 'PASSWORDHERE',
        '' : 'Log In', #In hindsight Idk if this is necessary, but I don't feel like checking now
        }
    data = urllib.urlencode(payload)
    req = urllib2.Request(authentication_url, data)
    resp = urllib2.urlopen(req)
    cj.save("./cookies.txt") #./ to make sure its in the right directory

    tc = opener.open("http://www.roblox.com/My/Money.aspx").read() #Only accessible if logged in
#    checksession = re.findall('My Transactions',tc) #The rest is to check if log in was success by looking for specific phrase in the page

#    if len(checksession) > 0:
#        print "Login success!"

rawDescription: Python - How do I save/load specific cookies?


code:
import itertools
for i,j in itertools.product(all, all):
  print i,j

rawDescription: python for-loop getting increasingly slower when the data is a little big

description: Python for-loop getting increasingly slower when the data is a little big


code:
import time
import hashlib
from multiprocessing import Pool

def worker(x):
    data = bytearray(xrange(256)) * 4 * 1024
    md5 = hashlib.md5()
    for x in xrange(1024):
        md5.update(data)

if __name__ == '__main__':
    num_workers = 4
    pool = Pool(num_workers)
    start = time.time()
    pool.map(worker, xrange(num_workers))
    print num_workers / (time.time() - start), 'Gb/s'

rawDescription: Python Multiprocessing imap chunksize

description: Python Multiprocessing imap chunksize


code:
def foo(self, a):
    def closuer(b):
        print "closure %d, %d" % (self.val, a)
    return closuer

rawDescription: python closure + oop

description: Python closure + oop


code:
if extension == ".png":

    folder_path_length = len(folder_path)+1
    extension = len(file)-4 + folder_path_length
    filename = file[:extension]

    os.rename(current, current[:extension])

    extension = len(file)-4
    truncated_filename = file[:extension]

    print "\n\t%s \n\tnow renamed to: %s" % (filename, truncated_filename)

    files_renamed_count +=1

rawDescription: Remove file extension from file within dir


code:
for li in soup.select('div.Updated.coLeft li'):
    print ' '.join(li.stripped_strings)

rawDescription: How to filter data and print out string using python beautifulsoup?

description: Filter data and print out string using python beautifulsoup


code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"

rawDescription: Selenium Python: how to wait until the page is loaded?


code: print doCombine(target, x, len(target), 0, 0)

rawDescription: Generating combinations of a string (not permutations)


code:
from datetime import timedelta

start = timedelta(hours=1)
end = timedelta(hours=6)
duration = end - start
print duration.total_seconds() / 60

rawDescription: Calculating the times into minutes

description: Calculating the times into minutes


code:
for line in open("file.txt"):
 if "Hello" in line:
   print "Yes"
   break

rawDescription: How to make This if?

description: Make This if


code:
import json
import requests
import urllib
args = {
        'client_id': '',#your client id here
        'client_secret': '',#your azure secret here
        'scope': 'http://api.microsofttranslator.com',
        'grant_type': 'client_credentials'
    }
oauth_url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
oauth_junk = json.loads(requests.post(oauth_url,data=urllib.urlencode(args)).content)
translation_args = {
        'text': "hello",
        'to': 'ru',
        'from': 'en'
        }
headers={'Authorization': 'Bearer '+oauth_junk['access_token']}
translation_url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
translation_result = requests.get(translation_url+urllib.urlencode(translation_args),headers=headers)
print translation_result.content

rawDescription: Microsoft Translator API in Python

description: Microsoft Translator API in Python


code:
weird_list = [(u'I', u'O'), (u'met', u'O'), (u'with', u'O'), (u'Alan', u'PERSON'), (u'yesterday', u'O')]
for word, tag in weird_list:
    if tag == 'PERSON':
        print word

rawDescription: Extract tuple containing 'PERSON' in StanforNER list result


code:
# -- update_batch.py --
import sys
import os

def main():
    if len(sys.argv) == 1:
        return

    appId = sys.argv[1]
    print "appId", appId

    # Your script to minify javascipt
    #os.chdir(r".\template")
    #cmd = r'jscom.py ./js/new/xxx_plugin.js xxx_plugin.js %s.appspot.com'%appId
    #os.system(cmd)

    os.chdir("..")
    # Perform appcfg.py to update GAE server
    cmd = r'"C:\Program Files\Google\google_appengine\appcfg.py"'
    os.system(cmd + " update . " + " -A %s"%appId)

    #os.system(cmd + " backends . " + " update worker " + " -A %s"%appId)

if __name__ == "__main__":
    main()

# Usage update_batch.py YOUR_APP_ID_HERE

rawDescription: Minify/compress javascript and css on deployment in webapp2?


code:
# student class, inits with name and student id
class Student:
    def __init__(self,name,grade, attendance):
        self.name = name
        self.grade = grade
        self.attendance = attendance
    def __str__(self):
        return self.name

# list of students
students = []

# creates two students
s1 = Student('terry', 99, 99)
s2 = Student('jack', 42, 1)

# add students in the list
students.append(s1)
students.append(s2)
print students

rawDescription: Using classes in Python

description: Using classes in Python


code:
from collections import defaultdict
import re
params = {
    'a': 'b',
    'c': 'd',
    'foo[bar]': 'element1',
    'foo[baz]': 'element2',
    'foo[qis]': 'element3',
    'foo[borfarglan][bofgl]': 'element4',
    'foo[borfarglan][bafgl]': 'element5',
}

def split(string, brackets_on_first_result = False):
    matches = re.split("[\[\]]+", string)
    matches.remove('')
    return matches

def mr_parse(params):
    results = {}
    for key in params:
        if '[' in key:
            key_list = split(key)
            d = results
            for partial_key in key_list[:-1]:
                if partial_key not in d:
                    d[partial_key] = dict()
                d = d[partial_key]
            d[key_list[-1]] = params[key]
        else:
            results[key] = params[key]
    return results
print mr_parse(params)

rawDescription: Parsing dictionary-like URL parameters in Python


code:
from sqlalchemy.sql import func
stmt = session.query(Address.user_id, func.count('*').\
        label('address_count')).\
        group_by(Address.user_id).subquery()
for u, count in session.query(User, stmt.c.address_count).\
    outerjoin(stmt, User.id==stmt.c.user_id).order_by(stmt.c.address_count):
    print u, count

rawDescription: Order by Count of OneToMany Elixir Relationship


code:
for post in soup.findAll('div',{"class","post"}):
    for paragraph in post.findAll('p'):
        if  "_gaq.push" in paragraph.text:    # <---------
            continue
        print paragraph.text

rawDescription: Python Continue statement if does not contain gaq tag


code:
p = db.Programs.find_one({'Title':'...'})

pipe = [
        {'$match':{'_Program':DBRef('Programs',p['_id']),'Duration':{'$gt':0}}},
        {'$group':{'_id':'$_Program', 'AverageDuration':{'$avg':'$Duration'}}}
        ]

eps = db.Episodes.aggregate(pipeline=pipe)

print eps['result']

rawDescription: How can I use Mongodb Aggregation in this example?

description: Use Mongodb Aggregation in this example


code:
import re

regexes = [
    "foo.*",
    "bar.*",
    "qu*x"
    ]

# Make a regex that matches if any of our regexes match.
combined = "(" + ")|(".join(regexes) + ")"

if re.match(combined, mystring):
    print "Some regex matched!"

rawDescription: Python: Elegant way to check if at least one regex in list matches a string


code:
from abc import abstractmethod, ABCMeta

class OrderedAbstractMethod(object):
    def __init__(self):
        self.counter = 0
    def __call__(self,func):
        func._method_order = self.counter
        self.counter += 1
        return abstractmethod(func)

ordered_abstract_method = OrderedAbstractMethod()

class Abstract(object):
    __metaclass__ = ABCMeta
    @ordered_abstract_method
    def first(self):
        pass
    @ordered_abstract_method
    def second(self):
        pass
    @ordered_abstract_method
    def third(self):
        pass
    @ordered_abstract_method
    def fourth(self):
        pass

print "Unordered methods: ",[method[0] for method in   Abstract.__dict__.items() if not method[0].startswith("_") ]
# here it printed out -    ['second', 'third', 'fourth', 'first']

print "Ordered methods: ", sorted([method for method in   Abstract.__dict__.items() if not method[0].startswith("_") ], key= lambda m: m[1]._method_order)

rawDescription: Ordering class methods without instantiation


code:
def evenNum(x):
  even_nums = []
  for num in x:
    if num%2==0:
      even_nums.append(num)
      print num
  return even_nums

rawDescription: For-Loop a list returning a None value in the end


code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(('hostname', 22))
    print "Port 22 reachable"
except socket.error as e:
    print "Error on connect: %s" % e
s.close()

rawDescription: How to check if SSH connection was established with AWS instance

description: Check if SSH connection was established with AWS instance


code:
import difflib
print '\n'.join(difflib.ndiff([x1], [x2]))

rawDescription: Python Two Identical Strings are viewed as Different


rawDescription: Find a string in a list of list in python


code:
from fabric.api import *

env.hosts = ['123.456.789.101']
env.user = 'John'
env.key_filename = '/Users/John/.ssh/google_compute_engine'
env.forward_agent = True

def update():
  '''
  update workers code
  '''
  with cd('~/myrepo'):
      # pull changes
      print colors.cyan('Pulling changes...')
      run('git pull origin master')

rawDescription: Permission denied on git repository with Fabric


code:
import os, re

NUM_RE = re.compile(r'\d+')

for name in os.listdir('.'):
    match = NUM_RE.search(name)
    if match is None or match.start() == 0:
        continue  # no number or number already at start
    newname = match.group(0) + name[:match.start()] + name[match.end():]
    print 'renaming', name, 'to', newname
    #os.rename(name, newname)

rawDescription: Python: rename all files in a folder using numbers that file contain


code:
import os
os.chmod("my_script.sh", 0744)

rawDescription: Set execute bit for a file using python

description: Set execute bit for a file using python


code:
from ctypes import sizeof
from ctypes import byref
import re
from ctypes import c_ulong, create_string_buffer

from constants.defines import READ_PROCESS_MEMORY
from constants.defines import VIRTUALQUERYEX
from constants.structures import SYSTEM_INFO
from constants.structures import MEMORY_BASIC_INFORMATION

import win32security, win32con, win32api, pywintypes
import sys
import os

rules = None

def AdjustPrivilege( priv ):
    flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
    htoken =  win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
    id = win32security.LookupPrivilegeValue(None, priv)
    newPrivileges = [(id, win32security.SE_PRIVILEGE_ENABLED)]
    win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)

def ReadProcessMemory(ProcessID, rules):
    base = 0
    memory_basic_information = MEMORY_BASIC_INFORMATION()
    AdjustPrivilege("seDebugPrivilege")
    #pHandle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ | win32con.PROCESS_VM_OPERATION , 0, ProcessID)
    pHandle = win32api.OpenProcess(win32con.PROCESS_VM_READ | win32con.PROCESS_QUERY_INFORMATION, 0, ProcessID)

    while VIRTUALQUERYEX(pHandle.handle, base, byref(memory_basic_information), sizeof(memory_basic_information)) > 0:
        count = c_ulong(0)
        #MEM_COMMIT && MEM_PRIVATE
        #if memory_basic_information.State == 0x1000 and memory_basic_information.Type == 0x20000:
        try:
            buff = create_string_buffer(memory_basic_information.RegionSize)
            if READ_PROCESS_MEMORY(pHandle.handle, base, buff, memory_basic_information.RegionSize, byref(count)):
                #print buff.raw
                res_rx1 = ["REGEX_STRING"]
                found_rx1 = []
                for regex in res_rx1:
                    found_rx1 += re.findall(regex, buff.raw, re.DOTALL|re.UNICODE)
                    found_rx1 = list(set(found_rx1))
                if len(found_rx1)>0:
                    for line in found_rx1:
                        print line
                #matches = rules.match(data=buff.raw)
                #for m in matches:
                #    print m, "0x%x" % memory_basic_information.BaseAddress
        except:
            pass
        base += memory_basic_information.RegionSize

    win32api.CloseHandle(pHandle)
    #base += system_info.dwPageSize

ReadProcessMemory(1280, rules)

rawDescription: Python Windows API dump process in buffer then REGEX search


code:
import httplib2

# Start of the workaround for SSL3
# This is a monkey patch / module function overriding
# to allow pages that only work with SSL3

# Build the appropriate socket wrapper for ssl
try:
    import ssl # python 2.6
    httplib2.ssl_SSLError = ssl.SSLError
    def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if disable_validation:
            cert_reqs = ssl.CERT_NONE
        else:
            cert_reqs = ssl.CERT_REQUIRED
        # Our fix for sites the only accepts SSL3
        try:
            # Trying SSLv3 first
            tempsock = ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file,
                                       cert_reqs=cert_reqs, ca_certs=ca_certs,
                                       ssl_version=ssl.PROTOCOL_SSLv3)
        except ssl.SSLError, e:
            tempsock = ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file,
                                       cert_reqs=cert_reqs, ca_certs=ca_certs,
                                       ssl_version=ssl.PROTOCOL_SSLv23)
        return tempsock
    httplib2._ssl_wrap_socket = _ssl_wrap_socket
except (AttributeError, ImportError):
    httplib2.ssl_SSLError = None
    def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise httplib2.CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock)
    httplib2._ssl_wrap_socket = _ssl_wrap_socket

# End of the workaround for SSL3

if __name__ == "__main__":
    h1 = httplib2.Http()
    resp, content = h1.request("YOUR_SSL3_ONLY_LINK_HERE", "GET")
    print(content)

rawDescription: SSL version in httplib2 - EOF occurred in violation of protocol


code:
import pandas as pd
data = pd.read_csv('example.csv', sep='\t')
print data
data.dropna()
data.dropna().to_csv('example_clean.csv')

rawDescription: Remove row from CSV that contains empty cell using Python


code:
class saveconfig(object):
    def __init__(self, f):
        self.f = f

    def __get__(self, instance, owner):
        def wrapper(*args):
            print "Saving configuration"
            print instance.cfg
            return self.f(instance, *args)
        return wrapper

rawDescription: Inside a decorator-class, access instance of the class which contains the decorated method


code:
while x > 0:
    if num%x == 0:
        if ifprime(x):
                print "I've found a prime! "
                print x
                prime[len(prime):] = [x]
    x -= 1

rawDescription: Runtime Crash For A Very Basic Python Program


code:
def print_board(board):
    for row in board:
        print ' '.join(row)

def game_board(width, height):
    return [['_' for i in xrange(width)] for j in xrange(height)]

board = game_board(3,3)

board[0][2] = 'X'
board[1][1] = 'X'

print_board( board )

rawDescription: str assignment in a matrix in python

description: Str assignment in a matrix in python


code:
import networkx as nx
# weighted MultiGraph
M = nx.MultiGraph()
M.add_edge(1,2,weight=7)
M.add_edge(1,2,weight=19)
M.add_edge(2,3,weight=42)

# create weighted graph from M
G = nx.Graph()
for u,v,data in M.edges_iter(data=True):
    w = data['weight']
    if G.has_edge(u,v):
        G[u][v]['weight'] += w
    else:
        G.add_edge(u, v, weight=w)

print G.edges(data=True)
# [(1, 2, {'weight': 26}), (2, 3, {'weight': 42})]

rawDescription: Networkx : Convert multigraph into simple graph with weighted edges


code:
from collections import defaultdict
for line in myfile:
    # tokenize
    word_counts = defaultdict(int)
    for word in line:
        if word in hotwords:
            word_counts[word] += 1
    print '\n'.join('%s: %s' % (k, v) for k, v in word_counts.items())

rawDescription: Counting (and writing) word frequencies for each line within text file


code:
import SimpleHTTPServer
import SocketServer

PORT = 8000

class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def __init__(self, req, client_addr, server):
        SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)

    def do_GET(self):
        # cut off a query string
        if '?' in self.path:
            self.path = self.path.split('?')[0]
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

class MyTCPServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

if __name__ == '__main__':
    httpd = MyTCPServer(('localhost', PORT), CustomHandler)
    httpd.allow_reuse_address = True
    print "Serving at port", PORT
    httpd.serve_forever()

rawDescription: Why does SimpleHTTPServer redirect to ?querystring/ when I request ?querystring?


code:
import os, xlrd

path = 'c:/users/c_thv/desktop' #path to jpg files
path1 = 'c:/users/c_thv/desktop/x.xls'

data =[] #list of jpg filenames in folder

#lets create a filenames list without the jpg extension
for name in os.listdir(path):
    fileName, fileExtension = os.path.splitext(name)
    if fileExtension =='.jpg':
        data.append(fileName)

#lets create a list of old filenames in the excel column a

book = xlrd.open_workbook(path1)
sheet = book.sheet_by_index(0)
oldNames =[]
for row in range(sheet.nrows):
    oldNames.append(sheet.cell_value(row,0))

#lets create a list with the new names in column b
newNames =[]
for row in range(sheet.nrows):
    newNames.append(sheet.cell_value(row,1))

#now create a dictionary with the old name in a and the corresponding new name in b

fileNames = dict(zip(oldNames,newNames))

print fileNames

#lastly rename your jpg files

for f in data:
    if f in fileNames.keys():
        os.rename(path+'/'+f+'.jpg', path+'/'+fileNames[f]+'.jpg')

rawDescription: Bulk Search/replacing of filenames using python


code:
from itertools import islice

for i,elm in islice(enumerate(some_list),7,40):
    print i,elm

rawDescription: Python: slices of enumerate

description: Python: slices of enumerate


code:
#!/usr/bin/env python
from collections import defaultdict

my_list = [
    ['Task Type 1', 'Project 1'],
    ['Task Type 2', 'Project 1'],
    ['Task Type 4', 'Project 1'],
    ['Task Type 3', 'Project 2'],
    ['Task Type 4', 'Project 2'],
    ['Task Type 1', 'Project 3'],
    ['Task Type 1', 'Project 3'],
    ['Task Type 4', 'Project 4']
]

# create mapping according to our filter value
# in our case, project to it's types
projects_to_types = defaultdict(list)
for x in my_list:
    projects_to_types[x[1]].append(x[0])

# sort all lists of types - this promises
# the equation of two identical lists
# returns the same results (lists have order)
projects_to_types = {k:sorted(v) for k, v in projects_to_types.iteritems()}

# a function to create a filter over a mapping
# like the one we created, the filter is a generator
def rules_filter_generator(original):
    # take a list of rules and filter out keys whose
    # values match any rule
    def filter_restricted(rules, minimum_length=2):
        # a set will give us better, more readable and faster code.
        # convert to tuples since list isn't hashable (mutable).
        rule_set = set(map(lambda x: tuple(sorted(x)), rules))
        for k, v in original.iteritems():
            if len(v) >= minimum_length and not tuple(v) in rule_set:
                yield k
    return filter_restricted

# use the filter specifically on the mapping we've created
generator = rules_filter_generator(projects_to_types)

# test (consume the generator to a list)
print list(generator([
    ['Task Type 3', 'Task Type 4'],
    ['Task Type 3', 'Task Type 3', 'Task Type 4']
]))

# prints: set(['Project 3', 'Project 1'])

rawDescription: Coming up with a list of duplicates that appear in list of list except under certain conditions


code:
import random

class property_getter(object):
    def __init__(self, getter):
        self.getter = getter

    def __get__(self, obj, cls):
        return self.getter(obj)

class Random(object):
    @property_getter
    def random_number(self):
        return random.randint(0,10)

x = Random()
print x.random_number
print x.random_number
x.random_number = 5
print x.random_number

rawDescription: Override @property getter in runtime

description: Override @property getter in runtime


code:
class TreeWidgetItem(QtGui.QTreeWidgetItem):
    def __init__(self, *args, **kwargs):
        QtGui.QTreeWidgetItem.__init__(self, *args, **kwargs)
        self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable)

    def setData(self, column, role, value):
        if role == QtCore.Qt.EditRole:
            # do important stuff here...
            print 'before: "%s", after: "%s"' % (
                self.text(column), value.toString())
        QtGui.QTreeWidgetItem.setData(self, column, role, value)

rawDescription: Tracking changes in QTreeWidget

description: Tracking changes in QTreeWidget


code:
import time
import sys

for i in range(5):
    print 'i=',i
    sys.stdout.flush()
    time.sleep(1)

rawDescription: calling python from R with instant output to console

description: Calling python from R with instant output to console


rawDescription: What is the equivalent of php's print_r() in python?


code:
import requests
r = requests.get("http://bit.ly/XXXX")
print r.url

rawDescription: Converting shortened URLs from different URL-shortening services back via Python


code:
import inspect
def compact(*args):
    d = {}
    for a in args:
        d[a] = inspect.stack()[1][0].f_locals[a]
    return d
def test():
    x = 5
    print compact('x')
test()

rawDescription: Python: Convert args to dict, like PHP's compact?


code:
class mylist(list):

    def __init__(self, name, age):
        super(mylist, self).__init__()
        self.name = name
        self.age = age

    def displaycount(self):
        print "total employees {0}".format(self.age)

emp1 = mylist('lada', 20)
emp1.displaycount()

rawDescription: Python no output from class method

description: Python no output from class method


code:
#!venv/bin/python2

import os
from django.conf import settings

if __name__ == '__main__' and __package__ is None:
    os.sys.path.append(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(__file__))))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rest_apis.settings")

import django

django.setup()
from django.core.mail import EmailMessage
from djcelery.models import TaskMeta
from datetime import datetime, timedelta, time

USERS_TO_NOTIFY = ['ops@yopmail.com']
TIME_THRESHOLD_INTERVAL = 60

def send_email(email_subject_line, email_body):
    email = EmailMessage(email_subject_line,
                         email_body,
                         settings.EMAIL_HOST_USER,
                         USERS_TO_NOTIFY
                         )
    email.send()

def main():
    current_time = datetime.now()   # Get Current TimeStamp
    time_threshold = current_time - timedelta(minutes=TIME_THRESHOLD_INTERVAL) # Get 60 minutes past current time stamp
    celery_taskmeta_objects = TaskMeta.objects.filter(status="FAILURE", date_done__gte=time_threshold)

    email_body = "Below are the tasks which failed : "
    if celery_taskmeta_objects.exists():
        for celery_taskmeta in celery_taskmeta_objects:
            print celery_taskmeta.task_id
            email_body += "\n\ntask_id : %s" % celery_taskmeta.task_id
            email_body += "\nstatus : %s" % celery_taskmeta.status
            email_body += "\ndate : %s" % celery_taskmeta.date_done
            email_body += "\ntraceback :"
            email_body += "\n%s\n\n" % celery_taskmeta.traceback
        email_subject_line = '[URGENT] Celery task failure in last %s minutes' % (TIME_THRESHOLD_INTERVAL)
        send_email(email_subject_line, email_body)

main()

rawDescription: Celery send mail for every failed task

description: Celery send mail for every failed task


code:
#!/usr/bin/env python

from igraph import Graph
from itertools import izip

def pairs(l):
    """Given a list l, returns an iterable that yields pairs of the form
    (l[i], l[i+1]) for all possible consecutive pairs of items in l"""
    return izip(l, l[1:])

def convert(g):
    # Get the old vertex names from g
    if "name" in g.vertex_attributes():
        old_names = map(str, g.vs["name"])
    else:
        old_names = map(str, xrange(g.vcount))

    # Get the outdegree vector of the old graph
    outdegs = g.outdegree()

    # Create a mapping from old node IDs to the ID of the first node in
    # the linear subgraph corresponding to the old node in the new graph
    new_node_id = 0
    old_to_new = []
    new_names = []
    for old_node_id in xrange(g.vcount()):
        old_to_new.append(new_node_id)
        new_node_id += outdegs[old_node_id]
        old_name = old_names[old_node_id]
        if outdegs[old_node_id] <= 1:
            new_names.append(old_name)
        else:
            for i in xrange(1, outdegs[old_node_id]+1):
                new_names.append(old_name + "." + str(i))

    # Add a sentinel element to old_to_new just to make our job easier
    old_to_new.append(new_node_id)

    # Create the edge list of the new graph and the weights of the new
    # edges
    new_edgelist = []
    new_weights = []

    # 1) Create the linear subgraphs
    for new_node_id, next_new_node_id in pairs(old_to_new):
        for source, target in pairs(range(new_node_id, next_new_node_id)):
            new_edgelist.append((source, target))
            new_weights.append(0)

    # 2) Create the new edges based on the old ones
    for old_node_id in xrange(g.vcount()):
        new_node_id = old_to_new[old_node_id]
        for edge_id in g.incident(old_node_id, mode="out"):
            neighbor = g.es[edge_id].target
            new_edgelist.append((new_node_id, old_to_new[neighbor]))
            new_node_id += 1
            print g.es[edge_id].source, g.es[edge_id].target, g.es[edge_id]["weight"]
            new_weights.append(g.es[edge_id]["weight"])

    # Return the graph
    vertex_attrs = {"name": new_names}
    edge_attrs = {"weight": new_weights}
    return Graph(new_edgelist, directed=True, vertex_attrs=vertex_attrs, \
            edge_attrs=edge_attrs)

rawDescription: Convert graph to have outdegree 1 (except extra zero weight edges)


rawDescription: How to convert tuple of tuples to pandas.DataFrame in Python?

description: Convert tuple of tuples to pandas.DataFrame in Python


code:
from Tkinter import *

def get_info():
    print e.index(INSERT)

root = Tk()
e = Entry(root)
e.pack()
Button(root, text="get info", command=get_info).pack()
root.mainloop()

rawDescription: Getting Cursor position in Tkinter entry widget


code:
from functools import wraps
from inspect import getcallargs

def decorator(f):
    @wraps(f)
    def new(**kwargs):
        # filter only those vars, that are in the list of function's named args
        from_globals = {arg: globals()[arg] for arg in getcallargs(f)}
        # overwrite them with user supplied kwargs
        from_globals.update(kwargs)
        f(**from_gobals)

    return new

@decorator
def f(a=None, b=None, c=None):
    print a, b, c

a = 1
b = 2
c = 3

f(a=2, b=4) # 2 4 3

rawDescription: Use global variables as default values

description: Use global variables as default values


code: print l[l.index("str1") + 1: l.index("str4")]

rawDescription: Getting a segment of a list

description: Getting a segment of a list


code:
def oddsquare(numbers):
    for n in numbers:
        if n % 2 == 1:
            print n**2
        else:
            return ""
while True:
    startingnumber1 = int(raw_input('Enter your starting number:'))
    endingnumber = int(raw_input('Enter your ending number:')) + 1
    list1 = range(startingnumber1, endingnumber)
    oddsquare(list1)

rawDescription: Python - Can't figure out how to re-run my program


code:
#!/usr/bin/python
import cmd
from gi.repository import Gtk
import threading

"""
The bottom of this file has all the goodies for this solution.
Scroll down to see the cool stuff.
"""
class WindowWidget(object):

    def __init__(self):
        win = Gtk.Window()
        win.connect("delete-event", Gtk.main_quit)
        win.show_all()
        self.window = win;
    def create_button(self):
        self.button = Gtk.Button(label="Click Here")
        self.button.connect("clicked", self.on_button_clicked)
        self.window.add(self.button)
        self.window.show_all()

    def on_button_clicked(self, widget):
        print 'something happened'
        return

class InteractiveShell(cmd.Cmd):
    #Simple command processor example.
    prompt='>'
    def __init__(self, gtk_object):
        cmd.Cmd.__init__(self)
        # or cmd.Cmd.__init__(self)
        self.gtk_object = gtk_object

    def do_greet(self, line):
        print "hello"

    def do_setbutton(self, line):
        """
        Using GLib.idle_add to insert a callback into the Gtk loop.
        This makes synchronizing the main thread where gtk is at, and
        this second thread easier.
        """
        GLib.idle_add(self.idle_setbutton)

    def idle_setbutton(self):
        self.gtk_object.create_button()

    def do_exit(self, line):
        """Close the gtk main loop, and return True to exit cmd."""

        print 'Exiting me.'
        return True

    def postloop(self):
        print 'Closing window.'
        if Gtk.main_level() > 0:
            Gtk.main_quit()

def worker(s):
    s.cmdloop()
    return

#isn't required
#def worker2(num):
#    Gtk.main()

if __name__ == '__main__':

    ww = WindowWidget()
    mainshell = InteractiveShell(ww)
    #This list is superfluous
    #threads = []

    def quit_all(event, user_data):
        print 'x out of window. Trying to exit shell.'
        #I don't know if the next line is needed.
        #It's here just in case.
        Gtk.main_quit()
        #offending line cmdloop won't exit
        #but now it will because of sys.exit
        r = mainshell.onecmd('exit')

        #Problem found with sys.exit
        #When this program closes terminal input is working, but
        #it's blank. Here comes the fix.
        #Works, but it is messy -> os.system('reset')
        #Does not work -> Popen('reset', shell=True)
        #The command 'stty sane' is the best way to keep the
        #terminal from having blank input after closing.
        #os.system('stty sane')
        #Actually use Popen for this.
        Popen('stty sane', shell=True)
        #alternatively
        #os.system('stty echo')
        #sys.exit can only exit the whole program from the main thread.
        sys.exit(0)
        #Threads are closing, and so is the window.
        #This is a clean close.
        return False

    ww.window.connect("delete-event", quit_all)

    t = threading.Thread(target=worker, args=(mainshell))
    #No need to create a thread for Gtk.main
    #threads.append(t)
    #t2 = threading.Thread(target=worker2, args=(2,))
    #threads.append(t2)
    t.start()
    #t2.start()
    #Gtk.main needs to be called after the second thread is started.
    #This will keep Gtk from blocking the second thread.
    #Gtk.main loop in the python main thread is easier to deal with
    #to make sure that work for Gtk will get finished.
    #Can use GLib.idle_add to insert functions, and methods into
    #the Gtk loop.
    Gtk.main()

rawDescription: Why won't cmd.cmdloop stop with cmd.onecmd in Python?


code:
c=subprocess.Popen("lsmod",stdout=subprocess.PIPE)
gr=subprocess.Popen(["grep" ,"ath"],stdin=c.stdout,stdout=subprocess.PIPE)
print gr.communicate()[0]

rawDescription: Read feedback from commands?

description: Read feedback from commands


rawDescription: image segmentation by k means clustering in python

description: Image segmentation by k means clustering in python


code:
# open & read raw file
f=open(args[0], 'r')
text = f.read()

#
text = map(lambda s: s.split(" ", 1), text.strip().replace("'","").splitlines())
tree = [{'prefix': '', 'children':[]}]
stack = [tree[0]]

for id, attr in text:
    while not id.startswith(stack[-1]['prefix']):
        stack.pop()
    node = {'prefix': id, 'attr': attr, 'children': []}
    stack[-1]['children'].append(node)
    stack.append(node)

pprint.pprint( tree)

print json.dumps( tree)
f=open(args[1], 'w')
f.write(json.dumps( tree, sort_keys=True, indent=1))

rawDescription: Building a json tree structure from identifiers


code:
from System import DateTime, TimeSpan, DayOfWeek

endDate = DateTime.Now
startDate = DateTime.Now
span = TimeSpan()
daySpan = span.FromDays(1)

while startDate.DayOfWeek != DayOfWeek.Monday:
    startDate -= daySpan

print startDate, endDate

rawDescription: Workweek start and end date

description: Workweek start and end date


code:
def unpack_args(func):
    def deco_func(*args):
        if isinstance(args, tuple):
            args = args[0]

        return func(*args)

    return deco_func

def func1(*args):
    return args

def func2(*args):
    return args

@unpack_args
def func3(*args):
    return args

print func1(1,2,3)    # > (1,2,3)
print func2(1,2,3)    # > (1,2,3)
print func1(*func2(1,2,3))    # > (1,2,3)
print func1(func2(1,2,3))    # > ( (1,2,3), )
print func3(func2(1,2,3))   # > (1,2,3)

rawDescription: Python list as *args?

description: Python list as *args


code:
tokenValue = request.headers.get("token")

if tokenValue == None:
  return "x-auth-token not passed in header, please pass the token."
else:
  print "Token passed is", tokenValue

rawDescription: Parse Header in Post Call Python

description: Parse Header in Post Call Python


code:
import memcache

def fib(n):
    v = mc.get(str(n))
    if not v is None:
        print "Hit on cache: %s = %s" %(n, v)
        return v

    print "Miss on cache"
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        v = fib(n-1) + fib(n-2)
        mc.set(str(n), v)
        return v

mc = memcache.Client(['127.0.0.1:11211'], debug=0)
x=input('which fibonnaci do you want?')
print fib(x)

rawDescription: Explain how to use memory caches in Python


code:
import os
print os.popen('php YourScript.php').read()

rawDescription: How to include output of PHP script in Python driven Plone site?

description: Include output of PHP script in Python driven Plone site


code:
if any(all(x in y for x in al) for y in bl):
    print 'exist'

rawDescription: How to find a list in another list with some different request

description: Find a list in another list with some different request


code:
lst = [['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]

def count(lst):
    # declare dictionary that we are going to return
    foo = {}
    # iterate sublist
    for sublist in lst:
        # make sublist into unique element list
        sublist = list(set(sublist))
        for element in sublist:
            # if element found in foo dic, increment
            if element in foo:
                foo[element] += 1
            # else, init with 1
            else:
                foo[element] = 1
    return foo

res = count(lst)
print res

rawDescription: count sublists that has a specific term in python

description: Count sublists that has a specific term in python


code:
import sys
for line  in iter(namedPipe.readline, ''):
  print line

rawDescription: Avoid buffered reading on "for line in ..."


code:
from itertools import groupby
import operator
#input
l = [['Cautus  B.V.', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['Slever ', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T. Slever']]
#script
sortkey = operator.itemgetter(1,4)
l_clean = sorted(l,key=sortkey)

l_final = [(k, list(v)) for k,v in groupby(l_clean, key = operator.itemgetter(1))]

for k,v in l_final:
   info_rest = v[0][:4]+v[0][5:]
   info_combine = map(operator.itemgetter(4),v)
   print k,info_combine,info_rest

rawDescription: Add extra information to the zip

description: Add extra information to the zip


code:
details = [x for x in myList if x[0] == prompt]
if details:
    print details

rawDescription: Trouble printing a list within a list

description: Trouble printing a list within a list


code:
import copy
T = Test(20)
T1 = copy.copy(T)
T1.a = 40
print T.a
# Output 20
print T1.a
# Output 40

rawDescription: Print Pass by Value in Python?

description: Print Pass by Value in Python


code:
import types, copy_reg
def code_ctor(*args):
    # delegate to new.code the construction of a new code object
    return types.CodeType(*args)
def reduce_code(co):
    # a reductor function must return a tuple with two items: first, the
    # constructor function to be called to rebuild the argument object
    # at a future de-serialization time; then, the tuple of arguments
    # that will need to be passed to the constructor function.
    if co.co_freevars or co.co_cellvars:
        raise ValueError, "Sorry, cannot pickle code objects from closures"
    return code_ctor, (co.co_argcount, co.co_nlocals, co.co_stacksize,
        co.co_flags, co.co_code, co.co_consts, co.co_names,
        co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno,
        co.co_lnotab)
# register the reductor to be used for pickling objects of type 'CodeType'
copy_reg.pickle(types.CodeType, reduce_code)
if __name__ == '__main__':
    # example usage of our new ability to pickle code objects
    import cPickle
    # a function (which, inside, has a code object, of course)
    def f(x): print 'Hello,', x
    # serialize the function's code object to a string of bytes
    pickled_code = cPickle.dumps(f.func_code)
    # recover an equal code object from the string of bytes
    recovered_code = cPickle.loads(pickled_code)
    # build a new function around the rebuilt code object
    g = types.FunctionType(recovered_code, globals( ))
    # check what happens when the new function gets called
    g('world')

rawDescription: What are the arguments to the types.CodeType() python call?


code:
previous = 2
n = 0
for i in range(3,10000000):
    if Is_Prime(i):
        n = n+1
        current  = i
        gn = current - previous
        previous = i
        if n % 1000 == 0:
            print "Now at the gap between prime",n,"and prime",n+1
            average_gap = (i-2)/float(n);
            # (n+1)st prime is i, 1st prime is 2. n gaps.
            # float(n) so that we get float, not int division!
            print "Average gap so far:", average_gap
            print "ln(p)/average_gap:", math.log(i) / average_gap
        if n % 100000 == 0:
            break

rawDescription: Prime Number Theorem Python

description: Prime Number Theorem Python


code:
from flask import request
from werkzeug.useragents import UserAgent

user_agent = UserAgent(request.headers.get('User-Agent'))
print user_agent.platform

rawDescription: How can I know if a user of my Flask WebApp use a Windows or Linux machine?

description: Know if a user of my Flask WebApp use a Windows or Linux machine


code:
def dupechecker():

 t = texttable.Texttable()
 t.header(["Category", "Number of dupe values", "Value which appears multiple times"])
 for key, value in d.iteritems():
   has_dupe = False
   for x, y in collections.Counter(value).items():
     if y > 1:
      has_dupe = True
      t.add_row([key, y, x])
   if not has_dupe:
      t.add_row([key, 0, ''])
 print t.draw()

rawDescription: Identify duplicate values in dictionary and print in a table


code:
#!/usr/bin/python

import readline
import logging
import signal
import os

LOG_FILENAME = '/tmp/completer.log'
HISTORY_FILENAME = '/tmp/completer.hist'

logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)

class YouAreTooSlow(Exception): pass

def get_history_items():
    return [ readline.get_history_item(i)
             for i in xrange(1, readline.get_current_history_length() + 1)
             ]

class HistoryCompleter(object):

    def __init__(self):
        self.matches = []
        return

    def complete(self, text, state):
        response = None
        if state == 0:
            history_values = get_history_items()
            logging.debug('history: %s', history_values)
            if text:
                self.matches = sorted(h
                                      for h in history_values
                                      if h and h.startswith(text))
            else:
                self.matches = []
            logging.debug('matches: %s', self.matches)
        try:
            response = self.matches[state]
        except IndexError:
            response = None
        logging.debug('complete(%s, %s) => %s',
                      repr(text), state, repr(response))
        return response

def input_loop():
    if os.path.exists(HISTORY_FILENAME):
        readline.read_history_file(HISTORY_FILENAME)
    print 'Max history file length:', readline.get_history_length()
    print 'Startup history:', get_history_items()
    try:
        while True:
            line = raw_input('Prompt ("stop" to quit): ')
            if line == 'stop':
                break
            if line:
                print 'Adding "%s" to the history' % line
    finally:
        print 'Final history:', get_history_items()
        readline.write_history_file(HISTORY_FILENAME)

# Register our completer function

def slow_handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise YouAreTooSlow()

def pre_input_hook():
    signal.signal(signal.SIGALRM, slow_handler)
    signal.alarm(10)

readline.set_pre_input_hook(pre_input_hook)
readline.set_completer(HistoryCompleter().complete)

# Use the tab key for completion
readline.parse_and_bind('tab: complete')

# Prompt the user for text
input_loop()

rawDescription: Python3: readline equavalent in select.select()


code:
def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind (current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

rawDescription: Zed Shaw exercise 20 doesn't work

description: Zed Shaw exercise 20 doesn't work


code:
from scrapy import Selector
sel = Selector(text='
I am a product!
')
print sel.css('.product').extract()

rawDescription: Scrapy grab div with multiple classes?

description: Scrapy grab div with multiple classes


code:
#!/usr/bin/python
import sys
import socket

file_nm = sys.argv[1]
with open(file_nm, 'r') as f:
  for host in f:
    print socket.gethostbyname(host.strip())

rawDescription: Using python socket.gethostbyname to accept multiple arguments in the command Prompt


code:
def my_gen(generators):
    while True:
        rez = ()
        for gen in generators:
            try:
                rez = rez + (gen.next(),)
            except StopIteration:
                pass
        if rez:
            yield rez
        else:
            break

print [x for x in my_gen((iter(xrange(2)), iter(xrange(3)), iter(xrange(1))))]

[(0, 0, 0), (1, 1), (2,)] #output

rawDescription: Traversing a sequence of generators

description: Traversing a sequence of generators


code:
import numpy
import glob

for f in glob.glob("*.csv"):
    print f

    r = numpy.recfromcsv(f)
    print numpy.interp(5, r.depth, r.temp)

rawDescription: basic python vlookup equivalent

description: Basic python vlookup equivalent


code:
if  'A' is team:
    print 'TEAM A\n', 'At Position ', position , TeamA[position - 1]
if  'B' is team:
    print 'TEAM B\n', 'At Position ', position , TeamB[position - 1]

rawDescription: Selecting a position from a lists using Python


code:
import Image

images = ['image.jpg','image.jpg','image.jpg','image.jpg','image.jpg','image.jpg','image.jpg']

hsize = min(5,len(images))
vsize = (len(images)/5) + 1

print hsize,vsize

vspace = 10
hspace = 10

(h,w) = Image.open(images[0]).size

im = Image.new('RGB',((hsize*(h+hspace)),(vsize*(w+vspace)) ))

for i,filename in enumerate(images):
    imin = Image.open(filename).convert('RGB')
    xpos = i % hsize
    ypos = i / hsize
    print xpos,ypos
    im.paste(imin,(xpos*(h+hspace),ypos*(w+vspace)))
im.save('output.jpg')

rawDescription: Drawing multiple Images to a tiff file in python


code:
def import_class(modulename, classname):
    ''' Returns imported class. '''
    try:
        return getattr(__import__(modulename, globals(), locals(), [classname], -1), classname)
    except AttributeError:
        print 'Error in importing class. "%s" has no class "%s"' % (modulename, classname)
        return None
    except ImportError as e:
        print 'Error in importing class: %s' % (e)
        return None

rawDescription: Rewrite from x import y statement to use import_module


rawDescription: Access my data structure in Python

description: Access my data structure in Python


code:
import lxml
from lxml import etree
source_xml = " path to your xml file"
et = etree.parse(source_xml)
value = et.xpath("//document/request/pair[name='q']/value/text()")
print " ".join(value)

rawDescription: Removing garbage characters from line / Robust XML parser in python


code:
import inspect
import sys

def logging_tracer(frame, event, arg):
    lines, firstline = inspect.getsourcelines(frame)

    def local_tracer(local_frame, event, arg):
        if event == 'line' and frame is local_frame:
            print event, frame.f_lineno,'\t', lines[frame.f_lineno - firstline]
            #print event, lines[frame.f_lineno - firstline]
            #print frame.f_code.co_name, frame.f_lineno, event, arg

    if frame.f_code in LOG_THESE_FUNCTIONS:
        print event, frame.f_lineno,'\t', lines[frame.f_lineno - firstline + (event == 'call')]
        #print frame.f_code.co_name, event, arg
        return local_tracer
    else:
        return None

LOG_THESE_FUNCTIONS = set()

def loggingdecorator(func):
    LOG_THESE_FUNCTIONS.add(func.func_code)

    def _wrapper():
        old_trace_function = sys.gettrace()
        sys.settrace(logging_tracer)
        try:
            result = func()
        except:
            raise
        else:
            return result
        finally:
            sys.settrace(old_trace_function)
    return _wrapper

rawDescription: Decorator to log function execution line by line


code:
q = request.GET.get('q')
if q:
    print q

rawDescription: How to avoid writing request.GET.get() twice in order to print it?

description: Avoid writing request.GET.get() twice in order to print it


code:
response = requests.get('[url here]')
print type(response)  #  <class 'requests.models.Response'>

rawDescription: Some help understanding my own Python code


code:
def is_prime2(n, div = None):
    """Recursive primality test"""
    if div is None:
        div = n - 1
    while div >= 2:
        if n % div == 0:
            print 'No, {number} is not prime.'.format(number = n, div = div)
            return False
        else:
            return is_prime2(n, div-1)
    else:
        print 'Yes, {number} is prime indeed.'.format(number = n)
        return 'True'

is_prime2(4)
is_prime2(5)
is_prime2(6)

rawDescription: Python: Optional parameter default dependent on passed argument value


code:
rows = soup.select("table.MsoTableGrid tr")
for r in rows:
    faculty_info = r.find_all("td")[1:3]
    if len(faculty_info) == 2:
        print faculty_info[0].text.strip(), faculty_info[1].text.strip()

rawDescription: How can I extract text from a span tag using beautiful soup 4?

description: Extract text from a span tag using beautiful soup 4


code:
yourstring= '"ABC", "XYZ,MNO", "KLM"\n"ABC", "MN"\n"ABC", "123", "10", "OPPA GANGNAM STYLE", "LADY"'

import csv
import StringIO

class MyDialect(csv.Dialect):
    strict = True
    skipinitialspace = True
    quoting = csv.QUOTE_ALL
    delimiter = ','
    quotechar = '"'
    lineterminator = '\n'

b=StringIO.StringIO(yourstring)
r=csv.reader(b,MyDialect())

for i in r:
    print len(i), ':',' @ '.join(i)

rawDescription: Python - how to read/parse csv like line?


code:
if 0:
    print 'a'
elif 1:
    print 'b'

# prints 'b'

rawDescription: how does randint() evaluate in an if, and else statement?

description: How does randint() evaluate in an if, and else statement


code:
a = 'change these letters äöå'
b = map( (lambda x: a.replace(x, "{0}.".format(x))), 'å ä ö'.split())
for c in b:
  print c

rawDescription: String characters misinterpreted when iterating over string with unicode chars


code:
from os.path import exists

ready = 0
files = [somefile_1, somefile_2, somefile_3]

while not all(exists(f) for f in files):
    print 'There are some files missing. Restarting script.'
    sleep(300)
    removePIDfile()
    execfile(r'D:\Workspace\tools\PKG_Maker\PKG_Maker.py')

print 'Files downloaded'

rawDescription: Python "while" loop with an "if" statement


code:
from collections import defaultdict
mailAddressList = ['chip@plastroltech.com',
    'spammer@example.test',
    'webdude@plastroltech.com',
    'spammer@example.test',
    'spammer@example.test',
    'support@plastroltech.com'
]

index = defaultdict(list)
for i, email in enumerate(mailAddressList):
    index[email].append(i)

print [(email, positions) for email, positions in index.items()
                        if len(positions) > 1]
# [('spammer@example.test', [1, 3, 4])]

rawDescription: Find multiple occuring string in array and output index


code:
import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

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

print "serving at port", PORT
httpd.serve_forever()

rawDescription: Python Show webpage on connect to server


code: print type(some_model)

rawDescription: Django - Find out which model the variable belongs to


code:
for eachname in names:
    d = ' '.join(eachname.string.split()) + '\n'
    print d
    myfile.write(d)

rawDescription: Remove 'u from a webscrape output

description: Remove 'u from a webscrape output


code:
obj=SomePythonObject()
list_of_methods=dir(obj)
for i in list_of_methods:
    try:
        item = getattr(obj, i, None)
        if callable(item):
            item()
    except:
        print i,'failed'

rawDescription: Is there a way to use methods dynamically within a for loop in python?


code:
def get_player_choice(choices):
    labels,actions = zip(*choices)
    choice_actions = dict(enumerate(actions,1))
    for choice_id,label in enumerate(labels,1):
        print "%d. %s"%(choice_id,label)
    while True:
        result = raw_input("Select Option:")
        try:
            return choice_actions[int(result)]
        except TypeError:
            print "ERROR: Please enter a number choice!"
        except KeyError:
            print "ERROR: Unknown Choice!"

def LookAround():
     print "You See Something!"
def Quit():
     print sys.exit

menu_a = [
("Look Around",LookAround),
("Quit!",Quit)
       ]
fn = get_player_choice(menu_a)
fn()

rawDescription: Designing an RPG for class, stuck with my loops


code:
import requests
import xml.etree.ElementTree as et

req = requests.get('http://www.agenzia-interinale.it/sitemap-5.xml')
root = et.fromstring(req.content)
for i in root:
    print i[0].text  # the <loc> text

rawDescription: Using Beautifulsoup in Python to iterate over non href links within an xml and retrieve specific information


code:
from ID3 import *
try:
    id3info = ID3('/some/file/moxy.mp3')
    print id3info
    id3info['TITLE'] = "Green Eggs and Ham"
    id3info['ARTIST'] = "Moxy Früvous"
    for k, v in id3info.items():
        print k, ":", v
except InvalidTagError, message:
    print "Invalid ID3 tag:", message

rawDescription: Python: How can I access an mp3 file's metadata using Python?


code:
import boto
print boto.config.get_value('Credentials', 'aws_access_key_id')

rawDescription: Unable to switch between two boto.cfg files