code:
class CustomNCPAuthBackend(object):
    """
    This is custom authentication backend.
    Authenticate against the webservices call.

    The method below would override authenticate() of django.contrib.auth
    """
    def authenticate_password(self, username=None, password=None):
        print "inside authenticate of username and password with username being : "+username
        return None

    def authenticate_token(self,token=None):
        print "inside authenticate of token with token being : "+token
        return None

    def authenticate(self, token=None, username=None, password=None):
        if token is not None:
             return self.authenticate_token(token)
        else:
             return self.authenticate_password(username, password)

rawDescription: django custom authentication backend with custom middleware (both username,password and token absed auth)

description: Django custom authentication backend with custom middleware (both username,password and token absed auth)


code:
import string
import random
import time

print('Welcome to the password checker')
characters = string.ascii_lowercase + string.digits + string.ascii_uppercase + \
             ' .,!?;:`"+/*()@#$&_-='
characters = list(characters)
random.shuffle(characters)

password = input("Enter your password: ")
tryFirst = characters.pop() * len(password)

Finished = False

attempt = 0
while not Finished:
    print("{}) {}".format(attempt, tryFirst))
    trySecond = ''
    Finished = True
    try:
        char = characters.pop()
    except IndexError:
        print 'No more characters to check'
    for num in range(len(password)):
        if tryFirst[num] != password[num]:
            Finished = False
            trySecond += char
        else:
            trySecond += password[num]
    attempt += 1
    tryFirst = trySecond
    time.sleep(0.05)
print("Password found! That took {} attempts".format(attempt))
if attempt < 100:
    print("Your password isn't safe")
elif attempt < 250:
    print("Your password is quite safe, could do with improvement")
elif attempt < 400:
    print("Your password is adequately safe")
elif attempt < 600:
    print("Your password is safe")

rawDescription: Password Checker

description: Password Checker


code:
import ast

L = ["hello", "3", "3.64", "-1"]

def tryeval(val):
  try:
    val = ast.literal_eval(val)
  except ValueError:
    pass
  return val

print [tryeval(x) for x in L]

rawDescription: converting python list of strings to their type

description: Converting python list of strings to their type


code:
db.foo.insert({
'date': '2011-04-01',
'region': 'NY',
'ranking': [
{ 'item': 'Coca-Cola', 'price': 1.00, 'version': 1 },
{ 'item': 'Diet Coke', 'price': 1.25, 'version': 1 },
{ 'item': 'Diet Pepsi', 'price': 1.50, 'version': 1 },
]})

db.foo.insert({
'date': '2011-05-01',
'region': 'NY',
'ranking': [
{ 'item': 'Diet Coke', 'price': 1.25, 'version': 1 },
{ 'item': 'Coca-Cola', 'price': 1.00, 'version': 1 },
{ 'item': 'Diet Pepsi', 'price': 1.50, 'version': 1 },
]})

db.foo.insert({
'date': '2011-06-01',
'region': 'NY',
'ranking': [
{ 'item': 'Coca-Cola', 'price': 1.00, 'version': 1 },
{ 'item': 'Diet Pepsi', 'price': 1.50, 'version': 1 },
{ 'item': 'Diet Coke', 'price': 1.25, 'version': 1 },
]})

def position_of(item, ranking):
    for i, candidate in enumerate(ranking):
            if candidate['item'] == item:
                    return i
    return None

print [position_of('Diet Coke', x['ranking'])
       for x in db.foo.find({'ranking.item': 'Diet Coke'}, ['ranking.item'])]

# prints [1, 0, 2]

rawDescription: mongodb python get the element position from an array in a document

description: Mongodb python get the element position from an array in a document


code:
NUM_ANSWERS = 4
NUM_COMBOS = 2**NUM_ANSWERS
NO,YES = 'no','yes'

def index(answers):
    """ Convert a list of yes/no answers into binary number. """
    binstr = ''.join([('1' if a is 'yes' else '0') for a in answers])
    return int(binstr, 2)

def answers(index):
    """ Convert binary value of number into list of yes/no answers. """
    masks = [2**p for p in range(NUM_ANSWERS-1, -1, -1)]
    bits = [((index & m) / m) for m in masks]
    return [[NO,YES][b] for b in bits]

# condition expressions
cond_expr1 = lambda a1,a2,a3,a4: a1 is YES and a2 is NO  # a3,a4 ignored
cond_expr2 = lambda a1,a2,a3,a4: (
                ( a1 is YES and a2 is NO ) or ( a3 is YES and a4 is NO )
             )
# build tables for each condition
cond1 = []
cond2 = []
for i in range(NUM_COMBOS):
    ans_combo = answers(i)
    cond1.append( cond_expr1(*ans_combo) )
    cond2.append( cond_expr2(*ans_combo) )

# once tables are built, you can lookup the corresponding conditional
print cond1[ index(['yes', 'no', 'no', 'yes']) ]  # True
print cond2[ index(['yes', 'no', 'yes', 'no']) ]  # True

rawDescription: python: how to validate userdefined condition?

description: Python: how to validate userdefined condition


code:
s = 'caaat caat cat kittycaat, rack, smaaack'
x = 0
for i in s.split():
    if i.count('a') > x:
        x = i.count('a')
        word = i
print word

rawDescription: Searching the word in a list with more a's


code:
import urllib
import urllib2

url = 'https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction'

values = {'inUserName' : 'USER',
          'inUserPass' : 'PASSWORD'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)

req.add_header('Host', 'www.locationary.com')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
req.add_header('Accept-Language', 'en-us,en;q=0.5')
req.add_header('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.7')
req.add_header('Connection','keep-alive')
req.add_header('Referer','http://www.locationary.com/')
req.add_header('Cookie','site_version=REGULAR; __utma=47547066.1079503560.1321924193.1322707232.1324693472.36; __utmz=47547066.1321924193.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; locaCountry=1033; locaState=1795; locaCity=Montreal; jforumUserId=1; PMS=1; TurnOFfTips=true; Locacookie=enable; __utma=47547066.1079503560.1321924193.1322707232.1324693472.36; __utmz=47547066.1321924193.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; PMS=1; __utmb=47547066.15.10.1324693472; __utmc=47547066; JSESSIONID=DC7F5AB08264A51FBCDB836393CB16E7; PSESSIONID=28b334905ab6305f7a7fe051e83857bc280af1a9; __utmc=47547066; __utmb=47547066.15.10.1324693472; ACTION_RESULT_CODE=ACTION_RESULT_FAIL; ACTION_ERROR_TEXT=java.lang.NullPointerException')
req.add_header('Content-Type','application/x-www-form-urlencoded')

response = urllib2.urlopen(req)
page = response.read()

print page

rawDescription: Why isn't my Python program working? It uses HTTP Headers


code:
import subprocess
sp = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
output, _ = sp.communicate()
print "Status:", sp.wait()
print "Output:"
print output

rawDescription: Redirect an output command to a variable or file?


code:
import types
import opcode

# Opcode constants used for comparison and replacecment
LOAD_FAST = opcode.opmap['LOAD_FAST']
LOAD_GLOBAL = opcode.opmap['LOAD_GLOBAL']
STORE_FAST = opcode.opmap['STORE_FAST']

DEBUGGING = True

def append_arguments(code_obj, new_locals):
    co_varnames = code_obj.co_varnames   # Old locals
    co_names = code_obj.co_names      # Old globals
    co_argcount = code_obj.co_argcount     # Argument count
    co_code = code_obj.co_code         # The actual bytecode as a string

    # Make one pass over the bytecode to identify names that should be
    # left in code_obj.co_names.
    not_removed = set(opcode.hasname) - set([LOAD_GLOBAL])
    saved_names = set()
    for inst in instructions(co_code):
        if inst[0] in not_removed:
            saved_names.add(co_names[inst[1]])

    # Build co_names for the new code object. This should consist of
    # globals that were only accessed via LOAD_GLOBAL
    names = tuple(name for name in co_names
                  if name not in set(new_locals) - saved_names)

    # Build a dictionary that maps the indices of the entries in co_names
    # to their entry in the new co_names
    name_translations = dict((co_names.index(name), i)
                             for i, name in enumerate(names))

    # Build co_varnames for the new code object. This should consist of
    # the entirety of co_varnames with new_locals spliced in after the
    # arguments
    new_locals_len = len(new_locals)
    varnames = (co_varnames[:co_argcount] + new_locals +
                co_varnames[co_argcount:])

    # Build the dictionary that maps indices of entries in the old co_varnames
    # to their indices in the new co_varnames
    range1, range2 = xrange(co_argcount), xrange(co_argcount, len(co_varnames))
    varname_translations = dict((i, i) for i in range1)
    varname_translations.update((i, i + new_locals_len) for i in range2)

    # Build the dictionary that maps indices of deleted entries of co_names
    # to their indices in the new co_varnames
    names_to_varnames = dict((co_names.index(name), varnames.index(name))
                             for name in new_locals)

    if DEBUGGING:
        print "injecting: {0}".format(new_locals)
        print "names: {0} -> {1}".format(co_names, names)
        print "varnames: {0} -> {1}".format(co_varnames, varnames)
        print "names_to_varnames: {0}".format(names_to_varnames)
        print "varname_translations: {0}".format(varname_translations)
        print "name_translations: {0}".format(name_translations)

    # Now we modify the actual bytecode
    modified = []
    for inst in instructions(code_obj.co_code):
        # If the instruction is a LOAD_GLOBAL, we have to check to see if
        # it's one of the globals that we are replacing. Either way,
        # update its arg using the appropriate dict.
        if inst[0] == LOAD_GLOBAL:
            print "LOAD_GLOBAL: {0}".format(inst[1])
            if inst[1] in names_to_varnames:
                print "replacing with {0}: ".format(names_to_varnames[inst[1]])
                inst[0] = LOAD_FAST
                inst[1] = names_to_varnames[inst[1]]
            elif inst[1] in name_translations:
                inst[1] = name_translations[inst[1]]
            else:
                raise ValueError("a name was lost in translation")
        # If it accesses co_varnames or co_names then update its argument.
        elif inst[0] in opcode.haslocal:
            inst[1] = varname_translations[inst[1]]
        elif inst[0] in opcode.hasname:
            inst[1] = name_translations[inst[1]]
        modified.extend(write_instruction(inst))

    code = ''.join(modified)
    # Done modifying codestring - make the code object

    return types.CodeType(co_argcount + new_locals_len,
                          code_obj.co_nlocals + new_locals_len,
                          code_obj.co_stacksize,
                          code_obj.co_flags,
                          code,
                          code_obj.co_consts,
                          names,
                          varnames,
                          code_obj.co_filename,
                          code_obj.co_name,
                          code_obj.co_firstlineno,
                          code_obj.co_lnotab)

def instructions(code):
    code = map(ord, code)
    i, L = 0, len(code)
    extended_arg = 0
    while i < L:
        op = code[i]
        i+= 1
        if op < opcode.HAVE_ARGUMENT:
            yield [op, None]
            continue
        oparg = code[i] + (code[i+1] << 8) + extended_arg
        extended_arg = 0
        i += 2
        if op == opcode.EXTENDED_ARG:
            extended_arg = oparg << 16
            continue
        yield [op, oparg]

def write_instruction(inst):
    op, oparg = inst
    if oparg is None:
        return [chr(op)]
    elif oparg <= 65536L:
        return [chr(op), chr(oparg & 255), chr((oparg >> 8) & 255)]
    elif oparg <= 4294967296L:
        return [chr(opcode.EXTENDED_ARG),
                chr((oparg >> 16) & 255),
                chr((oparg >> 24) & 255),
                chr(op),
                chr(oparg & 255),
                chr((oparg >> 8) & 255)]
    else:
        raise ValueError("Invalid oparg: {0} is too large".format(oparg))

