Skip to main content

用于根据任意用户定义的命名约定自动化和生成字符串的工具集。

项目描述

在线文档 (ReadTheDocs)

文件状态 https://badge.fury.io/py/nomenclate.svg https://circleci.com/gh/AndresMWeber/Nomenclate.svg?style=svg https://coveralls.io/repos/github/AndresMWeber/Nomenclate/badge.svg?branch=master https://img.shields.io/pypi/pyversions/nomenclate.svg <nav class="contents" id="contents" role="doc-toc">

内容

</nav>

1概要

Nomenclate 是一种创建持久对象的工具,可用于生成遵循您指定的命名约定的字符串。有一组当前的命名约定(格式字符串)可以按照某些创建规则进行替换或扩展。您可以根据需要添加任意令牌并注册您自己指定的令牌过滤。

在~/.nomenclate.yml中创建了一整套 YAML 定义的后缀/侧替换字符串。您可以在此处自定义配置。

1.1概念定义

令牌

:格式字符串的一个组成部分,它是一个有意义的符号/定义对,将被正则表达式的语法过滤。简化的表示可以是 token=value,其中令牌(在格式字符串中找到)将被解析为符合令牌语法/语法规则的值

格式化字符串

: 一个字符串,表示用任意分隔符分隔的一系列标记。

例如 - side_location_nameDecoratorVar_childtype_ purpose_type

注意:Nomenclate 自动支持对标记进行驼峰式大小写以将它们分隔为自然分隔符。

有关解析/组合的评论,请查看此处

1.2特点

  • 将具有任意语法/语法的命名约定应用于字符串标记的格式

  • 给定可扩展的特定于标记的语法规则类的格式字符串的自上而下解析

  • 持久状态对象实例

  • 最新的在线帮助文​​档

  • 用户可定制的 YAML/人类可读的配置文件

  • 简单的对象属性或字典状态操作

  • 跨 Python 兼容:测试并使用 Python 3.6、3.7 和 3.8

  • 跨平台兼容:适用于Linux、Mac OS、Windows环境

  • 完整的模块/类文档

  • 明智的令牌值输入/转换(如side='left'具有自动令牌语法替换)

2安装

窗户等

一种通用的安装方法(适用于 Windows、Mac OS X、Linux 等,并且始终提供最新版本)是使用pip

$ pip install Nomenclate
$ poetry add Nomenclate

(如果pip安装由于某种原因失败,您可以尝试easy_install nomenclate作为后备。)

3用法

3.1 Python包使用

通过包级功能使用此工具

import nomenclate
# Create empty name object
nomenclate_empty = nomenclate.Nom()

# At any time you can query the state of the nomenclate object through the state property
>>> nomenclate_empty.state
{'name': '', 'childtype': '', 'location': '', 'var': '', 'type': '', 'side': '', 'decorator': '', 'purpose': ''}

# You can also create a nomenclate with initialized kwargs
nomenclate_init_kwargs = nomenclate.Nom(name='test', type='group')

# Your Nomenclate object has now been initialized and all of the default token set have been added based on
# The default format_string property from the `~/.nomenclate.yml` config file
# default: side_location_nameDecoratorVar_childtype_purpose_type
>>> nomenclate_init_kwargs.state
{'name': 'test', 'childtype': '', 'location': '', 'var': '', 'type': 'group', 'side': '', 'decorator': '', 'purpose': ''}

# Feel free to manipulate each token's value on a property basis
>>> nomenclate_init_kwargs.location = 'rear'

# Now that you're all set up you can use the get method to obtain a string representation of your conventionalized output:
>>> nomenclate_init_kwargs.get()
'rr_test_GRP'

# As you'll notice both tokens group and location have been composed following the replacements that can be found in the config YAML file.  This way things like "left" just need to be entered as "left" and then based on the yaml will replace automatically with anything you want.  Finally you don't need to enter things like "L" and worry about it later on!

# The format string will automate the process of hot swapping naming formats allows any string to be input.
>>> nomenclate_init_kwargs.format
'side_location_nameDecoratorVar_childtype_purpose_type'
>>> nomenclate_init_kwargs.format = 'name_type'
>>> nomenclate_init_kwargs.state
{name:'test', type='group'}

# You can enter static text that will always be present in the name by surrounding with parenthesis
# For now they only support alphanumeric characters.

>>> nomenclate_init_kwargs.format = 'side_location_nameDecoratorVar_(static.text)childtype_purpose_type'
>>> nomenclate_init_kwargs.name = 'test'
>>> nomenclate_init_kwargs.location = 'rear'
>>> nomenclate_init_kwargs.type = 'group'
'rr_test_staticText_GRP'

