feat: add simplified methods for get_primitive_object
All checks were successful
Deploy Sphinx Docs / build-and-deploy (push) Successful in 2m15s
All checks were successful
Deploy Sphinx Docs / build-and-deploy (push) Successful in 2m15s
This commit is contained in:
@@ -47,3 +47,12 @@ Type Converter
|
||||
|
||||
.. autoclass:: ConvertError
|
||||
.. autofunction:: get_primitive_object
|
||||
|
||||
.. note::
|
||||
|
||||
Methods :data:`get_str_object`, :data:`get_int_object`, :data:`get_float_object` and :data:`get_bool_object` are simplified method for :data:`get_primitive_object`, they return ``get_primitive_object(val, <type>)`` and confirm the return type is specified type.
|
||||
|
||||
.. autofunction:: get_str_object
|
||||
.. autofunction:: get_int_object
|
||||
.. autofunction:: get_float_object
|
||||
.. autofunction:: get_bool_object
|
||||
|
||||
@@ -51,21 +51,13 @@ source_suffix = [".rst"]
|
||||
templates_path = ["_templates"]
|
||||
exclude_patterns = ["build", "Thumbs.db", ".DS_Store"]
|
||||
|
||||
# def setup(app: 'Sphinx'):
|
||||
# from typing import List
|
||||
# from sphinx_prompt import PromptDirective
|
||||
|
||||
# class PromptWithVersion(PromptDirective):
|
||||
# content: List[str]
|
||||
note_i18n_text = "Translation links only works in official documentation website, if you're testing locally, they may could not be reached."
|
||||
|
||||
# def run(self):
|
||||
# self.assert_has_content()
|
||||
# for i in range(len(self.content)):
|
||||
# self.content[i] = self.content[i].replace('@@VERSION@@', release)
|
||||
# return super().run()
|
||||
rst_prolog = f"""
|
||||
.. |NOTE_I18N| replace:: {note_i18n_text}
|
||||
"""
|
||||
|
||||
# app.add_directive('prompt-version', PromptWithVersion)
|
||||
# autodoc_setup(app)
|
||||
|
||||
# -- Options for HTML output -------------------------------------------------
|
||||
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
primitive-type documentation
|
||||
============================
|
||||
|
||||
`English <index.html>`__ | `中文(简体) <zh_CN/index.html>`__
|
||||
|
||||
.. note::
|
||||
|
||||
|NOTE_I18N|
|
||||
|
||||
Here is the documentation of primitive-type.
|
||||
|
||||
primitive-type is a Python library, you can check a value or object if the type of it is primitive, or convert it to other primitive type.
|
||||
|
||||
@@ -9,7 +9,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: primitive-type \n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-02-10 11:37+0800\n"
|
||||
"POT-Creation-Date: 2026-02-12 13:00+0800\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language: zh_CN\n"
|
||||
@@ -54,7 +54,9 @@ msgstr "原始值的类型别名。"
|
||||
msgid ""
|
||||
"Including: :class:`str`, :class:`int`, :class:`float`, :class:`bool`, and"
|
||||
" :obj:`None`."
|
||||
msgstr "包括::class:`str`, :class:`int`, :class:`float`, :class:`bool`, and :obj:`None`."
|
||||
msgstr ""
|
||||
"包括::class:`str`, :class:`int`, :class:`float`, :class:`bool`, and "
|
||||
":obj:`None`."
|
||||
|
||||
#: of primitive_type.types.PrimitiveMap:1
|
||||
msgid "Type alias for a mapping of string keys to primitive values."
|
||||
@@ -146,7 +148,11 @@ msgstr "一个应在类型转换失败时被抛出的错误。"
|
||||
msgid "Get a primitive object from a given value."
|
||||
msgstr "从给定的一个值中获取一个原始对象。"
|
||||
|
||||
#: of primitive_type.converter.get_primitive_object:3
|
||||
#: of primitive_type.converter.get_bool_object:3
|
||||
#: primitive_type.converter.get_float_object:3
|
||||
#: primitive_type.converter.get_int_object:3
|
||||
#: primitive_type.converter.get_primitive_object:3
|
||||
#: primitive_type.converter.get_str_object:3
|
||||
msgid "The given value wants to be converted."
|
||||
msgstr "需要被转换(类型)的给定对象。"
|
||||
|
||||
@@ -163,7 +169,10 @@ msgid ""
|
||||
":class:`float` -- The origin object or convert to. * :class:`bool` -- The"
|
||||
" origin object or convert to. * :obj:`None` -- Only when given value is "
|
||||
":obj:`None`."
|
||||
msgstr "* :class:`str` -- 如果给定的值是一个普通字符串,或者指定了转换类型。 * :class:`int` -- 原始的或转换出的对象。 * :class:`float` -- 原始的或转换出的对象。 * :class:`bool` -- 原始的或转换出的对象。 * :obj:`None` -- 仅在给定的对象为 :obj:`None` 时。"
|
||||
msgstr ""
|
||||
"* :class:`str` -- 如果给定的值是一个普通字符串,或者指定了转换类型。 * :class:`int` -- 原始的或转换出的对象。"
|
||||
" * :class:`float` -- 原始的或转换出的对象。 * :class:`bool` -- 原始的或转换出的对象。 * "
|
||||
":obj:`None` -- 仅在给定的对象为 :obj:`None` 时。"
|
||||
|
||||
#: of primitive_type.converter.get_primitive_object:7
|
||||
msgid ""
|
||||
@@ -187,6 +196,47 @@ msgstr ":class:`bool` -- 原始的或转换出的对象。"
|
||||
msgid ":obj:`None` -- Only when given value is :obj:`None`."
|
||||
msgstr ":obj:`None` -- 仅在给定的对象为 :obj:`None` 时。"
|
||||
|
||||
#: ../../source/api.rst:53
|
||||
msgid ""
|
||||
"Methods :data:`get_str_object`, :data:`get_int_object`, "
|
||||
":data:`get_float_object` and :data:`get_bool_object` are simplified "
|
||||
"method for :data:`get_primitive_object`, they return "
|
||||
"``get_primitive_object(val, <type>)`` and confirm the return type is "
|
||||
"specified type."
|
||||
msgstr "方法 :data:`get_str_object`、:data:`get_int_object`、:data:`get_float_object` 和 :data:`get_bool_object` 是 :data:`get_primitive_object` 的简化方法,他们会返回 ``get_primitive_object(val, <type>)`` 并确认返回类型是指定的类型。"
|
||||
|
||||
#: of primitive_type.converter.get_str_object:1
|
||||
msgid "Get a string object from a given value."
|
||||
msgstr "从给定的一个值中获取一个原始对象。"
|
||||
|
||||
#: of primitive_type.converter.get_str_object:5
|
||||
msgid "A string object."
|
||||
msgstr "一个字符串对象。"
|
||||
|
||||
#: of primitive_type.converter.get_int_object:1
|
||||
msgid "Get an integer object from a given value."
|
||||
msgstr "从给定的一个值中获取一个整数对象。"
|
||||
|
||||
#: of primitive_type.converter.get_int_object:5
|
||||
msgid "An integer object."
|
||||
msgstr "一个整数对象。"
|
||||
|
||||
#: of primitive_type.converter.get_float_object:1
|
||||
msgid "Get a float object from a given value."
|
||||
msgstr "从给定的一个值中获取一个浮点数对象。"
|
||||
|
||||
#: of primitive_type.converter.get_float_object:5
|
||||
msgid "A float object."
|
||||
msgstr "一个浮点数对象。"
|
||||
|
||||
#: of primitive_type.converter.get_bool_object:1
|
||||
msgid "Get a boolean object from a given value."
|
||||
msgstr "从给定的一个值中获取一个布尔值对象。"
|
||||
|
||||
#: of primitive_type.converter.get_bool_object:5
|
||||
msgid "A bool object."
|
||||
msgstr "一个布尔值对象。"
|
||||
|
||||
#~ msgid "Entrypoint of this package."
|
||||
#~ msgstr ""
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: primitive-type \n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-02-10 12:30+0800\n"
|
||||
"POT-Creation-Date: 2026-02-12 13:29+0800\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language: zh_CN\n"
|
||||
@@ -20,15 +20,15 @@ msgstr ""
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"Generated-By: Babel 2.18.0\n"
|
||||
|
||||
#: ../../source/index.rst:16
|
||||
#: ../../source/index.rst:22
|
||||
msgid "Code References"
|
||||
msgstr "代码参考"
|
||||
|
||||
#: ../../source/index.rst:16
|
||||
#: ../../source/index.rst:22
|
||||
msgid "Develop"
|
||||
msgstr "开发"
|
||||
|
||||
#: ../../source/index.rst:16
|
||||
#: ../../source/index.rst:22
|
||||
msgid "Home"
|
||||
msgstr "主页"
|
||||
|
||||
@@ -37,32 +37,40 @@ msgid "primitive-type documentation"
|
||||
msgstr "primitive-type 文档"
|
||||
|
||||
#: ../../source/index.rst:9
|
||||
msgid "`English <index.rst>`__ | `中文(简体) <zh_CN/index.html>`__"
|
||||
msgstr ""
|
||||
|
||||
#: ../../source/index.rst:13
|
||||
msgid "|NOTE_I18N|"
|
||||
msgstr "翻译链接仅在官方文档网站有效;若在本地测试,可能无法访问。"
|
||||
|
||||
#: ../../source/index.rst:15
|
||||
msgid "Here is the documentation of primitive-type."
|
||||
msgstr "这里是 primitive-type 的文档。"
|
||||
|
||||
#: ../../source/index.rst:11
|
||||
#: ../../source/index.rst:17
|
||||
msgid ""
|
||||
"primitive-type is a Python library, you can check a value or object if "
|
||||
"the type of it is primitive, or convert it to other primitive type."
|
||||
msgstr "primitive-type 是一个 Python 库,你可以用来检查一个值或者对象是不是原始类型,或将其转换为其他原始类型。"
|
||||
|
||||
#: ../../source/index.rst:13
|
||||
#: ../../source/index.rst:19
|
||||
msgid "See the `Code References <api.html>`__ for more details."
|
||||
msgstr "查看 `代码参考 <api.html>`__ 以获取更多详情。"
|
||||
|
||||
#: ../../source/index.rst:24
|
||||
#: ../../source/index.rst:30
|
||||
msgid "Indices and tables"
|
||||
msgstr "目录和索引"
|
||||
|
||||
#: ../../source/index.rst:26
|
||||
#: ../../source/index.rst:32
|
||||
msgid ":ref:`genindex`"
|
||||
msgstr ""
|
||||
|
||||
#: ../../source/index.rst:27
|
||||
#: ../../source/index.rst:33
|
||||
msgid ":ref:`modindex`"
|
||||
msgstr ""
|
||||
|
||||
#: ../../source/index.rst:28
|
||||
#: ../../source/index.rst:34
|
||||
msgid ":ref:`search`"
|
||||
msgstr ""
|
||||
|
||||
@@ -77,3 +85,9 @@ msgstr ""
|
||||
#~ "documentation for details."
|
||||
#~ msgstr ""
|
||||
|
||||
#~ msgid "`English <index.rst>`__ | `中文(简体) <zh_CN/index.html>`__ @@NOTE_I18N@@"
|
||||
#~ msgstr ""
|
||||
|
||||
#~ msgid "@@NOTE_I18N@@"
|
||||
#~ msgstr ""
|
||||
|
||||
|
||||
@@ -13,7 +13,9 @@ class ConvertError(TypeError):
|
||||
|
||||
|
||||
@beartype
|
||||
def get_primitive_object(val: Primitive, obj_type: type[Primitive] = None) -> Primitive:
|
||||
def get_primitive_object(
|
||||
val: Primitive, obj_type: type[Primitive] = None
|
||||
) -> Primitive:
|
||||
"""Get a primitive object from a given value.
|
||||
|
||||
:param val: The given value wants to be converted.
|
||||
@@ -30,7 +32,9 @@ def get_primitive_object(val: Primitive, obj_type: type[Primitive] = None) -> Pr
|
||||
|
||||
if val is None:
|
||||
if obj_type_required:
|
||||
raise TypeError("Given value is None, could not convert to other type!")
|
||||
raise TypeError(
|
||||
"Given value is None, could not convert to other type!"
|
||||
)
|
||||
return None
|
||||
|
||||
if not obj_type_required:
|
||||
@@ -72,3 +76,43 @@ def get_primitive_object(val: Primitive, obj_type: type[Primitive] = None) -> Pr
|
||||
raise ConvertError(f"Could not convert '{val}' to type '{obj_type}'.")
|
||||
|
||||
return val
|
||||
|
||||
|
||||
def get_str_object(val: Primitive) -> str:
|
||||
"""Get a string object from a given value.
|
||||
|
||||
:param val: The given value wants to be converted.
|
||||
|
||||
:return: A string object.
|
||||
"""
|
||||
return get_primitive_object(val, str) # ty: ignore[invalid-return-type]
|
||||
|
||||
|
||||
def get_int_object(val: Primitive) -> int:
|
||||
"""Get an integer object from a given value.
|
||||
|
||||
:param val: The given value wants to be converted.
|
||||
|
||||
:return: An integer object.
|
||||
"""
|
||||
return get_primitive_object(val, int) # ty: ignore[invalid-return-type]
|
||||
|
||||
|
||||
def get_float_object(val: Primitive) -> float:
|
||||
"""Get a float object from a given value.
|
||||
|
||||
:param val: The given value wants to be converted.
|
||||
|
||||
:return: A float object.
|
||||
"""
|
||||
return get_primitive_object(val, float) # ty: ignore[invalid-return-type]
|
||||
|
||||
|
||||
def get_bool_object(val: Primitive) -> bool:
|
||||
"""Get a boolean object from a given value.
|
||||
|
||||
:param val: The given value wants to be converted.
|
||||
|
||||
:return: A bool object.
|
||||
"""
|
||||
return get_primitive_object(val, bool) # ty: ignore[invalid-return-type]
|
||||
|
||||
Reference in New Issue
Block a user