Passed
Push — master ( 628c62...b7208b )
by Randy
01:39
created

libs.tools.Tools.lists_separate()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
import os
2
import sys
3
import time
4
import traceback
5
6
"""
7
    Copyright (c) 2020 Star Inc.(https://starinc.xyz)
8
9
    This Source Code Form is subject to the terms of the Mozilla Public
10
    License, v. 2.0. If a copy of the MPL was not distributed with this
11
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
12
"""
13
14
check_file = ".alive"
15
16
17
class Tools:
18
    @staticmethod
19
    def get_time(time_format: str = "%b %d %Y %H:%M:%S %Z"):
20
        """
21
        Get datetime with format
22
        :param time_format: string of format codes
23
        :return:
24
        """
25
        time_ = time.localtime(time.time())
26
        return time.strftime(time_format, time_)
27
28
    @staticmethod
29
    def error_report():
30
        """
31
        Report errors as message
32
        :return: string
33
        """
34
        occur_time = Tools.get_time()
35
        err1, err2, err3 = sys.exc_info()
36
        traceback.print_tb(err3)
37
        tb_info = traceback.extract_tb(err3)
38
        filename, line, _, text = tb_info[-1]
39
        error_info = "occurred in\n{}\non line {}\nin statement {}".format(filename, line, text)
40
        return "%s\nSystem Error:\n%s\n%s\n%s\n%s\n" % (occur_time, err1, err2, err3, error_info)
41
42
    @staticmethod
43
    def logger(error_msg, silent: bool = True):
44
        """
45
        Journal or print error message
46
        :return:
47
        """
48
        if silent:
49
            with open("service.log", "a+") as log:
50
                log.write(error_msg + "\n")
51
        else:
52
            print(error_msg)
53
54
    @staticmethod
55
    def set_ready(status: bool):
56
        """
57
        Set status whether service is ready or not
58
        :param status: bool of status
59
        :return:
60
        """
61
        if status and not os.path.exists(check_file):
62
            with open(check_file, "w") as f:
63
                f.write("")
64
        elif not status and os.path.exists(check_file):
65
            os.remove(check_file)
66
67
    @staticmethod
68
    def check_ready():
69
        """
70
        Check status that service is ready or not
71
        :return: bool of status
72
        """
73
        return os.path.isfile(check_file)
74
75
    @staticmethod
76
    def lists_separate(lists: list, numbers: int):
77
        """
78
        Split lists to average
79
        :param lists: list you want to separate
80
        :param numbers: numbers in part you want
81
        :return:
82
        """
83
        for count in range(0, len(lists), numbers):
84
            yield lists[count:count + numbers]
85