rawDescription: How can I get the index of next non-NaN number with series in pandas?

description: Get the index of next non-NaN number with series in pandas


code:
def minimum(a, b, li):
    min(itertools.islice(li, a, b + 1))

rawDescription: Inclusive range of list Python

description: Inclusive range of list Python


code: Image2_mask = ma.masked_array(Image2 > 0, Image2)

rawDescription: Overlay an image segmentation with numpy and matplotlib


code: result[idx == 0] = pd.NaT

rawDescription: Finding the closest date given a date in a groupby dataframe (Python)


rawDescription: Ndb default order does not preserve insertion order


rawDescription: Simple python program to read . gcode file


code:
def solve(smin, smax, coef1, coef2):
    """
    Return a list of lists of non-negative integers `n` that satisfy
    the inequalities,

    sum([coef1[i] * n[i] for i in range(len(coef1)]) > smin
    sum([coef2[i] * n[i] for i in range(len(coef1)]) < smax

    where coef1 and coef2 are equal-length lists of positive integers.
    """
    if smax < 0:
        return []

    n_max = ((smax-1) // coef2[0])
    solutions = []
    if len(coef1) > 1:
        for n0 in range(n_max + 1):
            for solution in solve(smin - n0 * coef1[0],
                                  smax - n0 * coef2[0],
                                  coef1[1:], coef2[1:]):
                solutions.append([n0] + solution)
    else:
        n_min = max(0, (smin // coef1[0]) + 1)
        for n0 in range(n_min, n_max + 1):
            if n0 * coef1[0] > smin and n0 * coef2[0] < smax:
                solutions.append([n0])
    return solutions

rawDescription: find the set of integers for which two linear equalities holds true

description: Find the set of integers for which two linear equalities holds true


code: list(interpolate([12, None, None, 17, 20, None, 28]))

rawDescription: Python series algorithm

description: Python series algorithm


code: df['pct_ch'] = df.groupby('product_desc')['prod_count'].pct_change() + 1

rawDescription: How to create rolling percentage for groupby DataFrame

description: Create rolling percentage for groupby DataFrame


code: r'some text you know (\d+) other text you also know'

rawDescription: Python and Regex - extracting a number from a string


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

def annotate_dim(ax,xyfrom,xyto,text=None):

    if text is None:
        text = str(np.sqrt( (xyfrom[0]-xyto[0])**2 + (xyfrom[1]-xyto[1])**2 ))

    ax.annotate("",xyfrom,xyto,arrowprops=dict(arrowstyle='<->'))
    ax.text((xyto[0]+xyfrom[0])/2,(xyto[1]+xyfrom[1])/2,text,fontsize=16)

x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
annotate_dim(plt.gca(),[0,0],[np.pi,0],'$\pi$')

plt.show()

rawDescription: Annotating dimensions in matplotlib

description: Annotating dimensions in matplotlib


rawDescription: Python: xticks and labels stuck on one side of the subplot


rawDescription: Fetching maximum tweets from twitter using Twython


code:
df['ln'] = df['l'] + df['n'].astype(str)
df.set_index(['g', 'ln'])['v'].unstack('ln')

rawDescription: reshaping a data frame in pandas

description: Reshaping a data frame in pandas


code: next(i for i in s.index[::-1] if s[i] != 0)

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


rawDescription: python variable length values in dictionary

description: Python variable length values in dictionary


code:
def locateGameDirs(filelist, root=os.curdir): #Find a list of files, return directories.
    toplevel = [folder for folder in os.listdir(root) if os.path.isdir(os.path.join(root, folder))] #List of top-level directories
    fileset = set(filelist)

    for path, dirs, files in os.walk(os.path.abspath(root)):

        curdir = path.split('\\')[1] #The directory os.walk is currently in.

        try: #Thrown here because there's a nonexistant(?) first entry.
            youarehere = toplevel.index(curdir)
            progress = int(((youarehere)/len(toplevel))*100)
        except:
            pass

        for filename in returnMatches(filelist, [k.lower() for k in files]):
            yield filename, path + "\\", progress

rawDescription: How to get progress of os.walk in python?

description: Get progress of os.walk in python


code:
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

# draw the black line
top_ax.axhline(0, color='k', zorder=-1)

# box x-coords and text labels
boxes = zip(np.arange(0.1, 1, 0.2), np.arange(0.2, 1, 0.2))
box_text = ('A1', 'B1', 'B2', 'A2')
# color indicators for boxes
colors = (0, 1, 1, 0)
# construct Rects
patches = [Rectangle(xy=(x0, -1), width=(x1-x0), height=2) for x0,x1 in boxes]
p = PatchCollection(patches, cmap='jet')
# this maps the colors in [0,1] to the cmap above
p.set_array(np.array(colors))
top_ax.add_collection(p)

# add text
[top_ax.text((x0+x1)/2., 1.2, text, ha='center')
    for (x0,x1), text in zip(boxes, box_text)]

# adjust ylims
top_ax.set_ylim(-2, 2)

rawDescription: How to plot heat map with matplotlib?

description: Plot heat map with matplotlib


code: from pkg import Class11, Class21, function21

rawDescription: Python - Can I import specific classes when importing all from a package


code:
target.write("%r\n%r\n%r") % (line1, line2, line3)
#                      --^

rawDescription: Why is target.write ignoring my %r formatting?


code:
from bs4 import BeautifulSoup
import urllib.request

url = 'http://www.bbc.co.uk/sport/football/teams/manchester-city/results/'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page)

for match in soup.select('table.table-stats tr.report'):
    team1 = match.find('span', class_='team-home')
    team2 = match.find('span', class_='team-away')
    score = match.abbr
    if not all((team1, team2, score)):
        continue

    print(team1.text, score.text, team2.text)

rawDescription: Get link text from HTML using beautifulsoup


code:
X(True).method()
X(False).method()

rawDescription: How to access "self" inside the scope of a class?

description: Access "self" inside the scope of a class


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['cumu'].plot(kind='line')

plt.show()

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


code:
def length(n):
    r = []
    for i in n:
        r.append(len(n))
    return r

rawDescription: Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings


code:
def close_geometry(self, geometry):
   if geometry.empty or geometry[0].empty:
       return geometry # empty

   if(geometry[-1][-1] == geometry[0][0]):
       return geometry  # already closed

   result = None
   for linestring in geom:
      if result is None:
          resultstring = linestring.clone()
      else:
          resultstring.extend(linestring.coords)

   geom = Polygon(resultstring)

   return geom

rawDescription: How to convert a GEOS MultiLineString to Polygon using Python?

description: Convert a GEOS MultiLineString to Polygon using Python


rawDescription: Need to add appointments into empty dictionary


code:
import json
import pandas
import requests
import xmltodict

web_request = requests.get(u'http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Basketball')

# Make that unweidly XML doc look like a native Dictionary!
result = xmltodict.parse(web_request.text)

# Next, convert the nested OrderedDict to a real dict, which isn't strictly necessary, but helps you
#   visualize what the structure of the data looks like
normal_dict = json.loads(json.dumps(result.get('pinnacle_line_feed', {}).get(u'events', {}).get(u'event', [])))

# Now, make that dictionary into a dataframe
df = pandas.DataFrame.from_dict(normal_dict)

rawDescription: Parsing through a deep-nested XML File in Python


rawDescription: Find if char precedes substring

description: Find if char precedes substring


code:
newstring = ' '.join(split[:5])+' ...'
font.getsize(newstring)
newstring

rawDescription: Python PIL: Replace Out-of-Range Text with Ellipsis


code: filename = next(glob.iglob(os.path.join(folder, name + '.*')))

rawDescription: Open existing file of unknown extension

description: Open existing file of unknown extension


code:
#if BUFSIZ < 8192
#define SMALLCHUNK 8192
#else
#define SMALLCHUNK BUFSIZ
#endif

rawDescription: Why is readlines() reading much more than the sizehint?


code:
import types
me.rebound = types.MethodType(unbound, me, Person)

rawDescription: Why isn't self being automatically passed to a method that is set on an object after its instantiation?


code: re.findall(r'[^/,]', a)

rawDescription: regular expression split also includes empty string result

description: Regular expression split also includes empty string result


rawDescription: Selecting rows from an HDFStore given list of indexes


code: root.cancel_after(a_timer) #cancel the timers call back

rawDescription: Timer or stopwatch in python

description: Timer or stopwatch in python


rawDescription: Printing a list with string.format rounding for float members


code:
import collections, os

link_ids = ['111','222','333']
filenames = ['111-foo.txt','111-bar.txt','222.txt']
File = collections.namedtuple("File", "fname fext") # named-tuple set-up

files = {File(*os.path.splitext(f)) for f in filenames}
# -> set([File(fname='222', fext='.txt'),
#         File(fname='111-bar', fext='.txt'),
#         File(fname='111-foo', fext='.txt')])

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


code:
0 ['PackageDto', ['fetName', 'a'], ['fetValue', 'b']]
1 ['PackageDto', ['fetName', 'c'], ['fetValue', 'd']]
2 ['PackageDto', ['fetName', 'd'], ['fetValue', 'z']]
3 ['PackageDto', ['fetName', 'f'], ['fetValue', 'Sq']]
4 ['PackageDto', ['fetName', 'g'], ['fetValue', 'p']]

rawDescription: Python Navigate Through Nested List Like Tree


code: out = bradley_roth_numpy(img, windowsize, threshold)

rawDescription: Bradley-Roth Adaptive Thresholding Algorithm - How do I get better performance?


code:
reader = csv.reader(idata.split("\r\n"))

stocks = []
for line in reader:
    if line == '':
        continue

    stock, price, volume, stime = line
    price = float(price)
    volume = int(volume)

    stocks.append((stock, price, volume, stime))

csr.executemany('INSERT INTO test.prices VALUES (?,?,?,?)', stocks)

rawDescription: Python change data into sequence

description: Python change data into sequence


rawDescription: Working with dialog box to download file from a site in python


code: [a for a in dir(obj) if not a.startswith('__')]

rawDescription: Iterate over object attributes in python


code:
import pandas, numpy
ewma = pandas.stats.moments.ewma
EMOV_n = ewma( ys, com=2 )

rawDescription: How to predict time series in scikit-learn?

description: Predict time series in scikit-learn


code:
from example import Base

class One(Base):
    hello = "world"

class Two(Base):
    foo = "bar"

arr = [One(), Two()]

arr[0].Enumerate()
arr[1].Enumerate()

rawDescription: How to get all class attr names from derived class using boost::python?

description: Get all class attr names from derived class using boost::python


code:
with open('textfile.lua','r') as my_file:
    line_nos = [i for i, line in enumerate(my_file) if line.lower().strip() == SentenceIMLookingfor]

rawDescription: Python: Comparing two strings that should be the same but that are not


code:
def get_context_data(self, **kwargs):
    context = super(ResultView, self).get_context_data(**kwargs)
    context["sales"] = self.get_queryset().aggregate(Sum('sales'))
    # Your variables are in GET, not POST
    context["company"] = self.request.GET.get("company")
    context["region"] = self.request.GET.get("region")
    return context

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

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


code:
import html.parser
html.parser.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:
today = date.today()  # datetime.date(2016, 4, 12)
a_date = date(2015,10,10)
today.month - a_date.month

rawDescription: Python Sqlalchemy: Filter by datediff of months


code:
@app.route('/index')
def index():
    posts = Post.query.order_by(Post.pub_date.desc())  #SQLAlchemy query.
    return render_template('index.html', posts=posts) #sends all posts to template.

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:
import re
s = "There is a CEP 30.160-0131 and a  30.160-0132 in that sentence, which repeats itself like there is a CEP 30.160-0131 and a  30.160-0132 in that sentence."
m = re.findall(r"CEP \d+\.\d+-\d+|(\d+\.\d+-\d+)", s)
print(m)

rawDescription: Python regex negative lookbehind not failing match


code: search = search.order_by(search_order)

rawDescription: Can I dynamically change order_by attributes in my query call?

description: Dynamically change order_by attributes in my query call


rawDescription: Python Pandas - Remove values from first dataframe if not in second dataframe


code:
f1= some_object.filter_with_prefix( 'foo', object )
self.customers = Fest.objects.filter(**f1)
f2= some_object.filter_with_prefix( 'bar', object )
self.customers = Fest.objects.filter(**f2)
f3= some_object.filter_with_prefix( 'baz', object )
self.customers = Fest.objects.filter(**f3)

rawDescription: Dynamic Django Filter with Prerequisite

description: Dynamic Django Filter with Prerequisite


code:
def save(self):
    image = Image.open(self.full_image_large)
    (w, h) = image.size
    for width, target in zip([900.0, 350.0, 120.0],
                             [self.full_image_large,
                              self.full_image,
                              self.thumbnail_image]):
        r = width/w
        im = image.resize((int(math.floor(r*w)),
                           int(math.floor(r*h))),
                           Image.ANTIALIAS)
        im.save(target)

    super(Artwork, self).save()

rawDescription: How to use Django to resize a user's image and upload to Amazon S3?

description: Use Django to resize a user's image and upload to Amazon S3


code:
source = Phonon.MediaSource("test.ogg")
player = Phonon.createPlayer(Phonon.MusicCategory, source)
player.play()

rawDescription: PyQt Phonon Audio Player

description: PyQt Phonon Audio Player


rawDescription: Python - Replacing characters in a list

description: Python - Replacing characters in a list


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

from mymodule import rm

import mock
import unittest

class RmTestCase(unittest.TestCase):

    @mock.patch('mymodule.os.path')
    @mock.patch('mymodule.os')
    def test_rm(self, mock_os, mock_path):
        # set up the mock
        mock_path.isfile.return_value = False

        rm("any path")

        # test that the remove call was NOT called.
        self.assertFalse(mock_os.remove.called, "Failed to not remove the file if not present.")

        # make the file 'exist'
        mock_path.isfile.return_value = True

        rm("any path")

        mock_os.remove.assert_called_with("any path")

rawDescription: Stubbing vs Mocking in Python

description: Stubbing vs Mocking in Python


code:
class MyHandler(tornado.web.RequestHandler):
     @gasync
     def get(self):
         # Parse the input data in some fashion
         data = get_data_from_request()

         # This could be anything using python sockets, urllib ...
         backend_response = send_data_to_backend(data)

         # Write data to HTTP client
         self.write(backend_response)

         # You have to finish the response yourself since it's asynchronous
         self.finish()

rawDescription: Making an asynchronous call synchronous in tornado


code:
import pyglet

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(vsync = False)
        self.sprites = {}
        self.sprites['testlabel'] = label = pyglet.text.Label("Hello World!",
                        color=(255,255,255,255),
                        font_size=36,
                        x=self.width//2, y=self.height//2, #self here, being pyglet.window.Window that we've inherited and instanciated with super().
                        anchor_x="center", anchor_y="center")
        self.alive = 1

    def on_draw(self):
        self.render()

    def render(self):
        self.clear()
        for sprite_name, sprite_obj in self.sprites.items():
            sprite_obj.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            # This is very important, this queries (and empties)
            # the pyglet event queue, if this queue isn't cleared
            # pyglet will hang because it can't input more events,
            # and a full buffer is a bad buffer, so we **NEED** this!
            event = self.dispatch_events()

win = Window()
win.run()

rawDescription: Pyglet hello world example doesn't show label until a key is pressed


code:
# http://norvig.com/sudoku.html

# from pprint import pprint

# concatenates letter and number to create cell
def cross(A, B):
    return [a + b for a in A for b in B]

def test():
    # 9 rows and 9 cols
    assert len(squares) == 81
    # row, col, and 9x9 square
    assert len(unitlist) == 27
    # each square 3 x 3
    assert all(len(units[s]) == 3 for s in squares)
    # row + col + square - repeats
    assert all(len(peers[s]) == 20 for s in squares)
    # should be row, column, and square for cell
    assert units['C2'] == [['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
                           ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'],
                           ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']]

    # Next assertion fails
    # pprint(peers['C2'])
    # peersc2 = ['A2', 'B2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'C1', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'A1',
    #            'A3', 'B1', 'B3']
    # print(list(set(peersc2) - set(peers['C2'])))

    # should be row, column, and square without repeats
    assert peers['C2'] == set(['A2', 'B2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2',
                            'C1', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9',
                            'A1', 'A3', 'B1', 'B3'])

digits = '123456789'
rows = 'ABCDEFGHI'
cols = digits
# makes each square
squares = cross(rows, cols)

# Creates arrays with each row 'A1', 'B1', 'C1', etc.
unitlist = ([cross(rows, c) for c in cols] +
            [cross(r, cols) for r in rows] +
            [cross(rs, cs) for rs in ('ABC', 'DEF', 'GHI') for cs in
             ('123', '456', '789')])
# assigns cell to all associated rows, columns, and squares
# ex) I5 > A5-I5 + I1-I9 + G4-G6, H4-H6, I4-I6
units = dict((s, [u for u in unitlist if s in u])
             for s in squares)
# creates the row and col pairs for each cell
# A1 = A1-I1 + A1-A9 + A1-A3, B1-B3, C1-C3
peers = dict((s, set(sum(units[s], [])) - set([s]))
             for s in squares)

test()

rawDescription: Assertion error failing - can't find a reason - Norvig Sudoku


code: df.to_pickle(file_name)  # where to save it, usually as a .pkl

rawDescription: How to store data frame using PANDAS, Python

description: Store data frame using PANDAS, Python


rawDescription: sorting a list of two columns based on one column python

description: Sorting a list of two columns based on one column python


code:
myfield = forms.CharField(
    widget=forms.TextArea(
        attrs={'class': 'some-class',
           'id': 'SomeId',
           'rows': '10',
           'style': 'margin-top: 10px;',
        }
    )
)

rawDescription: When to use a django form and when to use a html one?


code:
my_input = newsample.lower()
for item in set(my_input):
      print(my_input.count(item))
# the trick here is to iterate through the unique values and for each element,
# to count the item that appears in the _whole_ (now lowercase) input.

rawDescription: Count all characters, then print how many of which characters


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

def my_view(request, category):
    form = MyForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            feedback = form.save(commit=False)
            feedback.category = category
            feedback.save()
            return http.HttpResponse("Thanks for posting!")
    return render(request, "mytemplate.html", {'form': form})

# urls.py
(r'^(?P<category>\w+)/feedback/$', 'my_view')

# user can visit dev/feedback or support/feedback and the feedback will be tagged appropriately

rawDescription: Multiple Instances of a django app, does django support this


rawDescription: Getting the closest date to a given date


code:
def get_key(self):
    from uuid import uuid1
    return uuid1().hex[:20]

rawDescription: Amazon S3 and Cloudfront - Publish file uploaded as hashed filename


code:
int_obj = interface(root) # you need to create a variable to hold an instance of the interface you create. Otherwise, its immediately deleted.
int_obj.text(str1, str2)

rawDescription: Python 2.7 Tkinter classes

description: Python 2.7 Tkinter classes


rawDescription: Grouping data by frequency

description: Grouping data by frequency


code:
def isOK( word ):
    condition_one = len( word ) > 3 and word in words
    condition_two = not any( w.startswith( word ) for word in words )

    return not( condition_one or condition_two )

rawDescription: Python Logic Help:

description: Python Logic Help:


rawDescription: Suppressing scientific notation in pandas?


code:
fighters = [player, enemy]
fighters.sort(key=lambda f: f.speed)
for fighter in fighters:
    fighter.attack(fighters)

rawDescription: Do functions in ordered by values

description: Do functions in ordered by values


rawDescription: Python Output to File Higher Floating Point Precision


rawDescription: Python/Django: Schedule task in realtime


code: counts[np.arange(1000), row] += 1

rawDescription: Efficiently indexing numpy array with a numpy array


code:
import win32api

win32api.MessageBox(0, 'hello', 'title')

rawDescription: Alert boxes in Python?

description: Alert boxes in Python


code:
def bf_num_spaths(G):
    n_spaths = dict.fromkeys(G, 0.0)

    for source in G:
        for target in G:
            if source == target:
                continue
            for path in nx.all_shortest_paths(G, source, target):
                for node in path[1:]: # ignore firs element (source == node)
                    n_spaths[node] += 1 # this path passes through `node`

    return n_spaths

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


rawDescription: Printing an upside down triangle in Python 3.4.2?


rawDescription: Count the number of words in a string

description: Count the number of words in a string


code:
from itertools import takewhile
from collections import Counter

def modal(xs):
       counter = Counter(xs).most_common()
       _, count = counter[0]
       return takewhile(lambda x: x[1] == count, counter)

rawDescription: what is the most effective way to find the modal values of a list?

description: What is the most effective way to find the modal values of a list


code:
myJson = [
    {
        "name":"TopLevel folder",
        "id":"0",
        "type":"folder",
    },
    {
        "name":"MidLevel folder",
        "id":"1",
        "type":"folder",
        "parent":"0"
    },
    {
        "name":"Bottom Level folder",
        "id":"2",
        "type":"folder",
        "parent":"1"
    },
    {
        "name":"Vacation Picture",
        "id":"3",
        "type":"jpg",
        "parent":"2"
    },
]

rawDescription: Sorting JSON object(s) into a Hierarchy

description: Sorting JSON object(s) into a Hierarchy


code: test['tweet'].apply(lambda x: [item for item in x if item not in stop])

rawDescription: Python remove stop words from pandas dataframe


code:
def run(self):
    doc = self.curse(callback=self.render)
    self.render(doc)

def curse(self, callback=None):
    while self.cursor.alive:
        doc = self.cursor.next()
        callback(doc)

rawDescription: Where to put custom gen.Tasks in Tornado?


code:
from MoinMoin.util import pysupport

modules = pysupport.getPackageModules(__file__)

rawDescription: Is there a way to incorporate python code into moinmoin pages?


code: df["price"][df["signal"] == "S"]

rawDescription: List Comprehension Loop

description: List Comprehension Loop


code:
x = '''

'''
soup = bs4.BeautifulSoup(x)
print(soup.prettify(indent_width=3))

rawDescription: Custom indent width for BeautifulSoup .prettify()


code:
nrows = 8
df.loc[eval(" | ".join("df.A.shift(-{0}).lt(4)".format(n)
                       for n in range(1, nrows + 1))), 'new'] = 0

rawDescription: create new column that compares across rows in pandas dataframe

description: Create new column that compares across rows in pandas dataframe


code:
import builtins
dir(builtins)

rawDescription: Is the list of Python reserved words and builtins available in a library?


code: np.column_stack(np.broadcast([1, 2, 3], 0, [3, 2, 1]))

rawDescription: How to stack arrays and scalars in numpy?

description: Stack arrays and scalars in numpy


code: source /path/to/your/env/bin/activate

rawDescription: How to change the python version that a virtual environment uses in Ubuntu 14.04?

description: Change the python version that a virtual environment uses in Ubuntu 14.04


rawDescription: inequality comparison of numpy array with nan to a scalar

description: Inequality comparison of numpy array with nan to a scalar


code:
def sentence(text):
    iter_text = iter(text.split())
    # split text on spaces and create a single iterator from it

    def word(i_text, num_chars):
        """Helper function to return a list of the num_chars length
        pulled out of the iterator i_text"""

        return [next(i_text) for _ in range(num_chars)]
        # [int(next(i_text)) ... ] for your literal output, but since you're
        # encrypting as a string it seems more consistent to DECRYPT to a string

    return [word(iter_text, int(ch)) for ch in iter_text]

rawDescription: How do I recreate a nested list from a long string of information in Python?

description: Recreate a nested list from a long string of information in Python


code: "Content of odd line",Number1,Number2

rawDescription: Python - Parsing Conundrum

description: Python - Parsing Conundrum


code: wh = {bldg:(area**0.5, area**0.5) for (bldg, area) in sqft.items()}

rawDescription: What type of list is best for calling specific items?


code:
import StringIO
S=StringIO.StringIO()  # a file like string buffer
np.lib.npyio.format.write_array(S,a*3.3)
S.seek(0)   # rewind the string
np.lib.npyio.format.read_array(S)

rawDescription: how to read numpy 2D array from string?

description: Read numpy 2D array from string


rawDescription: Transform part of string in Python

description: Transform part of string in Python


code:
class Employee:

    def __init__(self, name , number , YearsWorked, Salary):
        self._Name = name
        self._StaffNr = number
        self._YearsWorked = YearsWorked
        self._Salary = Salary
        self._HolidayEntitlement = 0

    def print_employee_details(self):
        print ("Employee Name is {} working since past {} having salary {}").format(self._Name, self._YearsWorked, self._Salary)

rawDescription: python classes and values

description: Python classes and values


rawDescription: Hashing sequences with constraints

description: Hashing sequences with constraints


code:
x = (x > 9 and 0) or x
x = 0 or x
x =  x

rawDescription: Zero assignement in inline comparison

description: Zero assignement in inline comparison


rawDescription: Longest prefix of array equal to a value


code:
coords = {0: (146, 158, 140, 164),
          1: (180.5, 158, 174.5, 164),
          2: (209, 158, 215, 164)}
counter = 0

def printer(event):
    global counter
    if 130 <= event.x < 224 and 197 <= event.y < 305:
        if counter in coords:
            a, b, c, d = coords[counter]
            canvas.create_oval(a, b, c, d, outline="indianred2", fill="red", width=1.5)
        counter += 1

rawDescription: "If, Then" Statement - registering back-to-back mouse clicks


code: reader = CategorizedPlaintextCorpusReader('~/MainFolder/', r'.*\.txt', cat_pattern=r'(\w+)/*')

rawDescription: Creating a custom categorized corpus in NLTK and Python


code: str((x for x in range(3)))

rawDescription: Python FOR Loop in a list in a print function


code:
src="hello\n#foo\n#bar\n#baz\nworld\n"
def isplit(s, t): # iterator to split string s at character t
    i=j=0
    while True:
        try:
            j = s.index(t, i)
        except ValueError:
            if i<len(s):
                yield s[i:]
            raise StopIteration
        yield s[i:j]
        i = j+1
'\n'.join(x for x in isplit(src, '\n') if x[0]!='#')

rawDescription: How do I split a string and rejoin it without creating an intermediate list in Python?

description: Split a string and rejoin it without creating an intermediate list in Python


code:
for i in b:
    if a_app.has_key(i) and a_app[i] > 0:
        a_app[i] -= 1

rawDescription: Compare values of two arrays in python

description: Compare values of two arrays in python


code:
from collections import Counter
def solve(lis):
    c = Counter(lis)
    mx = c.most_common()[0][1]
    #or mx = max(c.values())
    return [k for k, v in c.items() if v == mx]

print (solve(['a','b','c','b','d','a']))
print (solve(['a','a','b','b','c','c','d']))
print (solve(['a','a','a','b','b','b','c','c','d','d','d'] ))

rawDescription: All the common elements present in the List of Python


code:
from sklearn.cross_validation import KFold
cv = KFold(180000, 9)

rawDescription: Grabbing two different sections of a list


code:
menu = [
    "Encrypt message",
    "Decrypt message",
    "Secure Encrypt Message",
    "Exit",
    ]
for n, x in enumerate(menu, start=1):
    print("{}) {}".format(n, x))
selection = input("Please select: ")

rawDescription: How to stop a dictionary shuffling? (Python)

description: Stop a dictionary shuffling? (Python)


code:
from collections import Counter
lines = []
with open('foo.data', 'r') as foo:
    lines = foo.readlines()
c = Counter([l.split(" ")[0] for l in lines])
c

rawDescription: How to count a set of strings in a file in Python?

description: Count a set of strings in a file in Python


code: from inclusive import irange as range, ixrange as xrange

rawDescription: Is it OK to re-use the names of Python's built-in functions?


rawDescription: Python not all in operation

description: Python not all in operation


code:
countries = {
    'Japan': ['brown', 'cow', 'around'],
    'Korea': ['black', 'chicken', '344'],
    'China': ['grey', '3411', 'leaf'],
    'Indonesia': ['red', 'green', '']
}

rawDescription: How to return a list of items from a different py file

description: Return a list of items from a different py file


code:
profile = User.get_profile()
denmark = Country.objects.get(name = 'Denmark')
profile.countries.add(denmark)

rawDescription: Country-based Super User Access

description: Country-based Super User Access


code:
import time
time.asctime()
asctime = 4253
time.asctime() # Notice that asctime here is unaffected as its inside the time module(s) namespace

rawDescription: Using function names as variables in python


code:
import datetime as DT
import dateutil.rrule as RR
today = DT.date.today()

aset = RR.rruleset()
aset.rrule(RR.rrule(RR.WEEKLY, byweekday=RR.TH, byhour=18, count=3, dtstart=today))
aset.rrule(RR.rrule(RR.WEEKLY, byweekday=RR.SA, byhour=10, count=3, dtstart=today))
for date in aset:
    print(date)

rawDescription: python-dateutil - RRule - Different times for different weekdays

description: Python-dateutil - RRule - Different times for different weekdays


code:
import scipy as sc
D = sc.array([0.133e-4,0.243e-4,0.283e-4])
dx = sc.sqrt(D*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:
files = ["file1","file2","file3"]

with open("output_file","w") as outfile:
    with open(files[0]) as f1:
        for line in f1:        #keep the header from file1
            outfile.write(line)

    for x in files[1:]:
        with open(x) as f1:
            for line in f1:
                if not line.startswith("#"):
                    outfile.write(line)

rawDescription: concatenate several file remove header lines

description: Concatenate several file remove header lines


code:
for i in _search(_database_variants, _search_regions):
    print(i)

rawDescription: Matching values in nested dictionaries

description: Matching values in nested dictionaries


rawDescription: adding text ticklabels to pcolor heatmap

description: Adding text ticklabels to pcolor heatmap


code: lst = [v for i, v in enumerate(lst) if i % 2 == 0]

rawDescription: Remove odd elements from list in Python

description: Remove odd elements from list in Python


rawDescription: What if Chinese strings shown in the pandas DataFrame in Python


code:
def split_curve(x, y, npts):
    from scipy.interpolate import interp1d
    f = interp1d(x, y)
    xnew = np.linspace(x.min(), x.max(), num=npts, endpoint=False)
    ynew = f(xnew)
    return zip(xnew[1:], ynew[1:])

rawDescription: Split numpy array into similar array based on its content


code:
def numeric_prefix(s):
    n = 0
    for c in s:
        if not c.isdigit():
            return n
        else:
            n = n * 10 + int(c)
    return n

rawDescription: removing non-numeric characters from a string

description: Removing non-numeric characters from a string


code:
import re

with open('file.js', 'r') as f:
    content = f.read()

functions = re.findall(r'(function\s.+?\(.*?\)\n.+?\n\})', content, re.DOTALL)
for i, func in enumerate(functions):
    with open('func{}.js'.format(i), 'w') as f:
        f.write(func)

rawDescription: Read pattern from file and write to another file using python


code:
import csv
import xml.etree.ElementTree as ET
tree = ET.parse('/tmp/test123.xml')
root = tree.getroot()
doc = open('/tmp/output123.csv', 'w')
for child in root:
    for country in root.findall(str(child.tag)):
        rec = '%s,%s,%s' %(str(child.tag), str(country.find('rank').text), str(country.find('gdppc').text))
    for neighbor in country.findall('neighbor'):
        rec= rec+ ',' + str(neighbor.attrib['name']) +',' + str(neighbor.attrib['direction'])
    doc.write(rec + '\n')
doc.close()

rawDescription: How to convert xml into a strucutured csv

description: Convert xml into a strucutured csv


code:
from scipy import spatial
tree = spatial.KDTree(arr)
distances, indices = tree.query([4, 0])
tree.data[indices]

rawDescription: Search for closest value in list of lists in python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(external_sites_html)
# Now we can get the tags of the external site from the soup variable.
title = soup.title.string

rawDescription: Get Title and Description of external URL using Django


code:
X=np.ones((10,10),int)*2
y=np.eye(3)
I,J=np.nonzero(y)
X[I+3,J+2]=y[I,J]
X

rawDescription: Scipy sparse matrix assignment using only stored elements


code:
r = np.arange(0.1,100.01,0.01)
#r and xvals are numpy arrays, so numpy functions can be applied in one step
xvals=(2.0 - B(rb1,drs1) ) * r / rb1 + B(rb1,drs1)
P1=2*xvals+np.log1p(np.exp(-2*xvals))

rawDescription: Floating point problems in asymptotic functions approaching zero - Python


code:
from nltk.tokenize import sent_tokenize

paragraph = "How does chickens harden over the acceptance? Chickens comprises coffee. Chickens crushes a popular vet next to the eater. Will chickens sweep beneath a project? Coffee funds chickens. Chickens abides against an ineffective drill."
highlights = ["vet","funds"]
sentencesWithHighlights = []

rawDescription: Finding the surrounding sentence of a char/word in a string


code:
class Foo(object):
    def __init__(self):
        self.bar = 1
    @classmethod
    def Copies(cls, n):
        return [cls() for i in xrange(n)]

Foo()
Foo.Copies(3)

rawDescription: Creating a list of objects in Python constructor


rawDescription: Fill missing values in a list of dictionaries with months and years


code:
def sublist(lst1, lst2):
   ls1 = [element for element in lst1 if element in lst2]
   ls2 = [element for element in lst2 if element in lst1]
   return ls1 == ls2

rawDescription: Checking if list is a sublist

description: Checking if list is a sublist


code:
x
y
x_new
y_new_mean

rawDescription: Python Numpy: Replace duplicate values with mean value


code:
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip,
    QPushButton, QApplication)
from PyQt5.QtGui import QFont

app = QApplication(sys.argv)

w = QWidget()

QToolTip.setFont(QFont('SansSerif', 10))
w.setToolTip('This is a QWidget widget')

btn = QPushButton('Button', w)
btn.setToolTip('This is a QPushButton widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)

w.setGeometry(300, 300, 300, 200)
w.setWindowTitle('Tool Tips')
w.show()

sys.exit(app.exec_())

rawDescription: Why Tkinter can be procedural and PyQt can only be used just with the OO paradigm?


code:
def all_prod(s):
    return (''.join(v) for u in chain(product(s, repeat=i) for i in range(len(s)+1)) for v in u)

rawDescription: using python itertools to generate custom iteration

description: Using python itertools to generate custom iteration


code:
import operator
class CarManager(models.Manager):
    def get_query_set(self):
        auths = super(CarManager, self).get_query_set().all().order_by('-creation')
        ordered = sorted(auths, key=operator.attrgetter('birthday'))
        return ordered

class Car(models.Model):
    sorted = CarManager()

rawDescription: Django custom order_by

description: Django custom order_by


code:
def density_estimation(m1, m2):
    X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions = np.vstack([X.ravel(), Y.ravel()])
    values = np.vstack([m1, m2])
    kernel = stats.gaussian_kde(values)
    Z = np.reshape(kernel(positions).T, X.shape)
    return X, Y, Z

rawDescription: Pyplot Scatter to Contour plot

description: Pyplot Scatter to Contour plot


rawDescription: Operators to measure difference in Python


code:
import itertools
len(list(itertools.combinations_with_replacement(range(1, 10), 5)))

rawDescription: Count numbers in range which sum of digits is same as product


code:
def __lt__(self, other):
    column = self.treeWidget().sortColumn()
    k1 = self.text(column)
    k2 = other.text(column)
    return _human_key(k1) < _human_key(k2)

rawDescription: Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator


code: gen = (x for x in lists if 5000 < x[0] < 10000)

rawDescription: minimum of list of lists

description: Minimum of list of lists


code:
import functools

class MyDB(object):

    submodule_methods = {}

    def __init__(self):
        for k,v in self.submodule_methods.items():
            self.__dict__[k] = SubmodulePointer(self,v)

        self.db_stuff = "test"

class SubmodulePointer(object):

    def __init__(self,db,methods):
        self.db = db
        for name,func in methods.items():
            self.__dict__[name] = functools.partial(func,db)

rawDescription: Access parent object from submodule

description: Access parent object from submodule


code:
if any(s in mystring for s in ('foo', 'bar', 'hello')):
    pass

rawDescription: Is there any nicer way to write successive "or" statements in Python?


code:
az= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
az= az[:17]+'11'+az[19:]
az

rawDescription: change 2 byte in a string

description: Change 2 byte in a string


code: img

rawDescription: Why ndimage.label(image) labels extra shapes among the considered ones?


code: ordering = {word: i for i, word in enumerate(predefined_list)}

rawDescription: python sort list based on key sorted list

description: Python sort list based on key sorted list


rawDescription: How to get all mappings between two lists?

description: Get all mappings between two lists


code:
script_thread = None

def startScript():
    global script_thread
    # If we don't already have a running thread, start a new one
    if not script_thread:
        script_thread = MyScript()
        script_thread.start()

def stopScript():
    global script_thread
    # If we have one running, stop it
    if script_thread:
        script_thread.stop()
        script_thread = None

rawDescription: Managing script execution using GUI "buttons" from Tkinter module


code:
df = pd.DataFrame([[1,np.nan,np.nan],[np.nan,np.nan,np.nan],[np.nan,6,np.nan]])
np.where(~np.isnan(df))

rawDescription: Retrive the (index,column) tuple of non nan values


rawDescription: Python: Boolean List to Binary String

description: Python: Boolean List to Binary String


code:
import csv

validRows=[]
with open('data', 'r') as f:
    reader=csv.reader(f, delimiter=' ')
    for row in reader:
        if all([item.isdigit() for item in row]):
            validRows+=[row]
print(validRows)

rawDescription: keeping track of valid rows in csv file

description: Keeping track of valid rows in csv file


code: LOGIN_REDIRECT_URL = 'account-redirect'

rawDescription: Django - Login and redirect to user profile page


code: args=('%(logfilename)s',)

rawDescription: Python change logfile location at runtime


code:
import re
p = re.compile(r'query(?!\s*(?:management|resolution))')
test_str = "The query resolution with this query management should not get highlighted. Only this query should get highlighted.The query resolution with this query management should not get highlighted. Only this query should get highlighted."
m = p.search(test_str)
if m:
    print(m.group())

rawDescription: Multiple Regex Matches Using Negative LookAhead


code:
import regex
s = '\U0001f680\U0001f618\U0001f44d\U0001f3fe\U0001f1e6\U0001f1ee'
import unicodedata as ud
ud.category(s[0])
ud.category(s[1])
ud.category(s[2])
ud.category(s[3])
ud.category(s[4])
ud.category(s[5])
regex.findall(r'\p{So}\p{Sk}*',s)

rawDescription: Split a string of Emojis into single Emoji character


code:
class MemoizeJac(object):
    """ Decorator that caches the value gradient of function each time it
    is called. """
    def __init__(self, fun):
        self.fun = fun
        self.jac = None
        self.x = None

    def __call__(self, x, *args):
        self.x = numpy.asarray(x).copy()
        fg = self.fun(x, *args)
        self.jac = fg[1]
        return fg[0]

    def derivative(self, x, *args):
        if self.jac is not None and numpy.alltrue(x == self.x):
            return self.jac
        else:
            self(x, *args)
            return self.jac

rawDescription: How to return cost, grad as tuple for scipy's fmin_cg function

description: Return cost, grad as tuple for scipy's fmin_cg function


code:
data = {}

with open('filename') as f:
    for line in f:
        row = line.split()
        data[row[0]] = row[1]

rawDescription: Extracting data from a text file

description: Extracting data from a text file


code: blacklist = {'steve', 'richard'}

rawDescription: Delete items from a list if the item contains an entry in a blacklist


code:
list = [1, 2, 3]
l2 = list_replace(list, [3], [3, 4, 5])
print('Changed: {0}'.format(l2))
print('Original: {0}'.format(list))

list = [1, 2, 3, 3, 3]
l2 = list_replace(list, [3], [8, 8], 2)
print('Changed: {0}'.format(l2))
print('Original: {0}'.format(list))

list = [1, 2, 3, 3, 3]
l2 = list_replace(list, [2, 3], [8, 8], 2)
print('Changed: {0}'.format(l2))
print('Original: {0}'.format(list))

rawDescription: Python: Find-replace on lists

description: Python: Find-replace on lists


rawDescription: Can I plot several histograms in 3d?

description: Plot several histograms in 3d


rawDescription: Python: Efficiently calling subset variables of multiple returns function


code:
with open('text_output.txt', 'w') as fout:
    subprocess.Popen(cmd, stdout=fout,
                          stderr=subprocess.PIPE,
                          stdin=subprocess.PIPE)

rawDescription: Running shell script on flask

description: Running shell script on flask


code:
s = simulation
s.addGroup("teapotarmy")
...

rawDescription: Calling multiple functions from a class in Python without repeating the class name every time


code: y = fltr(x, ['dropdown_value', 'nm_field', 'url_app', 'dt_reg'])

rawDescription: Python filter nested dict given list of key names


code:
def permutateIndexes(array, perm):
    return array[perm][:, perm]

rawDescription: Permuting numpy's 2d array indexes

description: Permuting numpy's 2d array indexes


code:
import gc
import othermod

f = othermod.Foo()

objs = gc.get_objects()
# print(len(objs))
# 3519

print(len([obj for obj in objs if isinstance(obj,othermod.Foo)]))
# 2

rawDescription: Access all instances of a class while debugging


rawDescription: get slice from multi-dimensions in python

description: Get slice from multi-dimensions in python


code:
WordLenLi = []
for word in st.split(): #for each word in a list of words
    WordLenLi.append(len(word)) #insert the length of the word into WordLenLi

rawDescription: how to implement a list of paragraph word lengths using a for loop

description: Implement a list of paragraph word lengths using a for loop


code:
from collections import defaultdict
import csv

def d1(): return defaultdict(int)
def d2(): return defaultdict(d1)
def d3(): return defaultdict(d2)
dictionary = d3()

with open('input.csv') as input_file:
    next (input_file);
    for line in csv.reader(input_file):
        dictionary[line[0]][line[1]][line[2]] += 1

assert dictionary['Feature1']['host1']['lichost1'] == 2
assert dictionary['Feature1']['host2']['lichost1'] == 1
assert dictionary['Feature2']['host1']['lichost2'] == 1
assert dictionary['InvalidFeature']['host1']['lichost1'] == 0

rawDescription: Pythonic way to update nested dictionaries in Python


rawDescription: How can I add an additional row and column to an array?

description: Add an additional row and column to an array


code:
import pandas as pd
df = pd.DataFrame(data = {"v1":v1, "v2":v2}, index=totalIDs)
df.ix[subsampleIDs]

rawDescription: doing better than numpy's in1d mask function: ordered arrays?

description: Doing better than numpy's in1d mask function: ordered arrays


code:
with A() as a:
    with B() as b:
        suite

rawDescription: how to write results in a for loop to multiple CSV files

description: Write results in a for loop to multiple CSV files


code:
class Meta(type):
    def __new__(cls, clsname, clsbase, clsdict):
        newcls = super(Meta, cls).__new__(cls, clsname, clsbase, clsdict)
        def custom_repr(self):
            return '{}, Custom __repr__'.format(clsname)
        newcls.__repr__ = custom_repr
        return newcls

class Foo(object):
    __metaclass__ = Meta

rawDescription: Can one modify the default repr behavior through a metaclass?


code:
select([True if roles_table.c.is_admin or roles_table.c.id == 1 else False],
       roles_table.c.id == persons_table.c.role_id)

rawDescription: Sqlalchemy select condition

description: Sqlalchemy select condition


code:
# /scheduler.py

from scrapy.core.scheduler import Scheduler

class MyScheduler(Scheduler):

    def enqueue_request(self, request):
        if not request.dont_filter and self.df.request_seen(request, self.spider):
            self.df.log(request, self.spider)
            return False
        dqok = self._dqpush(request)
        if dqok:
            self.stats.inc_value('scheduler/enqueued/disk', spider=self.spider)
        else:
            self._mqpush(request)
            self.stats.inc_value('scheduler/enqueued/memory', spider=self.spider)
        self.stats.inc_value('scheduler/enqueued', spider=self.spider)
        return True

rawDescription: Scrapy - Retrieve spider object in dupefilter


code:
with open(path, 'r') as f:
    N = int(input('What line do you need? > '))
    print("This is the line.")
    print(next(itertools.islice(csv.reader(f), N, None)))

rawDescription: Best way to access the Nth line of csv file

description: Access the Nth line of csv file


code:
class A(ComplexModel):
    i = Integer

class B(A):
    s = Unicode

class C(A):
    d = DateTime

rawDescription: How do you @rpc _returns polymorphic types in spyne?


code: result = test.split(' ')[4]

rawDescription: Split string in the Python script

description: Split string in the Python script


rawDescription: Equivalent of named tuple in NumPy?

description: Equivalent of named tuple in NumPy


rawDescription: Check a generated string against a preset string in python


code: num = int(your_str, 8)

rawDescription: Most pythonic way to convert a string to a octal number


rawDescription: Lock free read only List in Python?

description: Lock free read only List in Python


code:
table2 = pd.to_datetime(table2)
idx = table2.searchsorted(df['Date'].values)
df['New'] = table2[idx]
print(df)

rawDescription: Complicated datetime merge pandas

description: Complicated datetime merge pandas


code:
my_lst = [0 for _ in range(15)]
candidates = list(range(15))
random.shuffle(candidates)

for i in candidates[0:5]:
    my_lst[i] = 1

rawDescription: Random.randint on lists in Python

description: Random.randint on lists in Python


code: import numpy as np

rawDescription: By dumping a python list, creating a python ".py" file which returns the same python list


rawDescription: Append length of string for each string in list


code:
class Machine(dict):
    def __init__(self):
        dct = {'poweron':'OUTP ON', 'home' : 'MV 0, 0', 'identify': '*IDN?'}
        super(Machine, self).__init__(dct)
    def __dir__(self):
        return sorted(self)
    def __getattr__(self, k):
        if k in self:
            return self[k]
        raise AttributeError(k)
    def __setattr__(self, k, v):
        if k in self:
            self[k] = v
        super(Machine, self).__setattr__(k, v)

m = Machine()
m.home
m.home = 'new value'
m.home

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


code:
import functools
class SubClass(TopClass):

    @property
    @functools.lru_cache()
    def thing(self):
        thing = get_some_thing_from_db('thing')
        if not thing:
            raise AttributeError()
        return TheThing(thing)

rawDescription: Proper way of using properties with try and except


code:
def get_foo(self):
    if not self._foo:
        self._foo = Bar()
    return self._foo

rawDescription: What's Python equivalent to or equals expression, to get return foo or foo = 'bar' working?


code:
from string import punctuation

count = 0
for word in speech.lower().split():
    word = word.strip(punctuation)
    if word in contractions:
        count += 1

rawDescription: Count occurrences of elements in string from a list?


code:
[
    {'score': u'? - ?', 'team1': u'Atletico Madrid', 'team2': u'Malmo FF'},
    {'score': u'? - ?', 'team1': u'Olympiakos', 'team2': u'Juventus'},
    {'score': u'? - ?', 'team1': u'Liverpool', 'team2': u'Real Madrid'},
    {'score': u'? - ?', 'team1': u'PFC Ludogorets Razgrad', 'team2': u'Basel'},
    ...
]

rawDescription: BeautifulSoup data scrapping and database


rawDescription: convert multiple csv to xls using python with number stored as integer not text

description: Convert multiple csv to xls using python with number stored as integer not text


code:
num1 = float(raw_input("What will be your first number?:\n"))
num2 = float(raw_input("What will be your second number?:\n"))
Q_3 = raw_input("What will be your operation?:\n")
if Q_3 in ("+", "-", "/", "*", "%", "**"):
    print(eval("%s %s %s" % (num1, Q_3, num2)))

rawDescription: Simple Python Calculator Challenge 2.7

description: Simple Python Calculator Challenge 2.7


code: True

rawDescription: What is the most efficient way to search nested lists in python?


code:
from statistics import mean

def ordered_cluster(data, max_diff):
    current_group = ()
    for item in data:
        test_group = current_group + (item, )
        test_group_mean = mean(test_group)
        if all((abs(test_group_mean - test_item) < max_diff for test_item in test_group)):
            current_group = test_group
        else:
            yield current_group
            current_group = (item, )
    if current_group:
        yield current_group

data = [4.1, 4.05, 4.14, 4.01, 3.97, 4.52, 4.97, 5.02, 5.05, 5.2, 5.18, 3.66, 3.77, 3.59, 3.72]

print(list(ordered_cluster(data, 0.5)))

rawDescription: Identifying groups of similar numbers in a list


code:
fobj_in = open('in_file.txt')
fobj_out = open('out_file.txt', 'w')

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


rawDescription: How to parse the xml file in the google-blogger in python?

description: Parse the xml file in the google-blogger in python


rawDescription: Pandas pivot or groupby for dynamically generated columns


code:
import matplotlib.pyplot as plt
plt.scatter(ser.index, ser)
plt.show()

rawDescription: How to Scatter Plot Series using Pandas

description: Scatter Plot Series using Pandas


code:
import sys
print(sum(map(int, sys.argv[1:])))

rawDescription: I want to create python script file: i am new to it


code:
for each_line in results_f:
    (name,score) = each_line.split()
    scores.append(float(score))
    surfers.append(name)

rawDescription: Python : The second for loop is not running


code: a,b,c,d,e = 'every good boy does fine'.split()

rawDescription: list items to its own variable

description: List items to its own variable


rawDescription: Django: dynamic template rendering

description: Django: dynamic template rendering


code:
process = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.communicate(br"\u0061")

rawDescription: Using subprocess to get output

description: Using subprocess to get output


code:
import csv
import os.path
with open("C:/*my dir*/UCSC to Ensembl.csv", "rb") as f:
    reader = csv.reader(f, delimiter = ',')
    for row in reader:
        graph_filename = os.path.join("C:/folder", row[0] + "_nt_counts.txt")
        if os.path.exists(graph_filename):
            print (row[1])

rawDescription: Python reading files in a directory

description: Python reading files in a directory


code: cancel(grad)

rawDescription: Smart rewriting of expressions in sympy

description: Smart rewriting of expressions in sympy


code:
import numpy as np

MP_TICKSIZE = np.random.rand(len(plt.gca().xaxis.get_major_ticks())) * 30.0

rawDescription: change fontsize of each tick of one axis with python matplotlib

description: Change fontsize of each tick of one axis with python matplotlib


code:
class Student:
    def __init__(self, name):
        self.name = name

class ClassRoom:
    def __init__(self, students):
        self.students = students

john, sam = Student('John'), Student('Sam')
patrick, michael, bill = Student('Patrick'), Student('Michael'), Student('Bill')

klass1 = ClassRoom([john, sam])
klass2 = ClassRoom([patrick, michael, bill])

def where_is(student, klasses):
    return next((x for x in klasses if student in x.students), None)

assert klass1 is where_is(john, [klass1, klass2])
assert klass2 is where_is(patrick, [klass1, klass2])

nobody = Student('foo')
assert None is where_is(nobody, [klass1, klass2])

rawDescription: Pythonic way to yield / return main item within a sub loop


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

rawDescription: Storing and Accessing node attributes python networkx


rawDescription: building full path filename in python,

description: Building full path filename in python,


rawDescription: parsing a dns configuration file with python

description: Parsing a dns configuration file with python


code:
import re
try:
    from urllib2 import urlopen
    from urlparse import urljoin
except ImportError: # Python 3
    from urllib.parse import urljoin
    from urllib.request import urlopen

from bs4 import BeautifulSoup, SoupStrainer # pip install beautifulsoup4

url = "http://site.com"
only_links = SoupStrainer('a', href=re.compile('somepage'))
soup = BeautifulSoup(urlopen(url), parse_only=only_links)
urls = [urljoin(url, a['href']) for a in soup(only_links)]
print("\n".join(urls))

rawDescription: How to find and extract link from webpage?

description: Find and extract link from webpage


code:
dd.index.date

array([datetime.date(2007, 1, 1), datetime.date(2007, 1, 1),
       datetime.date(2007, 2, 1), datetime.date(2007, 2, 1),
       datetime.date(2008, 1, 1), datetime.date(2008, 1, 1),
       datetime.date(2008, 2, 1), datetime.date(2008, 2, 1)], dtype=object)

dd.index.month
array([1, 1, 2, 2, 1, 1, 2, 2])

rawDescription: Average of a groupby or a groupby of an average on a dataframe with duplicated indexes


code:
con = pyodbc.connect("DRIVER={SQL Server}; SERVER=my_server_ip; DATABASE=db_name; UID=user_id; PWD=password; CHARSET=UNICODE")
cur = con.cursor()

rawDescription: Can I get data from my MSSql server and upload it to app engine via some python scripts?

description: Get data from my MSSql server and upload it to app engine via some python scripts


code:
#!/usr/bin/env python2
import json
import sys

for line in sys.stdin:
    json.dump(line.split(), sys.stdout) # split on any whitespace
    print

rawDescription: Python multithreading - writing to file is 10x slower


code: bisect.insort(times, 25)

rawDescription: Efficiently associating a single value with an interval


code:
def substrings_in_set(s, words):
    if s in words:
        yield [s]
    for i in range(1, len(s)):
        if s[:i] not in words:
            continue
        for rest in substrings_in_set(s[i:], words):
            yield [s[:i]] + rest

rawDescription: Breaking a string into individual words(Python)


code:
from functools import partial, reduce
self_compose = lambda cb,n: partial (reduce, lambda v,cb: cb(v), [cb]*n)

rawDescription: Python lambda self composing function: How to return a function that is repeated n amount of times?


code:
soup = BeautifulSoup(requests.get("http://oilprice.com/Latest-Energy-News/World-News").content)

divs = soup.find_all("div","categoryArticle__content")
print([(d.find("p", {"class": "categoryArticle__meta"}).text.split("|")[0].strip(), d.a["href"]) for d in divs])

rawDescription: Python and beautifulSoup: scraping two values - url and date and having the return results paired


rawDescription: How to create a big file quickly with Python

description: Create a big file quickly with Python


code:
import collections

counts = collections.Counter()
for row in my_reader:
    counts[row[3]] += 1

rawDescription: Count numbers from row in CSV file

description: Count numbers from row in CSV file


code:
s = 'This is a strange sentence.    There are too many spaces.And.Some periods are not.  placed      properly.'
re.sub(r'(?<!\.)\s+', ' ' ,re.sub(r'\.\s*(?!$)', r'.  ', s))

rawDescription: Python (2.7) - Replacing multiple patterns in a string using re


code:
def find_key(input_dict, value):
    return next((k for k, v in input_dict.items() if v == value), None)

find_key({1:'a', 2:'b', 3:'c', 4:'d'}, 'b')
find_key({1:'a', 2:'b', 3:'c', 4:'d'}, 'z') is None
def find_key(input_dict, value):
    return {k for k, v in input_dict.items() if v == value}

find_key({1:'a', 2:'b', 3:'c', 4:'d'}, 'b')
find_key({1:'a', 2:'b', 3:'c', 4:'d', 5:'b'}, 'b')
find_key({1:'a', 2:'b', 3:'c', 4:'d'}, 'z')

rawDescription: return key by value in dictionary

description: Return key by value in dictionary


code:
set(ratings.map(lambda r: r.product).collect())
## {1, 2, 3, 4, 5}

set(ratings.map(lambda r: r.user).collect())
## {1, 2, 3, 4}

rawDescription: Spark ALS predictAll returns empty

description: Spark ALS predictAll returns empty


code: db.session.query(Foo.Foo_no, Foo.props1).join(Bar, Foo.bar_no == Bar.bar_no).filter(Bar.status == filters['status']).all()

rawDescription: Sql filter query with join objects

description: Sql filter query with join objects


code:
Counter.objects.get_or_create(name = name)
Counter.objects.filter(name = name).update(count = F('count')+1)

rawDescription: Atomic increment of a counter in django

description: Atomic increment of a counter in django


code: rna.append(dna_rna.get(char, 'n'))

rawDescription: How do I replace characters in a string using a character map?

description: Replace characters in a string using a character map


code:
def __setitem__(self, key, item):
    if type(item) is list:
        self.data[key] = NotifyList(item, self, str(key)) # Don't forget the new item's parent
    else:
        self.data[key] = item
    self.alertParent(str(key), str(item)) # Report up the chain instead of printing

rawDescription: How to detect what element of a nested list has changed? (python)

description: Detect what element of a nested list has changed? (python)


code: myButton = Button(master, text='Enter', command=enter_button)

rawDescription: Write Input from Entry Widget into CSV (tkinter)


code:
list0 = ["text","text","","","text","","text","text","","text"]

# Find the "gap" - the first consectutive empty strings
# gap_pos remains 0 if no gap is found
gap_pos = 0
gap_size = 2
for i in range(len(list0)-gap_size):
    if all(x == '' for x in  list0[i:i+gap_size]):
        gap_pos = i+1
        break # remove this if you want the last gap

# Find the non-empty strings that are detected after the gap
after_gap = filter(lambda x : x != '', list0[gap_pos+1:])

# allocate this group starting at a specific index (e.g. index 5)
specific_index = 5
for i in range(len(after_gap)):
    allocate_at = i + specific_index
    # Make sure not to go out-of-bounds
    if allocate_at < len(list0):
        list0[allocate_at] = after_gap[i]

rawDescription: find an empty value gap in a list and allocate a group of strings

description: Find an empty value gap in a list and allocate a group of strings


rawDescription: Constant time random selection and deletion


rawDescription: How to add to dictionaries, but not dictionaries - Python

description: Add to dictionaries, but not dictionaries - Python


rawDescription: Python List Indexing and Returning the Item


code: RDCW_CALLBACK_F = ctypes.WINFUNCTYPE(None, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.POINTER(OVERLAPPED))

rawDescription: Watchdog compatibility: A workaround for "CancelIoEx"


code:
list_of_lists[0][0] = 7
list_of_lists

rawDescription: Can I put a tuple into an array in python?

description: Put a tuple into an array in python


code: foo.update({'url': d.get('url'), 'users.user': 'tommy'}, {'$inc': {'users.$.value': 1}})

rawDescription: Correcting database structure with MongoDB


code:
from pygame import *
from pygame.locals import *
from math import *

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

init()

screen = display.set_mode((800,600))

clock = time.Clock()

hours = 5
minutes = 59
seconds = 50

running=True
while running:

    # events

    for e in event.get():
        if e.type == QUIT:
            running = False

    # (variable) modifications

    seconds_ang = 270 + 6 * seconds

    seconds_dx = int(cos(radians(-seconds_ang))*200)
    seconds_dy = int(sin(radians(-seconds_ang))*200)

    minutes_ang = 270 + 6 * minutes
    #minutes_ang = 270 + 6 * (minutes + seconds/60.) # smoother move

    minutes_dx = int(cos(radians(-minutes_ang))*200)
    minutes_dy = int(sin(radians(-minutes_ang))*200)

    hours_ang = 270 + 30 * hours
    #hours_ang = 270 + 30 * (hours + minutes/60.) # smoother move

    hours_dx = int(cos(radians(-hours_ang))*150)
    hours_dy = int(sin(radians(-hours_ang))*150)

    seconds += 1

    if seconds == 60:
        seconds = 0
        minutes += 1

        if minutes == 60:
            minutes = 0
            hours += 1

            if hours == 12:
                hours = 0

    # draw

    screen.fill((0,0,0))
    draw.circle(screen, (255,255,255), (400,300), 200, 3)
    draw.line(screen,(0,255,255), (400,300), (400 + hours_dx, 300 - hours_dy), 5)
    draw.line(screen,(255,255,0), (400,300), (400 + minutes_dx, 300 - minutes_dy), 2)
    draw.line(screen,(255,0,255), (400,300), (400 + seconds_dx, 300 - seconds_dy), 1)

    # flip

    display.flip()

    # FPS (Frames Per Seconds)

    clock.tick(1)

quit()

rawDescription: Clock in Python

description: Clock in Python


code: [list(gen) for gen in generator_of_generators]

rawDescription: python generator of generators?

description: Python generator of generators


code:
def some_decorator(func):
    @functools.wraps(func)
    def decorated(*args, **kwargs):
        res = func(*args, **kwargs)
        # Your code here
        return res
    return decorated

rawDescription: How to use the same python decorator for all types of methods/functions?

description: Use the same python decorator for all types of methods/functions


code: orderby = request.GET.get('orderby')

rawDescription: Django passing a string in url

description: Django passing a string in url


code:
from sklearn.metrics import jaccard_similarity_score
print(jaccard_similarity_score(df[0], df[1]))
0.43

rawDescription: How to compute jaccard similarity from a pandas dataframe

description: Compute jaccard similarity from a pandas dataframe


code:
from django.shortcuts import render
from django import forms

from testapp.models import Person

class LimitedCreateForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ('name', 'hobbies')

def create_limited(request):
    submitted = ""
    new_user = None

    if request.method == 'POST':
        submitted = str(request.POST)
        form = LimitedCreateForm(request.POST)
        if form.is_valid():
            new_user = form.save()

    else:
        form = LimitedCreateForm()

    data = {
        'form': form,
        'submitted': submitted,
        'new_user': new_user,
    }

    return render(request, 'create_limited.html', data)

rawDescription: UserCreationForm Django

description: UserCreationForm Django


code:
import urllib
import urlparse

query=urllib.urlencode({"test":1})
urls=['http://www.aaa.com',
      'http://www.aaa.com/home',
      'http://www.aaa.com/?location=home',
      'http://www.aaa.com/?location=home&page=1']
for url in urls:
    x=urlparse.urlparse(url)
    new_query=(x.query+'&'+query) if x.query else query
    y=urlparse.urlunparse((x.scheme,x.netloc,x.path,x.params,new_query,x.fragment))
    print(y)

rawDescription: Automatically create valid links

description: Automatically create valid links


rawDescription: Django: How to use Django's view functions instead of a hardcoded action?


code:
import pandas as pd

df = pd.read_csv('file_name.csv')
y  =  df[['gini']]
X  =  df.drop(['gini'])

rawDescription: Creating a X input matrix and y output vector via Python


code:
import datetime
import json

class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.isoformat()
        elif isinstance(obj, datetime.date):
            return obj.isoformat()
        elif isinstance(obj, datetime.timedelta):
            return (datetime.datetime.min + obj).time().isoformat()
        else:
            return super(DateTimeEncoder, self).default(obj)

data = {
    'dt1': datetime.datetime.now(),
    'dt2': datetime.date.today(),
    'dt3': datetime.timedelta(4),
    's': 'test'
}

print(json.dumps(data, cls=DateTimeEncoder))

rawDescription: Why pycharm reports "can not find reference 'isoformat'" in line 6


code:
def show_results_async(text):
   query  =  { '$or':[{'title':{'$regex':text}},{'author':{'$regex':text}} ]}
   posts = Posts.objects(__raw__=(query))
   return jsonify(result=posts.to_dict())

rawDescription: Flask Jsonify mongoengine query

description: Flask Jsonify mongoengine query


code:
prev_round = df['round'] - 1
opp_player = 3 - df.player_alias   # effectively maps 2 to 1 and 1 to 2

ix = pd.MultiIndex.from_arrays([df.gameId, prev_round, opp_player])
df['opp_prev_action'] = df.set_index(['gameId', 'round', 'player_alias']
                                     ).reindex(ix).action.values

rawDescription: Pandas: Dataframe self-join with complex conditions


rawDescription: python numpy slice notation (COMMA VS STANDARD INDEX)

description: Python numpy slice notation (COMMA VS STANDARD INDEX)


code:
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

rawDescription: Check if there are values other than a certain value exists in a list


code:
@jit
def loopy_dist(x, y, z, L, periodicX, periodicY, periodicZ):
  N = len(x)
  dx = np.zeros((N,N))
  dy = np.zeros( (N,N) )
  dz = np.zeros( (N,N) )
  d = np.zeros( (N,N) )

  def periodic_diff(x1, x2, periodic):
    diff = x1 - x2
    if periodic:
      if diff > (L / 2): diff -= L
      if diff < (-L/2): diff += L
    return diff

  for i in xrange(N):
    for j in xrange(N):
      dx[i,j] = periodic_diff(x[j], x[i], periodicX)
      dy[i,j] = periodic_diff(y[j], y[i], periodicY)
      dz[i,j] = periodic_diff(z[j], z[i], periodicZ)
      d[i,j] = dx[i,j] ** 2 + dy[i,j] ** 2 + dz[i,j] ** 2
      if d[i,j] == 0: d[i,j] = -1
      else: d[i,j] = np.sqrt(d[i,j])
  return d, dx, dy, dz

rawDescription: Optimizing Python function with Parakeet


rawDescription: Convert a binary string into IEEE-754 single precision - Python


rawDescription: Numpy broadcasting with comparison operator; cyclic iteration


code:
class Vector(object):
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z

rawDescription: 3D Collision detection not functioning properly


rawDescription: merge two lists to make list of lists

description: Merge two lists to make list of lists


code:
import diff_match_patch

textA = "the cat in the red hat"
textB = "the feline in the blue hat"

#create a diff_match_patch object
dmp = diff_match_patch.diff_match_patch()

# Depending on the kind of text you work with, in term of overall length
# and complexity, you may want to extend (or here suppress) the
# time_out feature
dmp.Diff_Timeout = 0   # or some other value, default is 1.0 seconds

# All 'diff' jobs start with invoking diff_main()
diffs = dmp.diff_main(textA, textB)

# diff_cleanupSemantic() is used to make the diffs array more "human" readable
dmp.diff_cleanupSemantic(diffs)

# and if you want the results as some ready to display HMTL snippet
htmlSnippet = dmp.diff_prettyHtml(diffs)

rawDescription: Implementing Google's DiffMatchPatch API for Python 2/3


code:
"Machine_ID","WINDOWS","PROCESSOR","SYSPAGEFILE","Sausages","DISPLAY_RES","Candlepower","CPU_NAME","STRENGTH","OS.Arch"
"q27905448","Microsoft Windows XP [Version 5.1.2600]","Intel(R) Celeron(R) D CPU 3.20GHz","1524MB","","800x600","","Intel(R) PRO/1000 GT Desktop Adapter","","x86"

rawDescription: batch parsing multiple registry dumps to csv

description: Batch parsing multiple registry dumps to csv


rawDescription: Python remove couple of duplicates from List


code:
ns = "http://www.foo.com/bar"
em = ElementMaker(namespace=ns, nsmap={'bar': ns})
em.child_element
em.child_element()
em.__isabstractmethod__
bool(em.__isabstractmethod__)

rawDescription: class attribute considered abstract method in python 2.7 - abc module

description: Class attribute considered abstract method in python 2.7 - abc module


rawDescription: extract main diagonal of a sparse matrix in scipy.sparse python

description: Extract main diagonal of a sparse matrix in scipy.sparse python


code:
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import Alias, FromClause
from sqlalchemy.orm import aliased

class TableSample(Alias):

    __visit_name__ = 'tablesample'

    def __init__(self, selectable, argument=None, method=None,
                 seed=None):
        super(TableSample, self).__init__(selectable)
        self.method = method
        self.argument = argument
        self.seed = seed

def tablesample(element, argument=None, method=None, seed=None):
    if isinstance(element, FromClause):
        return TableSample(element, argument=argument, method=method, seed=seed)
    else:
        return aliased(element, TableSample(element.__table__,
            argument=argument, method=method, seed=seed))

@compiles(TableSample)
def compile_tablesample(element, compiler, **kwargs):
    s = "%s TABLESAMPLE %s(%s)" % (
        compiler.visit_alias(element, **kwargs),
        element.method or 'SYSTEM',
        element.argument)

    if element.seed:
        s += " REPEATABLE (%s)" % compiler.process(element.seed, **kwargs)

    return s

rawDescription: How to TABLESAMPLE with SQLAlchemy?

description: TABLESAMPLE with SQLAlchemy


rawDescription: How to check for common elements between two lists in python

description: Check for common elements between two lists in python


code:
model = CountVectorizer()
transformed_train = model.fit_transform(train_corpus)
transformed_test = model.fit_transform(test_corpus)

rawDescription: CountVectorizer deleting features that only appear once


code:
def print_dict(dict_):
    keys = list(dict_.keys())
    keys.sort()
    print("Col1      Col2  Col3")
    for x in range(5):
        check_1 = (dict_["test{0}".format(x)][0])
        check_2 = (dict_["test{0}".format(x)][1])
        print("test{0}".format(x), check_1, check_2, sep = '     ')

rawDescription: Defining a function that prints dictionaries neatly


rawDescription: How to make a diagonal matrix with canopy that has a displacement in the diagonal?

description: Make a diagonal matrix with canopy that has a displacement in the diagonal


code: np.ma.array(np.tile(arr, 2).reshape(2, 3), mask=~cond).argmax(axis=1)

rawDescription: How do I vectorize this loop in numpy?

description: Vectorize this loop in numpy


code:
from itertools import tee
s = "The quick brown fox jumps over the lazy dog"
l = [0, 1, 8, 14, 18, 27, 43]

def get_slices(s, l):
    it1, it2 = tee(l)
    next(it2)
    yield from (s[start:end] for start, end in zip(it1, it2))

list(get_slices(s,l))

rawDescription: What's the pythonic way to cut a string at multiple positions?


code:
from subprocess import Popen, STDOUT
import os

Popen(['google-chrome'], stdout=os.open(os.devnull, os.O_RDWR), stderr=STDOUT)

rawDescription: Using subprocess.call to open a program

description: Using subprocess.call to open a program


code:
x = list(invRegex.invert('[a-z][0-9]?.'))
y = list(inverse_regex.ipermute('[a-z][0-9]?.'))
print(len(x))
# 26884
print(len(y))
# 1100

rawDescription: Generate random string from regex character set


code:
start = time.time()
cmd = sys.executable + " -c 'import time; time.sleep(2)' >/dev/null &"
subprocess.check_output(cmd, shell=True)
assert (time.time() - start) < 1

rawDescription: Python subprocess .check_call vs .check_output


rawDescription: Python: how to split a string by multiple strings


code:
def new3(normlized_a):
    weights  = numpy.floor(normalized_a * 100)
    total_wt = np.sum(weights)
    rows, cols = weights.shape
    row_values = np.dot(weights.sum(axis=1), np.arange(rows)) / total_wt
    col_values = np.dot(weights.sum(axis=0), np.arange(cols)) / total_wt
    return row_values, col_values

rawDescription: get average (row, col) cell given array values as weights?

description: Get average (row, col) cell given array values as weights


code:
import xbmcaddon
addon = xbmcaddon.Addon()
addon.setSetting('setting_id', 'true')

rawDescription: how to check the value to overwritten them in xbmc?

description: Check the value to overwritten them in xbmc


code:
import mpl_toolkits.basemap as mp
Zg = mp.interp(dataIN,lonIN,latIN,lonOUT,latOUT,
               checkbounds=False, masked=False, order=1)

rawDescription: Python 2d interpolation of 3d array

description: Python 2d interpolation of 3d array


code: merged.loc[merged.dummy != False, df1.columns]

rawDescription: Set differences on columns between dataframes


code:
class SerializerRegistry(object):
    def __init__(self):
        self._classes = {}
    def add(self, cls):
        self._classes[cls.__module__, cls.__name__] = cls
        return cls
    def object_hook(self, dct):
        module, cls_name = dct.pop('__type__', (None, None))
        if cls_name is not None:
            return self._classes[module, cls_name].from_dict(dct)
        else:
            return dct
    def default(self, obj):
        dct = obj.to_dict()
        dct['__type__'] = [type(obj).__module__,
                           type(obj).__name__]
        return dct

registry = SerializerRegistry()

@registry.add
class A(object):
    def __init__(self, item1):
        self.item1 = item1
    def __repr__(self):
        return str(self.__dict__)
    def to_dict(self):
        return dict(item1=self.item1)
    @classmethod
    def from_dict(cls, dct):
        return cls(**dct)

s = json.dumps(A(1), default=registry.default)
a = json.loads(s, object_hook=registry.object_hook)

rawDescription: How to make simplejson serializable class

description: Make simplejson serializable class


code:
def hangman(secret_word):
    from string import ascii_lowercase
    print ("Welcome to Hangman!")
    print ("I am thinking of a word that is {} letters long\n-----------".format(len(secret_word)))
    guesses = 8
    letters_guessed = []
    missed_l = ''
    correct_l = ''
    secret_word = secret_word.lower()
    while True:
        # if all the letters in secret_word are in correct_l , the user has won
        if all(x in correct_l for x in secret_word):
            print ("Congratulations, you won!")
            break
        # if len missed letters is 8, user has used up all guesses so break/end teh game
        elif len(missed_l) == 8:
            print ("Sorry, you ran out of guesses. The word was {}.".format(secret_word))
            break
        print ("You have {} guesses left".format(guesses))
        print ("Available Letters: {}".format(" ".join([x for x in ascii_lowercase if x not in letters_guessed])))
        guess = raw_input("Please guess a letter: ").lower()
        # if guess in letters_guessed  user has already guessed that letter
        if guess in letters_guessed:
            print ("Sorry You've already guessed that letter: {}\n-----------".format(guess))
        # else if it is in the word, add  guess to correct_l and to letters guessed
        elif guess in secret_word:
            letters_guessed.append(guess)
            correct_l += guess
            print("Good guess: {}\n-----------".format(" ".join([x if x in letters_guessed else "_" for x in secret_word])))
        # if users guess is not in the word, minus a  life/guess
        elif guess not in secret_word:
            letters_guessed.append(guess)
            guesses -= 1
            print (" That letter is not in my word: {}\n-----------".format(" ".join([x if x in letters_guessed else "_" for x in secret_word])))
            missed_l += guess

rawDescription: Python: Revealing a word for Hangman (Strings, replace, index, and strip)


code:
grouped = {}

for dt in list_of_dts:
    grouped.setdefault(dt.hour, []).append(dt)

result = grouped.values()

rawDescription: Split list of datetime by hours using python


code:
import sqlite3
c1 = sqlite3.connect("file::memory:?cache=shared")
c1.execute('CREATE TABLE foo (bar, baz)')
c1.execute("INSERT INTO foo VALUES ('spam', 'ham')")
c1.commit()
c2 = sqlite3.connect("file::memory:?cache=shared")
c2 = sqlite3.connect("file::memory:?cache=shared")
list(c2.execute('SELECT * FROM foo'))
c3 = sqlite3.connect('/tmp/sqlite3.db')
c3.execute("ATTACH DATABASE 'file::memory:?cache=shared' AS inmem")
list(c3.execute('SELECT * FROM inmem.foo'))

rawDescription: How can I attach an in-memory SQLite database in Python?

description: Attach an in-memory SQLite database in Python


code:
import inspect

def func_has_positional_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varargs) # varargs: name of the * argument or None
def func_has_keyword_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varkw)   # varkw: name of the ** argument or None

rawDescription: Introspect function to determine which argument unpacking (positional or keyword) was used


code:
text.tag_config('highlight', foreground='red')
text.tag_config('unhighlight', foreground='black')

rawDescription: How to color the last word added in text with tkinter

description: Color the last word added in text with tkinter


code:
import unicodedata

tag = "#tag"
text = u"test string with #tág inserted and a #tag"
text=unidecode(text)
re.findall(tag, text, re.IGNORECASE)

rawDescription: python regular expression also match special characters

description: Python regular expression also match special characters


code:
joined.loc[joined['price'] < joined['percentil_25'], 'class'] =1
joined.loc[(joined['price'] >= joined['percentil_25']) & (joined['price'] < joined['percentil_50']), 'class'] =2
joined.loc[(joined['price'] >= joined['percentil_50']) & (joined['price'] < joined['percentil_75']), 'class'] =3
joined.loc[joined['price'] >= joined['percentil_75'], 'class'] =4

classe_final = joined[['cku', 'class']]

rawDescription: python, operation on big pandas Dataframe

description: Python, operation on big pandas Dataframe


code: ['five', 'two', 'three', 'six', 'four', 'one']

rawDescription: How to sort a list with duplicate items by the biggest number of duplicate occurrences - Python

description: Sort a list with duplicate items by the biggest number of duplicate occurrences - Python


rawDescription: Ignoring plotting data points of certain value


code:
import unittest

class MessageExampleTest(unittest.TestCase):
    def setUp(self):
        self.longMessage = True

    def test_with_custom_message(self):
        self.assertEqual('apples', 'oranges', 'You should not compare apples and oranges.')

rawDescription: When unit testing in Python, how can I display my custom message together with the default message when an assert fails?


code:
def __getitem__(self, k):
    """
    Retrieves an item or slice from the set of results.
    """

    # ... trimmed ...

    if isinstance(k, slice):
        qs = self._clone()
        if k.start is not None:
            start = int(k.start)
        else:
            start = None
        if k.stop is not None:
            stop = int(k.stop)
        else:
            stop = None
        qs.query.set_limits(start, stop)
        return list(qs)[::k.step] if k.step else qs

    qs = self._clone()
    qs.query.set_limits(k, k + 1)
    return list(qs)[0]

rawDescription: Limiting the number of results from a Django QuerySet, without using slice


code:
[x.rstrip(',') for x in shlex.split(s)]
Out[20]: ['format', 'db', 'this is string a', '0', '0Ah']

rawDescription: How to divide string like this into items in Python

description: Divide string like this into items in Python


code:
for a in set(list_a) - set(list_b):
    writeFile(...)

rawDescription: Comparing large list of hashes to other large list of hashes


code:
min_key = min(S, key=D.get)
min_val = D[min_key]

rawDescription: Python: iterating over subset of dictionary


code: {e['id']: (e['lat'], e['lon']) for e in elements if e['type'] == 'node'}

rawDescription: How to access way coordinates from a large (.json-based) list structure

description: Access way coordinates from a large (.json-based) list structure


rawDescription: Python - List of unique dictionaries

description: Python - List of unique dictionaries


code:
def reshape_based(x,m):
    shp = x.shape[:-1]
    n_ele = np.prod(shp)
    return x.reshape(n_ele,-1)[np.arange(n_ele),m.ravel()].reshape(shp)

def indices_based(x,m):  ## @B. M.'s solution
    firstdims=np.indices(x.shape[:-1])
    ind=tuple(firstdims)+(m,)
    return x[ind]

rawDescription: indexing in numpy (related to max/argmax)

description: Indexing in numpy (related to max/argmax)


code: [('the', 'red', 'barn', 'and', 'found', 'chickens')]

rawDescription: Python regex endline

description: Python regex endline


code: return newInp

rawDescription: How to write a lot of nested 'for' loops (Recursively)

description: Write a lot of nested 'for' loops (Recursively)


code:
input_var1 = raw_input("Enter something: ")
input_var2 = raw_input("Enter something: ")
print ("you entered: " + input_var1 + ", " + input_var2)

rawDescription: Passing inputs from a text file to python program using batch file


code:
@admin.route('/manage')
@login_required
def manage():
    return render_template('admin/manage/default.html',
                           title='Default page for manage',
                           current=None)

@admin.route('/manage/menu_settings')
@login_required
def manage_menu_settings():
    return render_template('admin/manage/site_configuration.html',
                           title='Site configuration',
                           current='menu_settings')

@admin.route('/manage/internet_market')
@login_required
def manage_internet_market():
    return render_template('admin/manage/internet_market.html',
                           title='Internet market',
                           current='internet_market')

rawDescription: dynamic render_template() in flask

description: Dynamic render_template() in flask


code:
import timeit

interned = 'foo'
not_interned = (interned + ' ').strip()

assert interned is not not_interned

d = {interned: 'bar'}

print('Timings for short strings')
number = 100000000
print(timeit.timeit(
    'd[interned]',
    setup='from __main__ import interned, d',
    number=number))
print(timeit.timeit(
    'd[not_interned]',
    setup='from __main__ import not_interned, d',
    number=number))

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

interned_long = interned * 100
not_interned_long = (interned_long + ' ').strip()

d[interned_long] = 'baz'

assert interned_long is not not_interned_long
print('Timings for long strings')
print(timeit.timeit(
    'd[interned_long]',
    setup='from __main__ import interned_long, d',
    number=number))
print(timeit.timeit(
    'd[not_interned_long]',
    setup='from __main__ import not_interned_long, d',
    number=number))

rawDescription: In Python, when are two objects the same?


rawDescription: Check if a list of numbers is in a certain range?


rawDescription: Finding the corresponding item in two tuples with Python


code:
soup = BeautifulSoup(html, "html.parser")
allA = soup.find("div", {"class" : "overview"}).find_all("a")

classInfo = {}
currentClass = None
for item in allA:
    if item['class'] == ['course_name']:
        classInfo[item.text] = []
        currentClass = item.text
    else:
        classInfo[currentClass] += [(item.text, int(re.search(item.text + r"</a> (\d+)", html).group(1)))]

print(classInfo)

rawDescription: Scrape information from challenging website with no guiding HTML structure


code:
with open("in.txt") as f:
    for line in f:
        a, rest = line.split(":",1)
        print([a] + map(int,rest.split(",")))

rawDescription: List Comprehensions - How to have strings and integers in one list?


code:
import pylab as py

data = [["peach", 1.0, 1.0],
        ["apples", 19, 3.5],
        ["oranges", 7, 2.2],
        ["grapes", 23, 7.8]]

for item in data:
    py.scatter(item[1], item[2], s=700*item[1],
           c=(item[2]/10.0, 0, 1 - item[2]/10.0),
           marker=r"$ {} $".format(item[0]), edgecolors='none' )

py.show()

rawDescription: "Clean" way to use words as markers in matplotlib? And make font size and color differ?


rawDescription: Edgelist from a binary tree

description: Edgelist from a binary tree


code:
def chainsplit(n,p):
    return sorted(list(set(chain.from_iterable([list(permutations(i,len(i)))
        for i in list(combinations_with_replacement(range(n+1),p)) if sum(i) == n]))))

rawDescription: An iterative rather than recursive algorithm to find all ways to split n into m pieces


code: print('{0} of {1} downloaded'.format(file_info.st_size, file_size))

rawDescription: Parsing print in python

description: Parsing print in python


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


code:
seen = dict()
seen_setdefault = seen.setdefault
new_row = ["" if cell in seen else seen_setdefault(cell, cell) for cell in row]

rawDescription: How do you remove duplicates from a list in Python whilst preserving order and length?

description: Remove duplicates from a list in Python whilst preserving order and length


code:
class CircleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Circle
        fields = ('name', 'avatar', 'desc', 'image', 'created_date')
        read_only_fields = ('created_date',)

    def to_representation(self, obj):
        # call the super() to get the default serialized data
        serialized_data = super(CircleSerializer, self).to_representation(obj)
        custom_representation = {'circles': serialized_data} # insert the above response in a dictionary
        return custom_representation

rawDescription: django-rest-framwork how can i return the {[data1,data2]} instead of [data1,data2]

description: Django-rest-framwork how can i return the {[data1,data2]} instead of [data1,data2]


code: ssa_flat = [x for sublist in subsetA_nodeID for x in sublist]

rawDescription: compare two lists and return not matching items

description: Compare two lists and return not matching items


code:
from collections import namedtuple

Tree = namedtuple("Tree", ["level_num", "parent", "children"])
# equivalent to
# # class Tree:
# #     def __init__(self, level_num: int,
# #                        parent: int,
# #                        children: list):
# #         self.level_num = level_num
# #         self.parent = parent
# #         self.children = children

def build_trees(values, levels):
    trees = []  # list of Trees
    pending_trees = []
    vs_ls = zip(values, levels)
    last_v, last_l = next(vs_ls)
    test_l = last_l + 1
    for v, l in zip(values, levels):
        if l > last_l:
            # we've found a new tree
            if l != last_l + 1:
                # What do you do if you get levels like [0, 1, 3]??
                raise ValueError("Improper leveling: {}".format(levels))
            test_l = l

            # Stash the old tree and start a new one.
            pending_trees.append(cur_tree)
            cur_tree = Tree(level_num=last_l, parent=last_v, children=[])

        elif l < test_l:
            # tree is finished

            # Store the finished tree and grab the last one we stashed.
            trees.append(cur_tree)
            try:
                cur_tree = pending_trees.pop()
            except IndexError:
                # No trees pending?? That's weird....
                # I can't think of any case that this should happen, so maybe
                # we should be raising ValueError here, but I'm not sure either
                cur_tree = Tree(level_num=-1, parent=-1, children=[])

        elif l == test_l:
            # This is a child value in our current tree
            cur_tree.children.append(v)
    # Close the pending trees
    trees.extend(pending_trees)
    return trees

rawDescription: How do I test a sum tree?

description: Test a sum tree


rawDescription: Random weighted choice

description: Random weighted choice


code:
import mmap
import re

pattern = re.compile(b'the ultimate answer is ([0-9]+)')
with open("datafile.txt", "rb") as f:
    # memory-map the file, size 0 means whole file
    mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)

    # PROT_READ only on *nix as the file is not writable
    for match in pattern.finditer(mm):
        # process match
        print("The answer is {}".format(match.group(1).decode('ascii')))

    mm.close()

rawDescription: Python string processing optimization

description: Python string processing optimization


code:
s = message.get("date")    # e.g. "Wed, 23 Jan 2011 12:03:11 -0700"
print(time.strftime("%Y-%m-%d-%H-%M-%S", dtFormat(s)))

rawDescription: Parse date/time from a string

description: Parse date/time from a string


rawDescription: Python on windows, open plot windows next to each other


rawDescription: Python: Pass A Directory with argparse

description: Python: Pass A Directory with argparse


rawDescription: Run function with positional and optional arguments in parallel in python (follow up)


code: rgb = numpy.fromstring(data, '3uint8')

rawDescription: Speed up a delta filter in python/numpy

description: Speed up a delta filter in python/numpy


code:
def mix(a,b):
    if len(a) != len(b):
        return
    elif not a:
        return ""
    else:
        return a[0] + b[0] + mix(a[1:], b[1:])

rawDescription: How to use recursion to mix 2 strings and return both as 1 'interlaced' string?

description: Use recursion to mix 2 strings and return both as 1 'interlaced' string


rawDescription: How to implement this Depth-First-Search in a Sudoku Solver

description: Implement this Depth-First-Search in a Sudoku Solver


code:
from matplotlib import rcParams
rcParams['xtick.major.pad']='8'

rawDescription: Python: how to reduce spacing between plot and xticks using matplotlib


code: timeit.timeit(stmt='[sqrt(n) for n in range(100)]', setup='from math import sqrt', number=10000)

rawDescription: Using exponentiation **0.5 less efficient than math.sqrt?


code:
x = [False, False, False, 10, False, False]
(i for i in x if i!=False).next()

rawDescription: Find a unique element in a list

description: Find a unique element in a list


code:
def column_matches(line, substring, which_column):
    delimiter = '\t'
    columns = line.split(delimiter)
    value = columns[which_column]
    if value == substring:
        return True
    else:
        return False

rawDescription: Python Search function in a tab delimited column file


code:
import sys
import os
from time import strftime

if len(sys.argv) != 2: # if CLi does not equal to 2 commands print
    print ("usage:progammename.py logname.ext")
    sys.exit(1)

logSys    = sys.argv[1]
newLogSys = "%s_%s.csv" % (logSys,strftime("%Y_%m_%d_ %H_%M_%S"))

with open(logSys,'rb') as log, open(newLogSys,'wb') as nL:
    nL.writelines(lineI.replace(':' if lineI[0]=='#' else ' ', ',')
                  for lineI in log)

rawDescription: Python Replacing white spaces in a text file with a comma


code:
with open('highscores.txt', 'rb') as f:
    reader = csv.reader(f)
    scores = (int(row[-1]) for row in reader)
    topscores = sorted(scores, reverse=True)

rawDescription: How to display top 10 highscores from a comma separated value (.txt) file in Python (Pygame)

description: Display top 10 highscores from a comma separated value (.txt) file in Python (Pygame)


code: ['GGKKKTKICDKVSHEEDRISQ', 'ISEILFHLSTKDSVRTSALST', 'FDSHRDSWIRKLRLDLGYHHD', 'HLDVHCFHDNKIPLSIYTCTT', 'PEFVSLP\nCLKIMHFENVSYP', 'ELILFSTMYPKGNVLQLRSDT', 'YAPLLQCLRAKMYSTK\nNFQI', 'DFVNTGGRYQKKKVIEDILID', 'RDLVISSNTWKEFFLYSKSRP', 'MLPTLLESCPKLESLILVMSS']

rawDescription: Regular expression not capturing all the elements


code:
df1.reset_index(inplace=True)
df2.reset_index(inplace=True)

merged = pd.merge(df1,df2,left_on=[df1.date.dt.year,'INCAGG'],
        right_on=[df2.date.dt.year,'INCAGG'],suffixes=['','R'],
        how='left').set_index(['date','INCAGG'])

del merged['dateR']

rawDescription: Pandas: Merge datasets of different frequency


rawDescription: Python slice assignment in the reverse order doesn't work! Why?


code: rowheights = [.2*inch] * len(data)

rawDescription: What determines the vertical space in Reportlab tables?


code: [map(tuple, line) for line in image]

rawDescription: changing sub-sublist into tuples in python

description: Changing sub-sublist into tuples in python


rawDescription: How to split a matrix into 4 blocks using numpy?

description: Split a matrix into 4 blocks using numpy


code:
class TestLogout(TestCase):

   def test_logout(self):
        self.client = Client()
        # Assuming there is a user exists in tests db
        # or make a user like.
        # User.objects.create_user(username='fred', email='test@test.com', password='secret')
        self.client.login(username='fred', password='secret')
        response = self.client.get('/cost_control/logout/')
        self.assertEqual(response.status_code, 302)

rawDescription: Coverage test django logout

description: Coverage test django logout


code: argparse_dict = vars(args)

rawDescription: Using Argparse and Json together

description: Using Argparse and Json together


code: ((bit_offset + Bit_Offset + i) % 8)

rawDescription: Understanding bit-manipulation function in Python


code:
s = '< a href="http://xxxx">Some text< /a>'
re.sub(r'<[^<>]*>', r'', s)

rawDescription: How to get href title from a element and delete all string?

description: Get href title from a element and delete all string


rawDescription: Pythonic formatting from 1 to 01

description: Pythonic formatting from 1 to 01


code:
lp = np.einsum('ij,ijk->ik', points, matrices)
lp[0]
points[0].dot(matrices[0])
lp[1]
points[1].dot(matrices[1])

rawDescription: Large point-matrix array multiplication in numpy


code:
def save_model(self, request, obj, form, change):
    """
    Given a model instance save it to the database.
    """

    # your custom stuff here, this is the file path, change "upload_field_name"
    # by your actual FileField name
    obj.upload_field_name.path

    obj.save()

rawDescription: Django custom admin methods on uploaded file


code:
class App:
    def __init__(root, self):
        # initialize your class with all your widgets
        self.fc_lb = root
        self.button = Button(fc_lb, text="Analyze selected feature classes", command=lambda *events:self.usr_fc(*events))
# Note  ^^^^ all your widgets and data will be accessible through the `self` reference to your GUI instance.
        self.UsrFCList = []
        self.lbox = AutoSzLB(self.fc_lb,selectmode=EXTENDED)
        # . . . you'd have to add all the other setup required to make your GUI

    def usr_fc(self, *events):
    #          ^^^^ you now have access to everything in the instance namespace available inside usr_fc
        self.UsrFCList = []
        selctd_indices = lbox.curselection()
        lst_select = list(selctd_indices)
        for i in lst_select:
            self.UsrFCList.append(lbox.get(i))
        self.lbox.quit()

    # . . . and you'd add all the other functions that need to work with internal GUI widgets/data

rootWindow = Tk()
newGUI = App(rootWindow)  # Create a new GUI instance
rootWindow.mainloop()

# Hopefully that gives you the idea.

rawDescription: set new list variable equal to list generated in a function that has listbox values passed to it

description: Set new list variable equal to list generated in a function that has listbox values passed to it