Skip to main content

global-manager

Tests codecov Mypy pypi downloads versions

Global thread context manager.

Installation

pip install global-manager

Examples

from typing import Optional
import dataclasses
import logging
from global_manager import GlobalManager

logger = logging.getLogger('my_view')

@dataclasses.dataclass
class UserContext:
    """Some context"""
    ip: str
    profile_id: int

class UserContextManager(GlobalManager[UserContext]):
    @classmethod
    def get_current_user_context(cls) -> Optional[UserContext]:
        """a business meaning method name is a good practice"""
        return cls.get_current_context()


def my_func():
    user_context: Optional[UserContext] = UserContextManager.get_current_user_context()
    # retrieve context
    logger.debug('my_view profile_id=%s, ip=%s', user_context.profile_id, user_context.ip)
    # other logic ...


# some where in code, m.b. in web framework
def my_view(request):
    # Imagine that the data is obtained from the request
    user_context = UserContext(ip='127.0.0.1', profile_id=1)
    
    with UserContext(user_context):
        return my_func()

Context in context.

with UserContext(uc1):
    UserContext.get_current_user_context()  # uc1 context
    
    with UserContext(uc2):
        UserContext.get_current_user_context() # uc2 context

    UserContext.get_current_user_context()  # uc1 context

UserContext.get_current_user_context()  # None

Async notations is supported too.

async with UserContext(uc1):
    UserContext.get_current_user_context()  # uc1 context
    
    async with UserContext(uc2):
        UserContext.get_current_user_context() # uc2 context

    UserContext.get_current_user_context()  # uc1 context

UserContext.get_current_user_context()  # None

Metadata

Release files for global-manager 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for global-manager 1.0.4
File Size Uploaded Provenance Actions
global_manager-1.0.4.tar.gz 3.7 kB Without provenance Details for global_manager-1.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for global-manager 1.0.4
File Interpreter ABI Platform Actions
global_manager-1.0.4-py3-none-any.whl Python 3 none any Details for global_manager-1.0.4-py3-none-any.whl

Total release size: 8.3 kB

Release files / global_manager-1.0.4.tar.gz

Download URL global_manager-1.0.4.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5684025aa3281b27948e3df681331181bbe7c294b743839034087d3e94629e3e
BLAKE2b-256 checksum
How to use checksums
1f8644780fb8b1e167ee00000db6a1a542d38866f5e43c8b06c9ebc74245f963
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release files / global_manager-1.0.4-py3-none-any.whl

Download URL global_manager-1.0.4-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2fee46be370768cf5367b0df67a540a1586184523f55049a40e6f970545681c
BLAKE2b-256 checksum
How to use checksums
1593cd616b1e13ca76942b30d03c125a6416cd386e9389db5fd0d43992b2e299
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Security for global-manager 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page