code:
not_on_hd = []
for i,file in enumerate(flickr_photos):
    if os.path.exists("/path/to/"+file):
        continue
    else:
        not_on_hd.append(file)
        flickr_photos.pop(i)
print(not_on_hd)

rawDescription: Python compare differences in XML.Etree and file and folder names in directory listings


rawDescription: How to replace the the diagonal elements of a matrix by a vector in SymPy?

description: Replace the the diagonal elements of a matrix by a vector in SymPy


code:
from django.views.generic import TemplateView

class QuizScoreView(TemplateView):
    template_name = 'quiz_score.html'

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        context['Score'] = request.GET['Score']
        return self.render_to_response(context)

rawDescription: Accessing GET Form data from - Javascript Form in Django


code:
def create_from_state_dict(cls, state_dict):
    mgr = manager_of_class(cls)
    instance = mgr.new_instance()
    for key, value in state_dict.iteritems():
        setattr(instance, key, value)
    return instance

rawDescription: Recreate some sqlalchemy object

description: Recreate some sqlalchemy object


code:
for i, row in enumerate(raw):
   if rowlen == 13:  # This is true only for csv files I want to shorten
       raw[i] = row[0:8]
       rowlen = 8

rawDescription: Remove unwanted columns from CSV file

description: Remove unwanted columns from CSV file


rawDescription: How to change elements in sparse matrix in Python's SciPy?

description: Change elements in sparse matrix in Python's SciPy


code: list(enumerate("patata"))

rawDescription: Lists, conditionals and loops don't give the result expected


code:
from flask import Flask
from flask import render_template, abort, jsonify, request,redirect, json
from my_app.machine_learning import analyzer
app = Flask(__name__)
app.debug = True

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/learning', methods=['POST'])
def learning():
    data = json.loads(request.data)
    # data == {"userInput": "whatever text you entered"}
    response = analyzer(data)
    return jsonify(response)

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

rawDescription: Python-Flask: Pass data to machine learning python script and get results back


code: input_str = "\n".join(input_txt) + "\n"

rawDescription: Dynamic string operations in Python

description: Dynamic string operations in Python


code: tup = tuple((element.foo, element.bar) for element in alist)

rawDescription: Use list comprehension to build a tuple

description: Use list comprehension to build a tuple


code: sorted(d.items(), key=lambda v: (v[1], len(v[0])), reverse=True)

rawDescription: Sort a dictionary 'd' by d.values(), then by length of the corresponding keys


code:
ps = subprocess.Popen(cmd,stderr=sb.PIPE)
ps.communicate()

rawDescription: running ffmpeg from Popen inside (twisted) timer.LoopingCall() stalls

description: Running ffmpeg from Popen inside (twisted) timer.LoopingCall() stalls


code:
menu_choice = ""

while menu_choice not in ["1", "2"]: # not ideal with lots of choices
    print("This menu only accepts numerical input. Please try again.\n")
    menu_Choice = input("\n")

rawDescription: Is there a way to make a "Not a number" if statement in Python?


code: out = dict([(f1(*M), tuple(map(f2, N))) for M,N in zip(keys, values)])

rawDescription: Make a dictionary from two lists after applying a function


code:
x = Test()
y = Test()
x == y
x.value = 7
x == y
y.value = 7
x == y

rawDescription: How to do a deepequals on a object in python

description: Do a deepequals on a object in python


code:
import sqlite3
db = sqlite3.connect(":memory:")
cursor = db.cursor()
cursor.execute("create table test(i)")
cursor.execute("insert into test(i) values (1)")
cursor.execute("insert into test(i) values (2)")
cursor.execute("insert into test(i) values (3)")
cursor.execute("delete from test where i >= 2")
cursor.rowcount

rawDescription: Python: retrieve number of rows affected with SQL DELETE query


code:
from multiprocessing.reduction import ForkingPickler
import StringIO

def forking_dumps(obj):
    buf = StringIO.StringIO()
    ForkingPickler(buf).dump(obj)
    return buf.getvalue()

rawDescription: Python - Passing a TCP socket object to a multiprocessing Queue


code:
match.size  # 13
match.a     # 3
match.b     # 4

rawDescription: Use Regular Expressions (or another python module) to compare text/characters?


code:
def unique(sequence):
    seen = set()
    for e in sequence:
        if e not in seen:
            seen.add(e)
            yield e

rawDescription: Python list giving different order

description: Python list giving different order


code: np.fromstring(VIstring, sep=',')

rawDescription: Convert a numpy array to a CSV string and a CSV string back to a numpy array


rawDescription: Numpy.unique - getting output with consistent depth


code:
#where master_copy is the first multidimensional list, data_copy is the second
#the search columns are the columns we want to search against
for line in master_copy:
    for m in master_search_columns:
        found = False
        for d in data_search_columns:
            data_copy = sorted(data_copy, key=lambda x: x[d], reverse=False)
            results = binary_search(line, data_copy,m, d)
            found = results["result"]
            if found:
                line = update_row(line, data_copy[results["index"]], column_mapping)
                found_count = found_count +1
                break
        if found:
            break

rawDescription: Python Linear Search Better Efficiency

description: Python Linear Search Better Efficiency


code:
if not isinstance(matcher, re._pattern_type):
    matcher = re.compile('%s$' % re.escape(matcher))

rawDescription: Determine whether Python object is regex or string


code: from PyQt4.QtWebkit import *

rawDescription: PyQt4 new modul

description: PyQt4 new modul


code: run(arg1, arg2, kwarg1="three times!")

rawDescription: Globally-scoped variable: can its value change before it is picked up by the thread?


rawDescription: Solving system using linalg with constraints


rawDescription: Nested for loops in Python compared to map function


code: lis = [y for x in shakespeare for y in x]

rawDescription: Python :: NLTK concatenating list of sentences


code:
df.groupby('id').median().count(1) == 1
indexers = df.groupby('id').median().count(1) == 1

rawDescription: Pandas: rare data form disrupts normal statistical analysis


code:
import re
regex = re.compile(r'\([^\(\)]+\)')

def eval_expr(expr):
    match = regex.search(expr)
    if match:
        sub_expr = match.group()
        return eval_expr(expr.replace(sub_expr, str(eval_expr(sub_expr[1:-1]))))
    args = expr.split()
    op = args.pop()
    return eval(op.join(args))

rawDescription: python split and evaluate

description: Python split and evaluate


code:
# Python 2.x
for key, value in e.iteritems():
    if value == '':
        e[key] = d.get(key)

# Python 3.x
for key, value in e.items():
    if value == '':
        e[key] = d.get(key)

rawDescription: Python: compare keys of 2 dictionaries and update the value of one


code:
import __builtin__

def open(name, mode='', buffer=0):
    return __builtin__.open(name, mode, buffer) # place a BreakPoint here

rawDescription: Breaking the python code when particular file is being opened


code:
def __init__(arg1, arg2=None):
    if arg2 is None:
        construct_one_way()
    else:
        do_something_different()

rawDescription: How to have multiple ways to construct an object of same class in python?

description: Have multiple ways to construct an object of same class in python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

First header

First div to go with a header

Second header

Second div to go with a header

''')
for header in soup.select('div h3'):
    next_div = header.find_next_sibling('div')
    print(header.text, next_div.text)

rawDescription: how to grab alternating child tags in python beautifulsoup

description: Grab alternating child tags in python beautifulsoup


rawDescription: Add null character to string in python

description: Add null character to string in python


code:
import random

def binomial_random_sample(n, p):
    ret = 0
    for j in range(n):
        if random.random() < p:
            ret += 1
    return ret

rawDescription: Rewriting numpy.random.binomial using a For loop


code:
# hard limiting
def limiter(x, treshold=0.8):
    transfer_len = 1000
    transfer = np.concatenate([ np.repeat(-1, int(((1-treshold)/2)*transfer_len)),
                                np.linspace(-1, 1, int(treshold*transfer_len)),
                                np.repeat(1, int(((1-treshold)/2)*transfer_len)) ])
    return apply_transfer(x, transfer)

# smooth compression: if factor is small, its near linear, the bigger it is the
# stronger the compression
def arctan_compressor(x, factor=2):
    constant = np.linspace(-1, 1, 1000)
    transfer = np.arctan(factor * constant)
    transfer /= np.abs(transfer).max()
    return apply_transfer(x, transfer)

rawDescription: How to amplify sounds without distortion in Python

description: Amplify sounds without distortion in Python


code:
VertexNb = cmds.polyEvaluate(Mesh, v=1)
weight = cmds.getAttr('{0}.inputTarget[0].baseWeights[0:{1}]'.format(blendShapeNode, VertexNb))

rawDescription: How to change the weights of a deformer from a script in Maya?

description: Change the weights of a deformer from a script in Maya


code:
[sublist for i, sublist in enumerate(yourlist)
    if i + 1 == len(yourlist) or (sublist[0], sublist[4]) != (yourlist[i+1][0], yourlist[i + 1][4])]

rawDescription: How to remove an entry in a list based on a value in the sublist

description: Remove an entry in a list based on a value in the sublist


rawDescription: Trying to put matrices into a set using python, but it still allows duplicates


rawDescription: Press "Z" key to continue

description: Press "Z" key to continue


code:
class PairObject(object):

    def __init__(self, _part1, _part2):
        self.part1 = _part1
        self.part2 = _part2

    def pair(self):
        return (self.part1, self.part2)

    def mean(self):
        return np.mean((self.part1, self.part2))

# Then use it like this:
p1 = PairObject([2, 3, 4], [5, 6, 7])
p1.pair() # returns ([2, 3, 4], [5, 6, 7])
p1.mean() # returns 4.5

rawDescription: Automatization issue

description: Automatization issue


code: G.add_node('abc', dob=1185, pob='usa', dayob='monday')

rawDescription: Storing and Accessing node attributes python networkx


code:
from models import db
from flask import Flask
import unittest

class TestExample(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        db.init_app(self.app)
        with self.app.app_context():
            db.create_all()

rawDescription: Flask-SQLAlchemy not creating tables using create_all()


code:
df = df.set_index('date')
pd.rolling_apply(df, window=5, func=lambda x: x.argmin(), min_periods=1)

rawDescription: Most recent max/min value

description: Most recent max/min value


code:
def parser(lines):
    lines_iter = iter(lines)
    block = []
    for line in lines_iter:

        if line.startswith('if'):
            bl = ' '.join(line.split()[1:]).replace('(', '').replace(')', '')
            block.append({'name':bl, 'children':parser(lines_iter)})

        elif line.startswith('else'):
            be = ' '.join(line.split()[1:]).replace('(', '').replace(')', '')
            block.append({'name':be, 'children':parser(lines_iter)})

        elif not line.startswith(('if','else')):
            block2 = line
            block.append({'name':block2})
        else:
            break
    return block

rawDescription: Converting an "If Else" statement to a JSON file


code: print(result)

rawDescription: Remove all data in a DF by group based on a condition (pandas,python3)


code:
array = []
with open("input.csv") as f:
    for line in f:
        array.append(map(float, line.split(",")))

rawDescription: Python: How to change the location of multiple values in a csv file into a single line values?


code:
def sumvars(x, y, z):
    return sumvars_with_intermediates(x, y, z).final

def sumvars_with_intermediates(x, y, z):
    ...
    return my_namedtuple(final, first_step, second_step)

rawDescription: Returning intermediate results from function in Python


code:
# use sorted list
for t in my_sorted_list:
    # t is a tuple
    k1 = t[0]
    value = t[1]

rawDescription: Compare CSV files using python

description: Compare CSV files using python


code:
urls.py
from .routers import router
urlpatterns = patterns(
    url(r'^api/v1/', include(router.urls)),
)

rawDescription: How to implement nested serializer in django rest framework?

description: Implement nested serializer in django rest framework


code:
products = list(product("ABC", repeat=i) for i in range(4))
result = list(x for lst in products for x in lst)
result

rawDescription: Python Cartesian power generator over a list of powers


code: liste = liste[DebutListe:FinListe]

rawDescription: Infinite Loop i don't know why ( DichotomySearch )


code:
def f(word):
    vowels = {"a", "e", "i", "o", "u"}
    consonants = {'t', 'b', 'm', 'h', 'y', 'w', 'z', 'p', 'v', 'd', 'g', 'k', 'j', 'n', 'r', 'q', 'x', 'c', 's','f', 'l'}
    pairs = []
    it = iter(word)
    # get first char
    prev = next(it,"")
    # iterate over the rest starting at the second char
    for ch in it:
        # compare prev and current
        if prev in consonants and ch in vowels:
            pairs.append(prev + ch)
        # update prev
        prev = ch
    return pairs

rawDescription: Splitting the letters of a word based on conditions, and returning in a list (Python)


rawDescription: Applying complex function to several timeseries


code:
class ModelBase(models.Model):
    def __str__(self):
        return self._infoMethod()
    def __unicode__(self):
        return self._infoMethod()

rawDescription: is it possible to dynamically define a method's name?

description: Is it possible to dynamically define a method's name


code:
HELP_TEXT = """Explanation text.....
         ..................
         """

class Question(...):
    ...
    help_text = HELP_TEXT

rawDescription: Django help_text line length convention

description: Django help_text line length convention


code: df.where(df.apply(lambda x: x.index < pd.Timestamp(x.name[2])))

rawDescription: Python Pandas - replace values with NAN in multiple columns based on mutliple dates?


rawDescription: Convenient way to slice from multiindexed rows based on range of keys at different levels with pandas dataframe


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
from scrapy.item import Item, Field

class MyItem(Item):
    url=Field()

class someSpider(CrawlSpider):
  name = "My script"
  domain=raw_input("Enter the domain:\n")
  allowed_domains = [domain]
  starting_url=raw_input("Enter the starting url with protocol:\n")
  start_urls = [starting_url]
  items=[] #list with your URLs
  f=open("items.txt","w")

  rules = (Rule(LxmlLinkExtractor(allow_domains=(domain)), callback='parse_obj', follow=True),)

  def parse_obj(self,response):
    for link in LxmlLinkExtractor(allow_domains=(self.domain)).extract_links(response):
        if link not in self.items: #check if it's already parsed
            self.items.append(link)   #add to list if it's not parsed yet
            #do your job on adding it to a file
            item = MyItem()
            item['url'] = link.url
            self.f.write(item['url']+"\n")

rawDescription: How to stop my crawler from logging duplicates?

description: Stop my crawler from logging duplicates


code:
def flatten(kvs):
    (key, (left, right)) = kvs
    for v in left:
        yield (key, (v, right))

aggregated = (rdd1
    .aggregateByKey(set(), set_update, lambda u1, u2: u1 | u2))

rdd2_partitioned = rdd2.partitionBy(aggregated.getNumPartitions())

(aggregated.join(rdd2_partitioned)
    .flatMap(flatten))

rawDescription: Does spark's distinct() function shuffle only the distinct tuples from each partition


code:
import re
Variable = "The sun shines, that's fine [not, for, everyone] and if it rains, it Will Be better."
Variable1 = re.sub("\[[^]]*\]", lambda x:x.group(0).replace(',',''), Variable)

rawDescription: Replace a string located between

description: Replace a string located between


rawDescription: Identify groups of continuous numbers in a list


code: dis.dis(compile('i in s', '<>', 'exec'))

rawDescription: python 2.7 writing "x in set" vs "set.__contains__(x)"

description: Python 2.7 writing "x in set" vs "set.__contains__(x)"


code:
with open(r'H:\test\test.dat','rb') as f:
    data = f.read()
with open(r'H:\test\test.dat','wb') as f:
    f.write(header + data)

rawDescription: Adding binary header

description: Adding binary header


code: (not d['logins'], d['logins'], d['date_used'] if d['logins'] else d['date_added'])

rawDescription: Sort a dictionary with custom sorting function


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

rawDescription: How to pair every item in a 2 lists as fractions until the end of said lists?

description: Pair every item in a 2 lists as fractions until the end of said lists


code: averages = [[elem.next() for elem in row] for row in array]

rawDescription: Using python to extract a few lines from a data file


code: 'branch \n  subpattern 1 \n    literal 116 \n    literal 101 \n    literal 115 \n    literal 116 \n  max_repeat 1 65535 \n    in \n      category category_space\n  max_repeat 0 65535 \n    in \n      category category_word\n  groupref 1 \nor\n  literal 97 \n  literal 98 \n  literal 99 \n  literal 100 \n  literal 101 \n'

rawDescription: REGEX - String version of the parsing of a pattern done by `sre_parse`


code:
A  # Input array
B  # Block size
out # Output array

rawDescription: Implement MATLAB's im2col 'sliding' in Python


code:
@event.listens_for(Session, "before_flush")
def before_flush(session, flush_context, instances):
    for obj in session.new | session.dirty:
        if isinstance(obj, Thread):
            obj.word_count = sum(c.word_count for c in obj.comments)
        elif isinstance(obj, Comment):
            obj.thread.word_count = sum(c.word_count for c in obj.comments)

rawDescription: Can SQLAlchemy events be used to update a denormalized data cache?


rawDescription: Is there a version of list.sort() that returns the sorted list?


code:
import logging

_logger = logging.getLogger(__name__)
_logger.info("context type: " + type(context))
_logger.info("context content: " + str(context))

rawDescription: How to fetch values from a one2many field in odoo

description: Fetch values from a one2many field in odoo


rawDescription: Numpy subset array between first and last occurence of logical expression match


code:
char_val = ord(each_character)
block_decode(char_val)

rawDescription: Python RSA Decrypt for class using vs2013


rawDescription: Best approach for getting the first differential in all directions and all elements


code:
quotedString.setParseAction(removeQuotes)
list_of_names = delimitedList(quotedString)
boolean_value = oneOf("True False").setParseAction(lambda t: t[0]=='True')

def named_parameter(label, paramtype):
    expr = Literal(label) + Suppress('=') + paramtype(label)
    return expr

person_start = Literal("person(").suppress()
first = named_parameter("firstname", quotedString)
last = named_parameter("lastname", quotedString)
dob = named_parameter("dob", quotedString)
hobbies = named_parameter("hobbies", Suppress("[") + list_of_names + Suppress("]"))
is_minor = named_parameter("is_minor", boolean_value)
person_end = Suppress(")")
comma = Literal(",").suppress()

parameter = first | last | dob | hobbies | is_minor
my_data = person_start + delimitedList(parameter) + person_end

rawDescription: PyParsing a string representing a function


code:
scriptPath = sys.path[0]
toolSharePath = os.path.abspath(scriptPath)
# or, in one line: toolSharePath = os.path.abspath(sys.path[0])
toolDataPath = os.path.join(toolSharePath, "ToolData")

rawDescription: Resolving mixed slashes from sys.path and os.path.join


code:
from itertools import combinations
len(list(a for a in combinations(range(1, 101), 3)))

rawDescription: Finding Optimal Change for Lowest Coin Total


code:
seqDic = dict.fromkeys(seq, 0)
for i in seq: seqDic[i] += 1
dv = list(seqDic)

rawDescription: Learning Python and using dictionaries

description: Learning Python and using dictionaries


code: reduce(lambda x, y: x*y, a1)

rawDescription: Python: divide elements of a list based on a condition


rawDescription: How to convert integer into date object python?

description: Convert integer into date object python


rawDescription: Zero out matrix higher diagonal using numpy


code:
strg = "garbagegarbageFLGSTdata1FLGENFLGSTdata2FLGENgarbagegarbageFLGSTdata3FLGEN"
pattern = re.compile(r'(?<=FLGST)(\S*?)(?=FLGEN)')
re.findall(pattern, strg)

rawDescription: Find Multiple Flagged String within String Python


code:
if os.environ.has_key('OPENSHIFT_REPO_DIR'):
    SSL_ON = True
    SSL_ALWAYS = True

rawDescription: .htaccess seemingly ignored on OpenShift


code: frame = MainWindow(None, "test")

rawDescription: TypeError: __init__() takes at least 3 arguments (1 given)


rawDescription: Pygame couldn't open image

description: Pygame couldn't open image


code:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
import re
from todo.items import wendyItem

class wendySpider(BaseSpider):
    name = "wendyspider"
    allowed_domains = ["wendys.com.sg"]
    start_urls = ["http://www.wendys.com.sg/outlets.php"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        cells = hxs.select('//div[@id="menu_list"]//td[@valign="top"][.//span[@class="foodTitle"]]')
        items = []
        for cell in cells:
            item = wendyItem()

            # get all text nodes
            # some lines are blank so .strip() them
            lines = cell.select('.//text()').extract()
            lines = [l.strip() for l in lines if l.strip()]

            # first non-blank line is the place name
            item['name'] = lines.pop(0)

            # for the other lines, check for "Opening hours" and "Telephone"
            # to store lines in correct list container

            address_lines = []
            hours_lines = []
            telephone_lines = []

            opening_hours = False
            telephone = False

            for line in lines:
                if 'Opening Hours' in line:
                    opening_hours = True
                elif 'Telephone' in line:
                    telephone = True
                if telephone:
                    telephone_lines.append(line)
                elif opening_hours:
                    hours_lines.append(line)
                else:
                    address_lines.append(line)

            # last address line is the postal code + town name
            item['address'] = "\n".join(address_lines[:-1])
            item['postal'] = address_lines[-1]

            # ommit "Opening hours" (first element in list)
            item['hours'] = "\n".join(hours_lines[1:])

            item['contact'] = "\n".join(telephone_lines)

            items.append(item)

        return items

rawDescription: scrapy, how to separate text within a HTML tag element

description: Scrapy, how to separate text within a HTML tag element


code:
from numbers import Number

def multiply(o, mul):
    if isinstance(o, dict):
        return {k: multiply(v, mul) for k, v in o.items()}
    elif isinstance(o, Number):
        return o * mul
    else:
        return o

rawDescription: Applying dictionary comprehension to a defaultdict


code:
import boto.emr
from boto.emr.bootstrap_action import BootstrapAction

action = BootstrapAction(name="Bootstrap to add SimpleCV",
                         path="s3n://<my bucket uri>/bootstrap-simplecv.sh")

conn = boto.emr.connect_to_region('us-west-2')
jobid = conn.run_jobflow(name='My jobflow',
                         log_uri='s3://<my log uri>/jobflow_logs',
                         steps=[step],  # step defined elsewhere
                         bootstrap_actions=[action])

rawDescription: how to install custom packages on amazon EMR bootstrap action in code?

description: Install custom packages on amazon EMR bootstrap action in code


code: parent_list = item_dict[thisFrame][2] # [x, y, list_index]

rawDescription: Managing big point lists, best approach?

description: Managing big point lists, best approach


code:
p = re.compile(':(-?\d\.\d+)\s')
m = p.match(str(line))
dataList = m.groups()

rawDescription: Need help extracting data from a file

description: Need help extracting data from a file


code:
fig, ax = plt.subplots()
ax.plot([3,1,2,4,0,5,3,2,0,2,4])
add_cursor(fig, ax)

rawDescription: IPython Notebook widgets for Matplotlib interactivity


code: [tuple(x) if isinstance(x, set) else (x,) for x in literal_eval(s)]

rawDescription: Pythonic way to parse preflib Orders with Ties files


code:
def parse_preflop_hand(hand):
    if len(hand) < 3 and hand[0] != hand[1]:
        parsed_hand = [hand + 's', hand + 'o']
    else:
        parsed_hand = [hand]
    yield from parsed_hand

rawDescription: Python: Best way to parse the following

description: Python: Best way to parse the following


code:
from urlparse import urljoin

import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor

class CompItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()
    data = scrapy.Field()
    name = scrapy.Field()
    date = scrapy.Field()

class criticspider(CrawlSpider):
    name = "hand"
    allowed_domains = ["consumercomplaints.in"]
    start_urls = ["http://www.consumercomplaints.in/bysubcategory/mobile-handsets"]
    rules = (
        Rule(LinkExtractor(restrict_xpaths="//div[@class='pagelinks']"), follow=True, callback="parse_results"),
    )

    def parse_results(self, response):
        sites = response.xpath("//td/div[@id]")
        for site in sites:
            item = CompItem()
            item['date'] = site.xpath('.//td[@class="small"]/text()').extract()[1]
            item['name'] = site.xpath('.//td[@class="small"]//a/text()').extract()[0]
            item['title'] = site.xpath('.//td[@class="complaint"]/h4/a/text()').extract()[0]

            item['link'] = site.xpath('.//td[@class="complaint"]/h4/a/@href').extract()[0]
            if item['link']:
                if 'http://' not in item['link']:
                    item['link'] = urljoin(response.url, item['link'])
                yield scrapy.Request(item['link'],
                                    meta={'item': item},
                                    callback=self.anchor_page)

    def anchor_page(self, response):
        old_item = response.request.meta['item']

        old_item['data'] = response.xpath('.//td[@class="compl-text"]/div/text()').extract()
        yield old_item

rawDescription: Why scrapy not giving all the results and the rules part is also not working?


code:
self.text = Text(self, height=30, width=30)
self.text.pack()
self.text.insert(END, "TEXT TO BE INSERTED")

rawDescription: Run a function inside a tkinter

description: Run a function inside a tkinter


code:
x = ['apple', 'fruit', 'orange', 'fruit', 'lemon', 'fruit']
x[1::2]
x[::2]

rawDescription: How to delete all the odd ordinal items in a list?

description: Delete all the odd ordinal items in a list


code:
import tempfile

fp1 = tempfile.NamedTemporaryFile()
fp1.write('foo')
fp2 = open(fp1.name)
# Because of buffering we've not yet written to the file.
assert fp2.read() == ''
fp1.flush()
# We've now explicitly written to the file.
assert fp2.read() == 'foo'
fp1.close()
fp2.close()

rawDescription: Second file handler on a file behaves unexpectedly


code:
f = open("junk.txt", "a")
f
type(f)

rawDescription: What data type would the variable in "TEXTFILE = open('x.txt','a')" be?


code:
args = [3, 6]
range(*args)            # It's equivalent to range(3, 6)

rawDescription: Python zip single list element

description: Python zip single list element


code:
def chunkstring(string, length):
    return (string[0+i:length+i] for i in range(0, len(string), length))

rawDescription: What's the best way to split a string into fixed length chunks and work with them in Python?


code: list = [list(group) for key, group in itertools.groupby(list, operator.itemgetter(1))]

rawDescription: Getting duplicates in dict

description: Getting duplicates in dict


rawDescription: Float value is equal -1.#IND

description: Float value is equal -1.#IND


code: pixel_byte = self._array_[y * row_width + x // 8]

rawDescription: Calculating pixel in array

description: Calculating pixel in array


code:
{'applied_discounts': [],
 'base_cost_price': '0.0000',
 'base_price': '0.0000',
 'base_total': '0.0000',
 'base_wrapping_cost': '0.0000',
 'bin_picking_number': '',
 'configurable_fields': [],
 'cost_price_ex_tax': '0.0000',
 'cost_price_inc_tax': '0.0000',
 'cost_price_tax': '0.0000',
 'ebay_item_id': '',
 'ebay_transaction_id': '',
 'event_date': '',
 'event_name': None,
 'fixed_shipping_cost': '0.0000',
 'id': '',
 'is_bundled_product ': True,
 'is_refunded': False,
 'name': '',
 'option_set_id': None,
 'order_address_id': 2,           # should be 0
 'order_id': 614534,              # should be 0
 'parent_order_product_id': 3,    # should be 0
 'price_ex_tax': '0.0000',
 'price_inc_tax': '0.0000',
 'price_tax': '0.0000',
 'product_id': '',
 'product_options': [],
 'quantity': 1,                   # should be 0
 'quantity_shipped': 0,
 'refund_amount': '0.0000',
 'return_id': 0,
 'sku': '',
 'total_ex_tax': '0.0000',
 'total_inc_tax': '0.0000',
 'total_tax': '0.0000',
 'type': 'physical',
 'weight': '0.0000',
 'wrapping_cost_ex_tax': '0.0000',
 'wrapping_cost_inc_tax': '0.0000',
 'wrapping_cost_tax': '0.0000',
 'wrapping_message': '',
 'wrapping_name': ''}

rawDescription: Converting an unknown data structure to a known data structure in Python


code:
INSTALLED_APPS = (
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #'south',
    'app_salon',
    'app_agenda',
)

rawDescription: Page not found grappelli-django install

description: Page not found grappelli-django install


code: dict((key, int(value)) for key, value in items)

rawDescription: Build a dictionary with a regular expression


code:
A=np.genfromtxt(txt, delimiter=',',dtype=str)
A
A.dtype
A.shape
A[:,4]

rawDescription: 2D Numpy Array of Strings WITH column access


code:
import re

def splitOn(*seps): return partial(re.split, '|'.join(re.escape(sep) for sep in seps))

transitions = imap(splitOn(',', '->'), drop(5, input))

rawDescription: How to easily transition from Haskell to Python 3

description: Easily transition from Haskell to Python 3


code:
hello
bar
hello
barbar
hello
barbarbar
hello

rawDescription: Python : Adding a code routine at each line of a block of code


code:
lock = None
def initialize_lock(l):
   global lock
   lock = l

def scenario_1_pool_no_manager(jobfunc, args, ncores):
    """Runs a pool of processes WITHOUT a Manager for the lock and queue.

    """
    lock = mp.Lock()
    mypool = mp.Pool(ncores, initializer=initialize_lock, initargs=(lock,))
    queue = mp.Queue()

    iterator = make_iterator(args, queue)

    mypool.imap(jobfunc, iterator) # Don't pass lock. It has to be used as a global in the child. (This means `jobfunc` would need to be re-written slightly.

    mypool.close()
    mypool.join()

return read_queue(queue)

rawDescription: Understanding Multiprocessing: Shared Memory Management, Locks and Queues in Python


code:
data = [pd.DataFrame.from_csv(f) for f in glob.glob("*.csv")]
df = pd.DataFrame(columns=['ID'])
for d in data:
    cols = [x for x in d.columns if x not in df.columns or x == 'ID']
    df = pd.merge(df, d[cols], on='ID', how='outer', suffixes=['',''])

rawDescription: Looping a merge for a amount of csv files using pandas


code:
def minimum(li, a=0, b=None):
    if b is None:
        b = len(li) - 1
    if b - a < 0:
        raise ValueError("minimum() arg is an empty sequence")
    current_min = li[a]
    for index in xrange(a, b + 1):
        current_min = min(current_min, li[index])

    return current_min

rawDescription: Inclusive range of list Python

description: Inclusive range of list Python


code:
def shifted_range(rangeob, shift):
    size, shift = rangeob.stop, shift * rangeob.step
    return ((i + shift) % size for i in rangeob)

rawDescription: Python 3.x Shifting Ranges

description: Python 3.x Shifting Ranges


code:
rotate("hello", 1)
rotate("hello", 6)

rawDescription: swap letters in a string in python

description: Swap letters in a string in python


code:
s = "Red Joe"
' '.join(reversed(s.split()))

rawDescription: Reverse String order

description: Reverse String order


code:
def backupOperation():
   p = subprocess.Popen(["ls","-la"], stdout=subprocess.PIPE, universal_newlines=True)
   output, err = p.communicate()
   print("\n".join([ x.split(None,1)[0] for x in output.splitlines()]))

rawDescription: How to cut python output in rows and columns

description: Cut python output in rows and columns


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

import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html', test="Hello, world!")

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = tornado.web.Application( handlers=[
        (r'/', IndexHandler)],
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        template_path=os.path.join(os.path.dirname(__file__), "templates"))
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

rawDescription: How to pass arguments from tornado to a js file but not html?

description: Pass arguments from tornado to a js file but not html


code:
def myupdater(self, current, total):
    m = 100 * current / total
    self.gauge_1.SetValue(m)
    wx.Yield()

rawDescription: Wxpython's gauge not refreshing

description: Wxpython's gauge not refreshing


rawDescription: zero values to nan values in an array of Python

description: Zero values to nan values in an array of Python


rawDescription: Convert binary to ASCII and vice versa

description: Convert binary to ASCII and vice versa


code: scores = safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_

rawDescription: How do I view a model produced using scikit-learn?

description: View a model produced using scikit-learn


code:
def split_at_idx(s, idx):
    return [s[i:j] for i,j in zip([0]+idx, idx+[None])]

rawDescription: Efficiently partition a string at arbitrary index


code:
import numpy as np
np.trim_zeros(s, 'b').index[-1]

rawDescription: Find last non-zero element's index in pandas series


code:
from pyspark.sql.functions import col

exprs = [col("Parameters").getItem(k).alias(k) for k in keys]
df.select(*exprs)

rawDescription: PySpark converting a column of type 'map' to multiple columns in a dataframe


rawDescription: Find the top value from a list of tuples


code:
x=repr('\r\r\r')
y=repr('\r\r')
x
y

rawDescription: Finding if string is substring of some string in python: a special case


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

arr = np.random.randint(0, 100, (100000,1))

df = pd.DataFrame(arr)

cnt = Counter(df[0])

df_p = pd.DataFrame(cnt, index=['data']).T

df_p['cumu'] = df_p['data'].cumsum()

df_p.plot(kind='scatter', x='data', y='cumu')

plt.show()

rawDescription: Python: faster way of counting occurences in numpy arrays (large dataset)


rawDescription: I want to write a code that can take two numbers and create a txt file between those numbers in decreasing order


code:
a and b
b and c

rawDescription: How to iterate through list in Python/Jython (like with HashMaps)?

description: Iterate through list in Python/Jython (like with HashMaps)


code:
test = {}
for i in range(10):
    nested = test[str(i)] = {}
    for j in range(15):
        nested[str(j)] = i * j

with open('boo.json', 'w') as f:
    json.dump(test, f)

rawDescription: Error in writing Python dictionary to a file


code:
df["yr_id_sum"] = df.groupby(["id", "year"])["value"].transform(sum)
df

rawDescription: Sum by year and id in Pandas

description: Sum by year and id in Pandas


code:
var_accesses = [tablelist[0] + '.' + var for tablelist in list for var in tablelist[1:]]
var_accesses_string = ', '.join(var_accesses)
tables = [tablelist[0] for tablelist in list]
tables_string = ', '.join(tables)
sql_query = 'SELECT {} FROM {}'.format(var_accesses_string, tables_string)

rawDescription: Extracting multiple values from multiple tables from Database using python


code:
#!/usr/bin/env python

from distutils.core import setup

d = setup(
          # Here come your setup arguments
         )
print(d.get_option_dict('install'))

rawDescription: Referring to existing distutils options inside setup.cfg and setup.py


code:
import numpy as np

A = np.arange(24).reshape((2, 3, 4))
B = np.einsum('kij', A)

i, j, k = np.indices(B.shape)

print(np.all(B[i, j, k] == A[k, i, j]))
# True

rawDescription: How to understand the result of this np.einsum('kij',A)?

description: Understand the result of this np.einsum('kij',A)


rawDescription: Python thread holds up the rest of the script


code:
filename = 'file'
for line in lines:
    file = open(filename + line[21:22] + '.txt', 'a')
    file.write(line)

rawDescription: file separation on the basis of matching character

description: File separation on the basis of matching character


code: session.ConvertMIME = False

rawDescription: reading Lotus Notes documents via COM

description: Reading Lotus Notes documents via COM


code:
a=['airplane','track','car','train']
a + a

rawDescription: shuffle list with repetition constraint 2-back python

description: Shuffle list with repetition constraint 2-back python


code:
def func():
    print('hello')

my_var = func
my_var()  #=>hello

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

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


code:
matched_filenames = [f.fname + f.fext for f in matched]
# -> ['222.txt']

rawDescription: Python efficient way to compare list item with partial item from other list


rawDescription: How to deny users to create an instance of a parent object in Python

description: Deny users to create an instance of a parent object in Python


code:
from itertools import tee, izip

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

rawDescription: Python Equivalent to Ruby's #each_cons?

description: Python Equivalent to Ruby's #each_cons


code:
if _os.name in ("nt", "ce"):
    pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform == "cygwin":
    pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
else:
    pythonapi = PyDLL(None)

rawDescription: How to get current Python DLL?

description: Get current Python DLL


code: fun = lambda tpl: wanted in tpl

rawDescription: Python - using dictionary to count keys and values


rawDescription: python for each match in a string copy to an array

description: Python for each match in a string copy to an array


rawDescription: Fix values that are typed into a doublespinbox in PyQt


rawDescription: Parse HTML and look for multiple classes and tags - most elegant way?


rawDescription: Pandas Dataframes to_html: Highlighting table rows


code:
import urllib
def get_success_url(self):
    params = {
        'company': self.request.POST.get('company'),
        'region': self.request.POST.get('region')
    }

    return ''.join([reverse('result'), '?', urllib.urlencode(params.items())])

rawDescription: django- why after redirecting, the form display "None"

description: Django- why after redirecting, the form display "None"


code:
import HTMLParser
HTMLParser.HTMLParser().unescape('Suzy & John')

rawDescription: How do I unescape HTML entities in a string in Python 3.1?

description: Unescape HTML entities in a string in Python 3.1


code: x

rawDescription: Pandas Dataframe: get index of max element


code:
# delete a post
@app.route('/<path:url>/d')
@login_required
def delete_post(url):
    post = Post.get_post(url)
    if post is None:
        flash('post not found')
        return redirect(url_for('home'))
    db.session.delete(post)
    db.session.commit()
    Topic.update_counts()
    flash('Your post has been deleted')
    return redirect(url_for('index'))

rawDescription: how to put data(id) in form (wtforms) and get it back on submit

description: Put data(id) in form (wtforms) and get it back on submit


code:
@dbus.service.method('fm.last.api.account', out_signature="s",
                     async_callbacks=("callback", "errback"))
def getUsername(self, callback, errback):
    reactor.callLater(3, callback, "alice")

rawDescription: Implementation of an async method in Python DBus


code:
from setuptools import setup, find_packages

setup(
    name='your_project_name',
    version='0.1',
    description='A description.',
    packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']),
    package_data={'': ['license.txt']},
    include_package_data=True,
    install_requires=[],
)

rawDescription: Including non-Python files with setup.py


rawDescription: Finding nodes between two nodes

description: Finding nodes between two nodes


code:
import pprint
pprint.pprint(d)
uniq_d = list(map(dict, frozenset(frozenset(i.items()) for i in d)))
pprint.pprint(uniq_d)

rawDescription: How to make values in list of dictionary unique?

description: Make values in list of dictionary unique


code:
import collections
def namedtuple_with_defaults(typename, field_names, default_values=()):
    T = collections.namedtuple(typename, field_names)
    T.__new__.__defaults__ = (None,) * len(T._fields)
    if isinstance(default_values, collections.Mapping):
        prototype = T(**default_values)
    else:
        prototype = T(*default_values)
    T.__new__.__defaults__ = tuple(prototype)
    return T

rawDescription: Named tuple and optional keyword arguments


code:
def newlist(old_list):
    return [''.join(x) for x in old_list]

rawDescription: Unable to perform join in the inner lists of a list of lists


code:
import ipywidgets as widgets
from functools import partial
from IPython.display import display

def button_callback(counter, w):
    counter.increment()

counter = Counter()
w = widgets.Button(description='Click me')
w.on_click(partial(button_callback, counter))
display(w)

#... sometime later

if counter.value > SOME_AMOUNT:
    do_stuff()

rawDescription: Incrementing a counter using ipython widgets


code:
gen = lazy(df,formulas)
gen.next()
gen.next()

rawDescription: How to create lazy_evaluated dataframe columns in Pandas

description: Create lazy_evaluated dataframe columns in Pandas


code:
0>None
4>None
-4>None
4<None
0<None
-2<None

rawDescription: Python Arguments

description: Python Arguments


code:
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

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

        w = QtGui.QWidget() # container widget
        l = QtGui.QVBoxLayout() # your layout
        w.setLayout(l) # set the layout on your container widget

        btn = QtGui.QPushButton(self)
        btn.setStyleSheet("border-image: url('somepicture.png');")

        label = QtGui.QLabel('Hello!')

        l.addWidget(btn) # add your widget to the layout
        l.addWidget(label) # add the label to the layout

        self.setCentralWidget(w) # add the container widget to the QMainWindow

        self.show()

app = QtGui.QApplication([])
window = MainWindow()

app.exec_()

rawDescription: PyQt: How can i set stylesheet contents on scaled mode?


code:
tr = 'account_id 37318 not found'
tr1 = '2735723'
all(i not in tr for i in ('not found', 'account_id'))
all(i not in tr1 for i in ('not found', 'account_id'))

rawDescription: Python dealing with string-matching

description: Python dealing with string-matching


code: i+1  # i does not yet exist, and since i+1 is a modification on i, this will not work

rawDescription: How do I create variables without declaration in python?

description: Create variables without declaration in python


rawDescription: Creating a dictionary from a text file Python


code:
from datetime import timedelta, datetime
base_date = "10/29 06:58 AM"
b_d = datetime.strptime(base_date, "%m/%d %I:%M %p")

min(list_date, key = func)

rawDescription: Getting the closest date to a given date


code:
from numpy.core.umath_tests import inner1d

res4 = inner1d(U.transpose(0, 2, 1), V.T)

rawDescription: Tensor multiplication with numpy tensordot


code:
data.sort(key=lambda i: i[1]) # sort with timestamp
results = []

def keyfunc(timestamp, interval = 5*60):
    # defined a key function.
    # 1. parse the datetime string to datetime object
    # 2. count the time delta (seconds)
    # 3. divided the time delta with interval, which is (6*60) here
    xt = datetime(2013, 4,3)
    dt = datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')
    delta_second = int((dt - xt).total_seconds())
    normalize_second = (delta_second / interval) * interval
    return xt + timedelta(seconds=normalize_second)

for k, g in groupby(data, key=lambda i: keyfunc(i[1])):
    # k would be time interval "03/04/2013 13:30:00", "03/04/2013 13:35:00" ....
    # g would be the level, timestamp pair belong to the interval
    avg_level = sum([x[0] for x in g]) / len(g)
    results.append((k, avg_level))

rawDescription: Grouping data by frequency

description: Grouping data by frequency


code:
df = read_csv(StringIO(data),sep='\s+')
df

rawDescription: Suppressing scientific notation in pandas?


code:
import pickle
data = {'mac':4, 'mid':5}
data
dataPickle = pickle.dumps(data)
dataPickle
unpick = pickle.loads(dataPickle)
unpick

rawDescription: Send multiple data (text and images) to a server through a socket


code:
ax.contour(...)
ax.plot(...)
# etc.

rawDescription: how to add a plot on top of another plot in matplotlib?

description: Add a plot on top of another plot in matplotlib


code:
for button in self.radioButtons:
    button.grid_forget()
del self.radioButtons

rawDescription: How to delete (or de-grid) Tkinter GUI objects stored in a list (Python)

description: Delete (or de-grid) Tkinter GUI objects stored in a list (Python)


rawDescription: String split with minimum size

description: String split with minimum size


rawDescription: Python Update in MySQL

description: Python Update in MySQL


code: hist = dict((key,val) for key, val in enumerate(numpy.bincount(data)) if val)

rawDescription: How can I generate a complete histogram with numpy?

description: Generate a complete histogram with numpy


code:
def num_spaths(G):
    n_spaths = dict.fromkeys(G, 0.0)
    for n in G:
        my_single_source_shortest_path(G, n, n_spaths)
    return n_spaths

rawDescription: Python: how to compute the number of shortest paths passing through one node?


code:
ip_str = input("Enter a string: ")
count = {}.fromkeys('aeiouAEIOU', 0)
total = 0

for char in ip_str:
   if char in count:
       count[char] += 1
       total += 1

print(count)
print("Number of vowels:", total)

rawDescription: Count the vowels in a string

description: Count the vowels in a string


code:
g1 = Greet(name="John Doe", job="Programmer")

g2 = Greet(name="Janis Duh", age="34", job="Singer")
g2.name = "Janis Joplin"

g3 = Greet()
g3.name = "Smith"
g3.age = "22"
g3.job = "Designer"
print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g1.name, g1.age, g1.job, g1.greet))
g1.shout()

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g2.name, g2.age, g2.job, g2.greet))
g2.shout()

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g3.name, g3.age, g3.job, g3.greet))
g3.shout()

rawDescription: Changing an instance variable when another has been changed


rawDescription: How to use scrapy.Request to load an element from another page into an item

description: Use scrapy.Request to load an element from another page into an item


rawDescription: change column values in CSV using python

description: Change column values in CSV using python


code:
df = pd.DataFrame({'ints': ['3', '5'], 'Words': ['Kobe', 'Bryant']})
df.apply(pd.to_numeric, errors='ignore').info()

rawDescription: convert entire pandas dataframe to integers in pandas (0.17.0)

description: Convert entire pandas dataframe to integers in pandas (0.17.0)


code: highScoreList.sort(key=lambda x: (-x[1], x[2]))

rawDescription: Order list of multiple items in Python by value


code: df[df.index.map(lambda x: x[0] in stk_list)]

rawDescription: How to filter rows of Pandas dataframe by checking whether sub-level index value within a list?

description: Filter rows of Pandas dataframe by checking whether sub-level index value within a list


rawDescription: Take dot product of first and middle entry, second and middle+1 entries until middle-1 and last entry python/numpy


rawDescription: Create new column in panda data frame

description: Create new column in panda data frame


rawDescription: How to merge dictionaries within two lists if they have a common key-value pair?

description: Merge dictionaries within two lists if they have a common key-value pair


rawDescription: Return elements of list as individual items in Python


code:
# Valid Code
for i in range(10):
  b = i

for j in range(b):
  c = j

# Also Valid Code.
for i in range(10):
  b = i

  for j in range(b):
    c = j

rawDescription: indent python file (with pydev) in eclipse

description: Indent python file (with pydev) in eclipse


rawDescription: Numpy: Replace every n element in the first half of an array


code:
import unittest

from bisect import bisect_left, bisect_right

def find_range(array, a, b):
    """Find elements that are greater than a and less than b.
    Returns a tuple (start,end) where array[start] is the first
    value and array[end] is the last value.
    If no value is found, returns start=end=-1.
    """
    start = bisect_right(array,a)
    end = bisect_left(array,b)
    if start==end:
        return (-1,-1)
    else:
        return (start, end-1)

class TestCase(unittest.TestCase):
    Y = [1, 3, 5, 10, 15]
    givenY = [3, 4, 5, 6, 7, 8, 9, 10, 11]

    def test_empty_array(self):
        self.assertEqual( (-1, -1), find_range([], 1, 2) )

    def test_all_values_larger(self):
        self.assertEqual( (-1, -1), find_range([4,5,6], 1, 3) )

    def test_all_values_larger_or_equal(self):
        self.assertEqual( (-1, -1), find_range(self.givenY, self.Y[0], self.Y[1]) )

    def test_both_endpoints_inside_list(self):
        self.assertEqual( (1, 1), find_range(self.givenY, self.Y[1], self.Y[2]))

    def test_2(self):
        self.assertEqual( (3, 6), find_range(self.givenY, self.Y[2], self.Y[3]) )

    def test_no_values_larger_or_equal_to_upper_limit(self):
        self.assertEqual( (8, 8), find_range(self.givenY, self.Y[3], self.Y[4]) )

    def test_sample(self):
        self.assertEqual( (3,3), find_range([1,3,5,7], 5, 8)  )
        self.assertEqual( (3,3), find_range([1,3,5,7], 6, 8)  )

if __name__=="__main__":
    unittest.main()

rawDescription: Finding elements of an array lying between two values


code:
import argparse

class OneOrThree(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        if len(values) == 1:
            setattr(namespace, self.dest, values)
            setattr(namespace, 'result', decimal_to_dms(*values))
        elif len(values) == 3:
            setattr(namespace, self.dest, values)
            setattr(namespace, 'result', dms_to_decimal(*values))
        else:
            raise parser.error('Expected 1 or 3 arguments')

def dms_to_decimal(deg,minute,sec):
    assert float(minute) < 60.0, 'Mintue value: %s must be less than 60' % float(minute)
    assert float(sec) < 60.0, 'Second value: %s must be less than 60' % float(sec)
    return float(deg)+float(minute)/60.0+float(sec)/(60.0*60.0)

def decimal_to_dms(deg):
    minute = 60.0*(deg-int(deg))
    sec = 60.0*(minute-int(minute))
    return int(deg),int(minute),sec

parser = argparse.ArgumentParser(
    usage='%(prog)s deg [minute, sec]',)
parser.add_argument('dms_args', nargs='+',
                    type=float,
                    action=OneOrThree,
                    metavar='deg [minute, sec]',
                    help='Given one arg, returns dms. Given three args, returns deg')
args = parser.parse_args()
print(args.result)

rawDescription: argparse conditional argument structure

description: Argparse conditional argument structure


code:
import urllib

urllib.urlretrieve('ftp://server/path/to/file', 'file')
# if you need to pass credentials:
#   urllib.urlretrieve('ftp://username:password@server/path/to/file', 'file')

rawDescription: Python: download a file over an FTP server


code:
y = x.reshape(-1,2).T
y

rawDescription: Broadcasting issues with numpy.linalg.lstsq


code:
action = None
for key in request.POST.keys():
    if key.startswith('action:'):
        action = key[7:]
        break

rawDescription: Get ID of one of multiple buttons in HTML form in Django


code:
('calcext', 'urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0')
('chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0')
('config', 'urn:oasis:names:tc:opendocument:xmlns:config:1.0')
('css3t', 'http://www.w3.org/TR/css3-text/')
('dc', 'http://purl.org/dc/elements/1.1/')
('dom', 'http://www.w3.org/2001/xml-events')
('dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0')
('draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0')
('drawooo', 'http://openoffice.org/2010/draw')
('field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0')
('fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0')
('form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0')
('formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0')
('grddl', 'http://www.w3.org/2003/g/data-view#')
('loext', 'urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0')
('math', 'http://www.w3.org/1998/Math/MathML')
('meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0')
('number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0')
('of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2')
('office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0')
('officeooo', 'http://openoffice.org/2009/office')
('ooo', 'http://openoffice.org/2004/office')
('oooc', 'http://openoffice.org/2004/calc')
('ooow', 'http://openoffice.org/2004/writer')
('rpt', 'http://openoffice.org/2005/report')
('script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0')
('style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0')
('svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0')
('table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0')
('tableooo', 'http://openoffice.org/2009/table')
('text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0')
('xforms', 'http://www.w3.org/2002/xforms')
('xhtml', 'http://www.w3.org/1999/xhtml')
('xlink', 'http://www.w3.org/1999/xlink')
('xsd', 'http://www.w3.org/2001/XMLSchema')
('xsi', 'http://www.w3.org/2001/XMLSchema-instance')

rawDescription: Extract data using lxml in Python from every namespace attribute


code:
from __future__ import print_function
from  mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

assert size > 1

if rank == 0:
    result = comm.recv(source = MPI.ANY_SOURCE, tag = MPI.ANY_TAG)
    print(result)
elif rank == 1:
    comm.send((1, 3.14), dest = 0)

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

description: Create struct for passing between nodes using mpi4py


code:
for table in alltables:
    link = table.find('a')
    top100 = link.string
    print(top100)

rawDescription: Python 3 Beautiful Soup Data type incompatibility issue


code: myList=[myStr[i:i+4] for i in xrange(0,len(myStr),4)]

rawDescription: What is the easiest but not fastest way to format this string into a list? Python


code:
def A(dx, dy):
  return math.degrees( math.atan2(dy, dx) )

def LengthDir(length, angle):
  radian_angle = math.radians(angle)
  return (length * math.cos(radian_angle), length * math.sin(radian_angle))

rawDescription: python - 2 math questions

description: Python - 2 math questions


code:
# Extract the values along the line, using cubic interpolation
zi = scipy.ndimage.map_coordinates(z, np.vstack((row, col)))

rawDescription: Plot cross section through heat map

description: Plot cross section through heat map


code:
words = {}
with open('a.txt') as f:
  for l in f:
    cols = l.split()
    word = cols[0]
    nums = [int(e) for e in cols[1:]]
    words[word] = nums

# Printing all words with their number lists
for k, v in words.iteritems():
  print("%s -> %s" % (k, v))

# Getting specific ones
print(words['ABC'][1]) # -> 34
print(words['hre'][2]) # -> 356

# Showing it's an int - doing some basic arithmetic operation
i = words['ABC'][1] + 50 # -> 84
print(i)

rawDescription: Python--how to extract information from a mixed of digits and characters and save them in a two dimentional list?


code: dict((option, all_options[option]) for option in selected_options if option in all_options)

rawDescription: Python: Check if any list element is a key in a dictionary


code:
def split_at(text, delimiter, opens='<([', closes='>)]', quotes='"\''):
    result = []
    buff = ""
    level = 0
    is_quoted = False

    for char in text:
        if char in delimiter and level == 0 and not is_quoted:
            result.append(buff)
            buff = ""
        else:
            buff += char

            if char in opens:
                level += 1
            if char in closes:
                level -= 1
            if char in quotes:
                is_quoted = not is_quoted

    if not buff == "":
        result.append(buff)

    return result

rawDescription: Splitting comma delimited strings in python


rawDescription: python, rebuilding lists

description: Python, rebuilding lists


code:
def split(x, f):
    return list(itertools.compress(x, f)), list(itertools.compress(x, (not i for i in f)))

rawDescription: Python equivalent of R "split"-function

description: Python equivalent of R "split"-function


code:
class data_main:
    def __init__(self):
        # Contains data for each Pi
        self.data_plot = data()
        self.data_overflow = data()

rawDescription: Creating classes in dictionaries - values being duplicated between classes?


code:
is_unrecognised = not all(conditions)
# may be you can write
# if not is_unrecognised:
#    you code in else

rawDescription: How can I optimise my if else loop python?

description: Optimise my if else loop python


code:
data
target

rawDescription: Python: How to read file and store certain columns in array


code:
from collections import defaultdict

def tree():
    return defaultdict(tree)

nested = defaultdict(tree)

for word in words:
    node = nested
    for char in word:
        node = node[char.upper()]

rawDescription: Create a nested dictionary for a word python


code: the_answer = mynos[0] - mynos[1]

rawDescription: Generating two random numbers and subtracting them


code:
import random
random.seed(1)

rawDescription: How to use random numbers properly in "Higher or Lower" game. I'm only a beginner

description: Use random numbers properly in "Higher or Lower" game. I'm only a beginner


code:
D=(0.133e-4,0.243e-4,0.283e-4)
for i, dval in enumerate(D):
    dx[i] = sc.sqrt(dval*dt/M)

rawDescription: refactor code in order to use 3 values to make plot & tuple indices must be integers, not float

description: Refactor code in order to use 3 values to make plot & tuple indices must be integers, not float


code:
#header file2
spam
eggs

rawDescription: concatenate several file remove header lines

description: Concatenate several file remove header lines


code:
import pandas as pd
import numpy as np

def topic_prob_extractor(hdp=None, topn=None):
    topic_list = hdp.show_topics(topics=-1, topn=topn)
    topics = [int(x.split(':')[0].split(' ')[1]) for x in topic_list]
    split_list = [x.split(' ') for x in topic_list]
    weights = []
    for lst in split_list:
        sub_list = []
        for entry in lst:
            if '*' in entry:
                sub_list.append(float(entry.split('*')[0]))
        weights.append(np.asarray(sub_list))
    sums = [np.sum(x) for x in weights]
    return pd.DataFrame({'topic_id' : topics, 'weight' : sums})

rawDescription: Hierarchical Dirichlet Process Gensim topic number independent of corpus size


rawDescription: Is there a way to preserve duplicate keys in python dictionary


code: from uncertainties import *  # sin, cos, etc.

rawDescription: Definition of mathematical operations (sin…) on NumPy arrays containing objects


code: sum(sum(range(n + 1)) for n in range(6))

rawDescription: Lambdas and sums Python

description: Lambdas and sums Python


code:
import csv
from collections import OrderedDict
from xml.etree.ElementTree import ParseError
import collections
from __future__ import print_function

def get_clientresponses_2(filename = 's.csv'):

    with open(filename, 'rU') as infile:
        reader = csv.DictReader(infile)         # read the file as a dictionary for each row ({header : value})
        data = {}
        for row in reader:
            for header, value in row.items():
                try:
                    data[header].append(value)
                except KeyError:
                    data[header] = [value]

        client_responses = data['E'] #returns a list
        for client_response in client_responses:
            xml_string = (''.join(client_response))
            xml_string = xml_string.replace('&', '')
            try:
                root = ElementTree.XML(xml_string)
#                 print(root)
                return root
            except ET.ParseError:
                print("catastrophic failure")
                continue

def allocate_and_write_2():

    with open(filename, 'r') as infile:
        reader = csv.DictReader(infile)         # read the file as a dictionary for each row ({header : value})
        header = set()
        results = []
        #     data = {} # this is not needed for the purpose of this organization
        for row in reader:
            for xml_data in get_clientresponses_2():
                xml_dat =  dict(flatten_dict(xml_data))
                row.update(xml_dat)        # just for XML data
                results.append(row)         # everything else
                header.update(row.keys())  # can't forget headers

        #     print(row) # returns dictionary of key values pairs (headers : values)
        #     print(results) # returns list wrapper for dictionary
        #     print(headers) #returns set of all headers
            headers_list = list(header)
        #     print(headers_list) #list form of set

            with open('csv_output.csv', 'wt') as f:
                writer = csv.writer(f)
                writer.writerow(headers_list)
                for row in results:
                    data = [row.get(x, '') for x in headers_list]
#                     print(data)
                    writer.writerow(data)
    #             writer.writerows(zip(headers_list, data))

rawDescription: How to pass a function within another function when using Python

description: Pass a function within another function when using Python


code:
class ProfileForm(forms.ModelForm):
    class Meta:
         model = Profile
         fields = ("name", "description")

rawDescription: How to create a profile registration form in Django?

description: Create a profile registration form in Django


code:
def read(filename):
    rows = []
    x = open(filename)
    y = csv.reader(x)
    for row in y:
        rows.append(row)
    x.close()
    return rows

rawDescription: Condensing Repetitive Code

description: Condensing Repetitive Code


code:
def new_func(x):
    return foo(x,300)

rawDescription: pass function to function with different number of variables

description: Pass function to function with different number of variables


code:
out = dict(getattr(self, self.LOOKUPDICT[k])({k: query[k]})
           for k in query if k not in self.exclusions)

rawDescription: Python dictionary comprehension

description: Python dictionary comprehension


code:
...
with open('output.txt', 'w') as f:
    print >>f, result
    for item, count in sorted(result.iteritems()):
        if count >= 2:
            print >>f, " ".join(item).encode('utf8'), count

rawDescription: Redirecting the print output to a .txt file in Python


code:
item = stock.stock_list(location_name)
complete_list = [stock.stock_price(x) for x in sorted(item)]

rawDescription: How to loop through a function for each item in list?

description: Loop through a function for each item in list


code:
def stop(self):
    """Stop running the event loop.

    Every callback scheduled before stop() is called will run.
    Callback scheduled after stop() is called won't.  However,
    those callbacks will run if run() is called again later.
    """
    self.call_soon(_raise_stop_error)

def _raise_stop_error(*args):
    raise _StopError

rawDescription: python asyncio run event loop once?

description: Python asyncio run event loop once


code:
import matplotlib.pyplot as plt

ax = plt.gca()

ax.fill(*p.boundary.xy, color='y')
ax.axvline(x_val, color='b')

ax.plot([x_val, ] * len(points), points, 'b.')
ax.plot([x_val, ] * 2, minmax, 'ro', ms=10)

rawDescription: Python - Return y coordinates of polygon path given x


rawDescription: Getting the indexes to the duplicate columns of a numpy array


rawDescription: Find maximum value of minimum elements in tuple


rawDescription: Creating C like Enum in Python

description: Creating C like Enum in Python


rawDescription: how to measure execution time of functions (automatically) in Python

description: Measure execution time of functions (automatically) in Python


code: return [final_img]

rawDescription: Serving an image from PIL dynamically

description: Serving an image from PIL dynamically


code:
def force_init(n):
    # equivalent to np.array(list(range(number_of_particles)))
    count = np.linspace(0, number_of_particles-1, number_of_particles)
    x = (count * 2) % limit_x + radius
    y = (count * 2) / limit_x + radius
    return np.column_stack((x, y))

rawDescription: Many particles in box - physics simulation


code:
def f(x):
    while True:
        yield x
        x = x * x

for v in f(2):
    if v > 100000:
        break
    print(v),        # 2 4 16 256 65536

rawDescription: Generating list of iterations on own output in Python


code:
for i in range(4,7):
    main(i)

rawDescription: Simulate multiple coin toss streak

description: Simulate multiple coin toss streak


code: s = sum(v)

rawDescription: Declare and assign an amount of variables from an input?


code:
f = Win32Font("Arial", 15)
im = f.renderText("this is just a test")
im.save("c:/hope.png")

rawDescription: python: use windows api to render text using a ttf font

description: Python: use windows api to render text using a ttf font


code: a = array('u', u'\0' * size)

rawDescription: initializing Arrays in python

description: Initializing Arrays in python


code:
import xml.etree.ElementTree as ET
s = """    <data>
        <plan_main>
            <plan>
               <name>John</name>
               <id>1</id>
            </plan>
            <plan>
               <name>Charlie</name>
               <id>2</id>
            </plan>
         </plan_main>
       <location>
            <country>
               GB
            </country>
            <country>
               DE
            </country>
       </location>
    </data>"""
root = ET.fromstring(s)
for plan_main in root.findall('./plan_main'):
    for plan in plan_main:
        name = plan.find('name').text
        if name.lower() != "john":
            plan_main.remove(plan)
print(ET.tostring(root).decode('utf-8'))

rawDescription: Python: Find and Remove Child from XML file and output to new file


code: df['direction'] / df['volatility']

rawDescription: Calculating Kaufman's Efficiency Ratio in Python with Pandas?


code:
def boolList2BinString(lst):
    return '0b' + ''.join(['1' if x else '0' for x in lst])
boolList2BinString([True, True, False])
print(boolList2BinString([True, True, False]))

rawDescription: Python: Boolean List to Binary String

description: Python: Boolean List to Binary String


rawDescription: How are arguments passed to a function through __getattr__


code:
dist = weighted.sum()
dist / dist.sum()

rawDescription: Weighting values based on variables

description: Weighting values based on variables


code: result[i][j]

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


code:
with open(filen, "r") as fileinput:
    for line in fileinput:
        x, y, z = line.split()
        y = float(y) - 316400
        output_text = "{}   {:.3f}   {}\n".format(x, y, z)
        fileout.write(output_text)

rawDescription: Readlines() and calculations in for loop


code: some_var = raw_input("Input (no longer than 40 characters): ")[:40]

rawDescription: Limiting raw_input in python

description: Limiting raw_input in python


code: Submit("save", _("Get Started"),css_class="pull-right", style='display: none;')

rawDescription: if statement in Django crispy form, conditional form layout

description: If statement in Django crispy form, conditional form layout


code:
context = daemon.DaemonContext()
context.open()
try:
    do_main_program()
finally:
    context.close()

rawDescription: Can you use python-daemon with Python 2.4?

description: Use python-daemon with Python 2.4


code:
#!/usr/bin/env python3
import os
# set the directory, the outfile and the tag below
dr = "/path/to/directory"; out = "/path/to/newfile"; tag = ".txt"

for f in [f for f in os.listdir(dr) if f.endswith(".txt")]:
    open(out, "+a").write(("").join([l for l in open(dr+"/"+f).readlines()[22:25]])+"\n")

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


code: df['t'] = [0.02 * i for i in range(len(df))]

rawDescription: Pandas new column with constant increments


code:
# some example data
np.random.seed(0)
n_values = 1000000
n_items = 100000
values = np.random.rand(n_values)
items = np.random.rand(n_items)
values.sort()
items.sort()

rawDescription: searching sorted items into a sorted sequence

description: Searching sorted items into a sorted sequence


rawDescription: Why does JSONDecoder deserialize json String to Python Objects intuitively?


code:
match = over.search(s, pos = m_brown.end())
match.group()
match.span()

rawDescription: Find length of string matched by regex

description: Find length of string matched by regex


code: [mincoinList[price-coin] for coin in coins if coin <= price]

rawDescription: Returning minimum of array with for loop iteration


code: result = numpy.einsum('ij,kjl->kil', A, u)

rawDescription: Multiplying matrix A times multi-dimensional matrix "matrix-wise?"


code:
MyTuple = namedtuple('MyTuple', ['attr1', 'attr2', 'attr3'])
new_tuple = MyTuple.from_kws(attr1=1, attr2=2, attr3=3)

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


code:
import os
os.system('echo \'Server = /foo/$arch\' ')

rawDescription: "Escaping" $ when executing a remote bash command from python fabric


code: r'"Postal Code":(.+?),'

rawDescription: Python capture group and quote it with regex


rawDescription: Problems with zoom and menu (Tkinter)

description: Problems with zoom and menu (Tkinter)


code:
file = open('cart_output.txt', 'wt')

def cart(n, seq):
    from itertools import product
    buflist = []
    append = buflist.append
    b = 8
    while b < n:
        n = n - 1
        for p in product(seq, repeat=n):
            append(''.join(p))
    file.write('\n'.join(buflist)+'\n')

file.close()

rawDescription: python itertools product slow is the write speed to a output file a bottleneck

description: Python itertools product slow is the write speed to a output file a bottleneck


code: df.sort_values(['group_min', 'i', 'b'], ascending=[False, False, True])

rawDescription: Sort pandas DataFrame by multiple columns and duplicated index


rawDescription: How to make equivalency in Python?

description: Make equivalency in Python


code:
def product_no_repeats(*args):
    for p in itertools.product(*args):
        if len(set(p)) == len(p):
            yield p

rawDescription: Randomly generating 3-tuples with distinct elements in python


code:
from Tkinter import Tk, Text, DISABLED
r = Tk()
Text(r, state=DISABLED).grid()
r.mainloop()

rawDescription: Is there a way to make a Text widget in Tkinter non-clickable and non-editable?


rawDescription: Django JSONField filtering

description: Django JSONField filtering


rawDescription: numpy arange and where

description: Numpy arange and where


code:
class Person:
    # ...

    @property
    def administrator(self):
        return self.role and (self.role.is_admin or self.role.id == 1)

rawDescription: Sqlalchemy select condition

description: Sqlalchemy select condition


code:
array_ = np.array([0, 71, 598, 71, 0, 0, 243])
_, inverse = np.unique(array_, return_inverse=True)
inverse
np.bincount(inverse)[inverse]

rawDescription: numpy fastest way to transform an array's elements to their frequency

description: Numpy fastest way to transform an array's elements to their frequency


code:
with open('filename', 'r') as f:
    contents = f.read()    # results in a str object

rawDescription: Python open("x", "r") function, how do I know or control which encoding the file is supposed to have?


code:
def pension (pension_contribution, income):

    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")

    else:
        print("Your annual pension contribution is R", pension_contribution * 12)

income = int(input("What is your annual income?"))
pension_contribution = int(input("What is your monthly pension contribution?"))

pension(pension_contribution, income) #call function

rawDescription: Use user input within as input in a function Python


code:
blpcfgokakmgnkcojhhkbfbldkacnbeo
pjkljhegncpnkpknbcohdijeoejaedia
apdfllckaahabafndbhieahigkjlhalf
...

rawDescription: python - identify characters from numbers in csv file

description: Python - identify characters from numbers in csv file


rawDescription: The activities in abaqus/viewer's query>probe values is not recorded in the macro?


code:
def recursive_dict(element):
    retval = {}
    retval["tag"] = element.tag
    if element.text:
        retval["text"] = element.text

    if element.tail:
        retval["tail"] = element.tail

    if element.attrib:
        retval["attributes"] = element.attrib

    if len(element) > 0:
        retval["children"] = [recursive_dict(child_element) for child_element in element]

    return retval

rawDescription: Why doesn't recursive_dict from the lxml FAQs work?


code:
import module

...

some_important_thing = module.some_important_thing

#Bottleneck loop
for item in items:
   #module.some_important_thing()
   some_important_thing()

rawDescription: Is it costly in Python to put classes in different files?


code:
dicts = [{'amount': 42140.0, 'name': 'Payment', 'account_id_credit': 385, 'type': u'expense', 'account_id_debit': 476},
         {'amount': 43926.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'payable', 'account_id_debit': 641},
         {'amount': 3800.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476},
         {'amount': 46330.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476},
         {'amount': 67357.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'payable', 'account_id_debit': 323},
         {'amount': 26441.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476}]

def aggregate(dicts, keys):
    def worker(aggr, dic):
        key_vals = tuple(dic[key] for key in keys)
        aggr.setdefault(key_vals, {key: [] for key in dic.iterkeys()})
        for key, value in dic.iteritems():
            aggr[key_vals][key].append(value)
        return aggr

    assert len(set(tuple(dic.iterkeys()) for dic in dicts)) == 1
    return reduce(worker, dicts, {})

keys = ("account_id_credit", "type", "account_id_debit")
aggr_expense = [dic for keys, dic in aggregate(dicts, keys).iteritems() if keys[1] == u"expense"]
merged_expense = [{key: sum(value) if key == "amount" else value[0] for key, value in dic.iteritems()}
                  for dic in aggr_expense]
result = merged_expense + filter(lambda dic: dic["type"] != u"expense", dicts)
print(result)

rawDescription: Python: merge n-dictionaries depending on several values + sum


rawDescription: Extracting XML Element and Attribute Data with Python 3


code:
c_maxes = df.groupby(['A', 'B']).C.transform(max)
df = df[df.C == c_maxes]

rawDescription: Python(pandas): removing duplicates based on two columns keeping row with max value in another column


code: pua = [unichr(i) for i in xrange(0xE000, 0xf900)]

rawDescription: List all unicode codes between U+E000–U+F8FF using Python


rawDescription: Drawing Stars with turtle in python

description: Drawing Stars with turtle in python


code: lines = open("textfile").read().split()

rawDescription: Moving each value of a text file to its own line in Python


rawDescription: Moving row values between columns based on other column values in a Pandas DataFrame


rawDescription: numpy.convolve python old numbers cant change

description: Numpy.convolve python old numbers cant change


rawDescription: How to apply key function on tuple elems in sorted()?

description: Apply key function on tuple elems in sorted()


code:
def __getattr__(self, k):
    """ Defines the names of the methods to be the same as the
    keys of the dictionaries.
    """
    if k in self:
        def getter_setter(new_value=None):
            if new_value is None:
                return self[k]
            self[k] = new_value
        return getter_setter
    raise AttributeError(k)

rawDescription: Extract methods of a class from the keys of a dictionary


code:
header = table.horizontalHeader()
header.setStretchLastSection(True)

rawDescription: How to make QTableWidget's columns assume the maximum space?

description: Make QTableWidget's columns assume the maximum space


code:
def MyClass(a="v1", b="v2", cls=namedtuple("MyClass", ["a", "b"])):
    return cls(a, b)

rawDescription: Python: how to implement __getattr__()?

description: Python: how to implement __getattr__()


code:
r = Root()
n = Node(r)
r.children[0] is n  #True
n.parent is r  #True

rawDescription: best way to represent a bidirectional traversable hierarchy

description: Represent a bidirectional traversable hierarchy


code:
fnx = lambda: set(RND.sample(string.ascii_uppercase, 7))
S = [fnx() for c in range(5)]

rawDescription: Pairwise Set Intersection in Python

description: Pairwise Set Intersection in Python


code:
import os
os.getpgrp()
import sys
cmd = sys.executable + ' -c "import os; print(os.getpgrp())"'
os.system(cmd) #!!! same process group
import subprocess
import shlex
subprocess.check_call(shlex.split(cmd))
subprocess.check_call(cmd, shell=True)
subprocess.check_call(cmd, shell=True, preexec_fn=os.setpgrp) #!!! new

rawDescription: python kill parent process but child process left

description: Python kill parent process but child process left


code:
def group(iterable):
    myIter = iter(iterable)

    run = [next(myIter)]
    def continuesRun(x):
        return run[-1]==x-1

    for x in myIter:
        if continuesRun(x):
            run.append(x)
        else:
            yield run
            run = [x]
    yield run

rawDescription: Grouping consecutive dates together

description: Grouping consecutive dates together


code:
Team1, Name1, Surname1, Name3, Surname3
Team2, Name1, Surname1, Name2, Surname2
Team3, Name1, Surname1
Team4, Name2, Surname2
Team5, Name3, Surname3

rawDescription: Aggregate text field from several lines (javascript/python)


rawDescription: How to count everything in quotes as only one item

description: Count everything in quotes as only one item


code:
from itertools import chain

a_2 = list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(a, c)))
b_2 = list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(b, c)))

rawDescription: Generate repeated elements in a list based on elements of another list


code: max(d, key=d.get)

rawDescription: Get dict key by max value

description: Get dict key by max value


code:
filter_matrix = np.array(filterColumns)
combination_array = np.array(combination)
bool_matrix = filter_matrix == combination_array[newaxis, :]   #not sure of the newaxis position
subset = raw_data[bool_matrix]

rawDescription: Python & Numpy - create dynamic, arbitrary subsets of ndarray


rawDescription: Selecting Subset of Pandas DataFrame

description: Selecting Subset of Pandas DataFrame


code:
class mydecorator(object):
    def __init__(self, f):
        self.f = f
        self.x = 0

    def __call__(self, *k, **kw):
        self.x += 1
        return f(self.x)

rawDescription: Class decorators vs function decorators

description: Class decorators vs function decorators


code:
p = ['', '']
from bs4 import BeautifulSoup
soup = BeautifulSoup(''.join(p), 'html.parser')
src_links = [img['src'] for img in soup.find_all('img')]
src_links

rawDescription: Python Regex over list of strings

description: Python Regex over list of strings


code:
def depth(d, level=1):
    if not isinstance(d, dict) or not d:
        return level
    return max(depth(d[k], level + 1) for k in d)

rawDescription: Know the depth of a dictionary

description: Know the depth of a dictionary


code:
num_in_ind = np.bincount(a[:,0])
results = [np.sort(a[a[:,0]==ii,1])[num_in_ind[ii]/2] for ii in np.unique(a[:,0])]

rawDescription: dealing with arrays: how to avoid a "for" statement

description: Dealing with arrays: how to avoid a "for" statement


code:
bare=False
# Subscribe using a specific resource, and not the bare JID

subscribee='somejid@example.com'
# Subscribe a different JID to the node, if authorized

options=data_form
# Provide subscription options using a XEP-0004 data form

rawDescription: sleekxmpp pubsub example

description: Sleekxmpp pubsub example


code:
from collections import namedtuple

NamedSeq = namedtuple('NamedSeq', ('vertex', 'visited', 'letter'))

array = NamedSeq(values[0], values[1], values[2])
#or
array = NamedSeq(vertex=values[0], letter=values[2], visited=values[1])

rawDescription: Passing values to array indexes in Python


code:
df["Event"] = (df["delta"] >= 5).groupby(df["rx"]).cumsum() + 1
df

rawDescription: Python: Create an incrementing variable that changes based on a condition


rawDescription: Combining multiple identical SQLite queries into one


code:
def sort_mean(fle):
    avgs, high = [], []
    with open(fle) as f:
        for row in csv.reader(f, delimiter=" "):
            name, scores = row[0], row[1:]
            srt = map(int, scores)
            avgs.append((name, sum(srt,0.0) / len(srt)))
            high.append((name, max(srt)))
    avgs.sort(key=itemgetter(1), reverse=1)
    high.sort(key=itemgetter(1), reverse=1)
    return avgs, high

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


code:
#ScriptName : Login.py
#---------------------
from selenium import webdriver

#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

baseurl = "http://www.mywebsite.com/login.php"
username = "admin"
password = "admin"

xpaths = { 'usernameTxtBox' : "//input[@name='username']",
           'passwordTxtBox' : "//input[@name='password']",
           'submitButton' :   "//input[@name='login']"
         }

mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.maximize_window()

#Clear Username TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()

#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)

#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()

#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)

#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()

rawDescription: How to use Selenium with Python?

description: Use Selenium with Python


code:
import dis
dis.dis(compile('''\
if j > 0 and i < m and B[j-1] > A[i]:
    pass
