Skip to content

Instantly share code, notes, and snippets.

@bencharb
bencharb / rm_ds_store_files.sh
Last active August 29, 2015 13:56
Remove all DS_Store files
sudo find / -name ".DS_Store" -depth -exec rm {} \;
@bencharb
bencharb / dict_to_properties.py
Created March 29, 2014 18:41
dict to properties: foo.get('bar') to foo.bar (dict to object with dotted notation properties)
# From Nery Jr on SO: http://stackoverflow.com/a/21502348
# If you want to save lines of code and leave the most flexible solution, we can deserialize the json string to a dynamic object:
p = lambda:None
p.__dict__ = json.loads('{"action": "print", "method": "onData", "data": "Madan Mohan"}')
# >>>> p.action
# output: u'print'
@bencharb
bencharb / get_parent_keys
Created August 4, 2014 03:06
get parent keys from nested dictionary
import collections
def get_parent_keys(dct, capture_nested_key_parents=True):
"""Get parent keys for all values in dictionary of dictionaries.
Returns k,v dictionary with lists of parent keys.
Modified from http://stackoverflow.com/a/18820433
"""
reverse_dict = {}
def keypaths(nested):
@bencharb
bencharb / gist:a89e4b22d97eacb16886
Created September 6, 2014 00:30
start/stop postgres on mac
The Homebrew package manager includes launchctl plists to start automatically. For more information run brew info postgres.
Start manually:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Stop manually:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
@bencharb
bencharb / get_public_methods
Last active August 29, 2015 14:15
Get public methods from class
#!/bin/python
import operator
import inspect
_get_method_name = operator.itemgetter(0)
def get_private_methods(cls):
def is_private(attr):
return inspect.isbuiltin(attr) or inspect.ismethoddescriptor(attr)
return map(_get_method_name, inspect.getmembers(cls, is_private))
@bencharb
bencharb / is_positive_int
Last active August 29, 2015 14:17
is positive int
#!/usr/bin/python
def is_positive_int(thing):
try:
assert(unicode(thing).isdigit())
except Exception:
return False
else:
return True
def _test_is_positive_int():
@bencharb
bencharb / get_object_members
Created March 22, 2015 04:39
get object members
#!/bin/python
import inspect
import itertools
DEFAULT_OBJECT_ATTRS = set(dir(type('dummy', (object,), {})))
FILTER_VALUE_FUNCTIONS = inspect.isroutine, inspect.ismethod
FILTER_NAME_FUNCTIONS = [lambda n: n in DEFAULT_OBJECT_ATTRS]
def get_object_members(cls_or_instance, keep_sunders=False):
@bencharb
bencharb / header_hierarchy_ascii
Created May 7, 2015 19:41
Header Hierarchy ASCII
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T I T L E
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@bencharb
bencharb / get_content_type
Last active August 29, 2015 14:22
Django ContentType Index
#!/bin/python
#
class ContentTypeIndex(object):
"""
Get content type based on class name, model name, pk, etc
In [00]: get_content_type = ContentTypeIndex(ContentType.objects.all())
In [01]: get_content_type('database')
Out[01]: <ContentType: database>
In [02]: get_content_type(Database)
Out[02]: <ContentType: database>
@bencharb
bencharb / simple_dict_cache
Last active August 29, 2015 14:22
SimpleCache capped dict
class CappedDict(collections.OrderedDict):
default_max_size = 50
def __init__(self, *args, **kwargs):
self.max_size = kwargs.pop('max_size', self.default_max_size)
super(CappedDict, self).__init__(*args, **kwargs)
def __setitem__(self, key, val):
if key not in self:
max_size = self.max_size-1 # so the dict is sized properly after adding a key
self._prune_dict(max_size)