code:
import re

def merge_dicts(*dict_args):
    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return result

def make_dict(str_arg, op):
    result = {}
    result = dict(s.split(op) for s in str_arg.split(","))
    return result

'''
Samples
lst = r'  <name>Test Name</name>'
lst = r'  <alt name />'
lst = r'  <file local="C:\file\file1.doc" type="word">http://filestore/file1.doc</file>'
lst = r'  <file local="" type="excel">http://filestore/file2.xls</file>'
lst = r'  <file local="C:\file\file3.xls" type="excel" />'
lst = r'  <file local="" type="ppt" />'
'''
def match_pattern(file_str):
    #<description>desc blah</description>'
    pattern1 = r'''(?x)
    ^
    \s*                                             # cut leading whitespace
    (?P<whole_thing>
        < (?P<tag_open> (\w+?|\w*\s\w+?)+) \b       # word boundary, so we can
        >                                           # skip attributes
        (?P<tag_body> .+? )                         # insides
        </ (?P<tag_close> (\w+?|\w*\s\w+?)+) >      # closing tag, nothing interesting
    )
    $'''

    #<alt name />
    pattern2 = r'''(?x)
    ^
    \s*
    (?P<whole_thing>
        < (?P<tag_open> (\w+?|\w*\s\w+?)+) \b
        \s/>
    )
    $'''

    #<file local="C:\file\file1.doc" type="word">http://filestore/file1.doc</file>'
    pattern3 = r'''(?x)
        ^
        \s*
        (?P<whole_thing>
            < (?P<tag_open> (\w+?|\w*\s\w+!=?)+) \b
            \s
            (?P<tag_attrib1> (\w*\=.*?))            # 1st attribute
            \s
            (?P<tag_attrib2> (\w*\=.*))             # 2nd attribute
            .*? >
            (?P<tag_body> .+? )
            </ (?P<tag_close> (\w+?|\w*\s\w+?)+) >
        )
        $'''

    #<file local="" type="ppt" />
    pattern4 = r'''(?x)
        ^
        \s*
        (?P<whole_thing>
            < (?P<tag_open> (\w+?|\w*\s\w+!=?)+) \b
            \s
            (?P<tag_attrib1> (\w*\=.*?))            # 1st attribute
            \s
            (?P<tag_attrib2> (\w*\=.*))             # 2nd attribute
            \s/>
        )
        $'''

    pat_str = 'pattern'
    pat_val = 1
    return_dict = {}
    while (pat_val <= 4):
        pattern = pat_str+str(pat_val)
        matchObj = re.match(eval(pattern), file_str, re.L|re.M)

        if matchObj:
            #for k, v in matchObj.groupdict().items():
            #    print('matchObj.group({!r}) == {!r}'.format(k, v))
            if pat_val == 1:
                body = matchObj.group('tag_body')
                return_dict = {matchObj.group('tag_open'): body}
            elif pat_val == 2:
                return_dict = {matchObj.group('tag_open'): ''}
            elif pat_val == 3:
                attr1 = make_dict(matchObj.group('tag_attrib1'), '=')
                attr2 = make_dict(matchObj.group('tag_attrib2'), '=')
                body = {'url': matchObj.group('tag_body')}
                attrib = merge_dicts(attr1, attr2, body)
                return_dict = {matchObj.group('tag_open'): attrib}
            elif pat_val == 4:
                attr1 = make_dict(matchObj.group('tag_attrib1'), '=')
                attr2 = make_dict(matchObj.group('tag_attrib2'), '=')
                body = {'url': ''}
                attrib = merge_dicts(attr1, attr2, body)
                return_dict = {matchObj.group('tag_open'): attrib}
            return return_dict
        else:
            pat_val = pat_val + 1
            if pat_val > 4:
                print("No match!!")

#print(match_pattern(lst))

def in_file(file):
    result = {}
    with open(file, "r") as file:
        data = (file.read().splitlines())
        for d in data:
            if data.index(d) == 0 or data.index(d) == len(data)-1:
                if data.index(d) == 0:
                    print(re.sub('<|/|>', '', d))
            elif d:
                lst = []
                dct = {}
                if 'file' in match_pattern(d).keys():
                    for i in match_pattern(d).items():
                        if 'file' in result.keys():
                            lst = result['file']
                            lst.append(i[1])
                            dct = {i[0]: lst}
                            result = merge_dicts(result, dct)
                            #print(result['file'])
                        else:
                            dct = {i[0]: [i[1]]}
                            result = merge_dicts(result, dct)
                else:
                    result = merge_dicts(result, match_pattern(d))
                    print('else', match_pattern(d))
    return result

print(in_file('C:\\test.nfo'))

rawDescription: ParseError parsing empty valued xml doc

description: ParseError parsing empty valued xml doc


code:
class A(object):
    def __init__(self):
        print("initializing A")
        self.__a()
    def __a(self):
        print("A.a()")

class B(A):
    def __init__(self):
        super().__init__()
        # add stuff for B
        self.bnum=3 # required by B.a()
    def __a(self):
        print("B.__a(), bnum=%i"%self.bnum)

rawDescription: python super calling child methods

description: Python super calling child methods


code:
cost = T.nnet.categorical_crossentropy(
    Y.reshape((Y.shape[0] * Y.shape[1], X.shape[2])),
    X.flatten())

rawDescription: Indexing a tensor in the 3rd dimension

description: Indexing a tensor in the 3rd dimension


code:
with open('userdata/addedfiles', 'r') as read_files:
    file_lines = read_files.readlines()
    # print the length of the lines from the input file
    print(len(file_lines), "files added")

    # do stuff per line (which in your case is a file name)
    for file_name in file_lines:
        print(file_name.strip())

rawDescription: How to open multiple files one-by-one and print the contents in Python

description: Open multiple files one-by-one and print the contents in Python


code:
dpi = im.info['dpi']  # Warning, throws KeyError if no DPI was set to begin with

# resize, etc.

out.save("out.jpg", dpi=dpi)

rawDescription: Python Image Library Image Resolution when Resizing


code:
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot((1,2,3,4))

# Point in figure coordinates
ptsFig = (0.5,0.5)

# Convert figure coordinates to display coordinates
ptsDisp = fig.transFigure.transform(ptsFig)

# Convert display coordinates to data coordinates
inv = ax.transData.inverted()
ptsData = inv.transform(ptsDisp)

# Plot point in data coordinates
ax.plot(ptsData[0], ptsData[1], 'ro', ms = 20)
# Use the figure transform to plot the same point using figure cooridnates.
ax.plot(ptsFig[0], ptsFig[1], 'g*', ms = 20, transform = fig.transFigure)

plt.show()

rawDescription: find the location of a clicked point on an image in matplotlib

description: Find the location of a clicked point on an image in matplotlib


code:
def build_active_solution(progid="VisualStudio.DTE.7.1"):
    from win32com.client import GetActiveObject
    dte = GetActiveObject(progid)
    sb = dte.Solution.SolutionBuild
    sb.Build(True)

    output = dte.Windows['Output'].Object.ActivePane.TextDocument.Selection
    output.SelectAll()
    return output.Text

rawDescription: Connect to a running instance of Visual Studio 2003 using COM, build and read output


code:
ctypes_array = (ctypes.c_char * MEM_SIZE).from_address(ptr)
with open('test.raw', 'wb') as f:
    f.write(ctypes_array)

numpy_array = numpy.frombuffer(ctypes_array, dtype=numpy.byte)
numpy_array.tofile('test.raw')

rawDescription: Python: Access/Save memory block just given by pointer without copying


rawDescription: How to Eat Memory using Python?

description: Eat Memory using Python


code:
my_file_data = "     3.146    -1.339      .358    29.214"
data = my_file_data.strip().split('     ')
# do stuff with your data
print(data[0])

rawDescription: Split string on white space

description: Split string on white space


rawDescription: Python OpenCV how to save an image after tranforming it


code:
self.e = tk.Entry(...)
...
self.e.insert("insert", "new text")

rawDescription: Add a string at the insertion point cursor location - Python


code:
def sum_gauss(start, end):
    return ((end-start+1)/2) * (end+start)

rawDescription: sum of natural number between range

description: Sum of natural number between range


code:
from django.db.models import Q

@login_required
def event_new(request, person_uuid=None):
    today = datetime.datetime.today()
    #valid_until may be empty
    profile = Profile.objects.filter(company=request.user.company).filter(Q(valid_until__gte=today)|Q(valid_until=None))

rawDescription: Get objects with date greater than today or empty date


rawDescription: Use values() in Django query to get Model instead of ID


code: # emp is an object with employee_id of 1532

rawDescription: Why use assert when saving new record in MotorEngine Tornado?


code: my_list = sorted(set(my_list))

rawDescription: What is the cleanest way to do a sort plus uniq on a Python list?


code:
from django.db import models

class QuerySetManager(models.Manager):
    def get_query_set(self):
        return self.model.QuerySet(self.model)

# Using it in a model:

from django.db.models.query import QuerySet

class Entry(models.Model):
   ...
   objects = QuerySetManager()
   ...
   class QuerySet(QuerySet):
        def chckactive(self):
            return self.filter(active=True)

#   Entry.objects.all().chckactive()

rawDescription: For any query on a django model, performing query only on elements that have active set as true


code:
import requests, pprint
from bs4 import BeautifulSoup as BS

url = "https://forums.spacebattles.com/threads/the-wizard-of-woah-and-the-impossible-methods-of-necromancy.337233/"
r = requests.get(url)
soup = BS(r.content, "html.parser")

#To find all posts from a specific user everything below this is for all posts
specific_messages = soup.findAll('li', {'data-author': 'author_name'})

#To find every post from every user
posts = {}

message_container = soup.find('ol', {'id':'messageList'})
messages = message_container.findAll('li', recursive=0)
for message in messages:
    author = message['data-author']
    #or don't encode to utf-8 simply for printing in shell
    content = message.find('div', {'class':'messageContent'}).text.strip().encode("utf-8")
    if author in posts:
        posts[author].append(content)
    else:
        posts[author] = [content]
pprint.pprint(posts)

rawDescription: Python script that finds post by a specific user on an xenoforo forum


rawDescription: Extracting expression

description: Extracting expression


