This file contains hidden or 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
# http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python/19829714#19829714 | |
class Dict(dict): | |
def __missing__(s, k): | |
v = s[k] = type(s)() | |
return v |
This file contains hidden or 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 shutil | |
import os.path | |
import zipfile | |
import collections | |
try: | |
import zlib | |
compress = zipfile.ZIP_DEFLATED | |
except ImportError: | |
compress = zipfile.ZIP_STORED |
This file contains hidden or 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 | |
class Dict(collections.MutableMapping): | |
def __init__(s, *args, **kwargs): | |
s.data = dict(*args, **kwargs) | |
def __getitem__(s, k): return s.data[k] | |
def __setitem__(s, k, v): s.data[k] = v | |
def __delitem__(s, k): del s.data[k] | |
def __iter__(s): return iter(s.data) | |
def __repr__(s): return repr(s.data) |
This file contains hidden or 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
try: | |
import cPickle as pickle | |
except ImportError: | |
import pickle | |
class Dict(dict): | |
""" | |
Dict that tracks changes. Changes = (New, Changed, Removed) | |
""" | |
def __init__(s, *args, **kwargs): |
This file contains hidden or 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 Timer(object): | |
verbose = False | |
def __init__(s, name): | |
from time import time | |
s.time = time | |
s.name = name | |
def __enter__(s): s.start = s.time() | |
def __exit__(s, *err): | |
if s.verbose: print "%s...\t\tElapsed time: %sms." % (s.name, (s.time() - s.start) * 1000) |
This file contains hidden or 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 traceback | |
try: | |
pass | |
# Do scriptjob code in here. Since Scriptjobs do not display exceptions, do it ourselves. | |
except: | |
print traceback.format_exc() | |
raise |
This file contains hidden or 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 math, collections | |
class Vector(tuple): | |
__slots__ = () | |
def __neg__(s): return s.__class__(-a for a in s) | |
def __pos__(s): return s.__class__(+a for a in s) | |
def __ne__(s, v): return False if s == v else True | |
magnitude = property(lambda s: math.sqrt(sum(s * s))) # |s| | |
def __nonzero__(s): return 1 != len(set(a for a in s if not a)) | |
def __add__(s, v): return s.__class__(a + b for a, b in zip(s, v)) | |
def __div__(s, v): return s.__class__(a / b for a, b in zip(s, v)) |
This file contains hidden or 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 time | |
import threading | |
import collections | |
import maya.cmds as cmds | |
import maya.utils as utils | |
class window(object): | |
def __init__(s): | |
name = "Testwindow" | |
s.maya = Maya() |
This file contains hidden or 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 | |
class DB(collections.MutableMapping): | |
""" Database Interface """ | |
_default = object() | |
def __init__(s): | |
s._cnt = 0 | |
s._del = set() | |
with s: s._data = dict((a, s._default) for a in s.initkeys()) | |
def __enter__(s): |
This file contains hidden or 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
# http://www.peterbe.com/plog/uniqifiers-benchmark | |
def unique(lst): | |
a = set(lst) | |
return [b for b in lst if b in a and not a.remove(b)] |