Skip to content

Instantly share code, notes, and snippets.

View lbillingham's full-sized avatar

Laurence Billingham lbillingham

View GitHub Profile
@lbillingham
lbillingham / stupid.py
Last active June 9, 2017 20:07
Stupid python chaining
class ChainableMeta(type):
"""this is in no way evil"""
def __getattr__(cls, _):
return Chainable()
class Chainable(object, metaclass=ChainableMeta):
"""
Can we do loads of chained attribute lookups
that always get to the same place?
"""
@lbillingham
lbillingham / print-sitepackages.bash
Created April 26, 2017 15:04
print sitepackages directory
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
@lbillingham
lbillingham / django-and-ipython.md
Created April 25, 2017 13:48
Django in ipython shell

Use ipython shell with Django

Ordinary Django shell

$ python manage.py shell

does not do autocompletion, doc pretty-printing or other nice-to-have-things.

A way around this is using the ipython shell. We'll assume that both django and ipython are installed (in an active virtualenv),

@lbillingham
lbillingham / wdc_per_statuion_holdings.py
Created February 14, 2017 16:36
what observatory data do we have in the BGS WDC web app?. front end at http://wdc.bgs.ac.uk/dataportal/
import requests
url_base = r'http://app.geomag.bgs.ac.uk/wdc/'
station_resource = 'stations'
stations_url = url_base + station_resource
response = requests.get(stations_url)
# dictionary with IAGA codes as primary keys
stations = response.json()
@lbillingham
lbillingham / class_scope.py
Created January 20, 2017 09:30
python class scope is odd
class Spam(object):
"""
Demo of python class scope
fails
on py 3.5
> `File "class_scope.py", line 20, in <listcomp>`
> `...`
> `NameError: name 'ham_gen_list' is not defined`
on py 2.7
@lbillingham
lbillingham / 1k_kp_samples_somewhat_storm_balanced.csv
Created November 18, 2016 09:22
~ 1000 samples of Kp (and ap) between 1998 and 2015. somewhat balanced for storms vs quiet
ap kp_num kp_sym
1998-07-06 08:59:00+00:00 15 3.0 3o
1998-07-10 02:59:00+00:00 4 1.0 1o
1998-07-29 11:59:00+00:00 6 1.6666666666666667 2-
1998-08-07 11:59:00+00:00 22 3.6666666666666665 4-
1998-08-08 08:59:00+00:00 6 1.6666666666666667 2-
1998-08-08 11:59:00+00:00 7 2.0 2o
1998-08-20 17:59:00+00:00 27 4.0 4o
1998-09-07 08:59:00+00:00 4 1.0 1o
1998-09-27 17:59:00+00:00 6 1.6666666666666667 2-
@lbillingham
lbillingham / split_into_2_files.py
Created September 13, 2016 11:52
python click + pytest example
# hello.py
import click
@click.command()
@click.option(
'--name', default='world',
prompt='greet whom?',
help='who should i greet?'
)
def main(name):
@lbillingham
lbillingham / keyword_args_to_attributes.py
Last active June 10, 2016 18:47
demo class that will take any ascii uppercase keyword argument and add it as an attribute: could be useful for magnetic elements
# -*- coding: utf-8 -*-
"""
Demo class to hold magnetic elements data.
Any keyword argument indexed by an ascii_uppercase letter
will become an attribute
"""
import doctest
import math
import string
@lbillingham
lbillingham / setup.py
Created April 28, 2016 10:10
is this legit in setup.py?
import sys
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
if 'develop' in sys.argv:
requirements = [i.strip() for i in open("requirements_dev.txt").readlines()]
else:
requirements = [i.strip() for i in open("requirements.txt").readlines()]
@lbillingham
lbillingham / shell_interact_test.py
Created March 30, 2016 11:41
python sub process test (blocking IO) works on py 2.6 on _old_ server
import inspect
import os
import subprocess
import unittest
class TestAThing(unittest.TestCase):
def test_this_file_seen_by_ls(self):
proc_ls = subprocess.Popen(['/bin/bash'],
stdin=subprocess.PIPE,