Skip to content

Instantly share code, notes, and snippets.

Guy Arad guyarad

Block or report user

Report or block guyarad

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
guyarad /
Last active Aug 4, 2019
Enable pickling on Python classes that uses __slots__
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__)
guyarad / Tidy
Last active Sep 14, 2016
Text filter intended to be used with TextWrangler application (Mac OSX) in order to reformat (tidy-up) a JSON file.
View Tidy
# Script was modified from:
# Modified by Guy Arad, and can be found in:
# Installation:
# Copy to "~/Library/Application Support/TextWrangler/Text Filters"
import wrapt
import functools
def for_all_methods(decorator):
Based on
When applied to a class, will automatically decorate all public
methods (ones that don't start with an underscore) using the
given ``decorator``.
guyarad /
Created Oct 5, 2016
Wrapper for Python classic enum to be used as SQL Alchemy enum type
# ==========
class PythonEnumWrapper(sa.Enum):
def __init__(self, cls):
Initializes a new ``PythonEnumWrapper`` to be used as ``sqlalchemy.Enum``
cls: a classic enumeration type
guyarad /
Last active Nov 11, 2016
Lazy Logging - the below snippet is relevant when you wish to log some debugging information, but that information is relatively costly to generate. Ideally, you'll want to generate that information only when DEBUG level is actually enabled. In other words, only when the message formatting actually occurs.
class LazyLogging(object):
A utility class that wraps a method
def __init__(self, function):
self.function = function
def __str__(self):
import logging
logger = logging.getLogger("log")
def simple():
if logger.isEnabledFor(logging.DEBUG):
logger.debug('Stupid log message ' + ' '.join([str(i) for i in range(20)]))
guyarad /
Created Feb 8, 2017
Timing context manager
def timing(label=None, time_func=simulator.timestamp):
Can be used in conjunction with ``with`` statement to easily measure duration.
label: represents the measurement
A callable. Once invoked will return one of the following (duration in seconds):
guyarad /
Last active Jul 30, 2019
PyCharm issuing wrong and annoying intellisense warnings (Python 2.7.14, PyCharm 2017.3.2)
# 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}
guyarad / mongodb_collection_sizes.js
Last active Dec 29, 2019 — forked from joeyAghion/mongodb_collection_sizes.js
List mongodb collections in descending order of size. Helpful for finding largest collections. First number is "size," second is "storageSize."
View mongodb_collection_sizes.js
function getReadableFileSizeString(fileSizeInBytes, decimalPlacer=1) {
// for more concise solutions see:
var i = 0;
var scale = 1024;
var byteUnits = [' B', ' KiB', ' MiB', ' GiB', ' TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
while (fileSizeInBytes >= scale) {
fileSizeInBytes = fileSizeInBytes / scale;
guyarad /
Created Nov 26, 2019 — forked from lucasea777/
Python C Extension Hello World
gcc -fpic --shared $(python3-config --includes) greetmodule.c -o
# can also use $(pkg-config --cflags python-3.5)
# or
# python3 install --record files.txt --user
You can’t perform that action at this time.