code:
import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()

url = 'https://iol1.iroquois.com/infopost/Pages/OperationallyAvailable.php?parentId=100'
browser.get(url)
WebDriverWait(browser,10000).until(EC.visibility_of_element_located((By.TAG_NAME,'body')))
date_element = browser.find_element_by_id('searchDateTextfield-inputEl')
date_element.click()
date_element.send_keys(Keys.HOME)

# For date 10 Oct 2015
date_element.send_keys("10042015")
date_element.send_keys(Keys.TAB)
browser.find_element_by_xpath("//span[@id='retrieveButton-btnInnerEl']").click()
time.sleep(100)
browser.close()

rawDescription: Python Selenium: input textbox, send_keys not working


code:
class EchoMibInstrumController(instrum.AbstractMibInstrumController):
    def readVars(self, vars, acInfo=(None, None)):
        return vars
    def readNextVars(self, vars, acInfo=(None, None)):
        return vars
    def writeVars(self, vars, acInfo=(None, None)):
        return vars

rawDescription: pysnmp command responder - Handle get/set requests with external data (not mib)

description: Pysnmp command responder - Handle get/set requests with external data (not mib)


code:
numbers = [i for i in the_list if isinstance(i, int)]
symbols = [i for i in the_list if isinstance(i, str)]

rawDescription: Separating numbers from symbols in lists; python


code:
comma_separated_float_list_re = re.compile('^([-+]?\d*\.?\d+[,\s]*)+$')
validate_comma_separated_float_list = RegexValidator(
              comma_separated_float_list_re,
              _(u'Enter only floats separated by commas.'), 'invalid')

class CommaSeparatedFloatField(CharField):
    default_validators = [validators.validate_comma_separated_float_list]
    description = _("Comma-separated floats")

    def formfield(self, **kwargs):
        defaults = {
            'error_messages': {
                'invalid': _(u'Enter only floats separated by commas.'),
            }
        }
        defaults.update(kwargs)
        return super(CommaSeparatedFloatField, self).formfield(**defaults)

rawDescription: Django: Model field for storing a list of floats?


code: P = P0*Sin(Omega*(1-t))/Sin(Omega) + P1*Sin(Omega * t)/Sin(Omega)

rawDescription: Points on a geodesic line

description: Points on a geodesic line


code:
import subprocess as sub

[...]

with open(logfile, "a", 0) as File_obj:
    for cmd in (MyCommand, MyMount, MyBackup):
        with sub.Popen(cmd, shell=True, stdout=sub.PIPE, universal_newlines=True) as proc:
            while proc.poll() is None:
                log = [TimeStamp + line for line in proc.stdout.readlines()]
                File_obj.writelines(log)

rawDescription: How to intercep each line of stdout after issuing a command at prompt with python

description: Intercep each line of stdout after issuing a command at prompt with python


code:
def browse(request):

    business_industry = request.GET.get('business_industry', '')
    business_address_region = request.GET.get('business_address_region', '')
    employment_type = request.GET.get('employment_type', '')
    pay_rate = request.GET.get('pay_rate', '')
    keywords = request.GET.get('keywords', '')

    form = JobSearchForm()

    filters = Q(active_listing=True)

    if business_industry:
        filters &= Q(business_industry=business_industry)
    if business_address_region:
        filters &= Q(business_address_region__icontains=business_address_region)
    if keywords:
        filters &= Q(job_description__icontains=keywords)

    job_listings = JobListing.objects.filter(filters).distinct()

    context_dict = {
        'joblistings': job_listings,
        'form': form
    }

    return render(request, 'browse.html', context_dict)

rawDescription: How to make Django Query lookup change based on user input

description: Make Django Query lookup change based on user input


code:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)

ax.bar([0,1,2,3],[7900,9400,8700,9990],facecolor='#5080B0',edgecolor='k',width=0.3)

ax.set_xticks([0.15,1.15,2.15,3.15])

ax.set_xticklabels(['beek1','beek2','orbath','Kroo'],
                   rotation='vertical',y=0.05,va='bottom')

rawDescription: Matplotlib: How do I have the xtick labels apear on the other side of the x-axis?


code:
from fabric.operations import run

oracle_path = run('echo $ORACLE_PATH', shell=True)

with settings(host_string = 'user@server.com'):
    get(os.path.join(oracle_path, 'network/admin/tnsnames.ora'))

rawDescription: fabric - is there a way to expand shell variables?

description: Fabric - is there a way to expand shell variables


code:
animal_count = 0
other_count = 0

## List of known keywords (Animals)
animal_keywords = ['Panda', 'Elephant', 'Lemur']

# with will automatically close your file
with open("list.txt") as f:
    # iterate over the file object, getting each line
    for line in f:
        # if any keyword is in the line, increase animal count
        if any(animal in line for animal in animal_keywords):
            animal_count += 1
        # else there was no keyword in the line so increase other
        else:
            other_count += 1

rawDescription: comparing and counting items in python (while loop)

description: Comparing and counting items in python (while loop)


code:
if sys.platform == 'win32':
   if getattr(sys, 'frozen', False):
      os.environ['TKDND_LIBRARY'] = os.path.join(os.path.dirname(sys.executable), 'tkdnd2.7')

rawDescription: How to Install and Use TkDnD with Python 2.7 Tkinter on OSX?


code:
from threading import Thread
import Queue

in_queue = Queue.Queue()
out_queue = Queue.Queue()

def work():
    while True:
        try:
            sonId = in_queue.get()
        ###do your things here
            result = sonID + 1

        ###you can even put your thread results again in another queue here
            out_queue.put(result)   ###optional

        except:
            pass
        finally:
            in_queue.task_done()

for i in range(20):

    t = Thread(target=work)
    t.daemon = True
    t.start()

for son in range(10):
    in_queue.put(son)

in_queue.join()

while not out_queue.empty():

    result = out_queue.get()

    ###do something with your result here

    out_queue.task_done()

out_queue.join()

rawDescription: Python threading design

description: Python threading design


code:
from PyQt5.QtCore import QUrl, QTimer, QIODevice
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import (QNetworkAccessManager,
                             QNetworkReply,
                             QNetworkRequest)
from PyQt5.QtWebKitWidgets import QWebView

import sys

class ExampleNetworkAccessManager(QNetworkAccessManager):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

    def createRequest(self, operation, request, device):
        if request.url().scheme() == 'example':
            return ExampleReply(self, operation, request, device)
        return super().createRequest(operation, request, device)

class ExampleReply(QNetworkReply):

    def __init__(self, parent, operation, request, device):
        super().__init__(parent=parent)
        self.setRequest(request)
        self.setOperation(operation)
        self.setUrl(request.url())
        self.bytes_read = 0
        self.content = b''

        # give webkit time to connect to the finished and readyRead signals
        QTimer.singleShot(200, self.load_content)

    def load_content(self):
        if self.operation() == QNetworkAccessManager.PostOperation:
            # handle operations ...
            pass
        # some dummy content for this example
        self.content = b'''

hello world!

...

            '''
        self.open(QIODevice.ReadOnly | QIODevice.Unbuffered)
        self.setHeader(QNetworkRequest.ContentLengthHeader, len(self.content))
        self.setHeader(QNetworkRequest.ContentTypeHeader, "text/html")
        self.readyRead.emit()
        self.finished.emit()

    def abort(self):
        pass

    def isSequential(self):
        return True

    def bytesAvailable(self):
        ba = len(self.content) - self.bytes_read + super().bytesAvailable()
        return ba

    def readData(self, size):
        if self.bytes_read >= len(self.content):
            return None
        data = self.content[self.bytes_read:self.bytes_read + size]
        self.bytes_read += len(data)
        return data

    def manager(self):
        return self.parent()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wv = QWebView()
    enam = ExampleNetworkAccessManager()
    wv.page().setNetworkAccessManager(enam)
    wv.show()
    wv.setUrl(QUrl("example://test.html"))
    app.exec()

rawDescription: Serving resource to QWebView of PyQT5

description: Serving resource to QWebView of PyQT5


code:
import time

try:
    while True:
        time.sleep(1)  # 1s
except KeyboardInterrupt:
    print("Bye!")

rawDescription: keeping Python script alive constantly to check for changes

description: Keeping Python script alive constantly to check for changes


code:
while ... :
    currentEdge = next(e for e in userEdges if e.nodeTo == node)
    path.append(currentEdge.nodeFrom)

rawDescription: django select_related tree path

description: Django select_related tree path


code: x, y = pygame.mouse.get_pos()

rawDescription: pygame - How to split the X and Y coordinates from get_pos

description: Pygame - How to split the X and Y coordinates from get_pos


code:
class TestContext(BaseContext):
    """A subclass of the (dynamically determined) BaseContext,
    by overriding various methods, we could customize the functionality
    of this context, but the tutorial doesn't ask us to do this.
    """
    pass

rawDescription: Python + OpenGL context indented Block

description: Python + OpenGL context indented Block


code:
d={'one' : Series([1, 2, 3], index=['a', 'b', 'c']), 'two' :Series([datetime.datetime(2014, 7, 9) , datetime.datetime(2014, 7, 10) , datetime.datetime(2014, 7, 11) ], index=['a', 'b', 'c'])}
df = pd.DataFrame(d)

def avg_datetime(series):
    dt_min = series.min()
    deltas = [x-dt_min for x in series]
    return dt_min + functools.reduce(operator.add, deltas) / len(deltas)

print(avg_datetime(df['two']))

rawDescription: Datetime objects with pandas mean function


code:
if (valueRed > 140 or valueBlue > 140 or valueGreen > 140):
    setCoor(pixel, white)
else:
    setColor(pixel, black)

rawDescription: Code ignoring my if statement? Unsure why this code isn't working


rawDescription: Python: getting lowest integer in list of tuples


code:
directions = ['north', 'south', 'east', 'west', 'down', 'up', 'down', 'right']
verbs = ['go', 'stop', 'kill', 'eat']
stops = ['the', 'in', 'at', 'of', 'from', 'at', 'it']
nouns = ['door', 'bear', 'princess', 'cabinet']

def scan(thewords):

    thewords = thewords.split()
    sentence = []

    for i in thewords:
        if i in directions:
            sentence.append(('direction', i))

        elif i in verbs:
            sentence.append(('verb', i))

        elif i in stops:
            sentence.append(('stop', i))

        elif i in nouns:
            sentence.append(('noun', i))

        elif i.isdigit():
            sentence.append(('number', convert_number(i)))

        else:
            sentence.append(('error', i))

    return sentence

def convert_number(s):
    try:
        return int(s)

    except ValueError:
        return None

rawDescription: LPTHW Excercise 48 Help - Working with Tuples in Lists


code:
x = {'James': [10, 7, 9], 'Bob': [3, 8, 4], 'Charles': [6, 2, 5]}
y = [(k, max(v)) for k, v in x.items()]
for k, v in sorted(y, key=lambda a: a[1], reverse=True):
    print (k, v)

rawDescription: How can I access values from a key that stores more than one value in a dictionary in python?

description: Access values from a key that stores more than one value in a dictionary in python


code: columns = [tree.columnWidth(index) for index in range(tree.columnCount())]

rawDescription: Storing QListWidget column widths

description: Storing QListWidget column widths


code:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
import logging
from logging.handlers import SMTPHandler
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonTracebackLexer
import smtplib

class ColorfulSMTPHandler(SMTPHandler):

    def emit(self, record):
        try:
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP(self.mailhost, port)
            msg = MIMEMultipart('alternative')
            msg['Subject'] = self.getSubject(record)
            msg['From'] = self.fromaddr
            msg['To'] = ",".join(self.toaddrs)
            msg['Date'] = formatdate()

            text = self.format(record)
            msg.attach(MIMEText(text, 'plain'))
            if record.exc_text:
                html_formatter = HtmlFormatter(noclasses=True)
                tb = highlight(record.exc_text, PythonTracebackLexer(), html_formatter)

                info = (self.formatter or logging._defaultFormatter)._fmt % record.__dict__
                info = '
%s
' % info

                html = ('%s%s')% (info, tb)
                msg.attach(MIMEText(html, 'html'))
            if self.username:
                if self.secure is not None:
                    smtp.ehlo()
                    smtp.starttls(*self.secure)
                    smtp.ehlo()
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg.as_string())
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

rawDescription: Color coding traceback in the SMTPHandler and sending HTML


code:
import os

for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        print(os.path.join(dirname, filename))

    # Advanced usage:
    # editing the 'dirnames' list will stop os.walk() from recursing into there.
    if '.git' in dirnames:
        # don't go into any .git directories.
        dirnames.remove('.git')

rawDescription: Directory listing in Python

description: Directory listing in Python


code: re.match('(ABC|EFH)(PQR|STU)', 'ABCPQR01 is not at all good').groups()

rawDescription: How can I capture this through a regex?

description: Capture this through a regex


code:
class Ui_Frame(QtGui.QWidget):
    def __init__(self, parent=None):
        # Here, you should call the inherited class' init, which is QDialog
        QtGui.QWidget.__init__(self, parent)
        xlist = [1,2,3]
        y = "Default"
        self.setupUi(xlist,y)

    def setupUi(self, xlist, y):
        self.setObjectName(_fromUtf8("Frame"))
        self.resize(800, 400)
        self.tableWidget = QtGui.QTableWidget(self)
        ...

rawDescription: Transitioning from QTDesigners style class to more regularly referenced style


code:
class DomainCreateView(CreateView):
    """
    Creates a Domain
    """
    form_class = DomainForm
    template_name = 'customer/create_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainCreateView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """
        Validate and save the form
        """
        company = self.request.user.get_profile().company
        self.object = form.save(company)
        return super(ModelFormMixin, self).form_valid(form)

class DomainDeleteView(DeleteView):
    """
    Deletes a Domain
    """
    model = Domain
    template_name = 'customer/delete_domains.html'
    success_url = reverse_lazy('manage_domains')

    @method_decorator(login_required)
    @method_decorator(only_managers_allowed)
    def dispatch(self, *args, **kwargs):
        """
        The Dispatcher
        """
        return super(DomainDeleteView, self).dispatch(*args, **kwargs)

rawDescription: Converting old function-based generic CRUD views to the new style class-based generic CRUD views


code:
with open("filename", "wb") as f:
    f.seek(10239)    # seek to 10k - 1
    f.write("\0")    # write a single byte

rawDescription: Generating binary objects of size N, with N being specified by the user


code:
def telnet_service(resource, documents):
    """
        fetch data from telnet device;
        update 'documents' accordingly
    """
    pass

app = Eve()
app.on_insert += telnet_service

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

rawDescription: How to do a custom insert inside a python-eve app

description: Do a custom insert inside a python-eve app


code:
#!/usr/bin/python3
import Xlib
import Xlib.display

disp = Xlib.display.Display()
root = disp.screen().root

NET_WM_NAME = disp.intern_atom('_NET_WM_NAME')
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')

root.change_attributes(event_mask=Xlib.X.FocusChangeMask)
while True:
    try:
        window_id = root.get_full_property(NET_ACTIVE_WINDOW, Xlib.X.AnyPropertyType).value[0]
        window = disp.create_resource_object('window', window_id)
        window.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
        window_name = window.get_full_property(NET_WM_NAME, 0).value
    except Xlib.error.XError: #simplify dealing with BadWindow
        window_name = None
    print(window_name)
    event = disp.next_event()

rawDescription: How to “correctly” detect application name when changing focus event occurs with python xlib


code:
class MyModelClass(Base):
    ...

event.listen(
    MyModelClass,
    "mapper_configured",
    setup_schema)

rawDescription: Where to put automatic setup_schema for ColanderAlchemy+Pyramid


code:
import subprocess
import os

def combine_pdfs(infiles, outfile, basedir=''):
    """
    Accept a list of pdf filenames,
    merge the files,
    save the result as outfile

    @param infiles: list of string, names of PDF files to combine
    @param outfile: string, name of merged PDF file to create
    @param basedir: string, base directory for PDFs (if filenames are not absolute)
    """

    # From the pdftk documentation:
    #   Merge Two or More PDFs into a New Document:
    #   pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf

    if basedir:
        infiles = [os.path.join(basedir,i) for i in infiles]
        outfile = [os.path.join(basedir,outfile)]

    pdftk =   [r'C:\Program Files (x86)\Pdftk\pdftk.exe']   # or wherever you installed it
    op =      ['cat']
    outcmd =  ['output']

    args = pdftk + infiles + op + outcmd + outfile
    res = subprocess.call(args)

combine_pdfs(
    ['p1.pdf', 'p2.pdf'],
    'p_total.pdf',
    'C:\\Users\\Me\\Downloads'
)

rawDescription: Python programming - Windows focus and program process


code:
import random

with open(input_file) as data:
    with open(test_output, 'w') as test:
        with open(train_output, 'w') as train:
            header = next(data)
            test.write(header)
            train.write(header)
            for line in data:
                if random.random() > 0.85:
                    train.write(line)
                else:
                    test.write(line)

rawDescription: Python: How to sample data into Test and Train datasets?


code:
import collections
import itertools

source = [
  {
     "sample_id": "read1",
     "seg_1": None,
     "lukM-F": "D",
     "23s_SA": None,
     "see": None,
     "sed": "ND"
  },
  {
     "sample_id": "read2",
     "seg_1": None,
     "lukM-F": "ND",
     "23s_SA": None,
     "see": "D",
     "sed": "ND"
  },
  {
     "sample_id": "read3",
     "seg_1": None,
     "lukM-F": "D",
     "23s_SA": None,
     "see": "ND",
     "sed": "None"
  }
]

size = len(source)

# for python2 you should use iteritems() method
iterators_chain = itertools.chain(*[x.items() for x in source])
counter = collections.Counter(iterators_chain)

for (key, val), count in counter.items():
    if count == size and val is None:
        for x in source:
            x.pop(key)

rawDescription: python remove repeated values in set

description: Python remove repeated values in set


code: [max(len(str(x)) for x in line) for line in zip(*foo)]

rawDescription: Find max length of each column in a list of lists


rawDescription: Merge elements in a list, if they are in a certain unicode range


code:
x = input()
os.path.normpath(os.path.join(x, 'v.mp4'))

rawDescription: Use user input as path to save file in Python 3?


code:
import dis
def f(lst):
 return lst[:]

def f1(lst):
 return list(lst)

dis.dis(f)
dis.dis(f1)

rawDescription: Why is copying a list using a slice[:] faster than using the obvious way?


code:
from PyQt4 import QtGui
from PyQt4 import QtWebKit

class DisplayHTML(QtWebKit.QWebView):

    def __init__(self, html, parent=None):
        super().__init__(parent)
        self.setHtml(html)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    html_template = "
Hello, {first}, how {second} you {third}?
"
    values_dict = {
        'first': 'Joe',
        'second': 'are',
        'third': 'today',
    }
    html_ready_to_render = html_template.format(**values_dict)
    window = DisplayHTML(html_ready_to_render)
    window.show()
    sys.exit(app.exec_())

rawDescription: Qt/pyQt/PySide - Create document template for printing


code:
def intersect(a, b):
    """Intersection of a and b."""
    return (k for k in a if k in b)

def dot(a, b):
    """Dot product of values in a and b."""
    return sum((a[k] * b[k]) for k in intersect(a, b))

def l2norm(a):
    """L2 norm, aka Euclidean length, of a regarded as a vector."""
    return sqrt(sum(v ** 2 for v in a.itervalues()))

def similarity(a, b):
    """Cosine similarity of a and b."""
    return dot(a, b) / (l2norm(a) * l2norm(b))

rawDescription: Find the more similar items with weighted datas


code:
myletter = 'D'
grid = ['ABC','DEF','GHI']
for index, value in enumerate(grid):
    if myletter in value:
        subindex = value.index(myletter)
        break

print [index, subindex]

rawDescription: Python 3 ~ Find position of letter in a grid


code: df.apply(lambda x: reduce(logical_or,x), axis=1)

rawDescription: How do I columnwise reduce a pandas dataframe?

description: Columnwise reduce a pandas dataframe


code:
def rec_range(n):
    if n < 1:
        return ()
    else:
        return rec_range(n - 1) + (n - 1,)

print(rec_range(4)) # prints (0, 1, 2, 3)

rawDescription: Python Recursion: Range

description: Python Recursion: Range


code:
import numpy as np
np.fft.ifftshift(1.0 / (1.0 + (radius / cutoff) ** (2*n)))

rawDescription: Syntax for converting code from MATLAB to PYTHON


code:
verbose_item_pattern_3 = re.compile(r"""
  ^            # start-of-line
  \t*          # 0-or-more tabspace
  [ ]*         # 0-or-more blank space
  I            # a capital I
  [tT][eE][mM] # one character from each of the three sets this allows for unknown case
  \t*          # 0-or-more tabspace
  [ ]*         # 0-or-more blankspace
  \d{1,2}      # 1-or-2 digits
  [.]?         # 0-or-1 literal .
  \(?          # 0-or-1 literal open paren
  [a-e]?       # 0-or-1 letter in the range a-e
  \)?          # 0-or-1 closing paren
  .*           # any number of unknown characters so we can have words and punctuation
  $            # end-of-line
  (?<![0-9])   # NOT preceded by a decimal digit (via look-behind)
  """, re.VERBOSE|re.MULTILINE)

rawDescription: Why is this regular expression not excluding the line when there are numbers at the end of a line in this string


rawDescription: how to convert 3.9055320000e+06 this type of number to interger in python?

description: Convert 3.9055320000e+06 this type of number to interger in python


code:
import re

places = {'#': 'what',
          '@': 'who',
          '!': 'why',
         }

def replace(m):
  all = m.group(0)
  first, rest = all[0], all[1:]
  return '%s' % (
    places[first], rest, all)

markedup = re.sub(r'[#!@]\w+', replace, text)

rawDescription: Python re question - sub challenge

description: Python re question - sub challenge


code:
import xml.etree.ElementTree as ET
data = '''\
<node1>
  <node2 a1="x1"> ... </node2>
  <node2 a1="x2"> ... </node2>
  <node2 a1="x1"> ... </node2>
</node1>
'''
doc = ET.fromstring(data)
e = doc.find('node2/[@a1="x2"]')
doc.remove(e)
print(ET.tostring(doc))
# <node1>
#   <node2 a1="x1"> ... </node2>
#   <node2 a1="x1"> ... </node2>
# </node1>

rawDescription: how do I search for an xml tag with the matching closing tag?

description: Search for an xml tag with the matching closing tag


code: byte_message = chr(0xaa) + chr((bits6_4 << 4) | bits3_0)

rawDescription: Forming bytes bit by bit and concatenating bytes


rawDescription: Python PIL, preserve quality when resizing and saving


code:
data
index
data_ma = np.ma.array(data, mask = np.arange(data.shape[1]) > index[:, 1, None])
np.array(data_ma.mean(axis=1))

rawDescription: Find the average using an array of indexes


code:
from operator import itemgetter

class Find_Overlaps(webapp2.RequestHandler):
    def get(self):
        all_dates = []
        first_dates = db.GqlQuery("SELECT * FROM First_Set")
        for date in first_dates:
            row = {'dataset':'First_Set', 'dbkey':date.key(), 'offset':date.start_time, 'type': -1}
            all_dates.append(row)
            row = {'dataset':'First_Set', 'dbkey':date.key(), 'offset':date.end_time, 'type': 1}
            all_dates.append(row)

        second_dates = db.GqlQuery("SELECT * FROM Second_Set")
        for date in second_dates:
            row = {'dataset':'Second_Set', 'dbkey':date.key(), 'offset':date.start_time, 'type': -1}
            all_dates.append(row)
            row = {'dataset':'Second_Set', 'dbkey':date.key(), 'offset':date.end_time, 'type': 1}
            all_dates.append(row)

        newlist = sorted(all_dates, key=itemgetter('offset','type'))
        number_datasets = 2 #goal is to find overlaps in all sets not only the best overlaps, that's why this is needed
        loopcnt = 0
        update_bestend = 0
        overlaps = []
        for row in newlist: #Below is mostly from Marzullo's alghorithm
            loopcnt = loopcnt - row['type']#this is to keep track of overall tally
            if update_bestend == 1:
                if loopcnt == (number_datasets - 1):
                    bestend = row['offset']
                    end_set = row['dataset']
                    end_key = row['dbkey']
                    overlaps.append({'start':beststart,'start_set':start_set,'start_key':start_key,'end':bestend,'end_set':end_set,'end_key':end_key})
                    update_bestend = 0
            if loopcnt == number_datasets:
                beststart = row['offset']
                start_set = row['dataset']
                start_key = row['dbkey']
                update_bestend = 1

        for overlap in overlaps: #just to see what the outcome is
            self.response.out.write('start: %s, start_set: %s, end: %s, end_set: %s<br>' % (overlap['start'], overlap['start_set'], overlap['end'], overlap['end_set']))

rawDescription: Compare many date ranges in google app engine datastore (Many to many, Python)


code: g = sns.jointplot(x1, x2, kind="kde", ratio=5, size=7, space=0, n_levels=100)

rawDescription: Seaborn jointplot joint density level / color scale adjustment


code:
#!/usr/bin/python

import feedparser
import codecs
import datetime

f = codecs.open('bzusnews.tsv', 'a', 'utf-8')
f.write("Spalte" + "\n")

feeds = feedparser.parse('http://www.buzzfeed.com/usnews.xml')

for feed in feeds.entries:
    f.write("\t".join((feed.title, feed.author, feed.published, feed.link)) + "\n")

f.close()

rawDescription: Transform pubDate to string in Python

description: Transform pubDate to string in Python


code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from sklearn.cluster import KMeans

MY_FILE = '/home/Jian/Downloads/total_watt.csv'

df = pd.read_csv(MY_FILE, parse_dates=[0], header=None, names=['datetime', 'consumption'])

df['date'] = [x.date() for x in df['datetime']]
df['time'] = [x.time() for x in df['datetime']]

stacked = df.pivot(index='time', columns='date', values='consumption').fillna(0).stack()

# do unsupervised clustering
# =============================================
estimator = KMeans(n_clusters=3, random_state=0)
X = stacked.values.reshape(len(stacked), 1)
cluster = estimator.fit_predict(X)

# check the mean value of each cluster
X[cluster==0].mean()  # Out[53]: 324.73175293698534
X[cluster==1].mean()  # Out[54]: 6320.8504071851467
X[cluster==2].mean()  # Out[55]: 1831.1473140192766

# plotting
# =============================================

fig, ax = plt.subplots(figsize=(10, 8))
x = stacked.index.labels[0]
y = stacked.index.labels[1]
ax.scatter(x[cluster==0], y[cluster==0], label='mean: {}'.format(X[cluster==0].mean()), c='g', alpha=0.8)
ax.scatter(x[cluster==1], y[cluster==1], label='mean: {}'.format(X[cluster==1].mean()), c='r', alpha=0.8)
ax.scatter(x[cluster==2], y[cluster==2], label='mean: {}'.format(X[cluster==2].mean()), c='b', alpha=0.8)
ax.legend(loc='best')

rawDescription: How can I visualize my csv data into cluster

description: Visualize my csv data into cluster


code:
for i in commands:
    if "PUSH" in i:
        for n in i:
            if n.isdigit():
                stack.append(int(n))
return stack # move outside the loop

rawDescription: Appending correctly

description: Appending correctly


code:
import os
from subprocess import check_call
from tempfile import mktemp
from scikits.audiolab import wavread, play
from scipy.signal import remez, lfilter
from pylab import *

# convert mp3, read wav
mp3filename = 'XC124158.mp3'
wname = mktemp('.wav')
check_call(['avconv', '-i', mp3filename, wname])
sig, fs, enc = wavread(wname)
os.unlink(wname)

# bandpass filter
bands = array([0,3500,4000,5500,6000,fs/2.0]) / fs
desired = [0, 1, 0]
b = remez(513, bands, desired)
sig_filt = lfilter(b, 1, sig)
sig_filt /=  1.05 * max(abs(sig_filt)) # normalize

subplot(211)
specgram(sig, Fs=fs, NFFT=1024, noverlap=0)
axis('tight'); axis(ymax=8000)
title('Original')
subplot(212)
specgram(sig_filt, Fs=fs, NFFT=1024, noverlap=0)
axis('tight'); axis(ymax=8000)
title('Filtered')
show()

play(sig_filt, fs)

rawDescription: Plot spectogram from mp3

description: Plot spectogram from mp3


code:
import json
data = dict(zip(comic_book_titles, dates))
json.dumps(data)

rawDescription: Python converting a text file into a json object


code:
import socket
from time import sleep
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while s.connect_ex(("10.0.0.1", 80)) != 0:
    sleep(10)

rawDescription: how to continuously trying to connect with a socket till it come up?

description: Continuously trying to connect with a socket till it come up


code:
class Photographer(models.Model):
    # as above, but:
    likes = models.ManyToManyField('Photo', through='PhotoLikes', related_name='likedby', blank=True)

class PhotoLikes(models.Model):
    class Meta:
        db_table = 'djangoapp_photographer_likes'
        # or whatever your table is called
    photo = models.ForeignKey(Photo)
    photographer = models.ForeignKey(Photographer)
    liked = models.DateTimeField(null=True, blank=True, auto_now_add=True)

rawDescription: Is it possible to add a datetime column to a manytomany field?


code:
from datetime import datetime
from datetime import timedelta

createdTime = datetime.strptime('2016-06-09T14:37:05.000+0200', '%Y-%m-%dT%H:%M:%S.%f')
resolvedTime = datetime.strptime('2016-06-10T10:53:12.000+0200', '%Y-%m-%dT%H:%M:%S.%f')

duration = resolvedTime - createdTime

rawDescription: Jira python calculate time

description: Jira python calculate time


code:
conn = MySQLdb.connect (host = "localhost",
                        user = "testuser",
                        passwd = "testpass",
                        db = "test")
cursor = conn.cursor()

cursor.execute(...)
conn.commit()

rawDescription: MySQL INSERT data does not get stored in proper db, only temporary?


code: value = 'Test' if 1 == 1 else 'NoTest'

rawDescription: Python Conditional Variable Setting

description: Python Conditional Variable Setting


code: ramp = lambda x: x if x >= 0 else 0

rawDescription: Rampfunction code

description: Rampfunction code


rawDescription: Efficient way to fill Numpy Array

description: Efficient way to fill Numpy Array


code:
df = DataFrame({'A' : [1,1,1,2,2,3], 'B' : list('bbcdae') })
df.dtypes
df['B'] = pd.Categorical(df['B'],categories=list('ghbaedfc'))
df
df.dtypes
df.sort(['B','A'])

rawDescription: sorting rows in a pandas dataframe in a way which is not alphabetical

description: Sorting rows in a pandas dataframe in a way which is not alphabetical


code: [np.median(i) for i in x]

rawDescription: Getting median of a multidemensional array of differing lengths


rawDescription: Sorting Nested List with Object List

description: Sorting Nested List with Object List


code:
def get_context_data(self, form, **kwargs):
    context = super(PaperworkWizard, self).get_context_data(form=form, **kwargs)

    #this line makes camp available as a var in the template context.
    #it sets it to an OBJECT RETRIEVED USING THE SLUG FROM THE URL.
    context.update({'camp': Camp.objects.get(slug=self.kwargs.get("slug"))})
    return context

rawDescription: How can I access the URL params and use them if I'm using a formwizard in Django?

description: Access the URL params and use them if I'm using a formwizard in Django


code:
def timer(m):
    i = 0
    while True:
        sys.stdout.write('\r%s\b%d' % (m,i))
        sys.stdout.flush()
        sleep(1)
        sys.stdout.write('\r  \b')
        i = i + 1

rawDescription: A timer to the infinity

description: A timer to the infinity


code:
import csv
from collections import defaultdict

d = defaultdict(list)

with open('Links.csv', 'rb') as inf:
    reader = csv.reader(inf, delimiter=',', quoting=csv.QUOTE_NONE)
    for row in reader:
        d[row[2]].append(row)

with open('outfile.csv', 'w') as outf:
    for k,v in d.iteritems():
        outf.write('{}\n'.format(k))
        for i in v:
            up = '{1},Establish {4} link {0} at {3} Kbps to {2}.\n'
            down = up.replace('Establish','Terminate').replace('to','from')
            outf.write(up.format(*i))
            outf.write(down.format(*i))
        outf.write('\n')

rawDescription: Reading CSV, writing information to new CSV and sorting by column


code:
myfunc = mylib.myfunc
myfunc.restype = c_int
myfuncFUNCTYPE = CFUNCTYPE(STRING)
myfunc.argtypes = [POINTER(c_char), callbackFUNCTYPE]

def mycb(result, userdata):
    userdata.append(result)

input="A large chunk of data."
userdata = []
myfunc(input, myfuncFUNCTYPE(lambda x: mycb(x, userdata)))

rawDescription: Python objects as userdata in ctypes callback functions


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

def func(x, y, z):
    return x ** 2 + y ** 2 + z ** 2

# Nx, Ny, Nz = 181, 181, 421
Nx, Ny, Nz = 18, 18, 42

subsample = 2
Mx, My, Mz = Nx // subsample, Ny // subsample, Nz // subsample

# Define irregularly spaced arrays
x = np.random.random(Nx)
y = np.random.random(Ny)
z = np.random.random(Nz)

# Compute the matrix D of shape (Nx, Ny, Nz).
# D could be experimental data, but here I'll define it using func
# D[i,j,k] is associated with location (x[i], y[j], z[k])
X_irregular, Y_irregular, Z_irregular = (
    x[:, None, None], y[None, :, None], z[None, None, :])
D = func(X_irregular, Y_irregular, Z_irregular)

# Create a uniformly spaced grid
xi = np.linspace(x.min(), x.max(), Mx)
yi = np.linspace(y.min(), y.max(), My)
zi = np.linspace(y.min(), y.max(), Mz)
X_uniform, Y_uniform, Z_uniform = (
    xi[:, None, None], yi[None, :, None], zi[None, None, :])

# To use griddata, I need 1D-arrays for x, y, z of length
# len(D.ravel()) = Nx*Ny*Nz.
# To do this, I broadcast up my *_irregular arrays to each be
# of shape (Nx, Ny, Nz)
# and then use ravel() to make them 1D-arrays
X_irregular, Y_irregular, Z_irregular = np.broadcast_arrays(
    X_irregular, Y_irregular, Z_irregular)
D_interpolated = interpolate.griddata(
    (X_irregular.ravel(), Y_irregular.ravel(), Z_irregular.ravel()),
    D.ravel(),
    (X_uniform, Y_uniform, Z_uniform),
    method='linear')

print(D_interpolated.shape)
# (90, 90, 210)

# Make plots
fig, ax = plt.subplots(2)

# Choose a z value in the uniform z-grid
# Let's take the middle value
zindex = Mz // 2
z_crosssection = zi[zindex]

# Plot a cross-section of the raw irregularly spaced data
X_irr, Y_irr = np.meshgrid(sorted(x), sorted(y))
# find the value in the irregular z-grid closest to z_crosssection
z_near_cross = z[(np.abs(z - z_crosssection)).argmin()]
ax[0].contourf(X_irr, Y_irr, func(X_irr, Y_irr, z_near_cross))
ax[0].scatter(X_irr, Y_irr, c='white', s=20)
ax[0].set_title('Cross-section of irregular data')
ax[0].set_xlim(x.min(), x.max())
ax[0].set_ylim(y.min(), y.max())

# Plot a cross-section of the Interpolated uniformly spaced data
X_unif, Y_unif = np.meshgrid(xi, yi)
ax[1].contourf(X_unif, Y_unif, D_interpolated[:, :, zindex])
ax[1].scatter(X_unif, Y_unif, c='white', s=20)
ax[1].set_title('Cross-section of downsampled and interpolated data')
ax[1].set_xlim(x.min(), x.max())
ax[1].set_ylim(y.min(), y.max())

plt.show()

rawDescription: Interpolation/subsampling of 3D data in python without VTK


code:
res = df.groupby(list(df.columns)).size()
res
res.index = [tuple(x) for x in res.index]
res = res.reset_index().set_index(0)
res

rawDescription: count the tuples of a pandas Dataframe

description: Count the tuples of a pandas Dataframe


code:
addresses = []

soup = BeautifulSoup(response, "lxml" );

for address in soup.find_all ('span', {'itemprop' : 'address'}):
    addresses.append(address.text)

file = 'output.csv'
with open(file,'wb') as f:
    writer = csv.writer(f)
    writer.writerow(addresses)

rawDescription: Python For loop and BeautifulSoup results into CSV


code:
with open('input_file', 'r') as input_file:
    with open('output_file', 'w') as output_file:
        for line in input_file:
            if should_delete(line):
                pass
            else:
                output_file.write(line)

rawDescription: How to effectively remove a line in the middle of a large file?

description: Effectively remove a line in the middle of a large file


rawDescription: How can I play a .wav (or any other kind of file) file? Like actual audio output

description: Play a .wav (or any other kind of file) file? Like actual audio output


code: sub = subprocess.call(['sed', 's/=/ = /g;s/=  = /==/g', infilename], stdout = outfile)

rawDescription: In python, execute multiple terminal commands on a same file and save all the changes once


code:
counter = 0
keys = {}
while True:
    for event in pygame.event.get() :
        if event.type == pygame.KEYDOWN :
            keys[event.key] = counter
        elif event.type == pygame.KEYUP :
            diff[event.key] = keys.pop(event.key) - counter
    counter += 1

rawDescription: Fastest way to sum two lists of items with each other in python


code:
from selenium.webdriver.firefox import webdriver
from datetime import datetime
import time

#to install 'apscheduler' use this command 'pip install apscheduler'
from apscheduler.scheduler import Scheduler

#init the scheduler
sched = Scheduler()

starttime=time.time()

driver = webdriver.WebDriver()
driver.get('https://www.google.co.uk/finance?q=LON:SXX')

#here we are decor the logger function

@sched.interval_schedule(seconds=1)
def logger():
    with open("log.txt", "a") as f:
        # get the current price & time
        tme = datetime.now().strftime('%H:%M:%S')
        current_price = [price.text for price in driver.find_elements_by_xpath('//*[@id="price-panel"]/div[1]/span') if price.text]
        volume = [vol.text for vol in driver.find_elements_by_xpath('/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/table[1]/tbody/tr[4]/td[2]') if vol.text]
        symbol = [sym.text for sym in driver.find_elements_by_xpath('/html/body/div/div/div[3]/div[1]/div/div[2]/div[2]/span') if sym.text]
        f.write ("%s - %s - %s - %s \n" % (symbol, current_price, volume, tme))
        f.close

sched.start()

rawDescription: Initial delay whilst looping a function every n seconds in Python 2.7


code:
import yweather
client = yweather.Client()
client.fetch_woeid('Australia')

rawDescription: How to find woeid (where on earth id) of a country

description: Find woeid (where on earth id) of a country


code:
results = [
    {'name': 'Sam', 'code': 'CDZ', 'cat_name': 'category1', 'cat_code': 'GROUP1'},
    {'name': 'James', 'code': 'CDF', 'cat_name': 'category1', 'cat_code': 'GROUP1'},
    {'name': 'Ellie', 'code': 'CDT', 'cat_name': 'category2', 'cat_code': 'GROUP2'}]
from collections import defaultdict
D = defaultdict(list)
for item in results:
 D[item['cat_code'], item['cat_name']].append((item['code'], item['name']))

import pprint
pprint.pprint(dict(D))

rawDescription: sorting and grouping multiple dictionaries in python

description: Sorting and grouping multiple dictionaries in python


code:
import numpy as np
import scipy
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_palette("hls", 1)
data = np.random.randn(30)
p=sns.kdeplot(data, shade=True)

x,y = p.get_lines()[0].get_data()

#care with the order, it is first y
#initial fills a 0 so the result has same length than x
cdf = scipy.integrate.cumtrapz(y, x, initial=0)

nearest_05 = np.abs(cdf-0.5).argmin()

x_median = x[nearest_05]
y_median = y[nearest_05]

plt.vlines(x_median, 0, y_median)
plt.show()

rawDescription: How to locate the median in a (seaborn) KDE plot?

description: Locate the median in a (seaborn) KDE plot


code: [status,cmdout] = system( 'C:\python27\python.exe python.py input.xlsx' );

rawDescription: Running multiple m-files by batch on Windows OS "without" closing previously run m-file


code:
class MyTable(Base):
    __tablename__ = 'my_table'

    id = Column(Integer, primary_key=True)
    code = Column(String(4))
    name = Column(String(10))

    @validates('code', 'name')
    def validate_code(self, key, value):
        max_len = getattr(self.__class__, key).prop.columns[0].type.length
        if value and len(value) > max_len:
            return value[:max_len]
        return value

rawDescription: Truncating too long varchar when inserting to MySQL via SQLAlchemy


code:
from collections import Counter
from string import ascii_letters

def get_word_frequencies(filename):
    with open(filename) as f:
        c = Counter(f.read())
    return {k:v for k,v in c.items() if k in ascii_letters}

c = get_word_frequencies('derp.py')

print(c)
# {'o': 12, 'h': 1, 'C': 2, 't': 16, 'i': 18, 'y': 1, 'u': 5, 'f': 11, 'p': 6,
# 'v': 2, 'c': 10, 'm': 7, 'n': 13, 'k': 3, 'd': 5, 'a': 6, 'q': 2, 'w': 3,
# 's': 10, 'g': 3, 'r': 19, 'l': 6, 'e': 25}

rawDescription: Split characters from txt file

description: Split characters from txt file


code:
import pygame, random, os, sys

pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()

width = 640
height = 480

black = (  0,  0,  0)
white = (255,255,255)

playerimg = pygame.image.load("images/shipup.png")

class player: # Class for player ship
    def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img
        self.lives = lives
        self.angle = angle
        self.direc = direc

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc

playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")

bulletxc = 0
bulletyc = 0

class bullet: # Class for shooting bullets
    def __init__(self, x, y, xc, yc, w, h, img):
        self.x = x
        self.y = y
        self.xc = xc
        self.yc = yc
        self.w = w
        self.h = h
        self.img = img

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))
        self.x += self.xc
        self.y += self.yc
        if playership.direc == "right": # Determines what direction to shoot
            bulletxc = 6
            bulletyc = 0
        if playership.direc == "left":
            bulletxc = -6
            bulletyc = 0
        if playership.direc == "up":
            bulletyc = -6
            bulletxc = 0
        if playership.direc == "down":
            bulletyc = 6
            bulletxc = 0

bulletlist = bullet(-33, -33, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()

def mainloop():
    global bulletlist
    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

                                                # Basic movements V
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    playership.yc = -3
                    playership.img = pygame.image.load("images/shipup.png")
                    bulletyc = -6
                    bulletxc = 0

                if event.key == pygame.K_s:
                    playership.yc = 3
                    playership.img = pygame.image.load("images/shipdown.png")
                    bulletyc = 6
                    bulletxc = 0

                if event.key == pygame.K_d:
                    playership.xc = 3
                    playership.img = pygame.image.load("images/shipright.png")
                    bulletxc = 6
                    bulletyc = 0

                if event.key == pygame.K_a:
                    playership.xc = -3
                    playership.img = pygame.image.load("images/shipleft.png")
                    bulletxc = -6
                    bulletyc = 0

                if event.key == pygame.K_SPACE: # Here is where the bullet fires.
                    bulletlist = bullet(playership.x + 15, playership.y + 15, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    playership.yc = 0

                if event.key == pygame.K_s:
                    playership.yc = 0

                if event.key == pygame.K_d:
                    playership.xc = 0

                if event.key == pygame.K_a:
                    playership.xc = 0

        gameDisplay.fill(black)

        playership.update()
        bulletlist.update()

        if playership.x < 0: # Collisions against the sides of the screen.
            playership.x = -1

        if playership.y < 0:
            playership.y = -1

        if playership.x + playership.h > width:
            playership.x = width - playership.h

        if playership.y + playership.h > height:
            playership.y = height - playership.h

        pygame.display.update()
        clock.tick(60)

if __name__ == "__main__":
    mainloop()

pygame.quit()
quit()

rawDescription: Bullets not showing up when trying to fire in pygame


code:
import os

dir='e:\www\files\delivery'

for root, dirs, files in os.walk(dir):
  for name in files:
    if name.endswith(("_thumb.jpg")):
      os.remove(os.path.join(root, name))

rawDescription: Loop through directory, delete

description: Loop through directory, delete


code:
def cross(series, cross=0, direction='cross'):
    """
    Given a Series returns all the index values where the data values equal
    the 'cross' value.

    Direction can be 'rising' (for rising edge), 'falling' (for only falling
    edge), or 'cross' for both edges
    """
    # Find if values are above or bellow yvalue crossing:
    above=series.values > cross
    below=np.logical_not(above)
    left_shifted_above = above[1:]
    left_shifted_below = below[1:]
    x_crossings = []
    # Find indexes on left side of crossing point
    if direction == 'rising':
        idxs = (left_shifted_above & below[0:-1]).nonzero()[0]
    elif direction == 'falling':
        idxs = (left_shifted_below & above[0:-1]).nonzero()[0]
    else:
        rising = left_shifted_above & below[0:-1]
        falling = left_shifted_below & above[0:-1]
        idxs = (rising | falling).nonzero()[0]

    # Calculate x crossings with interpolation using formula for a line:
    x1 = series.index.values[idxs]
    x2 = series.index.values[idxs+1]
    y1 = series.values[idxs]
    y2 = series.values[idxs+1]
    x_crossings = (cross-y1)*(x2-x1)/(y2-y1) + x1

    return x_crossings

# Test it out:
time = [0, 0.1, 0.21, 0.31, 0.40, 0.49, 0.51, 0.6, 0.71, 0.82, 0.93]
voltage = [1,  -1,  1.1, -0.9,    1,   -1,  0.9,-1.2, 0.95, -1.1, 1.11]
df = DataFrame(data=voltage, index=time, columns=['voltage'])
x_crossings = cross(df['voltage'])
y_crossings = np.zeros(x_crossings.shape)
plt.plot(time, voltage, '-ob', x_crossings, y_crossings, 'or')
plt.grid(True)

rawDescription: Calculating crossing (intercept) points of a Series or DataFrame


rawDescription: Removing last 2D array from 3D array

description: Removing last 2D array from 3D array


code:
import time
import ctypes
import ctypes.wintypes

SEE_MASK_NOCLOSEPROCESS = 0x00000040
SEE_MASK_INVOKEIDLIST = 0x0000000C

class SHELLEXECUTEINFO(ctypes.Structure):
    _fields_ = (
        ("cbSize",ctypes.wintypes.DWORD),
        ("fMask",ctypes.c_ulong),
        ("hwnd",ctypes.wintypes.HANDLE),
        ("lpVerb",ctypes.c_char_p),
        ("lpFile",ctypes.c_char_p),
        ("lpParameters",ctypes.c_char_p),
        ("lpDirectory",ctypes.c_char_p),
        ("nShow",ctypes.c_int),
        ("hInstApp",ctypes.wintypes.HINSTANCE),
        ("lpIDList",ctypes.c_void_p),
        ("lpClass",ctypes.c_char_p),
        ("hKeyClass",ctypes.wintypes.HKEY),
        ("dwHotKey",ctypes.wintypes.DWORD),
        ("hIconOrMonitor",ctypes.wintypes.HANDLE),
        ("hProcess",ctypes.wintypes.HANDLE),
    )

ShellExecuteEx = ctypes.windll.shell32.ShellExecuteEx
ShellExecuteEx.restype = ctypes.wintypes.BOOL

sei = SHELLEXECUTEINFO()
sei.cbSize = ctypes.sizeof(sei)
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_INVOKEIDLIST
sei.lpVerb = "properties"
sei.lpFile = "C:\\Desktop\\test.txt"
sei.nShow = 1
ShellExecuteEx(ctypes.byref(sei))
time.sleep(5)

rawDescription: Show Explorer's properties dialog for a file in Windows


code:
import csv
with open('/path/to/your/csv/file') as fobj:
    reader = csv.DictReader(fobj)
    for row in reader:
        valid = checkUrl(row['url_to_check'])
        print('%s is %svalid' % (row['url_to_check'], '' if valid else 'in'))

rawDescription: Check multiple url from csv if valid or not, using python


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

df = pd.read_csv('species.csv')
df = df.dropna() # Drop the "no hits" line
df['A'] = np.random.rand(len(df)) * 100 + 1

# Do the summing to get the values for each layer
def nested_pie(df):

    cols = df.columns.tolist()
    outd = {}
    gb = df.groupby(cols[0], sort=False).sum()
    outd[0] = {'names':gb.index.values, 'values':gb.values}
    for lev in range(1,7):
        gb = df.groupby(cols[:(lev+1)], sort=False).sum()
        outd[lev] = {'names':gb.index.levels[lev][gb.index.labels[lev]].tolist(),
                     'values':gb.values}
    return outd

outd = nested_pie(df)
diff = 1/7.0

# This first pie chart fill the plot, it's the lowest level
plt.pie(outd[6]['values'], labels=outd[6]['names'], labeldistance=0.9,
        colors=plt.style.library['bmh']['axes.color_cycle'])
ax = plt.gca()
# For each successive plot, change the max radius so that they overlay
for i in np.arange(5,-1,-1):
    ax.pie(outd[i]['values'], labels=outd[i]['names'],
           radius=np.float(i+1)/7.0, labeldistance=((2*(i+1)-1)/14.0)/((i+1)/7.0),
           colors=plt.style.library['bmh']['axes.color_cycle'])
ax.set_aspect('equal')

rawDescription: Hierarchic pie/donut chart from Pandas DataFrame using bokeh or matplotlib


code:
filter_args = {}
filter_args['date'] = my_date
filter_args['sum'] = my_sum
if request.POST.get('operators'):
    filter_args['operator'] = MyForm.cleaned_data['operators']
payments = Transactions.objects.filter(**filter_args)

rawDescription: Single QuerySet to select some records or all records


code: df.to_csv('filename.csv', sep=',' , header=False , index=False)

rawDescription: How do you remove the column name row from a pandas DataFrame?

description: Remove the column name row from a pandas DataFrame


rawDescription: WxPython AddSpacer

description: WxPython AddSpacer


code:
import sys
import json
import xlwt

data = []

for line in open(sys.argv[1]):
    try:
        data.append(json.loads(line))
    except:
        pass

authors = [i['user']['screen_name'] for i in data]
lang = [i['lang'] for i in data]

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet 1')

column_number = 0
worksheet.write(0, column_number, 'Authors')
for row_number, item in enumerate(authors):
    worksheet.write(row_number+1, column_number, item)

column_number = 1
worksheet.write(0, column_number, 'Language')
for row_number, item in enumerate(lang):
    worksheet.write(row_number+1, column_number, item)

workbook.save('tweets.xls')

rawDescription: Write column headers with xlwtin Python

description: Write column headers with xlwtin Python


code: dist_python_DATA = foo.py

rawDescription: How can I disable generation of pyc and pyo files?

description: Disable generation of pyc and pyo files


rawDescription: numpy stack rows of ndarray

description: Numpy stack rows of ndarray


code:
import threading
import random, time
import gtk
#Initializing the gtk's thread engine
gtk.threads_init()

class FractionSetter(threading.Thread):
    """This class sets the fraction of the progressbar"""

    #Thread event, stops the thread if it is set.
    stopthread = threading.Event()

    def run(self):
        """Run method, this is the code that runs while thread is alive."""

        #Importing the progressbar widget from the global scope
        global progressbar

        #While the stopthread event isn't setted, the thread keeps going on
        while not self.stopthread.isSet() :
            # Acquiring the gtk global mutex
            gtk.threads_enter()
            #Setting a random value for the fraction
            progressbar.set_fraction(random.random())
            # Releasing the gtk global mutex
            gtk.threads_leave()

            #Delaying 100ms until the next iteration
            time.sleep(0.1)

    def stop(self):
        """Stop method, sets the event to terminate the thread's main loop"""
        self.stopthread.set()

def main_quit(obj):
    """main_quit function, it stops the thread and the gtk's main loop"""
    #Importing the fs object from the global scope
    global fs
    #Stopping the thread and the gtk's main loop
    fs.stop()
    gtk.main_quit()

#Gui bootstrap: window and progressbar
window = gtk.Window()
progressbar = gtk.ProgressBar()
window.add(progressbar)
window.show_all()
#Connecting the 'destroy' event to the main_quit function
window.connect('destroy', main_quit)

#Creating and starting the thread
fs = FractionSetter()
fs.start()

gtk.main()

rawDescription: Multithreading in pygtk

description: Multithreading in pygtk


code:
def sample():
    linesamples = set()
    infile = open(args.infile, 'r')
    outputfile = open(args.outputfile, 'w')
    # count the number of fastq "chunks" in the input file:
    seqs = subprocess.check_output(["grep", "-c", "@", str(args.infile)])
    # randomly select n fastq "chunks":
    seqsamples = random.sample(xrange(0,int(seqs)), int(args.n))
    for i in seqsamples:
        linesamples.add(int(4*i+0))
        linesamples.add(int(4*i+1))
        linesamples.add(int(4*i+2))
        linesamples.add(int(4*i+3))
    # make a list of the lines that are to be fetched from the fastq file:
    # fetch lines from input file and write them to output file.
    line_num = 0
    for line in infile:
        if line_num in linesamples:
            outputfile.write(line)
        line_num += 1
    outputfile.close()

rawDescription: How can I make my Python script faster?

description: Make my Python script faster


code:
import matplotlib.pyplot as plt

N = {1: 12, 2: 15, 3: 8, 4: 4, 5: 1}
plt.bar(N.keys(), N.values(), align='center')
plt.show()

rawDescription: How do I create a histogram from a hashmap in python?

description: Create a histogram from a hashmap in python


code: plt.plot([percentage(tts, vol) for vol, tts in zip(Volume, TTS)])

rawDescription: one list a percentage of another

description: One list a percentage of another


rawDescription: Fastest way to convert an iterator to a list


code:
min_value = np.iinfo(im.dtype).min
max_value = np.iinfo(im.dtype).max

rawDescription: Maximum allowed value for a numpy data type


code:
class Man(Human):
    def __init__(self):
        super(Man, self).__init__()
        print("Hi this is Man Constructor")

class Woman(Human):
    def __init__(self):
        super(Woman, self).__init__()
        print("Hi this is Woman Constructor")

rawDescription: Python Constructor Chaining and Polymorphism


code:
def get_random_point_in_polygon(region, urban=True):
    while True:
        address = ogr.Geometry(ogr.wkbPoint)
        address.AddPoint(random.uniform(region.get_region().geometry().GetEnvelope()[0],
                                    region.get_region().geometry().GetEnvelope()[1]),
                     random.uniform(region.get_region().geometry().GetEnvelope()[2],
                                    region.get_region().geometry().GetEnvelope()[3]))
        if region.get_region().geometry().Contains(address):
            if urban is True:
                for item in ShapesInput.urban:
                    if item.geometry().Contains(address):
                        return address
            else:
                return address

rawDescription: How to check in a list of shapefiles whether a point falls inside AND it is the same of a given FIELD?

description: Check in a list of shapefiles whether a point falls inside AND it is the same of a given FIELD


code: subprocess.call("<bash command here>", shell = True)

rawDescription: Python (3.4) run SSH commands on local terminal


code:
class SomeObj:
    pass

obj1=SomeObj()
obj1.items=[1, 2, 3]
obj2=SomeObj()
obj2.items=[4, 5, 6]
obj_list=[obj1, obj2]
[obj.items for obj in obj_list]
[item for obj in obj_list for item in obj.items]
import itertools
list(itertools.chain(*[obj.items for obj in obj_list]))

rawDescription: How to get a flat list from a nested list using comprehension?

description: Get a flat list from a nested list using comprehension


code: [ord(c) for c in '\x0f\x07UUU']

rawDescription: Converting PySerial Readline From String to Binary


code:
x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
y = x.view(np.recarray)
type(x), type(y)

rawDescription: is there any way to determine if an numpy array is record/structure array?

description: Is there any way to determine if an numpy array is record/structure array


code:
def get_child_perms(self, folder, request, perm_list):
    # Folder contains other folders
    children =  folder.get_children()
    if children:
        # For every sub-folder
        return [self.get_child_perms(subfolder, request, perm_list)
                for subfolder in children]
    return [folder.has_read_permission(request)]

rawDescription: Python recursively appending list function


code:
import gc

def objects_by_id(id_):
    for obj in gc.get_objects():
        if id(obj) == id_:
            return obj
    raise Exception("No found")

rawDescription: Converting a memory address to a function object


code:
def myview(request):
    # do stuff
    option_checked = 'myvariable.checkboxoption' in request.REQUEST
    # do more stuff and return the response

rawDescription: Validating checkboxes from form in Django


code:
from django.forms import forms

class MyBoundField(object):
    pass

forms.BoundField = MyBoundField

rawDescription: How to I modify a class of a library to get it to use my extension of another library class?


code:
def f(): pass
g = f
f.__name__
g.__name__

rawDescription: How to get the function name as string in Python?

description: Get the function name as string in Python


