openspg/python/knext/operator/spg_record.py

137 lines
4.0 KiB
Python
Raw Normal View History

2023-10-26 10:34:08 +08:00
# -*- coding: utf-8 -*-
# Copyright 2023 Ant Group CO., Ltd.
2023-10-26 10:34:08 +08:00
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
2023-10-26 10:34:08 +08:00
#
# http://www.apache.org/licenses/LICENSE-2.0
2023-10-26 10:34:08 +08:00
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.
2023-10-26 10:34:08 +08:00
import pprint
from typing import Dict, Any, List
2023-12-06 17:26:39 +08:00
class SPGRecord:
2023-11-21 15:17:02 +08:00
"""Data structure in operator, used to store entity information."""
2023-10-26 10:34:08 +08:00
2023-12-11 15:34:02 +08:00
def __init__(self, spg_type_name: str = "", properties: Dict[str, str] = None):
2023-12-11 11:32:45 +08:00
self._spg_type_name = None
2023-10-26 10:34:08 +08:00
self._properties = None
2023-12-11 11:32:45 +08:00
self._spg_type_name = spg_type_name
2023-10-26 10:34:08 +08:00
if properties is None:
properties = {}
self._properties = properties
@property
2023-12-11 11:32:45 +08:00
def spg_type_name(self) -> str:
"""Gets the spg_type_name of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
2023-12-11 11:32:45 +08:00
:return: The spg_type_name of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:rtype: str
"""
2023-12-11 11:32:45 +08:00
return self._spg_type_name
2023-10-26 10:34:08 +08:00
2023-12-11 11:32:45 +08:00
@spg_type_name.setter
def spg_type_name(self, spg_type_name: str):
"""Sets the spg_type_name of this SPGRecord.
2023-10-26 10:34:08 +08:00
2023-12-11 11:32:45 +08:00
:param spg_type_name: The spg_type_name of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:type: str
"""
2023-12-11 11:32:45 +08:00
self._spg_type_name = spg_type_name
2023-10-26 10:34:08 +08:00
@property
def properties(self) -> Dict[str, str]:
2023-12-11 11:32:45 +08:00
"""Gets the properties of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
2023-12-11 11:32:45 +08:00
:return: The properties of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:rtype: dict
"""
return self._properties
@properties.setter
def properties(self, properties: Dict[str, str]):
2023-12-11 11:32:45 +08:00
"""Sets the properties of this SPGRecord.
2023-10-26 10:34:08 +08:00
2023-12-11 11:32:45 +08:00
:param properties: The properties of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:type: dict
"""
self._properties = properties
def get_property(self, name: str, default_value: str = None) -> str:
2023-12-11 11:32:45 +08:00
"""Gets a property of this SPGRecord by name. # noqa: E501
2023-10-26 10:34:08 +08:00
:param name: The property name. # noqa: E501
:param default_value: If property value is None, the default_value will be return. # noqa: E501
:return: A property value. # noqa: E501
:rtype: str
"""
return self.properties.get(name, default_value)
def update_property(self, name: str, value: str):
2023-12-11 11:32:45 +08:00
"""Updates a property of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:param name: The updated property name. # noqa: E501
:param value: The updated property value. # noqa: E501
:type: str
"""
self.properties[name] = value
def update_properties(self, properties: Dict[str, str]):
2023-12-11 11:32:45 +08:00
"""Updates properties of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:param properties: The updated properties. # noqa: E501
:type: dict
"""
self.properties.update(properties)
def remove_property(self, name: str):
2023-12-11 11:32:45 +08:00
"""Removes a property of this SPGRecord. # noqa: E501
2023-10-26 10:34:08 +08:00
:param name: The property name. # noqa: E501
:type: str
"""
self.properties.pop(name)
def remove_properties(self, names: List[str]):
"""Removes properties by given names. # noqa: E501
:param names: A list of property names. # noqa: E501
:type: list
"""
for name in names:
self.properties.pop(name)
def to_str(self):
"""Returns the string representation of the model"""
return pprint.pformat(self.to_dict())
def to_dict(self):
"""Returns the model properties as a dict"""
return {
2023-12-11 11:32:45 +08:00
"SPGTypeName": self.spg_type_name,
"properties": self.properties,
2023-10-26 10:34:08 +08:00
}
2023-12-06 17:26:39 +08:00
@classmethod
def from_dict(cls, input: Dict[str, Any]):
2023-10-26 10:34:08 +08:00
"""Returns the model from a dict"""
2023-12-11 11:32:45 +08:00
return cls(input.get("SPGTypeName"), input.get("properties"))
2023-10-26 10:34:08 +08:00
def __repr__(self):
"""For `print` and `pprint`"""
return pprint.pformat(self.to_dict())