Skip to content

Instantly share code, notes, and snippets.

View bradmontgomery's full-sized avatar

Brad Montgomery bradmontgomery

View GitHub Profile
@bradmontgomery
bradmontgomery / ShortIntroToScraping.rst
Created February 21, 2012 02:00
Really short intro to scraping with Beautiful Soup and Requests
@bradmontgomery
bradmontgomery / process_pool_executor_example.py
Created March 3, 2017 20:06
Simple example of using ProcessPoolExecutor vs. serial execution in python.
import hashlib
import sys
from concurrent.futures import ProcessPoolExecutor
from time import sleep, time
def t1(n):
"""Silly function whose time increases as n does."""
for i in range(n):
@bradmontgomery
bradmontgomery / kill_attrs.py
Created November 11, 2010 23:12
A way to remove all HTML attributes with BeautifulSoup
from BeautifulSoup import BeautifulSoup
def _remove_attrs(soup):
for tag in soup.findAll(True):
tag.attrs = None
return soup
def example():
doc = '<html><head><title>test</title></head><body id="foo" onload="whatever"><p class="whatever">junk</p><div style="background: yellow;" id="foo" class="blah">blah</div></body></html>'
@bradmontgomery
bradmontgomery / publishers.py
Created May 13, 2019 13:56
Simple example of MQTT publishers / subscriber using paho.mqtt
#!/usr/bin/env python
"""
Spawn a lot of publishers.
"""
import json
import os
import random
import sys
@bradmontgomery
bradmontgomery / count_words.py
Last active August 15, 2022 19:11
playing with python's `collections.Counter`
"""
Use a Counter to find the most common words in "The Wonderful Wizard of Oz" by
L. Frank Baum.
Available in (mostly) plain text at:
https://archive.org/stream/wonderfulwizardo00baumiala/wonderfulwizardo00baumiala_djvu.txt
Note: This code also counts the words in the header, so it's not a *realistic*
applicaton, but more of a demonstration of python's Counter.
@bradmontgomery
bradmontgomery / friendly.py
Created February 4, 2019 22:36
A function to enumerate and print a list of items horizontally using the space available in your terminal (optionally, without wrapping text).
"""
A function to enumerate and print a list of items horizontally using the
space available in your terminal (optionally, without wrapping text).
Example:
>>> friendly.test()
1. a cat 2. a dog 3. Three pairs of socks 4. A kick-ass bicycle
5. chickens 6. A quick brown fox 7. and a lazy dog
8. Lorem ipsum dolor sit amet, consectetur
@bradmontgomery
bradmontgomery / main.py
Last active June 13, 2022 20:00
Hack to get the uncompressed size of a gzip file without reading the whole thing.
#!/usr/bin/env python
"""
Test if we can reliably figure out the uncompressed size of .gz file...
"""
import gzip
import os
import subprocess
@bradmontgomery
bradmontgomery / mempy-2022-05-16.md
Last active May 17, 2022 00:18
MEMpy 2022-05-16 --> What's new in Python 3.11

MEMpy 2022-05-16

What's new in python 3.11 -- Coming Soon!

python release timeline

Official Releases from 1996 - 2022

@bradmontgomery
bradmontgomery / samples.py
Created September 5, 2013 02:23
Reading & filtering csv data
# read from a csv
# http://docs.python.org/2/library/csv.html#csv.reader
import csv
def read_csv_file(filename):
# read a csv file and return a list of lists
results = []
with open('analytics.csv', 'rU') as f:
myreader = csv.reader(f)
for row in myreader:
@bradmontgomery
bradmontgomery / main.py
Created August 21, 2018 02:38
Examples of using concurrent.futures with Threads & Processes and the joblib library to read data from a bunch of files & do some simple parallell processing.
#!/usr/bin/env python
"""
process some data
"""
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from joblib import Parallel, delayed
import os
import statistics