code:
import numpy as np
import pandas as pd
x = pd.DataFrame(np.arange(1,13).reshape(2,-1))
y = pd.DataFrame(np.arange(1,13).reshape(-1,2).T)
x
y

rawDescription: How to get the same data.frame with Data.Frame in pandas module of python?

description: Get the same data.frame with Data.Frame in pandas module of python


code:
import re

old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear'

with open('fruits.out', 'w') as f:
    fruits = re.findall(r'[^\W\d]+', old_fruits)
    f.write('\n'.join(fruits))

rawDescription: Python: Retain only words in string, each on newline


code:
import codecs
import json
import os

def startupCheck():
    if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
        # checks if file exists
        print ("File exists and is readable")
    else:
        print ("Either file is missing or is not readable, creating file...")
        with codecs.open(os.path.join(PATH, 'Accounts.json'), 'w') as db_file:
            db_file.write(json.dumps({}))

rawDescription: Python: Checking for JSON files and creating one if needed


code:
sql = "INSERT INTO mytable (a,b,c) VALUES (%(qwe)s, %(asd)s, %(zxc)s);"
data = {'qwe':1, 'asd':2, 'zxc':None}

conn = MySQLdb.connect(**params)

cursor = conn.cursor()
cursor.execute(sql, data)
cursor.close()

conn.close()

rawDescription: Quickest way to dump Python dictionary (dict) object to a MySQL table?


code:
import logging

fileh = logging.FileHandler('/tmp/logfile', 'a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fileh.setFormatter(formatter)

log = logging.getLogger()  # root logger
for hdlr in log.handlers:  # remove all old handlers
    log.removeHandler(hdlr)
log.addHandler(fileh)      # set the new handler

rawDescription: How to change filehandle with Python logging on the fly with different classes and imports

description: Change filehandle with Python logging on the fly with different classes and imports


code:
from itertools import product
list(product(A,B,C))

rawDescription: Generate permutation among several lists


code:
#Dev by Mkee
import Tkinter as tk
import sys

class Shuffle(object):
    def __init__(self,master=None):
        self.master=master
        self.text=tk.Text(master)
    def hide(self):
        self.text.pack_forget()
    def show(self):
        self.text.pack(side=tk.LEFT, padx=5, pady=5)

class Buttons(object):
    def __init__(self,master=None):
        self.master=master
        self.red = tk.Button(self.master, text="Red", bg="red", fg="white")
        self.green = tk.Button(self.master, text="Green", bg="green", fg="black")
        self.blue = tk.Button(self.master, text="Blue", bg="blue", fg="white")
    def hide(self):
        self.red.pack_forget()
        self.green.pack_forget()
        self.blue.pack_forget()
    def show(self):
        self.red.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
        self.green.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
        self.blue.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)

class MainApp(object):
    def __init__(self,master=None):
        self.master=master
        app=self.app=tk.Tk()
        app.title("Mkee's Tools")
        app.geometry('300x200')
        self.shuffle=Shuffle(master)
        self.buttons=Buttons(master)
        self.current=None
        menubar=tk.Menu(app)
        program_menu=tk.Menu(menubar)
        program_menu.add_command(label='Shuffle',
                                 command=lambda: self.show(self.shuffle))
        program_menu.add_command(label='Buttons',
                                 command=lambda: self.show(self.buttons))
        program_menu.add_command(label='Quit',command=sys.exit)
        menubar.add_cascade(label='Programs', menu=program_menu)
        app.config(menu=menubar)
    def show(self,obj):
        if self.current != obj:
            try: self.current.hide()
            except AttributeError: pass
            self.current=obj
            obj.show()

def main():
    m=MainApp()
    m.app.mainloop()

if __name__=='__main__':
    main()

rawDescription: Tkinter (python 2.7.2) Help needed

description: Tkinter (python 2.7.2) Help needed


code: persons_list = [person for person in persons_list if person.sexe != 'Male']

rawDescription: Delete element from list where sub-element matches string


code:
from collections import Counter
from itertools import chain

mylist = [
    [1,2,3,4],
    [2,5,6,7,7],
    [4,2,8,9]
]

counts = Counter(chain(*map(set,mylist)))

[[i for i in sublist if counts[i]==1] for sublist in mylist]
#[[1, 3], [5, 6, 7, 7], [8, 9]]

rawDescription: Cleanest way to remove common list elements across multiple lists in python


code:
areas = relationship('DbArea',
    backref = backref('routes', order_by = 'DbRoute.name'),
    secondary = area_relationship_table,
    primaryjoin = area_id == area_relationship_table.c.descendent,
    secondaryjoin = DbArea.area_id == area_relationship_table.c.ancestor,
    innerjoin = True, order_by = DbArea.name,
    foreign_keys =
       [area_relationship_table.c.ancestor,
            area_relationship_table.c.descendent])

rawDescription: SQL Alchemy Closure Table Relationship Definition


code:
import unittest
import sys
import pdb
import functools
import traceback
def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                info = sys.exc_info()
                traceback.print_exception(*info)
                pdb.post_mortem(info[2])
        return wrapper
    return decorator

class tests(unittest.TestCase):
    @debug_on()
    def test_trigger_pdb(self):
        assert 1 == 0

rawDescription: Python Unit Testing: Automatically Running the Debugger when a test fails


code:
from tkinter import *
import csv

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()

    def output(self):
        Label(text='Name:').pack(side=LEFT,padx=5,pady=5)
        self.e = Entry(root, width=10)
        self.e.pack(side=LEFT,padx=5,pady=5)

        self.b = Button(root, text='Submit', command=self.writeToFile)
        self.b.pack(side=RIGHT,padx=5,pady=5)

    def writeToFile(self):
        with open('WorkOrderLog.csv', 'a') as f:
            w=csv.writer(f, quoting=csv.QUOTE_ALL)
            w.writerow([self.e.get()])

if __name__ == "__main__":
    root=Tk()
    root.title('Auto Logger')
    root.geometry('1000x100')
    app=App(master=root)
    app.mainloop()
    root.mainloop()

rawDescription: Using Tkinter to export user input to a .csv file


code:
import binascii
import struct

text='8192323E'
print(struct.unpack('<f',binascii.unhexlify(text))[0])
# 0.17438699305057526

rawDescription: Single precision big endian float values to Python's float (double precision, big endian)


code:
import shlex
import StringIO # use in place of files

list(shlex.shlex(StringIO.StringIO('Some tokens. 123, 45.67 "A string with whitespace"')))

rawDescription: How to get the next token (int, float or string) from a file in Python?

description: Get the next token (int, float or string) from a file in Python


code:
sample = os.popen('dmesg').read()

# Sample
sample = """
-random long line ---long line----
size of : 12
mydata : 0x5b
mydata : 0xa8
mydata : 0xcc
mydata : 0x18
mydata : 0x15
mydata : 0x18 --random line--
"""

concat_out = ''.join([word.replace("0x","")
                      for word in sample.split()
                      if word.startswith("0x")])
print(concat_out)

rawDescription: Find specific string, then copy the word after it and concatenate it to a variable


code:
import Tkinter
Tkinter.Tk().bell()

rawDescription: Can I get messagebox's icon="warning" noise on a custom Tkinter Toplevel() window?

description: Get messagebox's icon="warning" noise on a custom Tkinter Toplevel() window


code:
import rpy2.robjects as robjects
FX= robjects.r('''
         pstable(seq(-2,4,0.1),
                     alpha =alfa_x,
                     beta = -1,
                     gamma = scale_x,
                     delta = delta_x,
                     pm = 1, lower.tail = TRUE,
                     log.p = FALSE, subdivisions = 1000)
           ''')

rawDescription: UsageError: unrecognized arguments: in %R line


code:
import random
a = range(1,11)
a.remove(3)
a.remove(4)
a
random.choice(a)

rawDescription: How to print a random number from a set, except from one or two of the numbers?

description: Print a random number from a set, except from one or two of the numbers


code:
# ParkingLots with even numbered lot_numbers
ParkingLot.objects.annotate(odd=F('lot_number') % 2).filter(odd=False)

# ParkingLots with odd numbered lot_numbers
ParkingLot.objects.annotate(odd=F('lot_number') % 2).filter(odd=True)

rawDescription: Django QuerySet querying or filtering "Odd" and/or "Even" value in a particular field


code:
import ctypes
x = ctypes.create_string_buffer('howdy!')
x.value
x[0] = 'C'
x.value

rawDescription: ctypes buffer modification

description: Ctypes buffer modification


code:
def __metaclass__(cls, bases, attrs):
    type.__new__(metaclassGoesHere, cls, bases, attrs)

rawDescription: Python metaclass to run a class method automatically on derived class


code:
from collections import defaultdict
get_group = lambda x: x % 4
d = defaultdict(list)
for value in range(10):
    d[get_group(value)].append(value)

dict(d)

rawDescription: linked list in python

description: Linked list in python


code:
def our_special_filter( some_queryset ):
    for row in some_queryset:
        if row.aMethod(): yield row

result= our_special_filter( SomeModel.objects.filter(...) )

rawDescription: Django - custom filter that calls a method


code:
from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

A = -0.75, -0.25, 0, 0.25, 0.5, 0.75, 1.0
B = 0.73, 0.97, 1.0, 0.97, 0.88, 0.73, 0.54

plt.plot(A,B)
for xy in zip(A, B):                                       # <--
    ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') # <--

plt.grid()
plt.show()

rawDescription: Label python data points on plot

description: Label python data points on plot


code:
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

def draw_circle(grid, x0, y0, radius):
    ny, nx = grid.shape
    y, x = np.ogrid[:ny, :nx]
    dist = np.hypot(x - x0, y - y0)
    grid[dist < radius] = True
    return grid

# Generate 3 circles...
a = np.zeros((512, 512), dtype=np.bool)
draw_circle(a, 100, 200, 30)
draw_circle(a, 400, 350, 20)
draw_circle(a, 200, 260, 20)

# Label the objects in the array.
labels, numobjects = ndimage.label(a)

# Now find their bounding boxes (This will be a tuple of slice objects)
# You can use each one to directly index your data.
# E.g. a[slices[0]] gives you the original data within the bounding box of the
# first object.
slices = ndimage.find_objects(labels)

#-- Plotting... -------------------------------------
fig, ax = plt.subplots()
ax.imshow(a)
ax.set_title('Original Data')

fig, ax = plt.subplots()
ax.imshow(labels)
ax.set_title('Labeled objects')

fig, axes = plt.subplots(ncols=numobjects)
for ax, sli in zip(axes.flat, slices):
    ax.imshow(labels[sli], vmin=0, vmax=numobjects)
    tpl = 'BBox:\nymin:{0.start}, ymax:{0.stop}\nxmin:{1.start}, xmax:{1.stop}'
    ax.set_title(tpl.format(*sli))
fig.suptitle('Individual Objects')

plt.show()

rawDescription: Shape recognition with numpy/scipy (perhaps watershed)


code:
from django.db.models import Count

Tourist.objects.annotate(countries_visited=Count('country'))

rawDescription: Following a relationship backwards, and annotating the results


code:
def foo(self):
   self.subplot.clear()
   hb = self.subplot.hexbin(...)
   if self.cb:
      self.figure.delaxes(self.figure.axes[1])
      self.figure.subplots_adjust(right=0.90)  #default right padding
   self.cb = self.figure.colorbar(hb)

rawDescription: remove colorbar from figure in matplotlib

description: Remove colorbar from figure in matplotlib


code: new_headers = [header for header in headers if '2016' not in header]

rawDescription: Delete index in list if multiple strings are matched


code:
ref = ['k','l','j','i']
to_sort = [0,1,2,3]
sorted((ref.index(r), x) for r, x in zip(sorted(ref), to_sort))
zip(*_)[1]

rawDescription: Shuffling numpy array according to list values


code:
frame = driver.find_element_by_xpath('//frame[@name="main"]')
driver.switch_to.frame(frame)
pass1 = driver.find_element_by_id("PASSFIELD1")

rawDescription: Python with selenium: unable to locate element which really exist


code:
import pandas as pd
d = pd.DataFrame([['AA',4],['BB',3],['CC',2],['DD',1]],columns=['id','frequency'])
k=((' '+d.id)*d.frequency).apply(lambda x: pd.Series(x.split(' '))).stack()
df = pd.DataFrame(k[(k.T != '')])
df.reset_index(inplace=True)
df.columns=['l0','frequency','id']
df[['id','frequency']]

rawDescription: Repeat rows based on field in Pandas

description: Repeat rows based on field in Pandas


code:
mychoice = {1: 0.2, 2: 0.7, 3:0.1}
current = 0
limits = {}

for key in mychoice:
    limits[key] = (current,current + mychoice[key])
    current = current + mychoice[key] #Next range should start at the end of current

#This should give a new dictionary: {1:(0,0.2),2:(0.2,0.9),3;(0.9,1)}

r = random.random() # float between 0 and 1

for key in limits:
    range = limits[key]
    if r >= range[0] and r < range[1]:
          return key
return None

rawDescription: Given a dictionary of weights that sum to 1 - how to use random to select a weighted key


code:
import tkinter
a = tkinter.Tk()
for i in range(10):
   label = tkinter.Label(a, text=str(i))
   label.grid(column=0, row=i)
for label in a.grid_slaves():
   if int(label.grid_info()["row"]) > 6:
      label.grid_forget()

rawDescription: remove widgets from grid in tkinter

description: Remove widgets from grid in tkinter


code: cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED")

rawDescription: Problem with concurrent reading and writing (distinct processes) using MySQL and Python


code:
img=cv.LoadImage("asd.png")
mat=cv.GetMat(img)
mat[3,1]
(83.0, 88.0, 89.0)

rawDescription: Capture Image as Array with Python OpenCV


code: "(" + ", ".join(column_names) + ")"

rawDescription: Pass values in a Python list values without parentheses to variable


code:
from django.http import HttpResponse

def serve_pdf_view(request):
    pdf_data = magically_create_pdf()
    return HttpResponse(pdf_data, content_type='application/pdf')

rawDescription: How can i make the user download the file when page is viewed in django

description: Make the user download the file when page is viewed in django


rawDescription: Get offset of current buffer in vim (in particular, via python scripting)


code:
def foo(a, b, c, *args):
    return len(args)

def bar(a, b, c, **kwargs):
    if kwargs["magicnumber"] == 7:
      return True
    return False

rawDescription: Multiple Function Arguments - learnpython.org


code:
from flask import Flask
from flask.ext.login import LoginManager, UserMixin, login_user, login_required

app = Flask('foo')
app.config['SECRET_KEY'] = 'abcd'
login_manager = LoginManager(app)

class User(UserMixin):
    def get_id(self):
        return 1

    def get_auth_token(self):
        print("get_auth_token called")
        return 'my_auth_token'

@login_manager.token_loader
def my_token_loader(token):
    print("my_token_loader called")
    return User()

@app.route('/login')
def login():
    login_user(User(), remember=True)
    return ''

@app.route('/protected')
@login_required
def protected():
    return ''

with app.test_client() as tc:
    # login will set session cookie as well as remember me cookie
    # User.get_auth_token will be called because we have defined a token_loader
    tc.get('/login')

    # clear the session cookie so the next request will use remember me
    tc.cookie_jar.clear_session_cookies()

    # our token_loader function should be called now as Flask-Login attempts to
    # log us in with the remember me cookie
    tc.get('/protected')

rawDescription: Flask Login - @login_manager.token_loader not being called


code:
def filter(self, record):
    if hasattr(record, 'spider'):
        record.name = record.spider.name
    elif any(record.name.startswith(l + '.') for l in self.loggers):
        record.name = record.name.split('.', 1)[0]
    return True

rawDescription: Multiple Spiders in CrawlerProcess - How to get a log per spider?


code:
import zipfile

with zipfile.ZipFile('/path/to/goodfile.xlsx', 'r') as zgood:
    styles_xml = zgood.read('xl/styles.xml')

with zipfile.ZipFile('/path/to/badfile.xlsx', 'a') as zbad:
    zbad.writestr('xl/styles.xml', styles_xml)

rawDescription: repairing Excel file

description: Repairing Excel file


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

x = np.arange(0, 10, 0.1)
y = np.sin(x)

plt.ion()
ax = plt.gca()
ax.set_autoscale_on(True)
line, = ax.plot(x, y)

for i in xrange(100):
    line.set_ydata(y)
    ax.relim()
    ax.autoscale_view(True,True,True)
    plt.draw()
    y=y*1.1
    plt.pause(0.1)

rawDescription: plotting dynamic data using matplotlib

description: Plotting dynamic data using matplotlib


code: last_digit = str(number)[-1]

rawDescription: Python - substr

description: Python - substr


code:
import pygame,sys,math #---- Import modules we will need

pygame.init() #---- Initialize the module

def get_rad(origin_x,origin_y,x,y): #----- Returns the appropriate radius
    return math.sqrt((origin_x - x)**2 + (origin_y - y)**2) #----- Distance between 2
                                                            #----- points

screen = pygame.display.set_mode((400,400)) #----- Sets up the screen
clock  = pygame.time.Clock() #------- Sets up the clock

mouse_button = 0 #--------- This variable is used to determine whether a mouse button
                 #--------- has been pressed

draw_final_circle = False #---------- This variable lets us know that we should draw the
                          #---------- final circle

while True: #------ main loop

    clock.tick(60) #------ Limit the Fps

    mouse_button0  = mouse_button #-------- This variable holds the previous value of
                                  #-------- mouse_button(it will be useful later)

    mouse_x,mouse_y = pygame.mouse.get_pos() #----- Get the mosue coordinates

    for e in pygame.event.get(): #---- Cycle through events

        if e.type == pygame.QUIT: pygame.quit();sys.exit() #--Quit when window is closed

        if e.type == pygame.MOUSEBUTTONDOWN: #---- If the mouse button is pressed
            if mouse_button == 0: #---- if the mouse button is released
                mouse_button = 1 #----- set it to pressed basically
                originx,originy = mouse_x,mouse_y #---- keep the mouse_x,mouse_y pos

        if e.type == pygame.MOUSEBUTTONUP: #---- if the mouse button is released
             if mouse_button == 1: #-------- if it is pressed
                 mouse_button = 0 #--------- set it to released

    screen.fill((255,255,255)) #---- clear the screen

    #-------- If a mouse button is pressed and a circle can be drawn (rad>width) ------#
    if mouse_button == 1 and get_rad(originx,originy,mouse_x,mouse_y) > 1:

        rad = int(get_rad(originx,originy,mouse_x,mouse_y)) #---- get the radius(as int)
        pos = mouse_x,mouse_y
        pygame.draw.circle(screen,(0,0,0),pos,rad,1) #--- draw the circle

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

    #---------- if the button is released but in the previous loop it was pressed -----#
    if mouse_button == 0 and mouse_button0 == 1:

        draw_final_circle = True #----- set the final circle boolean to True

    if draw_final_circle: #----- if the final circle is decided
        pygame.draw.circle(screen,(0,0,0),pos,rad,1) #---- keep drawing it

    pygame.display.flip() #----- flip the buffer

rawDescription: Pygame - blitting for a preview picture

description: Pygame - blitting for a preview picture


code:
import unicodedata

for char in string:
    status = unicodedata.east_asian_width(char)
    if status == 'F':
         print('{0} is full-width.'.format(char))
    elif status == 'H':
        print('{0} is half-width.'.format(char))

rawDescription: Checking a character is fullwidth or halfwidth in Python


code:
import Tkinter as tk

def onKeyPress(event):
    text.insert('end', 'You pressed %s\n' % (event.char, ))

root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()

rawDescription: Detect key input in Python

description: Detect key input in Python


code:
l = to_split.split("/", 2)
a, b, c = l + [None] * (3 - len(l))

rawDescription: Splitting a string in Python to 3 segments appointed to 3 variables depending on item count


code:
class Meta:
    ordering = ['user__first_name', 'user__last_name']

rawDescription: Sort by field in ForeignKey model

description: Sort by field in ForeignKey model


code:
found = []
with open("/var/log/syslog", "r") as vpnfile:
    for line in vpnfile:
        if "failed CHAP" in line:
            found.append(line)
        # no need to check if "failed CHAP" is not in the line, since you
        # already know it's not there from the first test failing
if found:
    print(" ".join(found))
else:
    print("Nadda")

rawDescription: Need help using Python to parse log file


code:
consumer.register('kind', callback_func)
consumer.wait()
# more code goes here...

rawDescription: py-amqp/flopsy: Waiting for a single AMQP message in Python

description: Py-amqp/flopsy: Waiting for a single AMQP message in Python


rawDescription: Python script to read http paramters from a file


code:
def get_session(customer_id):
    sqlite_url = 'sqlite:////path/customer%s.db' % customer_id
    engine = create_engine(sqlite_url)

    # initialize the db if it hasn't yet been initialized
    Base.metadata.create_all(engine)

    Session = sessionmaker(bind=engine)
    session = Session()

    return session

rawDescription: Flask-Sqlalchemy multiple databases (binds) with the same model (class DB.Model)


code:
def foo(request):
    try:
        bar = 1 + int(request.GET['bar'])
    except:
        bar = 0
    return render('parrot.html', {'bar':bar})

rawDescription: Django Request unpacking and render local template more efficiently


code:
def isUSER1():
  env.user="USER1"
  with settings(abort_on_prompts=True):
    if env.host.find("@")==-1:
      try :
        sudo('whoami')
        env.hosts.remove(env.host)
        fullhost=env.user+'@'+env.host
        env.hosts.append(fullhost)
      except :
        pass

rawDescription: Fabric test username before connecting

description: Fabric test username before connecting


cmd: python -u $file

rawDescription: How to make Sublime Text read PYTHONPATH environment variable on Windows

description: Make Sublime Text read PYTHONPATH environment variable on Windows


code:
def trackerText(self, point):

    if _record:
        self.recording = True
        pos = self.invTransform(point)
        if(len(self.recorded_data)):
            if(eucliddist((pos.x(), pos.y()), self.recorded_data[-1]) > 0.0):
                self.recorded_data.append((pos.x(), pos.y()))
        else:
            self.recorded_data.append((pos.x(), pos.y()))

rawDescription: Recording a curve / polygon from canvas with PyQwt via Mouse


code: np.repeat(coords.reshape(-1, 2), number.ravel(), axis=0)

rawDescription: Using np.repeat on sub-arrays

description: Using np.repeat on sub-arrays


code:
class OldStyle: pass

os = OldStyle(); os.__class__; dir(os)
class NewStyle(object): pass

ns = NewStyle(); ns.__class__; dir(ns)

rawDescription: Something disturbing about PyDev content assist


code:
urls.py
from django.views.generic import UpdateView
from myapp.forms import InfoForm
urlpatterns = patterns('',
    url(r'^info/(?P<pk>[-_\w]+)/update/$', UpdateView.as_view(model= Info,template_name="hello/home.html",form_class=InfoForm), name='info_update'),
)
# you might need to include a success url in the **kwargs i.e. success_url="/thankyou/"

