code:
def setup_logger(logger):
    logger.setLevel(logging.DEBUG)

    sh = logging.StreamHandler()
    formatter = logging.Formatter(LOG_FORMAT)
    sh.setFormatter(formatter)

    def decorate_emit(fn):
    # add methods we need to the class
        def new(*args):
            levelno = args[0].levelno
            if(levelno >= logging.CRITICAL):
                color = '\x1b[31;1m'
            elif(levelno >= logging.ERROR):
                color = '\x1b[31;1m'
            elif(levelno >= logging.WARNING):
                color = '\x1b[33;1m'
            elif(levelno >= logging.INFO):
                color = '\x1b[32;1m'
            elif(levelno >= logging.DEBUG):
                color = '\x1b[35;1m'
            else:
                color = '\x1b[0m'
            # add colored *** in the beginning of the message
            args[0].msg = "{0}***\x1b[0m {1}".format(color, args[0].msg)

            # new feature i like: bolder each args of message
            args[0].args = tuple('\x1b[1m' + arg + '\x1b[0m' for arg in args[0].args)
            return fn(*args)
        return new
    sh.emit = decorate_emit(sh.emit)
    logger.addHandler(sh)

rawDescription: Color logging using logging module in Python


code:
import numpy

def rgba_unpremultiply(data):
    a = numpy.fromstring(data, 'B') # Treat the string as an array of bytes
    a = a.astype('I') # Cast array of bytes to array of uints, since temporary values needs to be larger than byte
    alpha = a[3::4] # Every 4th element starting from index 3
    alpha = numpy.where(alpha == 0, 255, alpha) # Don't modify colors where alpha is 0
    a[0::4] = a[0::4] * 255 // alpha # Operates on entire slices of the array instead of looping over each element
    a[1::4] = a[1::4] * 255 // alpha
    a[2::4] = a[2::4] * 255 // alpha
    return a.astype('B').tostring() # Cast back to bytes

rawDescription: Process RGBA data efficiently using python?


code:
with open(class_name, "r") as f:
    lines = f.readlines()

lines.append("{name} : {score}\n".format(name=name, score=score))

with open(class_name, "w") as f:  # re-opening as "w" will blank the file
    for line in sorted(lines):
        f.write(line)

rawDescription: Python writing to .txt alphabetically

description: Python writing to .txt alphabetically


code:
class Base():

    def __init__(self):
       self.a = 10

class Derived(Base):

    def __init__(self):
       super(Derived, self).__init__()  # calls Base.__init__
       self.b = 'abc'

def main():
    base = Base()
    derived = Derived()

rawDescription: Copying values from the object of base class in Python


code:
from django.contrib.comments.signals import comment_was_posted

def thank_user(sender, comment=None, request=None, **kwargs):
    messages.add_message( request, messages.SUCCESS, 'You comment has been posted!' )
comment_was_posted.connect(thank_user)

rawDescription: How to add a confirmation message for comments using both Django's built-in framework?

description: Add a confirmation message for comments using both Django's built-in framework


code:
# imports same as question

class TestRevisioning(test.TestCase):
    fixtures = ['MyModel']

    def testDelete(self):
        object1 = models.MyModel.objects.first()
        # set up initial revision
        with reversion.create_revision():
            object1.save()
        # continue with remainder of the test as per the question.
        # ... etc.

rawDescription: How to create initial revision for test objects when using django-reversion in test case

description: Create initial revision for test objects when using django-reversion in test case


code:
attrs_list = Entity.objects.filter(**filters).distinct().values_list('someattr', flat=True)
a = Character.objects.filter(someotherattr__in=attrs_list)

rawDescription: Comparing two QuerySets in Python/Django


code:
descriptionHtml = pageopen.xpath('/html/body//div/div/div//div//span/@data-description')
descriptionBody = lxml.html.fromstring(descriptionHtml)
descriptionText = descriptionBody.xpath('text()')

rawDescription: strip html tags from an xpath @attribute

description: Strip html tags from an xpath @attribute


code:
import subprocess
output = subprocess.check_output(['echo', 'Hello'], shell=True)
print(output)

rawDescription: executing command line, hide return and save it

description: Executing command line, hide return and save it


code:
mm = gensim.corpora.MmCorpus(bz2.BZ2File('enwiki-latest-pages-articles_tfidf.mm.bz2'))
lda = gensim.models.ldamulticore.LdaMulticore(corpus=mm, id2word=id2word, num_topics=100, workers=4)

rawDescription: gensim LdaMulticore not multiprocessing?

description: Gensim LdaMulticore not multiprocessing


code:
kwd_mark = object()     # sentinel for separating args from kwargs

def cached_call(*args, **kwargs):
    key = args + (kwd_mark,) + tuple(sorted(kwds.items()))
    return cache.get(key)

rawDescription: How to hash *args **kwargs for function cache?

description: Hash *args **kwargs for function cache


code:
def argcheck(stack, funcname, arglist, exceptlist):
    returnlist=[]
    count=0
    for xtype in arglist:
        if not xtype or stack[-1] in xtype:
            returnlist.append(stack[-1])
            stack.pop()
        else:
            raise Exception(funcname, exceptlist[count])

rawDescription: Check for type in Python as wildcard

description: Check for type in Python as wildcard


code:
from django.db.models import get_models

for model in get_models():
    admin.site.register(model)

rawDescription: Is there any way to activate django admin site without admin.py


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

fig, ax = plt.subplots(1,1)
x=np.linspace(np.pi,4*np.pi,100)
N=len(x)
ax.set_xlim(len(x))
ax.set_ylim(-1.5,1.5)
line,  = ax.plot([],[],'o-')

def init():
    line.set_ydata(np.ma.array(x[:], mask=True))
    return line,

def animate(i, *args, **kwargs):
    y=np.sin(x*i)
    line.set_data(np.arange(N),y)            # update the data
    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init,
     frames=100, interval=10, blit= False, repeat = False)
ani.save('2osc.mp4', writer="ffmpeg")
fig.show()

rawDescription: Saving animations using ffmpeg and matplotlib


code:
import sys, signal
from PyQt4 import QtGui, QtCore, QtWebKit

class Sp():
  def save(self, ok, frame=None):
    if frame is None:
        print ('main-frame')
        frame = self.webView.page().mainFrame()
    else:
        print('child-frame')
    print('URL: %s' % frame.baseUrl().toString())
    print('METADATA: %s' % frame.metaData())
    print('TAG: %s' % frame.documentElement().tagName())
    print()

  def handleFrameCreated(self, frame):
    frame.loadFinished.connect(lambda: self.save(True, frame=frame))

  def main(self):
    self.webView = QtWebKit.QWebView()
    self.webView.page().frameCreated.connect(self.handleFrameCreated)
    self.webView.page().mainFrame().loadFinished.connect(self.save)
    self.webView.load(QtCore.QUrl("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe_scrolling"))

signal.signal(signal.SIGINT, signal.SIG_DFL)
print('Press Crtl+C to quit\n')
app = QtGui.QApplication(sys.argv)
s = Sp()
s.main()
sys.exit(app.exec_())

rawDescription: How to get the html dom of a webpage and its frames

description: Get the html dom of a webpage and its frames


code:
def parse_place(place):
    """This returns an object how we want it returned."""
    location.formatted_address = place.get('formatted_address')
    location.latitude = place['geometry']['location']['lat']
    location.longitude = place['geometry']['location']['lng']

    latitude = place['geometry']['location']['lat']
    longitude = place['geometry']['location']['lng']
    return (location, (latitude, longitude))

class GoogleV3Place(GoogleV3):
    """Simply extends the GoogleV3 to bucket the object into a place"""

    def parse_json(self, page, exactly_one=True, parse_place_fn = parse_place):
        """Returns location, (latitude, longitude) from json feed."""
        if not isinstance(page, basestring):
            page = util.decode_page(page)
        self.doc = json.loads(page)
        places = self.doc.get('results', [])

        if not places:
            check_status(self.doc.get('status'))
            return None
        elif exactly_one and len(places) != 1:
            raise ValueError(
                "Didn't find exactly one placemark! (Found %d)" % len(places))

        if exactly_one:
            return parse_place_fn(places[0])
        else:
            return [parse_place_fn(place) for place in places]

rawDescription: Python Polymorphism. How do you overide a function within a method


code:
p = permutations(range(10), 2)
result = [str(x[0]) + str(x[1]) for x in p]

rawDescription: how to concatenate the numbers returned from python permutations()?

description: Concatenate the numbers returned from python permutations()


code:
def coerce(x, y):
    t = type(x + y)
    return (t(x), t(y))

rawDescription: coerce() equivalent in python 3

description: Coerce() equivalent in python 3


code:
word_list = ['banana', 'apple', 'pineapple']
sorted(word_list,
       key=lambda word: sum(ch in 'aeiou' for ch in word),
       reverse=True)

rawDescription: Sorting a list based on number of vowels in the words in python


rawDescription: Python Regular Express Lookahead multiple conditions


code: ["s" + str(i) for i in xrange(1,11)]

rawDescription: Equivalent of R's paste command for vector of numbers in Python


code:
re.findall('enum\s*{\s*([^}]*)};', 'enum {A,B,C};')
re.findall("typedef\s+struct\s+(\w+)\s+(\w+);", "typedef struct blah blah;")

rawDescription: Python regex to extract data from C header file


code:
def kp_find(ar, level):
    result = ar[:]
    sum_ = 0
    while sum_ < level:
        sum_ += result.pop()
    return result

kp_find([1,2,3,4,5,6,7,8,9,10], 25)

rawDescription: Return remaining items in list, if last n elements in list greater than a number


code:
#!/usr/bine/env python
import subprocess
import gtk

### Of course, you should have the gui built and know which widgets to use for this.
viewer = self.builder.get_widget('txtview')
proc = subprocess.Popen('ls -al /home'.split(), stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
while True:
    line = proc.stdout.readline()
    viewer.get_buffer().instert_at_cursor(line)
    if not line:
       break

rawDescription: Python GUI (glade) to display output of shell process


code:
def assign_group_to_newly_registered_users(sender, instance, **kwargs):
    group, created = Group.objects.get_or_create(name='Newly Registered Users')
    group.user_set.add(instance)

post_save.connect(assign_group_to_newly_registered_users, sender=User, dispatch_uid=__file__)

rawDescription: How do I programatically create a group with Django?

description: Programatically create a group with Django


code:
import multiprocessing
list_of_variables=something #a 2D numpy array with all your combinations of variable values,
             #each row has a different combination of parameter values
number_of_combinations=shape(list_of_variables)[0] #number of rows
def notebook(i):
    variables=list_of_variables[i,:]
    #all your code here
pool = multiprocessing.Pool(processes=n)  # Create a pool with n workers.
pool.map(notebook,range(number_of_combinations))
pool.close()
pool.join()

rawDescription: How to run an ipython notebook in parallel just changing some parameter values?

description: Run an ipython notebook in parallel just changing some parameter values


code:
import imp
imp.find_module('test') # A file I created at my current dir
imp.find_module('sys')  # A system module
imp.find_module('lxml') # lxml, which I installed with pip
imp.find_module('lxml')[1]

rawDescription: How do I find the path for a failed python import?

description: Find the path for a failed python import


code:
aList = [123, 'Xyz', 'zaRa', 'Abc', 123]
mystring = ''.join(map(str, aList))
for letter in 'XRA':
    print('{} = {}'.format(letter, mystring.count(letter)))

rawDescription: Counting specific character in list [python]


code:
from astropy.table import Table
import pandas as pd

t = Table.read('file.fits')
df = pd.DataFrame.from_records(t, columns=t.columns)
df.AC = df.AC.astype(float)

rawDescription: How to change the dtype of a numpy recarray when one of the columns is an array?

description: Change the dtype of a numpy recarray when one of the columns is an array


code:
import numpy as np
t = df["time"]
dat = df["data"]
p = np.poly1d(np.polyfit(t,data,2))

rawDescription: pandas extrapolation of polynomial

description: Pandas extrapolation of polynomial


code:
from PyQt4 import QtGui

class MyWindow(QtGui.QMainWindow):

    def __init__(self, parent):
        QtGui.QMainWindow.__init__(self, parent)

        table = QtGui.QTableWidget(3, 3, self)  # create 3x3 table
        table.setHorizontalHeaderLabels(('Col 1', 'Col 2', 'Col 3'))
        table.setVerticalHeaderLabels(('Row 1', 'Row 2', 'Row 3'))
        for column in range(3):
            for row in range(3):
                table.setItem(row, column, QtGui.QWidget(self))  # your contents

        self.setCentralWidget(table)
        self.show()

rawDescription: adding a header to pyqt list

description: Adding a header to pyqt list


code:
import inspect

listOfFunctions = [func_name for func_name, func in module.__dict__.iteritems()\
                  if inspect.isfunction(func)]

for func_name in listOfFunctions:
    array_func = getattr(array, func_name)
    array_func()

rawDescription: How to call every function in an imported python module

description: Call every function in an imported python module


rawDescription: how to parse values with different delimiter in python

description: Parse values with different delimiter in python


code:
def incrementA(self, node):
  if node.name == "Node A": self.valueOfA += 1
  else: raise ValueError("node is not authorized to increment A")

rawDescription: How can you provide this access specifier protection in python?

description: Provide this access specifier protection in python


code:
filename = input ("filename: ");
with open (filename, "w") as f:
  f.write (input ());

rawDescription: Take user input and put it into a file in Python?


rawDescription: Remove `dtype` fields from structured array


rawDescription: 2d boolean array using n^2 bits of space


code:
def parse_file():
  with open('SOMETHING.txt', 'r') as f:
    c1 = 0
    c2 = 0
    for i in f:
      w = i.split()
      c1 += len(w)
      for j in w:
        if j.endswith('LY'):
          c2 += 1
    return {'Total words': c1, 'Words ending with LY': c2}

rawDescription: Count every word in a text file python

description: Count every word in a text file python


code:
import pandas as pd
pd.set_option('display.notebook_repr_html', False)

rawDescription: How can I remove borders from display of dataframe in iPython QtConsole (Anaconda)?

description: Remove borders from display of dataframe in iPython QtConsole (Anaconda)


code:
idTracker = {}
idCounter = 1
for id, movie in movies.iteritems():
    corpus.append(movie)
    idTracker[idCounter] = id
    idCounter +=1

rawDescription: Recuperating original term doc-id in sci-kit tfidf Vectorizer


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

df = pandas.DataFrame(dict(graph=['Item one', 'Item two', 'Item three'],
                           n=[3, 5, 2], m=[6, 1, 3]))

ind = np.arange(len(df))
width = 0.4

fig, ax = plt.subplots()
ax.barh(ind, df.n, width, color='red', label='N')
ax.barh(ind + width, df.m, width, color='green', label='M')

ax.set(yticks=ind + width, yticklabels=df.graph, ylim=[2*width - 1, len(df)])
ax.legend()

plt.show()

rawDescription: How to plot multiple horizontal bars in one chart with matplotlib

description: Plot multiple horizontal bars in one chart with matplotlib


code:
class SplitBoolOpPass1(ast.NodeTransformer):
  def visit_If(self, node):
      while isinstance(node.test, ast.BoolOp):
        new_node = ast.If(test=node.test.values.pop(), body=node.body, orelse=node.orelse)
        if isinstance(node.test.op, ast.And):
          if len(node.test.values) == 1:
            node.test = node.test.values[0]
          node.body = [new_node]
        else:
          if len(node.test.values) == 1:
            node.test = node.test.values[0]
          node.orelse = [new_node]
      node = self.generic_visit(node) # recusion
      return node

rawDescription: Python tracing and conditional jumps

description: Python tracing and conditional jumps


code:
dont_start_with_these = ('10.', '172.16.', '172.31.', '192.168')

filtered_ip = [ip for ip in fil2 if not ip[0].startswith(dont_start_with_these)]

rawDescription: Filterig/removing/excluding private IPs with port from a nested list


code:
import re
str = "Hello, *world*! Let's /go/."
str = re.sub(r"\*([^\*]*)\*", r"\textbf{\1}", str)
str = re.sub(r"/([^/]*)/",   r"\textit{\1}", str)
str

rawDescription: How does one parse simple inline markup (i.e. *bold*), in Python?

description: Parse simple inline markup (i.e. *bold*), in Python


code:
valid_column_sorts = ['size', 'last_modified', 'path']

if column_sort in valid_column_sorts:
    if sort == 'desc':
        column_sort = '-' + column_sort
    results = results.order_by(column_sort)

rawDescription: Better way to do conditional django queries


code:
p = mystring.find("http:")
substring = mystring[:p]

rawDescription: Remove From Substring On Python

description: Remove From Substring On Python


code:
from django.db.models import Max

SensorType.objects.filter(sensorreading__device=device) \
                  .annotate(newest_reading_id=Max('sensorreading__id'))

rawDescription: Annotate with a filtered related object set


code:
q1 = session.\
     query(beard.person.label('person'),
           beard.beardID.label('beardID'),
           beard.beardStyle.label('beardStyle'),
           sqlalchemy.sql.null().label('moustachID'),
           sqlalchemy.sql.null().label('moustachStyle'),
     ).\
     filter(beard.person == 'bob')

q2 = session.\
     query(moustache.person.label('person'),
           sqlalchemy.sql.null().label('beardID'),
           sqlalchemy.sql.null().label('beardStyle'),
           moustache.moustachID,
           moustache.moustachStyle,
     ).\
     filter(moustache.person == 'bob')

result = q1.union(q2).all()

rawDescription: RIGHT OUTER JOIN in SQLAlchemy

description: RIGHT OUTER JOIN in SQLAlchemy


code:
with open("Input.txt") as in_file:
    print(next(idx for idx, line in enumerate(in_file) if "b" in line))
# 1

rawDescription: position of string in a file

description: Position of string in a file


code:
n, k = int(input()), int(input())
num = sorted([ int(input()) for i in range(0,n) ])
min_unfairness = min( max_value - min_value for min_value, max_value in    zip(num, num[k-1:]) )
print(min_unfairness)

rawDescription: Performance Improvement Basics in Python


code:
tuple1 = (1, 2)
tuple2 = (1, 2, 3, 4, 5)
set(tuple1).issubset(tuple2)
set(tuple2).issuperset(tuple1)

rawDescription: How to check if all elements in a tuple or list are in another?

description: Check if all elements in a tuple or list are in another


code:
spellOne(number)
print (result)
return(result)

rawDescription: Python: Why doesn't my program print anything?


code:
#-------------------------------------------------------------------
# integerbox2 - like integerbox(), but without bounds checking.
#-------------------------------------------------------------------
def integerbox2(msg=""
               , title=" "
               , default=""
               , image=None
               , root=None):
    """
    Show a box in which a user can enter an integer.

    In addition to arguments for msg and title, this function accepts
    an integer argument for "default".

    The default argument may be None.

    When the user enters some text, the text is checked to verify that it
    can be converted to an integer, **no bounds checking is done**.

    If it can be, the integer (not the text) is returned.

    If it cannot, then an error msg is displayed, and the integerbox is
    redisplayed.

    If the user cancels the operation, None is returned.

    :param str msg: the msg to be displayed
    :param str title: the window title
    :param str default: The default value to return
    :param str image: Filename of image to display
    :param tk_widget root: Top-level Tk widget
    :return: the integer value entered by the user

    """
    if not msg:
        msg = "Enter an integer value"

    # Validate the arguments and convert to integers
    exception_string = ('integerbox "{0}" must be an integer.  '
                        'It is >{1}< of type {2}')
    if default:
        try:
            default=int(default)
        except ValueError:
            raise ValueError(exception_string.format('default', default,
                                                     type(default)))

    while 1:
        reply = enterbox(msg, title, str(default), image=image, root=root)
        if reply is None:
            return None
        try:
            reply = int(reply)
        except:
            msgbox('The value that you entered:\n\t"{}"\n'
                   'is not an integer.'.format(reply), "Error")
            continue
        # reply has passed validation check, it is an integer.
        return reply

rawDescription: Python EasyGUI- Integerbox Bound Limits

description: Python EasyGUI- Integerbox Bound Limits


code:
from django.conf import settings

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions

from django.http import HttpResponse

import requests
import mimetypes

class Reports(APIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request, ssn, validity, file):
        response = requests.get(settings.PROXY_BASE_URL + "/reports/" + ssn + "/" + validity + "/" + file)
        mimetype = mimetypes.guess_type(settings.PROXY_BASE_URL + "/reports/" + ssn + "/" + validity + "/" + file) #Return an array
        return HttpResponse(response, content_type=mimetype[0])

rawDescription: Using Django Rest Framework as security layer for file system processing


code:
from tempfile import mkstemp
from shutil import move
from os import remove, close

def replace(file_path, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with open(abs_path,'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(line.replace(pattern, subst))
    close(fh)
    #Remove original file
    remove(file_path)
    #Move new file
    move(abs_path, file_path)

rawDescription: Search and replace a line in a file in Python


code:
send_time_ms = time.time()
...
recv_time_ms = time.time()
rtt_in_ms = round(recv_time_ms - send_time_ms, 3)

rawDescription: Calculate RTT with 3 decimal places in Python 3


code:
def get_inputs():
    # a list to collect the inputs
    rval = []

    for colours in range(4):
        # range(4) will walk through [0,1,2,3]

        # this will add to the end of the list
        rval.append(userInput())

    # after the for loop ran four times, there will be four items in the list
    return rval

rawDescription: Assigning many similar variables in a for loop


code:
class Singleton(object):
    _instance = None
    a=0
    def __new__(self):
        if not self._instance:
            self._instance = super(Singleton,self).__new__(self)
        self._instance.a+=1
        return self._instance

rawDescription: Why is this printing 5 5 5 instead of 5 6 7? (multiple singleton calls)


code:
from django.core.paginator import Paginator

def events_view(request):
    # range of queryset sent in request params

    start_time = int(request.GET.get('start_time'))
    end_time = int(request.GET.get('end_time'))
    page_no = int(request.GET.get('page_no', 1))

    objects = Event.objects.filter(event_time__gte = start_time,
                         event_time__lte = end_time).order_by('event_time')
    p = Paginator(objects, 10)

    requested_page = p.page(page_no)
    requested_objects_list = requested_page.object_list #  required objects list

    return HttpResponse(status=200)

rawDescription: Django: Get queryset after a certain ID

description: Django: Get queryset after a certain ID


code:
def score_func(y_true, y_pred):
    """Calculate f1 score given the predicted and expected labels"""
    return f1_score(y_true, y_pred, pos_label='yes')

f1_scorer = make_scorer(score_func)
GridSearchCV(svr, parameters, scoring=f1_scorer)

rawDescription: Machine learning gridsearch for svm

description: Machine learning gridsearch for svm


code:
import itertools

for c1 in cars:
    collides_environment(c1)

for c1, c2 in itertools.combinations(cars):
    collides(c1, c2)

rawDescription: How do I handle multiple sprites collisions when there are >10 sprites?

description: Handle multiple sprites collisions when there are >10 sprites


code:
l = [1.0,"foo",2,3,"bar"]
print(sorted(l, key=lambda x: isinstance(x,int)))
[1.0, 'foo', 'bar', 2, 3]

rawDescription: Sorted by type in Python

description: Sorted by type in Python


code:
import os
from subprocess import Popen, PIPE

outside, inside = os.pipe()

one = Popen(['/path/to/one'], stdout = inside)
two = Popen(['/path/to/two'], stdout = inside)

merge = Popen(['/path/to/merge'], stdin = outside)

rawDescription: Linking two subprocess output to another subprocesses input


code:
from django.contrib.localflavor.us.forms import USStateField

class UserProfileForm(forms.ModelForm):
    state = USStateField()
    zip = USZipCodeField()
    class Meta:
        model = UserProfile

rawDescription: Django - USStateField vs USZipCodeField

description: Django - USStateField vs USZipCodeField


rawDescription: Hex string to signed int in Python 3.2?

description: Hex string to signed int in Python 3.2


code:
import csv
import os
import sys

def open_csv(filename, mode='r'):
    """ Open a csv file in proper mode depending on Python verion. """
    return (open(filename, mode=mode+'b') if sys.version_info[0] == 2 else
            open(filename, mode=mode, newline=''))

def process_file(filename, start_date=None, new_dir=None):
    # Read the entire contents of the file into memory skipping rows before
    # any start_date given (assuming row[0] is a date column).
    with open_csv(filename, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)  # Save first row.
        contents = [row for row in reader if start_date and row[0] >= start_date
                                                or not start_date]

    # Create different output file path if new_dir was specified.
    basename = os.path.basename(filename)  # Remove dir name from filename.
    output_filename = os.path.join(new_dir, basename) if new_dir else filename
    if new_dir and not os.path.isdir(new_dir):  # Create directory if necessary.
        os.makedirs(new_dir)

    # Open the output file and create a CSV writer for it.
    with open_csv(output_filename, 'w') as f:
        writer = csv.writer(f)

        # Add name of new column to header.
        header = ['Pipe'] + header  # Prepend new column name.
        writer.writerow(header)

        # Data for new column is the base filename without extension.
        new_column = [os.path.splitext( os.path.split(basename)[1] )[0]]

        # Process each row of the body by prepending data for new column to it.
        writer.writerows((new_column+row for row in contents))

rawDescription: delete rows by date and add file name column for multiple csv

description: Delete rows by date and add file name column for multiple csv


code:
def CronbachAlpha(itemscores):
    itemscores = numpy.asarray(itemscores)
    itemvars = itemscores.var(axis=1, ddof=1)
    tscores = itemscores.sum(axis=0)
    nitems = len(itemscores)

    return nitems / (nitems-1.) * (1 - itemvars.sum() / tscores.var(ddof=1))

rawDescription: Improving performance of Cronbach Alpha code python numpy


code:
import itertools

df_results = list()
orderings = itertools.permutations(df["b"].unique())
for ordering in orderings:
    df_2 = df.copy()
    df_2["b_key"] = pd.Categorical(df_2["b"], [i for i in ordering])
    df_2.sort_values("b_key", inplace=True)
    df_2.drop(["b_key"], axis=1, inplace=True)
    df_results.append(df_2)

for df in df_results:
    print(df)

rawDescription: Permute groups in Pandas

description: Permute groups in Pandas


code:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(1)
H, xedges, yedges = np.histogram2d(x, y, bins=50, weights=w)
H2, _, _ = np.histogram2d(x,y, bins=50)
extent = [0,1, xedges[-1], xedges[0]]
plt.imshow(H/H2, extent=extent,interpolation='nearest')
plt.colorbar()
plt.show()

rawDescription: Python 2d-histogram success rate per bin


code:
from scipy import stats
class your_distribution(stats.rv_continuous):
    def _pdf(self, x):
        return ( sin(x) ** (-0.75) ) / (4.32141 * (x ** (1/5)))

distribution = your_distribution()
distribution.rvs()

rawDescription: How to simulate from an (arbitrary) continuous probability distribution?

description: Simulate from an (arbitrary) continuous probability distribution


code:
import re
value = """2016-01-15T11:40:00-08:00 Valedictorian candidates meet for instructions
2016-02-26T15:05:00-08:00 Valedictorian written submissions due
2016-03-17 St. Patrick's Day
2016-03-25 Good Friday
2016-03-27 Easter Sunday
"""
re.sub(r'T[\d:-]+ ', ' ', v)

rawDescription: Python Regular Expression

description: Python Regular Expression


code:
s = "abc def, abcdef"
w = {"abc": "xxx", "def": "yyy"}
def replace(text, words):
    regex = r"\b(?:" + "|".join(re.escape(word) for word in words) + r")\b"
    reobj = re.compile(regex, re.I)
    return reobj.sub(lambda x:words[x.group(0)], text)
replace(s, w)

rawDescription: Parsing text with python and mapping to dictionary words


code:
data = np.zeros((W, H), dtype=np.ubyte)
## (fill in data here)

## Use m=33 since this is apparently the only mode with
## square pixels and also the highest resolution
## (unless it prints too slowly for your liking)
m = 33

nH = W // 256  ## note this is integer division, but SO's
               ## syntax hilighting thinks it looks like a comment.
nL = W % 256

## Divide the array into sections with shape Wx24:
for n in range(data.shape[1] // 24):
    ## Note that if the image height is not a multiple of 24,
    ## you'll have to pad it with zeros somehow.

    strip = data[:, n*24:(n+1)*24]

    ## Convert each strip into a string of bytes:

    strip = strip.reshape(W, 3, 8)
    bytes = (strip * (2**np.arange(8)[np.newaxis, np.newaxis, :])).sum(axis=2) # magic
    byteString = bytes.astype(np.ubyte).tostring()

    ## Send the command to POS

rawDescription: Billing aliens via POS printer and image print


code:
test = con.cursor()
test.execute("SELECT COUNT(*) FROM programs")
x = test.fetchone()[0]
for running_total in xrange(1, x + 1, 69):
    print(running_total)

rawDescription: Finding the rows of data in a database

description: Finding the rows of data in a database


code:
import ast
s = "['accounting', 5, 9, 11, 0, 0]"
ast.literal_eval(s)

rawDescription: Python not recognizing list data as a list upon import


code:
from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle
from win32file import GENERIC_WRITE, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, FILE_SHARE_WRITE

import os
import shutil

source_dir_path = r"c:\my_source"
destination_dir_path = r"c:\my_target"

creation_times = [(d, os.path.getctime(os.path.join(source_dir_path, d))) for d in os.listdir(source_dir_path)]

for filename, ctime in creation_times:
    src = os.path.join(source_dir_path, filename)
    target = os.path.join(destination_dir_path, filename)
    shutil.copy(src, target)

    fh = CreateFile(target, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
    SetFileTime(fh, ctime, ctime, ctime)
    CloseHandle(fh)

rawDescription: Changing directory create times (ctime) using python in windows


code:
def make_list(d):
    def append_children(parent, d):
        children = [[x['Child']] for x in d if x['Parent'] == parent[0]]
        if children:
            parent.append(children)
            for child in children:
                append_children(child, d)

    results = [[x['Child']] for x in d if x['Parent'] == 'top']
    for parent in results:
        append_children(parent, d)

    return results

rawDescription: Django - create nested html list from list of dictionary objects


code:
for i, line in enumerate(f, no_of_headers):
    a,b,c = line.strip().split() # splits the line by whitespace

rawDescription: Parsing multiple values from a file in Python


code:
for f in in_files:
    file = os.path.join(path, f)
    with open(file, "r") as text:
        hit_count = 0
        hit_count1 = 0
        for line in text:
            if moldesc[0] in line:
                Text1 = line.split()[-1]
                hit_count = 1
            if moldesc[1] in line:
                Text2 = line.split()[-1]
                hit_count1 = 1
            if hit_count and hit_count1:
                print >> desc, f + "," + Text1 + "," + Text2
                break

rawDescription: Python extract text files

description: Python extract text files


code:
x = input("Enter your string")
while not set(x).issubset({'m', 'u', 'i'}):
    print("false")
    x = input("Enter your string")
else:
    print("true")

rawDescription: How to continue loop until true

description: Continue loop until true


code:
def _read_write(self):

    # This allows us to get multiple
    # lower-level timeouts before we give up.
    # (but see later note about Python 3)

    time_out_max = self.timeout/3

    # We have two sockets we care about

    socs = [self.client, self.target]

    # Loop until error or timeout
    count = 0
    while 1:
        count += 1

        # select is very efficient.  It will let
        # other processes execute until we have
        # data or an error.

        # We only care about receive and error
        # conditions, so we pass in an empty list
        # for transmit, and assign transmit results
        # to the _ variable to ignore.

        # We also pass a timeout of 3 seconds, which
        # is why it's OK to divide the timeout value
        # by 3 above.

        # Note that select doesn't read anything for
        # us -- it just blocks until data is ready.

        (recv, _, error) = select.select(socs, [], socs, 3)

        # If we have an error, break out of the loop

        if error:
            break

        # If we have receive data, it's from the client
        # for the target, or the other way around, or
        # even both.  Loop through and deal with whatever
        # receive data we have and send it to the other
        # port.

        # BTW, "if recv" is redundant here -- (a) in
        # general (except for timeouts) we'll have
        # receive data here, and (b) the for loop won't
        # execute if we don't.

        if recv:
            for in_ in recv:

                # Read data up to a max of BUFLEN,

                data = in_.recv(BUFLEN)

                # Dump the data out the other side.
                # Indexing probably would have been
                # more efficient than this if/else

                if in_ is self.client:
                    out = self.target
                else:
                    out = self.client

                # I think this may be a bug.  IIRC,
                # send is not required to send all the
                # data, but I don't remember and cannot
                # be bothered to look it up right now.

                if data:
                    out.send(data)
                    # Reset the timeout counter.
                    count = 0

        # This is ugly -- should be >=, then it might
        # work even on Python 3...

        if count == time_out_max:
            break

    # We're done with the loop and exit the function on
    # either a timeout or an error.

rawDescription: Python select used in python program

description: Python select used in python program


rawDescription: Python OpenCV cv.WaitKey spits back weird output on Ubuntu modulo 256 maps correctly


code:
def get_tasks():

    page = request.args.get('page')
    if page is not None:
        try:
            page = int(request.args.get('page'))
        except:
            return jsonify({'result': 'Invalid parameter'})
    else:
        page = 1
    tasks= models.Task.newest().paginate(page, 7, True)
    # This list will hold all your tasks in JSON format
    jsontasks = []
    # Since tasks is a pagination object, you should use tasks.items
    for task in tasks.items:
        jsontasks.append(task.to_json())
    #How do I return the tasks in an appropriate format? Like this..
    return jsonify({'tasks': jsontasks, 'success': 1})

rawDescription: Flask REST API responding with a JSONArray


code:
mystyle = easyxf('pattern: pattern solid, fore_colour blue')

for row in data:
    rowx += 1
    for colx, value in enumerate(row):
        if rowx % 2 == 0:
            # apply style for even-numbered rows
            ws0.write(rowx, colx, value, mystyle)
        else:
            # no style for odd-numbered rows
            ws0.write(rowx, colx, value)

rawDescription: Python - excel - xlwt: colouring every second row


code:
class MyModel(models.Model):
    @classmethod
    def my_function(cls):
        new_instance = cls(...)
        new_instance.save()
        # etc...

rawDescription: How do I add a function to a model that doesn't require a model instance?

description: Add a function to a model that doesn't require a model instance


code:
data = []
with open(r'stuff.txt') as f:
    for line in f:
        fields = line.split()
        rowdata = map(float, fields)
        data.extend(rowdata)

print(sum(data)/len(data))

rawDescription: Finding average in .txt file python

description: Finding average in .txt file python


code: new_dict = { k: len(v) for k, v in old_dict.iteritems()}

rawDescription: Python: dict comprehensions: making a new dictionary with new values from old dictionary ids


code: json.dumps({ 'compressedData' : base64.b64encode(zString) })

rawDescription: UTF-8 compatible compression in python

description: UTF-8 compatible compression in python


code:
def reverse_id_lookup(id_code, users):
    id_code = int(id_code.split("#", 1)[1])
    for user, info in users.items():
        if info['id'] == id_code:
            return user
    else:
        raise Exception("ID does not exist yet.")

rawDescription: Reverse look up on user id

description: Reverse look up on user id


code:
import requests
r = requests.post("http://web-address.com", {'parameter': 'value'})
r.text

rawDescription: Is there any way to crawl a website with POST form?


code:
base = 42
a = (5,10)
b = (4,39)

if a > b:
   b, a = a, b

overflow = int(b[1] < a[1])

c = (b[0]-a[0]-overflow, b[1]+base*overflow-a[1])
print(c)

rawDescription: Difference of decimal numbers with another base


rawDescription: Encode and pad netbios name using python


code:
from mayavi.api import Engine
from mayavi.sources.api import ParametricSurface
from mayavi.modules.api import Surface
from mayavi import mlab
from tvtk.tools import visual

import numpy as np

def Arrow_From_A_to_B(x1, y1, z1, x2, y2, z2,scale=None):
    ar1=visual.arrow(x=x1, y=y1, z=z1)
    ar1.length_cone=0.4

    arrow_length=np.sqrt((x2-x1)**2+(y2-y1)**2+(z2-z1)**2)
    if scale is None:
        ar1.actor.scale=[arrow_length, arrow_length, arrow_length]
    else:
        ar1.actor.scale=scale
    ar1.pos = ar1.pos/arrow_length
    ar1.axis = [x2-x1, y2-y1, z2-z1]
    return ar1

engine = Engine()
engine.start()
scene = engine.new_scene()
scene.scene.disable_render = True # for speed

visual.set_viewer(scene)

surfaces = []
for i in range(2):
    source = ParametricSurface()
    source.function = 'ellipsoid'
    engine.add_source(source)

    surface = Surface()
    source.add_module(surface)

    actor = surface.actor # mayavi actor, actor.actor is tvtk actor
    #actor.property.ambient = 1 # defaults to 0 for some reason, ah don't need it, turn off scalar visibility instead
    actor.property.opacity = 0.7
    actor.property.color = (0,0,1) # tuple(np.random.rand(3))
    actor.mapper.scalar_visibility = False # don't colour ellipses by their scalar indices into colour map
    actor.property.backface_culling = True # gets rid of weird rendering artifact when opacity is < 1
    actor.property.specular = 0.1
    #actor.property.frontface_culling = True
    actor.actor.orientation = np.array([1,0,0]) * 360 # in degrees
    actor.actor.origin = np.array([0,0,0])
    actor.actor.position = np.array([0,0,0])
    actor.actor.scale = np.array([ 0.26490647,  0.26490647,  0.92717265])
    actor.enable_texture=True
    actor.property.representation = ['wireframe', 'surface'][i]
    surfaces.append(surface)

Arrow_From_A_to_B(0,0,0, 0.26490647, 0, 0,np.array([0.26490647,0.4,0.4]))
Arrow_From_A_to_B(0,0,0, 0, 0.26490647, 0,np.array([0.4,0.26490647,0.4]))
Arrow_From_A_to_B(0,0,0, 0, 0, 0.92717265,np.array([0.4,0.4,0.92717265]))

source.scene.background = (1.0,1.0,1.0)
scene.scene.disable_render = False # now turn it on

# set the scalars, this has to be done some indeterminate amount of time
# after each surface is created, otherwise the scalars get overwritten
# later by their default of 1.0
for i, surface in enumerate(surfaces):
    vtk_srcs = mlab.pipeline.get_vtk_src(surface)
    print('len(vtk_srcs) = %d' % len(vtk_srcs))
    vtk_src = vtk_srcs[0]
    try: npoints = len(vtk_src.point_data.scalars)
    except TypeError:
        print('hit the TypeError on surface i=%d' % i)
        npoints = 2500
    vtk_src.point_data.scalars = np.tile(i, npoints)

# on pick, find the ellipsoid with origin closest to the picked coord,
# then check if that coord falls within that nearest ellipsoid, and if
# so, print out the ellispoid id, or pop it up in a tooltip

mlab.show()

rawDescription: How to plot 3D ellipsoid with Mayavi

description: Plot 3D ellipsoid with Mayavi


code:
lists = {}
lists[strs] = []
lists[strs].append(somevalue)

rawDescription: Creating a list based on value stored in variable in python


code:
sample = [5, 1, 3, 9, 7, 13, 12, 5]
class_boundaries = [(1, 4), (4, 7), (7, 10), (10, 14)]
classified = [[X for X in sample if LO <= X <= HI] for LO,HI in class_boundaries]
counts = [sum(LO <= X <= HI for X in sample) for LO,HI in class_boundaries]

rawDescription: Creating a nested list referencing specific ranges


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

limit=10

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle(plt.cycler('color', plt.cm.Accent(np.linspace(0, 1, limit))))
for limit in range(1,limit+1):
    x=np.random.randint(10, size=10)
    y=limit*x
    plt.plot(x,y,lw=2, label=limit)
    plt.legend(loc='best')

rawDescription: How to add a new color in matplotlib graph (or use colormaps)?

description: Add a new color in matplotlib graph (or use colormaps)


code: a_file = open('chr_ord.txt', 'w', encoding='utf-8')

rawDescription: How to avoid automatic ASCII encoding on Python 3?

description: Avoid automatic ASCII encoding on Python 3


code: direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs)

rawDescription: Django: automatically import MEDIA_URL in context


code:
bad_extensions = {
    'bmp', 'dll', 'eot', 'exe', 'foo', 'gif', 'gnumeric', 'graffle', 'gz',
    'gzip', 'icns', 'ico', 'idx', 'jpg', 'js', 'map', 'nib', 'node', 'o',
    'ods', 'otf', 'pack', 'pdf', 'pfx', 'phar', 'png', 'ser', 'strings',
    'tgz', 'ttf', 'woff', 'woff2', 'xcf', 'xls', 'xlsx', 'xml', 'zip'
}

rawDescription: Python3 Fast Way To Check If Element In Collection Of Elements


rawDescription: ASCII to Hex Conversion in RS232 Serial Dialogue in Python and Hyperterminal


code:
recordtype = dtype([('name', str_, 20), ('age', int32), ('weight', float32)])
people = array([('Joaquin', 51, 60.0), ('Cat', 18, 8.6)], dtype=recordtype)
people

rawDescription: Reading a file with string and float with loadtxt


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

x=random.sample(1000)
y=np.arange(1000)
plt.plot(x,y)

rawDescription: horizontal plot in python

description: Horizontal plot in python


rawDescription: Writing a binary buffer to a file in python


code:
def get_info(soup, parse_line):
    info = parse_line(soup)

get_info(soup, lambda x: x.findAll('div',{'id':'hideinse'})[0]('a',href=True))

rawDescription: Pass parameter into Python function

description: Pass parameter into Python function


code:
s = "you ask my name then ill ask yours"
[val  + " = "  + str(index) for index, val in enumerate(s.split())]

rawDescription: how do you assign a value to every word in a list

description: Assign a value to every word in a list


code:
from tkinter import *

class App():
    def __init__(self):
        root = Tk()

        self.last_point = (0, 0)

        self.prev_var = StringVar(value='-:-')
        self.curr_var = StringVar(value='-:-')

        labels = Frame(root)
        labels.pack()

        Label(labels, text='Last Point Clicked: ').pack(side=LEFT)
        prev = Label(labels, textvariable=self.prev_var)
        prev.pack(side=LEFT)
        Label(labels, text='Current point: ').pack(side=LEFT)
        curr = Label(labels, textvariable=self.curr_var)
        curr.pack(side=LEFT)

        self.canvas = Canvas(root, background='white')
        self.canvas.pack()
        self.canvas.bind('<Button-1>', self.on_click)

        self.canvas.bind('<Motion>', self.on_motion)
        self.line = self.canvas.create_line(0, 0, 0, 0)
        self.curr_text = self.canvas.create_text(0, 0)

        root.mainloop()
    def on_click(self, event):
        # Last click in absolute coordinates
        self.prev_var.set('%s:%s' % self.last_point)
        # Current point in relative coordinates
        self.curr_var.set('%s:%s' % (event.x - self.last_point[0], event.y - self.last_point[1]))
        self.last_point = event.x, event.y
    def on_motion(self, event):
        self.canvas.coords(self.line, self.last_point[0], self.last_point[1], event.x, event.y)
        self.canvas.coords(self.curr_text, event.x, event.y)
        self.canvas.itemconfigure(self.curr_text, text="%s\n%s\n\n\n" % (event.x - self.last_point[0], event.y - self.last_point[1]))

App()

rawDescription: Python. Return current cursor position and position last left mouse click


code:
Sim = np.identity(n) # diagonal with ones (100 percent similarity)

for i in range(n):
    for j in range(i+1, n):    # +1 skips the diagonal
        Sim[i, j]= simfunction(list_doc[i], list_doc[j])

# Expand the matrix (copy triangle)
tril = np.tril_indices_from(Sim, -1) # take lower & upper triangle's indices
triu = np.triu_indices_from(Sim, 1)  # (without diagonal)
Sim[tril] = Sim[triu]

rawDescription: Use a similarity function for clustering scikit-learn


code:
values = []
def f2(a,b):
    values.append((a,b))
    #todo: actual function logic goes here

rawDescription: Python - Extract private variables from a function?


code:
class MyModel(models.Model):
    class Meta:
        ordering = ('name', ) #Or change to any other attributes.

rawDescription: django custom manager return ordering

description: Django custom manager return ordering


code:
def __lt__(self, other):
    if not isinstance(other, Rank):  # or self.__class__ instead of Rank
        return NotImplemented
    return self.value < other.value

rawDescription: Returning 5 best cards containing a pair from a set of 7 playing cards using Python


rawDescription: Conditional reset of data in panda dataframe


code:
def check_token(self=None):
    # whenever a user posts a form we verify that the csrf token is valid.
    if cherrypy.request.method == 'POST':
        token = cherrypy.session.get('_csrf_token')
        if token is None or cherrypy.request.params.get('csrf_token') == None or token != cherrypy.request.params['csrf_token']:
            raise cherrypy.HTTPError(403)

cherrypy.tools.Functions = cherrypy.Tool('before_handler', check_token)

rawDescription: CherryPy: How to process a request before it has reached the application method?


code:
import tkinter as tk

parent = tk.Tk()

menubar = tk.Menu(parent)
show_all = tk.BooleanVar()
show_all.set(True)
show_done = tk.BooleanVar()
show_not_done = tk.BooleanVar()

view_menu = tk.Menu(menubar)
view_menu.add_checkbutton(label="Show All", onvalue=1, offvalue=False, variable=show_all)
view_menu.add_checkbutton(label="Show Done", onvalue=True, offvalue=0, variable=show_done)
view_menu.add_checkbutton(label="Show Not Done", onvalue=1, offvalue=0, variable=show_not_done)
menubar.add_cascade(label='View', menu=view_menu)
parent.config(menu=menubar)

parent.mainloop()

rawDescription: Making Menu options with Checkbutton in Tkinter?


rawDescription: Float values as dictionary key

description: Float values as dictionary key


code:
import  wx
import  wx.calendar

class MyCalendar(wx.Frame):
    def __init__(self, *args, **kargs):
        wx.Frame.__init__(self, *args, **kargs)
        self.cal = wx.calendar.CalendarCtrl(self, -1, wx.DateTime_Now())
        self.timer = wx.Timer(self)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.update_date)

    def update_date(self, evt):
        date = wx.DateTime_Now()
        self.cal.SetDate(date)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyCalendar(None)
    frame.Show()
    app.MainLoop()

rawDescription: Setting the Date on the wx.calendar CalendarCtrl Widget


code:
class D(dict):
    def __missing__(self, key):
        self[key]=D()
        return self[key]

d=D()
d['a']['b']['c']=3

rawDescription: Generating python dict keys on the fly

description: Generating python dict keys on the fly


code:
connecting = ClientCreator(reactor, RedisListenerProtocol).connectTCP(HOST, PORT)
def connected(listener):
    listener.subscribe("newChannelName")
    listener.unsubscribe("channelName")
connecting.addCallback(connected)

rawDescription: Subscribe and unsubscribe to channels after the connection has been made with txredisapi


code:
from numpy import unravel_index,fill_diagonal,nanargmax,nanargmin
from bottleneck import nanmean

a = df(columns=['abc','xyz', 'jkl'])
a.loc['abc'] = [1, 0.2 , -0.01]
a.loc['xyz'] = [-0.34, 1, 0.23]
a.loc['jkl'] = [0.5, 0.4, 1]

b = a.values.copy()

fill_diagonal(b, None)

imax = unravel_index(nanargmax(b), b.shape)
imin = unravel_index(nanargmin(b), b.shape)
print(a.index[imax[0]],a.columns[imax[1]])
print(a.index[imin[0]],a.columns[imin[1]])
print(nanmean(b))

rawDescription: Getting mean, max, min from pandas dataframe


code:
def remove_accents(s):
   return ''.join((c for c in unicodedata.normalize('NFD', s.decode('utf-8')) if unicodedata.category(c) != 'Mn'))

rawDescription: Python utf-8 accents problem

description: Python utf-8 accents problem


code:
tau = np.array([.1, .2, .3])
num_repeats = 3
plot_colors = ['r-','b-','k-']
labels = ['tau = 0.1','tau = 0.2','tau = 0.3']
plot_handles=[None]*3

for k in np.arange(tau.size):
  for ell in np.arange(num_repeats):
    ( _, _, n_iter, history,_ ) = opti.minimise (theta0, f_tol=f_tol, theta_tol = theta_tol, tau=tau[k], N=3,report=50000, m=1)
    true_energy = -59.062
    error = np.absolute(true_energy - history[:,num_p])
    if ell == 0:
        label = labels[k]
    else:
        label = ''
    plot_handles[k] = plt.plot(np.arange(0, n_iter), error, plot_colors[k], label=label)

plt.xlabel('Iteration')
plt.ylabel('Absolute Error')
plt.yscale('log')
plt.legend(handles=plot_handles)
plt.show()

rawDescription: Python legend labels as expression

description: Python legend labels as expression


code:
def sliding(a, n):
    return (a[i:i+n] for i in xrange(len(a) - n + 1))

def substring_count(a, b):
    return sum(s == b for s in sliding(a, len(b)))

assert list(sliding('abcde', 3)) == ['abc', 'bcd', 'cde']
assert substring_count('ababaa', 'aba') == 2

rawDescription: Overlapping count of substring in a string in Python


code: e.xpath('.//input | .//select')

rawDescription: Using XPath with lxml.html

description: Using XPath with lxml.html


rawDescription: How do I implement a sliding window in Python?

description: Implement a sliding window in Python


code:
import pandas as pd

data = pd.DataFrame({'ID':[1,1,1,2,2,2,2,3,3,3],
                'yyyymm':[201501,201502,201503,201506,201507,201508,201509,201503,201504,201505],
                'value1':[0,1,3,0,0,1,0,0,0,0],
                'value2':[123,113,115,0,0,115,0,0,0,0]})

final = pd.DataFrame(columns=["ID", "time_value1", "value1", "time_value2", "value2"])

def findTimes(df):
    for index, row in df.iterrows():
        if row["value1"] > 0 or row["value2"] > 0:

            final.loc[index,"ID"] = row["ID"]
            final.loc[index,"time_value1"] = row["value1"]
            final.loc[index,"value1"] = row["value1"]
            final.loc[index,"time_value2"] = row["value2"]
            final.loc[index,"value2"] = row["value2"]

            break

data.sort_values("yyyymm").groupby("ID").apply(lambda x: findTimes(x))

rawDescription: Group processing in Pandas Python

description: Group processing in Pandas Python


code:
from subprocess import Popen, PIPE
dd_process = Popen(['dd', 'if=/dev/sda'], stdout=PIPE)
ssh_process = Popen(['ssh', 'root@example.com', 'dd','of=/dev/sda'],stdin=dd_process.stdout, stdout=PIPE)
dd_process.stdout.close() # enable write error in dd if ssh dies
out, err = ssh_process.communicate()

rawDescription: Python piping output between two subprocesses


code: primport = subprocess.check_output('/opt/mongodb/bin/mongo localhost27017 --eval "printjson(rs.isMaster())" | grep "primary" | cut -d ":" -f 2,3', shell = True)

rawDescription: fetching and splitting an address from MongoDB to only return the host ID

description: Fetching and splitting an address from MongoDB to only return the host ID


code:
dirname = "/some/path/"
filename = "somefile.txt"
pathname = os.path.abspath(os.path.join(dirname, filename))
if pathname.startswith(dirname):
   os.remove(pathname)

rawDescription: File deletion using rm command

description: File deletion using rm command


code:
import numpy as np
array = [1, 20, 55, 33, 4555555, 1]
arrayCompare = zip(array, array[1:] + [0], [0] + array)

weights = [.7 * x + .15 * (y + z) for x, y, z in arrayCompare]

avg = np.average(array, weights=weights)

rawDescription: Weighted smoothing of a 1D array - Python


code:
match = [ (season1, tour1, date1, home1, visitor1, home_score1, visitor_score1),
          (season2, tour2, date2, home2, visitor2, home_score2, visitor_score2),
          (season3, tour3, date3, home3, visitor3, home_score3, visitor_score3) ]

cur.executemany(sql_add, match)

rawDescription: Python script is locked when accessing SQLite database in loop


code: collection.find({'loc':{'$within':{'$polygon':[[linestart_x, linestart_y], [linestart_x+jiggle, linestart_y+jiggle], [lineend_x, lineend_y], [lineend_x+jiggle, lineend_y+jiggle]]}}})

rawDescription: Pymongo search geo on line

description: Pymongo search geo on line


code:
import pprint
people = [
    {"first": "Brian", "last": "Kernighan"},
    {"first": "Dennis", "last": "Richie"},
]
pprint.pformat(people, indent=4)

rawDescription: How do i get python's pprint to return a string instead of printing?

description: Get python's pprint to return a string instead of printing


code:
def findLines():
    myWord = 'someWordIWantToSearchFor'
    answer = []
    with open('daily.txt') as myfile:
        lines = myfile.readlines()
    for line in lines:
        if myWord in line:
            answer.append(line)
    return answer

rawDescription: How to find a word in a string in a list? (Python)

description: Find a word in a string in a list? (Python)


code:
import wx

DATA = {
    0 : (u"Test2", "123456", ),
    1 : (u"Test", "123456",),
    2 : (u"doe", "156789", ),
    3 : (u"John", "13455", )
}

class MyApp(wx.App):

    def OnInit(self):
        frame = MyFrame(None)
        frame.Show()
        self.SetTopWindow(frame)
        return True

class MyFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, wx.NewId(), size=(500, -1))
        wx.Frame.CenterOnScreen(self)
        self.panel = MyPanel(self)

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.list = MyListCtrl(self,3)
        self.add_button = wx.Button(self, label="add")
        h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        h_sizer.Add(self.add_button, proportion=1, flag=wx.ALL, border=5)
        v_sizer = wx.BoxSizer(wx.VERTICAL)
        v_sizer.Add(h_sizer, proportion=0, flag=wx.EXPAND)
        v_sizer.Add(self.list, proportion=1, flag=wx.EXPAND, border=5)
        self.SetSizer(v_sizer)
        self.add_button.Bind(wx.EVT_BUTTON,self.onAdd)

    def onAdd(self, event):
        self.new_w = NewWindow(self)
        self.new_w.Show()

class MyPopupMenu(wx.Menu):

    def __init__(self,parent, item):
        super(MyPopupMenu,self).__init__()
        self.parent = parent
        self.item = item
        menuEdit = wx.MenuItem(self,wx.NewId(), 'edit %s' % item.GetText())
        self.AppendItem(menuEdit)
        self.Bind(wx.EVT_MENU, self.onEdit, menuEdit)
        menuDel = wx.MenuItem(self,wx.NewId(), 'delete %s' % item.GetText())
        self.AppendItem(menuDel)
        self.Bind(wx.EVT_MENU, self.OnDelete, menuDel)

    def onEdit(self,e):
        self.parent.parent.edit_w = NewWindow(self.parent.parent)
        self.parent.parent.edit_w.Show()

    def OnDelete(self,e):
        self.parent.DeleteAllItems()

class MyListCtrl(wx.ListCtrl):
    def __init__(self, parent, columns):
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
        self.parent = parent
        self.R_MOUSE = 0
        self.InsertColumn(0, "name")
        self.InsertColumn(1, "phone")
        self.InsertColumn(2, "address")
        self.SetColumnWidth(0, 200)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelect)
        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.onRightClick)

        self.itemDataMap = {}
        self.refresh_list()

    def onRightClick(self,evt):
        pos = evt.GetPosition()
        self.PopupMenu(MyPopupMenu(self, self.itemSel), pos)

    def refresh_list(self):
        self.DeleteAllItems()
        for entry in DATA.items():
            self.Append(entry[1])
            self.SetItemData(entry[0],entry[0])

    def OnSelect(self, event):
        index = event.GetIndex()
        self.itemSel = self.GetItem(itemId=index)

class NewWindow(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, size=(400,250), style=wx.DEFAULT_FRAME_STYLE)
        self.parent = parent
        self.CenterOnParent()
        panel = wx.Panel(self)

    def onClick(self,event):
        self.Close()

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()

rawDescription: Wxpython: Couldn't retrieve information about list control item XXX


code:
from flask import Flask
from werkzeug.routing import BaseConverter

app = Flask(__name__)

class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]

app.url_map.converters['regex'] = RegexConverter

@app.route('/<regex("[abcABC0-9]{4,6}"):uid>-<slug>/')
def example(uid, slug):
    return "uid: %s, slug: %s" % (uid, slug)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

rawDescription: Does Flask support regular expressions in its URL routing?


code:
pattern = re.compile('.*?(\d+\D*)(\..*)')
k = pattern.search(image_id)
saved_value = k.groups()[0] # this will give you the value
                            # of the fist matched group
                            # as a string

rawDescription: regex match saving

description: Regex match saving


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

fig,ax = plt.subplots(1)
ax.set_aspect('equal')

# The data array
m1 = np.random.rand(5,5)
# The alpha array. Normalize your map2 to the range 0,1
m2 = np.linspace(0,1,25).reshape(5,5)

p = ax.pcolormesh(m1)
plt.savefig('myfig.png')

for i,j in zip(p.get_facecolors(),m2.flatten()):
    i[3] = j # Set the alpha value of the RGBA tuple using m2

plt.savefig('myfig.png')

rawDescription: Use a variable to set alpha opacity in a colormap


code: a = '{0:016b}'.format(int(strNUMBER))

rawDescription: Python string to binary string

description: Python string to binary string


code:
from django.db.models import F

Car.objects.filter(id__gte=id_parameter).update(F('field') + 1)

rawDescription: Django model views | How to Update objects from id_parameter to last, and increment 1 to value?


code:
elem = driver.find_elements_by_class_name('b')
divs = driver.find_elements_by_class_name('a')
url = []

for div in divs:
    links = div.find_elements_by_xpath('.//a')
    if len(links) > 0:
        url.append(links[0].get_attribute('href'))
    else:
        url.append('')
i=0

while i < 3:
    print [elem[i].text, url[i]]
    i += 1

rawDescription: Extracting link when a nested div is present and a blank when there is no nested div using selenium


code:
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel

iris = datasets.load_iris()
X, y = iris.data, iris.target
model = RandomForestClassifier(5000)

new_model = SelectFromModel(model, threshold=0.5)

rawDescription: Reduce the number of features based on feature_importances_


code: shuffled = sorted(qs, key=lambda item: item.order if item.order != 999 else 999 + random.random())

rawDescription: Shuffle queryset in Django

description: Shuffle queryset in Django


code:
for col in df:
    if np.isnan(df[col].iloc[0]) == True:
        del df[col]

rawDescription: How to delete a column in pandas dataframe if df[0,i] is nan?

description: Delete a column in pandas dataframe if df[0,i] is nan


code:
from PIL import Image
from cStringIO import StringIO

a = Image.new('RGB', (10, 10), 'black')
# ...
buf = StringIO()
a.save(buf, 'png')
b64img = '<img src="data:image/png;base64,{0}" />'.format(buf.getvalue().encode('base64'))

rawDescription: Return a Dynamic png from Pylons

description: Return a Dynamic png from Pylons


code:
class App:
    def __init__(self, master):
        self.stats = []
        idx = 0
        for i in range(4):
            for j in range(4):
                stat = Label(text="Stat {}".format(idx+1), bg='#990000', bd=3, relief=SOLID, padx=30, pady=20)
                stat.grid(row=i, column=j)
                self.stats.append(stat)
                idx += 1
        root.bind("<F1>", self.callConsole)
    def callConsole(self, master):
        self.commandConsole = Console(root)
        root.wait_window(self.commandConsole.top)
        command = self.commandConsole.commandVar
        if 'redalert' in command:
            digits = [c for c in command if c.isdigit()]
            number = int("".join(digits))
            self.stats[number-1].config(bg="red")

rawDescription: Tkinter: using a variable to config a label


code:
def nogen():
  '''Create a list of values 0-3 and print each one as you build the list.'''
  r = []
  for i in range(3):
    print(i)
    r.append(i)
  return r

def firstgen():
  '''Create an iterator of values 0-3 and print each one as you yield the value.'''
  for i in range(3):
    print(i)
    yield i

a = nogen() # This function is eager and does everything at once.
a
b = firstgen() # Note there's no output immediately after this line.
list(b) # The output happens as the list coercion causes the generator to execute.
c = firstgen() # You can also see this a step at a time using `next`
next(c)
next(c)
next(c)
next(c)

rawDescription: Python 2.7 yield is creating strange behavior


rawDescription: Does Python have an "or equals" function like ||= in Ruby?


rawDescription: Python comparing list of lists

description: Python comparing list of lists


code:
def conditional_sum(row):
    underlying = row['UNDERLYING'][0]  # extra '[0]' is required due to multi leve index in column names
    return row.loc['AUG12'].apply(lambda x: 0 if x < underlying else x).sum()

df.apply(conditional_sum, axis=1)

rawDescription: Python/Pandas: Conditional summing of columns


rawDescription: Deciphering hex-codes as (probably) a timestamp


code:
tree = lxml.etree.parse("http://www.somedomain.org/people/atom/author_name")
xmlns = {"atom": "http://www.w3.org/2005/Atom"}
listings = tree.xpath("//atom:entry", namespaces=xmlns)

listings_info = []

for listing in listings:
    listings_info.append({
        "link": listing.xpath("./atom:link/@href", namespaces=xmlns),
        "title": listing.xpath("./atom:title", namespaces=xmlns),
        "content": listing.xpath("./atom:content", namespaces=xmlns),
    })

rawDescription: findtext and xpath not returning anything from my parsed xml document

description: Findtext and xpath not returning anything from my parsed xml document


code:
def join_generator(joiner, iterable):
    i = iter(iterable)
    yield next(i)  # First value, or StopIteration

    while True:
       # Once next() raises StopIteration, that will stop this
       # generator too.
       next_value = next(i)
       yield joiner
       yield next_value

joined = list(join_generator(0, [1, 2, 3, 4]))

rawDescription: Is there a "join" like function for generic lists in Python?


code:
from multiprocessing import Manager
manag = Manager()
serviceLock = manag.Lock()
serviceStatusDict = manag.dict()

rawDescription: Share memory areas between celery workers on one machine


code:
from functools import reduce
items = ("DE111", "DE112")

def byFormattedId(value):
   return "(FormattedID = \"%s\")" % value

def ors(statement, value):
   return "(%s OR %s)" % (statement, value)

x = list(map(byFormattedId, items))
y = reduce(ors, x)

rawDescription: Rallydev API pyral: Is there way to get all defects by Formatted ID in one query?


code:
import numpy as np
from numpy.random import choice
names = ['Harry', 'Sally', 'Joe', 'Bob', 'Angela', 'Jack', 'Jill', 'Jeff']
weights = [1,4,6,3,5,7,10,14]
p = np.array(weights, dtype=float) / sum(weights)
p
choice(names, size=5, p=p)
choice(names, size=5, p=p)
choice(names, size=5, p=p)

rawDescription: What's the pythonic way to run a lottery?


code:
def run(self):
    while True:
        set_mode = getattr(self, self.mode)
        set_mode()
        time.sleep(self.blinkDuration)
        self.next_mode()

def next_mode(self):
    self.mode = {
            self.ledModes.ledON: self.ledModes.ledBlink,
            self.ledModes.ledBlink: self.ledModes.ledOFF,
            self.ledModes.ledOff: self.ledModes.ledOn,
            }[self.mode]

rawDescription: Moving from one enum state to the next and cycling through


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

#Random sample data
x = np.random.random_integers(0, 5, 10)
y = np.random.random_integers(0, 5, 10)

plt.scatter(x,y, c='orange')
plt.ylim([0, y.max()])
plt.xlim([0, x.max()])
plt.grid()
plt.show()

rawDescription: Scatter plot in matplotlib origin aligned


code:
catString = "randomtextrandomtext (cats:1)=1 randomtext randomtextrandomtext (cats:3)=3 randomtext randomtext (cats:1540)=1,540 randomtextrandomtext randomtext (cats:85):85 randomtext"

result = ''
end = -1
try:
    while True:
        start = catString.index('(cats:', end+1)
        end = catString.index(')', start)
        result += catString[start:end+1]

except ValueError:
    pass

print(result)

rawDescription: Extracting substring from string in Python


code:
import re

text = 'You can visit Stack Overflow'
new_text = re.sub(r'href="http(s)?:\/\/(.+?)"', r'href="https://mydomain.com/\2"', text)

rawDescription: Python: Generate new URLs for links stored in a string


code:
from string import ascii_letters as letters
from numpy.random import randint
from numpy import array

from pandas import Series

n = 1000
ltrs = Series(array(list(letters))[randint(len(letters), size=n)])
ltrs.value_counts().value_counts().plot(kind='bar')

rawDescription: Quick way creating counts for keys in a table


rawDescription: Convert Unicode Output of Tuple into string Python


code:
import requests

url = 'https://www.someserver.com/toplevelurl/somepage.htm'

res = requests.get(url, auth=('USER', 'PASSWORD'))

status = res.status_code
text   = res.text

rawDescription: HTTPS log in with urllib2

description: HTTPS log in with urllib2


rawDescription: launch application from python3 then continue other code

description: Launch application from python3 then continue other code


code:
from tkinter import *
import random

root = Tk()
lbl = Label(root)
lbl.pack()

def replace_text():
    lbl.config(text=str(random.random()))
    root.after(1000, replace_text)

replace_text()
root.mainloop()

rawDescription: random number and tkinter not working

description: Random number and tkinter not working


rawDescription: How to scatter plot one x data versus several unequal y data plots in matplotlib.pyplot

description: Scatter plot one x data versus several unequal y data plots in matplotlib.pyplot


code:
def get_form(self, step=None, data=None, files=None):
    if step is None:
        step = self.steps.current
    if step == "1":
        prev_data = self.get_cleaned_data_for_step(self.get_prev_step(
                                                     self.steps.current))
        cnt = prev_data['extra_from_step0'] #use appropriate key as per your form

        return formset_factory(LegForm, extra=cnt, max_num=cnt)

    return super(YourFormWizard, self).get_form(step, data, files)

rawDescription: Adding Forms to a Formset based on previous FormWizard step


rawDescription: python regex multiple find

description: Python regex multiple find


code:
class file_counter(object):
    def __init__(self):
        self.position = self.size = 0

    def seek(self, offset, whence=0):
        if whence == 1:
            offset += self.position
        elif whence == 2:
            offset += self.size
        self.position = min(offset, self.size)
        self.size = max(self.size, self.position)

    def tell(self):
        return self.position

    def write(self, string):
        self.position += len(string)
        self.size = max(self.size, self.position)

rawDescription: Python image library, how to compress image into desired file size?


rawDescription: Splitting by ',' in a dictionary while retaining the lists inside


code:
#!/usr/bin/python

import csv

infile = 'input.csv'
outfile = 'output.csv'

wfh = open(outfile, 'w')

with open(infile, 'r') as fh:

    reader = csv.DictReader(fh, delimiter=',')

    for row in reader:
        name = row['Name']
        age = row['Age']
        height = row['Height']

    if age >= 16:
        wfh.write("{},{},{}".format(name, age, height))

wfh.close()

rawDescription: Checking the Value of a Specific Column in Every Row of a CSV with Python


code:
def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count+=1
        else:
            return count

rawDescription: string count with overlapping occurrences

description: String count with overlapping occurrences


rawDescription: Python XML regular expression matching issue


code:
img = cv2.imread(path, 0)
ret, thresh = cv2.threshold(img, 70, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('image', thresh)
cv2.imwrite("h2kcw2/out1.png", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

rawDescription: Text binarization

description: Text binarization


code:
from nltk.corpus import wordnet as wn
from itertools import product

wordx, wordy = "cat","dog"
sem1, sem2 = wn.synsets(wordx), wn.synsets(wordy)

maxscore = 0
for i,j in list(product(*[sem1,sem2])):
  score = i.wup_similarity(j) # Wu-Palmer Similarity
  maxscore = score if maxscore < score else maxscore

rawDescription: check if two words are related to each other

description: Check if two words are related to each other


code:
def getidof(s):
    return id(s)

s = 'blabla'
id(s) == getidof(s)  # True

rawDescription: Using a variable at once or passing it around


code:
time = xray.DataArray(arr.time.values, coords=[arr.time.values], dims=['time'])
time_first = time.resample('1M', 'time', how='first')
time_last = time.resample('1M', 'time', how='last')
time_diff = time_last - time_first

rawDescription: Python xarray: Extract first and last time value within each month of a timeseries


code:
import pyttsx

engine = pyttsx.init()
engine.say("Your Message")
engine.runAndWait()

rawDescription: How to convert text to speech in python

description: Convert text to speech in python


code:
import inspect

def compact(*names):
    caller = inspect.stack()[1][0] # caller of compact()
    vars = {}
    for n in names:
        if n in caller.f_locals:
            vars[n] = caller.f_locals[n]
        elif n in caller.f_globals:
            vars[n] = caller.f_globals[n]
    return vars

def extract(vars):
    caller = inspect.stack()[1][0] # caller of extract()
    for n, v in vars.items():
        caller.f_locals[n] = v   # NEVER DO THIS - not guaranteed to work

rawDescription: Python equivalent of PHP's compact() and extract()


code:
from PyQt4 import QtCore, QtGui

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.checkbox1 = QtGui.QCheckBox('Option one', self)
        self.checkbox2 = QtGui.QCheckBox('Option two', self)
        self.buttonOk = QtGui.QPushButton('Ok', self)
        self.buttonOk.clicked.connect(self.accept)
        self.buttonCancel = QtGui.QPushButton('Cancel', self)
        self.buttonCancel.clicked.connect(self.reject)
        layout = QtGui.QGridLayout(self)
        layout.addWidget(self.checkbox1, 0, 0, 1, 2)
        layout.addWidget(self.checkbox2, 1, 0, 1, 2)
        layout.addWidget(self.buttonOk, 2, 0)
        layout.addWidget(self.buttonCancel, 2, 1)

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        widget = QtGui.QWidget(self)
        layout = QtGui.QVBoxLayout(widget)
        self.button = QtGui.QPushButton('Show Dialog', self)
        self.button.clicked.connect(self.handleButton)
        layout.addWidget(self.button)
        self.setCentralWidget(widget)

    def handleButton(self):
        dialog = Dialog(self)
        if dialog.exec_() == QtGui.QDialog.Accepted:
            print('Option one: %s' % dialog.checkbox1.isChecked())
            print('Option two: %s' % dialog.checkbox2.isChecked())
        else:
            print('Cancelled')
        dialog.deleteLater()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 200, 100)
    window.show()
    sys.exit(app.exec_())

rawDescription: Block QMainWindow while child widget is alive , pyqt


code:
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'your_test_app',
    'here_goes_your_other_app'
)

