Skip to content
Snippets Groups Projects
Commit 2e6983e5 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

changes in `satella.coding.misc`

parent fe5e2936
No related branches found
No related tags found
No related merge requests found
......@@ -4,4 +4,4 @@
* added `timeout` and `timeouted` property to `satella.time.measure`
* thanks to this some code has been simplified
* some methods from `measure` are pending deprecation
* changes in `satella.coding.misc`
__version__ = '2.14.17_a3'
__version__ = '2.14.17_a4'
......@@ -27,6 +27,7 @@ def sync_threadpool(tpe: tp.Union[ExecutorWrapper, ThreadPoolExecutor],
assert isinstance(tpe, ThreadPoolExecutor), 'Must be a ThreadPoolExecutor!'
with measure(timeout=max_wait) as measurement:
# noinspection PyProtectedMember
workers = tpe._max_workers
atm_n = AtomicNumber(workers)
cond = Condition()
......@@ -39,6 +40,7 @@ def sync_threadpool(tpe: tp.Union[ExecutorWrapper, ThreadPoolExecutor],
futures = [tpe.submit(decrease_atm) for _ in range(workers)]
# wait for all currently scheduled jobs to be picked up
# noinspection PyProtectedMember
while tpe._work_queue.qsize() > 0:
if max_wait is not None:
if measurement() > max_wait:
......
......@@ -49,10 +49,8 @@ def precondition(*t_ops: Condition, **kw_opts: Condition):
for t_op in t_ops:
if t_op is None:
precond_ = _TRUE
elif isinstance(t_op, str):
precond_ = source_to_function(t_op)
else:
precond_ = t_op
precond_ = source_to_function(t_op)
tn_ops.append(precond_)
......@@ -100,10 +98,7 @@ def postcondition(condition: Condition):
:param condition: callable that accepts a single argument, the return value of the function.
Can be also a string, in which case it is an expression about the value x of return
"""
if isinstance(condition, str):
q = dict(globals())
exec('_precond = lambda x: ' + condition, q)
condition = q['_precond']
condition = source_to_function(condition)
def outer(fun):
@wraps(fun)
......
......@@ -100,18 +100,25 @@ def chain_callables(callable1: tp.Callable, callable2: tp.Callable) -> tp.Callab
return inner
def source_to_function(src: str) -> tp.Callable[[tp.Any], tp.Any]:
def source_to_function(src: tp.Union[tp.Callable, str]) -> tp.Callable[[tp.Any], tp.Any]:
"""
If src is callable, return it as-is
Transform a string containing a Python expression with a variable x to a lambda.
It will be treated as if it was appended to 'lambda x: '
WARNING: Do not run untrusted data. Familiarize yourself with the dangers of passing
unvalidated data to exec() or eval()!
:param src: a callable or a Python string expression
:return: a callable
"""
q = dict(globals())
exec('_precond = lambda x: ' + src, q)
return q['_precond']
if callable(src):
return src
else:
q = dict(globals())
exec('_precond = lambda x: ' + src, q)
return q['_precond']
def update_attr_if_none(obj: object, attr: str, value: tp.Any,
......
......@@ -7,7 +7,7 @@ def is_running_as_root() -> bool:
"""
Is this process running as root?
Checks whether EUID is 0
Checks whether effective UID is 0
:return: bool
:raises OSError: called on Windows!
......
......@@ -11,12 +11,12 @@ class PIDFileLock:
Usage:
>>> with PIDFileLock('myservice.pid'):
>>> with PIDFileLock('my_service.pid'):
>>> ... rest of code ..
Any alternatively
>>> pid_lock = PIDFileLock('myservice.pid')
>>> pid_lock = PIDFileLock('my_service.pid')
>>> pid_lock.acquire()
>>> ...
>>> pid_lock.release()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment