Skip to main content

Hat utility library

Project description

This library is part of Hat Open project - open-source framework of tools and libraries for developing applications used for remote monitoring, control and management of intelligent electronic devices such as IoT devices, PLCs, industrial automation or home automation systems.

Development of Hat Open and associated repositories is sponsored by Končar - Power Plant and Electric Traction Engineering Inc. (Končar KET - https://www.koncar-ket.hr).

For more information see:

About

Common utility functions not available as part of standard library.

  • hat.util.first:

    def first(xs: typing.Iterable[T],
              fn: typing.Callable[[T], bool] = lambda _: True,
              default: typing.Optional[T] = None
              ) -> typing.Optional[T]:
        """Return the first element from iterable that satisfies predicate
           `fn`, or `default` if no such element exists.
    
        Args:
            xs: collection
            fn: predicate
            default: default value
    
        Example::
    
            assert first(range(3)) == 0
            assert first(range(3), lambda x: x > 1) == 2
            assert first(range(3), lambda x: x > 2) is None
            assert first(range(3), lambda x: x > 2, 123) == 123
            assert first({1: 'a', 2: 'b', 3: 'c'}) == 1
            assert first([], default=123) == 123
    
        """
  • hat.util.CallbackRegistry:

    class RegisterCallbackHandle(typing.NamedTuple):
        """Handle for canceling callback registration."""
    
        cancel: typing.Callable[[], None]
        """cancel callback registration"""
    
        def __enter__(self): ...
    
        def __exit__(self, *args): ...
    
    ExceptionCb: typing.Type = typing.Callable[[Exception], None]
    """Exception callback"""
    
    class CallbackRegistry:
        """Registry that enables callback registration and notification.
    
        Callbacks in the registry are notified sequentially with
        :meth:`CallbackRegistry.notify`. If a callback raises an exception, the
        exception is caught and `exception_cb` handler is called. Notification
        of subsequent callbacks is not interrupted. If handler is `None`, the
        exception is reraised and no subsequent callback is notified.
    
        Example::
    
            x = []
            y = []
            registry = CallbackRegistry()
    
            registry.register(x.append)
            registry.notify(1)
    
            with registry.register(y.append):
                registry.notify(2)
    
            registry.notify(3)
    
            assert x == [1, 2, 3]
            assert y == [2]
    
        """
    
        def __init__(self,
                     exception_cb: typing.Optional[ExceptionCb] = None): ...
    
        def register(self,
                     cb: typing.Callable
                     ) -> RegisterCallbackHandle:
            """Register a callback."""
    
        def notify(self, *args, **kwargs):
            """Notify all registered callbacks."""
  • hat.util.parse_url_query:

    def parse_url_query(query: str) -> typing.Dict[str, str]:
        """Parse url query string.
    
        Returns a dictionary of field names and their values.
    
        Args:
            query: url query string
    
        Example::
    
            url = urllib.parse.urlparse('https://pypi.org/search/?q=hat-util')
            args = parse_url_query(url.query)
            assert args == {'q': 'hat-util'}
    
        """
  • hat.util.get_unused_tcp_port and hat.util.get_unused_udp_port:

    def get_unused_tcp_port() -> int:
        """Search for unused TCP port"""
    
    def get_unused_udp_port() -> int:
        """Search for unused UDP port"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

hat_util-0.4.1.dev20210120-cp39-none-any.whl (4.4 kB view details)

Uploaded CPython 3.9

hat_util-0.4.1.dev20210120-cp38-none-any.whl (4.4 kB view details)

Uploaded CPython 3.8

File details

Details for the file hat_util-0.4.1.dev20210120-cp39-none-any.whl.

File metadata

  • Download URL: hat_util-0.4.1.dev20210120-cp39-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for hat_util-0.4.1.dev20210120-cp39-none-any.whl
Algorithm Hash digest
SHA256 90eab2c9fac34a4b341f9ab7b7dc59f1a9b89662b7d52a8ab960d120bd708f44
MD5 0002ddc64789ae0234731884144b5e58
BLAKE2b-256 7e0727a06e572635fac6ab1e9d35ce002ea1e5d88f28ac81f6aadd3f379a9aa0

See more details on using hashes here.

File details

Details for the file hat_util-0.4.1.dev20210120-cp38-none-any.whl.

File metadata

  • Download URL: hat_util-0.4.1.dev20210120-cp38-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for hat_util-0.4.1.dev20210120-cp38-none-any.whl
Algorithm Hash digest
SHA256 6b3dd55ae7b31f8bf98ae2172df35a06b91aba42431ed3f8206134602902b045
MD5 edcde36ddb3cd6ce92d3db629e90850d
BLAKE2b-256 dea44d72be739d3c140fcc0f76239966ebc7c7930250f5a647e7c47a44c0f203

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page