Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using of group.to_int leads to errors #109

Open
amyasnikov opened this issue Jun 11, 2023 · 3 comments
Open

Using of group.to_int leads to errors #109

amyasnikov opened this issue Jun 11, 2023 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@amyasnikov
Copy link

Python version: 3.10.12
TTP version: 0.9.4

Example of the error:

import ttp

template = '''
<group name="interfaces" to_int="mask">
interface {{name}}
 ip address {{address}}/{{mask}}
</group>
'''

data = '''
interface Ethernet0/1
 ip address 1.1.1.1/24
interface Ethernet0/2
 no ip address
'''

parser = ttp.ttp(template=template, data=data)
parser.parse()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 449, in parse
    self.__parse_in_one_process()
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 518, in __parse_in_one_process
    parserObj.parse(groups_indexes=input_obj.groups_indexes)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2628, in parse
    RSLTSOBJ.make_results(self.vars, raw_results, main_results=self.main_results)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2838, in make_results
    if self.processgrp() is not False:
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 3251, in processgrp
    self.record["result"], flags = self._ttp_["group"][func_name](
  File "/usr/local/lib/python3.10/site-packages/ttp/group/to_converters.py", line 19, in to_int
    v = data[k]
KeyError: 'mask'

Expected behavior: TTP ignores the absence of "mask" key for Ethernet0/2 and outputs the result without an error

@amyasnikov
Copy link
Author

And another one example

import ttp

template = '''
<group name="interfaces" to_int="trunk_vlan">
interface {{ name }}
 switchport trunk allowed vlan {{ trunk_vlan | split(',') }}
</group>
'''

data = '''
interface Ethernet0/1
 switchport trunk allowed vlan 5,10,15
'''

parser = ttp.ttp(template=template, data=data)
parser.parse()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 449, in parse
    self.__parse_in_one_process()
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 518, in __parse_in_one_process
    parserObj.parse(groups_indexes=input_obj.groups_indexes)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2628, in parse
    RSLTSOBJ.make_results(self.vars, raw_results, main_results=self.main_results)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2838, in make_results
    if self.processgrp() is not False:
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 3251, in processgrp
    self.record["result"], flags = self._ttp_["group"][func_name](
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 69, in __call__
    return self._ttp_[self.parent_dir][self.function_name](*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/ttp/group/to_converters.py", line 21, in to_int
    data[k] = int(v)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

Maybe it's a misuse of to_int, but it will be very useful to get a list of integers from a list of strings via this clause.

@dmulyalin dmulyalin added bug Something isn't working enhancement New feature or request labels Jun 11, 2023
dmulyalin added a commit that referenced this issue Jun 25, 2023
@dmulyalin
Copy link
Owner

dmulyalin commented Jun 25, 2023

Hello, thank you for raising this issue, both requests should be addressed in 0.9.5, feel free to test and let me know how it goes.

@dmulyalin
Copy link
Owner

Updated documentation with an example of how to use intlist variable to convert list of string into list of integers - https://ttp.readthedocs.io/en/latest/Groups/Functions.html#to-int

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants