Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
@mikeckennedy
mikeckennedy / monitor.py
Last active January 27, 2016 18:49
Add opbeat integration to Pyramid web apps
import opbeat
import opbeat.instrumentation
import opbeat.instrumentation.control
from pyramid.events import NewRequest
from pyramid.events import subscriber
from pyramid.httpexceptions import HTTPRedirection, HTTPException
opbeat_is_debug_mode = False
opbeat.instrumentation.control.instrument()
@mikeckennedy
mikeckennedy / dict_vs_list_speed_test.py
Created May 12, 2016 06:20
If you had a large set of data and you need to "find" a subset of elements by some value (equality not a computation, such as between two values), it is dramatically faster. See the output comments at the very end of this file for numbers.
# ############ DICTIONARY vs. LIST LOOKUP PERF #############
# Create by Michael Kennedy (@mkennedy)
#
# Overview:
# If you had a large set of data and you need to "find" a subset
# of elements by some value (equality not a computation, such as
# between two values), it is dramatically faster. See the output
# comments at the very end of this file for numbers.
#
import collections
@mikeckennedy
mikeckennedy / memory_per_via_slots.py
Last active May 25, 2016 15:12
Custom types store their data in individualized, dynamic dictionaries via self.__dict__. Using __slots__ to limit available attribute names and move the name/key storage outside the instance to a type level can significantly improve memory usage. See EOF for perf numbers.
# ############ __slots__ for improved memory usage #############
# Create by Michael Kennedy (@mkennedy)
#
# Overview:
# Custom types store their data in individualized, dynamic dictionaries
# via self.__dict__. Using __slots__ to limit available attribute names
# and move the name/key storage outside the instance to a type level
# can significantly improve memory usage. See EOF for perf numbers.
#
# Parse this busted file:
#
# A,B,C,D,E,
# A,B,C,D,E,
# A,B,C,D,E, A,B,C,D,E,
#
# A,B,C,D,E,
# A,B,C,D,E,
data = []
@mikeckennedy
mikeckennedy / snap-ci-talkpythontraining.config
Created August 5, 2016 14:26
The Snap CI config settings to checkout, setup and test Talk Python Training at https://snap-ci.com
# Setup a virtual environment (running Python 3.5 foundation)
python -m venv ~/.python3_venv
# Activate the virtual environment
source ~/.python3_venv/bin/activate
# Install a few dependencies needed to bootstrap things
pip install setuptools nose mailchimp
# Register the website package (pyramid web app) as a package
@mikeckennedy
mikeckennedy / download_python_bytes.py
Created November 25, 2016 08:04
Python script to download MP3s from Python Bytes
# requires request and Python 3
import requests
import sys
import os
from xml.etree import ElementTree
def main():
rss_url = 'https://pythonbytes.fm/episodes/rss'
resp = requests.get(rss_url)
@mikeckennedy
mikeckennedy / extractdocx.py
Created January 27, 2017 17:54 — forked from etienned/extractdocx.py
Simple function to extract text from MS XML Word document (.docx) without any dependencies.
try:
from xml.etree.cElementTree import XML
except ImportError:
from xml.etree.ElementTree import XML
import zipfile
"""
Module that extract text from MS XML Word document (.docx).
(Inspired by python-docx <https://github.com/mikemaccana/python-docx>)
@mikeckennedy
mikeckennedy / loopy_pythonic.py
Last active February 18, 2017 15:15
Feedback for a student :)
# Original version
def main():
name = print_intro()
the_list = bash_list.commands
flag='1'
while flag=='1':
random.shuffle(the_list)
correct, incorrect, answer_list = question_loop(the_list)
percentage = calculate_stats(correct, incorrect)
print_results(correct, incorrect, percentage, name)
@mikeckennedy
mikeckennedy / stocks_v1.py
Last active June 27, 2017 17:46
Sorting example
import collections
Stock = collections.namedtuple('Stock', 'symbol, open, high, low, close, volume')
stocks = [
Stock('AAL', 49, 49.1, 48.47, 48.63, 11901883),
Stock('AAPL', 145.13, 147.16, 145.11, 146.28, 33917635),
Stock('ADBE', 143.75, 145.59, 143.06, 145.41, 3383524),
Stock('AMZN', 1002.54, 1004.62, 998.02, 1003.74, 2832807),
Stock('GOOGL', 975.5, 986.62, 974.46, 986.09, 1524905),
symbol open high low close volume
AAL 49 49.1 48.47 48.63 11901883
AAPL 145.13 147.16 145.11 146.28 33917635
ADBE 143.75 145.59 143.06 145.41 3383524
AMZN 1002.54 1004.62 998.02 1003.74 2832807
GOOGL 975.5 986.62 974.46 986.09 1524905
MAT 20.22 20.75 20.11 20.68 10603967
MSFT 70.09 71.25 69.92 71.21 26803888
NTES 320 333.78 319 333.56 1356386