code:
# For randomness, shuffle the entire array
np.random.shuffle(dataset)

blocks, _ = np.unique(dataset[['idx1', 'idx2']], return_inverse=True)
block_count = np.bincount(_)
where = np.argsort(_)
block_start = np.concatenate(([0], np.cumsum(block_count)[:-1]))

# If we have n elements in a block, and we assign 1 to each array, we
# are left with only n-2. If we randomly assign a fraction x of these
# to the first array, the expected ratio of items will be
# (x*(n-2) + 1) : ((1-x)*(n-2) + 1)
# Setting the ratio equal to 4 (80/20) and solving for x, we get
# x = 4/5 + 3/5/(n-2)

x = 4/5 + 3/5/(block_count - 2)
x = np.clip(x, 0, 1) # if n in (2, 3), the ratio is larger than 1
threshold = np.repeat(x, block_count)
threshold[block_start] = 1 # first item goes to A
threshold[block_start + 1] = 0 # seconf item goes to B

a_idx = threshold > np.random.rand(len(dataset))

A = dataset[where[a_idx]]
B = dataset[where[~a_idx]]

rawDescription: stratified sampling in numpy

description: Stratified sampling in numpy


rawDescription: Python: Unexpected regex behaviour for {n} match


code:
def dumb_prime(N):
    """dumb implementation of is N prime?"""
    for f in generate_possible_factors(N):
        if is_factor(f, N):
            return False
    return True

rawDescription: How to use IPython.parallel map() with generators as input to function

description: Use IPython.parallel map() with generators as input to function


code:
from datetime import datetime
datetime.utcfromtimestamp(float("1294989360"))

rawDescription: Parsing datetime in Python..?

description: Parsing datetime in Python..


code:
def logIt(self, str):
    cmd = "console.log({0});".format(json.dumps(str))
    json.dumps({ "script": cmd })

rawDescription: Escaping JavaScript Strings in Python

description: Escaping JavaScript Strings in Python


code:
def permutation_rec1(onelist,prelist):
    if not onelist:
        yield prelist

    lenlist = len(onelist)
    for i, oneitem in enumerate(onelist):
        leftlist = [onelist[j] for j in range(lenlist) if j != i]
        yield from permutation_rec1(leftlist, prelist + [oneitem])

rawDescription: why can't yield be used in a recursive function

description: Why can't yield be used in a recursive function


code: waa()

rawDescription: Remove package and module name from sphinx function


code:
a = foo()
a.next()
a.send("yieldnow")
a.send("this is lost")
a.send("yieldnow")
a.send("this is lost")
a.send("yieldnow")
a.send("this is lost")
a.send("yieldnow")

rawDescription: Attempting to understand yield as an expression


code:
List1 =[7,6,9]
List2 = List1         # both List1, List2 point to the same object [7, 6, 9]
List1.sort()          # sort List1 in-place, affects the original object
List1, List2
List1 =[7,6,9]
List2 = List1         #same as above
List1 = sorted(List1) #sorted returns a new list, so List1 now points to this new list
List1, List2          #List2 is still unchanged

rawDescription: Can anyone explain why this sorting won't work?


code:
def update_file(filename, ln):
    with open(filename, 'r') as f:
        lines = f.readlines()

    with open(filename, 'w') as f:
        for idx,line in enumerate(lines):
            (idx in ln and f.write('------'))
            f.write(line)

rawDescription: How to add a string to a specific line

description: Add a string to a specific line


rawDescription: Python interval interesction

description: Python interval interesction


code:
res = []
for x in content2:
    if x: # x is not empty (0.0)
        res.append(float(x))
    else:
        res.append(None)
content2[:] = res # replaces items in content2 with those from res

rawDescription: python changing integers/ floats to None in list

description: Python changing integers/ floats to None in list


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

fig = plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05, aspect=10)
#cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])
plt.colorbar(im, cax=cax)

plt.show()

rawDescription: matplotlib imshow fixed aspect and vertical colorbar matching master axis height

description: Matplotlib imshow fixed aspect and vertical colorbar matching master axis height


code:
...
stmt = "INSERT INTO table (first, second, A, B) VALUES (%s, %s, %s, %s)"
cursor.executemany(stmt, data)
...

rawDescription: How can I iterate over Pandas pivot table? (A multi-index dataframe?)

description: Iterate over Pandas pivot table? (A multi-index dataframe?)


rawDescription: How to store value instead of the list that the symbol stands for in python?

description: Store value instead of the list that the symbol stands for in python


code:
import random

l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

replace = random.choice(l)
l[l.index(replace)] = 'x'

print(l)
i = input("Which letter was replaced? ")

if i == replace:
    print("That's right!")
else:
    print("Nope, it was {} that got replaced!".format(replace))

rawDescription: Store and Replace the Word from a List (Python)


code:
def calc_histo(i):
    # create new array 'd_new' by doing some math on DATA using argument i
    histo = to_np_array(histo_shared)
    histo += np.histogram(d_new, bins=BINS,
        range=(DMIN, DMAX))[0].astype(np.int32)

rawDescription: Using numpy array in shared memory slow when synchronizing access


code:
data = {'A': {1: ['Teddy', 'Daniel', 'Alex'],
              2: ['Rob', 'Steve', 'Mark', 'Sandy'],
              3: ['Ruth', 'Sarah', 'James']},
        'B': {1: ['Teddy', 'Alex', 'Steve', 'Sandy', 'Daniel'],
              2: ['Mark', 'Sarah', 'Rob']},
        'C': {1: ['Teddy', 'Daniel', 'Alex'],
              2: ['Mark', 'Sandy']}}

rawDescription: How to find reoccuring string groups (order does not matter) in a dictionary of lists?

description: Find reoccuring string groups (order does not matter) in a dictionary of lists


code:
def test1(a, b, *args, **kwargs): pass
def test2(a, b): pass
def test3(a, b, *args): pass
func_has_positional_args(test1)
func_has_keyword_args(test1)
func_has_positional_args(test2)
func_has_keyword_args(test2)
func_has_positional_args(test3)
func_has_keyword_args(test3)

rawDescription: Introspect function to determine which argument unpacking (positional or keyword) was used


code:
def split_on_parts(s, *parts):
    return [s[sum(parts[:p]):sum(parts[:p+1])] for p in range(len(parts))]

rawDescription: Split string into array with many char pro items


code:
import re
protein_regex = re.compile('^[A-Z]+')
dna = 'PROTEINNAMEatcgatcg... JFENVKDFDFLK'
protein_regex.sub('', dna)

rawDescription: How to find first non-uppercase letter in the string using python

description: Find first non-uppercase letter in the string using python


code:
from urllib import unquote
from flask import json

data = json.loads(unquote(request.query_string.partition('&')[0]))
task = data.get('task')

rawDescription: flask - how to get parameters from a JSON GET request

description: Flask - how to get parameters from a JSON GET request


code: D

rawDescription: How do I create a scipy sparse matrix from a pandas dataframe?

description: Create a scipy sparse matrix from a pandas dataframe


code:
def get_match(list_, el):
    import bisect

    i = bisect.bisect_left(list_, el)

    if i == len(list_):
        return i - 1
    elif list_[i] == el:
        return i
    elif i > 0:
        j = i - 1

        if list_[i] - el > el - list_[j]:
            return j
    return

reference = [99999.0, 100000.0, 100001.0]

sensor = [99999.8234, 99999.9723, 100000.00123, 100000.9924, 100001.02]

for el in reference:
    print(get_match(sensor,el))

None
None
3

rawDescription: Efficiently match up approximate timestamps in Python


code:
import pandas as pd

org = ['doclist[0]', 'doclist[0]', 'doclist[1]', 'doclist[4]', 'doclist[4]']
np = [0, 1, 1, 1, 0]
pr = [0, 0, 0, 0, 1]
df = pd.DataFrame({'Organization':org, 'NP':np, 'Pr':pr})

# Make a "lookup" dataframe of the sums for each category
# (Both the "NP" and "Pr" colums of "sums" will contain the result)
sums = df.groupby('Organization').agg(lambda x: x['NP'].sum() + x['Pr'].sum())

# Lookup the result based on the contents of the "Organization" row
df['Sum'] = df.apply(lambda row: sums.ix[row['Organization']]['NP'], axis=1)

rawDescription: Using groupby to operate only on rows that have the same value for one of the columns pandas python


rawDescription: Format APNS-style JSON message in Python for use with Amazon SNS


rawDescription: Python 2.7 Counting number of dictionary items with given value


code:
import csv

ids = {}

with open('file 2.csv') as f_file2:
    csv_file2 = csv.reader(f_file2, skipinitialspace=True)
    header = next(csv_file2)

    for cols in csv_file2:
        ids[cols[0]] = cols[1]

with open('file 1.csv', 'rb') as f_file1, open('output.csv', 'wb') as f_output:
    csv_file1 = csv.reader(f_file1, delimiter='\t')
    csv_output = csv.writer(f_output, delimiter='\t')

    for cols in csv_file1:
        csv_output.writerow([ids.get(cols[0], cols[0]), ids.get(cols[1], cols[1]), cols[2]])

rawDescription: I'd like to read a column of a tab-delimited file, search for a matching entry in another file, then replace the term


code:
target1 = 'mjzzmjzzmjzz'
key1 = 'zz'
countSubStringMatch(target1, key1)
countSubStringMatch(target1, 'foo') is None

rawDescription: If-else inside a while statement

description: If-else inside a while statement


code:
selected[0]
selected[1]

rawDescription: Python algorithm - picking objects from a list based on object attributes


code:
forms.py
from django import forms

from .models import CheckList, CheckListOption

class CheckListForm(forms.ModelForm):
    class Meta:
        model = CheckList
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(CheckListForm, self).__init__(*args, **kwargs)
        if self.instance.pk:
            self.fields['options'].queryset = CheckListOption.objects.filter(
                checklisttype=self.instance.checklist_type_id
            )
        else:
            self.fields['options'].queryset = CheckListOption.objects.none()

rawDescription: Django Model Design - Many-To-Many Fields


code:
import tesseract
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)

rawDescription: Tesseract use subset of letters

description: Tesseract use subset of letters


rawDescription: Making a dictionary where values are based off of a regex - Python


code:
LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
)

rawDescription: i18n load partially in locale app

description: I18n load partially in locale app


code:
from scipy.optimize import minimize

x0 = [] # fill with your initial guesses
new_points = minimize(fitness, x0, method='Nelder-Mead')  # or whatever algorithm

rawDescription: How to use scipy to optimize the position of n points?

description: Use scipy to optimize the position of n points


code:
def try_float(x):
    try:
        float(x)
        return True
    except (TypeError, ValueError):
        return False

value = '1, 2.3, 4, 5, blah, 6.78'
numbers = [float(x) for x in value.split(',') if try_float(x)]

rawDescription: How to map a list to integers

description: Map a list to integers


code:
import argparse
parser = argparse.ArgumentParser(description='Get Files')
parser.add_argument('-i','--input', help='Input file(s)',required=True, nargs='+')
args = parser.parse_args()
for filename, num in zip(*[iter(args.input)]*2):
    print(filename, num)
    # with open(filename) as f:
    #     ....

rawDescription: Adding an additional argument to an argparse argument


code:
x = "This"
y = 345

mssql_cur.execute("SELECT * FROM mytable WHERE colx = ? AND coly = ?", x, y)

rawDescription: Does pyodbc support any form of named parameters?


code: d[splitstring[0]] = splitstring[1]

rawDescription: Key:Value Pairs from txt file into a dictionary


code:
from collections import Counter
s = 'hello, world \u2615 \U0001F60A'
counts = Counter(s)
for character, count in counts.most_common():
    print(character, '-', count)

rawDescription: How can I count the number of each character in a Python string?

description: Count the number of each character in a Python string


code:
ylimit = 0

ylim(ylimit,)

rawDescription: Matplotlib, Set multiple parameters at once?


code:
import re, os, sys
class Plugin(object):
    """
    The base class from which all plugins are derived.  It is used by the
    plugin loading functions to find all the installed plugins.
    """
    def __init__(self, foo):
        self.foo = foo
    # Any useful base plugin methods would go in here.

def get_plugins(plugin_dir):
    """Adds plugins to sys.path and returns them as a list"""

    registered_plugins = []

    #check to see if a plugins directory exists and add any found plugins
    # (even if they're zipped)
    if os.path.exists(plugin_dir):
        plugins = os.listdir(plugin_dir)
        pattern = ".py$"
        for plugin in plugins:
            plugin_path = os.path.join(plugin_dir, plugin)
            if os.path.splitext(plugin)[1] == ".zip":
                sys.path.append(plugin_path)
                (plugin, ext) = os.path.splitext(plugin) # Get rid of the .zip extension
                registered_plugins.append(plugin)
            elif plugin != "__init__.py":
                if re.search(pattern, plugin):
                    (shortname, ext) = os.path.splitext(plugin)
                    registered_plugins.append(shortname)
            if os.path.isdir(plugin_path):
                plugins = os.listdir(plugin_path)
                for plugin in plugins:
                    if plugin != "__init__.py":
                        if re.search(pattern, plugin):
                            (shortname, ext) = os.path.splitext(plugin)
                            sys.path.append(plugin_path)
                            registered_plugins.append(shortname)
    return registered_plugins

def init_plugin_system(cfg):
    """
    Initializes the plugin system by appending all plugins into sys.path and
    then using load_plugins() to import them.

        cfg - A dictionary with two keys:
        plugin_path - path to the plugin directory (e.g. 'plugins')
        plugins - List of plugin names to import (e.g. ['foo', 'bar'])
    """
    if not cfg['plugin_path'] in sys.path:
        sys.path.insert(0, cfg['plugin_path'])
    load_plugins(cfg['plugins'])

def load_plugins(plugins):
    """
    Imports all plugins given a list.
    Note:  Assumes they're all in sys.path.
    """
    for plugin in plugins:
        __import__(plugin, None, None, [''])
        if plugin not in Plugin.__subclasses__():
            # This takes care of importing zipped plugins:
            __import__(plugin, None, None, [plugin])

rawDescription: Bundle a Python app as a single file to support add-ons or extensions?


code: np.argsort(y, order=('x','y'))

rawDescription: how to sort a scipy array with order attribute when it does not have the field names?

description: Sort a scipy array with order attribute when it does not have the field names


code:
def format(data):
    dic = {}
    for line in data.splitlines():
        if ':' not in line:
            continue
        name, _, value = line.partition(':')
        name = name.strip()
        if name == 'Name':
            dic['F_NAME'], dic['L_NAME'] = value.split(None, 1)  # strips whitespace for us
        elif name == 'DOB':
            dic['DD'], dic['MM'], dic['YY'] = (v.strip() for v in value.split('/', 2))
        elif name == 'Address':
            dic['STREET'], dic['CITY'], dic['ZIP'] = (v.strip() for v in value.split(', ', 2))
    return dic

rawDescription: Failing to append to dictionary. Python

description: Failing to append to dictionary. Python


code:
[{"link": "http://www.randstad.nl/vacatures/1486457/klantadviseur-glasvezel-tijdelijk-%28m-v%29", "title": "Wil je per direct aan de slag in een afwisselend project?  Heb jij ervaring in de glasvezel- of ICT-branche? Werk jij graag in een hectische, dynamische omgeving?\nDan hebben wij een leuke tijdelijke baan voor jou! \nDeze snelgroeiende en onafhankelijke serviceprovider levert een breed scala aan diensten zoals internet, televisie en telefonie op het glasvezelnetwerk . \nIn deze veelzijdige functie ben je verantwoordelijk voor een zo positief mogelijke klantbeleving. Je zorgt voor een proactieve, deskundige advisering aan klanten over diensten en de levering ervan. Je bent verantwoordelijk voor de Backoffice Support van het Klant Contact Center, je denkt actief mee over verbeteringen in communicatie, applicaties en de manier van werken binnen het bedrijf. Je ziet kansen in alle dagelijkse handelingen om de commercie te verhogen op de afdeling.\nWat ga je doen?\nBeantwoorden van tweede- en derdelijns vragen van klanten en externe partners, \nBewaken van procedures voor correcte administratieve afhandeling,\nVerwerken van orders en uitsturen hiervan per post en e-mail,\nBijdragen aan optimaliseren primaire klantprocessen.\nWie ben jij?\nJe hebt een opleiding afgerond op MBO+/HBO niveau, of bent studerend aan HBO of WO,\nMinimaal 1 jaar inbound contactcenterervaring,\nJe hebt een perfecte beheersing van de Nederlandse taal in woord en geschrift,\nErvaring met het Office pakket,\nWerkervaring in glasvezel- en/of ICT-branche is een grote pr\u00e9\nVerder ben je:\nBetrokken, betrouwbaar, transparant en innovatief,\nZelfstandig,\nKlantgericht en commercieel,\nJe vindt het leuk om klanten te enthousiasmeren,\nJe bent een oplossingsgerichte teamspeler en je hebt affiniteit met ICT.\nZie jij jezelf als de perfecte kandidaat voor deze baan? Dan horen we graag vandaag nog van je! Reageer direct via de solliciteer button met je Cv en vertel ons waarom jij de geschikte kandidaat bent voor deze functie!"},
{"link": "http://www.randstad.nl/vacatures/1360086/embedded-software-design-engineer-electric-controls-%28m-v%29", "title": "Ben jij degene die nieuwe dingen bedenkt? Doe je dat het liefst in een team van specialisten? En vind je het leuk bezig te zijn in het werkveld van elektrische installaties voor (railgebonden) voertuigen? lees dan snel verder...\nWat ga jij doen?\nOpstellen van systeem-, product- en module specificaties conform bestek, normen en voorschriften\nHet opstellen van softwarespecificaties en het ontwerp van de software\nFormuleren en uitvoeren van testplannen voor afname beproevingen\nRisicomanagement door toepassing van FMEA en FTA\nOpstellen van ontwerp- en gebruikers documentatie\nRapporteren aan de projectleider\n \nWat verwachten wij van jou?\nMinimaal een HBO-opleiding (Bij voorkeur WO), richting elektrotechniek, embedded systems of gelijkwaardig vakgebied\nRelevante ervaring met het toepassen van normen en richtlijnen voor kwaliteit en veiligheid\nUitstekende communicatieve vaardigheden\nSterk ontwikkeld analytisch denkvermogen\nUitstekende beheersing van de Engels en Nederlandse taal is noodzakelijk\nKennis van en ervaring met elektrische installaties voor railgebonden voertuigen\nKennis van en ervaring met CAN, MVB, C/C++, .NET/C#, FPGA, real-time OS, TCMS, CMMI, EN61508 SIL3, EN50128, FMEA en Matlab/Simulink\nHerken jij jezelf in dit profiel en heb jeinmiddels een aantal jaar werkervaring in jouw vakgebied? Ben jij toe aan de volgende stap in je carriere reageer dan snel via 06-15087716 of mail bas.kooter.rnl@nl.randstad.com"},
...
]

rawDescription: Extract text from an extracted new page from main page


code: f = open('testtext.txt', 'rb')

rawDescription: Unable to read complete text file in Python


code:
@app.route('/home', methods=['GET'])
@app.route('/files', methods=['GET'])
@require_login
def just_render():
    file_name = request.path.replace('/', '')
    template = '{file_name}.html'.format(file_name=file_name)

    return render_template(template)

rawDescription: Is there a way to easily define routes for multiple standard pages in Flask?


code:
sorted_input = sorted(input, key=itemgetter(1))
groups = groupby(sorted_input, key=itemgetter(1))
[{'type':k, 'items':[x[0] for x in v]} for k, v in groups]

rawDescription: Python group by

description: Python group by


code:
def writeshort(txt):
    wordlist = []
    for item in txt:
        if len(item) > 4:
            continue
        wordlist += [item] # or wordlist.append(item) as in your first snippet
    return wordlist

rawDescription: Python - Print only words with less than five characters


rawDescription: grouping lines from a file with regular expression

description: Grouping lines from a file with regular expression


code:
import sys
from PyQt4 import QtGui
from FormProc import FormProc

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    MainWindow = FormProc()
    MainWindow.show()
    sys.exit(app.exec_())

rawDescription: python qwidget closeEvent doesn't call

description: Python qwidget closeEvent doesn't call


code:
import datetime as dt
from collections import defaultdict

def filter_out_common_queries(querylist):
    # add your logic here
    return querylist

queries_time = defaultdict(list)  # personally, I'd use 'set' as the default factory
with open('log.txt') as f:
    for line in f:
        fields = [ x.strip() for x in line.split(',') ]
        timestamp = dt.datetime.strptime(fields[0], "%H:%M:%S")
        queries_time[fields[1]].append(timestamp)

with open('search.txt') as inputf, open('search_output.txt', 'w') as outputf:
    for line in inputf:
        fields = [ x.strip() for x in line.split(',') ]
        timestamp = dt.datetime.strptime(fields[0], "%H:%M:%S")
        queries = filter_out_common_queries(fields[1].split())  # "adidas watches for men" -> "adidas" "watches" "for" "men". "for" is a very generic keyword. You should do well to filter these out
        results = []
        for q in queries:
            poss_timestamps = queries_time[q]
            for ts in poss_timestamps:
                if timestamp - dt.timedelta(seconds=15) <= ts <= timestamp:
                    results.append(q)
        outputf.write(line.strip() + " - {}\n".format(results))

rawDescription: comparing two text file and find out the related word in python

description: Comparing two text file and find out the related word in python


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("pcf", help="pmd, checkstyle, or findbug")
parser.add_argument("mcb", help="major, critical, and blocker")
args = parser.parse_args()

# Use boolean flags
pmd  = 'p' in args.pcf
checkstyle = 'c' in args.pcf
findbug = 'f' in args.pcf

major = 'm' in args.mcb
critical = 'c' in args.mcb
blocker = 'b' in args.mcb

rawDescription: Passing in multiple options for argparse in Python


rawDescription: Numpy, how to convert a 2D array to 3D (by grouping cols to 2 list)


rawDescription: Flask redirecting multiple routes

description: Flask redirecting multiple routes


code:
import Queue

#somewhere accessible to both:
callback_queue = Queue.Queue()

def from_dummy_thread(func_to_call_from_main_thread):
    callback_queue.put(func_to_call_from_main_thread)

def from_main_thread_blocking():
    callback = callback_queue.get() #blocks until an item is available
    callback()

def from_main_thread_nonblocking():
    while True:
        try:
            callback = callback_queue.get(False) #doesn't block
        except Queue.Empty: #raised when queue is empty
            break
        callback()

rawDescription: Execute Python function in Main thread from call in Dummy thread


code:
# Test 600 element array :)
arr = [1] * 600
b3 = BitArray.from_array(arr)
print(int(b3) == ((2**600) - 1))  # True

rawDescription: Python dynamic bit manipulation example

description: Python dynamic bit manipulation example


code:
import games
import games2
import games3

GAMES = {
    'color': games.color,
    'food': games.food,
    'car': games.car,
    'toy': games.toy,
    'somethingelse': games2.somethingelsefunction,
    }

while True:
    question = input('Please enter your question: ').lower()
    if not question:
        break
    for key, func in GAMES.items():
        if key in question:
            func()
            break
    else:
        print('I do not have a game for that! :(')

print('Byebye!')

rawDescription: How to iterate through modules with user input?

description: Iterate through modules with user input


code: pivot = df.pivot_table(values='Usage', rows='Day', cols=['TOD', 'Weekend'])

rawDescription: Sliding window average across time axes

description: Sliding window average across time axes


code:
from __future__ import print_function
from  itertools import tee, izip, ifilterfalse

def window(iterable,n):
    els = tee(iterable,n)
    for i,el in enumerate(els):
        for _ in range(i):
            next(el, None)
    return izip(*els)

def replace(iterable,placeholder=255):
    it = enumerate(iterable)

    def save_last(iterable):
        for i in iterable:
            yield i
        replace.last_index = i[0] #Save the last value
    it = save_last(it)

    it = ifilterfalse(lambda x: x[1]==placeholder, it)
    for i,(left,right) in enumerate(window(it,2)):
        if i==0:
            for j in range(left[0]):
                yield placeholder
        yield left[1]
        if right[0]>left[0]+1:
            if left[1]==right[1]:
                for _ in range(right[0]-left[0]-1):
                    yield left[1]
            else:
                for _ in range(right[0]-left[0]-1):
                    yield placeholder
    yield right[1]
    if right[0]<replace.last_index:
        for i in range(replace.last_index-right[0]):
            yield placeholder

a = [255,1,255,255,1,255,255,255,2,2,255,255,255,2,2,3,255,255,255,3,255,255]
print('\nInput: {}'.format(a))
output = list(replace(a))
print('Proram output: {}'.format(output))
print('Goal output  : {}'.format([255,1,1,1,1,255,255,255,2,2,2,2,2,2,2,3,3,3,3,3,255,255]))

rawDescription: Generator which leaves a placeholder at the beginning and at the end of the input iterator intact


code: mySound.setVolume(0.5) # 0.0~1.0

rawDescription: How to keep playing a sound at a given frequency and volume in Python?

description: Keep playing a sound at a given frequency and volume in Python


code:
from django.shortcuts import HttpResponse
from django.utils import simplejson

from ticket.models import Milestone

def project_choices(request):
    milestone_list = []
    project_id = request.GET.get('project_id')
    milestones = Milestone.objects.filter(project = project_id)
    [milestone_list.append((each_milestone.pk,each_milestone.name)) for each_milestone in milestones]
    json = simplejson.dumps(milestone_list)
    return HttpResponse(json, mimetype='application/javascript')

rawDescription: How to set dynamic mapping between two models in django

description: Set dynamic mapping between two models in django


code:
function_map = {
    'convertFile': convertFile,
    'conv': convertFile,
}
parser = argparse.ArgumentParser()
parser.add_argument( 'command', nargs=1 )
parser.add_argument( 'fileName', nargs='+' )
args= parser.parse_args()
function = function_map[args.command]
function( args.fileName )

rawDescription: execute python script with function from command line, Linux

description: Execute python script with function from command line, Linux


code: ' '.join(item[0] for item in lst)

rawDescription: How can I return a string from the list of pairs?

description: Return a string from the list of pairs


code: indices, = (a != row).any(1).nonzero()

rawDescription: filtering elements of matrix by row in python scipy/numpy

description: Filtering elements of matrix by row in python scipy/numpy


code:
import dis
dis.dis(compile("if somevar in {'foo', 'bar', 'baz'}: pass", '<stdin>', 'exec'))

rawDescription: frozenset() - Example of when one might use them

description: Frozenset() - Example of when one might use them


code: os.environ['USER_IS_ADMIN'] = '1'

rawDescription: Google App Engine: Webtest simulating logged in user and administrator


rawDescription: How to clear cookies in scrapy?

description: Clear cookies in scrapy


code:
import ctypes
intArray10 = ctypes.c_int * 10
myArray = intArray10(*(0 for _ in xrange(10)))

rawDescription: Can you create traditional fixed length and type arrays in Python?

description: Create traditional fixed length and type arrays in Python


rawDescription: Python re.compile, split string into two while preserving delimiter


code:
def parse_items(self, response):
    for deal in response.xpath("//div[@class='user-comment-text'][1]"):
        item = DesidimeItem()

        item["deal"]  = deal.xpath(".//p/text()").extract()
        item["link"] = deal.xpath(".//p[1]/a[1]/@href").extract()

        yield item

rawDescription: scrapy spider code check

description: Scrapy spider code check


code:
processes = [
  Process(target=ImageData.shepard_interpolation, args=(ImageData(50, 50), q,))
  for _ in range(3) ]
for process in processes:
    process.start()
for process in processes:
    process.join()

rawDescription: Multiprocessing in Python - Concurrence

description: Multiprocessing in Python - Concurrence


code: json.dumps({'success': True, 'data': celldict.values()})

rawDescription: format python output to json

description: Format python output to json


code:
def json_as_dotted_path(obj, path=''):
    result = []
    for key, value in obj.iteritems():
        new_path = path + '.'  + key if path else key
        if isinstance(value, dict):
            result.extend(json_as_dotted_path(value, path=new_path))
        else:
            result.append(new_path)
    return result

json = {
    "root": {
        "Child": {
            "subChild": 10,
            "subChild2": 20
        },
        "Child2": {
            "subChild2": 20,
            "subChild3": 500
        }

    }
}

from pprint import pprint

pprint(json_as_dotted_path(json))

rawDescription: Traverse json object to print root to children path separeted by dot


code: backward_string = lowercase[::-1]

rawDescription: I need help finishing my palindrome checking python code


rawDescription: Python reference to regex in parentheses


code:
file_1 = open("file_1.txt")
file_1 = file_1.read()
file_1 = file_1.split("\n")

file_2 = open("file_2.txt")
file_2 = file_2.read()
file_2 = file_2.split("\n")

file_3 = open("file_3.txt")
file_3 = file_3.read()
file_3 = file_3.split("\n")

rawDescription: Merge 3 Textfiles with python

description: Merge 3 Textfiles with python


code:
def extract(source):
    for line in iter(source):
        _, start, end, _, _, _, _, _, _, text = line.strip().split(',', 9)
        yield start, end, text

rawDescription: Replacing and ading text in text files in Python


code:
s = 'PT1H28M26S'

hr, min = map(int, s.translate(None,"PTMS")[:-2].split("H"))
print(hr,min)
(1, 28)

rawDescription: How to parse a time

description: Parse a time


code: image = np.empty((1376,960), np.uint16)

rawDescription: read and display raw image using python

description: Read and display raw image using python


code: oldMac += "\n".join(stanza1)

rawDescription: Beginner python: Formatting Lines

description: Beginner python: Formatting Lines


code:
word = 'weke'
suggestions = list()
letters = [chr(x) for x in range(97,97+26)]
for i in range(len(word)):
    for a in letters: # or do this in a smarter way to iterate
        newword = word
        newword[i] = a
        if newword in corpus: suggestions.append(newword)

rawDescription: Trie Backtracking in Recursion

description: Trie Backtracking in Recursion


code:
aggrfn = lambda ts: 1 if 0 < ts.sum() else 0
df.pivot_table('item_quantity', 'projectid', 'vendor_name', aggrfn, 0)
df.pivot_table('item_quantity', 'projectid', 'project_resource_type', aggrfn, 0)
df.groupby('projectid')['item_quantity'].aggregate({'total_quantity':'sum'})

rawDescription: Pandas groupby apply how to speed up

description: Pandas groupby apply how to speed up


code:
def unoptimized(x):
    exec('pass')
    return cos(x)

dis.dis(unoptimized)

rawDescription: What is the difference between locals and globals when using Python's eval()?


