validity icon indicating copy to clipboard operation
validity copied to clipboard

Bug: Unable to use imports inside Name Sets

Open amyasnikov opened this issue 1 year ago • 0 comments

Discussed in https://github.com/amyasnikov/validity/discussions/128

Originally posted by aascedu October 7, 2024 Hello Anton, hello everyone,

I am trying to use name sets to help with complience tests. I want to use the re module.

import re

def find_deny_rule(device, target):
    normalized_target = re.sub(r'\s+', ' ', target).strip()
    
    for rule in device.config["access-lists"]["98"]["rules"]:
        if "source" in rule and "mode" in rule:
            normalized_rule = re.sub(r'\s+', ' ', rule).strip()
            if normalized_rule == normalized_target:
                return True
    return False

__all__ = ['find_deny_rule', 're']

Is not allowed in the GUI since the import re is not a from-import. However how do I import the module re then ? I tried this then :

from re import sub

def find_deny_rule(device, target):
    normalized_target = sub(r'\s+', ' ', target).strip()
    
    for rule in device.config["access-lists"]["98"]["rules"]:
        if "source" in rule and "mode" in rule:
            normalized_rule = sub(r'\s+', ' ', rule).strip()
            if normalized_rule == normalized_target:
                return True
    return False

__all__ = ['find_deny_rule', 'sub']

And I get this error : NameError: name 'sub' is not defined

amyasnikov avatar Oct 07 '24 21:10 amyasnikov