Skip to content

Instantly share code, notes, and snippets.

View pganssle's full-sized avatar
👨‍💼
Acclimating to new job, full bore FOSS contributions should resume soon.

Paul Ganssle pganssle

👨‍💼
Acclimating to new job, full bore FOSS contributions should resume soon.
View GitHub Profile
@pganssle
pganssle / .clang-format
Created August 9, 2019 14:19
Clang format style that approximates PEP 7
# A clang-format style that approximates Python's PEP 7
# Useful for IDE integration
BasedOnStyle: Google
AlwaysBreakAfterReturnType: All
AllowShortIfStatementsOnASingleLine: false
AlignAfterOpenBracket: Align
BreakBeforeBraces: Stroustrup
ColumnLimit: 79
DerivePointerAlignment: false
IndentWidth: 4
@pganssle
pganssle / datetime_predicates.py
Last active June 8, 2019 18:57
Demo code written to help @Zac-HD develop improved datetime strategies for Hypothesis.
# Released under MPL 2.0 per https://github.com/HypothesisWorks/hypothesis/blob/master/LICENSE.txt
# and CC-0: https://creativecommons.org/publicdomain/zero/1.0/legalcode
#
# Feel free to contact Paul Ganssle if you'd like this released under other terms.
from datetime import datetime, timedelta, timezone
from dateutil import tz
ZERO = timedelta(0)

Instructions

These are steps for attempting to reproduce this test failure.

Get the repository

If you don't have CPython cloned locally

If you don't already have a local CPython clone, go to wherever you clone repositories and execute:

git clone git@github.com:python/cpython.git
cd cpython
#![feature(concat_idents,duration_as_u128)]
extern crate pyo3;
use std::thread;
use std::time;
use pyo3::prelude::*;
use pyo3::ffi::*;
use std::ffi::CString;
#!/usr/bin/env bash
# Parse CLI arguments
REGEXES=()
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key=$1
case $key in
-r|--regex)
REGEXES+=("$2")
import timeit
import operator
def time_func(f, N, k=3):
"""Basic version of IPython's %timeit magic"""
t = min(timeit.timeit(f, number=N) for _ in range(k))
t_per = t / N
if t_per < 1e-6:
units = 'ns'
import random
import string
from timeit import timeit
def _recombine_skipped_queue(tokens, skipped_idxs):
"""
>>> tokens = ["foo", " ", "bar", " ", "19June2000", "baz"]
>>> skipped_idxs = set([0, 1, 2, 5])
>>> _recombine_skipped(tokens, skipped_idxs)

Intro

Most of us have faced a point when trying to make things work with the Python datetime module by just trying things around. Datetime is one of those APIs that seems easy to use but requires the developer to have a deep understanding of what things actually mean, as otherwise it is really easy to introduce unexpected bugs given the actual complexity of date and time related issues.

Time Standards

The first concept we need to grasp when working with time is a standard that defines how we can measure units of time. The same way we have standards to measure weight or length that define kilograms or meters, we need a way to accurately define what

@pganssle
pganssle / pandas_categorical_demo.py
Last active September 30, 2015 19:38
MWE demonstrating a problem using `apply()` to convert multiple DataFrame columns to categorical.
import pandas as pd
import sys
pdf = pd.DataFrame(dict(name= ('Earl', 'Eve', 'Alan', 'Randall', 'Danielle'),
age= ( 29, 17, 73, 31, 62),
gender= ( 'M', 'F', 'M', 'M', 'F'),
nationality=( 'US', 'UK', 'CAN', 'CAN', 'US'),
height= ( 182.9, 167.6, 175.3, 170.2, 172.8)),
columns=('name', 'age', 'gender', 'nationality', 'height'))
pdf = pdf.set_index('name')
@pganssle
pganssle / keybase.md
Created June 3, 2015 15:20
keybase.md

Keybase proof

I hereby claim:

  • I am pganssle on github.
  • I am pganssle (https://keybase.io/pganssle) on keybase.
  • I have a public key whose fingerprint is 6B49 ACBA DCF6 BD1C A206 67AB CD54 FCE3 D964 BEFB

To claim this, I am signing this object: