Test Failed
Push — develop ( d7cf39...faa4bd )
by Nicolas
04:34 queued 10s
created

glances/timer.py (1 issue)

Discourage usage of global

Coding Style Minor
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""The timer manager."""
21
22
from time import time
23
from datetime import datetime
24
25
# Global list to manage the elapsed time
26
last_update_times = {}
27
28
29
def getTimeSinceLastUpdate(IOType):
30
    """Return the elapsed time since last update."""
31
    global last_update_times
0 ignored issues
show
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
32
    # assert(IOType in ['net', 'disk', 'process_disk'])
33
    current_time = time()
34
    last_time = last_update_times.get(IOType)
35
    if not last_time:
36
        time_since_update = 1
37
    else:
38
        time_since_update = current_time - last_time
39
    last_update_times[IOType] = current_time
40
    return time_since_update
41
42
43
class Timer(object):
44
45
    """The timer class. A simple chronometer."""
46
47
    def __init__(self, duration):
48
        self.duration = duration
49
        self.start()
50
51
    def start(self):
52
        self.target = time() + self.duration
53
54
    def reset(self):
55
        self.start()
56
57
    def get(self):
58
        return self.duration - (self.target - time())
59
60
    def set(self, duration):
61
        self.duration = duration
62
63
    def finished(self):
64
        return time() > self.target
65
66
67
class Counter(object):
68
69
    """The counter class."""
70
71
    def __init__(self):
72
        self.start()
73
74
    def start(self):
75
        self.target = datetime.now()
76
77
    def reset(self):
78
        self.start()
79
80
    def get(self):
81
        return (datetime.now() - self.target).total_seconds()
82