code:
f = open('data.txt', 'r')
lines = f.read()
lines = lines.split("\n")
print(lines)
for line in lines:
    line = line.split()
    if len(line) > 0:
        srcIp = line[2]
        dstIp = line[4]
        print(srcIp.split('.'),dstIp.split('.'))

rawDescription: Python- Split out src IP, src Port, dest IP, dest Port and create a dictionary


code:
def ttree_to_json(ttree,level=0):
    result = {}
    for i in range(0,len(ttree)):
        cn = ttree[i]
        try:
            nn  = ttree[i+1]
        except:
            nn = {'level':-1}

        # Edge cases
        if cn['level']>level:
            continue
        if cn['level']<level:
            return result

        # Recursion
        if nn['level']==level:
            dict_insert_or_append(result,cn['name'],cn['value'])
        elif nn['level']>level:
            rr = ttree_to_json(ttree[i+1:], level=nn['level'])
            dict_insert_or_append(result,cn['name'],rr)
        else:
            dict_insert_or_append(result,cn['name'],cn['value'])
            return result
    return result

rawDescription: Creating a tree/deeply nested dict from an indented text file in python


code:
def valid_date(s):
    try:
        return datetime.strptime(s, "%Y-%m-%d")
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(s)
        raise argparse.ArgumentTypeError(msg)

rawDescription: Specify format for input arguments argparse python


code:
def find_the_right_match():
  # now 'm' is a stateful regex
  m = re.match('foo (\w+) bar (\d+)', line)
  if m:
    # do stuff with m.group(1) and m.group(2)
    return # <== exit nested function only
  m = re.match('baz whoo_(\d+)', line)
  if m:
    # do stuff with m.group(1)
    return

find_the_right_match()

rawDescription: Python's re module - saving state?

description: Python's re module - saving state


code:
result = [dictB[5] for dictB in listB
          if 5 in dictB and dictB[5] in interesting_vals]

rawDescription: Python: How to iterate lists of dictionaries Fast


code:
self.My_Clock = Clock
self.My_Clock.schedule_interval(self.Update, 1/60.)

rawDescription: how to make kivy widgets make a pause

description: Make kivy widgets make a pause


code: line = bytearray(line)

rawDescription: Python: Read binary file into buffer as integer then Slice


code:
from itertools import groupby
from operator import itemgetter

result = {k: (min(item[1] for item in gv), max(item[2] for item in gv))
         for k, g in groupby(sorted(inputlist, key=itemgetter(0)), itemgetter(0))
         for gv in (list(g),)}

rawDescription: Can Python's comprehensions make groups?

description: Can Python's comprehensions make groups


code:
import numpy as np

rows_a, rows_v, cols = (10, 15, 3)
a = np.arange(rows_a*cols).reshape(rows_a,cols)
v = np.arange(rows_v*cols).reshape(rows_v,cols)

def using_loop():
    c = 0
    for i in range(len(v)):
        D = ((a-v[i])**2).sum(axis=-1)
        c += D.min()
    return c

def using_broadcasting():
    return ((a[:,np.newaxis,:]-v)**2).sum(axis=-1).min(axis=0).sum()

rawDescription: Difference between every pair of columns of two numpy arrays (how to do it more efficiently)?


rawDescription: Binary selection process

description: Binary selection process


code:
def __init__(self,new):
    if type(new) is BSTNode:
        self._data = new.data
    else:
        self._data = new
    self._left = None
    self._right = None

rawDescription: Confusion with recursion and property setters in Python


rawDescription: Python: Remove Sublists from List if Same Including/Not Including Order


rawDescription: Computing persistence number of an integer


code:
def pairs(i):
    return list(zip(i, i[1:]))

my_list = ['I', 'swear', 'I', 'googled', 'first']
res_list = pairs(my_list)
print(res_list)
# [('I', 'swear'), ('swear', 'I'), ('I', 'googled'), ('googled', 'first')]
print([' '.join(a) for a in res_list])
# ['I swear', 'swear I', 'I googled', 'googled first']

rawDescription: get items 2 by 2 from a list in Python

description: Get items 2 by 2 from a list in Python


code:
computer_list = []
computer_list.append(Computer("rainier", "online", "1/1/2012", "desktop"))

computer_dict = {}
Computer.new_to_dict("baker", "online", "1/1/2012", "laptop", computer_dict)

rawDescription: What is a better way to store a list of properties against one item in Python?


code:
import subprocess
myfile="c:\\"
myEditor="gvim" #assuming gvim is in the path. If not use the full path
subprocess.Popen(myEditor + ' ' +mydir)

rawDescription: Python: Open a modules source code in Finder/Windows explorer


code:
from cStringIO import StringIO
import sys

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

rawDescription: how do i block stdout from showing my password during my subprocess call in python

description: Block stdout from showing my password during my subprocess call in python


code:
import math

class Foo:
    def __init__(self):
        self.foo = 4
    def bar(self):
        print("hello world")

    def foo(self):
        return self.bar()

def bar():
    return 123

f = Foo()
f.bar()
bar()

rawDescription: Comment Python function calls in a string with source code


code: [indexed, map] = imread('http://i.stack.imgur.com/OxFwB.png');

rawDescription: Read in an indexed color image in Python


code:
sys.version
id(open)
id(io.open)
io.open is open

rawDescription: How do I find out if one Python function is an alias for another one?

description: Find out if one Python function is an alias for another one


code:
[(datetime.date(2014, 4, 15), {'first_name': 'Mike', 'last_name': 'Jackson'}),
 (datetime.date(2014, 4, 15), {'first_name': 'Hannah', 'last_name': 'Jackson'})]
[(datetime.date(2014, 4, 16), {'first_name': 'Tom', 'last_name': 'Jackson'}),
 (datetime.date(2014, 4, 16), {'first_name': 'Macy', 'last_name': 'Jackson'})]

rawDescription: Iterating trough list and comparing dicts inside of list


rawDescription: Is there a way to make python str.partition ignore case?


code:
data # After sorting
scores

rawDescription: Numpy, sorting based on column twice

description: Numpy, sorting based on column twice


code:
obj = [None] * 100
for i in range(100):
    name=input('Your name:')
    obj[i] = Person(name)

rawDescription: Python - how to connect word with iteration


code:
A_1 = [1]
gbl = globals()
gbl['A_1']

rawDescription: Going through names of a set of matrices in Python


rawDescription: Can I access variable X in function B, if it is located in function A, which call function B?

description: Access variable X in function B, if it is located in function A, which call function B


code:
import warnings
WV = type("WV", (Warning, ValueError), {})
warnings.simplefilter("error", WV)
warnings.warn("wv", WV)

rawDescription: Raising and catching exceptions in an expression in Python 3


rawDescription: How to operate elementwise on a matrix of type scipy.sparse.csr_matrix?

description: Operate elementwise on a matrix of type scipy.sparse.csr_matrix


code:
from itertools import imap
input_list[:] = [x for x in imap(str.strip, input_list) if x]

rawDescription: Python - list comprehension in this case is efficient?


rawDescription: Select rows from a DataFrame based on values in a column in pandas


rawDescription: Python Decimals format

description: Python Decimals format


rawDescription: create a dict with unique keys and various list values from a tuple

description: Create a dict with unique keys and various list values from a tuple


rawDescription: Splitting a string with multiple delimiters and conditions


code:
import time
def some_view(request):
    for i in range(1, 31):
        request.response.body_file.write(".")
        time.sleep(1)

rawDescription: Can I render different parts of a Jinja2 template separately using Pyramid?

description: Render different parts of a Jinja2 template separately using Pyramid


rawDescription: Filtering a dictionary by multiple values


rawDescription: Path not found when executing my python script from my cmd prompt


code:
def type_chooser(N):
  import numpy as np
  for dtype in [np.uint8, np.uint16, np.uint32, np.uint64]:
    if N <= dtype(-1):
      return dtype
  raise StandardError('{} is really big!'.format(N))

rawDescription: Python: pick appropriate datatype size (int) automatically


code:
import requests
import time
oldtime = ''
for i in xrange(100):
    r = requests.get(url='http://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg',stream=True)
    newtime = r.headers['last-modified']
    if newtime != oldtime:
        image = r.content
        oldtime = newtime
    # you can put your time.sleep() statement here, but it is not necessary

rawDescription: python keep-alive connection and download image

description: Python keep-alive connection and download image


code:
offset=1
i=0
while i<len(str_numbers):
    if i==5:
        offset=2
    int_number = int(str_numbers[i:i+offset])
    i+=offset

rawDescription: Python - Looping through a string pulling 1 character at a time and then 2


code: getData(bedfile, matrix1, ... , matrix2, ...)

rawDescription: Using *args to call python function an undefined number of times


code: math.sqrt(sum((a[k] - b[k])**2 for k in a.keys()))

rawDescription: Calculate euclidean distance between two vector (bag of words) in python


code: rel = np.diff(input) / input[:-1]

rawDescription: Pandas time series operations

description: Pandas time series operations


code:
def __enter__(self):
    return self

def __exit__(self, type, value, traceback):
    if self.stdout:
        self.stdout.close()
    if self.stderr:
        self.stderr.close()
    if self.stdin:
        self.stdin.close()
    # Wait for the process to terminate, to avoid zombies.
    self.wait()

rawDescription: Exception handling when using Python's subprocess.Popen


code:
def reverse_binary_search(a, threshold):
    lo = 0
    for t in threshold:
        if lo < 0:
            raise ValueError('lo must be non-negative')
        hi = len(a)
        while lo < hi:
            mid = (lo+hi)/2
            if t > a[mid][6]:
                hi = mid
            else:
                lo = mid+1
        yield lo

my_list = [[3,0.99,1], [2,0.98,54], [10,.85,4], [1,0.7,10], [12,0.69,31], [12,0.65,43], [1.56,0]]
threshold = [0.97, 0.90, 0.83, 0.6]

index_list = list(reverse_binary_search(my_list, threshold))

rawDescription: Python: index of fist element smaller than threshold in reverse sorted list


code:
@add_visibility_fields
class IMember(Interface):
    first_name = schema.TextLine(title=u'first name')
    last_name = schema.TextLine(title=u'last name')

rawDescription: How to dynamically add attributes to an interface

description: Dynamically add attributes to an interface


code:
Counter('不')
repr('不')

rawDescription: Word-counter won't print foreign characters


code:
psimaps[-2] += psimaps[-1] - transitiveParams
psimaps = psimaps[:-1]

rawDescription: How to eliminate for-loop and use list comprehension when using numpy array?

description: Eliminate for-loop and use list comprehension when using numpy array


rawDescription: Elementwise logical checks using numpy arrays


code:
from collections import defaultdict
indexed_sums = defaultdict(int)
for o in xbs:
    indexed_sums[(o.W, o.X, o.Y)] += o.Z

rawDescription: Summing Attribute in a list of Python Objects


code: {k: v for k,v in a.items() if k[2] == 'l1p'}

rawDescription: Get elements of a tuple-indexed dictionary specifying only one field of the tuple


code:
class SpringSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(SpringSemesterEventManager, self).get_query_set() \
            .filter(is_spring_semester=True)

class FallSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(FallSemesterEventManager, self).get_query_set() \
            .filter(is_fall_semester=True)

class Event(models.Model):
    event_date = models.DateField()

    @property
    def is_spring_semester(self):
        month = self.event_date.month
        return True if month >= 1 and month <= 5 else False

    @property
    def is_fall_semester(self):
        month = self.event_date.month
        return True if month >= 8 and month <= 12 else False

    objects = models.Manager()
    spring_semester = SpringSemesterEventManager()
    fall_semester = FallSemesterEventManager()

rawDescription: How to define range of months for any year on Django model?

description: Define range of months for any year on Django model


rawDescription: Python - Regexp - Find function names but not function calls


code:
from collections import defaultdict

def add(v1, v2):
    assert isinstance(v1, SparseVector) and isinstance(v2, SparseVector)
    assert v1.size == v2.size
    values = defaultdict(float) # Dictionary with default value 0.0
    # Add values from v1
    for i in range(v1.indices.size):
        values[v1.indices[i]] += v1.values[i]
    # Add values from v2
    for i in range(v2.indices.size):
        values[v2.indices[i]] += v2.values[i]
    return Vectors.sparse(v1.size, dict(values))

rawDescription: How to add two Sparse Vectors in Spark using Python

description: Add two Sparse Vectors in Spark using Python


code:
cursor.execute("UPDATE company SET name = replace(name, ',', '')")
db.commit()

rawDescription: (Python) How to edit existing rows in a SQL DB?


code:
from sphinx.application import Sphinx
s = Sphinx(...)
s.build(...)

rawDescription: access eggs in python?

description: Access eggs in python


code:
with open('test1.csv', newline='') as f:
    for line in f:
        cols = line.split(',')
        numbers = [float(i) for i in cols[1:-1]]
        mean = sum(numbers) / len(numbers)
        name = cols[0]
        print("The row contains %s" % ','.join(numbers))
        print("The average for %s is %2f\n" % (name, mean))

rawDescription: How to count numbers in a list from a csv file filtering out words and commas

description: Count numbers in a list from a csv file filtering out words and commas


code: self.registerForm("MAIN", MainForm())

rawDescription: Using Npyscreen not for the whole terminal


code:
import binascii
binascii.hexlify(s)

rawDescription: How do I force recv() in Socket to NOT convert my hex values into ASCII if it can (python)

description: Force recv() in Socket to NOT convert my hex values into ASCII if it can (python)


code: camera = Camera(complex_camera, total_level_width, total_level_height)

rawDescription: Add scrolling to a platformer in pygame

description: Add scrolling to a platformer in pygame


code:
import csv
from itertools import zip_longest

with open('test.txt') as csvfile:
    rows = csv.reader(csvfile)

    res = list(zip_longest(*rows))
    print(res)
    # [('6', '5', '7'), ('2', '2', None), ('4', None, None)]

    res2 = [list(filter(None.__ne__, l)) for l in res]
    print(res2)
    # [['6', '5', '7'], ['2', '2'], ['4']]

rawDescription: How can I turn a csv file into a list of list in python

description: Turn a csv file into a list of list in python


rawDescription: Ordered Dictionary is not sorting

description: Ordered Dictionary is not sorting


rawDescription: Pandas Multi-indexing from a Flatten DataFrame


code:
plt.scatter(x,y,s=1)
if px!='':
    plt.plot([px,x],[py,y],'-o',markersize=1)

rawDescription: matplotlib: Don't compress plot in the horizontal direction

description: Matplotlib: Don't compress plot in the horizontal direction


code:
from scrapy.spidermiddlewares.offsite import OffsiteMiddleware
from scrapy.utils.httpobj import urlparse_cached

class SimpleOffsiteMiddleware(OffsiteMiddleware):

    def spider_opened(self, spider):
        # don't build a regex, just use the list as-is
        self.allowed_hosts = getattr(spider, 'allowed_domains', [])
        self.domains_seen = set()

    def should_follow(self, request, spider):
        if self.allowed_hosts:
            host = urlparse_cached(request).hostname or ''
            # does 'www.example.com' end with 'example.com'?
            # test this for all allowed domains
            return any([host.endswith(h) for h in self.allowed_hosts])
        else:
            return True

rawDescription: How to work with a very large "allowed_domains" attribute in scrapy?

description: Work with a very large "allowed_domains" attribute in scrapy


code:
for i, url in enumerate(urls):
    yield scrapy.Request("http://www.example.com", meta={'cookiejar': i},
        callback=self.parse_page)

rawDescription: Scrapy - how to manage cookies/sessions

description: Scrapy - how to manage cookies/sessions


code:
import io
io.BytesIO().mode

rawDescription: Pass io.BytesIO object to gzip.GzipFile and write to GzipFile


code:
import numpy as np

a = np.array([1,2,3,4])
d = np.diag(a)
# or simpler: d = np.diag([1,2,3,4])

print(d)

rawDescription: How to convert a column or row matrix to a diagonal matrix in Python?

description: Convert a column or row matrix to a diagonal matrix in Python


code:
def statement_constructor(list_type, list_size):
    template_string = ""
    input_string = ""
    array = []
    for z in range(list_size):
        string_part = "{:<8}"
        template_string += string_part
        if list_type == "signs_list":
            array.append('x' if len(signs_list[z]) >= i else '')
        elif list_type == 'test_list':
            array.append(test_list[z])
        else:
            array.append(test_string_list[z])
    print (template_string).format(*array)

length = len(test_string_list)
for i in range(len(max(signs_list)), 0, -1):
    statement_constructor('signs_list', length)
statement_constructor('test_list', length)
statement_constructor('test_string_list', length)

rawDescription: Basic Python List Excercise

description: Basic Python List Excercise


code:
fewest_items_closest_sum_with_repetition([2,3,4,7,20,25], 105)
fewest_items_closest_sum_with_repetition((40,79), 80)

rawDescription: reaching the goal number

description: Reaching the goal number


code:
def tries(path, url, i, max_tries=1):
    for ty in range(1, max_tries+1):
        try:
            req = requests.get(url)
            page_content = req.content
            with open('{}{}.html'.format(path, i), 'w') as fid:
                fid.write(page_content)
            break
        except requests.exceptions.ConnectionError as e:
            print("Error {} for try {}".format(e, ty))

for ind, url in enumerate(urls):
    tries(path, url, ind, 4)

rawDescription: Incorrect output when downloading .html files


rawDescription: Python : fill cuboid with spheres

description: Python : fill cuboid with spheres


rawDescription: Python string function that removes one duplicate pair from multiple duplicates


rawDescription: How to specify size (dimension) of .eps when exported from matplotlib using savefig()

description: Specify size (dimension) of .eps when exported from matplotlib using savefig()


rawDescription: What type of file is the "sound fragment" parameter for audioop?


code:
def select_join(dfs, index):
    result = dfs[0].reindex(index)
    for df in dfs[1:]:
        result = result.join(df, how='inner')
    return result

rawDescription: pandas concat DataFrame on different Index

description: Pandas concat DataFrame on different Index


code:
for i in xrange(10, 0, -1):
    with open('C:/TestReq_100 Records.txt', 'r') as ipf:
        with open('processed.txt', 'w') as opf:
            for line in ipf:
                num = line.strip()
                opf.write(num)

rawDescription: writing to a file with python

description: Writing to a file with python


code:
f = open('test.json')
df = pd.read_json(next(f))
metdata = json.loads(next(f))

rawDescription: Saving Pandas DataFrame and meta-data to JSON format


rawDescription: Pandas Ordered Histogram Matrix

description: Pandas Ordered Histogram Matrix


code:
del msg['From']
msg['From'] = f

rawDescription: bug in my mail sending pogram in python

description: Bug in my mail sending pogram in python


code: key, val = height_by_gender_and_agegroup(person)

rawDescription: returning multiple values in python

description: Returning multiple values in python


code:
def countWords(s):
    words = s.split()
    return len([word for word in words if len(word)>=2])

rawDescription: Retrieving total number of words with 2 or more letters in a document using python


rawDescription: Reshaping/Combining several sub-matrices to one matrix in multi-dimensional space


code:
def encode(binary):
    # Aligning on bytes
    binary = '0' * (8 - len(binary) % 8) + binary
    # Generating the corresponding character for each
    # byte encountered
    return ''.join(chr(int('0b' + binary[i:i+8], base = 2))
                   for i in xrange(0, len(binary), 8))

rawDescription: python Binary string to binary data

description: Python Binary string to binary data


code: new_list_copy = old_list[:]

rawDescription: Python make list copy, not reference

description: Python make list copy, not reference


code:
class Employees:
  def gatherAttrs(self):
    attrs = []
    for key in ["name", "job", "pay", "age"]:
        attrs.append('%s = %s' % (key, getattr(self,key)))
    return ', '.join(attrs)

rawDescription: Print out dictionary by (specific) key

description: Print out dictionary by (specific) key


rawDescription: Matplotlib: how to set the current figure?


code:
from insignificant_digit_cutter import cut_insignificant_digits_recursively

def is_almost_equal(first, second, places):
    '''returns True if first and second equal.
    returns true if first and second aren't equal but have exactly the same
    structure and values except for a bunch of floats which are just almost
    equal (floats are almost equal if they're equal when we consider only the
    [places] most significant digits of each).'''
    if first == second: return True
    cut_first = cut_insignificant_digits_recursively(first, places)
    cut_second = cut_insignificant_digits_recursively(second, places)
    return cut_first == cut_second

rawDescription: assertAlmostEqual in Python unit-test for collections of floats

description: AssertAlmostEqual in Python unit-test for collections of floats


code:
myList =[1,2,3,4,5,6,7,8,9,10]
d = {}
for index, value in enumerate(myList, start=1):
    d['numb'+str(index)] = value

d['numb1']
d['numb2']
d['numb10']

rawDescription: Save Numbered Variable

description: Save Numbered Variable


rawDescription: python pandas extract unique dates from time series

description: Python pandas extract unique dates from time series


code: new_values[0] > new_values[3] > new_values[1] > new_values[4] > new_values[2]

rawDescription: How to sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array

description: Sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array


code:
def my_range(start, end, how_many):
    incr = float(end - start)/(how_many - 1)
    return [start + i*incr for i in range(how_many-1)] + [end]

rawDescription: Is there a built-in Python function to generate 100 numbers from 0 to 1?


code: overlap = [n for n in range(7) if (bitmask1[n] == bitmask2[n] == '1')]

rawDescription: Python, SQLAlchemy: Storing business hours as binary bitmask


code:
from pandas.util.testing import assert_series_equal
assert_series_equal(s1, s2)

rawDescription: Comparing pandas.Series for equality when they contain nan?


code:
u -= u_val[:,None]
y -= y_val[:,None]

rawDescription: Python Changing Array Values in While Loop


rawDescription: I'm creating a quiz which is for primary school students


code: soup.select('table tr table tr td a')

rawDescription: Nested tags in BeautifulSoup - Python

description: Nested tags in BeautifulSoup - Python


code:
d = {}
set_with_path(d, ['one', 'two', 'three'], 'val')
d
set_with_path(d, ['one', 'four', 'six'], 'val2')
d

rawDescription: Python array from set of keys

description: Python array from set of keys


code:
def formatbyte(n):
    return "$%02X" % n

def formatword(n):
    return "$%04X" % n

rawDescription: Strategy pattern in Python when a "strategy" consists of more than one function


code:
import requests
from lxml import html

url = "http://www.tspc.oregon.gov/lookup_application/LDisplay_Individual.asp?id=535454R3L38"
page = requests.get(url)

tree = html.fromstring(page.text)
for license_row in tree.xpath(".//tr[td[1] = 'License Type']/following-sibling::tr[1]"):
    license_data = license_row.xpath(".//td/text()")
    print(license_data)

rawDescription: Creating a local variable to represent a range of numbers within a xpath tr[ ]?


code: arr = [[runxy, runx], [runy, nonex]]

rawDescription: Select a function in a Pythonic way

description: Select a function in a Pythonic way


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

Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= np.random.random((5, 4))
df = pd.DataFrame(data, index=Index, columns=Cols)

plt.pcolor(df > 0.5, cmap='RdYlGn')
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()

rawDescription: Color a heatmap in Python/Matplotlib according to requirement