if __name__=='__main__':
    import dis

    class Foo(object):
        y = 1

    z = 1
    def test(x):
        foo = Foo()
        foo.y = 1
        foo = x + y + z + foo.y
        print foo

    code_obj = append_arguments(test.func_code, ('y',))
    f = types.FunctionType(code_obj, test.func_globals, argdefs=(1,))
    if DEBUGGING:
        dis.dis(test)
        print '-'*20
        dis.dis(f)
    f(1)

rawDescription: Python function local name binding from an outer scope


code:
class Parent(object):
    def __init__(self, a, b):
        print 'a', a
        print 'b', b

class Child(Parent):
    def __init__(self, c, d, *args, **kwargs):
        print 'c', c
        print 'd', d
        super(Child, self).__init__(*args, **kwargs)

test = Child(1,2,3,4)

rawDescription: How to override constructor of Python class with many arguments?

description: Override constructor of Python class with many arguments


code:
import re
s = '(3,1)-[(7,2),1,(a,b)]-8a'
pattern = r'(\(.+?\))'
print re.findall(pattern, s)

rawDescription: Python regex to find characters inside delimiters


code:
regex = '(?P<first>"Tom":{"c":(.+?),"b":(.+?),"a":(.+?)})|(?P<second>"Tom":{"a":(.+?),"c":(.+?),"b":(.+?)})'
# Careful as this raises an exception if no match was found
m = re.search( regex, somestring ).groupdict()
if m['first']:
    print 'First'
else:
    print 'Second'

rawDescription: Conditionals and regex

description: Conditionals and regex


code:
d = {'key':'value'}
print d
d['mynewkey'] = 'mynewvalue'
print d

rawDescription: Add key to a dictionary in Python?

description: Add key to a dictionary in Python


code:
IMAGE_NAME = []
IMAGE_DATA = []

import sys
import wx

def deletepic(self):
    try:
        self.parent.bitmap.Destroy()
    except:
        print sys.exc_info()

def sendnewpic(self):
    if self.parent.bitmap: deletepic(self)
    if IMAGE_DATA[self.image_listsel] != '':
        try:
            print IMAGE_DATA[self.image_listsel]
            bmp = wx.Image(IMAGE_DATA[self.image_listsel], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.parent.scroll_img.SetScrollbars(1, 1, bmp.GetWidth(), bmp.GetHeight())
            self.parent.bitmap = wx.StaticBitmap(self.parent.scroll_img, -1, bmp, (0, 0))
            self.parent.Refresh()
        except:
            pass

def areachange(self, pg):
    print pg
    try:
        if IMAGE_DATA[self.image_listsel] == '':
            deletepic(self)
    except:
        pass

    if pg == "Images":
        self.images_area.Show()
    else:
        self.images_area.Hide()

class imageTab(wx.Panel):

    def __init__(self, parent, grandparent):
        wx.Panel.__init__(self, parent)
        self.parent = grandparent
        self.image_listsel = 0
        self.listBox = wx.ListBox(self, size=(200, -1), choices=IMAGE_NAME, style=wx.LB_SINGLE)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.VERTICAL) #change to horizontal for side by side
        self.sizerMain = wx.BoxSizer()
        self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
        self.listBox.Bind(wx.EVT_LISTBOX, self.imagesel)
        btn = wx.Button(self, label="Create New",size=(200, 40))
        btnTwo = wx.Button(self, label="Test 2",size=(200, 40))
        btn.Bind(wx.EVT_BUTTON, self.newAddImage)
        self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
        btnSizer.Add(btn, 0, wx.ALL, 5)
        btnSizer.Add(btnTwo, 0, wx.ALL, 5)
        self.sizer.Add(btnSizer)
        self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=0)
        self.SetSizer(self.sizerMain)

    def imagesel(self, evt):
        self.image_listsel = self.listBox.GetSelection()
        sendnewpic(self)

    def newAddImage(self, evt):
        IMAGE_NAME.append('hi')
        IMAGE_DATA.append('')
        self.listBox.Set(IMAGE_NAME)
        self.listBox.SetSelection(len(IMAGE_NAME)-1)
        self.imagesel(None) #making it a selected image, globally

    def reName(self,parent):
        sel = self.listBox.GetSelection()
        text = self.listBox.GetString(sel)
        renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
        if renamed != '':
            IMAGE_NAME.pop(sel)
            IMAGE_NAME.insert(sel,renamed)
            self.listBox.Set(IMAGE_NAME)
            self.listBox.SetSelection(sel)

class MyPanel(wx.Panel):

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

        wx.Panel.__init__(self, *args, **kwargs)
        self.notebook = wx.Notebook(self, size=(225, -1))
#
        self.tab_images = imageTab(self.notebook, self)
        # add the pages to the notebook with the label to show on the tab
        self.notebook.AddPage(self.tab_images, "Pics", select=True)

        self.scroll_img = wx.ScrolledWindow(self, -1)
        self.scroll_img.SetScrollbars(1, 1, 600, 400)

        self.images_area = wx.StaticBox(self, -1, '')
        self.sizerBox = wx.StaticBoxSizer(self.images_area, wx.HORIZONTAL)

        self.sizerBox2 = wx.BoxSizer()
        self.sizerBox.Add(self.scroll_img, 1, wx.EXPAND|wx.ALL, 10)
        self.sizerBox2.Add(self.sizerBox, 1, wx.EXPAND|wx.ALL, 10)
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
#
        btnSizer = wx.BoxSizer() #change to horizontal for side by side
        btnTwo = wx.Button(self, label="Load File", size=(200, 40))
        btnTwo.Bind(wx.EVT_BUTTON, self.onOpenFile)

        self.bmp = None
        self.bitmap = None

        btnSizer.Add(btnTwo, 0, wx.TOP, 15)
        self.sizerBox2.Add(btnSizer)
        #
        self.sizer.Add(self.sizerBox2, proportion=1, flag=wx.EXPAND)

        self.SetSizer(self.sizer)
        self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
        areachange(self, self.notebook.GetPageText(0))

    def OnClickTop(self, event):
        self.scroll_img.Scroll(600, 400)

    def OnClickBottom(self, event):
        self.scroll_img.Scroll(1, 1)

    def OnPageChanged(self, event):
        new = event.GetSelection()
        areachange(self, self.notebook.GetPageText(new))
        event.Skip()

    def OnPageChanging(self, event):
        event.Skip()

    def onOpenFile(self, evt):
        """ Open a file"""
        filename = wx.FileSelector()
        if filename != '':
            IMAGE_DATA[ self.tab_images.image_listsel] = filename
            self.tab_images.imagesel(None)
            print IMAGE_DATA

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.panel = MyPanel(self)
        self.Show()

app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()

rawDescription: Destroy() wxpython simple error?

description: Destroy() wxpython simple error


code:
string = 'happy'
letter = 'p'
string = string.replace(letter, letter + letter)
print string

rawDescription: How to double a char in a string?

description: Double a char in a string


code:
word = temp[2]
type(word) # expect str
len(word) # expect 4
print repr(word) # expect 'INFO'

rawDescription: Python Array Parsing

description: Python Array Parsing


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

class Derived1(Base):
    def __init__(self, x, y):
        Base.__init__(self, x)
        self.y = y

class Derived2(Base):
    def __init__(self, x, z):
        Base.__init__(self, x)
        self.z = z

d1 = Derived1(10, 20)
d2 = Derived2(30, 40)
print d1.x   # Will show 10
print d2.x   # Will show 30

rawDescription: Is it possible for two singleton subclasses to use their own instance of superclass in Python?


code:
def not_allowed(*args, **kwargs): return False

class User(object):
    def __init__(self, userid):
        self.userid = userid

    def __getattr__(self, attr):
        return not_allowed

    def view_profile(self):
        return True

class Admin(User):
    def edit_comment(self):
        return True

class Expert(User):
    def delete_post(self):
        return True

class MultiRoleUser(User):
    """A multirole user has all the power of all the roles together"""
    def __init__(self, userid, roles):
        super(MultiRoleUser, self).__init__(userid)
        self.roles = [role(userid) for role in roles] # create instances needed

    def __getattr__(self, attr):
        all_funcs = [getattr(x, attr, None) for x in self.roles]
        return any(x() for x in all_funcs if x) # check permission if there was one

u = MultiRoleUser('userid', [Admin, Expert])

print 'u.edit_comment:', u.edit_comment
print 'u.delete_post:', u.delete_post

rawDescription: Smartly handle different permissions with inheritance


code:
def A():
  exec ''
  B(locals())
  print x

def B(d):
  d['x'] = 1

A()

rawDescription: Adding to local namespace in Python?

description: Adding to local namespace in Python


code:
headers = ["data", "title", "here", "testing", "stackoverflow"]
Row = namedtuple('Row', headers)
for raw_row in reader:
    row = Row._make(raw_row)

    # Do math
    score = row.data * row.here / stackoverflow
    # Print for user sake
    print score
    # Change the testing value
    new_testing = row.testing + (score - row.title)
    new_row = row._replace(testing=new_testing)

    # Do something with new_row...

rawDescription: Making CSV files easier to modify/navigate within Python using headers / column names?


rawDescription: Adjacent/Connected Elements Array

description: Adjacent/Connected Elements Array


code:
access_token = ""
def setAccessToken(token):
    global access_token
    access_token = token

if __name__ == '__main__':
    print 'Running Utilities : access_token =', access_token

rawDescription: Execfile running the file twice when used with import


code:
def get_images(url, soup):
    #this makes a list of bs4 element tags
    print 'URL: ', url
    n = 0
    images = [img for img in soup.findAll('img')]

    #compile our unicode list of image links
    image_links = [each.get('src') for each in images]
    for each in image_links:
        print "maybe an image"
        print each
        n = n + 1
        path = urlparse.urlparse(each).path
        fn = (os.path.split(path)[1]).strip()
        ext = (os.path.splitext(fn)[1]).strip().lower()
        if (fn == '' or ext == ''):
            continue

        fn = os.path.join ("images", fn)

        print 'Each> ', each

        result = urllib.urlretrieve(os.path.join(url, each), fn)
        print result

    return n

rawDescription: urlretrieve seems to corrupt image file

description: Urlretrieve seems to corrupt image file


code:
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.PhantomJS('/usr/local/bin/phantomjs')
driver.get("https://www.linkedin.com/in/joymerrillsti")
driver.set_window_size(1124, 850)

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "full-name")))

print driver.find_element_by_class_name('full-name').text
print driver.find_element_by_css_selector('div.profile-picture img').get_attribute('src')
print driver.find_element_by_id('headline-container').text
print driver.find_element_by_id('location-container').text
print driver.find_element_by_id('summary-item').text
print driver.find_element_by_id('overview-summary-education').text

rawDescription: Getting different attribute value using selenium


code:
import matplotlib.pyplot as plt
import time
import random
from collections import deque
import numpy as np

# simulates input from serial port
def random_gen():
    while True:
        val = random.randint(1,10)
        yield val
        time.sleep(0.1)

a1 = deque([0]*100)
ax = plt.axes(xlim=(0, 20), ylim=(0, 10))
d = random_gen()

line, = plt.plot(a1)
plt.ion()
plt.ylim([0,10])
plt.show()

for i in range(0,20):
    a1.appendleft(next(d))
    datatoplot = a1.pop()
    line.set_ydata(a1)
    plt.draw()
    print a1[0]
    i += 1
    time.sleep(0.1)
    plt.pause(0.0001)                       #add this it will be OK.

rawDescription: Real time matplotlib plot is not working while still in a loop


code:
a = 100
a *= 1.15
print a # Returns 115

rawDescription: Python - Percentage (maths use) - Simplified x=a+15%


code:
import csv
import glob

files = []
readers = []
fields = set()

try:
    for filename in glob.glob('in*.csv'):
        try:
            fileobj = open(filename, 'rb')
        except IOError:
            print "Failed to open {}".format(filename)
            continue
        files.append(fileobj)  # for later closing

        reader = csv.DictReader(fileobj)
        fields.update(reader.fieldnames)  # reads the first row
        readers.append(reader)

    with open('result.csv', 'wb') as outf:
        writer = csv.DictWriter(outf, fieldnames=sorted(fields))
        writer.writeheader()
        for reader in readers:
            # copy across rows; missing fields will be left blank
            for row in reader:
                writer.writerow(row)
finally:
    # close out open file objects
    for fileobj in files:
        fileobj.close()

rawDescription: Fastest & I/O efficient way to combine heterogeneous CSV files in Python


code:
import win32file, win32api
import os

def ProgressRoutine(TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred,
    StreamNumber, CallbackReason, SourceFile, DestinationFile, Data):
    print Data
    print TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred, StreamNumber, CallbackReason, SourceFile, DestinationFile
    ##if TotalBytesTransferred > 100000:
    ##    return win32file.PROGRESS_STOP
    return win32file.PROGRESS_CONTINUE

temp_dir=win32api.GetTempPath()
fsrc=win32api.GetTempFileName(temp_dir,'cfe')[0]
fdst=win32api.GetTempFileName(temp_dir,'cfe')[0]
print fsrc, fdst

f=open(fsrc,'w')
f.write('xxxxxxxxxxxxxxxx\n'*32768)
f.close()
## add a couple of extra data streams
f=open(fsrc+':stream_y','w')
f.write('yyyyyyyyyyyyyyyy\n'*32768)
f.close()
f=open(fsrc+':stream_z','w')
f.write('zzzzzzzzzzzzzzzz\n'*32768)
f.close()

operation_desc='Copying '+fsrc+' to '+fdst
win32file.CopyFileEx(fsrc, fdst, ProgressRoutine, operation_desc, False,   win32file.COPY_FILE_RESTARTABLE)

rawDescription: How to use win32 API's with python?

description: Use win32 API's with python


code:
import re
from sys import stdout

#stdout = open("result.txt", 'w+')

def read(in_file):
  cases = []
  caselines_index = []
  readlines = []
  readlines_num = []
  with open(in_file, 'r') as file:
    readfile = file.read().strip()
    for line in readfile.split('\n'):
      readlines_num.append(line.strip())
    regex = re.compile("switch\(\$\d\).+?\}", re.DOTALL)
    readfile = re.sub(regex, ' ', readfile)
    for line in readfile.split('\n'):
      readlines.append(line.strip())
    for line in readlines:
      case_search = re.search("case\s\".+?\"\:\s", line)
      if case_search:
        caselines_index.append(readlines.index(line))
    #print caselines_index
    caselines_index_iter = iter(caselines_index)
    try:
      int_line_index = int(next(caselines_index_iter))
    except:
      print "No cases found"
    try:
      int_next_index = int(next(caselines_index_iter))
    except:
      int_next_index = len(readlines) - 1
    while True:
      try:
        case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()
        match1 = re.search("nvp_add", case_text)
        match2 = re.search("details", case_text)
        if match1 or match2:
          case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]) + 1, case_text]
          cases.append(case)
        int_line_index = int_next_index
        int_next_index = int(next(caselines_index_iter))
      except StopIteration:
        case_text = ' '.join(readlines[int_line_index:len(readlines) - 1]).strip()
        case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]), case_text]
        cases.append(case)
        break
  return cases

def work(cases):
  MATCH = 1
  for case_list in cases:
    details = []
    nvp_add = []
    caseline = case_list[0].strip()
    nvp = re.findall("details\(.+?\)", case_list[2].strip())

    for item in nvp:
      result_list = re.findall("(\$.+?)[\,\)]", item)

      for result in result_list:
        if "$*" not in result:
          details.append(result)

    nvp = re.findall("nvp_add\(.+?\)", case_list[2].strip())

    for item in nvp:
      result_list = re.findall("(\$.+?)[\,\)]", item)

      for result in result_list:
        if "$*" not in result:
          nvp_add.append(result)

    missing_from_details, missing_from_nvp_add = [], []
    missing_from_details = [o for o in nvp_add if o not in set(details)]
    missing_from_nvp_add = [o for o in details if o not in set(nvp_add)]
    if missing_from_nvp_add or missing_from_details:
      MATCH = 0
      print caseline + "   LINE - " + str(case_list[1] + 1)
      for mismatch in missing_from_details:
        print "Missing from details:"
        print mismatch
      for mismatch in missing_from_nvp_add:
        print "Missing from nvp_add:"
        print mismatch
      print "\n"
  if MATCH == 1:
    print "MATCH"
  else:
    print "MISMATCHES"

def main():
  in_file = "target1.txt"
  cases = read(in_file)
  work(cases)

if __name__=="__main__":
  main()

rawDescription: How do I filter nested cases to be filter out python

description: Filter nested cases to be filter out python


code:
a = (  2, 3, 4,  )
b = (1,          )
c = (          5,)
d = (1,   3,   5,)

iters = [iter(x) for x in (a, b, c, d)]

this = [next(i) for i in iters]

while True:
    try:
        key = min(i for i in this if i != None)
    except ValueError:
        break
    for i, val in enumerate(this):
        if val == key:
            print val,
            this[i] = next(iters[i], None)
        else:
            print None,
    print

rawDescription: Joining multiple iteratorars by a key

description: Joining multiple iteratorars by a key


rawDescription: how to format float number as integer?

description: Format float number as integer


code:
import ast, inspect

def find_tests(module):
    # generate AST from module's source
    tree = ast.parse(inspect.getsource(module))
    # return tests in module, assuming they are top level function definitions
    return [node for node in tree.body if isinstance(node, ast.FunctionDef)]

def print_docstrings(test):
    for node in test.body:
        if isinstance(node, ast.Expr):
            # print lineno and docstring
            print node.value.lineno, node.value.s

if __name__ == '__main__':
    import test_adding
    for test in find_tests(test_adding):
        print_docstrings(test)

rawDescription: Extracting "extra" docstrings from Python code?


code:
import re

filename = '/tmp/toto.txt'

write_byte_re= r'WriteByte\([^,]+,\s*([^\)]+)\)'

# look for all potential substitutions
search_values = set()
f = open(filename)
for line in f:
    print line
    match_object = re.match(write_byte_re, line)
    if match_object is None: # nothing found, keep looking
        continue
    else:
        search_values.add(match_object.group(1)) # record the value

f.seek(0) # rewind file

substitutions = {}
for value in search_values:
    print "What do you want to replace '%s' with? (press return to keep as is)"
    new_value = raw_input('> ')
    if new_value != '':
        substitutions[value] = new_value

changed_lines = []
for line in f:
    match_object = re.match(write_byte_re, line)
    if match_object is not None:
        value = match_object.group(1)
        if value in substitutions: # not in the dictionary if the user said nothing
            new_value = substitutions[value]
            # modify line
            line = re.sub('\b%s\b' % value, new_value, line)
    changed_lines.append(line)

f.close()

# write output
f = open(filename, 'w')
f.writelines(changed_lines)
f.close()

rawDescription: text replace in python

description: Text replace in python


code:
def get_leaves(node, list_of_leaves=None):
    list_of_leaves = [] if list_of_leaves is None else list_of_leaves
    kids=getchildren(node)
    for i in kids:
        if leafnode(i):
            get_leaves(i, list_of_leaves)
        else:
            a=process_leaf(i)
            list_of_leaves.append(a)

def calling_fn():
    result = []
    get_leaves(rootnode, list_of_leaves=result)
    print result

rawDescription: dealing with python global variables when using recursive functions

description: Dealing with python global variables when using recursive functions


code:
import os
print os.stat('somefile.ext').st_size

rawDescription: how to get image size (bytes) using PIL

description: Get image size (bytes) using PIL


code:
def search(location,item,querylist):
    with open(os.path.join(location,item), 'r') as f:
        countlist = []
        for search in querylist: #any search value after the first one is incorrectly reporting "0"
            countsearch = 0
            for line in f:
                if search in line:
                    countsearch = countsearch + 1
            countlist.append(search)
            countlist.append(countsearch) #mechanism to update countsearch is not working for any value after the first
            f.seek(0)
    print item, countlist

rawDescription: Multiple Word Search not Working Correctly (Python)


code:
from math import floor, ceil
from random import randint

def random_step(low, high, step):
    low_scaled = ceil(float(low)/step)
    high_scaled = floor(float(high)/step)
    return randint(low_scaled, high_scaled) * step

print random_step(3.0, 8.5, 0.5) # 4.5

rawDescription: How to do random sampling within interval with given interval value?

description: Do random sampling within interval with given interval value


code:
interpdata = []
j = 0
for i in stack:
    try:
        temp = np.where( stack[j] == 50, stack[j-1], modis[j] )
        temp = np.where( temp == 50, stack[j+1], temp )
        temp = np.where( temp == 50, stack[j-2], temp )
        temp = np.where( temp == 50, stack[j+2], temp )
        temp = np.where( temp == 50, stack[j-3], temp )
        temp = np.where( temp == 50, stack[j+3], temp )
        temp = np.where( temp == 50, stack[j-4], temp )
        temp = np.where( temp == 50, stack[j+4], temp )
    except IndexError:
        print 'IndexError Passed'
        pass
    else:
        pass
    interpdata [j, :, :] = temp
    j = j + 1

rawDescription: Array/Image Interpolation in python

description: Array/Image Interpolation in python


code:
from Tkinter import *

class display():
    def __init__(self, controlframe):
        self.controlframe = controlframe
        self.namelist = ["Mike", "Rachael", "Mark", "Miguel", "Peter", "Lyn"]

    def callback(self, index):
        print self.namelist[index]

    def showlist(self):
        self.frame = Frame(self.controlframe, height=1)
        self.frame.pack()
        row = 0
        for index, x in enumerate(self.namelist):
            label = Label(self.frame, text="%s " % x, width=17, anchor="w") #limit the name to 17 characters
            fooButton = Button(self.frame, text="Foo",
                               command=lambda index=index: self.callback(index))
            label.grid(row=row, column=0, sticky="W")
            fooButton.grid(row=row, column=1)
            row = row + 1

tk = Tk()

D = display(tk)
D.showlist()
tk.mainloop()

rawDescription: Button returning specific string [Tkinter]


code:
servers = []
for port in portlist:
    datasocket = ("0.0.0.0", port)

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(datasocket)
    server.listen(5)

    servers.append(server)

while True:
    # unpack the returned list
    ready_server, _, _ = select.select(servers, [], [])

    for s in ready_server:
        # use s.accept instead of server.accept
        connection, address = s.accept()
        print str(address) + " has connected!\n"
        msg ="hello\n"
        connection.send(msg)
        connection.close()

rawDescription: multiple network sockets python

description: Multiple network sockets python


code:
# Not a data descriptor because it doesn't define __set__
class NonDataDescriptor(object):
    def __get__(self, obj, objtype):
        return 3

class Namespace(object):

    # Data descriptor - defines both __get__ and __set__
    @property
    def foo(self):
        return 3

    bar = NonDataDescriptor()

x = Namespace()
x.__dict__['foo'] = 4  # We bypass the foo descriptor's __set__ here,
x.bar = 4              # but there's no bar setter, so this one goes to the dict

# x now has foo and bar descriptors and __dict__ entries

print x.foo  # prints 3 - data descriptor wins over instance __dict__ entry
print x.bar  # prints 4 - instance __dict__ entry wins over non-data descriptor

rawDescription: Couple of clarifications on python descriptors


code:
import os.path
print os.path.abspath(os.path.join(yourpath, os.pardir))

rawDescription: How do I get the parent directory in Python?

description: Get the parent directory in Python


code:
import ephem

def const(planet_name, date_string):
    planet_class = getattr(ephem, planet_name)
    planet = planet_class()
    south_bend = ephem.Observer()
    south_bend.lat = '41.67'
    south_bend.lon = '-86.26'  # west is negative
    south_bend.date = date_string
    planet.compute(south_bend)
    return ephem.constellation((planet.ra, planet.dec))

print const(raw_input('Planet: '), raw_input('yyyy/mm/dd: '))

rawDescription: Cannot print alt and az using pyephem in function


code:
from inspect import stack

class Foo:
    def __init__(self):
    	print __file__
    	print self.__class__.__name__
    	print stack()[0][3]

f = Foo()

rawDescription: How do I get the current file, current class, and current method with Python?

description: Get the current file, current class, and current method with Python


code:
capacity = {} #Create it before the loop and use this through out the below loop.
while (lines < travels + 1):
 data = lines + 1
 startFrom = raw_input ('The package travels from: ')
 startFrom = str(startFrom)
 arriveIn = raw_input ('The package arrives to: ')
 arriveIn = str(arriveIn)
 pack = raw_input('Number of packages: ')
 pack = int(pack)
 print startFrom, '--->', arriveTo, ': ', pack
 if startFrom in capacity:#Style change and more pythonic
  capacity[startFrom] = capacity[startFrom] + pack
 else:
  capacity[startFrom] = pack
print capacity

rawDescription: Why does this dictionary in Python only store the last input?


code:
retrosheet = sc.newAPIHadoopFile(
    '/path/to/retrosheet/file',
    'org.apache.hadoop.mapreduce.lib.input.TextInputFormat',
    'org.apache.hadoop.io.LongWritable',
    'org.apache.hadoop.io.Text',
    conf={'textinputformat.record.delimiter': '\nid,'}
)
(retrosheet
    .filter(lambda (k, v): v)
    .map(lambda (k, v): (
        v if v.startswith('id') else 'id,{0}'.format(v)).splitlines()))

rawDescription: creating spark data structure from multiline record

description: Creating spark data structure from multiline record


code:
from itertools import izip

def solve(*lists):
  d = {}
  for i, k in enumerate(izip(*lists)):
    d.setdefault(k, []).append(i)
  for k, v in d.items():
    if len(v) > 1:
      print k, v

solve(list1, list2, list3)
#(3, 'b', 'T3') [2, 4]
#(5, 'e', 'T5') [6, 7]

rawDescription: Finding duplicates in few lists

description: Finding duplicates in few lists


code:
class my_class:
    @classmethod
    def make_time_string(cls, **kwargs):
        pass

    @classmethod
    def make_title(cls, **kwargs):
        pass

    @classmethod
    def give_local_time(cls, **kwargs):
        pass

    @classmethod
    def data_count(cls, **kwargs):
        pass

print my_class.make_time_string
print my_class.make_title
print my_class.give_local_time
print my_class.data_count

rawDescription: Why are my class methods unbound?

description: Why are my class methods unbound


code:
class Unbuffered(object):
   def __init__(self, stream):
       self.stream = stream
   def write(self, data):
       self.stream.write(data)
       self.stream.flush()
   def __getattr__(self, attr):
       return getattr(self.stream, attr)

import sys
sys.stdout = Unbuffered(sys.stdout)
print 'Hello'

rawDescription: Disable output buffering

description: Disable output buffering


code:
if my_queryset.exists():
    print "QuerySet has Data"
else:
    print "QuerySet is empty"

rawDescription: how to specify empty queryset to not empty queryset : [ ] to [<object: hi>]

description: Specify empty queryset to not empty queryset : [ ] to [<object: hi>]


code:
f = open("textfile.txt", "r")
for item in f:
    first_comma_pos = item.find(",")
    print item
    print first_comma_pos
    first_colon_pos = item.find(" : ")
    print first_colon_pos
    second_comma_pos = item.find(",", first_comma_pos+1)  # Note change
    second_colon_pos = item.find(" : ", second_comma_pos)
    print second_colon_pos
    item = (item[:first_colon_pos+3] +
        '"' + item[first_colon_pos+3:second_comma_pos] + '"' +
        item[second_comma_pos:second_colon_pos+3] +
        '"' + item[second_colon_pos+3:-2] + '"}\n')  # Note change
    print item
    saveFile= open("result.txt", "a")
    saveFile.write(item)
    saveFile.write('\n')
    saveFile.close()

rawDescription: Put the inverted quotes around a string using index , python


rawDescription: How to access data from Multidimensional Array

description: Access data from Multidimensional Array


code:
import cv2
import numpy as np

# Load the images
img =cv2.imread('messi4.jpg')

# Convert them to grayscale
imgg =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# SURF extraction
surf = cv2.SURF()
kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False)

# Setting up samples and responses for kNN
samples = np.array(descritors)
responses = np.arange(len(kp),dtype = np.float32)

# kNN training
knn = cv2.KNearest()
knn.train(samples,responses)

# Now loading a template image and searching for similar keypoints
template = cv2.imread('template.jpg')
templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
keys,desc = surf.detect(templateg,None,useProvidedKeypoints = False)

for h,des in enumerate(desc):
    des = np.array(des,np.float32).reshape((1,128))
    retval, results, neigh_resp, dists = knn.find_nearest(des,1)
    res,dist =  int(results[0][0]),dists[0][0]

    if dist<0.1: # draw matched keypoints in red color
        color = (0,0,255)
    else:  # draw unmatched in blue color
        print dist
        color = (255,0,0)

    #Draw matched key points on original image
    x,y = kp[res].pt
    center = (int(x),int(y))
    cv2.circle(img,center,2,color,-1)

    #Draw matched key points on template image
    x,y = keys[h].pt
    center = (int(x),int(y))
    cv2.circle(template,center,2,color,-1)

cv2.imshow('img',img)
cv2.imshow('tm',template)
cv2.waitKey(0)
cv2.destroyAllWindows()

rawDescription: OpenCV 2.4.1 - computing SURF descriptors in Python


code:
dataset = [{'': 0, 'FAILED': 412.698, 'PASSED': 587.301},
           {'': 440, 'FAILED': 31.746, 'PASSED': 968.253},
           {'': 880, 'FAILED': 253.968, 'PASSED': 746.031},
           {'': 1320, 'FAILED': 714.285, 'PASSED': 285.714}]

data_orders = [['', 'FAILED', 'PASSED'],
               ['', 'FAILED', 'PASSED'],
               ['', 'PASSED', 'FAILED'],
               ['', 'PASSED', 'FAILED']]

FAILED = []
PASSED = []
for dataset_row,data_order_row in zip(dataset,data_orders):
    if data_order_row[1] == 'FAILED':
        FAILED.append((dataset_row[''],dataset_row['FAILED']+dataset_row['']))
        PASSED.append((dataset_row['FAILED']+dataset_row[''],dataset_row['FAILED']+dataset_row['']+dataset_row['PASSED']))
    else:
        FAILED.append((dataset_row['']+dataset_row['PASSED'],dataset_row['FAILED']+dataset_row['']+dataset_row['PASSED']))
        PASSED.append((dataset_row[''],dataset_row['']+dataset_row['PASSED']))

soln_failed = [(0, 412.698), (440, 471.746), (1626.031, 1879.999), (1605.714, 2319.999)]
soln_passed = [(412.698, 999.999), (471.746, 1439.999), (880, 1626.031), (1320, 1605.714)]

print FAILED == soln_failed
print PASSED == soln_passed

rawDescription: How can I change from a list of dictionaries to a list of tuples

description: Change from a list of dictionaries to a list of tuples


code:
from datetime import datetime
from sqlalchemy import create_engine, Integer, Column, DateTime, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.orm import Session

engine = create_engine('postgresql:///example', echo=True)
Base = declarative_base(bind=engine)
session = Session(bind=engine)

class Article(Base):
    __tablename__ = 'article'

    id = Column(Integer, primary_key=True)
    views = Column(Integer, nullable=False, default=1)
    ts = Column(DateTime, nullable=False, default=datetime.utcnow)

    @hybrid_method
    def popularity(self, gravity=1.8):
        seconds = (self.ts - datetime.utcnow()).total_seconds()
        hours = seconds / 3600

        return (self.views - 1) / (hours + 2) ** gravity

    @popularity.expression
    def popularity(self, gravity=1.8):
        seconds = func.extract('epoch', self.ts - func.now())
        hours = seconds / 3600

        return (self.views - 1) / func.power((hours + 2), gravity)

Base.metadata.create_all()

a1 = Article(views=100)
a2 = Article(views=200)

session.add_all((a1, a2))
session.commit()

comparison = session.query(Article, Article.popularity()).all()

for a, pop in comparison:
    print 'py: {} db: {}'.format(a.popularity(), pop)

rawDescription: order by a method contained within a class in SQLAlchemy

description: Order by a method contained within a class in SQLAlchemy


code:
hands = {} # Create a new dictionary to hold the hands.
hands["flush"] = make_hand(deck) # Generate some hands using your function.
hands["straight"] = make_hand(deck) # Generate another hand with a different name.
print hands["flush"] # Access the hand later.

rawDescription: list named with a function argument in python

description: List named with a function argument in python


code:
for link in soup.select("#trendingbox > a"):
    print link.get('href')

rawDescription: Scraping specified links with BeautifulSoup


code:
import pyinotify

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        # Processing of created file goes here.
        print "Created:", event.pathname

wm = pyinotify.WatchManager()

notifier = pyinotify.Notifier(wm, EventHandler())
wm.add_watch('/tmp', pyinotify.IN_CREATE, rec=True)
notifier.loop()

rawDescription: Run script when a folder or file is created


code:
input_file_names = ["A1_B1_100000.txt", "A2_B2_100000.txt",
                    "A1_B2_100000.txt", "A2_B1_100000.txt"]

GROUP_SIZE = 4
file_write = open("average_generations.txt", 'w')
mutation_average = open("mutation_average", 'w')

for file_name in input_file_names:
    with open(file_name, 'r') as input_file:
        print "processing file", file_name
        count = 0
        totals = None
        for line in input_file:
            fields = line.split('\t')
            try:
                fields.remove('\n') # remove empty field (why)?
            except ValueError:
                pass
            if not totals: # first line?
                totals = map(float, fields)
            else:
                for i in xrange(len(fields)):
                    totals[i] += float(fields[i])
            count += 1

        averages = [total/count for total in totals]

        print_counter = 0
        for average in averages:
            print average
            if print_counter % GROUP_SIZE == 0:
                file_write.write(str(average)+'\n')
            print_counter += 1

file_write.write('\n')
file_write.close()

mutation_average.close() # ????

rawDescription: Python: Memory Limit?

description: Python: Memory Limit


code:
engToEstDict = {}

