forked from allure-framework/allure-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
95 lines (70 loc) · 2.4 KB
/
utils.py
File metadata and controls
95 lines (70 loc) · 2.4 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
import sys
import time
import uuid
import inspect
import hashlib
import platform
from six import text_type
def md5(*args):
m = hashlib.md5()
for arg in args:
part = arg.encode('utf-8')
m.update(part)
return m.hexdigest()
def uuid4():
return str(uuid.uuid4())
def now():
return int(round(1000 * time.time()))
def platform_label():
major_version, _, __ = platform.python_version_tuple()
implementation = platform.python_implementation()
return '{implementation}{major_version}'.format(implementation=implementation.lower(),
major_version=major_version)
def represent(item):
"""
>>> represent(None)
'None'
>>> represent(123)
'123'
>>> represent('hi') == u"'hi'"
True
>>> represent(u'привет') == u"'привет'"
True
>>> from sys import version_info
>>> represent(str(bytearray([0xd0, 0xbf]))) == u"'\u043f'" if version_info.major < 3 else True
True
>>> from struct import pack
>>> result = "<type 'str'>" if version_info.major < 3 else "<class 'bytes'>"
>>> represent(pack('h', 0x89)) == result
True
>>> result = "<type 'int'>" if version_info.major < 3 else "<class 'int'>"
>>> represent(int) == result
True
>>> represent(represent) # doctest: +ELLIPSIS
'<function represent at ...>'
>>> represent([represent]) # doctest: +ELLIPSIS
'[<function represent at ...>]'
>>> class ClassWithName(object):
... pass
>>> represent(ClassWithName)
"<class 'utils.ClassWithName'>"
"""
if sys.version_info.major < 3 and isinstance(item, str):
try:
item = item.decode(encoding='UTF-8')
except UnicodeDecodeError:
pass
if isinstance(item, text_type):
return u'\'%s\'' % item
elif isinstance(item, (bytes, bytearray)):
return repr(type(item))
else:
return repr(item)
def func_parameters(func, *a, **kw):
bowels = inspect.getargspec(func) if sys.version_info.major < 3 else inspect.getfullargspec(func)
args_dict = dict(zip(bowels.args, map(represent, a)))
kwargs_dict = dict(zip(kw, list(map(lambda i: represent(kw[i]), kw))))
kwarg_defaults = dict(zip(reversed(bowels.args), reversed(list(map(represent, bowels.defaults or ())))))
kwarg_defaults.update(kwargs_dict)
return args_dict, kwarg_defaults