Comments (4)
Have a look at tests on how to define templates within functions using triple quotes:
ttp/test/pytest/test_anonymous_group.py
Line 28 in 8e67084
from ttp.
Probably you now invoking ttp parser object properly, this works for me using Python 3.7.7
:
import pprint
from ttp import ttp
data = """
interface Loopback0
description Fabric Node Router ID
ip address 192.2.101.70 255.255.255.255
ip pim sparse-mode
ip router isis
clns mtu 1400
end
interface Loopback0
description Fabric Node Router ID
ip address 192.2.101.71 255.255.255.255
ip pim sparse-mode
ip router isis
clns mtu 1400
end
"""
template = """{{ ignore("\s+") }}ip address {{ ip_address }} 255.255.255.255"""
parser = ttp(data, template)
parser.parse()
res = parser.result()
pprint.pprint(res)
# prints:
# [[[{'ip_address': '192.2.101.70'}, {'ip_address': '192.2.101.71'}]]]
from ttp.
I had tried the above method earlier and it works. However, lets say I create the template inside a python function, where it will automatically be indented, like so:
def parse(data):
template = """
{{ ignore("\s+") }}ip address {{ ip_address }} 255.255.255.255 <== indented whitespaces
"""
parser = ttp(data=data, template=template)
parser.parse()
print(parser.result())
This is adding extra whitespaces due to the indentation and it breaks the template parsing. For one line templates, I can declare them inline and escape this problem. But for bigger templates, it is challenges and makes readability of the template difficult.
from ttp.
Thank you, this works perfectly.
from ttp.
Related Issues (20)
- Hello everyone, how do I parse import_RT and export_RT in the following configuration file using TTP. HOT 1
- Question about parsing non hierarchical data HOT 4
- Config generation from TTP template HOT 1
- Group functions filter out tail match results HOT 2
- Incorrect behavior if text notstartswith '\n' and template with '_end_' indicates
- resuball functions will execution many times HOT 3
- Cannot parse identical keys in different groups HOT 2
- Record multiple variables from a single group? HOT 1
- matching irrelevant group HOT 1
- Using of group.to_int leads to errors HOT 3
- TTP KeyError while running macro HOT 1
- Handling a variable that conditionally appears in output to be parsed? HOT 2
- Add new method to TTP to emit template content HOT 1
- How to correlate/enforce previously matched attribute? HOT 2
- Incorrect "default" behavior in nested groups HOT 6
- Incorrect macro behavior in nested groups HOT 2
- Request: linenumber match function
- The asterisks at the end of the key_name are removed
- \1 in key_value used in group name path_string
- Not closing group match properly when matching lines appear in multiple places in config HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ttp.