code:
# figure set up
fig, ax_lst = plt.subplots(2, 1)
ax_lst = ax_lst.ravel()

#fake data
data = rand(512, 512)
x = np.linspace(0, 5, 512)
X, Y = meshgrid(x, x)

data2 = np.sin(X ** 2 + Y **2)
# plot the first time#fake data

im = ax_lst[0].imshow(data, interpolation='nearest',
                            origin='bottom',
                            aspect='auto', # get rid of this to have equal aspect
                            vmin=np.min(data),
                            vmax=np.max(data),
                            cmap='jet')

cb = plt.colorbar(im)

pc = ax_lst[1].pcolor(data)
cb2 = plt.colorbar(pc)

rawDescription: How to update pcolor in matplotlib?

description: Update pcolor in matplotlib


rawDescription: Identify groups of continuous numbers in a list


code:
[True, False, True, True, None]
[True, False, False, True, None]
[True, False, None, True, True]
[True, False, None, True, False]

rawDescription: Generate Truth Table but leave some variables undefined in python


code: {'ActiveState Komodo Edit': 0, '.Net Framework': 0, 'ACT DCP': 0, 'ActivePerl': 0, 'Adobe Flash Player': 1, '7-Zip': 1, 'ActiveTCL': 0}

rawDescription: python - find occurrences of strings from a reference file within an input file

description: Python - find occurrences of strings from a reference file within an input file


rawDescription: Convert from string to tuple with "\" in python


code:
def c(self):
    return self.a + self.b
property(c)

rawDescription: Dynamically update attributes of an object that depend on the state of other attributes of same object


rawDescription: How can I train a Genetic Programming algorithm onto a variable sequence of descriptors?

description: Train a Genetic Programming algorithm onto a variable sequence of descriptors


code:
from threading import Thread
from collections import deque
import time

class Pool():

    def __init__(self, n_threads):
        self.n_threads = n_threads

    def map(self, func, data):
        inqueue = deque(data)
        result = deque()

        workers = [Worker(inqueue, result, func) for i in range(self.n_threads)]

        for worker in workers:
            worker.start()

        for worker in workers:
            worker.join()

        return list(result)

class Worker(Thread):

    def __init__(self, inqueue, outqueue, func):
        '''
        A worker that calls func on objects in inqueue and
        pushes the result into outqueue

        runs until inqueue is empty
        '''

        self.inqueue = inqueue
        self.outqueue = outqueue
        self.func = func
        super().__init__()

    # override the run method, this is starte when
    # you call worker.start()
    def run(self):
        while self.inqueue:
            data = self.inqueue.popleft()
            print('start')
            result = self.func(data)
            self.outqueue.append(result)
            print('finished')

def test(x):
    time.sleep(x)
    return 2 * x

if __name__ == '__main__':
    data = 12 * [1, ]

    pool = Pool(6)
    result = pool.map(test, data)

    print(result)

rawDescription: How to process input in parallel with python, but without processes?

description: Process input in parallel with python, but without processes


code:
lis1=map(list,s)
lis1

rawDescription: Store each character of a String in a list


rawDescription: Search between two files in Python

description: Search between two files in Python


code:
from dateutil import parser
import re
input_ls = ['Nov-2015', 'Quarter 1, 2016', 'Jan-2016', 'Dec-2015','Feb-2016', 'Quarter 4, 2015']
res = []
for x in input_ls:
    #Match x if it is having a digit followed by a comma followed by a space and followed by four digits
    qtr = re.findall('(\d),\s(\d{4})',x)
    #If match not found parse it as date by prefixing '1,' to the string
    if len(qtr) == 0:
        res.append(parse('1,' + x))
    #If matching then format it to a string format that is acceptable to the dateutil parser
    else:
        res.append(parse(str(int(qtr[0][0])*3)+'-30-'+qtr[0][1]))
#Sort the output list
out = zip(res,input_ls)
out.sort()
#Reverse it
out.reverse()

rawDescription: Custom sorting a list in python

description: Custom sorting a list in python


rawDescription: Unable to fetch <div> tag values in python


code:
for i in range(1, 10):
    print("i =", i, ":", end=" ")
    for j in range(1, 10):
        print("{:2d}".format(i * j), end=" ")
    print()

rawDescription: Multiplication table for double digit numbers using nested loops in Python


code:
class MessagesUpdateThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.exiting = False

    def __del__(self):
        self.exiting = True
        self.wait()

    def run(self):
        while not self.exiting:
            self.emit(QtCore.SIGNAL("updateMessages()"))
            self.sleep(1)

rawDescription: Message displaying in QListWidget/View on boolean condition


code:
def toNumber(s):
    if not s:
        return 0
    first = s[0]
    value = int(first) if first.isdigit() else 0
    return value + toNumber(s[1:])
toNumber('ab4h5h6')

rawDescription: Python using recursion to take a mixed string and add up the integers in the string


code:
timer = QtCore.QTimer()
timer.start(1000)
timer.timeout.connect(updateAllWidgets)

rawDescription: PySide and Qt Properties: Connecting signals from Python to QML


code:
numpy.log(data)
numpy.exp(x)

rawDescription: Which Python module is suitable for data manipulation in a list?


code:
for i in seq:
  seqDic[i] = 1 + seqDic.get(i, 0)

rawDescription: Learning Python and using dictionaries

description: Learning Python and using dictionaries


code:
import importlib
from A import B
importlib.import_module('A.B.C')
importlib.import_module('A.B.D')
# Now you can use B.C & B.D

rawDescription: "from A import B.C" in Python?

description: "from A import B.C" in Python


code:
Manufacturer #:
PAW11295

rawDescription: python beautifulsoup extract text

description: Python beautifulsoup extract text


code:
# heart of the update() loop in Python 2:
for elem, count in iterable.iteritems():
    self[elem] = self_get(elem, 0) + count

# heart of the __add__() loop in Python 2:
result = Counter()
for elem, count in self.items():
    newcount = count + other[elem]
    if newcount > 0:
        result[elem] = newcount
for elem, count in other.items():
    if elem not in self and count > 0:
        result[elem] = count
return result

rawDescription: Which is faster Counter()+=Counter or Counter.update(Counter)?


code:
im = np.zeros((5,5,3), dtype=np.uint8)
im2 = im.copy()

# Change a pixel in the blue band:
im2[0,0,2] = 255

# The transpose here is just so that the bands are printed individually
print (im == im2).T

rawDescription: Opencv: How to detect pixel color change


code:
df = pd.DataFrame(np.random.randn(4,4), columns=list('ABCD'))
df
df.mean()
df.mean().order()

rawDescription: Pandas: Sorting columns by their mean value


code: x, y = (os.getpid(param) for param in ("process1", "process2"))

rawDescription: Dynamic method in python

description: Dynamic method in python


code:
import csv

new_rows = []
with open('csvpatpos.csv','r') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
        row = [ x.zfill(4) for x in row ]
        new_rows.append(row)

with open('csvpatpos.csv','wb') as f:
    csv_f = csv.writer(f)
    csv_f.writerows(new_rows)

rawDescription: python: adding a zero if my value is less then 3 digits long

description: Python: adding a zero if my value is less then 3 digits long


code:
obj = apps.get_model(app_label=app_name, model_name=model_name)
#obj = locate('LessonApp.models.%s' % otype) # old way of locating
try:
    search_list = list(obj.objects.\
                       exclude(Q(**{field: ''}) | Q(**{'{0}__isnull'.format(field): True})).\
                       values_list(field, flat=True).distinct().order_by(field))
    if q:
        search_list = filter(lambda s: s[:len(q)].lower() == q.lower(), search_list)  # search the list for q, ignore case
    return HttpResponse(json.dumps(search_list), content_type='application/json')
except Exception as ex:
    return HttpResponse('{"result": "error", "error": "DB Error\n%s"}' % ex, content_type='application/json')

rawDescription: Access data from autocomplete search form


code:
yourString = """MY_FILE = E:\test.jpg

BAND_SUBSET = {}

SPATIAL_SUBSET1 = {}
SPATIAL_SUBSET2 = {}

PARA1 = {}

END = END"""

rawDescription: Writing a text file using python

description: Writing a text file using python


code: result = "Greater" if 3 > 2 else "Smaller"

rawDescription: I cannot understand function comparisons in Python


code:
import numpy as np
coins = 800000
slots = 900000

coin_positions = np.random.randint(slots, size=coins)

coins_per_slot = np.histogram(coin_positions, bins=np.arange(slots + 1))[0]

rawDescription: How to vectorize simulation of distributing coins in numpy?

description: Vectorize simulation of distributing coins in numpy


code:
print (num_list[num_list[0]])
print (num_list[num_list[1]])
print (num_list[num_list[2]])
print (num_list[num_list[3]])
print (num_list[num_list[4]])

rawDescription: Can someone tell me where the numbers disappear?


code:
alist = [[1,2],[10,10],[5,5]]
if len(alist)%2:
    alist.append(alist[-1])

[[(x+y)*.5  for x,y in zip(*item)] for item in zip(alist, alist[1:])]
[[(x+y)*.5  for x,y in zip(*item)] for item in zip(alist[::2], alist[1::2])]

rawDescription: Create a list where each element is a created based on the index of a nested list in python


code:
class Pet(object):
    active = None

    def move(self, dx, dy):
        if Pet.active and isinstance(self, Pet.active):
            print("Moving")
        else:
            # You may want to raise an exception here
            print("This pet isn't active!")

    @classmethod
    def set_active(cls, active_pet):
        """ Specify the type of the active pet. """
        cls.active = active_pet

class Cat(Pet):
    pass

class Dog(Pet):
    pass

class Mouse(Pet):
    pass

c = Cat()
d = Dog()
m = Mouse()
Pet.set_active(Cat)
c.move(2,4)
d.move(2,4)
m.move(2,4)

rawDescription: Disabling the function in base class in python


rawDescription: I need help to structure the python csv to json in the following format, pease


rawDescription: Python: Making a read-only property accessible via **vars(some_class)


rawDescription: Python: how to have dynamicly generated function arguments without using an eval


code:
import ast, numpy
s = str(numpy.asarray(lst))
s
list(ast.literal_eval(",".join(s.split()).replace("[,", "[")))

rawDescription: Converting numpy array to a list

description: Converting numpy array to a list


code:
for i in exts:
    # Check the specified cmdline position and extension
    if cmd_parts[i[1]].strip().endswith(i[0]):
        lg.debug("Checking "+cmd_parts[i[1]])
        running_script = cmd_parts[i[1]]

        # Make sure that the interpretter matches the extension
        if running_script.endswith(i[0]) and not cmd_parts[0].startswith(i[2]):
            lg.error("Wrong interpretter... go away...")
            sys.exit(-1)

        break

rawDescription: Password Management for non-interactive process


code:
import lxml.html as LH

content='<input name="myInput" value="This is what i want"/>'

doc=LH.fromstring(content)
for val in doc.xpath("//input[@name='myInput']/@value"):
    print(val)

rawDescription: Python lxml, matching attributes

description: Python lxml, matching attributes


code:
class Post(Document):
    title = StringField()
    tags = ListField(StringField())

post1 = Post(title='Fun with MongoEngine', tags=['mongodb', 'mongoengine']).save()
post2 = Post(title='Loving Mongo', tags=['mongodb']).save()

rawDescription: Trouble querying ListField with mongoengine


code: from __future__ import division

rawDescription: Python boids implementation

description: Python boids implementation


code: sims

rawDescription: Matching array elements to dictionary keys in Python


code:
update_or_create(defaults=None, **kwargs)
person, created = Person.objects.update_or_create(
    name='Robert',
    defaults={'name': 'Bob'},
)

rawDescription: Create Django model or update if exists

description: Create Django model or update if exists


rawDescription: Mode of grouped data in (py)Spark

description: Mode of grouped data in (py)Spark


code:
orig_stdout = sys.stdout
with open('test.xml', "w+") as outfile:
    sys.stdout = outfile

sys.stdout = orig_stdout

rawDescription: How to print to screen after redirect print to file, in python?

description: Print to screen after redirect print to file, in python


code:
searchTxt = """Abc Defg Hijk Lmn
Xyz Lmn jkf gkjhg"""

data = """kggf **Abc Defg Hijk** fgff jfkjgk djkfkgf
 Akggf **Abc ** fgff jfkjgk djkfkgf
 Akggf **Abc Defg  fgff jfkjgk djkfkgf
 gjshgs gskk **Xyz Lmn jkf**
 fgsgdf fkgksk **Xyz Lmn**"""

searchWords = []
for line in (line for line in searchTxt.split('\n') if line.strip()):
    words = line.split()
    searchWords.extend([' '.join(words[:i]) for i in xrange(len(words), 0, -1)])

searchWords = sorted(searchWords, key=len, reverse=True)# to look first for the longest string match

res = set([line for sword in searchWords for line in data.split('\n') if sword in line])

# OR

res = []
for line in data.split('\n'):
    for sword in searchWords:
        if sword in line:
            res.append(line)
            break

rawDescription: String slicing in python

description: String slicing in python


code:
to_remove = [x for x in balls if x.times_bounced > N]
for x in to_remove:
    balls.remove(x)

rawDescription: How to remove specific object from a list, if I can't use the variable it is attached to, to find its index?

description: Remove specific object from a list, if I can't use the variable it is attached to, to find its index


code:
import inspect
def decorate_all_methods(decorator):
    def apply_decorator(cls):
        for k, f in cls.__dict__.items():
            if inspect.isfunction(f):
                setattr(cls, k, decorator(f))
        return cls
    return apply_decorator

rawDescription: Try/Except Every Method in Class?

description: Try/Except Every Method in Class


code: del result['Sex']

rawDescription: Write to_csv with Python Pandas: Choose which column index to insert new data


code:
from xlsxwriter.workbook import Workbook

workbook  = Workbook('chart_combined.xlsx')
worksheet = workbook.add_worksheet()

# Add a format for the headings.
bold = workbook.add_format({'bold': True})

# Make the columns wider.
worksheet.set_column('B:C', 12)

# Add the worksheet data that the charts will refer to.
headings = ['ID', ' Reading 1', ' Avg Reading 1']
data = [
    [0,    1,    2,    3,    4],
    [4.2,  4.4,  4.5,  5.6,  1.2],
    [3.98, 3.98, 3.98, 3.98, 3.98],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])

# Create a combined column and line chart that share the same X and Y axes.

# First create a column chart. This will use this as the primary chart.
column_chart = workbook.add_chart({'type': 'column'})

# Configure the data series for the primary chart.
column_chart.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$6',
    'values':     '=Sheet1!$B$2:$B$6',
})

# Create a line chart. This will use this as the secondary chart.
line_chart = workbook.add_chart({'type': 'line'})

# Configure the data series for the secondary chart.
line_chart.add_series({
    'name':       '=Sheet1!$C$1',
    'categories': '=Sheet1!$A$2:$A$6',
    'values':     '=Sheet1!$C$2:$C$6',
})

# Combine the charts.
column_chart.combine(line_chart)

# Insert the chart into the worksheet
worksheet.insert_chart('E2', column_chart)

workbook.close()

rawDescription: xlsxwriter combining chart types

description: Xlsxwriter combining chart types


code: serializer = AccountSerializer(account, context={'request': request})

rawDescription: How to add a static User field to django query

description: Add a static User field to django query


rawDescription: openpyxl Python Iterating Through Large Data List

description: Openpyxl Python Iterating Through Large Data List


code:
kernel = np.ones(20, dtype=bool)
for i in range(N):
    spike_times = spiketimes[i]
    datarow = data[i]
    mask = np.zeros(len(datarow), dtype=bool)
    indexes = (spike_times / dt).astype(int)
    mask[indexes] = True
    mask = np.convolve(mask, kernel)[10:-9]

    means[i] = datarow[~mask].mean()

rawDescription: how to create a mask from time points for a numpy array?

description: Create a mask from time points for a numpy array


rawDescription: dictionary of dictionaries(nested dicts)

description: Dictionary of dictionaries(nested dicts)


code:
cells_c  = [ And(1 <= X[i][j], X[i][j] <= 9)
         for i in range(9) for j in range(9) ]

rawDescription: How to define a matrix?

description: Define a matrix


code:
for widget in reversed(self.widgets):
    newWidget = widget.getChildAtPoint(x, y)
    if newWidget:
        found = True
        self.hoverWidget = newWidget
        if prevWidget != self.hoverWidget:
            if prevWidget:
                prevWidget.doMouseLeave(x, y)
            if self.hoverWidget:
                self.hoverWidget.doMouseEnter(x, y)
        break

rawDescription: Faster way to do a loop with a fair bit of stuff in it


code:
def get_all(url, base):
    r = requests.get(url)
    page = r.text

    soup = BeautifulSoup(page)
    for team_links in soup.select("div.details h3 a"):
        yield (urljoin(base, team_links["href"]))
    nxt = soup.find("div", {"class": "pages"}).find("span", text="Next")
    while nxt:
        r = requests.get(urljoin(base, nxt.find_previous("a")["href"]))
        page = r.text
        soup = BeautifulSoup(page)
        for team_links in soup.select("div.details h3 a"):
            yield (urljoin(base, team_links["href"]))
        nxt = soup.find("div", {"class": "pages"}).find("span", text="Next")

rawDescription: Why am I getting duplicate links ? And how do I fetch links on the next pages?


code:
z = np.load('arr.npy').astype(np.int32)
y, x = np.indices(z.shape)
valid_z = (y.ravel()>0) & (z.ravel()>0)
x_valid = x.ravel()[valid_z]
y_valid = y.ravel()[valid_z]
z_valid = z.ravel()[valid_z]
# fitting best curve
fig = plt.figure(figsize=(5,3))
ax = fig.add_subplot(111)
ax.scatter(x_valid, y_valid, c=z_valid, alpha=0.2, s=20, edgecolor='none',
        cmap=plt.cm.jet)
# finding best-fit curve
z = np.polyfit(x_valid, y_valid, w=z_valid**0.5, deg=1)
p = np.poly1d(z)
# plotting
x_plot = np.linspace(x_valid.min(), x_valid.max(), 100)
y_plot = p(x_plot)
ax.plot(x_plot, y_plot, '-r', lw=2)
ax.set_xlim(0, x.shape[1])
ax.set_ylim(0, y.shape[0])

ax.legend(loc='lower left', frameon=False, fontsize=8)
fig.savefig('test.png', bbox_inches='tight')

rawDescription: Fit a line to a matrix in python

description: Fit a line to a matrix in python


code:
import requests

url = 'http://time.jsontest.com/'

r = requests.get(url)
# use .json() for json response data
r.json()
{u'date': u'03-28-2015',
 u'milliseconds_since_epoch': 1427574682933,
 u'time': u'08:31:22 PM'}

# to access the field
r.json()['date']
u'03-28-2015'

rawDescription: How to pull information from a Json

description: Pull information from a Json


code:
subprocess.Popen('sh abc.sh < a_file_that_you_prepared.txt',
                 shell=True,
                 stdout=subprocess.PIPE)

rawDescription: Press enter as command input

description: Press enter as command input


rawDescription: How to have negative zero always formatted as positive zero in a python string?

description: Have negative zero always formatted as positive zero in a python string


code: b = a + datetime.timedelta(seconds=3)

rawDescription: What is the standard way to add N seconds to datetime.time in Python?


code:
found = rc.db_query({'submitted': {
  '$lt': datetime.now() - timedelta(hours=1),
  '$gt': datetime.now() - timedelta(hours=4),
}})

rawDescription: IPython parallel : how to recover job IDs from IPcontroller


code:
class Object:
    def __init__(self, key):
        self.key = key

    def __eq__(self, other):
        if isinstance(other, type(self)):
            return self.key == other.key
        return NotImplemented

    def __ne__(self, other):
        return not type(self).__eq__(self, other)

    def __hash__(self):
        return hash(self.key)

objects = set()
o1 = Object(1)
o2 = Object(1)
objects.add(o1)
objects.add(o2)

print (o1, o2)   # <__main__.Object object at 0x105996ba8> <__main__.Object object at 0x105996be0>
print (objects)  # {<__main__.Object object at 0x105996ba8>}

rawDescription: How do I make sure an attribute value is unique in Python?

description: Make sure an attribute value is unique in Python


code:
lst = "one, one, one, two, two, three, three, three, three".split(", ")
unique_set = set(lst)

rawDescription: appending list in python with things not already in it

description: Appending list in python with things not already in it


code:
class Reverser(object):
  def __init__(self, s): self.s = s
  def __lt__(self, other): return other.s < self.s
  def __eq__(self, other): return other.s == self.s

rawDescription: Help sorting: first by this, and then by that


code:
def in_values(s, d):
    """Does `s` appear in any of the values in `d`?"""
    for v in d.values():
        if s in v:
            return True
    return False

in_values('cow', animals)

rawDescription: Python: Testing if a value is present in a defaultdict list


code:
SF = secondFile()
SF.create_something()
variable = SF.variable

rawDescription: Accessing a variable defined inside a function Python


code: links = (a["href"] for a in soup.select("a[href$=geom.txt]"))

rawDescription: BeautifulSoup not downloading files as expected


code: results = [o.method() for o in myObjects()]

rawDescription: best way to deal with a large number of objects in python

description: Deal with a large number of objects in python


code:
data2 = data.unstack(level=0).reorder_levels([1,0], axis=1).sort_index(axis=1)
data2

rawDescription: How do I slice a Multindex timeseries dataframe on Pandas?

description: Slice a Multindex timeseries dataframe on Pandas


code:
from itertools import chain
':'.join(chain.from_iterable(data))

rawDescription: Python Iterate an integer as long as elements exist in a list


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(yourhtml)
span = soup.find('span', {'class': 'sr_gs_rackrate_total'})
b = span.parent
b.span.extract()
b.text

rawDescription: How to search for string with beautiful soup

description: Search for string with beautiful soup


code:
s='abcd'
d={'a':1, 'b':2, 'c':3}
all(c in d for c in s)

rawDescription: How can I check if the characters in a string are in a dictionary of values?

description: Check if the characters in a string are in a dictionary of values


code:
stats.get(('Waukegan, IL'))
stats.get(('Waukegan, IL'))[-1]

rawDescription: Splitting a state name with a city name, return a list containing both?


rawDescription: 'and' operator in string.contains

description: 'and' operator in string.contains


code: number = [int(s) for s in lines.split() if s.isdigit()]

rawDescription: Create a list of numbers that exist in a file with Python


code:
import nltk
nltk.__version__
pattern = r"""
(?x)                   # set flag to allow verbose regexps
(?:[A-Z]\.)+           # abbreviations, e.g. U.S.A.
|\d+(?:\.\d+)?%?       # numbers, incl. currency and percentages
|\w+(?:[-']\w+)*       # words w/ optional internal hyphens/apostrophe
|(?:[+/\-@&*])         # special characters with meanings
"""
from nltk.tokenize.regexp import RegexpTokenizer
tokeniser=RegexpTokenizer(pattern)
line="My weight is about 68 kg, +/- 10 grams."
tokeniser.tokenize(line)

rawDescription: Python: Regular Expression not working properly


code:
if name:
    objs = condition(objs, radio_name, name=name)
if number:
    objs = condition(objs, radio_number, number=number)

rawDescription: How can I fix the following Django code? Using variables within function command

description: Fix the following Django code? Using variables within function command


code:
foo = draw_with_delayed_replacement(a)
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()
foo.next()

rawDescription: shuffle list with repetition constraint 2-back python

description: Shuffle list with repetition constraint 2-back python


code: creator = article_node.xpath('.//dc:creator', namespaces=article_node.nsmap)

rawDescription: python : lxml xpath tag name with colon

description: Python : lxml xpath tag name with colon


code:
my_file = open("classScores.txt", "a")
my_file.write("{}:{}".format(username,correctAnswers))

rawDescription: python: Primary school Arithmetic quiz- saving results to a .txt file

description: Python: Primary school Arithmetic quiz- saving results to a .txt file


rawDescription: Python Numpy Array Operator x += y Different From x = x + y?


code: set_1 |= dic_1.viewkeys() & list_1

rawDescription: How can I extract list items using dictionary keys?

description: Extract list items using dictionary keys


code:
from scipy.io import loadmat
Just_data = [loadmat(f)['data'] for f in Matrix_files]

rawDescription: Load many files into one array - Python

description: Load many files into one array - Python


code: result['nbDocument'] = result.document_id.apply(lambda x: len(x))

rawDescription: Pandas dataframe frequency operation

description: Pandas dataframe frequency operation


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

cmap = LinearSegmentedColormap.from_list('name', ['red', 'blue'])

fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10, 10)), cmap=cmap)
fig.colorbar(im)
plt.show()

rawDescription: Remove a section of a colormap

description: Remove a section of a colormap


code:
from setuptools import setup
# you may need setuptools instead of distutils

setup(
    # basic stuff here
    scripts = [
        'scripts/myscript.sh'
    ]
)

rawDescription: Python - install script to system

description: Python - install script to system


rawDescription: numpy.power() and math.pow() don't give the same result

description: Numpy.power() and math.pow() don't give the same result


code:
def parse_byte(byte):
    return byte & mask2, byte & mask1, byte & mask0

rawDescription: Bit masking in Python

description: Bit masking in Python


rawDescription: Python: Get volume serial number from lnk file?


rawDescription: Sum and Division example (Python)

description: Sum and Division example (Python)


code:
def convert_json(d, convert):
    new_d = {}
    for k, v in d.iteritems():
        new_d[convert(k)] = convert_json(v,convert) if isinstance(v,dict) else v
    return new_d

rawDescription: Converting identifier naming between camelCase and underscores during JSON serialization/deserialization


code:
count = 0
phrase = "hello, world"
for iteration in range(5): #iterate 5 times
    while True:
        #count = count + len(phrase)
        count += len(phrase)  # add the length of phrase to current value of count.
        break                 # break out of while loop, while loop
                              # runs only once for each iteration
    #print the value of current count
    print("Iteration " + str(iteration) + "; count is: " + str(count))

rawDescription: Can somebody please explain this code to me?


code:
from flask.ext.admin.contrib.sqla.view import ModelView, func

class PaidOrderView(ModelVew):
    def get_query(self):
      return self.session.query(self.model).filter(self.model.paid==True)

    def get_count_query(self):
      return self.session.query(func.count('*')).filter(self.model.paid==True)

rawDescription: Flask-Admin default filters

description: Flask-Admin default filters


code:
import subprocess
subprocess.call("tr \',\' \' \' < records.txt", shell=True)

rawDescription: Use sed command in python

description: Use sed command in python


code:
import subprocess

with open('message.log', 'w') as stdout_file:
    my_process = subprocess.Popen(["iPython.exe", "script.py"],
                                   stdout=stdout_file,
                                   stderr=subprocess.STDOUT)

rawDescription: redirect print to log files, in subprocesses too

description: Redirect print to log files, in subprocesses too


code:
def matryoshka(cls):

    # get types of classes
    class classtypes:
        pass
    classtypes = (type, type(classtypes))

    # get names of all public names in outer class
    directory = [n for n in dir(cls) if not n.startswith("_")]

    # get names of all non-callable attributes of outer class
    attributes = [n for n in directory if not callable(getattr(cls, n))]

    # get names of all inner classes
    innerclasses = [n for n in directory if isinstance(getattr(cls, n), classtypes)]

    # copy attributes from outer to inner classes (don't overwrite)
    for c in innerclasses:
        c = getattr(cls, c)
        for a in attributes:
            if not hasattr(c, a):
                setattr(c, a, getattr(cls, a))

    return cls

rawDescription: Nested Python class needs to access variable in enclosing class


rawDescription: For Loop in Python 3.0

description: For Loop in Python 3.0


code: tmp.append({key: val.replace("<br>", " ")})

rawDescription: How to Alter a Dict's Values in a List Comprehension


rawDescription: Unable to use query with Redis Object Mapper (ROM)


code:
print('{} Eastern in various other timezones:'.format(now_et.strftime('%I %p')))
print('Central : {}'.format(now_ct.strftime('%I %p')))
print('Mountain: {}'.format(now_mt.strftime('%I %p')))
print('Pacific : {}'.format(now_pt.strftime('%I %p')))

rawDescription: Python - Convert time to different time zone with am/pm


code: # apt-get install python-netaddr

rawDescription: pysnmp mac to human readable

description: Pysnmp mac to human readable


code: [{'foo': 'bar', 'myKey': 'one'}, {'foo': 'bar', 'myKey': 'two'}, {'yourKey': 'three', 'foo': 'bar'}]

rawDescription: Sort python list of dictionaries by key if key exists


rawDescription: Getting the closest date to a given date


code:
class Queue:
        # SAME AS BEFORE
        def __str__(self):
            return str(self.items)

rawDescription: Taking a queue of integers and making copies of that element


code:
@pm.observed
def b(value=[.2,.3], mu=a):
    return pm.normal_like(value, mu, 100.)

rawDescription: How should I use @pm.stochastic in PyMC?

description: How should I use @pm.stochastic in PyMC


rawDescription: Group and merge similar entries together using Pandas


code: df.drop(['Year', 'Month', 'Day', 'Lat', 'Long'], axis=1, inplace=True)

rawDescription: How to use Pandas to take multi year average in Python

description: Use Pandas to take multi year average in Python


rawDescription: django Url endswith regex in url path

description: Django Url endswith regex in url path


rawDescription: Suppressing scientific notation in pandas?


code:
for entry in data:
    get_func(*entry[:2])(*entry[2:])

rawDescription: How can I easily unpack extra elements of array into single variable?

description: Easily unpack extra elements of array into single variable


code:
def insert_doc(doc):
    doc['_id'] = str(db.seqs.find_and_modify(
        query={ 'collection' : 'admin_collection' },
        update={'$inc': {'id': 1}},
        fields={'id': 1, '_id': 0},
        new=True
    ).get('id'))

    try:
        db.admin_collection.insert(doc)

    except pymongo.errors.DuplicateKeyError as e:
        insert_doc(doc)

rawDescription: How to autoincrement id at every insert in Pymongo?

description: Autoincrement id at every insert in Pymongo


code:
from ctypes import (PYFUNCTYPE, py_object, POINTER, c_double, c_longlong,
                    pydll, CFUNCTYPE, c_bool, cdll)

import pointer2ndarray
tonumpyarray = PYFUNCTYPE(py_object, POINTER(c_double), c_longlong)(
    ("tonumpyarray", pydll.LoadLibrary(pointer2ndarray.__file__)))

@CFUNCTYPE(c_bool, POINTER(c_double), c_longlong)
def callback(data, size):
    a = tonumpyarray(data, size)
    # call scipy functions on the `a` array here
    return True

cpplib = cdll.LoadLibrary("call_callback.so") # your C++ lib goes here
cpplib.call_callback(callback)

rawDescription: How to convert pointer to c array to python array

description: Convert pointer to c array to python array


code: new_list = random.sample(old_list, 5)

rawDescription: Choose 5 different elements from a list?

description: Choose 5 different elements from a list


code: import functions

rawDescription: Python console doesn't get updated (with PyCharm)


code:
c0 = mybitarray(c0)
c0
c0 << 4
c0 >> 4

rawDescription: How to left shift a bitarray in python

description: Left shift a bitarray in python


code:
from re import findall
line = "Hello my name is {{name          }} and I am {{          age   }} years old."
findall("{{\s*(.*?)\s*}}", line)

rawDescription: Finding a string pattern in a string

description: Finding a string pattern in a string


rawDescription: Mapping specific letters onto a list of words


code:
fvd_exists = a['first_visit_date'] != ''
np.where(fvd_exists, a['trainer_managing'], a['trainer_operator'])

rawDescription: Return column conditionally in python using numpy


rawDescription: Most Efficient Implementation of Spreadsheet Data


code:
distances = weather.apply(
    lambda row: dist(lat, long, row['Latitude'], row['Longitude']),
    axis=1)
weather.loc[distances.idxmin(), 'StationName']

rawDescription: How to find the closest match based on 2 keys from one dataframe to another?

description: Find the closest match based on 2 keys from one dataframe to another


code:
import numpy as np
import pandas as pd
Timestamp = pd.Timestamp
df1 = pd.DataFrame({'date': (Timestamp('2012-08-01'),
                             Timestamp('2012-08-02'),
                             Timestamp('2012-08-03'),
                             Timestamp('2012-10-29'),
                             Timestamp('2012-10-30'),
                             Timestamp('2012-11-01'),
                             Timestamp('2012-10-15'),  # on then end_date
                             Timestamp('2012-09-04'),  # outside an interval
                             Timestamp('2012-09-05'),  # on then start_date
                             ),
                    'value': (82, 20, 94, 58, 73, 1, 2, 3, 4)})

print(df1)
df2 = pd.DataFrame({'end_date': (
                        Timestamp('2012-10-15'),
                        Timestamp('2012-09-04'),
                        Timestamp('2012-11-01')),
                    'other_value': ("foo", "bar", "foobar"),
                    'start_date': (
                        Timestamp('2012-09-05'),
                        Timestamp('2012-08-01'),
                        Timestamp('2012-10-16'))})
df2 = df2.reindex(columns=['start_date', 'end_date', 'other_value'])
df2.sort(['start_date'], inplace=True)
print(df2)

# Convert to DatetimeIndexes so we can call the searchsorted method
date_idx = pd.DatetimeIndex(df1['date'])
start_date_idx = pd.DatetimeIndex(df2['start_date'])
# Add one to the end_date so the original end_date will be included in the
# half-open interval.
end_date_idx = pd.DatetimeIndex(df2['end_date'])+pd.DateOffset(days=1)

start_idx = start_date_idx.searchsorted(date_idx, side='right')-1
end_idx = end_date_idx.searchsorted(date_idx, side='right')
df1['idx'] = np.where(start_idx == end_idx, end_idx, np.nan)

result = pd.merge(df1, df2, left_on=['idx'], right_index=True)
result = result.reindex(columns=['idx', 'date', 'value', 'other_value'])
print(result)

rawDescription: Pandas merge with logic

description: Pandas merge with logic


code:
# ...
capsule = self.effectiveWinId()
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
handle = ctypes.pythonapi.PyCapsule_GetPointer(capsule, None)
win32gui.SetWindowLong(handle, win32con.GWL_WNDPROC, self.new_window_procedure)
# ...

rawDescription: How to deal with PyCapsule type inside Python

description: Deal with PyCapsule type inside Python


code:
while True:
    # do stuff
    if condition:
        break

rawDescription: Several functions without global variable


rawDescription: Shallow copy, why does the list not change


code:
class camisaEvento(models.Model):

    ...

    @property
    def sum_quantity(self):
        # just guessing, don't know exactly what you need
        # it's using python's list comprehension, but you can use a more explicit
        # syntax if you're not familiar with this one
        return sum([obj.field_to_sum for obj in
                    quantidadeEvento.objects.filter(fk_field=self.id)])

rawDescription: Django - Display filtered sum of a field from a model on template


rawDescription: Pyramid self.request.POST is empty - no post data available


code:
i = start
while i < stop:
    # do things
    i += step

rawDescription: Changing while and for loops in python

description: Changing while and for loops in python


code:
actions = {
           STATES.IDLE   : None          ,
           STATES.FIRED  : 'start_moving',
           STATES.MOVING : 'move'        ,
           STATES.RESET  : 'reset'       ,
          }

[...]

def update(self):
    method_name = self.__class__.actions[self.state]
    if method_name and callable(getattr(self, method_name)):
        getattr(self, method_name)(self)

rawDescription: Python: Callback issue

description: Python: Callback issue


code:
def create_filter_minfreq_inwords(scored, words, minfreq):
    def bigram_filter(w1, w2):
        return (w1 not in words and w2 not in words) and (
                (w1, w2) in scored and scored[w1, w2] <= minfreq)
    return bigram_filter

rawDescription: Combining filters in nltk collocations

description: Combining filters in nltk collocations


rawDescription: Plotting the difference between two datetime64[ns]


code:
# simple method
class Session(object):
    def __init__(self):
        self.a_set = False
        self.b_set = False
    def reset(self):
        self.b_set = False

rawDescription: Python object state tracking idiom

description: Python object state tracking idiom


code:
import urllib2
import shutil
remote_fo = urllib2.urlopen('http://docs.python.org/library/shutil.html')
with open('bigfile', 'wb') as local_fo:
    shutil.copyfileobj(remote_fo, local_fo)

rawDescription: Python: saving large web page to file

description: Python: saving large web page to file


code:
def inOval(pt1, oval):

    # get the radii
    rx = abs(oval.getP1().getX() - oval.getP2().getX())/2
    ry = abs(oval.getP1().getY() - oval.getP2().getY())/2

    # get the center
    h = oval.getCenter().getX()
    k = oval.getCenter().getY()

    # get the point
    x = pt1.getX()
    y = pt1.getY()

    # use the formula
    return (x-h)**2/rx**2 + (y-k)**2/ry**2 <= 1

rawDescription: Checking if clicks are within a graphic object [Python Graphics Module]


code:
if type(X) == np.ndarray:
    result = (X[..., np.newaxis] * y).sum()
else:
    result = (X * y).sum()

rawDescription: Numpy Dimensions: Multiplications with scalar/matrix


rawDescription: How to get all submatrices of given size in numpy?

description: Get all submatrices of given size in numpy


code: round(float_number, decimal_places)

rawDescription: Rounding integers in python

description: Rounding integers in python


code:
displacements = [a.dtype.fields[field][1] for field in ['int','dbl']]
print(displacements)

rawDescription: How to create struct for passing between nodes using mpi4py

description: Create struct for passing between nodes using mpi4py


code:
def change(lol):
    for index,item in enumerate(lol):
        if isinstance(item, list):
            change(item)
        elif item.startswith('-'):
            lol[index] = ['not',item.split('-')[1]]
    return lol

rawDescription: Python: Iterate over each item in nested-list-of-lists and replace specific items


code:
@celery.task()
def add(x, y):
    logger = add.get_logger(logfile='tasks.log')
    logger.info("Adding %s + %s" % (x, y))
    return x + y

rawDescription: Send log messages from all celery tasks to a single file


code:
for dict_size in range(100,6000,100):
    words = set([''.join(random.choice(string.ascii_lowercase) for x in range(3)) for _ in range(dict_size)])
    t1 = Timer(lambda: graph1()).timeit(10)
    t2 = Timer(lambda: graph2()).timeit(10)
    print('%d,%f,%f' % (dict_size,t1,t2))

rawDescription: Efficiently build a graph of words with given Hamming distance


rawDescription: Printing Variable names and contents as debugging tool; looking for emacs/Python shortcut


code:
signs = ["spade","hearts","club","diamond"]
[[(j,i) for j in range(1,11)] for i in signs]

rawDescription: nested comprehension in python to generate a deck of cards

description: Nested comprehension in python to generate a deck of cards


code:
import re
re.split('; |, ',str)

rawDescription: Python: Split string with multiple delimiters


code:
class MySpider(Spider):
    name = 'my_spider'

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

      endpoints = kwargs.get('start_urls').split(',')
      self.start_urls = ["http://www.google.com/patents/" + x for x in endpoints]

rawDescription: Passing list as arguments in Scrapy

description: Passing list as arguments in Scrapy


code:
from another_test import test_once_more
result = test_once_more()
question = input("1 letter ")
for letter in question:
    if letter == "a":
        result = result.replace("1","a")
        print (result)

rawDescription: AttributeError: 'function' object has no attribute 'replace'


rawDescription: How can I iterate through the indices of the list?

description: Iterate through the indices of the list


code:
import subprocess

def suspend_screensaver():
    'suspend linux screensaver'
    proc = subprocess.Popen('gsettings set org.gnome.desktop.screensaver idle-activation-enabled false', shell=True)
    proc.wait()

def resume_screensaver():
    'resume linux screensaver'
    proc = subprocess.Popen('gsettings set org.gnome.desktop.screensaver idle-activation-enabled true', shell=True)
    proc.wait()

rawDescription: Inhibit screensaver with Python

description: Inhibit screensaver with Python


code:
import itertools

def checkio(data):
    s = reduce(lambda x,y:x+y,data) # s is the sum, you don't need a loop
    half_sum = s / 2

    # instead of random.sample, using itertools to find all possible combinations
    # of all possibles lenghts
    perms = []
    for i in range(len(data) + 1):
        p = itertools.combinations(data, i)
        perms += p

    # min of a list comprehension to find the minimal sum >= half_sum
    m = min([a for a in map(sum, perms) if a >= half_sum])
    # that's the sum of "what's left", members of the list no in the choosen sum
    n = s - m
    # we want the difference between the two
    return abs(n - m)

rawDescription: How to calculate a number representing the lowest possible weight difference

description: Calculate a number representing the lowest possible weight difference


code:
df1.loc[df1["b"].isin(bad), "b"] = "other"
df1

rawDescription: Reassigning strings by value count in Pandas


code: lambda s, w=outfile.write: w(s+"\n")

rawDescription: What is lambda doing in this python code?


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

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)

x = np.linspace(0, np.pi, 100)

line1, = ax1.plot(x, np.cos(3*x), color='red')
line2, = ax2.plot(x, np.sin(4*x), color='green')

# The key to the position is bbox_to_anchor: Place it at x=0.5, y=0
# in axes coordinates.
# "upper center" is basically saying center horizontal alignment and
# top vertical alignment in this case
ax1.legend([line1, line2], ['yep', 'nope'], bbox_to_anchor=[0.5, 0],
           loc='upper center', ncol=2, borderaxespad=0.25)

plt.show()

rawDescription: How to position and align a matplotlib figure legend?

description: Position and align a matplotlib figure legend


code:
log.log('title type= %s and value= %r' % (str(type(title)), title))
log.log('name type= %s and value= %r' % (str(type(name)), name))

rawDescription: Python string comparisons?

description: Python string comparisons


code: numpy.logaddexp(logA, logB)

rawDescription: Python's equivalent to R's logspace_add function?


code:
dis.dis(fact1)
dis.dis(fact2)

rawDescription: Why does removing the else slow down my code?


code:
l = LogEntry(user_id=request.user.id, actions_flag=CHANGE, change_message="...")
l.save()

rawDescription: Django - Custom Admin Actions Logging

description: Django - Custom Admin Actions Logging


code:
for k in set(d) - s:
    del d[k]

rawDescription: Remove entries of a dictionary that are not in a set


code:
#!/usr/bin/env python

import gtk
import rsvg

window = gtk.Window()
window.set_title("Foo")
window.connect("destroy", gtk.main_quit)
window.show()

svg = rsvg.Handle(file='cards.svg')
pixbuf = svg.get_pixbuf(id='#3_diamond')

image = gtk.Image()
image.set_from_pixbuf(pixbuf)
image.show()

window.add(image)

gtk.main()

rawDescription: How do I render *parts* of a svg file?

description: Render *parts* of a svg file


code:
import numpy as np
x = np.array([521, 3, 1, 2, 1, 1, 5])
np.argsort(x)

rawDescription: Weird Data manipulation in Pandas

description: Weird Data manipulation in Pandas


code:
@app.route('/')
def home():
    foos = Foo.query.all()
    for foo in foos:
        db.session.add(Bar(foo))
        db.session.expunge(foo)
    db.session.commit()

    return render_template_string('''
        {% for foo in foos %}
            {{ foo.name }}
        {% endfor %}
    ''', foos=foos)

rawDescription: Proper way to prevent SQLAlchemy from re-running queries on expired objects?


rawDescription: Pressed button after leaving messagebox in tkinter


code: object_dict = dict( (y, CharInStageList(x,y)) for x,y in data )

rawDescription: How to add Objects to Class with a loop in Python?

description: Add Objects to Class with a loop in Python


rawDescription: How to replace empty string with zero in comma-separated string?

description: Replace empty string with zero in comma-separated string


rawDescription: Getting a time index in python for pandas dataframe


rawDescription: Detecting geographic clusters

description: Detecting geographic clusters


code: frame.resample('1H').agg({'radiation': np.sum, 'tamb': np.mean})

rawDescription: How to resample a dataframe with different functions applied to each column?

description: Resample a dataframe with different functions applied to each column


rawDescription: One x axis, one y axis on left, one y axis on right


code:
glBindTexture(GL_TEXTURE_2D, self.texture[i])
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST)

rawDescription: OpenGL Framebuffer with more than one texture


rawDescription: 3D matrix perspective transform

description: 3D matrix perspective transform


code:
from matplotlib import pyplot as pl
from matplotlib import cm

pl.scatter(times, values, c = cm.spectral(1.*values/max(values)))

rawDescription: Plot a curve that has different colors depending on a parameter


code:
pair_dist = d.cdist(position, position)
pair_d = np.triu(pair_dist<=4, k=1) # k=1 to exclude the diagonal
for i, j in zip(*np.nonzero(pair_d)):
    ...

rawDescription: Many particles in box - physics simulation


code:
class BunchOfStuff(object):
    def __init__(self):
        self.variable1 = 5
        self.variable2 = 10
        self.variable3 = 15
    @property
    def variable4(self):
        return self.variable1 + self.variable2 + self.variable3

stuff = BunchOfStuff()
print(stuff.variable4) # prints 30 -- no need to call it, it acts like a variable
stuff.variable1 = 10
print(stuff.variable4) # prints 35

rawDescription: Refreshing a list of variables with one command


code:
import itertools

n = 10
m = n + 1

def innerproduct(A, B):
    s = 0
    for k in range(n):
        s += A[k] * B[k]
    return s

leadingzerocounts = [0] * m
for S in itertools.product([-1, 1], repeat=n - 1):
    S1 = S + (1,)
    S1S1 = S1 * 2
    for C in itertools.combinations(range(n - 1), n // 2):
        F = list(S1)
        for i in C:
            F[i] *= -1
        leadingzerocounts[0] += 4
        for i in range(1, m):
            if innerproduct(F, S1S1[i:i + n]):
                break
            leadingzerocounts[i] += 4
print(leadingzerocounts)

rawDescription: How to speed up multiple inner products in python

description: Speed up multiple inner products in python


code:
Delegation
class LockProxy(object):
    def __init__(self, obj):
        self.__obj = obj
        self.__lock = RLock()
        # RLock because object methods may call own methods
    def __getattr__(self, name):
        def wrapped(*a, **k):
            with self.__lock:
                getattr(self.__obj, name)(*a, **k)
        return wrapped

lockedset = LockProxy(set([1,2,3]))

rawDescription: How to make built-in containers (sets, dicts, lists) thread safe?

description: Make built-in containers (sets, dicts, lists) thread safe


code:
from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.http import HttpResponse
from social import exceptions as social_exceptions

class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, exception.__class__.__name__):
            return HttpResponse("catched: %s" % exception)
        else:
            raise exception

rawDescription: How do I handle exceptions on Python Social Auth

description: Handle exceptions on Python Social Auth


rawDescription: Verify if previous line have same string than current line and sum value of another column


code:
import time,random

x=random.sample(xrange(3600), 3600) #create randomly ordered list of size 3600

i=0

for url in urls:
    time.sleep(x[i])   #sleep for a random value between (0 to 3600)
    i+=1               #increment 'i' by 1

time.sleep(sum(x[i:]))  #sum the rest of the values

rawDescription: Python script loop to assign timeslot to each line in input file


rawDescription: Grabbing selection between specific dates in a DataFrame


code: print(yaml.load("{age: wise, name: wut}"))

rawDescription: PyYAML dumping invalid YAML unless param includes an array


code:
import struct
with open("foo.bin", 'wb') as f:
    f.write(struct.pack('h', 0b0010010110101))

rawDescription: python Binary string to binary data

description: Python Binary string to binary data


code: extent = [min(xedges), max(xedges), min(yedges), max(yedges)]

rawDescription: How to enlarge image axis in Python?

description: Enlarge image axis in Python


rawDescription: fancy indexing multiple dimensions: Boolean Matrix for hypercube?

description: Fancy indexing multiple dimensions: Boolean Matrix for hypercube


code:
plt.figure()
nx.draw_networkx(T, pos=pos2, with_labels=False, node_size = 15)
plt.show()

rawDescription: Python: how to visualize the Minimum Spanning Tree of a network?


code:
from itertools import product

[{(i, j): 'G{}{}'.format(i, j)} for i, j in product(range(l), repeat=2)]

rawDescription: Best pythonic way of mapping a specific function with multiple arguments


rawDescription: List of ints to string

description: List of ints to string


rawDescription: How to save JSON string into CSV or TXT in Python?

description: Save JSON string into CSV or TXT in Python


code:
url="http://einstein.biz/"
import urllib.request
m = urllib.request.urlopen(url)
from bs4 import BeautifulSoup
soup = BeautifulSoup(m)
s = soup.find_all('a')
[i.string for i in s]

rawDescription: How to extract all the link name from a html page

description: Extract all the link name from a html page


code:
import io
from csv import DictWriter
out_csv = io.StringIO('''''')

fieldnames = ['Size', col_name]
writer = DictWriter(out_csv, fieldnames=fieldnames)
writer.writeheader()

for key, value in data.items():
    row = {'Size': key, col_name: value}
    print(row)
    writer.writerow(row)

print(out_csv.getvalue())

rawDescription: Change values of CSV based on user Response


code:
from numpy.random import rand
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

NUM = 3
ellipse = lambda: Ellipse(rand(2)*10, rand(), rand(), rand()*360)
ells = [ellipse() for i in range(NUM)]

fig, ax = plt.subplots()

for i, e in enumerate(ells):
    ax.add_patch(e)
    e.set(clip_box=ax.bbox, alpha=rand(), facecolor=rand(3),
          label='Ellipse{}'.format(i+1))

ax.legend()
ax.set(xlim=[0, 10], ylim=[0, 10], aspect='equal')

plt.show()

rawDescription: Label plotted ellipses

description: Label plotted ellipses


code:
with open("list.csv", "U") as file1, open("example.csv", "rb") as file2, open("output.csv", "wb+") as file3:
    reader1 = csv.reader(file1)
    reader2 = csv.reader(file2)
    writer = csv.writer(file3)

    reader1_rows = [row for row in reader1]
    reader2_rows = [row for row in reader2]

    for num, row in enumerate(reader1_rows):
        for word in reader2_rows[num]:
            for item in row:
                if item in word:
                    writer.writerow([item, word])

rawDescription: Similarities between two csv files

description: Similarities between two csv files


rawDescription: Pandas if statement across list of columns


code: python3 /path/to/collect_lines.py

rawDescription: Code for coyping specific lines from multiple files to a single file (and removing part of the copied lines)


code:
import csv

speed = 74.3
fine = 35

driver_details = {}

with open('Driver Details.csv') as f_driver_details:
    csv_driver_details = csv.reader(f_driver_details)
    header = next(csv_driver_details)       # skip the header

    for row in csv_driver_details:
        driver_details[row[7]] = row

with open('Standard Reg.csv') as f_standard_reg, open('Output log.csv', 'w', newline='') as f_output:
    csv_standard_reg = csv.reader(f_standard_reg)
    header = next(csv_standard_reg)     # skip the header
    csv_output = csv.writer(f_output)

    for row in csv_standard_reg:
        try:
            driver = driver_details[row[3]]
            print('Match found - Fine {}, Speed {}\n{} {}\n{}'.format(fine, speed, driver[0], driver[1], '\n'.join(driver[2:7])))
            csv_output.writerow(driver[0:7] + [speed, fine])
        except KeyError as e:
            print('No match found')

rawDescription: How to compare two csv files in Python

description: Compare two csv files in Python


code:
class B(A):
    def f(self):
        try:
            self.__class__, cls = A, self.__class__
            A.f(self)
        finally:
            self.__class__ = cls
        print("World")

rawDescription: Python recursive method calls with super


code:
from django.db.models.loading import get_model

x = Streams.objects.all() # all stream objects
y = x[0].content_type # CommentData ContentType object
model_class = get_model(app_label=y.app_label, model_name=y.model)

model_class.objects.all() # all objects of the type

rawDescription: Using Generic Foreign Keys to retrieve data


code:
c = [('Pineapple',1,'Hawaii'),('Orange',3,'Florida'),('Banana',1,'Hawaii'),('Apple',1,'Washington'),('Cherry',2,'Washington')]
# sort first by number (in reverse order) then by state, and finally by fruit
d = sorted(c,key=getsortfunction(c,[(1,True),(2,False),(0,False)]))

# sort c first by number (in reverse order), then by fruit, ignoring state
e = sorted(c,key=getsortfunction(c,[(1,True),(0,False)]))

rawDescription: Sort at various levels in Python

description: Sort at various levels in Python


code: new_tuple = namedtuple('MyTuple', ['attr1', 'attr2', 'attr3'])('Bob', 'John', 'Tom')

rawDescription: Is it possible to specify attribute names when creating an instance of namedtuple?


code:
@app.route('/add-some-item', method=['POST'])
def add_some_item():
    some_item = SomeItem(foo=request.form.get('foo'))
    db.session.add(some_item)
    db.session.commit()
    return 'The new id is: {}'.format(some_item.id)

rawDescription: Can I use Flask-SQLAlchemy and SQLAlchemy at the same time?

description: Use Flask-SQLAlchemy and SQLAlchemy at the same time


code:
class Meta:
    model = YourModel
    fields = ('title', 'content')

rawDescription: Generating custom forms from DB schema

description: Generating custom forms from DB schema


code: {'icy-description': 'Evropa 2', 'Via': '1.1 s670-6.noc.rwth-aachen.de:80 (Cisco-WSA/8.8.0-085)', 'icy-genre': 'Various', 'icy-url': 'http://www.evropa2.cz', 'icy-pub': '0', 'ice-audio-info': 'ice-samplerate=44100;ice-bitrate=128;ice-channels=2', 'Date': 'Fri, 29 Jan 2016 17:24:20 GMT', 'icy-br': '128, 128', 'Content-Type': 'audio/mpeg', 'Connection': 'keep-alive', 'Transfer-Encoding': 'chunked', 'icy-name': 'Evropa 2', 'Server': 'Icecast 2.3.2', 'Cache-Control': 'no-cache'}

rawDescription: Python 3 - Urllib3 read internet radio metadata


rawDescription: Python Regular Expression to find all combinations of a Letter Number Letter Designation


code:
import numpy as np

def generate_orig(T=1., nt=100000, lbd=500., mu=0, sigma=1., S0=0):
    dt = T/nt
    St  = [S0]  * nt
    sqrtdt = np.sqrt(dt)
    dBt = np.random.normal(0, sqrtdt, nt)

    for k in xrange(1, nt):
        dSt = lbd * (mu - St[k-1]) * dt + sigma * dBt[k]
        St[k] = St[k-1] + dSt
    return St

def generate(T=1., nt=100000, lbd=500., mu=0, sigma=1., S0=0, npaths=1):
    dt = T/nt
    St  = np.full((nt, npaths), S0)
    sqrtdt = np.sqrt(dt)
    dBt = np.random.normal(0, sqrtdt, size=(nt, npaths))

    for k in xrange(1, nt):
        dSt = lbd * (mu - St[k-1]) * dt + sigma * dBt[k]
        St[k] = St[k-1] + dSt
    return St

rawDescription: Loop optimization for random process

description: Loop optimization for random process


code: dateList = [record.datetime for user in users for record in user.records]

rawDescription: Accessing all class variables in a container without a loop


rawDescription: pandas groupby dropping columns

description: Pandas groupby dropping columns


code:
currList=set(['pathA/file1', 'pathA/file2', 'pathB/file3'])
processList=set(['pathA/file1', 'pathA/file9', 'pathA/file3'])

rawDescription: re-implement __eq__ to compare sets with symmetric_difference in python

description: Re-implement __eq__ to compare sets with symmetric_difference in python


code: ind = ind.flatten()

