Routines to manipulate the tags on the keys of a dict.
Used by the processing objects.
1# Copyright (c) 2023 Valentin Goldite. All Rights Reserved. 2"""Routines to manipulate the tags on the keys of a dict. 3 4Used by the processing objects. 5""" 6import copy 7import re 8from typing import Any, Dict, List, Tuple 9 10 11def clean_tag(flat_key: str, tag_name: str) -> str: 12 """Clean a tag from a flat key. 13 14 It removes all occurrences of the tag with the exact name. 15 16 Parameters 17 ---------- 18 flat_key : str 19 The flat key to clean. 20 tag_name : str 21 The name of the tag to remove, with or without the '@' prefix. 22 23 Returns 24 ------- 25 flat_key : str 26 The cleaned flat key. 27 28 Examples 29 -------- 30 >>> clean_tag('abc@tag.def@tag_2.ghi@tag', 'tag') 31 abc.def@tag_2.ghi 32 33 .. note:: 34 `tag_name` is supposed to be the exact name of the tag. 35 """ 36 if tag_name[0] == "@": 37 tag_name = tag_name[1:] 38 # Replace "@tag@other_tag" by "@other_tag" 39 parts = flat_key.split(f"@{tag_name}@") 40 flat_key = "@".join(parts) 41 # Replace "@tag." by "." 42 parts = flat_key.split(f"@{tag_name}.") 43 flat_key = ".".join(parts) 44 # Remove "@tag" at the end of the string 45 if flat_key.endswith(f"@{tag_name}"): 46 flat_key = flat_key[: -len(f"@{tag_name}")] 47 return flat_key 48 49 50def clean_all_tags(flat_key: str) -> str: 51 """Clean all tags from a flat key. 52 53 Parameters 54 ---------- 55 flat_key : str 56 The flat key to clean. 57 58 Returns 59 ------- 60 flat_key : str 61 The cleaned flat key. 62 """ 63 list_keys = flat_key.split(".") 64 for i, key in enumerate(list_keys): 65 key = re.sub(r"@.*", "", key) 66 list_keys[i] = key 67 flat_key = ".".join(list_keys) 68 return flat_key 69 70 71def dict_clean_tags(flat_dict: Dict[str, Any]) -> Tuple[Dict[str, Any], List[str]]: 72 """Clean a dict from all tags and return the list of keys with tags. 73 74 Parameters 75 ---------- 76 flat_dict : Dict[str, Any] 77 The flat dict to clean. 78 79 Returns 80 ------- 81 clean_dict : Dict[str, Any] 82 The cleaned flat dict without tags in the keys. 83 tagged_keys : List[str] 84 The list of keys with tags that have been cleaned. 85 """ 86 items = list(flat_dict.items()) 87 clean_dict = copy.deepcopy(flat_dict) 88 tagged_keys = [] 89 for key, value in items: 90 if "@" in key: 91 del clean_dict[key] 92 clean_dict[clean_all_tags(key)] = value 93 tagged_keys.append(key) 94 return clean_dict, tagged_keys 95 96 97def is_tag_in(flat_key: str, tag_name: str, *, full_key: bool = False) -> bool: 98 """Check if a tag is in a flat key. 99 100 The tag name must be the exact name, with or without the "@". 101 It supports the case where there are other tags that are prefixes 102 or suffixes of the considered tag. 103 104 Parameters 105 ---------- 106 flat_key : str 107 The flat key to check. 108 tag_name : str 109 The name of the tag to check, with or without the '@' prefix. 110 full_key : bool, optional 111 If True, check for the full key. If False, check for the last part of 112 the flat key (after the last dot) that correspond to the parameter name. 113 By default, False. 114 115 Returns 116 ------- 117 bool 118 True if the tag is in the flat key, False otherwise. 119 """ 120 if tag_name[0] == "@": 121 tag_name = tag_name[1:] 122 if not full_key: 123 flat_key = flat_key.split(".")[-1] 124 is_in = ( 125 flat_key.endswith(f"@{tag_name}") 126 or f"@{tag_name}@" in flat_key 127 or f"@{tag_name}." in flat_key 128 ) 129 return is_in
API Documentation
def
clean_tag(flat_key: str, tag_name: str) -> str:
12def clean_tag(flat_key: str, tag_name: str) -> str: 13 """Clean a tag from a flat key. 14 15 It removes all occurrences of the tag with the exact name. 16 17 Parameters 18 ---------- 19 flat_key : str 20 The flat key to clean. 21 tag_name : str 22 The name of the tag to remove, with or without the '@' prefix. 23 24 Returns 25 ------- 26 flat_key : str 27 The cleaned flat key. 28 29 Examples 30 -------- 31 >>> clean_tag('abc@tag.def@tag_2.ghi@tag', 'tag') 32 abc.def@tag_2.ghi 33 34 .. note:: 35 `tag_name` is supposed to be the exact name of the tag. 36 """ 37 if tag_name[0] == "@": 38 tag_name = tag_name[1:] 39 # Replace "@tag@other_tag" by "@other_tag" 40 parts = flat_key.split(f"@{tag_name}@") 41 flat_key = "@".join(parts) 42 # Replace "@tag." by "." 43 parts = flat_key.split(f"@{tag_name}.") 44 flat_key = ".".join(parts) 45 # Remove "@tag" at the end of the string 46 if flat_key.endswith(f"@{tag_name}"): 47 flat_key = flat_key[: -len(f"@{tag_name}")] 48 return flat_key
Clean a tag from a flat key.
It removes all occurrences of the tag with the exact name.
Parameters
- flat_key (str): The flat key to clean.
- tag_name (str): The name of the tag to remove, with or without the '@' prefix.
Returns
- flat_key (str): The cleaned flat key.
Examples
>>> clean_tag('abc@tag.def@tag_2.ghi@tag', 'tag')
abc.def@tag_2.ghi
tag_name
is supposed to be the exact name of the tag.
def
is_tag_in(flat_key: str, tag_name: str, *, full_key: bool = False) -> bool:
98def is_tag_in(flat_key: str, tag_name: str, *, full_key: bool = False) -> bool: 99 """Check if a tag is in a flat key. 100 101 The tag name must be the exact name, with or without the "@". 102 It supports the case where there are other tags that are prefixes 103 or suffixes of the considered tag. 104 105 Parameters 106 ---------- 107 flat_key : str 108 The flat key to check. 109 tag_name : str 110 The name of the tag to check, with or without the '@' prefix. 111 full_key : bool, optional 112 If True, check for the full key. If False, check for the last part of 113 the flat key (after the last dot) that correspond to the parameter name. 114 By default, False. 115 116 Returns 117 ------- 118 bool 119 True if the tag is in the flat key, False otherwise. 120 """ 121 if tag_name[0] == "@": 122 tag_name = tag_name[1:] 123 if not full_key: 124 flat_key = flat_key.split(".")[-1] 125 is_in = ( 126 flat_key.endswith(f"@{tag_name}") 127 or f"@{tag_name}@" in flat_key 128 or f"@{tag_name}." in flat_key 129 ) 130 return is_in
Check if a tag is in a flat key.
The tag name must be the exact name, with or without the "@". It supports the case where there are other tags that are prefixes or suffixes of the considered tag.
Parameters
- flat_key (str): The flat key to check.
- tag_name (str): The name of the tag to check, with or without the '@' prefix.
- full_key (bool, optional): If True, check for the full key. If False, check for the last part of the flat key (after the last dot) that correspond to the parameter name. By default, False.
Returns
- bool: True if the tag is in the flat key, False otherwise.