Skip to content

Instantly share code, notes, and snippets.

View mpurdon's full-sized avatar

Matthew Purdon mpurdon

View GitHub Profile
@mpurdon
mpurdon / sum_nums.py
Last active July 7, 2016 05:16
Parse numbers from strings and sum them
"""
Proof of concept
Someone wanted to parse number from random strings and sum them as part of some school project.
@author: Matthew Purdon
"""
from __future__ import print_function
import sys
@mpurdon
mpurdon / complex result
Created January 30, 2015 03:56
Result object that can act like a string or a boolean when needed.
"""
Proof of concept
Returning a tuple is nice in python but it would be better if we could return
an object that was truthy when compared and stringy when cast as a string
@author Matthew Purdon
"""
from collections import namedtuple
@mpurdon
mpurdon / grandfather clause
Created January 30, 2015 04:00
Playing with exceptions through inherited methods.
"""
Proof of Concept
Following the course of an exception in a class hierarchy similar to that
in Django views.
@author Matthew Purdon
"""
import importlib
mod = importlib.import_module('dir.mymodule')
MyClass = getattr(mod, 'MyClass')
@mpurdon
mpurdon / gist:13d8896ff190edee60f8
Created March 18, 2015 17:47
Pythonic BizzFuzzes
if __name__ == '__main__':
numbers = [1, 3, 5, 15, 17, 99, 100, 210, 217]
for number in numbers:
print 'Bizz'*(number%3==0) + 'Fuzz'*(number%5==0) or number
print '--'
BIZZ_FUZZ = {3: 'Bizz', 5: 'Fuzz'}
for number in numbers:
@mpurdon
mpurdon / portscan.py
Last active August 29, 2015 14:25
Threaded port scanner for finding chromecasts
#!/usr/bin/env python
"""
Does a quick scan of the current subnet for the specified IP range and ports
- Used to find a listening chromecast
"""
import threading
import time
@mpurdon
mpurdon / reverse.py
Last active July 7, 2016 04:57
Reverse a string
"""
String reversal Algorithm
Technically we would do 'something'[::-1] to reverse a string but
as an excercise I did it myself without using additional memory.
This is technically a lie since strings are immutable in python but
it gives you the idea...
"""
@mpurdon
mpurdon / kwargs_from_strings.py
Created July 7, 2016 05:01
Given a string variable, and value variable pass them into a function as kwarg key and value
# -*- coding: utf-8 -*-
"""
Normally we call a function like so:
some_function(key=value)
But what if we want to dynamically set a key and value from variables?
What if we want the function to expect a variable number of kwargs?
"""
@mpurdon
mpurdon / colinear.py
Created July 7, 2016 05:09
Given a tic tac toe grid, determine if three points form a straight line. Better than hard-coding the solutions like a noob.
# -*- coding: utf-8 -*-
"""
There are a few algorithms for determining collinearity,
since we are in two dimensional space with limited neighbours
we can afford to use the simpler first difference one.
"""
from __future__ import print_function
from operator import itemgetter
@mpurdon
mpurdon / sleep_check.py
Created July 7, 2016 05:20
The sleep function is not very precise, this basically checks how bad it is on your system.
import time
from datetime import datetime
def check_sleep(amount):
start = datetime.now()
time.sleep(amount)
end = datetime.now()
delta = end-start
return delta.seconds + delta.microseconds/1000000.