rawDescription: Implementing np.subtract.outer for index of array([[array .... structure in python


code: your_list_of_lists.sort(key = lambda x : map(abs,x) )

rawDescription: Sort list of lists by unique reversed absolute condition


code:
sub_event_b = SubEventB.objects.create(title='what a lovely day')
other_model = OtherModel()
other_model.event = sub_event_b

rawDescription: Django model subclassing approaches

description: Django model subclassing approaches


code:
import numpy as np
import pandas as pd
import itertools as IT

def using_iteration(df1):
    df1 = df1.drop_duplicates()
    data = set()
    for vid, grp in df1.groupby(['Vehicle_ID']):
        for c1, c2 in IT.combinations(grp['City'], 2):
            if c1 > c2:
                c1, c2 = c2, c1
            data.add((c1, c2, vid))
    df = pd.DataFrame(list(data), columns=['City_x', 'City_y', 'Vehicle_Count'])
    result = df.groupby(['City_x', 'City_y']).count()
    return result

def using_merge(df1):
    df1 = df1.drop_duplicates()
    df3 = pd.merge(df1, df1, on='Vehicle_ID', how='left')
    mask = df3['City_x'] < df3['City_y']
    df3 = df3.loc[mask]
    result = df3.groupby(['City_x', 'City_y']).count()
    result = result.rename(columns={'Vehicle_ID':'Vehicle_Count'})
    return result

def generate_df(nrows, nids, strlen):
    cities = (np.random.choice(list('ABCD'), nrows*strlen)
              .view('|S{}'.format(strlen)))
    ids = np.random.randint(nids, size=(nrows,))
    return pd.DataFrame({'Vehicle_ID':ids, 'City':cities})

df1 = pd.DataFrame([
    [1, 'A'], [1, 'B'], [1, 'C'],
    [2, 'A'], [2, 'C'], [2, 'C'], [2, 'A'],
    [3, 'C'], [3, 'B'], [3, 'C'], [3, 'B']], columns=['Vehicle_ID', 'City'])

df = generate_df(10000, 50, 2)
assert using_merge(df).equals(using_iteration(df))

rawDescription: Count by combination

description: Count by combination


code:
url = 'http://store.highsnobiety.com/sitemap_products_1.xml'

page = requests.get(url)
tree = html.fromstring(page.content)

# skip first url tag with no image:title
url_tags =  tree.xpath("//url[position() > 1]")

data = [(e.xpath("./image/title//text()")[0],e.xpath("./loc/text()")[0]) for e in  url_tags]

rawDescription: Using LXML to print out individual strings from file


code:
settings = QtCore.QSettings(org_name, app_name)
main_win.restoreGeometry(settings.value('geometry'))
main_win.restoreState(settings.value('state'))
main_win._ui.dockWin.setFloating(settings.value('dockWin/isFloating')=='true')

rawDescription: Qt - Pyside - .saveGeom() .saveState() (again)


code: qry = """SELECT "id" FROM "tickets" WHERE "Status" = 'Pending'"""

rawDescription: Python SQlite returning an Object rather than a string


rawDescription: adaptable function to slice up nested lists?

description: Adaptable function to slice up nested lists


code:
import random
import collections
other = {'A':'B', 'B':'A'}

def serve(prob, player):
    if random.random() < prob:
        return player
    else:
        return other[player]

def simOneSet(probA, probB):
    prob = {'A':probA, 'B':probB}
    score = collections.Counter()

    serving = "A"
    while not setOver(score['A'], score['B']):
        for i in range(2):
            winner = serve(prob[serving], serving)
            score[winner] += 1
        if score['A'] == 10 and score['B'] == 10:
            winner = serve(prob[serving], serving)
            score[winner] += 1
            serving = winner

    return score['A'], score['B']

def setOver(scoreA, scoreB):
    return max(scoreA, scoreB) >= 21

print(simOneSet(0.5,0.5))

rawDescription: Table tennis simulator

description: Table tennis simulator


rawDescription: Format hex digits for character code \x

description: Format hex digits for character code \x


code:
import turtle
import random

def star(color, side_length, x, y):
    print(color, side_length, x, y)
    turtle.color(color)
    turtle.begin_fill()
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    for i in range(5):
        turtle.forward(side_length)
        turtle.right(144)
        turtle.forward(side_length)
    turtle.end_fill()

def random_color():
    randvar = randrange(0, 5)
    if randvar == 0:
        return ('red')
    elif randvar == 1:
        return ('blue')
    elif randvar == 2:
        return ('green')
    elif randvar == 3:
        return ('yellow')
    else:
        return ('black')

def length():
    return random.randrange(5, 71)

def xcoord():
    return random.randrange(-280, 281)

def ycoord():
    return random.randrange(-200, 201)

def night_sky():
    z = int(input('How many stars do you want?'))
    for i in range(z):
        color = random_color()
        side_length = length()
        x = xcoord()
        y = ycoord()
        star(color, side_length, x, y)

night_sky()

rawDescription: Drawing Stars with turtle in python

description: Drawing Stars with turtle in python


rawDescription: Discretization of probability array in Python


code:
root_node = {}
for data in formdata:
    add_data(root_node, data['formname'], data['formvalue'])

rawDescription: HTML form data to recursive json dict

description: HTML form data to recursive json dict


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

data = np.random.random((10,10))
labels = 'abcdefghij'

fig, ax = plt.subplots()
im = ax.imshow(data, cmap='gray', interpolation='none')
fig.colorbar(im)

# Set the major ticks at the centers and minor tick at the edges
locs = np.arange(len(labels))
for axis in [ax.xaxis, ax.yaxis]:
    axis.set_ticks(locs + 0.5, minor=True)
    axis.set(ticks=locs, ticklabels=labels)

# Turn on the grid for the minor ticks
ax.grid(True, which='minor')

plt.show()

rawDescription: Matplotlib: Move ticklabels between ticks


code:
def prepareinsert(mydict):
    query = """
    INSERT INTO {0} ({1}) VALUES ({2});
    """
    d = dict(mydict)  # don't modify the input dictionary if you want to reuse it
    table = d.pop('table')
    columns = ','.join(d.keys())
    placeholders = ','.join(['%s'] * len(d))
    values = d.values()
    return (query.format(table, columns, placeholders), values)

rawDescription: Using pythons dictionary's to create a generic MySQL insert string


code:
class BestShoesReviewManager(models.Manager):
    def get_query_set(self):
        qs =  super(DahlBookManager, self).get_query_set()
        return qs.filter(owner_review__ratings__rating_attribute__attribute= 'overall')\
                 .order_by(owner_review__ratings__rating)

rawDescription: Help on my django models for my shoe review website


code:
class MyClass(object):

    def __init__(self):
        # prevents infinite recursion from self.data = {'a': 'v1', 'b': 'v2'}
        # as now we have __setattr__, which will call __getattr__ when the line
        # self.data[k] tries to access self.data, won't find it in the instance
        # dictionary and return self.data[k] will in turn call __getattr__
        # for the same reason and so on.... so we manually set data initially
        super(MyClass, self).__setattr__('data', {'a': 'v1', 'b': 'v2'})

    def __setattr__(self, k, v):
        self.data[k] = v

    def __getattr__(self, k):
        # we don't need a special call to super here because getattr is only
        # called when an attribute is NOT found in the instance's dictionary
        try:
            return self.data[k]
        except KeyError:
            raise AttributeError

rawDescription: Python: how to implement __getattr__()?

description: Python: how to implement __getattr__()


code:
@api.multi
def open_partner_form(self):
    form_view_id = self.env.ref(
        'base.view_partner_form').id
    for event_registration in self:
        return {
            'name': _('%s') % event_registration.partner_id,
            'view_type': 'form',
            'view_mode': 'form',
            'res_id': event_registration.partner_id.id,
            'views': [(form_view_id, 'form'), ],
            'res_model': 'res.partner',
            'type': 'ir.actions.act_window',
            'target': 'current',
            'flags': {'action_buttons': True},
        }

rawDescription: OpenERP. Open from tree a form but from other related field


code:
from datetime import datetime, timedelta
old_time = '2014-01-07T09:29:35Z'

def time_converter(old_time, time_zone):
    time_zone = float(time_zone[:3] + ('.5' if time_zone[3] == '3' else '.0'))
    str_time = datetime.strptime(old_time, "%Y-%m-%dT%H:%M:%SZ")
    return (str_time + timedelta(hours=time_zone)).strftime("%Y-%m-%dT%H:%M:%SZ")

if __name__ == '__main__':
    for time_zone in ('+0400', '+0430', '-1400'):
        print(time_converter(old_time, time_zone))

rawDescription: Convert UTC string to local datetime string in python?


rawDescription: Computing the nth 6 character permutation of an alphabet


code:
fid = open('out.txt')  # Just copy what you put in your question inside a file.
l = fid.readlines()  # Read the whole file into a list.
first = {}  # create a dictionary.
for i in l:
    s = i.split()  # split the list using spaces (default)
    first[s[0]] = [int(s[j]) for j in range(1,len(s))] # list comprehension to transform string into its for you number lists.

print(first)
addn = 11
for key, value in first.items():
    if addn in value:
        g= "yes"
        print(key, addn, g)
        #print d
    else:
        g= "no"
        print(key, addn, g)

    value.append(300)
    value.append(22)
    value = sorted(value, key=int)

    print("State:", key, value)

rawDescription: Creating array from CSV in Python while limiting columns used


code:
import os.path

PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
PIP_DOWNLOAD_CACHE = os.path.abspath(PROJECT_ROOT+'/packages/'),

rawDescription: Installing selected packages from requirements.txt


code:
from datetime import date, timedelta

current_date = date(year=2000, month=1, day=1)
current_date.isoformat()

next_date = current_date + timedelta(days=1)
next_date.isoformat()

previous_date = current_date - timedelta(days=1)
previous_date.isoformat()

rawDescription: Python equivalent of Tcl "clock add" command for date-spinning spinbox?


code:
class MockLogger(object):
    def debug(msg, *args, **kwargs): pass
    def info(msg, *args, **kwargs): pass
    def warn(msg, *args, **kwargs): pass
    def error(msg, *args, **kwargs): pass
    def critical(msg, *args, **kwargs): pass

class Test(unittest.TestCase):
    def test_func(self):
        _logger1=testmodule.logger1
        _logger2=testmodule.logger2
        testmodule.logger1=MockLogger()
        testmodule.logger2=MockLogger()
        # perform test
        testmodule.logger1=_logger1
        testmodule.logger2=_logger2

rawDescription: Override python logging for test efficiency


code:
sourcefile = "C:\\Python25\\PC_New.txt"
filename2 = "C:\\Python25\\PC_reduced.txt"

offending = ["Exception","Integer","RuntimeException"]

def fixup( filename ):
    fin = open( filename )
    fout = open( filename2 , "w")
    for line in fin:
        if True in [item in line for item in offending]:
            continue
        fout.write(line)
    fin.close()
    fout.close()

fixup(sourcefile)

rawDescription: Python script to remove lines from file containing words in array


code: res = dict([ (t, nt(*t)) for t in pairs ])

rawDescription: Pairwise Set Intersection in Python

description: Pairwise Set Intersection in Python


code:
encd=open('jalf_encode_err','r').read()
chardet.detect(encd)
unicode(encd, chardet.detect(encd)['encoding']).encode('ascii')
unicode(encd, chardet.detect(encd)['encoding']).encode('ascii', 'ignore')
len(encd)
len(unicode(encd, chardet.detect(encd)['encoding']).encode('ascii', 'ignore'))

rawDescription: Python mailbox encoding errors

description: Python mailbox encoding errors


code:
def Any(cls):
    class Any(cls):
        def __eq__(self, other):
            return True
    return Any()

rawDescription: Assert that a method was called with one argument out of several


code:
arabicdict = {}
for n in range(0x600, 0x700):
    c = chr(n)
    try:
        id =  unicodedata.name(c).lower()
        if 'arabic letter' in id:
            arabicdict[c] = id[14:]
    except ValueError:
        pass

rawDescription: How to map a arabic character to english string using python

description: Map a arabic character to english string using python


code:
placemarker = 1
while True:
    print ("Welcome")
    command = input ("Enter command: ")
    if command == "register": #String variable here
        placemarker = 20

if placemarker == 20:
    registerMe = input ("What is your name? ")

rawDescription: How to link in Python

description: Link in Python


code:
from subprocess import call

qsub_call = "qsub -l walltime=72:00:00 %s"
call(qsub_call % "test_job.sh", shell=True)

rawDescription: Submitting jobs using python

description: Submitting jobs using python


code: (callable(x) for x in iterable)

rawDescription: question about filtering a dictionary using a list in python

description: Question about filtering a dictionary using a list in python


code:
import re

#from english_american_dictionary import ame_to_bre_spellings
ame_to_bre_spellings = {'tire':'tyre', 'color':'colour', 'utilize':'utilise'}

def replacer_factory(spelling_dict):
    def replacer(match):
        word = match.group()
        return spelling_dict.get(word, word)
    return replacer

def ame_to_bre(text):
    pattern = r'\b\w+\b'  # this pattern matches whole words only
    replacer = replacer_factory(ame_to_bre_spellings)
    return re.sub(pattern, replacer, text)

def main():
    #with open('test_file.txt') as in_file:
    #    text = in_file.read()
    text = 'foo color, entire, utilize'

    #with open('output_test_file.txt', 'w') as out_file:
    #    out_file.write(ame_to_bre(text))
    print(ame_to_bre(text))

if __name__ == '__main__':
    main()

rawDescription: Python 2.7 - find and replace from text file, using dictionary, to new text file


code:
getattr(np, 'long')
np.dtype(getattr(np, 'long'))
np.dtype(getattr(np, 'int'))
np.dtype(getattr(np, 'float64'))
np.dtype(getattr(np, 'float'))

rawDescription: How can I create a numpy dtype from the string 'long'?

description: Create a numpy dtype from the string 'long'


code:
def add_day(x):
    d = x.date()+DT.timedelta(1)
    return mtl.localize(x.replace(year=d.year, month=d.month, day=d.day, tzinfo=None))

rawDescription: How to find next day's Unix timestamp for same hour, including DST, in Python?

description: Find next day's Unix timestamp for same hour, including DST, in Python


code:
class Node(object):

    def __init__(self, title, children=None):
        self.title = title
        self.children = children or []

    def __str__(self):
        return self.title

    def __iter__(self):
        yield self
        for child in self.children:
            for node in child:
                yield node

rawDescription: Implementing a depth-first tree iterator in Python


rawDescription: Numpy.unique - getting output with consistent depth


code:
df = pd.DataFrame({"a": [2]*10, "field": range(10)})
ban_field = [3,4,6,7,8]
df[~df.field.isin(ban_field)]

rawDescription: Remove lines in dataframe using a list in Pandas


code:
def delete_blocks(iterable, blocks):
    exclude = set()
    for block in blocks:
        exclude.update(range(*block))
    return [cell for i, cell in enumerate(iterable) if i not in exclude]

# Try it out
test_string = '0123456789abc'
blocks = [(5, 7), (2, 4), (6, 10)]
result = ''.join(delete_blocks(test_string, blocks))

print('Before: {!r}'.format(test_string))
print('Blocks:', blocks)
print('After: {!r}'.format(result))

rawDescription: Python: delete substring by indices

description: Python: delete substring by indices


code:
sm1 = sm.reshape(N*M,L)  # combine 1st 2 dim to simplify repeat
z1 = np.repeat(sm1, N*M, axis=0).reshape(N*M,N*M,L)
x1 = np.sum(sm1 - z1, axis=0).reshape(N,M,L)

rawDescription: Despite many examples online, I cannot get my MATLAB repmat equivalent working in python


code:
map(':'.join, zip(lst[::2], lst[1::2]))  # In Python 2.x
list(map(':'.join, zip(lst[::2], lst[1::2])))  # In Python 3.x

rawDescription: How to connect the list to get ip address with port?

description: Connect the list to get ip address with port


code:
z = y.split(", ")
print(z)

rawDescription: Python - Convert String into Iterable Integer List


rawDescription: show2() method won't display sub classes

description: Show2() method won't display sub classes


rawDescription: numerical sort a column containing numbers and strings (pandas/python)

description: Numerical sort a column containing numbers and strings (pandas/python)


code:
from statistics import mean
import csv

def sort_mean(fle):
    with open(fle) as f:
       for name, *scores in csv.reader(f, delimiter=" "):
            srt = sorted(map(int, scores))
            print("Highest score for {} is  {}".format(name, srt[-1]))
            print("Average score for {} is {}".format(name, mean(srt)))

rawDescription: Python: Import from text file to list and sort/average based on multiple columns


code:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1) # Make example exactly reproducable

num_points = 20
lon = 0.1 * np.random.random(num_points) - 7.5
lat = 0.1 * np.random.random(num_points) + 41.8
z = 0.05 * np.random.random(num_points)

fig, ax = plt.subplots()
artist = ax.scatter(lon, lat, c=z, s=200, cmap='gist_earth')
fig.colorbar(artist)
ax.axis('tight')
plt.show()

rawDescription: How do I reshape a numpy array for use with scipy interpolate?

description: Reshape a numpy array for use with scipy interpolate


code:
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'app.authentication.APIKeyAuthentication',
    )
}

rawDescription: Using Django Scopes to distinguish between user and client


code:
def test_index_page(self):
    res = self.app.get('/')
    self.failUnlessEqual(res.status_int, 200)

rawDescription: Best Way to Run Functional Tests of a WSGI Application?


code:
result = []
# here you need to iterate through the the columns of the first and the rows of
# the second matrix. How you do it, depends on the implementation (how you store
# the matrices). Also, make sure you check the dimensions are the same.
# The simplest case is if you have a list of columns:

N = len(m1)
M = len(m2[0])
for i in range(N):
    for j in range(M):
        results.append(pool.apply_async(getProductElement, (m1, m2, i, j)))

rawDescription: How do I make processes able to write in an array of the main program?

description: Make processes able to write in an array of the main program


code: sorted(children, key=lambda child: (child.noise, -child.fitness))

rawDescription: Sorting based on two variables in python


code: X2 = normalize(X1[:, [3,4]], axis=0) #axis=0 for column-wise

rawDescription: How to normalize only certain columns in scikit-learn?

description: Normalize only certain columns in scikit-learn


rawDescription: Python List Change

description: Python List Change


code:
req = requests.get(server, auth=('user',"pass"))
req.json

rawDescription: Get request python as a string

description: Get request python as a string


rawDescription: Saving and loading multiple objects in python pickle file


code:
import re
# example company page
html = requests.get('http://www.indiainfoline.com/Markets/Company/Adani-Power-Ltd/533096').text
EMAIL_REGEX = re.compile("mailto:([A-Za-z0-9.\-+]+@[A-Za-z0-9_\-]+[.][a-zA-Z]{2,4})")
re.findall(EMAIL_REGEX, html)
# and there you got a list of found emails
...

rawDescription: How can I best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV?

description: Best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV


code:
(data
    .groupby("Region")
    .agg(F.avg("Salary"), F.sum(F.col("IsUnemployed").cast("long"))))

rawDescription: How to count a boolean in grouped Spark data frame

description: Count a boolean in grouped Spark data frame


code: itertools.starmap(afunc, aniter)

rawDescription: Can you apply an operation directly to arguments within map/reduce/filter?

description: Apply an operation directly to arguments within map/reduce/filter


code:
from lxml import html

ms = '''

              Location:

             90 km S. of Prince Rupert

            <mytag>
            Hello World
            </mytag>

'''

mytree = html.fromstring(ms)  #this creates a 'tree' in memory
for e in mytree.iter():       # iterate through the elements
    if e.tag == 'td':         #focus on the elements that are td elements
        if 'location' in e.text_content().lower(): # if location is in the text of a td
            for sib in e.itersiblings(): # find all the siblings of the td
                sib.text_content()   # print the text

rawDescription: How to extract text from html conditionally in beautifulsoup

description: Extract text from html conditionally in beautifulsoup


code:
import calendar, datetime, time

#Timestamp is a datetime object in UTC time
def UTC_time_to_epoch(timestamp):
  epoch = calendar.timegm(timestamp.utctimetuple())
  return epoch

rawDescription: Convert a UTC time to epoch

description: Convert a UTC time to epoch


code:
class TestFunctionToTest(unittest.TestCase):

    @mock.patch('validate._sub_function_1')
    def test_basic(self, m_sub_function_1):
        m_sub_function_1.return_value = 'rv_sf1'
        function_to_test(...)

rawDescription: Confirming function calls inside a function I'm unittesting with mock module


code:
rules = (
    Rule(SgmlLinkExtractor(allow=r'.*'), callback='parse_items', follow=True),
)

def parse_items(self, response):
    sel = Selector(response)

    i = IdeeItem()
    i['website']    =   sel.xpath("""/html/body/table/tr[1]/td[2]/div[4]/div/table/tr[4]/td/h4/text()""").extract()
    i['title']  =   sel.xpath("""/html/body/table/tr[1]/td[2]/div[4]/div/table/tr[2]/td/h4/text()""").extract()
    yield i

    i = IdeeItem()
    i['website']    =   sel.xpath("""/html/body/table/tr[1]/td[2]/div[4]/div/table/tr[6]/td/h4/text()""").extract()
    i['email']  =    sel.xpath("""//html/body/table/tr[1]/td[2]/div[4]/div/table/tr[8]/td/a[1]/text()""").extract()
    yield i

rawDescription: How to crawl several elements at once?

description: Crawl several elements at once


rawDescription: Changing the label for the module when registering on Django admin


code:
def require_post_params(params):
    def decorator(func):
        @wraps(func, assigned=available_attrs(func))
        def inner(request, *args, **kwargs):
            if not all(param in request.POST for param in params):
                return HttpResponseBadRequest()
            return func(request, *args, **kwargs)
        return inner
    return decorator

rawDescription: Python decorator to check for POST parameters on Django


code: [item for item in L if f(item)]

rawDescription: What is a better pythonic version of this conditional deleting?


code:
from kivy.config import Config
Config.set('graphics', 'fullscreen', 'fake')
Config.set('graphics', 'position', 'custom')
Config.set('graphics', 'top', '300')
Config.set('graphics', 'left', '300')

from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
    def build(self):
        button = Button(text="Exit", size_hint=(None, None))
        button.bind(on_press=exit)
        return button

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

rawDescription: How can I hide the main window titlebar and place a transparent background in kivy framework?

description: Hide the main window titlebar and place a transparent background in kivy framework


code:
x = raw_input("What action would you like to take")
character.go(x.split()[-1].lower(), location)

rawDescription: Turn user input strings into a function call


code: sock.connect('192.168.1.35', 9050)  # connect to remote server

rawDescription: Two Raspberries communicating each other


code:
def get_queryset(self):
    # account_id will be None if the second url was the one that matched.
    account_id = self.kwargs.get('account_id', None)

    if account_id:
        # raise 404 if no account is found for the current user
        account = get_object_or_404(Account, pk=account_id, user=self.request.user)

        queryset = Transaction.objects.filter(account=account)
    else:
        # we're going to show all transactions for the user, rather than a specific account
        queryset = Transaction.objects.filter(account__user=self.request.user)

    return queryset

rawDescription: Restricting access to Django generic views


code:
import os
os.system('bash -c vpn-connect')

rawDescription: How to call a self-defined command line function in Python

description: Call a self-defined command line function in Python


rawDescription: Can you "stream" images to ffmpeg to construct a video, instead of saving them to disk?


code:
from itertools import groupby
import json
names = ["name", "description","info","author", "year"]

with open("test.csv") as f, open("out.json","w") as out:
    grouped = groupby(map(str.rstrip,f), key=lambda x: x.startswith("|-"))
    for k,v in grouped:
        if not k:
            json.dump(dict(zip(names,v)),out)
            out.write("\n")

rawDescription: How to parse text file in Python and convert to JSON

description: Parse text file in Python and convert to JSON


code:
for i, trial in dfTrials.iterrows():
    dfTrials.loc[i, "response"] = "answer {}".format(trial["no"])

dfTrials

rawDescription: Edit pandas dataframe row-by-row

description: Edit pandas dataframe row-by-row


code:
df = pandas.DataFrame({"s1_x": scipy.randn(10), "s1_y": scipy.randn(10), "s2_x": scipy.randn(10), "s2_y": scipy.randn(10)})
df.columns = pandas.MultiIndex.from_tuples([tuple(c.split('_')) for c in df.columns])
df.stack(0).reset_index(1)

rawDescription: Reshaping dataframes in pandas based on column labels


code:
import numpy as np
x = np.array([1,1,1,2,2,2,5,25,1,1])
y = np.bincount(x)
ii = np.nonzero(y)[0]

rawDescription: numpy: most efficient frequency counts for unique values in an array

description: Numpy: most efficient frequency counts for unique values in an array


code:
val = 10
set_col = 5

for row in range(rows):
    data[row][set_col] = val

rawDescription: Python - best way to set a column in a 2d array to a specific value


rawDescription: Sort tuples by averages of second element


code:
class A:
    def __init__(self, words):
        self.words = words

a_objects = []
file = open("testfile.txt")
for line in file:
    words = line.split()
    a_objects.append(A(words))

rawDescription: How to create objects for a class automatically?

description: Create objects for a class automatically


rawDescription: Parsing large amount of dates with pandas - scalability - performance drops faster than linear


code:
for (k, v) in non_unique:
    if k in d:
        d[k] += v

rawDescription: How to merge and convert list of tuples to Dict

description: Merge and convert list of tuples to Dict


code:
def words_in_string(word_list, a_string):
    return set(word_list).intersection(a_string.split())

rawDescription: Python: how to determine if a list of words exist in a string


code: A[:, :, None, None] * B

rawDescription: Array of submatrices to be multiplied by array of scalars


code:
three_mon_timedelta = datetime.timedelta(days=3 * 365/12)
TODAY + three_mon_timedelta

rawDescription: How do you add "3 months" to a datetime.date object in python?

description: Add "3 months" to a datetime.date object in python


code:
res = check_output(["nodejs","test.js"])
print(res)
b'Hello World\n'

rawDescription: Save Javascript variable value and read it in python


code:
for event, elem in iterparse(source):
    if elem.tag == "record":
        # do something with the <record> element

        elem.clear()  # clean up

rawDescription: Parsing XML out of big files with Python


code:
myset = set((1, 5, 10, 22))

[...]

if x in myset:
    pass

rawDescription: In Python what's some other ways to write a if x==1 or x==5 or x==10...?


code:
def factor(n, _memo={1: []}):
    """returns a list of the prime factors of integer n"""
    if n <= 0: raise ValueError("Can't factor %r" % n)
    localmemo = {}
    orgn = n
    p = primes.generator()  # yields an infinite iterable
    for x in p:
        if n in _memo:
            for k in localmemo:
                localmemo[k].extend(_memo[n])
            _memo.update(localmemo)
            return _memo[orgn]
        localmemo[n] = []
        if n % x == 0:
            n = n/x
            for k in localmemo:
                localmemo[k].append(x)
            p.send(x)  # get `x` again next time

rawDescription: Critique my prime_factors() function

description: Critique my prime_factors() function


rawDescription: Add a number to the front of a number row python


code:
self.modified_label.configure(image=IMAGE_HERE)
self.modified_label.image = IMAGE_HERE

rawDescription: Python Tkinter buttons to replace labels


code:
import argparse
class RenameAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest,
                argparse.Namespace(
                    **dict(zip(('oldfile', 'newfile'),
                               values))))

parser = argparse.ArgumentParser()
parser.add_argument('--rename', type=str, nargs=2, help='some help',
                    metavar=('OLDFILE', 'NEWFILE'),
                    action=RenameAction)
args = parser.parse_args()

print(args.rename.oldfile)

rawDescription: Python argparse: name parameters

description: Python argparse: name parameters


code: INSTALLED_APPS += get_apps()

rawDescription: building reusable package in django

description: Building reusable package in django


rawDescription: parse query string with urllib in Python 2.4

description: Parse query string with urllib in Python 2.4


code: X[:,None] - w  # or X[:,np.newaxis] - w

rawDescription: Subtracting numpy arrays of different shape efficiently


code:
class PermissionChecker(object):
    @property
    def permissions(self):
        return [self.is_superuser(), self.is_god()]

    def is_superuser(self):
        # use self.property just like self.name...
        return True

    def is_god(self):
        return True

rawDescription: How to use self.method() in class creation time?

description: Use self.method() in class creation time


code: data = np.fromregex(fname, regexp, dtype='f')

rawDescription: NumPy reading file with filtering lines on the fly


code: zip(a, b)

rawDescription: Python adding values from one list to those of another


rawDescription: How to dynamically change the filter in a pandas data with subgroups of another column?

description: Dynamically change the filter in a pandas data with subgroups of another column


code:
@app.route('/<username>/<postname>', methods=['GET'])
def view_user_post(username, postname):
    ...

rawDescription: Flask URL Routing

description: Flask URL Routing


code:
from django.shortcuts import get_object_or_404
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required

@login_required
def crave_edit(request, id=None):
    instance = get_object_or_404(crave_data, pk=id) if id else None
    if instance and instance.person != request.user:
        raise PermissionDenied()
    if request.method=="POST":
        form=CraveDataForm(request.POST, request.FILES, instance=instance)
        if form.is_valid():
            crave_made = form.save(commit=False)
            crave_made.person = request.user
            crave_made.save()
    else:
        form=CraveDataForm(instance=instance)

    return render(request, "crave/crave_edit.html", {'form' : form})

rawDescription: while submitting data in first form blank object is getting created for second form django

description: While submitting data in first form blank object is getting created for second form django


code: [x[::-1] for x in product(numpy.arange(0, 0.3, 0.1), range(5))]

rawDescription: how to iterate using itertools for iterables having different lengths in python?

description: Iterate using itertools for iterables having different lengths in python


code: example.xpath(u'//description')  # returns a list of elements which matched the tag name.

rawDescription: Basic Python XML data extraction

description: Basic Python XML data extraction


code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(X[...,0], Y[...,0], f)
plt.show()

rawDescription: Surface where height is a function of two functions, and a sum over the third


code:
application/core.py
from application.api import resource
...
# api definition

rawDescription: Getting the database connection from application context with Flask-Restful


code: d = json.loads(data)

rawDescription: In place editing of Python dictionary or parsing json to object


code:
test = " " * 16
sys.getsizeof(test)

rawDescription: How can I construct a Python string that is 16 bytes long?

description: Construct a Python string that is 16 bytes long


rawDescription: How to flatten axes of a multidimensional array without making copies in NumPy?

description: Flatten axes of a multidimensional array without making copies in NumPy


code:
from dateutil import parser
parser.parse(s)

rawDescription: not getting date time from correctly formatted string

description: Not getting date time from correctly formatted string


code:
import functools
test_input_a = functools.partial(my_test_input, retval=7)
test_input_b = functools.partial(my_test_input, retval="Foo")

rawDescription: Python 3 unittest simulate user input

description: Python 3 unittest simulate user input


code:
david = car('David', 'Burger King')
david.Driver
david.Odometer

rawDescription: Assignment: Python 3.3: add keys and values from dictionary to class


code:
from wand.image import Image
with Image(filename='logo:') as img:
    img.colorspace = 'gray'
    img.save(filename='logo_gray.jpg')

rawDescription: What's the best way to greyscale in python/django?


code:
class A(object):
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
        self.freeze = True

    def __setattr__(self, attr, value):
        if getattr(self, "freeze", False) and not hasattr(self, attr):
            raise AttributeError("You shall not set attributes!")
        super(A, self).__setattr__(attr, value)

rawDescription: In Python, count the number of variables in a class or prevent adding new class variables


code: new_word = word.replace(word[i], '_ ')

rawDescription: How can I replace the letters of a string with underscores?

description: Replace the letters of a string with underscores


code:
class FileLogObserver:
    """
    Log observer that writes to a file-like object.

    @type timeFormat: C{str} or C{NoneType}
    @ivar timeFormat: If not C{None}, the format string passed to strftime().
    """
    timeFormat = None

    def __init__(self, f):
        self.write = f.write
        self.flush = f.flush

    def getTimezoneOffset(self, when):
        """
        Return the current local timezone offset from UTC.

        @type when: C{int}
        @param when: POSIX (ie, UTC) timestamp for which to find the offset.

        @rtype: C{int}
        @return: The number of seconds offset from UTC.  West is positive,
        east is negative.
        """
        offset = datetime.utcfromtimestamp(when) - datetime.fromtimestamp(when)
        return offset.days * (60 * 60 * 24) + offset.seconds

    def formatTime(self, when):
        """
        Format the given UTC value as a string representing that time in the
        local timezone.

        By default it's formatted as a ISO8601-like string (ISO8601 date and
        ISO8601 time separated by a space). It can be customized using the
        C{timeFormat} attribute, which will be used as input for the underlying
        C{time.strftime} call.

        @type when: C{int}
        @param when: POSIX (ie, UTC) timestamp for which to find the offset.

        @rtype: C{str}
        """
        if self.timeFormat is not None:
            return time.strftime(self.timeFormat, time.localtime(when))

        tzOffset = -self.getTimezoneOffset(when)
        when = datetime.utcfromtimestamp(when + tzOffset)
        tzHour = abs(int(tzOffset / 60 / 60))
        tzMin = abs(int(tzOffset / 60 % 60))
        if tzOffset < 0:
            tzSign = '-'
        else:
            tzSign = '+'
        return '%d-%02d-%02d %02d:%02d:%02d%s%02d%02d' % (
            when.year, when.month, when.day,
            when.hour, when.minute, when.second,
            tzSign, tzHour, tzMin)

    def emit(self, eventDict):
        text = textFromEventDict(eventDict)
        if text is None:
            return

        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t")}
        msgStr = _safeFormat("[%(system)s] %(text)s\n", fmtDict)

        util.untilConcludes(self.write, timeStr + " " + msgStr)
        util.untilConcludes(self.flush)  # Hoorj!

    def start(self):
        """
        Start observing log events.
        """
        addObserver(self.emit)

    def stop(self):
        """
        Stop observing log events.
        """
        removeObserver(self.emit)

rawDescription: How to format twisted logs?

description: Format twisted logs


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

test = pd.DataFrame(np.random.randn(100,3))

bkgd_color='black'
text_color='white'

fig = plt.figure(facecolor=bkgd_color)

ax = fig.add_subplot(1, 1, 1)

chart = test.cumsum().plot(ax=ax)
chart.set_axis_bgcolor(bkgd_color)

# Modify objects to set colour to text_color

# Set the spines to be white.
for spine in ax.spines:
    ax.spines[spine].set_color(text_color)

# Set the ticks to be white
for axis in ('x', 'y'):
    ax.tick_params(axis=axis, color=text_color)

# Set the tick labels to be white
for tl in ax.get_yticklabels():
    tl.set_color(text_color)
for tl in ax.get_xticklabels():
    tl.set_color(text_color)

leg = ax.legend(loc='best') # Get the legend object

# Modify the legend text to be white
for t in leg.get_texts():
    t.set_color(text_color)

# Modify the legend to be black
frame = leg.get_frame()
frame.set_facecolor(bkgd_color)

plt.show()

rawDescription: change a charts border area color

description: Change a charts border area color


code: value = reduce(lambda x, f: f(x), funclist, init_value)

rawDescription: Subsequent Application of Python First Class Functions: Any Way Cleaner Than Nesting?


code:
allowed_words = set(['','word01', np.nan])
set(w.word.unique()) - allowed_words

rawDescription: how to check if a column in a data frame contains only a specific set of values

description: Check if a column in a data frame contains only a specific set of values


code:
max_versions = session.query(Cls.name, func.max(Cls.version).label('max_version'))\
                      .group_by(Cls.name).subquery()
objs = session.query(Cls).join((max_versions,
           and_(Cls.name == max_versions.c.name,
                Cls.version == max_versions.c.max_version)
       )).all()

rawDescription: sqlalchemy - grouping items and iterating over the sub-lists

description: Sqlalchemy - grouping items and iterating over the sub-lists


code: functionName(argument);

rawDescription: vague explanation of an exercise

description: Vague explanation of an exercise


rawDescription: Paste list to an Excel file

description: Paste list to an Excel file


code:
from api import common

@api.route('/users')
def get_users():
    return common.get_users()

rawDescription: Support multiple API versions in flask

description: Support multiple API versions in flask


code:
class A(models.Model):
    att1= models.ForeignKey(B, related_name='A_model')
    att2 = models.CharField(max_length=128)

class B(models.Model):
    ident = models.CharField(max_length=128)

rawDescription: Can I delete an item of a queryset in python but without deleting that item on the database?

description: Delete an item of a queryset in python but without deleting that item on the database


code:
removeset = set(removelist)
newlist = [v for i, v in enumerate(oldlist) if i not in removeset]

rawDescription: Remove multiple items from list in Python