Created
May 19, 2025 15:32
-
-
Save sebastianknopf/2b32af8d24672489a4952f60b1623ed9 to your computer and use it in GitHub Desktop.
Utility functions to work with objectified LXML data implemented in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def exists(obj, path): | |
path = path.split('.') | |
level0 = path[0] | |
level1 = '.'.join(path[1:]) | |
return obj is not None and hasattr(obj, level0) if len(path) == 1 else exists(getattr(obj, level0), level1) if hasattr(obj, level0) else False | |
def get_elements(obj, path): | |
if exists(obj, path): | |
path = path.split('.') | |
destination = obj | |
for element in path: | |
destination = getattr(destination, element) | |
return destination | |
return list() | |
def get_value(obj, path, default=None): | |
if exists(obj, path): | |
path = path.split('.') | |
destination = obj | |
for element in path: | |
destination = getattr(destination, element) | |
if hasattr(destination, 'text'): | |
return destination.text | |
else: | |
return default | |
return default | |
def get_attribute(obj, path, default=None): | |
regex = re.compile('\.(?![^{]*})') | |
objectpath = regex.split(path) | |
objectpath = '.'.join(objectpath[:-1]) | |
if exists(obj, objectpath): | |
path = regex.split(path) | |
destination = obj | |
for element in path[:-1]: | |
destination = getattr(destination, element) | |
if hasattr(destination, 'attrib') and path[-1] in destination.attrib: | |
return destination.attrib[(path[-1])] | |
else: | |
return default | |
return default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment