-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.py
47 lines (37 loc) · 1.36 KB
/
status.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This class provides services for reporting status, logging, etc.
import datetime
import sys
import time
class Status(object):
def __init__(self, resource_name):
self.__summary = True
self.__name = resource_name
self.__timestamp = datetime.datetime.now()
@property # when you do lvar = Status.summary, it will call this function
def summary(self):
print("In the getter, __summary is %s" % self.__summary,
file=sys.stderr)
return (self.__summary, self.__timestamp)
@summary.setter # when you do Status.summary = value, it will call this function
def summary(self, summary: bool):
print(
"In the setter, name is %s will become %s" % (self.__name, summary),
file=sys.stderr)
self.__summary = summary
self.__timestamp = datetime.datetime.now()
def __str__(self):
return (f"At {self.__timestamp}, {self.__name} is {self.__summary} ")
if __name__ == "__main__":
black = Status("black")
green = Status("green")
black.summary = True
time.sleep(.3)
(state, timestamp) = black.summary
print(f"At {timestamp}, black was changed to {state} ")
black.summary = False
time.sleep(.7)
(state, timestamp) = black.summary
print(f"At {timestamp}, black was changed to {state} ")