# Now entering all these values by properties is fun and all, however there is a convenience function that can digest dictionaries
>>> test_nom = nomenclate.Nom()
>>> test_nom.merge_serialization({'name':'test', 'location':'rear', 'type':'group'})
>>> test_nom.get()

# As you might have guessed, using state and merge_serialization you can pass naming values from instance to instance (as you can see __eq__ has been defined for Nomenclate instances):
>>> nom_a = nomenclate.Nom(name='test', location='rear')
>>> nom_b = nomenclate.Nom()
>>> nom_b == nom_a
False
>>> nom_b.merge_serialization(nom_a.state)
>>> nom_b == nom_a
True

# Optionally you can just pass the nomenclate object itself
>>> nom_b.token_dict.reset() # Internal function to be made into a public method later...
>>> nom_b == nom_a
False
>>> nom_b.merge_serialization(nom_a)
>>> nom_b == nom_a
True

3.2 YAML 配置文件规则

到目前为止,后缀是 Maya 对象的查找字典,但我稍后会添加更多支持。

要正确输入命名格式字符串:

输入您要使用的所有标记,以及命名标记标签的描述性值,例如:

姓名

并将其按您设置的格式化字符串中的顺序放置在您想要的位置。如果您想要分隔名称或分隔名称,只需输入您想要使用的任何分隔符,例如_. 它将保留这些作为分隔符。 name_side_type

另外,如果您希望它们使用驼峰式大小写,例如名称和类型: side_nameType ,它将自动为您为给定令牌值输入的任何内容提供驼峰式大小写。

在配置 YAML 文件 ( ~/.nomenclate.yml ) 中,在标题Naming_formats下定义您的格式,并使用您认为合适的子部分名称(以下示例可选嵌套在“节点”下):

naming_formats:
    node:
        your_format: name_sidePurpose_type
如果您希望静态字符串始终出现在格式字符串中,只需用括号将其括起来(目前只接受字母数字字符),例如版本:

(v) 格式字符串中的版本: side_name_(v)version_(static_text_example)

例子:

如果版本为 3 并且您的版本填充配置设置为 2 将评估为: v02

进一步的版本/var/日期特定令牌说明:

有 3 个具有特定格式功能的命名标记,可为您提供自定义结果。您可以通过在例如 var1、var2 之后添加一个数字来指定多个字段以增加粒度

变量

这取决于配置中的 var 设置为上限或下限

a:根据字母表中的位置返回一个字符,如果你过去,它会从 aa -> az -> ba -> bz 等开始。

A:根据字母表中的位置返回一个字符,如果你过去,它会从 AA -> AZ -> BA -> BZ 等开始。

版本

将根据 version_padding 配置设置返回一个字符串编号

日期

将基于用户将输入的日期时间模块格式化字符串或默认为 YYYY-MM-DD 以字符串形式返回日期

请指定您想要覆盖默认行为的任何分隔符(或缺少)只需修改配置

可以在此处找到完整的选项列表: 日期时间文档

如果您需要任何自定义令牌值转换函数,您可以通过从nomenclate.core.rendering.RenderBase继承并像这样实现其渲染函数来指定它们:

import nomenclate

class RenderCustom(nomenclate.core.rendering.RenderBase):
    token = 'custom'
    def render(cls, value, token, nomenclate_object, **kwargs):
        """ Always prepend "meh"

        :param value: str, the un-parsed/formatted token value
        :param token: str, the name of the token in question
        :param nomenclate_object: nomenclate.Nom, the nomenclate instance (for checking attribute values/config settings)
        :return: str, the final syntax adhering token value
        """
        return 'meh' + value

否则,除非您在自定义渲染器中为特定命名标记指定选项列表,否则它只会将文本替换为您在命名对象上设置的命名标记。选项列表将用作命名令牌有效性的过滤器或用作 UI 的查找表,如果您在其后指定不同的长度。除非在“<naming_token>_length”下的overall_config部分中另有说明,否则它将使用列表中的第一个。如果之后没有缩写列表,则只需将其写为带有-的列表

4版本支持

目前这个包支持 Python 2.7、3.5 和 3.6

5归因

由 WPZOOM许可证 设置的 WPZOOM 开发者图标集- 由 David Ferreira 设计。
iconauth制作的图标来自 www.flaticon.com

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

nomenclate-2.6.5.tar.gz (35.9 kB 查看哈希

已上传 source

内置分布

nomenclate-2.6.5-py3-none-any.whl (35.8 kB 查看哈希

已上传 py3