tomllib --- تجزیه پرونده‌های TOML

کد منبع: Lib/tomllib


This module provides an interface for parsing TOML 1.1.0 (Tom's Obvious Minimal Language, https://toml.io). This module does not support writing TOML.

اضافه شده در نسخه‌ی 3.11: The module was added with support for TOML 1.0.0.

تغییر یافته در نسخه‌ی 3.15: Added TOML 1.1.0 support. See the What's New for details.

هشدار

هنگام تجزیه داده از منابع غیرقابل‌اعتماد، احتیاط کنید. یک رشته TOML مخرب ممکن است باعث شود کدگشا مقدار قابل‌توجهی از منابع پردازنده و حافظه را مصرف کند. محدود کردن اندازه داده‌ای که تجزیه می‌شود، توصیه می‌شود.

همچنین ملاحظه نمائید

بسته‌ی Tomli-W یک ابزار نوشتن TOML است که می‌تواند همراه با این ماژول استفاده شود و یک API نوشتن ارائه می‌دهد که برای کاربران ماژول‌های marshal و pickle کتابخانه استاندارد آشناست.

همچنین ملاحظه نمائید

بسته TOML Kit یک کتابخانه TOML با حفظ سبک است که هم قابلیت خواندن و هم قابلیت نوشتن دارد. این بسته به‌عنوان جایگزین توصیه‌شده برای این ماژول جهت ویرایش پرونده‌های TOML از پیش موجود پیشنهاد می‌شود.

این ماژول توابع زیر را تعریف می‌کند:

tomllib.load(fp, /, *, parse_float=float)

یک پرونده TOML را می‌خواند. اولین آرگومان باید یک شیء پرونده دودویی و قابل خواندن باشد. یک dict برمی‌گرداند. انواع TOML را با استفاده از این جدول تبدیل به پایتون تبدیل می‌کند.

parse_float با رشته‌ی هر عدد اعشاری در TOML که باید کدگشایی شود فراخوانی می‌شود. به‌طور پیش‌فرض، این معادل float(num_str) است. این را می‌توان برای استفاده از یک نوع داده یا پارسرٔ دیگر برای اعداد اعشاری در TOML (برای مثال decimal.Decimal) به‌کار برد. این فراخوانی‌پذیر نباید یک dict یا یک list برگرداند، در غیر این صورت یک ValueError پرتاب می‌شود.

در صورت نامعتبر بودن سند TOML، یک TOMLDecodeError پرتاب خواهد شد.

tomllib.loads(s, /, *, parse_float=float)

TOML را از یک شیء str بارگذاری می‌کند. یک dict برمی‌گرداند. انواع TOML را با استفاده از این جدول تبدیل به پایتون تبدیل می‌کند. آرگومان parse_float همان معنایی را دارد که در load() دارد.

در صورت نامعتبر بودن سند TOML، یک TOMLDecodeError پرتاب خواهد شد.

استثناهای زیر در دسترس هستند:

exception tomllib.TOMLDecodeError(msg, doc, pos)

زیرکلاسی از ValueError با ویژگی‌های اضافی زیر:

msg

پیام خطای قالب‌بندی‌نشده.

doc

سند TOML که در حال تجزیه است.

pos

اندیس doc که در آن تجزیه شکست خورد.

lineno

سطر متناظر با pos.

colno

ستون متناظر با pos.

تغییر یافته در نسخه‌ی 3.14: پارامترهای msg، doc و pos افزوده شدند. ویژگی‌های msg، doc، pos، lineno و colno افزوده شدند.

منسوخ شده از نسخه‌ی 3.14: ارسال آرگومان‌های جایگاهی با قالب آزاد منسوخ شده است.

مثال‌ها

تجزیه یک پرونده TOML:

import tomllib

with open("pyproject.toml", "rb") as f:
    data = tomllib.load(f)

تجزیه‌ی یک رشته TOML:

import tomllib

toml_str = """
python-version = "3.11.0"
python-implementation = "CPython"
"""

data = tomllib.loads(toml_str)

جدول تبدیل

TOML

پایتون

سند TOML

dict

رشته

str

عدد صحیح

int

float

float (قابل پیکربندی با parse_float)

بولی

bool

تاریخ‌زمان آفست‌دار

datetime.datetime (ویژگی tzinfo به نمونه‌ای از datetime.timezone تنظیم شده است)

تاریخ‌زمان محلی

datetime.datetime (ویژگی tzinfo روی None تنظیم‌شده)

تاریخ محلی

datetime.date

زمان محلی

datetime.time

آرایه

فهرست

جدول

dict

جدول درون‌خطی

dict

آرایه‌ای از جدول‌ها

فهرستی از دیکشنری‌ها

Limits and interoperability considerations

tomllib places some limits on the documents it can handle, and it preserves details that other TOML parsers are allowed to ignore. When writing portable TOML files, only use features that are guaranteed or recommended by the standard.

The implementation details listed here may change in future versions of Python.

Tables/dicts

The TOML spec does not guarantee key/value pairs in TOML documents and tables to be in any specific order.

جزئیات پیاده‌سازی در CPython: tomllib loads dictionary entries in the order they appear in the source.

Integers

TOML recommends supporting integers in range(−2**63, 2**63).

جزئیات پیاده‌سازی در CPython: tomllib uses Python's limit on integer string conversion (4300 digits by default).

Floats

TOML recommends supporting at least IEEE 754 binary64 values, which means that numbers with more than 15 significant decimal digits are likely to be rounded.

جزئیات پیاده‌سازی در CPython: tomllib uses Python float by default; on many common platforms this is the recommended binary64. See sys.float_info for details.

Nesting limit

TOML 1.1.0 does not recommend a limit on how deeply arrays and tables may be nested inside one another. (A limit of 100 has been proposed for a future version of TOML.)

جزئیات پیاده‌سازی در CPython: In tomllib, the nesting level is mainly limited by Python's recursion limit. Note that code that calls tomllib may contribute to the limit.