code:
bar_id = Column(Integer, ForeignKey(Bar.id))
bar = relationship(Bar, uselist=False)

rawDescription: sqlalchemy: one-to-one relationship with declarative

description: Sqlalchemy: one-to-one relationship with declarative


code:
class Product(models.Model):
    parent = models.ForeignKey('self', null=True, blank=True,related_name="variants")
    category = models.ForeignKey(Category, related_name='products')

def save(self, *args, **kwargs):
    if self.parent and not self.pk and not self.category:
        self.category = self.parent.category  # Could use setattr getattr too but this is easier to read imo
    return super(Product, self).save(*args, **kwargs)

rawDescription: django: how to use a parent's attribute for a field's default

description: Django: how to use a parent's attribute for a field's default


code:
import pandas as pd
from collections import OrderedDict

entity_dict = OrderedDict()
entity_dict['bam'] = 1.0
entity_dict['ham'] = 1.0
entity_dict['jam'] = 0.82390874094431876
entity_dict['kam'] = 1.0
entity_dict['lam'] = 1.0
entity_dict['mam'] = 0.82390874094431876
entity_dict['pam'] = 1.0
entity_dict['ram'] = 1.0
entity_dict['sam'] = 0.82390874094431876
entity_dict['tam'] = 1.0

entity_df = pd.DataFrame.from_dict(entity_dict, orient='index').T

print(entity_df)

rawDescription: Dataframe not getting ordered by column names


code:
import readline

def raw_input_no_history():
    input = raw_input()
    readline.remove_history_item(readline.get_current_history_length()-1)
    return input

rawDescription: raw_input without leaving a history in readline

description: Raw_input without leaving a history in readline


code:
class Model(object):
  ...
  def transaction_start(self):
    return Redland.librdf_model_transaction_start(self._model)

rawDescription: Storage transactions in Redland's Python bindings?


code:
from datetime import datetime
import os
import json
from pprint import pprint

now = datetime.now()

testFile = open("feed.json")
feed = json.load(testFile)

data_list = []

for x in feed['data']:
    if x['threatscore'] > 90:
        data = {}
        data['Event']={}
        data['Event']["date"] = now.strftime("%Y-%m-%d")
        data['Event']["threat_level_id"] = "1"
        data['Event']["info"] = "HybridAnalysis"
        data['Event']["analysis"] = 0
        data['Event']["distribution"] = 3
        data['Event']["orgc"] = "Malware"
        data['Event']["Attribute"] = []
        if 'hosts' in x:
            data['Event']["Attribute"].append({
                'type': 'ip-dst',
                'category': 'Network activity',
                'to-ids': True,
                'distribution': '3',
                'value': x['hosts']})
        if 'md5' in x:
            data['Event']["Attribute"].append({
                'type': 'md5',
                'category': 'Payload delivery',
                'to-ids': True,
                'distribution': '3',
                'value': x['md5']})
        if 'domains' in x:
            data['Event']["Attribute"].append({
                'type': 'domain',
                'category': 'Network activity',
                'to-ids': True,
                'distribution': '3',
                'value': x['domains']})
        attributes = data["Event"]["Attribute"]
        data["Event"]["Attribute"] = []
        for attribute in attributes:
            for value in attribute["value"]:
                    if value == " ":
                        pass
                    else:
                        new_attr = attribute.copy()
                        new_attr["value"] = value
                        data["Event"]["Attribute"].append(new_attr)
        data_list.append(data)

with open('output.txt', 'w') as outfile:
    json.dump(data_list, outfile)

rawDescription: Python manipulating json, lists, and dictionaries


code:
import asyncio
import contextlib
import os
import locale

class SubprocessProtocol(asyncio.SubprocessProtocol):
    def pipe_data_received(self, fd, data):
        if fd == 1:
            name = 'stdout'
        elif fd == 2:
            name = 'stderr'
        text = data.decode(locale.getpreferredencoding(False))
        print('Received from {}: {}'.format(name, text.strip()))

    def process_exited(self):
        loop.stop()

if os.name == 'nt':
    # On Windows, the ProactorEventLoop is necessary to listen on pipes
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()
with contextlib.closing(loop):
    # This will only connect to the process
    transport = loop.run_until_complete(loop.subprocess_exec(
        SubprocessProtocol, 'python', '-c', 'print(\'Hello async world!\')'))[0]
    # Wait until process has finished
    loop.run_forever()
    print('Program exited with: {}'.format(transport.get_returncode()))

rawDescription: How to stream stdout/stderr from a child process using asyncio, and obtain its exit code after?

description: Stream stdout/stderr from a child process using asyncio, and obtain its exit code after


code:
import logging
import hashlib
import os
outfile = "hash.log"
indir = "/Users/daniel/Sites/work"
logging.basicConfig(filename=outfile, filemode="w", format='%(message)s', level=logging.DEBUG)
for filename in (file for file in os.listdir(indir) if os.path.isfile(file) and not file.endswith("~")):
    with open(filename) as checkfile:
        logging.info(hashlib.md5(checkfile.read()).hexdigest())

rawDescription: python saving output from a for iteration and subprocess for checksum

description: Python saving output from a for iteration and subprocess for checksum


code:
class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)

        self.table = QtGui.QTableWidget(parent=self)
        self.table.setColumnCount(2)
        self.table.setRowCount(2)
        self.table.setHorizontalHeaderLabels(['col1','col2'])
        self.table.setVerticalHeaderLabels(['row1','row2'])
        self.table.setItem(0,0,QtGui.QTableWidgetItem('foo'))
        self.table.setItem(0,1,QtGui.QTableWidgetItem('bar'))
        self.table.setItem(1,0,QtGui.QTableWidgetItem('baz'))
        self.table.setItem(1,1,QtGui.QTableWidgetItem('qux'))

        layout = QtGui.QGridLayout()
        layout.addWidget(self.table, 1, 0)
        self.setLayout(layout)

        self.clip = QtGui.QApplication.clipboard()

    def keyPressEvent(self, e):
        if (e.modifiers() & QtCore.Qt.ControlModifier):
            selected = self.table.selectedRanges()

            if e.key() == QtCore.Qt.Key_C: #copy
                s = '\t'+"\t".join([str(self.table.horizontalHeaderItem(i).text()) for i in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1)])
                s = s + '\n'

                for r in xrange(selected[0].topRow(), selected[0].bottomRow()+1):
                    s += self.table.verticalHeaderItem(r).text() + '\t'
                    for c in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1):
                        try:
                            s += str(self.table.item(r,c).text()) + "\t"
                        except AttributeError:
                            s += "\t"
                    s = s[:-1] + "\n" #eliminate last '\t'
                self.clip.setText(s)

rawDescription: copy pyqt table selection, including column and row headers

description: Copy pyqt table selection, including column and row headers


code:
from matplotlib.markers import TICKDOWN

def significance_bar(start,end,height,displaystring,linewidth = 1.2,markersize = 8,boxpad  =0.3,fontsize = 15,color = 'k'):
    # draw a line with downticks at the ends
    plt.plot([start,end],[height]*2,'-',color = color,lw=linewidth,marker = TICKDOWN,markeredgewidth=linewidth,markersize = markersize)
    # draw the text with a bounding box covering up the line
    plt.text(0.5*(start+end),height,displaystring,ha = 'center',va='center',bbox=dict(facecolor='1.', edgecolor='none',boxstyle='Square,pad='+str(boxpad)),size = fontsize)

pvals = [0.001,0.1,0.00001]
offset  =1
for i,p in enumerate(pvals):
    if p>=0.05:
        displaystring = r'n.s.'
    elif p<0.0001:
        displaystring = r'***'
    elif p<0.001:
        displaystring = r'**'
    else:
        displaystring = r'*'

    height = offset +  max(cell_lysate_avg[i],media_avg[i])
    bar_centers = index[i] + numpy.array([0.5,1.5])*bar_width
    significance_bar(bar_centers[0],bar_centers[1],height,displaystring)

rawDescription: How to add significance levels on bar graph using Python's Matplotlib?

description: Add significance levels on bar graph using Python's Matplotlib


code:
from itertools import combinations
seq_len = 6
_0_count = 2
positions = range(seq_len)
_0_positions = combinations(positions, _0_count)
[[0 if i in zpos else 1 for i in positions] for zpos in _0_positions]

rawDescription: Find all combinations with i 0s and (n-i) 1s in array of size n.


rawDescription: App Engine Entity to Dictionary

description: App Engine Entity to Dictionary


code:
class MySQLAlchemySessionMiddleware(object):
    def process_request(self, request):
        request.db_session = settings.Session()

    def process_response(self, request, response):
        try:
            session = request.db_session
        except AttributeError:
            return response
        try:
            session.commit()
            return response
        except:
            session.rollback()
            raise

    def process_exception(self, request, exception):
        try:
            session = request.db_session
        except AttributeError:
            return
        session.rollback()

rawDescription: Best way to integrate SqlAlchemy into a Django project

description: Integrate SqlAlchemy into a Django project


code:
import io
from scipy import misc

def show_tree(decisionTree, file_path):
    dotfile = io.StringIO()
    export_graphviz(decisionTree, out_file=dotfile)
    pydot.graph_from_dot_data(dotfile.getvalue()).write_png(file_path)
    i = misc.imread(file_path)
    plt.imshow(i)

# To use it
show_tree(decisionT, 'test.png')

rawDescription: Display Decision Tree in IPython Notebook


code:
from django.contrib.admin.widgets import ManyToManyRawIdWidget

class VerboseManyToManyRawIdWidget(ManyToManyRawIdWidget):
    def __init__(self, rel, attrs=None):
        super(VerboseManyToManyRawIdWidget, self).__init__(rel, attrs)

    def label_for_value(self, value):
        values = value.split(',')
        str_values = []
        key = self.rel.get_related_field().name
        for v in values:
            try:
                obj = self.rel.to._default_manager.using(self.db).get(**{key: v})
                str_values += [escape(u'%s' % obj)]
            except self.rel.to.DoesNotExist:
                str_values += [u'???']
        return u' <strong>%s</strong>' % (u', '.join(str_values))

class MyAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in ('foo', 'bar'):
            try:
                del kwargs['request']
            except KeyError:
                pass
            kwargs['widget'] = VerboseManyToManyRawIdWidget(db_field.rel)
            return db_field.formfield(**kwargs)
        return super(MyAdmin,self).formfield_for_dbfield(db_field,**kwargs)

rawDescription: How to show raw_id value of a ManyToMany relation in the Django admin?

description: Show raw_id value of a ManyToMany relation in the Django admin


code:
answers = NewAnswer.objects.filter(user=request.user).filter(audioData__database=id).values_list('audioData_id', 'question_id')
available_pairs = AudioQuestionPair.objects.filter(database=id)
for a in answers:
    available_pairs = available_pairs.exclude(audioData_id=a[0], question_id=a[1])

rawDescription: Django exclude Items from Queryset from another Queryset


code: L = [(x, y[0]) for x, y in L]

rawDescription: List Changing In Python

description: List Changing In Python


code:
import string
def list_difference(list1, list2):
    """Uses list1 as the reference, returns list of items not in list2."""
    diff_list = []
    for item in list1:
        if not item in list2:
            diff_list.append(item)
    return diff_list

def list_in_list_newline_diff(h, i):
    """When h[0] equals i[0], diffs h[2] against i[2], returns diff'd h."""
    new_h = [[],[],[]]
    hh = zip(h[0], h[1], h[2])
    ii = zip(i[0], i[1], i[2])
    for hi, hd, hr in hh:
        # print 'host %s\n%s' % (hi, hr.split('\n'))
        for pi, pd, pr in ii:
                if hi == pi:
                #print 'prev %s\n%s' % (pi, pr.split('\n'))
                    hr = string.join(list_difference(hr.split('\n'), pr.split('\n')), sep='\n')
        if hr == '':
            continue
        new_h[0].append(hi)
        new_h[1].append(hd)
        new_h[2].append(hr)
    return new_h

