-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcebula_common.py
84 lines (62 loc) · 2.58 KB
/
cebula_common.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
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
from collections import OrderedDict
from loguru import logger
import hashlib
import os
import pickle
import redis
import threading
import traceback
import unidecode
class IdList:
def __init__(self, config: dict):
self.config = config
self.redis = redis.Redis(**self.config)
self.lock = threading.Lock()
def is_id_present(self, id_):
with self.lock:
return self.redis.sismember('cebulapy_cache', id_)
return False
def put_ids(self, ids):
with self.lock:
if len(ids) > 0:
self.redis.sadd('cebulapy_cache', *ids)
def __getstate__(self):
state_dict = self.__dict__.copy()
del state_dict['redis']
del state_dict['lock']
return state_dict
def __setstate(self, state_dict):
state_dict['lock'] = threading.Lock()
state_dict['redis'] = redis.Redis(**state_dict['config'])
self.__dict__ = state_dict
def sha1sum(string_to_encode: str) -> str:
return hashlib.sha1(string_to_encode.encode('utf-8')).hexdigest()
def sorted_dict(input_dict: dict) -> dict:
dict_ = input_dict.copy()
if 'randomize_user_agent' in dict_ and 'user_agent' in dict_:
del dict_['user_agent']
return dict(OrderedDict(sorted(dict_.items())))
def get_pickle_dir() -> str:
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'pickles')
def does_pickle_exist(data_hash: str, class_name: str) -> bool:
return os.path.exists(os.path.join(get_pickle_dir(), f"{class_name}-{data_hash}.pickle"))
def write_pickle(data_hash: str, object_to_be_pickled):
try:
with open(os.path.join(get_pickle_dir(), f"{object_to_be_pickled.__class__.__name__}-{data_hash}.pickle"), 'wb') as pickle_file:
pickle.dump(object_to_be_pickled, pickle_file)
except Exception as e:
logger.error(f"cebula_common: Got exception: {e}")
os.unlink(os.path.join(get_pickle_dir(), f"{object_to_be_pickled.__class__.__name__}-{data_hash}.pickle"))
traceback.print_stack()
def load_pickle(data_hash: str, class_name: str):
with open(os.path.join(get_pickle_dir(), f"{class_name}-{data_hash}.pickle"), 'rb') as pickle_file:
return pickle.load(pickle_file)
def remove_accents(input_string: str) -> str:
return unidecode.unidecode(input_string)
def for_all_methods(decorator):
def decorate(cls):
for attr in cls.__dict__: # there's propably a better way to do this
if callable(getattr(cls, attr)):
setattr(cls, attr, decorator(getattr(cls, attr)))
return cls
return decorate