Skip to content

Commit 9d313e5

Browse files
committed
feat: using "num2words and fixing command = output
1 parent 6eda4dc commit 9d313e5

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"importlib-metadata>=4.0",
3131
"pygithub>=1.59.1",
3232
"lxml>=4.9.3",
33+
"num2words>=0.5.0",
3334
"numpy>=1.14.0,<1.25.0; python_version<'3.9'",
3435
"numpy>=1.14.0; python_version>='3.9'",
3536
"py-asciimath==0.3.0",
@@ -46,6 +47,7 @@ tests = [
4647
"click==8.1.7",
4748
"pygithub==2.4.0",
4849
"lxml==5.3.0",
50+
"num2words==0.5.13",
4951
"numpy==2.1.2",
5052
"parse==1.20.2",
5153
"pytest==8.3.2",
@@ -60,6 +62,7 @@ doc = [
6062
"build>= 0.10.0",
6163
"flit>=3.8.0",
6264
"jupyter_sphinx==0.5.3",
65+
"num2words==0.5.13",
6366
"numpy==2.1.2",
6467
"numpydoc==1.8.0",
6568
"pandas==2.2.3",

src/pyconverter/xml2py/ast_tree.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import logging
2424
import textwrap
25+
from num2words import num2words as num
2526
from typing import List
2627
import warnings
2728

@@ -51,34 +52,8 @@
5152
'``"``': "``",
5253
}
5354

54-
word_digit = [
55-
"zero",
56-
"one",
57-
"two",
58-
"three",
59-
"four",
60-
"five",
61-
"six",
62-
"seven",
63-
"eight",
64-
"nine",
65-
]
66-
6755
superlatif = ["st", "nd", "rd", "th"]
6856

69-
superlatif_digit = [
70-
"",
71-
"first",
72-
"second",
73-
"third",
74-
"fourth",
75-
"fifth",
76-
"sixth",
77-
"seventh",
78-
"eighth",
79-
"ninth",
80-
]
81-
8257

8358
CLEANUP = {
8459
",, ": ", ",
@@ -167,9 +142,10 @@ def to_py_arg_name(name: str) -> str:
167142
if arg[1].isdigit():
168143
raise ValueError(f"The code needs to be expanded to handle numbers")
169144
elif arg[1:3] not in superlatif:
170-
arg = f"{word_digit[int(arg[0])]}{arg[1:]}"
145+
arg = f"{num(arg[0])}{arg[1:]}"
171146
else:
172-
arg = f"{superlatif_digit[int(arg[0])]}{arg[3:]}"
147+
arg = f"{num(arg[0], to="ordinal")}{arg[3:]}"
148+
print(arg)
173149

174150
if ("," in arg and "--" in arg) or arg == "–":
175151
return ""
@@ -2189,6 +2165,11 @@ def __init__(
21892165
self._description = description
21902166
self._initial_argument = initial_argument
21912167

2168+
@property
2169+
def py_arg_name(self) -> str:
2170+
"""Python-compatible term."""
2171+
return to_py_arg_name(self._name)
2172+
21922173
@property
21932174
def is_arg_elipsis(self):
21942175
"""
@@ -2343,11 +2324,6 @@ def str_types(self, join_str: str) -> str:
23432324
ptype_str = join_str.join([parm_type.__name__ for parm_type in self.types])
23442325
return ptype_str
23452326

2346-
@property
2347-
def py_arg_name(self) -> str:
2348-
"""Python-compatible term."""
2349-
return to_py_arg_name(self._name)
2350-
23512327
def resized_description(
23522328
self, description: str | None = None, max_length: int = 100, indent: str = ""
23532329
) -> List[str]:
@@ -2877,8 +2853,14 @@ def py_source(self, custom_functions=None, indent=""):
28772853
"""
28782854
if custom_functions is None or self.py_name not in custom_functions.py_names:
28792855

2880-
if len(self.py_args) > 0:
2881-
command = 'command = f"' + self.name + ",{" + "},{".join(self.py_args) + '}"\n'
2856+
if len(self.arg_desc) > 0:
2857+
command = 'command = f"' + self.name
2858+
for arg in self.arg_desc:
2859+
command += ',{'
2860+
command += arg.py_arg_name
2861+
command += '}'
2862+
command += '"\n'
2863+
# ",{" + "},{".join(self.arg_desc.py_arg_name) + '}"\n'
28822864
else:
28832865
command = 'command = f"' + self.name + '"\n'
28842866
return_command = "return self.run(command, **kwargs)\n"

0 commit comments

Comments
 (0)