Best Python code snippet using molecule_python
cache.py
Source:cache.py
...3STATE_DIR = "/tmp/pyfunstate"4class CachedClass(object):5 def __init__(self, name):6 self.name = name7 sf = self._get_state_file("r")8 self._data_cache = json.loads(sf.read())9 sf.close()10 self._cache_context = None11 def clear(self):12 if self.get_cache_context() in self._data_cache:13 self._data_cache[self.get_cache_context()] = {}14 def clear_all(self):15 self._data_cache = {}16 def get_cache_context(self):17 return self._cache_context18 def get_contextless_cache(self):19 if True not in self._data_cache:20 self._data_cache[True] = {}21 return self._data_cache[True]22 def _get_state_file(self, mode):23 if not os.path.isdir(STATE_DIR):24 os.mkdir(STATE_DIR)25 fname = "funtoolstate_%s.json" % self.name26 fpath = os.path.join(STATE_DIR, fname)27 if not os.path.isfile(fpath):28 f = open(os.path.join(fpath), "w")29 f.write("{}")30 f.close()31 f = open(os.path.join(fpath), mode)32 return f33 def save_contextless_cache(self):34 sf = self._get_state_file("w")35 sf.write(json.dumps(self.get_contextless_cache()))36 sf.close()37 def clear_contextless_cache(self):38 sf = self._get_state_file("w")39 sf.write("{}")40 sf.close()41 self.get_contextless_cache().clear()42 def set_cache_context(self, context_value):43 self._cache_context = context_value44def cache(func, contextless=False):45 def wrapper(self, *args, **kwargs):46 if not isinstance(self, CachedClass):47 raise TypeError("Cache can only be called on subclasses of CachedClass!")48 func_name = func.__name__49 if func_name == "wrapper":50 raise Exception("Use cache decorator before any other decorator!")51 data = None52 kwargs_entries = sorted(list(kwargs.items()))...
request_handler.py
Source:request_handler.py
...39 self._asset_id40 )41 state['x_uid'] = json.loads(oauth_token)42 return state43 def _get_state_file(self):44 dirpath = os.path.split(__file__)[0]45 state_file = "{0}/{1}_state.json".format(dirpath, self._asset_id)46 return state_file47 def delete_state(self):48 state_file = self._get_state_file()49 try:50 os.remove(state_file)51 except Exception:52 pass53 return True54 def save_state(self, state):55 state = self._encrypt_state(state)56 state_file = self._get_state_file()57 try:58 with open(state_file, 'w+') as fp:59 fp.write(json.dumps(state))60 except Exception:61 pass62 return True63 def load_state(self):64 state_file = self._get_state_file()65 state = {}66 try:67 with open(state_file, 'r') as fp:68 in_json = fp.read()69 state = json.loads(in_json)70 except Exception:71 pass72 state = self._decrypt_state(state)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!