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[1] = 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)
    play_selection_card = setting.add(
        option='play_selection_card',
        default=['Peashooter', 'SunFlower']
    )

Metadata

Release files for conf-base 0.0.5

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.5
File Size Uploaded
conf_base-0.0.5.tar.gz 9.5 kB Details

Built distribution (wheel)

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

Total release size: 19.6 kB

Release files / conf_base-0.0.5.tar.gz

Download URL conf_base-0.0.5.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fef4725861c4c7bdd981b1aa9d0686a4dbb06ee4678c65d9f9e7480ac0b7786a
BLAKE2b-256 checksum
How to use checksums
d54bdb8005da75bd18d6f9895aef2c096847fc0874b184f097da3d8ad7f33436
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.5-py3-none-any.whl

Download URL conf_base-0.0.5-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5179cb70c2b80c8c9ea7e5a34d598b9eb90929d96d86b11fec11d52d0d0fb81
BLAKE2b-256 checksum
How to use checksums
d14bb2d6223affc6ed4a8568c713d5793625bfa2ea792f02e566ab9a5c2e84c3
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

This release

0.0.5 This release

2 release files

0.0.4

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