rawDescription: Remove duplicate elements of a list when a different element matches - Python


code:
def kolorigu():
    text2.delete('1.0', END)
    for sonsilabo in teksto:
        text2.insert(INSERT,sonsilabo,"silabo")
        ########################
        text2.update_idletasks()
        ########################
        time.sleep(2)

rawDescription: Tkinter text highlight with delay doesn't work


rawDescription: Scrapy : getting only td elements with ALIGN=RIGHT


code: cropped = bimage[cropDim[0]:cropDim[1], cropDim[2]:cropDim[3]]

rawDescription: how to convert python list[a,b,c,d] to [a:b, c:d] format

description: Convert python list[a,b,c,d] to [a:b, c:d] format


rawDescription: web2py embedding python into jQuery

description: Web2py embedding python into jQuery


code:
test_list = [ 11, 18, 27, 30, 30, 31, 36, 40, 45, 53 ]
test_series = pd.Series(test_list, name='value_rank')
pd.qcut(test_series, 5, retbins=True, labels=False)

rawDescription: pandas qcut not putting equal number of observations into each bin

description: Pandas qcut not putting equal number of observations into each bin


code:
import win32com.client

xlApp = win32com.client.Dispatch("Excel.Application")
workBook = xlApp.Workbooks.Open("spreadsheet.xls")
xlApp.ExecuteExcel4Macro("FDSFORCERECALC(False)")
workBook.Save()
workBook.Close()
xlApp.Quit()

rawDescription: Swapping VBA code for python

description: Swapping VBA code for python


code:
font = wx.Font(9, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
face = font.GetFaceName()
size = font.GetPointSize()
self.out.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT,"face:%s,size:%d" % (face, size))

rawDescription: wxpython: how to get StyledTextCtrl to render all text in monospace font?

description: Wxpython: how to get StyledTextCtrl to render all text in monospace font


code:
import itertools
n = 4
list(itertools.product((0,1), repeat=n))

rawDescription: binary list contains all possible options by knowing its length in python

description: Binary list contains all possible options by knowing its length in python


code:
parse_list = ['A','B','C','D']

def createdict(l, d):
    if len(l) == 0:
        return None
    d = dict()
    d['name'] = l[0]
    d['childs'] = [createdict(l[1:], d)]
    return d

resultDict = createdict(parse_list, dict())

rawDescription: How to get list of dict instead of using collection.defaultdict in python

description: Get list of dict instead of using collection.defaultdict in python


code:
import networkx as nx
import pygraphviz as pgv # pygraphviz should be available

G = nx.DiGraph()
G.add_edge('a','aa')
G.add_edge('a','ab')
G.add_edge('a','bbc')
G.add_edge('b','ab')
G.add_edge('b','bb')
G.add_edge('c','bbc')
G.add_edge('bb','bba')
G.add_edge('bb','bbc')
A = nx.to_agraph(G)
one = A.add_subgraph(['a','b','c'],rank='same')
two = A.add_subgraph(['aa','ab','bb'],rank='same')
three = A.add_subgraph(['bba','bbc'],rank='same')
A.draw('example.png', prog='dot')

rawDescription: Pygraphviz / networkx set node level or layer


code:
import fnmatch
import os
from netaddr import *
for ip in IPSet(IPRange('192.168.25.47', '192.168.25.50')):
    os.system(r'net use z: \\%s\c$' % ip)
    for file in os.listdir(r'z:\Windows\system32'):
        if fnmatch.fnmatch(file, 'bob.exe'):
            print (ip, file)
    os.system(r"net use Z: /delete /Y")

rawDescription: For Each IP Loop

description: For Each IP Loop


code:
df.columns.levels[0]
df.columns = df.columns.set_levels(['one', 'two'], level=0)
df
df.columns.levels[0]

rawDescription: python pandas: rename single column label in multi-index dataframe

description: Python pandas: rename single column label in multi-index dataframe


code:
x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  # give a shape accepted by tf.nn.max_pool

valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

valid_pad.get_shape() == [1, 1, 1, 1]  # valid_pad is [5.]
same_pad.get_shape() == [1, 1, 2, 1]   # same_pad is  [5., 6.]

rawDescription: What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow?


code:
import csv
import requests

__csvfilepathname__ = r'c:\test\test.csv'
__url__ = 'https://server.domain.com/test.csv'

def csv_reader(filename, enc = 'utf_8'):
    with open(filename, 'r', encoding = enc) as openfileobject:
        reader = csv.reader(openfileobject)
        for row in reader:
            #do something
            print(row)
    return

def csv_from_url(url):
    line = ''
    datalist = []
    s = requests.Session()
    r = s.get(url)
    for x in r.text.replace('\r',''):
        if not x[0] == '\n':
            line = line + str(x[0])
        else:
            datalist.append(line)
            line = ''
    datalist.append(line)
    # at this point you already have a data list 'datalist'
    # no need really to use the csv.reader object, but here goes:
    reader = csv.reader(datalist)
    for row in reader:
        #do something
        print(row)
    return

def main():
    csv_reader(__csvfilepathname__)
    csv_from_url(__url__)
    return

if __name__ == '__main__':
    main ()

rawDescription: Editing a downloaded CSV in memory before writing


code:
jinja2.Template('{% if a is none %}None{% endif %}').render(a=None)
u'None'
jinja2.Template('{% if a is none %}None{% endif %}').render(a=False)
u''

rawDescription: Jinja 2 Templates: how I check in an if statement whether the boolean is False or None


code:
routes_in = (
('/admin','/admin'),
('/admin/$anything','/admin/$anything'),
('/$app/appadmin','/$app/appadmin'),
('/$app/appadmin/$anything','/$app/appadmin/$anything'),
('/Appname/$profileid/','/Appname/default/index/$profileid'),
('/Appname/$profileid/$c/$f/','/Appname/$c/$f/$profileid'),
('/Appname/$profileid/$c/$f/$arg1','/Appname/$c/$f/$arg1/$profileid'),
('/Appname/$profileid/$c/$f/$arg1/$arg2','/Appname/$c/$f/$arg1/$arg2/$profileid'),
)
routes_out = (
('/admin','/admin'),
('/admin/$anything','/admin/$anything'),
('/$app/appadmin','/$app/appadmin'),
('/$app/appadmin/$anything','/$app/appadmin/$anything'),
('/Appname/default/index/$profileid','/Appname/$profileid/'),
('/Appname/$c/$f/$profileid','/Appname/$profileid/$c/$f/'),
('/Appname/$c/$f/$arg1/$profileid','/Appname/$profileid/$c/$f/$arg1'),
('/Appname/$c/$f/$arg1/$arg2/$profileid','/Appname/$profileid/$c/$f/$arg1/$arg2'),
)

rawDescription: web2py routing for all controllers?

description: Web2py routing for all controllers


code: all(value in some_map for value in required_values)

rawDescription: Checking to See if a List Exists Within Another Lists?


code:
class test(object):
    def __init__(self):
        self.a = []

rawDescription: Class retains previous content where new instance is expected


code:
Event.objects.filter(dtime_start__lte=request_datetime,
                     dtime_end__gte=request_datetime)

rawDescription: Find Out A Datetime is Between Two Datetimes in Django


code: nosetests -v --collect-only

rawDescription: List all Tests Found by Nosetest

description: List all Tests Found by Nosetest


rawDescription: PySide/PyQt Text in QTextBrowser disappears after clicking on a link in it


code:
class CSVReader(object):
    def read_if_reloaded(self):
        # Do your thing here
        self.expires_on = self.calculate_next_load_time()
        self.checksum = self.calculate_current_checksum()

@charts.route('/pie')
def pie():
  if request.headers.get('ETag') == charts.data.checksum:
      return ('', 304, {})
  # ...
  response = jsonify(my_data)
  response.headers['Expires'] = charts.data.expires_on
  response.headers['ETag'] = charts.data.checksum
  return response

rawDescription: making a cache for json which can be explicitly purged

description: Making a cache for json which can be explicitly purged


code:
from openerp import models, fields, api

class ProductCodeWizard(models.TransientModel):
    _name = 'nk.code_wizard'

    template = fields.Many2one(
        'product.template',
        required=True,
        default=lambda self: self.env.context['active_id'],
    )
    replacement_code = fields.Char(string="Replacement Code")
    original_code = fields.Char(
        string="Original Code",
        related='template.default_code',
        readonly=True,
    )

    @api.one
    def action_replace(self):
        self.template.default_code = self.replacement_code

rawDescription: Using popup window to get user inputs and use those inputs in button's function in OpenERP


code:
from lxml import etree
import sys
import os
import glob
import argparse
import pandas as pd
import re

pos_conversion_rules = {
    '[.;:?!]':'XPS',
    '[,]':'XPW',
    'blablabla':'BLABLA'
}

def look_up(i):
    for k in pos_conversion_rules:
        pattern = re.compile(k)
        if pattern.match(i):
            return pos_conversion_rules[k]

path = "~/where/my/files/are/"
f = "*.xml"
docs = [f for f in glob.glob(os.path.join(path,f))]

parser = etree.XMLParser(recover=True)

for d in docs:
    x = []
    tree = etree.parse(d,parser)

    for node in [z for z in  tree.iterfind(".//LM")]:
        x.append([node.text,
                  node.get('lemma'),
                  look_up(node.get('catg'))])
        if node.tail.rstrip():
            x.append([node.tail.rstrip(),
                      node.tail.rstrip(),
                      look_up(node.tail.rstrip())])

    df = pd.DataFrame(x)

rawDescription: Parse a character outside a tag in xml

description: Parse a character outside a tag in xml


code:
a = numpy.empty_like (b)
a[:] = b

rawDescription: How to copy data from a numpy array to another

description: Copy data from a numpy array to another


code:
from ctypes import *

msvcrt = CDLL('msvcrt')
message_string = b"Hello World!\n"
msvcrt.printf(b"Testing: %s\n", message_string)

rawDescription: Python 3.5 ctypes libc printf() only prints first byte of string Windows 7/10


code: return bool(coll)

rawDescription: What's the recommended way to return a boolean for a collection being non-empty in python?


code:
import urllib, urllib2
url = '/doaction.php'
data= {'hashcode': 'blah', 'name':'blahblah', 'type':'blahblahblah'}
request = urllib2.Request(url, urllib.urlencode(data))
response = urllib2.urlopen(request)

rawDescription: Submitting form (Python)

description: Submitting form (Python)


code: result = gaussian_filter(a, sigma = 1., mode='wrap')

rawDescription: Blurring a 3D numpy array with periodic boundary conditions


code:
class RateField(models.DecimalField):
    def __init__(self, *args, **kwargs):
        kwargs.update(dict(max_digits=2, decimal_places=1, choices=MY_CHOICES))
        super(RateField, self).__init__(*args, **kwargs)

rate = RateField()

rawDescription: Django - Model with preset field (custom field)


rawDescription: Inconsistency when I'm mapping lists into a dictionary using dict and zip - Python


code:
allTrue = all(somePredicate(elem) for elem in someIterable)
anyTrue = any(somePredicate(elem) for elem in someIterable)

rawDescription: Check if a predicate evaluates true for all elements in an iterable in python


code:
d={}
for pid,vid,v in df.itertuples(index=False):
    d.setdefault(pid,[])
    d[pid].append((vid,v))

rawDescription: Pandas to_dict group all non index columns into a list of tuples


code: subprocess.call(["path/to/app"] + arg)

rawDescription: How to send array as argument?

description: Send array as argument


code:
from django.shortcuts import get_object_or_404, redirect

def post(request, slug, post_id):
    obj = get_object_or_404(Post, pk=post_id)
    if obj.slug != slug:
        return redirect('post', slug=obj.slug, post_id=obj.pk)

rawDescription: Django slug and id as URL redirect

description: Django slug and id as URL redirect


code:
import os
import pandas as pd

file_names = []
data_frames = []
for filename in os.listdir(path):
    name = os.path.splitext(filename)[0]
    file_names.append(name)
    df = pd.read_csv(filename, header=None)
    df.rename(columns={0: name}, inplace=True)
    data_frames.append(df)

combined = pd.concat(data_frames, axis=1)

rawDescription: File names (from multiple files) as a column names in one data frame


code:
import time
import fluidsynth

fs = fluidsynth.Synth()
fs.start()

sfid = fs.sfload("example.sf2")
fs.program_select(0, sfid, 0, 0)

fs.noteon(0, 60, 30)
fs.noteon(0, 67, 30)
fs.noteon(0, 76, 30)

time.sleep(1.0)

fs.noteoff(0, 60)
fs.noteoff(0, 67)
fs.noteoff(0, 76)

time.sleep(1.0)

fs.delete()

rawDescription: Playing streaming sounds in python from frequency, chords


code:
# Given a threshold, return a function suitable for
# use by old cmp argument
def comparator(threshold):
    def compare(A, B):
        if abs(A.y - B.y) < threshold:
            return cmp(A.x, B.x)
        else:
            return cmp(A.y, B.y)
    return compare

from functools import cmp_to_key
my_cmp = comparator(0.6)  # Or whatever threshold you need
sorted_list = sorted(my_list, key=cmp_to_key(my_cmp))

rawDescription: How to sort object with two criteria in Python3?

description: Sort object with two criteria in Python3


rawDescription: Sum of all values in a Python dict

description: Sum of all values in a Python dict


code: soup.find_all(scope=["row", "col"])

rawDescription: BeautifulSoup find_all() to find elements that have one of several acceptable attribute values


rawDescription: Sorting a list of lists based on a list of strings


code:
import numpy as np
import cv2
import pylab as pl