code:
for x in mylist:
    yield x[:5]

rawDescription: Return sublists of List of lists

description: Return sublists of List of lists


code:
print('10' > '5')
False

rawDescription: List sorts correctly one time, then the subsequent times it sorts incorrectly


code:
import pandas as pd
import io # only needed to import sample data

data = """
    date          tweet_id    tweet
    2015-10-31    50230       tweet_1
    2015-10-31    48646       tweet_2
    2015-10-31    48748       tweet_3
    2015-10-31    46992       tweet_4
    2015-11-01    46491       tweet_5
    2015-11-01    45347       tweet_6
    2015-11-01    45681       tweet_7
    2015-11-01    46430       tweet_8
    """

df = pd.read_csv(io.StringIO(data), delimiter='\s+', \
                 index_col=False, parse_dates = ['date'])

# Tweet count starts here
df_count = df.set_index('date').resample('D', how='count') # 'D' for offset calendar day
df_count = df_count.drop(df_count.columns[1:], axis=1)
df_count.columns = ['count']

print(df)

rawDescription: How to count daily word frequency from twitter?

description: Count daily word frequency from twitter


code: sub = np.array([blockshaped(a, 2, 2) for a in test2])

rawDescription: Python - Get "subarrays" of 3d array

description: Python - Get "subarrays" of 3d array


code: numpy.uint32(0) - numpy.uint32(1)

rawDescription: Subtracting 3D numpy arrays in Python Vs Matlab


code:
all_values = []
for x in range(Xs):
    for y in range(Ys):
        values = []
        for z in range(Zs):
            values = [x, y, z]
            for variable in variables:
                values.append(data[variable][x][y][z])
            all_values.append(values)

cur.executemany(sql, all_values)

rawDescription: Fastest way to combine and normalise data from mutiple multi-dimensional arrays


rawDescription: Python BeautifulSoup Mix Matching items in Table


code:
multis = '%s, '*23
multis = multis[:-2]

rawDescription: this python script can be shortened/optimized, how?

description: This python script can be shortened/optimized, how


code: palindromes = [x for x in xrange(min, max) if isPalindrome(x)]

rawDescription: How to generate a list of palindrome numbers within a given range?

description: Generate a list of palindrome numbers within a given range


code:
def run_example1():
    """
    This is example number 1. Running it should give you the following output:

    >>> run_example1()
    "This is the output from example1.py"
    """

    os.system('python example1.py') # or you could use subprocess here

if __name__ == "__main__":
    import doctest
    doctest.testmod()

rawDescription: Running a set of Python scripts in a list


code:
def is_name(x):
  return # if x is a name or not

rawDescription: Python: Linking Lists Together

description: Python: Linking Lists Together


code:
from django.core.cache import cache

# ...
cache.delete('my_url')

rawDescription: Clearing specific cache in Django

description: Clearing specific cache in Django


code:
def test_methodFoo(self):
    self.object_under_test.methodFoo()
    self.assertTrue(all([booObject.shooMethod.called for
                         booObject in self.object_under_test.booObjectList]))

rawDescription: How to MagicMock a list of object with method and get assert count


code:
class CustomManager(models.Manager):
    def get_query_set(self):
        return super(CustomManager, self).get_queryset().filter(canceled=False)

class MyModel(models.Model):
    # Blah blah
    objects = CustomManager()

rawDescription: Modify default queryset in django

description: Modify default queryset in django


code: names = [friend[u'name'] for friend in friends if friend['type'] == 0]

rawDescription: Counting lines in python without using a textfile


code:
import subprocess

proc = subprocess.Popen(['ping', '-t', 'google.com'], shell=True)

raw_input("<RETURN> to terminate")
proc.terminate()

raw_input("<RETURN> to end")

rawDescription: Why does Popen.terminate() terminate subprocess run with command interpreter (cmd.exe) on Windows?


code:
import Tkinter as tk

class ButtonBlock(object):
    def __init__(self, master):
        self.master = master
        self.button = []
        self.button_val = tk.IntVar()
        entry = tk.Entry()
        entry.grid(row=0, column=0)
        entry.bind('<Return>', self.onEnter)
        entry.focus()
        clear_button = tk.Button(master, text='Clear', command=self.onClear)
        clear_button.grid(row=0, column=1)
    def onClear(self):
        for button in self.button:
            button.destroy()
    def onEnter(self, event):
        entry = event.widget
        num = int(entry.get())
        self.onClear()
        for i in range(1, num+1):
            self.button.append(tk.Radiobutton(
                self.master, text=str(i), variable=self.button_val, value=i,
                command=self.onSelect))
            self.button[-1].grid(sticky='WENS', row=i, column=0, padx=1, pady=1)
    def onSelect(self):
        print(self.button_val.get())

if __name__ == '__main__':
    root = tk.Tk()
    ButtonBlock(root)
    root.mainloop()

rawDescription: Tkinter - How would I go about resetting list of objects?


code:
import pickletools
import pickle
pik = pickle.dumps(dict(zip(list('abcde'),[1,2,3,4,5])))
pickletools.dis(pik)
pik

rawDescription: Delete a Variable that has been Pickled

description: Delete a Variable that has been Pickled


rawDescription: representing list of tuples as 2d array in python

description: Representing list of tuples as 2d array in python


code:
from selenium import webdriver
from selenium.webdriver.common import action_chains

driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")

action = action_chains.ActionChains(driver)

login = driver.find_element_by_class_name("login-icon")
login.click()

driver.switch_to.frame(driver.find_element_by_id('custlogin'))

username = driver.find_element_by_id('username')

action.move_to_element(username).perform()

username.send_keys('Gus Gabel')

rawDescription: Python Selenium: Can't find the login element of costar.com


code:
import time

def timed_input(prompt):
    start = time.time()
    s = input(prompt)
    return s, time.time() - start

rawDescription: Timing/Recording input() in Python 3.1

description: Timing/Recording input() in Python 3.1


rawDescription: how to Iterate in exhaustively in python 2?

description: Iterate in exhaustively in python 2


code:
for eachitem in dicta:
    for k in ['NAME','STATE','COUNTRY','REGION','LNAME']:
        del eachitem[k]

rawDescription: deleting element from python dictionary

description: Deleting element from python dictionary


code:
tip_predict = (tips.groupby(["day", "sex"])
                   .total_bill
                   .mean()
                   .apply(lambda x: x * .2)
                   .reset_index(name="tip"))
tip_all = pd.concat(dict(data=tips[["day", "sex", "tip"]], model=tip_predict),
                    names=["kind"]).reset_index()

sns.factorplot("day", "tip", "kind", data=tip_all, col="sex",
               kind="point", linestyles=["-", "--"], markers=["o", "D"])

rawDescription: Showing data and model predictions in one plot using Seaborn and Statsmodels


code:
for city in sorted(poster_cities.keys()):
    city_posters = poster_cities[city]
    city_poster_count = len(poster_cities[city])

rawDescription: Django Query and Grouping

description: Django Query and Grouping


rawDescription: Different results when running in python interpreter vs. script file


code:
class Foo: bar = 'baz'

timeit.timeit('outcome = Foo.bar', 'from __main__ import Foo')

rawDescription: How to use Python 3.4's enums without significant slowdown?

description: Use Python 3.4's enums without significant slowdown


code:
starter = functools.partial(startencoder, crf=args.crf, preset=args.preset)
procs = []
maxprocs = cpu_count()
for ifile in args.files:
    while len(procs) == maxprocs:
        manageprocs(procs)
    procs.append(starter(ifile))
while len(procs) > 0:
    manageprocs(procs)

rawDescription: How to process sub-batches of asynchronous jobs?

description: Process sub-batches of asynchronous jobs


rawDescription: I'm trying to get the correct minutes, and seconds when using integers


rawDescription: Python Regular Expression to Edit First Comment


code:
df = pd.DataFrame([[np.nan, 1], [0, np.nan], [1, 2]])
df
pd.isnull(df.values)
pd.isnull(df.values).any(1)
np.nonzero(pd.isnull(df.values).any(1))
df.index[np.nonzero(pd.isnull(df.values).any(1))]

rawDescription: Find indices where df is null

description: Find indices where df is null


code:
m = re.search(r"[a-z]*\(([a-z, ]*)\)", string)
m.group(1)
m.group(1).split(', ')

rawDescription: Python re: Storing multiple matches in variables


code:
import operator

x,y = Ints("x y")

a = operator.add(x,y)

rawDescription: Z3Py: getting the function corresponding to each operator e.g. '*', '==', '-'


code:
x = A.tocoo()
x.data = np.log(x.data)
A = x.todok()

rawDescription: scipy.sparse default value

description: Scipy.sparse default value


code:
class BaseTest(unittest.TestCase):

    def setUp(self):
        # This makes things nicer if the previous test fails
        # - without this all subsequent tests fail
        self.tearDown()

        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()
        session.expunge_all()
        session.rollback()

rawDescription: Creating databases in SQLAlchemy tests with PostgreSQL


code:
import numpy as np
np.array([1,2,3,4])

rawDescription: How to maintain different version of a python module?

description: Maintain different version of a python module


rawDescription: pandas DataFrame.groupby with a tolerance

description: Pandas DataFrame.groupby with a tolerance


code:
import numpy as np
N, K = 3, 4
X = np.arange(N*K).reshape(N,K)
# idx indicates which element you wish to remove from each row
idx = np.arange(N)

mask = np.ones_like(X, dtype='bool')
mask[np.arange(X.shape[0]), idx] = 0

Y = X[mask].reshape(N, K-1)
t = X[~mask]

rawDescription: Numpy array, removing one arbitrary element per row


code:
def __contains__(self, target):
    for element in self:
        if target is element or target == element:
            return True
    return False

rawDescription: Does Python's 'in' operator for lists have an early-out for successful searches


code:
name,type,number = i.split(',')
# now name="Adam", type="widgets", number="5769"

rawDescription: spliting list and adding values

description: Spliting list and adding values


code: x = zip(name, animal)

rawDescription: enumerate is iterating of letters of strings in a list instead of elements

description: Enumerate is iterating of letters of strings in a list instead of elements


code: tck = splrep(x, y, w=np.ones(len(x_spl)))

rawDescription: scipy splrep() with weights not fitting the given curve

description: Scipy splrep() with weights not fitting the given curve


code:
import json
from collections import defaultdict

l = [('HCS', 'Assured Build', 'Implementation', 'Hardware Stack'),
     ('HCS', 'Assured Build', 'Implementation', 'SA and SF'),
     ('HCS', 'Assured Build', 'Testing and Validation', 'NFRU-SS'),
     ('HCS', 'Assured Build', 'Testing and Validation', 'NRFU-UC'),
     ('HCS', 'Assured Platform', 'Restoration', 'AS Build'),
     ('HCS', 'Assured Platform', 'Restoration', 'Capacity Management'),
     ('HCS', 'Assured Platform', 'Migration', 'Document Review')]

d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
for key1, key2, key3, value in l:
    d[key1][key2][key3].append(value)

print(json.dumps(d, indent=4))

rawDescription: list of tuples to dictionary of dictionaries

description: List of tuples to dictionary of dictionaries


code:
class foo(Parent):
    @require_super
    def function(self, arg):
        s = "No super"

rawDescription: How to enforce (with a warning) that parent functions are called by the children that override them?

description: Enforce (with a warning) that parent functions are called by the children that override them


rawDescription: Process CSV in Python

description: Process CSV in Python


rawDescription: Dictionary Comprehension for list values


code:
def do_tasks(b):
    rs = ResultSet([])
    for a in b:
        rs.add(c.delay(a))
    return rs.get()

rawDescription: Add n tasks to celery queue and wait for the results


