-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbite_207.py
More file actions
49 lines (36 loc) · 1.25 KB
/
bite_207.py
File metadata and controls
49 lines (36 loc) · 1.25 KB
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
# Online Python - IDE, Editor, Compiler, Interpreter
import random
from functools import wraps
from time import sleep
def cached_property(func):
"""decorator used to cache expensive object attribute lookup"""
@wraps(func)
def getter(self, *args, **kwargs):
if not hasattr(cached_property, "cache"):
cached_property.cache = dict()
if func not in cached_property.cache:
cached_property.cache[func] = func(self)
return cached_property.cache[func]
return getter
class Planet:
"""the nicest little orb this side of Orion's Belt"""
GRAVITY_CONSTANT = 42
TEMPORAL_SHIFT = 0.12345
SOLAR_MASS_UNITS = 'M\N{SUN}'
def __init__(self, color, _mass =None):
self.color = color
self._mass = None
def __repr__(self):
return f'{self.__class__.__name__}({repr(self.color)})'
@cached_property
def mass(self):
scale_factor = random.random()
sleep(self.TEMPORAL_SHIFT)
self._mass = (f'{round(scale_factor * self.GRAVITY_CONSTANT, 4)} '
f'{self.SOLAR_MASS_UNITS}')
return self._mass
mass = property(mass)
planet = Planet('Blue', 17.15)
#print(planet)
masses = [planet.mass for _ in range(10)]
print(masses)