Check for usage of undefined variables
1 | # Copyright 2019 Extreme Networks, Inc. |
||
2 | # |
||
3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
||
4 | # you may not use this file except in compliance with the License. |
||
5 | # You may obtain a copy of the License at |
||
6 | # |
||
7 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
8 | # |
||
9 | # Unless required by applicable law or agreed to in writing, software |
||
10 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
12 | # See the License for the specific language governing permissions and |
||
13 | # limitations under the License. |
||
14 | |||
15 | from __future__ import absolute_import |
||
16 | |||
17 | import six |
||
18 | |||
19 | import datetime |
||
20 | |||
21 | __all__ = [ |
||
22 | 'to_human_time_from_seconds' |
||
23 | ] |
||
24 | |||
25 | if six.PY3: |
||
26 | long_int = int |
||
27 | else: |
||
28 | long_int = long # NOQA |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
29 | |||
30 | |||
31 | def to_human_time_from_seconds(seconds): |
||
32 | """ |
||
33 | Given a time value in seconds, this function returns |
||
34 | a fuzzy version like 3m5s. |
||
35 | |||
36 | :param time_seconds: Time specified in seconds. |
||
37 | :type time_seconds: ``int`` or ``long`` or ``float`` |
||
38 | |||
39 | :rtype: ``str`` |
||
40 | """ |
||
41 | assert (isinstance(seconds, int) or isinstance(seconds, int) or |
||
42 | isinstance(seconds, float)) |
||
43 | |||
44 | return _get_human_time(seconds) |
||
45 | |||
46 | |||
47 | def _get_human_time(seconds): |
||
48 | """ |
||
49 | Takes number of seconds as input and returns a string of form '1h3m5s'. |
||
50 | |||
51 | :param seconds: Number of seconds. |
||
52 | :type seconds: ``int`` or ``long`` or ``float`` |
||
53 | |||
54 | :rtype: ``str`` |
||
55 | """ |
||
56 | |||
57 | if seconds is None: |
||
58 | return None |
||
59 | |||
60 | if seconds == 0: |
||
61 | return '0s' |
||
62 | |||
63 | if seconds < 1: |
||
64 | return '%s\u03BCs' % seconds # Microseconds |
||
65 | |||
66 | if isinstance(seconds, float): |
||
67 | seconds = long_int(round(seconds)) # Let's lose microseconds. |
||
68 | |||
69 | timedelta = datetime.timedelta(seconds=seconds) |
||
70 | offset_date = datetime.datetime(1, 1, 1) + timedelta |
||
71 | |||
72 | years = offset_date.year - 1 |
||
73 | days = offset_date.day - 1 |
||
74 | hours = offset_date.hour |
||
75 | mins = offset_date.minute |
||
76 | secs = offset_date.second |
||
77 | |||
78 | time_parts = [years, days, hours, mins, secs] |
||
79 | |||
80 | first_non_zero_pos = next((i for i, x in enumerate(time_parts) if x), None) |
||
81 | |||
82 | if first_non_zero_pos is None: |
||
83 | return '0s' |
||
84 | else: |
||
85 | time_parts = time_parts[first_non_zero_pos:] |
||
86 | |||
87 | if len(time_parts) == 1: |
||
88 | return '%ss' % tuple(time_parts) |
||
89 | elif len(time_parts) == 2: |
||
90 | return '%sm%ss' % tuple(time_parts) |
||
91 | elif len(time_parts) == 3: |
||
92 | return '%sh%sm%ss' % tuple(time_parts) |
||
93 | elif len(time_parts) == 4: |
||
94 | return '%sd%sh%sm%ss' % tuple(time_parts) |
||
95 | elif len(time_parts) == 5: |
||
96 | return '%sy%sd%sh%sm%ss' % tuple(time_parts) |
||
97 |