This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo find / -name ".DS_Store" -depth -exec rm {} \; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | |
T I T L E | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer