code:
from multiprocessing import Process
import time

def foo1(thread):
    print time.ctime()
    time.sleep(10.5)
    print time.ctime()

def progress_bar(timer = 10):
    digits = 4
    delete = '\b' * 6
    time_slot = float(timer) / 100
    for i in range(1, 101):
        delete_bar = '\b' * 52
        if i == 1:
            bar = '|' + ' ' * 50 + '|'
        else:
            bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
        print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
        time.sleep(time_slot)
    print ''

def main():
    p1 = Process(target=foo1, args=('this',))
    # p1 = Process(target = foo1('this'))
    p1.start()
    time.sleep(0.1)
    p2 = Process(target=progress_bar)
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    main()

rawDescription: Python multiprocessing output

description: Python multiprocessing output


code:
from bs4 import BeautifulSoup
import urllib2
url="http://jobsearch.monsterboard.nl/browse/"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
jobs=soup.findAll('a',{'class':'slJobTitle'})
for eachjob in jobs:
 print eachjob['href']

rawDescription: BeautifulSoup4 - All links within 1 div on multiple pages


code:
for i in range(0, 128):
    data += chr(bus.read_byte(address))

print repr(data)
    #prints the whole string as it is received

data = filter(lambda a: a != '\x00')

print repr(data)
    #prints the string without any '\x00' characters.

rawDescription: Raspberrypi Python bus.read_byte

description: Raspberrypi Python bus.read_byte


code:
seq1 = [100, 200, 300]
seq2 = [10, 20, 30, 40]
seq3 = [1000, 2000]

print len(min(seq1, seq2, seq3))
#prints 4 (the length of seq2 because it has the smallest values)

print min(len(x) for x in [seq1, seq2, seq3])
#prints 2 which is the minimum of all the lengths

rawDescription: IndexError: list index out of range for working code


code:
import re

fcontent = '''
def test_a(self):
    var["hello"]
    var["world"]

def test_b(self):
    var["hola"]
    var["mundo"]
'''

dict_ = {}
chunks = [chunk for chunk in fcontent.split('def ') if chunk.strip()]
for chunk in chunks:
    tname = re.findall(r'test\_\w+', chunk)[0]
    vars = re.findall(r'var\[\"\w+\"\]', chunk)
    dict_[tname] = vars
for k, v in dict_.items():
    print k
    for e in v:
        print "\t%s" % e

rawDescription: Find specific variables inside a function and return them sorted


code:
hex_data = "FF"
number = int("0x" + hex_data, 16)
print '%03d' % number # either this
print '{:03d}'.format(number) # or this (Python >= 2.6)

rawDescription: How to Print decimal 0 as 000 in python

description: Print decimal 0 as 000 in python


code:
class PollForm(forms.ModelForm): # used in manage view
                    class Meta:
                        model = StandardWork
                        exclude = ('cbs_and_role','publish', 'standard_work_number')
                        widgets = {
                          'system_description': forms.Textarea(attrs={'rows':3}),
                        }
                    def __init__(self, *args, **kwargs):
                        super(PollForm, self).__init__(*args, **kwargs)
                        if self.instance:
                            print "printing in form"
                            print self.instance.id
                            self.fields['ideal_behaviour'].queryset = IdealBehaviour.objects.filter(cbs__exact=cbsobject)

rawDescription: filter choices for many to many field in modelform django

description: Filter choices for many to many field in modelform django


code:
o = OrderedDict([('toast',5.80),('waffles',2.30),('pancakes',3.99)])
print o.keys().index('waffles')

rawDescription: Equivalent of list.index() for an OrderedDict?


code:
import os

for filename in os.listdir('.'):
   print filename

rawDescription: Python operating on files in a folder - 'for file in folder'


code:
import win32api as api, win32con as con, pywintypes
import win32com.client
_objWMIService = win32com.client.Dispatch('WbemScripting.SWbemLocator')
_objSWbemServices = _objWMIService.ConnectServer('.', 'root\\cimv2')
wmiquery = _objSWbemServices.ExecQuery

# get_regval(regkey) is simple registry reading function.
def get_monitor_res():
    dtd = 54  # start byte of detailed timing desc.

    try:  # get PNP id to find EDID in registry
        for monitor in wmiquery('Select * from Win32_DesktopMonitor'):
            # http://msdn.microsoft.com/en-us/library/aa394122%28VS.85%29.aspx
            if monitor.Availability in (3, 7, 13, 14, 15, 16): # connected
                curres = (monitor.ScreenWidth, monitor.ScreenHeight)
                print 'DEBUG: Current monitor resolution from WMI: %s' % (curres,)
                regkey = ('HKLM\\SYSTEM\\CurrentControlSet\\Enum\\' +
                    monitor.PNPDeviceID + '\\Device Parameters\\EDID')
                edid = get_regval(regkey)
                if edid:
                    print 'DEBUG: EDID Version: %s.%s' % (edid[18], edid[19])
                    # upper nibble of byte x 2^8 combined with full byte
                    hres = ((edid[dtd+4] >> 4) << 8) | edid[dtd+2]
                    vres = ((edid[dtd+7] >> 4) << 8) | edid[dtd+5]
                    print 'DEBUG: EDID DTD0: ' + str((hres, vres))
                    res = (hres, vres)
                    break  # give up on first success
                else:
                    raise RuntimeError, 'EDID not found in registry'
    except (RuntimeError, Exception), e:
        print 'ERROR: %s.' % e

    return res

rawDescription: How to query NATIVE hardware resolution of primary monitor in Windows?

description: Query NATIVE hardware resolution of primary monitor in Windows


code:
import threading

q = 0
def f():
    global q
    q += 1
    print q
    threading.Timer(10, f).start()

f()

rawDescription: changing global variable with thread in Python

description: Changing global variable with thread in Python


code:
#!/usr/bin/python

import socket

def main(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 54321
    max_hops = 40
    icmp = socket.getprotobyname('icmp')
    udp = socket.getprotobyname('udp')
    ttl = 1
    while True:
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
        recv_socket.bind(("", port))
        send_socket.sendto("", (dest_name, port))
        curr_addr = None
        curr_name = None
        try:
            _, curr_addr = recv_socket.recvfrom(512)
            curr_addr = curr_addr[0]
            try:
                curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
        except socket.error:
            pass
        finally:
            send_socket.close()
            recv_socket.close()

        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = "*"
        print "%d\t%s" % (ttl, curr_host)

        ttl += 1
        if curr_addr == dest_addr or ttl > max_hops:
            break

if __name__ == "__main__":
    main('google.com')

rawDescription: recvfrom() not receiving any data - python

description: Recvfrom() not receiving any data - python


code:
import Tkinter as tk
import time

root = tk.Tk()

def change():
    button.config(text='Running')
    button.config(state='disabled')
    print "start"
    root.after(5000,changeback)
def changeback():
    print "end"
    button.config(state='normal')
    button.config(text="Run")

button = tk.Button(root,text="Run",command=change)
button.pack()

root.mainloop()

rawDescription: Change text on tkinter button during call back execution


code:
from itertools import islice

for line in islice(inputFile, 2, None, 4):
    print line

rawDescription: Using splitlines() in python to only get some lines


code:
def iterate_nodes_and_call_exec_func(nodes):
    """
    Get a dict as an argument, containing linux services (initd) as the keys,
    and a list of nodes on which the linux service needs to be checked/
    Iterate over the list of nodes to process,
    create a list of nodes that shouldn't exceed the batch size provided (or 1 if not provided).
    Then using the parallel-ssh module, call the restart_service func on x nodes in parallel (where x is the batch size)
    and provide the linux service (initd) to process.
    If batch_sleep arg was provided, call the sleep func and provide the batch_sleep argument between each batch.
    """

    for initd in nodes.keys():
        work = dict()
        work[initd] = []
        count = 0
        for node in nodes[initd]:
            count += 1
            work[initd].append(node)
            if len(work[initd]) == args.batch:
                restart_service(work[initd], initd)
                work[initd] = []
                if args.batch_sleep > 0 and count < len(nodes[initd]):
                    logger.info('*** Sleeping for %d seconds before moving on to next batch ***', args.batch_sleep)
                    general_utils.sleep_func(int(args.batch_sleep))
        if len(work[initd]) > 0:
            restart_service(work[initd], initd)

def restart_service(nodes, initd):
    """
    Get a list of nodes and linux service as an argument,
    then connect by Parallel SSH module to the nodes and run the service restart command..
    """
    command = 'service {0} restart'.format(initd)
    logger.info('Connecting to {0} to restart the {1} service...'.format(nodes, initd))
    try:
        client = pssh.ParallelSSHClient(nodes, pool_size=args.batch, timeout=10, num_retries=1)
        output = client.run_command(command, sudo=True)
        for node in output:
            for line in output[node]['stdout']:
                if client.get_exit_code(output[node]) == 0:
                    print '[{0}]{1}{2}  {3}{4}'.format(node, Color.BOLD, Color.GREEN, line, Color.END)
                else:
                    print '[{0}]{1}{2}  ERROR! {3}{4}'.format(node, Color.BOLD, Color.RED, line, Color.END)
                    logger.error('[{0}]  Result of command {1} output: {2}'.format(node, command, line))

    except pssh.AuthenticationException:
        print "{0}{1}ERROR! SSH failed with Authentication Error. Make sure you run the script as root and try again..{2}".format(Color.BOLD, Color.RED, Color.END)
        logger.error('SSH Authentication failed, thrown error message to the user to make sure script is run with root permissions')
        sys.exit(2)

    except pssh.ConnectionErrorException as error:
        print("[{0}]{1}{2} ERROR! SSH failed with error: {3}{4}\n".format(error[1], Color.RED, Color.BOLD, error[3], Color.END))
        logger.error("[{0}] SSH Failed with error: {1}".format(error[1], error[3]))
        restart_service(nodes[nodes.index(error[1])+1:], initd)

    except KeyboardInterrupt:
        general_utils.terminate(logger)

def generate_nodes_by_initd_dict(nodes_list):
    """
    Get a list of nodes as an argument.
    Then by calling the get_initd function for each of the nodes,
    Build a dict based on linux services (initd) as keys and a list of nodes on which the initd
    needs to be processed as values. Then call the iterate_nodes_and_call_exec_func and provide the generated dict
     as its argument.
    """
    nodes = {}
    for node in nodes_list:
        initd = general_utils.get_initd(logger, args, node)
        if initd in nodes.keys():
            nodes[initd].append(node)
        else:
            nodes[initd] = [node, ]

    return iterate_nodes_and_call_exec_func(nodes)

def main():
    parse_args()
    try:
        general_utils.init_script('Service Restart', logger, log)
        log_args(logger, args)
        generate_nodes_by_initd_dict(general_utils.generate_nodes_list(args, logger, ['service', 'datacenter', 'lob']))

    except KeyboardInterrupt:
        general_utils.terminate(logger)

    finally:
        general_utils.wrap_up(logger)

if __name__ == '__main__':
    main()

rawDescription: Multiple SSH Connections in a Python 2.7 script- Multiprocessing Vs Threading


code:
L1 = [x[0:-1] for x in train_data]
L2 = [x[-1] for x in train_data]

for x in L1:
    print x

for x in L2:
    print [x]

rawDescription: Accessing elements in a nested list

description: Accessing elements in a nested list


code:
class MyClass(object):
    def __init__(self):
        self.w = {}

    def __setitem__(self, k, v):
        self.w[k] = v

    def __getitem__(self, k):
        return self.w[k]

mc = MyClass()
mc['aa'] = 12
print mc['aa']

rawDescription: I wan to print 'eee fff {'e':'eee','f':'fff'}',how can i get it. (it is about __setattr__)


code:
import win32api
from time import sleep

count = 0
savedpos = win32api.GetCursorPos()
while(True):
    if count>20*5: # break after 5sec
        break

    curpos = win32api.GetCursorPos()
    if savedpos != curpos:
        savedpos = curpos
        print "moved to " + str(savedpos)

    sleep(0.05)
    count +=1

rawDescription: In python, on windows, how to wait until mouse moves?


code:
#!/usr/bin/python
import ogr
from IPython import embed
import sys

drv = ogr.GetDriverByName('ESRI Shapefile') #We will load a shape file
ds_in = drv.Open("MN.shp")    #Get the contents of the shape file
lyr_in = ds_in.GetLayer(0)    #Get the shape file's first layer

#Put the title of the field you are interested in here
idx_reg = lyr_in.GetLayerDefn().GetFieldIndex("P_Loc_Nm")

#If the latitude/longitude we're going to use is not in the projection
#of the shapefile, then we will get erroneous results.
#The following assumes that the latitude longitude is in WGS84
#This is identified by the number "4326", as in "EPSG:4326"
#We will create a transformation between this and the shapefile's
#project, whatever it may be
geo_ref = lyr_in.GetSpatialRef()
point_ref=ogr.osr.SpatialReference()
point_ref.ImportFromEPSG(4326)
ctran=ogr.osr.CoordinateTransformation(point_ref,geo_ref)

def check(lon, lat):
    #Transform incoming longitude/latitude to the shapefile's projection
    [lon,lat,z]=ctran.TransformPoint(lon,lat)

    #Create a point
    pt = ogr.Geometry(ogr.wkbPoint)
    pt.SetPoint_2D(0, lon, lat)

    #Set up a spatial filter such that the only features we see when we
    #loop through "lyr_in" are those which overlap the point defined above
    lyr_in.SetSpatialFilter(pt)

    #Loop through the overlapped features and display the field of interest
    for feat_in in lyr_in:
        print lon, lat, feat_in.GetFieldAsString(idx_reg)

#Take command-line input and do all this
check(float(sys.argv[1]),float(sys.argv[2]))
#check(-95,47)

rawDescription: Check if a geopoint with latitude and longitude is within a shapefile


code:
def GiveAsciiChart5():
    vals = [(value, chr(value)) for value in range(32, 127)]
    for i, (v, c) in enumerate(vals):
        print " |ASCII Value|: ", "|", v, "|", " |Character|: ", "|", c, "|",
        if not i % 4:
            print "\n",

rawDescription: converting program to list comprehension

description: Converting program to list comprehension


code:
import csv
import io

csv_str = u"""Type,Country,State,County,City,
1,USA,,,
2,USA,OH,,
3,USA,OH,Franklin,
4,USA,OH,Franklin,Columbus
4,USA,OH,Franklin,Springfield
4,USA,WI,Dane,Madison
"""

counties = []
states = []

def handle_missing_data(row):
    try:
        rtype = int(row[0])
    except ValueError:
        return []

    rtype = row[0]
    country = row[1]
    state = row[2]
    county = row[3]

    rows = []
    # if a state is present and it hasn't a row of it's own
    if state and state not in states:
        rows.append([rtype, country, state, '', ''])
        states.append(state)

    # if a county is present and it hasn't a row of it's own
    if county and county not in counties:
        rows.append([rtype, country, state, county, ''])
        counties.append(county)

    # if the row hasn't already been added add it now
    if row not in rows:
        rows.append(row)

    return rows

csvf = io.StringIO(csv_str)
reader = csv.reader(csvf)
for row in reader:
    new_rows = handle_missing_data(row)
    for new_row in new_rows:
        print new_row

rawDescription: Csv - Inserting missing rows

description: Csv - Inserting missing rows


code:
def supplier_detail(request, supplier_id):
    s = get_object_or_404(Supplier, pk=supplier_id)
    for employee in s.supplieremployees_set.all():
        print employee.expe_fname
    return render_to_response('suppliersdb/supplier_detail.html', {'supplier': s})

rawDescription: django: Retrieve object via backwards relationship in view.py, not template

description: Django: Retrieve object via backwards relationship in view.py, not template


code:
import re
with open("filename") as f:
    for line in f:
        if re.match(r"^\d+.*$",line):
            print line

rawDescription: Read lines starting with numbers

description: Read lines starting with numbers


code:
def main( ):
    plc = OmronPLC( )
    ip = plc.openFins('ip_address', port)
    d2000 = plc.readMemC('D2000', 2)
    d2005 = plc.readMemC('D2005', 5)
    plc.close ( )
    print ip, d2000, d2005
    return [ip, d2000, d2005]

rawDescription: Create CSV file from read Data from def main()


code:
from collections import Counter
llist = ['a','b','c','b','e','a','f','e','f','e','e','e','a']
c = Counter(llist)
for i,j in c.most_common():
    print j,i

rawDescription: Count of items in a list in descending order


rawDescription: How to apply CMA-ES optimization to an arbitrary user defined objective function in Python?

description: Apply CMA-ES optimization to an arbitrary user defined objective function in Python


code:
from bs4 import BeautifulSoup

with open('text.html') as input_file:
    soup = BeautifulSoup(input_file)
    a_links = soup.find_all('a')
    for link in a_links:
        print link

rawDescription: How can I print specific tags from a html file with Python?

description: Print specific tags from a html file with Python


code:
import hashlib
class User:
    def __init__(self, emailAddress, fName):
        parts = emailAddress.split('@')
        if len(parts) != 2:
            raise Exception("Invalid email address: %s" % emailAddress)
        self.emailAddress = emailAddress
        self.emailMd5     = hashlib.md5(emailAddress).hexdigest()
        self.domain       = parts[1]
        self.fName        = fName

u = User("me@you.com","uname") # create instance
print u.fName # print instance attribute

rawDescription: Python, Class has no Attribute. How can I return a value of a value that has been passed into class?


code:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import pandas as pd
from sys import exit
def update_lines(num, data, line):
    # NOTE: there is no .set_data() for 3 dim data...
    line.set_data(data[0:2, :num])
    line.set_3d_properties(data[2, :num])
    return line

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Reading the data from a CSV file using pandas
repo = pd.read_csv('data.csv',sep=',',header=0)
data = np.array((repo['x'].values, repo['y'].values, repo['z'].values))
print data.shape[1]
#exit()

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
line = ax.plot(data[0, 0:1], data[1, 0:1], data[2, 0:1])[0]

# Setting the axes properties
ax.set_xlim3d([-2.0, 2.0])
ax.set_xlabel('X')

ax.set_ylim3d([-2.0, 2.0])
ax.set_ylabel('Y')

ax.set_zlim3d([-2.0, 2.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, data.shape[1], fargs=(data, line), interval=50, blit=False)

plt.show()

rawDescription: Plotting 3D trajectory from CSV data using matplotlib


code:
from collections import Counter

c = Counter([i for j in trainY for i in j])

print c['php'] # Returns 2
print c.most_common(5) # Print the 5 most common items.

rawDescription: Faster way to count number of string occurrences in a numpy array python


code:
import os, glob

WATCH_DIR = 'D:/tivo'
TO_FIND = ['*big.brother.uk.s15*', '*mock.the.week*', '*family.guy*']

history = set(open('history.txt').read().splitlines())

new_files = set()
for pattern in TO_FIND:
        files = glob.glob(os.path.join(WATCH_DIR, pattern))
        # optionally strip directories from file names
        files = [os.path.basename(f) for f in files]
        new_files.update(files)

new_files = new_files.difference(history)
for f in sorted(new_files):
        print "new: %s" % f

history.update(new_files)
open('history.txt', 'w').write('%s\n' % '\n'.join(sorted(history)))

rawDescription: How to get regex to return string (not a regex object)?

description: Get regex to return string (not a regex object)


code:
out_L = "C2"
out_H = "F2"

hex_out = ''.join((out_H, out_L))
value = int(hex_out, 16) # value = 62146
if value> 0x7FFF:
    value -= 0x10000

print value # output -3390

rawDescription: Working with two's complement in Python

description: Working with two's complement in Python


code:
conn = sqlite3.connect('mydb')
conn.close()
try:
  resultset = conn.execute("SELECT 1 FROM my_table LIMIT 1;")
except sqlite3.ProgrammingError as e:
  print e

rawDescription: How to tell if Python SQLite database connection or cursor is closed?

description: Tell if Python SQLite database connection or cursor is closed


code:
# this prints a blank line
print
"""
some text
"""

# this prints 'some text'
print """
some text
"""

rawDescription: How to style CGI python with CSS?

description: Style CGI python with CSS


code:
def chunks(seq, n):
    # http://stackoverflow.com/a/312464/190597 (Ned Batchelder)
    """ Yield successive n-sized chunks from seq."""
    for i in xrange(0, len(seq), n):
        yield seq[i:i + n]

for i,group in enumerate(dynamic_grouper(iterable, chunk_sizes)):
    for item in chunks(group, 5):
        print "Group %i" % i
        print "Items %s" % list(item)

rawDescription: grouper with a chunk size sequence in python?

description: Grouper with a chunk size sequence in python


code:
from datetime import datetime
from pytz import timezone

fmt = "%Y-%m-%d %H:%M:%S"

# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(fmt)

# Convert to US/Pacific time zone
now_pacific = now_utc.astimezone(timezone('US/Pacific'))
print now_pacific.strftime(fmt)

# Convert to Europe/Berlin time zone
now_berlin = now_pacific.astimezone(timezone('Europe/Berlin'))
print now_berlin.strftime(fmt)

rawDescription: How to get user's local timezone other than server timezone(UTC) in python?

description: Get user's local timezone other than server timezone(UTC) in python


code:
def run_bot():
        print("Getting Subreddit ...")
        subreddit = r.get_subreddit(SUBREDDIT)
        print("Getting comments ...")
        comments = subreddit.get_comments(limit=SUBMISSION_ID)
        submission = r.get_submission(submission_id='49q8l1')
        flat_comments = praw.helpers.flatten_tree(submission.comments)
        already_done = set()
        for comment in flat_comments:
            print comment.body
            if comment.body == "/u/TruthAndDareBot Truth" and comment.id not in already_done:
                randomTruth = random.choice(truths)
                comment.reply(randomTruth)
                print("Found a truth and I'm going to reply to it. Comment ID is: "+ comment.id)
                cache.append(comment.id)
            if comment.body == "/u/TruthAndDareBot Dare" and comment.id not in already_done:
                randomDare = random.choice(dares)
                comment.reply(randomDare)
                print("Found a Dare and I'm going to reply to it. Comment ID is: "+ comment.id)
                cache.append(comment.id)

rawDescription: Reddit bot stops after checking all comments


code:
def h(user_function, parameters=()):
    for a in range(4):
        for b in range(3):
            if (user_function(a, b, *parameters)):
                print "it's True"
            else:
                print "it's False"

rawDescription: Handling a user-defined function that may have a variable number of arguments


code:
import random
from twisted.internet import task

class MyProcess:
  def __init__(self):
    self.stats = []
    self.lp = None
  def myloopingCall(self):
    print "I have %s stats" % len(self.stats)
  def myMainFunction(self,reactor):
    self.stats.append(random.random())
    reactor.callLater(0,self.myMainFunction,reactor)
  def start(self,reactor):
    self.lp = task.LoopingCall(self.myloopingCall)
    self.lp.start(2)
    reactor.callLater(0,self.myMainFunction,reactor)
  def stop(self):
    if self.lp is not None:
      self.lp.stop()
    print "I'm done"

if __name__ == '__main__':
  myproc = MyProcess()
  from twisted.internet import reactor
  reactor.callWhenRunning(myproc.start,reactor)
  reactor.addSystemEventTrigger('during','shutdown',myproc.stop)
  reactor.callLater(10,reactor.stop)
  reactor.run()

rawDescription: Threads in twisted... how to use them properly?


rawDescription: Python's most efficient way to choose longest string in list?


code:
import multiprocessing as mp
import os

def gen_fil():
    folder = 'C:\mptest'
    if not os.path.exists(folder):os.makedirs(folder)
    name = os.path.join(folder,'000.txt')
    i=0
    while os.path.exists(name):
        i+=1
        name=os.path.join(folder,'{:03d}.txt'.format(i))
    h = open(name, 'a', 0)
    h.close()

gen_fil()

def worker(n):
    return n*n

if __name__=='__main__':

    cpus = mp.cpu_count()

    p = mp.Pool(cpus)
    data = range(int(5e7))
    print 'start'
    print 'num cpus', cpus
    try:
        results = p.map(worker, data)
        p.close()
        p.join()
    except:
        p.terminate()
        p.join()
        raise
    print len(results)
    print 'end'

rawDescription: How does Python handle multiprocessing on Windows?


code:
X = pd.DataFrame(np.random.normal(0, 1, (100, 3)))
X['NCP'] = np.random.randint(0, 5, 100)
X[X['NCP'] == 0] += 100

groups = X.groupby('NCP')

print groups.groups.keys()

# This gets a number of subplots equal to the number of groups in a single
# column.  you can adjust this yourself if you need.
fig, axes = plt.subplots(len(groups.groups), 1, figsize=[10, 12])

# Loop through each group and plot boxplot to appropriate axis
for i, k in enumerate(groups.groups.keys()):
    group = groups.get_group(k)
    group.boxplot(ax=axes[i], return_type='axes')

rawDescription: pandas boxplot, groupby different ylim in each subplot

description: Pandas boxplot, groupby different ylim in each subplot


code:
#IMPORTS
from xml.dom import minidom
import urllib

#IMPORTING XML FILE
xmldocurl = 'http://gamebattles.majorleaguegaming.com/ps4/call-of-duty-ghosts/team/TeamCrYpToNGamingEU/stats.xml'
settings = urllib.urlopen(xmldocurl).read()
final = minidom.parseString(settings)

recentMatches = final.getElementsByTagName('recentMatches')[0]

for node in recentMatches.childNodes:
    if node.nodeName == "match":
        nodes = node.getElementsByTagName('url')
        print nodes[0].childNodes[0].data
        nodes = node.getElementsByTagName('date')
        print nodes[0].getAttribute('timestamp')
        break

rawDescription: I want to be able to chose which nodes to be printed when importing an XML document to Python


code:
import __main__ as main
print hasattr(main, '__file__')

rawDescription: Tell if python is in interactive mode

description: Tell if python is in interactive mode


code:
class A:
   def bar(self):
       print 'Bar'

   def apply(self, func):
       func()  # call it like any other function

   def copyApply(self, func):
       cpy = copy.copy(self)
       func(cpy)  # cpy becomes the self parameter

a = A()
func = a.bar  # don't call the function yet

a.apply(func)       # call the bound method `a.bar`
a.apply(a.bar)      # same as the line above
a.copyApply(A.bar)  # call the unbound method `A.bar` on a new `A`

rawDescription: Call a functor in Python

description: Call a functor in Python


code:
for k, v in user_actions.iteritems():
    print v['notes']

rawDescription: Django deserialize JSON request with nested dictionary


code:
import pandas as pd
import csv
import sys

def ignore_comments(fp, start_fn, end_fn, keep_initial):
    state = 'keep' if keep_initial else 'start'
    for line in fp:
        if state == 'start' and start_fn(line):
            state = 'keep'
            yield line
        elif state == 'keep':
            if end_fn(line):
                state = 'drop'
            else:
                yield line
        elif state == 'drop':
            if start_fn(line):
                state = 'keep'

if __name__ == "__main__":

    df = open('x.in')
    df = csv.reader(df, skipinitialspace=True)
    df = ignore_comments(
        df,
        lambda x: x == ['header1', 'header2', 'header3'],
        lambda x: x == [],
        False)

    df = pd.read_csv(df, engine='python')
    print df

rawDescription: Python pandas: read csv with multiple tables repeated preamble


code:
short_urls = ['google.com', 'twitter.com', 'google.com']
duplicates_removed_list = list(set(short_urls))
print duplicates_removed_list # Prints ['google.com', 'twitter.com']

rawDescription: Compare items in list with nested for-loop


code:
b_timedelta = datetime.timedelta(hours=b.hour, minutes=b.minute, seconds=b.second,  microseconds=b.microsecond)
result = a + b_timedelta
print result
print result.strftime('%H:%M:%S.%f')

rawDescription: How to add between two deltatime in python

description: Add between two deltatime in python


code:
n=int(raw_input('n='))
for i in range(1,n+1):
    for j in range(1,n+1):
        if j==1 or j==n or j==i:
            print'#',
        else:
            print' ',

    print

rawDescription: Print the following pattern in Python language


code:
import csv

spreadsheet_filenames = [
    '0113_RE_fscom.csv',
    '0213_RE_fscom.csv',
]

for filename in spreadsheet_filenames:
    with open(filename, 'r') as csvfile:
        sales = csv.reader(csvfile)
        for row in sales:
            print row

rawDescription: How to merge vertically several csv files in Python?

description: Merge vertically several csv files in Python


code:
class X(object): pass

x=X()
exec 'a=23' in vars(x)
x.a

rawDescription: What is the best way to create a Python object when you have the class implementaion stored in a string?


code:
from win32api import GetSystemMetrics

print "Width =", GetSystemMetrics(0)
print "Height =", GetSystemMetrics(1)

rawDescription: How do I get monitor resolution in Python?

description: Get monitor resolution in Python


code:
for line in input_file:
    numbers = line.replace('\n',"").split(',')

    min = int(numbers[0])
    for num in numbers:
        n = int(num)
        if n < min:
            min = n
    print min

rawDescription: How to find the smallest number in each line of numbers? Without using min()

description: Find the smallest number in each line of numbers? Without using min()


code:
deleted_folders = []

for r,d,f in os.walk(directory):
    for dir in d:
         timestamp = os.path.getmtime(os.path.join(r,dir))
         if now-numdays > timestamp:
             try:
                 shutil.rmtree(os.path.join(r,dir))
                 deleted_folders.append("Deleted folders are: %s" %
                                        os.path.join(r,dir))
             # Bad, it's almost never appropriate to catch all Exceptions
             # In this case, OSError seems better
             except Exception,e:
                 pass

body = MIMEText("\n".join(deleted_folders), "plain")
msg.attach(body)

rawDescription: Sending email in python, message missing


code:
import paramiko
import sys

nbytes = 4096
hostname = 'hostname'
port = 22
username = 'username'
password = 'password'
command = 'ls'

client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)

stdout_data = []
stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(command)
while True:
    if session.recv_ready():
        stdout_data.append(session.recv(nbytes))
    if session.recv_stderr_ready():
        stderr_data.append(session.recv_stderr(nbytes))
    if session.exit_status_ready():
        break

print 'exit status: ', session.recv_exit_status()
print ''.join(stdout_data)
print ''.join(stderr_data)

session.close()
client.close()

rawDescription: python paramiko ssh

description: Python paramiko ssh


code:
from openpyxl import load_workbook

wb = load_workbook('test.xlsx', use_iterators=True)
ws = wb.get_sheet_by_name('Sheet1')

# printing coordinates of all rows and cols
for row in ws.iter_rows(row_offset=2):
    for cell in row:
        print cell.coordinate

# printing coordinates from "A3:E5" range
for row in ws.iter_rows(range_string="A1:E3", row_offset=2):
    for cell in row:
        print cell.coordinate

rawDescription: openpyxl row iterator ignores row_offset argument?

description: Openpyxl row iterator ignores row_offset argument


code:
from multiprocessing.managers import BaseManager
from multiprocessing.managers import BaseProxy

class DataSetClass(list):
    @property
    def n_training(self):
        return len(self) * 0.8
    @property
    def n_validation(self):
        return len(self) * 0.2

class DataSetProxy(BaseProxy):
    _exposed_ = ('append', '__len__')
    def append(self, item):
        return self._callmethod('append', item)
    def __len__(self):
        return self._callmethod('__len__')

class MyManager(BaseManager):
    pass

MyManager.register('DataSet', DatasetClass, DataSetProxy)

if __name__ == '__main__':
    manager = MyManager()
    manager.start()
    dataset = manager.DataSet()
    dataset.append('item1')
    dataset.append('item2')
    print len(dataset)

rawDescription: How to inherit "Manager().list" in python?

description: Inherit "Manager().list" in python


code:
s = 'W:1.0,X:1.1(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5,(F:0.6,G:0.7)H:0.8)Y:0.9'

def max_depth(s, start_ch='(', end_ch=')'):
    depth = 0
    best = 0
    for ch in s:
        if ch == start_ch:
            depth += 1
            best = max(depth, best)
        elif ch == end_ch:
            depth -= 1
            if depth < 0:
                raise ValueError('illegal string - unmatched close-paren')
    if depth:
        raise ValueError('illegal string - unmatched open-paren')
    return best

print max_depth(s)    # => 2

rawDescription: How to search for repetitions in strings in Python

description: Search for repetitions in strings in Python


code:
import numpy as np
import os

dtype = np.dtype([
    ("time", np.float32),
    ("PosX", np.float32),
    ("PosY", np.float32),
    ("Alt", np.float32),
    ("Qx", np.float32),
    ("Qy", np.float32),
    ("Qz", np.float32),
    ("Qw", np.float32),
    ("dist", np.float32),
])

f = open("myfile", "rb")
f.seek(1863, os.SEEK_SET)

data = np.fromfile(f, dtype=dtype)
print x

rawDescription: Read a binary file using Numpy fromfile and a given offset


code:
import sys, time
from PyQt4 import QtGui as qt
from PyQt4 import QtCore as qtcore

app = qt.QApplication(sys.argv)
class widget(qt.QWidget):
    def __init__(self, parent=None):
        qt.QWidget.__init__(self)

    def appinit(self):
        thread = worker()
        self.connect(thread, thread.signal, self.testfunc)
        thread.start()

    def testfunc(self, sigstr):
        print sigstr

class worker(qtcore.QThread):
    def __init__(self):
        qtcore.QThread.__init__(self, parent=app)
        self.signal = qtcore.SIGNAL("signal")
    def run(self):
        time.sleep(5)
        print "in thread"
        self.emit(self.signal, "hi from thread")

def main():
    w = widget()
    w.show()
    qtcore.QTimer.singleShot(0, w.appinit)
    sys.exit(app.exec_())

main()

rawDescription: pyqt4 emiting signals in threads to slots in main thread

description: Pyqt4 emiting signals in threads to slots in main thread


code:
def startString(char, stringlist):
    newlist = []
    for string in stringlist:
        if string.startswith(char):
            newlist.append(string)
    print newlist

rawDescription: Python - Add words from string list that begin with a specific char


code:
print subprocess.check_output(["program",
                               "-v",
                               "-a5",
                               "-t%i" % test,
                               "-oURL",
                               "-uUSER",
                               "-pPASS"])

rawDescription: Variable inside subprocess.check_output argument ? [Python]


code:
class Foo(object):
    def bar(self, name):
        return name
    def bar2(self, attr, value):
        print attr, value
    def __getattr__(self, attr):
        return self.bar(attr)
    def __setattr__(self, attr, value):
        self.bar2(attr, value)
foo = Foo()
print foo.name
foo.name = 'not name'

rawDescription: How to access member function as member variable in python classes?

description: Access member function as member variable in python classes


code:
import urllib
urllib.unquote('/media/music/k%C3%A1lm%C3%A1n balogh and the gipsy cimbalom band/aven shavale').decode('utf8')
print urllib.unquote('/media/music/k%C3%A1lm%C3%A1n balogh and the gipsy cimbalom band/aven shavale').decode('utf8')

rawDescription: Weird character encoding issue with python / nautilus scripts combo


code:
def myhook(ui, repo, **kwargs):
    print kwargs
    from pdb import set_trace
    set_trace()

rawDescription: ignore certain mercurial commands in mercurial hook

description: Ignore certain mercurial commands in mercurial hook


code:
def hi():
    print 'hi'

funcs = {hi: 0}

funcs   # {<function hi at 0x...0>: 0}

for func in funcs:
    func()             # call func, prints "hi"
    funcs[func] += 1   # increment call counter

funcs   # {<function hi at 0x...0>: 1}

rawDescription: Using a function as dict key

description: Using a function as dict key


code:
seq="TCATTCTaTTTAAAAAAatATAAAaGGGgTTTTGGGgTTTtGGGGTTTtGGGG"
stop_codons = ["TAG", "TAA"]

def printcount(seq, stop_codons, start):
    found = False
    count = 0
    n = start
    while n < len(seq):
        if seq[n:n+3] in stop_codons:
            found = True
            break
        count += 1
        n += 3
    print count if found else 0

printcount(seq, stop_codons, 0)
printcount(seq, stop_codons, 1)
printcount(seq, stop_codons, 2)

rawDescription: Counting the distance between values with python


code:
d = {
  'str'  : StringProperty,
  'time' : unicode(DateTimeProperty)
}
s = simplejson.dumps(d)
print s

rawDescription: How to use simplejson to serialize and maintain properties?

description: Use simplejson to serialize and maintain properties


code:
while True:
    input_x = int(raw_input("insert # or 0 to quit"))
    if input_x == 0: # break loop if user enters 0
        print "Goodbye"
        break
    print input_x + 1
    print input_x

rawDescription: How to reset inputs in python?

description: Reset inputs in python


code:
# t1 class is the same as yours

class t2:
    def __init__(self, t1):
        self.t1 = t1
    def getVar(self):
        print self.t1.returnVar(t1)

obj1 = t1()
obj2 = t2(obj1)  # give the t2 instance the t1 instance

print obj2.getVar()

rawDescription: Calling class attribute without creating an instance to that class


code:
if os.system("/usr/libexec/oah/translate > /dev/null 2>&1"):
  print "Not installed"
else:
  print "Installed"

rawDescription: How to tell if rosetta is installed? --MacPython

description: Tell if rosetta is installed? --MacPython


code:
import numpy

# create some random data for testing
sz = 100
a  = numpy.random.random(sz*sz/2 - sz/2).astype('S50')

# convert back to float for a test on minus signs,
# as it would be done if a is read as string values
amins = numpy.where(a.astype(float) <= 0, "", "-")

# get the values without minus signs
aplus = numpy.char.lstrip(a, "-")

# addup to negated string values
aminus = numpy.char.add(amins, aplus)

# create an empty matrix
m = numpy.zeros(shape=(sz,sz), dtype='S51')
# ids of the upper triangle
u_ids = numpy.triu_indices(sz,1)
# set upper values
m[u_ids] = a
# switch coordinates to set lower values
m[u_ids[1],u_ids[0]] = aminus
# fill diag with zeros
numpy.fill_diagonal(m, numpy.zeros(sz).astype('S51'))

print m

rawDescription: What is the most efficient way to get this kind of matrix from a 1D numpy array?


code:
l = []
for x in range(0, 50):
    for y in range(0, 50):
        if hash((x,y)) in l:
            print "Fail: ", (x,y)
        l.append(hash((x,y)))
print "Test Finished"

rawDescription: Time complexity of accessing a Python dict


code:
import serial

ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
   value = float(ser.readline().strip())
   print 'New value is {0:0.2f}'.format(value)

rawDescription: Understanding characters received from Arduino


code:
import sqlite3

db = sqlite3.connect("C:/temp/MyLittleDatabase")
db.execute("CREATE TABLE IF NOT EXISTS T(x)")
db.execute("INSERT INTO T VALUES (42)")
cursor = db.execute("SELECT x FROM T")
for row in cursor:
    print row[0]

rawDescription: Python: sqlite3

description: Python: sqlite3


code:
from functools import partial

funcs = [lambda x, y: x + y] * 10 # a list of ten placeholder functions.

def z(functions, x, y):
    return sum(f(x, y) for f in functions)

a = partial(z, funcs, 1)
# 'a' is basically the same as: lambda y: z(funcs, 1, y)
print a # <functools.partial object at 0x7fd0c90246d8>
print a(1) # 20
print a(2) # 30
print a(3) # 40

rawDescription: How to define sum of multiple arbitrary functions?

description: Define sum of multiple arbitrary functions


code:
NAMESPACES = {'ns': 'http://www.starstandard.org/STAR/5', 'ns1': 'http://www.openapplications.org/oagis/9'}
ro_xml = '{}.xml'.format(6001265)
parser = etree.XMLParser(ns_clean=True)
tree = etree.parse(ro_xml, parser)

vehicle = {}
vehicle_info = tree.xpath(twc.XML_VEHICLE_INFO, namespaces=NAMESPACES)
for child in vehicle_info:
    vehicle.update({child.tag: child.text})
model = residence['{%s}Model' % NAMESPACES['ns']]
print model

rawDescription: lxml ns_clean=True does not appear to work

description: Lxml ns_clean=True does not appear to work


code:
for line in open("file"):
    print line[4:8] , line[11:16]

rawDescription: Formatted Input in Python

description: Formatted Input in Python


code:
from itertools import groupby
with open('testing.txt') as file:
    goodlines = [line.strip() for line in file if line.strip()]
    splitted = list( list(val[1]) for val in groupby(goodlines, lambda l: l[0].isdigit()))
    names = [ list(val)[0] for val in splitted[::2] ]
    val_split = [ [v.split() for v in val] for val in splitted[1::2] ]
    val_sorted = [ sorted(val, key=lambda v:int(v[1]))[-1][0] for val in val_split]
    result = dict(zip(names, val_sorted))
print result

rawDescription: Read different lines from file to work with


code:
import networkx as nx

data = [("S", [], ["a","b","c","d"]),
        ("A", ["a","b"], ["e"]),
        ("B", ["a","c"], ["f"]),
        ("Si", ["a","b","c","d","e","f"], [])]

G = nx.DiGraph()
#G = nx.Graph() # maybe you want an undirected graph?
nodes = []
for n,inedges,outedges in data:
    nodes.append(n)
    for s in inedges:
        G.add_edge(s,n)
    for t in outedges:
        G.add_edge(n,t)

P = nx.projected_graph(G,nodes)

print list(P.nodes())
print list(P.edges())
# OUTPUT
# ['A', 'S', 'B', 'Si']
# [('A', 'Si'), ('S', 'A'), ('S', 'Si'), ('S', 'B'), ('B', 'Si')]

rawDescription: Use python to generate graph using node constraints


code:
class Foo(object):
    def haha(self):
        print 3
    def __init__(self):
        def haha():
            print 4
        self.haha = haha

a = Foo()
a.haha
a.haha()

rawDescription: Defining class functions inside class functions: Python


code:
b=re.findall(r'[a-z][-\'a-z]*[\'a-z]',a)
for i in b: print i

rawDescription: Python Regex Help Needed (Basic)

description: Python Regex Help Needed (Basic)


code:
for n1 in range(0,len(numbers)):
    for n2 in range(1,len(numbers)):
        if numbers[n1]==numbers[n2] and n1 != n2:
            print numbers
            print numbers[n1]
            endLoop=True
    if endLoop:
        break

rawDescription: How to check if there is a repeated value in a list and get that value

description: Check if there is a repeated value in a list and get that value


code:
import smtplib
fromMy = 'yourMail@yahoo.com' # fun-fact: from is a keyword in python, you can't use it as variable, did abyone check if this code even works?
to  = 'SomeOne@Example.com'
subj='TheSubject'
date='2/1/2010'
message_text='Hello Or any thing you want to send'

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )

username = str('yourMail@yahoo.com')
password = str('yourPassWord')

try :
    server = smtplib.SMTP("smtp.mail.yahoo.com",587)
    server.login(username,password)
    server.sendmail(fromMy, to,msg)
    server.quit()
    print 'ok the email has sent '
except :
    print 'can\'t send the Email'

rawDescription: Sending e-mails using yahoo account in python


code:
def find_max(s):
    _ret = []

    def string_iter(concat, compare):

        def appender():
            if len(concat) >= len(_ret[-1]):
                if len(concat) > len(_ret[-1]):
                    while _ret:
                        _ret.pop()
                _ret.append(concat)

        if len(compare) == 0:
            if len(_ret) != 0:
                appender()
            else:
                _ret.append(concat)
            return

        if concat[-1] < compare[0]:
            concat += compare[0]
            string_iter(concat, compare[1:])
        else:
            if len(_ret) != 0:
                appender()
            else:
                _ret.append(concat)
            string_iter(compare[0], compare[1:])

    string_iter(s[0], s[1:])

    return _ret

print find_max('abc')      # -->['abc']
print find_max('afz')      # -->['afz']
print find_max('cba')      # -->['c','b','a']
print find_max('zfa')      # -->['z','f','a']
print find_max('abczabc')  # --> ['abcz']
print find_max('abcabcpaidfbkjabdsfilabdfkabldfjadf')   # --> ['abcp', 'abdfk']

rawDescription: How can I find the longest ascending substring (or tied substrings)?

description: Find the longest ascending substring (or tied substrings)


code:
if __name__ == '__main__':
    # Open the file for reading.
    rd_file_name = "../txt/q1.txt"
    rd_file = open(rd_file_name, 'r')

    # Read through the header
    for line in rd_file.readlines():
        # Decide what to do based on the content in the line.
        if "header" in line.lower():
            # Don't print the header
            pass
        elif line.strip() == "":
            # Quit if you see a blank line
            break
        else:
            # We print the body.  Lines end with a carriage return, so we don't
            # add another one.
            print line,

    # Clean up
    rd_file.close()

rawDescription: Print lines from file between header and empty line


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

browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()

try:
    WebDriverWait(browser, 3).until(EC.alert_is_present(),
                                   'Timed out waiting for PA creation ' +
                                   'confirmation popup to appear.')

    alert = browser.switch_to_alert()
    alert.accept()
    print "alert accepted"
except TimeoutException:
    print "no alert"

rawDescription: Check if any alert exists using selenium with python


code:
#!/usr/bin/env python2.7

import re

RE_WORD = re.compile(r'\b[a-zA-Z]+\b')

keywords = frozenset(['foo', 'bar', 'joe', 'mauer'])
listOfStrings = ['I am frustrated', 'this task is foobar', 'mauer is awesome']

for i in listOfStrings:
    for word in RE_WORD.findall(i):
        if word in keywords:
            print i
            continue

rawDescription: Search through list of strings and determine if there is an exact match in separate list of strings. python. sentiment analysis


code:
import Tkinter as tk

bgcolors = ("blue", "green", "yellow", "purple", "red", "#a1dbcd")

def cycle():
    delta = 500
    delay = delta
    for x in range(3):
        for c in bgcolors:
            window.after(delay, lambda c=c: window.configure(bg=c))
            delay += delta
        print x

window = tk.Tk()
window.pack_propagate(0)

b = tk.Button(window, text='cycle bg', command=cycle)
b.pack()

window.mainloop()

rawDescription: Python ForLoop with nested after() functions happening after loop


code:
def test(*args, **kwargs):
    print "Fooo"

import maya.OpenMaya as OpenMaya
idx = OpenMaya.MEventMessage.addEventCallback("SelectionChanged", test)

#when ever you finish doing your stuff
OpenMaya.MMessage.removeCallback(idx)

rawDescription: How to get from Maya event of a change highlight objects in the scene?

description: Get from Maya event of a change highlight objects in the scene


code:
import subprocess
import time

lights_proc = subprocess.Popen(["/usr/bin/python","/home/pi/python/light30.py"])
time.sleep(5) # sleep for 5 seconds
print subprocess.Popen(["/usr/bin/python","/home/pi/python/camtest.py"],stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()

rawDescription: Raspberry Pi Model B Threading. Run 2 python scripts concurrently


code:
def postorder_transversal(self, add_newline=True):
    if self.node == None:
        return 0
    for child in self.children:
        child.postorder_transversal(add_newline=False)
    print self.node,
    if add_newline:
        print

rawDescription: Python: Printing a new line after a recursive function


code:
import json
jsoninput = json.loads(jsonoutput)
print jsoninput
print jsoninput['1']

rawDescription: cannot output a json encoded dict containing accents (noob inside)

description: Cannot output a json encoded dict containing accents (noob inside)


code:
def function1(n, function_object):
    if n > 0:
        function_object() # <---- Added ()
        function1(n-1, function_object)

def function2(x, word):
    for i in range(x):
        print word

y = 'pluto'

function1(3, lambda: function2(3, y)) # <---

rawDescription: How do I use recursion to write a function that calls another function repeatedly in Python?

description: Use recursion to write a function that calls another function repeatedly in Python


code:
from datetime import datetime

def time_diff(start, end):
    start_dt = datetime.strptime(start, '%H:%M:%S')
    end_dt = datetime.strptime(end, '%H:%M:%S')
    diff = (end_dt - start_dt)
    return diff.seconds

scores = {}
with open('input.txt') as fin:
    for line in fin.readlines():
        values = line.split(',')
        scores[values[0]] = time_diff(values[1],values[2])

with open('sorted.txt', 'w') as fout:
    for key, value in sorted(scores.iteritems(), key=lambda (k,v): (v,k)):
        fout.write('%s,%s\n' % (key, value))

rawDescription: Python - Calculating Time Difference from Text File


code:
import  os,platform,logging

if platform.platform('windows'):
    logging_file=os.path.join(os.getenv('HOMEDRIVE'),os.getenv('HOMEPATH'),'test.log')
else:
    logging_file = os.path.join(os.getenv('HOME'),'test.log')

print"logging to",logging_file

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s : %(levelname)s :%(message)s',
                    filename=logging_file,
                    filemode='w')

logging.debug("start of the asdfasdfprogram")
logging.info("doing something")
logging.warning("u are gonna die")

rawDescription: No Log file is generated -- python using logging module


code:
import pyodbc
import numpy

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer;Trusted_Connection=yes;')
cur = conn.cursor()
cur.execute('select object_id from sys.objects')
results = cur.fetchall()
results_as_list = [i[0] for i in results]
array = numpy.fromiter(results_as_list, dtype=numpy.int32)
print array

rawDescription: Best approach to query SQL server for numpy


rawDescription: Print list in table format in python

description: Print list in table format in python


code:
regex = re.compile(r"Date of death.*?([0-9][0-9]? [A-z]+ [0-9]{4})", re.DOTALL)
print regex.findall(stri)

rawDescription: Regex date match: "27 January 1756" in "Date of death\n27 January 1756" without also returning the "Date of death" part of string


code:
try:
    i = vocab.index(word)
except ValueError as e:
    print e # Optional
    i = -1

rawDescription: How to get the index in the 'in' statement in Python

description: Get the index in the 'in' statement in Python


code:
import numpy as np

n = 10
#also works for non-square A
A = np.random.randint(2, size=(n*2,n)).astype(np.int8)
#force the inclusion of some hits, to keep our algorithm on its toes
##A[0] = A[1]

def base_pack_lazy(a, base, dtype=np.uint64):
    """
    pack the last axis of an array as minimal base representation
    lazily yields packed columns of the original matrix
    """
    a = np.ascontiguousarray( np.rollaxis(a, -1))
    init = np.zeros(a.shape[1:], dtype)
    packing = int(np.dtype(dtype).itemsize * 8 / (float(base) / 2))
    for columns in np.array_split(a, (len(a)-1)//packing+1):
        yield reduce(
            lambda acc,inc: acc*base+inc,
            columns,
            init)

def unique_count(a):
    """returns counts of unique elements"""
    unique, inverse = np.unique(a, return_inverse=True)
    count = np.zeros(len(unique), np.int)
    np.add.at(count, inverse, 1)        #note; this scatter operation requires numpy 1.8; use a sparse matrix otherwise!
    return unique, count, inverse

def has_identical_row_sums_lazy(A, combinations_index):
    """
    compute the existence of combinations of rows summing to the same vector,
    given an nxm matrix A and an index matrix specifying all combinations

    naively, we need to compute the sum of each row combination at least once, giving n^3 computations
    however, this isnt strictly required; we can lazily consider the columns, giving an early exit opportunity
    all nicely vectorized of course
    """

    multiplicity, combinations = combinations_index.shape
    #list of indices into combinations_index, denoting possibly interacting combinations
    active_combinations = np.arange(combinations, dtype=np.uint32)

    for packed_column in base_pack_lazy(A, base=multiplicity+1):       #loop over packed cols
        #compute rowsums only for a fixed number of columns at a time.
        #this is O(n^2) rather than O(n^3), and after considering the first column,
        #we can typically already exclude almost all rowpairs
        partial_rowsums = sum(packed_column[I[active_combinations]] for I in combinations_index)
        #find duplicates in this column
        unique, count, inverse = unique_count(partial_rowsums)
        #prune those pairs which we can exclude as having different sums, based on columns inspected thus far
        active_combinations = active_combinations[count[inverse] > 1]
        #early exit; no pairs
        if len(active_combinations)==0:
            return False
    return True

def has_identical_triple_row_sums(A):
    n = len(A)
    idx = np.array( [(i,j,k)
        for i in xrange(n)
            for j in xrange(n)
                for k in xrange(n)
                    if i<j and j<k], dtype=np.uint16)
    idx = np.ascontiguousarray( idx.T)
    return has_identical_row_sums_lazy(A, idx)

def has_identical_double_row_sums(A):
    n = len(A)
    idx = np.array(np.tril_indices(n,-1), dtype=np.int32)
    return has_identical_row_sums_lazy(A, idx)

from time import clock
t = clock()
for i in xrange(10):
    print has_identical_double_row_sums(A)
    print has_identical_triple_row_sums(A)
print clock()-t

rawDescription: Find two pairs of pairs that sum to the same value


code:
print aresult.item()
1

rawDescription: Pandas - Retrieve Value from df.loc

description: Pandas - Retrieve Value from df.loc


code:
import subprocess

p = subprocess.Popen('jsonlint -cq /home/test/notokay1.json'.split(),
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
out, err = p.communicate()

print "Standard Output:", out
print "Standard Error Output:", err
print "Return Code:", p.returncode

rawDescription: Get Shell Output with python

description: Get Shell Output with python


code:
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

rawDescription: Send email in Python like PHP

description: Send email in Python like PHP


code:
import platform
print platform.linux_distribution()

rawDescription: Pythonic way to determine OS distribution RHEL, CentOS or SUSE


code:
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import gobject
import gtk
from egg import trayicon

label = gtk.Label("Over here")

def callback(widget, ev):
    label.set_text("You found me")

def timeout():
    label.set_text("What are you waiting for?")

tray = trayicon.TrayIcon("TrayIcon")
box = gtk.EventBox()
box.add(label)
tray.add(box)
tray.show_all()

box.connect("button-press-event", callback)

gobject.timeout_add(3000L, timeout)

gtk.main()

rawDescription: Interacting with a gtk.container while gtk.main() is executing?


code:
import ctypes
class Foo(object):
    bar='baz'

foo=ctypes.py_object(Foo)
print 'Memory adress of Foo.bar object:',
print ctypes.pythonapi.PyObject_GetAttrString(foo,'bar') # prints the pointer

ctypes.pythonapi.PyObject_GetAttrString.restype = ctypes.py_object

print 'Actual Foo.bar after we set restype correctly:',
print ctypes.pythonapi.PyObject_GetAttrString(foo,'bar') # prints "baz"

rawDescription: Wrapping a function which returns a pointer to a python object with ctypes


code:
from flask import request

@app.route('/')
def index():
    print request.headers

rawDescription: Print raw HTTP request in Flask or WSGI

description: Print raw HTTP request in Flask or WSGI


code:
class Agent(object):
    def __get__(self, obj, objtype):
        print 'Agent %s called from %s ' % (id(self), obj.name)

class X(object):
    agent = Agent()

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

a = X('Foo')
a.agent

b = X('Bar')
b.agent

rawDescription: Python object hierarchy; Referencing an owner instance?


code:
from Tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.a = 1
        self.b = 2
        self.label = Label(self, text="Right-click to display menu", width=40, height=20)
        self.button = Button(self, text="Quit", command=self.destroy)
        self.label.bind("<Button-3>", self.do_popup)
        self.label.pack()
        self.button.pack()
    def do_popup(self, event):
        popup = Popup(self, self.a, self.b)
        try:
            popup.tk_popup(event.x_root, event.y_root)
        finally:
            popup.grab_release()

class Popup(Menu):
    def __init__(self, master, a, b):
        Menu.__init__(self, master, tearoff=0)
        self.a = a
        self.b = b
        self.add_command(label="Next", command=self.next)
        self.add_command(label="Previous")
        self.add_separator()
        self.add_command(label="Home")
    def next(self):
        print self.a, self.b

app = App()
app.mainloop()

rawDescription: how to pass values to popup command on right click in python tkinter

description: Pass values to popup command on right click in python tkinter


code:
import os
os.system('net view > conn.tmp')
f = open('conn.tmp', 'r')
f.readline();f.readline();f.readline()

conn = []
host = f.readline()
while host[0] == '\\':
    conn.append(host[2:host.find(' ')])
    host = f.readline()

print conn
f.close()

rawDescription: Finding Live Nodes on LAN using Python

description: Finding Live Nodes on LAN using Python


code:
sums = [0] * N

for x in range (N2):
    I = 1 #resets the variables
    Q = 0
    U = 0
    for x in range (N):
        theta = 90 + randint(-dtheta, dtheta) #random component
        #you don't need to copy these variables, they're redundant
        #Q0=Q
        #U0=U
        #I0=I

        #reuse the P-value from previous iteration
        I = (I * cosh(dTp) + S * sinh(dTp)) * exp(-dT)
        Q = (Ip * cos(2 * theta) + Q) * exp(-dT)
        U = (Ip * sin(2 * theta) + U) * exp(-dT)
        P = 100 * sqrt(Q**2 + U**2) / I
        print P

        #add the value of P to the corresponding index at x in sums[]
        sums[x] += P

#this is called a list comprehension
#it is a nicer way of looping over an iterable object (like a list)
avgs = [n / float(N2) for n in sums]

rawDescription: Python Find Average in Nested Loop

description: Python Find Average in Nested Loop


code:
str_with_ainsi_esc = '\x1b[10;10HSalut'
print re.sub('\x1b\[\d+;\d+H','',str_with_ainsi_esc)

rawDescription: Python and binary data in regexps

description: Python and binary data in regexps


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

def main():

    f = open("a",'r')
    lines = f.readlines()  # lines is actually a list
    f.close()

    print lines  # You'll see your issue, here!
    try:
        for line in lines:
            sys.stdout.write(line)  # Works now
    except:
        print "?"

if __name__ == "__main__":
    main()

rawDescription: Why doesn't sys.stdout.write work in python2?


code:
def merge_seq3(seq):
    ''' Merges a given sequence of strings
        Each string will consiste of word[index] or word[bottom index:top index]
        ex: given   ['A.p[11]','A.p[1:10]','B.p[1:2]','B.p[2]','B.p[3]','B.p[0]','A.p[0]']
            becomes ['A.p[1:11]','B.p[0:3]','A.p[0]']
    '''
    result = []
    for key, g in itertools.groupby(seq,lambda x:x.split('[')[0]):
        values = set()
        for item in g:
            split = item[len(key)+1:-1].split(':')
            if len(split) == 1:
                values.add(int(split[0]))
            else:
                values.update(xrange(int(split[0]),int(split[1])+1))

        if values:
            for k, g in groupby(enumerate(sorted(values)), lambda (i,x):i-x):
                m = map(itemgetter(1), g)
                if len(m) == 1:
                    result.append('%s[%s]'%(key,m[0]))
                else:
                    result.append('%s[%s:%s]'%(key,m[0],m[-1]))

    return result

rawDescription: Large list manipulation optimization

description: Large list manipulation optimization


code:
def mix_up(a, b):

    a, b = a.replace(a[0:2], b[0:2]) , b.replace(b[0:2], a[0:2])

    return a + " " + b

print mix_up('keegan', 'josh') #joegan kesh

rawDescription: How do I switch the first two letters of one string with the first two letters of the second string?

description: Switch the first two letters of one string with the first two letters of the second string


code:
l1 = sorted(myDict.items(),key=lambda (k,v):v[0])
l1.sort(key=lambda (k,v):v[1],reverse=True)

rawDescription: How sort a dict using the values of a list inside (two sequential criteria)


code:
lst = ['X', 'R', 'Y', 'F']
replacements = {'X': ['X', 'R', 'Y', 'F']}
new_lst = [replacements.get(i, i) for i in lst]
print new_lst     # [['X', 'R', 'Y', 'F'], 'R', 'Y', 'F']

rawDescription: Python - Best way to replace an element in a list with a key value from a dictionary


code:
X=np.zeros((100,85)) # just to get the right shape here

print X.shape
# (100, 85)

x=np.arange(X.shape[0])
y=np.arange(X.shape[1])

print x.shape
# (100,)
print y.shape
# (85,)

xx,yy=np.meshgrid(x,y,indexing='ij')

print xx.shape
#(100, 85)
print yy.shape
#(100, 85)

rawDescription: How to use meshgrid with large arrays in Matplotlib?

description: Use meshgrid with large arrays in Matplotlib


code:
import StringIO
import csv
import itertools

data = """CITY,AMOUNT,AMOUNT2,AMOUNTn
London,20,21,22
Tokyo,45,46,47
London,55,56,57
New York,25,26,27"""

# I use StringIO to create a file like object for demo purposes
f = StringIO.StringIO(data)
fieldnames = f.readline().strip().split(',')
key = lambda x: x[0] # the first column will be a grouping key
# rows must be sorted by city before passing to itertools.groupby
rows_sorted = sorted(csv.reader(f), key=key)
outfile = StringIO.StringIO('')
writer = csv.DictWriter(outfile, fieldnames=fieldnames, lineterminator='\n')
writer.writeheader()
for city, rows in itertools.groupby(rows_sorted, key=key):
    # remove city column for aggregation, convert to ints
    rows = [[int(x) for x in row[1:]] for row in rows]
    agg = [sum(column) for column in zip(*rows)]
    writer.writerow(dict(zip(fieldnames, [city] + agg)))

print outfile.getvalue()

# CITY,AMOUNT,AMOUNT2,AMOUNTn
# London,75,77,79
# New York,25,26,27
# Tokyo,45,46,47

rawDescription: Parse CSV file with and aggregate values, multiple columns


code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt

# Generate some data
x = mdates.drange(dt.datetime(2012, 01, 01), dt.datetime(2013, 01, 01),
                  dt.timedelta(weeks=2))
y = np.linspace(1, 10, 20)
data = np.random.random((y.size, x.size))

# Plot
fig = plt.figure()
ax = fig.add_subplot(111)
qmesh = ax.pcolormesh(x, y, data)
fig.colorbar(qmesh,ax=ax)
ax.axis('tight')

# Set up as dates
ax.xaxis_date()
fig.autofmt_xdate()

plt.show()

rawDescription: Dates along x-axis of quadmesh

description: Dates along x-axis of quadmesh


code:
text = 'Damien'
def my_function():
    return 'Damien'

# All of the following will print "Hello, Damien":
print 'Hello, %s' % 'Damien'
print 'Hello, %s' % text
print 'Hello, %s' % my_function()

rawDescription: Python - Turning a created string into a conversion specifier


code:
Doc = soup.find('div', {'class': 'document'}) # assuming soup is your main content
for i in [x for x in Doc.contents if x != '\n']:
    print i

rawDescription: Extract contents of the div?

description: Extract contents of the div


code:
persons={}
for row in results:
    persons[row[0]] = {
                        'personId' : row[0],
                        'personName' : row[1],
                        'personEmail' : row[2]
                 }
print json.dumps(persons)

rawDescription: Inserting data from MySql to Json array

description: Inserting data from MySql to Json array


code:
l=s.split('\n')
print '\n'.join(sorted(l,key=lambda x : int(x.split('|')[1].strip())))

rawDescription: How to sort by numbers in python?

description: Sort by numbers in python


rawDescription: Unable to put a variable in Python's print


code:
import sys
from PyQt4 import QtGui

class QCustomComboBox (QtGui.QComboBox):
    CHECK_QICON   = QtGui.QIcon.fromTheme('call-start')
    UNCHECK_QICON = QtGui.QIcon.fromTheme('call-stop')

    def __init__(self, *args, **kwargs):
        super(QCustomComboBox, self).__init__(*args, **kwargs)
        listsItem = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
        for index in range(0, len(listsItem)):
            self.addItem(listsItem[index])
            self.setItemIcon(index, self.UNCHECK_QICON)
        self.activated.connect(self.customActivated)

    def hidePopup (self):
        pass # Force only ESC button to exit.

    def customActivated (self, index):
        # self.blockSignals(True) # Force first index only
        # self.setCurrentIndex(0)
        # self.blockSignals(False)
        stateQIcon = self.itemIcon(index)
        newQIcon = {
            self.CHECK_QICON.name()   : self.UNCHECK_QICON,
            self.UNCHECK_QICON.name() : self.CHECK_QICON,
        } [stateQIcon.name()]
        self.setItemIcon(index, newQIcon)
        print self.export() # View last update data

    def export (self):
        listsExportItem = []
        for index in range(0, self.count()):
            stateQIcon = self.itemIcon(index)
            state = {
                self.CHECK_QICON.name()   : True,
                self.UNCHECK_QICON.name() : False,
            } [stateQIcon.name()]
            listsExportItem.append([str(self.itemText(index)), state])
        return listsExportItem

myQApplication = QtGui.QApplication([])
myQCustomComboBox = QCustomComboBox()
myQCustomComboBox.show()
sys.exit(myQApplication.exec_())

rawDescription: How to customize QComboBox

description: Customize QComboBox


code:
def printParam(parameter):
    def callback():
        print "You selected %s" % parameter
    return callback

for parameter in parameters:
    menu.addAction(parameter, printParam(parameter))

rawDescription: Python dynamic function generation

description: Python dynamic function generation


code:
import xmlrpclib

conn_settings = \
{
    "user" : "noob",
    "pass" : "1234",
    "host" : "localhost",
    "port" : 8080,
    "path" : ""
}

conn_str = "http://" + ("%(user)s:%(pass)s@" % conn_settings if(conn_settings.get("user", "")) else "") + "%(host)s:%(port)d%(path)s" % conn_settings
print "Connecting using: %s" % conn_str

client = xmlrpclib.ServerProxy(conn_str)

print "You can call this"
print client.system.listMethods()

print "Trying Query"
print client.ContentAPI.queryResult("1376")

rawDescription: Please help porting xmlrpc java syntax to python


code:
import praw
r = praw.Reddit(user_agent='my_cool_application')
for item in r.get_top():
    print item

rawDescription: Pull headlines from Reddit

description: Pull headlines from Reddit


code:
class ClassProperty(object):
    def __init__(self, func):
        self.func = func
    def __get__(self, inst, cls):
        return self.func(cls)

class A(object):
    model_fields = ['field1', 'field2', 'field3']

    @ClassProperty
    def fields(cls):
        return cls.model_fields + ['extra_field']

print A.fields

rawDescription: Iterable property

description: Iterable property


code:
import sqlite3

# at startup
conn = sqlite3.connect("keywords.db")

def search_location(name2):
    name3 = ' '.join(name2)
    c = conn.cursor()
    c.execute('SELECT location FROM kmedicals WHERE id = ?', (name3,))
    for (location,) in c:
        print name3.capitalize(),':' '\n',location
        break
    else:
        pass  # not found

rawDescription: How to improve sqlite database request and speed

description: Improve sqlite database request and speed


code:
from twisted.internet.defer import inlineCallbacks

@inlineCallbacks
def foo():
    x = yield bar()
    print x

rawDescription: Is it possible to use tornado's gen.engine and gen.Task with twisted?


code:
def testMsed(self):
  for i in range(self.tot_iter):
    print i
    fun = rice_output.msed(self.dsed_in[i],self.a_in[i],self.pb_in[i])
    value = self.msed_out[i]
    testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("msed",i,value,fun)
    self.assertEqual(round(fun,3),round(self.msed_out[i],3),testFailureMessage)

rawDescription: Python Unit Testing a loop function

description: Python Unit Testing a loop function


code:
import csv
cr = csv.reader(open("temp.csv","rb"))
arr = range(100) #adjust to needed
x = 0
for row in cr:
    arr[x] = row
    x += 1

print arr[:22] #adjust to needed

rawDescription: extract excel columns into python array

description: Extract excel columns into python array


code:
from BeautifulSoup import BeautifulSoup
import re

soup = BeautifulSoup(open('prueba.html').read())
item = re.sub('\s+', ' ', soup.h2.a.text)
price = soup.find('p', {'class': 'productlist_mostwanted_price'}).text
price = re.search('\d+\.\d+', price).group(0)

print item, price

rawDescription: Python BeautifulSoup parsing

description: Python BeautifulSoup parsing


code:
reservations = conn.get_all_instances(instance_ids=[sys.argv[1]])
instances = [i for r in reservations for i in r.instances]
for i in instances:
    key_name = i.key_name
    security_group = i.groups[0].id
    instance_type = i.instance_type
    print "Now Spinning New Instance"
    subnet_name = i.subnet_id
    reserve = conn.run_instances(image_id=ami_id,key_name=key_name,instance_type=instance_type,security_group_ids=[security_group],subnet_id=subnet_name)

rawDescription: How to Launch a instance in VPC using boto?

description: Launch a instance in VPC using boto


code:
from shapely.geometry import LineString

line1 = LineString([(i, i) for i in range(5)])
line2 = zip(range(5)[::-1], range(5))

if line1.crosses(line2):
    print 'yeah!'

rawDescription: Find out if a GPS waypoint has been passed


code:
counts = {}
for each in column_names:
    count = {}
    print each + ':'
    for row in columns[each]:
        count[row] = count.get(row,0) + 1
    counts[each] = count

rawDescription: Using python to find the most common value(s) in the column of CSV file


code:
if self.phone:
    if self.isp() == "east":
        self.launchbell()
    else:
        self.launchtelus()

if self.cpe_ip and self.cpe_passwd:
    print 'check modem...'
    self.modemstatus()

if self.pppoe:
    radius = sgp_radius.Radius(self.pppoe)
    print 'check radius logs...'
    self.data = radius.sgp()
    self.radius_save()

rawDescription: how to simplify boolean logic in if else statement?

description: Simplify boolean logic in if else statement


code:
numbers = [44,198,57,48,658,24,7,44,44,44.]
index_mapping = {}
indexes = []
next_index = 1
for number in numbers:
    if number in index_mapping:
        # already seen
        indexes.append(index_mapping[number])
    else:
        # a new one
        indexes.append(next_index)
        index_mapping[number] = next_index
        next_index += 1

print indexes

rawDescription: list in python, loop for, array

description: List in python, loop for, array


code:
MAXPOINTS = 10
JUNKWORDS={"trouble":6,"solution":5,"charge":3,"virus":7}
fil = open("filnamne.txt", "r")

foundwords = {}

points = 0

for word in fil.read().split():
   if word in JUNKWORDS:
       if word not in foundwords:
           foundwords[word] = 0
       points += JUNKWORDS[word]
       foundwords[word] += 1

if points > 10:
    print "SPAM"
    for word in foundwords:
        print word, foundwords[word]*JUNKWORDS[word]
else:
    print "The file doesn't contain any junk"

rawDescription: Programming concept

description: Programming concept


code:
def setter():
    print('In setter')
    gevent.sleep(3)
    print("After first sleep")
    evt.set()     #first set
    ### now clear evt
    evt.clear()
    ###
    print 'second sleep'
    gevent.sleep(3)
    evt.set()     #second set
    print 'end of setter'

rawDescription: python gevent.event.Event

description: Python gevent.event.Event


code:
from itertools import product

def multi_replace(s, d):
    indexes, replacements = zip(*[(i, d[c]) for i, c in enumerate(s) if c in d])
    # indexes: (1, 3)
    # replacements: (['A', 'G'], ['C', 'T'])

    l = list(s)  # turn s into sth. mutable
    # iterate over cartesian product of all replacement tuples ...
    for p in product(*replacements):
        for index, replacement in zip(indexes, p):
            l[index] = replacement
        yield ''.join(l)

d = {'R': ['A', 'G'], 'Y': ['C', 'T']}
s = 'ARCY'

for perm in multi_replace(s, d):
    print perm

AACC
AACT
AGCC
AGCT

s = 'RRY'

AAC
AAT
AGC
AGT
GAC
GAT
GGC
GGT

rawDescription: Permutations using a multidict

description: Permutations using a multidict


code:
import tarfile
import StringIO
import time

tar = tarfile.TarFile("test.tar", "w")

string = StringIO.StringIO()
string.write("hello")
string.seek(0)

info = tarfile.TarInfo(name='dir')
info.type = tarfile.DIRTYPE
info.mode = 0755
info.mtime = time.time()
tar.addfile(tarinfo=info)

info = tarfile.TarInfo(name='dir/foo')
info.size=len(string.buf)
info.mtime = time.time()
tar.addfile(tarinfo=info, fileobj=string)

tar.close()

rawDescription: How can I programmatically create a tar archive of nested directories and files solely from Python strings and without temporary files?

description: Programmatically create a tar archive of nested directories and files solely from Python strings and without temporary files


code:
class Example:
    Variable = 2           # static variable

print Example.Variable     # prints 2   (static variable)

# Access through an instance
instance = Example()
print instance.Variable    # still 2  (ordinary variable)

# Change within an instance
instance.Variable = 3      #(ordinary variable)
print instance.Variable    # 3   (ordinary variable)
print Example.Variable     # 2   (static variable)

# Change through Class
Example.Variable = 5       #(static variable)
print instance.Variable    # 3  (ordinary variable)
print Example.Variable     # 5  (static variable)

rawDescription: How to declare a static attribute in Python?

description: Declare a static attribute in Python


code:
def formater():
    mydict=defaultdict(dict)
    mylist=[]
    with open('dict-values.txt','r') as f:
        for line in f:
            mylist.append(line.strip())

        for item in mylist:
            key,value =item.split(':')
            mydict[key]=value
    return mydict

def parseText1(text):
    text_patterns=formater()
    for key,value in text_patterns.iteritems():
        pattern=re.compile(key)
        if pattern.findall(text):
            text=re.sub(key,value,text)
            text=re.sub('='+'\s','=',text)
    return text

result = defaultdict(dict)
item = count()
list_out=[]
with open(os.path.abspath('catalog/commands1.txt'),'r') as fread:
    for line in fread:
        if not line:
            continue
        parts=parseText1(line)
        list_out.append(parts.rstrip())
        #list_out.append(parts.split())

print "AFTER - " + str(list_out)

rawDescription: Read a flat text file and replace specific list of patterns using python


code:
# Tail
from __future__ import with_statement

find_str = "FIREFOX"                    # String to find
fname = "g:/autoIt/ActiveWin.log_2"     # File to check

with open(fname, "r") as f:
    f.seek (0, 2)           # Seek @ EOF
    fsize = f.tell()        # Get Size
    f.seek (max (fsize-1024, 0), 0) # Set pos @ last n chars
    lines = f.readlines()       # Read to end

lines = lines[-10:]    # Get last 10 lines

# This returns True if any line is exactly find_str + "\n"
print find_str + "\n" in lines

# If you're searching for a substring
for line in lines:
    if find_str in line:
        print True
        break

rawDescription: Most efficient way to search the last x lines of a file in python


code:
import socket

class Client(object):

    def __init__(self):
        self.buffer = ''
        self.sock = None

    def connect(self,address):
        self.buffer = ''
        self.sock = socket.socket()
        self.sock.connect(address)

    def get_msg(self):
        '''Append raw data to buffer until sentinel is found,
           then strip off the message, leaving the remainder
           in the buffer.
        '''
        while not '\n' in self.buffer:
            data = self.sock.recv(4096)
            if not data:
                return ''
            self.buffer += data
        sentinel = self.buffer.index('\n') + 1
        msg,self.buffer = self.buffer[:sentinel],self.buffer[sentinel:]
        return msg

    def close(self):
        self.sock.close()

if __name__ == '__main__':
    c = Client()
    c.connect((HOST,PORT))
    while True:
        msg = c.get_msg()
        if not msg:
            break
        print repr(msg)
    c.close()

rawDescription: Multiple writes get handled as single one


code:
from Tkinter import*
class Myclassx:
     def __init__(self):
        windx = Tk()
        btnx = Button(text="okayx",command =self.printx)
        btnx.pack()
        windx.mainloop() # <----
     def printx(self):
        print "see you later"

oleyx=Myclassx()

rawDescription: Can't show tk window

description: Can't show tk window


rawDescription: How can i make a set in my list in python?

description: Make a set in my list in python


code:
def get_first_num(text):
    words = text.split()

    for word in words:
        if word.isdigit():
            return word

print get_first_num('This string contains numbers 456 but also 5 and 34')

rawDescription: Check my string for numbers. Python

description: Check my string for numbers. Python


code:
def collector(fileIn):
    d = {}
    with open(fileIn, "r") as f:
        for line in f:
            clu, gen, spec, fam = line.split()
            d.setdefault(gen, []).append((spec, fam))
    return d

def compare_files(f1, f2):
    d1 = collector(f1)
    d2 = collector(f2)
    for genus in d1:
        try:
            if len(d1[genus]) != len(d2[genus]):
                print genus, "is different"
        except:
            print genus, "not found in file 2"

rawDescription: How to compare clusters 2.0

description: Compare clusters 2.0


code:
with open('myfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader) # just ignore the result
    for row in reader:
        print row

rawDescription: Printing out csv rows apart from first row


code:
# Function to be tested
def callSleep(timer):
    timer.sleep(5)

# Example usage
def main():
    import time
    timer = time

    callSleep(timer)

# Example test
def testFunction():

    class MockTimer:
        numCalled = 0
        withValue = 0
        def sleep(self, val):
            self.numCalled += 1
            self.withValue = val

    mockTimer = MockTimer()

    callSleep(mockTimer)

    print "Num called:", mockTimer.numCalled, "with value", mockTimer.withValue

rawDescription: Renaming a builtin function for testing purposes


code:
import pycurl
import StringIO
b = StringIO.StringIO()
conn = pycurl.Curl()
conn.setopt(pycurl.URL, 'http://www.example.org')
conn.setopt(pycurl.WRITEFUNCTION, b.write)
conn.perform()
print b.getvalue()

rawDescription: pycurl: RETURNTRANSFER option doesn't exist

description: Pycurl: RETURNTRANSFER option doesn't exist


code:
numbers = 1,2
print ",".join("'{0}'".format(n) for n in numbers)

rawDescription: Python Join a list of integers

description: Python Join a list of integers


code:
class ObjectWithEvents(object):
    callbacks = None

    def on(self, event_name, callback):
        if self.callbacks is None:
            self.callbacks = {}

        if event_name not in self.callbacks:
            self.callbacks[event_name] = [callback]
        else:
            self.callbacks[event_name].append(callback)

    def trigger(self, event_name):
        if self.callbacks is not None and event_name in self.callbacks:
            for callback in self.callbacks[event_name]:
                callback(self)

class MyClass(ObjectWithEvents):
    def __init__(self, contents):
        self.contents = contents

    def __str__(self):
        return "MyClass containing " + repr(self.contents)

def echo(value): # because "print" isn't a function...
    print value

o = MyClass("hello world")
o.on("example_event", echo)
o.on("example_event", echo)
o.trigger("example_event") # prints "MyClass containing \"Hello World\"" twice

rawDescription: Does Python classes support events like other languages?


code:
import urllib

# outputs "foo%2Fbar"
print urllib.quote('foo/bar', '')

rawDescription: Can I write a file name containing path separators in Linux?

description: Write a file name containing path separators in Linux


code:
def get_new_dict(my_list):
    rv = {}
    key = None
    vals = []
    for item in my_list:
        if item.startswith('#'):
            if key is not None:
                rv[key] = vals
                vals = []
            key = item
        else:
            vals.append(item)
    rv[key] = vals
    return rv

list1 = ['#model1','path1','path2','path4','path5','#model2','path12',
         'path13','#model3','path4','path6']

print get_new_dict(list1)

rawDescription: Python list to dictionary in python, with selected list


code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

while True:
    conn, addr = s.accept()

    print 'Connection address:', addr

    while True:
        data = conn.recv(BUFFER_SIZE)
        if not data: break
        print "received data:", data
        conn.send(data)

    conn.close()

rawDescription: How do I make my TCP server run forever?

description: Make my TCP server run forever


code:
import pygame, sys
import time
from pygame.locals import *

import math

### some simple vector helper functions, stolen from http://stackoverflow.com/a/4114962/142637
def magnitude(v):
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
    return [ u[i]+v[i] for i in range(len(u)) ]

def sub(u, v):
    return [ u[i]-v[i] for i in range(len(u)) ]

def dot(u, v):
    return sum(u[i]*v[i] for i in range(len(u)))

def normalize(v):
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]

pygame.init()
fpsClock=pygame.time.Clock()
screen= pygame.display.set_mode((640,480))
pygame.display.set_caption('Test')
fontobj= pygame.font.SysFont('Arial',50)
mousex,mousey=0,0
x_1=15
x_2=600 #these varaibles (x_1, x_2) are different, but they are constants-- they will never change; think jon, the paddle will not move from left to right
y=0 #the y variable changes, but for this test it will be the same for both paddles bc they are moving in unisen.
x_ball=320
y_ball=240
direction=""

### the speed of the ball and the paddles
speed = 5

### a cache for font objects
cache={}
def get_msg(msg):
    if not msg in cache:
      cache[msg] = fontobj.render(msg, False , pygame.Color('green'))
    return cache[msg]

def draw_stuff (y):
        msg=str(x_ball)
        global x_ball,y_ball,direction

        ### get the font surface from the cache
        textobj=get_msg(msg)
        screen.blit(textobj,(160,5))
        screen.blit(textobj,(480,5))
        pygame.draw.line(screen,pygame.Color('grey'),(320,0), (320,480), 4)
        pygame.draw.line(screen,pygame.Color('grey'),(0,3), (640,3), 10)
        pygame.draw.line(screen,pygame.Color('grey'),(0,475), (640,475), 10)
        pygame.draw.rect(screen, pygame.Color('grey'),(x_1,y,30,192))
        pygame.draw.rect(screen, pygame.Color('grey'),(x_2,y,30,192))
        if x_ball==60 or x_ball==570:
            print "we have reached the side",fpsClock.get_fps()
            if ball_hit(y,x_ball,y_ball):
                topl,middlel,bottoml=loc_of_ball_hitl(y,x_ball,y_ball)
                topr,middler,bottomr=loc_of_ball_hitr(y,x_ball,y_ball)
                if topl:
                    direction="upleft"
                elif middlel:
                    direction='midleft'
                elif bottoml:
                    direction='downleft'
                elif topr:
                    direction="upright"
                elif middler:
                    direction="midright"
                elif bottomr:
                    direction="downright"
                else:
                    direction=""

        ### create a vector
        move = (0, 0)
        if not direction:
            print "we have ",fpsClock.get_fps()
            move = (1, 0)
        elif direction=="upleft":
            move = (1, -1)
        elif direction=="midleft":
            move = (1, 0)
        elif direction=="downleft":
            move = (1, 1)
        elif direction=="upright":
            move = (-1, -1)
        elif direction=="midright":
            move = (-1, 0)
        elif direction=="downright":
            move = (-1, 1)
        ### normalize it and apply the speed
        move = [int(c * speed) for c in normalize(move)]
        ### update ball position with movement vector
        x_ball, y_ball = x_ball + move[0], y_ball + move[1]
        ball(x_ball, y_ball)

def ball(x,y):
    pygame.draw.circle(screen, pygame.Color('red'), (x,y), 15, 0)
    pygame.display.update()
def ball_hit(y,ball_x,ball_y):
    if ball_x==60 and ball_y>=y and ball_y<y+192 or ball_x==570 and ball_y>=y and ball_y<y+192:
        return True
    return False
def loc_of_ball_hitl(y,ball_x,ball_y):
   middle=False
   top=False
   bottom=False
   if ball_x==60 and ball_y>=y+64 and ball_y<y+128:
        middle=True
   elif ball_x==60 and ball_y>=y and ball_y<y+64:
        top=True
   elif ball_x==60 and ball_y>=y+128 and ball_y<y+192:
        bottom=True
   return top, middle, bottom
def loc_of_ball_hitr(y,ball_x,ball_y):
   middle=False
   top=False
   bottom=False
   if ball_x==570 and ball_y>=y+64 and ball_y<y+128:
        middle=True
   elif ball_x==570 and ball_y>=y and ball_y<y+64:
        top=True
   elif ball_x==570 and ball_y>=y+128 and ball_y<y+192:
        bottom=True
   return top, middle, bottom
while True:
    screen.fill(pygame.Color('black'))
    if mousey>y:
        y+=speed
    if mousey<y:
        y-=speed

    ### call draw_stuff only once
    draw_stuff(y)
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
        elif event.type== MOUSEMOTION:
            mousex,mousey=event.pos
    pygame.display.update()
    ### realistic framerate
    fpsClock.tick(60)

rawDescription: Why does this pygame code lag?

description: Why does this pygame code lag


code:
print '{:2}'.format(1)
print '{:2}'.format(23)

rawDescription: Printing with exactly 1 space if a single digit?


code:
import tweepy, datetime, time

def get_tweets(api, username):
    page = 1
    deadend = False
    while True:
        tweets = api.user_timeline(username, page = page)

        for tweet in tweets:
            if (datetime.datetime.now() - tweet.created_at).days < 1:
                #Do processing here:

                print tweet.text.encode("utf-8")
            else:
                deadend = True
                return
        if not deadend:
            page+=1
            time.sleep(500)

get_tweets(api, "anmoluppal366")

rawDescription: Getting tweets by date with tweepy

description: Getting tweets by date with tweepy


code:
with open('somefile') as fileobj:
    lines = list(fileobj)

for index in indices:
    print lines[index]

rawDescription: list of numbers correspond to a line in a file

description: List of numbers correspond to a line in a file


code:
import imaplib
im = imaplib.IMAP4(hostname)
im.login(user, password)
im.select('INBOX')

# you can use im.search() to obtain message ids
msg_ids = '1, 4, 7'
labels = ['foo', 'bar', 'baz']

# add the flags to the message
im.store(msg_ids, '+FLAGS', '(%s)' % ' '.join(labels))

# fetch and print to verify the flags
print im.fetch(ids, '(FLAGS)')

im.close()
im.logout()

rawDescription: Custom Tagging messages in IMAP through python


code:
import pygtk
pygtk.require('2.0')
import gtk
import glib

def timer_cb():
    print "5 seconds elapsed."
    return False

def show_cb(widget, data=None):
    glib.timeout_add(5000, timer_cb)

def destroy_cb(widget, data=None):
    gtk.main_quit()

def main():
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)

    window.connect("show", show_cb)

    window.connect("destroy", destroy_cb)

    window.show()

    gtk.main()

if __name__ == "__main__":
    main()

rawDescription: Running function 5 seconds after pygtk widget is shown


code:
import shlex
import subprocess

# Tokenize the shell command
# cmd will contain  ["ping","-c1","google.com"]
cmd=shlex.split("ping -c1 google.com")
try:
   output = subprocess.check_output(cmd)
except subprocess.CalledProcessError,e:
   #Will print the command failed with its exit status
   print "The IP {0} is NotReacahble".format(cmd[-1])
else:
   print "The IP {0} is Reachable".format(cmd[-1])

rawDescription: Ping to a specific IP address using python


code:
from itertools import permutations

def code():
    valid_list = []
    for line,lists in enumerate(permutations(range(4))):
        flag_valid = True
        for index,elements in enumerate(lists):
            if index != len(lists) - 1:  #list index out of range escape condition
                if abs(lists[index] - lists[index + 1]) == 1:
                    flag_valid = False
                    break;
        if flag_valid == True:
                valid_list.append(lists)
    return valid_list

print code()

rawDescription: How do I check elements of the list here

description: Check elements of the list here


code:
import os
f= r"D:\Reg"
folder_name= os.path.basename(f)
def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size
print "{0:<20}      {1:>0}".format("Folder"," Size")
print "{0:<20}        {1:>0}".format(folder_name,str(get_size(f))+" bytes")

rawDescription: Calculating size folder python

description: Calculating size folder python


code:
import serial
from time import sleep

conn = serial.Serial('/dev/ttyS0',
                     baudrate=9600,
                     bytesize=serial.EIGHTBITS,
                     parity=serial.PARITY_NONE,
                     stopbits=serial.STOPBITS_ONE,
                     timeout=1,
                     xonxoff=0,
                     rtscts=0
                     )
# Wake Modem
conn.setDTR(True)
sleep(3)
conn.setDTR(False)

sleep(5)

# Start talking
conn.setDTR(True)
try:
    while True:
        conn.write('AT'+chr(13));
        print conn.readline() # readlines() will probably never return.
finally:
    conn.close()

rawDescription: Serial communication. Sending DTR in the right way?


code:
import numpy as np
from itertools import izip
from grouping import group_by

users   = 100
months  = 20
binsize = 40
bins    = 500/binsize+1

counts = np.random.randint(0,500, size=(users, months))
events = np.random.randint(0,2,   size=(users, months))

binned = np.zeros((bins, months), np.float)
for b, c, e in izip(binned.T, counts.T, events.T):
    i, s = group_by(c / binsize).mean(e.astype(np.float))
    b[i] = s
print binned

rawDescription: Tricky sparse matrix manipulation + a surface plot


code:
def get_filenames():
    filelist = []
    prompts = [
        "Please input the full name (e.g. text_file.txt) or path of a text file:",
        "Please input any additional text files or simply press enter to continue:"
    ]
    while True:
        filename = raw_input(prompts[len(filelist) > 0]).strip()
        if not filename:
            break
        filelist.append(filename)
    return filelist

def get_filelines(filelist):
    files = {}
    for filename in filelist:
        with open(filename, 'rb') as fp:
            files[filename] = fp.readlines()
    return files

if __name__=='__main__':
    print get_filelines(get_filenames())

rawDescription: Python: dynamic list names

description: Python: dynamic list names


code:
from bs4 import BeautifulSoup

example = '''

      Title here

      Title 2 here
'''

soup = BeautifulSoup(example)
for div in soup.findAll('div', {'class': 'foo'}):
    a = div.findAll('a')[1]
    print a.text.strip(), '=>', a.attrs['href']

rawDescription: beautifulsoup - extracting link within a div

description: Beautifulsoup - extracting link within a div


code:
# construct a python dictionary to serialize to JSON later
item = { "name": "Marcus0.1", "start": 500000, "end": 1361640526000 }

resp = requests.post("https://this.is.an.external.domain",
              data=json.dumps(item),  # serialize the dictionary from above into json
              headers={
                       "Content-Type":"application/json",
                       "Accept": "application/json"
                      })

print resp.status_code
print resp.content

rawDescription: Django POST curl REST api

description: Django POST curl REST api


code:
class Hashable(object):
    def __hash__(self):
        print '__hash__ was called'
        return super(Hashable, self).__hash__()

{Hashable(): None}

rawDescription: __hash__ method and documentation. Probable error?


code:
import json
import sys
import string
from time import strftime

scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
dict2 = {} #Create one output dict
for line in scan_results:
    if ".jpg" in line:
        lst = []
        result = line.split('\\')
        result_split = result[5].split(' ')
        filename = result_split[0]
        raw_status = result_split[3]
        if "OK" in raw_status:
            status = "Okay"
            status_code = "0"
        dict2[filename] = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status} #Add to that dict.
print json.dumps(dict2) #Print it out at the end.
sys.stdout = saved
f.close()
print "JSON report written"

rawDescription: Writing dicitonary to JSON using Python

description: Writing dicitonary to JSON using Python


code:
import httplib2, urllib, json, pprint, getpass, string, time, MySQLdb
import sys
def usage():
    print "Usage: python26 mysql.py or ./mysql.py"
    sys.exit(1)

if len(sys.argv) != 1:
    usage()

# Connect to the database and create the tables
conn = MySQLdb.connect (host = "localhost",
                   user = "XXXXXXXXXX",
                   passwd = "XXXXXXXX")
cursor = conn.cursor ()
cursor.execute ("DROP DATABASE IF EXISTS tenable")
cursor.execute ("CREATE DATABASE tenable")
cursor.execute ("USE tenable")
cursor.execute ("""
CREATE TABLE cumvulndata
(
  offset         BIGINT(10),
  pluginName     TEXT,
  repositoryID   SMALLINT(3),
  severity       TINYINT(2),
  pluginID       MEDIUMINT(8),
  hasBeenMitigated   TINYINT(1),
  dnsName        VARCHAR(255),
  macAddress     VARCHAR(40),
  familyID       INT(4),
  recastRisk     TINYINT(1),
  firstSeen      DATETIME,
  ip             VARCHAR(15),
  acceptRisk     TINYINT(1),
  lastSeen       DATETIME,
  netbiosName    VARCHAR(255),
  port           MEDIUMINT(5),
  pluginText     MEDIUMTEXT,
  protocol       TINYINT(3)
  )
  """)
cursor.execute ("""INSERT INTO cumvulndata   (offset,pluginName,repositoryID,severity,pluginID,hasBeenMitigated,dnsName,macAddress,familyID,recastRisk,firstSeen,ip,acceptRisk,lastSeen,netbiosName,port,pluginText,protocol)
    VALUES
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",   ('123','plugin','10','1','12','1',"dnsName","macAddress",'15','1','2011-2-2',"ip",'9','2012-5-2',"netbiosName",'123',"pluginText","2"))

#Commit the changes.
conn.commit()
cursor.close()
conn.close()

rawDescription: Python MySQL data import

description: Python MySQL data import


code:
foo = ['MAR', 'TFFVGGNFK', 'LNGSK', 'QSIK', 'EIVER', 'LNTASIPENVEVVICPPATYLDYSVSLVK']

count = 0
for bar in foo:
    newcount = count + len(bar)
    print count, '-', newcount-1, bar
    count = newcount

rawDescription: How to know the first and last position on a string that is already on a list in python?

description: Know the first and last position on a string that is already on a list in python