def compute_pointness(I, n=5):
    # Compute gradients
    # GX = cv2.Sobel(I, cv2.CV_32F, 1, 0, ksize=5, scale=1)
    # GY = cv2.Sobel(I, cv2.CV_32F, 0, 1, ksize=5, scale=1)
    GX = cv2.Scharr(I, cv2.CV_32F, 1, 0, scale=1)
    GY = cv2.Scharr(I, cv2.CV_32F, 0, 1, scale=1)
    GX = GX + 0.0001  # Avoid div by zero

    # Threshold and invert image for finding contours
    _, I = cv2.threshold(I, 100, 255, cv2.THRESH_BINARY_INV)
    # Pass in copy of image because findContours apparently modifies input.
    C, H = cv2.findContours(I.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    heatmap = np.zeros_like(I, dtype=np.float)
    pointed_points = []
    for contour in C:
        contour = contour.squeeze()
        measure = []
        N = len(contour)
        for i in xrange(N):
            x1, y1 = contour[i]
            x2, y2 = contour[(i + n) % N]

            # Angle between gradient vectors (gx1, gy1) and (gx2, gy2)
            gx1 = GX[y1, x1]
            gy1 = GY[y1, x1]
            gx2 = GX[y2, x2]
            gy2 = GY[y2, x2]
            cos_angle = gx1 * gx2 + gy1 * gy2
            cos_angle /= (np.linalg.norm((gx1, gy1)) * np.linalg.norm((gx2, gy2)))
            angle = np.arccos(cos_angle)
            if cos_angle < 0:
                angle = np.pi - angle

            x1, y1 = contour[((2*i + n) // 2) % N]  # Get the middle point between i and (i + n)
            heatmap[y1, x1] = angle  # Use angle between gradient vectors as score
            measure.append((angle, x1, y1, gx1, gy1))

        _, x1, y1, gx1, gy1 = max(measure)  # Most pointed point for each contour

        # Possible to filter for those blobs with measure > val in heatmap instead.
        pointed_points.append((x1, y1, gx1, gy1))

    heatmap = cv2.GaussianBlur(heatmap, (3, 3), heatmap.max())
    return heatmap, pointed_points

def plot_points(image, pointed_points, radius=5, color=(255, 0, 0)):
    for (x1, y1, _, _) in pointed_points:
        cv2.circle(image, (x1, y1), radius, color, -1)

def main():
    I = cv2.imread("glLqt.jpg", 0)
    heatmap, pointed_points = compute_pointness(I, n=5)
    pl.figure()
    pl.imshow(heatmap, cmap=pl.cm.jet)
    pl.colorbar()
    I_color = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB)
    plot_points(I_color, pointed_points)
    pl.figure()
    pl.imshow(I_color)

if __name__ == '__main__':
    main()

rawDescription: Is it possible in OpenCV to plot local curvature as a heat-map representing an object's "pointiness"?


code:
people = models.UserRelationships.query.filter_by(user_id=user_id, active=ACTIVE_RECORD)
friends = people.filter_by(friends=YES)

return jsonify(friends.first().as_dict())

rawDescription: Confused by Python returning JSON as string instead of literal


code:
U = np.dot(u, y)
d = U/w
a = u*d[:,None]
c = a.sum()

rawDescription: What is the best vectorization method here?


code:
def menu_items(request):
    if 'pm' in request.resolver_match.namespaces:
        return {
            'projects': Project.objects.all().order_by('name'),
            'people': People.objects.filter(Q(group="MAAD") | Q(group="OAR")).order_by('name')
        }
    return {}

rawDescription: Only run context preprocessor for views from my app


code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QSettings, QPoint, QSize

class myApp(QWidget):
    def __init__(self):
        super(myApp, self).__init__()

        self.settings = QSettings( 'My company', 'myApp')

        # Initial window size/pos last saved. Use default values for first time
        self.resize(self.settings.value("size", QSize(270, 225)))
        self.move(self.settings.value("pos", QPoint(50, 50)))

    def closeEvent(self, e):
        # Write window size and position to config file
        self.settings.setValue("size", self.size())
        self.settings.setValue("pos", self.pos())

        e.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = myApp()
    frame.show()
    app.exec_()

rawDescription: How to remember last geometry of PyQt application?

description: Remember last geometry of PyQt application


code:
import re

with open("Tyger.txt") as f:
    words = re.findall("\w+", f.read())

rawDescription: Taking punctuation out of a a python list


code:
web.header('Content-Type', 'text/plain')
return json.dumps(...)

rawDescription: Return multiple lines in web.py

description: Return multiple lines in web.py


code: terms = re.split(r"[+\-/*^()]+", cleanFunc)

rawDescription: Mystery undesired split-by-period in python re.split()


rawDescription: how do I pass dir 'abc/file.js' as slug in django url?

description: Pass dir 'abc/file.js' as slug in django url


code:
import subprocess
from multiprocessing import Process

Process(target=subprocess.call, args=(('ls', '-l', ), )).start()

rawDescription: start python script as background process from within a python script

description: Start python script as background process from within a python script


code:
classmap = {
  'foo': foo
}

obj = classmap[parser['name']]()

rawDescription: python dynamic class names

description: Python dynamic class names


code:
choice = ''
mother_dict= {'son_dict':{'Name':'Jason','Age':26},'daughter_dict':{'Name':'Emma','Age':19}}
father_dict = {}
while choice not in mother_dict:
    choice = raw_input('Which dict do you want? ')

father_dict[choice] = mother_dict[choice]

rawDescription: implement dynamic nested dictionaries?

description: Implement dynamic nested dictionaries


code:
from IPython.display import SVG, display
def show_svg():
    display(SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg'))

rawDescription: Display SVG in IPython notebook from a function


code:
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)

rawDescription: How to properly handle Redis connections with Python / Python RQ?

description: Properly handle Redis connections with Python / Python RQ


code:
import sys

from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
le = QtGui.QLineEdit(w)
le.setEchoMode(QtGui.QLineEdit.Password)
le.show()
w.show()

sys.exit(app.exec_())

rawDescription: PyQt -How do make my text have an asterisk on it?


rawDescription: Python subtract first element from each respective row of matrix


code:
c = get_config()
c.InteractiveShellApp.exec_lines = [
    'import numpy as np\n'
    'import scipy as sp\n'
    'import matplotlib as plt\n'
]

rawDescription: Configuring Jupyter default imports

description: Configuring Jupyter default imports


code: s="svn cp {root_dir}/{trunk}/ {root_dir}/{tag}/".format(**locals())

rawDescription: Python: Formatting a string using variable names placeholders


code:
def hollowSquare(height, symb, count=0):
    if count < height-1:
        if not count:
            print (symb * height)
        else:
            print (symb + ' ' * (height-2) + symb)
        hollowSquare(height, symb, count+1)
    else:
        print (symb * height)

hollowSquare(5, '*')

print('-----')

def hollowSquare(height,symb,count):
    innerSquare = height -2
    print ('*' * height)
    for i in range(innerSquare):
        print ('*' + ' ' * innerSquare + '*')
    print ('*' * height)

hollowSquare(5, '*', 0)

rawDescription: Re-writing function recursively

description: Re-writing function recursively


code:
str_occs = 'Occ1,Occ2,Occ3'
list_occs = str_occs.split(',')

def prev_occ(occupation, list_occs):
    prev_occ_index = list_occs.index(occupation) + 1
    try:
        ret_val = list_occs[prev_occ_index]
    except:
        ret_val = "No prior occupations."

    return ret_val

rawDescription: Python, loop that gets a value then tests that value again


code:
import re
# Use "with" so the file will automatically be closed
with open("input.txt", "r") as fobj:
    text = fobj.read()
# Use word boundary anchors (\b) so only five-digit numbers are matched.
# Otherwise, 123456 would also be matched (and the match result would be 12345)!
output = re.findall(r'\b\d{5}\b', text)
# Join the matches together
out_str = ",".join(output)
# Write them to a file, again using "with" so the file will be closed.
with open("output.txt", "w") as outp:
    outp.write(out_str)

rawDescription: Python regex findall into output file

description: Python regex findall into output file


code:
import threading
import socket

def print_message(socket):
  data = socket.recv(512)
  print(data)

def handle_client(clientsocket):
  print("We have a new client")
  thread = threading.Thread(target=print_message, args=(clientsocket,))
  thread.run()

# create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
serversocket.bind((socket.gethostname(), 8080))
# become a server socket
serversocket.listen(5)

while True:
  # accept connections from outside
  (clientsocket, address) = serversocket.accept()
  # now do something with the clientsocket
  # in this case, we'll pretend this is a threaded server
  ct = handle_client(clientsocket)

rawDescription: Server that accept multiple client

description: Server that accept multiple client


code:
# Import the modules to send commands to the system and access GPIO pins
from subprocess import call
import RPi.GPIO as gpio
from time import sleep
from os import system

gpio.setmode(gpio.BCM) # Set pin numbering to BCM numbering
gpio.setup(22, gpio.IN) # Set up pin 22 as an input

def confirmation(pin):
    #print "Confirmation Function on pin %d" % pin
    system('echo Press again to confirm Shut down menu! | wall -n') # let all logged in users know
    gpio.remove_event_detect(pin)
    gpio.add_event_detect(pin, gpio.RISING, bouncetime=200)
    sleep(2) # if button has been pressed again within 2 seconds, shut down function is called
    if gpio.event_detected(pin):
        shutdown(pin)
    else:
        #print "canceled"
        system('echo Shut down canceled! | wall -n') # let all logged in users know
        main()

def resetPin(pin):
    #print "Pin %d has been reset" % pin
    gpio.remove_event_detect(pin)
    gpio.add_event_detect(pin, gpio.RISING, bouncetime=200)

def reboot(pin):
    #print "Reboot Function on pin %d" % pin
    call('reboot', shell=False)
    exit(0)

# Define a function to run when an interrupt is called
def shutdown(pin):
    #print "ShutDown function on pin %d" % pin
    system('echo Press again if you want to reboot instead of shut down! | wall -n') # let all logged in users know
    gpio.remove_event_detect(pin)
    gpio.add_event_detect(pin, gpio.RISING, bouncetime=200)
    sleep(2) # if the button has been pressed for a third time, within 2 seconds, Pi will reboot
    if gpio.event_detected(pin):
        reboot(pin)
    else:
        call('halt', shell=False)
        exit(0)

# Define a function to keep script running
def main():
    #print "Main Function"
    resetPin(22)
    while True:
        sleep(1)
        if gpio.event_detected(22):
            confirmation(22)

main() # Run the main function to keep script running

rawDescription: Python "shutdown / reboot raspberry pi" script, using a single button


code:
import sys
import clr

sys.path.append(r"C:\Users\...")

clr.AddReference("MyDll")

from mynamespace import myclass

x = myclass()

rawDescription: Use .NET dll in Python

description: Use .NET dll in Python


code:
#!/usr/bin/env python
# encoding: utf-8

import xlwt
import xlrd
import os

current_file = xlwt.Workbook()
write_table = current_file.add_sheet('sheet1', cell_overwrite_ok=True)

key_list = [u'City', u'Country', u'Received Date', u'Shipping Date', u'Weight', u'1635']
for title_index, text in enumerate(key_list):
    write_table.write(0, title_index, text)

file_list = ['1635.xlsx', '1644.xlsx']

i = 1
for name in file_list:
    data = xlrd.open_workbook(name)

    table = data.sheets()[0]
    nrows = table.nrows
    for row in range(nrows):
        if row == 0:
            continue
        for index, context in enumerate(table.row_values(row)):
            write_table.write(i, index, context)
        i += 1

current_file.save(os.getcwd() + '/result.xls')

rawDescription: Python to combine Excel spreadsheets

description: Python to combine Excel spreadsheets


code: response.headers['P3P'] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'

rawDescription: Cookie not accepted on IE when authenticating with Graph API (oauth) - iframe app


code:
from scrapy.contrib.feedexport import FileFeedStorage
import os
import bz2

MB = 1024 * 1024

class Bz2FileFeedStorage(FileFeedStorage):
  IN_PROGRESS_MARKER = ".inprogress"

  def __init__(self, uri):
    super(Bz2FileFeedStorage, self).__init__(uri)
    self.in_progress_file = self.path + Bz2FileFeedStorage.IN_PROGRESS_MARKER

  def open(self, spider):
    dirname = os.path.dirname(self.path)
    if dirname and not os.path.exists(dirname):
      os.makedirs(dirname)
    return bz2.BZ2File(self.in_progress_file, "w", 10 * MB)

  def store(self, file):
    super(Bz2FileFeedStorage, self).store(file)
    os.rename(self.in_progress_file, self.path)

rawDescription: Rename output file after scrapy spider complete


rawDescription: sql "LIKE" equivalent in django query

description: Sql "LIKE" equivalent in django query


rawDescription: Python Data Structure Recommendations for a 2D Grid


code:
import bs4

your_html = """..."""
soup = bs4.BeautifulSoup(your_html)
big_dict = {}

for table in soup.find_all("table"):
    key = table.find("em").get_text().strip()
    big_dict[key] = []
    headers = []
    for th in table.find_all("th"):
        headers.append(th.get_text().strip())
        big_dict[key].append({headers[-1]: []})
    for row in table.find_all("tr"):
        for i, cell in enumerate(row.find_all("td")):
            big_dict[key][i][headers[i]].append(cell.get_text().strip())

print(big_dict)

rawDescription: how to change soup file to 'dict' save?

description: Change soup file to 'dict' save


code:
class nlcmap(LinearSegmentedColormap):
    """A nonlinear colormap"""

    name = 'nlcmap'

    def __init__(self, cmap, levels):
        self.cmap = cmap
        self.monochrome = self.cmap.monochrome
        self.levels = asarray(levels, dtype='float64')
        self._x = self.levels-self.levels.min()
        self._x/= self._x.max()
        self._y = linspace(0, 1, len(self.levels))

    def __call__(self, xi, alpha=1.0, **kw):
        yi = interp(xi, self._x, self._y)
        return self.cmap(yi, alpha)

rawDescription: Nonlinear colormap with Matplotlib

description: Nonlinear colormap with Matplotlib


code:
from pathlib import Path

p = Path(__file__).parents[1]

print(p)
# /absolute/path/to/two/levels/up

rawDescription: python: get directory two levels up

description: Python: get directory two levels up


code:
esk = None

while True:
    # load this batch
    scan_generator = MyTable.scan(max_results=10, exclusive_start_key=esk)

    # do something usefull
    for item in scan_generator:
        pass  # do something usefull
    # are we done yet ?
    else:
        break;

    # Load the last keys
    esk = scan_generator.kwargs['exclusive_start_key'].values()

rawDescription: Pagination in Amazon DynamoDB using Boto


code:
import matplotlib.pyplot as plt

data_images = [
    [[1,2,3],[1,2,3],[1,2,3]],
    [[1,1,1],[2,2,2],[3,3,3]],
    [[1,2,1],[2,2,2],[1,2,1]],
]

#----------------------------------

index = 0

def toggle_images(event):
    global index

    index += 1

    if index < len(data_images):
        plt.imshow(data_images[index])
        plt.draw()
    else:
        plt.close()

#----------------------------------

plt.imshow(data_images[index])

plt.connect('key_press_event', toggle_images)
plt.show()

rawDescription: How to set matplotlib to show every image of an array?

description: Set matplotlib to show every image of an array


code:
import datetime
d1 = datetime.date.today()
from dateutil.relativedelta import relativedelta
d1 + relativedelta(months=1)
d2 = datetime.date(year=2012,month=1,day=31)
d2 + relativedelta(months=1)

rawDescription: How to find one month later from a date with django?

description: Find one month later from a date with django


code:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from mayavi import mlab

import random

def draw_graph3d(graph, graph_colormap='winter', bgcolor = (1, 1, 1),
                 node_size=0.03,
                 edge_color=(0.8, 0.8, 0.8), edge_size=0.002,
                 text_size=0.008, text_color=(0, 0, 0)):

    H=nx.Graph()

    # add edges
    for node, edges in graph.items():
        for edge, val in edges.items():
            if val == 1:
                H.add_edge(node, edge)

    G=nx.convert_node_labels_to_integers(H)

    graph_pos=nx.spring_layout(G, dim=3)

    # numpy array of x,y,z positions in sorted node order
    xyz=np.array([graph_pos[v] for v in sorted(G)])

    # scalar colors
    scalars=np.array(G.nodes())+5
    mlab.figure(1, bgcolor=bgcolor)
    mlab.clf()

    #----------------------------------------------------------------------------
    # the x,y, and z co-ordinates are here
    # manipulate them to obtain the desired projection perspective
    pts = mlab.points3d(xyz[:,0], xyz[:,1], xyz[:,2],
                        scalars,
                        scale_factor=node_size,
                        scale_mode='none',
                        colormap=graph_colormap,
                        resolution=20)
    #----------------------------------------------------------------------------

    for i, (x, y, z) in enumerate(xyz):
        label = mlab.text(x, y, str(i), z=z,
                          width=text_size, name=str(i), color=text_color)
        label.property.shadow = True

    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
    mlab.pipeline.surface(tube, color=edge_color)

    mlab.show() # interactive window

# create tangled hypercube
def make_graph(nodes):

    def make_link(graph, i1, i2):
        graph[i1][i2] = 1
        graph[i2][i1] = 1

    n = len(nodes)

    if n == 1: return {nodes[0]:{}}

    nodes1 = nodes[0:n/2]
    nodes2 = nodes[n/2:]
    G1 = make_graph(nodes1)
    G2 = make_graph(nodes2)

    # merge G1 and G2 into a single graph
    G = dict(G1.items() + G2.items())

    # link G1 and G2
    random.shuffle(nodes1)
    random.shuffle(nodes2)
    for i in range(len(nodes1)):
        make_link(G, nodes1[i], nodes2[i])

    return G

# graph example
nodes = range(10)
graph = make_graph(nodes)
draw_graph3d(graph)

rawDescription: Drawing multilevel graphs with networkx?

description: Drawing multilevel graphs with networkx


code:
from itertools import izip_longest # in py3, this is renamed zip_longest

def flatten(nested_list):
    return zip(*_flattengen(nested_list)) # in py3, wrap this in list()

def _flattengen(iterable):
    for element in izip_longest(*iterable, fillvalue=""):
        if isinstance(element[0], list):
            for e in _flattengen(element):
                yield e
        else:
            yield element

rawDescription: Expand and flatten a ragged nested list

description: Expand and flatten a ragged nested list


code:
import ctypes
ctypes.windll.user32.LockWorkStation ()

rawDescription: Logout or switch user in Windows using Python


code:
words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo', 'question', 'format']
difflib.get_close_matches('Hello', words)

rawDescription: Python: find closest string (from a list) to another string


code:
from __future__ import print_function
from collections import deque
import threading
import time
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol

class MsgReceiver(Protocol):
    def __init__(self, myQueue):
        self.myQueue = myQueue

    def connectionMade(self):
        print("Connection made!")

    def dataReceived(self, msg):
        self.myQueue.append(msg)

class MsgReceiverFactory(Factory):
    def __init__(self, myQueue):
        self.myQueue = myQueue

    def buildProtocol(self, addr):
        return MsgReceiver(self.myQueue)

def showMessages(myQueue):
    while True:
        time.sleep(1)
        try:
            msg = myQueue.popleft()
            print(msg)
        except IndexError:
            print("No messages in the queue")

myQueue = deque()

factory = MsgReceiverFactory(myQueue)
p = threading.Thread(target=showMessages, args=(myQueue,))
p.daemon = True
p.start()

reactor.listenTCP(42000, factory)
reactor.run()

rawDescription: can I return data in python twisted's dataRecieved method

description: Return data in python twisted's dataRecieved method


rawDescription: In python, how to convert a hex ascii string to raw internal binary string?


code:
textfile = open('output.txt','w')
syslist = dir(sys)

MAX_COLUMNS = 3 # Maximum number of columns to print
colcount = 0    # Track the column number

for x in syslist:
    # First thing we do is add one to the column count when
    # starting the loop. Since we're doing some math on it below
    # we want to make sure we don't divide by zero.
    colcount += 1

    textfile.write(x)

    # After each entry, add a tab character ("\t")
    textfile.write("\t")

    # Now, check the column count against the MAX_COLUMNS. We
    # use a modulus operator (%) to get the remainder after dividing;
    # any number divisible by 3 in our example will return '0'
    # via modulus.
    if colcount % MAX_COLUMNS == 0:
        # Now write out a new-line ("\n") to move to the next line.
        textfile.write("\n")

textfile.close()

rawDescription: Write text to file in columns

description: Write text to file in columns


code:
np.random.seed(0)
np.random.shuffle(arr) # in-place
sets = np.array_split(arr, 4)

rawDescription: Efficient splitting of data in Python

description: Efficient splitting of data in Python


code:
import numpy as np
import theano
import theano.tensor as T

# Create some data with 100 samples, 10 features
X = np.random.randn(100, 10)
X_sym = T.fmatrix('X')
# Create prediction function
predict_function = theano.function(inputs=[X_sym], outputs=self.y_pred)

# See the actual prediction
print(predict_function(X))

rawDescription: How to read values of Theano's LogisticRegression class predictions within DeepLearningTutorials package?

description: Read values of Theano's LogisticRegression class predictions within DeepLearningTutorials package


code:
from google.appengine.ext import db
q = db.GqlQuery("SELECT * FROM Song WHERE composer = 'Lennon, John'")

rawDescription: Google App Engine GQL query on localhost


code:
import itertools

flat_a = itertools.chain.from_iterable(
            (e.list if e.has_list else (e,))
                for e in a)

for xa, xb in zip(flat_a, b):
    xa.b = xb

rawDescription: Traverse a list of lists and a list together


code: mydatetime += timedelta( minutes=3.5 )

rawDescription: How to represent a time that does not change - Python

description: Represent a time that does not change - Python


code:
def paths(self, acc=[]):
    if self.is_leaf():
        yield [self.node]+acc

    for child in self.children:
        for leaf_path in child.paths([self.node]+acc): # these two
            yield leaf_path                            # lines do that

rawDescription: Python (yield): all paths from leaves to root in a tree


code:
import numpy as np
import h5py
...
f = h5py.File("foo.hdf5", "r")
set1 = np.asarray(f["foo/bar"])
var1 = set1["var1"]
var2 = set1["var2"]
var3 = set1["var3"]

rawDescription: What is a fast way in Python to read HDF5 compound dtype arrays?


code:
m = max(a)
[i for i, j in enumerate(a) if j == m]

rawDescription: How to find all positions of the maximum value in a list?

description: Find all positions of the maximum value in a list


code:
# E is (M, N), U is (M, K), s is (K,) and Vt is (K, N), where K = min(M, N)
# f is an integer such that 0 < f < K

U, s, Vt = np.linalg.svd(E, full_matrices=False)
Fbar = U[:, :f].dot(np.diag(s[:f])).dot(Vt[:f, :])

rawDescription: Need function to re-construct matrix E from the partial product


code:
import time
from tkinter import *
tk = Tk()
fps=30
increment=2
t = ('FPS: %s; Speed: %s'% (fps,increment))
canvas = Canvas(tk,height='500',width='500')
canvas.pack()
canvas.create_oval(175,100,325,250,fill='red',outline='black')
canvas.create_line(50,100,450,100)
canvas.create_line(50,450,450,450)
canvas.create_text(250,30,text='Bounce!',fill='red',font=('Consolas',30))
w = canvas.create_text(250,75,text= t,fill='black',font=('Consolas',20)) #mainproblem

while True:
    increment += 1
    if increment % 2 == 0 and fps > 1:
        fps = fps - 1
    t = ('FPS: %s; Speed: %s'% (fps,increment))

    canvas.itemconfig(w, text= t)

    for x in range(0, int(200/increment)):
        canvas.move(1,0,increment)
        tk.update()
        time.sleep(1/fps)
    for x in range(0, int(200/increment)):
        canvas.move(1,0,-(increment))
        tk.update()
        time.sleep(1/fps)

rawDescription: Python tkinter module text transitioning


code:
from Tkinter import *

import matplotlib
#matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import os
import matplotlib.mlab as mlab

class App:

    def __init__(self, master, figureList=[]):
        frame = Frame(master, bg='#00BFFF', borderwidth=1, relief=RAISED)
        frame.pack(expand=YES, fill=BOTH)

        master.title("Listed Plots")
        #Fullscreen for windows
        if os.name == 'nt':
            master.wm_state('zoomed')

        #Create a Canvas
        self.canvas = canvas = Canvas(frame,bg='#00BFFF', relief=SUNKEN)
        canvas.config(highlightthickness=0)

        #Create a Scrollbar Horisontal
        hbar=Scrollbar(frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=canvas.xview)

        #Create a Scrollbar Vertical
        vbar=Scrollbar(frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
        vbar.config(command=canvas.yview)

        canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        canvas.pack(side=TOP,expand=True,fill=BOTH)

        #Create a Frame in the canvas
        canvFrame = Frame(canvas, bg='#00BFFF')
        canvFrame.pack()

        Label(canvFrame, text="Matplots of data", bg='#B4EEB4').pack(side=TOP,fill=X)

        plotNbr = 1
        windowHeight = 10

        for fig in figureList:
            Label(canvFrame, text="#Plotnumber: " + str(plotNbr), bg='#FF7F24').pack(side=TOP,fill=X)

            frm = Frame(canvFrame, bg='#9FB6CD')
            frm.pack(side=TOP, fill=X)

            self.canvasMPL2, self.canvasMPLToolBar2 = getCanvas(frm, fig)
            self.canvasMPL2.pack(side=TOP)
            self.canvasMPLToolBar2.pack(side=TOP)

            #Create space for the plot and tool bar.
            windowHeight = windowHeight + 680
            plotNbr = plotNbr + 1

        canvas.config(width=1200,height= windowHeight)
        canvas.config(scrollregion=(0,0,1200, windowHeight))

        canvas.create_window(0,0, anchor = NW, window = canvFrame, width = 1200, height = windowHeight)

        canvas.focus_set() #Doesnt work with FigureCanvasTkAgg, this steals the focus
        #canvas.focus_force()
        #canvas.grab_set_global()

        #scrollwheel settings
        canvas.configure(yscrollincrement='25')

##        def rollWheel(event):
##            #print "Mousescroll"
##            direction = 0
##            if event.num == 5 or event.delta == -120:
##                direction = 1
##            if event.num == 4 or event.delta == 120:
##                direction = -1
##            event.widget.yview_scroll(direction, UNITS)

        master.bind('<MouseWheel>', lambda event,s=self: self.rollWheel(event))
        canvas.bind('<Button-4>', lambda event: rollWheel(event))
        canvas.bind('<Button-5>', lambda event: rollWheel(event))

    def rollWheel(self, event):
        #print "Mousescroll"
        direction = 0
        if event.num == 5 or event.delta == -120:
            direction = 1
        if event.num == 4 or event.delta == 120:
            direction = -1
##        event.widget.yview_scroll(direction, UNITS)
        self.canvas.yview_scroll(direction, UNITS)

plottedFigures = [] #To store matplotlib figures

def addPlottedFig(figure):
    '''
    Matplotlib figures to be shown in the GUI
    '''
    plottedFigures.append(figure)

def getCanvas(masterWidget, figure=None):
    '''
    Returns canvas of plot and canvas of tool bar
    '''
    if(figure==None):
        f = getHistoGramPlot() #For testing
        canvas = FigureCanvasTkAgg(f, master=masterWidget)
    else:
        canvas = FigureCanvasTkAgg(figure, master=masterWidget)

    canvas.show()
    toolbar = NavigationToolbar2TkAgg( canvas, masterWidget )
    toolbar.update()

    #return the plot and the toolbar
    return canvas.get_tk_widget(), canvas._tkcanvas

def initiate(FigureList=None):
    '''
    Start the GUI Application
    '''
    root = Tk()
    app = App(root, FigureList)
    root.mainloop()

def main():
    '''
    Shows 2 histograms plots in a tkinter GUI
    '''
    fig1 = getHistoGramPlot()
    fig2 = getHistoGramPlot()

    figList = [fig1, fig2]

    initiate(figList)

def getHistoGramPlot():
    '''
    Return a figure of a histogram
    '''
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)
    fig = plt.figure(figsize=(12, 6), dpi=100)
    ax = fig.add_subplot(111)
    n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.6)
    bincenters = 0.5*(bins[1:]+bins[:-1])
    mu = np.median(x)
    sigma = np.std(x)
    y = mlab.normpdf( bincenters, mu, sigma)
    l = ax.plot(bincenters, y, 'r--', linewidth=1)
    ax.set_xlabel('Values')
    ax.set_ylabel('Probability')
    xlimTop = max(x)
    xlimBottom = min(x)
    ax.set_xlim(xlimBottom, xlimTop)
    ax.grid(True)

    return fig

if __name__ == '__main__':
    main()

rawDescription: Why does FigureCanvasTkAgg disables mouse scroll event in scrolled matplotlib GUI?


code:
from itertools import chain
from django.forms import RadioSelect
from django.utils.encoding import force_unicode

class RadioSelectNotNull(RadioSelect):
    def get_renderer(self, name, value, attrs=None, choices=()):
        """Returns an instance of the renderer."""
        if value is None: value = ''
        str_value = force_unicode(value) # Normalize to string.
        final_attrs = self.build_attrs(attrs)
        choices = list(chain(self.choices, choices))
        if choices[0][0] == '':
            choices.pop(0)
        return self.renderer(name, str_value, final_attrs, choices)

rawDescription: How to get rid of the bogus choice generated by RadioSelect of Django Form

description: Get rid of the bogus choice generated by RadioSelect of Django Form


code:
#circle.circle_computation()
if circle_create == True:
    #circle_list[i].draw_circle()
    for j in xrange(i):
        circle_list[j].draw_circle()

rawDescription: Accessing all elements in an iterated list under a loop in Python


code:
# import only cv, no opencv
# this also brings in sub modules such as highgui
import cv
# no "cv" prepended before all method names
src_mat = cv.LoadImageM('yourfilename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
# let's show the image in a window
cv.NamedWindow('your name', 1)
cv.ShowImage('your name', src_mat)
cv.WaitKey

rawDescription: Difference between "import cv" and "import opencv.cv" using Python + OpenCV?


code:
import re
s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt'
re.findall('OU=([^,]+)', s)
re.findall('OU=([^,]+)', s)[1:3]
' / '.join(re.findall('OU=([^,]+)', s)[1:3])

rawDescription: python parsing string using regex

description: Python parsing string using regex


code:
from bs4 import BeautifulSoup
data = """

    Title A

     First Subtitle of Title A
    "Text for first subtitle"

"""
soup = BeautifulSoup(data)
titles = soup.find_all('strong')
titles[0].text
titles[1].get_text(strip=True)
titles[1].next_sibling.strip()

rawDescription: BeautifulSoup: Parsing bad Wordpress HTML


code:
ingredients = set(['sugar', 'flour', 'apple'])
recipe_elements = set([i.strip() for i in recipe.split(' ')])
used_ingredients = ingredients & recipe_elements    # the intersection

rawDescription: Extract topic keywords from text

description: Extract topic keywords from text


code:
def find_all_ImageSequences(dir):

    child_directories = [x for x in os.listdir(dir) if len(x.split(".")) == 1]
    if len(child_directories) >= 1:
        for directory in child_directories:
            if os.path.isdir(dir + "\\" + directory):
                find_all_ImageSequences(dir + "\\" + directory)

    for object in os.listdir(dir):
        if object.endswith(('.dpx','.jpg','.jpeg','.exr','.tif')):
            image_filename = object.split(".")[0]
            Image_Sequences_List.append(dir + "\\" + str(object))
            if any([x for x in os.listdir(dir) if x.split(".")[0] != image_filename and len(x.split(".")) > 1]):
                extras = set([x.split(".")[0] for x in os.listdir(dir) if x.split(".")[0] != image_filename])
                for i in extras:
                    Image_Sequences_List.append(i)
            break

rawDescription: Find image sequences in a directory - Quicker than os.walk()?


code:
from django.db import models
from django import forms

class BitFlagFormField(forms.MultipleChoiceField):
    widget = forms.CheckboxSelectMultiple

    def __init__(self, *args, **kwargs):
        super(BitFlagFormField, self).__init__(*args, **kwargs)

class BitFlagField(models.Field):
    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return "Integer"

    def get_choices_default(self):
        return self.get_choices(include_blank=False)

    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        choicedict = dict(field.choices)

    def formfield(self, **kwargs):
        # do not call super, as that overrides default widget if it has choices
        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name),
                    'help_text': self.help_text, 'choices':self.choices}
        if self.has_default():
            defaults['initial'] = self.get_default()
        defaults.update(kwargs)
        return BitFlagFormField(**defaults)

    def get_db_prep_value(self, value):
        if isinstance(value, int):
            return value
        elif isinstance(value, list):
            return sum(value)

    def to_python(self, value):
        result = []
        n = 1
        while value > 0:
            if (value % 2) > 0:
                result.append(n)
            n *= 2
            value /= 2
        return sorted(result)

    def contribute_to_class(self, cls, name):
        super(BitFlagField, self).contribute_to_class(cls, name)
        if self.choices:
            func = lambda self, fieldname = name, choicedict = dict(self.choices):" and ".join([choicedict.get(value,value) for value in getattr(self,fieldname)])
            setattr(cls, 'get_%s_display' % self.name, func)

rawDescription: How should I represent a bit flags int field in django admin?


code:
s = Series(np.random.rand(10))
s
s.describe()
s.describe()[['count','mean']]

rawDescription: Modify output from Python Pandas describe


code:
import numpy as np

from bokeh.plotting import figure, show, output_notebook
import matplotlib as mpl

output_notebook()

N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (int(r), int(g), int(b)) for r, g, b, _ in 255*mpl.cm.viridis(mpl.colors.Normalize()(radii))
]

p = figure()

p.scatter(x, y, radius=radii,
          fill_color=colors, fill_alpha=0.6,
          line_color=None)

show(p)

rawDescription: Using colormap with bokeh scatter

description: Using colormap with bokeh scatter


code:
import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.tag_vars = {
            "underline": tk.IntVar(),
            "red": tk.IntVar(),
            }

        self.text = MyText(self, width=40, height=8)
        self.text.tag_configure("red", foreground="red")
        self.text.tag_configure("underline", underline=True)

        toolbar = tk.Frame(self)
        self.underline = tk.Checkbutton(self, text="Underline",
                                        onvalue = True, offvalue=False,
                                        variable = self.tag_vars["underline"]
                                        )
        self.red = tk.Checkbutton(self, text="Red",
                                  onvalue = True, offvalue=False,
                                  variable = self.tag_vars["red"]
                                  )
        self.underline.pack(in_=toolbar, side="left")
        self.red.pack(in_=toolbar, side="left")

        toolbar.pack(side="top", fill="x")
        self.text.pack(side="top", fill="both", expand=True)

class MyText(tk.Text):
    def __init__(self, parent, *args, **kwargs):
        tk.Text.__init__(self, *args, **kwargs)

        self.parent = parent

        # add a new bind tag, "CustomText" so we
        # can have code run after the class binding
        # has done it's work
        bindtags = list(self.bindtags())
        i = bindtags.index("Text")
        bindtags.insert(i+1, "CustomText")
        self.bindtags(tuple(bindtags))

        # set a binding that will fire whenever a
        # self-inserting key is pressed
        self.bind_class("CustomText", "<Key>", self.OnKey)

    def OnKey(self, event):
        # we are assuming this is called whenever
        # a character is inserted. Apply or remove
        # each tag depending on the state of the checkbutton
        for tag in self.parent.tag_vars.keys():
            use_tag = self.parent.tag_vars[tag].get()
            if use_tag:
                self.tag_add(tag, "insert-1c", "insert")
            else:
                self.tag_remove(tag, "insert-1c", "insert")

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

rawDescription: Apply tags in a text widget

description: Apply tags in a text widget


rawDescription: How to subtract a total from the closest multiple of 10

description: Subtract a total from the closest multiple of 10


code:
with open('filename') as f:
    for line in f:
        # Each line will be iterated; so you could call a function here
        # that does the connection via SSH
        print(line)

rawDescription: Print SSH output from Cisco Router to a text file


code: del lst[len(lst)-n:]  # do not remove the len(lst) since n==0 is possible !

rawDescription: How to cut off a varying length tail from a list elegantly?

description: Cut off a varying length tail from a list elegantly


code: sc = pyplot.scatter(pos_x, pos_y, c=values, s=values_scaled)

rawDescription: matplotlib: scatter and colorbar values

description: Matplotlib: scatter and colorbar values


code:
def zips():
    from itertools import izip
    li = range(1,20000000)
    next_li = iter(li)
    next(next_li)  # advance one step
    for tup in izip(li, next_li):
        pass
    del li

rawDescription: What is taking up the memory in this for-loop?


code:
for j in data:
    text_file.write('%s\n' % '\t'.join(str(x) for x in j))

rawDescription: Python - Iterating through a list of list with a specifically formatted output; file output


code:
def __getattr__(self, name):
    return [m.__dict__.get(name) for m in self.members]

rawDescription: Extract (not known beforehand) attributes from objects in a list


code: data = [line.split(', ') for line in fptr]

rawDescription: In python, how to break list of strings into list of list of strings?


code:
lst = ['1', '+', 'b1']
any(char.isalpha() for string in lst for char in string)

rawDescription: How to find out whether a list contains any letters

description: Find out whether a list contains any letters


code:
import pydot

menu = {'dinner':
            {'chicken':'good',
             'beef':'average',
             'vegetarian':{
                   'tofu':'good',
                   'salad':{
                            'caeser':'bad',
                            'italian':'average'}
                   },
             'pork':'bad'}
        }

def draw(parent_name, child_name):
    edge = pydot.Edge(parent_name, child_name)
    graph.add_edge(edge)

def visit(node, parent=None):
    for k,v in node.iteritems():
        if isinstance(v, dict):
            # We start with the root node whose parent is None
            # we don't want to graph the None node
            if parent:
                draw(parent, k)
            visit(v, k)
        else:
            draw(parent, k)
            # drawing the label using a distinct name
            draw(k, k+'_'+v)

graph = pydot.Dot(graph_type='graph')
visit(menu)
graph.write_png('example1_graph.png')

rawDescription: Dictionary object to decision tree in Pydot


code:
thing = [   ['Promotion not applied', 'Buy1, get 1 for FOR $4.50', '(', 'details', ')'],      [], ['Promotions Applied:', 'BUY 1, GET 1 FOR $4.50', '(', 'details', ')']   ]

promo_applied, _, promo_not_applied = map(lambda s:s[:2], thing)

rawDescription: how do i create a two list from a list inside of an array?

description: Create a two list from a list inside of an array


code:
pupils_dictionary = {}

for x in range(3):
    new_key = raw_input('Enter new key: ')
    new_age = raw_input('Enter new age: ')
    pupils_dictionary[new_key] = new_age
print(pupils_dictionary)

rawDescription: Python: Inserting into dictionary using FOR Loop


code: reviews = [{'content': c, 'author': a} for c, a in zip(contentList, authorList)]

rawDescription: How to put 2 lists into 1 Dictionary in Python?

description: Put 2 lists into 1 Dictionary in Python


code:
import os, sys, signal

def signal_handler(signal, frame):
    pid = os.getpid()
    print('You pressed Ctrl+C (pid = {0})'.format(pid))
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

rawDescription: How can I find out the PID of the affected process in signal handler

description: Find out the PID of the affected process in signal handler


rawDescription: How do I create a .MDB and import a CSV into it using Python

description: Create a .MDB and import a CSV into it using Python


code:
def encode(s):
    l = [ord(i) for i in s]
    return ''.join([chr(i + 2) for i in l])

rawDescription: Using a caesarian cipher on a string of text in python?


code:
import re
a = 'groups(1,12,23,12)'
re.findall("\d+", a)
map(int, re.findall("\d+", a))

rawDescription: Python:- Convert a string into list

description: Python:- Convert a string into list


code:
def joinby(value, arg):
    return arg.join(value)

rawDescription: Comma separated lists in django templates


code:
m = 0
def musica():
    global m
    if m == 0:
        music.play(loops=-1)
        m = 1

rawDescription: Playing a sound once pygame

description: Playing a sound once pygame


code:
tfv = TfidfVectorizer(min_df=3, max_features=None, strip_accents='unicode',
                      analyzer='word',token_pattern=r'\w{1,}',ngram_range=(1, 2),
                      use_idf=1,smooth_idf=1,sublinear_tf=1)
div = DictVectorizer()

X = []

# fit/transform the TfidfVectorizer on the training data
vectors = tfv.fit_transform(traindata)

for i, pagerank in enumerate(pageranks):
    feature_dict = {'pagerank': pagerank}
    # get ith row of the tfidf matrix (corresponding to sample)
    row = vect.getrow(i)

    # filter the feature names corresponding to the sample
    all_words = tfv.get_feature_names()
    words = [all_words[ind] for ind in row.indices]

    # associate each word (feature) with its corresponding score
    word_score = dict(zip(words, row.data))

    # concatenate the word feature/score with the datamining feature/value
    X.append(dict(word_score.items() + feature_dict.items()))

div.fit_transform(X)  # training data based on both Tfidf features and pagerank

rawDescription: How to normalize ranked data in scikit learn?

description: Normalize ranked data in scikit learn


code:
class LogMsg(unicode):
     def __new__(cls, string_):
        if string_[:7] == '!Silent':
            cls.nolog = True
            return super(LogMsg, cls).__new__(cls, string_[7:])
        else:
            cls.nolog = False
            return super(LogMsg, cls).__new__(cls, string_)

a = LogMsg('The hard drive is on fire!')
a
a.nolog
b = LogMsg('!SilentMy feet stink! :(')
b
b.nolog

rawDescription: Python Logging - Apply formatter globally


code:
data = {'a':a, 'b':b, 'c':c}
data['a'] is a

rawDescription: How to create numpy structured array with multiple fields of different shape?

description: Create numpy structured array with multiple fields of different shape


code:
conn = mysql.connector.connect(...)
result =  conn.cmd_query("LOAD DATA LOCAL INFILE query")
print (result['info_msg'])

rawDescription: mysql_info() equivalent in Python

description: Mysql_info() equivalent in Python


code:
urls = []
for row in rows:
    for key in row.custom:
        urls.append(row.custom[key].text)

rawDescription: Reading URL's from google docs spreadsheet python


code:
if __name__ == '__main__':
    Arbitrary_code_function.run()
    MRJOB_Jobname.run()

rawDescription: How do you filter s3 files before sending input to mrjob mapper?

description: Filter s3 files before sending input to mrjob mapper


code:
import win32gui, win32con

hwnd = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)

rawDescription: How can I maximize a specific window with Python?

description: Maximize a specific window with Python


code:
from multiprocessing import Pool
# import parsers here

parsers = {
    'parser1': parser1.process,
    'parser2': parser2.process,
    'parser3': parser3.process,
    'parser4': parser4.process,
    'parser5': parser5.process,
    'parser6': parser6.process,
    'parser7': parser7.process,
}

# Sets that define which items can use high parallelism,
# and which must use low
high_par = {"parser1", "parser3", "parser4", "parser6", "parser7"}
low_par = {"parser2", "parser5"}

def process_items(key, value):
    parsers[key](value)

def run_pool(func, items, num_items, check_set):
    pool = Pool(num_items)
    out = pool.map(func, (item for item in items if item[0] in check_set))
    pool.close()
    pool.join()
    return out

if __name__ == "__main__":
    items = [('parser2', x), ...] # Your list of tuples
    # Process with high parallelism
    high_results = run_pool(process_items, items, 4, high_par)
    # Process with low parallelism
    low_results = run_pool(process_items, items, 2, low_par)

rawDescription: How to change number of parallel processes?

description: Change number of parallel processes


code:
import string
import collections

def topNwords(mywords, N = 10, stoplist = set(), filtered = set()):
    # mywords = mycorpus.words()
    nocapitals = [word.lower() for word in mywords]
    filtered = [word for word in nocapitals if word not in stoplist]
    nopunctuation = [s.translate(None, string.punctuation) for s in filtered]
    woordcounter = collections.Counter(nopunctuation)
    top_ten = [word for word, freq in woordcounter.most_common(N)]
    return top_ten

top_ten = topNwords('This is a test. It is only a test. In case of a real emergency'.split(), N = 10)
print("De 10 meest frequente woorden zijn: {w}".format(w = ', '.join(top_ten)))

rawDescription: word frequency count strange results

description: Word frequency count strange results


code:
import os
from flask import Flask,redirect

app = Flask(__name__)

@app.route('/')
def hello():
    return redirect("http://www.example.com", code=302)

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

rawDescription: Redirecting to URL in Flask

description: Redirecting to URL in Flask


code:
s = ','.join(itertools.chain(dataset)) + '\n'
newfile = 'export.csv'
f = open(newfile,'w')
f.write(s)
numpy.savetxt(f, (numpy.transpose(datastack)), delimiter=', ')
f.close()

rawDescription: Writing csv header removes data from numpy array written below


code:
SYMBOLS_ALL = ['EUR/USD', 'USD/JPY', 'GBP/USD', 'EUR/GBP', 'USD/CHF', 'AUD/NZD', \
'CAD/CHF', 'CHF/JPY', 'EUR/AUD', 'EUR/CAD', 'EUR/JPY', 'EUR/CHF', 'USD/CAD', \
'AUD/USD', 'GBP/JPY', 'AUD/CAD', 'AUD/CHF', 'AUD/JPY', 'EUR/NOK', 'EUR/NZD', \
'GBP/CAD', 'GBP/CHF', 'NZD/JPY', 'NZD/USD', 'USD/NOK', 'USD/SEK']

for s in SYMBOLS_ALL:
    filename = s.replace("/","") + ".csv"
    open(filename, 'w')

rawDescription: Split all "/" and create a CSV off this arrays in python


code:
lst = ['hello', 'blah', 'boo', 'braininess']
heapq.nlargest(2, lst, key=len)

rawDescription: Find the two longest strings from a list || or the second longest list in PYTHON


code:
length = 4
indices = np.arange(length)
k_array = np.empty((length,) * 3 + (3,), dtype=np.intp)
k_array[..., 0] = indices
k_array[... ,1] = indices[:, None]
k_array[... ,2] = indices[:, None, None]

rawDescription: Numpy: what happens with 3D-boolean index array


rawDescription: Python Pandas - Re-ordering columns in a dataframe based on column name


code: FTP.storbinary("STOR " + FILE, open(FILE, 'rb'))

rawDescription: Python FTPLib Won't Upload Complete File


code:
import itertools
itertools.izip(a, itertools.islice(a,1,None))

rawDescription: Python get N elements from a list at a time using lambda


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

# First we read the DataFrame and resample it to get a mean on every hour
df = pd.read_csv('trayport.csv', header=None,
                 parse_dates=[0], index_col=0).resample('H', how='mean').dropna()
# Then we add a week field so we can filter it by the week
df['week']= df.index.map(lambda x: x.isocalendar()[1])

# Create week labels
weekdays = 'Mon Tue Wed Thu Fri Sat Sun'.split()

# Create the figure
fig, ax = plt.subplots()

# For every week we want to plot
for week in range(1, 4):
    # Select out the week
    dfw = df[df['week'] == week].copy()
    # Here we align all the weeks to span over the same time period so they
    # can be shown on the graph one over the other, and not one next to
    # the other.
    dfw['timestamp'] = dfw.index.values - (week * np.timedelta64(1, 'W'))
    dfw = dfw.set_index(['timestamp'])
    # Then we plot our data
    ax.plot(dfw.index, dfw[1], label='week %s' % week)
    # Now to set the x labels. First we resample the timestamp to have
    # a date frequency, and set it to be the xtick values
    resampled = dfw.resample('D')
    ax.set_xticks(resampled.index.values)
    # But change the xtick labels to be the weekdays.
    ax.set_xticklabels(weekdays)
# Plot the legend
plt.legend()

rawDescription: Pandas Tick Data Averaging By Hour and Plotting For Each Week Of History


code:
import pandas as pd
df = pd.DataFrame({
    'A': [['a','b','c','d'],['a','b','c'],['x','y','z','q'],['x','l']],
    'B': [12,7,2,9]
    })
print(df)

#               A   B
# 0  [a, b, c, d]  12
# 1     [a, b, c]   7
# 2  [x, y, z, q]   2
# 3        [x, l]   9

def cumulative_frequence(df, nodes):
    nodes = set(nodes)
    mask = df['A'].apply(lambda group: not nodes.isdisjoint(group))
    return df.ix[mask, ['B']].sum().item()

print(cumulative_frequence(df, ['a']))
print(cumulative_frequence(df, ['a','b']))
# 19
# 19

rawDescription: conditional cumulative sum for pandas array

description: Conditional cumulative sum for pandas array


code:
@receiver(post_save, sender=MyModel)
def my_handler(sender, **kwargs):
    ...

rawDescription: push notification using Django

description: Push notification using Django


rawDescription: How to ignore case while doing most_common in Python's collections.Counter?

description: Ignore case while doing most_common in Python's collections.Counter


code:
import mechanize

br = mechanize.Browser()
br.open('webpage.com')

email='user@domain.com'

br.select_form(nr=0)
br['email'] = email
br.form.add_file(open('filename'), 'mime-type', 'filename')
br.form.set_all_readonly(False)
br.submit()

rawDescription: unable to send data using urllib and urllib2 (python)

description: Unable to send data using urllib and urllib2 (python)


code:
import site
site.getsitepackages()

rawDescription: I deleted the site-packages python?

description: I deleted the site-packages python


code:
def getWeight(input_string):
   sum=0
   for letter in input_string:
      sum+=weight_dict[letter]
   return sum

rawDescription: Converting Single-Letter amino Acids in string to Molecular weights


code:
from django.http import JsonResponse
from django.template.loader import render_to_string

def view_name(request, params):
    objects = Object.objects.all()
    somevar = "something"
    request_id = 123456

    # Context to be passed on to template
    context = {'objects':objects}

    rendered_html = render_to_string('appname/template.html', context)

    return JsonResponse({
        "somevar": somevar,
        "html": rendered_html,
        "request_id": request_id
    })

rawDescription: Put a Rendered Django Template in Json along with some other items


code:
tokens = ['IX', 'C', 'D', 'XL', 'I', 'XC', 'M', 'L', 'CD', 'X', 'IV', 'CM']

def rn_split(numeral, results_so_far=[]):
    if len(numeral)==0:
        return results_so_far # Break the recursion
    for token in tokens:
        if numeral.startswith(token):
            results_so_far.append(token)
            recurse_numeral = numeral[ (len(token)): ]
            return rn_split(recurse_numeral, results_so_far)
    # Remainder of numeral didn't match.  Bail out
    results_so_far.append(numeral)
    return results_so_far

rawDescription: Splitting string based on list values into a list in Python


code:
noc = int(input('Enter number of courses: '))
for i in range(1, noc+1):
    grade = input('Enter grade for course no. {}: '.format(i))
    weight = input('Enter weight for course no. {}: '.format(i))
    #do something with grade and weight here.

rawDescription: Python 3.3: How to create a loop for a string


code:
vars = {}
for i in range(10):
    vars["Ilock"+str(i)] = something()

vars["Ilock4"] = 42

rawDescription: Python 2.7. Being able to fill in variable name instead of index number


code:
import mpmath
mpmath.gammainc(-1,1)
1-mpmath.gammainc(-1,1)
mpmath.mp.dps = 50 # arbitrary precision!
1-mpmath.gammainc(-1,1)

rawDescription: incomplete gamma function in python?

description: Incomplete gamma function in python


code:
from Tkinter import *
root = Tk()

v = IntVar()
v.set(0)

physVar = (("a", 0), ("vi", 1), ("vf", 2), ("t", 3), ("x", 4))

def change_radio():
    for i in range(5):
        e[i].grid()
        l[i].grid()
    e[v.get()].grid_remove()
    l[v.get()].grid_remove()

Label(root, text="Solve for:", padx=5).grid(row=0, column=0, columnspan=6)

r=[]
for txt, val in physVar:
    r.append(Radiobutton(root, text= txt, variable = v, command=change_radio, value=val))
    r[-1].grid(row=1, column=val+1)

l=[]
for txt, val in physVar:
    l.append(Label(root, text=txt))
    l[val].grid(column=0, row=val+2)

e=[]
for txt, val in physVar:
    e.append(Entry(root,text=txt))
    e[val].grid(column=1, columnspan=5, row=val+2)

e[0].grid_remove()
l[0].grid_remove()

root.grid_columnconfigure(1, minsize=4)
root.mainloop()

rawDescription: Change the appearance of labels with Tkinter events


code:
edit_verdad = "vd edit -u '+module.sudo.alias.'%s' = '%s'' \
                   -u '+module.sudo.ent.'%s'.cmd = ALL' \
                   -u '+module.sudo.ent.'%s'.comment = '%s'' \
                   -u '+module.sudo.ent.'%s'.host = ALL' \
                   -u '+module.sudo.ent.'%s'.passwd = 0' \
                   -u '+module.sudo.ent.'%s'.user = '%s'' \
                    '%s'"  %(group_name, options.username, group_name, comment, group_name, group_name, group_name, group_name, options.hostname, "LAST VARS")

rawDescription: TypeError: not enough arguments for format string - python


code:
[x for x in attributes if x['id']=='29'][0]

{'attribute': 's', 'group': 'taille_textile', 'id': '29'}

rawDescription: Return a specific dict from a list of dicts


code:
import math
import itertools

for ob in engine.All:
    if isinstance(ob, Flatlander):
        ob.energy -= decay

for pair in itertools.combinations(engine.All, 2):
    fooditems = [x for x in pair if isinstance(x, Food) and not x.skip]
    animals = [x for x in pair if isinstance(x, Flatlander) and not x.skip]
    if not (fooditems and animals):
        continue
    animal = animals[0]
    food = fooditems[0]
    if collides(animal.pos, animal.r, food.pos, food.r):
        # This seems an odd calculation to me but I think it follows your code.
        if animal.energy + food.r < animal.r ** 2 * math.pi:
            # eating is feasible; the animal always eats the food if it can
            food.skip = True
            animal.energy += food.r # Not the area?

for ob in engine.All:
    if isinstance(ob, Flatlander) and ob.energy < 0:
        ob.skip = True # dead

# Remove dead things
engine.All = [ob for ob in engine.All if not ob.skip]

# Draw everything (no dead things remain)
for ob in engine.All:
    if ob.drawable:
      ob.draw()

rawDescription: How effectively to remove elements while iterating through the list?


code:
content = ''
with open(filename, 'rt') as f:
    content = f.read()

matches = re.findall(REGEX, content)

rawDescription: Operating on a file's content despite a failure in the 'with' block


code:
import numpy as np
a = np.array([0.5,  0.75,  1.5])
np.diff(a)

rawDescription: Loop for Subtracting within Array Elements


code:
matrix = A.tocoo()

new_row = matrix.row // 5
new_col = matrix.col // 5
bin = (matrix.shape[0] // 5) * new_col + new_row
# Now do a little dance because this is sparse,
# and most of the possible bin should not be in new_row/new_col
# also need to group the bins:
unique, bin = np.unique(bin, return_inverse=True)
sum = np.bincount(bin, weights=matrix.data)
new_col = unique // (matrix.shape[0] // 5)
new_row = unique - new_col * (matrix.shape[0] // 5)

result = scipy.sparse.coo_matrix((sum, (new_row, new_col)))

rawDescription: How to efficiently make new matrix from sum of blocks from bigger sparse matrix

description: Efficiently make new matrix from sum of blocks from bigger sparse matrix


code:
"""Usage: %progName% [-h]

Options:
  -h --help
"""

from docopt import docopt, DocoptExit
import sys

if __name__ == '__main__':
    progName=sys.argv[0]
    for i, arg in enumerate(sys.argv):
        if arg.startswith('--progName='):
            ind=arg.index("=")+1
            progName=arg[ind:]
            del sys.argv[i]
            break

    usage=False
    try:
        args = docopt(__doc__, help=False)
        if args['--help']:
            usage=True
    except DocoptExit as e:
        usage=True
        print ("Bad arguments!")
    if usage:
        print(__doc__.replace('%progName%',progName))
        raise SystemExit
    print(args)

rawDescription: Include spaces in program name in docopt


code:
import subprocess
cmd = ['7z', 'a', 'Test.7z', 'Test', '-mx9']
sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

rawDescription: 7zip Commands from Python

description: 7zip Commands from Python


code:
def leafcounter(node):
    if isinstance(node, dict):
        return sum([leafcounter(node[n]) for n in node])
    else:
        return 1
    leafcounter(tree)

rawDescription: Python method 'count leaves'

description: Python method 'count leaves'


code:
a=float(0)
b=float(-2)
abs(a/b)

rawDescription: How to get rid of the minus in floating zero (-0.0)?

description: Get rid of the minus in floating zero (-0.0)


code:
data = pd.concat([seqdf2, pardf2])
sp = plt.subplot(111)

data.mul([1, 0], axis=0).plot.bar(color=colors,
                                  ax=sp,
                                  align='center',
                                  stacked=True)

data.mul([0, 1], axis=0).plot.bar(color=colors,
                                  ax=sp,
                                  legend=False)

rawDescription: Ploting two barplots side by side

description: Ploting two barplots side by side


code:
while not done:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    screen.fill(WHITE)

    # Every single iteration of the while loop will first reset the x to its initial value
    # You can make x be any value you want your set of rectangles to start from
    x = 0

    # Now start adding up to x's initial value
    for x in range(x, x+100, 10):
      pygame.draw.rect(screen, BLACK, [x, 0, 10, 10], 1)

    pygame.display.flip()

rawDescription: pygame rectangles drawn in loop scroll across screen

description: Pygame rectangles drawn in loop scroll across screen


code:
import datetime

datetime.datetime.strptime(u'Mar232012', '%b%d%Y').strftime('%m/%d/%Y')

rawDescription: Python: Convert unicode string to MM/DD/YYYY


code:
from itertools import chain
filename = 'D:/Workspace/Python/textfile.txt'
with open(filename, 'rb') as inf:
    header = next(inf)
    for index, line in enumerate(inf,start=1):
        with open('D:/Workspace/Python/textfile{}.txt'.format(index) ,'w') as outf:
            outf.write('Path : %s\n' %outf.name)
            outf.write(header)
            for line in chain([line], inf):
                if 'string' in line:
                    header = line
                    break
                outf.write(line)

    for idx in range(1, index):
        filename = 'textfile{}.txt'.format(idx)
        with open(filename, 'a') as outf:
            outf.write(line)

rawDescription: How to print path of the textfile that we just write in another directory and print string in every file in Python

description: Print path of the textfile that we just write in another directory and print string in every file in Python


code:
import sys
sys.path
sys.path.append(directory_to_be_added)

rawDescription: Python Path

description: Python Path


rawDescription: Transforming a rectangle in Python

description: Transforming a rectangle in Python


code:
import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Spin Button Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)

        self.text = wx.SpinCtrl(panel, value="1")
        self.text.SetRange(1, 8)
        self.text.SetValue(1)

        self.text.Bind(wx.EVT_SPINCTRL, self.OnSpin)
        self.text.Bind(wx.EVT_TEXT, self.OnSpin)

    def OnSpin(self, event):
        if self.text.GetValue() > 7:
               self.text.SetValue(7)
               wx.MessageDialog(self,"Test Message","Too Big!").ShowModal()

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

rawDescription: message box for spin ctrl event in wxpython

description: Message box for spin ctrl event in wxpython


code:
import fileinput
MAPS = (('*','999999999'),('**','999999999'))
with open('output.txt','w') as out_file:
  for line in open("test.txt",'r'):
    out_list = []
    for inp_cell in line.rstrip('\n').split('\t'):
      out_cell = inp_cell
      for old, new in MAPS:
        if out_cell == old:
          out_cell = new
      out_list.append(out_cell)
    out_file.write( "\t".join(out_list) + "\n" )

rawDescription: Replacing cell, not string

description: Replacing cell, not string


code:
import sys
from PyQt4 import QtCore, QtGui

class QCustomTreeWidget (QtGui.QTreeWidget):
    def __init__(self, parent = None):
        super(QCustomTreeWidget, self).__init__(parent)
        self.setDragEnabled(True)
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.resize(360,240)

    def dragEnterEvent (self, eventQDragEnterEvent):
        sourceQCustomTreeWidget = eventQDragEnterEvent.source()
        if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget):
            if self != sourceQCustomTreeWidget:
                sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
                eventQDragEnterEvent.accept()
            else:
                sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
                QtGui.QTreeWidget.dragEnterEvent(self, eventQDragEnterEvent)
        else:
            QtGui.QTreeWidget.dragEnterEvent(self, eventQDragEnterEvent)

    def dropEvent (self, eventQDropEvent):
        sourceQCustomTreeWidget = eventQDropEvent.source()
        if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget):
            if self != sourceQCustomTreeWidget:
                sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
                sourceQTreeWidgetItem = sourceQCustomTreeWidget.currentItem()
                isFound = False
                for column in range(0, self.columnCount()):
                    sourceQString = sourceQTreeWidgetItem.text(column)
                    listsFoundQTreeWidgetItem = self.findItems(sourceQString, QtCore.Qt.MatchExactly, column)
                    if listsFoundQTreeWidgetItem:
                        isFound = True
                        break
                if not isFound:
                    (sourceQTreeWidgetItem.parent() or sourceQCustomTreeWidget.invisibleRootItem()).removeChild(sourceQTreeWidgetItem)
                    self.invisibleRootItem().addChild(sourceQTreeWidgetItem)
            else:
                sourceQCustomTreeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
                QtGui.QTreeWidget.dropEvent(self, eventQDropEvent)
        else:
            QtGui.QTreeWidget.dropEvent(self, eventQDropEvent)

class QCustomQWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomQWidget, self).__init__(parent)
        self.my1QCustomTreeWidget = QCustomTreeWidget(self)
        self.my2QCustomTreeWidget = QCustomTreeWidget(self)
        items = [QtGui.QTreeWidgetItem([name]) for name in ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']]
        self.my1QCustomTreeWidget.addTopLevelItems(items)
        self.allQHBoxLayout = QtGui.QHBoxLayout()
        self.allQHBoxLayout.addWidget(self.my1QCustomTreeWidget)
        self.allQHBoxLayout.addWidget(self.my2QCustomTreeWidget)
        self.setLayout(self.allQHBoxLayout)

app = QtGui.QApplication([])
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.show()
sys.exit(app.exec_())

rawDescription: How to stop QTreeWidget from creating the item duplicates on Drag and Drop

description: Stop QTreeWidget from creating the item duplicates on Drag and Drop


code: sys.modules['x.y'] = y

rawDescription: Mask a python submodule from its package's __init__.py


code: t = Timer(10, os._exit, [0])

rawDescription: Python end parent process from thread

description: Python end parent process from thread


rawDescription: How to delete an animated gif from a frame with wxPython?

description: Delete an animated gif from a frame with wxPython


code: sudo('python manage.py collectstatic --noinput', user='www-data')

rawDescription: Fabric - sudo -u

description: Fabric - sudo -u


code:
class EscapingJSONEncoder(json.JSONEncoder):
    def encode(self, obj):
        if isinstance(obj, basestring):
            obj = jinja2.Markup(obj).unescape()
        return json.JSONEncoder.encode(self, obj)

rawDescription: How to unescape HTML characters when encoding JSON with Python

description: Unescape HTML characters when encoding JSON with Python


code:
last_names = [
'Hag ', 'Hag ', 'Basmestad ', 'Grimlavaag ', 'Kleivesund ',
'Fintenes ', 'Svalesand ', 'Molteby ', 'Hegesen ']
last_names.reverse()
last_names

rawDescription: Reversing list with strings in python

description: Reversing list with strings in python


code:
class Composite(Parent):
    def __init__(self, x, list_of_classes, list_of_args):
        Parent.__init__(self, x)
        self._objs = [
            the_class(self.x, *the_args)
            for the_class, the_args
            in zip(list_of_classes, list_of_args)
            if isinstance(the_class, Parent.__class__)
        ]

    def do(self):
        for o in self._objs: o.do()

rawDescription: How to create objects dynamically in an elegant way in python?

description: Create objects dynamically in an elegant way in python


code:
from collections import Counter
dates = ['5-11-15','5-12-15','5-11-15']
c = Counter()
c.update(dates)
print(c)

rawDescription: How do I count the number of times a date shows up in a list?

description: Count the number of times a date shows up in a list


rawDescription: Django user not in request object when running tests


code:
import wx

class sizertest(wx.Panel):

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)
        vsizer = wx.BoxSizer(wx.VERTICAL)
        hsizer1 = wx.BoxSizer(wx.HORIZONTAL)

        button1 = wx.Button(self,-1,"button 1")
        self.Bind( wx.EVT_BUTTON,self.button1,button1)
        hsizer1.Add(button1,1,wx.EXPAND)

        button2 = wx.Button(self,-1,"button 2")
        self.Bind( wx.EVT_BUTTON,self.button2,button2)
        hsizer1.Add(button2,1,wx.EXPAND)

        vsizer.Add(hsizer1,.1,wx.EXPAND)
        self.SetSizer(vsizer)

        tc = wx.TextCtrl(self,-1,"",style=wx.TE_MULTILINE)
        hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer2.Add(tc,1,wx.EXPAND)
        vsizer.Add(hsizer2,1,wx.EXPAND)

    def button1(self,event):pass
    def button2(self,event):pass

if __name__ == "__main__":
    app = wx.App()
    frame = wx.Frame(parent=None, id=-1, title="sizer test")
    panel = sizertest(frame)

    frame.Show()
    app.MainLoop()

rawDescription: wxPython - picking the right sizer to use in an application

description: WxPython - picking the right sizer to use in an application


code:
class Category(models.Model):

    parent = models.ForeignKey('self', default=None, null=True, blank=True, related_name='nested_category')
    nesting_level = models.IntegerField()
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)

rawDescription: How do I add nested categories to a Django model?

description: Add nested categories to a Django model


rawDescription: Filtering on multiple values from a single field - Django


code:
import StringIO
f = StringIO.StringIO("hello world")
f.read(6)
f.tell()
f.seek(0)
f.tell()
f.read()

rawDescription: Is it possible to rewind a python StringIO in-memory file?


code:
class Ui_frmInicial(QWidget):
    def __init__(self, window):                         #window parameter
        QtGui.QWidget.__init__(self)
        self.resize(400, 250)
        self.btnOpen = QtGui.QPushButton(self)
        self.btnOpen.setGeometry(QtCore.QRect(110, 170, 111, 41))
        self.btnOpen.clicked.connect(lambda c: window.show()) #lambda callback

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Ui_mainForm()                              #window definition
    ui = Ui_frmInicial(window)                          #ui initialisation
    ui.show()
    sys.exit(app.exec_())

rawDescription: QMainwindow doesn't show when I clicked a qpushbutton into a QWidget


code:
N, M, K = 100, 200, 300
matrixs = [sparse.rand(N, M, format="csr") for i in xrange(K)]
matrixs2 = [m.tolil().reshape((1, N*M)) for m in matrixs]
m1 = sparse.vstack(matrixs2).tocsr()

# test with dense array
#m2 = np.vstack([m.toarray().reshape(-1) for m in matrixs])
#np.allclose(m1.toarray(), m2)

rawDescription: (Python Scipy) How to flatten a csr_matrix and append it to another csr_matrix?


code:
class MyForm(Form):
    select1 = SelectField('Select 1', ...)
    select2 = SelectField('Select 2', ...)
    select3 = SelectField('Select 3', ...)
    def validate(self):
        if not Form.validate(self):
            return False
        result = True
        seen = set()
        for field in [self.select1, self.select2, self.select3]:
            if field.data in seen:
                field.errors.append('Please select three distinct choices.')
                result = False
            else:
                seen.add(field.data)
        return result

rawDescription: How do I validate wtforms fields against one another?

description: Validate wtforms fields against one another