View build.sh
gcc -fpic --shared $(python3-config --includes) greetmodule.c -o greet.abi3.so | |
# can also use $(pkg-config --cflags python-3.5) | |
# or | |
# python3 setup.py install --record files.txt --user |
View mongodb_collection_sizes.js
function getReadableFileSizeString(fileSizeInBytes, decimalPlacer=1) { | |
// for more concise solutions see: https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string | |
var i = 0; | |
var scale = 1024; | |
var byteUnits = [' B', ' KiB', ' MiB', ' GiB', ' TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; | |
while (fileSizeInBytes >= scale) { | |
fileSizeInBytes = fileSizeInBytes / scale; | |
i++; | |
} |
View pycharm-intellisense-issues.py
# Python 2.7.14, PyCharm 2017.3.2 | |
from collections import defaultdict | |
some_default_dict = defaultdict(list) | |
some_default_dict[1] = 'a' | |
some_default_dict[2] = 'b' | |
some_objects = [{'id': i} for i in range(10)] | |
obj_by_id = {obj['id']: obj for obj in some_objects} |
View timing.py
@contextmanager | |
def timing(label=None, time_func=simulator.timestamp): | |
""" | |
Can be used in conjunction with ``with`` statement to easily measure duration. | |
Args: | |
time_func: | |
label: represents the measurement | |
Returns: | |
A callable. Once invoked will return one of the following (duration in seconds): |
View lazy_logging_benchmark.py
import logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger("log") | |
def simple(): | |
if logger.isEnabledFor(logging.DEBUG): | |
logger.debug('Stupid log message ' + ' '.join([str(i) for i in range(20)])) | |
View lazy_logging.py
# IMPLEMENTATION | |
class LazyLogging(object): | |
""" | |
A utility class that wraps a method | |
""" | |
def __init__(self, function): | |
self.function = function | |
def __str__(self): |
View classic_enum_wrapper_for_sa.py
# DEFINITION | |
# ========== | |
class PythonEnumWrapper(sa.Enum): | |
def __init__(self, cls): | |
""" | |
Initializes a new ``PythonEnumWrapper`` to be used as ``sqlalchemy.Enum`` | |
Args: | |
cls: a classic enumeration type | |
""" |
View debugging_decorators.py
import wrapt | |
import functools | |
def for_all_methods(decorator): | |
""" | |
CLASS DECORATOR. | |
Based on http://stackoverflow.com/a/6307868/916568 | |
When applied to a class, will automatically decorate all public | |
methods (ones that don't start with an underscore) using the | |
given ``decorator``. |
View Tidy JSON.py
#!/usr/bin/python | |
# | |
# Script was modified from: | |
# https://steveswinsburg.wordpress.com/2014/09/26/textwrangler-filters-to-tidy-xml-and-tidy-json/ | |
# Modified by Guy Arad, and can be found in: | |
# https://gist.github.com/guyarad/d80b9d5652a6616e1c3138b6a86310c4 | |
# | |
# Installation: | |
# Copy to "~/Library/Application Support/TextWrangler/Text Filters" | |
# |
View slots_pickling_mixin.py
class SlotsPicklingMixin(object): | |
# otherwise, subclasses will still have `__dict__` | |
__slots__ = () | |
def __init__(self): | |
assert hasattr(self, '__slots__') | |
def __getstate__(self): | |
all_slots = itertools.chain.from_iterable( | |
getattr(t, '__slots__', ()) for t in type(self).__mro__) |