# The with statement automatically closes the file afterwards. Furthermore, one shouldn't
# overwrite builtin names like "file", "dict" and so on (even though it's possible).
with open("dictionary.txt", "r") as f:
    for line in f:
        if not line:
            break

        # Map the Estonian to the English word in the dictionary-typed variable
        pair = lines.split("\t")
        engToEstDict[pair[0]] = pair[1]

# Then, lookup of Estonian words is simple
print engToEstDict["hello"] # should probably print "tere", says Google Translator

rawDescription: Dictionaries in Python

description: Dictionaries in Python


code:
import glob
for f in glob.glob("*.f"):
    print f

rawDescription: Python: os.listdir alternative/certain extensions


code:
from binascii import hexlify

mac = netsnmp.Varbind("ipNetToMediaPhysAddress."+i+"."+ipadd)
macadd = netsnmp.snmpget(mac, Version = 2, DestHost = ip, Community = comm)
print hexlify(macadd[0])

rawDescription: NET-SNMP + Python Mac Address shows as \x00\


code:
import collections

def hasher():
  return collections.defaultdict(hasher)

hash = hasher()
hash['element1']['sub1']['subsub1'] = 'value1'
if 'subsub1' in hash['element1']['sub1']:
  print 'found value'

rawDescription: In a Python dict of dicts, how do you emulate Perl's auto-vivification behavior?


code:
a = [1, 3, 5]
b = a
a[:] = [x + 2 for x in a]
print b

rawDescription: How to modify list entries during for loop?

description: Modify list entries during for loop


code:
def algorithm(input):
    print input
    p = PorterStemmer()
    while 1:
        output = ''
        word = ''
        if input == '':
            break
        for c in input:
            if c.isalpha():
                word += c.lower()
            elif word:
                output += p.stem(word, 0,len(word)-1)
                word = ''
                output += c.lower()
        else:
            output += p.stem(word, 0, len(word)-1)
        print output
        return output

rawDescription: Porter Stemmer Algorithm Not returning the expected output? when modified into def


code:
m = re.match('(?i)m(|[ldf])(\d+)', text)
if m:
  print m.group(2)

rawDescription: Shorten the regex usage syntax in Python


code:
from itertools import dropwhile, islice

data = ['a', 'b', 'c', 'd', 'e', 'f']
for after in islice(dropwhile(lambda L: L != 'c', data), 1, None):
    print after

rawDescription: Start loop after certain element in list is reached


code:
#!/usr/bin/python

import urllib
import re

base_url = 'http://en.wikibooks.org/wiki/Category:Recipes'
next_base = 'http://en.wikibooks.org'
recipes = []

# this is just the sample function
# you should handle your proxy logic here too
def get_links(url):
    request = urllib.urlopen(url)
    content = request.read()
    # I just use one-off re expression
    links = re.findall(r'/wiki/Cookbook:(?!Recipes)(?!Table_of_Contents).*" ', content)

    global recipes
    recipes += links

    try:
        # again, one-off re expression
        next_url = re.findall(r'/w/index.*>next 200', content)[0].split('\" ')[0]
        print "fetching next url: " + str(next_base + next_url)
        return get_links(next_base + next_url)
    except IndexError:
        print "all recipes fetched."
        print recipes
        return

if __name__ == '__main__':
    print "start fetching..."
    get_links(base_url)

rawDescription: Crawling By Next Page Link

description: Crawling By Next Page Link


code:
import urllib2, StringIO, gzip
f = urllib2.urlopen("http://www.heavenplaza.com")
data = StringIO.StringIO(f.read())
gzipper = gzip.GzipFile(fileobj=data)
print gzipper.read()

rawDescription: Python urllib2 parse html problem

description: Python urllib2 parse html problem


code:
from lockfile.pidlockfile import PIDLockFile
from lockfile import AlreadyLocked

pidfile = PIDLockFile("/var/run/mydaemon.pid", timeout=-1)
try:
    pidfile.acquire()
except AlreadyLocked:
    try:
        os.kill(pidfile.read_pid(), 0)
        print 'Process already running!'
        exit(1)
    except OSError:  #No process with locked PID
        pidfile.break_lock()

#pidfile can now be used to create DaemonContext

rawDescription: python-daemon context fails to start when a stale PID file is present

description: Python-daemon context fails to start when a stale PID file is present


code:
t1=threading.Thread(target=self.read)
t1.start()
print "something"

rawDescription: Python thread starts running before calling Thread.start


code:
class MyEchoClient(Protocol):
    def dataReceived(self, data):
        someFuncProcessingData(data)
        self.transport.write(b'test')

class MyEchoClientFactory(ReconnectingClientFactory):
    def buildProtocol(self, addr):
        print 'Connected.'
        return MyEchoClient()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason
        ReconnectingClientFactory.clientConnectionFailed(self, connector,
                                                     reason)

rawDescription: Python - Twisted client - check protocol.transport connection in ping loop


code:
foo = Literal('foo').setResultsName('foo')
bar = Literal('bar').setResultsName('bar')
grammar = foo | bar
parsed = grammar.parseString('bar foo')
print parsed
# (['bar'], {'bar': [('bar', 0)]})
print parsed.asDict()
# {'bar': 'bar'}

rawDescription: Type checking on results with pyparsing

description: Type checking on results with pyparsing


code:
# Assuming Python 2.x
# For Python 3.x support change print -> print(..) and Tkinter to tkinter
from Tkinter import *
import time

class alien(object):
     def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(self.root, width=400, height = 400)
        self.canvas.pack()
        self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',         fill='blue')
        self.alien2 = self.canvas.create_oval(2, 2, 40, 40, outline='white', fill='red')
        self.canvas.pack()
        self.root.after(0, self.animation)
        self.root.mainloop()

     def animation(self):
        track = 0
        while True:
            x = 5
            y = 0
            if track == 0:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, x, y)
                    self.canvas.move(self.alien2, x, y)
                    self.canvas.update()
               track = 1
               print "check"

            else:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, -x, y)
                    self.canvas.move(self.alien2, -x, y)
                    self.canvas.update()
               track = 0
            print track

alien()

rawDescription: Python Tkinter Animation

description: Python Tkinter Animation


code:
with open("your_file") as f:
    my_val = ""
    header = ""
    for i,line in enumerate(f):
        line = line.split(",")
        if len(line)==1:
            my_val = line[0]
        elif i==1:
            header = "newCol," + ",".join(line)
            print header
        else:
            print "{},{}".format(my_val, ",".join(line))

rawDescription: Add column value to proceeding rows until a new column value


code:
def print_message(message):
    print "\n"
    print "-"*10
    print message
    print "-"*10
    print "\n"

print_message('This is a test of the %s system' % codes[0])
print_message('This is not a test of the %s system and all is good'% codes[1])

rawDescription: In python how do I assign a series of commands to one variable?


code:
import win32gui

def enumerationCallaback(hwnd, results):
    text = win32gui.GetWindowText(hwnd)
    if text.find("Mozilla Firefox") >= 0:
        results.append((hwnd, text))

mywindows = []
win32gui.EnumWindows(enumerationCallaback, mywindows)
for win, text in mywindows:
    print text

def recurseChildWindow(hwnd, results):
    win32gui.EnumChildWindows(hwnd, recurseChildWindow, results)
    print hwnd
    # try to get window class, text etc using SendMessage and see if it is what we want

mychildren = []
recurseChildWindow(mywindows[0][0], mychildren)

rawDescription: how to get firefox address bar url for python (pywin32)

description: Get firefox address bar url for python (pywin32)


code: print "%-25s %-15s %15s" % ("STACK","OVERFLOW","PYTHON")

rawDescription: What's a good way to format this?

description: What's a good way to format this


code:
import ast

allowed_functions = set([
    #math library
    'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
    'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf',
    'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod',
    'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp',
    'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians',
    'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc',
    #builtins
    'abs', 'max', 'min', 'range', 'xrange'
    ])

allowed_node_types = set([
    #Meta
    'Module', 'Assign', 'Expr',
    #Control
    'For', 'If', 'Else',
    #Data
    'Store', 'Load', 'AugAssign', 'Subscript',
    #Datatypes
    'Num', 'Tuple', 'List',
    #Operations
    'BinOp', 'Add', 'Sub', 'Mult', 'Div', 'Mod', 'Compare'
    ])

safe_names = set([
    'True', 'False', 'None'
    ])

class SyntaxChecker(ast.NodeVisitor):

    def check(self, syntax):
        tree = ast.parse(syntax)
        self.passed=True
        self.visit(tree)

    def visit_Call(self, node):
        if node.func.id not in allowed_functions:
            raise SyntaxError("%s is not an allowed function!"%node.func.id)
        else:
            ast.NodeVisitor.generic_visit(self, node)

    def visit_Name(self, node):
        try:
            eval(node.id)
        except NameError:
            ast.NodeVisitor.generic_visit(self, node)
        else:
            if node.id not in safe_names and node.id not in allowed_functions:
                raise SyntaxError("%s is a reserved name!"%node.id)
            else:
                ast.NodeVisitor.generic_visit(self, node)

    def generic_visit(self, node):
        if type(node).__name__ not in allowed_node_types:
            raise SyntaxError("%s is not allowed!"%type(node).__name__)
        else:
            ast.NodeVisitor.generic_visit(self, node)

if __name__ == '__main__':
    x = SyntaxChecker()
    while True:
        try:
            x.check(raw_input())
        except Exception as e:
            print e

rawDescription: Executing mathematical user code on a python web server, what is the simplest secure way?


code:
from docutils import core
text = '|python|\n\n.. |python| image:: python.jpg\n'
print core.publish_string(text)

rawDescription: Resolve Substitutions in RestructuredText


code:
for y in range(0, len(x), 2):
    print "%s%s" % (x[y], x[y+1])

rawDescription: Iterate over a python sequence in multiples of n?


code:
import os.path
import sys

print os.path.abspath(sys.argv[0])

rawDescription: Help with filetype association!

description: Help with filetype association!


rawDescription: Split string into tuple (Upper,lower) 'ABCDefgh' . Python 2.7.6


code:
counter = 1
for key, value in results.iteritems():
    if key == "results":
        svalue = str(value)
        while counter != -1:
            tweetposstart = svalue.find("u'text'",counter)
            tweetposend = svalue.find("u'from_user_name'",counter)
            if tweetposstart != -1:
                tweet = svalue[tweetposstart:tweetposend]
                print tweet
                counter = counter + tweetposstart
            else:
                counter = tweetposstart

rawDescription: python twython tweet extraction

description: Python twython tweet extraction


code:
import itertools

with open("a1") as fin1, open("b1") as fin2:
    for line1, line2 in itertools.izip(fin1, fin2):
        if '77' in line1:
            print line2

rawDescription: Python 2.7. Read one file using enumerate to get line numbers and go to another file using line numbers


code:
my_val = []
row=20
col=15

f = open("%s" %filename,"r")
lines = f.readlines()

for i in range(1,row,3):
    a = lines[i].split(";")
    for j in range(2,col,2):
        my_val.append(float(a[j]))

print my_val

f.close()

rawDescription: reading across the rows and columns

description: Reading across the rows and columns


code:
a_dict = dict(k=4, z=2)
a_dict.update(dict(l=1))
print a_dict

rawDescription: Why does dict(k=4, z=2).update(dict(l=1)) return None in Python?


code:
try:
    fileName = 'C:\pro.txt'
    textF = open(fileName, 'r')
    paragraph = textF.readlines()
except IOError:
    print("Not found")

paragraphUpper = [w.strip() for w in paragraph if w.isupper()]
print paragraphUpper

