1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | class KeyedInstance(object): |
---|
5 | """A class whose instances have a unique identifier of some sort |
---|
6 | No two instances with the same unique ID should exist - if we try to create |
---|
7 | a second instance, the first should be returned. |
---|
8 | """ |
---|
9 | |
---|
10 | _instances = dict() |
---|
11 | |
---|
12 | def __new__(cls, *p, **k): |
---|
13 | instances = cls._instances |
---|
14 | clskey = str(cls) |
---|
15 | if clskey not in instances: |
---|
16 | instances[clskey] = dict() |
---|
17 | instances = instances[clskey] |
---|
18 | |
---|
19 | key = cls._key(*p, **k) |
---|
20 | if key not in instances: |
---|
21 | instances[key] = super(KeyedInstance, cls).__new__(cls) |
---|
22 | return instances[key] |
---|
23 | |
---|
24 | @classmethod |
---|
25 | def _key(cls, *p, **k): |
---|
26 | """Given a unique identifier, return a dictionary key |
---|
27 | This should be overridden by child classes, to specify which parameters |
---|
28 | should determine an object's uniqueness |
---|
29 | """ |
---|
30 | raise NotImplementedError() |
---|
31 | |
---|
32 | @classmethod |
---|
33 | def clear(cls): |
---|
34 | # Allow cls.clear() as well as uniqueInstance.clear(cls) |
---|
35 | if str(cls) in cls._instances: |
---|
36 | del cls._instances[str(cls)] |
---|