• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

askomics / flaskomics / 6590008757

20 Oct 2023 03:58PM UTC coverage: 83.758% (+0.4%) from 83.31%
6590008757

push

github-actions

web-flow
Merge pull request #420 from askomics/dev

Release 4.5.0

633 of 633 new or added lines in 29 files covered. (100.0%)

6240 of 7450 relevant lines covered (83.76%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.1
/askomics/libaskomics/Utils.py
1
import random
1✔
2
import re
1✔
3
import time
1✔
4
import traceback
1✔
5
import sys
1✔
6

7

8
class Utils():
1✔
9
    """Contain utils fonction and classes"""
10

11
    @staticmethod
1✔
12
    def redo_if_failure(logger, max_redo, sleep_time, call, *args):
1✔
13
        """Redo a function if it fail, with a sleep time between each try
14

15
        Parameters
16
        ----------
17
        logger : app.logger
18
            The flask logger to log error
19
        max_redo : int
20
            Max number of try
21
        sleep_time : int
22
            Time between each try.
23
        call : function
24
            The function to redo if faliure
25
        *args
26
            Args of the function
27
        """
28
        i = 0
1✔
29
        while True:
30
            i += 1
1✔
31
            try:
1✔
32
                call(*args)
1✔
33
                break
1✔
34
            except Exception as e:
×
35
                if i == max_redo:
×
36
                    raise e
×
37
                traceback.print_exc(file=sys.stdout)
×
38
                logger.debug("Fail to execute {}. Retrying in {} sec...".format(call.__name__, sleep_time))
×
39
                time.sleep(sleep_time)
×
40
                sleep_time = sleep_time * 2
×
41
                continue  # redo
×
42

43
    @staticmethod
1✔
44
    def is_valid_url(url):
1✔
45
        """Test if a string an url
46

47
        Parameters
48
        ----------
49
        url : string
50
            The url to test
51

52
        Returns
53
        -------
54
        bool
55
            True is url is valid
56
        """
57
        regex = re.compile(
1✔
58
            r'^(?:http|ftp)s?://'  # http:// or https://
59
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain...
60
            r'localhost|'  # localhost...
61
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
62
            r'(?::\d+)?'  # optional port
63
            r'(?:/?|[/?]\S+)$',
64
            re.IGNORECASE)
65

66
        return re.match(regex, url) is not None
1✔
67

68
    @staticmethod
1✔
69
    def get_random_string(number):
1✔
70
        """return a random string of n character
71

72
        Parameters
73
        ----------
74
        number : int
75
            number of character of the random string
76

77
        Returns
78
        -------
79
        str
80
            a random string of n chars
81
        """
82
        alpabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
1✔
83
        return ''.join(random.choice(alpabet) for i in range(number))
1✔
84

85
    @staticmethod
1✔
86
    def camel_case(string):
1✔
87
        """camelCase a string"""
88
        return ''.join(x for x in string.title() if not x.isspace())
×
89

90
    @staticmethod
1✔
91
    def unique(dup_list):
1✔
92
        """return the list with duplicate elements removed and keep order"""
93
        return [i for n, i in enumerate(dup_list) if i not in dup_list[n + 1:]]
1✔
94

95
    @staticmethod
1✔
96
    def intersect(a, b):
1✔
97
        """return the intersection of two lists"""
98
        return list(set(a) & set(b))
×
99

100
    @staticmethod
1✔
101
    def union(a, b):
1✔
102
        """return the union of two lists"""
103
        return list(set(a) | set(b))
×
104

105
    @staticmethod
1✔
106
    def humansize_to_bytes(hsize):
1✔
107
        """Convert human-readable string into bytes
108

109
        Parameters
110
        ----------
111
        hsize : string
112
            Human readable string
113

114
        Returns
115
        -------
116
        int
117
            Bytes
118
        """
119
        units = {
1✔
120
            "b": 1,
121
            "kb": 10**3,
122
            "mb": 10**6,
123
            "gb": 10**9,
124
            "tb": 10**12,
125
            "o": 1,
126
            "ko": 10**3,
127
            "mo": 10**6,
128
            "go": 10**9,
129
            "to": 10**12,
130
            "": 1,
131
            "k": 10**3,
132
            "m": 10**6,
133
            "g": 10**9,
134
            "t": 10**12
135
        }
136

137
        number = ""
1✔
138
        unit = ""
1✔
139
        for char in ''.join(hsize.lower().split()):
1✔
140

141
            if char.isdigit() or char == ".":
1✔
142
                number += char
1✔
143
            else:
144
                unit += char.lower()
1✔
145
        number = float(number)
1✔
146

147
        if number == 0:
1✔
148
            return 0
1✔
149
        return int(number * units[unit])
1✔
150

151
    @staticmethod
1✔
152
    def check_key_in_list_of_dict(list_of_dict, key):
1✔
153
        """Check if a key is empty in a list of dict
154

155
        Parameters
156
        ----------
157
        list_of_dict : List of dict
158
            List of dict to test
159
        key : str
160
            The key
161

162
        Returns
163
        -------
164
        bool
165
            False if all key are empty
166
        """
167
        for elem in list_of_dict:
1✔
168
            if elem[key]:
1✔
169
                return True
×
170

171
        return False
1✔
172

173
    @staticmethod
1✔
174
    def is_url(url):
1✔
175
        """Check is string is an url
176

177
        Parameters
178
        ----------
179
        url : string
180
            string to test
181

182
        Returns
183
        -------
184
        bool
185
            True if string is url
186
        """
187
        regex = re.compile(
1✔
188
            r'^(?:http|ftp)s?://'  # http:// or https://
189
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain...
190
            r'localhost|'  # localhost...
191
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
192
            r'(?::\d+)?'  # optional port
193
            r'(?:/?|[/?]\S+)$',
194
            re.IGNORECASE)
195
        return re.match(regex, url) is not None
1✔
196

197

198
class cached_property(object):
1✔
199
    """Like @property on a member function, but also cache the calculation in
200
    self.__dict__[function name].
201
    The function is called only once since the cache stored as an instance
202
    attribute override the property residing in the class attributes. Following accesses
203
    cost no more than standard Python attribute access.
204
    If the instance attribute is deleted the next access will re-evaluate the function.
205
    Source: https://blog.ionelmc.ro/2014/11/04/an-interesting-python-descriptor-quirk/
206

207
    usage
208
    -----
209
    class Shape(object):
210

211
        @cached_property
212
        def area(self):
213
            # compute value
214
            return value
215

216
    Attributes
217
    ----------
218
    func : TYPE
219
        Description
220
    """
221
    __slots__ = ('func')
1✔
222

223
    def __init__(self, func):
1✔
224

225
        self.func = func
1✔
226

227
    def __get__(self, obj, cls):
1✔
228

229
        value = obj.__dict__[self.func.__name__] = self.func(obj)
1✔
230
        return value
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc