• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LudovicRousseau / pyscard / 11417416674

19 Oct 2024 11:43AM UTC coverage: 65.458% (-0.09%) from 65.543%
11417416674

push

github

LudovicRousseau
PCSCCardRequest: use "if xxx not in yyy:"

It is more Pythonist than "if not xxx in yyy:"

104 of 476 branches covered (21.85%)

0 of 2 new or added lines in 1 file covered. (0.0%)

130 existing lines in 23 files now uncovered.

4296 of 6563 relevant lines covered (65.46%)

4.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/smartcard/Synchronization.py
1
"""
2
from Thinking in Python, Bruce Eckel
3
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Observer.html
4

5
(c) Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
6

7
Simple emulation of Java's 'synchronized'
8
keyword, from Peter Norvig.
9
"""
10

11
from __future__ import annotations
20✔
12

13
import functools
20✔
14
import sys
20✔
15
import threading
20✔
16
from collections.abc import Iterable
20✔
17
from typing import Any, Callable, Protocol, TypeVar
20✔
18

19
if sys.version_info >= (3, 10):
20✔
20
    from typing import ParamSpec
16✔
21
else:
UNCOV
22
    from typing_extensions import ParamSpec
4✔
23

24

25
T = TypeVar("T")
20✔
26
P = ParamSpec("P")
20✔
27

28

29
def synchronized(method: Callable[P, T]) -> Callable[P, T]:
20✔
30
    @functools.wraps(method)
20✔
31
    def f(self: _SynchronizationProtocol, *args: Any, **kwargs: Any) -> Any:
20✔
32
        with self.mutex:
19✔
33
            return method(self, *args, **kwargs)
19✔
34

35
    return f
20✔
36

37

38
def synchronize(klass: type, names: str | Iterable[str] | None = None) -> None:
20✔
39
    """Synchronize methods in the given class.
40
    Only synchronize the methods whose names are
41
    given, or all methods if names=None."""
42

43
    if isinstance(names, str):
20✔
44
        names = names.split()
20✔
45
    for name, val in list(klass.__dict__.items()):
20✔
46
        if callable(val) and name != "__init__" and (names is None or name in names):
20✔
47
            setattr(klass, name, synchronized(val))
20✔
48

49

50
class _SynchronizationProtocol(Protocol):
20✔
51
    mutex: threading.Lock | threading.RLock
20✔
52

53

54
class Synchronization(_SynchronizationProtocol):
20✔
55
    # You can create your own self.mutex, or inherit from this class:
56

57
    def __init__(self):
20✔
58
        self.mutex = threading.RLock()
19✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc