Skip to content

Instantly share code, notes, and snippets.

View jhbuhrman's full-sized avatar

Jan-Hein Bührman jhbuhrman

View GitHub Profile
@jhbuhrman
jhbuhrman / readable_reduce.py
Created October 10, 2023 14:19
A perhaps pythonic application of `functools.reduce()`
import enum, functools, operator
from typing import Self
class Color(enum.IntFlag):
RED = enum.auto()
GREEN = enum.auto()
BLUE = enum.auto()
@classmethod
def from_string_pythonic(cls, string: str) -> Self:
@jhbuhrman
jhbuhrman / pd_df_mem.py
Created August 11, 2019 19:22
Trying to get a grip on Pandas memory management
import gc
import pandas as pd
import numpy as np
import psutil
process = psutil.Process()
print(f"pid = {process.pid}")
dtypes = ['int64', 'float64', 'complex128', 'object', 'bool']
data = dict([(t, np.ones(shape=0x1000 * 4096).astype(t)) for t in dtypes])
@jhbuhrman
jhbuhrman / data_frames.py
Created December 5, 2018 08:44
Custom attrib validator with its info in the doc string of the containing attrs class
import logging
from typing import List
import attr
import pandas as pd
_log = logging.getLogger(__name__)
@attr.s(auto_attribs=True, repr=False, slots=True, hash=True)
@jhbuhrman
jhbuhrman / iterutils.py
Last active November 22, 2018 13:39
Some extra iterable convenience functions
# Copyright 2018 Jan-Hein Bührman
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
@jhbuhrman
jhbuhrman / has_no_attribute_in_eq_method.py
Created September 7, 2018 13:07
Demonstrating mypy false positive (as of mypy 0.620)
class SimpleClass:
some_int: int
some_str: str
def __init__(self, some_int: int, some_str: str) -> None:
self.some_int = some_int
self.some_str = some_str
def __repr__(self) -> str:
return (f'{self.__class__.__qualname__}'