code:
wid=Tk()
wid1=Button(None,text="hello guys",command=hello) #ur function hello
wid2=Button(None,text="Quit",command=quitbtn) #ur function quitbtn
wid3=Button(None,text="upload it",command=my)  #ur function my
wid1.grid(row=0,column=0)
wid2.grid(row=0,column=1)
wid3.grid(row=0,column=2)

wid.mainloop()

rawDescription: Python Script for creating a GUI using opencv


code:
import io

with io.open('output.txt', 'w', encoding='utf8') as f:
    for header in soup.find_all(['h1', 'h2', 'h3']):
        f.write(header.get_text() + u'\n')
        for elem in header.next_siblings:
            if elem.name and elem.name.startswith('h'):
                # stop at next header
                break
            if elem.name == 'p':
                f.write(elem.get_text() + u'\n')

rawDescription: Print heading and corresponding paragraph in beautiful soup


rawDescription: make my enumerated index correspond if I manually peek forward in the generator?

description: Make my enumerated index correspond if I manually peek forward in the generator


rawDescription: graph from weighted edge list from a database

description: Graph from weighted edge list from a database


rawDescription: I use os.system to run executable file, but it need a .so file, it can't find library


rawDescription: Can someone explain mdict in python (scipy.io), for example in scipy.io.savemat()?


code:
from operator import itemgetter

items_2_and_5 = itemgetter(2, 5)

field_two, field_five = items_2_and_5(data.split('|', 6))

rawDescription: Python Split() - Optimum way to get multiple fields?


code: decoded = response.json()

rawDescription: How to convert a JSON string into a Python data structure

description: Convert a JSON string into a Python data structure


rawDescription: Where should I place a config.txt in my python project


code: my_module_classes = tuple(x[1] for x in inspect.getmembers(my_module,inspect.isclass))

rawDescription: Python -- Check if object is instance of any class from a certain module


code:
class UpdateManager(object):
  waiters = []
  attrs = []
  other_attrs = []

  @classmethod
  def set_attr(cls, attr):
    cls.attrs.append(attr)

  @classmethod
  def set_other_attr(cls, attr):
    cls.other_attrs.append(attr)

  @classmethod
  def add_callback(cls, cb):
    cls.waiters.append(cb)

  @classmethod
  def send(cls):
    for cb in cls.waiters:
      cb(cls.attrs, cls.other_attrs)

rawDescription: Having persistent runtime objects with Tornado


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

im = Image.open('/home/jofer/logo.png')
height = im.size[1]

# We need a float array between 0-1, rather than
# a uint8 array between 0-255
im = np.array(im).astype(np.float) / 255

fig = plt.figure()

plt.plot(np.arange(10), 4 * np.arange(10))

# With newer (1.0) versions of matplotlib, you can
# use the "zorder" kwarg to make the image overlay
# the plot, rather than hide behind it... (e.g. zorder=10)
fig.figimage(im, 0, fig.bbox.ymax - height)

# (Saving with the same dpi as the screen default to
#  avoid displacing the logo image)
fig.savefig('/home/jofer/temp.png', dpi=80)

plt.show()

rawDescription: how to insert a small image on the corner of a plot with matplotlib?

description: Insert a small image on the corner of a plot with matplotlib


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

alpha = ['ABC', 'DEF', 'GHI', 'JKL']

data = np.random.random((4,4))

fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

plt.show()

rawDescription: matplotlib matshow labels

description: Matplotlib matshow labels


code:
class Foo():
    var = "Foo"
    @classmethod
    def print_var(cls):
        print(cls.var)

class Bar(Foo):
    var = "Bar"

Bar.print_var()
Foo.print_var()

rawDescription: Access overridden class variable in parent of subclass


code:
# Save JSON file
f = open('names.json', 'w')
f.write( json.dumps(json_ready, sort_keys=True, indent=4) )

# Query scraper
app = subprocess.Popen("/usr/local/bin/casperjs /var/www/html/tracker/scraper.js", shell=True)
app.wait()
out, errs = app.communicate()

rawDescription: Casper.js unable to open JSON file when activated through python script


rawDescription: Get the value of a tag which has another tag before its value with BeautifulSoup


code:
C = Context({"person": p})
C.get("person")
C.get("person").name

rawDescription: Evaluate variable in context

description: Evaluate variable in context


code:
from pyramid.renderers import render

opts = {} # a dictionary of globals to send to your template
body = render('email.mako', opts, request)

rawDescription: Turbomail Integration with Pyramid

description: Turbomail Integration with Pyramid


rawDescription: Pandas, unexpected behavior of .resample('B')


code:
Models
class Product(models.Model):
    title = models.CharField(max_length=255)
    short_description = models.TextField(blank=True,null=True)
    video_link = models.URLField(blank=True,null=True)
    price = models.IntegerField(db_index=True,blank=True,null=True)
    user = models.ForeignKey(
        to=User,
        related_name="products",
    )

    ...

class ProductCategory(models.Model):
    subcategory = models.CharField(max_length=255)
    product = models.ForeignKey(
        to=Product,
        related_name="categories",
    )
    date = models.DateTimeField(auto_now_add=True)

    ...

    def __unicode__(self):
        return self.subcategory  # Tech, Books, etc.

rawDescription: DjangoRestFramework - How to get result from different serializers into one


rawDescription: How to merge multiple arrays in python?

description: Merge multiple arrays in python


code:
from twisted.web import server
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory

class SomeWebThing(Resource):
    def render_GET(self, request):
        return "hello\n"

class SomeClient(Protocol):
    def dataReceived(self, data):
        p = self.factory.site.buildProtocol(self.transport.addr)
        p.transport = self.transport
        p.dataReceived(data)

class SomeClientFactory(ClientFactory):
    protocol = SomeClient
    def __init__(self, site):
        self.site = site

if __name__ == '__main__':
    root = Resource()
    root.putChild('thing', SomeWebThing())
    site = server.Site(root)
    reactor.listenTCP(8000, site)

    factory = SomeClientFactory(site)
    reactor.connectTCP('localhost', 9000, factory)

    reactor.run()

rawDescription: Manually giving the twisted (web) network stack a packet to process?


code: df.index.name = None

rawDescription: How to remove ugly row in pandas.dataframe

description: Remove ugly row in pandas.dataframe


code: format(f, ".15g")

rawDescription: Python float to Decimal conversion

description: Python float to Decimal conversion


code:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
d = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j'] # another list with same length
map(lambda x: x>=4, a)  # this will return a boolean list
import itertools
itertools.compress(d, map(lambda x: x>4, a)) # magic here !
[ _ for _ in itertools.compress(d, map(lambda x: x>=4,a)) ] # iterate the result.

rawDescription: Find the indices of elements greater than x


code:
import os
s = os.urandom(100000)

rawDescription: How to produce an arbitary string with a specific length in python?

description: Produce an arbitary string with a specific length in python


rawDescription: How to print non-ascii characters to file in Python 2.7

description: Print non-ascii characters to file in Python 2.7


code:
def pdiff(x):
    return p1(x)-p2(x)

rawDescription: Python - matplotlib: find intersection of lineplots


code:
class Foo(object):
    _a = 2

    @property
    def a(self):
        Foo._a += 1
        return Foo._a - 1

    b = 2

rawDescription: Python: How to do extra stuff when a specific attribute of an object is accessed?


code: showsPlayer

rawDescription: how to fetch javascript contents in python

description: Fetch javascript contents in python


code:
class BookSerializer(serializers.ModelSerializer):
    book_written_by = serializers.SerializerMethodField('get_author')

    class Meta:
        model = User

    def get_author(self, obj):
        return {'Author': obj.book_written_by.pk}

rawDescription: What is the easiest way to get custom serialized model with ForeignKey field


code:
from twisted.internet.protocol import Factory
from twisted.protocols.portforward import ProxyServer, ProxyFactory
class Balancer(Factory):
    def __init__(self, hostports):
        self.factories = []
        for (host, port) in hostports:
            self.factories.append(ProxyFactory(host, port))
    def buildProtocol(self, addr):
        nextFactory = self.factories.pop(0)
        self.factories.append(nextFactory)
        return nextFactory.buildProtocol(addr)

rawDescription: General question regarding wether or not use Twisted in TCP proxy project


code:
import numpy as np
from scipy.ndimage import measurements

arr = np.array([
[0,0,0,0,0,0],
[0,1,1,0,0,0],
[0,1,1,0,0,1],
[0,0,0,0,1,1],
[0,0,0,0,1,1]
])

labeled_array, number_of_features = measurements.label(arr)
print(labeled_array)

rawDescription: How to distinguish between multiple shapes in a figure or array?

description: Distinguish between multiple shapes in a figure or array


code:
if ip in d:
   if domain not in d[ip]:
     d[ip].append(domain)
else:
   d[ip] = [domain]

rawDescription: How to get unique keys and list of unique values in python dictionary?

description: Get unique keys and list of unique values in python dictionary


code:
rows = 8
cols = 8
a = numpy.arange(rows * cols).reshape((rows, cols))
fill = numpy.zeros((rows - 1) * cols, dtype='i8').reshape((rows - 1, cols))
stacked = numpy.vstack((a, fill, a))
major_stride, minor_stride = stacked.strides
strides = major_stride, minor_stride * (cols + 1)
shape = (cols * 2 - 1, cols)
numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
diags = numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
diags.sum(axis=1)

rawDescription: Find diagonals sums in numpy (faster)

description: Find diagonals sums in numpy (faster)


code:
from __future__ import division
import autopy
import time

x0, y0 = 800, 300
xf, yf = 100, 600

total_time = 5.00  # in seconds
draw_steps = 1000  # total times to update cursor

dx = (xf-x0)/draw_steps
dy = (yf-y0)/draw_steps
dt = total_time/draw_steps

for n in xrange(draw_steps):
    x = int(x0+dx*n)
    y = int(y0+dy*n)
    autopy.mouse.move(x,y)
    time.sleep(dt)

rawDescription: simulate cursor motion from X,Y to X,Y

description: Simulate cursor motion from X,Y to X,Y


code:
def triu_indices_cumsum(N):

    # Length of R and C index arrays
    L = (N*(N-1))/2

    # Positions along the R and C arrays that indicate
    # shifting to the next row of the full array
    shifts_idx = np.arange(2,N)[::-1].cumsum()

    # Initialize "shift" arrays for finally leading to R and C
    shifts1_arr = np.zeros(L,dtype=int)
    shifts2_arr = np.ones(L,dtype=int)

    # At shift positions along the shifts array set appropriate values,
    # such that when cumulative summed would lead to desired R and C arrays.
    shifts1_arr[shifts_idx] = 1
    shifts2_arr[shifts_idx] = -np.arange(N-2)[::-1]

    # Finall cumsum to give R, C
    R_arr = shifts1_arr.cumsum()
    C_arr = shifts2_arr.cumsum()
    return R_arr, C_arr

rawDescription: Processing upper triangular elements only with NumPy einsum


code:
from subprocess import check_call, STDOUT, CalledProcessError

with open("out.txt","w") as f:
    try:
        check_call(['ls', '-l'], stdout=f, stderr=STDOUT)
    except CalledProcessError as e:
        print(e.message)

rawDescription: How to execute and save result of an OS command to a file

description: Execute and save result of an OS command to a file


code:
result = (tmp
    .withColumn("end_time", end_time)
    .where(f.col("end_time").isNotNull())
    .withColumn("duration", duration)
    .withColumnRenamed("datetime", "start_time"))

rawDescription: PySpark - Combining Session Data without Explicit Session Key / Iterating over All Rows


code:
from quicksect import IntervalNode

class Interval(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end
        self.removed = False

def maximize_nonoverlapping_count(intervals):
    intervals = [Interval(start, end) for start, end in intervals]
    # sort by the end-point
    intervals.sort(key=lambda x: (x.end, (x.end - x.start)))   # O(n*log n)
    tree = build_interval_tree(intervals) # O(n*log n)
    result = []
    for smallest in intervals: # O(n) (without the loop body)
        # pop the interval with the smallest end-point, keep it in the result
        if smallest.removed:
            continue # skip removed nodes
        smallest.removed = True
        result.append([smallest.start, smallest.end]) # O(1)

        # remove (mark) intervals that overlap with the popped interval
        tree.intersect(smallest.start, smallest.end, # O(log n + m)
                       lambda x: setattr(x.other, 'removed', True))
    return result

def build_interval_tree(intervals):
    root = IntervalNode(intervals[0].start, intervals[0].end,
                        other=intervals[0])
    return reduce(lambda tree, x: tree.insert(x.start, x.end, other=x),
                  intervals[1:], root)

rawDescription: Python - Removing overlapping lists

description: Python - Removing overlapping lists


code:
words = []
while True:
   y = input()
   if y == "###":
     break
   y = y.lower()
   y = y.split()
   words.extend(y)

rawDescription: Parsing multiple lines in Python

description: Parsing multiple lines in Python


code:
idx = mydf.columns.get_level_values('level_2').map(lambda t: 'mystring' in t)
mydf.loc[:, idx]

rawDescription: In pandas, select all columns whose label contains a given string at a specified level


code:
['', 'A', 'B', 'C', 'AA', 'AB', 'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC', 'AAA', 'AAB', 'AAC', 'ABA', 'ABB', 'ABC', 'ACA', 'ACB', 'ACC', 'BAA', 'BAB', 'BAC', 'BBA', 'BBB', 'BBC', 'BCA', 'BCB', 'BCC', 'CAA', 'CAB', 'CAC', 'CBA', 'CBB', 'CBC', 'CCA', 'CCB', 'CCC']

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

rawDescription: using python itertools to generate custom iteration

description: Using python itertools to generate custom iteration


code:
def build_cdf(distrib):
    cdf = []
    val = 0
    for key, freq in distrib.items():
        val += freq
        cdf.append((val, key))
    return (val, cdf)

rawDescription: Sampling keys due to their values

description: Sampling keys due to their values


rawDescription: How to find index of a character after space in python

description: Find index of a character after space in python


code:
def all_max(sequence, key):
    top_value = key(max(sequence, key=key))
    return [item for item in sequence if key(item) == top_value]

rawDescription: What is an elegant way to solve this max and min problem in Ruby or Python?


code:
import __builtin__
original_import = __builtin__.__import__
def import_hook(name, *args, **kwargs):
   if name == 'cStringIO': raise ImportError('test case module import failure')
   else: return original_import(name, *args, **kwargs)

__builtin__.__import__ = import_hook
somecode()

rawDescription: Trigger ImportError to test Coverage of Fallback Code


code:
for index, label in enumerate(self.labels):
    if index == self.__actual_choice:
        label.modify_font(self.__font_big)
    else:
        label.modify_font(self.__font_small)

rawDescription: Optimize python code for working with object names


code:
file_list = os.listdir(trainDir)
file_list.sort(key=lambda s: int(os.path.splitext(s)[0]))

rawDescription: read files according to their serial numbers and not names

description: Read files according to their serial numbers and not names


rawDescription: Functional masking of numpy string array in Python


code: writer.writerow(['Tmin']+Tmin) #note this is already printing the result to a file so dont "print" the return value

rawDescription: Writing to a CSV with python without brackets


code:
import serial

del serial.Serial.__del__

rawDescription: How do I override PySerial's "__del__" method in Python?

description: Override PySerial's "__del__" method in Python


code:
for k, v in sorted(somedictionary.iteritems()):
   doSomething(k, v)

rawDescription: in python, is there a one line pythonic way to get a list of keys from a dictionary in sorted order?

description: In python, is there a one line pythonic way to get a list of keys from a dictionary in sorted order


code:
import re
s = """cluster8032:  WoodR1|Wood_4286 Q8R1|EIK58010 F113|AEV64487.1 NFM421|PSEBR_a4327"""
results = re.findall('\|([^ ]*)',s) #list of ids that have been extracted from string
print('\n'.join(results)) #pretty output

rawDescription: How to copy specific data out of a file using python?

description: Copy specific data out of a file using python


code:
mask = np.zeros((height, width))
value = np.zeros((height, width))
for y in range(height):
    for x in range(width):
        if not point_is_in_mask(mask, (x, y)):
            value[y][x] = np.nan

rawDescription: how to mask the specific array data based on the shapefile

description: Mask the specific array data based on the shapefile


code: def some_plot(): return plot(x,y)

rawDescription: Is there a better way to re-use plots Matplotlib?


code:
for server in threadlist:
  ...

rawDescription: My first python program: can you tell me what I'm doing wrong?


code:
l = ['thing', 'foo', 'bar', 'doodad']
list(enumerate(l))

rawDescription: I don't understand the operation here: for index, item in enumerate(choices):


rawDescription: replace blanks in numpy array

description: Replace blanks in numpy array


rawDescription: Python Regex - Capturing a sentence based on the beginning and ending


code:
for value in json_obj.itervalues():
     temp_arr.append(value[0]["firstName"])

rawDescription: JSON object array parsing in django

description: JSON object array parsing in django


rawDescription: Python if statement condition using dictionary string


rawDescription: Grouping equal values from an array into subarrays


rawDescription: converting IP string to hex for dpkt.IP

description: Converting IP string to hex for dpkt.IP


code:
DEFAULT_LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'loggers': {
        'scrapy': {
            'level': 'DEBUG',
        },
        'twisted': {
            'level': 'ERROR',
        },
    }
}

rawDescription: Getting Scrapy Loggers

description: Getting Scrapy Loggers


rawDescription: How to iterate for specific elements in a list?

description: Iterate for specific elements in a list


code:
def foo_intensive(a=10, b=20, c=30, d=40):
    [i * j for i in range(a * b) for j in range(c * d)]

rawDescription: Why do argument-less function calls execute faster?


code:
from selenium import webdriver
ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

rawDescription: Selenium: understanding wait

description: Selenium: understanding wait


rawDescription: Merge two sorted iterators without replace


code:
def digitalSum(n):
    if n < 10 :
        return n
    return n % 10 + digitalSum( n // 10 )

rawDescription: Digital sum, Python

description: Digital sum, Python


rawDescription: Avoiding expensive calculations in Python IRC bot


code: square_list = square_list + start_list

rawDescription: Python, issue with for loop and List can't figure it out


code:
someBasicInfoPage= BasicInfoPage( ... )
m= MOS( ..., someBasicInfoPage )

rawDescription: Python-passing variable between classes

description: Python-passing variable between classes


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

step=1./13.
x=np.arange(0,14)*step
y=np.sin(2*np.pi*x)

fig,ax = plt.subplots()

ax.plot(x,y,'r*')

def fractions(x,pos):
    if np.isclose((x/step)%(1./step),0.):
        # x is an integer, so just return that
        return '{:.0f}'.format(x)
    else:
        # this returns a latex formatted fraction
        return '$\\frac{{{:2.0f}}}{{{:2.0f}}}$'.format(x/step,1./step)
        # if you don't want to use latex, you could use this commented
        # line, which formats the fraction as "1/13"
        ### return '{:2.0f}/{:2.0f}'.format(x/step,1./step)

ax.xaxis.set_major_locator(ticker.MultipleLocator(step))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fractions))

plt.show()

rawDescription: How to label the x axis in fraction numbers?

description: Label the x axis in fraction numbers


code:
class Create(CreateView):
  model = coremodels.Review
  template_name = 'location/test.html'
  form = SomeForm

rawDescription: 2 fields from different models in a form


code:
import pickle
termfile = open('terms.pkl', 'wb')
documentfile = open('documents.pkl', 'wb')
pickle.dump(term_frequency, termfile)
pickle.dump(document_frequency, documentfile)
termfile.close()
documentfile.close()

rawDescription: Python 2.7: Persisting search and indexing


code:
def fill(matrix, x, y, color1, color2):

    matWidth = len(matrix)
    matHeight = len(matrix[0])
    if x < 0 or y < 0 or x >= matWidth or y >= matHeight:
        return matrix

    if matrix[x][y] == color2 or matrix[x][y] != color1:
        return matrix
    if matrix[x][y] == color1:
        matrix[x][y] = color2

    fill(matrix, x - 1, y, color1, color2)
    fill(matrix, x + 1, y, color1, color2)
    fill(matrix, x, y - 1, color1, color2)
    fill(matrix, x, y + 1, color1, color2)

    return matrix

rawDescription: Need help to modify flood fill algorithm


code:
def test_rendering(self):
    slug = "hello_world"
    view = View(slug)
    request = DummyRequest([''])
    d = _render(view, request)
    def rendered(ignored):
        self.assertEquals(request.responseCode, 200)
        self.assertEquals("".join(request.written), "...")
        ...
    d.addCallback(rendered)
    return d

rawDescription: How to test twisted web resource with trial?

description: Test twisted web resource with trial


rawDescription: globbing a range with padded 0's- python

description: Globbing a range with padded 0's- python


code: split_data = ping_data.split(',')

rawDescription: Receive multiple values via pyserial and display in Python GUI


code: b, total_cost = year_calc(day_cost, days, total_days, sum,b,total_years)

rawDescription: Returning multiple values from function in python 3.5


code:
(df['Duration']
 .apply(lambda x: sum([a*int(b)
                       for a,b in zip([60., 1., 1./60],
                                      re.findall(r'\d+', x))]))
)

rawDescription: Python: converting Trip duration of h min sec and leave only minute count


code:
import b
b.some_func()

rawDescription: How do I import other scripts?

description: Import other scripts


code:
def flatten_traitobject(traitobject, *types):
    node_map = {}
    node_path = []
    def nodeRecursiveMap(traitobject, node_path):
        for key in traitobject.editable_traits():
            val = traitobject.get(key)[key]
            for type in types:
                if isinstance(val, types[0]):
                    node_map['.'.join(node_path + [key])] = val
            try:
                nodeRecursiveMap(val, node_path + [key])
            except (AttributeError, TypeError):
                pass
    nodeRecursiveMap(traitobject, node_path)
    return node_map

rawDescription: Enthought Traits.HasTraits class as a nested dictionary?


code:
for value in my_dic.values():
    del value[:]

rawDescription: clearing a dictionary but keeping the keys

description: Clearing a dictionary but keeping the keys


code:
hello = r'\n\
'
hello
len(hello)
list(hello)

rawDescription: I do not understand Python raw string

description: I do not understand Python raw string


code:
class BeginUpload(webapp2.RequestHandler):
    # do whatever you want
    upload_task = TestGCSReaderPipeline()
    upload_task.start()
    # do whatever you want

rawDescription: Upload content from a BIG CSV to CloudSQL using App Engine Python


code:
import os

def main(images, root='/local/mnt/workspace', target='wc3123'):
    for image in images:
        os.unlink(os.path.join(root, target, image))

if __name__ == '__main__':
    main(['test1.img', 'test2.img', 'test3.img'])

rawDescription: Removing images mentioned in a list

description: Removing images mentioned in a list


rawDescription: Python 3, increment iterator

description: Python 3, increment iterator


code:
import sys
# calculate and stuff
sys.exit(100)

rawDescription: Best way to return a value from a python script

description: Return a value from a python script


code:
import os
dirname = os.path.dirname(__file__)

STATIC_PATH = os.path.join(dirname, 'static')
TEMPLATE_PATH = os.path.join(dirname, 'templates')

rawDescription: is there a better way to handle index.html with Tornado?

description: Is there a better way to handle index.html with Tornado


code: specfun = lambda a,b: None

rawDescription: Mocking function argument names to work with inspect.getargspec


code:
def func1(A):
    return np.triu(A.T,1) + A

def func2(A):
    out = A.T + A
    idx = np.arange(A.shape[0])
    out[idx,idx] = A[idx,idx]
    return out

def func3(A):
    out = A.T + A
    np.fill_diagonal(out,np.diag(A))
    return out

def func4(A):
    out = A.T + A
    mask = np.eye(out.shape[0],dtype=bool)
    out[mask] = A[mask]
    return out

def func5(A):
    return np.where(np.eye(A.shape[0],dtype=bool),A,A.T+A)

def func6(A):
    return np.where(np.triu(np.ones(A.shape[0],dtype=bool),1),A.T,A)

rawDescription: Convert triangle matrix to square (Python | NumPy)?


code:
data = Data(query)
obj1 = Object(data, params1)
obj2 = Object(data, params2)

rawDescription: Sharing data between class objects

description: Sharing data between class objects


rawDescription: PyQt/Pyside - Create & Connect Dynamically - capturing function and arguments in lambda


code: User.profile = property(lambda self: self.get_profile())

rawDescription: How does passing a lambda function for a property() work when extending Django's user model?


code:
import tkinter as tk

def mimicClick( _button):
    _button.config(relief=tk.SUNKEN)
    _button.after(100, lambda :  _button.config(relief=tk.RAISED))

master = tk.Tk()
buttons = []
for i in range(10):
    b = tk.Button(master, text= i)
    b.grid(row = i // 3, column = i % 3, padx = 10, pady = 10)
    buttons.append(b)
    master.bind(str(i), lambda e, i = i: mimicClick(buttons[i]) )

master.mainloop()

rawDescription: Tkinter press button with command

description: Tkinter press button with command


code: num = raw_input(">> ").split() # remove the int cast

rawDescription: Python Calculator, No Answer

description: Python Calculator, No Answer


code:
if __name__ == '__main__':

    import sys
    from package import app
    sys.exit(app.run())

rawDescription: Generally speaking, how are (Python) projects structured?


code:
@bp.route('/<post_id>/<slug>')
@login_required
def post(post_id, slug):
    """ find the post and then show it """
    p = Post.query.get(post_id)
    return render_template("post/single_post.html", post=p)

rawDescription: add slugified title to url

description: Add slugified title to url


code:
from timeit import timeit
setup = 'import numpy as np; a=np.arange(0, 1000000)'
snippets = [
    'assert np.mean(a[a!=999999]) == 499999.0',
    'm=np.ma.masked_array(a,a==999999); assert np.ma.mean(m) == 499999.0',
    'assert sum(x for x in a if x != 999999)/999999 == 499999'
]
timings = [timeit(x, setup=setup, number=10) for x in snippets]
print('\n'.join(str(x) for x in timings))

rawDescription: How to calculate mean of values in the list omitting a special value?

description: Calculate mean of values in the list omitting a special value


code:
p = Popen(["ls", "/home/niklas/test"], stdin=PIPE, stdout=PIPE)
p.communicate()
p.returncode

rawDescription: Proper way of re-using and closing a subprocess object


code:
DBC = DBConnection('localhost',3306,'root','','sohbet')
con = DBC.connect_db()

rawDescription: Retrieving New Rows of MySQL Database without Restarting App in Python


rawDescription: How do I make variable values to be saved after running a Python script in PyCharm?

description: Make variable values to be saved after running a Python script in PyCharm


code:
t_df.head()
t_df.shape

rawDescription: Python Pandas: What is the fastest way to create a datetime index?


code: df.columns = ['col_' + ind for ind in range(1,len(df.columns)+1)]

rawDescription: How to automate renaming columns in Pandas

description: Automate renaming columns in Pandas


code:
from urllib.parse import urljoin
# or for python 2: from urlparse import urljoin
from flask import redirect

@app.endpoint('static')
def static(filename):
    static_url = app.config.get('STATIC_URL')

    if static_url:
        return redirect(urljoin(static_url, filename))

    return app.send_static_file(filename)

rawDescription: Serve static files from a CDN rather than Flask in production


code:
data = []
data.append(('delete', delete1))
data.append(('delete', delete2))
data.append(('delete', delete3))

output = '{%s}' % ',\n'.join(['"{}": {}'.format(action, json.dumps(dictionary)) for action, dictionary in data])

rawDescription: Python json encode with same keys (solr)


code:
ChildA.get_x_list()
ChildB.get_x_list()

rawDescription: Python: Class attribute which defaults to parent's value and is collectible for entire hierarchy


code:
import numpy

x = numpy.array([[1,2],[5,7]])
det_x = numpy.linalg.det(x)

#or

import numpy as np

x = np.array([[1,2],[5,7]])
det_x = np.linalg.det(x)

rawDescription: Python: How to use functions inside numpy?


rawDescription: Get parents directories of a file, up to a point


rawDescription: How to create an array of dimension n+1 given a function returning an array of dimension n

description: Create an array of dimension n+1 given a function returning an array of dimension n


code:
[u'http://www.fetise.com/apparel/shirts',
 u'http://www.fetise.com/apparel/tees',
 u'http://www.fetise.com/apparel/tops-and-tees',
 u'http://www.fetise.com/accessories/belts',
 u'http://www.fetise.com/accessories/cufflinks',
 u'http://www.fetise.com/accessories/jewellery',
 u'http://www.fetise.com/accessories/lighters',
 u'http://www.fetise.com/accessories/others',
 u'http://www.fetise.com/accessories/sunglasses',
 u'http://www.fetise.com/accessories/ties-cufflinks',
 u'http://www.fetise.com/accessories/wallets',
 u'http://www.fetise.com/accessories/watches',
 u'http://www.fetise.com/footwear/boots',
 u'http://www.fetise.com/footwear/casual',
 u'http://www.fetise.com/footwear/flats',
 u'http://www.fetise.com/footwear/heels',
 u'http://www.fetise.com/footwear/loafers',
 u'http://www.fetise.com/footwear/sandals',
 u'http://www.fetise.com/footwear/shoes',
 u'http://www.fetise.com/footwear/slippers',
 u'http://www.fetise.com/footwear/sports',
 u'http://www.fetise.com/innerwear/boxers',
 u'http://www.fetise.com/innerwear/briefs',
 u'http://www.fetise.com/personal-care/deos',
 u'http://www.fetise.com/personal-care/haircare',
 u'http://www.fetise.com/personal-care/perfumes',
 u'http://www.fetise.com/personal-care/personal-care',
 u'http://www.fetise.com/personal-care/shavers',
 u'http://www.fetise.com/apparel/tees/gifts-for-her',
 u'http://www.fetise.com/footwear/sandals/gifts-for-her',
 u'http://www.fetise.com/footwear/shoes/gifts-for-her',
 u'http://www.fetise.com/footwear/heels/gifts-for-her',
 u'http://www.fetise.com/footwear/flats/gifts-for-her',
 u'http://www.fetise.com/footwear/ballerinas/gifts-for-her',
 u'http://www.fetise.com/footwear/loafers/gifts-for-her',
 u'http://www.fetise.com/sale']

rawDescription: How to use scrapy request class

description: Use scrapy request class


code:
from pyspark.ml.feature import VectorAssembler

ignore = ['id', 'label', 'binomial_label']
assembler = VectorAssembler(
    inputCols=[x for x in df.columns if x not in ignore],
    outputCol='features')

assembler.transform(df)

rawDescription: Create feature vector programmatically in Spark ML / pyspark


code:
iterator = (item for item in [[1, 2, 3], [2, 3, 4, 5]])
df = pd.DataFrame.from_records(iterator,
                               columns=list('abcd'))
print(df)
#    a  b  c   d
# 0  1  2  3 NaN
# 1  2  3  4   5

rawDescription: Combine Python Pandas Dataframe more Efficiently than List Append Method


code:
from operator import itemgetter
wlist2 = [(word, float(time_dict[start[1:]]), float(time_dict[end[1:]])) for word, start, end in w_list]
wlist2.sort(key = itemgetter(1))

rawDescription: Extracting (interval) values from multiple dictionaries inside one large list, and combining these with corresponding lists inside another large list


code:
from traitlets import CInt, link
class Counter(widgets.DOMWidget):
    value = CInt(0, sync=True)

rawDescription: Linking ipython widget button and slider values


code:
__file__ = "game_folder"    # <-- This code is needed for CX_freeze, to avoid NameError.
file_path = os.path.join(dirname(__file__), "images", "energy.png")

rawDescription: Python 3.3: Unable to load an image using Pygame if game folder is not in C drive


rawDescription: Python, remove specific columns from file


code: 0in map(print,range(10))  # tokenizer is fine with no space between 0 and in

rawDescription: Shortest code for depleting a generator in code golfing


code:
class MyThread(Thread):
    def __init__(self, command, template, env, build_flavor, logger, func=None):
        Thread.__init__(self)
        self.command = command
        self.template = template
        self.env = env
        self.build_flavor = build_flavor
        self.logger = logger
        self.func = func

    def run(self):
        self.logger.info('Running (%s)...this may take several minutes. Please be patient' % self.build_flavor)
        run_command(self.command, self.template, self.env)
        self.logger.info('Complete (%s)' % self.build_flavor)
        # call func if it is there
        if self.func:
            self.func()
        return

rawDescription: Python threading: monitoring threads and executing additional code after threads complete


code:
import os
import re

def fix_pys(path, pattern, repl):
    for root, dirs, files in os.walk(path):
        for f in files:
            name, ext = os.path.splitext(f)
            if ext == '.py':
                with open(os.path.join(root, f), 'r+') as py:
                    new_py = re.sub(pattern, repl, py.read())
                    py.seek(0)
                    py.write(new_py)
                    py.truncate()

rawDescription: creating your own python code fixer (not debugger, although it could be used for that)

description: Creating your own python code fixer (not debugger, although it could be used for that)


code:
df2.index = [str(i) + '_'+ str(j) for i, j in df2.index]
df2

rawDescription: Pandas stacking DataFrame and concatenating name of columns with index


rawDescription: How do I get another field from a Pandas Group By?

description: Get another field from a Pandas Group By


code:
import datetime
datetime.datetime.strptime('10/05/2012', '%d/%m/%Y').strftime('%Y-%m-%d')

rawDescription: Convert date format python

description: Convert date format python


code:
parameters = Schema({
    Required('zip'): Coerce(int),
})

rawDescription: How to validate URL parameters in Flask app?

description: Validate URL parameters in Flask app


code: aap1_10_years.loc[aap1_10_years['event'] == 1, ['Close', 'event']]

rawDescription: How can i do calculations on subsets , Panda's way, without looping

description: Do calculations on subsets , Panda's way, without looping


code: erosion = scipy.ndimage.binary_erosion(land, structure=np.ones((3,3)))

rawDescription: Isolating coastal grids using convolving window (python)


code: pprint(xyz)

rawDescription: dictionary that contains list

description: Dictionary that contains list


rawDescription: removing "()" using python

description: Removing "()" using python


rawDescription: Python name variable from string

description: Python name variable from string


code:
def has_prop1(cls):
    @property
    def prop1(self):
        return self._prop1

    cls.prop1 = prop1

    return cls

@has_prop1
class A(object):
    pass

rawDescription: How to share attributes without an is-a relationship in Python?

description: Share attributes without an is-a relationship in Python


code:
urlpatterns = patterns("myproject.myapp.views",
    (r"^jobs/(?P<id>\d+)/edit/$",    "myproject.myapp.views.edit"),
    (r"^jobs/(?P<id>\d+)/delete/$",  "myproject.myapp.views.delete"),
    (r"^jobs/(?P<id>\d+)/update/$",  "myproject.myapp.views.update"),
    (r"^jobs/insert/$",              "myproject.myapp.views.insert"),
)

urlpatterns += patterns("",
    (r"^blog/(?P<object_id>\d+)/$",  "django.views.generic.list_detail.object_detail", info_dict, "NOIDEA-detail"),
    (r"^blog/$",                    "django.views.generic.list_detail.object_list",   info_dict, "NOIDEA-community"),
)

rawDescription: How to use one app to satisfy multiple URLs in Django

description: Use one app to satisfy multiple URLs in Django


rawDescription: Put this request with Python httplib

description: Put this request with Python httplib


code:
while len(prime) < 10:
    for z in odd_list:
        ...

rawDescription: Cannot get this loop to stop

description: Cannot get this loop to stop


code:
print(repr(binary_split_array[0]))
''

rawDescription: join function of a numpy array composed of string

description: Join function of a numpy array composed of string


code: help(string) # on Python 3

rawDescription: Alphabet range python

description: Alphabet range python


code:
locx /= locx.length()
locy /= locy.length()

rawDescription: Getting local 2D coordinates of vertices of a planar polygon in 3D space


code:
playlist = Playlist.objects.get(id=playlist_id)

if request.method == 'POST':
    form = PlaylistEditForm(request.POST, instance=playlist)
    if form.is_valid():
        form.save()

rawDescription: How to change data in Django ModelForm?

description: Change data in Django ModelForm


rawDescription: Pandas: slow date conversion

description: Pandas: slow date conversion


code: df.xs('white', level='colour')

rawDescription: Pandas `DataFrameGroupBy` and `SeriesGroupBy`


code:
for tag in soup():
    for attribute in ["class", "id", "name", "style"]:
        del tag[attribute]

rawDescription: Remove all inline styles using BeautifulSoup


code:
to_db = []
for i in range(10):
    to_db.append((d.entries[i].title, d.entries[i].date))

import MySQLdb
conn = MySQLdb.connect(host="localhost",user="me",passwd="pw",db="mydb")
c = conn.cursor()
c.executemany("INSERT INTO mytable (title, date) VALUES (%s, %s)", to_db)

rawDescription: combine lines from 2 prints to single line and insert into mysql database

description: Combine lines from 2 prints to single line and insert into mysql database


code:
from collections import Counter
intersection = Counter( [2,3,3,4,4,5] ) & Counter( [0, 2, 2, 3, 3, 4, 6, 8] )
intersection

rawDescription: Fastest way to compare ordered lists and count common elements *including* duplicates


code:
import urllib2
import json

data = []

f1 = open('WordsInCategory.text', 'w')

url1 ='https://en.wikipedia.org/w/api.php?\
action=query&format=json&list=categorymembers\
&cmtype=page&cmtitle=Category:Geography&cmlimit=100'

json_obj = urllib2.urlopen(url1)
data1 = json.load(json_obj)

for item in data1['query']:
    for i in data1['query']['categorymembers']:
        data.append(i['title'].encode('utf8')+"\n")

# Do additional requests, and append the new titles to the data array

f1.write(''.join(set(data)))

f1.close()

rawDescription: How to prevent writing into txt file the same words using open(text.txt,a)?

description: Prevent writing into txt file the same words using open(text.txt,a)


code: sum(len(i) > 20 for i in s)

rawDescription: Python 2.7 counting number of strings

description: Python 2.7 counting number of strings


code: dict((x, a.get(x, 0) + b.get(x, 0)) for x in set(a)|set(b))

rawDescription: combining two python dictionaries into one when the net values are not positive

description: Combining two python dictionaries into one when the net values are not positive


code:
name = 'alabama'
vowels = {'a', 'e', 'i', 'o', 'u'}
nick = name[0] + ''.join(l for l in name[1:] if l not in vowels)
nick

rawDescription: Best practice: prepopulate SlugField in Django (without Admin module)


code:
global d
d = someClass()
def application(environ, start_response):
    # get the instruction
    password = somethingfromwsgi # read the tutorials on WSGI to get the post stuff
    # figure out WHAT to do
    global d
    success = d.doSomething()
    # or success = funccall()
    prog = pexpect.spawn('python someprogram.py')
    prog.expect("Password: ")
    prog.sendline(password)
    i = prog.expect(["OK","not OK", "error"])
    if i==0:
        start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
        return "Success"
    elif i==1:
        start_response("500 Internal Server Error", [("Content-Type", "text/html; charset=utf-8")])
        return "Failure"
    elif i==2:
        start_response("500 Internal Server Error", [("Content-Type", "text/html; charset=utf-8")])
        return "Error"

rawDescription: How to set up Python server side with javascript client side

description: Set up Python server side with javascript client side


code:
pattern = re.compile(r"\[\[([\w ]+)(?:\|[\w ]+)?\]\]")
text = "[[a|b]] fdkjf [[c|d]] fjdsj [[efg]]"
results = pattern.findall(text)

# outputs ['a', 'c', 'efg']

rawDescription: Python regex for finding contents of MediaWiki markup links


code:
import csv

with open('input.txt', 'r') as f_input, open('output.txt', 'w', newline='') as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)
    csv_output.writerows([cols[1], cols[13], cols[15]] for cols in csv_input)

rawDescription: Extract columns from file to another file


code:
x = gmpy2.mpz(123456)
bin(x)
n = 0
while True:
    n = gmpy2.bit_scan1(x, n)
    if n is None:
        break
    print(n)
    n = n + 1

rawDescription: Efficiently convert gmpy2.mpz to numpy boolean array


rawDescription: Slice only working on the last line in a file using 'with open' and 'readline'


code:
class CustomMetaClass(type):

      def __init__(cls, classname, bases, attrs):
          super(CustomMetaClass, cls).__init__(classname, bases, attrs)

          cls.mapping['key'] = cls.get_choices()

class Custom(metaclass = CustomMetaClass): # assuming you are using python 3

      mapping = {}

      @classmethod
      def get_choices(cls):
          # add your custom code here
          pass

rawDescription: In Python, how to initialize a class attribute from the return value of a class method?


code:
from collections import OrderedDict

od = OrderedDict()
import csv
with open("in.csv") as f,open("new.csv" ,"w") as out:
    r = csv.reader(f)
    wr= csv.writer(out)
    header = next(r)
    for row in r:
        loc,rest = row[0], row[1:]
        od.setdefault(loc, []).extend(rest)
    wr.writerow(header)
    for loc,vals in od.items():
        wr.writerow([loc]+vals)

rawDescription: Python: Reading thru CSV file, finding columns with same value and appending their other column values


code:
import random
from collections import namedtuple, defaultdict
from operator import attrgetter

Song = namedtuple('Song', ('artist', 'title', 'length'))

# the length is not correct
Mozart_1 = Song('Mozart', 'Don Giovanni', 3.5)
Mozart_2 = Song('Mozart', 'Serenata Notturna', 2.98)
Mozart_3 = Song('Mozart', 'Violin Concerto No. 3 in G, 1st Movement', 8.43)
Bach_1 = Song('Bach', 'Air', 6.18)
Bach_2 = Song('Bach', 'Toccata in D Minor', 12.44)
Beethoven_1 = Song('Beethoven', 'Für Elise', 2.47)

playlist = [Beethoven_1, Mozart_3, Bach_1, Mozart_2, Mozart_1, Bach_2] # unsorted

# one possible optimum
# OPTIMUM = [Mozart_1, Bach_1, Mozart_2, Beethoven_1, Mozart_3, Bach_2]

def optimize(items, quality_function, stop=1000, randrange=random.randrange):
    length = len(items)
    no_improvement = 0
    best = 0
    while no_improvement < stop:
        i = randrange(length)
        j = randrange(length)
        copy = items[:]
        copy[i], copy[j] = copy[j], copy[i]

        q = quality_function(copy)
        if q > best:
            items, best = copy, q
            no_improvement = 0
        else:
            no_improvement += 1
    return items

def quality_maxmindist(items):
    s = 0
    for item in set(items):
        indcs = [i for i in range(len(items)) if items[i] == item]
        if len(indcs) > 1:
            s += sum(1. / (indcs[i+1] - indcs[i]) for i in range(len(indcs)-1))
    return 1. / s

def spread_equal_items_apart(items, key, stop=optimize.__defaults__[0]):
    keys, key_to_items = keys_and_key_to_items_dict(items, key)
    keys = optimize(keys, quality_maxmindist)
    re = []
    for k in keys:
        re.append(key_to_items[k].pop())
    return re

def keys_and_key_to_items_dict(items, key):
    key_to_items = defaultdict(list)
    keys = []
    for i in items:
        k = key(i)
        keys.append(k)
        key_to_items[k].append(i)
    return keys, key_to_items

if __name__ == '__main__':
    new = spread_equal_items_apart(playlist, attrgetter('artist'))
    print(new)

rawDescription: Arrange elements in a list so that similar elements are farthest apart


code:
import module  # The module which contains the call to input

class TestClass:

    def test_function_1(self):
        # Override the Python built-in input method
        module.input = lambda: 'some_input'
        # Call the function you would like to test (which uses input)
        output = module.function()
        assert output == 'expected_output'

    def test_function_2(self):
        module.input = lambda: 'some_other_input'
        output = module.function()
        assert output == 'another_expected_output'

    def teardown_method(self, method):
        # This method is being called after each test case, and it will revert input back to original function
        module.input = input

rawDescription: Pytest: How to test a function with input call?


code:
f = open('fileToWriteTo', 'wb')
bitstreamObject.tofile(f)

rawDescription: Save a bitstream to a file using python

description: Save a bitstream to a file using python


code:
import pprint

# Define the keywords I want to see first
preferred_projects = ['one', 'two', 'three']

# example data
AllMyProjectsFromaDatasource = [{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
                                { 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
                                { 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
                                { 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},
                                { 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}
                               ]

def keyfunc(x):
    # keyword primary key
    # (add index to list comprehension when keyword is in name)
    preferred_key = [float(idx)
                     for idx, i in enumerate(preferred_projects)
                     if i in x['name']]
    # found at least one match in preferred keywords, use first if any, else infinity
    keyword_sortkey = preferred_key[0] if preferred_key else float('inf')
    # return tuple to sort according to primary and secondary key
    return keyword_sortkey, x['name']

AllMyProjectsFromaDatasource.sort(key=keyfunc)

pprint.pprint(AllMyProjectsFromaDatasource)

rawDescription: Sorting list of dictionaries with primary key from list of keywords and alphabetical order as secondary key


code:
import time, wsgiref.handlers

class MyApplication(object):
    def __call__(self, environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
        return self.page()

    def page(self):
        yield '<html><body>'
        for i in range(10):
            yield '<div>%i</div>'%i
            time.sleep(1)

application= MyApplication()
if __name__=='__main__':
    wsgiref.handlers.CGIHandler().run(application)

rawDescription: Display the result on the webpage as soon as the data is available at server


code:
import sys, os
base = os.path.dirname(__file__)
sys.path.append(os.path.join(base, 'lib'))

rawDescription: Setting up multiple modules python appengine


code: res

rawDescription: list comprehension with np.where to compare two arrays and combine them it first entry is equal

description: List comprehension with np.where to compare two arrays and combine them it first entry is equal


code:
str = "mazda mazda6 mazda 6 mazda3 mazda2"

result = re.sub(r'\bmazda(\d+)', r'\1', str)

rawDescription: Regex search and replace pattern with modified pattern - Python


rawDescription: Separating a string using predefined characters


rawDescription: Multiple data histogram in python

description: Multiple data histogram in python


code:
class MyForm(forms.ModelForm):
    class Meta():
        model=Subgroup

rawDescription: django form with select fields

description: Django form with select fields


code:
from wand.image import Image
from wand.api import library

with Image() as new_gif:
    # Tell new gif how to manage background
    library.MagickSetOption(new_gif.wand, 'dispose', 'background')
    for img_path in input_images:
        library.MagickReadImage(new_gif.wand, img_path)
    new_gif.save(filename=output_path)

rawDescription: wand: How to assemble transparent gif / clear background each frame

description: Wand: How to assemble transparent gif / clear background each frame


code: global_objs = list(globals().items())

rawDescription: Python Instantiate All Classes Within a Module


code:
class MyStreamer(TwythonStreamer):

    # the init function is called when you create instance of class
    def __init__(self):
        self.stop_time = datetime.datetime.now() + datetime.timedelta(hours=1)

    # ...

    def on_success(self, data):
        if datetime.datetime.now() > self.stop_time:
            raise Exception("Time expired")

        # ...

rawDescription: How to have a function exit after 1 hour in Python

description: Have a function exit after 1 hour in Python


code:
import csv
from collections import defaultdict
sample = '''\
Sender A,Receiver G,43,Info...
Sender H,Receiver R,43,Info...
Sender A,Receiver G,27,Info...
Sender N,Receiver Z,43,Info...
Sender A,Receiver G,1367,Info...
Sender R,Receiver P,43,Info...
Sender A,Receiver G,43,Info...
Sender H,Receiver R,111,Info...
'''.splitlines(True)
seen = defaultdict(list)
reader = csv.reader(sample)
for row in reader:
    key = (row[0], row[1], row[2])  # sender, receiver, id
    seen[key].append(row)

import sys
writer = csv.writer(sys.stdout)
for collected in seen.values():
    if len(collected) > 1:
        writer.writerows(collected)

rawDescription: Python - order columns and compare rows in csv


code:
player1_name = raw_input('Player1 please enter your name: ')
player2_name = raw_input('Player2 please enter your name: ')

rawDescription: Python Rock/Paper/Scissors with name input


rawDescription: boost python, using a namespace other than main global

description: Boost python, using a namespace other than main global


code:
def DerivedMock():
    mock = BaseMock()

    # Set Mock private attributes to Derived
    mock._mock_methods.extend(dir(Derived))
    mock._mock_name = 'DerivedMock'
    mock._spec_class = Derived

    mock.three.return_value = 'tres'

    # Defining DerivedMock.four would cause a spec error
    # mock.four.return_value = 'quatro'

    return mock

rawDescription: How to mock a class hierarchy

description: Mock a class hierarchy


rawDescription: Updating a 2D database (CSV) with other databases in Python?


code:
def saythread(engine, lock, location, text):
    with lock:
        engine.say(text)
        engine.runAndWait()

engine = pyttsx.init()
lock = threading.Lock()
t = threading.Thread(
        target=saythread,
        args=(engine, lock, 1, "here we go"),
        name='sayitthread')
t.start()

rawDescription: Scoping rules and threads

description: Scoping rules and threads


rawDescription: Preceding Word Length

description: Preceding Word Length


code:
# stuff...
stdout=stdout.strip()
stdout=stdout.split('\n')

rawDescription: Child thread doesn`t really spawn or has some issue in python?


code: phi = symbols('phi')

rawDescription: Exact user defined expressions in Sympy derivatives


code: indices = [i for i, c in enumerate(secret) if c == guess]

rawDescription: I need help fixing this hangman function


rawDescription: Null a dictionary inside a function in python


rawDescription: In python, can you pass variadic arguments after named parameters?


code:
import simpy
import simpy.util

def simulation_run(env):
    print("Starting a run at time [%i]" % env.now)
    yield env.timeout(5)
    print("Ending a run at time [%i]" % env.now)

if __name__ == '__main__':
    env = simpy.Environment()
    simpy.util.start_delayed(env, simulation_run(env), delay=5)
    print("Starting simulation.")
    env.run(20)
    print("End of simulation.")

rawDescription: Starting a simpy simulation run using a callback


rawDescription: How do I delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted?

description: Delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted


code:
class ActiveCheckerExtension(jinja2.ext.Extension):
    """
    This will give us a {% check_active %} tag.
    """

    template = 'Active is : %s'
    tags = set(['check_active'])

    def _render_tag(self, context, caller):
        return jinja2.Markup(self.template % unicode(context['active']))

    def parse(self, parser):
        ctx_ref = jinja2.nodes.ContextReference()
        lineno = next(parser.stream).lineno
        node = self.call_method('_render_tag', [ctx_ref], lineno=lineno)
        return jinja2.nodes.CallBlock(node, [], [], [], lineno=lineno)

rawDescription: How can a custom jinja2 tag interface with the context of a flask request


code:
class Sttlmnt_typ_Master(models.Model):
    symbol = models.CharField(max_length=10, editable=False)
    symbol_name =  models.CharField(max_length=35, editable=False)

    def __unicode__(self):
        return self.symbol_name

class Auction(models.Model):
    dates = models.DateTimeField(editable=False, null=True)
    sr_no = models.IntegerField(editable=False, null=True)
    symbol = models.ForeignKey(Sttlmnt_typ_Master, editable=False)
    series = models.CharField(max_length=35, editable=False, null=True)
    qty = models.IntegerField(editable=False, null=True)

rawDescription: Django foreign key in model

description: Django foreign key in model


code: temp = list[:]

rawDescription: Python: manipulating list in place

description: Python: manipulating list in place


code:
proc = subprocess.Popen(a1.split())
# code will proceed

# use proc.communicate later on

rawDescription: Use subprocess in Python

description: Use subprocess in Python


code: myButton = wx.Button(self.bitmap2, -1, size =(30,30), pos=(20,20))

rawDescription: Avoiding unnecessary memory consumption in Python and wxPython


code: new_list = [item for item in lst if not any(char.isdigit() for char in item)]

rawDescription: Python: Issues with "if not any"

description: Python: Issues with "if not any"


code: uk_users = User.objects(country='uk')

rawDescription: Grabbing multiple documents with flask-mongoengine


code:
import bge
from random import random

def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner
    scene = bge.logic.getCurrentScene()

    #Your list
    objlist = [obj for obj in scene.objects]
    entity = objlist[obj]
    add = scene.addObject(obj,loc,0)
    add(entity,' location',0)

rawDescription: How to access objects from specific layer in blender game engine

description: Access objects from specific layer in blender game engine


code:
import numpy as np

data = [[(1, 2), (3, 4)],
        [(5, 6), (7, 8)]]

data = np.array(data)

x = data[:,:,0]
velocity = data[:,:,1]

rawDescription: How do I plot series of points on rows in matplotlib?

description: Plot series of points on rows in matplotlib


code:
over_threshold = hist > 10

fig = plt.figure()
s = fig.add_subplot(1, 1, 1)
s.set_xlim(-4, 4)
s.set_ylim(-4, 4)
s.imshow(
    over_threshold, interpolation='nearest',
    extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],
    cmap=matplotlib.cm.viridis)
fig.savefig('t3.png')

area = over_threshold.sum() * (xedges[1] - xedges[0]) * (yedges[1] - yedges[0])
print(area)

rawDescription: How to calculate python 2D scatter points occupied area

description: Calculate python 2D scatter points occupied area


code: [ set(tuple(x) for x in d[k]) for k in d ]

rawDescription: Intersection of subelements of multiple list of lists in dictionary


code:
from tkinter import *
from random import randint
def roll():
    out['text'] = str(randint(1, 6))
window = Tk()
out = Label(window, text = ' ')
button = Button(window, text='Press to roll!', command=roll)
button.pack()
out.pack()

rawDescription: height not working python (3.4)

description: Height not working python (3.4)


rawDescription: PyQt4: why do we need to pass class name in call to super()


code:
[{'sellPrice': 209, 'meanPrice': 378, 'buyPrice': 224},
    {'sellPrice': 210, 'meanPrice': 378, 'buyPrice': 224}]

rawDescription: Retrieve multiple values from JSON

description: Retrieve multiple values from JSON


rawDescription: I am using Python and I would like to find the maximum value(s) and position(s) of that(those) value(s) in a multidimensional list


rawDescription: Python list as a list indices

description: Python list as a list indices


code: a, b, c = t

rawDescription: What's going on in this code?

description: What's going on in this code


code:
somedict = { "item"     : [ x[0] for x in data ],
             "settings" : { "axisx" : [ x[1] for x in data ],
                            "axisy" : [ 0, 100],
                            "colour" : "ff9900" }
           }

rawDescription: Python Tuple to JSON output

description: Python Tuple to JSON output


City: NewYork

FirstName: John

LastName: Smith

CarName: Toyota BMW

DogName: Spike Rusty

City: Sydney

FirstName: Ben

LastName: Swan

CarName: Volkswagen Ford Audi

DogName: Buddy Max

City: London

FirstName: Julia

LastName: Brown

rawDescription: Python csv to nested JSON

description: Python csv to nested JSON


code: df.pivot_table(values="incoming", index=pd.DatetimeIndex(df.date).to_period("M"), columns="goods", aggfunc="sum")

rawDescription: Filling gaps for cumulative sum with Pandas


code:
def my_view(request):
    f = Foo.objects.get(id=1234)
    extra_attribute = attr_value
    return render(request, 'my_template.html',
                  {'foo_instance': f, 'extra_attribute': attr_value})

rawDescription: Django model object with dynamically added attributes


rawDescription: Efficiently page query results

description: Efficiently page query results


code:
import numpy as np
import numba as nb
import numpy

def func(re, ws, a, l, r):

    for x1 in range(a**l):
        for x2 in range(a**l):
            for x3 in range(a**l):
                f11 = 0
                cv1 = numpy.ndarray.sum(
                numpy.absolute(numpy.subtract(ws[x1], ws[x2])))
                cv2 = numpy.ndarray.sum(
                numpy.absolute(numpy.subtract(ws[x1], ws[x3])))
                if cv1 == 0:
                    f11 += 1
                if cv2 == 0:
                    f11 += 1
                re[x1][x2][x3] = 1.0*r/(a**l-2)*(numpy.product(numpy.absolute(
                            numpy.subtract((2*ws[x1]+ws[x2]+ws[x3]), 2)))-f11)
                f11 *= 1.0*(1-r)/2
                re[x1][x2][x3] += f11

rawDescription: Performance issue in python with nested loop


code:
from nltk.corpus import cmudict # $ pip install nltk
# $ python -c "import nltk; nltk.download('cmudict')"

def to_piglatin(word, pronunciations=cmudict.dict()):
    word = word.lower() #NOTE: ignore Unicode casefold
    i = 0
    # find out whether the word start with a vowel sound using
    # the pronunciations dictionary
    for syllables in pronunciations.get(word, []):
        for i, syl in enumerate(syllables):
            isvowel = syl[-1].isdigit()
            if isvowel:
                break
        else: # no vowels
            assert 0
        if i == 0: # starts with a vowel
            return word + "way"
        elif "y" in word: # allow 'y' as a vowel for known words
            return to_piglatin_naive(word, vowels="aeiouy", start=i)
        break # use only the first pronunciation
    return to_piglatin_naive(word, start=i)

def to_piglatin_naive(word, vowels="aeiou", start=0):
    word = word.lower()
    i = 0
    for i, c in enumerate(word[start:], start=start):
        if c in vowels:
            break
    else: # no vowel in the word
        i += 1
    return word[i:] + word[:i] + "w"*(i == 0) + "ay"*word.isalnum()

rawDescription: Pig Latin Translator

description: Pig Latin Translator