Skip to content

Instantly share code, notes, and snippets.

# Works on Py3
def parse_version_failsafe(version_string):
try:
return Version(
unicodedata.normalize('NFKD', six.text_type(version_string))
)
except (UnicodeError, InvalidVersion):
return None
@gthank
gthank / numpy-ified-great-circle.py
Created September 16, 2016 11:44
numpy-based method for calculating (great circle) distance between two series of points.
import numpy as np
import pandas as pd
from geopy.distance import EARTH_RADIUS
def numpy_distance(src_lats, src_longs, dest_lats, dest_longs):
"""Calculate distance between (effectively) two Series of points."""
# Convert from degrees to radians.
src_lats = src_lats.apply(np.deg2rad)
src_longs = src_longs.apply(np.deg2rad)
dest_lats = dest_lats.apply(np.deg2rad)
dest_longs = dest_longs.apply(np.deg2rad)
import datetime
def get_date(x):
"""Extract date from the provided timestamp.
Useful because the Pandas data model makes it hard to invoke
instance methods of values directly."""
return x['Occurred Date or Date Range Start'].date()
@gthank
gthank / error
Created June 6, 2016 21:16
`pip install jupyter` output
⇒ pip install jupyter [ruby-2.2.1]
Collecting jupyter
Downloading https://dev-infrastructure.rescuetime.com/root/pypi/+f/f81/d039e084c2c0c/jupyter-1.0.0-py2.py3-none-any.whl
Collecting qtconsole (from jupyter)
Downloading https://dev-infrastructure.rescuetime.com/root/pypi/+f/046/29b6bfeaa40fd/qtconsole-4.2.1-py2.py3-none-any.whl (104kB)
100% |████████████████████████████████| 112kB 1.4MB/s
Collecting ipykernel (from jupyter)
Downloading https://dev-infrastructure.rescuetime.com/root/pypi/+f/8dc/3c636084d2471/ipykernel-4.3.1-py2.py3-none-any.whl (93kB)
100% |████████████████████████████████| 102kB 1.8MB/s
Collecting nbconvert (from jupyter)
@gthank
gthank / jython-file-io.py
Created September 30, 2015 20:20
An example of some Jython for doing File IO stuff in Java and Python
from org.python.core.io import FileIO, FileUtil
path = "/tmp/hello_world.txt"
python_fio = FileIO(path, "r")
java_is = python_fio.asInputStream()
python_file = FileUtil.wrap(java_is)
workbook = jxl.Workbook.getWorkbook(java_is)
# Do stuff with your Pythonic file interface here.
2014-11-14T16:29:58.065531+00:00 ip-172-18-1-52 charon: 14[IKE] authentication of 'C=US, O=RescueTime, CN=client' with RSA successful
2014-11-14T16:29:58.065570+00:00 ip-172-18-1-52 charon: 14[IKE] no RSA private key found for 'C=US, O=RescueTime, CN=clientvpn.rescuetime.com'
conn ios
keyexchange=ikev1
# This was working, but required the local ipsec.secrets file
# authby=xauthrsasig
# xauth=server
left=%defaultroute
leftsubnet=0.0.0.0/0
leftfirewall=yes
leftcert=serverCert.pem
right=%any
$ ip route show table all
default via 172.18.1.1 dev eth0
172.18.1.0/24 dev eth0 proto kernel scope link src 172.18.1.154
broadcast 127.0.0.0 dev lo table local proto kernel scope link src 127.0.0.1
local 127.0.0.0/8 dev lo table local proto kernel scope host src 127.0.0.1
local 127.0.0.1 dev lo table local proto kernel scope host src 127.0.0.1
broadcast 127.255.255.255 dev lo table local proto kernel scope link src 127.0.0.1
broadcast 172.18.1.0 dev eth0 table local proto kernel scope link src 172.18.1.154
local 172.18.1.154 dev eth0 table local proto kernel scope host src 172.18.1.154
broadcast 172.18.1.255 dev eth0 table local proto kernel scope link src 172.18.1.154
$ sudo ipsec statusall
Status of IKE charon daemon (strongSwan 5.1.2, Linux 3.13.0-29-generic, x86_64):
uptime: 89 minutes, since Oct 02 18:38:37 2014
malloc: sbrk 1486848, mmap 0, used 435536, free 1051312
worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 11
loaded plugins: charon test-vectors ldap aes rc2 sha1 sha2 md4 md5 random nonce x509 revocation constraints pkcs1 pkcs7 pkcs8 pkcs12 pem openssl xcbc cmac hmac ctr ccm gcm attr kernel-netlink resolve socket-default stroke updown eap-identity eap-radius xauth-generic xauth-eap radattr addrblock
Virtual IP pools (size/online/offline):
10.0.0.0/24: 254/1/0
Listening IP addresses:
10.230.25.28
$ sudo iptables-save
# Generated by iptables-save v1.4.21 on Thu Oct 2 19:59:57 2014
*filter
:INPUT ACCEPT [407:42791]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [121:24299]
-A FORWARD -s 10.0.0.1/32 -i eth0 -m policy --dir in --pol ipsec --reqid 4 --proto esp -j ACCEPT
-A FORWARD -d 10.0.0.1/32 -o eth0 -m policy --dir out --pol ipsec --reqid 4 --proto esp -j ACCEPT
COMMIT
# Completed on Thu Oct 2 19:59:57 2014