code:
import gdata.docs.service
import gdata.docs.data
from google.appengine.api import urlfetch

# get file from url
result = urlfetch.fetch('http://example.com/test.docx')
headers = result.headers
data = result.content

# authenticate client object
client = gdata.docs.service.DocsService()
client.ClientLogin('gmail', 'password')

# create MediaSource file wrapper
ms = gdata.MediaSource(file_handle=result.content,
content_type=headers['content-type'],
content_length=int(headers['content-length']))

# upload specific folder, return URL of doc
google_doc_name = 'title'
folder_uri = '/feeds/folders/private/full/folder:j7XO8SJj...'
entry = client.Upload(ms, google_doc_name, folder_or_uri=secret.G_FOLDER_URI)
edit_url = entry.GetAlternateLink().href

rawDescription: Python GAE upload to Google Docs

description: Python GAE upload to Google Docs


code:
self.browser.form.new_control('text','options[5]',{'value':''})
self.browser.form.fixup()
self.browser["options[5]"] = "New option."

rawDescription: Mechanize - Add to form post submission

description: Mechanize - Add to form post submission


code:
for result in cursor.stored_results():
    print(result.description)
    return result.fetchall()

rawDescription: Mysql-Python-Connector get columns names from select statement in stored procedure


code:
import os

from setuptools import setup
from setuptools.command.build_py import build_py

def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)

class my_build_py(build_py):
    def run(self):
        self.my_outputs = []
        if not self.dry_run:
            target_dir = os.path.join(self.build_lib, "install-entry-missing")
            self.mkpath(target_dir)
            output = os.path.join(target_dir, "my_file.txt")
            touch(output)
            self.my_outputs.append(output)
        build_py.run(self)

    def get_outputs(self):
        outputs = build_py.get_outputs(self)
        outputs.extend(self.my_outputs)
        return outputs

setup_args = dict(
    name='install-entry-missing',
    version='1.0.0',
    description='',
    author='author',
    author_email='author@example.com',
    packages = ["install-entry-missing"],
    cmdclass={'build_py': my_build_py}
)

if __name__ == '__main__':
    setup(**setup_args)

rawDescription: pip install misses some generated files when writing installed-files.txt

description: Pip install misses some generated files when writing installed-files.txt


code:
def trace(prime):
    def sub(paths):
        result = []
        """Increases path depth by one"""
        for path in paths:
            pathResult = []
            for parentOfLast in asc(path[len(path)-1]):
                pathResult.append(path + [parentOfLast])
            #print("Path: " + str(pathResult))
            result += (pathResult)
        return result
    result = [[prime]]
    while len(result[0]) < len(str(prime)):
        result = sub(result)
    return result

rawDescription: Recursive Function For Tracing Deletable Primes - Python 3


code:
import functools

def conditional_memoize(fn):
    cache = {}

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        key = args + tuple(sorted(kwargs.iteritems()))
        if key in cache:
            return cache[key]
        result, flag = fn(*args, **kwargs)
        if flag:
            cache[key] = result
        return result

    return wrapper

rawDescription: Conditional memoization?

description: Conditional memoization


code:
numTimes = dict()
target = (len(a)//2)+1

for ele in a:
    if ele not in numTimes:
        numTimes[ele] = 1
    else:
        numTimes[ele] +=1

    if numTimes[ele] == target:
        return ele

rawDescription: Finding max element in a list

description: Finding max element in a list


code:
import re

s = re.sub(r"\bnot\b", "NOT", s)

rawDescription: Unexpected behaviour of string.replace() in Python


code:
Base = declarative_base()

node_to_node = Table("node_to_node", Base.metadata,
    Column("left_node_id", Integer, ForeignKey("node.id"), primary_key=True),
    Column("right_node_id", Integer, ForeignKey("node.id"), primary_key=True)
)

class Node(Base):
    __tablename__ = 'node'
    id = Column(Integer, primary_key=True)
    label = Column(String)
    right_nodes = relationship("Node",
                        secondary=node_to_node,
                        primaryjoin=id==node_to_node.c.left_node_id,
                        secondaryjoin=id==node_to_node.c.right_node_id,
                        backref="left_nodes"
    )

rawDescription: Create many to many on one table

description: Create many to many on one table


code:
INSERT_INTO_HOMEFEED_BATCH = 10000

@shared_task
def insert_into_homefeed(photo_id, user_id, index=0):
    # Grab the list of all follower IDs from Redis for user_id.
    r_server = redis.Redis(connection_pool=pool)

    range_limit = index + INSERT_INTO_HOMEFEED_BATCH - 1 # adjust for zero-index

    followers_list_batch = r_server.lrange("followers:%s" % (user_id), index, range_limit)

    if not followers_list_batch:
        return # zero followers or no more batches

    # Now for each follower_id in followers_list_batch, find their homefeed key
    # in Redis and insert the photo_id into that homefeed list.
    for follower_id in followers_list:
        homefeed_list = r_server.lpush("homefeed:%s" % (follower_id), photo_id)

    insert_into_homefeed.delay(photo_id, user_id, range_limit + 1)

rawDescription: Django, Celery, Redis, RabbitMQ: Chained Tasks for Fanout-On-Writes


code: app = webapp2.WSGIApplication([('/blog/?', BlogPage), ('/blog/newpost', NewPost)], debug=True)

rawDescription: GAE multiple WSGI files for multiple URL handlers


code:
def read_file(list_of_lists):
    """Reads a file and returns a 2D list containing the pattern.
    O = Alive
    . = Dead
    """
    end_list = []
    for A_List in list_of_lists:
        sub_list = []
        for A_String in A_List:
            for item in A_String:
            #Adding 0's and 1's to sub_list when given the right input
                if item == '.':
                    sub_list.append(0)
                elif item == 'O':
                    sub_list.append(1)
            #Adding the sub
            end_list.append(sub_list)
    return end_list

rawDescription: Altering values of a list in a list

description: Altering values of a list in a list


code: return "".join([str(x) + "\n" for x in self.bars])

rawDescription: Returning individual string representation of all objects in list of objects


code:
def save_c(v):
    print("c :" + v)

class Writer(object):
    @classmethod
    def save(cls, t, v):
        cls.writers[t](cls, v)

    @classmethod
    def save_a(cls, v):
        print("a :" + v)

    @staticmethod
    def save_b(v):
        print("b :" + v)

    def _wrapper(writer):
        if isinstance(writer, classmethod):
            return type('ClassmethodWrapper', (object,), {'__call__':
                        lambda self, cls, v: writer.__func__(cls, str(v))})()
        elif isinstance(writer, staticmethod):
            return type('StaticmethodWrapper', (object,), {'__call__':
                        lambda self, cls, v: writer.__func__(str(v))})()
        else:
            return type('VanillaWrapper', (object,), {'__call__':
                        lambda self, cls, v: writer(str(v))})()

    writers = list(map(_wrapper, (save_a, save_b, save_c)))

if __name__ == "__main__":
    Writer.save(0, [1, 2, 3])  # -> a :[1, 2, 3]
    Writer.save(1, [4, 5, 6])  # -> b :[4, 5, 6]
    Writer.save(2, [7, 8, 9])  # -> c :[7, 8, 9]

rawDescription: How to reference a class method in a class attribute?

description: Reference a class method in a class attribute


code:
lst = [(1, [32, 432, 54]), (1, [43, 54, 65]), (2, [2, 43, 54]), (2, [1, 5, 6])]
import itertools
[(key,) + tuple(v for k, v in grp)
    for key, grp in itertools.groupby(lst, key=lambda x: x[0])]

rawDescription: Python: Join lists of lists by the first element


code:
class Thing(Base):
    __tablename__ = 'things'
    id = Column(Integer(), primary_key=True)
    raw_data = Column(String())

    @property
    def data(self):
        # add some checking here too
        return json.loads(self.raw_data)

    @data.setter
    def data(self, value):
        # dito
        self.raw_data = json.dumps(value)

rawDescription: SQLAlchemy column synonym with different type


code:
if not all(self.shape):
    # How to determine this better?
    is_reduction = False
    try:
        is_reduction = not isinstance(f(_EMPTY_SERIES), Series)
    except Exception:
        pass

    if is_reduction:
        return Series(NA, index=self._get_agg_axis(axis))
    else:
        return self.copy()

rawDescription: Pandas: why does DataFrame.apply(f, axis=1) call f when the DataFrame is empty?


rawDescription: How can I sort a Python dictionary sort by key without using Ordereddict?

description: Sort a Python dictionary sort by key without using Ordereddict


code:
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

rawDescription: How to use Python's groupby

description: Use Python's groupby


code:
class Node(object):
    def __init__(self, letter='', final=False):
        self.letter = letter
        self.final = final
        self.children = {}
    def __contains__(self, letter):
        return letter in self.children
    def get(self, letter):
        return self.children[letter]
    def add(self, letters, n=-1, index=0):
        if n < 0: n = len(letters)
        if index >= n: return
        letter = letters[index]
        if letter in self.children:
            child = self.children[letter]
        else:
            child = Node(letter, index==n-1)
            self.children[letter] = child
        child.add(letters, n, index+1)

def load_dictionary(path):
    result = Node()
    for line in open(path, 'r'):
        word = line.strip().lower()
        result.add(word)
    return result

words = load_dictionary('dictionary.txt')

rawDescription: most efficient data structure for a read-only list of strings (about 100,000) with fast prefix search

description: Most efficient data structure for a read-only list of strings (about 100,000) with fast prefix search


code:
import datetime

d = datetime.datetime(2008, 1, 1)
end = datetime.datetime(2009, 1, 1)

while d <= end:
    print(d.strftime("%Y%m%d%H%M%S"))
    d += datetime.timedelta(hours=6)

rawDescription: Convert a Time list to ISO Format Python


code:
sys_stdout  = sys.stdout
sys.stdout = open(...)
...
sys.stdout = sys_stdout

rawDescription: I need to overwrite the stdout for one line, and then revert the change


code:
with open("out.csv", "w") as f:
  writer = csv.writer(f) #Uses Excel dialect by default
  for thing in things:
    #...
    EG = la.eigvals(H)
    EGr = EG.real
    writer.writerow(EGr)

rawDescription: Python: How do I write a xls file line-by-line through a loop?


code: re.sub(r'\bchem\b', r' chemistry ', str)

rawDescription: Regular expression to match a word while preserving end of line


code:
with open("log.txt") as infile:
    for line in infile:
        do_something_with(line)

rawDescription: Read large text files in Python, line by line without loading it in to memory


code:
class Game(db.Model):
  players = db.ListProperty()

game1.players = ['tom', 'bob']
game2.players = ['joe', 'tom']

# this query will match all games where tom is one of the players
query = Game.all().filter('players =', 'tom')

rawDescription: Google app engine gql query two properties with same string


code:
import wx
mod = dir(wx)
[item for item in mod if item.startswith('ICON_')]

rawDescription: list of wx.ICON_ options in wxpython

description: List of wx.ICON_ options in wxpython


code:
import numbers

def strip_empty_structs(struct):
    def keep(v):
        return isinstance(v, numbers.Number) or v

    if type(struct) == list:
        struct = [strip_empty_structs(s) for s in struct]
        struct = filter(keep, struct)
    if type(struct) == dict:
        for k, v in struct.iteritems():
            struct[k] = strip_empty_structs(v)
        struct = dict(
            (
                (k, v) for k, v in struct.iteritems()
                if keep(v)
            )
        )

    return struct

rawDescription: Deep remove empty lists and values from

description: Deep remove empty lists and values from


code:
text = """ name = Kurradu
 image =
 image_size =
 producer = P. Kiran
 writer =
 starring = Varun Sandesh,Neha Sharma
 music = Achu
 cinematography = Sarvesh Murari
 editing =
 distributor =
 released = 12 November 2009
 studio =
 country = IND"""

d = dict(map(str.strip, line.split('=', 1)) for line in text.splitlines())

# d is now:

{'cinematography': 'Sarvesh Murari',
 'country': 'IND',
 'distributor': '',
 'editing': '',
 'image': '',
 'image_size': '',
 'music': 'Achu',
 'name': 'Kurradu',
 'producer': 'P. Kiran',
 'released': '12 November 2009',
 'starring': 'Varun Sandesh,Neha Sharma',
 'studio': '',
 'writer': ''}

rawDescription: Convert text into dict

description: Convert text into dict


rawDescription: python join list 2 to list 1 line by line

description: Python join list 2 to list 1 line by line


code:
class Student(models.Model):
    name = models.CharField(max_length=30)
    courses = models.ManyToManyField('Course')

class Course(models.Model):
    # Yoga, Spanish, French, etc.
    name = models.CharField(max_length=30)

class Score(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    points = models.IntegerField(max_length=4)
    course = models.ForeignKey('Course')
    student = models.ForeignKey('Student')

rawDescription: Creating a Foreign Key in Django Model

description: Creating a Foreign Key in Django Model


rawDescription: Python regular expression remove period from number at end of sentence


code:
aString = 'abcd'
newString =''
colors= [0.0, 1.0, 0.5, 1.0]
for i in aString:
    newString = newString + '%s'%(colors.pop(0)*255,i)

response = HttpResponse(newString)

rawDescription: HTML Newbie Question: Colored Background for Characters in Django HttpResponse


rawDescription: Creating class objects in a function and retaining them


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

#Create radius and theta arrays, and a 2d radius/theta array
radius = np.linspace(0.2,0.4,51)
theta = np.linspace(0,2*np.pi,51)
R,T  = np.meshgrid(radius,theta)

#Calculate some values to plot
Zfun = lambda R,T: R**2*np.cos(T)
Z = Zfun(R,T)

#Create figure and polar axis
fig = plt.figure()
ax = fig.add_subplot(111, polar = True)

ax.pcolor(T,R,Z)    #Plot calculated values

#Plot thick red section and label it
theta = np.linspace(0,np.pi/4,21)
ax.plot(theta,[1.23 for t in theta],color='#AA5555',linewidth=10)   #Colors are set by hex codes
ax.text(np.pi/8,1.25,"Text")

ax.set_rmax(1.25)   #Set maximum radius

#Turn off polar labels
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)

rawDescription: Matplotlib: draw a series of radial lines on PolarAxes


code:
@app.route('/errorform', methods=['GET', 'POST'])
def errorform():
    form = ErrorForm()
    logs = str(open(file, "r").readlines()[int(file_len(file))]).rstrip()
    if form.validate_on_submit():
        if form.includeLogs.data == False:
            return redirect('mailto:test@mail.com?subject=Feedback&body=<insert your message here>')
        else:
            return redirect('mailto:test@mail.com?subject=Feedback&body=<insert your message here> \n\n Logs: %s' % (logs))
    return render_template('main.html', form=form, show_results=0, page = 'errorform')

rawDescription: webbrowser.open('mailto:....') launches mail client in local execution environment but not on production server

description: Webbrowser.open('mailto:....') launches mail client in local execution environment but not on production server


code:
import math
x = int(input("How many cities? "))
a = math.factorial(x)
print("For", x, "cities, there are", a, "possible routes")

rawDescription: Traveling Salesman assignment

description: Traveling Salesman assignment


rawDescription: Python: Number of the Week in a Month

description: Python: Number of the Week in a Month


code:
def get_subpackages(module):
    dir = os.path.dirname(module.__file__)
    def is_package(d):
        d = os.path.join(dir, d)
        return os.path.isdir(d) and glob.glob(os.path.join(d, '__init__.py*'))

    return filter(is_package, os.listdir(dir))

rawDescription: Python: Finding all packages inside a package


code:
from datetime import datetime
d = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
d  # d is a string
datetime.strptime(d, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %I:%M:%S %p")

rawDescription: Conversion of 24 hours date/time to 12 hours format and vice-versa


code:
def templater(template,function,message):
    template = template.split('function')
    for i,f in enumerate(template):
        if function in f:
            template[i] = f.replace('[msg]',message)
    return 'function'.join(template)

rawDescription: search and replace using python

description: Search and replace using python


code:
import Image
import numpy as np

im = Image.open('test.png')
im = im.convert('RGBA')

data = np.array(im)   # "data" is a height x width x 4 numpy array
red, green, blue, alpha = data.T # Temporarily unpack the bands for readability

# Replace white with red... (leaves alpha values alone...)
white_areas = (red == 255) & (blue == 255) & (green == 255)
data[..., :-1][white_areas.T] = (255, 0, 0) # Transpose back needed

im2 = Image.fromarray(data)
im2.show()

rawDescription: Python: PIL replace a single RGBA color

description: Python: PIL replace a single RGBA color


code:
with open("file", "rb+") as file:
    file.write(b"...")

rawDescription: open file for random write without truncating?

description: Open file for random write without truncating


code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLCDNumber
from random import randint

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initui()

    def initui(self):
        self.lcd = QLCDNumber(self)

        button = QPushButton('Generate', self)
        button.resize(button.sizeHint())

        layout = QVBoxLayout()
        layout.addWidget(self.lcd)
        layout.addWidget(button)

        self.setLayout(layout)
        button.clicked.connect(self.handleButton)

        self.setGeometry(300, 500, 250, 150)
        self.setWindowTitle('Rand Integer')
        self.show()

    def handleButton(self):
        self.lcd.display(self.randomint())

    def randomint(self):
        random = randint(2, 99)
        return random

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())

rawDescription: PyQt5 button to run function and update LCD


code:
import math, sys, os, pygame, random, time

pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption('Example')

black = ( 0, 0, 0)

def shapeBlit():
    screen.fill(white)
    smiley = pygame.image.load('picture.jpg')
    x = random.randint(1,100)
    y = random.randint(1,100)
    screen.blit(smiley,(x,y))

c = 0

done = False
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop

    screen.fill(black)
    while c == 0:
        shapeBlit()
        pygame.display.flip()
        time.sleep(5)
pygame.quit()

rawDescription: Multiple moving shapes in pygame?

description: Multiple moving shapes in pygame


code:
import numpy as np
from scipy.ndimage.filters import gaussian_filter

def kl(p, q):
    p = np.asarray(p, dtype=np.float)
    q = np.asarray(q, dtype=np.float)

    return np.sum(np.where(p != 0, p * np.log(p / q), 0))

def smoothed_hist_kl_distance(a, b, nbins=10, sigma=1):
    ahist, bhist = (np.histogram(a, bins=nbins)[0],
                    np.histogram(b, bins=nbins)[0])

    asmooth, bsmooth = (gaussian_filter(ahist, sigma),
                        gaussian_filter(bhist, sigma))

    return kl(asmooth, bsmooth)

rawDescription: KL ( Kullback-Leibler) distance with histogram smoothing in Python


code:
MyModuleHandler = logging.handlers.RotatingFileHandler('MyModule.log', maxBytes= 5000000, backupCount=5)
MyModuleHandler.setFormatter(formatter)
MyModuleHandler.setLevel(logging.DEBUG)

specialHandler = logging.handlers.RotatingFileHandler('special.log', maxBytes= 5000000, backupCount=5)
specialHandler.setFormatter(formatter)
specialHandler.setLevel(logging.INFO)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)

