forked from adafruit/Adafruit_Blinka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
61 lines (49 loc) · 1.55 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Module providing runtime utility objects to support the Micro/CircuitPython api"""
class Enum(object):
"""
Object supporting CircuitPython-style of static symbols
as seen with Direction.OUTPUT, Pull.UP
"""
def __repr__(self):
"""
Assumes instance will be found as attribute of own class.
Returns dot-subscripted path to instance
(assuming absolute import of containing package)
"""
cls = type(self)
for key in dir(cls):
if getattr(cls, key) is self:
return "{}.{}.{}".format(cls.__module__, cls.__qualname__, key)
return repr(self)
@classmethod
def iteritems(cls):
"""
Inspects attributes of the class for instances of the class
and returns as key,value pairs mirroring dict#iteritems
"""
for key in dir(cls):
val = getattr(cls, key)
if type(val) is cls:
yield (key, val)
class ContextManaged:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.deinit()
class Lockable(ContextManaged):
_locked = False
def try_lock(self):
if self._locked:
return False
else:
self._locked = True
return True
def unlock(self):
if self._locked:
self._locked = False
else:
raise ValueError("Not locked")
def patch_system():
import sys
from adafruit_blinka import time
sys.modules['time'] = time