code:
def on_button_clicked(self, widget):
    dialog = Gtk.FileChooserDialog("Save file", self,
        Gtk.FileChooserAction.SAVE,
        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
         Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
    self.handle_file_dialog(dialog)

def handle_file_dialog(self, dialog):
    response = dialog.run()
    if response == Gtk.ResponseType.OK:  # OK button was pressed or existing file was double clicked
        cansave = False
        if os.path.exists(dialog.get_filename()) == True:  # does file already exists?
            dialog2 = DialogSaveFile(self, dialog.get_filename())  # ask to confirm overwrite
            response = dialog2.run()
            if response == Gtk.ResponseType.OK:
                cansave = True
                dialog2.destroy()
            else:
                dialog2.destroy()
                # We need to re-run the file dialog to detect the buttons
                self.handle_file_dialog(dialog)
                return
        else:
            cansave = True
        if cansave == True:  # save new file
            open(dialog.get_filename(), "w").close
            dialog.destroy()
        else:
            pass
    else:
        dialog.destroy()

rawDescription: Python GTK: confirm overwrite dialog blocks filechooser dialog


code:
class SYSTEM_POWER_INFORMATION(Structure):
    _fields_ = [
            ("MaxIdlenessAllowed", c_ulong),
            ("Idleness", c_ulong),
            ("TimeRemaining", c_ulong),
            ("CoolingMode", c_ubyte)
            ]

call_nt_power_information = windll.powrprof.CallNtPowerInformation
call_nt_power_information.restype = c_uint

def get_idle_timer():
    # Get the time remaining before idle
    info = SYSTEM_POWER_INFORMATION()
    r = call_nt_power_information(SystemPowerInformation, None, 0, pointer(info), sizeof(SYSTEM_POWER_INFORMATION))
    if r != 0:
        raise Exception("Call to CallNtPowerInformation failed with code " + r)

return info.TimeRemaining

rawDescription: Identify if power state change is caused by user or by power policy


rawDescription: Community detection with igraph in Python


code:
result = []
for h, i in hull:
    result.append(hulls[h][i])
return result

rawDescription: Python list return statement deciphering


rawDescription: Python array to 1-D Vector

description: Python array to 1-D Vector


rawDescription: How to create an igraph object from a dictionary in Python

description: Create an igraph object from a dictionary in Python


code:
class JsonMinify(UIModule):
    def render(self, path):
        code=self.render_string(path)
        return slimit.minify(code)

rawDescription: tornado: UIModule for including minified javascript

description: Tornado: UIModule for including minified javascript


rawDescription: How do i replace nan in 2d array with other 2d array value

description: Replace nan in 2d array with other 2d array value


code:
d = collections.OrderedDict()
# build an OrderedDict of artist to track(s)
for fn in files:
  artist, title = fn.split('-')
  if (artist,) not in d:
    d[artist,] = []
  d[artist,].append(fn)

def window(seq, n=2):
  it = iter(seq)
  result = tuple(itertools.islice(it, n))
  if len(result) == n:
    yield result
  for elem in it:
    result = result[1:] + (elem,)
    yield result

while len(d) > 255:
  # find the pair of adjacent keys with minimal number of files contained
  k1, k2 = min(window(d), key=lambda x: len(d[x[0]] + d[x[1]]))
  # join them into the first key and nuke the latter
  d[k1] += d[k2]
  del d[k2]

rawDescription: binning files into approximately equal sized directories

description: Binning files into approximately equal sized directories


code:
from tkinter import *
root = Tk()
root.geometry("800x700+0+0")

#---------Backgroud of the main canvas-----`enter code here`-------

backGroundCanvas = Canvas(root,bg = 'white', width = 800, height = 600)
backGroundCanvas.focus_set()
backGroundCanvas.pack()

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

#--------The other widgets out of the canvas--------

scoreLabel = Label(root, text = 'Your score is: 0', bg = 'red')
scoreLabel.place(x = 300, y = 600)

root.mainloop()

rawDescription: Python Tkinter widget.place method does not work


code:
def tree(n):
    treeStr = ""
    for i in range(0,n):
        level = " "*(n-i) + "/" + " "*(2*i) + "\\"
        treeStr += level.center(n+1) + "\n"
    print(treeStr)

tree(10);

rawDescription: Printing a text tree in python

description: Printing a text tree in python


code:
df = df.set_index(['Date', 'ID'])
df = df.unstack(-1)
df = df.diff()
df = df.stack()
df = df.reset_index()

rawDescription: How can I keep the intersection of a panel between dates using Pandas?

description: Keep the intersection of a panel between dates using Pandas


code:
import cherrypy
import os
import json

MEDIA_DIR = os.path.join(os.path.abspath("."), "media")

class AjaxApp(object):
    @cherrypy.expose
    def index(self):
        # Sample page that displays the number of records in "table"
        # Open a cursor, using the DB connection for the current thread
        return """

hi

        """

    @cherrypy.expose
    def submit222(self):
        # get data from db
        res = { 'asdf': 1,
               'asdfw' : 3 }
        return json.dumps(res)

config = {'/media':
                {'tools.staticdir.on': True,
                 'tools.staticdir.dir': MEDIA_DIR,
                }
        }

cherrypy.tree.mount(AjaxApp(), '/', config=config)
cherrypy.engine.start()

rawDescription: Passing data in Cherrypy to a Javascript front-end chart


rawDescription: how to switch strings from 14/Nov/2012 into 2012/11/14 without converting to datetime in Python?

description: Switch strings from 14/Nov/2012 into 2012/11/14 without converting to datetime in Python


code:
fh = open('my.txt','r').read()
unique_chars = set(fh)
len(unique_chars) #for the length.

rawDescription: How to get all unique characters in a textfile? unix/python

description: Get all unique characters in a textfile? unix/python


code:
import os

with open("data.txt") as infile:
    for line in infile:
        line = line.strip()
        if os.path.isfile(os.path.join("sounds", line)):
            os.rename(os.path.join("sounds", line), os.path.join("sounds", os.path.splitext(line)[0] + '.wav'))

rawDescription: python compare and replace strings in txt file with filenames

description: Python compare and replace strings in txt file with filenames


code:
from re import sub
from rest_framework.authtoken.models import Token
from core.models import OrganizationRole, Organization, User

class OrganizationMiddleware(object):

  def process_view(self, request, view_func, view_args, view_kwargs):
    header_token = request.META.get('HTTP_AUTHORIZATION', None)
    if header_token is not None:
      try:
        token = sub('Token ', '', request.META.get('HTTP_AUTHORIZATION', None))
        token_obj = Token.objects.get(key = token)
        request.user = token_obj.user
      except Token.DoesNotExist:
        pass
    #This is now the correct user
    print (request.user)

rawDescription: Django and Middleware which uses request.user is always Anonymous


code:
def attrsetter(attr):
  def set_any(self, value):
    setattr(self, attr, value)
  return set_any

a = property(fset=attrsetter('_a'))
b = property(fset=attrsetter('_b'))
c = property(fset=attrsetter('_c'))

rawDescription: Python: multiple properties, one setter/getter


code:
def visit_expr(self, node):
    """Do something in here!"""
    self.generic_visit(node)

ExprVisitor = type('ExprVisitor', (ast.NodeVisitor,), {
    'visit_' % cls.__name__: visit_expr for cls in ast.expr.__subclasses__()})

rawDescription: Python 'ast' module with Visitor pattern - get node's group, not concrete class


code: some_operation((a and b) and c)

rawDescription: split a boolean expression by the outermost AND operators

description: Split a boolean expression by the outermost AND operators


code:
def get_int(prompt="Enter an Integer:"):
    while True:
        try:
           return int(input(prompt))
        except ValueError:
           print("That is not an integer!")

x = get_int("What is 1+1")

rawDescription: How to change pythons response to a string input in a int(input('Q1')), to a looped Q?

description: Change pythons response to a string input in a int(input('Q1')), to a looped Q


code:
def starts_with_vowel(word):
    # return True if the word starts with a vowel and False otherwise
    return word[0] in ['a', 'e', 'i', 'o', 'u']

def encode(word):
    # translate a single word to the secret language
    # call starts with vowel to decide which pattern to follow
    if starts_with_vowel(word):
        return word[1:] + word[0] + 'ar'
    else:
        return word + 'way'

def translate(message):
    # translate the whole text to the secret language
    # call encode to translate individual words in text
    return ' '.join(encode(word) for word in message)

rawDescription: Program in Python

description: Program in Python


code:
for rec in db:
    if not rec.deleted:
        row_tuple = (rec["name"], rec["address"], rec["age"])
        rows_list.append(row_tuple)

rawDescription: Python DBF module is adding extra rows to a table export


code:
inputs = ["abc def", ...]
inputs_map = zip(inputs, [i.replace(' ', '') for i in inputs])

user_input = get_user_input()

user_input = inputs_map.get(user_input, user_input)

rawDescription: How to add a space for user?

description: Add a space for user


code:
tgt="Hello [patient], you had a study on [date]"
template=tgt.replace('[', '{').replace(']', '}')
data={'patient':'Bob', 'date':'10/24/2013'}
template.format(**data)

rawDescription: Regex Replace multiple words, use them to build list


code:
import pyparsing
ing_ending = pyparsing.Regex(r'(?i)\b[a-z]+ing\b')
ing_ending.parseString("jumping")

rawDescription: Using pyparsing to match against a specific ending of a string


code:
from lxml.cssselect import CSSSelector
from lxml.html import fromstring
html = '

Blog Entry

'
h = fromstring(html)
sel = CSSSelector("a[rel=bookmark]")
[e.text for e in sel(h)]

rawDescription: Python library to do jQuery-like text extraction?


code:
# Your data
bakeries = ['a','b','c']
breadtypes = ['flat','rye','white']
# Output data
results = {}

for bakery in bakeries:
    # A line for each bakery
    print('How many of each type of bread for %s: ' % bakery)

    # The python3 dict comprehension and a generator
    results[bakery] = {
        breadtype: breadtypevalue for breadtype, breadtypevalue
        in ((bt, input('%s: ' % bt)) for bt in breadtypes)
    }

# Wanna print that?
for bakery in results:
    print('Here are the different types of breads for %s:' % bakery)
    print(', '.join(['%s: %s' % (breadtype, number) for breadtype, number in results[bakery].items()]))

rawDescription: List comprehension/list creating loop in python 3.x


code:
import csv

names = []
values = []

with open(input_file, 'r') as infile:
    csv_reader = csv.reader(infile, delimiter=' ')
    for line in csv_reader:
        names.append(line[0])
        values.append(line[1])
infile.close()

rawDescription: Python: read mixed data from text file

description: Python: read mixed data from text file


code:
import os
path = "some/path/to/location"
foldername = (self.directoryname.get())
file_path = os.path.join(path,foldername)
os.makedirs(file_path)
filename = 'results.xlsx'
full_path = os.path.join(file_path,filename)
workbook = xlsxwriter.Workbook(full_path)
workbook.close()

rawDescription: How to write(save) files to a directory instead of desktop?

description: Write(save) files to a directory instead of desktop


code:
from MyApp import myObj #instance of MyClass
import clr
clr.AddReference('System.Core')
from System import Func

def OtherMethod(ds):
    if ds.Data.Length > 0 :
        quot = sum(ds.Data.Real)/sum(ds.Data.Imag)
        return quot
    return 0.0

myObj.ProcessMethod = Func[IDataSource, object](OtherMethod)

rawDescription: Assigning an IronPython method to a C# delegate


code:
x = """
a comment

an answer

an answer

a reply

"""

xs = x.split("\n")

for tag in xs:
    if "<div" in tag and "</div" in tag:
        soup = BeautifulSoup(tag)
        html = "{}\n{}".format(html, "
{}
".format(soup.text))
    elif "") + 1:]))
    elif "</div" in tag:
        html = "{}\n{}".format(html, "
")

rawDescription: How to parse html nested blocks to lists with python BeautifulSoup?

description: Parse html nested blocks to lists with python BeautifulSoup


code:
# assuming `d` is your dictionary
order_as_string = ''.join([y[0] for y in sorted(d.iteritems(), key=lambda x: x[1], reverse=True)])

rawDescription: How do I sort a dictionary and get a string of the keys in python?

description: Sort a dictionary and get a string of the keys in python


code:
input_string = "FE CC ED ED CD"
pairs = [tuple(ord(letter) - ord('A') for letter in pair) for pair in input_string.split()]
output = ''.join(grid[x][y] for x, y in pairs)

rawDescription: Locating items in a nested list and decoding a ciphercode in python


code:
from mock import patch
foo = ['original', 'stuff']
with patch('__main__.foo', new=['mocked', 'items']):
    assert foo == ['mocked', 'items']
assert foo == ['original', 'stuff']

rawDescription: to mock a list using mock library 1.0.1

description: To mock a list using mock library 1.0.1


rawDescription: Matplotlib Plot list of 16 lists

description: Matplotlib Plot list of 16 lists


code:
from PIL import Image, ImageDraw

points = (
    (30, 40),
    (120, 60),
    (110, 90),
    (20, 110),
    (30, 40),
    )

im = Image.new("RGB", (130, 120))
dr = ImageDraw.Draw(im)
dr.line(points, fill="red", width=9)
for point in points:
    dr.ellipse((point[0] - 4, point[1] - 4, point[0]  + 4, point[1] + 4), fill="red")
im.save("polygon.png")

rawDescription: How to increase thickness of polygon in PIL ImageDraw

description: Increase thickness of polygon in PIL ImageDraw


code:
from multiprocessing import Process

def f(name):
    print('hello', name)

p = Process(target=f, args=('bob',))
p.start()
p.join()

rawDescription: Python multiprocessing returns nothing

description: Python multiprocessing returns nothing


rawDescription: Downloading a different file each day using Selenium


code:
def longest_list_in_tree(tree):
    if not isinstance(tree, list):
        return 0 # This is a leaf-value of the tree, it has "length 0"

    own_length = len(tree)
    longest_descendant_of_each_subtree = [
        longest_list_in_tree(subtree) for subtree in tree
    ]

    return max([own_length] + longest_descendant_of_each_subtree)

rawDescription: Can you explain the following function?

description: Explain the following function


code:
def parse_dir_contents(self, response):
    for sel in response.xpath('//div[@class="container"]'):
        item = LuItem()
        item['Description'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]//p').extract()
        item['Description'] = [ ''.join(field.split(',')) for field in item.get('Description', [])]
        yield item

rawDescription: Escape comma in Scrapy Xpath

description: Escape comma in Scrapy Xpath


code:
import numpy as np
points = np.random.rand(100, 3)
points[(0.5 < points[:,2]) & (points[:,2] < 0.6)]

rawDescription: Selecting specific array positions in Python


code:
import lxml.etree as ET
import io

content = '''<?xml
    version='1.1'
    encoding='utf-8'
?>
<!DOCTYPE root [
    <!ATTLIST test
        attr (A | B | C) 'B'
    >
    <!ELEMENT test (#PCDATA)>
    <!ELEMENT root (test)*>
]>
<root></root>'''

tree = ET.parse(io.BytesIO(content))
info = tree.docinfo
dtd = info.internalDTD

for elt in dtd.elements():
    print(elt)
    print(elt.content)
    print

# <lxml.etree._DTDElementDecl object name='test' prefix=None type='mixed' at 0xb73e044c>
# <lxml.etree._DTDElementContentDecl object name=None type='pcdata' occur='once' at 0xb73e04ac>

# <lxml.etree._DTDElementDecl object name='root' prefix=None type='element' at 0xb73e046c>
# <lxml.etree._DTDElementContentDecl object name='test' type='element' occur='mult' at 0xb73e04ac>

rawDescription: Getting internal DTD with lxml

description: Getting internal DTD with lxml


code:
substring = '(?=AAA)' #or "(?=AAA)"
len(re.findall(substring, string))
2

rawDescription: python re.findall() with list

description: Python re.findall() with list


rawDescription: How to find user-defined minimum and maximum value in a list of tuples

description: Find user-defined minimum and maximum value in a list of tuples


code:
def post_response(worker, req, environ, resp):
    worker.log.debug("%s", worker.pid)

rawDescription: Get worker id from Gunicorn worker itself


code:
merge_list = glob.glob(gndlbsum + "*gndlbsum.csv")
file_writer_lbsum = os.path.join(target_dir, "gndlbsum_master.csv")

# Append each csv file in the list to the output file
first_file = True
for file in merge_list:
    with open(file, "rU") as csv_file:
        reader = csv.reader(csv_file, delimiter="|")
        with open(file_writer_lbsum, "w" if first_file else "a") as f:
            writer = csv.writer(f, delimiter="|")
            # Unless it's the first file, skip its header row
            if not first_file:
                next(reader)
                first_file = False
            writer.writerows(reader)

rawDescription: csv writer puts quotes around each row

description: Csv writer puts quotes around each row


code:
import requests
response = requests.get('http://httpbin.org/get?foo=bar')
dir(response)
'url' in dir(response)
vars(response).keys()

rawDescription: Python-requests: Where to find all possible attributes?


code:
import datetime
current_time = datetime.datetime(2011,12,12,12,0,0) # High Noon!
end_time = datetime.datetime(2011,12,25,0,0,0)      # Santa?
while end_time-current_time > datetime.timedelta(0):
    do_stuff()
    current_time = current_time - datetime.timedelta(hours=1)

rawDescription: comparing times with datetime

description: Comparing times with datetime


code:
from models import Project
from rest_framework import serializers

class ProjectSerializer(serializers.ModelSerializer):
    """
    Serializes the Project model to send and receive valid JSON data.
    """
    # define a SerializerMethodField
    action = serializers.SerializerMethodField(method_name="get_data_for_action")

    class Meta:
      model = Project
      fields = ('action', 'title', 'id', 'endDate', 'startDate', 'product')

    def get_data_for_action(self, obj):
        return "createproject" # always add this value in the 'action' key of serialized object representation

rawDescription: Adding an action field to a post using Django Rest Framework


code: wrpcap("temp.cap", pkts)

rawDescription: Generate a pcap file at the end of a Python script


code: getWords = lambda x: ''.join(i if i.isalnum() else ' ' for i in x).split()

rawDescription: How to match alphanumeric characters in python regexp?

description: Match alphanumeric characters in python regexp


code:
N, K, M = map(int, input().split())
niz = list(map(int, input().split()))
nizk = list(range(1, K+1))
izlazi = []

def GetMinLengthStartingFromFirstElement(seq):
    nizkk = []
    for z in range(0, len(seq)):
        if seq[z] in nizk:
            continue
        else:
            return -1

    for p in range(0, len(seq)):
        if seq[p] not in nizkk:
            nizkk.append(seq[p])
            nizkk.sort()

        if nizkk == nizk:
            index = p
            return index+1
        else:
            continue
    return -1

def GetMinLength(seq):
    if len(seq) == 0:
        return -1
    else:
        curMinLength = GetMinLengthStartingFromFirstElement(seq)
        if curMinLength == -1:
            return -1
        minLengthFromRest = GetMinLength(seq[1:])
        if minLengthFromRest > -1:
            return min(curMinLength,minLengthFromRest)
        else:
            return curMinLength;

for r in range(0, M):
    operacija = list(map(int, input().split()))
    index = 0

    if operacija[0] == 2:
        minLength = GetMinLength(niz)
        izlazi.append(minLength)
    else:
        index, repl = map(int, operacija[1:])
        niz[index - 1] = repl

print(izlazi)

rawDescription: Competition practise task (Python)

description: Competition practise task (Python)


rawDescription: How to convert a tuple of tuples to a list of lists?

description: Convert a tuple of tuples to a list of lists


code:
import csv

with open('C:/Path/InputFile.csv', 'rb') as fin, \
     open('C:/Path/OutputFile.txt', 'wb') as fout:
    reader = csv.DictReader(fin)
    writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
    writer.writeheader()
    writer.writerows(reader)

rawDescription: Convert csv file to pipe delimited file in python


code:
merged = {}

for item in original:
    key = (item['price'], item['tickettypecode'], item['oneway'])
    if key in merged:
        for mergekey in ['inboundJourneys','outboundJourneys']:
            # assign extended copy rather than using list.extend()
            merged[key][mergekey] = merged[key][mergekey] + item[mergekey]
    else:
        merged[key] = item.copy()

mergedlist = merged.values()

rawDescription: How can I iterate over a list of dictionaries and merge dictionaries to form new shorter list of dicts?

description: Iterate over a list of dictionaries and merge dictionaries to form new shorter list of dicts


code:
@app.route("/<user_name>/<int:user_id>")
def user_details(user_name, user_id):
    # ...
    # some code
    # ...
    if request.args.get('settings'):
        return render_template('user_settings.html', ...)
    else:
        return render_template('user_details.html', ...)

rawDescription: Can I use one route for multiple functions?

description: Use one route for multiple functions


rawDescription: match part of a string until it reaches the end of the line (python regex)

description: Match part of a string until it reaches the end of the line (python regex)


code:
import threading
import time
def func(l):
    i = 0
    while i < 15:
            l.append(i)
            i += 1
            time.sleep(1)
def foo(t):
    t.start()
    i = 20
    while i > 0:
            print(l)
            i -= 1
            time.sleep(0.5)
l = []
t = threading.Thread(target=func, args = (l,))
foo(t)

rawDescription: what is wrong with python my python code?

description: What is wrong with python my python code


code:
from subprocess import Popen, PIPE

proc = Popen(["halSummarizeMutations", hal_output], stdout=PIPE)
summary_mutation = proc.communicate()[0]

rawDescription: Storing output of command in a variable

description: Storing output of command in a variable


code:
pd.Panel(dict(enumerate([pd.DataFrame(x) for x in data])))
pd.Panel(dict(enumerate([pd.DataFrame(x) for x in data])), items=['buy', 'sell'], major_axis=[0, 1], minor_axis=['eurusd', 'eurcny'])

rawDescription: List of JSON records into Pandas Panel?

description: List of JSON records into Pandas Panel


code:
import numpy as np
from scipy import sparse

users, I = np.unique(user_item[:,0], return_inverse=True)
items, J = np.unique(user_item[:,1], return_inverse=True)

points = np.ones(len(user_item), int)
mat = sparse.coo_matrix(points, (I, J))

rawDescription: construct sparse matrix using categorical data

description: Construct sparse matrix using categorical data


rawDescription: Perform a reverse cumulative sum on a numpy array


code:
imgAr1[0] = "slides/01.jpg";
imgAr1[1] = "slides/los-altos-remodel.jpg";
imgAr1[2] = "slides/los-altos-remodeling.jpg";
imgAr1[3] = "slides/los-altos-home-remodeling.jpg";
imgAr1[4] = "slides/home-remodel-los-altos.jpg";
imgAr1[5] = "slides/remodel-los-altos.jpg";
imgAr1[6] = "slides/los-altos-remodeling-contractors.jpg";
imgAr1[7] = "slides/remodeling-los-altos.jpg";
imgAr1[8] = "slides/los-altos-home-remodel.jpg";
imgAr1[9] = "slides/home-remodeling-los-altos.jpg";

rawDescription: How to get the images links from a page with slides

description: Get the images links from a page with slides


code:
import functools

def euler_014(max_=1000000):
    longest = max_len = None
    for n in range(1, max_):
        length = len_collatz(n)
        if max_len is None or length > max_len:
            max_len, longest = length, n
    return longest

def memoize(f):
    cache = {}
    @functools.wraps(f)
    def func(*args):
        if args not in cache:
            cache[args] = f(*args)
        return cache[args]
    return func

@memoize
def len_collatz(n):
    if n == 1:
        return 1
    if n % 2:
        n = (n * 3) + 1
    else:
        n //= 2
    return 1 + len_collatz(n)

rawDescription: Get the index of a maximum value in a set number of iterations in Python's Itertools


code:
import scipy.ndimage as im
x= im.median_filter(x, (self.m,self.m))

rawDescription: Remove spike noise from data in Python

description: Remove spike noise from data in Python


code:
from PyQt4 import QtCore, QtGui
from locale import getdefaultlocale

app = QtGui.QApplication(sys.argv)

locale = getdefaultlocale()

translator = QtCore.QTranslator(app)
translator.load('/usr/share/my_app/tr/qt_%s.qm' % locale[0])
app.installTranslator(translator)

rawDescription: How to add multi language support to a python QT program

description: Add multi language support to a python QT program


code:
def get_cell_content(self):
    currentRow = self.tableWidget.currentRow()
    currentCol = self.tableWidget.currentColumn()
    if currentRow > -1:
        text = (self.tableWidget.item(currentRow, currentCol).text(), )

    self.textBrowser.setHtml(text)

rawDescription: Information from QTableWidget Cell to QTextBrowser - PySide


code:
wb1 = load_workbook('master.xlsx')
wb2 = load_workbook('master.xlsx')
wb1 == wb2
wb1
wb2

rawDescription: Comparing openpyxl workbooks

description: Comparing openpyxl workbooks


code:
from collections import deque
queue = deque(maxlen=2)
queue.append(1)
queue
queue.append(2)
queue
queue.append(3)
queue

rawDescription: Appending into fix-sized list

description: Appending into fix-sized list


code: len([phrase for phrase in nltk.Chunker(sentence) if phrase[1] == 'VP'])

rawDescription: Detect English verb tenses using NLTK

description: Detect English verb tenses using NLTK


rawDescription: python app engine Model query for models in the same group

description: Python app engine Model query for models in the same group


code:
files = ['title1.pdf', 'title2.pdf', 'title3.pdf']
files += [ "%.2d.pdf" % j for j in range(1,100)]
pages = [[folder+file for file in files] for folder in foldernames]

rawDescription: Can anyone show me how to do this without using loops?


code:
tmp_filter = pandas.DataFrame(None) #The dataframe you want
# Create your list and your dataframe
tmp_list = [[1, 2], [1, 3]]
tmp_df = pandas.DataFrame([[1,2,4],[0,1,2],[1,3,0]], columns = ['A','B','C'])

#This function will pass the df pass columns by columns and
#only keep the columns with the value you want
def pass_true_df(df, cond):
    for i, c in enumerate(cond):
        df = df[df.iloc[:,i] == c]
    return df

# Pass through your list and add the row you want to keep
for i in tmp_list:
    tmp_filter = pandas.concat([tmp_filter, pass_true_df(tmp_df, i)])

rawDescription: Selecting rows from pandas DataFrame using a list


code:
s = """List of the keyboard shortcuts for Word 2000
Sequences: strings, lists and tuples - PythonLearn"""
findword = 'lists'
numwords = 2

for i in s.split('\n'):
    z = i.split(' ')

    for x in [x for (x, y) in enumerate(z) if findword in y]:
        print(' '.join(z[max(x-numwords,0):x+numwords+1]))

rawDescription: How I display 2 words before and after a key search word in Python


rawDescription: Getting the first elements of a dictionary without using a loop


rawDescription: Does the python range generator generate all values or yields them progressively?


code:
import numpy as np
import matplotlib.pyplot as plt
plt.ion()

# make some data of some size
nblocks = 10
block_size = 1000
size = block_size*nblocks
data = np.random.normal(0.,1.,size=size)

# create a matplotlib figure with some plotting axes
fig = plt.figure()
ax = fig.add_subplot(111)

# display the figure
plt.show()

# storage for blocks to keep
kept_blocks = []

for block in data.reshape(nblocks,block_size):
    #plot the block
    ax.plot(block)

    #force matplotlib to rerender
    plt.draw()

    # ask user for some input
    answer = raw_input("Keep block? [Y/n]")
    if answer.lower() != "n":
        kept_blocks.append(block)

    #clear the plotting axes
    ax.cla()

# turn the kept blocks into a 2D array
kept_blocks = np.vstack(kept_blocks)

#or a 1D array
#kept_blocks = np.hstack(kept_blocks)

rawDescription: Windows of data with possibility to decide if store


code:
fig, ax = plt.subplots(1)
ax.hist(data)

vmin = data.min().min()
vmax = data.max().max()
data_range = vmax - vmin
scale_factor = 0.1
xmin = vmin - scale_factor*vmax
xmax = vmax + scale_factor*vmax
ax.set_xlim(xmin, xmax)

rawDescription: How to reduce size of a plot, keeping numbers axis the same?

description: Reduce size of a plot, keeping numbers axis the same


code:
import urllib2
import simplejson as json

opener = urllib2.build_opener()
req = urllib2.Request('http://localhost:8000', data=json.dumps({'a': 'b'}),
      headers={'Content-Type': 'application/json'})
response = opener.open(req)

rawDescription: Not possible to set content-type to application/json using urllib2


code:
f = open("dna.txt","r")
sequence_1 = f.readline()
sequence_1 = f.readline()  # Get the actual sequence.
sequence_line = False      # This will switch back and forth to skip every other line.
for line in f:             # Iterate over all remaining lines.
  if sequence_line:        # Only test this every other line.
    if line == sequence_1:
      print("Conserved")
    else:
      print("Not Conserved")
  sequence_line = not sequence_line   # Switch the boolean every iteration.
f.close()

rawDescription: Python comparing strings within a conditional


rawDescription: Splitting Python List

description: Splitting Python List


code:
import csv

with open(filename, 'rb') as inputfile:
    reader = csv.reader(inputfile, delimiter=';')
    next(reader)  # skip first line

    names, values = zip(*reader)

rawDescription: Two lists from list comprehension in python language


code: maze = raw_input('Maze File: ')

rawDescription: How to avoid IndexError

description: Avoid IndexError


code: s[s.find(s_l)+len(s_l): s.find(s_r)]

rawDescription: Python: getting substring starting and ending between two given substrings


code:
import json
d = {'Chemistry': 97.27, 'Civics': 100.0, 'History': 73.07}
lst = [{ 'class': { k: { 'grade': v } } } for k, v in d.items()]
lst
json.dumps(lst)
print(json.dumps(lst, indent=2))

rawDescription: Creating JSON in Python

description: Creating JSON in Python


code:
class Param(caffe.Layer):
    def setup(self, bottom, top):
        self.blobs.add_blob(1,2,3)
        self.blobs[0].data[...] = 0

    def reshape(self, bottom, top):
        top[0].reshape(10)

    def forward(self, bottom, top):
        print(self.blobs[0].data)
        self.blobs[0].data[...] += 1

    def backward(self, top, propagate_down, bottom):
        pass

rawDescription: Pycaffe: How to create custom weights in a python layer?


code:
#! /usr/bin/env python

from subprocess import call, STDOUT
import os
if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w')) != 0:
    print("Nope!")
else:
    print("Yup!")

rawDescription: Python script to determine if a directory is a git repository


code:
string_list = ["PII", "AZO", "CVCO", "MOD"]
from itertools import combinations
for a, b in combinations(string_list, 2):
    print(a, b)

rawDescription: How to avoid comparing duplicate strings in a list

description: Avoid comparing duplicate strings in a list


code:
split_string = line.split(",")
for element in split_string:
    if element in Lethality:
        count = count + 1

rawDescription: Python - Count how many elements from a list appear in a csv line


code:
indices = [['islink',], ['authors', 0, 'name']]
result = []
for index in indices:
  value = input_dict
  for single_index in index:
    value=value[single_index]
  result.append(value)

rawDescription: Mapping nested dictionaries in python

description: Mapping nested dictionaries in python


code:
from multiprocessing import Process, Manager

def worker_func(proc_id,results):

    # Do your thing

    results[proc_id] = ["your dataset from %s" % proc_id]

def convert_dataset_to_csv(dataset):

    # Placeholder example.  I realize what its doing is ridiculous

    converted_dataset = [ ','.join(data.split()) for data in dataset]
    return  converted_dataset

m = Manager()
d_results= m.dict()

worker_count = 100

jobs = [Process(target=worker_func,
        args=(proc_id,d_results))
        for proc_id in range(worker_count)]

for j in jobs:
    j.start()

for j in jobs:
    j.join()

with open('somecsv.csv','w') as f:

    for d in d_results.values():

        # if the actual conversion function benefits from multiprocessing,
        # you can do that there too instead of here

        for r in convert_dataset_to_csv(d):
            f.write(r + '\n')

rawDescription: concatenating csv files nicely with python

description: Concatenating csv files nicely with python


rawDescription: Fast image coordinate lookup in Numpy

description: Fast image coordinate lookup in Numpy


code:
result = [
    {
        'id': id,
        'name': next(x['name'] for x in d if x['id'] == id),
        'movies': [x['movie']  for x in d if x['id'] == id]
    }
    for id in set(x['id'] for x in d)]

rawDescription: Combine a list of dictionaries ; specific use case


code: regex_a = re.compile("(a|an)$")

rawDescription: Using the Regex OR operator to accommodate user input of "A" or "An"


rawDescription: Python: Find Surrounding parts of a string with irrelevant section in the middle


code: {i:n for i,n in enumerate("hello")}

rawDescription: A Small Addition to a Dict Comprehension


code: s.sendmail(FROM, TO, "the answer is " + str(x))

rawDescription: in python, how to send variable value by email

description: In python, how to send variable value by email


code:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
# add nodes, edges, etc.
nx.draw_spring(G)
plt.show()

rawDescription: Draw a weighted complex network with NetworkX


code:
#Create the file-like objects from canvases
file_like_1 = StringIO.StringIO()
file_like_2 = StringIO.StringIO()
#... etc...
canvas_1.print_pdf(file_like_1)
canvas_2.print_pdf(file_like_2)
#...etc....

#NOW create the zipfile
response = HttpResponse(mimetype='application/zip')
response['Content-Disposition'] = 'filename=all_plots.zip'

buff = StringIO.StringIO()
archive = zipfile.ZipFile(buff,'w',zipfile.ZIP_DEFLATED)
archive.writestr('plot_1.pdf',file_like_1.getvalue())
archive.writestr('plot_2.pdf',file_like_2.getvalue())
#..etc...
archive.close()
buff.flush()
ret_zip = buff.getvalue()
buff.close()
response.write(ret_zip)
return response

rawDescription: Python/Django - Can I create multiple pdf file-like objects, zip them and send as attachment?


code:
node_coords = { i: (node_coords_tmp[0][ct],
                    node_coords_tmp[1][ct],
                    node_coords_tmp[2][ct]) for ct, i in enumerate(node_map) }

rawDescription: What is an efficient way to convert arrays/lists to dictionaries?


code:
for scary in scary_gov_data:

  t = Table(scary['name'], metadata,
      Column('col1', Integer, primary_key=True),
      Column('col2', String(16), nullable=False))

rawDescription: Python/SqlAlchemy: Binding attributes (100s) to Base sub-classes sensibly


code:
def calculateBill(food):
    total = 0
    for k in food:
        if inventory.get(k, 0) > 0:
            total += prices[k]                # updates total
            inventory[k] = inventory[k] - 1   # updates inventory
    return total

rawDescription: Can't figure out a Python exercise with dictionaries


code:
def Kaprekar(number):
    if number == '6174':
        return 1
    elif number != '6174':
        sortedString = ''.join(sorted(number))
        reversedString = ''.join(sorted(number, reverse=True))
        sortedIntMin = int(sortedString)
        reversedIntMax = int(reversedString)
        num = reversedIntMax - sortedIntMin
        print("processen kors", num )
        return  1 + Kaprekar(str(num))

print(" helo world,  lets do this: ")
print("det behovdes " , Kaprekar("1547") , "iterationer for processen")

rawDescription: convert following iterative code to recursive Python

description: Convert following iterative code to recursive Python


code:
import scrapy
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BboSpider(scrapy.Spider):
    name = "bbo"
    allowed_domains = ["bridgebase.com"]
    login_page = "http://www.bridgebase.com/myhands/myhands_login.php?t=%2Fmyhands%2Findex.php%3F"

    def start_requests(self):
        driver = webdriver.PhantomJS()
        driver.get(self.login_page)

        driver.find_element_by_id("username").send_keys("user")
        driver.find_element_by_id("password").send_keys("password")

        driver.find_element_by_name("submit").click()

        driver.save_screenshot("test.png")
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "Click here for results of recent tournaments")))

        cookies = driver.get_cookies()
        driver.close()

        yield scrapy.Request("http://www.bridgebase.com/myhands/index.php", cookies=cookies)

    def parse(self, response):
        if "recent tournaments" in response.body:
            self.log("Login successful")
        else:
            self.log("Login failed")
        print(response.body)

rawDescription: Using InitSpider with splash: only parsing the login page?


code:
from flask import Flask, render_template, session, request, redirect, url_for
import xmlrpclib
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
            session['user'] = request.form['username']
            session['passwd'] = request.form['password']
            return redirect(url_for('menu'))
    return render_template('login.html')
@app.route('/menu/')
def menu():
    user = session.get('user', None)
    passwd = session.get('passwd', None)

rawDescription: Python CGI: Persistence with Session Keys


code:
import pygame
from pygame.locals import *

def name():
    pygame.init()
    screen = pygame.display.set_mode((480, 360))
    name = ""
    font = pygame.font.Font(None, 50)
    while True:
        for evt in pygame.event.get():
            if evt.type == KEYDOWN:
                if evt.unicode.isalpha():
                    name += evt.unicode
                elif evt.key == K_BACKSPACE:
                    name = name[:-1]
                elif evt.key == K_RETURN:
                    name = ""
            elif evt.type == QUIT:
                return
        screen.fill((0, 0, 0))
        block = font.render(name, True, (255, 255, 255))
        rect = block.get_rect()
        rect.center = screen.get_rect().center
        screen.blit(block, rect)
        pygame.display.flip()

if __name__ == "__main__":
    name()
    pygame.quit()

rawDescription: how to make pygame print input from user

