Skip to content

Commit 84588ec

Browse files
committed
Add .api.decimal() encoder
Simply casts to `Decimal` then calls `item()` on it as normal for most basic types.
1 parent df93c33 commit 84588ec

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

tomlkit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from tomlkit.api import comment
66
from tomlkit.api import date
77
from tomlkit.api import datetime
8+
from tomlkit.api import decimal
89
from tomlkit.api import document
910
from tomlkit.api import dump
1011
from tomlkit.api import dumps
@@ -33,6 +34,7 @@
3334
"comment",
3435
"date",
3536
"datetime",
37+
"decimal",
3638
"document",
3739
"dump",
3840
"dumps",

tomlkit/api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime as _datetime
22

33
from collections.abc import Mapping
4+
from decimal import Decimal
45
from typing import IO
56
from typing import Iterable
67
from typing import Optional
@@ -96,6 +97,11 @@ def integer(raw: Union[str, int]) -> Integer:
9697
return item(int(raw))
9798

9899

100+
def decimal(raw: Union[str, Decimal, float]) -> String:
101+
"""Create an string item from a ``decimal.Decimal``."""
102+
return item(Decimal(raw))
103+
104+
99105
def float_(raw: Union[str, float]) -> Float:
100106
"""Create an float item from a number or string."""
101107
return item(float(raw))

0 commit comments

Comments
 (0)