-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
55 lines (41 loc) · 1.29 KB
/
util.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
import requests
import json
import logging
import toml
from config import PATH, HTTP_TIMEOUT
from pathlib import Path
def apirequest(url, params):
logging.debug("Now getting '%s' with parameters %s" % (url, params))
r = requests.get(url, params = params, timeout = HTTP_TIMEOUT)
return r.json()
def mapobj(obj, fn):
newobj = {}
for (key, val) in obj.items():
newobj[key] = fn(val)
return newobj
def load_json(filename):
json_data = open(filename, "r").read()
return json.loads(json_data)
def load_datafile(filename):
path = PATH + "/commands/" + filename + ".json"
return load_json(path)
# This variable replacement stuff is pretty ugly here, because of the
# variable number of arguments, but i don't really know how to do this
# otherwise
def sprintf(string, args):
return string % args[:string.count("%s")]
def dump(val):
print(json.dumps(val, indent = 4))
"""
Converts an iterable to chunks, e.g.
batch([1,2,3,4], 2) == [1,2],[3,4]
"""
def batch_iterable(iterable, n = 1):
for i in range(0, len(iterable), n):
yield iterable[i:i + n]
def get_version():
confpath = Path(PATH) / "pyproject.toml"
with open(confpath) as f:
project = toml.load(f)
version = project["tool"]["poetry"]["version"]
return version