Skip to content

Commit 480cc5b

Browse files
author
Samiul Sk
committed
util: add utility functions
1 parent 48de1c0 commit 480cc5b

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

imagekitio/utils/__init__.py

Whitespace-only changes.

imagekitio/utils/calculation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import hashlib
2+
import hmac
3+
import uuid
4+
from datetime import datetime as dt
5+
6+
from imagekitio.constants import ERRORS
7+
8+
DEFAULT_TIME_DIFF = 60 * 30
9+
10+
11+
def hamming_distance(first: str, second: str) -> int:
12+
"""Calculate Hamming Distance between to hex string
13+
"""
14+
try:
15+
a = bin(int(first, 16))[2:].zfill(64)
16+
b = bin(int(second, 16))[2:].zfill(64)
17+
except TypeError:
18+
raise TypeError(ERRORS.INVALID_PHASH_VALUE.value)
19+
20+
return len(list(filter(lambda x: ord(x[0]) ^ ord(x[1]), zip(a, b))))
21+
22+
23+
def get_authenticated_params(token, expire, private_key):
24+
default_expire = int(dt.now().strftime("%s")) + DEFAULT_TIME_DIFF
25+
auth_params = {"token": token, "expire": expire, "signature": ""}
26+
27+
if not private_key:
28+
return
29+
signature = hmac.new(
30+
key=private_key.encode(),
31+
msg=(token + str(expire)).encode(),
32+
digestmod=hashlib.sha1,
33+
).hexdigest()
34+
35+
auth_params["token"] = token or str(uuid.uuid4())
36+
auth_params["expire"] = expire or default_expire
37+
auth_params["signature"] = signature
38+
39+
return auth_params

imagekitio/utils/formatter.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import re
2+
from collections import ChainMap, OrderedDict
3+
from typing import Dict, List
4+
5+
6+
def camel_to_snake(name):
7+
"""
8+
converts camelCase to snake_case for python
9+
"""
10+
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
11+
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
12+
13+
14+
def snake_to_lower_camel(word):
15+
"""
16+
changes word snake to lower camelCase example: my_plan -> MyPlan
17+
:return camelCaseWord
18+
"""
19+
word_list = word.split("_")
20+
if word_list:
21+
return word_list[0] + "".join(x.title() for x in word_list[1:])
22+
return word
23+
24+
25+
def request_formatter(data: dict) -> dict:
26+
"""Converts all keys to camelCase format required for ImageKit server
27+
:param data: dict()
28+
:return: converted_dict -> dict()
29+
"""
30+
return {snake_to_lower_camel(key): val for key, val in data.items()}
31+
32+
33+
def camel_dict_to_snake_dict(data: dict) -> dict:
34+
return {camel_to_snake(key): val for key, val in data.items()}
35+
36+
37+
def flatten_dict(dict_list: List[Dict]) -> OrderedDict:
38+
"""Convert list of dictionary to flatten dict
39+
:param dict_list: list of dictionary
40+
:return: flatten_dict
41+
"""
42+
flat_dict = OrderedDict()
43+
for dict_var in dict_list:
44+
flat_dict.update(dict_var)
45+
return flat_dict

0 commit comments

Comments
 (0)