rawDescription: How to use initialize FormModel with request.post and model instance.

description: Use initialize FormModel with request.post and model instance.


code:
import pygame

pygame.mixer.init()
pygame.init()

# Maybe you can subclass the pygame.mixer.Sound and
# add the methods below to it..
class Fader(object):
    instances = []
    def __init__(self, fname):
        super(Fader, self).__init__()
        assert isinstance(fname, basestring)
        self.sound = pygame.mixer.Sound(fname)
        self.increment = 0.01 # tweak for speed of effect!!
        self.next_vol = 1 # fade to 100 on start
        Fader.instances.append(self)

    def fade_to(self, new_vol):
        # you could change the increment here based on something..
        self.next_vol = new_vol

    @classmethod
    def update(cls):
        for inst in cls.instances:
            curr_volume = inst.sound.get_volume()
            # print inst, curr_volume, inst.next_vol
            if inst.next_vol > curr_volume:
                inst.sound.set_volume(curr_volume + inst.increment)
            elif inst.next_vol < curr_volume:
                inst.sound.set_volume(curr_volume - inst.increment)

sound1 = Fader("1.wav")
sound2 = Fader("2.wav")
sound1.sound.play()
sound2.sound.play()
sound2.sound.set_volume(0)

# fading..
sound1.fade_to(0)
sound2.fade_to(1)

while True:
    Fader.update() # a call that will update all the faders..

rawDescription: Fade Between Two Music Tracks in-progress in Pygame


code:
sel = selection()
while sel != 0:
    input()
    if sel==1:
        ...
    sel = selection()

rawDescription: Access a previously returned value - Python3.2


code:
def qsort (list):
    if list == []:
        return []
    else:
        pivot = list [0]
        lesser = qsort([x for x in list[1:] if x<pivot])
        greater = qsort([x for x in list[1:] if x>=pivot])
        return lesser + [pivot] + greater

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

rawDescription: classes and methods in python

description: Classes and methods in python


code:
import audiere
ds = audiere.open_device()
os = ds.open_array(input_array, 44100)
os.play()

rawDescription: Playing a sound from a wave form stored in an array


code:
if sys.platform == "win32":
    base = "Win32GUI"

rawDescription: How to make cx_Freeze python exe windowless

description: Make cx_Freeze python exe windowless


code:
def t_iterating(R, r, p):
    t = 2*pi          # It seems odd to me to start from 2*pi rather than 0.
    down()

    while t < 20*pi:  # This loops while t goes from 2*pi to 20*pi.
        t = t+0.01
        goto(formulaX(R, r, p, t), formulaY(R, r, p, t))
    up()

rawDescription: Spirograph Turtle Python

description: Spirograph Turtle Python


code:
def main():

    while True:
        entry = int(input('Enter a number or 0 to quit:'))
        if entry == 0:
            print('All done!')
            break
        if entry % 2 == 0:
            print(entry, 'is an even number.')
        else:
            print(entry, 'is an odd number.')

main()

rawDescription: write a program that determines if user input is even or odd and loops

description: Write a program that determines if user input is even or odd and loops


code:
for ax in [ax1,ax3]:
    for label in ax.get_yticklabels():
        label.set_visible(False)
for ax in [ax2,ax4]:
    # Change the labelling only
    ax.yaxis.set_tick_params(labelright='on', labelleft='off')

rawDescription: y labels right hand side in matplotlib plot with sharing x and y

description: Y labels right hand side in matplotlib plot with sharing x and y


code:
data = f.read()
c = struct.unpack_from('<8H', data, 16)

rawDescription: Convert Python array('B') to array('H') - always little-endian?


code:
gf = pd.DataFrame({'Id' : gen(l)})
gf.to_csv(results, index=False)

rawDescription: How to save pandas dataframe generated dynmically as column values as .csv file

description: Save pandas dataframe generated dynmically as column values as .csv file


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

carr = np.array([[0,0,0,1],[0,0,1,1],[0,1,0,1]]) # RGBA color array
ax = plt.axes(projection='3d')
h = ax.scatter([1,2,3],[1,2,3],[1,2,3], c=carr, edgecolors=carr)
plt.show()

rawDescription: How to set/get ridge of symbol edges in matplotlib scatterplot?

description: Set/get ridge of symbol edges in matplotlib scatterplot


code:
class MetaContainer():
    def __delitem__(self, key):
        self.__delattr__(key)
    def __getitem__(self, key):
        return self.__getattribute__(key)
    def __setitem__(self, key, value):
        self.__setattr__(key, value)

rawDescription: Implement list-like index access in Python


rawDescription: Get raw POST body in Python Flask regardless of Content-Type header


code:
def hook(obj):
    value = obj.get("timezone_id")
    # this is python 3 specific; I would check isinstance against
    # basestring in python 2
    if value and isinstance(value, str):
        obj["timezone_id"] = json.loads(value, object_hook=hook)
    return obj
data = json.loads(my_input, object_hook=hook)

rawDescription: Python decode nested JSON in JSON

description: Python decode nested JSON in JSON


code:
from tkinter import *
from tkinter import ttk

class Main_window(ttk.Frame):
    """A program"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Creates all the objects in the window"""

        self.min_lbl = ttk.Label(self, text = "1").grid(row = 0, column = 0,
                                                        sticky = W)
        self.max_lbl = ttk.Label(self, text = "100").grid(row = 0, column = 2,
                                                        sticky = W)

        spinval = IntVar()

        self.scale = ttk.Scale(self, orient = HORIZONTAL,
                                       length = 200,
                                       from_ = 1, to = 100,
                                       variable = spinval,
                                       command=self.accept_whole_number_only)
        self.scale.grid(row = 0,column = 1,sticky = W)

        self.spinbox = Spinbox(self, from_ = 1, to = 100,
                                       textvariable = spinval,
                                       command = self.update,
                                       width = 10)
        self.spinbox.grid(row = 0,column =3,sticky = W)

        self.go_bttn = ttk.Button(self, text = "Go",
                                  command = self.create_windows
                                  ).grid(row = 1, column = 1, sticky = W)

    def accept_whole_number_only(self, e=None):
        """Makes the numbers from the scale whole"""
        value = self.scale.get()
        if int(value) != value:
            self.scale.set(round(value))

    def update(self):
        """Updates the scale and spinbox"""
        self.scale.set(self.spinbox.get())

    def create_windows(self):
        """This function will create all the new windows"""
        value = self.scale.get()
        window_num = value
        negative_window_num = 1
        for n in range(int(window_num)):
            child = Toplevel()
            child.title("This is Window "+str(window_num)[:-2]+" of "+str(value)[:-2])
            child.geometry("350x200")
            app = Window_creator(child)

class Window_creator(ttk.Frame):
    """makes child windows"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """creates all the widgets in the window"""

def main():
    """Loops the window"""
    root = Tk()
    root.title("Programm")
    root.geometry("350x200")
    app = Main_window(root)
    root.mainloop()

main()

rawDescription: Create n number of windows in tkinter

description: Create n number of windows in tkinter


code:
import string
for i, x in enumerate(myList):
    x.label = string.uppercase[i]

rawDescription: python - assign letter of the alphabet to each value in a list

description: Python - assign letter of the alphabet to each value in a list


code: new_arr = [x for x in arr if x.startswith(sorted([y[:4] for y in arr])[-1])]

rawDescription: Filter an array and retrieve only strings starting with highest number


code:
N = 10
map(lambda t: map(lambda s: s[:N], t), quotes)

rawDescription: Short overview of contents of list of tuples


code:
person = Person(first_name='Guido', last_name='van Rossum')
Badge.objects.bulk_create([
    Badge(owner=person),
    Badge(owner=person),
    Badge(owner=person),
    Badge(owner=person),
])

rawDescription: What is the correct way to make django model saving trickle down?


code:
def topicWordCloud(model, topicNumber, WCmaxWords,WCwidth, WCheight):
    topic = model.components_[topicNumber]
    tupleList = [(tf_feature_names[i],int(topic[i]/topic.sum()*10000)) for i in range(len(topic))]
    wordcloud = WordCloud(width=WCwidth, height=WCheight, max_words=WCmaxWords, random_state=42).generate_from_frequencies(tupleList)
    return wordcloud

fig = plt.figure()
for i in range(n_topics):
    ax = fig.add_subplot(2,1,i+1)
    wordcloud = topicWordCloud(...)

    ax.imshow(wordcloud)
    ax.axis('off')

rawDescription: Side by side Wordclouds in matplotlib

description: Side by side Wordclouds in matplotlib


code:
import csv

with open('input.txt', 'rb') as file1:
    file1_data = dict(line.split(None, 2)[1::-1] for line in file1 if line.strip())

with open('data.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
    output = csv.writer(outputfile, delimiter='|')
    for line in file2:
        if line[:1] == '>':
            row = line.strip().split('|')
            key = row[0][1:]
            if key in file1_data:
                output.writerow(row + [file1_data[key]])
        else:
            outputfile.write(line)

rawDescription: how to enhance my dataset output using python

description: Enhance my dataset output using python


code: conf.set("es.query", "?q=me*")

rawDescription: How to read data from Elasticsearch to Spark?

description: Read data from Elasticsearch to Spark


code:
from netCDF4 import Dataset

# a dictionary of lists that hold all the datasets from a given day
grouped_datasets = {}

for f in listdir(path):
    if isfile(join(path,f)):
        full_path = join(path,f)
        f = Dataset(full_path, 'r')
        basetime = f.variables['base_time'][:]
        altitude = f.variables['alt'][:]
        temp = f.variables['tdry'][:]
        actual_date =  strftime("%Y-%m-%d %H:%M:%S", gmtime(basetime))
        measurement_day =  strftime("%Y-%m-%d", gmtime(basetime))

        # if we haven't encountered any datasets from this day yet...
        if measurement_day not in grouped_datasets:
            # add that day to our dict
            grouped_datasets[measurement_day] = []

        # now append our dataset to the correct day (list)
        grouped_datasets[measurement_day].append(f)

rawDescription: group a bunch of files by day

description: Group a bunch of files by day


code:
import tornado.httpserver
import tornado.ioloop
import tornado.web

class TextHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.generator = self.generate_text(1000)
        tornado.ioloop.IOLoop.instance().add_callback(self.loop)

    def loop(self):
        try:
            text = self.generator.next()
            self.write(text)
            tornado.ioloop.IOLoop.instance().add_callback(self.loop)
        except StopIteration:
            self.finish()

    def generate_text(self, n):
        for x in xrange(n):
            if not x % 15:
                yield "FizzBuzz\n"
            elif not x % 5:
                yield "Buzz\n"
            elif not x % 3:
                yield "Fizz\n"
            else:
                yield "%s\n" % x

application = tornado.web.Application([
    (r"/text/", TextHandler),
])

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()

rawDescription: Using a simple python generator as a co-routine in a Tornado async handler?


code: [x * y for x, y in zip(vec1, vec2)]

rawDescription: Restricted Assignment in Python's List Comprehensions?


code:
class AbstractPost(models.Model):
    title = CharField
    abstract = TextField
    body = TextField

    class Meta:
        abstract = True

class Post(AbstractPost):
    def save(self):
        post = super(Post, self).save()

        PostHistory.objects.create(
            post=post,
            title=post.title,
            abstract=post.abstract,
            body=post.body,
        )

class PostHistory(AbstractPost):
    post = ForeignKey(Post)

    class Meta:
        ordering = ['-pk']

class Image(models.Model):
    post = ForeignKey(Post)
    imagefile = ImageField

rawDescription: Model History in Django

description: Model History in Django