Best Python code snippet using autotest_python
base_job.py
Source:base_job.py
...115 try:116 return method(self, *args, **dargs)117 finally:118 if not already_have_lock:119 self._unlock_backing_file()120 wrapped_method.__name__ = method.__name__121 wrapped_method.__doc__ = method.__doc__122 return wrapped_method123# decorator for use with job_state methods124def with_backing_file(method):125 """A decorator to perform a lock-read-*-write-unlock cycle.126 When applied to a method, this decorator will automatically wrap127 calls to the method in a lock-and-read before the call followed by a128 write-and-unlock. Any operation that is reading or writing state129 should be decorated with this method to ensure that backing file130 state is consistently maintained.131 """132 @with_backing_lock133 def wrapped_method(self, *args, **dargs):134 self._read_from_backing_file()135 try:136 return method(self, *args, **dargs)137 finally:138 self._write_to_backing_file()139 wrapped_method.__name__ = method.__name__140 wrapped_method.__doc__ = method.__doc__141 return wrapped_method142class job_state(object):143 """A class for managing explicit job and user state, optionally persistent.144 The class allows you to save state by name (like a dictionary). Any state145 stored in this class should be picklable and deep copyable. While this is146 not enforced it is recommended that only valid python identifiers be used147 as names. Additionally, the namespace 'stateful_property' is used for148 storing the valued associated with properties constructed using the149 property_factory method.150 """151 NO_DEFAULT = object()152 PICKLE_PROTOCOL = 2 # highest protocol available in python 2.4153 def __init__(self):154 """Initialize the job state."""155 self._state = {}156 self._backing_file = None157 self._backing_file_initialized = False158 self._backing_file_lock = None159 def _lock_backing_file(self):160 """Acquire a lock on the backing file."""161 if self._backing_file:162 self._backing_file_lock = open(self._backing_file, 'a')163 fcntl.flock(self._backing_file_lock, fcntl.LOCK_EX)164 def _unlock_backing_file(self):165 """Release a lock on the backing file."""166 if self._backing_file_lock:167 fcntl.flock(self._backing_file_lock, fcntl.LOCK_UN)168 self._backing_file_lock.close()169 self._backing_file_lock = None170 def read_from_file(self, file_path, merge=True):171 """Read in any state from the file at file_path.172 When merge=True, any state specified only in-memory will be preserved.173 Any state specified on-disk will be set in-memory, even if an in-memory174 setting already exists.175 @param file_path: The path where the state should be read from. It must176 exist but it can be empty.177 @param merge: If true, merge the on-disk state with the in-memory178 state. If false, replace the in-memory state with the on-disk...
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!!