code:
import numpy as np
means = np.arange(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
stddevs = np.ones(10) # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

samples = np.random.normal(means, stddevs)

rawDescription: Python: NxM array of samples drawn from NxM normal distributions


code: mygraph.bind(prefix, URIRef(url))

rawDescription: RDFLib: Namespace prefixes in XML serialization


rawDescription: pandas's resample with fill_method: Need to know data from which row was copied?

description: Pandas's resample with fill_method: Need to know data from which row was copied


code:
with open('text.txt', 'r') as f: #text file containing the URLS
    with open('anothertest.txt', 'a') as fout:
        for url in f:
            print(u"URL Line: {}".format(url.encode('utf-8')))

            # you might want to remove endlines and whitespaces from
            # around the URL, which what strip() does
            article = Article(url.strip())
            article.download()
            article.parse()

            # write/append to file
            fout.write(article.title)
            fout.write(article.text)

            print(u"Title: {}".format(article.title.encode('utf-8')))

            # print authors only if there are authors to show.
            if len(article.authors) == 0:
                print('No author!')
            else:
                for author in article.authors:
                    print(u"Author: {}".format(author.encode('utf-8')))

            print("Text of the article:")
            print(article.text.encode('utf-8'))

rawDescription: Python Read URLs from File and print to file


code:
for name, case in inspect.getmembers(sys.modules[__name__],
                                     lambda cls: issubclass(cls, unittest.TestCase)):
    real_tearDown = case.tearDown
    def tearDown(self):
        self.removeElements()
        real_tearDown(self)
    case.tearDown = real_tearDown

rawDescription: Calling a function after or before tearDown of each unit testcase


code:
imp_height_flt, imp_height_converted = 5.9, 1.8
print("You entered %.1f\" which converts into "
      "%.2fm." % (imp_height_flt, imp_height_converted))
print("You entered ", imp_height_flt, "\" which converts into "
      "%.2f" % imp_height_converted, "m.",
      sep='')
print('You entered {:.1f}" which converts into '
      '{:.2f}m.'.format(imp_height_flt, imp_height_converted))

rawDescription: How to eliminate white space after backslash (Python 3.4)

description: Eliminate white space after backslash (Python 3.4)


code:
def main_2(time_step=10, n_parent=4, max_offspring=3):
    U = 10
    sigma = 2.
    cov = [[sigma**2, 0], [0, sigma**2]]
    size = n_parent * max_offspring * time_step
    samples = np.random.multivariate_normal(np.zeros(2), cov, size)

    parents = np.random.rand(n_parent, 2) * U
    for _ in range(time_step):
        litter_size = np.random.randint(1, max_offspring+1, n_parent)
        n_offspring = litter_size.sum()
        parents = np.repeat(parents, litter_size, axis=0)
        offspring = (parents + samples[:n_offspring]) % U
        samples = samples[n_offspring:]
        parents = np.random.permutation(offspring)[:n_parent]

    return parents

rawDescription: Improving performance of iterative 2D Numpy array with multivariate random generator


code: [float(n)/othervar for n in range(100, 200)]

rawDescription: Integer division instead of regular one in Python


code:
from django import forms
from add_location.models import Location, Region

class LocationForm(forms.ModelForm):
    class Meta:
        model = Location

class RegionForm(forms.ModelForm):
    location = forms.ModelChoiceField(Location.objects.all(),
                                      widget=forms.HiddenInput())

    class Meta:
        model = Region

rawDescription: Django: Creating a forgeinkey in a form on the spot.


code:
import inspect

def foo(bar):
    frame = inspect.currentframe()
    outer = inspect.getouterframes(frame)[1][0]
    outer.f_locals[bar] = 2 * outer.f_locals[bar]

a = 15
foo("a")
print(a) # prints 30

rawDescription: Send by ref/by ptr in python?

description: Send by ref/by ptr in python


code: "Welcome {first_name} Im glad you could come back Mr.{last_name}"

rawDescription: Accessing View Data In Model

description: Accessing View Data In Model


code: idx_unique_objIDs = np.concatenate(([0], np.nonzero(np.diff(objID))[0] + 1))

rawDescription: Fast Numpy calculation of a slight variation of the repeat and choose functions


rawDescription: sorting list of tuples by arbitrary key

description: Sorting list of tuples by arbitrary key


code:
2.0 .is_integer()
2.5 .is_integer()

rawDescription: Determining if a Variable is Numerically an Integer in Python


code:
import numpy as np
from scipy import stats
from mayavi import mlab

mu=np.array([1,10,20])
# Let's change this so that the points won't all lie in a plane...
sigma=np.matrix([[20,10,10],
                 [10,25,1],
                 [10,1,50]])

data=np.random.multivariate_normal(mu,sigma,1000)
values = data.T

kde = stats.gaussian_kde(values)

# Create a regular 3D grid with 50 points in each dimension
xmin, ymin, zmin = data.min(axis=0)
xmax, ymax, zmax = data.max(axis=0)
xi, yi, zi = np.mgrid[xmin:xmax:50j, ymin:ymax:50j, zmin:zmax:50j]

# Evaluate the KDE on a regular grid...
coords = np.vstack([item.ravel() for item in [xi, yi, zi]])
density = kde(coords).reshape(xi.shape)

# Visualize the density estimate as isosurfaces
mlab.contour3d(xi, yi, zi, density, opacity=0.5)
mlab.axes()
mlab.show()

rawDescription: Multivariate kernel density estimation in Python


code:
banned_set = {"badword1", "badword2"}
banned_set.isdisjoint("foo bar".split())
banned_set.isdisjoint("foo bar badword1".split())

rawDescription: Primitive Python IRC Chat bot for twitch


code:
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    token = models.CharField(max_length=100, blank=True)

rawDescription: How can I set up token-based authentication for web services using Python?

description: Set up token-based authentication for web services using Python


code: df['end_time'] = df['time'] + df['duration'] * pd.offsets.Minute(1)

rawDescription: Resampling a time series of events + duration into concurrent events


code:
df1['value2'] = df2.reindex(pd.DatetimeIndex(df1.date1).normalize()).values
df1.head()

rawDescription: Joining dataframes with different datetime frequencies


code:
done = False
for x in xs:
    for y in ys:
        if bad:
            done = True
            break

    if done:
        break

rawDescription: python: exit out of two loops

description: Python: exit out of two loops


code:
my_list = ['a', 'a', 'b', 'c', 'd', 'a', 'e', 'd', 'f', 'e']

def sorted_set(a_list):
    return sorted(set(a_list))

new_list = sorted_set(my_list)

rawDescription: How do I remove identical items from a list and sort it in Python?

description: Remove identical items from a list and sort it in Python


code: ' '.join(my_string.split())

rawDescription: Is there a string-collapse library function in python?


rawDescription: Is there a simpler way to change 0xff01 into \\uff01?


code:
a = np.ones((4, 4))
a[bottom_right(4, 2)] = 0

rawDescription: Zero out matrix higher diagonal using numpy


code:
class Rated(object):
  def __init__(self, rating, freq_dist):
    self.rating = rating
    self.freq_dist = freq_dist

a = Rated(5, nltk.FreqDist('the boy sees the dog'.split()))
b = Rated(8, nltk.FreqDist('the cat sees the mouse'.split()))
c = Rated(-3, nltk.FreqDist('some boy likes nothing'.split()))

trainingTexts = [a,b,c]

rawDescription: Separating nltk.FreqDist words into two lists?


code:
immutyps = int, long, float, tuple, frozenset, str, unicode
mutables = [x for x in thelist if not isinstance(x, immutyps)]
if len(mutables) != len(set(id(x) for x in mutables)):
  cryhavocandletloosethedogsofwar()

rawDescription: Is there a way to inspect the (differing) internal structures of Python objects that test as equal (==)?


rawDescription: Expand certain substrings elegantly inside a larger string in python


code: test = [w[0] for w in dictSentCheck(sentCheck)]

rawDescription: Looking for dictionary words in text file using dictionary in python


code:
# Notice the signature is similar to the SCons Glob() signature,
# look at scons-2.1.0/engine/SCons/Node/FS.py line 1403
def RecursiveGlob(pattern, ondisk=True, source=True, strings=False):
    matches = []
    # Instead of using os.getcwd() consider passing-in a path
    for root, dirnames, filenames in os.walk(os.getcwd()):
        cwd = Dir(root)
        # Glob() returns a list, so using extend() instead of append()
        # The cwd param isnt documented, (look at the code) but its
        # how you tell SCons what directory to look in.
        matches.extend(Glob(pattern, ondisk, source, strings, cwd))

    return matches

rawDescription: Use a Glob() in VariantDir() einvironment to find files recursively in Python?


code:
from xml.etree import ElementTree as ET
import json

data = '''
<root><table>
  <row field1="a" field2="b" field3="c" field4="d"/>
  <row field1="1" field2="2" field3="3" field4="4"/>
</table></root>'''

filename = 'data.json'
f_out = open(filename, 'w')
for elem in ET.fromstring(data).findall('.//table/row'):
        field1 = elem.get('field1')
        field2 = elem.get('field2')
        field3 = elem.get('field3')
        field4 = elem.get('field4')
        l = {'field1' : field1,
             'field2' : field2,
             'field3' : field3,
             'field4' : field4}
        json.dump(l, f_out)
        f_out.write('\n')

f_out.close()

rawDescription: Writing JSON to file - wrong format?

description: Writing JSON to file - wrong format


code:
similarity = sum(
    doca_dic[k] * docb_dic[k]
    for k in set(doca_dic) & set(docb_dic)
    )

rawDescription: Document Similarity: Comparing two documents efficiently


code:
l = [1, 2, 3, 4,5]
reverseList(l)
l
l = [1, 2, 3, 4]
reverseList(l)
l

rawDescription: Using recursion to reverse a list in python?


rawDescription: Python regex, fetch names from a string

description: Python regex, fetch names from a string


code:
ax = subplot(111)
ax.yaxis.set_ticks([0, 2, 4, 8])
imshow(data)

rawDescription: How to plot an image with non-linear y-axis with Matplotlib using imshow?

description: Plot an image with non-linear y-axis with Matplotlib using imshow


code:
from numpy.linalg import eigh
a
vals, vecs = eigh(a)

rawDescription: finding the real eigenvectors of a real symmetric matrix in numpy or scipy

description: Finding the real eigenvectors of a real symmetric matrix in numpy or scipy


code:
import nltk.tokenize as tokenize
import nltk
import random
random.seed(3)

def bag_of_words(words):
    return dict([word, True] for word in words)

def document_features(document):
    features = {}
    for word in word_features:
        features[word] = (word in document)
        # features['contains(%s)' % word] = (word in document_words)
    return features

movie_reviews = nltk.corpus.movie_reviews

documents = [(set(movie_reviews.words(fileid)), category)
             for category in movie_reviews.categories()
             for fileid in movie_reviews.fileids(category)]
random.shuffle(documents)

all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = all_words.keys()[:2000]

train_set = [(document_features(d), c) for (d, c) in documents[:200]]

classifier = nltk.NaiveBayesClassifier.train(train_set)

classifier.show_most_informative_features()
for word in ('love', 'hate'):
    # No hope in passing the tests if word is not in word_features
    assert word in word_features
    print('probability {w!r} is positive: {p:.2%}'.format(
        w = word, p = classifier.prob_classify({word : True}).prob('pos')))

tests = ["i love this city",
         "i hate this city"]

for test in tests:
    words = tokenize.word_tokenize(test)
    feats = bag_of_words(words)
    print('{s} => {c}'.format(s = test, c = classifier.classify(feats)))

rawDescription: Naive Bayes Classifier error

description: Naive Bayes Classifier error


code:
import re
import os
def normalize_path(path):
    return os.path.normpath(os.sep.join(re.split(r'\\|/', path)))

rawDescription: Python native parsing of path names with unknown path separators


code: json.dumps({'foo': u'τί ἐστίν; 統一碼!!! ✢✪✺'})

rawDescription: YAML as failsafe file dump format (using pyyaml)?


code:
for i in listA:
    if i in listB:
        return True
return False

rawDescription: Correct way to do 'if list in list' python


code:
class Foo(object):
    @staticmethod
    def foo(bar, baz, qux):
        pass

rawDescription: Execute function without sending 'self' to it


code:
a = HtmlXPathSelector(text=s)
a
a.select("//a/@href")
a.select("//a/@href").extract()

rawDescription: XPath Only Selects Part of Href

description: XPath Only Selects Part of Href


code:
for lines in urls:
    d = feedparser.parse(lines)

rawDescription: Python: looping through urls in file

description: Python: looping through urls in file


code:
if not filestocopy:
    return

rawDescription: Is there a better way to feed different parameters into a function with if-statements?


code:
list_of_strings = ["hello", "line two"]
handle = StringIO.StringIO('\n'.join(list_of_strings))
handle.read()

rawDescription: in python; convert list of files to file like object

description: In python; convert list of files to file like object


code: [next(reader) for _ in range(3)]

rawDescription: How to parse of a simple csv file?

description: Parse of a simple csv file


code:
import re
ip_at_end = re.compile(r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', re.MULTILINE)
example = '''\
Only addresses on the end of a line match: 123.241.0.15
Anything else doesn't: 124.76.67.3, even other addresses.
Anything that is less than a dotted quad also fails, so 1.1.4
does not match but 1.2.3.4
will.
'''
ip_at_end.findall(example)

rawDescription: How to search a document for IP addresses

description: Search a document for IP addresses


code:
import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

year = [20070102,20070806,20091208,20111109,20120816,20140117,20140813]
yvalues = [-0.5,-0.5,-0.75,-0.75,-0.5,-1.25,-1.25]

dates = [DT.datetime.strptime(str(int(date)),'%Y%m%d') for date in year]

fig, axes = plt.subplots(1, 1, figsize=(16, 9), dpi=100)

line1, = axes.plot(dates,yvalues, lw=2, marker='*', color='r')
axes.legend([line1],['VALUES'],loc=1)
axes.grid()
axes.set_xlabel('YEAR')
axes.set_ylabel('VALUES')

yticks = [-2.0,-1.75,-1.5,-1.25,-1.0,-0.75,-0.5,-0.25,0.0]
axes.set_yticks(yticks)
axes.set_yticklabels(["$%.2f$" % y for y in yticks]);

xfmt = mdates.DateFormatter('%Y-%m-%d')
axes.xaxis.set_major_formatter(xfmt)
axes.set_xticks(dates)
plt.xticks(rotation=25)
plt.show()

rawDescription: How to set the xticklabels for date in matplotlib

description: Set the xticklabels for date in matplotlib


code:
field = Model._meta.get_field(field_name)
# and
yield base.DeserializedObject(Model(**data), m2m_data)

rawDescription: Convert property to django model field

description: Convert property to django model field


code: coordinate = min_coordinate + i*step

rawDescription: Nx3 column data to 2d matrix for image processing


code: tuple(map(tuple, sub_c))

rawDescription: How should I go about subsampling from a scipy.sparse.csr.csr_matrix and a list


code:
d = np.asarray(zip(a,b,c))

d.dtype = [("0", int), ("1", int), ("2", int)]
d.shape = d.size
d.sort(order="1")

rawDescription: How to get numpy array from multiple lists of same length and sort along an axis?

description: Get numpy array from multiple lists of same length and sort along an axis


code:
df.groupby('id').sum().reset_index()
# or
df.groupby('id', as_index=False).sum()   # @John_Gait

rawDescription: Sum up non-unique rows in DataFrame

description: Sum up non-unique rows in DataFrame


code: txt = open(filename)

rawDescription: Why the syntax for open() and .read() is different?


code:
def getNGramBeginsWords(nGram, length):
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in wlist:
        stripline = line.strip() # you keep doing this
        # you can merge these two ifs
        if len(nGram)>len(stripline) or len(stripline)!= length:
            continue
        if stripline.endswith(nGram):
            words.append(stripline)
    return words

rawDescription: Passing in Global Variables in Python

description: Passing in Global Variables in Python


code: data = {k: [i for i in v if i] for k, v in data.iteritems()}

rawDescription: How to remove empty entries from a list in a dictionary using python

description: Remove empty entries from a list in a dictionary using python


rawDescription: Initializing list of numpy arrays

description: Initializing list of numpy arrays


code:
d = { 'token': 10,'custid':100,'cycle':"yes" }
condition = ' and '.join([key+'="'+str(value)+'"' for key,value in d.iteritems() if key in allowed_col])
select_stmt = "SELECT * FROM kart_user WHERE"
select_stmt = " ".join([select_stmt, condition])
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.

rawDescription: Python string substitution for dynamic column name and values


rawDescription: Sorting a list of dates in Python

description: Sorting a list of dates in Python


code:
class Veggie(PrettyEnum):
    celery = 1
    tomato = 2
    broccoli = 3

rawDescription: Python 2.7, enum34 and user friendly unicode representation of enum properties


code:
0.865000009537 # Without Round
0.865          # With Round

rawDescription: measuring the time to load a page - Python

description: Measuring the time to load a page - Python


code:
import  re
r = re.compile(r"MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_")
with open("/proc/meminfo") as f:
    out =[]
    for line in f:
        if r.search(line):
            v = line.rsplit(None, 2)[1]
            out.append(v)
print("  ".join(out))

rawDescription: converting bash/shell line into python 2.6

description: Converting bash/shell line into python 2.6


code:
from gcloud import datastore

# the location of the JSON file on your local machine
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/location/client_secret.json"

# project ID from the Developers Console
projectID = "THE_ID_OF_YOUR_PROJECT"

os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
client = datastore.Client(dataset_id=projectID)

rawDescription: upload a simple json file to google storage from a simple python script

description: Upload a simple json file to google storage from a simple python script


rawDescription: Python Beatifulsoup tag name and attribute conflict


code:
import unicodedata

def voice(char):
    if not ('\u3041' <= char <= '\u3096') or len(char) > 1 or unicodedata.decomposition(char):
        return char
    return unicodedata.normalize('NFC', char + '\u3099')

def semivoice(char):
    if not ('\u3041' <= char <= '\u3096') or len(char) > 1 or unicodedata.decomposition(char):
        return char
    return unicodedata.normalize('NFC', char + '\u309a')

def devoice(char):
    if not ('\u3041' <= char <= '\u3096') or len(char) > 1 or not unicodedata.decomposition(char):
        return char
    return unicodedata.normalize('NFD', char)[0]

rawDescription: Is there any way to do "logical" transformations between Japanese kana characters with different diacritical marks?


rawDescription: How to find first byte of a serial stream with python?

description: Find first byte of a serial stream with python


code:
class EventEngine:
  def __init__(self, top_level_class):
      self.top = top_level_class
      #a var from Map_Class can be called as such:
      #self.top.MapEngine.map[0][1]
      #and I can call variables from every class, not only those I
      #have loaded like before

rawDescription: How to Link Multiple Classes Together Efficiently


code: [set(x).intersection(y) for x, y in zip(a, b)]

rawDescription: How to get intersection of each element of two nested lists in python?

description: Get intersection of each element of two nested lists in python


code:
class MethodTracker(object):
    def collect_data(self):
        return dict(tup for method in self.tracker for tup in method(self))

class Example(MethodTracker):
    tracker = []

    @track(tracker)
    def method_a(self):
        return [('key_a1', 'val_a1'), ('key_a2', 'val_a2')]

    @track(tracker)
    def method_b(self):
        return [('key_b1', 'val_b1'), ('key_b2', 'val_b2')]

rawDescription: Calling methods inside class to modify list


code: os.system('ssh -t -t myname@myserver "nice -11 screen -U"')

rawDescription: SSH and Screen with a Python script

description: SSH and Screen with a Python script


code:
class Chat:
    def __init__(self, user, date, text):
        self.user = user
        self.date = date
        self.text = text

rawDescription: python - parsing and sorting dates

description: Python - parsing and sorting dates


code:
lst = []
for i, j in enumerate(string.ascii_uppercase, 1):
    lst.append('{}: {}'.format(i, j))

listPrinter(lst,2)

rawDescription: Split long column into several columns and print them side by side


code:
# Python 2
[x**2 for x in range(10)] == map(lambda x: x**2, range(10))
[2**i for i in range(13)] == map(lambda x: 2**x, range(13))
S = [x**2 for x in range(10)]
[x for x in S if x % 2 == 0] == filter(lambda x: x % 2 == 0, S)

rawDescription: Are Python list comprehensions the same thing as map/grep in Perl?


code:
def OnButtonClick3(self):
    make_report(self.name1, self.name2, self.reportname)

rawDescription: tkinter - ask the user for files directory and then run a script

description: Tkinter - ask the user for files directory and then run a script


code:
def remove_fully_correct(answer, guess):
        """(list,list) -> list
        Return a list that removes the chars from the first list that are the same and in the same position in the second list
        >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d'])
        ['a','c']
        """
        return [ele for index,ele in enumerate(answer) if ele != guess[index]]

In [6]: remove_fully_correct(['a','b','c','d'], ['d','b','a','d'])
Out[6]: ['a', 'c']

rawDescription: How to get a function to execute until the conditions are not sufficient

description: Get a function to execute until the conditions are not sufficient


code:
import ast
def type_of_value(var):
    try:
       return type(ast.literal_eval(var))
    except Exception:
       return str

rawDescription: Testing if Python string variable holds number (int,float) or non-numeric str?


code:
def encrypt(data, shift):
    return ''.join([chr(((ord(c) - ord('a') + shift) % 26) + ord('a')) if ord(c) in range(ord('a'), ord('z')+1) else chr(((ord(c) - ord('A') + shift) % 26) + ord('A')) for c in data])

rawDescription: Python Simple Encryption program: How to loop Z back to A


rawDescription: Django -- Multiple Databases -- Data Migration


code:
replace = {'1': 'Bob', '2': 'Bob'}

user_made_array = [replace.get(item, item) for item in user_made_array]

rawDescription: How to compare the elements of a list to a 2-dimensional list (without using a huge amount of if/elif/else statements)

description: Compare the elements of a list to a 2-dimensional list (without using a huge amount of if/elif/else statements)


code: UserDetails[0] = usernamechange + "\n"   # assuming you break info by lines

rawDescription: Editing a string in an Array

description: Editing a string in an Array


code: formattedList = ["%.2f" % member for member in theList]

rawDescription: Format all elements of a list

description: Format all elements of a list


code:
def f(args):
    return get_sub_matrix_C(*args)

pool = multiprocessing.Pool()
results = pool.map(f, ((i, other_args) for i in range(10)))

for i, res in enumerate(results):
    C[i*10:(i+1)*10,:10] = res

rawDescription: Python multiprocessing and shared numpy array


code: chiptype = int(my_text.split('Reported chip type:', 1)[-1].split('\n')[0].strip())

rawDescription: How to mine Specified from text in python?

description: Mine Specified from text in python


code:
#!/usr/bin/env python3

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket

class Ws:

  @cherrypy.expose
  def a(self):
    '''WebSocket upgrade method.
    Method must exist for ``WebSocketTool`` to work, 404 returned otherwise.
    '''

  @cherrypy.expose
  def b(self):
    pass

class HandlerA(WebSocket):

  def received_message(self, message):
    self.send('"A" is my reply')

class HandlerB(WebSocket):

  def received_message(self, message):
    self.send('"B" is my reply')

class App:

  @cherrypy.expose
  def index(self):
    return '''<!DOCTYPE html>

    '''

if __name__ == '__main__':
  cherrypy.config.update({
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 8
  })

  cherrypy.tools.websocket = WebSocketTool()
  WebSocketPlugin(cherrypy.engine).subscribe()

  cherrypy.tree.mount(Ws(), '/websocket', {
    '/a' : {
      'tools.websocket.on'          : True,
      'tools.websocket.handler_cls' : HandlerA
    },
    '/b' : {
      'tools.websocket.on'          : True,
      'tools.websocket.handler_cls' : HandlerB
    }
  })

  cherrypy.tree.mount(App(), '/')

  cherrypy.engine.signals.subscribe()
  cherrypy.engine.start()
  cherrypy.engine.block()

rawDescription: Cherrypy + Autobahn websockets on same port


code:
entries = user_string.split('*')
if set(entries).issubset(viable_entries) and len(set(entries)) == len(entries):
     ...

rawDescription: How to check whether a string is a concatenation of other strings with a character inserted between each string in python

description: Check whether a string is a concatenation of other strings with a character inserted between each string in python


code:
class AddressBook(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

class AddressForm(forms.ModelForm):
    class Meta:
        model = AddressBook

def edit_address(request, pk=None):
    existing_entry = AddressBook.objects.get(pk=pk)
    form = AddressForm(instance=existing_entry)
    return render(request, 'edit.html', {'form': form})

rawDescription: Editing django forms using django bootstrap forms


code:
from django.core.urlresolvers import reverse
from myproject.myapp import views
reverse(views.myview)

rawDescription: How do I successfully pass a function reference to Django’s reverse() function?

description: Successfully pass a function reference to Django’s reverse() function


code:
data = []
with open(filename) as fin:
    header = next(fin, '').split()
    for line in fin:
        m = re.match(r'(\d+) lines.*(\b\w+)$', line)
        if m:
            data.append(list(islice(fin, int(m.group(1)))))

rawDescription: Python: split a large text file by many headers


code: targetdata = originaldata[mapping[..., 0], mapping[..., 1]]

rawDescription: mapping between arrays in numpy

description: Mapping between arrays in numpy


code:
@app.route('/add/<int_list:values>')
def add(values):
    return str(sum(values))

rawDescription: Capture a list of integers with a Flask route


code:
def weightedAvg(weightedTimeseries):
    sumA = np.sum((np.where(ts.mask, 0., ts.data * weight) for ts, weight in weightedTimeseries), axis=0)
    sumB = np.sum((np.where(ts.mask, 0., weight) for ts, weight in weightedTimeseries), axis=0)
    return np.divide(sumA, sumB)

weightedAvg(((a1, 0.3), (bb, 0.7)))
# array([ 3.1,  2. ,  5. ,  5.1])

rawDescription: How to aggregate timeseries in Python?

description: Aggregate timeseries in Python


code:
reslist = list(root.iter())
result = ' '.join([element.text for element in reslist])

rawDescription: Python: Get all text from an XML document?


code: {'price': 3.59, 'other': [u'A, List, Of, Terms, To, Extract - 1 Feb 2013'], 'rrp': 3.99}

rawDescription: How to extract tags from HTML using Beautifulsoup in Python

description: Extract tags from HTML using Beautifulsoup in Python


code:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Wnck', '3.0')
from gi.repository import Gtk, Wnck

class DialogExample(Gtk.Dialog):

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "My Dialog", parent, 0, #or Gtk.DialogFlags.MODAL
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_default_size(100, 100)

        label = Gtk.Label("This is a dialog to display additional information")

        box = self.get_content_area()
        box.add(label)
        self.show_all()

class DialogWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Dialog Example")
        self.set_border_width(6)
        button = Gtk.Button("Open dialog")
        button.connect("clicked", self.on_button_clicked)
        self.add(button)

    def on_button_clicked(self, widget):
        dialog = DialogExample(self)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            print("The OK button was clicked")
        elif response == Gtk.ResponseType.CANCEL:
            print("The Cancel button was clicked")

        screen = Wnck.Screen.get_default()
        screen.force_update()  # recommended per Wnck documentation
        window_list = screen.get_windows()

        for window in window_list:
            print(window.get_name())

        window, window_list = (None,)*2
        screen = None

        dialog.destroy()

win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()

window = None
screen = None
Gtk.main()

rawDescription: How to test if GTK+ dialog has been created?

description: Test if GTK+ dialog has been created


code:
doc.xpath('//span[@class="graytext" and @style="font-size: 11px"]')
doc.xpath('//span[@class="graytext" and @style="font-size: 11px"]/text()')
first(doc.xpath('//span[@class="graytext" and @style="font-size: 11px"]/text()'),'').strip()

rawDescription: Parse the date string from html in lxml

description: Parse the date string from html in lxml


code:
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self._rcwin = None

    def showRigControlWindow(self):
        if self._rcwin is None:
            self._rcwin = RigControlWindow()
        self._rcwin.show()

rawDescription: PyQt4 : check the window is existing or not


code:
entry_points={
    'console_scripts': [
        'foobar = foobar:main'
    ],
},

rawDescription: module as script without .py in Python?

description: Module as script without .py in Python


rawDescription: Checking a List for a Sequence

description: Checking a List for a Sequence


code: result = re.findall(r'\w+(?=:?/)',data)

rawDescription: Regular expression to extract words before a slash


code:
import concurrent.futures
import functools
import itertools
import operator

def process_stuff(i, j, img, winSize):
    Process1 = Do_something(img[i: i + winSize[0], j: j + winSize[1]])
    Process2 = Do_something(Process1)

with concurrent.futures.ProcessPoolExecutor() as executor:
    i_iterator = arange(0, shape(img)[0] - window_size[0], 10)
    j_iterator = arange(0, shape(img)[1] - window_size[1], 10)
    product = itertools.product(i_iterator, j_iterator)
    iter1, iter2 = itertools.tee(product)
    i_iterator = map(operator.itemgetter(0), iter1)
    j_iterator = map(operator.itemgetter(1), iter2)

    do_process = functools.partial(process_stuff, img=img, winSize=winSize)
    executor.map(do_process, i_iterator, j_iterator)

rawDescription: How can I parallelize this Python for-loop?

description: Parallelize this Python for-loop


rawDescription: Finding the top 10 and converting it from centimeters to inches - Python


rawDescription: initialize defaultdict in Python

description: Initialize defaultdict in Python


rawDescription: Time tuple to a datetime

description: Time tuple to a datetime


code:
def checker(s,val):
   for k in s:
       if k==val:
          return False
       elif isinstance(s[k],dict):
          return checker(s[k],val)
   return True

s="""{"httpStatus": "OK", "payload": {"status": "OK"}, "httpCode": 200}"""
js=json.loads(s)
checker(js,'config')
s="""{"httpStatus": "OK", "payload": {"status": "OK", "config": {}}, "httpCode": 200}"""
js=json.loads(s)
checker(js,'config')

rawDescription: Regex substring not in string

description: Regex substring not in string


code:
def func2():
    import tkinter as tk

    root2 = tk.Tk()

    def kill2():
        root2.destroy()
        from py1 import func1
        func1()

    button2 = tk.Button(root2, bg = 'red', text = 'hit to kill py2 and start py1', command = kill2)
    button2.pack()

    root2.mainloop()

if __name__ == '__main__':
    func2()

rawDescription: Python3 Tkinter - Open .py file with button


rawDescription: How to parse empty value of parameter in HTTP request in python?

description: Parse empty value of parameter in HTTP request in python


rawDescription: Handling tuples as values within a dictionary (in list comprehensions)


code:
from django.conf import settings
from django.template import RequestContext

def index(request):
        return render_to_response('index.html', {'settings': settings},
        context_instance=RequestContext(request))

rawDescription: Django: how to use settings in templates?


code: out[masks] = vals

rawDescription: "Unsparsing" numpy Arrays with Given Masks


code:
import pandas as pd
import numpy as np
def search(df, *words):  #1
    """
    Return a sub-DataFrame of those rows whose Name column match all the words.
    """
    return df[np.logical_and.reduce([df['Name'].str.contains(word) for word in words])]   # 2

df = pd.DataFrame({'Name':['Virginia Google Governor',
                           'Governor Virginia',
                           'Governor Virginia Google']})
print(search(df, 'Governor', 'Virginia', 'Google'))

rawDescription: Searching Multiple Strings in pandas without predefining number of strings to use


code:
python --version
#Python 2.7.10

rawDescription: How can I determine python versions and installation date on windows

description: Determine python versions and installation date on windows


code:
from __future__ import print_function

print("\r Running... xxx", end="")  # works across both major versions

rawDescription: Print on same line at the first in python 2 and 3


code: ssqlContext = sqlContext._ssql_ctx

rawDescription: How to use a Scala class inside Pyspark

description: Use a Scala class inside Pyspark


rawDescription: iterate the range in for loop to satisfy the condition

description: Iterate the range in for loop to satisfy the condition


code:
timex = re.compile(r'(\d{{1,2}}:\d{{2}})\s*({foo})'.format(foo='|'.join(foo)))
timex.findall(bar)

rawDescription: Python 2.7 regex with format multiple list items


code:
s="""War and Peace

by Leo Tolstoy/Tolstoi"""

re.findall(r'\S+|\n',s)

rawDescription: Python splitting text file keeping newlines


code:
odds = iter(data[2])
odds_merged = zip(odds, odds, odds)
data_merged = zip(data[0], data[1], odds_merged)
len(data_merged)

rawDescription: Scrapy hxs.select() not selecting all results


code:
df = pd.DataFrame({'col':['a',0]})
df
df['col'] = 'str' + df['col'].astype(str)
df

rawDescription: Append string to the start of each value in a said column of a pandas dataframe (elegantly)


rawDescription: Get country code for timezone using pytz?


rawDescription: Python 3.3: Sorting a List of Objects/Methods


code:
df
d2 = df["favoured_instrument"].str.split("\s*,\s*", expand=True)
d2

rawDescription: Finding unique elements in cells of pandas DF and expanding DF to include columns with the names of those unique elements


code: std

rawDescription: Numpy - Matrix Generation from list comprehension


code:
a_loaded = loaded['a']
b_loaded = loaded['b']

rawDescription: Storing and loading numpy arrays as files


code:
class Demo:
  def __repr__(self):
    return '(repr called)'
  def __str__(self):
    return '(str called)'

d = Demo()
repr(d)
str(d)
'%r %s' % (d, d)

rawDescription: Tried to print letter of hindi and strange output


rawDescription: Python - Append to list nested in dict subclass


rawDescription: Finding the row indices of zero elements in csc_matrix


code:
def is_a_match(dirparent, dirname):
    return os.path.basename(dirparent) == "test" and \
    dirname.startswith("vvl") and dirname.endswith("cpp")

rawDescription: Python code to search a folder(not file) inside another folder


code: return cmp(ayy, byy) or cmp(amm, bmm) or cmp(add, bdd)

rawDescription: Simplify a date comparison

description: Simplify a date comparison


code:
df2 = df[df.value <= 5000]
df2.hist(bins=np.linspace(0,5000,101))
plt.savefig('hist1')

rawDescription: Plotting a histogram in Pandas with very heavy-tailed data


code: re.findall(r'(?:(?<=^)|(?<=\s))(@[^\s]+)', '@one bla bla bla @two @three@four #@five')

rawDescription: python regex with conditional lookbehind

description: Python regex with conditional lookbehind


code: import glob

rawDescription: Create classpath from existing jars in a Folder


code:
interlace_markers = existing_markers + new_markers
interlace_markers.sort(key = lambda x: x.marker_number)

rawDescription: Interlacing two lists of objects based on attributes of the list objects in python


code:
with open('data.txt') as fin, open('dataFinal.txt', 'w') as fout:
    fout.writelines(line.partition(':')[0] + '\n' for line in fin)

rawDescription: How to find and replace/remove text after a specified delims with Python?

description: Find and replace/remove text after a specified delims with Python


rawDescription: re.sub not working for a particular regex pattern

description: Re.sub not working for a particular regex pattern


code:
def out(field):
    b = []
    for columns in reversed(list(zip(*field))):
        b.append(''.join(map(str, columns)))
    return '\n'.join(b)

rawDescription: Python, slicing lists?

description: Python, slicing lists


code:
df = pd.DataFrame(np.random.randint(0,100,size=(10, 3)), columns=list('abc'))
df
c = df[['a','b']].stack().value_counts()
c
c[c>1]
vals = c[c>1].index
df[(df['a'].isin(vals)) | (df['b'].isin(vals))]

rawDescription: Remove Rows from pandas.Dataframe Based on Entries


code:
result = []
for elem in inputlist:
    elems = elem.split(', ')
    elems[4:] = [tuple(elems[4:])]
    result.append(tuple(elems))

rawDescription: How can I format a list into a tuple with sub-tuples?

description: Format a list into a tuple with sub-tuples


rawDescription: sort an array by row in pyhton

description: Sort an array by row in pyhton


code: result = [i for i in persons if all(j in permitted for j in i[-1])]

rawDescription: Python 2.7 - return list from list of lists where sub-list item contains certain character


code: func3 = func1(3)

rawDescription: What's a variable in a inner function definition in python?


code:
NP.nan_to_num(A)
MA.masked_equal(A, 0)

rawDescription: Performing operations on a NumPy arrray but masking  values along the diagonal from these operations


code:
with open('date.txt', 'rw') as f:
    lines = f.readlines()
    for i, line in enumerate(lines):
        if line.startswith('February'):
            line[i] = line[i].strip() + '2012\n'
    f.seek(0)
    for line in lines:
        f.write(line)

rawDescription: appending a data in a specific line of a text file in Python?

description: Appending a data in a specific line of a text file in Python


code: d = {itm:L.count(itm) for itm in set(L)}

rawDescription: How to count elements in a list and return a dict?

description: Count elements in a list and return a dict


code: data = json.loads(serializers.serialize('json', products))

rawDescription: Add Property to Django Result Object

description: Add Property to Django Result Object


code:
phonePattern = re.compile("\d?(\(?\d{3}\D{0,3}\d{3}\D{0,3}\d{4})")
result = phonePattern.sub(lambda m: re.sub('\d', 'X', m.group(1)), text)

rawDescription: How to replace phone number with Xs?

description: Replace phone number with Xs


code: users = relationship(..., query_class=MyOtherQuery)

rawDescription: SQLAlchemy - can you add custom methods to the query object?


code:
def print1by1(text, delay=0.1):
    for c in text:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(delay)
    print

print1by1("Hello there!")

rawDescription: How to make Python print one character at a time on the same line?

description: Make Python print one character at a time on the same line


code: self.button1_rex = tk.Button(..., command=lambda r=resNum: self.new_window(r))

rawDescription: How to determine which button is pressed out of Button grid in Python TKinter?

description: Determine which button is pressed out of Button grid in Python TKinter


code:
def f(x):
    x = 2

x = 1
f(x)
print(x)

rawDescription: Run while loop one extra time after condition is met


code:
a
your_code(a)
np.maximum.accumulate(a)

rawDescription: Compare element with previous along axis


rawDescription: Slice 2d array into smaller 2d arrays

description: Slice 2d array into smaller 2d arrays


code:
rng = pd.date_range('2013', freq='D', periods=10)
df = pd.DataFrame(np.random.randn(10), rng)
df.index.week
df.groupby(df.index.week).sum()

rawDescription: Pandas Dataframe splitted into weeks

description: Pandas Dataframe splitted into weeks


code:
ax.scatter(d1, 1, 200, marker=align_marker('>', halign='left'),
           clip_on=False, color='k', transform=ax.get_xaxis_transform())
ax.scatter(d2, 1, 200, marker=align_marker('<', halign='right'),
           clip_on=False, color='k', transform=ax.get_xaxis_transform())

rawDescription: Align matplotlib scatter marker left and or right


code:
import os
import colorsys
import Image

def hueChange(img, hue):
    # It's better to raise an exception than silently return None if img is not
    # an Image.
    img.load()
    r, g, b = img.split()
    r_data = []
    g_data = []
    b_data = []

    for rd, gr, bl in zip(r.getdata(), g.getdata(), b.getdata()):
        h, s, v = colorsys.rgb_to_hsv(rd / 255., bl / 255., gr / 255.)
        rgb = colorsys.hsv_to_rgb(hue/360., s, v)
        rd, gr, bl = [int(x*255.) for x in rgb]
        r_data.append(rd)
        g_data.append(gr)
        b_data.append(bl)

    r.putdata(r_data)
    g.putdata(g_data)
    b.putdata(b_data)
    return Image.merge('RGB',(r,g,b))

filename = 'image.png'
basename, ext = os.path.splitext(filename)
img = Image.open(filename).convert('RGB')
for hue in range(1, 360, 30):
    img2 = hueChange(img, hue)
    out_name = '{}_hue{:03d}.jpg'.format(basename, hue)
    img2.save(out_name)

rawDescription: RGB to HSV Python, change Hue continuously


code:
from scandir import walk
import curses
from os import path

try:
    stdscr = curses.initscr()
    curses.noecho()
    height, width = stdscr.getmaxyx()
    win = curses.newpad(height, 16383)
    for root, dirs, files in walk("/Users"):
        for file in files:
            file_path = path.join(root, file)
            win.addstr(0, 0, "Scanning: {0}".format(file_path))
            win.clrtoeol()
            win.clearok(1)
            win.refresh(0, 0, 0, 0, height-1, width-1)
finally:
    curses.endwin()

rawDescription: How do I deal with lines that are longer than the Curses window in Python?

description: Deal with lines that are longer than the Curses window in Python


code: pd.to_timedelta(df['time'], unit='s')

rawDescription: How to generate time series from a series of ordered numbers in Python's pandas

description: Generate time series from a series of ordered numbers in Python's pandas


code:
jsStr = """var doc = this;
           return ['result1','result2','result3']
           .every(function(key) {
               return doc.hasOwnProperty(key)
           });"""

coll.find({ "$where": jsStr })

rawDescription: Check for existence of multiple fields in MongoDB document


code:
# order the homes by distance
# pair up the homes by position with the home_numbers by distance

final_dict = {}
for road_id in road_dict:
    homes_on_road = [home for home in homes if home['road_id'] == road_id]
    ordered_homes = sorted(homes_on_road, key=lambda h: h['distance'])
    ordered_nums = [home['home_number'] for home in ordered_homes]

    for home, new_number in zip(homes_on_road, ordered_nums):
        final_dict[home['home_id']] = {1:new_number}

pprint(final_dict)

rawDescription: Sorting and re-numbering values in Python dictionary


code:
while flag != True:  # flag is never True, by the way
    if letter == "T":   # this is always True
        ...   # this will be parsed over and over again since it does not break

rawDescription: Python while loop logic

description: Python while loop logic


code:
from ast import literal_eval
with open('testfile') as f:
    dicts = [literal_eval(line) for line in list(f)]
    result = {'Zips':[d['Zip'] for d in dicts]}
    result.update(dicts[0])
    del result['Zip']
    del dicts

result

rawDescription: Collapse dictionary on key

description: Collapse dictionary on key


code:
import difflib, sys

tl=100000    # large number of lines

# create two test files (Unix directories...)

with open('/tmp/f1.txt','w') as f:
    for x in range(tl):
        f.write('line {}\n'.format(x))

with open('/tmp/f2.txt','w') as f:
    for x in range(tl+10):   # add 10 lines
        if x in (500,505,1000,tl-2):
            continue         # skip these lines
        f.write('line {}\n'.format(x))

with open('/tmp/f1.txt','r') as f1, open('/tmp/f2.txt','r') as f2:
    diff = difflib.ndiff(f1.readlines(),f2.readlines())
    for line in diff:
        if line.startswith('-'):
            sys.stdout.write(line)
        elif line.startswith('+'):
            sys.stdout.write('\t\t'+line)

rawDescription: Returning lines that differ between two files (Python)


rawDescription: django model for list of objects' relations to each other?

description: Django model for list of objects' relations to each other


code:
import weakref

class CallbackWrapper(object):
    def __init__(self, sender, callback):
        self.weak_obj = weakref.ref(callback.im_self)
        self.weak_fun = weakref.ref(callback.im_func)
        self.sender = sender
        self.handle = None

    def __call__(self, *things):
        obj = self.weak_obj()
        fun = self.weak_fun()
        if obj is not None and fun is not None:
            return fun(obj, *things)
        elif self.handle is not None:
            self.sender.disconnect(self.handle)
            self.handle = None
            self.sender = None

def weak_connect(sender, signal, callback):
    wrapper = CallbackWrapper(sender, callback)
    wrapper.handle = sender.connect(signal, wrapper)
    return wrapper

rawDescription: How to connect to a GObject signal in python, without it keeping a reference to the connecter?

description: Connect to a GObject signal in python, without it keeping a reference to the connecter


code:
import pyopencl as cl
import pyopencl.array
import numpy as np

platform = cl.get_platforms()[2]
print(platform)
devices = platform.get_devices()[0] #gpu
context = cl.Context(devices=[devices])
queue =  cl.CommandQueue(context)

kernel = cl.Program(context, """
    __kernel void derp(global char* a) {
        a[get_global_id(0)] = 1;
    }""").build()

buffarr = cl.array.zeros(queue, 4, dtype=np.uint8)

# (4, 1) ==> shape of the buffer
kernel.derp(queue, (4, 1), None, buffarr.data, global_offset=[2, 0])

np_data = buffarr.get()
print(np_data)
# within this demo the buffer contains currently [1,1,0,0]
assert np.array_equal(np_data, [0,0,1,1])
print("Ok")

rawDescription: pyopencl global_work_offset kernel argument

description: Pyopencl global_work_offset kernel argument


code:
NewCounter1 = counter()
NewCounter2 = counter()
NewCounter2.__class__.count = 3

rawDescription: What in the world is the attribute "__class__" in python


code:
with open(file, "r") as f:
    for line in f:
        line = line.rstrip()

rawDescription: Selecting item in a string from a starting point retrieved via regex (Python)


rawDescription: Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists


code:
header = '<title>Sample</title>'
body = '
Hello $name
'
footer = ''
html = Template(header+body+footer)
html.substitute(name='World')

rawDescription: How to pass a variable to html in a mail script?

description: Pass a variable to html in a mail script


code:
parser.add_option("-v", help="verbose on", dest="verbose", action="store_true")
parser.add_option("-q", help="verbose off", dest="verbose", action="store_false")

rawDescription: optparse(): Input validation

description: Optparse(): Input validation


code:
from scrapy.item import Item, Field

class DemoItem(Item):
    page = Field()
    urls = Field()
    pageurl = Field()
    title = Field()

rawDescription: scrapy crawl spider ajax pagination

description: Scrapy crawl spider ajax pagination


code:
rev_aliases = dict((nick, name)
                   for name, nick_list in aliases.iteritems()
                   for nick in nick_list)

rawDescription: Swapping keys and values with nested value items


code:
employee = Employee.objects.get(name='John Doe')
employee_expenses = employee.expenses.all()

rawDescription: django model foreignkeys question

description: Django model foreignkeys question


code: creación, longstring, lación, Regula, estado, misión

rawDescription: Encode Python list in order to use re module


code: print("Doubles- {0:d}-{1:0.6f}%".format(number_of_double_dice,       number_of_double_dice/count*100))

rawDescription: Rolling two dice and tabulating outcomes


code: stock_averages[:] = ("%.2f" % avg if abs(avg)>=rounding_point else ' '  for avg in stock_averages)

rawDescription: Output issue calculating moving average

description: Output issue calculating moving average


code: colors_at_breaks(gradient2n, breaks)

rawDescription: How to determine colors at intervals in a matplotlib colormap

description: Determine colors at intervals in a matplotlib colormap


code: min(data, key = lambda t: t[1])

rawDescription: tuple pairs, finding minimum, python

description: Tuple pairs, finding minimum, python


code: threads_dict[i] = MyThread(target=print_test, args=[i,])

rawDescription: how to automatically release a semaphore in a thread after run() is executed?

description: Automatically release a semaphore in a thread after run() is executed


code:
downlinestack = []
distributor=self
downlinestack += distributor.downline
while downlinestack:
    downline = downlinestack.pop()
    for person in downline:
        downlinestack.append(person.downline)

rawDescription: MLM downline distribution count

description: MLM downline distribution count


code:
@patch('mylib.requests.post', side_effect=mocked_post)
class MyTest(TestCase):

    def test_foo(self, post_mock):
        # Some test logic

        self.assertEqual(post_mock.call_count, 3)

rawDescription: How to get the call count using Mock @patch?

description: Get the call count using Mock @patch


code:
def ret_Nothing():
    return

def ret_None():
    return None

def ret_0():
    return 0

ret_Nothing() == None
ret_None() == None
ret_0() == None
# and...
repr(ret_Nothing())

rawDescription: Why would a function end with "return 0" instead of "return" in python?


code:
def get_server_list():
    return Resources.objects.all().values_list('server_id',
                                               'server__public_name')

class ProjectNewForm(forms.Form):
    project_name = forms.CharField(required=True,
                                   min_length=2,
                                   max_length=14,
                                   label=_(u'Project name'))
    project_description = forms.CharField(required=True,
                                          max_length=2000,
                                          widget=forms.Textarea(
                                              attrs={'rows': 4}
                                          ),
                                          label=_(u'Project description'))
    server = forms.Field(required=True,
                         widget=forms.Select,
                         label=_(u'Server'))

    class Meta:
        fields = [
            'project_name',
            'project_description',
            'server',
        ]

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('label_suffix', '')
        super(ProjectNewForm, self).__init__(*args, **kwargs)
        self.fields['server'] = \
            forms.ChoiceField(choices=
                              [(o[0], str(o[1])) for o in get_server_list()],
                              initial=1
                              )

rawDescription: How to join two tables by keys in django queryset

description: Join two tables by keys in django queryset


code:
d = {'abc':'AGCTAC', 'def': 'AGGTAC', 'ghi':'AGGTAG'}
zip(*d.values())

rawDescription: Parse positions of dictionary values into a list


code:
import os

usernames = []

for l in os.listdir('/path/to/directory/with/usernames/'):
    usernames.append(l)

usernames = set(usernames)

f = open('/path/to/6 million','r')
resultlist = []
for l in f:
    l = l.split(' ')
    if (l[1] in usernames):
        resultlist.append(l[1])
f.close()

common=open('/path/to/filf','w')
common.write('\n'.join(resultlist) + '\n')
common.close()

rawDescription: Comparing 40 million lines in a file with 6 million list items in Python


code:
# Create Text with glyph_path
surface.context.save()
font = surface.context.get_scaled_font()
glyphs, clusters, is_backwards = font.text_to_glyphs(
    0, -5, text_line_letters, with_clusters=True)
surface.context.glyph_path(glyphs)
surface.context.restore()

rawDescription: Digital cursor movement recorded in PDF creation visible in CorelDraw


code:
for word in re.finditer(r'(\w+)', sentence):
    ...

rawDescription: Optimizing the search of a string in a phrases. Don't know which Python structures I need to use


code:
def keyfunc(item):
    if item[-1].isalpha():
         return item[:-1]
    return item

rawDescription: Selecting items matching a specific condition from a list


code: linedefn = OneOrMore(value)

rawDescription: pyparsing whitespace match issues

description: Pyparsing whitespace match issues


code:
while True:
    form = input('If you would like to view form 1 please enter "1".If you would like to view form 2 please enter "2" and If you would like to view form 3 please enter "3"')
    if form not in {"1","2","3"}:
        print("Invalid choice")
    else:
        break

rawDescription: In my code i am getting a name error saying the name 'sort' is not defined


code:
j is 10
k is 0
j is 9
k is 1
j is 8
k is 2
j is 7
k is 3
j is 6
k is 4
j is 5
k is 5
j is 4
k is 6
j is 3
k is 7
j is 2
k is 8
j is 1
k is 9

rawDescription: Add a member variable / method to a Python generator?


code:
df = df < 0.05
df.astype(int)

rawDescription: If test on Pandas DataFrame

description: If test on Pandas DataFrame


rawDescription: Matplotlib: data not showing on the two sides in heatmap using x, y, z values stored in lists


code:
class Student(User):
    cell_phone = models.CharField(max_length=14)

    class Meta:
        abstract = True

class EmployeeStudent(models.Model):
    user = models.OneToOneField(Student)
    extension = models.CharField(max_length=4, null=True, blank=True, default=None)

class CustomerStudent(models.Model):
    first_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)
    email = models.CharField(max_length=25)

rawDescription: Using two database tables for one control in Django Admin


code:
for obj in complexobjects:
    listbox.insert(END, str(complexobjects))

rawDescription: Objects from Tkinter controls: how to go beyond StringVar, IntVar, DoubleVar, BooleanVar?


rawDescription: Numpy: find the the non-intersecting values of two arrays


code:
def find_all_paths(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return [path]
    if not graph.has_key(start):
        return []
    paths = []
    for node in graph[start]:
        if node not in path:
            newpaths = find_all_paths(graph, node, end, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths

rawDescription: Finding reachable vertices of a vertice in a graph


rawDescription: python - xPath syntax for second occurence

description: Python - xPath syntax for second occurence


code:
def get_class(class_path):
    module_path, class_name = class_path.rsplit(".", 1)

    try:
        module = __import__(module_path, fromlist=[class_name])
    except ImportError:
        raise ValueError("Module '%s' could not be imported" % (module_path,))

    try:
        cls = getattr(module, class_name)
    except AttributeError:
        raise ValueError("Module '%s' has no class '%s'" % (module_path, class_name,))

    return cls

rawDescription: Dynamically instantiate object of the python class similar to PHP new $classname?


code:
import random
number_of_rows = 100
number_of_sample_rows = 10
sorted(random.sample(range(number_of_rows), number_of_sample_rows))

rawDescription: Concatenate large files in sql-like way with limited RAM


rawDescription: Python regex - conditional matching?

description: Python regex - conditional matching


code:
att = random.randint(5,15)
monster.health -= att

rawDescription: random.randint function isnt working the way i would like it to

description: Random.randint function isnt working the way i would like it to


code:
i = 0
while i < len(myList):
    if(myList[i].count(None) >= 4):
        del myList[i]
    else:
        i += 1

rawDescription: Python: How to remove a list containing Nones from a list of lists?


code:
for raceid, pos1, pos2, pos3, value in csv.reader(f):
    oddses[raceid][int(pos1), int(pos2), int(pos3)] = value

rawDescription: Reformat data (permutations) in Python

description: Reformat data (permutations) in Python


rawDescription: I need to linearly interpolate a numpy array of dimension nx3x3


code:
import pandas as pd
data = pd.DataFrame({
    'device_id': pd.np.random.randint(0, 100, 150000),
    'datetime': pd.Series(pd.np.random.randint(1429449000, 1429649000, 150000) * 1E9).astype('datetime64[ns]')
}).sort('datetime')
data.head()

rawDescription: Fastest way to calculate average of datetime rows using pandas


code:
from math import ceil

Rfoobar = ceil(foobar * 100) / 100

rawDescription: How to round decimal digits always up

description: Round decimal digits always up


code:
def isitIn(char, aStr):
    if aStr == '':  # Check for empty string
        return False
    m = aStr[len(aStr) // 2]
    if char == m:
       return True
    elif len(aStr) == 1:
        return False
    else:
       if char < m:
           return isitIn(char, aStr[:len(aStr) // 2])
       elif char > m:
           return isitIn(char, aStr[len(aStr) // 2:])
    return isitIn(char, aStr)

rawDescription: Using a recursive bisection algorithm to check if character is in string


code:
width = 0.01
integral = integrate(f, 0, math.pi, width)
print(integral)

rawDescription: How to numerically integrate f(x) between a lower and an upper bound?

description: Numerically integrate f(x) between a lower and an upper bound


code:
ip = (getenv("HTTP_CLIENT_IP") or
getenv("HTTP_X_FORWARDED_FOR") or
getenv("REMOTE_ADDR") or
"UNKNOWN")

rawDescription: Download counter: cgi FieldStorage reset pressed button values


code:
keys = sorted(var.keys())
for ltrs in zip(*(var[k] for k in keys)):
    print(", ".join(ltrs))

rawDescription: Pythonic iteration over multiple lists in parallel


code:
s = "keep, ignore, keep_this_too, ignore, keep_this_also, ignore"
re.findall(r'([^,\s]+)', s)
re.findall(r'([^,\s]+)', s)[::2] # using slice to get every other matches.

rawDescription: Python Regex match every other word

description: Python Regex match every other word


code:
questions={'What is 3+3':['6','8'], 'What is the capital of Alaska': ['Juno','Anchorage']}
QUIZ(questions)

rawDescription: Python Tkinter: How do you create a toplevel window and destroy the previous window?


rawDescription: Get max length of multi-dimension tuple

description: Get max length of multi-dimension tuple


code:
from copy import copy, deepcopy
def calc(i, j, mat):
    if i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]):
        return 0 # out of borders
    elif mat[i][j] == 1:
        return 0 # this cell has already been visited
    elif i == len(mat)-1 and j == len(mat[0])-1:
        return 1 # reached destination (last cell)
    else:
        mat[i][j] = 1 # mark as visited
        # create copies of the matrix for the recursion calls
        m1 = deepcopy(mat)
        m2 = deepcopy(mat)
        m3 = deepcopy(mat)
        m4 = deepcopy(mat)
        # return the sum of results of the calls to the cells:
        # down + up + right + left
        return calc(i+1, j, m1) + calc(i-1, j, m2) + calc(i, j+1, m3) + calc(i, j-1, m4)

def do_the_robot_thing(m, n):
    # an un-visited cell will be marked with "0"
    mat = [[0]*n for x in xrange(m)]
    return calc(0, 0, mat)

print(do_the_robot_thing(3, 3))

rawDescription: Count the number of possible paths through a grid


rawDescription: Pythonistas, how do I move data from top to bottom to left to right in MySQL (think multiple values for each ID)?


code:
import dis
dis.dis(foo)

rawDescription: Python instance of class defined inside a method is still able to use local variables inside the method when called outside the method


code:
import re
value = "url: 'abs52fs', uid: '1fg23s4', type: 'fgh54e'"
url = re.search(r"url:\s'(.*?)'", value)
url.group(1)
uid = re.search(r"uid:\s'(.*?)'", value)
uid.group(1)
type = re.search(r"type:\s'(.*?)'", value)
type.group(1)

rawDescription: Write a Regular Expression in Python to match substring


code:
class MyBase(object):
   _x = 3
   x = property( lambda s: s._x, doc="Documentation for property x")

class MyObj(MyBase):
   _x = 0

rawDescription: Docstring inheritance for properties using sphinx's autodoc


code:
import lxml.etree as ET
xmlDataFileNameNew = 'ToXMLData.xml'
xmlDataFileHandler = open(xmlDataFileNameNew,'r+')
xmlstring = xmlDataFileHandler.read()
root = ET.fromstring(xmlstring)
print(ET.tostring(root, pretty_print=True))

rawDescription: lxml etree.fromstring not creating correct XML doc

description: Lxml etree.fromstring not creating correct XML doc


code:
import logging
import types

class ExtendedLogRecord(logging.LogRecord):

    def getMessage(self):
        """
        Return the message for this LogRecord.

        Return the message for this LogRecord after merging any user-supplied
        arguments with the message.
        """
        if not hasattr(types, "UnicodeType"): #if no unicode support...
            msg = str(self.msg)
        else:
            try:
                msg = str(self.msg)
            except UnicodeError:
                msg = self.msg      #Defer encoding till later
        if self.args:
            msg +=' '+' '.join(map(str,self.args))
        return msg

#Patch the logging default logging class
logging.RootLogger.makeRecord=lambda self,*args: ExtendedLogRecord(*args)

some_dict={'foo':14,'bar':15}
logging.error('text',15,'some dictionary',some_dict)

rawDescription: Why does python logging package not support printing variable length args?


code:
digits = '1234567ABCDEFG'
digit_map = {d:i for i, d in enumerate(digits)}

prev = 9999
matches = []
for n in '11257ABCEG':
    if digit_map[n] == prev + 1:
        matches.append(n)
    else:
        if len(matches) >= 3:
            print(matches)
        matches = [n]
    prev = digit_map[n]

rawDescription: Change base python counts in

description: Change base python counts in


code:
#forms.py
class DialogFormSetHelper(FormHelper):
 def __init__(self, *args, **kwargs):
    super(DialogFormSetHelper, self).__init__(*args, **kwargs)
    self.form_tag = False
    self.disable_csrf = True
    self.layout = Layout(
        Field('info1', rows='3'),
        Field('info2', rows='3'),
        Field('DELETE')  # <- ADD THIS LINE
    )

rawDescription: Django remove inline formset record in UpdateView


code:
def traverse(self, size, obj, tmpList):
    if self.label is None or size == 0:
        return []
    dic = {}
    tt = [children.label for children in obj.children]
    dic[obj.label] = tt
    tmpList.append(dic)
    for child in obj.children:
        child.traverse(len(child.children), child, tmpList)
    return tmpList

rawDescription: Parsing a tree using python

description: Parsing a tree using python