Skip to main content

一个便捷实用的上下文管理工具

Project description

Mapgraph

Mapgraph 是一个便捷实用的上下文管理工具

一阴一阳之谓道

CodeFactor GitHub CodeQL

功能

  • 上下文管理/切换
  • 描述器支持
  • 一流的类型支持

安装

pip install mapgraph

Or

pdm add mapgraph

入门指南

全局上下文对象

from mapgraph.instance_of import get_instance
from mapgraph.globals import GLOBAL_INSTANCE_CONTEXT

GLOBAL_INSTANCE_CONTEXT.store(666)

assert get_instance(int) == 666

描述器支持

from mapgraph.instance_of import InstanceOf, get_instance
from mapgraph.globals import GLOBAL_INSTANCE_CONTEXT

GLOBAL_INSTANCE_CONTEXT.store(666)

class Test:
    a = InstanceOf(int)

assert Test().a == 666

实例上下文

from mapgraph.instance_of import InstanceOf, get_instance
from mapgraph.context import InstanceContext
from mapgraph.globals import GLOBAL_INSTANCE_CONTEXT

context = InstanceContext()

GLOBAL_INSTANCE_CONTEXT.store(666)

context.store(6, "6")

assert get_instance(int) == 666

with context.scope():
    assert get_instance(int) == 6
    assert get_instance(str) == "6"

泛型支持

from typing import TypeVar, Generic
from typing_extensions import Annotated

from mapgraph.instance_of import get_instance
from mapgraph.context import InstanceContext
from mapgraph.globals import GLOBAL_INSTANCE_CONTEXT

from pydantic import Field

T = TypeVar("T")


class Obj(Generic[T]):
    a: T

    def __init__(self, a: T):
        self.a = a


context = InstanceContext()
local_context = InstanceContext()

GLOBAL_INSTANCE_CONTEXT.store("GLOBAL", 10, {"a": 10}, Obj[int](10))
local_context.store(1, {1: "a"}, Obj[str]("1"))
context.store("context")


assert get_instance(Obj[int]).a == 10

with local_context.scope():
    assert get_instance(Obj[str]).a == "1"
    with context.scope():
        assert get_instance(int) == 1
        assert get_instance(Annotated[int, Field(gt=1)]) == 10
        assert get_instance(dict) == {1: "a"}
        assert get_instance(dict[str, int]) == {"a": 10}

支持类型

like_isinstance

  • 基础类型 str/int/...
  • 容器泛型 list[T]/dict[K, V]/...
  • Union 类型类型
  • Type
  • TypeVar 类型变量
  • 泛型类 Generic[T]
  • Annotated/Field 注解类型
  • Protocol 协议类型
  • Protocol[T] 泛型协议类型
  • TypedDict 字典类型

like_issubclass

  • 基础类型 str/int
  • 容器泛型 list[T]/dict[K, V]
  • Union 类型类型
  • Type
  • TypeVar 类型变量
  • 泛型类 Generic[T]
  • Protocol 协议类型
  • Protocol[T] 泛型协议类型

Project details


Download files

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

Source Distribution

mapgraph-0.1.7.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

mapgraph-0.1.7-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file mapgraph-0.1.7.tar.gz.

File metadata

  • Download URL: mapgraph-0.1.7.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.0 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for mapgraph-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4e1194641a2e761755a7057dc276f4b31bca6c293bb32741d62a6adf9f7dfd1f
MD5 230fd6dc1f088904d5f64cad8080dcb0
BLAKE2b-256 b4fcdfbfead9ce2b038cfcf01dd615b1a718a7624fe9f422d4ccb2e1dfae3f2e

See more details on using hashes here.

File details

Details for the file mapgraph-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: mapgraph-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.0 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for mapgraph-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6d21a98b8424bd6c905e7d3f4cbbfc2e3191adc88d14a64eaae6a8af41b5029d
MD5 049e98463410c3551f427d501df48ef0
BLAKE2b-256 cad23cb94730c2bbbab9e27ba5bf869584867a36613c3791678bed45577171fd

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