Python fundamental type extension.
Project description
pytypex
Python fundamental type extension
Python 基础类型扩展
Implement types not provided by the standard library, such as static classes,
singleton classes, multiton classes, and atomic counters.
实现标准库未提供的类型,例如静态类、单例类、多例类、和原子计数器。
The preferred way to install typex is via pip.
安装 typex 的首选方法是通过 pip。
pip install typex
To upgrade logop to the latest version, use the following command.
要将 typex 升级到最新版本,请使用以下命令。
pip install --upgrade typex
Here are a few simple usage examples.
以下是一些简单的使用示例。
# Static class
from typex import Static
class MyStatic (Static):
my_static_var = 0
MyStatic() # raise TypeError
# Singleton class
from typex import Singleton
class MySingleton (Singleton):
def __init__(self):
print("MySingleton is initialized")
a = MySingleton() # MySingleton is initialized
b = MySingleton() # (Nothing)
c = MySingleton() # (Nothing)
print(a is b) # True
# Multiton class
from typex import Multiton
class MyMultiton (Multiton):
def __init__(self, value):
print(value)
a1 = MyMultiton(1) # 1
a2 = MyMultiton(2) # (Nothing)
b1 = MyMultiton.get_instance("new", 3) # 3
b2 = MyMultiton(4, instance_name="new") # (Nothing)
print(a1 is a2) # True
print(b1 is b2) # True
print(a1 is b1) # False
print(a1.instance_name) # default
print(b1.instance_name) # new
# For instances that have already executed the __init__ method,
# you do not need to pass value.
a3 = MyMultiton() # (Nothing)
print(a3.instance_name) # default
# Atomic counter
from typex import Atomic, AbsoluteAtomic, MultitonAtomic
a = AbsoluteAtomic()
for _ in range(4): print(a.value) # 0 1 2 3
b = AbsoluteAtomic()
for _ in range(4): print(b.count) # 4 5 6 7
c = AbsoluteAtomic()
for _ in range(4): print(c.value) # 8 9 10 11
d = Atomic()
for _ in range(4): print(d.count) # 12 13 14 15
# I'm too lazy to demonstrate, you just need to know that it has the
# characteristics of both Multiton and Atomic.
MultitonAtomic()
This project is licensed under the MIT License.
该项目使用 MIT 许可证授权。
pytypex Copyright (C) 2022 numlinka.
Project details
Release history Release notifications | RSS feed
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 Distribution
typex-0.2.1-py3-none-any.whl
(4.1 kB
view details)
File details
Details for the file typex-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: typex-0.2.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fdda70a87c4a49bd0f5710fa7437dd1a0da28837c678476692fc151f09fd16c |
|
MD5 | fb46895db8caee4d5d5b30dd3c08951c |
|
BLAKE2b-256 | a98fb48148d0e5688d78c542798e88e25715a51fdba11e349e58b0e93a4f2142 |