description: Make pygame print input from user


code:
#import package1.subpackage1 as subpackage1 # AttributeError: 'module' object has no attribute 'subpackage1'
subpackage1 = sys.modules[__name__.rpartition('.')[0]] # parent module

rawDescription: Import parent module package

description: Import parent module package


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

from body_parser import Extractor
import re

class FEOProcessor(object):

    CHECKS = [
        ('Standard JavaScript Inlining Optimization', ('EMBED_JAVASCRIPT',), 'check_js_inlining'),
        ('HTML5 Advanced Cache', ('JAVASCRIPT_HTML5_CACHE', 'CSS_HTML5_CACHE'), 'check_html5_advanced_cache'),
        ('Cookieless Resource Domain', ('RENAME_JAVASCRIPT', 'RENAME_CSS'), 'check_cookieless_resource_domain'),
        ('Minificatiopn of JS', ('MINIFY_JAVASCRIPT',), 'check_js_minifaction'),
        ('File Versioning', ('RENAME_JAVASCRIPT', 'RENAME_IMAGE', 'RENAME_CSS'), 'check_file_versioning'),
        ('Small Image Embedding', ('EMBED_IMAGE',), 'check_small_image_embedding'),
        ('Responsive Image Loading', ('RESPONSIVE_IMAGES',), 'check_responsive_image_loading'),
        ('Asynchronous JS and CSS Loading', ('ASYNC_JAVASCRIPT',), 'check_async_js_and_css_loading'),
        ('JS Pre-Execution', ('PRE_EXECUTE_JAVASCRIPT',), 'check_js_pre_execution'),
        ('EDGESTART', ('EDGESTART',), 'check_edgestart'),
        ('Invoke Click OnTouch', ('BlzFastClick',), 'check_click'),
        ('Cellular Connection Keep-Alive', ('blzEnableMobileHeartbeat',), 'check_cell'),
        ('On Demand Image Loading', ('img',), 'check_img'),
    ]

    def __init__(self):
        self.parser = Extractor()
        self.result = dict((k, False) for k,_,_ in self.CHECKS)

    for _, keys, name in CHECKS:
        locals()[name] = lambda self, result, _keys=keys: all(result.get(k, 0)>0 for k in _keys)

    def process_feo_debug_output(self, analysis_id, url):
        feed = self.parser.start_parser(analysis_id, url, True)
        # this works only if feed is the same as soup as you said
        result = self.get_feo_tags(feed)
        for name, _, func in self.CHECKS:
            if name == 'On Demand Image Loading':
                self.result[name] = (False, True)[getattr(self,func)(feed)]
            else:
                self.result[name] = (False, True)[getattr(self,func)(result)]
        return self.result

    def get_feo_tags(self, feed):
        result = {}
        tag_list = re.findall(r'(?:TextTransApplied):\s*((?:(?:[A-Z]+(?:_[A-Z\d]+)+)?\(\d+\)\s*(?:,\s*|;))*)', str(feed))
        for tag in tag_list:
            for element in tag.split(","):
                index = element.index('(')
                if element[:index].strip():
                    result[element[:index].strip()] = (element.split("(")[1].rstrip(");"))
        return result

    def check_edgestart(self, result):
        return 1 if 'EDGESTART' in result.keys() else 0

    def check_click(self, result):
        return 1 if 'BlzFastClick' in result.keys() else 0

    def check_cell(self, result):
        return 1 if 'blzEnableMobileHeartbeat' in result.keys() else 0

    def check_img(self, feed):
        return 1 if feed.find_all('img', attrs={'data-blzsrc': True, 'src': lambda x: 'data' not in x}) else 0

rawDescription: Add another use case to a python code

description: Add another use case to a python code


code:
with open ('d.txt','a') as fh:
  fh.write('Some text\n')
  savetxt(fh, a, delimiter=',')
  fh.write('Some other text\n')
  savetxt(fh, b, delimiter=',')

rawDescription: Saving numpy arrays as part of a larger text file


rawDescription: scrapy scrape html source code

description: Scrapy scrape html source code


code:
#default method to get file upload path
def get_upload_to(instance, filename):
    return instance.get_upload_to_path(filename)

class BaseImage(models.Model):
    description = models.CharField(max_length=200)
    image = models.ImageField(upload_to=get_upload_to)

    class Meta:
        abstract = True
    #method on the class to provide upload path for files specific to these objects
    def get_upload_to_path(instance, filename):
         return 'images/'+filename

class PostImage(BaseImage):
    in_text = models.BooleanField()

    #method to provide upload path for PostImage objects
    def get_upload_to_path(instance, filename):
    #change date.year etc to appropriate variables
         return 'images/news/%Y/%m/%d' % (date.year, date.month, date.day)

rawDescription: Class attribute inheritance in Django models


code:
re_pattern = '''
    \{t[ ]{1}           # start tag name
    d="                 # "d" attribute
    ([a-zA-Z0-9]*)      # "d" attribute content
    ".*?\}              # end of "d" attribute
    (.+?)               # tag content
    \{/t\}              # end tag
'''

rawDescription: Python regex: Multiple matches in one line (using findall())


rawDescription: zipping an arbitrary-length list of lists

description: Zipping an arbitrary-length list of lists


code:
s = socket.socket(...)
...
all_data = s.makefile().read(-1)  # or, equivalently, readall()
s.close()

rawDescription: Python (UNIX) sockets -- read all data

description: Python (UNIX) sockets -- read all data


code:
def show_most_informative_features(self, n=10):
    strlist = []
    # Determine the most relevant features, and display them.
    cpdist = self._feature_probdist
    # print('Most Informative Features')
    strlist.append('Most Informative Features')

    for (fname, fval) in self.most_informative_features(n):
            def labelprob(l):
                return cpdist[l,fname].prob(fval)
            labels = sorted([l for l in self._labels
                     if fval in cpdist[l,fname].samples()],
                    key=labelprob)
            if len(labels) == 1: continue
            l0 = labels[0]
            l1 = labels[-1]
            if cpdist[l0,fname].prob(fval) == 0:
                ratio = 'INF'
            else:
                ratio = '%8.1f' % (cpdist[l1,fname].prob(fval) /
                          cpdist[l0,fname].prob(fval))
            # print(('%24s = %-14r %6s : %-6s = %s : 1.0' %
            #      (fname, fval, ("%s" % l1)[:6], ("%s" % l0)[:6], ratio)))
            strlist.append(('%24s = %-14r %6s : %-6s = %s : 1.0' %
                          (fname, fval, ("%s" % l1)[:6], ("%s" % l0)[:6], ratio)))

    return strlist

# Useage
list = show_most_informative_features(classifier, 100)
file.writelines(list)

rawDescription: How to write features in nltk to a txt file?

description: Write features in nltk to a txt file


code:
from ast import literal_eval
s = r'''[{'text': 'This is my legit "test" \'message\' with "lots" of \'quotation marks\'', 'type': 'message', 'user': '<userID>', 'channel': '<channelID>', 'ts': '1450501889.000074', 'team': '<teamID'}]'''
print(literal_eval(s)[0]["text"])

rawDescription: Python Regex: Can't extract message containing escaped quotes


code:
import re
import csv

lineformat = re.compile(
    r'^(?P<count>\d+)[\s:]*'
    r'test[\s:]*(?P<test>\d+)[\s:]*'
    r'width[\s:]*(?P<width>\d+)[\s:]*'
    r'distance[\s:]*(?P<distance>\d+)[\s:]*'
    r'time[\s:]*(?P<time>\d+)[\s:]*'
    r'x[\s:]*(?P<x>\d+\.\d+)[\s:]*'
    r'y[\s:]*(?P<y>\d+\.\d+)[\s:]*'
    r'target[\s:]*(?P<target>\d+-\d+)[\s:]*'
)
fields = ('count', 'test', 'width', 'distance', 'time', 'x', 'y', 'target')

with open(inputfile) as finput, open(outputfile) as foutput:
    csvout = csv.DictWriter(foutput, fields=fields)
    for line in finput:
        match = lineformat.search(line)
        if match is not None:
            csvout.writerow(match.groupdict())

rawDescription: changing the format of a tab delimited csv file

description: Changing the format of a tab delimited csv file


code:
import sys

y = 1

def test():
    print("Hello?")
    x = (sys.stdin.readline())
    if x == ("hello\n"):
        print("Ah your back")
    else:
        print("Huh?")

while y == 1:
    test()

rawDescription: Answering Stdin With String

description: Answering Stdin With String


code:
bin_range = [0, 100000]
bin_input = raw_input("Enter bins as comma seperated list")
bin_input_parsed = [int(x.strip()) for x in bin_input.split(',')]

bins = [bin_range[0]] + bin_input_parsed + [bin_range[1]]
pd.cut(df['Quantity'], bins)

rawDescription: Pandas cut with user-defined bins

description: Pandas cut with user-defined bins


code:
def has_duplicate(input_str):
    fruits = []
    # Working with the string to create the "1st level" list
    for sublist in input_str.replace(' ','').replace('{','').split('}'):
        # In each list, getting the different fruits (list(set(..)))
        for fruit in list(set(sublist.split(','))):
            fruits.append(fruit)
    # now 'fruits' contains the list of the different fruits from each sublist
    # and contains multiple times the same fruit only if it appears in different
    # sublists
    return not len(list(set(fruits)))==len(fruits)

Input_Str = " {Apple, Banana, Orange} {Grapes, Pineapple} {Banana,Peach,Mango,strawberry }"
print(has_duplicate(Input_Str))

Input_Str = "{Apple, Banana, Apple} {Grapes, Pineapple} {Orange,Peach,Mango,strawberry }"
print(has_duplicate(Input_Str))

rawDescription: Finding duplicate substring in another part of string in Python


code:
for item1, item2 in zip(structure1, structure2):
    do_something(item1, item2)

rawDescription: How to cleanly iterate over two structures at the same time?

description: Cleanly iterate over two structures at the same time


code:
urlpatterns = patterns('',
    url(r'^product/(?P<name>[\w\+]+)/$', 'crunch.views.product_by_name', name='preview_by_name'),
)

rawDescription: Django pass multiple words through a URL conf


code:
def item_completed(self, results, item, info):

    for result in [x for ok, x in results if ok]:
        path = result['path']
        slug = slugify(item['designer'])

        settings = get_project_settings()
        storage = settings.get('IMAGES_STORE')

        target_path = os.path.join(storage, slug, os.path.basename(path))
        path = os.path.join(storage, path)

        # If path doesn't exist, it will be created
        if not os.path.exists(os.path.join(storage, slug)):
            os.makedirs(os.path.join(storage, slug))

        if not os.rename(path, target_path):
            raise DropItem("Could not move image to target folder")

    if self.IMAGES_RESULT_FIELD in item.fields:
        item[self.IMAGES_RESULT_FIELD] = [x for ok, x in results if ok]
    return item

rawDescription: How to download scrapy images in a dyanmic folder based on

description: Download scrapy images in a dyanmic folder based on


code:
[i for i in range(10)]
list(i for i in range(10))

rawDescription: Append to an empty list using a single line for-loop python


code: df1 = df1.join(df2, how='outer')

rawDescription: pandas combining 2 dataframes with different date indices

description: Pandas combining 2 dataframes with different date indices


code:
import urllib2

req = urllib2.Request("ftp://www.ngs.noaa.gov/cors/rinex/2014/100/txga/txga1000.14d.Z")
response = urllib2.urlopen(req)
data = response.read()

rawDescription: downloading a file, not the contents

description: Downloading a file, not the contents


code: {{ model.field|date:'l' }}

rawDescription: how to get day name from queryset in django template?

description: Get day name from queryset in django template


code:
# read all lines from the file into a list, where each list element is one line
lines = infile.readlines()

