This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantumutils.py
57 lines (50 loc) · 1.63 KB
/
quantumutils.py
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
import aiohttp
def fill(item:dict,**kwargs):
"""
:param item: dictionary to add attribute
:param kwargs: additional args
:return:
"""
def partition(text: str, length: int)->list:
"""
split a text according to its length
:param text: input text
:param length: maximum length of each substring
:return: a list of substrings
"""
res=[]
while len(text)>0:
res.append(text[:length])
text=text[length:]
return res
def tdm(td):
"""
converting a datetime.datetime object to a timestamp
:param td: A datetime.datetime instance
:return: time in milliseconds
"""
return ((td.days * 86400000) + (td.seconds * 1000)) + (td.microseconds / 1000)
def find(substring, string):
"""
gets the occurrences of the substring in the main string
:param substring: the text to find
:param string: the main search string
:return: the indices of occurrence
"""
last_found = -1 # Begin at -1 so the next position to search from is 0
while True:
# Find next index of substring, by starting after its last known position
last_found = string.find(substring, last_found + 1)
if last_found == -1:
break # All occurrences have been found
yield last_found
async def getjson(url):
"""
gets json content from url if supported
:param url: the url used
:return: the json output
"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
f = await response.json(encoding='utf8')
return f