code:
def allpaths(source_node, sink_node, memo_dict = None):
    if memo_dict is None:
        # putting {}, or any other mutable object
        # as the default argument is wrong
        memo_dict = dict()

    if source_node == sink_node: # Don't memoize trivial case
        return frozenset([(source_node,)])
    else:
        pair = (source_node, sink_node)
        if pair in memo_dict: # Is answer memoized already?
            return memo_dict[pair]
        else:
            result = set()
            for new_source in source_node.children:
                paths = allpaths(new_source, sink_node, memo_dict)
                for path in paths:
                    path = (source_node,) + path
                    result.add(path)
            result = frozenset(result)
            # Memoize answer
            memo_dict[(source_node, sink_node)] = result
            return result

rawDescription: list of all paths from source to sink in directed acyclic graph

description: List of all paths from source to sink in directed acyclic graph


rawDescription: Applying the collections.counter() on UTF-8


code:
a = [[('bring', 'VBG'), ('puffy', 'NN'), ('eyes', 'NNS'), ('back', 'RB')], [('droopy','NN'), ('eyes', 'NNS')]]
seen = set()
out_lis = []

out_lis

rawDescription: Delete duplicate tuples from list of list in Python


rawDescription: jsonpath_rw for Python: How to navigate relatively?

description: Jsonpath_rw for Python: How to navigate relatively


code: g = a + b + np.sqrt(d*d + e*e + f*f)

rawDescription: Transfer ownership of numpy data

description: Transfer ownership of numpy data


code:
x = GetData('d:/temp/test.xlsx')
x.data

rawDescription: Python 3.5 - Pandas - Call a method with a for loop from another method


code:
def all_casings(input_string):
    if not input_string:
        yield ""
    else:
        first = input_string[:1]
        if first.lower() == first.upper():
            for sub_casing in all_casings(input_string[1:]):
                yield first + sub_casing
        else:
            for sub_casing in all_casings(input_string[1:]):
                yield first.lower() + sub_casing
                yield first.upper() + sub_casing

rawDescription: Finding all possible case permutations in Python


rawDescription: Combine words with nearest index

description: Combine words with nearest index


code:
with open(fileName, 'br+') as f:
    f.write(b'\x07\x08\x07')

rawDescription: Python writing binary

description: Python writing binary


code:
import binascii
import crc16
hex_string = 'AA01'
crc = crc16.crc16xmodem(binascii.unhexlify(hex_string), 0xffff)
'{:04X}'.format(crc & 0xffff)
format(crc & 0xffff, '04X')

rawDescription: Calculate CRC-CCITT (0xFFFF) for HEX string


code:
t1 = threading.Thread(target=winsound.PlaySound("C:/Interactive Program/LIP Source Files/skyisup.wav", 2), args=(None))
t2 = threading.Thread(target=lipgui.msgbox("The sky is up."), args = (None))

rawDescription: How to run 2 different processes at once in Python

description: Run 2 different processes at once in Python


code: str(intersect(a, b)).replace('(', '').replace(')', '')

rawDescription: Getting rid of parentheses after dictionary intersection


code:
from itertools import permutations
from random import randint
def a(*args):
   return args[randint(0, len(args) - 1)]
sum(a(*perm) for perm in permutations((1, 2, 3, 4)))

rawDescription: Python : defining function that returns summation of all permutations


rawDescription: Replacing Names with Index From Separate File using Pandas


rawDescription: Python function that returns the value at index 0?


code:
#!/usr/bin/env python3
import subprocess

cmd = r'''
  $OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
  py -3 -c "import os; os.write(1, '\U0001f60a'.encode('utf-8')+b'\n')" |
  py -3 -c "import os; print(os.read(0, 512))"
'''
subprocess.check_call(["powershell", "-C", cmd])

rawDescription: Decode poweshell output possibly containing non-ascii unicode characters into a python string


code:
class SomeObj:
    @coroutine
    def __contains__(self, obj):
        exists = yield self.somemethod(obj)
        return exists

rawDescription: Python coroutines on builtin functions

description: Python coroutines on builtin functions


rawDescription: Django: Context processors in views, bad practice?


code:
oldlist = ['a','b','c']
enumerate(oldlist)
list(enumerate(oldlist))  # default start = 0
list(enumerate(oldlist, 1))  # explicitly specify `start`

rawDescription: Using lambda to create new list by altering/modifying old list


rawDescription: Slicing pandas groupby groups into equal lengths


code:
foo = Foo()
foo.__dict__['man'] = 3.14
print(foo.man)  # 3.14

rawDescription: Dynamically creating namespaces in Python


code:
class _Quicksort(object):
    count = 0
    def _quicksort(self, A, left=None, right=None):
        ...
        self._quicksort(A, ...)
        self.count += ...

rawDescription: Counting comparisons quicksort without global Python


code: object1.value_changed.connect(lambda val: self.store_var('var1', val))

rawDescription: PyQt, Python 3: Lambda slot assigning signal argument to a variable?


code:
class AccountDetails(models.Model):

    @classmethod
    def get_locked_for_update(cls, user):
        return cls.objects.raw(
            "SELECT * FROM yourapp_accountdetails FOR UPDATE WHERE user = %s", [user.id]
        )

rawDescription: select_for_update kind of functionality in django 1.3 to avoid race condition

description: Select_for_update kind of functionality in django 1.3 to avoid race condition


code:
@app.route('/getmethod/<jsdata>')
def get_javascript_data(jsdata):
    return json.loads(jsdata)[0]

rawDescription: How do I send data from JS to Python with Flask?

description: Send data from JS to Python with Flask


rawDescription: grouping a list in individual lists in a base list or completely individual in maya python

description: Grouping a list in individual lists in a base list or completely individual in maya python


code:
a = range(17, 30)

for i, item in enumerate(a):
    if (item % 3) == 0:
        print(a)
        print("del  {0}:{1}".format(i, item))
        del a[i]

rawDescription: what did Python do when I remove item from list in a loop

description: What did Python do when I remove item from list in a loop


rawDescription: What is the way data is stored in *.npy

description: What is the way data is stored in *.npy


code:
y_train_text = [["new york"],["new york"],["new york"],
                ["new york"],["new york"],["new york"],
                ["london"],["london"],["london"],["london"],
                ["london"],["london"],["new york","England"],
                ["new york","london"]]

rawDescription: Using MultilabelBinarizer on test data with labels not in the training set


code:
import numpy as np

def namestr(obj, namespace):
    return [name for name in namespace if namespace[name] is obj]

my_numpy = np.zeros(2)
print(namestr(my_numpy, globals()))

rawDescription: python - name of np array variable as string

description: Python - name of np array variable as string


code:
net = tflearn.input_data (shape=[None, train.shape [1]])
INPUT = net
net = tflearn.fully_connected (net, 500, activation  = 'tanh', regularizer = None, name = 'fc_en_1')

#hidden state
net = tflearn.fully_connected (net, 100, activation  = 'tanh', regularizer = 'L1', name = 'fc_en_2', weight_decay = 0.0001)
HIDDEN_STATE = net

net = tflearn.fully_connected (net, 500, activation  = 'tanh', regularizer = None, name = 'fc_de_1')
net = tflearn.fully_connected (net, train.shape [1], activation  = 'linear', name = 'fc_de_2')
OUTPUT = net
net = tflearn.regression(net, optimizer='adam', learning_rate=0.01, loss='mean_square', metric='default')

model = tflearn.DNN (net)

rawDescription: How to separate autoencoder into encoder and decoder (thensorflow + tflearn)

description: Separate autoencoder into encoder and decoder (thensorflow + tflearn)


code:
test[np.in1d(test, states)] = 1
test

rawDescription: Check if values in a set are in a numpy array in python


code:
cursor.execute(sql)
row = cursor.fetchone()
newID = row[0] + 1

rawDescription: Converting a tuple into an integer in python 3?


code:
all_data = [['abc', 123], ['cde', 234], ['fgh', 345]]
ids = set(['abc', 'fgh'])
values = filter(lambda value: value[0] in ids, all_data)
values

rawDescription: Filtering a list based on a second list

description: Filtering a list based on a second list


code: dataDaily = data.resample('D', how=lambda x: np.percentile(x['Col1'], q=90))

rawDescription: Pandas resampling using numpy percentile?


code: logout_url = users.create_logout_url("/") if users.is_current_user_admin() else None

rawDescription: GAE, oauth2, and admin users

description: GAE, oauth2, and admin users


code:
class MyWindow(QtWidgets.QDialog):
    def __init__(self, *args, **kwargs):
        super(MyWindow, self).__init__(*args, **kwargs)
        self.txt_input = QtWidgets.QLineEdit()
        self.btn_submit = QtWidgets.QPushButton('&Отправить')
        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addWidget(self.txt_input)
        self.vbox.addWidget(self.btn_submit)
        self.setLayout(self.vbox)

rawDescription: Returning value from QObject's method that uses signals


rawDescription: Issues with Bytes from a Microcontroller in Python


code:
Contacts = []
for j in range(count):
  fields = []
  for i in range(5):
    fields.append(next(fh))
  Contacts.append(Contact(*fields))

rawDescription: Creating list of Class Objects

description: Creating list of Class Objects


code:
query = service.data().ga().get(ids='ga:%d' % PROFILE_ID, start_date=START_DATE, end_date=END_DATE,metrics='ga:pageviews')
results = query.execute()

rawDescription: Google Analytics Core Reporting API Python library


code:
driver.find_element_by_class_name("btn__content").click() # <span> tag
# or
driver.find_element_by_class_name("btn__spinner").click() # <i> tag

rawDescription: Selenium python click button

description: Selenium python click button


code:
list1 = {1, 2, 3, 4}
list2 = {1, 2, 4}
set.difference(list1, list2)

rawDescription: Difference between lists in Python

description: Difference between lists in Python


code: expected_rows = [get_expected_rows(**row) for row in rows]

rawDescription: Python unpacking list value in kwargs

description: Python unpacking list value in kwargs


code: [(int(x) if x else 0) for x in data.split(',')]

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

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


code:
# salt size in bytes
SALT_SIZE = 16

# number of iterations in the key generation
NUMBER_OF_ITERATIONS = 20

# the size multiple required for AES
AES_MULTIPLE = 16

rawDescription: Encrypt / decrypt data in python with salt


code:
data = [['Three','One','Ten'],
 ['Three','Five','Nine'],
 ['Two','Five','Three'],
 ['Two','Three','Eight'],
 ['One','Three','Nine']]

from collections import Counter
[Counter(x) for x in data]

rawDescription: numpy - how do I count the occurrence of items in nested lists by index?

description: Numpy - how do I count the occurrence of items in nested lists by index


rawDescription: Python undo method mock

description: Python undo method mock


code:
from __future__ import division # this must be the very first import statement
score = sum(i*w(i) for i in x if i in y) / sum(i * w(i) for i in x)

rawDescription: Syntax of Mathemathical formula in Python


rawDescription: Combining all combinations of two lists into a dict of special form


code:
import cherrypy

class Band(object):
    def __init__(self):
        self.albums = Album()

    def _cp_dispatch(self, vpath):
        if len(vpath) == 1:
            cherrypy.request.params['name'] = vpath.pop()
            return self

        if len(vpath) == 3:
            cherrypy.request.params['artist'] = vpath.pop(0)  # /band name/
            vpath.pop(0) # /albums/
            cherrypy.request.params['title'] = vpath.pop(0) # /album title/
            return self.albums

        return vpath

    @cherrypy.expose
    def index(self, name):
        return 'About %s...' % name