rawDescription: Test Django app using models from a second app


code:
## first  find in which pair the startPoint and endPoint exist
for i,e in enumerate(linestring,1):
    if linestring[i-1,i].contains(startPoint):
        startIdx = i-1
    if linestring[i-1,i].contains(endPoint):
        endIdx = i

# build three list
# startLine from the beginning of line until start point
# middleLine from startPoint until endPoint
# endLine from endPoint until the last point
# transform every list to a LineString

# start the list with coords from Line starting with beginning until startPoint
startLine = linestring.coords[:startIdx]
# append startPoint coords
startLine.append( startPoint.coords)
# transform from list to LineString
startLine = LineSting(startLine)

middleLine =  list(startPoint.coords).extend(linestring.coords[startIdx+1:endIdx-1]).extend(endPoint.coord)
# transform from list to LineString
middleLine = LineSting(middleLine)

# start list with end point coords
endLine = endPoint.coords
# extend the list with coords from Line starting with endPoint until end
endLine.extend( linestring.coords[endIdx:])
# transform from list to LineString
endLine = LineSting(endLine)

rawDescription: Create a new curve from existing curve - Shapely


rawDescription: Get (column, row) index from NumPy array that meets a boolean condition


code:
import xml.etree.ElementTree as ET

from StringIO import StringIO
tree = ET.parse(StringIO(xml_data))
root = tree.getroot()

assets = {}

def find_rows(rowset, parent_id):
    for row in rowset.findall("row"):
        singleton = int((row.get('singleton')))
        flag = int((row.get('flag')))
        quantity = int((row.get('quantity')))
        typeID = int((row.get('typeID')))
        locationID = int((row.get('locationID', '0')))
        itemID = int((row.get('itemID')))

        assets[itemID] = {'singleton': singleton,
                          'flag': flag,
                          'quantity': quantity,
                          'typeID': typeID,
                          'locationID': locationID,
                          'parentID': parent_id}
        child_rowset = row.find("rowset")
        if child_rowset is not None:
            find_rows(child_rowset, itemID)

first_rowset = root.find('.//rowset[@name="assets"]')
find_rows(first_rowset, None)

rawDescription: Parsing Nested XML Data in Python Using ElementTree


code:
from signal import alarm, signal, SIGALRM, SIGKILL

def init_Pygame(surf):

    # this section is an unbelievable nasty hack - for some reason Pygame
    # needs a keyboardinterrupt to initialise in some limited circs (second time running)
    class Alarm(Exception):
        pass

    def alarm_handler(signum, frame):
        raise Alarm

    signal(SIGALRM, alarm_handler)
    alarm(3)
    print("Step 2 ")
    try:
        pygame.init()
        Screen = max(pygame.display.list_modes())
        surf = pygame.display.set_mode(Screen)
        alarm(0)
    except Alarm:
        raise KeyboardInterrupt
    return (surf)

rawDescription: running 2nd pygame program (for 2nd display) in 2nd SSH terminal pauses until ^C is pressed

description: Running 2nd pygame program (for 2nd display) in 2nd SSH terminal pauses until ^C is pressed


code:
return HttpResponse("
Hello!
", content_type="text/html")

rawDescription: Rendering HTML directly without using a template


code:
import StringIO
f = StringIO.StringIO("foo")
f.read()

rawDescription: In Python, how do I transform a string into a file?


rawDescription: Can't find a way to unstack a dataframe

description: Can't find a way to unstack a dataframe


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

x = np.random.rand(100)
y = np.random.rand(100)
category = np.random.random_integers(0, 3, 100)

markers = ['s', 'o', 'h', '+']
for k, m in enumerate(markers):
    i = (category == k)
    plt.scatter(x[i], y[i], marker=m)

plt.show()

rawDescription: Python scatter-plot: Conditions for marker styles?


rawDescription: NumPy - Sum of the elements on the secondary diagnoal of a 2D matrix


code:
#!/usr/bin/env python
from django.core.management import execute_manager
import imp

try:
    import settings_local
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings_local.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings_local)

rawDescription: django manage.py settings default

description: Django manage.py settings default


code: return next( (i for i,(k,v) in enumerate(items) if k=='Spock'),None)

rawDescription: Searching a 2-Dimensional Tuple/List in Python


code:
# my dummy channels:
r = np.ones((100,100),np.uint8)  * 100
g = np.ones((100,100),np.uint8)  * 70
b = np.ones((100,100),np.uint8)  * 10

#now, just merge them:
rgb = cv2.merge((r,g,b))

rawDescription: Conversion of three channel binary image to three channel RGB image


code:
import networkx as nx
import matplotlib.pyplot as plt

K_5=nx.complete_graph(10)
print(K_5.number_of_nodes(), K_5.number_of_edges())
nx.draw(K_5)
plt.savefig('test1.png')
K_5.clear()

plt.clf() # new line, to clear the old drawings

G = nx.Graph()
G.add_node(8)
nx.draw(G)
plt.savefig('test2.png')
print(G.number_of_nodes(), G.number_of_edges())

rawDescription: Graph Keeps Growing, Even After Clearing It


code:
buffers = ffi.new( "PUINT8[]", nb_buffer )
keepalive = []

for i in range( nb_buffer ):
    p = ffi.new("UINT8[]", buffer_size)
    keepalive.append(p)
    buffers[i] = p

c.function( buffers )

# keepalive stays alive at least until here

rawDescription: Create a CData-typed buffer in Python with CFFI


code:
from subprocess import call

with open("somefile.txt", 'r') as f:
    some_files_to_run = [line.split('\n')[0] for line in f.readlines()]
    for file_to_run in some_files_to_run:
        call(["python", file_to_run])

rawDescription: Python file to open a text file and run other python files in the text file


code:
my_str = "word anotherword +aspecialword lameword +heythisone +test hello"
" ".join(x[1:] for x in my_str.split() if x.startswith("+"))

rawDescription: Python: Grab each word after certain character in string


code:
class canvasFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"MyCanvas",size =(1200,400))

        self.fig = plt.Figure(figsize=(15,5))
        self.ax1 = self.fig.add_subplot(1,1,1)
        self.canvas = FigureCanvas(self, -1, self.fig)

        b1 = wx.CheckBox(self,-1,"Apples",(10,20),(160,-1))
        b2 = wx.CheckBox(self,-1,"Mango",(10,40),(160,-1))
        # first create your sizer for your checkboxes
        checkbox_sizer = wx.BoxSizer(wx.VERTICAL)
        checkbox_sizer.AddMany([b1,b2]) # add both buttons to the sizer
        #next create the main sizer
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        #add the checkbox sizer to the main_sizer
        main_sizer.Add(checkbox_sizer)
        # add the canvas to the main sizer
        main_sizer.Add(self.canvas)
        #Set the sizer as the sizer for the frame
        self.SetSizer(main_sizer)
        #call layout for the sizers to layout their elements
        self.Layout()
        #call fit to make the frame fit its contents
        self.Fit()

rawDescription: Aligning in wxpython

description: Aligning in wxpython


code:
from matplotlib import mlab
psdResults = mlab.psd(inputData, NFFT=512, Fs=sampleRate, window=blackman)

rawDescription: Reusing module references in Python (Matplotlib)


rawDescription: write a very long list (and assign variable) to file without text wrapping problems

description: Write a very long list (and assign variable) to file without text wrapping problems


code:
def foo(idx):
    return lambda t: make_frame(t, idx)

for idx, path in enumerate(paths):
    clip2 = VideoClip(foo(idx), False, 10)

rawDescription: python pass variable to function parameter

description: Python pass variable to function parameter


code: vals = list(filter(None, [x.get_text().strip() for x in tds[0].find_all('tr')]))

rawDescription: Python inline for if : Is there a better way to write this?


code:
donors = queryset.filter(
    donation__year__exact=2014
).annotate(sum_donation=Sum('donation__cheque_amount')).filter(sum_donation__gte=1000)

chairs = queryset.filter(specialtitle__chair_title__iendswith='Chair')

result = queryset.filter(pk__in=[person.id for person in set(donors) | set(chairs)])

rawDescription: Django QuerySet union operator are not commutative after annotated filter


code:
payload = {
  'user': 'newuser',
  'pass': '12345',
  'tos_understood': 'on',
  }

rawDescription: Checkbox input in python urllib

description: Checkbox input in python urllib


code:
n1 = raw_input('Εισάγετε Λεφτά :') or 0
n2 = raw_input('Εισάγετε Πλυσίματα Μέσα - Έξω :') or 0
n3 = raw_input('Εισάγετε Πλυσίματα Μηχανών :') or 0
n4 = raw_input('Εισάγετε Πλυσίματα Εξωτερικά :') or 0
n5 = raw_input('Εισάγετε Τι Ποσό Πήρε ο Σπύρος :') or 0
n6 = raw_input('Εισάγετε Έλλειμμα :') or 0
n7 = raw_input('Εισαγετε Πρόσθετο  Ποσό :') or 0
n8 = raw_input('Εισάγετε Ψιλά :') or 0
answer = (str(float(n1) + float(n5) - int(n2) * 10 - int(n3) * 8 - int(n4) * 7 + float(n6) - float(n7) - float(n8)))
print (float(answer))
input('Press Enter To Exit')

rawDescription: Python Input 0 automatically

description: Python Input 0 automatically


code:
subparsers = parser.add_subparsers(dest='operation', help='The operation that you want to run on the server.')
...
server_types = dict(spam=SpamServer, ham=HamServer)
args = parser.parse_args()
server = server_types[args.type_of_server]()
getattr(server, args.operation)(args)

rawDescription: Argparse Subparsers, and linking to Classes


code:
for name, value in sorted(os.environ.items()):
    print("   " + name + "=" + value)

rawDescription: How to sort environment variables in python

description: Sort environment variables in python


code:
dictionary1 = {'1':'one', '2':'two', '3':'three'}
dictionary2 = {'1':'uno', '2':'dos', '3':'tres'}
combined = {key:[dictionary1[key], dictionary2[key]] for key in dictionary1}
combined

rawDescription: merge two dictionaries by key

description: Merge two dictionaries by key


code:
import os
import subprocess
import threading
import functools

from time import sleep

class OutputMonitor(threading.Thread):

    """ Start the subprocess in separate thread and append it's output to a buffer. """

    def __init__(self, cmd):
        super(OutputMonitor, self).__init__()
        self.daemon = True
        self.cmd = cmd
        self.buffer = ''
        self.buflock = threading.Lock()

    def run(self):

        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        while popen.poll() is None:
            data = popen.stdout.read(4)
            if data != "":
                with self.buflock:
                    self.buffer += data

    def get_current_output(self):
        with self.buflock:
            buf = self.buffer
            self.buffer = ""
            return buf

class OutputHandler(threading.Thread):

    """
        Start a thread responsible for tracking subprocess output, and periodically
        check if it has produced new output. If so, call handler to process this data.
    """

    def __init__(self, cmd, interval, filepath):
        super(OutputHandler, self).__init__()
        self.om = OutputMonitor(cmd)
        self.interval = interval

        # Replace it with your handler init...
        self.filepath = filepath
        if os.path.exists(self.filepath):
            os.unlink(self.filepath)

    def run(self):

        self.om.start()
        while self.om.is_alive():
            sleep(self.interval)
            data = self.om.get_current_output()

            self._handle_data_chunk(data)

    def _handle_data_chunk(self, data):

        # Replace it with you handling.
        with open(self.filepath, 'a') as f:
            f.write(data)

if __name__ == '__main__':

    logfile_path = "C:\\log.txt"

    interval = 5
    cmd = ['ping', 'n', '10', '127.0.0.1']

    oh = OutputHandler(cmd, interval, logfile_path)
    oh.start()
    oh.join()

rawDescription: Partial read from "stdout" on Python using Popen


rawDescription: Search from end of string and split

description: Search from end of string and split


code:
for word in words.split():
    print(word)

rawDescription: Automatically separating words into letters?


code:
from math import sin, cos, radians, degrees, acos

def calc_dist(lat_a, long_a, lat_b, long_b):
    lat_a = radians(lat_a)
    lat_b = radians(lat_b)
    long_diff = radians(long_a - long_b)
    distance = (sin(lat_a) * sin(lat_b) +
                cos(lat_a) * cos(lat_b) * cos(long_diff))
    return degrees(acos(distance)) * 69.09

rawDescription: Django - how can I find the distance between two locations?


code:
import wave
win= wave.open('sample.wav', 'rb')
wout= wave.open('segment.wav', 'wb')

t0, t1= 1.0, 2.0 # cut audio between one and two seconds
s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
win.readframes(s0) # discard
frames= win.readframes(s1-s0)

wout.setparams(win.getparams())
wout.writeframes(frames)

win.close()
wout.close()

rawDescription: How to get the contents of the wav file into array so as to cut the required segment and convert it back to wav format using python?

description: Get the contents of the wav file into array so as to cut the required segment and convert it back to wav format using python


code:
import random
task_list = ['task_%d' % (i,) for i in range(1, 17)]
random.shuffle(task_list)
persons = zip(*[iter(task_list)]*4)
persons

rawDescription: Assigning Tasks to People

description: Assigning Tasks to People


code:
@conf.commands.register
def traceroute(target, dport=80, minttl=1, maxttl=30, sport=RandShort(), l4 = None, filter=None, timeout=2, verbose=None, **kargs):
    """Instant TCP traceroute
traceroute(target, [maxttl=30,] [dport=80,] [sport=80,] [verbose=conf.verb]) -> None
"""
    if verbose is None:
        verbose = conf.verb
    if filter is None:
        # we only consider ICMP error packets and TCP packets with at
        # least the ACK flag set *and* either the SYN or the RST flag
        # set
        filter="(icmp and (icmp[0]=3 or icmp[0]=4 or icmp[0]=5 or icmp[0]=11 or icmp[0]=12)) or (tcp and (tcp[13] & 0x16 > 0x10))"
    if l4 is None:
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/TCP(seq=RandInt(),sport=sport, dport=dport),
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)
    else:
        # this should always work
        filter="ip"
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/l4,
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)

    a = TracerouteResult(a.res)
    if verbose:
        a.show()
    return a,b

rawDescription: scapy::traceroute setting source ip address

description: Scapy::traceroute setting source ip address


code:
object = [{'number':'230/600'},{'number':'240/620'}]
for value in object:
    numbers = value['number'].split('/')
    new_object.append({'numbera': numbers[0], 'numberb': numbers[1]})
new_object

rawDescription: Split python object into two objects

description: Split python object into two objects


code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string('''
<MyWidget>:
    orientation: "vertical"
    Button:
        text: "switch"
        size_hint: 1, 0.1
        on_press:
            if item1.collapse: item1.dispatch('on_touch_down', item1)
            else: item2.dispatch('on_touch_down', item2)
    Accordion:
        AccordionItem:
            id: item1
            title: "1"
            Label:
                text: "1"
        AccordionItem:
            id: item2
            title: "2"
            Label:
                text: "2"
''')

class MyWidget(BoxLayout):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

rawDescription: Switching "tab" in accordion programmatically


code:
from subprocess import check_call

check_call(args, cwd="subdir")

rawDescription: Redirect files from Popen to subdirecotry


rawDescription: what is the difference between del a[:] and a = [] when I want to empty a list called a in python?

description: What is the difference between del a[:] and a = [] when I want to empty a list called a in python


code:
import re
strs="Limits paramA : (7, 45) paramB : (0, 0) paramC : (1, 23)"
[int(digit) for digit in re.findall(r'\d+',strs)]

rawDescription: Python parsing integers inside parantheses in a string


code:
import time
time.localtime()
_.tm_isdst

rawDescription: Python daylight savings time

description: Python daylight savings time


code:
threads = []
for dev in run_list:
    proc = threading.Thread(target=go, args=[dev])
    proc.start()
    threads.append(proc)

for proc in threads:
    proc.join()

rawDescription: python and ipython threading.activeCount()

description: Python and ipython threading.activeCount()


code:
import os
import sys
import platform

def machine():
    """Return type of machine."""
    if os.name == 'nt' and sys.version_info[:2] < (2,7):
        return os.environ.get("PROCESSOR_ARCHITEW6432",
               os.environ.get('PROCESSOR_ARCHITECTURE', ''))
    else:
        return platform.machine()

def os_bits(machine=machine()):
    """Return bitness of operating system, or None if unknown."""
    machine2bits = {'AMD64': 64, 'x86_64': 64, 'i386': 32, 'x86': 32}
    return machine2bits.get(machine, None)

print (os_bits())

rawDescription: In Python, how do you determine whether the kernel is running in 32-bit or 64-bit mode?


code:
file=open("file1.txt","r")
text="target"
count=0
for line in file:
    count += 1
    if text in line:
        print (count, line)

rawDescription: Replacing enumerate to output a string

description: Replacing enumerate to output a string


code:
import rpy2.robjects.lib.ggplot2 as ggplot2

class ThemeClassic(ggplot2.Theme):
    _constructor = ggplot2.ggplot2.theme_classic
    @classmethod
    def new(cls):
        res = cls(cls._constructor())
        return res

# Monkey patching ggplot2
ggplot2.theme_classic = ThemeClassic.new

rawDescription: how can theme_classic be accessed in rpy2 from ggplot2?

description: How can theme_classic be accessed in rpy2 from ggplot2


code:
log = open('/var/log/nbu/available_media' + adate, 'r')

# Splits on newline chars, skips the titles and ----- lines
lines = log.readlines()[2:]

# Break up each line by field
lines = [line.strip().split('\t') for line in lines]

# Going to form a dictionary mapping pool names to dictionaries of tape state counts
states_dict = {}
all_states = set()
for line in lines:
    if len(line) == 1:
        current_pool = line[0]
        states_dict[current_pool] = {}
    elif len(line) > 1:
        state = line[-1]
        states_dict[current_pool][state] = states_dict[current_pool].setdefault(state, 0) + 1
        all_states.add(state)

all_states = sorted(list(all_states))

# write output
with open('output_file_name.txt','wb') as outfile:
    outfile.write('Pool\t' + '\t'.join(all_states) + '\n')
    for pool in states_dict:
        outfile.write(pool)
        for state in all_states:
            outfile.write('\t' + states_dict[pool].setdefault(state, 0))
        outfile.write('\n')

rawDescription: Reading and formatting issues in python

description: Reading and formatting issues in python


code:
dd = {'a': 20, 'c': 60, 'b': 10}
keys = [i for i in dd]
[(keys[i],keys[j]) for i in range(len(keys)) for j in range(i+1,len(keys))]

rawDescription: How to create pairs form python dictionary keys

description: Create pairs form python dictionary keys


code:
import numpy as np
import matplotlib.pyplot as plt
R = np.array ([3.00, 2.50, 1.00, 3.3192, 2.383,  2.7128, 3.8662, 3.6724,   3.5112])

# Calculate the shape of the 2d array
n = int( np.sqrt( R.size ) )
C = R.reshape((n,n))

# Plot the matrix
plt.matshow(C,cmap="Reds")

ax = plt.gca()

# Set the plot labels
xlabels = ["B%d" % i for i in xrange(n+1)]
ylabels = ["A%d" % i for i in xrange(n+1)]
ax.set_xticklabels(xlabels)
ax.set_yticklabels(ylabels)

#Add text to the plot showing the values at that point
for i in xrange(n):
    for j in xrange(n):
        plt.text(j,i, C[i,j], horizontalalignment='center', verticalalignment='center')

plt.show()

rawDescription: Plot Distance matrix for a 1D array

description: Plot Distance matrix for a 1D array


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

# Trial function for adding vertical arrows to
def f(x):
    return np.sin(2*x)

x = np.linspace(0,10,1000)
y = f(x)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, 'k', lw=2)
ax.set_ylim(-3,3)

def add_force(F, x1):
    """Add a vertical force arrow F pixels long at x1 (in data coordinates)."""
    ax.annotate('', xy=(x1, f(x1)), xytext=(0, F), textcoords='offset points',
                arrowprops=dict(arrowstyle='<|-', color='r'))

add_force(60, 4.5)
add_force(-45, 6.5)

plt.show()

rawDescription: Python matlibplot Arrow pointing to a point on a curve


code:
import random

def print_mimic(mimic_dict, word):

    l = []
    for i in range(200):
        word = random.choice(mimic_dict.get(word, ''))
        l.append(word)

    print(' '.join(l))

rawDescription: Google Classes: Python -- Mimic.py Exercise


code:
r = '''\
aaron, 1
lllll, 10
kkkkk, 7'''.split('\n')
# r is an iterable of non-blank lines, like a file

def score(line):
    return int(line.split(',')[1])

for line in sorted(r, key=score, reverse=True):
    print(line)

###
lllll, 10
kkkkk, 7
aaron, 1

rawDescription: Sorting data in one .txt file from highest to lowest and printing it to another text file


code:
def do_login(context, login):
    driver = context.browser

    user_data = csv.DictReader(open('users.csv'))
    for row in user_data:
        row['user'] = login
        uname = row['username']
        pword = row['password']

        # something like this
        if login == row['usertype']:
            driver.find_element_by_name('username').send_keys(uname)
            driver.find_element_by_name('password').send_keys(pword)

rawDescription: Python - Read CSV cell from specific row - row index is passed in variable


code: sorted_data = OrderedDict((k, data[k]) for k in order)

rawDescription: Sorting a dictionary by a given list in Python


rawDescription: Parsing non-zero padded timestamps in Python


code:
from pygraphlib import pygraph, algo

edges = [(1,2),(2,3),(3,4),(4,6),(6,7),(3,5),(4,5),(7,1),(2,5),(5,7)]
graph = pygraph.from_list(edges)

pathList = []
shortestPath = algo.shortest_path(graph, startNode, endNode)
cost = shortestPath[len(shortestPath)-1][1]

while cost <= maxCost:
    if cost >= minCost:
        pathList.append(shortestPath)

    minEdgeWt = float('inf')
    for i in range(len(shortestPath)-1):
        if shortestPath[i+1][1] - shortestPath[i][1] < minEdgeWt:
            minEdgeWt = shortestPath[i+1][1] - shortestPath[i][1]
            edgeNodes = (shortestPath[i][0], shortestPath[i+1][0])

    #Not sure of the syntax here, edgeNodes is a tuple, and hide_edge requires an edge.
    graph.hide_edge(edgeNodes)
    shortestPath = alog.shortest_path(graph, startNode, endNode)
    cost = shortestPath[len(shortestPath)-1][1]

return pathList

rawDescription: Pygraph - path between two nodes with specific weight