rawDescription: Adding several elements to a list from text file


code:
test_list1 = [['Happy', 'Dog'], ['Sad', 'Dog'], ['Dog', 'Dog'], ['Angry', 'Dog']]
test_list2 = ["Happy", "Sad", "Angry"]
new_list = []

def emotion_detection(x, y):
    for i in x:
        for string in i:
            if string in y:
                new_list.append(1)
                break
        else:
            new_list.append(0)
    print new_list

rawDescription: Checking if string in list exists in a different list of strings Python


code:
def test():
    a=[set([1,2,3]),set([3,4,5])]
    x=set([1,4])
    for i in a:
        diff=i.difference(x)
        # a[idx]=diff
    print a

rawDescription: Temporary created during python list iteration?


code:
import itertools
keylist = ['key1', 'key4','key2']
worddict = {'key1': ['a','b','c'],
            'key2':['d','e','f'],
            'key3':['g','h','i'],
            'key4':['j','k','l']}
sentences = (' '.join(sentence)
             for sentence in itertools.product(*(worddict[k]
                                                 for k in keylist)))
print '\n'.join(sentences)

rawDescription: Finding all sentences from list of keywords to dict


code:
def func_mutable(dog):
    dog.append(1)

def func_immutable(strng):
    strng+="1"

dog = []
print dog #prints []
func_mutable(dog)
print dog #prints [1]

strng = "1"
print strng #prints "1"
func_immutable(strng)
print strng #still prints "1", and not "11"

rawDescription: Append vs. redefining variable in Python


code:
print("Welcome to the Barcode Scanner. There are two options: ")
print("Do you want to check an 8-digit or generate a check digit from a 7 digit number?")
usersDecision = input("Press 7 to generate the 7th digit, or press 8 to check an 8 digit number")

if usersDecision == "7":
    print("You have chosen to generate a check digit from a 7 digit number?")
    seven_digitCheck = input("Please enter your 7-digit number")

elif usersDecision == "8":
    print("You have chosen to check the validity of an 8 digit number.")
    eight_digitValid = input ("Please enter your 8 digit number")

else:
    print("This is not a valid option, Please leave")

multiply_1 = seven_digitCheck * 3

multiply_2 = eight_digitValid * 3

print "multiply_1: " + repr(multiply_1) + "\n"
print "multiply_2: " + repr(multiply_2) + "\n"

rawDescription: How to get a number from a string and then multiply the number?

description: Get a number from a string and then multiply the number


code:
from BeautifulSoup import BeautifulSoup
import urllib2
url = 'http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htm'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
for row in soup.html.body.findAll('tr'):
    data = row.findAll('td')
    if data and 'subclass 885online' in data[0].text:
        print data[4].text

rawDescription: How to extract certain parts of a web page in Python

description: Extract certain parts of a web page in Python


code:
if True:  # For testing -- use the other branch to read from a file
    # Declare some test content in a string...
    input_content = """
Stan,Marsh,Stan Marsh,1001,899,smarsh,smarsh@info.com
Eric,Cartman,Eric Cartman,1002,898,ecartman,ecartman@info.com
    """.strip()
    # And use the StringIO module to create a file-like object from it.
    from StringIO import StringIO
    input_file = StringIO(input_content)
else:
    # Or just open the file as normal. In a short script like this,
    # one doesn't need to worry about closing the file - that will happen
    # when the script ends.
    input_file = open('example.csv', 'rb')

# Declare the fields in the order they are in the file.
# zip() will use this later with the actual fields from the file
# to create a dict mapping.
fields = ('FN', 'LN', 'NAME', 'UIDN', 'GIDN', 'CN', 'EMAIL')  # Fields, in order

# Declare a template for the LDIF file. The %(...)s bits will be
# later interpolated with the dict mapping created for each input row.
template = u"""
dn: cn=%(CN)s,ou=People,dc=domain,dc=com
cn: %(CN)s
gidnumber: 20
givenname %(FN)s
homedirectory /home/users/%(USER)s
loginshell: /bin/sh
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
sn: %(LN)s
uid: %(USERNAME)s
telephoneNumber: %(TELE)s
uidnumber: %(UIDN)s
userpassword: {CRYPT}mrpoo
mail: %(EMAIL)s
"""

for line in input_file:
    # Create `vals` with some default values. These would be overwritten
    # if the CSV data (and of course the declared fields) contain them.
    vals = {"USER": "XXX", "TELE": "XXX", "USERNAME": "XXX"}

    # line.strip().split() will turn the string line,
    # for example 'foo,baz,bar\n' (trailing new line `strip`ped out)
    # into the list ['foo', 'baz', 'bar'].
    # zipping it with, say, ['LN', 'FN', 'EMAIL'] would yield
    # [('LN', 'foo'), ('FN', 'baz'), ('EMAIL', 'bar')] --
    # ie. a list of tuples with a key and a value.
    # This can be used by the `dict.update` function to replace and augment
    # the default values declared above.

    vals.update(zip(fields, line.strip().split(",")))

    # Finally, use the interpolation operator % to merge the template with the
    # values for this line and print it to standard output.

    print template % vals

rawDescription: Python: Using CSV to parse(?) variables and then output that to another file


code:
import cv2
import numpy as np

img = np.ones((200,250,3), dtype=np.uint8)
for i in range(50, 80, 1):
    for j in range(40, 70, 1):
        img[i][j]*=200

cv2.circle(img, (120,120), 20, (100,200,80), -1)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 20,
              param1=30,
              param2=15,
              minRadius=0,
              maxRadius=0)

print circles
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('circles', img)

k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

rawDescription: HoughCircles circle detection using opencv and python-


code:
def getprimes(n):
     numbers = [ i for i in range (2,n+1)]
     primes=[]

     while (numbers):
        print numbers[0]
        primes.append(numbers[0])
        print "primes",primes
        numbers=[k for k in numbers if k%numbers[0]!=0 ]
        print numbers

     print numbers
     return primes

rawDescription: why the list is not changed when I modify it within the function in python

description: Why the list is not changed when I modify it within the function in python


code:
import copy

a = range(11)

b = []

for i in a:
    b.append(i+1)

print b
#Out[10]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

c = copy.deepcopy(b)
# Here, what I expect is to safely save "b" and work on its copy.

c.insert(-1,10.5)

print c
#Out[13]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10.5, 11]

c.remove(11)
print c

#Out[15]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10.5]

# So far everything is right: I inserted and removed what I wanted.
# But then, when I check on my "backed-up b", it has been modified:

print b
#Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10.5]

# On the other hand, "a" remains the same; it seems the propagation do not affect
# "loop-parenthood":

print a

rawDescription: Python: operation being unexpectedly propagated along parent lists


code:
from bs4 import BeautifulSoup
import requests

senatorwiki = 'https://en.wikipedia.org/wiki/List_of_current_United_States_Senators'
html = requests.get(senatorwiki)
soup = BeautifulSoup(html.text, "lxml")
senatortable = soup.find('table',{'class':"sortable"})
rows = senatortable.findAll('tr')

for tr in rows:
    print tr.findAll('td')
    # to get next lines data of the list of tds is up to you ;)
    # print(state.text,ns.text,name.text,party.text,privsec.text)

rawDescription: Iterating through an entire table with BeautifulSoup


code:
a = [3,4,5,5,5,6]
b = [1,3,4,4,5,5,6,7]

a_multiset = collections.Counter(a)
b_multiset = collections.Counter(b)

overlap = list((a_multiset & b_multiset).elements())
a_remainder = list((a_multiset - b_multiset).elements())
b_remainder = list((b_multiset - a_multiset).elements())

print overlap, a_remainder, b_remainder

rawDescription: find the "overlap" between 2 python lists

description: Find the "overlap" between 2 python lists


code:
for key, val in item.media_content[0].iteritems():
    print key, val

rawDescription: Parse out key:values in dictionary nested in MRSS feed using Python Feedparser


code:
for section in open("foo").read().split("User")[1:]:
   user = section.split("\n")
   name = user[1]
   handle = user[2]
   description = user[3]
   print name, handle, description

rawDescription: Parsing a CSV file into columns (preferably using python)


code:
#!/usr/bin/env python
import os
from   select import poll, POLLIN

r_fd, w_fd = os.pipe()

p = poll()
p.register(r_fd, POLLIN)

os.write(w_fd, 'X') # Put something in the pipe so p.poll() will return

while True:
    events = p.poll(100)
    for e in events:
        print e
        os.read(r_fd, 1)

rawDescription: How does polling a file for changes work?


code:
import BaseHTTPServer

HOST_NAME = 'localhost'
PORT_NUMBER = 1337

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(s):
        s.send_response(200)
        s.send_header('Content-Type', 'text/html')
        s.end_headers()

        # Get parameters in query.
        params = {}
        index = s.path.rfind('?')
        if index >= 0:
            parts = s.path[index + 1:].split('&')
            for p in parts:
                try:
                    a, b = p.split('=', 2)
                    params[a] = b
                except:
                    params[p] = ''

        # !!!
        # Check if there is a color parameter and send to controller...
        if 'color' in params:
            print 'Send something to controller...'
        # !!!

        s.wfile.write('<pre>%s</pre>' % params)

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass

    httpd.server_close()

rawDescription: Call program from within a browser without using a webserver


code:
def max_value(inputlist):
    return max([sublist[-1] for sublist in inputlist])

print max_value(resultlist)
# 9.1931

rawDescription: Get max value from a list with lists?

description: Get max value from a list with lists


code:
import sys, traceback
import json

userQuited = False

def _quit(dummy):
    global userQuited
    userQuited = True

def _save(dummy):
    f=open("game.sav",'w+')
    json.dump(states, f)
    f.close

def _continue(dummy):
    f=open("game.sav",'r+')
    states = json.load(f)
    f.close

def user(name):
    states["player_name"] = name

states = {
    "player_health" : 100,
    "player_name" : "",
    "health_potion" : 0,
    "big_wrench" : 0,
    "blue_keycard" : 0,
    "current_page" : "page1"
}

book = {
    "page1": {
        "text": """You are in an underwater research facility that has been
attacked by unknown assailants. As the assistant to the head
researcher, you were hired in to assist Dr. Weathers with
the Lightning Sword project. Project Lightning Sword still
remains in large a mystery to you. Although you worked closely
with the doctor you were never allowed to see the big picture.
Right now that's the least of your worries, as chaos ensues around you.
Alarms were triggered a while ago. As you sit in your room,
you hear short bursts of automatic rifle fire and only one
thing is for certain, you have to survive.\n """,
        "inputText": "What would you like your player to be named?",
        "function": user,
        "next": "page2"
    },
    "page2": {
        "text": "Welcome, %(player_name)s please choose what you would like to do",
        "inputText": "0 = Quit, 1 = Start New Game, 2 = Continue",
        "choices": {
            "0" : "Quit",
            "1" : "page3",
            "2" : "Continue"
        }
    },
    "page3" : {
        "text" : """You are standing in your room, door is locked and you're wondering
if you will get out of this alive. The gunfire keeps getting closer and you know that you
can't stay here, you have to make a move and try and get out. Otherwise it's just a matter
of time before they find you and that option doesn't look very promising.\n\n

You are ready to try your escape but first you should try and get any useful
things from your room to use along the way. Your room-office is a bit bigger than a walk in closet,
textile flooring and gray walls encompas your view and the only things in the room are the bed you
are sitting on (not very comfortable, looks more like a prisoners bed), a framed picture on the wall
and your work desk which has barely enough space for your equipment and computer.""",
        "inputText": "1 = Walk over to the picture frame, 2 = Walk over to the desk, 3 = Exit the door",
        "choices": {
            "1" : "page4",
            "2" : "page5",
            "3" : "page7"
        }
    },

    "Quit" : {
        "text": "goodbye!",
        "inputText": "",
        "function" : _quit
    },
    "Continue" : {
        "text": "welcome back!",
        "inputText": "",
        "function" : _continue
    }
}

