code:
import time
import boto
import boto.ec2.networkinterface

from settings.settings import AWS_ACCESS_GENERIC

ec2 = boto.connect_ec2(*AWS_ACCESS_GENERIC)

interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(subnet_id='subnet-11d02d71',
                                                                    groups=['sg-0365c56d'],
                                                                    associate_public_ip_address=True)
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)

reservation = ec2.run_instances(image_id='ami-a1074dc8',
                                instance_type='t1.micro',
                                #the following two arguments are provided in the network_interface
                                #instead at the global level !!
                                #'security_group_ids': ['sg-0365c56d'],
                                #'subnet_id': 'subnet-11d02d71',
                                network_interfaces=interfaces,
                                key_name='keyPairName')

instance = reservation.instances[0]
instance.update()
while instance.state == "pending":
    print instance, instance.state
    time.sleep(5)
    instance.update()

instance.add_tag("Name", "some name")

print "done", instance

rawDescription: How to auto assign public ip to EC2 instance with boto

description: Auto assign public ip to EC2 instance with boto


code:
str = '<data-default-alt="Ford Truck">

$1,000.00
'

import re

brand=re.search('<data-default-alt=\"(.*?)">',str)
cost=re.search('\$(\d+,?\d*\.\d+)
',str)
if brand:
        print brand.group(1)
if cost:
        print cost.group(1)

rawDescription: python text parsing to get filtered output

description: Python text parsing to get filtered output


code:
response = requests.post('https://bitonic.nl/json/', data={'part': 'rate_convert', 'check': 'euros', 'euros': 46})
print response.json()

rawDescription: How to submit a javascript-form using Python requests library?

description: Submit a javascript-form using Python requests library


code:
import os

id = raw_input('please enter the file numbers id:')
file='AY{0}1.txt'.format(id)

if not os.path.exists(file):
    print "Error the file doesn't exists"
else:
    g,a,c,t=0,0,0,0
    with open(file,'r') as f:
        next(f)
        for line in f:
            for char in line.lower():
                if char == 'g':
                    g+=1
                if char == 'a':
                    a+=1
                if char == 'c':
                    c+=1
                if char == 't':
                    t+=1

    print "Guanina: {0}".format(g)
    print "Adenina: {0}".format(a)
    print "Citosina: {0}".format(c)
    print "Timina: {0}".format(t)

    gc = (g+c+0.) / (a+t+c+g+0.)

    print "Conteúdo GC: {0}".format(gc)

rawDescription: How to ask user's input to make a code interactive

description: Ask user's input to make a code interactive


code:
import random
from tornado import ioloop, gen
import toro

class C(object):
    def __init__(self):
        self.queue = toro.JoinableQueue()

    @gen.coroutine
    def start_jobs(self):
        while True:
            job_id = yield self.queue.get()
            self.job(job_id)

    @gen.coroutine
    def job(self, job_id):
        print 'job_id', job_id
        yield gen.sleep(random.random())
        print 'job_id', job_id, 'done'
        self.queue.task_done()

c = C()
for i in range(5):
    c.queue.put_nowait(i)

c.start_jobs()

io_loop = ioloop.IOLoop.instance()

# block until all tasks are done
c.queue.join().add_done_callback(lambda future: io_loop.stop())
io_loop.start()

rawDescription: Tornado asynchronous job in tornado gen coroutine


code:
deg_list =[]
deg_list_a=[]
deg_list_b=[]
deg_list_c=[]
degree=input("Enter the students Degree:")
while degree > 0:
    deg_list.append(degree);
    degree=input("Enter the students Degree:")
print len(deg_list)
while len(deg_list) > 0:        # Strictly greater than 0, not equal to 0.
    if deg_list[0] >= 16:
        # Use pop to access first element
        deg_list_a.append(deg_list.pop(0))
    elif deg_list[0] >= 10:    # One comparison per statement, only.
        deg_list_b.append(deg_list.pop(0))
    else:
        deg_list_c.append(deg_list.pop(0))  # c, not b.
print deg_list_a
print deg_list_b
print deg_list_c

rawDescription: why my code stop in secound while loop?

description: Why my code stop in secound while loop


code:
import requests
import urllib

BASE_URL = 'http://www.phillyhistory.org/PhotoArchive/'
QUERY_URL = BASE_URL + 'Thumbnails.ashx'
DETAILS_URL = BASE_URL + 'Details.ashx'

def get_media_url(asset_id):
    response = requests.post(DETAILS_URL, data={'assetId': asset_id})
    image_details = response.json()
    media_id = image_details['assets'][0]['medialist'][0]['mediaId']
    return '{}/MediaStream.ashx?mediaId={}'.format(BASE_URL, media_id)

def save_image(asset_id):
    filename = '{}.jpg'.format(asset_id)
    url = get_media_url(asset_id)

    with open(filename, 'wb') as f:
        response = requests.get(url)
        f.write(response.content)
    return filename

urlqs = {
    'maxx': '-8321310.550067',
    'maxy': '4912533.794965',
    'minx': '-8413034.983992',
    'miny': '4805521.955385',
    'onlyWithoutLoc': 'false',
    'sortOrderM': 'DISTANCE',
    'sortOrderP': 'DISTANCE',
    'type': 'area',
    'updateDays': '0',
    'withoutLoc': 'false',
    'withoutMedia': 'false'
}

data = {
    'start': 0,
    'limit': 12,
    'noStore': 'false',
    'request': 'Images',
    'urlqs': urllib.urlencode(urlqs)
}

response = requests.post(QUERY_URL, data=data)
result = response.json()

print '{} images found'.format(result['totalImages'])

for image in result['images']:
    asset_id = image['assetId']
    print 'Name: {}'.format(image['name'])
    print 'Asset ID: {}'.format(asset_id)

    filename = save_image(asset_id)
    print "Saved image to '{}'.\n".format(filename)

rawDescription: using xpath to parse images from the web

description: Using xpath to parse images from the web


code:
import itertools,re

my_string =  "n76a+q80a+l83a+i153a+l203f+r207a+s211a+s215w+f216a+e283l"
#first extract the numbers
my_numbers = map(int,re.findall("[0-9]+",my_string))
#split the string on + (useless comment)
parts = my_string.split("+")

def get_filler((a,b)):
    '''this method decides on the joiner'''
    return "_" if abs(a-b) < 10 else '+'

fillers = map(get_filler,zip(my_numbers,my_numbers[1:])) #figure out what fillers we need
print "".join(itertools.chain.from_iterable(zip(parts,fillers)))+parts[-1] #it will always skip the last part so gotta add it

rawDescription: Python find and replace upon condition / with a function


code:
count = 0
def counter(x,y):
    global count
    count = count + 1
    return cmp(x,y)

sorted(mylist, cmp=counter)

print count

rawDescription: How to find count of comparisons in method sort()

description: Find count of comparisons in method sort()


code:
from PyQt4 import QtCore

class Hello(QtCore.QObject):

    def __init__(self, msg):
        super(Hello, self).__init__()
        self.msg = msg
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.say)
        self.timer.start(500)
        self.i = 0

    def say(self):
        print self.msg
        self.i += 1
        if self.i > 5:
            QtCore.QCoreApplication.instance().quit()

if __name__ == "__main__":
    import sys
    app = QtCore.QCoreApplication(sys.argv)
    hello = Hello("Hello World!")
    sys.exit(app.exec_())

rawDescription: Using PyQT for a command prompt (Python) program


code:
import csv
with open("myfile", "rb") as infile:
    reader = csv.reader(infile, delimiter=",", skipinitialspaces=True)
    for line in reader:
       print line

rawDescription: python readline by '\r\n'

description: Python readline by '\r\n'


code:
Swear = ["curse", "curse", "curse"]

for i in Swear:
    if i in Userinput:
        print 'Quit Cursing!'

rawDescription: Python - If string contains a word from a list or set


code:
class Data:
    def __init__(self, data_file):
        self.f = data_file
        self.read()
        self.data()

    def read(self):
        lines = open(self.f).readlines()
        lines = [_.strip() for _ in lines]
        self.lines = lines

    def data(self, comment='#', sep=None):
        data = []
        for line in self.lines:
            if not line.startswith(comment):
                line = line.split(sep)
                data.append(line)
        self.data = data

    def select_data(self, tstart, tstop, tcol, dcol):
        d0 = []
        for m in self.data:
            if float(m[tcol]) >= tstart and float(m[tcol]) <= tstop:
                d0.append(m[dcol])
        return d0

d1 = Data('f1.txt')
d2 = Data('f2.txt')
d1s = d1.select_data(11, 20, 0, 1) # 1 for col 2
d2s = d2.select_data(11, 20, 0, 3) # 3 for col 4

if len(d1s) <= len(d2s):
    n0 = len(d1s)
else:
    n0 = len(d2s)

diff = []
for i in xrange(n0):
    diff.append(abs(float(d1s[i]) - float(d2s[i])) )

sum = 0
for m in diff:
    sum += m

print sum

rawDescription: Distance between two curves

description: Distance between two curves


code:
for link in link_list:
    u, v = link
    print u, v

rawDescription: How to split tuple with parentheses in Python?

description: Split tuple with parentheses in Python


code:
seconds = 26264
hours, seconds =  seconds // 3600, seconds % 3600
minutes, seconds = seconds // 60, seconds % 60
print hours, minutes, seconds

rawDescription: How to convert seconds into hh:mm:ss in python 3.x

description: Convert seconds into hh:mm:ss in python 3.x


code:
import os
directory = os.path.join("c:\\","path")
for root,dirs,files in os.walk(directory):
    for file in files:
       if file.endswith(".log") or file.endswith(".txt"):
           f=open(file, 'r')
           for line in f:
              if userstring in line:
                 print "file: " + os.path.join(root,file)
                 break
           f.close()

rawDescription: How to open all .txt and .log files in the current directory, search, and print the file the search was found

description: Open all .txt and .log files in the current directory, search, and print the file the search was found


code:
#####################
from Tkinter import *
#####################
string = ''

time_calc = Tk()
time_calc.geometry('500x400')
time_calc.title("Calculate A Time")
time_calc_frame= Frame(time_calc).grid(row=0, column=0)

jul_box = Entry(time_calc)
jul_box.insert(0, "Julian Date")
jul_box.pack(side = TOP)
jul_box.bind('<Return>')

def jd2gd(jd):
  global string
  jd=jd+0.5
  Z=int(jd)
  F=jd-Z
  alpha=int((Z-1867216.25)/36524.25)
  A=Z + 1 + alpha - int(alpha/4)

  B = A + 1524
  C = int( (B-122.1)/365.25)
  D = int( 365.25*C )
  E = int( (B-D)/30.6001 )

  dd = B - D - int(30.6001*E) + F

  if E<13.5:
            mm=E-1

  if E>13.5:
            mm=E-13

  if mm>2.5:
            yyyy=C-4716

  if mm<2.5:
            yyyy=C-4715

  months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  daylist=[31,28,31,30,31,30,31,31,30,31,30,31]
  daylist2=[31,29,31,30,31,30,31,31,30,31,30,31]

  h=int((dd-int(dd))*24)
  min =int((((dd-int(dd))*24)-h)*60)
  sec=86400*(dd-int(dd))-h*3600-min*60

  # Now calculate the fractional year. Do we have a leap year?
  if (yyyy%4 != 0):
            days=daylist2
  elif (yyyy%400 == 0):
            days=daylist2
  elif (yyyy%100 == 0):
            days=daylist
  else:
            days=daylist2

  hh = 24.0*(dd % 1.0)
  min = 60.0*(hh % 1.0)
  sec = 60.0*(min % 1.0)

  dd =  dd-(dd%1.0)
  hh =  hh-(hh%1.0)
  min =  min-(min%1.0)

  if hh < 10:
    hh = '0%s'%str(int(hh))
  else:
      hh = int(hh)
  if min < 10:
    min = '0%s'%str(int(min))
  else:
      min = int(min)
  if sec < 10:
    sec = '0%s'%str(int(sec))
  else:
      sec = int(sec)

  string = "%s/%s/%s  %s:%s:%s UTC" % (str(int(mm)), str(int(dd)), str(yyyy), str(hh),               str(min), str(sec))
  print string
  ##############
  return string
  ##############

def j2ghandler():
  jul = float(jul_box.get())

  #####################
  var.set(jd2gd(jul))
  #####################

j2g_button = Button(time_calc, text='Convert Julian\n to Gregorian', command = j2ghandler, width= 25, pady=5).pack(side=TOP)

#Draw Time String on Canvas
var = StringVar()
var.set(string)
label = Label(time_calc, textvariable=var)
label.pack(side=TOP)

##############################
time_calc.mainloop()
##############################

rawDescription: Labels Aren't Changing

description: Labels Aren't Changing


code:
from gevent import monkey
monkey.patch_socket()
import logging

import gevent
from gevent.queue import Queue
import pymysql as db

logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger("connection_pool")

class ConnectionPool:
    def __init__(self, db_config, time_to_sleep=30, test_run=False):
        self.username = db_config.get('user')
        self.password = db_config.get('password')
        self.host = db_config.get('host')
        self.port = int(db_config.get('port'))
        self.max_pool_size = 20
        self.test_run = test_run
        self.pool = None
        self.time_to_sleep = time_to_sleep
        self._initialize_pool()

    def get_initialized_connection_pool(self):
        return self.pool

    def _initialize_pool(self):
        self.pool = Queue(maxsize=self.max_pool_size)
        current_pool_size = self.pool.qsize()
        if current_pool_size < self.max_pool_size:  # this is a redundant check, can be removed
            for _ in xrange(0, self.max_pool_size - current_pool_size):
                try:
                    conn = db.connect(host=self.host,
                                      user=self.username,
                                      passwd=self.password,
                                      port=self.port)
                    self.pool.put_nowait(conn)

                except db.OperationalError, e:
                    LOGGER.error("Cannot initialize connection pool - retrying in {} seconds".format(self.time_to_sleep))
                    LOGGER.exception(e)
                    break
        self._check_for_connection_loss()

    def _re_initialize_pool(self):
        gevent.sleep(self.time_to_sleep)
        self._initialize_pool()

    def _check_for_connection_loss(self):
        while True:
            conn = None
            if self.pool.qsize() > 0:
                conn = self.pool.get()

            if not self._ping(conn):
                if self.test_run:
                    self.port = 3306

                self._re_initialize_pool()

            else:
                self.pool.put_nowait(conn)

            if self.test_run:
                break
            gevent.sleep(self.time_to_sleep)

    def _ping(self, conn):
        try:
            if conn is None:
                conn = db.connect(host=self.host,
                                  user=self.username,
                                  passwd=self.password,
                                  port=self.port)
            cursor = conn.cursor()
            cursor.execute('select 1;')
            LOGGER.debug(cursor.fetchall())
            return True

        except db.OperationalError, e:
            LOGGER.warn('Cannot connect to mysql - retrying in {} seconds'.format(self.time_to_sleep))
            LOGGER.exception(e)
            return False

# test (pytest compatible) -------------------------------------------------------------------------------------------
import logging

from src.py.ConnectionPool import ConnectionPool

logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger("test_connection_pool")

def test_get_initialized_connection_pool():
    config = {
        'user': 'root',
        'password': '',
        'host': '127.0.0.1',
        'port': 3305
    }
    conn_pool = ConnectionPool(config, time_to_sleep=5, test_run=True)
    pool = conn_pool.get_initialized_connection_pool()
    # when in test run the port will be switched back to 3306
    # so the queue size should be 20 - will be nice to work
    # around this rather than test_run hack
    assert pool.qsize() == 20

rawDescription: Python mysql (using pymysql) auto reconnect


code:
class MyClass(object):
    def __init__(self, s):
        self.s = s
    def __cmp__(self, other):
        return cmp(self.s, other.s)
    def __hash__(self):
        return hash(self.s) # Use default hash for 'self.s'

instance1, instance2 = MyClass("a"), MyClass("a")
instance3 = MyClass("b")

print instance2==instance1             # True
print instance2 in [instance1]         # True
print instance2 in set([instance1])    # True

rawDescription: In python - the operator which a set uses for test if an object is in the set


code:
import locale
lang, encoding = locale.getdefaultlocale()
mystring = u"blabla"
print mystring.encode(encoding)

rawDescription: string encodings in python

description: String encodings in python


code: print 'computing %s %% %s' % (num1, num2)

rawDescription: Printing python modulus operator as it is over command line


code:
import sys
import time
from PyQt4 import QtGui
from PyQt4 import QtCore

class QCustomThread (QtCore.QThread):
    startLoad    = QtCore.pyqtSignal(int)
    progressLoad = QtCore.pyqtSignal(int)
    statusLoad   = QtCore.pyqtSignal(bool)

    def __init__ (self, parentQWidget = None):
        super(QCustomThread, self).__init__(parentQWidget)
        self.wasCanceled = False

    def run (self):
        # Simulate data load estimation
        numberOfprogress = 100
        self.startLoad.emit(numberOfprogress)
        for progress in range(numberOfprogress + 1):
            # Delay
            time.sleep(0.1)
            if not self.wasCanceled:
                self.progressLoad.emit(progress)
            else:
                break
        self.statusLoad.emit(True if progress == numberOfprogress else False)
        self.exit(0)

    def cancel (self):
        self.wasCanceled = True

class QCustomMainWindow (QtGui.QMainWindow):
    def __init__ (self):
        super(QCustomMainWindow, self).__init__()
        # Create action with QPushButton
        self.startQPushButton = QtGui.QPushButton('START')
        self.startQPushButton.released.connect(self.startWork)
        self.setCentralWidget(self.startQPushButton)
        # Create QProgressDialog
        self.loadingQProgressDialog = QtGui.QProgressDialog(self)
        self.loadingQProgressDialog.setLabelText('Loading')
        self.loadingQProgressDialog.setCancelButtonText('Cancel')
        self.loadingQProgressDialog.setWindowModality(QtCore.Qt.WindowModal)

    def startWork (self):
        myQCustomThread = QCustomThread(self)
        def startLoadCallBack (numberOfprogress):
            self.loadingQProgressDialog.setMinimum(0)
            self.loadingQProgressDialog.setMaximum(numberOfprogress)
            self.loadingQProgressDialog.show()
        def progressLoadCallBack (progress):
            self.loadingQProgressDialog.setValue(progress)
        def statusLoadCallBack (flag):
            print 'SUCCESSFUL' if flag else 'FAILED'
        myQCustomThread.startLoad.connect(startLoadCallBack)
        myQCustomThread.progressLoad.connect(progressLoadCallBack)
        myQCustomThread.statusLoad.connect(statusLoadCallBack)
        self.loadingQProgressDialog.canceled.connect(myQCustomThread.cancel)
        myQCustomThread.start()

myQApplication = QtGui.QApplication(sys.argv)
myQCustomMainWindow = QCustomMainWindow()
myQCustomMainWindow.show()
sys.exit(myQApplication.exec_())

rawDescription: Implementing QProgressBar against a class


code:
import numpy as np
from matplotlib import pyplot as plt
from functools import partial

def onclick_cbar(cbar, event):
    if event.inaxes is cbar.ax:
        print cbar.mappable
        print cbar.mappable.get_clim()
        print event.xdata, event.ydata

fig = plt.figure()
y, x = np.mgrid[-1:1:100j, -1:1:100j]
z = np.sin(x**2 + y**2)
pcm = plt.pcolormesh(x, y, z)
cbar = plt.colorbar()
cid = fig.canvas.mpl_connect('button_press_event', partial(onclick_cbar, cbar))
plt.show()

rawDescription: matplotlib pan-zoom colorbar axis

description: Matplotlib pan-zoom colorbar axis


code:
import tesseract
import cv2.cv as cv

api = tesseract.TessBaseAPI()
api.Init('.','eng',tesseract.OEM_DEFAULT)
api.SetPageSegMode(tesseract.PSM_AUTO_OSD)

# Load image
img_data = cv2.imread('file.jpg')
image = cv.CreateImageHeader((width1,height1), cv.IPL_DEPTH_8U, channel1)
cv.SetData(image, img_data.tostring(),img_data.dtype.itemsize * channel1 * (width1))
tesseract.SetCvImage(image,api)

# Check number of chars
chars_iterator = api.AnalyseLayout()
num_chars = 1
while chars_iterator.Next(tesseract.RIL_SYMBOL) is True: num_chars += 1

# Break of there are too many chars
if num_chars > 1000:
    print "Too many chars!"
    break

# Reset api to delete previous layout iterator
api.Clear()
tesseract.SetCvImage(image,api)

# Do real OCR, and put this in its own thread if you want to kill it when it takes too long
result_xml = api.GetHOCRText(1)
print api.GetUTF8Text()

rawDescription: python tesseract get number of lines without OCR

description: Python tesseract get number of lines without OCR


rawDescription: operations on a NumPy array based on info in another Array

description: Operations on a NumPy array based on info in another Array


code:
from splinter import *
from dropbox import rest, session
from dropbox import client as dbclient
import time

# Dropbox API
APP_KEY = '###'
APP_SECRET = '###'
ACCESS_TYPE = 'dropbox'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
emailDropbox = '###'
passwordDropbox = '###'

request_token = sess.obtain_request_token()

urlDropbox = sess.build_authorize_url(request_token)

def phantomjsOAuth():
    # Target url
    print 'Target url: ', urlDropbox

    browser = Browser('phantomjs')
    print 'Starting phantomjs browser'
    print 'Visiting url'
    browser.visit(urlDropbox)

    # Email form
    print 'Is the email form present? ', browser.is_element_present_by_id('login_email')
    print 'Filling email form'
    browser.find_by_id('email-field').first.find_by_id('login_email').first.fill(emailDropbox)
    print 'Email form successfully filled'

    # Password form
    print 'Is the password form present? ', browser.is_element_present_by_id('login_password')
    print 'Filling password form'
    browser.find_by_id('login_password').first.fill(passwordDropbox)
    print 'Password form successfully filled'

    # Find login submit button
    print 'Is the "Submit" button present?', browser.is_element_present_by_name('login_submit_dummy')
    submitButton = browser.is_element_present_by_name('login_submit_dummy')

    if submitButton == True:
        print 'Pausing for 5 seconds to avoid clicking errors'
        time.sleep(5)
        print 'Attempting to click the "Submit" button in order to login'
        browser.find_by_name('login_submit_dummy').first.click()
        print '"Submit" button successfully clicked'

        # Allow connection with Dropbox
        print 'Is the "Allow" button present?', browser.is_element_present_by_css('.freshbutton-blue')
        allowButton = browser.is_element_present_by_css('.freshbutton-blue')

        if allowButton == True:
            print 'The "Allow" button is present, attempting to click..'
            browser.find_by_css('.freshbutton-blue').click()
            print 'The "Allow" button is successfully clicked, access to Dropbox is granted.'

            browser.quit()

            dropboxCode()

        else:
            print 'The "Allow" button is not present, quitting.'
            browser.quit()

    else:
        print 'The "Submit" button was not present, quitting.'
        browser.quit()

def dropboxCode():
    # The rest of the Dropbox code
    # This will fail if 'Allow' wasn't clicked
    access_token = sess.obtain_access_token(request_token)

    client = dbclient.DropboxClient(sess)
    print "linked account:", client.account_info()

    f = open('dropbox_api_test_file.txt')
    response = client.put_file('/Python/Apps/###/Orders/dropbox_api_test_file.txt', f)
    print "uploaded:", response
    sess.unlink()

if __name__ == "__main__":
    phantomjsOAuth()

rawDescription: Automate OAuth in Dropbox API - Clicking the submit button to login


code:
import re
str = '12-1 abcd fadf adfad'
m   = re.search('(\d+-\d+)', str)
if m:
    found = m.group(0)
    print found

rawDescription: Use Python regular expression to extract special strings


code:
data=open("file").readlines()
for n,line in enumerate(data):
    if line.startswith("line"):
       data[n] = "\n"+line.rstrip()
    else:
       data[n]=line.rstrip()
print '|'.join(data)

rawDescription: How can I combine multiple lines of text into one line in Python with a delimiter to separate them?

description: Combine multiple lines of text into one line in Python with a delimiter to separate them


code:
searchArray = [10,20,30,40,50,60,100,80,90,110]
slicedArray = searchArray[3:9]
print slicedArray.index(max(slicedArray))+3

rawDescription: how to get the maximum value from a specific portion of a array in python

description: Get the maximum value from a specific portion of a array in python


code:
def puzzle(s,a,b):
    if a*b != len(s):
       print "Not possible"
       return
    start =0
    end = b+1
    output =""
    for i in range(0,a):
        output= output+s[start: end]+"\n"
        start = end+1
        end = end+b
    print output

rawDescription: How to split string at regular intervals in Python 3

description: Split string at regular intervals in Python 3


code:
class Class1(object):
    def __init__(self):
        self.x = self.dosomething()

    def dosomething(self):
        return [99, 98]

class Class2(object):
    def __init__(self):
        y = Class1()
        print y.x

rawDescription: Use variables in between classes in python


code:
import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
   print 'matches'

rawDescription: Check python string format?

description: Check python string format


rawDescription: Iterate elements on multiple python lists


rawDescription: Multinomial distribution in PyMC

description: Multinomial distribution in PyMC


code:
import vlc

def setup_player(filename):
    vlc_instance = vlc.Instance()
    media = vlc_instance.media_new(filename)
    player = vlc_instance.media_player_new()
    player.set_media(media)
    player.play()# Start the player
    print player.get_state()# Print player's state
    player.stop()# Stop the player
    print player.get_state()# Print player's state

setup_player('filename')#Put media file name/location here

rawDescription: Wait for song played outside of python to finish


code:
import sys
from PyQt4.QtCore import QObject, pyqtSlot
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView

html = """

Hello!

QObject Test

JS test

"""

class ConsolePrinter(QObject):
    def __init__(self, parent=None):
        super(ConsolePrinter, self).__init__(parent)

    @pyqtSlot(str)
    def text(self, message):
        print message

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = QWebView()
    frame = view.page().mainFrame()
    printer = ConsolePrinter()
    view.setHtml(html)
    frame.addToJavaScriptWindowObject('printer', printer)
    frame.evaluateJavaScript("alert('Hello');")
    frame.evaluateJavaScript("printer.text('Goooooooooo!');")
    view.show()
    app.exec_()

rawDescription: PyQt (PySide), WebKit and exposing methods from/to Javascript


code:
for i, d in enumerate(database):
    print i, '->', d.itemSets

rawDescription: Python, print value and List in one line


code:
def print_list_matrix(lst, row_num):
    for i in range(row_num):
        print ' '.join(lst[i::row_num])

print_list_matrix(lst, 3)
print_list_matrix(lst, 4)

rawDescription: Printing a list down and then across

description: Printing a list down and then across


code:
def print_hello_world(): # first function
    print "hello world"

def in_an_if_statement(): # a function that uses first func in if statement
    if 1 == 1:
        print_hello_world()

def in_a_loop(): # a function that uses first func in a loop
    for i in range(3):
        print_hello_world()

if __name__ == '__main__':
    in_an_if_statement()
    print '----'
    in_a_loop()

rawDescription: How do I call a defined procedure within another def procedure? - Python

description: Call a defined procedure within another def procedure? - Python


code:
from ecdsa import VerifyingKey, BadSignatureError
vk = VerifyingKey.from_pem(open("public.pem").read())
message = open("message","rb").read()
sig = open("signature","rb").read()
try:
    vk.verify(sig, message)
    print "good signature"
except BadSignatureError:
    print "BAD SIGNATURE"

rawDescription: Python decrypt signatures with only a public key


code:
import os
import bz2

def split_xml(filename):
    ''' The function gets the filename of wiktionary.xml.bz2 file as  input and creates
    smallers chunks of it in a the diretory chunks
    '''
    # Check and create chunk diretory
    if not os.path.exists("chunks"):
        os.mkdir("chunks")
    # Counters
    pagecount = 0
    filecount = 1
    #open chunkfile in write mode
    chunkname = lambda filecount: os.path.join("chunks","chunk-"+str(filecount)+".xml.bz2")
    chunkfile = bz2.BZ2File(chunkname(filecount), 'w')
    # Read line by line
    bzfile = bz2.BZ2File(filename)
    for line in bzfile:
        chunkfile.write(line)
        # the </page> determines new wiki page
        if '</page>' in line:
            pagecount += 1
        if pagecount > 1999:
            #print chunkname() # For Debugging
            chunkfile.close()
            pagecount = 0 # RESET pagecount
            filecount += 1 # increment filename
            chunkfile = bz2.BZ2File(chunkname(filecount), 'w')
    try:
        chunkfile.close()
    except:
        print 'Files already close'

if __name__ == '__main__':
    # When the script is self run
    split_xml('wiki-files/tawiktionary-20110518-pages-articles.xml.bz2')

rawDescription: How to split large wikipedia dump .xml.bz2 files in Python?

description: Split large wikipedia dump .xml.bz2 files in Python


code:
input_implementations = { 'xls':xls, 'xml':xml, 'csv':csv }

def input_factory(filename):
    ext = os.path.splitext(filename)[1][1:].lower()
    impl = input_implementations.get(ext, None)
    if impl is None:
        print 'rain fire from the skies'
    else:
        return impl(filename)

rawDescription: Determine behavior based on subclass

description: Determine behavior based on subclass


code:
import win32process
print win32process.GetProcessMemoryInfo(win32process.GetCurrentProcess())

rawDescription: Process size in XP from Python

description: Process size in XP from Python


code:
import csv
reader = csv.reader(open('test.data', 'r'))
for row in reader:
    print '\t'.join(row)

import csv
reader = csv.reader(open('test.data', 'r'))
result = []
for row in reader:
    result.append(row)
print result[0][1]

rawDescription: Distribution of elements Python

description: Distribution of elements Python


code:
import wx

########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = wx.Panel(self)

        btn = wx.Button(panel, label="Ask Question")
        btn.Bind(wx.EVT_BUTTON, self.showMessageDlg)

    #----------------------------------------------------------------------
    def showMessageDlg(self, event):
        """
        Show a message
        """
        msg = "Do you want to continue?"
        title = "Question!"
        style =  wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION
        dlg = wx.MessageDialog(parent=None, message=msg,
                               caption=title, style=style)
        result = dlg.ShowModal()
        if result == wx.ID_YES:
            print "User pressed yes!"
        else:
            print "User pressed no!"
        dlg.Destroy()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

rawDescription: wxpython , passing boolean flag for background checking

description: Wxpython , passing boolean flag for background checking


code:
import teradata
import pyodbc
import sys

udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0",
        logConsole=False)

session = udaExec.connect(method="odbc", dsn="prod32",
        username="PRODRUN", password="PRODRUN");

i = 0
REJECTED = 'R';

f = file("output.txt","w");sys.stdout=f

cursor =  session.cursor();

ff_remaining = 0;

cnt = cursor.execute("SELECT  SEQ_NO,FRQFBKDC,PNR_RELOC FROM ttemp.ffremaining ORDER BY 1,2,3 ").rowcount;
rows = cursor.execute("SELECT  SEQ_NO,FRQFBKDC,PNR_RELOC FROM ttemp.ffremaining ORDER BY 1,2,3 ").fetchall();

for i in range(cnt):
    ff_remaining = cursor.execute("select count(*) as coun from  ttemp.ffretroq_paxoff where seq_no=? and status <> ?",(rows[i].seq_no,REJECTED)).fetchall();
    print ff_remaining[0].coun, rows[i].seq_no, REJECTED;

rawDescription: Connecting Python with Teradata using Teradata module


code:
a = ['a', 'b']
print " or ".join(repr(i) for i in a)

rawDescription: Python Concatenating Strings

description: Python Concatenating Strings


rawDescription: In laymans terms, what does the Python string format "g" actually mean?


code:
for index, entry in enumerate(longList):
    if entry == 'foo':
        print index

rawDescription: What is the proper way to track indexes in python?


code:
for b in range(Data.shape[1]):
    img = Data[:,b]
    img = np.reshape(img,(501,501))
    print "Saving %s of %s"%(b,Data.shape[1])
    save = os.path.join(savedir,"%s_%s"%(item,b))
    plt.imshow(img).figure.savefig(save)
    print "There are %d image(s) open"%len(plt.gca().images)
    plt.gcf().clf() # clear the figure

rawDescription: Writing a large number of files in python, noticable slowdown at the end


rawDescription: does findall by regex store unordered items in python dictionary?

description: Does findall by regex store unordered items in python dictionary


code:
import os, sigar

sg = sigar.open()
mem = sg.mem()
sg.close()
print mem.total() / 1024, mem.free() / 1024

rawDescription: Getting total/free RAM from within Python


code:
import collections
import itertools
import re

def find_words(line):
    for match in re.finditer("\w+", line):
        yield match.group().lower()

counts1 = collections.Counter()
counts2 = collections.Counter()
counts_pairs = collections.Counter()

with open("src.txt") as f1, open("tgt.txt") as f2:
    for line1, line2 in itertools.izip(f1, f2):
        words1 = list(find_words(line1))
        words2 = list(find_words(line2))
        counts1.update(words1)
        counts2.update(words2)
        counts_pairs.update(itertools.product(words1, words2))

print counts1["someword"]
print counts1["anotherword"]
print counts_pairs["someword", "anotherword"]

rawDescription: Python: fast iteration through file

description: Python: fast iteration through file


rawDescription: Python find the max date in a list which are no later than a given date


code:
#!/usr/bin/python -u

while True:
    try:
        print "This is only a test..."
        with open("/home/users/sj091190/loop.txt", "a") as loopFile:
            loopFile.write("This is only a test...")
            loopFile.write('\n')
            loopFile.flush()
        time.sleep(1)
    except KeyboardInterrupt:
        break
        quit()

rawDescription: Python script not writing to file when executed at boot


code:
import re

def texify(text):
    text = re.sub('\d+', lambda match: '_{' + match.group(0) + '}', text)
    return '$' + text.replace('*', '\\ast ') + '$'

print texify('x0+x1*x2+y40+y1')

rawDescription: Formatting python output for tex math mode?


code:
input = '17k, 14.05m, 1.235b'

multipliers = { 'k': 1e3,
                'm': 1e6,
                'b': 1e9,
              }

pattern = r'([0-9.]+)([bkm])'

for number, suffix in re.findall(pattern, input):
    number = float(number)
    print number * multipliers[suffix]

rawDescription: how to make a variable change from the text "1m" into "1000000" in python

description: Make a variable change from the text "1m" into "1000000" in python


code:
@receiver(post_save,sender=Resource)
def ResourceSaved(sender,**kwargs):
    print "Saved"

@receiver(post_delete,sender=Resource)
def ResourceDeleted(sender,**kwargs):
    print "Deleted"

rawDescription: Registering Multiple Signals in Django

description: Registering Multiple Signals in Django


code:
import matplotlib
matplotlib.use("TkAgg") #the back-end of matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import Tkinter as tk

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.lines import Line2D
from matplotlib import style

ROTOR_SPEED = 100
PUMP_STATUS = False

LARGE_FONT = ("Verdana", 12)
NORM_FONT = ("Verdana", 10)
SMALL_FONT = ("Verdana", 8)

style.use("ggplot")

fig = plt.figure()
fig.set_size_inches(10, 7)
top_graph = plt.subplot2grid((2, 2), (0, 0), colspan=2)

line1 = Line2D([], [], color='black')

line2 = Line2D([], [], color='red', linewidth=2)

top_graph.add_line(line1)

top_graph.set_xlim(0, 24)
top_graph.set_ylim(-20, 20)

top_graph.set_aspect('equal', 'datalim')

line, = top_graph.plot([], [], lw=1)
line2, = top_graph.plot([], [], lw=3)
xdata, ydata = [], []
xline, yline = [], []

def data_gen():
    t = data_gen.t
    cnt = 0
    height = 15
    while cnt < 1000:

        yield t, np.sin(2 * np.pi * t / 10.), 2*[t]*(height), range(-height, height)
        cnt+=1
        t += 0.05
data_gen.t = 0

def run(data):
    # update the data
    t, y, t2, rang = data

    repeats = t//24

    run.index = int(round(t%24.0/0.05))

    if len(xdata) <= run.index:
        xdata.append(t)
        ydata.append(y)
        xline = t2
        yline = rang

    else:
        xdata[run.index] = t - (24 * repeats)
        ydata[run.index] = y

        xline = map(lambda (a, b): a-b, zip(t2, [24*repeats]*len(t2)))
        yline = rang

    line.set_data(xdata, ydata)
    line2.set_data(xline, yline)
    line2.set_color('white')
    return line, line2
run.index = 1

class MainPage(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        #********** FRAMES*******#

        self.frames = {} #empty..

        frame = GraphPage(container, self)

        self.frames[GraphPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(GraphPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class GraphPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Example", font=LARGE_FONT)
        label.grid(row=0, column=0, sticky='N')

        canvas = FigureCanvasTkAgg(fig, self)

        canvas.show()
        canvas.get_tk_widget().grid(row=1, column=0, rowspan=6, columnspan=3, sticky='NSEW')

app = MainPage()
app.geometry("980x640")
ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=20,
    repeat=False)
app.mainloop()

rawDescription: How to redraw only the updated data using Matplotlib

description: Redraw only the updated data using Matplotlib


code:
import re
regexc = re.compile(r"(?:^|[^\\])'(([^\\']|\\'|\\\\)*)'")

def check(test, base, target):
    match = regexc.search(base)
    assert match is not None, test+": regex didn't match for "+base
    assert match.group(1) == target, test+": "+target+" not found in "+base
    print "test %s passed"%test

check("Empty","''","")
check("single escape1", r""" Example: 'Foo \' Bar'  End. """,r"Foo \' Bar")
check("single escape2", r"""'\''""",r"\'")
check("double escape",r""" Example2: 'Foo \\' End. """,r"Foo \\")
check("First quote escaped",r"not matched\''a'","a")
check("First quote escaped beginning",r"\''a'","a")

rawDescription: Regex for managing escaped characters for items like string literals


code:
num_hits = 0
num_outs = 0
for i in range(1, atBats):
    if random_pick(a,b) == 'Hit':
        num_hits += 1
    else:
        num_outs += 1
print num_hits, num_outs

rawDescription: Summing the number of instances a string is generated in iteration


code:
import multiprocessing as mp

def f(x):
    f.q.put('Doing: ' + str(x))
    return x*x

def f_init(q):
    f.q = q

def main():
    jobs = range(1,6)

    q = mp.Queue()
    p = mp.Pool(None, f_init, [q])
    results = p.imap(f, jobs)
    p.close()

    for i in range(len(jobs)):
        print q.get()
        print results.next()

if __name__ == '__main__':
    main()

rawDescription: Can I use a multiprocessing Queue in a function called by Pool.imap?

description: Use a multiprocessing Queue in a function called by Pool.imap


code:
original_string = '12 3 4 5'
splitted = original_string .split() #splitting
numeric_splitted = [int(numeric_string) for numeric_string in splitted] #casting
numeric_splitted.sort() #sorting
print numeric_splitted # [3,4,5,12] is printed

rawDescription: Sorting numbers in a string

description: Sorting numbers in a string


code:
import subprocess

proc = subprocess.Popen(["python", "-c", "import writer; writer.write()"], stdout=subprocess.PIPE)
out = proc.communicate()[0]
print out.upper()

rawDescription: Capture stdout from a script in Python

description: Capture stdout from a script in Python


code:
import re
from scrapy.http import FormRequest, Request
from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider

class BeaurepairesSpider(BaseSpider):
    name = "beaurepaires"
    allowed_domains = ["beaurepaires.com.au", "gdt.rightthere.com.au"]
    start_urls = ["http://www.beaurepaires.com.au/store-locator/"]

    def parse(self, response):
        yield FormRequest.from_response(response, formname='frm_dealer_locator',
                                        formdata={'dealer_postcode_textfield':'2115'},
                                        callback=self.parseBeaurepaires)

    def parseBeaurepaires(self, response):
        hxs = HtmlXPathSelector(response)

        script = str(hxs.select("//div[@id='jl_container']/script[4]/text()").extract()[0])
        url, script_name = re.findall(r'LoadScripts\("([a-zA-Z:/\.]+)", "(\w+)"', script)[0]
        url = "%s/locator/js/data/%s.js" % (url, script_name)
        yield Request(url=url, callback=self.parse_js)

    def parse_js(self, response):
        print response.body  # here are your locations - right, inside the js file

rawDescription: using scrapy to extract dynamic data - location based on postcodes

description: Using scrapy to extract dynamic data - location based on postcodes


code: print infile[0]

rawDescription: Get the last argument from optparse python


code:
data = [
        ['x', 'shop1', 1],
        ['x', 'shop1', 2],
        ['x', 'shop2', 3],
        ['x', 'shop2', 4],
        ['x', 'shop3', 5],
        ['y', 'shop4', 6],
        ['y', 'shop4', 7],
        ['y', 'shop4', 8]
    ]

def add(state, x):
    state[0] += 1
    state[1] = min(state[1], x)
    state[2] = max(state[2], x)
    state[3] += x
    return state

def merge(state1, state2):
    state1[0] += state2[0]
    state1[1] = min(state1[1], state2[1])
    state1[2] = max(state1[2], state2[2])
    state1[3] += state2[3]
    return state1

res = sc.parallelize(data).map(lambda x: ((x[0], x[1]), x[2])).aggregateByKey([0, 10000, 0, 0], add, merge)

for x in res.collect():
    print 'Client "%s" shop "%s" : count %d min %f max %f avg %f' % (
        x[0][0], x[0][1],
        x[1][0], x[1][1], x[1][2], float(x[1][3])/float(x[1][0])
    )

rawDescription: How to optimize this code on spark?

description: Optimize this code on spark


code:
useful_words = set(['Complain', 'Dropped calls', 'lolcat'])
for i in range(x):
    filtered_words = useful_words.intersection(set(in_data[i]))
    print filtered_words

rawDescription: Remove non-select text in Python Script

description: Remove non-select text in Python Script


code:
import types

class CallAll(type):
    """ MetaClass that adds methods to call all superclass implementations """
    def __new__(meta, clsname, bases, attrs):
        ## collect a list of functions defined on superclasses
        funcs = {}
        for base in bases:
            for name, val in vars(base).iteritems():
                if type(val) is types.FunctionType:
                    if name in funcs:
                        funcs[name].append( val )
                    else:
                        funcs[name] = [val]

        ## now we have all methods, so decorate each of them
        for name in funcs:
            def caller(self, *args,**kwargs):
                """ calls all baseclass implementations """
                for func in funcs[name]:
                    func(self, *args,**kwargs)
            attrs[name] = caller

        return type.__new__(meta, clsname, bases, attrs)

class B:
    def fn(self):
        print 'B'

class A:
    def fn(self):
        print 'A'

class C(A,B, object):
    __metaclass__=CallAll

c=C()
c.fn()

rawDescription: Inheritance in Python Such That All Base Functions Are Called


code:
allowed_modules = ['os', 're', 'your_module', 'parser_class1.py', 'parser_class2.py']

if not results.module:
    aparser.error('Error! no module')
try:
    if results.module in allowed_modules:
        module = __import__(results.module)
        print '%s imported as "module"'%(results.module)
    else:
        print 'hey what are you trying to do?'
except ImportError, e:
    print e

module.your_function(your_data)

rawDescription: Load modules conditionally Python

description: Load modules conditionally Python


code:
import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')

rawDescription: what does "from MODULE import _" do in python?

description: What does "from MODULE import _" do in python


code:
import threading
import urllib2
from time import sleep

def load_img(local_path, web_path):
    f = open(local_path, 'wb')
    f.write(urllib2.urlopen(web_path).read())
    f.close()

local_path = 'foo.txt'
web_path = 'http://www.google.com/'

img_thread = threading.Thread(target=load_img, args=(local_path, web_path))
img_thread.start()
while img_thread.is_alive():
    print "doing some other stuff while the thread does its thing"
    sleep(1)
img_thread.join()

rawDescription: Load image in new thread

description: Load image in new thread


code:
# stolen from Hackaholic's answer
import subprocess
child = subprocess.Popen('apcaccess',stdout=subprocess.PIPE)
msg,err = child.communicate()

# now create the dict:
myDict={}
#for i in msg.split("\n"):  # loop over lines
for i in msg.splitlines():  # EDIT: See comments
    splitted=i.split(":")  # list like ["HOSTNAME ", " doormon"]
    # remove leading & trailing spaces, add to dict
    myDict[splitted[0].strip()]=splitted[1].strip()

#Now, you can easily access the items:
print myDict["SERIALNO"]
print myDict["STATUS"]
print myDict["BATTV"]

for k in myDict.keys():
    print k +" = "+ myDict[k]

rawDescription: python return list from linux command output

description: Python return list from linux command output


code:
while 1:
  socket_list = [sys.stdin, s]
  # Get the list sockets which are readable
  read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
  for sock in read_sockets:
   #incoming message from remote server
   if sock == s:
      data = sock.recv(4096)
      if not data :
        print '\nDisconnected from server'
        sys.exit()
      else :
         #print data
         sys.stdout.write(data)

   #user entered a message
   else :
     msg = sys.stdin.readline()
     s.send(msg)

rawDescription: Check if data available in sockets in python


code:
import sys

print 'Working',

while working():
    work()

    print '.',

    sys.stdout.flush()

print 'Done'

rawDescription: Python print not working before long task?


code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")

# We add 35000 paragraphs with class `test` to the page so that we can
# later show how to get the first 10 paragraphs of this class. Each
# paragraph is uniquely numbered.
driver.execute_script("""
var html = [];
for (var i = 0; i < 35000; ++i) {
  html.push("
"+ i + "
");
}
document.body.innerHTML += html.join("");
""")

elements = driver.execute_script("""
return Array.prototype.slice.call(document.querySelectorAll("p.test"), 0, 10);
""")

# Verify that we got the first 10 elements by outputting the text they
# contain to the console. The loop here is for illustration purposes
# to show that the `elements` array contains what we want. In real
# code, if I wanted to process the text of the first 10 elements, I'd
# do what I show next.
for element in elements:
    print element.text

# A better way to get the text of the first 10 elements. This results
# in 1 round-trip between this script and the browser. The loop above
# would take 10 round-trips.
print driver.execute_script("""
return Array.prototype.slice.call(document.querySelectorAll("p.test"), 0, 10)
           .map(function (x) { return x.textContent; });;
""")

driver.quit()

rawDescription: How to return X elements [Selenium]?

description: Return X elements [Selenium]


code:
import httplib
conn = httplib.HTTPConnection("www.python.org")
try:
    conn.request("GET", "/index.html")
except Exception as e:
    #take action according to the error.
    print(type(e))
r1 = conn.getresponse()
print r1.status, r1.reason

rawDescription: python httplib and broken tcp connection

description: Python httplib and broken tcp connection


code:
soup = BeautifulSoup(data)

for row in soup.find_all('tr'):
    grade = row.select('th strong')
    name = row.select('td label')
    if grade and name:
        print grade[0].text, name[0].text

rawDescription: Parsing for text under specific tags in HTML, Python


code:
import pylab
import os
import cv
import cv2
import numpy

global_object_points = None
global_image_points = None
global_captured_corners = None
global_original_corners = None
global_success_index = None

global_font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 1.0, 1.0)

def get_camera_calibration_data(original_image_list, captured_image_list, board_width, board_height):
    """Get the map for undistorting projected images by using a list of original chessboard images and the list of images that were captured by camera.

    original_image_list - list containing the original images (loaded as OpenCV image).

    captured_image_list - list containing the captured images.

    board_width - width of the chessboard (number of files - 1)

    board_height - height of the chessboard (number of ranks - 1)

    """
    global global_object_points
    global global_image_points
    global global_captured_corners
    global global_original_corners
    global global_success_index
    print 'get_undistort_map'
    corner_count = board_width * board_height
    # Try to detect the corners of the chessboard. For each image,
    # FindChessboardCorners returns (found, corner_points). found is
    # True even if it managed to detect only a subset of the actual
    # corners.  NOTE: according to
    # http://opencv.willowgarage.com/wiki/documentation/cpp/calib3d/findChessboardCorners,
    # no need for FindCornerSubPix after FindChessBoardCorners
    captured_corners = [cv.FindChessboardCorners(img, (board_width, board_height)) for img in captured_image_list]
    original_corners = [cv.FindChessboardCorners(img, (board_width, board_height)) for img in original_image_list]
    success_captured = [index for index in range(len(captured_image_list))
                        if captured_corners[index][0] and len(captured_corners[index][1]) == corner_count]
    success_original = [index for index in range(len(original_image_list))
                        if original_corners[index][0] and len(original_corners[index][2]) == corner_count]
    success_index = [index for index in success_captured if (len(captured_corners[index][3]) == corner_count) and (index in success_original)]
    global_success_index = success_index
    print global_success_index
    print 'Successfully found corners in image #s.', success_index
    cv.NamedWindow('Image', cv.CV_WINDOW_AUTOSIZE)
    for index in success_index:
        copy = cv.CloneImage(original_image_list[index])
        cv.DrawChessboardCorners(copy, (board_width, board_height), original_corners[index][4], corner_count)
        cv.ShowImage('Image', copy)
        a = cv.WaitKey(0)
        copy = cv.CloneImage(captured_image_list[index])
        cv.DrawChessboardCorners(copy, (board_width, board_height), captured_corners[index][5], corner_count)
        cv.ShowImage('Image', copy)
        a = cv.WaitKey(0)
    cv.DestroyWindow('Image')
    if not success_index:
        return
    global_captured_corners = [captured_corners[index][6] for index in success_index]
    global_original_corners = [original_corners[index][7] for index in success_index]
    object_points = cv.CreateMat(len(success_index) * (corner_count), 3, cv.CV_32FC1)
    image_points = cv.CreateMat(len(success_index) * (corner_count), 2, cv.CV_32FC1)
    global_object_points = object_points
    global_image_points = image_points
    point_counts = cv.CreateMat(len(success_index), 1, cv.CV_32SC1)
    for ii in range(len(success_index)):
        for jj in range(corner_count):
            cv.Set2D(object_points, ii * corner_count + jj, 0, float(jj/board_width))
            cv.Set2D(object_points, ii * corner_count + jj, 1, float(jj%board_width))
            cv.Set2D(object_points, ii * corner_count + jj, 2, float(0.0))
            cv.Set2D(image_points, ii * corner_count + jj, 0, captured_corners[success_index[ii]][8][jj][0])
            cv.Set2D(image_points, ii * corner_count + jj, 1, captured_corners[success_index[ii]][9][jj][10])
        cv.Set1D(point_counts, ii, corner_count)
    # Create the output parameters
    camera_intrinsic_mat = cv.CreateMat(3, 3, cv.CV_32FC1)
    cv.Set2D(camera_intrinsic_mat, 0, 0, 1.0)
    cv.Set2D(camera_intrinsic_mat, 1, 1, 1.0)
    distortion_mat = cv.CreateMat(5, 1, cv.CV_32FC1)
    rotation_vecs = cv.CreateMat(len(success_index), 3, cv.CV_32FC1)
    translation_vecs = cv.CreateMat(len(success_index), 3, cv.CV_32FC1)
    print 'Before camera clibration'
    # Do the camera calibration
    cv.CalibrateCamera2(object_points, image_points, point_counts, cv.GetSize(original_image_list[0]), camera_intrinsic_mat, distortion_mat, rotation_vecs, translation_vecs)
    return (camera_intrinsic_mat, distortion_mat, rotation_vecs, translation_vecs)

if __name__ == '__main__':
    # angles - the angles at which the picture was rotated
    angles = [0, 36, 72, 108, 144, 180, 216, 252, 288, 324]
    # orig_files - list of original picture files used for projection
    orig_files =  ['../calibration/checkerboard/o_orig_%d.png' % (angle) for angle in angles]
    # img_files - projected image captured by camera
    img_files = ['../calibration/checkerboard/captured_imag_%d.bmp' % (angle) for angle in angles]

    # orig_files = ['o%d.png' % (angle) for angle in range(10, 40, 10)]
    # img_files = ['d%d.png' % (angle) for angle in range(10, 40, 10)]
    # Load the images
    print 'Loading images'
    captured_images = [cv.LoadImage(filename) for filename in img_files]
    orig_images = [cv.LoadImage(filename) for filename in orig_files]
    # Convert to grayscale
    gray_images = [cv.CreateImage((src.height, src.width), cv.IPL_DEPTH_8U, 1) for src in captured_images]
    for ii in range(len(captured_images)):
        cv.CvtColor(captured_images[ii], gray_images[ii], cv.CV_RGB2GRAY)
        cv.ShowImage('win', gray_images[ii])
        cv.WaitKey(0)
    cv.DestroyWindow('win')
    gray_orig = [cv.CreateImage((src.height, src.width), cv.IPL_DEPTH_8U, 1) for src in orig_images]
    for ii in range(len(orig_images)):
        cv.CvtColor(orig_images[ii], gray_orig[ii], cv.CV_RGB2GRAY)

    # The number of ranks and files in the chessboard. OpenCV considers
    # the height and width of the chessboard to be one less than these,
    # respectively.
    rank_count = 10
    file_count = 11
    camera_intrinsic_mat, distortion_mat, rotation_vecs, translation_vecs, = get_camera_calibration_data(gray_orig, gray_images, file_count-1, rank_count-1)
    xmap = cv.CreateImage(cv.GetSize(captured_images[0]), cv.IPL_DEPTH_32F, 1)
    ymap = cv.CreateImage(cv.GetSize(captured_images[0]), cv.IPL_DEPTH_32F, 1)
    cv.InitUndistortMap(camera_intrinsic_mat, distortion_mat, xmap, ymap)
    # homography = cv.CreateMat(3, 3, cv.CV_32F)
    map_matrix = cv.CreateMat(2, 3, cv.CV_32F)
    source_points = (global_original_corners[0][0], global_original_corners[0][file_count-2], global_original_corners[0][(rank_count-1) * (file_count-1) -1])
    image_points = (global_captured_corners[0][0], global_captured_corners[0][file_count-2], global_captured_corners[0][(rank_count-1) * (file_count-1) -1])
    # cv.GetPerspectiveTransform(source, target, homography)
    cv.GetAffineTransform(source_points, image_points, map_matrix)
    ii = 0
    cv.NamedWindow('OriginaImage', cv.CV_WINDOW_AUTOSIZE)
    cv.NamedWindow('CapturedImage', cv.CV_WINDOW_AUTOSIZE)
    cv.NamedWindow('FixedImage', cv.CV_WINDOW_AUTOSIZE)
    for image in gray_images:
        # The affine transform should be ideally calculated once
        # outside this loop, but as the transform looks different for
        # each image, I'll just calculate it independently to see the
        # applicability
        try:
            # Try to find ii in the list of successful corner
            # detection indices and if found, use the corners for
            # computing the affine transformation matrix. This is only
            # required when the optics changes between two
            # projections, which should not happend.
            jj = global_success_index.index(ii)
            source_points = [global_original_corners[jj][0], global_original_corners[jj][rank_count-1], global_original_corners[jj][-1]]
            image_points = [global_captured_corners[jj][0], global_captured_corners[jj][rank_count-1], global_captured_corners[jj][-1]]
            cv.GetAffineTransform(source_points, image_points, map_matrix)
            print '---------------------------------------------------------------------'
            print orig_files[ii], '<-->', img_files[ii]
            print '---------------------------------------------------------------------'
            for kk in range(len(source_points)):
                print source_points[kk]
                print image_points[kk]
        except ValueError:
            # otherwise use the last used transformation matrix
            pass

        orig = cv.CloneImage(orig_images[ii])
        cv.PutText(orig, '%s: original' % (os.path.basename(orig_files[ii])), (100, 100), global_font, 0.0)
        cv.ShowImage('OriginalImage', orig)
        target = cv.CloneImage(image)
        target.origin = image.origin
        cv.SetZero(target)
        cv.Remap(image, target, xmap, ymap, cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS, cv.ScalarAll(0))
        cv.PutText(target, '%s: remapped' % (os.path.basename(img_files[ii])), (100, 100), global_font, 0.0)
        cv.ShowImage('CapturedImage', target)
        target = cv.CloneImage(orig_images[ii])
        cv.SetZero(target)
        cv.WarpAffine(orig_images[ii], target, map_matrix, cv.CV_INTER_LINEAR | cv.CV_WARP_FILL_OUTLIERS)
        corrected_file = os.path.join(os.path.dirname(img_files[ii]), 'corrected_%s' % (os.path.basename(img_files[ii])))
        cv.SaveImage(corrected_file, target)
        print 'Saved corrected image to', corrected_file
        # cv.WarpPerspective(image, target, homography, cv.CV_INTER_LINEAR | cv.CV_WARP_INVERSE_MAP | cv.CV_WARP_FILL_OUTLIERS)
        cv.PutText(target, '%s: perspective-transformed' % (os.path.basename(img_files[ii])), (100, 100), global_font, 0.0)
        cv.ShowImage('FixedImage', target)
        print '==================================================================='
        cv.WaitKey(0)
        ii += 1
    cv.DestroyWindow('OriginalImage')
    cv.DestroyWindow('CapturedImage')
    cv.DestroyWindow('FixedImage')

rawDescription: How can I transform an image so that projected image is same as original

description: Transform an image so that projected image is same as original


code:
subject = '''\
=?UTF-8?B?15bXkNeqINeR15PXmden15Qg15HXodeZ16HXmdeqINeR15vXk9eZINec15TXkdeZ158g?=
 =?UTF-8?B?157Xk9eV16Ig15TXp9eZ15PXldeTINeU15bXlCDXnNeQINei15XXkdeTINeQ150g15DXoNeZINeo15U=?=
 =?UTF-8?B?16nXnSDXlNeo15HXlA==?='''
from email.header import decode_header
for line in subject.splitlines():
    bytes, encoding = decode_header(line)[0]
    print bytes.decode(encoding)

rawDescription: parsing email_message['Subject'] results 3 strings instead of 1

description: Parsing email_message['Subject'] results 3 strings instead of 1


code:
def getregvalue(key, subkey, value):

    import _winreg

    key = getattr(_winreg, key)
    handle = _winreg.OpenKey(key, subkey)
    (value, type) = _winreg.QueryValueEx(handle, value)

return value

print getregvalue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", "DefaultUserName")

rawDescription: Get winlows login name after launching python as another user


code:
import os
def is_sticky(path):
    return os.stat(path).st_mode & 01000 == 01000

rawDescription: How to check that a path has a sticky bit in python?

description: Check that a path has a sticky bit in python


code:
#!/usr/bin/python
import os
partitionsFile = open("/proc/partitions")
lines = partitionsFile.readlines()[2:]#Skips the header lines
for line in lines:
    words = [x.strip() for x in line.split()]
    minorNumber = int(words[1])
    deviceName = words[3]
    if minorNumber % 16 == 0:
        path = "/sys/class/block/" + deviceName
        if os.path.islink(path):
            if os.path.realpath(path).find("/usb") > 0:
                print "/dev/%s" % deviceName

rawDescription: Better way to script USB device mount in Linux


code:
import socket
import threading
import select
from Queue import Queue

class ClientThread ( threading.Thread ):

   def __init__ ( self, channel, details, queue=None ):
      self.channel = channel
      self.details = details
      self.queue = queue
      threading.Thread.__init__ ( self )

   def run ( self ):

      if self.queue:
         self.queue.put(1)

      print 'Received connection:', self.details [ 0 ]
      self.channel.send ( 'hello from server' )
      for x in xrange ( 10 ):
         print self.channel.recv ( 1024 )
      self.channel.close()
      print 'Closed connection:', self.details [ 0 ]

      if self.queue:
         self.queue.get_nowait()

# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )

rlist = [server]
work_queue = Queue()

def accept_client():
   channel, details = server.accept()
   ClientThread ( channel, details, work_queue ).start()

accept_client()

while not work_queue.empty():
   server_ready, _, _ = select.select(rlist,[],[], .25)
   if server in server_ready:
      accept_client()

print "Shutting down"
server.close()
print "Exiting"

rawDescription: Multi-threaded Python server that exits when threads finish


rawDescription: Semi Unique tuples in python? (aka. tuple primary keys?)


code:
import arcpy
arcpy.env.workspace = r"C:/SARP10/MACP_Tool"

#Defines fields to update and the field to use as join field
Table = "Test2.csv"
Input = "Test.gdb/MHs"
csvFields = ['Location_Details', 'Elevation', 'Rim_to_Invert', 'Rim_to_Grade', 'Cover_Size', 'Wall_Material', 'Wall_Diam', 'Wall_Lining_Interior', 'Photo2_Link', 'MH_InspectReportLink'] #update table fields
featFields = ['LOCDESC', 'RIMELEV', 'RIMTOINVERT', 'RIMTOGRADE','COVERSIZE','WALLMAT','DIAMETER','LINERTYPE','HYPERLINK_PHOTO2','HYPERLINK_RPT']#fc field names
csvKey = "Manhole_Number"
featKey = "FACILITYID"
csvFields.insert(0, csvKey)
featFields.insert(0, featKey)
print "Your table contains the following fields to be updated: {}\n".format(str(csvFields))

#Process: Create dictionary to store values from the update table, iterate through values and store in UpdateDict
UpdateDict = {}

with arcpy.da.SearchCursor(Table, csvFields) as cursor:
   for row in cursor:
      UpdateDict[row[0]] = dict(zip(featFields[1:], row[1:]))
## debug print "You have created update dictionary 'UpdateDict': \n{}\n\n".format(UpdateDict)

MHNum = len(UpdateDict) # gets # of MHs to be updatedMHKeys = sorted(UpdateDict.keys()) # gets key values, i.e. MH numbers
MHKeys = UpdateDict.keys() #calls outer keys (MH numbers, which are join values) into a list of keys
MHVals = UpdateDict.values()#calls inner nested key:value pairs to a list

##debug print "Dictionary keys: {}\n\n Dictionary values: {}\n\n".format(str(MHKeys),str(MHVals))
print "You are updating fields for the following {} manholes: {}".format(MHNum, str(MHKeys))

#Process: Iterates through feature class attribute table and updates desired attributes
with arcpy.da.UpdateCursor(Input, featFields) as curs:
    i = 0 #attribute table row counter
    for row in curs:
        i += 1
        for f in MHKeys:
            if f == row[0]:
                z = MHKeys.index(f)#get index location in MHKeys
                for y in range(0,len(featFields)-1):
                    row[y+1] = MHVals[z][featFields[y+1]]#use z to pull corresponding value in MHVals to correct key in MHKeys
                print "Current MH: {} \nUpdating Values: {} \n\n".format(f, UpdateDict.values()[z])
                curs.updateRow(row)
            else:
                pass

print "Updated {} of {} rows.".format(MHNum, i)
print "Script completed."

rawDescription: Python nested update cursor with nested dictionary. There's got to be an easier way


code:
reobj = re.compile('(.*?)', re.IGNORECASE)
result = reobj.findall(body)

for link in result:
        print link[0] + link[1]

rawDescription: python regex groups

description: Python regex groups


code:
import fileinput

batch = ['1', '2', '3', '4', '5']
list = ['A', 'B', 'C']

for i in list:
    for j in batch:
        os.chdir("/" + i + "/folder_" + j + "/")

        file_name = "script.txt"
        MD = "TEXT"
        input_file = fileinput.input(file_name, inplace=1)
        for line in input_file:
            if line.startswith("YELLOW"):
                print line.strip() + str(MD)
            else:
                print line,
        input_file.close() # Strange how fileinput doesn't support context managers

rawDescription: Inserting text to end of line python

description: Inserting text to end of line python


code:
def generate_primes(n):
  from math import sqrt
  primes=[]
  potentialPrimes=range(2,n+1)
  prime=potentialPrimes[0]
  while prime<sqrt(n):
      primes.append(prime)
      potentialPrimes.remove(prime)
      for potential in potentialPrimes:
          if potential%prime==0:
              potentialPrimes.remove(potential)
      prime=potentialPrimes[0]

  for potential in potentialPrimes:
      primes.append(potential)
  for number in primes:
      print number

rawDescription: Trying to generate primes from 1 - n, but consistently is missing primes 3 and 5.?


code:
test_list = ["dog", "cat", 12, 34, "boop"]

for i, x in enumerate(test_list):
    globals()["item_" + str(i + 1)] = x

print item_1
print item_2
print item_3
print item_4
print item_5

rawDescription: How to create a number of items from a list in python

description: Create a number of items from a list in python


code:
def checkExists(fileName, index=0):
    print "checkExists(" + fileName + ")"

    if exists(fileName):
        splitPath = split(newFile)
        splitName = splitext(splitPath[1])
        newSplitName = splitName[0] + "-" + str(index)

        index += 1
        newName = splitPath[0] + "/" + newSplitName + splitName[1]

        print "newName = " + newName
        return checkExists(newName, index)   # recurse
    else:
        print "(else) fileName = " + fileName
        print "(else) fileName = " + str(type(fileName))
        print ""
        return fileName

rawDescription: python function not repeating

description: Python function not repeating


code:
html = """
<select id="my">
  <option id="1">1a</option>
  <option id="2">2bb</option>
</select>

<select id="my1">
  <option id="11">11a</option>
  <option id="21">21bb</option>
</select>
"""

from HTMLParser import HTMLParser

class MyParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.ids = []

    def handle_starttag(self, tag, attrs):
        if tag == 'select':
            self.ids.extend(val for name, val in attrs if name == 'id')

p = MyParser()
p.feed(html)
print p.ids  # ['my', 'my1']

rawDescription: Selecting a string in python

description: Selecting a string in python


code:
import maya.cmds as cmds
import random
import math
from functools import partial

    #Dictionary containing weapon names and presets - presets contain values for shot sliders, distance sliders and multipliers for spread calculation
GunDictionary = {}
GunDictionary["weapon"] = ["Pistol", "Shotgun", "SMG", "Sniper", "RPG"]
GunDictionary["weaponSelected"] = GunDictionary["weapon"][0]
GunDictionary["Pistol_preset"] = [(1,18,9), (10,50,25), (0.1)]
GunDictionary["Shotgun_preset"] = [(1,4,2), (10,50,25), (0.3)]
GunDictionary["SMG_preset"] = [(5,30,15), (10,50,25), (0.2)]
GunDictionary["Sniper_preset"] = [(1,3,2), (10,50,25), (0.05)]
GunDictionary["RPG_preset"] = [(1,2,1), (10,50,25), (0)]

    #Initial cleanup of UIs
if (cmds.window("Gun_Select", exists = True)):
    cmds.deleteUI("Gun_Select")

    #Initial cleanup of scene
cmds.select(all=True)
cmds.delete()

    #Fire button condition - creates wall
def goShoot(gunSelected, numOfShots, distance, *pArgs):
    print "Begin"
    weaponName = gunSelected
    cmds.deleteUI(weaponName)
    createWall()
    bulletSpread(GunDictionary["weaponSelected"], distance)

#Cancel UI2 - deletes UI2
def cancelShoot(*pArgs):
    print "cancel"
    weaponName = GunDictionary["weaponSelected"]
    cmds.deleteUI(weaponName)

#Cancel UI1 - deletes UI1
def cancelSelect(*pArgs):
    print "cancel"
    cmds.deleteUI("Gun_Select")

#Function to create wall
def createWall():
    wall = cmds.polyCube(h=10, w=15, d=1, name='wall')
    cmds.move(0,5,0, 'wall')

#Function to generate bullet spread
def bulletSpread(gunSelected, distance):
    weaponName = GunDictionary["weaponSelected"]
    multiplier = GunDictionary[weaponName + "_preset"][2]
    distance = distance
    newRange = distance * multiplier
    print newRange

    #Function to create drop-down menu in createSelectUI
def printNewMenuItem(item):
    print item
    GunDictionary["weaponSelected"] = item
    return GunDictionary["weaponSelected"]

    #Function to take weapon selected in UI1 and call UI2, also calls function to calculate bullet spread
def ui_refreshSelWeapon(fun, *args):
    createGunUI(GunDictionary["weaponSelected"])

    #UI allowing user to pick from a selection of Guns in a drop-down menu
def createSelectUI():
    cmds.window("Gun_Select")
    cmds.columnLayout(adjustableColumn=True)

    GunSelectCtrl = cmds.optionMenu(label='Gun', changeCommand=printNewMenuItem)
    for i in GunDictionary["weapon"]:
        cmds.menuItem(label=i)

    cmds.button(label = "Continue", command = ui_refreshSelWeapon)

    cmds.button(label = "Cancel", command = cancelSelect)

    cmds.showWindow("Gun_Select")

createSelectUI()

def refreshValues(*args):
    function = args[0]
    weapon = args[1]
    get01 = cmds.intSliderGrp(args[2],query=True, value=True)
    get02 = cmds.intSliderGrp(args[3],query=True, value=True)
    print get01, get02
    function(weapon, get01, get02)

weapon_uiDic = {}

#Called after gun select, allows user to choose the number of shots and distance from wall
def createGunUI(gunSelected):

    weaponName = GunDictionary["weaponSelected"]

    if cmds.window(weaponName, exists=True):
       cmds.deleteUI(weaponName)

    cmds.window(weaponName)
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Gun_Select")

    numBullets = GunDictionary[weaponName + "_preset"][0]
    distToTarget = GunDictionary[weaponName + "_preset"][1]

    weapon_uiDic["NumBulletsCtrl"] = cmds.intSliderGrp(label='Number of Shots',
                                                       minValue=numBullets[0], maxValue=numBullets[1], value=numBullets[2], field=True)

    weapon_uiDic["DistCtrl"] = cmds.intSliderGrp(label='Distance to Target (metres)',
                                                 minValue=distToTarget[0], maxValue=distToTarget[1], value=distToTarget[2], field=True)

    weapon_uiDic["fireButton"] = cmds.button(label = "Fire",  command = '')
    cmds.button(weapon_uiDic["fireButton"], e=1,
                                            c=partial(refreshValues,
                                                      goShoot,
                                                      GunDictionary["weaponSelected"],
                                                      weapon_uiDic["NumBulletsCtrl"],
                                                      weapon_uiDic["DistCtrl"]))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow(weaponName)

rawDescription: Maya Python - Connecting UI to functions


code: if "exam" in "example": print "yes!"

rawDescription: One Line 'If' or 'For'

description: One Line 'If' or 'For'


code:
import random
sequence = open('3seq.txt', 'r')
seq1, seq2, seq3 = sequence.readlines()
sequence.close()

x = 3

def get_random(seq1, seq2, seq3):
    length = len(seq1)

    rand1 = ''
    rand2 = ''
    rand3 = ''

    for i in range(length):
        x = random.randrange(length)
        rand1 += seq1[x]
        rand2 += seq2[x]
        rand3 += seq3[x]
    print rand1
    print rand2
    print rand3
    print

for j in xrange(x):  # 'x' being defined at the top.
    get_random(seq1, seq2, seq3)

rawDescription: Python- How to repeat a loop for new result each time?


code:
href="{{ 'http://www.link.com/blah/page.htm'|cy:'http://www.link.com/welsh/blah/page.htm' }}"

import re
print re.findall("'(http://(?:.*?))'", href)
# ['http://www.link.com/blah/page.htm', 'http://www.link.com/welsh/blah/page.htm']

rawDescription: How can I find multiple URLs within a string (href attribute)

description: Find multiple URLs within a string (href attribute)


code:
# Store a hash.
import bcrypt
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
store_in_db(user, hashed) #Where user is the user to load the hash for, and store_in_db does what it says on the tin.

# Check against an existing hash
import bcrypt
hashed = load_from_db(user) # (get the password of the user from database) Where user is the user to load the hash for, and load_from_db does what it says on the tin.
if bcrypt.hashpw(password, hashed) == hashed: # Where password is a plaintext password attempt.
        print "It matches"
else:
        print "It does not match"

rawDescription: Is it better to save/insert the hashed string in database table before saving the password?


code:
import subprocess
print args
p = subprocess.Popen(args)

rawDescription: How do I connect a Python and a C program?

description: Connect a Python and a C program


code:
import itertools

l = [1,2,3,4,5,6]
ts = 8

def psl(l, ts):
    for i in range(len(l)):
        for c in itertools.combinations(l, i):
            if sum(c) == ts:
                print list(c)

psl(l, ts)

rawDescription: Print all combinations of list that sum to a number


code:
def selection_sort(list):
    for index in range(0, len(list)):
        iSmall = index
        for i in range(index,len(list)):
            if list[iSmall] > list[i]:
                iSmall = i
        list[index], list[iSmall] = list[iSmall], list[index]
    return list

if __name__ == '__main__':
    print selection_sort([5,2,4,6,1,3,])

rawDescription: Selection Sort Python with no built ins

description: Selection Sort Python with no built ins


code:
class People:

    def __init__(self, fname=None, lname=None, age=None, gender=None):
        self.fname = fname
        self.lname = lname
        self.age = age
        self.gender = gender

def display(self):
    print self.fname

people = [People('John','W Cooper',23,'Male'),
           People('James','W Cooper',30,'Male'),
           People('Kate','W Cooper',20,'Female')]
f = open("abc.txt", "w")
for person in people:
    f.write( person.fname +","+ person.lname +","+ str(person.age) +","+ person.gender + '\n' )
    person.display()
f.close()

rawDescription: How to populate Objects in a an existing file

description: Populate Objects in a an existing file


code:
from twisted.web.server import Site  # Site is a server factory for HTTP
from twisted.web.resource import Resource
from twisted.internet import reactor

class PrintPostBody(Resource):  # Resources are what Site knows how to deal with
    isLeaf = True  # Disable child lookup

    def render_POST(self, request):  # Define a handler for POST requests
        print request.content.read()  # Get the request body from this file-like object
        return "" # Define the response body as empty

reactor.listenTCP(80, Site(PrintPostBody()))
reactor.run()

rawDescription: Failed to get the HTTP POST request body using Twisted


code:
class Bar(Foo):
    instances = []
    def __init__(self, a):
            print a
            Bar.instances.append(self)

inst_b1 = Bar(3)
inst_b2 = Bar('Hello World!!!')
print len(Bar.instances)
print Bar.instances

rawDescription: how to find instances of a class at runtime in python

description: Find instances of a class at runtime in python


rawDescription: Number in python - 010

description: Number in python - 010


code:
new_list = [str(ip) for ip in netaddr.IPSet(ip_list) & (netaddr.IPSet(banned_subnets))]
print new_list

rawDescription: Comparing a set of IP addresses against subnets using python and netaddr module


code:
def queue_get_all(q):
    items = []
    maxItemsToRetreive = 10
    for numOfItemsRetrieved in range(0, maxItemsToRetreive):
        try:
            if numOfItemsRetrieved == maxItemsToRetreive:
                break
            items.append(q.get_nowait())
        except Empty, e:
            break
    return items

rawDescription: Get all items from thread Queue

description: Get all items from thread Queue


code:
import unittest

class FakeTest(unittest.TestCase):
    cl_att = []

    def test_a1(self):

        self.assert_(True)
        self.cl_att.append('a1')
        print "cl_att:", self.cl_att

    def test_a2(self):

        self.assert_(True)
        self.cl_att.append('a2')
        print "cl_att:", self.cl_att

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

rawDescription: How to add/change a variable across django test functions

description: Add/change a variable across django test functions


code:
import time
import csv

with open('your_file.csv', 'w') as f:
  csv_writer = csv.writer(f)
  s = ''
  while True:
    s = raw_input("Enter 'D' to write a log event ('q' to quit): ")
    if s == 'D':
      t = time.ctime()
      csv_writer.writerow(['D press event', t])
      print 'logged a D press event at {}'.format(t)
    elif s == 'q':
      break
    elif s:
      print "You entered {}, that wasn't a D.. ;)".format(s)

rawDescription: Python as a CMD data logger for time (run continously)


code:
import json

x = json.loads("""["[Ljava.lang.Object;",[["Object0",{"key0": null, "coolKey1": "coolValue",
"notCoolKey2":"something"}],["Object1",{"key3": "value1", "key4": "nonCoolValue",
"Id":"importantId0", "someId":"importantId1"}],false,["Object3", {"key6": "value6",
"nonkey7": "supercoolValue"}]]]""")

print x[1][1][1]["Id"]
print x[1][1][1]["someId"]

rawDescription: How to extract one substring from one Java Object in python?

description: Extract one substring from one Java Object in python


code:
from sqlalchemy import Table
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import UpdateBase

class InsertFromSelect(UpdateBase):
    def __init__(self, table, select):
        self.table = table
        self.select = select

@compiles(InsertFromSelect)
def visit_insert_from_select(element, compiler, **kw):
    return "INSERT INTO %s %s" % (
        compiler.process(element.table, asfrom=True),
        compiler.process(element.select)
    )

def cloneTable(name, table, metadata):
    cols = [c.copy() for c in table.columns]
    constraints = [c.copy() for c in table.constraints]
    return Table(name, metadata, *(cols + constraints))

# test data
from sqlalchemy import MetaData, Column, Integer
from sqlalchemy.engine import create_engine
e = create_engine('sqlite://')
m = MetaData(e)
t = Table('t', m, Column('id', Integer, primary_key=True),
          Column('number', Integer))
t.create()
e.execute(t.insert().values(id=1, number=3))
e.execute(t.insert().values(id=9, number=-3))

# create temp table
temp = cloneTable('temp', t, m)
temp.create()

# copy data
ins = InsertFromSelect(temp, t.select().where(t.c.id>5))
e.execute(ins)

# print result
for r in e.execute(temp.select()):
    print r

rawDescription: creating a temporary table from a query using sqlalchemy orm

description: Creating a temporary table from a query using sqlalchemy orm


code:
def testf2():
    try:
        db = connectToMysqlDB(None)

        sql_statement = " ".join([
            "select d.* ",
            "from dr_snapshot d ",
            "order by d.PremiseID asc, d.last_read asc; "])

        sel_cur = db.cursor()
        rc = sel_cur.execute(sql_statement)

        loop_ok = True
        while loop_ok:
            meter_row = sel_cur.fetchone()
            if meter_row:
                yield meter_row
            else:
                loop_ok = False
    except ProgrammingError:
        print "Tried to read a cursor after it was already closed"
    finally:
        sel_cur.close()

rawDescription: Is there an advantage to a database access generator function?


code:
def text_processing( file_location ):
    import re
    file_variable = open( file_location )
    lines = file_variable.readlines()

    double_letter = re.compile(r'.*(.)\1.*', re.IGNORECASE)
    double_letter_list = []

    for line in lines:
        for word in line.split(" "):
            match = double_letter.match(word)
            if match:
                double_letter_list.append(match.group())

    print double_letter_list

rawDescription: Return words with double consecutive letters


code:
import pyaudio
import wave
import sys

# length of data to read.
chunk = 1024

# validation. If a wave file hasn't been specified, exit.
if len(sys.argv) < 2:
    print "Plays a wave file.\n\n" +\
          "Usage: %s filename.wav" % sys.argv[0]
    sys.exit(-1)

'''
************************************************************************
      This is the start of the "minimum needed to read a wave"
************************************************************************
'''
# open the file for reading.
wf = wave.open(sys.argv[1], 'rb')

# create an audio object
p = pyaudio.PyAudio()

# open stream based on the wave object which has been input.
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = wf.getframerate(),
                output = True)

# read data (based on the chunk size)
data = wf.readframes(chunk)

# play stream (looping from beginning of file to the end)
while data != '':
    # writing to the stream is what *actually* plays the sound.
    stream.write(data)
    data = wf.readframes(chunk)

# cleanup stuff.
stream.close()
p.terminate()

rawDescription: pyaudio help play a file

description: Pyaudio help play a file


code:
import numpy
mydescriptor = {'names': ('gender','age','weight'), 'formats': ('S1', 'f4', 'f4')}
a = numpy.array([('M',64.0,75.0),('F',25.0,60.0)], dtype=mydescriptor)
print a
a.shape
a.resize(3)
a.shape
print a
a[2] = ('X', 17.0, 61.5)
print a

rawDescription: Adding row to numpy recarray

description: Adding row to numpy recarray


code:
if smallest_num.is_integer():
   # print as integer
   print "The smallest is %d" % smallest_num
else:
   # print as float
   print "The smallest is %f" % smallest_num

rawDescription: How do I print a float as an integer?

description: Print a float as an integer


code:
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
import pdfminer

# Open a PDF file.
fp = open('/Users/me/Downloads/test.pdf', 'rb')

# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)

# Create a PDF document object that stores the document structure.
# Password for initialization as 2nd parameter
document = PDFDocument(parser)

# Check if the document allows text extraction. If not, abort.
if not document.is_extractable:
    raise PDFTextExtractionNotAllowed

# Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager()

# Create a PDF device object.
device = PDFDevice(rsrcmgr)

# BEGIN LAYOUT ANALYSIS
# Set parameters for analysis.
laparams = LAParams()

# Create a PDF page aggregator object.
device = PDFPageAggregator(rsrcmgr, laparams=laparams)

# Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)

def parse_obj(lt_objs):

    # loop over the object list
    for obj in lt_objs:

        # if it's a textbox, print text and location
        if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):
            print "%6d, %6d, %s" % (obj.bbox[0], obj.bbox[1], obj.get_text().replace('\n', '_'))

        # if it's a container, recurse
        elif isinstance(obj, pdfminer.layout.LTFigure):
            parse_obj(obj._objs)

# loop over all pages in the document
for page in PDFPage.create_pages(document):

    # read the page into a layout object
    interpreter.process_page(page)
    layout = device.get_result()

    # extract text from this object
    parse_obj(layout._objs)

rawDescription: How to extract text and text coordinates from a pdf file?

description: Extract text and text coordinates from a pdf file


code:
#!/usr/bin/env python
import dbus

bus = dbus.SessionBus()

if "im.pidgin.purple.PurpleService" in bus.list_names():
    purple = bus.get_object("im.pidgin.purple.PurpleService",
            "/im/pidgin/purple/PurpleObject",
            "im.pidgin.purple.PurpleInterface")

    print "Connected to the pidgin DBus."
    for conv in purple.PurpleGetIms():
        purple.PurpleConvImSend(purple.PurpleConvIm(conv), "Ignore this message.")

else:
    print "Could not find piding DBus service, make sure Pidgin is running."

rawDescription: Send and receive messages via (libpurple) messenger protocols


code:
tmp_str = '''
print "Hello World!"
print "another hello world!"
         '''

exec tmp_str

rawDescription: Python client server dilemma

description: Python client server dilemma


code:
list1 = [[x] for x in range(10)]
list2 = list1[:4]
list1[1][0] = 100
print list2

rawDescription: Creating a new list from elements of an other list, referencing the elements of the latter


code:
for line in hosts:
        print line.split()[1:]

rawDescription: python - get hostname values from each line /etc/hosts

description: Python - get hostname values from each line /etc/hosts


code:
with open('x.csv', 'rb') as f:
    g = codecs.EncodedFile(f, 'utf8', 'utf-16le', 'ignore')
    c = csv.reader(g)
    for row in c:
        print row
        # and if you want to use unicode instead of str:
        row = [unicode(cell, 'utf8') for cell in row]

rawDescription: utf-16-le BOM csv files

description: Utf-16-le BOM csv files


code:
import mechanize,cookielib
# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Open odesk site
r = br.open('https://www.odesk.com/login.php')
form = br.forms().next()  # the login form is unnamed...
print form.action
form['login'] = 'yourlogin'
form['password'] = 'yourpassword'
br.form = form
br.submit()

print br.geturl()
#your form data goes here
r = br.open(br.geturl() + 'new/')
print br.geturl()
br.select_form(name="jobsPost")  # the form is unnamed...
br.find_control(name="category").value = ['Writing & Translation']
br.find_control(name="subcategory").value = ['35']
br.form['title'] = 'Filling up a profile'
br.form['description'] = 'Require assistance in filing up our profile'
br.find_control(name="job_type").value = ['Fixed']
br.form['job_budget'] = '10'
br.form['job_finish_date'] = '10-14-2012'
br.find_control(name="visibility").value = ['private']
br.submit()

print br.geturl()

rawDescription: odesk API POST job with Python

description: Odesk API POST job with Python


code:
from BeautifulSoup import BeautifulSoup
html = "<html>ÄÄRITALO!</html>"
soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
print soup.contents[0].string

rawDescription: Getting international characters from a web page?


code:
# This just spoofs the view_config decorator.
def view_config(route=''):
    def dec(f):
        def wrapper(*args, **kwargs):
            print "route={0}".format(route)
            return f(*args, **kwargs)
        return wrapper
    return dec

# Apply this decorator to methods for which you want to call view_config with
# the class name. It will tag them. The metaclass will apply view_config once it
# has the class name.
def auto_view_config(f):
    f.auto_view_config = True
    return f

class AutoViewConfigMeta(type):
    def __new__(mcls, name, bases, dict_):
        #This is called during class creation. _dict is the namespace of the class and
        # name is it's name. So the idea is to pull out the methods that need
        # view_config applied to them and manually apply them with the class name.
        # We'll recognize them because they will have the auto_view_config attribute
        # set on them by the `auto_view_config` decorator. Then use type to create
        # the class and return it.

        for item in dict_:
            if hasattr(dict_[item], 'auto_view_config'):
                method = dict_[item]
                del method.auto_view_config # Clean up after ourselves.
                # The next line is the manual form of applying a decorator.
                dict_[item] = view_config(route=name)(method)

        # Call out to type to actually create the class with the modified dict.
        return type.__new__(mcls, name, bases, dict_)

class simpleObject(object):
    __metaclass__ = AutoViewConfigMeta

class Test(simpleObject):

    @auto_view_config
    def activateTheView(self):
        foo = 'bar'

        print foo

if __name__=='__main__':
    t = Test()
    t.activateTheView()

rawDescription: Why does "self" outside a function's parameters give a "not defined" error?


code:
import argparse

parser = argparse.ArgumentParser(prog='manager')
sub = parser.add_subparsers(dest='command')
sub.add_parser('create', help='Create a account')
sub.add_parser('delete', help='Delete a account')

print parser.parse_args(['create'])
# Namespace(command='create')

rawDescription: Python argparse static arguments

description: Python argparse static arguments


rawDescription: Calculating the upper 5% of my discrete distribution


rawDescription: Python bigram - foreign script

description: Python bigram - foreign script


code:
a = os.system("some command")
b = commands.getoutput("some command")
print "commands have run"
print "output of os.system:",a
print "output of commands.getoutput",b

rawDescription: Difference between os.system("cmd") and commands.getoutput("cmd") ...?


code:
baseNumber = 3000000L
#dataType = 'float64'#
numBytes = 1
dataType = 'S1'
for powers in arange(1,7):
  l1 = baseNumber*10**powers
  print('working with %d elements'%(l1))
  print('number bytes required %f GB'%(l1*numBytes/1e9))
  try:
    fp = numpy.memmap('testa.map',dtype=dataType, mode='w+',shape=(1,l1))
    #works
    print('works')
    del fp
  except Exception as e:
    print(repr(e))

"""
working with 30000000 elements
number bytes required 0.030000 GB
works
working with 300000000 elements
number bytes required 0.300000 GB
works
working with 3000000000 elements
number bytes required 3.000000 GB
works
working with 30000000000 elements
number bytes required 30.000000 GB
works
working with 300000000000 elements
number bytes required 300.000000 GB
IOError(28, 'No space left on device')
working with 3000000000000 elements
number bytes required 3000.000000 GB
IOError(28, 'No space left on device')

"""

rawDescription: creating realy huge scipy array

description: Creating realy huge scipy array


code:
max_value = max(x[0] for x in A)
print [x for x in A if x[0] == max_value]

rawDescription: Python: extract tuples with max/min n'th element from array of tuples


code:
import itertools
mapping = {'d':[1,2,3,4],'e':['1','2'], 'v':['3','4']}
line = "3 0FAK3e CL00v"
subs = (
    [(key, value) for value in mapping[key]]
    for key in mapping
    if key in line
    )
for combination in itertools.product(*subs):
    lineb = line
    for letter, number in combination:
        lineb = lineb.replace(str(letter), str(number))
    print lineb

rawDescription: Mutating list while making multiple replacements in a loop


code:
#!/usr/bin/env python

from random import randrange
import wx
import wx.lib.scrolledpanel

import time
import threading
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub

class GUI(wx.Frame):

    def __init__(self, parent, id, title):
        screenWidth = 800
        screenHeight = 450
        screenSize = (screenWidth, screenHeight)
        wx.Frame.__init__(self, None, id, title, size=screenSize)
        self.locationFont = locationFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.SIMPLE_BORDER)
        panel.SetupScrolling()
        panel.SetBackgroundColour('#FFFFFF')
        panel.SetSizer(sizer)
        mainSizer.Add(panel, 15, wx.EXPAND|wx.ALL)
        self.SetSizer(mainSizer)

        self.text_labels = []  # Stores the labels where server data is displayed

        pub.subscribe(self.OnNewLabels, "NEW_LABELS")

    def OnNewLabels(self, labels):
        locations = labels
        print locations
        if len(self.text_labels) < len(labels):
            new_labels_needed = len(labels) - len(self.text_labels)
            label = "(no data)"
            for i in range(new_labels_needed):
                sPanels = wx.Panel(self.panel)
                text = wx.StaticText(sPanels, -1, label)
                text.SetFont(self.locationFont)
                text.SetForegroundColour('#0101DF')
                self.sizer.Add(sPanels, 0, wx.ALL, 5)
                self.sizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 0)
                self.text_labels.append(text)
            self.sizer.Layout()
        k = 0
        for label in locations:
            self.text_labels[k].SetLabel(str(label))
            k=k+1

###############################
#
#

def InterfaceThread():
    while True:
        # get the info from the server
        mylist =[]
        i = randrange(10)
        for k in range(1,i+1):
            mylist.append(randrange(10))

        # Tell the GUI about them
        wx.CallAfter(pub.sendMessage, "NEW_LABELS", labels = mylist)
        time.sleep(5)

class ServerInterface():

    def __init__(self):
        interface_thread = threading.Thread(target = InterfaceThread, args = ())
        interface_thread.start()

#############
#

if __name__=='__main__':
    app = wx.App()
    frame = GUI(parent=None, id=-1, title="Test")
    frame.Show()
    server_interface = ServerInterface()
    app.MainLoop()

rawDescription: Updating the labels of the panels in a GUI using threading in wxPython


code:
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print elem.get_attribute("href")

rawDescription: Fetch all href link using selenium in python


code:
from itertools import islice

binary_map = dict(zip("AB", "10"))  # Equivalent to {"A": "1", "B": "0"}
string_map = {52: "Case 1", 53: "Case 2"}

with open("my_text_file") as f:
    binary_str = "".join(binary_map[x.split()[2]] for x in islice(f, 9, None))

binary_value = int(binary_string, 2)
print string_map[binary_value]

rawDescription: Python: create a decimal number out of entries from a text file


code:
a = "hello"
b = "olhel"
print sorted(a) == sorted(b)

rawDescription: match two strings with letters in random order in python

description: Match two strings with letters in random order in python


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

# load the page
driver.get("https://www.google.com/recaptcha/api2/demo")

# get the submit button
bt_submit = driver.find_element_by_css_selector("[type=submit]")

# wait for the user to click the submit button (check every 1s with a 1000s timeout)
WebDriverWait(driver, timeout=1000, poll_frequency=1) \
  .until(EC.staleness_of(bt_submit))

print "submitted"

rawDescription: Python Selenium: How to let human interaction in a automated script?


code:
import pandas as pd

begin_month = pd.Series([1, 19, 145, 32, 54])
end_month = pd.Series([19,45,32,54,99])

df = pd.DataFrame({"begin_month":begin_month, "end_month": end_month})

df['parity'] = df['begin_month'] == df['end_month'].shift()
df.ix[0,'parity'] = True

print df

rawDescription: how to avoid writing a gross for-loop on a pandas dataframe

description: Avoid writing a gross for-loop on a pandas dataframe