class Album(object):
    @cherrypy.expose
    def index(self, artist, title):
        return 'About %s by %s...' % (title, artist)

if __name__ == '__main__':
    cherrypy.quickstart(Band())

rawDescription: Friendly URL for a REST WebService with CherryPy


rawDescription: How to select an option from a dropdown menu via where it is in the list?

description: Select an option from a dropdown menu via where it is in the list


rawDescription: Pythonic style for stepping through two lists and avoiding idx?


code:
import xml.sax

class MyXsdHandler(xml.sax.handler.ContentHandler):
    def parse(self, f):
        self.result = {}
        s = open(f, "r").read()
        xml.sax.parseString(s, self)
        return self.result

    def startElement(self, name, attrs):
        if name == "xsd:element":
            d = dict(attrs)
            self.result[attrs["name"]] = d

results = MyXsdHandler().parse("myfile.xsd")

rawDescription: How to get tag name and its corresponding attributes as a key:value pair from a xsd file

description: Get tag name and its corresponding attributes as a key:value pair from a xsd file


rawDescription: how to retrieve field value (values are itself string)in given line using regular expression in python

description: Retrieve field value (values are itself string)in given line using regular expression in python


code: re.sub(r'\d+\.(\d+)-\d+', lambda m: format(int(m.group(1)), ','), inputtext)

rawDescription: Python replace middle digits with commas thousand separator


code: [[row[i] for row in t] for i in range(len(t[1]))]

rawDescription: Transpose of a Matrix

description: Transpose of a Matrix


code:
from itertools import count, product, islice
from string import ascii_uppercase

def multiletters(seq):
    for n in count(1):
        for s in product(seq, repeat=n):
            yield ''.join(s)

rawDescription: Python: Pair alphabets after loop is completed


code:
query = db.session.query(Tag.title, db.func.count(Post.id)).join(Post.tags).group_by(Tag.id)

# This will give you a dictionary with keys the tag titles, and values the count of each
# Because you can iterate over the query, which will give you the results
# Or you can use query.all() and use it as you prefer.
results = dict(query)

rawDescription: Flask how to calculate tags count

description: Flask how to calculate tags count


rawDescription: Flask WTF-forms adding select and textarea


code:
for pageIndex in range(1, 5011):
    # Navigate to the search page
    browser.get("https://www.houz.com/page_num="+ str(pageIndex))
    time.sleep(6)

    links = browser.find_elements_by_css_selector('div > h3 > a')
    jobs = browser.find_elements_by_css_selector('div > div.description')

    for link, title in zip(links, jobs):
        url = link.text
        jobtitle = title.text

        with open("1Exportdata.csv", "a") as csvFile:
            csvFile.write(url + "," + jobtitle + "\n")

rawDescription: How to Print the information using selenium

description: Print the information using selenium


code: df[ df.apply( lambda x: abs(x) > x.mean() ) ] = 2

rawDescription: Transforming outliers in Pandas DataFrame using .apply, .applymap, .groupby


code:
from twisted.protocols.policies import LimitConnectionsByPeer
...
factory = ConsoleSMTPFactory()
wrapper = LimitConnectionsByPeer(ConsoleSMTPFactory(...))
reactor.listenTCP(465, wrapper)

rawDescription: Logging SMTP connections with Twisted

description: Logging SMTP connections with Twisted


rawDescription: Pandas Filter function returned a Series, but expected a scalar bool


rawDescription: List of ints to string

description: List of ints to string


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.settings import Settings
from scrapy.crawler import Crawler
from scrapy.statscol import StatsCollector

class MySpider(CrawlSpider):
    name = "mySpider"
    def get_url():
        url = raw_input('Enter the url of your website (including the http)')
        return url
    start_urls = [str(get_url())]
    extractor = SgmlLinkExtractor()
    rules = (Rule(extractor, callback='web_crawling',follow=True),)
    def web_crawling(self, response):
       settingObj=Settings()
       crawler=Crawler(settingObj)
       stat = StatsCollector(crawler)
       depth = stat.get_value('pages_crawled')
       return depth

rawDescription: Simple scrapy spider doesn't crawl

description: Simple scrapy spider doesn't crawl


code:
from django.template import Context, Template
t = Template('Starting in {{Duration}}, the {{Noun}} will be {{Adjective}}')
c = Context(dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken'))
t.render(c)

rawDescription: Replacing words in a sentence from database (Python/Django)


code:
import math
pi = math.pi
sin = math.sin
cos = math.cos

def createSphere(r, N=10):
    lst = []
    thetas = [(2*pi*i)/N for i in range(N)]
    phis = [(pi*i)/N for i in range(N)]
    for theta in thetas:
        for phi in phis:
            x = r * sin(phi) * cos(theta)
            y = r * sin(phi) * sin(theta)
            z = r * cos(phi)
            lst.append((x, y, z))
    return lst

rawDescription: Python program to create sphere coordinates not working


code: open(x, 'a').close()

rawDescription: Create empty file using python

description: Create empty file using python


code: (self.list[L][0] == operand.list[R][0]) and op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0)

rawDescription: How to do this conditional statement?

description: Do this conditional statement


code:
df = df.reset_index()
df['count'] = df.groupby('Type').apply(lambda x : pd.Series(np.arange(len(x)), x.index))
df.set_index(['Type', 'count'])

rawDescription: grouping or merging? dataframes pandas python

description: Grouping or merging? dataframes pandas python


code: lmod = [x[8:] for x in l]

rawDescription: How to exclude some indexes in a python loop?

description: Exclude some indexes in a python loop


code:
# read in the input in a chunked way
clean_input_reader = read_csv('input.csv', chunksize=500000)
with get_store('output.h5') as store:
    for chunk in clean_input_reader:

        # create a grouper for each chunk using the sub_group_hash
        g = chunk.groupby(sub_group_hash)

        # append each of the subgroups to a separate group in the resulting hdf file
        # this will be a loop around the sub_groups (100 max in this case)
        for grp, grouped in g:

            store.append('group_%s' % grp, grouped,
                         data_columns=['user_id','timestamp','category_clicked'],
                         min_itemsize=15)

rawDescription: Trouble with grouby on millions of keys on a chunked file in python pandas


code:
@memoized
def get_dict():
    return pickle.load(file)

foo = get_dict()['foo']

rawDescription: saving the file content in the memory

description: Saving the file content in the memory


code:
import re
regex = r'^home/(?P<text>[\w\s]{10})$'
re.search(regex, "home/1234567890").group(1)
re.search(regex, "home/12345 7890").group(1)
re.search(regex, "home/ abcd fghi").group(1)

rawDescription: Django URL matching any 140 characters

description: Django URL matching any 140 characters


code:
pki_data = X509Certificates()
certificates = [your_cert_der_data, root_cert_der_data]
for cert in certificates:
    pki_data.certificate.append(cert)
request.pki_data = pki_data.SerializeToString()

rawDescription: How to add signed certificates to a bitcoin bip70 payment message? python

description: Add signed certificates to a bitcoin bip70 payment message? python


code:
def read(blockSize): #blockSize is in bytes, varies from 1 byte, 1 KB and 1 MB
    loops = 1048576 * fileSize / blockSize #number of iterations, fileSize is 100 (Mb)
    with open("foo.txt") as fp:
        # Lies to OS to tell it we won't need any of the data
        os.posix_fadvise(fp.fileno(), 0, fileSize, os.POSIX_FADV_DONTNEED)
        # Changed our mind! Read it fresh
        os.posix_fadvise(fp.fileno(), 0, fileSize, os.POSIX_FADV_NORMAL)

        for j in xrange(loops):
            fp.read(blockSize)

rawDescription: How to clear my caches?

description: Clear my caches


code: '\n'.join('{}'.format(item) for item in myList)

rawDescription: print list elements line by line - is it possible using format

description: Print list elements line by line - is it possible using format


code:
def pow_to_mul(expr):
    """
    Convert integer powers in an expression to Muls, like a**2 => a*a.
    """
    pows = list(expr.atoms(Pow))
    if any(not e.is_Integer for b, e in (i.as_base_exp() for i in pows)):

        raise ValueError("A power contains a non-integer exponent")
    repl = zip(pows, (Mul(*[b]*e,evaluate=False) for b,e in (i.as_base_exp() for i in pows)))
    return expr.subs(repl)

rawDescription: Expanding algebraic powers in python (sympy)


code:
import spc
import matplotlib.pyplot as plt
x = [25,19,14,17,25,39,49,6,11,19,13,26,24,32,14,19]
cc = spc.Spc(x, spc.CHART_X_MR_X)
cc.get_chart()
plt.show()

rawDescription: Control Charts in Python

description: Control Charts in Python


code:
usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose", default=True,
                  help="make lots of noise [default]")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose",
                  help="be vewwy quiet (I'm hunting wabbits)")
parser.add_option("-f", "--filename",
                  metavar="FILE", help="write output to FILE")
parser.add_option("-m", "--mode",
                  default="intermediate",
                  help="interaction mode: novice, intermediate, "
                       "or expert [default: %default]")

rawDescription: Python 2.6 - Parse arguments

description: Python 2.6 - Parse arguments


code: scipy.stats.hypergeom.cdf(k, M, n, N)

rawDescription: Drawing cards from a deck in SciPy with scipy.stats.hypergeom


rawDescription: How do I avoid text between {{ }}

description: Avoid text between {{ }}


code:
for key in dic:
    if dic[key] == 0:
        print("The value for key {} was 0".format(key))

rawDescription: For loops in a dictionary and if statement


code:
class GalleryImage(Orderable):
    gallery = models.ForeignKey(Gallery, related_name="images")
    file = models.ImageField(upload_to="galleries")

rawDescription: Django-admin: images in zip file and inserting each image info in a database


rawDescription: How to read a file with variable multi-row data in Python

description: Read a file with variable multi-row data in Python


rawDescription: Data parsing from a table with BeautifulSoup


code:
from copy import deepcopy
my_table_no_headers= deepcopy(my_table)
my_table_no_headers.pop(0)
first_trip_id= my_table_no_headers[0][0]

rawDescription: How can I store a 2D array in python, like table in MySQL?

description: Store a 2D array in python, like table in MySQL


code:
def shift(ch, k):
    return chr(ord('a') + ((ord(ch) - ord('a')) +
                           (ord(k) - ord('a'))) % 26)

rawDescription: How to make shift('w','f') return 'b'?

description: Make shift('w','f') return 'b'


code:
f = FileLineWrapper(open("myfile.txt", "r"))
f.readline()
print(f.line)

rawDescription: Is there an easy way to tell which line number a file pointer is on?


code:
from moviepy.editor import *
clip  = VideoFileClip("original_file.mp4")
new_frames = [ some_effect(frame) for frame in clip.iter_frames()]
new_clip = ImageSequenceClip(new_frames, fps=clip.fps)
new_clip.write_videofile("new_file.mp4")

rawDescription: Moviepy - avoid writing to disk with ImageSequenceClip?


code: s = "I am going to apply for a job".split('pp')

rawDescription: Split string into two parts at double consonants in Python


code:
while True:
    name = input("Enter your name or press q to quit")
    if name == "q":
        break    # if user enters `q`, we will end the loop with this break statement
    print("Hi", name ,'!') # or else just print the name

rawDescription: Rebooting program after input() automatically on python


code:
operators = [operator.add, operator.sub, operator.mul]
random_operator = random.choice(operators)

rawDescription: Implement a random math operation (+, -, or *)


code:
class a:
     def __init__(self, text):
         self.text = text
     def speak(self):
         print(self.text+"!")
class b:
     def __init__(self, text):
         self.text = text
     def speak(self):
         print(self.text+" world!")

# Fake up 'some_other_module'
import sys, imp
fake_module = imp.new_module('some_other_module')
fake_module.a = a
fake_module.b = b
sys.modules['some_other_module'] = fake_module

# Now you can just import module.py, and it'll bind to the fake module
import module

rawDescription: Overwrite Python library dependencies

description: Overwrite Python library dependencies


rawDescription: Extract digits from string by condition

description: Extract digits from string by condition


code:
from z3c.form.browser.checkbox import CheckBoxFieldWidget
from z3c.form.form import Form

class EmailPreferenceForm(Form):

    fields = z3c.form.field.Fields(IEmailPreference)
    fields['email_optin'].widgetFactory = CheckBoxFieldWidget

rawDescription: Pre-tick specific checkbox in z3c.form list


rawDescription: Python Coin change SO CLOSE

description: Python Coin change SO CLOSE


rawDescription: How do I put all the strings that match my regular expression into a list in Python?

description: Put all the strings that match my regular expression into a list in Python


code: [sum(x) for x in zip(a,b)]

rawDescription: Adding two tuples elementwise

description: Adding two tuples elementwise


rawDescription: isinstance() is not behaving as I expect

description: Isinstance() is not behaving as I expect


code:
region = pd.DataFrame(df.pop("region").tolist())
df = df.join(region, rsuffix="_region")

rawDescription: Searching a Pandas series using a string produces a KeyError


code:
my_dictionary = {'foo' : 10, 'bar' : 20}

variable = my_dictionary['foo']
new_variable = 0 # Get the value from another place
new_variable += variable
my_dictionary['foo'] = new_variable

rawDescription: How do I assign a dictionary value to a variable in Python?

description: Assign a dictionary value to a variable in Python


rawDescription: Pythonic way to merge two overlapping lists, preserving order


rawDescription: Python's 2.7 socket.timeout behavior

description: Python's 2.7 socket.timeout behavior


rawDescription: Parsing Dictionary Into Spreadsheet in Python


code:
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(64))

    comments = relationship("Comment",
                        secondary=Table('user_comment', Base.metadata,
                            Column('user_id', ForeignKey('user.id'), primary_key=True),
                            Column('comment_id', ForeignKey('comment.id'), primary_key=True)
                        ),
                        order_by="Comment.date.desc()"
                )

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

rawDescription: sqlalchemy association_proxy pagination

description: Sqlalchemy association_proxy pagination


code: distance(list1[3:5], list2[3:5]) # take just list[3] and list[4] in consideration

rawDescription: calculate the distance of data on specific column using python language

description: Calculate the distance of data on specific column using python language


code: self.myList.extend([0] * (4 - len(self.myList)))

rawDescription: Padding a list in python with particular value


code: all_links = [tag['href'] for tag in soup.select('p a[href]')]

rawDescription: How to find all links in all paragraphs in Beautiful Soup

description: Find all links in all paragraphs in Beautiful Soup


code:
# /usr/lib/riak/erts-5.9.1/bin/erlc custom_mr.erl
# mkdir /var/lib/riak/custom_code
# mv custom_mr.beam /var/lib/riak/custom_code
# chown -R riak:riak /var/lib/riak/custom_code

rawDescription: What is the exact way to send erlang module and erlang function to the mapreduce face in python-riak client l


rawDescription: Why does this not work for project euler part 2? (Python) project euler instructions included


code:
# foo/some_foo.py
from something import some_module

rawDescription: import python module from another package

description: Import python module from another package


code: c = Counter(f.read().strip())

rawDescription: Count frequency of letters in a text file


rawDescription: Call a method of an object with arguments in Python


code:
import time
time.strftime("%I %p")

rawDescription: What's the fastest way to get time in am/pm?


code: [C.indices[C.indptr[i]:C.indptr[i+1]] for i in range(len(C.indptr)-1)]

rawDescription: Fastest way from logic matrix to list of sets


code:
#!/usr/bin/env python
import os
import subprocess

subprocess.check_call('date', env=dict(os.environ, TZ='Australia/Adelaide'))
# -> Fri Sep 25 05:02:34 ACST 2015

rawDescription: Python subprocess.call issue

description: Python subprocess.call issue


code:
import pandas as pd
dfs = list()
for filename in filenames:
    df = pd.read_csv(filename, parse_dates='Date', index_col='Date')
    # compute moving_mean
    dfs.append(df)

keys = [filename[:-4] for filename in filenames]
result = pd.concat(dfs, keys=keys, names=['ticker']).unstack('ticker')

rawDescription: pandas - merging multiple DataFrames

description: Pandas - merging multiple DataFrames


code: ticklabes = [r"$10^{{{}}}$".format(tick) for tick in ticks]

rawDescription: Aspect ratio in semi-log plot with Matplotlib


code:
def foo(): # Remove this comment
    ''' But do not remove this #1 docstring
    '''
    # Another comment
    pass

rawDescription: python regex to remove comments

description: Python regex to remove comments


code:
if name == sub_dict["name"]:
    # Update *the reference* to point at a new dictionary
    base_system_dict["base_system"] = changed_json_dict

rawDescription: Does changing a global variable in a recursive function change the top level


rawDescription: How to create multiple value dictionary from pandas data frame

description: Create multiple value dictionary from pandas data frame


code:
#mymodel.py
class MyModel(models.Model):
    ...
    class Meta:
        app_label = 'myapp'

rawDescription: A lot functions in Django Model, way to separate it?


code:
with open(your_file) as f:
    for line in f:
       if line.startswith("Password"):
            password = line.rstrip().split(None,1)[1]
            # do your check

rawDescription: Read specific area or string in a text file - Python 3.4.2


code: isinstance(other,(int,long,float))

rawDescription: Override __mul__ from a child class using parent implementation: leads to problems


rawDescription: Count unique elements row wise in an ndarray


code:
q = collections.deque([1,2,3,4,5,6,7,8])
for _ in xrange(5):
    q.rotate(-1)

q

rawDescription: Python array manipulation

description: Python array manipulation


code:
"""Allow a simple way to ensure execution is confined to one thread.

This module defines the Affinity data type that runs code on a single thread.
An instance of the class will execute functions only on the thread that made
the object in the first place. The class is useful in a GUI's main loop."""

__author__ = 'Stephen "Zero" Chappell <Noctis.Skytower@gmail.com>'
__date__ = '4 June 2012'
__version__ = 1, 0, 0

################################################################################

import sys
import _thread
import queue

################################################################################

def slots(names=''):
    "Sets the __slots__ variable in the calling context with private names."
    sys._getframe(1).f_locals['__slots__'] = \
        tuple('__' + name for name in names.replace(',', ' ').split())

################################################################################

class Affinity:

    "Affinity() -> Affinity instance"

    slots('thread, action')

    def __init__(self):
        "Initializes instance with thread identity and job queue."
        self.__thread = _thread.get_ident()
        self.__action = queue.Queue()

    def __call__(self, func, *args, **kwargs):
        "Executes function on creating thread and returns result."
        if _thread.get_ident() == self.__thread:
            while not self.__action.empty():
                self.__action.get_nowait()()
            return func(*args, **kwargs)
        delegate = _Delegate(func, args, kwargs)
        self.__action.put_nowait(delegate)
        return delegate.value

################################################################################

class _Delegate:

    "_Delegate(func, args, kwargs) -> _Delegate instance"

    slots('func, args, kwargs, mutex, value, error')

    def __init__(self, func, args, kwargs):
        "Initializes instance from arguments and prepares to run."
        self.__func = func
        self.__args = args
        self.__kwargs = kwargs
        self.__mutex = _thread.allocate_lock()
        self.__mutex.acquire()

    def __call__(self):
        "Executes code with arguments and allows value retrieval."
        try:
            self.__value = self.__func(*self.__args, **self.__kwargs)
            self.__error = False
        except:
            self.__value = sys.exc_info()[1]
            self.__error = True
        self.__mutex.release()

    @property
    def value(self):
        "Waits for value availability and raises or returns data."
        self.__mutex.acquire()
        if self.__error:
            raise self.__value
        return self.__value

rawDescription: Send/sync variable/text over LAN in Python 3


code:
var1, var2 = divmod(delta_ms,1000)
mins,secs,ms = divmod(var1,60) + (var2,)

rawDescription: An elegant one-line solution to extract data from Nested Tuples from divmod


code:
jrb = User.objects.create(username='jrb')
bill = User.objects.create(username='bill')
bob = models.Tenant.objects.create(name="Bob")
models.Room.objects.create(owner=jrb, maintainer=bill, tenant=bob)

User.objects.filter(owns__tenant=bob)

rawDescription: How to perform queries in Django following double-join relationships (or: How to get around Django's restrictions on ManyToMany "through" models?)

description: Perform queries in Django following double-join relationships (or: How to get around Django's restrictions on ManyToMany "through" models?)


code:
with open("some.txt") as f:
    file_data = f.read()

rawDescription: count the number of a certain triplet in a file (DNA codon analysis)

description: Count the number of a certain triplet in a file (DNA codon analysis)


code:
def module(path):
    def decorator(cls):
        if '.' in path:
            parent_path, name = path.rsplit('.', 1)
            module(parent_path)(type(name, (object,), { name: cls }))

        return sys.modules.setdefault(path, cls)
    return decorator

rawDescription: django: defining tags/filters/manager actions with less ceremony

description: Django: defining tags/filters/manager actions with less ceremony


rawDescription: pandas read in txt file without headers

description: Pandas read in txt file without headers


code: dbobject = mymodel.objects.all()

rawDescription: Python, Django, how to use getattr (or other method) to call object that has multiple attributes?


code:
def pheromone():
    grid_dict[(ant_position[0], ant_position[1])]

rawDescription: Is there something in python i can use to represent any positive whole number


code:
cursor = database.cursor()
cursor.execute("""UPDATE g_ent
                  SET total_points = total_points + %s
                  WHERE e_id = %s AND user = %s;""",
               (new_points, e_id, user_name)) # order of params revised
database.commit()

rawDescription: MySQL select and update in one query

description: MySQL select and update in one query


code:
console = logging.StreamHandler()
console.setFormatter(logging.Formatter(fmt))
logger.addHandler(console)

logger.error('hello')

rawDescription: How to decorate console logger messages in Python?

description: Decorate console logger messages in Python


code:
d = {}
d.setdefault(3, []).append(100)
d

rawDescription: Creating a dict of blank lists that are not the same list


code: L[:] = [s + '.txt' for s in L]

rawDescription: How to modify list entry in python 2.x, using loop

description: Modify list entry in python 2.x, using loop


rawDescription: Regex replace in List Comprehension in Python


rawDescription: split a 256 bit hash into 32 bit prefix in python

description: Split a 256 bit hash into 32 bit prefix in python


code: text_size = self.size

rawDescription: Python Kivy: Align text to the left side of a Label


code:
from itertools import izip_longest
''.join([l
    for pair in izip_longest(sample[::2].upper(), sample[1::2].lower(), fillvalue='')
    for l in pair])

rawDescription: Having trouble with whitespace and strings in a function


code:
width = GetDeviceCaps(screen, HORZRES);
height = GetDeviceCaps(screen, VERTRES);

rawDescription: Python PIL library-- Is there a way to select which screen ImageGrab.grab() grabs in a multi-monitor setup?


code:
class SignUpView(SomeMixin, FormView):

    def form_valid(self, form):
        ...

rawDescription: Django: How to provide context for a FormView?


code:
if __name__ == '__main__':
    import psutil
    system_process_names = {'smss.exe',
                            'csrss.exe',
                            'wininit.exe',
                            'winlogon.exe',
                            'services.exe',
                            'lsass.exe',
                            'lsm.exe'}
    system_processes = []

    print('SeDebugPrivilege Enabled')
    enable_privilege('SeDebugPrivilege')
    for proc in psutil.process_iter():
        try:
            name = proc.name().lower()
            path = proc.exe()
        except psutil.AccessDenied:
            print('{:04d} ACCESS_DENIED'.format(proc.pid))
            continue
        if name in system_process_names:
            system_process_names.remove(name)
            system_processes.append(proc)
            print('{:04d} {}'.format(proc.pid, path))
    assert not system_process_names

    print('\nSeDebugPrivilege Disabled')
    disable_privilege('SeDebugPrivilege')
    for proc in system_processes:
        try:
            path = psutil.Process(proc.pid).exe() # avoid cache
        except psutil.AccessDenied:
            path = 'ACCESS DENIED'
        print('{:04d} {}'.format(proc.pid, path))

rawDescription: Find the path of every running process in Python


code:
def iterate_strings(n):
    yield ''
    if n <= 0:
        return
    for c in string.ascii_lowercase:
        for s in iterate_strings(n - 1):
            yield c + s

rawDescription: increasing string size through loop

description: Increasing string size through loop


rawDescription: putting value of one array into a list in python

description: Putting value of one array into a list in python


code:
import itertools
import scipy.stats
import numpy as np
import matplotlib.pyplot as plt

# Generate some data... (Using a list to hold it so that the datasets don't
# have to be the same length...)
numdatasets = 4
stds = np.random.randint(1, 10, size=numdatasets)
means = np.random.randint(-5, 5, size=numdatasets)
values = [std * np.random.randn(100) + mean for std, mean in zip(stds, means)]

# Set up several subplots
fig, axes = plt.subplots(nrows=1, ncols=numdatasets, figsize=(12,6))

# Set up some colors and markers to cycle through...
colors = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
markers = itertools.cycle(['o', '^', 's', r'$\Phi$', 'h'])

# Now let's actually plot our data...
for ax, data, color, marker in zip(axes, values, colors, markers):
    counts, start, dx, _ = scipy.stats.cumfreq(data, numbins=20)
    x = np.arange(counts.size) * dx + start
    ax.plot(x, counts, color=color, marker=marker,
            markersize=10, linestyle='none')

# Next we'll set the various labels...
axes[0].set_ylabel('Cumulative Frequency')
labels = ['This', 'That', 'The Other', 'And Another']
for ax, label in zip(axes, labels):
    ax.set_xlabel(label)

plt.show()

rawDescription: Python: Matplotlib - probability plot for several data set


code:
aab
abc
acc

rawDescription: accumulating results for each row in a text while looping

description: Accumulating results for each row in a text while looping


code: que_garden = que.get()

rawDescription: How to increment class field within a Thread in python?

description: Increment class field within a Thread in python


code:
def seq_pow2(N):
    shp = 2*np.ones(np.log2(N),dtype=int)
    return np.arange(N).reshape(shp).T.ravel()

rawDescription: Iterate through a dynamic number of for loops (Python)


code:
key = lambda job: A.index(job[0])
jobs = OrderedDict(sorted(jobs.items(), key=key))

rawDescription: How to do sorting inside a python dictionary from a predefined list

description: Do sorting inside a python dictionary from a predefined list


code: potential_names = [x[:-3] for x in potential_names]

rawDescription: file.write() not permanently writing to file python

description: File.write() not permanently writing to file python


code:
def listdir_nohidden(path):
    return glob.glob(os.path.join(path, '*'))

rawDescription: How to ignore hidden files using os.listdir()? - Python

description: Ignore hidden files using os.listdir()? - Python


code:
class MyClass:
    ...

MyClass.instance = MyClass()

rawDescription: How to delete (tearDown) a singleton instance decorator in Python for unit testing?

description: Delete (tearDown) a singleton instance decorator in Python for unit testing


code:
from numpy import matrix
d = matrix(g.degree())
pref_score_matrix = d.T*d

rawDescription: Common neighbors and preferential attachment score matrixes using igraph for python


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

my_cmap = copy.copy(plt.cm.get_cmap('gray')) # get a copy of the gray color map
my_cmap.set_bad(alpha=0) # set how the colormap handles 'bad' values
lattice = plt.imread('path')
im = plt.imshow(data[0,:,:],vmin=v_min,vmax=v_max,extent=(0,32,0,32),interpolation='nearest',cmap='jet')

lattice[lattice< thresh] = np.nan # insert 'bad' values into your lattice (the white)

im2 = plt.imshow(lattice,extent=(0,32,0,32),cmap=my_cmap)

rawDescription: Making image white space transparent, overlay onto imshow()


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

vec = T.vector()
compare = T.isinf(vec)
out = vec[(1-compare).nonzero()]

v = [  1.,   1.,   1.,   1.,  np.inf,   3.,   4.,   5.,   6.,  np.inf]
v = np.asarray(v)

out.eval({var:v})
array([ 1.,  1.,  1.,  1.,  3.,  4.,  5.,  6.])

rawDescription: Remove inf from Theano array

description: Remove inf from Theano array


code: sys.stderr.write(' \b\b' * amount); sys.stderr.flush()

rawDescription: Using the clint progress bar to show the status of urllib.urlretrieve()


code:
def set_session(fn):
    @functools.wraps(fn)
    def wrapper(name, *args, **kwargs):
        session['name'] = name
        return fn(name, *args, **kwargs)
    return wrapper

@app.route('/home/',defaults={'name':None})
@app.route('/home/<name>/')
@set_session
def name(name):
    return ...

rawDescription: Is there a more efficient way to set a session variable for each view?


code:
countt('strike')
countt('strike')
countt('ball')
countt('ball')
countt('ball')
countt('strike')

rawDescription: Multiple Generators

description: Multiple Generators


code: r"([aeiou][bcdfghjklmnpqrstvwxz])+"

rawDescription: Regex in Python to find words that follow pattern: vowel, consonant, vowel, consonant


code: [[x[i:i+2] for x in a_list] for i in range(0, len(a_list[0]), 2)]

rawDescription: Pythonic way of manipulating nested list into nested chunked list


code: rgi = irp.RegularGridInterpolator(points=pts, values=vals)

rawDescription: Correct usage of scipy.interpolate.RegularGridInterpolator


rawDescription: Inheritance of attributes in python using _init_


code:
def xform(elt):
    if type(elt) == list and len(elt) > 1 and elt[0] == "ETSc":
        return elt[0] + " (" + elt[1] + ")"
    else:
        return str(elt)

output = [xform(elt) for elt in input]

rawDescription: setting a condition for a mixed list

description: Setting a condition for a mixed list


code: df['New_Sample'] = df.Sample.str[:1]

rawDescription: Pandas make new column from string slice of another column


rawDescription: best way to make nested lists?

description: Make nested lists


code:
    # -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version Oct  8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import os
import wx
import wx.xrc
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigureCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar
#from numpy import arange, sin, pi

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"计算机插值 v0.1", pos = wx.DefaultPosition, size = wx.Size( 508,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.FRAME_SHAPED|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ) )

        ####################################################################
        self.MainMenu = wx.MenuBar( 0 )
        self.File = wx.Menu()
        self.SaveAs = wx.MenuItem( self.File, wx.ID_ANY, u"另存为...", wx.EmptyString, wx.ITEM_NORMAL )
        self.File.AppendItem( self.SaveAs )

        self.Exit = wx.MenuItem( self.File, wx.ID_ANY, u"退出...", wx.EmptyString, wx.ITEM_NORMAL )
        self.File.AppendItem( self.Exit )

        self.MainMenu.Append( self.File, u"文件" )

        self.About_M = wx.Menu()
        self.About = wx.MenuItem( self.About_M, wx.ID_ANY, u"关于", wx.EmptyString, wx.ITEM_NORMAL )
        self.About_M.AppendItem( self.About )

        self.MainMenu.Append( self.About_M, u"关于" )

        self.SetMenuBar( self.MainMenu )

        ####################################################################
        MainSizer = wx.BoxSizer( wx.VERTICAL )

        self.MainPanel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        UserSizer = wx.BoxSizer( wx.VERTICAL )

        upperSizer = wx.BoxSizer( wx.HORIZONTAL )
        ####################################################################

        self.TextExplanation = wx.StaticText( self.MainPanel, wx.ID_ANY, u"请在下面输入框内输入要插值的数，以空格间隔。\n输入范围在(1.2, 2.0)。", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.TextExplanation.Wrap( -1 )
        upperSizer.Add( self.TextExplanation, 0, wx.ALL, 20 )

        upperSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        RadioBoxInterpolationChoices = [ u"线性插值", u"抛物线插值" ]
        self.RadioBoxInterpolation = wx.RadioBox( self.MainPanel, wx.ID_ANY, u"插值种类", wx.DefaultPosition, wx.DefaultSize, RadioBoxInterpolationChoices, 1, wx.RA_SPECIFY_COLS )
        self.RadioBoxInterpolation.SetSelection( 1 )
        upperSizer.Add( self.RadioBoxInterpolation, 0, wx.ALL, 5 )

        UserSizer.Add( upperSizer, 0, wx.EXPAND, 5 )

        lowerSizer = wx.BoxSizer( wx.HORIZONTAL )

        self.TextCtrVal = wx.TextCtrl( self.MainPanel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PROCESS_ENTER )
        lowerSizer.Add( self.TextCtrVal, 0, wx.ALL, 20 )

        lowerSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.ButtonCal = wx.Button( self.MainPanel, wx.ID_ANY, u"计算", wx.DefaultPosition, wx.DefaultSize, 0 )
        lowerSizer.Add( self.ButtonCal, 0, wx.ALL, 20 )

        self.ButtonCls = wx.Button( self.MainPanel, wx.ID_ANY, u"清空", wx.DefaultPosition, wx.DefaultSize, 0 )
        lowerSizer.Add( self.ButtonCls, 0, wx.ALL, 20 )

        UserSizer.Add( lowerSizer, 0, 0, 5 )

        PlotSizer = wx.BoxSizer( wx.VERTICAL )

        #################################################
        p1 = PlotPanel(self)
        PlotSizer.Add(p1, 1, wx.EXPAND |wx.ALL,1)

        #################################################
        UserSizer.Add( PlotSizer, 1, wx.EXPAND, 5 )

        self.MainPanel.SetSizer( UserSizer )
        self.MainPanel.Layout()
        UserSizer.Fit( self.MainPanel )
        MainSizer.Add( self.MainPanel, 1, wx.EXPAND |wx.ALL, 1 )

        self.SetSizer( MainSizer )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_MENU, self.SaveAsFile, id = self.SaveAs.GetId() )
        self.Bind( wx.EVT_MENU, self.ExitNow, id = self.Exit.GetId() )
        self.Bind( wx.EVT_MENU, self.Dialog, id = self.About.GetId() )
        self.TextCtrVal.Bind( wx.EVT_TEXT_ENTER, self.Calculate )
        self.ButtonCal.Bind( wx.EVT_BUTTON, self.Calcualte )
        self.ButtonCls.Bind( wx.EVT_BUTTON, self.ClearPlot )

        # Necessary varibles
        selection = self.RadioBoxInterpolation.GetSelection()

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class
    def SaveAsFile( self, event ):
        event.Skip()

    def ExitNow( self, event ):
        self.Close(True)

    def Dialog( self, event ):
        dlg = wx.MessageDialog( self, u"Test", u"Test", wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def Calculate( self, event ):
        event.Skip()

    def Calcualte( self, event ):
        event.Skip()

    def ClearPlot( self, event ):
        event.Skip()

###########################################################################
## Class PlotPanel
###########################################################################

class PlotPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent, -1,size=(300,530))
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.figure = matplotlib.figure.Figure(figsize=(5,4))
        self.axes = self.figure.add_subplot(111)
        self.y_max = 2.0
        self.canvas = FigureCanvas(self,-1,self.figure)

        self.sizer.Add(self.canvas,proportion =1, border = 5,flag = wx.ALL | wx.EXPAND)
        self.SetSizer(self.sizer)

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

rawDescription: How to remove blank area between sizers in wxPython

description: Remove blank area between sizers in wxPython


rawDescription: Storing an ascii lookup table in a file in Python


rawDescription: How can I get the number of records that reference a particular foreign key in Django?

description: Get the number of records that reference a particular foreign key in Django


code: x /= linalg.norm(x)

rawDescription: I'm having difficulty understanding the syntax of scipy.optimize


rawDescription: Quickly parse Python datetime in a non-local timezone, adjusting for daylight savings


code:
def func():
    print("hello from func")
    return 7

rawDescription: What does it mean when you assign int to a variable in Python?


code: dist = math.hypot(x2 - x1, y2 - y1)

rawDescription: How do I find the distance between two points?

description: Find the distance between two points


rawDescription: order all row according to contents of a row

description: Order all row according to contents of a row


code:
li[0].name
li[1].name

rawDescription: Python classes - each class call new id

description: Python classes - each class call new id


code:
from BeautifulSoup import BeautifulSoup
from pprint import pprint

store = {}
key = ()

soup = BeautifulSoup(data)

recs = soup.findAll('data')

for rec in recs:
    if rec.find('from'):
        key = (rec.find('from').text,
               rec.find('to').text)
    else:
        item = {}
        item['name'] = rec.find('name').text
        item['some_data'] = rec.find('info').find('some_data').text
        item['more_data'] = rec.find('info').find('more_data').text
        if store.has_key(key):
            store[key].append(item)
        else:
            store[key] = [ item ]

pprint(store)

rawDescription: Extract and group elements/tags with BeautifulSoup


code:
mapping = dict(zip(a, b))
matches = [mapping[value] for value in a1 if value in mapping]

rawDescription: How to compare list in Python?

description: Compare list in Python


code:
parser = argparse.ArgumentParser()
# other arguments
parser.add_argument('--file', type=open, action=LoadFromFile)
args = parser.parse_args()

rawDescription: how to get argparse to read arguments from a file with an option rather than prefix

description: Get argparse to read arguments from a file with an option rather than prefix


code:
def inverse(mdg):
    i = 0
    Sr = ''
    while i < len(mdg):
        Sr =Sr + mdg[-(i+1)]
        i = i+1
    return Sr
ch = (input('Veuillez entrer une liste des valeurs separees par virgules: '))
l1 = list(ch.split(','))
print((",".join(inverse(l1)).join(['[',']'])))

rawDescription: How to return a list with brackets?

description: Return a list with brackets


code: (path, _) = self._treeView.get_cursor()

rawDescription: Is it possible to unpack a tuple in Python without creating unwanted variables?


code:
# Generate a list
randIn = lambda x: int(x*random.random())
myString =[randIn(2) for i in range(10000)]

pattern = [1,0,0]
result = matchIt(pattern, myString)

print("The sample had {0} matches and its size was {1}.\n".format(result, len(myString)))

rawDescription: Simplifying for-if messes with better structure?


code:
def methodroute(route):
    def decorator(f):
        f.route = route
        return f
    return decorator

class App(object):
    @methodroute('/index/')
    def index(self):
        pass

rawDescription: Bottle framework and OOP, using method instead of function


code: df.b.values[df.a.values == 30] = 3

rawDescription: pandas: more elegant way to retrieve row and set value if exists?

description: Pandas: more elegant way to retrieve row and set value if exists


code:
for i in range(len(x)):
    if x[i] is not y[i]:
        y = tuple(y)
        break
else:
    y = x

rawDescription: Does deepcopy use copy-on-write?

description: Does deepcopy use copy-on-write


rawDescription: (Python--numpy) how to resize and slice an numpy array with out a loop?


rawDescription: Python Time Series

description: Python Time Series


code:
quadrant = [lambda x, y: (sign[0] * x, sign[1] * y, id (sign) )
    for sign in ((1, -1), (1, 1), (-1, 1), (-1, -1))]

for q in quadrant:
    print (q (2, 3) )

rawDescription: For Python 3.3, how can I make a list of functions using one lambda statement?


code:
lst = []
for e in engine.engines:
    for m in e.modules:
        lst.append(m)

rawDescription: Python nested select on named property

description: Python nested select on named property


code: result = reduce(lambda x,y: x and y%2==1, [1,3,5,7], True)

rawDescription: Equivalent for inject() in Python?

description: Equivalent for inject() in Python


code:
def get_object_list(self, request):
    return super(FollowingPhoto, self).get_object_list(request).filter(photographer__user__in = request.user.relationships.following()).exclude(photographer__user__in=request.user.relationships.blockers())

rawDescription: How can I implement blocking using Django-relationships?

description: Implement blocking using Django-relationships


code:
xtextplace = x[7]
ytextplace = y[7] / 2
plt.text(xtextplace,ytextplace,"Some Text")

rawDescription: Plotting a curve with different background colors in python


code:
headline = soup.find(class_='cd__headline-text')
print(headline.string)

rawDescription: How to obtain the content between a tag and it's ending in HTML using python' beautiful soup?

description: Obtain the content between a tag and it's ending in HTML using python' beautiful soup


code:
import numpy as np
import pyproj

def main():
    config = Config('PF.bin.hdr')
    grid1, grid2 = load_data('l1.bin', 'PF.bin', config.nrows, config.ncols)

    spf = grid1 + grid2
    spf.tofile('pySPF_L1.bin')

    easting_aoi, northing_aoi = subset_data(grid1, grid2, config)
    save_selected_region(easting_aoi, northing_aoi, config.zone,
                        'testPythonROI.txt')

def load_data(filename1, filename2, nrows, ncols):
    """It would really be good to use more descriptive variable names than "L1"
    and "PF".  I have no idea what L1 and PF are, so I'm just calling them
    grid1 and grid2."""
    grid1 = np.fromfile(filename1, dtype=np.float32).reshape(nrows, ncols)
    grid2 = np.fromfile(filename2, dtype=np.float32).reshape(nrows, ncols)
    return grid1, grid2

def subset_data(grid1, grid2, config):
    """Select points that satisfy some threshold criteria (explain??) and are
    within a user-specified rectangular AOI."""
    northing, easting = np.mgrid[:config.nrows, :config.ncols]
    easting = config.xstart + config.xgridsize * easting
    northing = config.ystart + config.ygridsize * northing

    grids = grid1, grid2, easting, northing
    grid1, grid2, easting, northing = [item[config.user_aoi] for item in grids]

    mask = (grid1 >= config.metal_threshold) & (grid2 >= config.pf_threshold)
    return easting[mask], northing[mask]

def save_selected_region(easting, northing, zone, filename):
    """Convert the given eastings and northings (in UTM zone "zone") to
    lat/long and save to a tab-delimited-text file."""
    lat, lon = utm2geographic(easting, northing, zone)
    data = np.vstack([easting, northing, lat, lon]).T
    with open(filename, 'w') as outfile:
        outfile.write('Easting\tNorthing\tLatitude\tLongitude\n')
        np.savetxt(outfile, data, delimiter='\t')

def utm2geographic(easting, northing, zone):
    """We need to know which datum/ellipsoid the UTM coords are in as well!!!!
    I'm assuming it's a Clark 1866 ellipsoid, based on the numbers in your
    code..."""
    utm = pyproj.Proj(proj='utm', zone=zone, ellip='clrk66')
    geographic = pyproj.Proj(proj='latlong', ellip='clrk66')
    return pyproj.transform(utm, geographic, easting, northing)

class Config(object):
    """Read and store configuration values for (something?)."""
    config_file = 'config.txt'
    def __init__(self, filename):
        """You should add docstrings to clarify what you're expecting
        "filename" to contain."""
        with open(filename, 'r') as infile:
            crs_values = list(infile)[12].split(',')
        crs_values = [float(item) for item in crs_values]
        self.xstart, self.ystart = crs_values[3:5]
        self.xgridsize, self.ygridsize = crs_values[5:7]
        self.zone = int(crs_values[7])

        with open(self.config_file, 'r') as infile:
            srs_values = list(infile)
        self.nrows, self.ncols = srs_values[1], srs_values[4]

        # It would be good to explain a bit about these (say, units, etc)
        self.metal_threshold = 5.0
        self.pf_threshold = 0.9

        self.user_aoi = self.read_user_aoi()

    def read_user_aoi(self):
        """Get an area of interest of the grids in pixel coordinates."""
        top_left = raw_input('Enter top left index\t')
        bottom_right = raw_input('Enter bottom right index\t')
        min_i, min_j = [int(item) for item in top_left.split()]
        max_i, max_j = [int(item) for item in bottom_right.split()]
        return slice(min_i, max_i), slice(min_j, max_j)

if __name__ == '__main__':
    main()

rawDescription: How, when and what to vectorize in python?


code:
def main():
    text = get_text()
    p_text = process_text(text)
    catalogue = process_catalogue(p_text)

rawDescription: Python: How to sum up the word count for each person in a dialogue?


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

d = '''
0 4194304
1 497420
 ... (put all the rest of the data in here)
'''

D = np.loadtxt(d.split('\n'))

x = D[:,0]
y = D[:,1]
y_ln = np.log(y)

n = D.shape[0]

A = np.array(([[x[j], 1] for j in range(n)]))
B = np.array(y_ln[0:n])

X = np.linalg.lstsq(A,B)[0]
a=X[0]; b=X[1]

# so now your fitted line is log(y) = a*x + b
# lets show it on a graph.
plt.figure()
plt.plot(x, a*x+b, '--')
plt.plot(x, y_ln, 'o')
plt.ylabel('log y')
plt.xlabel('x values')
plt.show()

# or use the original scales by transforming the data back again:

plt.figure()
plt.plot(x, np.exp(a*x+b), '--')
plt.plot(x, y, 'o')
plt.ylabel('y')
plt.xlabel('x values')
plt.yscale('log')
plt.show()

rawDescription: How to fit to a log scale

description: Fit to a log scale


code: Y_modified = np.where(variance > 0.5 * np.max(variance), Y, 0)

rawDescription: Filter an array based on density

description: Filter an array based on density


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
args, unknown = parser.parse_known_args(['--foo', 'BAR', 'spam'])
print(args)
# Namespace(foo='BAR')
print(unknown)
# ['spam']

rawDescription: Python argparse ignore unrecognised arguments


rawDescription: Append a NumPy array to a NumPy array

description: Append a NumPy array to a NumPy array


code:
string = "Special $#! characters   spaces 888323"
''.join(e for e in string if e.isalnum())

rawDescription: Remove all special characters, punctuation and spaces from string


code: None is next((element for element in a if element in b_set), None)

rawDescription: Finding if any element in a list is in another list and return the first element found


code:
import magicdate
magicdate.magicdate('today')
magicdate.magicdate('tomorrow')
magicdate.magicdate('yesterday')

rawDescription: What's the best way to make a time from "Today" or "Yesterday" and a time in Python?


code: {{ instance.contents_substring }}

rawDescription: Django substring

description: Django substring


rawDescription: python - using json with OrderedDict and Datetime

description: Python - using json with OrderedDict and Datetime


code: [i for item in sorted(d.iteritems()) for i in item]

rawDescription: Generator-expression, getting the same results of using append or extend in a for-loop


code: makedir ("/myfolder/%s" % (datetime.date.today().strftime("%d %B, %Y")))

rawDescription: Passing argument to python which is half "<direct string>" and half value of a variable


rawDescription: Adding multiple elements to a list in Python


code:
func1 = lambda P, Q, R: (not P) or (not Q) or (not R)
func2 = lambda P, Q, R: not(P and Q and R)
logical_equiv(func1, func2, True, False, True)

rawDescription: Getting the return value of functions passed in as variables


code: i = a.argsort(axis=None)[::-1]

rawDescription: Python numpy keep a list of indices of a sorted 2D array


rawDescription: Trying to get a UInt64 into a base32 string.....works in python not in vb.net


code:
def git_add(file_path):
    return subprocess.check_output(['git', 'add', file_path])

rawDescription: Python: GIT commit programmatically

description: Python: GIT commit programmatically


code:
import macropy.activate
# Activates macropy, modules using macropy cannot be imported before this statement
# in the program.
import target
# import the module using macropy

rawDescription: functional pipes in python like %>% from dplyr

description: Functional pipes in python like %>% from dplyr


code:
import json

#Build a simple list of dicts
s = 'abcdefg'
data = []
for i, c in enumerate(s, 1):
    d = dict(name=c, number=i)
    data.append(d)

fname = 'data.json'

#Save data
with open(fname, 'w') as f:
    json.dump(data, f, indent=4)

#Reload data
with open(fname, 'r') as f:
    newdata = json.load(f)

#Show all the data we just read in
print(json.dumps(newdata, indent=4))

rawDescription: How to make one json file as output in a python?

description: Make one json file as output in a python


code:
df.groupby('x', axis = 0).sum()\
  .groupby(lambda x: (k for k,v in row_groups.iteritems() if x in v).next()).sum()

rawDescription: Group pandas df according to set

description: Group pandas df according to set


code:
filename = 'the .csv file'
text_display_csv(filename,screen) # text is displayed
pygame.display.update() # screen updated

rawDescription: Python - Display Excel database in a grid


code:
axes.plot([-1, 1.5], [0, 0], color='black', lw=1, transform=axes.transAxes, clip_on=False)
axes.plot([-1, 1.5], [1, 1], color='black', lw=1, transform=axes.transAxes, clip_on=False)

rawDescription: Draw a separator or lines between subplots


code: complexFloat = complex(complexStr[:-1] + 'j')

rawDescription: Convert DataFrame string complex i to j python


code: l = sorted(l, key=lambda a: a['time'], reverse=True)

rawDescription: How to sort this list in Python?

description: Sort this list in Python


code:
subts = origs[:,None,:] - dests
d_all = np.sqrt(np.einsum('ijk,ijk->ij',subts,subts))

rawDescription: einsum and distance calculations

description: Einsum and distance calculations


code:
timeit.timeit("from __main__ import data; "
   "my_dict_list = { d['id']: d['name']+d['address']+d['age'] for d in data}",
    number = 100)

rawDescription: Optimizing a for-loop in python

description: Optimizing a for-loop in python


code:
tups = [a, b, c]
map(sum, zip(*tups))

rawDescription: Adding tuples to produce a tuple with a subtotal per 'column'


code:
for p in selector.xpath("//a[@class='source sr-filter']/span[@itemprop='name']/text()").extract():
    city,state,nation= p.split(',')
    item['City'] =  city
    item['State'] =  state
    item['Nation'] =  nation

rawDescription: How to write rejax and xpath for the below link?

description: Write rejax and xpath for the below link


code:
result = [re.sub(r'[\d|.]+M',r'',i) for i in d]
# re.sub(A,B,Str) replaces all A with B in Str.

rawDescription: Regex and python to remove a certain format of string


code:
with open(path, 'rb') as byte_file:
    a = array('B', byte_file.readline())

rawDescription: Python string contains byte array

description: Python string contains byte array


rawDescription: Python: Use Multiple Colors at once in turtle


code:
for i in master_file:
        columns = re.split('\s{2,}', i)
        last_name = columns[1]
        yield last_name

rawDescription: Exiting out of for loop and returning when at the end of a file


code:
import struct
struct.unpack('{}B'.format(len(foo)), foo)

rawDescription: Python 2 and 3 compatible method to convert bytes to integer


code:
Na=1000 #array
Nd = 10**4 #dict
data=randint(0,Nd,(Na,Na))
dic=dict(zip(range(Nd),randint(0,Nd,Nd)))
dicarray=np.array(list(dic.values()))

rawDescription: What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and replace with values)


code: import myapp.pkg_a.foo

rawDescription: Developing Python modules - adding them to the Path


code:
import subprocess
cat = subprocess.Popen(['cat'])

import signal, os
signal.signal(signal.SIGINT,
              lambda signum, frame: os.kill(cat.pid, signum))

subprocess.call(['gdb', '--pid=%d' % cat.pid])

rawDescription: How do you automate the launching/debugging of large scale projects?

description: Automate the launching/debugging of large scale projects


code: cur2.execute("SELECT  * from obs where ujd=%s;",(time[i],))

rawDescription: Psycopg2 not fetching all data that exists in database


code:
AA = set(map(tuple,A))
BB = set(map(tuple,B))

rawDescription: Python in vs iteration for nested list

description: Python in vs iteration for nested list


code: k = {x:v for x,v in k.iteritems() if x != 1}

rawDescription: What is the proper way of deleting item from a dictionary  in "for" loop (Python)?


rawDescription: Numpy: Add rows from one matrix to another by index


code:
from behave import configuration
from behave import __main__

# Adding my wanted option to parser.
configuration.parser.add_argument('-u', '--url', help="Address of your url")

# command that run behave.
__main__.main()

rawDescription: passing command line argument to python-behave

description: Passing command line argument to python-behave


code:
current_line = lines[0]
number = find_number(current_line)

rawDescription: Numbers in string

description: Numbers in string


code:
if key not in mydict.keys():
    mydict[key] = []
mydict[key].append(something)

rawDescription: Making a dictionary from a list of lists


rawDescription: Create an array from n copies of subarray in python


rawDescription: fetchall for loop multiple list to one

description: Fetchall for loop multiple list to one


code:
def smallest(seq):
    m = min(seq)
    return [i for i, x in enumerate(seq) if x == m]

rawDescription: How to tell when more than one index matches?

description: Tell when more than one index matches


code:
def modify_legend(**kwargs):
    import matplotlib as mpl

    l = mpl.pyplot.gca().legend_

    defaults = dict(
        loc = l._loc,
        numpoints = l.numpoints,
        markerscale = l.markerscale,
        scatterpoints = l.scatterpoints,
        scatteryoffsets = l._scatteryoffsets,
        prop = l.prop,
        # fontsize = None,
        borderpad = l.borderpad,
        labelspacing = l.labelspacing,
        handlelength = l.handlelength,
        handleheight = l.handleheight,
        handletextpad = l.handletextpad,
        borderaxespad = l.borderaxespad,
        columnspacing = l.columnspacing,
        ncol = l._ncol,
        mode = l._mode,
        fancybox = type(l.legendPatch.get_boxstyle())==mpl.patches.BoxStyle.Round,
        shadow = l.shadow,
        title = l.get_title().get_text() if l._legend_title_box.get_visible() else None,
        framealpha = l.get_frame().get_alpha(),
        bbox_to_anchor = l.get_bbox_to_anchor()._bbox,
        bbox_transform = l.get_bbox_to_anchor()._transform,
        frameon = l._drawFrame,
        handler_map = l._custom_handler_map,
    )

    if "fontsize" in kwargs and "prop" not in kwargs:
        defaults["prop"].set_size(kwargs["fontsize"])

    mpl.pyplot.legend(**dict(defaults.items() + kwargs.items()))

rawDescription: How to modify matplotlib legend after it has been created?

description: Modify matplotlib legend after it has been created


code:
client = Client(url)
result = client.service.Invoke(subm)

rawDescription: Python SOAP document handling

description: Python SOAP document handling


rawDescription: Calculating Integrals: One-liner solution?


code:
import os
import py7zlib

class SevenZFileError(py7zlib.ArchiveError):
    pass

class SevenZFile(object):
    @classmethod
    def is_7zfile(cls, filepath):
        """ Determine if filepath points to a valid 7z archive. """
        is7z = False
        fp = None
        try:
            fp = open(filepath, 'rb')
            archive = py7zlib.Archive7z(fp)
            n = len(archive.getnames())
            is7z = True
        finally:
            if fp: fp.close()
        return is7z

    def __init__(self, filepath):
        fp = open(filepath, 'rb')
        self.filepath = filepath
        self.archive = py7zlib.Archive7z(fp)

    def __contains__(self, name):
        return name in self.archive.getnames()

    def bytestream(self, name):
        """ Iterate stream of bytes from an archive member. """
        if name not in self:
            raise SevenZFileError('member %s not found in %s' %
                                  (name, self.filepath))
        else:
            member = self.archive.getmember(name)
            for byte in member.read():
                if not byte: break
                yield byte

    def readlines(self, name):
        """ Iterate lines from an archive member. """
        linesep = os.linesep[-1]
        line = ''
        for ch in self.bytestream(name):
            line += ch
            if ch == linesep:
                yield line
                line = ''
        if line: yield line

rawDescription: How to read from a text file compressed with 7z in Python

description: Read from a text file compressed with 7z in Python


code: df['highest'] = df.groupby(['test', 'analysis'])['second_pass'].transform(lambda x: x == np.amax(x)).astype(bool)

rawDescription: Pandas DataFrame Window Function

description: Pandas DataFrame Window Function


code:
root=etree.fromstring(responseFromAmazon)
AMAZON_NS="{http://webservices.amazon.com/AWSECommerceService/2009-10-01}"
item={}
for attr in root.iterfind(".//"+AMAZON_NS+"ItemAttributes"):
     item[attr[0].tag.replace(AMAZON_NS,"")]=attr[0].text

rawDescription: How to use lxml to grab specific parts of an XML document?

description: Use lxml to grab specific parts of an XML document


code: any_match = any(test(x) for x in stuff)

rawDescription: Python equivalent of LINQ All function?

description: Python equivalent of LINQ All function


code: [(k, v) for k, v in dic.iteritems() if k[1]=='c']

rawDescription: Referencing to elements of tuples when used as keys in a python dictionary


code:
def troz(x):
    return frob(x + int(str(x)[::-1]))

rawDescription: Generating list until certain condition is met using a single line


code:
import sys

a = ['hi','helllo']
first = True
for i in a:
    if not first:
        sys.stdout.write(' ')
    first = False
    for j in i:
         sys.stdout.write(j)
print()

rawDescription: How to use this for loop to get expected result in python

description: Use this for loop to get expected result in python


code:
from math import sqrt
def F(n):
    return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))

rawDescription: How to write the Fibonacci Sequence in Python

description: Write the Fibonacci Sequence in Python


code: python("print hello");

rawDescription: Firing a simple python script using a MEL button's -c flag?


rawDescription: Regular expression for class using Beautifulsoup


code:
import requests

example_url = 'http://www.w3schools.com/html/html_form_action.asp'
data_to_send = {'user': 'ThisIsMyUserName'}
response = requests.get(example_url, params=data_to_send)
contents = response.text
print(contents)

rawDescription: Python 2&3: both urllib & requests POST data mysteriously disappears


rawDescription: Python3 convert Unicode String to int representation


code:
seen = set()
for L in f:
    if L in seen:
        print(L)
    else:
        seen.add(L)

rawDescription: Find double lines; a faster way

description: Find double lines; a faster way


code:
import win32gui
win32gui.GetWindowText(win32gui.GetForegroundWindow())
#parse this and use GetObject to get your excel instance

rawDescription: Get name of active Excel workbook from Python


code:
class Friendship(db.Model):
    __tablename__ = 'friend'
    fk_user_from = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    fk_user_to = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    extra_field = db.Column(db.Integer)

class User (db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    user_to = db.relationship('Friendship',backref='to', primaryjoin=id==Friendship.fk_user_to)
    user_from = db.relationship('Friendship',backref='from', primaryjoin=id==Friendship.fk_user_from )

rawDescription: SQLAlchemy - Self referential Many-to-many relationship with extra column


code: ['JB74XYZ', 'Kerry', 'Katona', '44', 'Mansion_House', 'LV10YFB']

rawDescription: Python regex findall to read line in .csv file


code:
class Roles(db.Model, AuditColumns):

    id = Column(BigInteger, primary_key=True)
    username = Column(String(64), nullable=False, unique=True)
    password = Column(String(255), nullable=False)

    __mapper_args__ = {
        'extension': AuditExtension()}

    def __repr__(self):
        return self.username

rawDescription: SQLAlchemy - onupdate do not override current value


code:
for i in orientation:
    newCord = [firstCord[0] + i[0], firstCord[1] + i[1]]

rawDescription: List Value gets updated inside an if statement even though the condition is not true


code: x=Heart

rawDescription: Python's enum equivalent

description: Python's enum equivalent


code:
v = "something"
allowed = ["someoption", "someother"]
default = "default_value"
v = validate(v, allowed, default)

rawDescription: pass value by reference into python function

description: Pass value by reference into python function


code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict

def table_to_list(table):
    dct = table_to_2d_dict(table)
    return list(iter_2d_dict(dct))

def table_to_2d_dict(table):
    result = defaultdict(lambda : defaultdict(unicode))
    for row_i, row in enumerate(table.xpath('./tr')):
        for col_i, col in enumerate(row.xpath('./td|./th')):
            colspan = int(col.get('colspan', 1))
            rowspan = int(col.get('rowspan', 1))
            col_data = col.text_content()
            while row_i in result and col_i in result[row_i]:
                col_i += 1
            for i in range(row_i, row_i + rowspan):
                for j in range(col_i, col_i + colspan):
                    result[i][j] = col_data
    return result

def iter_2d_dict(dct):
    for i, row in sorted(dct.items()):
        cols = []
        for j, col in sorted(row.items()):
            cols.append(col)
        yield cols

if __name__ == '__main__':
    import lxml.html
    from pprint import pprint

    doc = lxml.html.parse('tables.html')
    for table_el in doc.xpath('//table'):
        table = table_to_list(table_el)
        pprint(table)

rawDescription: Parsing a table with rowspan and colspan


code:
import unicodedata
print(u"\u212b")
unicodedata.normalize("NFD", u"\u212b") # 2 Unicode codepoints
unicodedata.normalize("NFC", u"\u212b") # 1 Unicode codepoint
unicodedata.normalize("NFKD", u"\u212b") # 2 Unicode codepoints
unicodedata.normalize("NFKC", u"\u212b") # 1 Unicode codepoint

rawDescription: Calculate bytes of the unicode character in python


code:
import atexit
process = subprocess.Popen(args.server_file_path)
atexit.register(process.terminate)
pid = process.pid

rawDescription: Killing child process when parent crashes in python


code:
RsRGB = R8bit/255

GsRGB = G8bit/255

BsRGB = B8bit/255

rawDescription: Calculate the closest colourblind-friendly colour?


code: groupA[name].append(count)  # count must be single value

rawDescription: When i add to the list in my dictionary, it doesn't actually add to the list? python


code:
g = df.groupby('Date')
df.value / g.value.transform("sum") * df.wt

rawDescription: Calculate weighted average using a pandas/dataframe


code:
from collections import Counter

max_dict = Counter()
for d in original_dict_list:
    max_dict |= Counter(d)

rawDescription: Merge multiple dictionaries conditionally


code:
from mypackage import mymodule
do_something_with(mymodule.MY_CONSTANT)

#elsewhere
mymodule.MY_CONSTANT = 'new_value'

rawDescription: Reloading global Python variables in a long running process


rawDescription: Join pandas series while keeping new indices


code:
type(x)
len(x)
type(x[0])

rawDescription: Convert list of numpy.float64 to float in Python quickly


code:
for item in something:  # Default iterator: returns non-tuple objects
    do_something(item)

for (item,key) in something.iter_pairs(): # iter_pairs returns different iterator
    do_something_else(item, key)

rawDescription: how to program functions with alternative return value signatures in python? (next() for alternative iterators)

description: Program functions with alternative return value signatures in python? (next() for alternative iterators)


code:
df
df["movement"] = np.sign(df["closePrice"] - df["openPrice"])
df

rawDescription: How to add another label-like column for a dataframe in python?

description: Add another label-like column for a dataframe in python


code:
available_sizes = ['L', 'S', 'XL', 'M']
print(available_sizes) # ['L', 'S', 'XL', 'M']

# sort them using the existing sorted list of possible sizes
available_sizes.sort(key=lambda size: sizes.index(size))
print(available_sizes) # ['S', 'M', 'L', 'XL']

rawDescription: Python Order list of strings by content

description: Python Order list of strings by content


code:
from collections import Counter
text_count_1 = Counter(text_1)
text_count_2 = Counter(text_2)
difference = list((text_count_1 - text_count_2).elements()) + list((text_count_2 - text_count_1).elements())

rawDescription: Delta words between two TXT files

description: Delta words between two TXT files


code:
speed.update()
speed.copyto(rectangle)

rawDescription: Adding a speed vector to a position vector only works with integers


code: {'repo_1': [listOfRepositoryObjects], 'repo_2': [anotherListOfRepositoryObjects]}

rawDescription: Using pygit2's discover_repository to locate multiple repositories in a directory


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

x = [i for i in range(7)]
y = [i**2 for i in range(7)]
for i in range(1,len(x)):
    diffx = (x[i]-x[i-1])*0.15
    diffy = (y[i]-y[i-1])*0.15
    plt.plot((x[i-1]+diffx,x[i]-diffx),(y[i-1]+diffy,y[i]-diffy),color='black',linewidth=3)
    plt.scatter(x[i-1],y[i-1],marker='o',s=30,facecolor='white',edgecolor='black')

plt.plot((min(x),max(x)),(min(y),max(y)),color='red',linewidth=3,linestyle='--')

plt.show()

rawDescription: How to Plot ROC curve in pyhton

description: Plot ROC curve in pyhton


code:
class Meta(type):

    def __new__(cls, name, bases, attrs):
        attrs['_doc'] = attrs.get('__doc__', '')
        return super(Meta, cls).__new__(cls, name, bases, attrs)

    @property
    def __doc__(self):
        return self._doc + self.attributes_string()

rawDescription: How to modify class docstrings using classmethods

description: Modify class docstrings using classmethods


code:
with open(csvfilename, "wb") as csvfile:
    writer = csv.writer(csvfile, delimiter=",")
    writer.writerow(row)

rawDescription: Need to do a math operation on every line in several CSV files in Python


code:
def process_df(df, function, *args, **kwargs):
    function(df, *args, **kwargs)

rawDescription: How to use a functional in this case?

description: Use a functional in this case


code:
for node in tree.iterfind('.//logging/Adapter[@type="abcdef"]'):
    for child in node:
        # check if the key is 'Type A'
        if child.get('key') == 'Type A':
            child.set('value', 'false')
        # ... if 'Type B' ...

rawDescription: XML <arg> value Replacement in Python

description: XML <arg> value Replacement in Python


code: length = len(text)

rawDescription: How can I count the length of the output of print in Python?

description: Count the length of the output of print in Python


rawDescription: Extracting data in Tweepy in useable form (JSON)


code: newlist = [ filter(None, x) for x in oldlist ]

rawDescription: How do I create a new list with specific items from an old list in Python 2?

description: Create a new list with specific items from an old list in Python 2


rawDescription: Python CGI returning an http status code, such as 403?


rawDescription: Spark withColumn() performing power functions


code: td['frameId']

rawDescription: Work with tuple of dictionaries

description: Work with tuple of dictionaries


code:
import sqlite3
conn = sqlite3.connect(':memory:')
curs = conn.cursor()
uids = [1234,4321,1111]
qry = ("""SELECT cheese
            FROM cheeseshop
           WHERE id IN (%s)""" % ','.join('?'*len(uids)))
# 'SELECT cheese FROM cheeseshop WHERE id IN (?,?,?)'
curs.execute(qry, uids)
curs.fetchone()
# ...

rawDescription: Fastest way to make a dict a single comma-separated string


code:
def combine_lists(*lists):
    return [sum(l, []) for l in zip(*lists)]

combine_lists(l1, l2)

rawDescription: How to use *args to combine multiple lists?

description: Use *args to combine multiple lists


code:
def partition0(max_range, S):
    K = len(max_range)
    return np.array([i for i in itertools.product(*(range(i+1) for i in max_range)) if sum(i)<=S])

rawDescription: Generating a numpy array with all combinations of numbers that sum to less than a given number


code:
shp = A.shape
out = A.reshape(shp[0],shp[1],-1)[np.arange(shp[0]),:,B[:,0]*shp[3] + B[:,1]]

rawDescription: Numpy: Multidimensional index. Row by row with no loop


code:
def find_neigbours(start_vertex, levels):
    found = set()
    from_vertices = [start_vertex]
    for i in range(1, levels+1):
        new_from_vertices = set()
        for vertex in from_vertices:
            for neighbour in p_2_dict[vertex]:
                if neighbour == start_vertex:
                    continue
                new_from_vertices.add(neighbour)
                found.add( (neighbour, i) )
        from_vertices = new_from_vertices
    return found

rawDescription: Most Efficient Method of Searching Neighbour Data for Nth Neighbour


code: urls = [key.generate_url(100) for key in keys]

rawDescription: How do I create a list of files stored in my Amazon S3 bucket and display them as download links?

description: Create a list of files stored in my Amazon S3 bucket and display them as download links


rawDescription: How to work with data from NBA.com?

description: Work with data from NBA.com


code:
memory = {}
{name: memory[name] for name, count in a_list if not memory.__setitem__(name, count + memory.setdefault(name, 0))}

rawDescription: Adding and combining values with dictionary comprehensions?


code:
def pytest_collection_modifyitems(session, config, items):
    for item in items:
        if not _get_marker(item, 'parametrize'):
            continue

        format_params_marker = _get_marker(item, 'params_format')
        if not format_params_marker:
            continue

        params = item.callspec.params.copy()
        param_formatters = format_params_marker.kwargs
        for param_name, presenter in param_formatters.iteritems():
            params[param_name] = presenter(params[param_name])

        params_names_ordered = [name for name in item.fixturenames if name in params]
        default_format = '-'.join(['{{{}}}'.format(param_name) for param_name in params_names_ordered])
        format_string = format_params_marker.args[0] if format_params_marker.args \
            else default_format
        item.name = '{func}[{params}]'.format(func=item.name.split('[')[0],
                                              params=format_string.format(**params))

def _get_marker(item, marker):
    return item.keywords._markers.get(marker, None)

rawDescription: Pytest text annotation for test with tuple of parameters


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

def uniqueish_color():
    """There're better ways to generate unique colors, but this isn't awful."""
    return plt.cm.gist_ncar(np.random.random())

xy = (np.random.random((10, 2)) - 0.5).cumsum(axis=0)

fig, ax = plt.subplots()
for start, stop in zip(xy[:-1], xy[1:]):
    x, y = zip(start, stop)
    ax.plot(x, y, color=uniqueish_color())
plt.show()

rawDescription: python: how to plot one line in different colors

description: Python: how to plot one line in different colors


code: regex = re.compile(r'(?<!\s)\s')

rawDescription: python regex: use first blank as sep but maintain rest of blank sequence

description: Python regex: use first blank as sep but maintain rest of blank sequence


code:
bsf = np.einsum('ijkl,ijkl,kl->ikl',uvel,dz,dy)

bsf = np.einsum('ijkl,ijkl->ikl',uvel,dz)*dy

rawDescription: Speed up Python/Cython loops.

description: Speed up Python/Cython loops.


rawDescription: Advanced List sorting Python

description: Advanced List sorting Python


code:
# Import
import forms

# This statement references 'forms' at compile time
@forms.bind_form(form=forms.Create_User)
def create_user(**kwargs):
    ...

rawDescription: Modules that rely on each other

description: Modules that rely on each other


rawDescription: Change a list of list of str to list of list of int


code:
for i, row in enumerate(myiterable, start=2):
   if i>= limit: break
   ...

rawDescription: Easiest way to include a stop parameter in enumerate python?


code:
g = Generator()
g.foo
Generator.foo
g.bar
Generator.bar
g.foo()
Generator.foo(g)
'foo' in vars(Generator), 'bar' in vars(Generator)

rawDescription: Dynamic method generation in python

description: Dynamic method generation in python


code:
"""
  Class: MyClass
  Describe the class here.

  Attribute: attr1
  First attribute of the class

  Attribute: attr2
  Second one
"""
class MyClass:

    # Constructor: __init__
    # Describe the constructor.
    #
    # Parameters:
    #   arg1 - The first argument.
    def __init__(self, arg1):
        self.attr1 = arg1
        self.attr2 = "attr2"

    # Method: method1
    # Describe the method here.
    def method1(self):
        print("method1")

rawDescription: How to document class attributes with NaturalDocs

description: Document class attributes with NaturalDocs


code: key=lambda x: sum(map(itemgetter('play'), x))

rawDescription: Sorting a list of lists of dictionaries in python


rawDescription: 2D Array - Select range of columns

description: 2D Array - Select range of columns


code:
from astropy.coordinates import SkyCoord
from astropy.table import Table
import astropy.units as u
import numpy as np
phi = np.linspace(0,2*np.pi,20)
theta = np.linspace(0, np.pi, 20)
radecs = SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad)
app_mag = np.random.uniform(15,25, 20)
x=Table([radecs.ra,radecs.dec,app_mag], names=('RA', 'Dec','Apparent Magnitude'))
x[x['Dec'] > -10*u.degree]

rawDescription: How to index observable coordinates in an astropy Table

description: Index observable coordinates in an astropy Table


code:
sum_string = input('please enter a sum. for example: "1 + 2 + 10 ..."') #1
result = sum([float(i) for i in sum_string.split('+')])                 #2
print('the result is {0}'.format(result))                               #3

rawDescription: How to map all the values in a range upto n, range(n) in Python

description: Map all the values in a range upto n, range(n) in Python


code:
def source(script, update=1):
    pipe = Popen(". %s; env" % script, stdout=PIPE, shell=True)
    data = pipe.communicate()[0]
    env = dict((line.split("=", 1) for line in data.splitlines()))
    if update:
        os.environ.update(env)
    return env

rawDescription: how to make subprocess called with call/Popen inherit environment variables

description: Make subprocess called with call/Popen inherit environment variables


rawDescription: Split a list of tuples into sub-lists of the same tuple field


code:
from pprint import pprint
import re
import urllib2

from bs4 import BeautifulSoup
import demjson

page1 = urllib2.urlopen('http://www.indeed.co.uk/jobs?q=%22data+science%22')
soup = BeautifulSoup(page1)

pattern = re.compile(r"jobmap\[\d+\]= (.*?);")
script = soup.find('script', text=lambda text: text and "jobmap = {}" in text).text

data = [demjson.decode(item) for item in pattern.findall(script)]
pprint(data)

rawDescription: BeautifulSoup Text Mining - Variable String


code:
from bs4 import BeautifulSoup
BeautifulSoup.__module__     # module of BeautifulSoup
import requests
r = requests.get('http://www.google.com')
soup = BeautifulSoup(r.content)
soup.__module__               # module of a BeautifulSoup object

rawDescription: Test for which module an object is from rather than which type it is


code: hex(value)

rawDescription: Python hexadecimal comparison

description: Python hexadecimal comparison


code:
class Post(models.Model):
    ...
    image_url = models.URLField(max_length=200, blank=True)

rawDescription: creating img_upload_url using django

description: Creating img_upload_url using django


code:
item = test.get('store')
if item is None:
    item = run()

rawDescription: Callable as the default argument to dict.get without it being called if the key exists


code:
import requests
url = 'someurl'
r = requests.get(url)

print("first redirect",r.history[0].url)
print('full history')

for n,h in enumerate(r.history):
    print(n,h.url)

rawDescription: Python: Find First Redirect of URL

description: Python: Find First Redirect of URL


rawDescription: How can I get the base of a URL in Python?

description: Get the base of a URL in Python


rawDescription: Extract text() and get attributes from it


code:
import matplotlib.pyplot as plt
x = [1,2,3]
plt.subplot(211)
plt.plot(x, label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(0, 1), loc='upper left', ncol=1)
plt.show()

rawDescription: Matplotlib automatic legend outside plot


code:
if key in unassigned_items:
    do_something()

rawDescription: Checking indexed object value type

description: Checking indexed object value type


rawDescription: Reduce a sparse matrix in python created from a noncontiguous coordinates lists


rawDescription: How to remove duplicate items in multiple tuples of a dictionary?

description: Remove duplicate items in multiple tuples of a dictionary


code: t = tuple(lst)

rawDescription: RegEx in Python matching multiple substrings using one group


code: start = time.time()

rawDescription: Displaying algorithm run time in python

description: Displaying algorithm run time in python


rawDescription: Is it possible to copy and update a python dictionary in a single chained expression?


code:
import pandas as pd
import numpy as np

N = 300
dates = pd.date_range('19971002', periods=N, freq='B')
df = pd.DataFrame(np.random.randn(N, 1),index=dates,columns=list('A'))
df['Counter'] = df.groupby([df.index.year, df.index.month]).cumcount()+1
print(df.loc['1998-09-25':'1998-10-05'])

rawDescription: Start increment column at begining of month


code: {'type':'ineq', 'fun': lambda x: x}

rawDescription: Element-wise constraints in scipy.optimize.minimize


rawDescription: How do I efficiently combine similar dataframes in Pandas into one giant dataframe

description: Efficiently combine similar dataframes in Pandas into one giant dataframe


code:
from collections import Counter

with open('test.txt' ,'r') as f:
    sequences = Counter(f.read().replace("\n", ""))

for seq, count  in sequences.items():
    if count > 1:
        print('number {} appears {} times'.format(seq, count))

rawDescription: How to find sequence in text file

description: Find sequence in text file


rawDescription: Generating iterables of iterables with python itertools. (using the repeat function)


code:
from operator import attrgetter

# Keep only files with matching extension
filesonly = (e for e in os.scandir(somedir) if e.is_file() and e.name.endswith('.fits'))

# Keep the "smallest" 1000 entries sorted alphabetically by name
# then pull off the 1000th entry
# End with .name instead of .path if you don't need the whole path
file1000 = nsmallest(1000, filesonly, key=attrgetter('name'))[999].path

rawDescription: fastest way to get the filename of a specific file from a directory with python

description: Fastest way to get the filename of a specific file from a directory with python


code:
import sys
import ctypes
isinstance(ctypes.c_byte(1), object)

rawDescription: ctypes in python size with the `sys.getsizeof(Var)` method vs `ctypes.sizeof(Var)`

description: Ctypes in python size with the `sys.getsizeof(Var)` method vs `ctypes.sizeof(Var)`


rawDescription: Looking for a way to shard a MongoDB collection from within Python code


rawDescription: How do I reference a specific value in a nested NumPy array?

description: Reference a specific value in a nested NumPy array


code:
from collections import Counter
Counter(L1) & Counter(L2)

rawDescription: Compare similarities in a list using hashes?


rawDescription: Capture multiple words in group

description: Capture multiple words in group


code:
import cv2
import numpy as np
import cv           #the old cv interface

img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])

h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")

rawDescription: How to show the whole image when using OpenCV warpPerspective

description: Show the whole image when using OpenCV warpPerspective


code:
def shop_product(request, slug):
    tshirt = Tshirt.objects.get(slug=slug)
    product = {}

    if(request.GET.get('add') and request.GET.get('add')=='add'):
        product['name'] = tshirt.title
        product['price'] = tshirt.price_reg
        request.session['items'].append(product)
        request.session.modified = True

    if(request.GET.get('clear') and request.GET.get('add')=='clear' ):
        request.session['items'] = []
        request.session.modified = True

    context = {'items' : request.session['items']}

    return render(request, 'shop/shop_product.html', context)

rawDescription: Django session variable value lost after refreshing a page


code:
from ctypes import *

file = CDLL('./file.so')

# do stuff here

handle = file._handle # obtain the SO handle

cdll.LoadLibrary('libdl.so').dlclose(handle)

rawDescription: Can I explicitly close a ctypes CDLL?

description: Explicitly close a ctypes CDLL


code:
def interpolate_gaps(values, limit=None):
    """
    Fill gaps using linear interpolation, optionally only fill gaps up to a
    size of `limit`.
    """
    values = np.asarray(values)
    i = np.arange(values.size)
    valid = np.isfinite(values)
    filled = np.interp(i, i[valid], values[valid])

    if limit is not None:
        invalid = ~valid
        for n in range(1, limit+1):
            invalid[:-n] &= invalid[n:]
        filled[invalid] = np.nan

    return filled

rawDescription: Working with NaN values in matplotlib

description: Working with NaN values in matplotlib


rawDescription: How to find every walk in a numpy array

description: Find every walk in a numpy array


code:
for anchor in tbody.findAll('div', style="s1"):
    text = ''.join([x for x in anchor.contents if isinstance(x, bs4.element.NavigableString)])

rawDescription: How to use python beautiful soup to get only the level 1 navigableText?

description: Use python beautiful soup to get only the level 1 navigableText


code:
from threading import Thread
import time

def background_task():
    while not background_task.cancelled:
        self.mqConn.heartbeat_tick()
        time.sleep(30)
background_task.cancelled = False

t = Thread(target=background_task)
t.start()

background_task.cancelled = True

rawDescription: How to create a background threaded on interval function call in python?

description: Create a background threaded on interval function call in python


code:
# We only need column
_, column = search_in_table(table_name, search_for)

rawDescription: How do I satisfy the Unused Variable rule from PEP8 if I don't need a variable returned by a function?

description: Satisfy the Unused Variable rule from PEP8 if I don't need a variable returned by a function