''', '', 'exec'))
dis.dis(compile('''\
if j > 0 and i < m:
    if B[j-1] > A[i]:
        pass
''', '', 'exec'))

rawDescription: Why is nested `if` in Python much slower than parallel `and`?


code:
for k in loc_x_curve.keyframe_points:
    # k.co[0] is the frame number
    # k.co[1] is the keyed value
    k.interpolation = 'CUBIC'
    k.easing = 'EASE_IN'

rawDescription: How Can I Set the Interpolation of a F-Curve via Python? (Blender)


code:
indcol = df2.ix[:,0]
df2.columns = indcol

rawDescription: How to change header row in a python dataframe

description: Change header row in a python dataframe


code: {k:v for k,v in com_keys.items() if len(v)>1 }

rawDescription: Common items between all the keys of a dictionary


code:
class Example(object):
    def __init__(self):
        self.a=1
        self.b=2
    def cl_print(self):
        print("works : {0} {1}".format(self.a, self.b))
    def __getitem__(self, item):
        return self

rawDescription: How to have a list in python which returns a default item on an invalid index or no index at all

description: Have a list in python which returns a default item on an invalid index or no index at all


code:
import clr
clr.AddReference("System")
from System.Diagnostics import Process
Process.Start(r"mk:@MSITStore:C:\planta\client\Help\Planta.chm::/D-KA-0044095.html")

rawDescription: ironpython: How to call a chm Helpfile on Windows with parameters

description: Ironpython: How to call a chm Helpfile on Windows with parameters


rawDescription: Build list of tuples from simple list

description: Build list of tuples from simple list


code:
invalid_test = sc.parallelize([
  (2, 6), # No product in the training data
  (6, 1)  # No user in the training data
])
invalid_test
## ParallelCollectionRDD[500] at parallelize at PythonRDD.scala:423

model.predictAll(invalid_test).count()
## 0

invalid_test_ = invalid_test.map(lambda xs: tuple(int(x) for x in xs))
model.predictAll(invalid_test_).count()
## 0

rawDescription: Spark ALS predictAll returns empty

description: Spark ALS predictAll returns empty


code: print(''.join([c if c in guesses else '*' for c in word]))

rawDescription: Python 3, for a in list and a in word:

description: Python 3, for a in list and a in word:


code: counted

rawDescription: Aggregation on aggregated values

description: Aggregation on aggregated values


code:
# No default argument - you'd never want to use the default.
def with_random_swap(lst):
    # Use random.sample to pick two distinct random indices.
    index1, index2 = random.sample(xrange(len(lst)), 2)
    copy = lst[:]
    copy[index1], copy[index2] = copy[index2], copy[index1]
    return copy

rawDescription: Python TSP Berlin 52 with Simulated Annealing


code:
def lazyinit(cls):

    class p(object):
        def __init__(self, *args, **kws):
            self._init = lambda: cls(*args, **kws)
            self._obj  = None

        def __getattr__(self, k):
            if not self._obj:
                self._obj = self._init()
            return getattr(self._obj, k)

    return p

rawDescription: Automatically create (and keep) an object when accessed


code: cursor = get_db().cursor()

rawDescription: Mocking pyodbc module calls for django unit tests


code: foo.update({'url': d.get('url')}, {'$addToSet': {'users': {'user': 'john', 'value': 0}}})

rawDescription: Correcting database structure with MongoDB


code: key = +1

rawDescription: Python: create dict from list and auto-gen/increment the keys (list is the actual key values)?


code:
t = conn.begin()
try:
    conn.execute(RAW_SQL)
    t.commit()
except:
    t.rollback()

rawDescription: sqlalchemy raw sql query limit using connection.execute()

description: Sqlalchemy raw sql query limit using connection.execute()


code: driverKeys = cmds.keyframe("myAnimCurve", q=True, floatChange=True)

rawDescription: How do I find the time of a particular keyframe using cmds.selectKey?

description: Find the time of a particular keyframe using cmds.selectKey


code:
import itertools
import numpy as np
from scipy.linalg import toeplitz, det

rawDescription: Levinson algorithm in python

description: Levinson algorithm in python


code:
s.index = s.index.swaplevel(0, 1)
s = s.sort_index()
s.index.names = ['item_uid', 'created_at']  # as this is lost earlier
s

rawDescription: Efficiently re-indexing one level with "forward-fill" in a multi-index dataframe


rawDescription: Call a Python 2.7 module in Python 3.2

description: Call a Python 2.7 module in Python 3.2


code:
from pyspark.sql.types import IntegerType
from pyspark.sql.functions import udf

size_ = udf(lambda xs: len(xs), IntegerType())

df.where(size_(col("tokens")) <= 3).show()

## +---------+
## |   tokens|
## +---------+
## |[S, U, S]|
## +---------+

rawDescription: Filtering DataFrame using the length of a column


code:
missed_in_a = [x for x in a if x not in b]
missed_in_b = [x for x in b if x not in a]

rawDescription: restoring list from unified_diff output

description: Restoring list from unified_diff output


code: newlist = [round(x) for x in numbers if x > 0]

rawDescription: python list comprehension - learnpython.org

description: Python list comprehension - learnpython.org


code:
q = SomeQueueClass.connect('some.ip.address.0')
q.push('http://someurl.com')

rawDescription: Send url seed to EC2 instances using Python


code:
for worker in worker_list:
    total_hours = sum(worker_list[worker]) * 13

rawDescription: Looping and returning all values in a dictionary


rawDescription: Filter, group, and calculate statistics for Numpy matrix data


rawDescription: Python remove couple of duplicates from List


rawDescription: How can I iterate through a python 2D list and delete/replace characters from a specific column?

description: Iterate through a python 2D list and delete/replace characters from a specific column


code:
class PotatoSerializer:
    ...
    def build(self, validated_data):
        return self.Meta.model(**validated_data)

rawDescription: Create an instance from serializer without persisting it to db


code:
cols
list(map(lambda x: '_'.join(x.split()).replace('1/2', 'half'), cols))

rawDescription: Using dynamic lists in query in Pandas

description: Using dynamic lists in query in Pandas


code:
my_str = '1.35'
my_str_as_a_number = float(my_str)
my_str_as_a_number
my_str_as_a_number + 1

rawDescription: get the value without ' '

description: Get the value without ' '


code:
from middleware import redirect
redirect('/some/url/')

rawDescription: Django HTTP Redirect outside of view function


code:
data= (['a','','b',''],['c','',''])
tuple([e for e in l if e] for l in data)

rawDescription: Removing blank objects within a tuple

description: Removing blank objects within a tuple


code:
def __init__(self, path=None):
    self.urls_seen = set()
    RFPDupeFilter.__init__(self, path)

def request_seen(self, request):
    if request.url in self.urls_seen:
        return True
    else:
        self.urls_seen.add(request.url)

rawDescription: Scrapy crawling data from a forum

description: Scrapy crawling data from a forum


code: myTitle = soup('title')

rawDescription: Python fetching <title>

description: Python fetching <title>


rawDescription: Can't get Scrapy to return text in Div

description: Can't get Scrapy to return text in Div


code: np.fill_diagonal(df.values, 0)

rawDescription: Set values on the diagonal of pandas.DataFrame


code:
class User (db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    inbox = db.relationship('Message',backref='sender', primaryjoin=id==Message.emmiter_id)
    outbox = db.relationship('Message',backref='receiver', primaryjoin=id==Message.recipient_id)

rawDescription: Self-referential database with extra fields in sqlalchemy


rawDescription: How do I separate a space-delimited string into multiple variables?

description: Separate a space-delimited string into multiple variables


code: json.dumps([o.dump() for o in my_list_of_ipport])

rawDescription: how to convert a list of object to json format in python

description: Convert a list of object to json format in python


code:
constants = {}
with open('foo.h') as infile:
    for name, value in re.findall(r'#define\s+(\w+)\s+(.*)', infile):
        try:
            constants[name] = ast.literal_eval(value)
        except Exception as e:
            pass # maybe log something
with open('_foo_h.py', w) as outfile:
    outfile.write(repr(constants))

rawDescription: Is there a way to load the constant values stored in a header file via ctypes?


code:
users = {
   1: dict(Name="Mr. Foo", Location="Boston", Type="Secondary"),
   2: dict(Name="Mr. Bar", Location="New York", Type="Primary"),
   3: dict(Name="Mr. Quux", Location="Chicago", Type="Secondary"),
   #...
}

rawDescription: Small Tables in Python?

description: Small Tables in Python


rawDescription: Natural Naming Scheme for a dynamic set of members


code: LOGIN_URL = 'login'  # the name of the url pattern

rawDescription: Is LOGIN_REDIRECT_URL deprecated?

description: Is LOGIN_REDIRECT_URL deprecated


code:
X_Max = float('-inf')
X_Min = float('+inf')
Y_Max = float('-inf')
Y_Min = float('+inf')

for p in lasfile.File(inFile,None,'r'):
    X_Max = max(X_Max, p.x)
    X_Min = min(X_Min, p.x)
    Y_Max = max(Y_Max, p.y)
    Y_Min = min(Y_Min, p.y)

rawDescription: Python: How to improve performance of a script to obtain the Bounding Box of a set of points


code:
def __init__(self, Driver, Sponsor, Odometer=0, Speed=None):
    if speed is None:
        speed = randint(1,121)

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


code:
#!/usr/bin/env python
from subprocess import Popen, PIPE

p = Popen(['java', '-jar', 'myjarfile.jar'], stdin=PIPE, stdout=PIPE)
ans = p.communicate(b'haha')[0]
if p.returncode != 0:
   raise Error

rawDescription: subprocess python Popen to open java jar

description: Subprocess python Popen to open java jar


code: [{'updated': 0, 'original': 1}, {'updated': 1, 'original': 2}, {'updated': 1, 'original': 3}, {'updated': 2, 'original': 4}]

rawDescription: Efficiently merge lists into a list of dictionaries


code: MyProperty.__get__ = lambda self,instance,owner: self.get(instance,owner)

rawDescription: Can you change a python descriptor's __get__ method at runtime?

description: Change a python descriptor's __get__ method at runtime


code:
фвыафыв
sdadf
试试
阿斯达а
阿斯顿飞

rawDescription: Replace given line in files in Python

description: Replace given line in files in Python


code:
return HttpResponse(json.dumps(your_data))
# I like to return success/fail Booleans, personally

rawDescription: Submit without refresh in django

description: Submit without refresh in django


code:
# do this outside of the first loop
lines = ax2.get_lines()
for line, subsm in zip(lines, subsl):
    line.set_label(subsm)

ax2.legend()

rawDescription: Labels on matplotlib within a loop

description: Labels on matplotlib within a loop


rawDescription: How to query the maximum of counts of related records satisfying a predicate

description: Query the maximum of counts of related records satisfying a predicate


code:
def __init__(self, url, icon):
    self.url = url
    self.icon = icon

rawDescription: Python - most convenient way to define constant variables used just once


rawDescription: Multiple Re.Subs Python

description: Multiple Re.Subs Python


code: (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),

rawDescription: Where to put a Django template's dependent files?


code:
import re
rstr = r"(?P<DEF_FUNC>def (?P<NAME_FUNC>\w+)\s*\((.*?)\):)|(?P<OTHERS>\w+)"
regex = re.compile(rstr)
regex.groupindex

rawDescription: Finding in regex pattern the name of possible groups


code: req = urllib2.Request('http://www.google.fr', headers=headers)

rawDescription: Problem making a GET request and spoof User-Agent in urllib2


Why does 9007199254740993 != 9007199254740993.0


code:
def cosine_distance(u, v):
    """
    Returns the cosine of the angle between vectors v and u. This is equal to
    u.v / |u||v|.
    """
    return numpy.dot(u, v) / (math.sqrt(numpy.dot(u, u)) * math.sqrt(numpy.dot(v, v)))

rawDescription: Simple implementation of N-Gram, tf-idf and Cosine similarity in Python


code:
for name in dir(cls):  # look at the names on the class
    if not name.startswith('_'):  # ignore names with leading _
        unbound_field = getattr(cls, name)  # get the value
        if hasattr(unbound_field, '_formfield'):  # make sure it's a field
            fields.append((name, unbound_field))  # record it

rawDescription: How does WTForms know to use validate_<field name> if it is defined to validate a field?


code:
data ='[["abc", "state", "fsf", "val"], ["pqr", "state", "efg", "val2"]]'
arrayOfArrays = JSON.parse(data);
console.log(arrayOfArrays);

rawDescription: python list of tuples to javascript array of arrays

description: Python list of tuples to javascript array of arrays


code:
groups = [list(chunk) for key, chunk in itertools.islice(chunks, num_chunks)]
result = pool.map(worker, groups)

rawDescription: Chunking data from a large file for multiprocessing?


code: nb.tab(1, state="normal")

rawDescription: Python 3 tkinter: dynamically set an element's state to "normal"?


code:
from django.db.models.loading import get_model
model = get_model('appname', 'ModelName')

rawDescription: Dynamically generate import in Python, specifically for a Django model