Skip to main content

ConfigBase

一个简洁的 Python 配置数据管理框架,支持加密存储、属性描述符、嵌套配置段等功能。

安装

pip install conf_base

快速开始

from conf_base import ConfigMeta, Section, crypto

"""
测试使用示例
"""


class TestConfig(metaclass=ConfigMeta):
    # _crypto = crypto.PlainCrypto() # 默认使用
    # _crypto = crypto.XorCrypto()

    # 全局设置
    setting = Section('Setting')
    screen_size = setting.add(
        option='screen_size',
        default=3,
        validator=lambda x: x > 0  # 数据修改校验函数
    )
    full_screen = setting.add(option='full_screen', default=False)
    bgm_volume = setting.add(option='bgm_volume', default=1.0)
    sound_volume = setting.add(option='sound_volume', default=1.0)

    # 关卡设置
    level = Section('Level', 'Level2')

    complete_levels = level.add(
        option='中文',  # 配置项名称,在配置文件中显示
        default=[1, 2, 3],
        fix_type=False  # fix_type 默认为True 数据修改类型必须保持一致
    )

    play_selection_card = level.add(
        option='play_selection_card',
        default=['Peashooter', 'SunFlower']
    )


# 1.基础信息使用
print('section: ', TestConfig.screen_size.section)
print('option: ', TestConfig.screen_size.option)
print('value: ', TestConfig.screen_size.value)
print('default: ', TestConfig.screen_size.default)
print('-' * 20)

# 2.使用不同的配置项和数据修改
TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels.value = {1: 1}
print(TestConfig.complete_levels.value)

TestConfig.level.set_use_section('Level')
TestConfig.complete_levels.value = {2: 2}
print(TestConfig.complete_levels.value)

TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels.value = {3: 3}
print(TestConfig.complete_levels.value)

# 3.重置数据
TestConfig.complete_levels.reset()
print(TestConfig.complete_levels.value)


# 4.数据校验函数
# TestConfig.screen_size.value = -1
# ValueError: Setting.screen_size 配置项校验失败,值 -1 未通过 validator 校验
# TestConfig.screen_size.value = 'a'
# TypeError: Setting.screen_size 配置项期望类型 int 实际为 str, 否则请修改配置项 fix_type=False


# 5.不同的配置类存储到不同的配置文件
class TestConfig2(metaclass=ConfigMeta):
    _crypto = crypto.PlainCrypto('settings_2.json')  # 不同的类配置项使用不同的名称,防止文件名称重叠造成数据覆盖

    setting = Section('Setting')
    bgm_volume = setting.add(option='bgm_volume', default=1.0)

Metadata

Release files for conf-base 0.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 conf-base 0.0.4
File Size Uploaded
conf_base-0.0.4.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for conf-base 0.0.4
File Interpreter ABI Platform
conf_base-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / conf_base-0.0.4.tar.gz

Download URL conf_base-0.0.4.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a6bb5492bcce1c69bd33ffc1b5be44e17f79e9d6875e06e99f3f78493351663d
BLAKE2b-256 checksum
How to use checksums
76a8a4f5ecedba486f273da322904d44371baa852f73c91e5e0520b31a457c21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release files / conf_base-0.0.4-py3-none-any.whl

Download URL conf_base-0.0.4-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
173c96591f1c3c1288e292d098757ec5ba4f6917b936c78c5fa63d6820e95a69
BLAKE2b-256 checksum
How to use checksums
85451af4715b0bb94d67153e4dad4bd661ba9b956a098d6be9a688604668728f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release history Release notifications | RSS feed

0.0.5

2 release files

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

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