Skip to content

pid_record_entry

PIDRecordEntry

Bases: dict

Represents a PID record entry. For more information on the PID record format, see the documentation of the Typed PID Maker (https://kit-data-manager.github.io/webpage/typed-pid-maker/openapi.html). A PID record entry consists of a key, a value, and optionally a name.

Attributes: key:str The key of the entry value:str The value of the entry name:str The name of the entry (optional)

Source code in src/nmr_FAIR_DOs/domain/pid_record_entry.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class PIDRecordEntry(dict):
    """
    Represents a PID record entry.
    For more information on the PID record format, see the documentation of the Typed PID Maker (https://kit-data-manager.github.io/webpage/typed-pid-maker/openapi.html).
    A PID record entry consists of a key, a value, and optionally a name.

    Attributes:
    key:str The key of the entry
    value:str The value of the entry
    name:str The name of the entry (optional)
    """

    key: str = None
    value: str | dict = None
    name: str = None

    def __init__(self, key: str, value: str | dict, name: str = None):
        """
        Creates a PID record entry

        Args:
            key:str The key of the entry
            value:str The value of the entry
            name:str The name of the entry (optional)

        Raises:
            ValueError: If the key is None or the value is None
        """

        super().__init__()  # initialize the dictionary
        if key is None:  # if key is None, raise an error
            raise ValueError(f"Key must not be None: {self.__repr__()}")

        if value is None:  # if value is None, raise an error
            raise ValueError(f"Value must not be None: {self.__repr__()}")
        elif not isinstance(value, str) and not isinstance(
            value, dict
        ):  # if value is not a string or a dictionary, log a warning
            logger.warning(
                f"Value SHOULD be a string or a dictionary: {key}({name}), {value}"
            )

        try:
            if isinstance(value, str):  # if value is a JSON string, parse it
                self.value = json.loads(value)
            elif isinstance(value, dict):  # if value is a dictionary, use it as is
                self.value = value
            else:  # if the value is neither; parse as string
                self.value = str(value)
        except Exception as e:  # if parsing fails, log a warning
            logger.debug(f"Value is not a JSON string: {value}, {e}")
            self.value = value  # if value is not a JSON string, use it as is

        self.key = key
        self.name = name

    def __getitem__(self, item):
        """
        This method is called to get the value of the given key.
        This enables the use of the dictionary syntax to access the key, value, and name of the PID record entry.
        """
        if item == "key":
            return self.key
        elif item == "value":
            return self.value
        elif item == "name":
            return self.name
        else:
            return None

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        val = (
            json.dumps(self.value) if isinstance(self.value, dict) else self.value
        )  # if the value is a dictionary, convert it to a JSON string
        return json.dumps(
            {"key": self.key, "value": val, "name": self.name}
        )  # return the key, value, and name as a JSON string

    def toJSON(self):
        """
        Exports the PID record entry as JSON

        Returns:
        dict: The PID record entry as JSON
        """
        val = json.dumps(self.value) if isinstance(self.value, dict) else self.value

        if self.name is None:  # if the name is None, return only the key and value
            return {"key": self.key, "value": val}
        else:  # if the name is not None, return the key, value, and name
            return {"key": self.key, "value": val, "name": self.name}

    def __dict__(self):
        val = json.dumps(self.value) if isinstance(self.value, dict) else self.value
        return {"key": self.key, "value": val, "name": self.name}

__init__

__init__(key: str, value: str | dict, name: str = None)

Creates a PID record entry

Parameters:

Name Type Description Default
key str

str The key of the entry

required
value str | dict

str The value of the entry

required
name str

str The name of the entry (optional)

None

Raises:

Type Description
ValueError

If the key is None or the value is None

Source code in src/nmr_FAIR_DOs/domain/pid_record_entry.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(self, key: str, value: str | dict, name: str = None):
    """
    Creates a PID record entry

    Args:
        key:str The key of the entry
        value:str The value of the entry
        name:str The name of the entry (optional)

    Raises:
        ValueError: If the key is None or the value is None
    """

    super().__init__()  # initialize the dictionary
    if key is None:  # if key is None, raise an error
        raise ValueError(f"Key must not be None: {self.__repr__()}")

    if value is None:  # if value is None, raise an error
        raise ValueError(f"Value must not be None: {self.__repr__()}")
    elif not isinstance(value, str) and not isinstance(
        value, dict
    ):  # if value is not a string or a dictionary, log a warning
        logger.warning(
            f"Value SHOULD be a string or a dictionary: {key}({name}), {value}"
        )

    try:
        if isinstance(value, str):  # if value is a JSON string, parse it
            self.value = json.loads(value)
        elif isinstance(value, dict):  # if value is a dictionary, use it as is
            self.value = value
        else:  # if the value is neither; parse as string
            self.value = str(value)
    except Exception as e:  # if parsing fails, log a warning
        logger.debug(f"Value is not a JSON string: {value}, {e}")
        self.value = value  # if value is not a JSON string, use it as is

    self.key = key
    self.name = name

__getitem__

__getitem__(item)

This method is called to get the value of the given key. This enables the use of the dictionary syntax to access the key, value, and name of the PID record entry.

Source code in src/nmr_FAIR_DOs/domain/pid_record_entry.py
83
84
85
86
87
88
89
90
91
92
93
94
95
def __getitem__(self, item):
    """
    This method is called to get the value of the given key.
    This enables the use of the dictionary syntax to access the key, value, and name of the PID record entry.
    """
    if item == "key":
        return self.key
    elif item == "value":
        return self.value
    elif item == "name":
        return self.name
    else:
        return None

toJSON

toJSON()

Exports the PID record entry as JSON

Returns: dict: The PID record entry as JSON

Source code in src/nmr_FAIR_DOs/domain/pid_record_entry.py
108
109
110
111
112
113
114
115
116
117
118
119
120
def toJSON(self):
    """
    Exports the PID record entry as JSON

    Returns:
    dict: The PID record entry as JSON
    """
    val = json.dumps(self.value) if isinstance(self.value, dict) else self.value

    if self.name is None:  # if the name is None, return only the key and value
        return {"key": self.key, "value": val}
    else:  # if the name is not None, return the key, value, and name
        return {"key": self.key, "value": val, "name": self.name}