def processPage(page):
    answer = ""
    print(page["text"] % states)
    if len(page["inputText"]) > 1 :
        answer = raw_input(page["inputText"] % states)
    if "function" in page:
        page["function"](answer)
    if "next" in page:
        return page["next"]
    if "choices" in page:
        for choice in page["choices"]:
            if choice == answer:
                print page["choices"][choice]
                return page["choices"][choice]
    return ""

def main():
    global userQuited
    while(userQuited==False):
        states["current_page"] = processPage(book[states["current_page"] ])
        if (states["current_page"] != "Quit" and states["current_page"] != "Continue"):
            _save("")

main()

rawDescription: python 3.x - text based adventure game, save game function

description: Python 3.x - text based adventure game, save game function


code:
import os

found = False
source_folder=None
dest_folder=None
text_file=None

while not found:
    source_folder = str(raw_input("Enter full path of source folder: "))
    print source_folder
    if not os.path.isdir(source_folder):
        print(source_folder, 'This folder has not been found. Enter correct path. ')
    else:
        print("Folder path is correct!")
        found = True

found = False
while not found:
    dest_folder = raw_input("Enter full path of destination folder: ")
    if not os.path.isdir(dest_folder):
        print(dest_folder, 'This folder has not been found. Enter correct path. ')
    else:
        print("Folder path is correct!")
        found = True

found = False
while not found:
    text_file = raw_input("Enter full path your text file folder: ")
    if not os.path.isfile(text_file):
        print(text_file, 'This file has not been found. Enter correct path. ')
    else:
        print("File path is correct!")
        found = True

rawDescription: Python: Check folder directory given by user input


code:
from collections import namedtuple

class DataPack(object):
    def __init__(self, line, seperator =',', headerfields = None, groupfields = None):
        self.seperator = seperator
        self.header_fields = headerfields or ('date', 'time', 'nr', 'groups')
        self.group_fields = groupfields or ('nr', 'sensors','controlfields',
                                            't1values', 't2values')
        Header = namedtuple('Header', self.header_fields)

        self.header_part = line.split(self.seperator)[:self.data_start]
        self.data_start = len(self.header_fields)
        self.data_part = line.split(self.seperator)[self.data_start:]
        self.header = Header(*self.header_part)
        self.groups = self._create_groups(self.data_part, self.header.groups)

    def _create_groups(self, datalst, groups):
        """nr, sensors controllfield * 4, t1value*sensors, t2value*sensors """
        Group = namedtuple('DataGroup', self.group_fields)
        _groups = []
        for i in range(int(groups)):
            nr = datalst[0]
            sensors = datalst[1]
            controlfields = datalst [2:6]
            t1values = datalst[6:6+int(sensors)]
            t2values = datalst[6+int(sensors):6+int(sensors)*2]
            _groups.append(Group(nr, sensors, controlfields, t1values, t2values))
            datalst = datalst[6+int(sensors)*2:]
        return _groups

    def __str__(self):
        _return = []
        for group in self.groups:
            for sensor in range(int(group.sensors)):
                _return.append('%s, ' % self.header.date.replace('-','/'))
                _return.append('%s, ' % self.header.time)
                _return.append('%s, ' % group.nr)
                _return.append('%s, ' % (int(sensor) + 1,))
                _return.append('(%s, ' % group.t1values[int(sensor)])
                _return.append('%s)\n' % group.t2values[int(sensor)])
        return u''.join(_return)

if __name__ == '__main__':
    line = """2012-01-02,12:50:32,658,2,1,2,0,0,0,0,1556,1555,62,60,2,3,0,0,0,0,1559,1557,1557,63,64,65,0.305,0.265,0.304,0.308,0.309"""
    data = DataPack(line)
    for i in data.header: print i,
    for i in data.groups: print '\n',i
    print '\n',data
    print 'cfield 0:2 ', data.groups[0].controlfields[2]
    print 't2value 1:2 ', data.groups[1].t2values[2]

rawDescription: Extracting data from a string where the data structure is embedded in the string itself


code:
from sqlalchemy import tuple_, or_, and_

def select_by_composite_pk(table, values):
    "works only in a high-capability database like postgresql"
    return table.select().where(tuple_(*table.primary_key).in_(values))

def select_by_composite_pk_no_tuples(table, values):
    "works in any database"

    return table.select().where(
            or_(
                *[
                    and_(*[col == val for col, val in zip(table.primary_key, val)])
                    for val in values
                ]
            ))

def select_by_single_pk(table, values):
    "works in any database"
    return table.select().where(list(table.primary_key)[0].in_(values))

if __name__ == '__main__':
    from sqlalchemy import create_engine, Table, Column, Integer, MetaData

    eng = create_engine("postgresql://scott:tiger@localhost/test", echo=True)

    conn = eng.connect()
    trans = conn.begin()

    m = MetaData()

    # single PK column
    a = Table('a', m, Column('x', Integer, primary_key=True),
                            Column('y', Integer))

    # composite PK column
    b = Table('b', m, Column('x', Integer, primary_key=True),
                    Column('y', Integer, primary_key=True))

    m.create_all(conn)

    conn.execute(a.insert(), [
            {'x': i, 'y': i * 2} for i in xrange(10)
        ])
    conn.execute(b.insert(), [
            {'x': i, 'y': i * 2} for i in xrange(10)
        ])

    print conn.execute(
                select_by_composite_pk(a,
                    [tuple_(3, ), tuple_(5, ), tuple_(9, )])).fetchall()

    print conn.execute(
                select_by_composite_pk(b,
                    [tuple_(3, 6), tuple_(5, 10), tuple_(9, 18)])).fetchall()

    print conn.execute(
                select_by_composite_pk_no_tuples(b,
                    [(3, 6), (5, 10), (9, 18)])).fetchall()

    print conn.execute(
                select_by_single_pk(b, [3, 5, 9])).fetchall()

    # ORM query version
    from sqlalchemy.orm import Session
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class MyCompositeClass(Base):
        __table__ = b

    # get accepts a tuple
    print Session(conn).query(MyCompositeClass).get((5, 10))

rawDescription: Load records from arbitrary table by primary key passing a list as arguments


code:
def get_chars(string):
    l = []
    for c in string:
        if c not in l:
            l.append(c)
    return sorted(l)

with open('filename','r') as f:
    data = f.read()
data = [i.strip().lower() for i in data.split(',')]
search = input('Word to find: ').strip().lower()
for i in data[:]:
    if get_chars(i) == s_chars:
        print i

rawDescription: python file handling

description: Python file handling


code:
import re
with open('file.txt') as f:
    lines = sorted(f, key=lambda line: int(re.search(r"(?<=line:)\d+", line).group()))
for l in lines:
    print l

rawDescription: How to sort lines based on specific pattern in that line using python

description: Sort lines based on specific pattern in that line using python


code:
country_list = ['China', 'India', 'Japan']
numbers = list(x/10.0 for x in range(1, len(country_list)+1))
zip(country_list, numbers)
print "\n".join("{} {}".format(x, y) for x, y in _)

rawDescription: Mapping List of Strings to floating numbers - Python


code:
class ArticleAdmin(admin.ModelAdmin):
    list_filter = ["category",]
    list_display = ('category','article_type', 'get_domains')
    filter_horizontal = ("domain",)

    def get_domains(self, obj):
        x ="<br>".join([str(s.name) for s in obj.domain.all()])
        print x  # printing fine
        return x

    get_domains.allow_tags = True

rawDescription: The New line (\n) wasn't selected in Django admin changelist page


code:
import csv
import datetime

minimum_delta = datetime.timedelta(hours=2)

input_fn = r"X:\weatherresults\sea_breeze_dates_2000_2011.csv"
wanted_fn = r"X:\weatherresults\sea_breeze_dates_2000_2011_time_filtered.csv"

with open(input_fn, "rb") as input, open(wanted_fn, "wb") as wanted:
    reader = csv.DictReader(input, delimiter=",", skipinitialspace=True)
    fieldnames = reader.fieldnames
    writer_wanted = csv.DictWriter(wanted, fieldnames, delimiter=",")
    try:
        while 1:
            try:
                begin = next(reader)
                begin_dt = datetime.datetime(
                    year=int(begin["Year Month Day Hour Minutes in YYYY"]),
                    month=int(begin["MM"]), day=int(begin["DD"]),
                    hour=int(begin["HH24"]))
                end = next(reader)
                end_dt = datetime.datetime(
                    year=int(end["Year Month Day Hour Minutes in YYYY"]),
                    month=int(end["MM"]), day=int(end["DD"]),
                    hour=int(end["HH24"]))
                if end_dt - begin_dt > minimum_delta:
                    writer_wanted.writerow(begin)
                    writer_wanted.writerow(end)
            except ValueError as e:
                print "Failed to parse line", line_number
                print row
    except StopIteration:
        pass

rawDescription: Finding the time difference between events in csv file using datetime


code:
x = [(a,b,c) for c in range(1,1001) for b in range(1, c) for a in range(1,b) if a**2+b**2==c**2]
print x

rawDescription: List of numbers whose squares are the sum of two squares


code:
from xlrd import open_workbook
book = open_workbook(workbookName)
for sheet in book.sheets():
    for rowidx in range(sheet.nrows):
        row = sheet.row(rowidx)
        for colidx, cell in enumerate(row):
            if cell.value == "particularString" :
                print sheet.name
                print colidx
                print rowidx

rawDescription: Searching the string and getting the row and column value


code:
for cell in driver.find_elements_by_css_selector(".grid tr a[href$='fg=S2']"):
    print cell.text
    # if you want the number only, you need to strip the grades here
    # also need to check if S2 cell is empty or not

rawDescription: Scraping Webpage using Python

description: Scraping Webpage using Python


code:
#Problem 4
itemList = []
choice = ()
while choice != 4:
    print "1. Add an item."
    print "2. Find an item."
    print "3. Print the message board."
    print "4. Quit."
    choice = input("Enter your selection:")
    if choice == 4:
        break

    #Add an item
    if choice == 1:
        b = "bike"
        m = "microwave"
        d = "dresser"
        t = "truck"
        c = "chicken"
        itemType = raw_input("Enter the item type-b,m,d,t,c:")
        itemCost = input("Enter the item cost:")
        itemList.append((itemType, itemCost))
        print itemList

rawDescription: How do I add multiple items to a List in Python?

description: Add multiple items to a List in Python


code:
class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # filter the events through self
        self.ui.edit_notes.installEventFilter(self)

    def object_edit_notes(self, w):
        # this will convert a QString to a python unicode object
        something = unicode(self.ui.edit_notes.toPlainText())
        print something

    def eventFilter(self, widget, event):
        if (event.type() == QtCore.QEvent.FocusOut and
            widget is self.ui.edit_notes):
            print 'focus out'
            self.object_edit_notes('edit_notes')
            return False
        return QMainWindow.eventFilter(self, widget, event)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

rawDescription: Calling function from another class doesn't work properly


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

def onpick3(event):
    index = event.ind
    xy = event.artist.get_offsets()
    print '--------------'
    print xy[index]

fig, ax = plt.subplots()

x, y = np.random.random((2, 10))
x1, y1 = np.random.random((2, 10))

p = ax.scatter(x, y, marker='*', s=60, color='r', picker=True)
p1 = ax.scatter(x1, y1, marker='*', s=60, color='b', picker=True)

fig.canvas.mpl_connect('pick_event', onpick3)
plt.show()

rawDescription: getting x,y from a scatter plot with multiple datasets?

description: Getting x,y from a scatter plot with multiple datasets


code:
# Get a dictionary
myList = [{'age':x} for x in range(1,10)]

# Enumerate ages
for i, age in enumerate(d['age'] for d in myList):
    print i,age

rawDescription: Iterating over list of dictionaries

description: Iterating over list of dictionaries


code:
import datetime
a = []
csvReader = csv.reader(open(csv_file_to_open, 'rb'), delimiter=',')
for row in csvReader:
    a.append(row)

for row in a:
    try:
        datetime.datetime.strptime(row[0],'%Y-%m-%d')
        print 'yes'
    except ValueError:
        continue

rawDescription: Python - skip if has not pattern of date


code:
def bytes_to_long(bytes):
    result = 0l
    for c in bytes:
        result *= 256
        result += ord(c)
    return result

rawDescription: In Python, read chunks of a file as decimal numbers


code:
instance = Model.objects.get(name='firstName',surname='lastName')

print instance.birthday
print instance.name
print instance.surname

rawDescription: Django save or update model

description: Django save or update model


code:
try:
  from blah import bluh
except ImportError, e:
  import sys
  print 'Import error:', e
  print 'sys.path:', sys.path
  blah = __import__('blah')
  print 'blah is %r' % blah
  try:
    print 'blah is at %s (%s)' % (blah.__file__, blah.__path__)
  except Exception, e:
    print 'Cannot give details on blah (%s)' % e

rawDescription: Cannot import file in Python/Django

description: Cannot import file in Python/Django


code:
html = BeautifulSoup(p)
anchors = html.findAll('a', href='/slideshow')

for anchor in anchors:
    anchor['href'] = '/slideshow_v2'

print html.findAll('a', href='/slideshow_v2')

rawDescription: Replace tag with another in Python Django


code:
with open('data.txt','r') as f:

    first_filter = re.findall(r"[\w']+", f.read())
    measures = first_filter[first_filter.index('responseMeasures')+1:first_filter.index('sortProperty')]
    sortby = first_filter[first_filter.index('sortProperty')+1::]

    for measure in measures:
        print measure

    print "Group by:"

    for sort_type in sortby:
        print sort_type

rawDescription: Python regex search and grouping

description: Python regex search and grouping


code:
def QueryWhere(table,where={}):
    params = ["%s=?"%key for key in where.keys()] #take advantage of builtin query placeholders (?)
    c = db.cursor()
    c.execute("SELECT * FROM %s WHERE %s"%(table," AND ".join(params)),where.values())
    return c.fetchall()

print QueryWhere("users",{"name":"johndoe","pass":"1c4$g"})

rawDescription: Design pattern for generating SQL string


code:
titles = []

for item in channel.findall('item'):
    titles.append(item.find('title').text)
    description = item.find('description').text
    link = item.find('link').text

print random.choice(titles)

rawDescription: Cannot print individual string with random.choice, only single characters


code:
import arcpy
rows = arcpy.UpdateCursor(fc)
fields = arcpy.ListFields(fc, "", "String")
for row in rows:
    for field in fields:
        if field.type != "Geometry":
            if row.getValue(field.name) == '':
                row.setValue(field.name, 'ondciqwn')
                print "%s: Value = %s" % (field.name, row.getValue(field.name))

            if row.isNull(field.name):
                row.setValue(field.name, 'bvadvfe')
                print "%s: Value = %s" % (field.name, row.getValue(field.name))
    rows.updateRow(row)

rawDescription: ArcPy Cursor setValues

description: ArcPy Cursor setValues


code:
import pandas as pd
df = pd.DataFrame([[1, 2, "1,2,4"], [3, 4, "5,6"]], columns=['A', 'B', 'C'])
x = df.drop('C', axis=1).join(df.C.str.split(',', expand=True).stack().to_frame('C').reset_index(1, drop=True))
print x
y = df.drop('C', axis=1).join(pd.concat(df.C.str.split(',', expand=True).to_dict('series').values()).dropna().to_frame('C'))
print y

rawDescription: Parse/unpivot data into the column in pandas dataframe


code:
import xlrd
book = xlrd.open_workbook('pivot_table_demo.xls')
sheet = book.sheet_by_name('Summary')
for row_index in xrange(sheet.nrows):
    print sheet.row_values(row_index)

rawDescription: Extracting data from Excel Pivot Table Spreadsheet in Linux


code:
alternatives = ("// @in ", "// @out ", "// @ret ")
if any(a in sTxT for a in alternatives):
    print "found"

if all(a in sTxT for a in alternatives):
   print "found all"

rawDescription: python, "a in b" keyword, how about multiple a's?

description: Python, "a in b" keyword, how about multiple a's


code:
import gudev

client = gudev.Client(['block'])
for device in client.query_by_subsystem("block"):
    print device
    for device_key in device.get_property_keys():
        print "   property %s: %s" % (device_key, device.get_property(device_key))
    print

rawDescription: How do I use udev to find info about inserted video media (e.g. DVDs)

description: Use udev to find info about inserted video media (e.g. DVDs)


code:
result = commands.getoutput('diff a.txt b.txt')
if len(result) == 0:
   print 'Success'

rawDescription: Python How to check for no return

description: Python How to check for no return


code:
from itertools import combinations
t = (0, 1, 2)
print [subset for r in range(1,4) for subset in itertools.combinations(t,r)]

rawDescription: Subtuples of a tuple

description: Subtuples of a tuple


code:
def bad(default=[]):
    default.append(1)
    print default

bad()
bad()
bad()
def good(default=None):
    if default is None:
            default = []
    default.append(1)
    print default

good()
good()

rawDescription: Python TreeStructure (parent and children) class, children gets appended to it's own children?


code:
import datetime
def date_by_adding_business_days(from_date, add_days):
    business_days_to_add = add_days
    current_date = from_date
    while business_days_to_add > 0:
        current_date += datetime.timedelta(days=1)
        weekday = current_date.weekday()
        if weekday >= 5: # sunday = 6
            continue
        business_days_to_add -= 1
    return current_date

#demo:
print '10 business days from today:'
print date_by_adding_business_days(datetime.date.today(), 10)

rawDescription: Add n business days to a given date ignoring holidays and weekends in python


code:
MAXELEMENT = 8
ElementalSymbols = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O']
ElementalWeights = [1.00794, 4.002602, 6.941, 9.012182, 10.811, 12.0107, 14.0067, 15.9994];

ElementDict= OrderedDict(zip(ElementalSymbols, zip(range(0, MAXELEMENT), ElementalWeights)))

NewOrder = ['Be', 'C', 'H']

BlueMonday = OrderedDict((x, ElementDict[x]) for x in NewOrder)

print BlueMonday
OrderedDict([('Be', (3, 9.012182)), ('C', (5, 12.0107)), ('H', (0, 1.00794))])
print BlueMonday.items()
[('Be', (3, 9.012182)), ('C', (5, 12.0107)), ('H', (0, 1.00794))]

rawDescription: Pulling elements from dict/OrdredDict in arbitrary order


code:
a = """Hi, my name is Lance. Pleased to meet you.
Hi, my name is Jenny. Pleased to meet you.
Yo, dawg. Name's John Erik. Don't touch my fries.
Yo, dawg. Name's James. Don't touch my fries.
I like turtles 53669
Stefan commented on your video.
n00bpwn3rz commented on your video.
RJ wants to talk to you.
Jenny liked your photo.
Pi wants to talk to you.
Pi says visit my website at www.google.com
John Erik says visit my website at www.johniscool.com
James made fruity ice cubes."""

dirty_list = a.split('\n')
clean_list = [] # list of sets containing 'unique sets'
clean_list_pure = [] # list of the original sentences stored as sets in clean_list eg the output
for line in dirty_list:
    line_set = set(line.strip().split(' '))
    if all(len(line_set.intersection(clean_set)) < len(line_set)/2 for clean_set in clean_list):
        clean_list.append(line_set)
        clean_list_pure.append(line.strip())

for cl in clean_list_pure:
    print cl

rawDescription: Using Python, how do I add only distinct messages to a list?


code:
from datetime import datetime
from pytz import timezone
#set user timezone (GMT +10)
user_timezone = timezone('Etc/GMT+10')
#convert local time to user time
user_timezone_time = user_timezone.localize(datetime.now())
#format in 12hr time (%I for 12 hour time and %p for AM PM clarification)
formatted_user_timezone = user_timezone_time.strftime('%Y-%m-%d %I:%M:%S %p %Z%z')
print formatted_user_timezone

rawDescription: How can I get the time of a facebook user?

description: Get the time of a facebook user


code:
import lxml
from lxml import etree
import pykml
from pykml.factory import KML_ElementMaker as KML
from pykml import parser

x = KML.Folder(KML.name("meow"))

with open("Scratch Paper.kml", "r+") as f:
    doc = parser.parse(f).getroot()
    print doc.Document.Folder.Folder[3].name
    a = doc.Document.Folder[0]
    a.append(x)
    finished = (etree.tostring(doc, pretty_print=True))

with open("Scratch Paper.kml", "w+") as f:
    f.write(finished)

print "Done!"

rawDescription: Appending an empty folder to an existing folder in a KML file using Python


code:
import networkx as nx
import random

G = nx.Graph()
G.add_edge(1,2,weight=7)
G.add_edge(1,3,weight=2)
G.add_edge(1,4,weight=2)
G.add_edge(1,5,weight=6)
G.add_edge(2,3,weight=3)
G.add_edge(4,5,weight=3)

max_degree = 2
for node in G:
    number_to_remove = G.degree(node) - max_degree
    if number_to_remove > 0:
        remove = random.sample(G.edges(node), number_to_remove)
        weight_loss = sum(G[u][v]['weight'] for (u,v) in remove)
        G.remove_edges_from(remove)
        print remove, weight_loss

rawDescription: How to properly access edge properties in Networkx

description: Properly access edge properties in Networkx


code:
import msvcrt
import time
import threading

endFlag = False

class InputThread(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):
        global screenLock
        global txUpdated
        global txMessage
        lock = threading.Lock()
        print "Starting " + self.name
        while not endFlag:
            txMessage = self.raw_input_with_cancel()  # This can be cancelled by setting endFlag
            if (txMessage == ""):
                screenLock = 1
                txMessage = raw_input("Enter Tx String: ")
                screenLock = 0

                with lock:
                    txUpdated = 1

        print "Exiting " + self.name

    def raw_input_with_cancel(self, prompt=None):
        if prompt:
            print prompt,
        result = []
        while True:
            if msvcrt.kbhit():
                result.append(msvcrt.getche())
                if result[-1] in ['\r', '\n']:
                    print
                    return ''.join(result).rstrip()
            if endFlag:
                return None
            time.sleep(0.1)  # just to yield to other processes/threads

rawDescription: Python: Stop thread that is waiting for user input


code:
def G(**kwargs):
    for key, value in kwargs.items():
        print "{} = {}".format(key.split("__"), value)

G(some__key__here="val1", another__one__again="val2")

rawDescription: How to implement the Django query-filter expression API's kwarg syntax (e.g. `related_model__fieldname__condition`)?

description: Implement the Django query-filter expression API's kwarg syntax (e.g. `related_model__fieldname__condition`)