This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Arzaroth Lekva
committed
Dec 25, 2014
1 parent
02c0a6d
commit 87f4371
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# File: defaultordereddict.py | ||
# by Arzaroth Lekva | ||
# [email protected] | ||
# | ||
# Source: http://stackoverflow.com/a/6190500/562769 | ||
|
||
from collections import OrderedDict, Callable | ||
|
||
class DefaultOrderedDict(OrderedDict): | ||
def __init__(self, default_factory=None, *a, **kw): | ||
if (default_factory is not None and | ||
not isinstance(default_factory, Callable)): | ||
raise TypeError('first argument must be callable') | ||
OrderedDict.__init__(self, *a, **kw) | ||
self.default_factory = default_factory | ||
|
||
def __getitem__(self, key): | ||
try: | ||
return OrderedDict.__getitem__(self, key) | ||
except KeyError: | ||
return self.__missing__(key) | ||
|
||
def __missing__(self, key): | ||
if self.default_factory is None: | ||
raise KeyError(key) | ||
self[key] = value = self.default_factory() | ||
return value | ||
|
||
def __reduce__(self): | ||
if self.default_factory is None: | ||
args = tuple() | ||
else: | ||
args = self.default_factory, | ||
return type(self), args, None, None, self.items() | ||
|
||
def copy(self): | ||
return self.__copy__() | ||
|
||
def __copy__(self): | ||
return type(self)(self.default_factory, self) | ||
|
||
def __deepcopy__(self, memo): | ||
import copy | ||
return type(self)(self.default_factory, | ||
copy.deepcopy(self.items())) | ||
|
||
def __repr__(self): | ||
return 'OrderedDefaultDict(%s, %s)' % (self.default_factory, | ||
OrderedDict.__repr__(self)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# File: enum.py | ||
# by Arzaroth Lekva | ||
# [email protected] | ||
# | ||
|
||
import copy | ||
|
||
def enum(*args, **kwargs): | ||
enums = dict(zip(args, range(len(args))), **kwargs) | ||
enums['map'] = copy.copy(enums) | ||
enums['rmap'] = {} | ||
for k,v in enums.items(): | ||
try: | ||
enums['rmap'][v] = k | ||
except TypeError: | ||
pass | ||
return type('Enum', (), enums) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
# [email protected] | ||
# | ||
|
||
from arza import enum | ||
from src.enum import enum | ||
from collections import defaultdict | ||
|
||
class InventoryPony(object): | ||
|
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