配置文件支持micropython,其中配置结构仅在python中定义。

micropython-structured-config的Python项目详细描述


python的一个配置模块,其中config结构是 在python中定义,自动读写磁盘和字段 验证。在

此包提供的micropython端口 https://gitlab.com/alelec/structured_config支持json 存储文件。在

背景

有许多不同的模块和格式可供创建 python项目中的配置文件。在

我过去用过的所有方法都有一个主要的局限性 但是很常见;配置元素的主要定义是 不是用python编写的,就是用python之类的东西编写的 在你没有得到特别好的静态检查的地方 元素。在

我个人喜欢在像 在那里我可以充分利用检查和 自动完成。但是,如果您的配置不是用python编写的,那么我 别这么做。在

如果您想要对配置文件进行任何类型的自省,那么您最终将 一些配置文件的python解析器 元素在默认模板和 镜像类。本模块旨在消除这一限制。在

要求

structured_config依赖于micropython中包含的一些特性 v1.13所以你至少应该使用它。在

它还需要启用MICROPY_PY_DELATTR_SETATTRbuild设置。 默认情况下,这包含在stm32端口上,但对于其他端口,您将 需要启用此功能的自定义生成。在

基本用途

使用structured_config,项目中的config.py文件可以 像这样:

from structured_config import ConfigFile, Structure

class Config(Structure):

    class server(Structure):
        url = 'https:www.example.com'
        username = '<user>'
        password = '<password>'


    # Max number of tcp connections at any one time
    concurrent_connections =  32

    # Local service port
    service_port = 45080


config = Config('/path/to/config.json')

项目中的任何其他模块都可以

^{pr2}$

等等。你的IDE应该给你所有这些的完全自动完成功能 元素,因为它知道你的配置是一个普通类 常规静态属性。如果要在中更改这些配置项 编写代码,只需设置属性即可

from config import config

config.concurrent_connections = 64

config.__save__()

就这样。在中指向的json文件中将配置写入磁盘 Config()实例化

元素列表

如果需要一个稍微复杂一点的配置文件,其中包含元素列表, 这也可以处理

import structured_config
from structured_config import Structure, ConfigFile

# Pre-define the object we want to store a list of.
class Map(Structure):
    remote_path = None
    local_path = None


# Default configuration for the application
class Config(Structure):

    class server(Structure):
        url = 'https:www.example.com'
        username = '<user>'
        password = '<password>'

    mapping = [
        Map(
            remote_path="/test/",
            local_path="~/test/"
        ),
        Map(
            remote_path="/one/",
            local_path="~/two/"
        ),
        Map(
            remote_path="/two/",
            local_path="~/one/"
        )
    ]

config = Config('config.json')

你的主代码可以访问列表中所有的地图项 意味着。如果您将()个新的添加到列表中,或从列表中弹出()个旧的 列表中,配置会自动将它们写入磁盘。同样的道理 编辑具有 已添加到列表中。在

添加到列表中的元素类型也可以强制/验证, 例如

import structured_config
from structured_config import Structure, ConfigFile, List

# Pre-define the object we want to store a list of.
class Map(Structure):
    remote_path = None
    local_path = None


# Default configuration for the application
class Config(Structure):

    class server(Structure):
        url = 'https:www.example.com'
        username = '<user>'
        password = '<password>'

    mapping = List([
        Map(
            remote_path="/test/",
            local_path="~/test/"
        ),
        Map(
            remote_path="/one/",
            local_path="~/two/"
        ),
        Map(
            remote_path="/two/",
            local_path="~/one/"
        )
    ], type=Map)

config = Config('config.json')

现在创建新实例时,从文件加载,甚至 附加到列表后,将检查类型以确保它与 宣言。在

元素词典

上面的元素可以定义为Dict 可以控制dict中的值的类型

import structured_config
from structured_config import Structure, ConfigFile, Dict

# Pre-define the object we want to store a list of.
class Map(Structure):
    remote_path = None
    local_path = None

# Default configuration for the application
class Config(Structure):

    mapping = Dict({
        "test": Map(remote_path="/test/", local_path="~/test/"),
        }, type=Map)

config = Config('config.json')
config.mapping['new'] = Map(remote_path='c:\\new', local_path='~/new')

# Will raise an exception as the value is incorrect!
config.mapping['fails'] = "Nope"

格式转换和验证

如果你想强制一些属性的类型,我们已经得到了 还包括:

from structured_config import ConfigFile, Structure, TypedField, IntField

class config(Structure):

    concurrent_connections = IntField(32)

    path = TypedField('$HOME', os.path.expandvars)


config = Config('config.json')

可用类型字段的一些示例包括:*IntField:converts to int()*FloatField:转换为float()*StrField:转换为 str()*PathField:转换为路径库路径()

其他可以通过使用 TypeField(value, converter_funtion),或将TypeField子类化为 根据上面的那些。在

任何时候设置配置属性,它都将通过 先验证功能。原始(未转换)值将保存到 磁盘。在

config objects属性上的Get返回转换后的值,而不是 Field对象。在

现场文件

一旦使用Field()类型来定义元素,就可以 同时记录它们:

from structured_config import ConfigFile, Structure, TypedField, IntField

class config(Structure):

    concurrent_connections = IntField(32)            | "The number of connections allowed"
    path = TypedField('$HOME', os.path.expandvars)   | "User's home directory"


config = Config('config.json')

这些可以通过代码访问 Config.__fdoc__('concurrent_connections')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
反射Java反射:高负载下的NoSuchMethodException   java RxJava:one request>list of Integer>sequence of requests for each int>result to list   java为什么循环之前索引会增加   JavaSpring远程处理和RESTfulURL   java Hibernate搜索仅对我的实体的一部分进行索引   使用DPAD快速滚动时,java RecyclerView onCreateViewHolder调用过多   java将JSON解析到一个表中   java导航抽屉标题textview nullpointerexception   基于接口的Java链接队列   java Guice运行时依赖项参数重新注入   java展平/压缩ZSH中的深度嵌套目录   JavaSpring:Http406此请求标识的资源只能   java如何制作Android启动器图标   Java代码在windows上显示不正确(包含希腊语句子)   使用yourkit进行内存分析所用的java时间   java为什么可以序列化属性而不能序列化对象本身?