logging.getLogger('MyModule.special').setLevel(logging.DEBUG)
logging.getLogger('MyModule.special').addHandler(specialHandler)
logging.getLogger('MyModule.special').propagate = False

logging.getLogger('MyModule').addHandler(console)
logging.getLogger('MyModule').setLevel(logging.DEBUG)
logging.getLogger('MyModule').addHandler(MyModuleHandler)

rawDescription: Python logging - excluding submodule

description: Python logging - excluding submodule


code:
import random
import time
import wx

from threading import Thread
from wx.lib.pubsub import Publisher

########################################################################
class TestThread(Thread):
    """Test Worker Thread Class."""

    #----------------------------------------------------------------------
    def __init__(self, fname, sleepAmt):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.fname = fname
        self.sleepAmt = sleepAmt
        self.start()    # start the thread

    #----------------------------------------------------------------------
    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        time.sleep(self.sleepAmt)
        msg = "%s finished in %s seconds!" % (self.fname, self.sleepAmt)
        wx.CallAfter(Publisher().sendMessage, "update", msg)

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        panel = wx.Panel(self, wx.ID_ANY)
        self.updateText = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        self.btn = btn = wx.Button(panel, label="Start Thread")

        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.updateText, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        # create a pubsub receiver
        Publisher().subscribe(self.updateDisplay, "update")

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Runs the thread
        """
        for i in range(40):
            fname = "test%s.txt" % i
            secs = random.choice(range(3, 15))
            TestThread(fname, secs)

    #----------------------------------------------------------------------
    def updateDisplay(self, msg):
        """
        Receives data from thread and updates the display
        """
        data = msg.data + "\n"
        self.updateText.WriteText(data)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

rawDescription: wxpython threading textctrl delay

description: Wxpython threading textctrl delay


code: [val for pair in zip(l1, l2) for val in pair]

rawDescription: Interleaving two lists in Python

description: Interleaving two lists in Python


code:
class myArray(np.ndarray):
    def __new__(cls, *args, **kwargs):
        this = np.array(*args, **kwargs)
        this = np.asarray(this).view(cls)
        return this

    def __array_finalize__(self, obj):
        if obj.ndim == 4:
            self.myShape = (self.shape[0]*self.shape[1], self.shape[2]*self.shape[3])
        else:
            self.myShape = self.shape

    def myStuff(self):
        self = self.reshape(self.myShape)

rawDescription: Python - How to wrap the np.ndarray class?


code:
indices = random.sample(range(len(a)), k)   # k < N
b = a[indices]

rawDescription: Find whether a numpy array is a subset of a larger array in Python


code:
from scipy.integrate import quad
quad(Disk_cv.pdf, 0, 0.804)

rawDescription: Why does scipy.stats.rv_continuous choose the upper bound far too many times?


code:
items = sorted(d.items())

dict(enumerate([' '.join(b for a, b in items[:3])] + [b for a, b in items[3:]]))

rawDescription: Collapsing dict keys into one key: Python


code:
from django.db.models import ImageField
from wherever.mymodels import Pictures
...
field = Pictures._meta.get_field("image")
assertTrue(isinstance(field, ImageField))

rawDescription: django test model field

description: Django test model field


code:
from collections import Counter
counts = Counter(d.check(word) for (word,pos) in tokenizer(text))
good, bad = counts[True], counts[False]

rawDescription: Pythonic method of increasing two counters depending on boolean check


code:
from selenium import webdriver

driver = webdriver.Firefox()
while True:
    driver.get("http://www.nbe.gov.in/asr/neet_pdf/")

    if 'NEET-PG' in driver.title:
        break

rawDescription: How to write a python script to keep trying a webpage until it opens

description: Write a python script to keep trying a webpage until it opens


code:
vowels = 'aeiou'
sentence = 'python is fun'
new_words = []

for word in sentence.split():
  if word[0].lower() in vowels:
    new_words.append(word+'way')
  else:
    new_words.append(word[1:3]+word[0]+word[3:]+'gar')

' '.join(new_words)

'ytphongar isway unfgar'

rawDescription: Creation of a Function that transforms sentence in Python


code: x = random.uniform(-1,1)

rawDescription: How to make python random.random() incorporate negatives

description: Make python random.random() incorporate negatives


code:
from bs4 import BeautifulSoup
readfile = open("html.parser",'r')
soup = BeautifulSoup(readfile)

tables = soup.findChildren('table')

count = 0
for i in soup.find_all("font", {"size": "1"}):
    value = i.find_next_sibling("font", {"size": "2"})
    if value is not None and count < 4:
       print (i.text, ":", value.text if value else "No Value")
       count = count + 1

rawDescription: How to extract specific data from HTML files?

description: Extract specific data from HTML files


code:
oranges = [plane for plane in self.landings.itervalues()
           if plane.is_orange()]

rawDescription: extract from python dictionary

description: Extract from python dictionary


code:
with EXPR as VAR:
   BLOCK

#translates to:

mgr = (EXPR)
exit = type(mgr).__exit__  # Not calling it yet
value = type(mgr).__enter__(mgr)
exc = True
try:
    try:
        VAR = value  # Only if "as VAR" is present
        BLOCK
    except:
        # The exceptional case is handled here
        exc = False
        if not exit(mgr, *sys.exc_info()):
            raise
        # The exception is swallowed if exit() returns true
finally:
    # The normal and non-local-goto cases are handled here
    if exc:
        exit(mgr, None, None, None)

rawDescription: Confused about Python's with statement

description: Confused about Python's with statement


code:
Example
import time
import mysql.connector

cnx = mysql.connector.connect(user='raspberryPi_1234',
                              password='foobar',
                              host='mysql.example.com',
                              database='voltsandamps',
                              autocommit=true,
                              compress=true)  # compress the connection

cursor = cnx.cursor(prepared=True)            # this is what SQLAlchemy is missing

stmt = "INSERT INTO power_readings VALUES (" + ",".join(126*["?"]) + ")"

while true:
    cursor.execute(stmt, getPowerReadings())
    time.sleep(2)

rawDescription: Connection compression with sqlalchemy

description: Connection compression with sqlalchemy


rawDescription: GetAuthSubToken returns None

description: GetAuthSubToken returns None


code:
source
# producer adds to fixed-sized list; scanner uses them

import logging, multiprocessing, sys, time

def producer(objlist):
    '''
    add an item to list every sec; ensure fixed size list
    '''
    logger = multiprocessing.get_logger()
    logger.info('start')
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            return
        msg = 'ding: {:04d}'.format(int(time.time()) % 10000)
        logger.info('put: %s', msg)
        del objlist[0]
        objlist.append( msg )

def scanner(objlist):
    '''
    every now and then, run calculation on objlist
    '''
    logger = multiprocessing.get_logger()
    logger.info('start')
    while True:
        try:
            time.sleep(5)
        except KeyboardInterrupt:
            return
        logger.info('items: %s', list(objlist))

def main():
    logger = multiprocessing.log_to_stderr(
            level=logging.INFO
    )
    logger.info('setup')

    # create fixed-length list, shared between producer & consumer
    manager = multiprocessing.Manager()
    my_objlist = manager.list( # pylint: disable=E1101
        [None] * 10
    )

    multiprocessing.Process(
        target=producer,
        args=(my_objlist,),
        name='producer',
    ).start()

    multiprocessing.Process(
        target=scanner,
        args=(my_objlist,),
        name='scanner',
        ).start()

    logger.info('running forever')
    try:
        manager.join() # wait until both workers die
    except KeyboardInterrupt:
        pass
    logger.info('done')

if __name__=='__main__':
    main()

rawDescription: How to list Processes started by multiprocessing Pool?

description: List Processes started by multiprocessing Pool


code:
for i,listed in enumerate(open('input.txt')):
    sentence = listed.lower()
    a = sentence.count('a')
    # ... etc.

rawDescription: Finding a Word in a File

description: Finding a Word in a File


code:
import numpy
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from mpl_toolkits.axes_grid1 import make_axes_locatable

#Generate data
delta = 0.025

x = numpy.arange(-5.0, 5.0, delta)
y = numpy.arange(-5.0, 5.0, delta)

X, Y = numpy.meshgrid(x, y)

Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

#Plot
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1,  aspect='equal')

PC = ax1.pcolor(X, Y, Z)
CF = ax1.contour(X, Y, Z, 50, colors = "black")

plt.xlim(-4.0, 4.0)
plt.ylim(-2.0, 2.0)

divider = make_axes_locatable(ax1)
cax1 = divider.append_axes("right", size="5%", pad=0.05)

cbar = plt.colorbar(PC, cax = cax1)
cbar.add_lines(CF)

plt.show()

rawDescription: Matplotlib: same height for colorbar as for plot


code:
def ls(directory):
    """full-featured solution, via wrapping find"""
    import os
    files = os.popen4('find %s' % directory)[1].read().rstrip().split('\n')
    files.remove(directory)
    n = len(directory)
    if directory[-1] != os.path.sep:
        n += 1
    files = [f[n:] for f in files] # remove dir prefix
    return [f for f in files if os.path.sep not in f] # remove files in sub-directories

rawDescription: Workaround OSError with os.listdir

description: Workaround OSError with os.listdir


code:
read_file = open("file.txt", "r")
data = read_file.read()
read_file.close()
file_content = re.sub("\d+", "", data)
word = your_word
if word in file_content:
   newfile = open("newfile.txt", "w")
   print >> newfile, word
   newfile.close()

rawDescription: Read and search text file in Python?

description: Read and search text file in Python


code:
most_popular_posts = Post.objects.all().annotate(
    popularity=Count('votes__id', distinct=True)
).select_related('thread').distinct('thread_id').order_by(
    '-thread_id', '-popularity'
)

rawDescription: Eliminating Queries in Django

description: Eliminating Queries in Django


code: np.random.normal(0, 0.5)

rawDescription: How to add random noise to a signal using NumPy?

description: Add random noise to a signal using NumPy


code:
l = [1,2,3]
str(l) is str(l)
str(l) == str(l)

rawDescription: Why str(None) is not str(None)?

description: Why str(None) is not str(None)


code:
df = pd.DataFrame(np.random.randn(36, 3))
df.plot(drawstyle="steps", linewidth=2)
df.plot(drawstyle="steps-post", linewidth=2)

rawDescription: Pandas DataFrame step plot: where="post"


code:
from Crypto.Cipher import AES
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = "The answer is no"
ciphertext = obj.encrypt(message)
ciphertext
obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
obj2.decrypt(ciphertext)

rawDescription: How do I code a data encryption program using Python?

description: Code a data encryption program using Python


code:
def test_get_ticket(self):
    with self.app.test_request_context('/?main_id=14522&user_id=82'):
        methodOutput = brain_get_ticket.get_ticket(
            {'main_id': {1: 0}, 'status': {'Closed': 0},
             'available': {'False': 0}},
            "main_id, status, available", ['main_id', 'status', 'available'])
        correct_return_output = dict(
            to_be_working_on_last_id=0,
            to_be_working_on_id=6,
            information={'status': {'Closed': 1},
                         'available': {'False': 1},
                         'main_id': {1: 1}})
        self.assertEquals(
            json.loads(methodOutput.data),
            correct_return_output,
            "output was: {!r}, it should be {!r}".format(
                methodOutput.data, correct_return_output))

rawDescription: how to use a jsonfiy object in assertions?

description: Use a jsonfiy object in assertions


code:
import numpy as np
from scipy import optimize
from matplotlib import pyplot as plt
small = 1e-30
def wealth_evolution(price, wealth=10, rate=0.01, q=1, realEstate=0.1, prev_price=56):
    sum_wantedEstate = 100
    for delta in range(1,4):
        z = rate - ((price-prev_price) / (price + q / rate))
        k = delta * np.divide(1.0, float(np.maximum(small, z)))
        wantedEstate = (wealth / (price + q / rate)) * np.minimum(k, 1) - realEstate
        sum_wantedEstate += wantedEstate
    return sum_wantedEstate

price_range = np.linspace(0,10000,10000)
we = [wealth_evolution(p) for p in price_range]

plt.plot(price_range,we)
plt.xlabel('price')
plt.ylabel('wealth_evolution(price)')
plt.show()

rawDescription: Python scipy fsolve works incorrectly

description: Python scipy fsolve works incorrectly


code:
from suds.sax.text import Raw
import suds.client as sudscl

client = sudscl.Client('http://host/ws.wsdl', location='http://host/ws')

session = 'test123'
options = Raw('<rawXML><Item>Foobar</Item></rawXML>')

result = client.service.ExecuteSearch(session, options)

rawDescription: Python: Sending Raw XML via Suds (jurko)


code:
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('files', nargs='*')
parser.parse_args(['@filenames.txt'])

rawDescription: Argparse pattern where input filenames are given through a file or a list of filenames on the command line


rawDescription: Python Pandas printing out values of each cells


code:
from scrapy import project, signals
from scrapy.conf import settings
from scrapy.crawler import CrawlerProcess
from scrapy.xlib.pydispatch import dispatcher
from multiprocessing.queues import Queue
from multiprocessing import Process

class CrawlerWorker(Process):
    def __init__(self, spider, results):
        Process.__init__(self)
        self.results = results

        self.crawler = CrawlerProcess(settings)
        if not hasattr(project, 'crawler'):
            self.crawler.install()
        self.crawler.configure()

        self.items = []
        self.spider = spider
        dispatcher.connect(self._item_passed, signals.item_passed)

    def _item_passed(self, item):
        self.items.append(item)

    def run(self):
        self.crawler.crawl(self.spider)
        self.crawler.start()
        self.crawler.stop()
        self.results.put(self.items)

# The part below can be called as often as you want
results = Queue()
crawler = CrawlerWorker(MySpider(myArgs), results)
crawler.start()
for item in results.get():
    pass # Do something with item

rawDescription: Running Scrapy tasks in Python

description: Running Scrapy tasks in Python


code: dataStr = ',ff,fe,d0,ea'.replace(',', r'\x').decode('string-escape')

rawDescription: Byte string to long value

description: Byte string to long value


code:
import pandas as pd
df = pd.DataFrame([45362.0, 75345.0, 346157.0, 572575.0], columns=['code_x'])
df.code_x.apply(lambda x: str(int(x)).zfill(6))

rawDescription: Pandas - Add leading "0" to string values so all values are equal len


rawDescription: Condition on numpy arrays

description: Condition on numpy arrays


code:
def doit(text):
  import re
  matches=re.findall(r'\"(.+?)\"',text)
  # matches is now ['String 1', 'String 2', 'String3']
  return ",".join(matches)

doit('Regex should return "String 1" or "String 2" or "String3" ')
# result:
'String 1,String 2,String3'

rawDescription: Python Regex to find a string in double quotes within a string


code:
from lxml import etree

foo = StringIO('<foo><bar></bar></foo>')
tree = etree.parse(foo)
result = tree.xpath('//processing-instruction()')

rawDescription: Finding and converting XML processing instructions using Python


code:
import numpy as np
import pyqtgraph as pg
from PyQt4 import QtCore

a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan, 9, 10, 11])
b = np.array([1, 2, 3, np.nan, 3, 2, 1, np.nan, 1, 2, 3])
c = b+3

w = pg.PlotWindow()
finitecurve = pg.PlotDataItem(a, b, connect="finite", pen=(255, 255, 0))
normalcurve = pg.PlotDataItem(a, c, pen=(255, 0, 0))
w.addItem(normalcurve)
w.addItem(finitecurve)
w.show()

if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        pg.QtGui.QApplication.exec_()

rawDescription: None values in pyqtgraph

description: None values in pyqtgraph


code:
from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://www.airmilescalculator.com/distance/blr-to-cdg/")

distance = driver.find_element_by_id("totaldistancemilestext").text
print(distance)

rawDescription: Scraping dynamic html fields with lxml

description: Scraping dynamic html fields with lxml


code:
from Cookie import SimpleCookie

def my_app(environ, start_response):
    session_cookie = SimpleCookie()
    session_cookie['session'] = "somedata"
    session_cookie['session']["Path"] = '/'

    headers = []
    headers.extend(("set-cookie", morsel.OutputString())
                    for morsel
                    in session_cookie.values())

    start_response("200 OK", headers)

rawDescription: Set a cookie and retrieve it with Python and WSGI


code: df[(x < df.age) & (df.age < y) & (p < df.income) & (df.income < q)]

rawDescription: Pandas: Speed up filtering by creating index?


code:
tree.xpath('//text:a | //text:h',
           namespaces={'text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'})

rawDescription: Parsing ODF in Python with lxml

description: Parsing ODF in Python with lxml


code:
class A(models.Model):
    b = models.ForeignKey('B')

class B(models.Model):
    a = models.ForeignKey('A')

rawDescription: Django model preimport

description: Django model preimport


code:
def is_prime(a):
    return all(a % i for i in xrange(2, a))

rawDescription: Python: Verifying a prime number

description: Python: Verifying a prime number


code:
from ctypes import *

PyObject_HEAD = [
    ("ob_refcnt", c_size_t),
    ("ob_type", c_void_p),
]

class methodcallerobject(Structure):
    _fields_ = PyObject_HEAD + [
        ("name", c_void_p),
        ("args", c_void_p),
        ("kwds", c_void_p),
    ]

def magic(methcallobj):
    if not isinstance(methcallobj, operator.methodcaller):
        raise TypeError("not a methodcaller")

    c_methcallobj = cast(c_void_p(id(methcallobj)), POINTER(methodcallerobject)).contents

    return cast(c_methcallobj.name, py_object).value

rawDescription: Is it possible to get function name from operator.methodcaller?


code:
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import datetime
import math
MINUTE = 60
HOUR = 60*MINUTE
class App():
    def __init__(self):
        self.root = tk.Tk()
        self.done_time=datetime.datetime.now() + datetime.timedelta(seconds=HOUR/2) # half hour
        self.label = tk.Label(text="")
        self.label.pack()
        self.update_clock()
        self.root.mainloop()

    def update_clock(self):
        elapsed = self.done_time - datetime.datetime.now()
        h,m,s,fractional_s = elapsed.seconds/3600,elapsed.seconds/60,elapsed.seconds%60
        fractional_seconds = math.floor(elapsed.microseconds/1000000.0*100)
        self.label.configure(text="%02d:%02d:%02d.%02d"%(h,m,s))
        self.root.after(1000, self.update_clock)

app=App()

rawDescription: Making a Tkinter countdown timer update actively


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

window = 100
xs = np.arange(100)
ys = np.zeros(100)
ys[80:90] = 1
y2s = np.random.randn(100)/5.0+0.5

fig = plt.figure()
with sns.axes_style("ticks"):
    for idx,scenario in enumerate(["one", "two", "three"]):
        off = idx/10.+0.1
        ax=fig.add_axes([off,off,0.65,0.65], axisbg='None')
        ax.plot(xs, ys)
        ax.plot(xs, y2s)
        ax.set_title(scenario)
        sns.despine(offset=10)

rawDescription: Programmatically drawing overlaid offset plots in matplotlib


rawDescription: Numpy.where workaround

description: Numpy.where workaround


code:
import csv

def get_row(filename, number, column):
    with open(filename, 'r') as f:
        for row in csv.reader(f):
            if str(number) in row:
                return row[column+1]

rawDescription: How do I make my program give me the value of a certain cell in a CSV?

description: Make my program give me the value of a certain cell in a CSV


rawDescription: Odd behavior of Python operator.xor

description: Odd behavior of Python operator.xor


code:
var1 = "tuna"
var2 = "tunasandwich"
var3 = var2.replace(var1, "")

rawDescription: Subtract string text from string text

description: Subtract string text from string text


code:
import datetime
import msgpack

useful_dict = {
    "id": 1,
    "created": datetime.datetime.now(),
}

def decode_datetime(obj):
    if b'__datetime__' in obj:
        obj = datetime.datetime.strptime(obj[b'as_str'].decode(), "%Y%m%dT%H:%M:%S.%f")
    return obj

def encode_datetime(obj):
    if isinstance(obj, datetime.datetime):
        obj = {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f").encode()}
    return obj

packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)

rawDescription: MessagePack and datetime

description: MessagePack and datetime


code: d = {name: re.compile("%s=[a-z]+&" % name) for name in a}

rawDescription: How to convert a list of items into variables in python?

description: Convert a list of items into variables in python


rawDescription: Matlibplot and scipy Interpolate: Show and evenly disperse Dates


code:
from flask import url_for, redirect

@app.route('/u1/')
def u1():
    # do something
    return redirect(url_for('index'))

rawDescription: Redirect to one Flask view from another

description: Redirect to one Flask view from another


code: islice( myarr, 0, len(myarr)-1 )

rawDescription: Python: Adding element to list while iterating


code:
from splinter import Browser
import time

URL = 'https://mail.google.com/mail/u/0/?shva=1#trash'

with Browser() as browser:
    browser.visit(URL)

    username = browser.find_by_id('Email')
    username.fill(...)

    password = browser.find_by_id('Passwd')
    password.fill(...)

    button = browser.find_by_id('signIn')
    button.click()

    time.sleep(5)

    browser.visit(URL)

    empty_button = browser.find_by_xpath("//div[5]/div[2]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div[2]/div[3]/div/span")
    empty_button.click()

rawDescription: XPath not resolving to link when running test using Splinter


code:
import webapp2
from google.appengine.ext import ndb

def parent_key():
    return ndb.Key('My','Entities')

class SmallModel(ndb.Model):
    n = ndb.IntegerProperty(required=True)
    stamp = ndb.DateTimeProperty(auto_now_add=True)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world. Simple form. <form method="post"><input name="n" type="number"><input type="submit"></form>')
    def post(self):
        entity = SmallModel(parent=parent_key(),n=int(self.request.get('n')))
        entity.put()
        self.redirect('/list')

class List(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("here's a list:")
        entities = SmallModel.query(ancestor=parent_key())
        for entity in entities.iter():
            self.response.out.write(" %s " % entity.n)
        self.response.out.write("...end list.")

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/list',List)
], debug=True)

rawDescription: App Engine Dev Server datastore not updating fast enough?


code:
def set_user(user):
    #Make into context manager
    env.custom_user = user

def bar():
    with set_user("foo"):
        execute("sometask")

def sometask():
    sudo("command", user=env.custom_user)

rawDescription: fabric run command as a system user

description: Fabric run command as a system user


code:
def is_on_mount(path):
  while True:
    if path == os.path.dirname(path):
      # we've hit the root dir
      return False
    elif os.path.ismount(path):
      return True
    path = os.path.dirname(path)

path = '/mount/one/two/three'
is_on_mount(path)

rawDescription: Determine if subdirectory is on a mounted file system with Python


code:
# Invert the colormap
plt.imshow(X_train[0], cmap='gray_r')

# Invert your data
plt.imshow(-X_train[0], cmap='gray')

rawDescription: Which cmap (colormap) to use with matplotlib imshow() to diplay the MNIST dataset images as black characters on white background


rawDescription: Sorting two-dimensional dataframe using Pandas


code:
import io
file = io.BytesIO(b'this is a byte string')
file.read(2)
file.read(2)

rawDescription: How to read bytes as stream in python 3

description: Read bytes as stream in python 3


code:
import bpy
import xml.dom.minidom

path = "/Users/x/Documents/y/game_projects/test.xml"

dom = xml.dom.minidom.getDOMImplementation()
tree = dom.createDocument(None, "document", None)

root = tree.documentElement
root.setAttribute("version", "0.1")

for object in bpy.data.objects:
    if object.type == 'MESH' and object.data.shape_keys:

        objectElement = tree.createElement("object")
        objectElement.setAttribute("name", object.name)
        root.appendChild(objectElement)

        keysElement = tree.createElement("shape_keys")
        objectElement.appendChild(keysElement)

        keyBlocks = object.data.shape_keys.key_blocks
        for block in keyBlocks:
            keyElement = tree.createElement("key")
            keyElement.setAttribute("name", block.name)
            keysElement.appendChild(keyElement)

            for data in block.data:
                vertex = data.co
                element = tree.createElement("vertex")
                element.setAttribute("x", str(vertex.x))
                element.setAttribute("y", str(vertex.y))
                element.setAttribute("z", str(vertex.z))
                keyElement.appendChild(element)

file = open(path, "w", encoding="utf8")
tree.writexml(file, encoding = "UTF-8", indent = "\n", addindent = "\t")
file.close()

rawDescription: blender: how to export shape keys using python?

description: Blender: how to export shape keys using python


code:
L = L.reshape([i, P, 3])
L = numpy.rollaxis(L, 1)

rawDescription: changing shapes of a numpy array

description: Changing shapes of a numpy array


rawDescription: Unable to get associated vpc from the hosted zones boto


code:
@app.route('/index.html')
def index():
    return render_template('index.html')

rawDescription: href attribute value not working in Python Flask app for target HTML pages

description: Href attribute value not working in Python Flask app for target HTML pages


code:
mystring = ['1      SLES11 SP1 64bit            [datastore1] SLES11 SP1 64bit/SLES11 SP1 64bit.vmx   sles11_64Guest          vmx-08  ']
import re
re.split(r"\s{2,}", mystring[0].strip())

rawDescription: Python, Split a string by whitespaces, similar to awk


code:
import random
from django.conf import settings

# there might be a better way to do this...
DATABASE_ENGINE = settings.DATABASES[settings.DATABASES.keys()[0]]['ENGINE'].split('.')[-1]

def compare_hashes(request, i=None):
    competitors = Competitors.objects.all()

    if DATABASE_ENGINE == 'mysql':
        if not request.session.get('random_seed', False):
            request.session['random_seed'] = random.randint(1, 10000)
        seed = request.session['random_seed']
        competitors = competitors.extra(select={'sort_key': 'RAND(%s)' % seed}).order_by('sort_key')

    # now competitors is randomised but consistent for the session
    ...

rawDescription: Randomize a Django queryset once, then iterate through it


code:
import urllib
'file:' + urllib.pathname2url(r'c:\path\to\something')

rawDescription: urllib2.urlopen a local file, cross platform

description: Urllib2.urlopen a local file, cross platform


code:
global_dict = {
    'dict_1': {"a":5, "b":1, "c":8},
    'dict_2': {"a":2, "b":6, "c":11},
}

dict_name = raw_input("Enter a dict: ")

try:
    print(global_dict[dict_name]['c'])
except KeyError:
    print('Dict not found')

rawDescription: Using a string for a dictionary

description: Using a string for a dictionary


rawDescription: Pythons many ways of string formatting — are the older ones (going to be) deprecated?


code:
import sys
import subprocess

fname = sys.argv[1]
proc = subprocess.Popen(['sort', fname], stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
with open(fname, 'w') as f:
    f.write(stdout)

rawDescription: sorting file in place with Python on unix system

description: Sorting file in place with Python on unix system


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

rawDescription: Can you initiate a variable while declaring a loop in Python?

description: Initiate a variable while declaring a loop in Python


code:
map(lambda x: x[:2] + [1] + x[3:], array)

map(lambda x: x[:2] + my_func(x[2]) + x[3:], array)

rawDescription: Operation on 2d array columns

description: Operation on 2d array columns


code:
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import numpy
import pylab

# create figure and draw symbol with three points
fig = Figure(figsize=(5,4), dpi=600)
ax = fig.add_subplot(111)
ax.plot([0,1,2],[0,0,0], '-o', mec='b', mfc='g')

# get canvas, draw and get as string
canvas = FigureCanvasAgg(fig)
canvas.draw()
s = canvas.tostring_rgb()

# convert to array
l,b,w,h = fig.bbox.bounds
w, h = int(w), int(h)
X = numpy.fromstring(s, numpy.uint8)
X.shape = h, w, 3

# plot only the center as bitmap
pylab.imshow(X[h/2-50:h/2+50, w/2-0:w/2+80,:])
pylab.show()

rawDescription: How can I convert matplotlib path object to bitmap?

description: Convert matplotlib path object to bitmap


code:
t = Thread(target=myfunction)
t.daemon = True
t.start()

rawDescription: Threading behavior is weird in python subprocess


code:
from datetime import datetime
datetime.strptime("Tuesday July 7, 2015", "%A %B %d, %Y").strftime("%Y-%m-%d")

rawDescription: How to turn this into a Python Datetime

description: Turn this into a Python Datetime


code:
import cssutils

# Parse the stylesheet, replace color
parser = cssutils.parseFile('style.css')
for rule in parser.cssRules:
    try:
        if rule.selectorText == '#panel':
            rule.style.backgroundColor = 'blue'  # Replace background
    except AttributeError as e:
        pass  # Ignore error if the rule does not have background

# Write to a new file
with open('style_new.css', 'wb') as f:
    f.write(parser.cssText)

rawDescription: Replace CSS block of text within file Python


code: return input in operatorlist

rawDescription: Finding an item in a list

description: Finding an item in a list


code:
values = np.array([[0, 1], [2, 3]])
values.T

rawDescription: Convert array in python

description: Convert array in python


code:
import re
p = re.compile(r'(?:\B\W\s*)?\([^()]+\)')
test_str = "(text9 & text10) & text1 & (\"text2\" | \"text3\" | \"text4\") & \"text5\" ! (text6 | text7 | text8)"
print(p.findall(test_str))

rawDescription: parse a string using regex

description: Parse a string using regex


code:
import socks
s = socks.socksocket()
s.setproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
s.connect(("www.example.com",80))

rawDescription: Tor connection using sockets in Python

description: Tor connection using sockets in Python


code:
delta = 0.25
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = X*np.exp(-X**2-Y**2)
Z2 = Y*np.exp(-X**2-Y**2)

fig, ax = plt.subplots()

CS1 = ax.contour(X, Y, Z1, colors='k')
ax.clabel(CS1, inline=1, fontsize=10)

CS2 = ax.contour(X, Y, Z2, colors='r')
ax.clabel(CS2, inline=1, fontsize=10)

lines = [ CS1.collections[0], CS1.collections[-1], CS2.collections[0], CS2.collections[-1]]
labels = ['CS1_neg','CS1_pos','CS2_neg','CS2_pos']

plt.legend(lines, labels)

rawDescription: Matplotlib: Multiple legends for contour plot for multiple contour variables


code:
fig, ax = plt.subplots()

ax.plot(np.random.randn(100).cumsum(), c='k')

colors = ['r','b']

for n, tl in enumerate(ax.xaxis.get_ticklabels()):
    tl.set_color(colors[n%2])

rawDescription: matplotlib: displaying certain labels in different color

description: Matplotlib: displaying certain labels in different color


code:
mycards= ['0H','8H','7H','6H','AH','QS']
all((x == 'QS' or 'H' in x) for x in mycards)
#  True

rawDescription: How to check all the elements in a list that has a specific requirement?

description: Check all the elements in a list that has a specific requirement


code:
import numpy

n = 400  # approximate largest cube size that works on my laptop
# Fill channel cubes with random 8-bit integers
r = numpy.random.randint(256, size=(n,n,n)).astype(numpy.uint8)
g = numpy.random.randint(256, size=(n,n,n)).astype(numpy.uint8)
b = numpy.random.randint(256, size=(n,n,n)).astype(numpy.uint8)

# reorder data into for suitable for histogramming
data = numpy.vstack((r.flat, g.flat, b.flat)).astype(numpy.uint8).T

# Destroy originals to save space
del(r); del(g); del(b)

m = 256                                  # size of 3d histogram cube
hist, edges = numpy.histogramdd(
    data, bins=m, range=((-0.5,255.5),(-0.5,255.5),(-0.5,255.5))
    )

# Check that it worked
assert hist.sum() == n**3, 'Failed to conserve pixels'

rawDescription: numpy histogram with 3x3D arrays as indices

description: Numpy histogram with 3x3D arrays as indices


rawDescription: utf-8 string from python to java android in AWS

description: Utf-8 string from python to java android in AWS


code:
self.parent.bind("<Return>", self.validate_form)
Self.text.bind("<Return>", self.validate_form)

rawDescription: Preventing newlines in Text widget, but still proagate the event upwards?


code:
data = StringIO()
document.save(data)  # Or however the library requires you to do this.
zip_arch.writestr(docx_title, bytes(data.getvalue()))

rawDescription: How to add a temporary .docx file to a zip archive in django

description: Add a temporary .docx file to a zip archive in django


rawDescription: Efficiently Labeling Variable Values in Pandas


code:
from sqlalchemy.ext.associationproxy import association_proxy

class Record(Model, Base):
    """"""
    __tablename__ = "records"
    NAME          = Column(String, primary_key = True)
    TITLE         = Column(Text)
    COMPANY       = Column(Text)
    LOCATION      = Column(Text)
    DATE          = Column(Text)
    BLURB         = Column(Text)
    BULLET        = Column(Text)
    URL           = Column(Text)

    targets       = association_proxy("target_records", "target", creator = lambda target: Target_Record(target = target))

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.NAME or "Unamed")

class Target(Model, Base):
    """"""
    __tablename__ = "targets"
    NAME          = Column(Text, primary_key = True)
    MODULE        = Column(Text)
    URL           = Column(Text)
    JOB_URL       = Column(Text)
    COMPANY       = Column(Text)

    records       = association_proxy("target_records", "record", creator = lambda record: Target_Record(record = record))

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.NAME or "Unamed")

class Target_Record(Model, Base):
    __tablename__ = "target_records"
    target_id     = Column(Text  , ForeignKey("targets.NAME"), primary_key = True)
    record_id     = Column(String, ForeignKey("records.NAME"), primary_key = True)
    target        = relationship(Target, backref = backref("target_records"))
    record        = relationship(Record, backref = backref("target_records"))

rawDescription: SQLAlchemy - Cyclic data - CircularDependencyError


code: Y = [x.v for x in X]

rawDescription: Accessing Class Variables from a List in a nice way in Python


code:
import zipfile as z
z1 = z.ZipFile('z1.zip', 'a')
z2 = z.ZipFile('z2.zip', 'r')
z1.namelist()
z2.namelist()
[z1.writestr(t[0], t[1].read()) for t in ((n, z2.open(n)) for n in z2.namelist())]
z1.namelist()
z1.close()

rawDescription: Merge multiple zip files into a single zip file in Python


code:
import tweepy
import csv

keyword = 'McDonalds '
result = tweepy.api.search(q=keyword,rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" )

with open('some.csv', 'w') as acsv:
    w = csv.writer(acsv)
    w.writerow(('Keyword', 'Tweet', 'Lat', 'Lon'))
    for tweet in result:
        lat, lon = tweet.geo if tweet.geo else ('', '')
        w.writerow((keyword, tweet.text, lat, lon))

rawDescription: Write Tweety (python module)search results To CSV


code:
for c in range(40, -41, -1): # iterate over Celsius
    f = (c * (9 / 5)) + 32 # calculate Fahrenheit from Celsius
    print("{0:d}\t{1:.1f}".format(c, f)) # print both in one line, tab-separated

rawDescription: Problems aligning text in a table using string formatting


code:
points =  [10,10, 20,20]
pdb.gimp_paintbrush_default(layer, len(points), points)

rawDescription: How do you paint a stroke in Gimp with Python-fu?

description: Paint a stroke in Gimp with Python-fu


code:
html = '
2³
'
soup = BeautifulSoup.BeautifulSoup(html, 'html.parser')
out = soup.prettify(formatter="html")
file('tmp.html', 'wb').write(out)

rawDescription: How to print exponential units on BeautifulSoup

description: Print exponential units on BeautifulSoup


code:
L['maxval'] = week['value'].transform('max')
L

rawDescription: Multi column join in pandas from groupby


code:
w="Shum"
[w[:c] for c in range(len(w),0,-1)]

rawDescription: Python Cleaner way to get all substrings in a string


code: soup = BeautifulSoup(html, "lxml")

rawDescription: Webcrawling script producing different results on two different machines


code:
if s.setSession():
    response = make_response(redirect('/home'))
    response.set_cookie('session_id', s.session_id)
    return response

rawDescription: In Flask, set a cookie and then re-direct user


code:
def base_and_num(number, base):
    for i in str(number):
        if base <= int(i):
            return False
    return True

rawDescription: Validating Base Number

description: Validating Base Number


code:
from PySide import QtCore
import time, sys

class MyWorkerThread(QtCore.QThread):
    message = QtCore.Signal(str)

    def __init__(self, id, parent=None):
        super(MyWorkerThread, self).__init__(parent)
        self.id = id

    def run(self):
        for i in range(10):
            self.message.emit("%d: %d" % (self.id, i))
            time.sleep(0.2)

class MainProgram():
    def __init__(self, parent=None):
        self.threads = []

        self.addWorker(MyWorkerThread(1))
        self.addWorker(MyWorkerThread(2))

    def addWorker(self, worker):
        worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
        # connect the finished signal to method so that we are notified
        worker.finished.connect(self.workersFinished)
        self.threads.append(worker)

    def startWorkers(self):
        for worker in self.threads:
            worker.start()
            # no wait, no finished. you start the threads and leave.

    def workersFinished(self):
        if all(worker.isFinished() for worker in self.threads):
            # wait until all the threads finished
            QtCore.QCoreApplication.instance().quit()

    @QtCore.Slot(str)
    def printMessage(self, text):
        sys.stdout.write(text+'\n')
        sys.stdout.flush()

if __name__ == '__main__':
    app = QtCore.QCoreApplication(sys.argv)
    m = MainProgram()
    m.startWorkers()
    sys.exit(app.exec_())

rawDescription: How to get QThreads to work in a console PySide program?

description: Get QThreads to work in a console PySide program


rawDescription: python - logging stdout, but getting second blank line on each entry

description: Python - logging stdout, but getting second blank line on each entry


code:
#!/usr/bin/python
import wx
import wx.grid

class GridAndMenuFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,
                          wx.DefaultPosition, wx.Size(200, 150))

        mbar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(101, 'E&xit', 'Exit')
        mbar.Append(menu, '&File')
        self.SetMenuBar(mbar)

        grid = wx.grid.Grid(self, -1)
        # obviously, size and configure the grid here

if __name__ == '__main__':
    app = wx.App(redirect=False)
    win = GridAndMenuFrame(None, -1, "grid and menu")
    win.Show()
    app.MainLoop()

rawDescription: adding menubar to wxpython.grid Table

description: Adding menubar to wxpython.grid Table


code:
def mysend(self, msg):
    totalsent = 0
    while totalsent < MSGLEN:
        sent = self.sock.send(msg[totalsent:])
        if sent == 0:
            raise RuntimeError("socket connection broken")
        totalsent = totalsent + sent

rawDescription: C TCP Echo Server - Data displays only on close()


code:
# If @asynchronous is used with @gen.coroutine, (but
# not @gen.engine), we can automatically finish the
# request when the future resolves.  Additionally,
# the Future will swallow any exceptions so we need
# to throw them back out to the stack context to finish
# the request.

rawDescription: Tornado @gen.coroutine closes long polling connection


code:
max_range = max(s.index) + 1
s = s.reindex(index=range(1, max_range), fill_value=0)

rawDescription: Resampling Pandas Series with integer index (add missing index)


code:
from apitools.clients import bigquery

table_schema = bigquery.TableSchema()

# ‘string’ field
field_schema = bigquery.TableFieldSchema()
field_schema.name = 'fullName'
field_schema.type = 'string'
field_schema.mode = 'required'
table_schema.fields.append(field_schema)

# ‘integer’ field
field_schema = bigquery.TableFieldSchema()
field_schema.name = 'age'
field_schema.type = 'integer'
field_schema.mode = 'nullable'
table_schema.fields.append(field_schema)

# nested field
field_schema = bigquery.TableFieldSchema()
field_schema.name = 'phoneNumber'
field_schema.type = 'record'
field_schema.mode = 'nullable'

area_code = bigquery.TableFieldSchema()
area_code.name = 'areaCode'
area_code.type = 'integer'
area_code.mode = 'nullable'
field_schema.fields.append(area_code)

number = bigquery.TableFieldSchema()
number.name = 'number'
number.type = 'integer'
number.mode = 'nullable'
field_schema.fields.append(number)
table_schema.fields.append(field_schema)

# repeated field
field_schema = bigquery.TableFieldSchema()
field_schema.name = 'children'
field_schema.type = 'string'
field_schema.mode = 'repeated'
table_schema.fields.append(field_schema)

rawDescription: JSON table schema to bigquery.TableSchema for BigQuerySink


code:
from random import randint
from time import sleep
from threading import Thread
import sys

class RollAnimation ( Thread ):
    activated = True
    number = None

    def run ( self ):
        while self.activated:
            self.number = randint( 1, 100 )
            sys.stdout.write( '\r{: >3}'.format( self.number ) )
            sys.stdout.flush()
            sleep( 0.05 )

t = RollAnimation()
t.start()

# Waiting for enter
input() # use raw_input() for Python 2.x
t.activated = False

print( 'Final roll result:', t.number )

rawDescription: Rolling statistics for a game

description: Rolling statistics for a game


code:
import wx

class TestNoteBook(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 500))
        panel = wx.Panel(self)
        hsizer = wx.BoxSizer(wx.HORIZONTAL)

        leftpanel = wx.Panel(panel)
        notebook = wx.Notebook(leftpanel)
        posterpage = wx.Panel(notebook)
        listpage = wx.Panel(notebook)
        notebook.AddPage(posterpage, 'posters')
        notebook.AddPage(listpage, 'list')
        hsizer.Add(leftpanel, 1, wx.EXPAND)
        rightpanel = wx.Panel(panel)
        hsizer.Add(rightpanel, 1, wx.EXPAND)

        ##### Added code (
        leftpanel_sizer = wx.BoxSizer(wx.HORIZONTAL)
        leftpanel_sizer.Add(notebook, 1, wx.EXPAND)
        leftpanel.SetSizer(leftpanel_sizer)

        rightpanel.SetBackgroundColour('blue') # not needed, to distinguish rightpanel from leftpanel
        ##### Added code )

        panel.SetSizer(hsizer)

app = wx.App()
frame = TestNoteBook(None, -1, 'notebook')
frame.Show()
app.MainLoop()

rawDescription: wxpython notebook inside boxsizer

description: Wxpython notebook inside boxsizer


code:
import mechanize
import re
import xlwt
from bs4 import BeautifulSoup
import urllib2

book = xlwt.Workbook(encoding='utf-8', style_compression = 0)
sheet = book.add_sheet('Legi', cell_overwrite_ok = True)

for items in ['university student', 'high school student']:

    url = r'http://legistar.council.nyc.gov/Legislation.aspx'
    request = mechanize.Request(url)
    response = mechanize.urlopen(request)
    forms = mechanize.ParseResponse(response, backwards_compat=False)
    form = forms[0]
    response.close()

    form['ctl00$ContentPlaceHolder1$txtSearch'] = items

    submit_page = mechanize.urlopen(form.click())
    soup = BeautifulSoup(submit_page.read())
    aa = soup.find_all(href=re.compile('LegislationDetail'))

    results = [] # Initialize results here !!!
    for bb in aa:
        cc = bb.text

        #print cc
        results.append(cc)

    for row, legi_no in enumerate(results):
      sheet.write (row, 0, legi_no)

book.save("C:\\legi results.xls")

rawDescription: Writing results to .xls (Submit 2 queries to webpage, and store different results into .xls)


code:
my_list = ['aba', 'acba', 'caz']
result = [s for s in my_list if not s.strip('abc')]

rawDescription: Test if string ONLY contains given characters


code:
import ast
s = "[u'$649.99']"
ast.literal_eval(s)
ast.literal_eval(s)[0]

rawDescription: Get the text between string like [u'$649.99']


code:
def getPastAmountDecompiledFromFile(number):
    if bucketKeys.__len__() > 0:
        Found=[]
        latest=bucketKeys[0]
        while Found.__len__() < number:
            laterFound = False
            for k in bucketKeys:
                if latest in Found:
                    latest=k
                current = k.split("/")
                best = k.split("/")
                if k not in Found and latest != k:
                    if int(current[6]) > int(best[6]):
                        laterFound=True
                    if int(current[6]) == int(best[6]) and int(current[7]) > int(best[7]):
                        laterFound=True
                    if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) > int(best[8]):
                        laterFound=True
                    if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) == int(best[8]) and int(current[9]) > int(best[9]):
                        laterFound=True
                    if laterFound:
                        latest = k
            if laterFound:
                Found.append(latest)
        return getKeyFromKeyNames(Found)
    else:
        getKeysInFile()
        getPastAmountDecompiledFromFile(number)
    return

rawDescription: aws s3 boto retrieve objects by date

description: Aws s3 boto retrieve objects by date


code:
address = 'Anthony Street 35'
address_elem = driver.find_element_by_id("Address")
for letter in address:
    time.sleep(random.randint(1, 3))  # sleep between 1 and 3 seconds
    address_elem.send_keys(letter)

rawDescription: Can Selenium webdriver make a random time beween send keys (to simulate human behaviour when typing)?


rawDescription: Very Very Large Numbrs in Python

description: Very Very Large Numbrs in Python


code:
def update(self):
    # Move left/right
    self.rect.x += self.change_x

    cur_pos = self.rect.x - self.level.world_shift

    print(cur_pos)

    # test against boundaries:
    if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
        self.change_x *= -1

    # set animation frame:
    if self.change_x > 0:
        frame = (self.rect.x // 30) % len(self.enemy_moving_frames_r)
        self.image = self.enemy_moving_frames_r[frame]
    elif self.change_x < 0:
        frame = (self.rect.x // 30) % len(self.enemy_moving_frames_l)
        self.image = self.enemy_moving_frames_l[frame]

rawDescription: Pygame - Making a Sprite "walk" in the right direction


rawDescription: Abbreviation Reference for NLTK Parts of Speach


code:
import csv

sg = []
fqdn = {}
output = []
with open(r'file2.csv', 'rb') as src:
    read = csv.reader(src, delimiter=',')
    for dataset in read:
        sg.append(dataset)

with open(r'file1.csv', 'rb') as src1:
    read1 = csv.reader(src1, delimiter=',')
    for to_append, to_match in read1:
        fqdn[to_match.lower()] = to_append

for dataset in sg:
    to_append = fqdn.get(dataset[2].lower()) # If the key matched, to_append now contains the string to append, else it becomes None
    if to_append:
        dataset.append(to_append) # Append the field
        output.append(dataset) # Append the row to the result list

print(output)

rawDescription: Merge CSV row with a string match from a 2nd CSV file


code:
for i in range(3):
    l=plt.vlines(i,0,20,linestyles=[(0,(9,3,4,4))],colors='r')

rawDescription: customising line styles in Python using vlines

description: Customising line styles in Python using vlines


code:
import json
import sys
import os
data = []
if os.stat("data.txt").st_size != 0 :
    file = open('data.txt', 'r')
    data = json.load(file)
    print(data)

choice = input("What's your choice ?")
if choice == 'a':
    # Add a new joke.
    # See Point 3 of the "Requirements of admin.py" section of the assignment brief.
    jokeSetup = input('Enter setup of joke: ')
    jokePunchLine = input('Enter punchline of joke: ')
    entry = {'setup': jokeSetup , 'punchline': jokePunchLine}
    data.append(entry)
    file = open('data.txt', 'w')
    json.dump(data, file)
    file.close()
    print('Joke Added.')
    pass

elif choice == 's':
    # Search the current jokes.
    # See Point 5 of the "Requirements of admin.py" section of the assignment brief.
    searchTerm = input('Enter search term: ')
    file = open('data.txt', 'r')
    data = json.load(file)
    file.close()

    for sub_dict in data:
        if searchTerm in sub_dict['setup']:
            print(sub_dict['punchline'])
    pass
# or you could modify the last for loop, like this:
    for dict in data:
        if searchTerm in dict['setup'] or searchTerm in dict['punchline']:
            print('found!')
    pass

rawDescription: search for string in json file

description: Search for string in json file


code:
mydata = threading.local()
mydata.x = 1

rawDescription: Python Static Thread Variable

description: Python Static Thread Variable


code:
from genshi.core import Markup
description = Markup(description)

rawDescription: python/genshi How to produce dynamic XML elements

description: Python/genshi How to produce dynamic XML elements


rawDescription: Getting unique rows conditioned on year pandas python dataframe


code:
if amt <= balance: #Can't withdraw over your current balance
    balance -= amt
else:
    print("Error. Amount exceeds funds on record.")

rawDescription: Python: setting max for bank withdraw

description: Python: setting max for bank withdraw


code:
import re
targets = 'FD-234 XY-456 FD-285 XY-890 FD-999'
search = 'FD-2**'
pattern = search.replace('*', '.')
re.findall(pattern, targets)

rawDescription: Python - search list with only a few characters entered


code:
__author__ = "Software Authors Name"
__copyright__ = "Copyright (C) 2004 Author Name"
__license__ = "Public Domain"
__version__ = "1.0"

rawDescription: Writing copyright information in python code


code:
class LinksFromDomain(object):
    def __init__(self):
        self._spider_to_domains = {}

    def open_spider(self, spider):
        self._spider_to_domains[spider] = {}

    def close_spider(self, spider):
        # write out a csv file with all the items in some format.
        self._spider_to_domains[spider] = {}

    def process_item(self, item, spider):
        domains_dict = self._spider_to_domains[spider]
        if item['domain'] not in domains_dict:
            domains_dict[item['domain']] = []

        domains_dict[item['domain']].extend(item['links'])

rawDescription: Scrapy - How can I parse information on a per domain level


code:
f = open('text.txt', 'w')
f.write(' '.join("%s=%s" % (y, x) for x,y in input))
f.close()

rawDescription: Python ".write"

description: Python ".write"


code:
def recurs(l):
    for key in l.keys():
        if key == '_events':
            yield l[key]
        else:
            for x in recurs(l[key]):
                yield x

rawDescription: How to return the value of a recursive function

description: Return the value of a recursive function


code:
for sublist in lst:
   if len(sublist) == 3:
      sublist.insert(2,"Null")

lst

rawDescription: How to insert an item into a sublist if sublist is a certain length?

description: Insert an item into a sublist if sublist is a certain length


code:
try:
    from settings_local import *
except ImportError:
    pass

try:
    from settings_production import *
except ImportError:
    pass

rawDescription: My settings.py file in Django is getting too long. How do I split it into two?


code:
lst = [1, 2, 3]
list(lst)
next(lst)

rawDescription: Iterator vs Iterable?

description: Iterator vs Iterable


rawDescription: reading scientific notation csv file with numpy

description: Reading scientific notation csv file with numpy


code:
@periodic_task(run_every=datetime.timedelta(seconds=2))
@transaction.commit_on_success
def initialize_new_jobs():
    for obj in Queue.objects.select_for_update().filter(status__in=['I', 'Q']):
        obj.status = 'A'
        obj.save()
        create_other_task.delay(obj.id)

rawDescription: Celery periodic_task in Django run once(!)


code:
import os
os.system('/usr/bin/ssh localhost ls -la /')

rawDescription: How to establish a ssh connection through python or jython and run a script remotly?

description: Establish a ssh connection through python or jython and run a script remotly


code:
def input_not_type_hidden(tag):
    return tag.name == 'input' and tag.get('type') != 'hidden'

soup.find_all(input_not_type_hidden)

rawDescription: Can you return tags where condition is not met in BeautifulSoup?

description: Return tags where condition is not met in BeautifulSoup


code:
for row in readstream:
    row[0] = i
    outwriter.writerow(row)
    i += 1
    if i % 2 == 0:
        outwriter.writerow([])
        i += 1

rawDescription: Modify first element in csv file python

description: Modify first element in csv file python


code:
def foo(seq):
  if seq == None or seq == []:
    return []
  outp = [seq[0]]
  for a in seq:
    if a > outp[-1]:
      outp.append(a)
  return outp

rawDescription: Subsequence in a sequence of numbers

description: Subsequence in a sequence of numbers


code:
input_tuple = ("A", "b", "C", "d")
output_tuples = []
for subtuple_length in reversed(xrange(1, len(input_tuple))):
    for start_index in xrange(0, (len(input_tuple) + 1 - subtuple_length)):
        output_tuples.append(input_tuple[start_index:start_index + subtuple_length])

rawDescription: Subsets of a tuple

description: Subsets of a tuple


code:
if all(func(arg, my_dict[k]) for k in my_dict):
  ...

rawDescription: How to shorten code?

description: Shorten code


code:
df = pd.melt(df, id_vars=['company', 'size', 'year'])
df = df[df.value != 0]

rawDescription: Reshaping pandas DataFrame by transforming columns to labels contained in single column


code:
printcounter = 0

# Whatever a while loop is in Python
while (...):
    ...
    if (printcounter == 1000000):
        print('Progress report...')
        printcounter = 0
    ...
    printcounter += 1

rawDescription: Execute statement every N iterations in Python


code:
import os
import tarfile

home = '/home/'
backup_dir = '/backup/'

home_dirs = [ name for name in os.listdir(home) if os.path.isdir(os.path.join(home, name)) ]

for directory in home_dirs:
    full_dir = os.path.join(home, directory)
    tar = tarfile.open(os.path.join(backup_dir, directory+'.tar.gz'), 'w:gz')
    tar.add(full_dir)
    tar.close()

rawDescription: How to make tar backup using python

description: Make tar backup using python


code:
def strip_accents(s):
  return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))

rawDescription: python: Replacing special characters in a string

description: Python: Replacing special characters in a string


code:
list1 = ['a','c','e']
list2 = ['apple','carrot','elephant']
[x for z in zip(list1, list2) for x in z]

rawDescription: combine two lists into one using one by one element

description: Combine two lists into one using one by one element


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

# some data
x = np.arange(0, 10, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

# plot of the data
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
ax.plot(x, y1,'-k', lw=2, label='black sin(x)')
ax.plot(x, y2,'-r', lw=2, label='red cos(x)')
ax.set_xlabel('x', size=22)
ax.set_ylabel('y', size=22)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

rawDescription: My matplotlib.pyplot legend is being cut off


code:
class ProducerProcess(Process):

    def __init__(self, q, **kwargs):
        Process.__init__(self,)
        self.q = q

    def run():
        res = do_stuff()
        q.put(res)

class ConsumerProcess(Process):

    def __init__(self, q, **kwargs):
        Process.__init__(self,)
        self.q = q

    def run():
        while True:
            args = q.get(block=True) # wait until there is an item in the queue
            do_stuff(*args) # do stuff here

q = Queue()
p1 = ProducerProcess(q, **your_args)
p2 =ConsumerProcess(q, **extra_args)
p2.start()
p1.start()
# join the processes p1.join() p2.join() or use JoinableQueue depending what you need

rawDescription: Keep a process listening for intercommunication


code:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
date_range = gateway.jvm.your.package.DateRange()
date_range.someMethod()

rawDescription: Using an entire eclipse project in py4j

description: Using an entire eclipse project in py4j


code:
a = doc.getElementsByTagName('string')
for i in range(len(a)):
    if i == 0:
        a[i].firstChild.nodeValue = myvalue1
    if i == 7:
        a[i].firstChild.nodeValue = myvalue2
    if i == 9:
        a[i].firstChild.nodeValue = myvalue3

rawDescription: Modify (Change specific elements) XML file with minidom in python


code:
js_code = "return document.getElementsByTagName('a')"
your_elements = sel.execute_script(js_code)

rawDescription: How can I get html content written by JavaScript with Selenium/Python

description: Get html content written by JavaScript with Selenium/Python


code:
import inspect
def f(a, b): pass
inspect.getargspec(f)

rawDescription: Higher order functions with varying parameters in Python


code:
import matplotlib.pyplot as plt
import numpy as np
a = 1
phi = np.arange(0, 10*np.pi, 0.1)
x = a*phi*np.cos(phi)
y = a*phi*np.sin(phi)

dr = (np.diff(x)**2 + np.diff(y)**2)**.5 # segment lengths
r = np.zeros_like(x)
r[1:] = np.cumsum(dr) # integrate path
r_int = np.linspace(0, r.max(), 200) # regular spaced path
x_int = np.interp(r_int, r, x) # interpolate
y_int = np.interp(r_int, r, y)

plt.subplot(1,2,1)
plt.plot(x, y, 'o-')
plt.title('Original')
plt.axis([-32,32,-32,32])

plt.subplot(1,2,2)
plt.plot(x_int, y_int, 'o-')
plt.title('Interpolated')
plt.axis([-32,32,-32,32])
plt.show()

rawDescription: Discretize path with numpy array and equal distance between points


code:
from collections import defaultdict

mydict = defaultdict(list)
for line in file:
    a,b,c = line.split() # or something else
    mydict[a].append((b,c))

rawDescription: Create list of tuples from empty dict

description: Create list of tuples from empty dict


rawDescription: Create class instance in python from list as attributes


code:
ls =['RBS-UK','GOOG-US']
map(lambda x: x.replace('-UK', '-GB'), ls)

rawDescription: Python string manipulation without comparison


code:
import os
import shutil
files = os.listdir("Path")
for file in files:
    if file.startswith("Hello"):
       shutil.copy("Full path to file", "Full path to dest folder")

rawDescription: Copying files based on File name rather than data type in Python


code:
doc = {'objName': {'id': 12345678,'name': 'someName'}}
doc['_id'] = doc['objName']['id']
collection.insert_one(doc)

rawDescription: Formatting JSON before inserting to MongoDB


code:
if spin in red:
    print('{} Red'.format(spin))
elif spin in black:
    print('{} Black'.format(spin))
else:
    print('{}'.format(spin))

rawDescription: How do I print an item and the list it came from in Python?

description: Print an item and the list it came from in Python


code:
if __name__ == '__main__':
    root = Tk()
    root.title("My Database")
    root.geometry("500x400")
    app = start_window(root)
    root.mainloop()

rawDescription: Change Python Tkinter Frame Title

description: Change Python Tkinter Frame Title


code:
class SetApplicationName(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        request.current_app = view_func.__module__.split('.')[0]

rawDescription: Django: identify app in template

description: Django: identify app in template


code:
from flask_security.forms import RegisterForm
class ExtendedRegisterForm(RegisterForm):
    first_name = TextField('First Name', [Required()])
    last_name = TextField('Last Name', [Required()])
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)

rawDescription: Flask-security: allow first name and last name fields


code:
import sys
from Api.models import *
from rq import Queue, Connection, Worker

# importing the necessary namespace for the tasks to run
from tasks import *

# Provide queue names to listen to as arguments to this script,
with Connection():

    current_workers = Worker.all()
    working_queues = [queue.name for worker in current_workers for queue in worker.queues]
    proposed_queues = [user.name for user in ApiUser.objects.all()]
    queues_to_start = [queue for queue in proposed_queues if not queue in working_queues]

    if len(queues_to_start) > 0:
        qs = list(map(Queue, queues_to_start))
        w = Worker(qs)
        w.work(burst=True)
    else:
        print("Nothing to do here.")

rawDescription: How should I handle RQ workers for overlapping batch processes


code:
def main():
  user = input('enter a user: ')
  data = ['john','gogo','baby','user']
  if user not in data:
    #stop the rest of the code
    return
  print (user)

main()

rawDescription: Python3 if something happens, don't do this part of code


code: dictf = reduce(lambda x, y: dict((k, v + y[k]) for k, v in x.iteritems()), dict1)

rawDescription: How to sum dict elements

description: Sum dict elements


code: path = os.path.join(parentDir, filename)

rawDescription: Python equivalent to java.io.File

description: Python equivalent to java.io.File


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

#comment the next 2 lines to not hide top and right axis
fig = plt.figure()
ax = fig.add_subplot(111)

#uncomment next 2 lines to deal with ticks
#ax = fig.add_subplot(111)

#calculate data
x = np.arange(0.8,2.501,0.001)
y = 4*((1/x)**12 - (1/x)**6)

#plot
ax.plot(x,y)

#do not display top and right axes
#comment to deal with ticks
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)

## the original answer:
## see  http://old.nabble.com/Ticks-direction-td30107742.html
#for tick in ax.xaxis.majorTicks:
#  tick._apply_params(tickdir="out")

# the OP way (better):
ax.tick_params(axis='both', direction='out')
ax.get_xaxis().tick_bottom()   # remove unneeded ticks
ax.get_yaxis().tick_left()

plt.show()

rawDescription: How to simultaneously remove top and right axes and plot ticks facing outwards?

description: Simultaneously remove top and right axes and plot ticks facing outwards


code:
@classmethod
def show(cls, message):
        print("The message is: {}".format(message))

rawDescription: Imitating a static method

description: Imitating a static method


rawDescription: Confused about how to perform bitwise operations on 16bit binary number in python shell


code:
import os

LINK = '  {txt}'

TEMPLATE = """

  <meta charset="utf-8">
  <title>{title}</title>
  <link rel="stylesheet" href="{stylesheet}"/>

{content}

"""

def main():
    dirs = [fname for fname in os.listdir(".") if os.path.isdir(fname)]
    dirs.sort()   # in alphabetical order

    content = "\n".join(LINK.format(href=os.path.abspath(dirname), txt=dirname) for dirname in dirs)

    with open("index.html", "w") as outf:
        fields = {
            "title":      "My Directory List",
            "stylesheet": "css/normalize.css",
            "content":    content
        }
        outf.write(TEMPLATE.format(**fields))

if __name__=="__main__":
    main()

rawDescription: Why does my code only write the last line?


rawDescription: Make a draggable black box

description: Make a draggable black box


code:
permutaties = itertools.permutations([nltk.UnigramTagger, nltk.BigramTagger, nltk.TrigramTagger, nltk.DefaultTagger],3)
resultaten = []
for element in permutaties:
     resultaten.append(accuracy(element, brown_Train, brown_element))

def accuracy(element, brown_train,brown_element):
     if element is nltk.DeafultTagger:
        evaluator = element("NN")
     else:
        evaluator = element(brown_train, backoff=XXX)  #maybe insert more elif
                    #clauses to retrieve the proper backoff parameter --or you could
                    # usr a tuple in the call to permutations so the apropriate backoff
                    #is avaliable for each function to be called
     return  evaluator.evaluate(brown_test) # ? I am not shure  from your code if this is your intent

rawDescription: Iterate through different permutations of 4 functions in Python


code:
def fs2InvoiceNo(*args):
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
#get the XText interface
    method = ""
    try:
        text = model.Text
        tRange = text.End
        cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
        oTable = cursor.TextTable
        oCurCell = cursor.Cell
        method = "Doc"
    except:
        pass
    try:
        sheets = model.getSheets()
        sheet = model.CurrentController.getActiveSheet()
        oSelection = model.getCurrentSelection()
        oArea = oSelection.getRangeAddress()
        first_row = oArea.StartRow
        last_row = oArea.EndRow
        first_col = oArea.StartColumn
        last_col = oArea.EndColumn
        method = "Calc"
    except:
        pass
    if method == "":
        raise Exception("Unknown environment for this script")
#and get the string from Footswitch2 via a TCP port
    import os, socket, time
    from configobj import ConfigObj
    configuration_dir = os.environ["HOME"]
    config_filename = configuration_dir + "/fs2.cfg"
    if  os.access(config_filename, os.R_OK):
        pass
    else:
        return None
    cfg = ConfigObj(config_filename)
    #define values to use from the configuration file
    tcp_port = int(cfg["control"]["TCP_PORT"])
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.5)
    try:
        sock.connect(("localhost", tcp_port))
    except:
        return None
    sock.settimeout(0.5)
    try:
        sock.send(bytes('invoicenumber\n', 'UTF-8'))
    except:
        return None
    try:
        time.sleep(0.2)
        s_list = sock.recv(1024).decode('UTF-8')
        s_list = s_list.split("\n")
    except:
        return None
    lines_in_response = len(s_list)
    if lines_in_response is None:
        return None
    invoice_no = s_list[0]
    if invoice_no == "":
        invoice_no = "None"
    if method == "Doc":
        if oCurCell == None: # Are we inserting into a table or not?
            text.insertString(cursor, invoice_no, 0)
        else:
            cell = oTable.getCellByName(oCurCell.CellName)
            cellCursor = cell.createTextCursor() # use this cursor if you want the text inserted at the beginning of the cell
            cell.insertString(cursor, invoice_no, False) # use the standard cursor to insert at the current position
    else:
        cell = sheet.getCellByPosition(first_col, first_row)
        cell.String = invoice_no
    sock.close()
    return None

rawDescription: How do I test in a LibreOffice python macro whether I'm in a Writer or Calc document?

description: Test in a LibreOffice python macro whether I'm in a Writer or Calc document


rawDescription: Slicing a string into a list

description: Slicing a string into a list


code:
counter = 1
img_sources = sel.xpath("//img/@src").extract()
for img_source in img_sources:
    download_image(img_source, os.path.join(url, "%d.jpg" % (counter)))
    counter += 1

rawDescription: How to rename images downloaded by scrapy

description: Rename images downloaded by scrapy


code:
class Instance():
    def __init__(self, number):
        self.number = number

    def __lt__(self, other):
        return self.number < other.number
        # now min(iList) just works

rawDescription: Python - Minimum of a List of Instance Variables


code:
def binP(N, p, x1, x2):
    p = float(p)
    q = p/(1-p)
    k = 0.0
    v = 1.0
    s = 0.0
    tot = 0.0

    while(k<=N):
            tot += v
            if(k >= x1 and k <= x2):
                    s += v
            if(tot > 10**30):
                    s = s/10**30
                    tot = tot/10**30
                    v = v/10**30
            k += 1
            v = v*q*(N+1-k)/k
    return s/tot

def calcBin(vx, vN, vCL = 95):
    '''
    Calculate the exact confidence interval for a binomial proportion

    Usage:
    >>> calcBin(13,100)
    (0.07107391357421874, 0.21204372406005856)
    >>> calcBin(4,7)
    (0.18405151367187494, 0.9010086059570312)
    '''
    vx = float(vx)
    vN = float(vN)
    #Set the confidence bounds
    vTU = (100 - float(vCL))/2
    vTL = vTU

    vP = vx/vN
    if(vx==0):
            dl = 0.0
    else:
            v = vP/2
            vsL = 0
            vsH = vP
            p = vTL/100

            while((vsH-vsL) > 10**-5):
                    if(binP(vN, v, vx, vN) > p):
                            vsH = v
                            v = (vsL+v)/2
                    else:
                            vsL = v
                            v = (v+vsH)/2
            dl = v

    if(vx==vN):
            ul = 1.0
    else:
            v = (1+vP)/2
            vsL =vP
            vsH = 1
            p = vTU/100
            while((vsH-vsL) > 10**-5):
                    if(binP(vN, v, 0, vx) < p):
                            vsH = v
                            v = (vsL+v)/2
                    else:
                            vsL = v
                            v = (v+vsH)/2
            ul = v
    return (dl, ul)

rawDescription: Is there any python function/library for calculate binomial confidence intervals?


code:
def _nd_window(data, filter_function):
    """
    Performs an in-place windowing on N-dimensional spatial-domain data.
    This is done to mitigate boundary effects in the FFT.

    Parameters
    ----------
    data : ndarray
           Input data to be windowed, modified in place.
    filter_function : 1D window generation function
           Function should accept one argument: the window length.
           Example: scipy.signal.hamming
    """
    for axis, axis_size in enumerate(data.shape):
        # set up shape for numpy broadcasting
        filter_shape = [1, ] * data.ndim
        filter_shape[axis] = axis_size
        window = filter_function(axis_size).reshape(filter_shape)
        # scale the window intensities to maintain image intensity
        np.power(window, (1.0/data.ndim), output=window)
        data *= window

rawDescription: Extending 1D function across 3 dimensions for data windowing


code:
entity = MyModel(parent = parentkey,
                 name   = 'somename')
entity.put()

entity_by_id = MyModel.get_by_id(entity.key().id(), parent = parentkey)

rawDescription: google app engine python get_by_id does not function

description: Google app engine python get_by_id does not function


code:
class googleAppSpider(Spider):
    name = "googleApp"
    allowed_domains = ['play.google.com']
    start_urls = ['https://play.google.com/store/apps/category/GAME/collection/topselling_new_free?authuser=0']

    def parse(self,response):
        for i in range(0,10):
            yield FormRequest(url="https://play.google.com/store/apps/category/GAME/collection/topselling_new_free?authuser=0", method="POST", formdata={'start':str(i*60),'num':'60','numChildren':'0','ipf':'1','xhr':'1','token':'m1VdlomIcpZYfkJT5dktVuqLw2k:1455483261011'}, callback=self.data_parse)

    def data_parse(self,response):
        item = googleAppItem()
        map = {}
        links = response.xpath("//a/@href").re(r'/store/apps/details.*')
        for l in links:
            if l not in map:
                map[l] = True
                item['url'] = l
                yield item

rawDescription: How to simulate xhr request using Scrapy when trying to crawl data from an ajax-based webstie?

description: Simulate xhr request using Scrapy when trying to crawl data from an ajax-based webstie


code:
import sys
from PyQt4 import QtCore, QtGui

class Example(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)
        self._text = 'this is a test'
        self._index = 0
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.handleTimer)
        self.timer.start(200)

    def handleTimer(self):
        self._index += 1
        self.label.setText(self._text[:self._index])
        if self._index > len(self._text):
            self.timer.stop()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.setGeometry(300, 300, 250, 150)
    ex.show()
    sys.exit(app.exec_())

rawDescription: PyQt: Set text in a QLabel adding letter by letter


code:
for field in ['first_name', 'last_name']:
    value = getattr(self, field)
    setattr(self, field, value.title())

rawDescription: Django/Python update field values (during model save)


code:
tDict = {}

with open("Lexicon.txt", "r") as infile:
  for line in infile:
    s = line.split()
    tDict[s[0]] = s[1]

while True:
  wordIn = input("Enter the English word(s) to be translated (enter 'exit' to stop the program): ")

  if wordIn == "exit": break

  words = wordIn.split()
  for word in words:
    if word in tDict.keys():
      print(tDict[word])
    else:
      print("*" + word)

rawDescription: Using Loops in a Python Piglatin Translator


code:
from psutil import virtual_memory

mem = virtual_memory()
mem.total  # total physical memory available

rawDescription: Get total physical memory in Python

description: Get total physical memory in Python


code:
from pprint import pprint
pprint ([(i, scipy.stats.ks_2samp(dataset1, dataset2[:i])[1])
               for i in range(200,len(dataset2),200)])

rawDescription: scipy p-value returns 0.0

description: Scipy p-value returns 0.0


code:
l = [1,2,3]
m = l[:]
n = l
l.append(4)
m
n
n is l
m is l

rawDescription: Python : Why use "list[:]" when "list" refers to same thing?


code:
import csv
import collections

a = 'path/to/a'
b = 'path/to/b'
output = 'output/path'

pos = collections.defaultdict(list)

with open(b) as csvin:
    reader = csv.DictReader(csvin, delimiter='\t')
    for line in reader:
        pos[line['POS']].append(line['Sample_ID'])

with open(a) as csvin, open(output, 'wb') as csvout:
    reader = csv.DictReader(csvin, delimiter='\t')
    writer = csv.DictWriter(csvout, fieldnames=reader.fieldnames, delimiter='\t')
    writer.writeheader()
    for line in reader:
        fields = pos.get(line['POS'], [])
        for field in fields:
            line[field] = 'NA'
        writer.writerow(line)

rawDescription: Find a list of text and replace at matching fields


code:
page = 1
while page < num_pages + 1:
    # do stuff that possibly updates num_pages here
    page += 1

rawDescription: Dynamically change range in Python?

description: Dynamically change range in Python


code: zip(*(dct[i] for i in sorted(dct)))

rawDescription: Turning dictionary values into different lists - the dictionary values are tuples - Python


code:
import inspect

def caller():
    tup = inspect.stack()[2]
    return tup[0].f_globals[tup[3]] # <function peter at address>

def cry_wolf():
    func = caller()
    print("%s cried 'WOLF!'" % (func.__name__,)) # peter cried 'WOLF!'

def peter():
    cry_wolf()

rawDescription: Get function object of caller in Python 2.7?