forked from ucloud/ucloud-sdk-python2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs.py
More file actions
69 lines (51 loc) · 1.7 KB
/
funcs.py
File metadata and controls
69 lines (51 loc) · 1.7 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
# -*- coding: utf-8 -*-
import datetime
import time
def concat(*args):
""" cancat strings
>>> concat(42, 'foo', 'bar')
'42foobar'
"""
return "".join([str(s) for s in args])
def concat_without_dot(args):
""" replace blank
>>> concat_without_dot('42foo bar')
'42foobar'
"""
return "".join([str(s) for s in args.split()])
def search_value(array, origin_key, origin_value, dest_key):
""" given origin key and value,search dest_value form array by dest_key
>>> d = [{"UHostId": "foo", "Name": "testing"}]
>>> search_value(d, "Name", "testing", "UHostId")
'foo'
"""
arr = [i.get(dest_key, "") for i in array if i[origin_key] == origin_value]
if arr:
return arr[0]
return ""
def timedelta(timestamp, value, typ="days"):
""" given timestamp(10bit) and calculate relative delta time
>>> timedelta(0, 1, "days")
86400.0
:param timestamp: timestamp (round to second)
:param value: float, can as positive or negative
:param typ: days/hours
:return: timestamp
"""
value = int(value)
dt = datetime.datetime.fromtimestamp(float(timestamp))
if typ == "days":
dt += datetime.timedelta(days=value)
elif typ == "hours":
dt += datetime.timedelta(hours=value)
return time.mktime(dt.timetuple())
def get_timestamp(length=13):
""" get current timestamp string
>>> len(str(int(get_timestamp(10))))
10
:param length: length of timestamp, can only between 0 and 16
:return:
"""
if isinstance(length, int) and 0 < length < 17:
return float("{:.6f}".format(time.time()).replace(".", "")[:length])
raise ValueError("timestamp length can only between 0 and 16.")