# iterate through the list three lines at a time, until you run out of lines
line_number = 0
while line_number < len(lines):
    to_line = lines[line_number]
    from_line = lines[line_number+1]
    header_line = lines[line_number+2]
    line_number += 3

rawDescription: Assigning multiple lines of a file to multiple variables using .readline() while in a "for line in data" loop


code:
def render_with_jinja2(self, values, template_name):
    import jinja2
    env = jinja2.Environment(...)

rawDescription: Best practice for lazy loading Python modules


code:
#!/usr/bin/env python
import csv
filesource = 'PeopleAndResources.csv'
with open(filesource, 'rb') as f:
    reader = csv.reader(f, delimiter=';', quotechar='"')
    for row in reader:
        statement = "INSERT INTO ImportCSVTable " + \
        "(name, address, phone) " + \
        "VALUES ('%s', '%s', '%s')" % (tuple(row[0:3]))
        #execute statement

rawDescription: Reading and Storing csv data line by line in a postgres


code:
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
content = body['content']

rawDescription: Trying to parse `request.body` from POST in Django


code:
# adjust the plot limits
cut = 1.05
xmax= cut*max(xx for xx,yy in pos.values())
ymax= cut*max(yy for xx,yy in pos.values())
plt.xlim(0,xmax)
plt.ylim(0,ymax)

rawDescription: Saving a matplotlib/networkx figure without margins


code:
import xml.etree.ElementTree as ET
xmlstr=\
tree=ET.fromstring(xmlstr)
tree.remove(tree.findall('.//B')[1])

rawDescription: How do I remove a node in xml using ElementTree in Python?

description: Remove a node in xml using ElementTree in Python


code:
import timeit

start = timeit.default_timer()

rawDescription: time.time() not timing

description: Time.time() not timing


code:
cn = MySQLdb.connect(...)
c = cn.cursor()
try:
    c.executemany("""
        INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)
    """,  [
        (name_new,lot_bid_1_new,bid_1_new,off_1_new,lot_off_1_new,waktu),
        (name_new,lot_bid_2_new,bid_2_new,off_2_new,lot_off_2_new,waktu),
        # .. etc.
        (name_new,lot_bid_10_new,bid_10_new,off_10_new,lot_off_10_new,waktu)
    ])
    cn.commit()
except:
    cn.rollback()
    raise

rawDescription: Insert data in one table within multiple app at the same time with MySQL


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

df = pd.DataFrame(np.random.random((4,3)), columns=list('ABC'))
for col in ('A', 'B'):
    df[col].plot(legend=True)
plt.show()

rawDescription: How to plot two columns in a dataframe and add legends?

description: Plot two columns in a dataframe and add legends


code:
def to_ints_if_possible(seq):
    return [int(s) if s.isdigit() else s for s in seq]
lst = ["hello","ASKDJ","1","4","xcvs"]
converted = to_ints_if_possible(lst)

rawDescription: How to convert ints in a list of strings into ints in python?

description: Convert ints in a list of strings into ints in python


code:
class MagazineIssueForm(Form):
    title  = StringField()
    year   = IntegerField('Year')
    month  = SelectField(choices=MONTHS)

def edit_issue():
    publication = get_something_from_db()
    form = MagazineIssueForm(...)

    if publication.frequency == 'annual':
        del form.month

    # render our form

rawDescription: FlaskWTF validator prevents hiding form field


code:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.grid(False)
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

rawDescription: mplot3d: how to show the ticks but not the grids?

description: Mplot3d: how to show the ticks but not the grids


code: (-random() > random())*random()

rawDescription: Interesting task using random numbers only


code:
import string

def func(str1, str2):
    s1 = set(str1)
    s2 = set(str2)
    # requests are ambiguous "contained in both strings" can be either interpreted as intersections or union (i chose intersection)
    uni = s1.union(s2)
    letters_in_both_strings = [item for item in s1.intersection(s2) if item.isalpha()]
    non_letters_in_any_string = [item for item in uni if not item.isalpha()]
    all_letters = set(string.ascii_uppercase + string.ascii_lowercase)
    return letters_in_both_strings, all_letters.difference(uni), non_letters_in_any_string

str1 = input("Enter 1st string:")
str2 = input("Enter 2nd string:")
a, b, c = func(str1, str2)
print("|" + "".join(a) + "|\n|" + "".join(b) + "|\n|" + "".join(c) + "|\n")

rawDescription: Using sets in python to find common letters from two strings


rawDescription: Python matplotlib interval

description: Python matplotlib interval


code:
class PingPong:
    def ping(self):
        return "pong"

    def run(self):
        bottle.route('/ping', callback=self.ping)
        bottle.run()

if __name__ == "__main__":
    p = PingPong()
    p.run()

rawDescription: Why is self superfluous for a method when using bottle in a class?


code:
class SAOrderDetailForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ('sa_date',)
    sa_date = CustomDateTimeField()

class FASAOrderDetailForm(SAOrderDetailForm): #Inheritance matters!
    class Meta:
        model = Order
        fields = ('sa_date','fa_date')
    fa_date = CustomDateTimeField()

if foo:
    FormClass = FASAOrderDetailForm
else:
    FormClass = SAOrderDetailForm

rawDescription: Same datetime format for both display and input for DateTimeInput in Django


code:
def list(request):
    if request.method == 'POST':
        form = MyUploadForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = form.cleaned_data['docfile']
            for file_obj in request.FILES.getlist('docfile'):
                Document.objects.create(docfile=file_obj)
            return HttpResponseRedirect(reverse('multiupload.views.list'))

rawDescription: django mutiupload not working

description: Django mutiupload not working


code:
d={}
d['asdf']=3
d['ele']=90
d
d['ele']=None
d
del d['ele']
d

rawDescription: python: Is this a wrong way to remove an element from a dict?

description: Python: Is this a wrong way to remove an element from a dict


code:
class _Internal:
    ...

rawDescription: "Private" (implementation) class in Python


code:
for small_entry in small_list:
    for big_entry in big_list:
        if small_entry[1] == big_entry[1] :
            print(small_entry[0] + "matches" +  big_entry[0])

rawDescription: Identifying coordinate matches from two files using python


rawDescription: How to check if value is nan in unittest?

description: Check if value is nan in unittest


code:
with open("filename.txt") as f:
    numbers = [float(line.split()[2]) for line in f]

rawDescription: Filtering out file contents in Python

description: Filtering out file contents in Python


code:
from elementtree.SimpleXMLWriter import XMLWriter
import sys

w = XMLWriter(sys.stdout)

html = w.start("html")

w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()

w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")

w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")

w.close(html)

rawDescription: Iteratively write XML nodes in python

description: Iteratively write XML nodes in python


code:
logger = cherrypy.log.access_log
logger.removeHandler(logger.handlers[0])

rawDescription: Silencing cherrypy

description: Silencing cherrypy


code:
class LFSeq(object):
    def __init__(self, func):
        self.evaluated = []
        self.func = func
    def __iter__(self):
        index = 0
        while True:
            if index == len(self.evaluated):
                self.evaluated.append(self.func())
            yield self.evaluated[index]
            index += 1

rawDescription: Implicitly  binding callable objects to instances


rawDescription: How to save trajectories of tracked objects with trackpy?

description: Save trajectories of tracked objects with trackpy


code:
from django import template
import dateutil

register = template.Library()

@register.filter(name='to_date')
def to_date(value):
    return dateutil.parser.parse(value)

rawDescription: Django templatetags ISO to date

description: Django templatetags ISO to date


code:
def test_add_category_post(app, session):
    """Does add category post a new category?"""
    TESTEMAIL = "test@test.org"
    TESTUSER = "Joe Test"
    user = Users.query.filter(Users.email==TESTEMAIL).first()
    category = Category(name="Added Category", users_id=user.id)
    form = CategoryForm(formdata=None, obj=category)
    with app.test_client() as c:
        with c.session_transaction() as sess:
            sess['email'] = TESTEMAIL
            sess['username'] = TESTUSER
            sess['users_id'] = user.id
            response = c.post(
                '/category/add', data=form.data, follow_redirects=True)
    assert response.status_code == 200
    added_category = Category.query.filter(
        Category.name=="Added Category").first()
    assert added_category
    session.delete(added_category)
    session.commit()

rawDescription: Testing a POST that uses Flask-WTF validate_on_submit


code:
counter = 0
while True:
   line = day_out.readline()
   if "day" in line:
      do_things()
      counter = 0
   elif line == '':
      time.sleep(60)
      counter += 1
   if counter > 5:
       output("day file not updating")

rawDescription: Python issue with tracking continuously updated log file


code:
Table=[['Name','Age','Level','Score'],['Tom','15','5','999'],
   ['Bob','105','7','2']]

LongestItem=0
for List in Table:
    for Item in List:
        if len(Item)>LongestItem:
            LongestItem=len(Item)

for List in Table:
    for Item in enumerate(List):
        List[Item[0]] = '{:{}}'.format(List[Item[0]],LongestItem)

for Index in range(len(Table)):
    print(' '.join(Table[Index]))

rawDescription: table printer with sort

description: Table printer with sort


code:
import requests
from lxml import html

response = requests.get('http://cse.iitkgp.ac.in/faculty4.php?_=1450503917634')
parsed_body = html.fromstring(response.content)

for row in parsed_body.xpath('.//td[@class="fcardcls"]'):
    name = row.findtext(".//a[@href]/b")
    name = ' '.join(name.split())  # getting rid of multiple spaces

    research_areas = row.xpath('.//*[. = "Research Areas: "]/following-sibling::text()')[0].split(", ")

    print(name, research_areas)

rawDescription: Python lxml xpath no output

description: Python lxml xpath no output


code: list(chunks(range(0, 11), 4))

rawDescription: Generating a enclosing div tag every fourth element in python


code:
c = "cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"
from http.cookies import SimpleCookie
cookie = SimpleCookie()
cookie.load(c)
cookie
{key: value.value  for key, value in cookie.items()}

rawDescription: Python - convert set-cookies response to array of cookies


code:
myObjs = ['myObject1','myObject2','myObject3']

for obj in myObjs:
    setattr(className, obj, mayaClass.create(vars()[objs]))
    func1()
    func2()

rawDescription: Creating variable names dynamically from list


code:
from django.test import Client

@given('I am logged in')
def impl(context):
    c = Client()
    c.login(email='test@email.com',password='password')

    cookie = c.cookies['sessionid']

    b = context.browser
    b.get(context.server_url + '/404-loads-fastest/') # selenium will set cookie domain based on current page domain
    b.add_cookie({'name': 'sessionid', 'value': cookie.value, 'secure': False, 'path': '/'})

rawDescription: How to login user during Given step using django-behave

description: Login user during Given step using django-behave


rawDescription: numpy convert categorical string arrays to an integer array

description: Numpy convert categorical string arrays to an integer array


code:
url = "http://action.sumofus.org/api/ak_action_count_by_action/?action=nhs-patient-corporations&additional="
number = int(requests.get(url).text)

rawDescription: Item visible in browser not collected by scraper


rawDescription: Is it faster to loop through a Python set of number or a set of letters?


code:
import re
html = '''

CORPORATE HEADQUARTERS

277 Park Avenue

New York, New York 10172

United States

BACKGROUND

He graduated Blabala

'''
re.search('(?s)
\s*CORPORATE HEADQUARTERS\s*
.*?
([^<>]+) \d+', html).group(1).strip()
re.search('(?s)
\s*BACKGROUND\s*
([^<>]+)', html).group(1).strip()

rawDescription: Beautifulsoup Parsing- detail info

description: Beautifulsoup Parsing- detail info


code:
import csv
from itertools import product
csvfile = csv.reader(open('filename', 'rb'), delimiter=',')
list(product(*zip(*list(csvfile))))

rawDescription: Combining all values of two different columns in a CSV


code:
import calendar

input = "BBC_Two_HD-19052015-2320-Newsnight.mts"

# Remove broadcaster name
input = '-'.join(input.split("-")[1:])

# Get show name
show = ''.join(' '.join(input.split("-")[2:]).split(".mts")[:-1])

# Get time string
timestr = ''.join(input.split("-")[0])

day = int(''.join(timestr[0:2])) # The day is the first two digits
month = calendar.month_name[int(timestr[2:4])] # The month is the second two digits
year = timestr[4:8] # The year is the third through sixth digits

# And the new string:
new = show + " " + str(day) + " " + month + " " + year + ".mts"

print(new) # "Newsnight 19 May 2015.mts"

rawDescription: Renaming files with no fixed char length in Python


code:
dfi = df.set_index(['a','b','c'])
dfi.sortlevel(inplace = True)
value = dfi["value"].values
value[dfi.index.get_loc((2, 1, 7))]

rawDescription: Approach to speed up pandas multilevel index selection?


code:
import _winreg as reg
from datetime import datetime

key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
secs = reg.QueryValueEx(key, 'InstallDate')[0] # This is stored as a UNIX timestamp
date = datetime.fromtimestamp(secs)

rawDescription: Check the installation date of Windows XP


code:
import sys

class Blackhole(object):

    def write(self, string):
        pass

stdout = sys.stdout
sys.stdout = Blackhole()

function_el_spammo()

sys.stdout = stdout

rawDescription: How can I suppress the output to console when importing packages in RPy2 in Python?

description: Suppress the output to console when importing packages in RPy2 in Python


code:
file1Size = float(os.stat(file1).st_size)
file2Size = float(os.stat(file2).st_size)

rawDescription: How can I find the ratio between two file sizes as a percentage in Python?

description: Find the ratio between two file sizes as a percentage in Python


code:
def doComputation(self):
    rpcResult = blockingCallFromThread(reactor, self.myRPCDoer.doRPC)
    return self.computeSomethingFrom(rpcResult)

rawDescription: Use my own main loop in twisted

description: Use my own main loop in twisted


code: newList = [[each_list[i] for i in list1] for each_list in list2]

rawDescription: Python- Extract specific columns from list


code:
import asyncio

class AsyncFetcherApp(FetcherApp):
    async def is_fetched_data_valid(self):  # async here
        data = await self.fetcher_implementation.fetch()  # await here
        return self._internal_validator(data)

class AsyncMyFetcher(AbstractFetcher):
    def __init__(self, client):
        super().__init__()
        self.client = client

    async def fetch(self):  # async here
        result = await self.client.fetch_data()  # await here
        return result

class AsyncClient:
    async def fetch_data(self):
        await asyncio.sleep(1)  # Just to sure it works
        return 1

async def main():
    async_client = AsyncClient()
    my_fetcher = AsyncMyFetcher(async_client)
    fetcherApp = AsyncFetcherApp(my_fetcher)
    # ...
    is_valid = await fetcherApp.is_fetched_data_valid()  # await here
    print(repr(is_valid))

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

rawDescription: Making async calls from an async library?


code:
@app.route('/')
def index():
    form = msForm()

    if request.method == 'POST' and form.validate():
        processed_text = model.most_similar(positive=[form.ms_3.data, form.ms_2.data], negative=[form.ms_1.data])
        return processed_text[0][0]

    return render_template('my-form.html', form=form)

rawDescription: Getting Data from wtforms

description: Getting Data from wtforms


rawDescription: Traversing through a list while comparing 2 elements


code:
def group(l, window, size):
    return [l[index:index + size] for index in xrange(0, len(l) - 1, window)]

rawDescription: How to construct a list of grouped item based on another list in python?

description: Construct a list of grouped item based on another list in python


code:
import nltk
sentence = """At eight o'clock on Thursday morning
Arthur didn't feel very good."""
tokens = nltk.word_tokenize(sentence)
tokens
tagged = nltk.pos_tag(tokens)
tagged[0:6]

rawDescription: How do I remove verbs, prepositions, conjunctions etc from my text?

description: Remove verbs, prepositions, conjunctions etc from my text


code:
def update_score(new_score, file_name="save.txt"):
    with open(file_name,'r+') as saved_file:
        existing_score = int(saved_file.read())
    if new_score > existing_score:
        # replace existing score
        with open(file_name,'w') as saved_file:
            saved_file.write(str(new_score))

rawDescription: In Python how can I replace a value in a text file with a new updated version of that value using a function?


code:
def my_view(request):
    model_name = "myapp.MyModel"   #make sure the app name is also provided
    from django.db.models import get_model
    model_class = get_model(*model_name.split('.'))

    class MyModelForm(ModelForm):
        class Meta:
            model = model_class
        # other attributes of the form

    form = MyModelForm()
    # ...

rawDescription: generic CRUD views on django

description: Generic CRUD views on django


rawDescription: Python: an iteration over a non-empty list with no if-clause comes up empty. Why?


code:
#!/usr/bin/env python

import wx
import datetime

class AFrame(wx.Frame):

    def __init__ (self):
        wx.Frame.__init__(self, parent=None, id=-1, title="Coundown to Christmas", size=(400, 400))

        # 2015.12.25 1:00:00
        self.future_time = datetime.datetime(2015, 12, 25, 1, 0, 0)

        # create timer
        self.timer = wx.Timer(self)
        # assign draw_image to timer
        self.Bind(wx.EVT_TIMER, self.draw_image, self.timer)
        # start timer
        self.timer.Start(1000)

        self.statbmp = wx.StaticBitmap(self)
        self.draw_image()
        self.Refresh()

        self.Show()

    def draw_image(self, event=None): # event required by timer

        # get deltatime
        secondsLeft = self.future_time - datetime.datetime.now()

        # get seconds, round to integer,
        secondsLeft = int(secondsLeft.total_seconds())

        if secondsLeft <= 0:
            secondsLeft = 0
            if self.timer.IsRunning():
                self.timer.Stop()

        # convert to text
        secondsLeft = str(secondsLeft)

        # select the width and height of the blank bitmap
        # must fit frame
        w, h = 400, 400

        # create the blank bitmap as background
        draw_bmp = wx.EmptyBitmap(w, h)

        #create canvas.
        canvas = wx.MemoryDC(draw_bmp)
        #fill the canvas with white
        canvas.SetBrush(wx.Brush('white'))
        canvas.Clear()

        #get text dimentions.
        tw, th = canvas.GetTextExtent(secondsLeft)

        #draw the text.
        canvas.DrawText(secondsLeft, (w - tw) / 2, (h - th) / 2 )

        self.statbmp.SetBitmap(draw_bmp)

app = wx.App()
AFrame()
app.MainLoop()

rawDescription: wxpython frame doesn't re-draw it's contents in while loop

description: Wxpython frame doesn't re-draw it's contents in while loop


code:
from uuid import uuid4
with open("machine-id", "w") as f:
    f.write(uuid4().hex + "\n")

rawDescription: Correct way to create /etc/machine-id from python


code: _l = re.sub(r'(?:\=|\=\>|\()\s*(true|false|null)\s*(?:\)|\;|\,)', lambda pattern: pattern.group(0).upper(), _l)

rawDescription: Python regex replace, with lambda function, overwriting non-capturing groups


code:
class MetaSum(type):

    def __repr__(cls):

            sum_str = str(cls.sum)

            cls.sum = 0

            return sum_str

    def __call__(cls, *args):

            for arg in args:

                    cls.sum += arg

            return cls

class sum_numbers(object, metaclass = MetaSum):
    sum = 0

print (sum_numbers(2)(3)(4)) # this only works in python 3

rawDescription: how to implement a function like sum(2)(3)(4)......(n) in python?

description: Implement a function like sum(2)(3)(4)......(n) in python


code:
import matplotlib.pyplot as plt
ax = plt.gca()

canvas = ax.figure.canvas

rawDescription: Is there a method to get the get parent canvas for axes in matplotlib?


code:
with open('huge_file.dat', 'rb') as f:
    f.seek(1024 * 1024 * 1024)  # skip 1GB
    ...

rawDescription: How can a Python program load and read specific lines from a file?


code:
person = Person.get_or_insert('Peter')
# other interesting & useful code here...
# later, maybe we need to retrieve that key name from the object:
name = person.key().name()
assert(name == "Peter")

rawDescription: Google App Engine - Datastore get_or_insert key_name confusion


code:
pairs = [
    (u'Type', u'RES'),
    (u'CustomerId', u'1110566212417')
]
collection = dict(pairs)

In [1]: collection
Out[1]: {u'CustomerId': u'1110566212417', u'Type': u'RES'}

rawDescription: I have two values in a Tuple like ('Type', 'RES') can i assign like Type=RES and store it in dict?


code:
import xarray as xr
import numpy as np
import holoviews as hv;
hv.notebook_extension()

#Building 2D Array (X & Y)
dist = np.linspace(-0.5,0.5,202)   # Linear spatial sampling
XY,YX = np.meshgrid(dist, dist)

#Add along 3rd Dimension
Z_list = []
for i in range(10):
    Z_list.append(xr.DataArray(XY*i,dims=["x","y"]))

#Concat list of 2D Arrays into a 3D Array
DA_3D = xr.concat(Z_list,dim="z")
# DA_3D.shape
# (10, 202, 202)

def image_slice(k):
    return(hv.Image(DA_3D[k,:,:].values))

keys = list(range(10))

# Construct a HoloMap by evaluating the function over all the keys
hmap = hv.HoloMap([(k, image_slice(k)) for k in keys], kdims=['z_axis'])

# Construct a HoloMap by defining the sampling on the Dimension
dmap = hv.DynamicMap(image_slice, kdims=[hv.Dimension('z_axis', values=keys)])

rawDescription: How to use HoloMap in Holoviews to view 2D Array slices (Python 3.5.1)

description: Use HoloMap in Holoviews to view 2D Array slices (Python 3.5.1)


code:
YourModel.objects.filter(
    fieldname__gte=datetime.datetime.now()-datetime.timedelta(seconds=2)
)

rawDescription: Getting lastest Records in Django

description: Getting lastest Records in Django


code:
from wikipedia import Wikipedia
from wiki2plain import Wiki2Plain

lang = 'simple'
wiki = Wikipedia(lang)

try:
    raw = wiki.article('Uruguay')
except:
    raw = None

if raw:
    wiki2plain = Wiki2Plain(raw)
    content = wiki2plain.text

rawDescription: Extract the first paragraph from a Wikipedia article (Python)


code:
import sys
import getpass

s = getpass.getpass()

try:
    u = s.decode(sys.stdin.encoding).encode('UTF-8')
except TypeError:
    u = s.encode('UTF-8')

rawDescription: Using getpass with python 2 and 3 compatibility


code:
import re

keys = ['first_condition', 'extra_condition', 'testing', 'other']
d = dict(re.findall(r'^(.*)\((.*)\)$', text, re.M))
result = [d.get(key, '') for key in keys]

rawDescription: Python: regex: find if exists, else ignore


code:
import struct
def processKey(data):
    num = int("".join([x for x in data if x.isdigit()]))
    spaces = data.count(' ')
    return num / spaces
key1 = '4 @1 46546xW%0l 1 5'
key2 = '12998 5 Y3 1 .P00 '
sk1 = processKey(key1)
sk2 = processKey(key2)
sk1
sk2
sk3 = "^n:ds[4U"
fullappend = struct.pack('>ii%ds' % len(sk3),sk1,sk2,sk3)
fullappend
len(fullappend)
from hashlib import md5
md5(fullappend).hexdigest()
md5(fullappend).digest()

rawDescription: How to add the binary of a int with the binary of a string

description: Add the binary of a int with the binary of a string


code:
def convert_pdf_to_png(blob):
    pdf = Image(blob=blob)

    pages = len(pdf.sequence)

    image = Image(
        width=pdf.width,
        height=pdf.height * pages
    )

    for i in xrange(pages):
        image.composite(
            pdf.sequence[i],
            top=pdf.height * i,
            left=0
        )

    return image.make_blob('png')

rawDescription: imagemagick wand save pdf pages as images

description: Imagemagick wand save pdf pages as images


code:
@app.route('/<path:path>')
def catch_all(path):
    if path.endswith('/'):
        return handle_folder(path)
    else:
        return handle_file(path)

rawDescription: Flask: Handle catch all url different if path is directory or file


code:
start_year = driver.find_element_by_id("startyear")
start_year.clear()
start_year.send_keys("2004")

end_year = driver.find_element_by_id("endyear")
end_year.clear()
end_year.send_keys("2015")

rawDescription: scrapy: how to type argument in a blank space

description: Scrapy: how to type argument in a blank space


code:
x = [2, 0, 1, 1, 2, 4]
y = [[j for j in range(len(x)) if x[j]==i] for i in range(max(x)+1)]

rawDescription: Creating a 2D list of indices from a 1D list


code:
import sys

_hook_registered = False

def register_handler(force = False):
    global _hook_registered

    if _hook_registered and not force:
        return

    orig_excepthook = sys.excepthook

    def error_catcher(*exc_info):
        import logging
        log = logging.getLogger(__name__)
        log.critical("Unhandled exception", exc_info=exc_info)
        orig_excepthook(*exc_info)

    sys.excepthook = error_catcher

    _hook_registered = True

rawDescription: Adding function to sys.excepthook

description: Adding function to sys.excepthook


code:
def idsToElements(ids):
    return map(doc.GetElement, ids)

rawDescription: more pythonic version of list iteration function

description: More pythonic version of list iteration function


code:
find('red blue', 'd', 0) # starts at index 0 and finds 'd' at index 2
find('red blue', 'd', 3) # starts at index 3 and finds no d's
find('red blue', 'e', 3) # starts at index 3, so misses the 'e' at index 1

rawDescription: I need assistance in Python - Searching within a function


code:
list1 = ['GM2', 'GM1' ,'GM3']
list2 = ['A', 'B', 'C']
result = sorted(zip(list1, list2))
result

rawDescription: Python Parallel Sorting

description: Python Parallel Sorting


code:
from visual import *

scene.title = "Faces example"
scene.width = 600
scene.height = 400

f = frame()
tri = faces(
    pos = [
        [0.,0.,0.], [1.,0.,0.], [0.,1.,0.],   # first tri - vertices
        [0.,0.,0.], [-1.,0.,0.], [0.,-1.,0.]  # second tri - vertices
    ],
    color = [
        [1.,0.,0.], [0.5,0.5,0.], [0.,1.,0.], # first tri - colors
        [0.,0.,1.], [0.,0.5,0.5], [0.,1.,0.]  # second tri - colors
    ],
    frame = f
)

tri.make_normals()
tri.make_twosided()

while True:
    rate(100)
    f.rotate(angle=0.01)

rawDescription: Create a single face in VPython?

description: Create a single face in VPython


code:
class parent(wx.Frame):
      def __init__(self, parent):
            ## Ur GUI's code ##

            self.Bind( wx.EVT_CLOSE, self.ParentFrameOnClose )
      def ParentFrameOnClose(self, event):
            self.DestroyChildren()  ## Destroy the children first
            self.Destroy()    ## Destroy the parent then.

rawDescription: how to check if a frame was exited by the user clicking the close button?

description: Check if a frame was exited by the user clicking the close button


rawDescription: Make a sprite get gradually get smaller pygame


code:
import PrettyTable

firstRow = input ('What would you like to put in your first row sir?')
secondRow = input ('What would you like to put in your second row sir?')
thirdRow = input ('What would you like to put in your third row sir?')
fourthRow = input ('What would you like to put in your fourth row sir?')

x = PrettyTable(["Moive Title", "Ratings"])
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row([firstRow])
x.add_row([secondRow])
x.add_row([thirdRow])
x.add_row([fourthRow])
print (x)

rawDescription: how to raw input to dictionary

description: Raw input to dictionary


code:
from flask import request
request.headers.get('your-header-name')

rawDescription: How to get http headers in flask?

description: Get http headers in flask


code:
class LimitGenericSitemap(GenericSitemap):
    limit = 2000

rawDescription: Caching sitemaps in django

description: Caching sitemaps in django


code:
class LogOnline(ndb.Model):
    timeOnline = ndb.DateTimeProperty(default=None)

    def _post_put_hook(self, future):
        future.get_result() #wait untill the  PUT operation has completed
        #set memcache with all current online users
        ...

rawDescription: Using @property with the ndb datastore in google app engine


rawDescription: How to search document by oid in mongoengine

description: Search document by oid in mongoengine


code:
# there are no builtin avg function
def avg(lst):
    return sum(lst)/len(lst)

rows = list(avg(row) for row in a)

rawDescription: unknown vector size python

description: Unknown vector size python


code:
if fnmatch.fnmatch(file, '*[!p][!y]'):
        print(file)

rawDescription: Negating a fnmatch pattern

description: Negating a fnmatch pattern


code:
import importlib
import sys

sys.path.insert(0, 'C:/hidden/assistant/AnsData/')
# "variable" is a string naming your module without the .py extension
answermodule = importlib.import_module(variable)

rawDescription: Using execfile to go to a directroy then run a variable


code:
class ExampleTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        time.sleep(1)  # sleep time in seconds

rawDescription: Python unit test. How to add some sleeping time between test cases?


code:
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

rawDescription: receiving data over a python socket

description: Receiving data over a python socket


code:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()

rawDescription: subprocess.call and tar -zxvf

description: Subprocess.call and tar -zxvf


code:
for line in open(filename):
    row = line[:-1].split(",")
    arg = {'date': date,
           'store_id': row[0].strip(),
           'price': row[1].strip(),
           'description': row[2].strip()}
    if not arg['store_id'] and not arg['description']:
        continue
    # cur.execute bit here...

rawDescription: How to skip empty lines in csv file when reading with python script?

description: Skip empty lines in csv file when reading with python script


code:
from itertools import permutations

l = ["A","B","C"]

list(permutations(l))

rawDescription: Python all permutations of a list

description: Python all permutations of a list


code:
def current_frame():
    i = None
    def gen():
        yield i.gi_frame.f_back
    i = gen()
    return next(i).f_back

class Loop(object):
    jump = False
    def __call__(self, frame, event, arg):
        if self.jump:
            frame.f_lineno = self.lineno
            self.jump = False
        return None if event == 'call' else self
    def __enter__(self):
        import sys
        sys.settrace(self)
        current_frame().f_back.f_trace = self
        self.lineno = current_frame().f_back.f_lineno
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type is None:
            self.jump = True
        else:
            import sys
            sys.settrace(None)
            current_frame().f_back.f_trace = None
            return exc_type is StopIteration

def flatten(x):
    if isinstance(x, list):
        if x:
            first, rest = flatten(x[0]), flatten(x[1:])
            with Loop():
                yield next(first)
            with Loop():
                yield next(rest)
            pass
    else:
        yield x

rawDescription: Puzzle: Recursive generator without loops/imports


code:
test
import ast
res = ast.literal_eval("b%s" % test)
import bz2
len(bz2.decompress(res))

rawDescription: Unicode strings to byte strings without the addition of backslashes


code:
import random
thenum = random.randint(1000, 9999)
print (thenum)
tally = 1
guessone = int(input("What is your first guess?: "))
strguessone = str(guessone)
strthenum = str(thenum)
if strguessone [0] == strthenum [0]:
    tally += 1
if  strguessone [1] ==  strthenum [1]:
    tally += 1
if strguessone [2] == strthenum [2]:
    tally += 1
if  strguessone [3] ==  strthenum [3]:
    tally += 1

print (tally)
if tally == 5:
    print ("You Win")

rawDescription: wont add tally master mind game python

description: Wont add tally master mind game python


code:
positions = random.sample(range(100), amount_you_want)
for coord in positions:
    matrix[coord%10][coord//10] = 'O'

rawDescription: Set on random postion

description: Set on random postion


rawDescription: Replace "zero-columns" with values from a numpy array


code:
while True:  # Yes, continue listening forever...
   resp_key = event.waitKeys(keyList=['left','right','up','down','return'])[0]  # pick first and only response.

   # Set different texts dependent on received response
   if resp_key == 'left':
      yourTextStimulus.text = '5'
   elif resp_key == 'right':
      yourTextStimulus.text = '7'
   elif resp_key == 'up':
      yourTextStimulus.text = '2'

   # .. or break out of the loop if return was pressed
   elif resp_key == 'return':
      break  # oh, do NOT continue forever... we're finished on return :-)
   # Actually show text if loop was not ended
   yourTextStimulus.draw()
   win.flip()

rawDescription: PsychoPy Builder: how to update screen each keypress?


rawDescription: Python regex or split/strip to get int from between brackets in line of text


code:
my_string='ONETWOTHREEFOURFIVESIXSEVENEIGHTNINETEN'
lengths = (3, 3, 5, 4, 4, 3)

words = []
start = 0
for i in lengths:
    words.append(my_string[start:start+i])
    start += i

print(words)  # ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX']

rawDescription: Cutting list python base on condition

description: Cutting list python base on condition


code:
import numpy as np
a = np.random.rand(1024, 1024)  # create array of random values
b = array.tobytes()
np.fromstring(b).reshape(1024,1024)

rawDescription: How can I unserialize a numpy array that was cast to a bytestring?

description: Unserialize a numpy array that was cast to a bytestring


code:
from itertools import groupby

lst = [[0, 'Ealing Broadway', 103.89],
    [0, 'Notting Hill Gate', 103.89],
    [0, 'Mile End', 103.89],
    [1, 'Ealing Broadway', 59.089999999999996],
    [2, 'Notting Hill Gate', 40.279999999999994],
    [3, 'Mile End', 68.86999999999999]]

new_list = []
for key, group in groupby(lst, lambda x: x[0]):
    new_list.append(list(group))

main_dict = {}
for item in new_list:
    main_dict[item[0][0]] = {'length': item[0][2], 'interchange': [stn[1] for stn in item]}

rawDescription: change a list into dictionary

description: Change a list into dictionary


code: np.allclose( my_matrix, np.zeros((3,3)) )

rawDescription: python, test whether all values in matrix less than numpy.finfo(float).eps

description: Python, test whether all values in matrix less than numpy.finfo(float).eps


code:
with open('file') as f:
    lines = f.read().splitlines()
[list(i) for i in lines]

rawDescription: Reading a text file into a 2d list in Python, without deleting spaces


code:
import cherrypy
import os
import json

CURDIR = os.getcwd()

cherrypy.config.update({
    "tools.staticdir.dir" : CURDIR,
    "tools.staticdir.on" : True
    })

# Loopuoobjekt für die Templates

# Liefert das Gerenderte Template zurück
class Root(object):
    @cherrypy.expose
    def index(self):
        return """

Hallo Haus!

    <button id=1>Licht 1</button>

    <button id=2>Licht 2</button>

    <button id=3>Licht 3</button>

    <button id=4>Licht 4</button>

"""

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def switch(self, id):
        print("Licht nr {} wurde geschaltet".format(id))
        return json.dumps({"text" : "schalter {} ".format(id)})

cherrypy.quickstart(Root())

rawDescription: Getting Data back from Python with Cherrypy and jQuery/AJAX


code:
def amount_of_pizzas():
    # global numPizza - you don't need this since you are returning it
    num_pizza = input("Enter the number of pizzas wanted (max 5)")
    try:
        num_pizza = int(num_pizza)
    except ValueError:
        print('Please enter a number, not a string')
        amount_of_pizzas() # Go through the loop again
    if num_pizza > 5:
        print('You can have no more than 5 pizzas')
        amount_of_pizzas()
    # Only return if all checks have passed
    return num_pizza

rawDescription: Problems with ValueError

description: Problems with ValueError


code:
while True:
    userInput = raw_input('Enter text :')
    if re.match("REGEX", userInput):
         break

rawDescription: Loop back to beginning of code if input doesn't match REGEX | Python 2.7


rawDescription: Selecting specific excel rows for analysis in pandas/ipython?