Skip to content

Commit

Permalink
support "paths.with.123.inside"
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 732093550
  • Loading branch information
Qwlouse authored and The kauldron Authors committed Feb 28, 2025
1 parent afafe04 commit cb65a07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kauldron/kontext/path_grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tensor_slice_key.2: tensor_axis_key ","
!slice_key: [number] ":" [number]
| [number] ":" [number] ":" [number]

_identifier: STAR | DOUBLE_STAR | IDENTIFIER
_identifier: STAR | DOUBLE_STAR | IDENTIFIER | DEC_NUMBER

NONE: "None"
BOOLEAN: "True" | "False"
Expand Down
4 changes: 4 additions & 0 deletions kauldron/kontext/path_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def BOOLEAN(args: str) -> bool:
def STRING(args: str) -> str:
return ast.literal_eval(args)

@staticmethod
def DEC_NUMBER(args: str) -> Any:
return int(args)

@staticmethod
def tuple_key(args: list[Any]) -> tuple[Any, ...]:
return tuple(args)
Expand Down
10 changes: 10 additions & 0 deletions kauldron/kontext/path_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def test_parse():
]


def test_parse_integer_key():
parts = path_parser.parse_parts('a.123.b[10]')
assert parts == [
'a',
123,
'b',
10,
]


def test_parse_interms_example():
parts = path_parser.parse_parts('interms.model.__call__[0]')
assert parts == ['interms', 'model', '__call__', 0]

0 comments on commit cb65a07

Please sign in to comment.