Skip to content

Commit 3500c3e

Browse files
committed
py_lua_helper: added checks for typed get operations, example updated, added get_bool method
1 parent 744321b commit 3500c3e

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

python_lua_helper/py_lua_helper.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ def get(self, key: str, default: str = None) -> str:
368368
def get_int(self, key: str, default: int = None) -> int:
369369
"""Get variable value as integer with defaults on type conversion error."""
370370
try:
371-
return int(self._variables.get(key, default))
371+
value_type = self.get_type(key)
372+
if value_type == "number":
373+
return int(self._variables.get(key, default))
374+
raise ValueError(f"Invalid value type: {value_type}")
372375
except ValueError:
373376
if default is not None:
374377
return int(default)
@@ -377,12 +380,27 @@ def get_int(self, key: str, default: int = None) -> int:
377380
def get_float(self, key: str, default: float = None) -> float:
378381
"""Get variable value as float with defaults on type conversion error."""
379382
try:
380-
return float(self._variables.get(key, default))
383+
value_type = self.get_type(key)
384+
if value_type == "number":
385+
return float(self._variables.get(key, default))
386+
raise ValueError(f"Invalid value type: {value_type}")
381387
except ValueError:
382388
if default is not None:
383389
return float(default)
384390
raise
385391

392+
def get_bool(self, key: str, default: bool = None) -> bool:
393+
"""Get variable value as bool with defaults on type conversion error."""
394+
try:
395+
value_type = self.get_type(key)
396+
if value_type == "boolean":
397+
return bool(self._variables.get(key, default))
398+
raise ValueError(f"Invalid value type: {value_type}")
399+
except ValueError:
400+
if default is not None:
401+
return bool(default)
402+
raise
403+
386404
def get_table_start(self, key: str) -> int:
387405
"""Get start indexed element index of table if variable is a table and indexed (keyless) elements present, 0 if no indexed elements present"""
388406
if key in self._metadata:

python_lua_helper/test_example.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@
136136
print(f"example.py: cfg['config.sub.mixed.key'] = {cfg.get('config.sub.mixed.key', 'NOT_FOUND')}")
137137

138138
# Show extra cmdline parameters passed to lua script as loader.args table and assigned to config.sub.loader_args
139+
print("example.py: === test passing extra params (or cmdline args) into lua config ===")
139140
print(f"example.py: config.sub.loader_args indices sequence: {cfg.get_table_seq('config.sub.loader_args')}")
140141
for i in cfg.get_table_seq('config.sub.loader_args'):
141142
print(f"example.py: cfg['config.sub.loader_args.{i}'] = {cfg.get(f'config.sub.loader_args.{i}', 'NOT_FOUND')}")
143+
144+
# Test typed get
145+
print("example.py: === test getting values with specific type from config.sub.types table ===")
146+
print(f"example.py: get bool value, no fallback: cfg['config.sub.types.b'] = {cfg.get_bool('config.sub.types.b')}")
147+
print(f"example.py: get missing bool value, fallback: cfg['config.sub.types.b1'] = {cfg.get_bool('config.sub.types.b1', False)}")
148+
print(f"example.py: get bool value from int, fallback: cfg['config.sub.types.i'] = {cfg.get_bool('config.sub.types.i', False)}")
149+
150+
print(f"example.py: get int value, no fallback: cfg['config.sub.types.i'] = {cfg.get_int('config.sub.types.i')}")
151+
print(f"example.py: get missing int value, fallback: cfg['config.sub.types.i1'] = {cfg.get_int('config.sub.types.i1', -1)}")
152+
print(f"example.py: get missing int value, fallback from float num: cfg['config.sub.types.i1'] = {cfg.get_int('config.sub.types.i1', -2.6)}")
153+
print(f"example.py: get int value from bool, fallback: cfg['config.sub.types.b'] = {cfg.get_int('config.sub.types.b', -1)}")
154+
155+
print(f"example.py: get float value, no fallback: cfg['config.sub.types.f'] = {cfg.get_float('config.sub.types.f')}")
156+
print(f"example.py: get missing float value, fallback: cfg['config.sub.types.f1'] = {cfg.get_float('config.sub.types.f1', -1.1)}")
157+
print(f"example.py: get float value from bool, fallback: cfg['config.sub.types.b'] = {cfg.get_float('config.sub.types.b', -1.1)}")

python_lua_helper/test_example/example.cfg.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ config =
2525
extra_2=loader.extra[2],
2626
loader_args=loader.args,
2727
mixed={ 1, "text", true, key="test_value" },
28-
empty_table={ }
28+
empty_table={ },
29+
types = {
30+
b=true,
31+
i=100,
32+
f=99.99,
33+
s="string value",
34+
}
2935
},
3036
paths=
3137
{

0 commit comments

Comments
 (0)