Skip to content

Commit

Permalink
Updating hints on ModelIndex functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Lydersen committed Sep 6, 2022
1 parent aa5c2e2 commit 7038c49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Changelog
=========

Version 0.1
Version 0.0.5
===========

- First edition
41 changes: 32 additions & 9 deletions src/pyprediktormapclient/model_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ def __init__(self, url: str):
self.object_types = self.get_object_types(return_format="json")

def as_dataframe(self, content) -> pd.DataFrame:
"""Function to convert a json string to Pandas DataFrame
Args:
content (str): the json string
"""
if content is None:
return None
return pd.DataFrame(content)

def request(self, method: str, endpoint: str, data=None) -> json:
def request(self, method: str, endpoint: str, data=None) -> str:
"""Function to perform the request to the ModelIndex server
Args:
method (str): "GET" or "POST"
endpoint (str): The last part of the url (without the leading "/")
data (str): defaults to None but can contain the data to tsend to the endpoint
Returns:
JSON: The result if successfull
"""
if method == 'GET':
result = requests.get(self.url + endpoint)
elif method == 'POST':
Expand All @@ -34,20 +48,26 @@ def request(self, method: str, endpoint: str, data=None) -> json:
else:
return None

def get_namespace_array(self, return_format="dataframe") -> json:
def get_namespace_array(self, return_format="dataframe") -> str:
content = self.request('GET', 'query/namespace-array')
if return_format == "dataframe":
return self.as_dataframe(content)
return content

def get_object_types(self, return_format="dataframe") -> json:
def get_object_types(self, return_format="dataframe") -> str:
content = self.request('GET', 'query/object-types')
if return_format == "dataframe":
return self.as_dataframe(content)
return content

def get_object_type_id_from_name(self, type_name: str) -> str:
"""Function to get object type id from type name
Args:
type_name (str): type name
Returns:
str: the type id that corresponds with the id
"""
try:
obj_type = next(item for item in self.object_types if item["BrowseName"] == type_name)
Expand All @@ -56,11 +76,14 @@ def get_object_type_id_from_name(self, type_name: str) -> str:
object_type_id = obj_type.get("Id")
return object_type_id

def get_objects_of_type(self, type_name: str, return_format="dataframe"):
def get_objects_of_type(self, type_name: str, return_format="dataframe") -> str:
"""Function to get all the types of an object
Args:
type_name (str): type name
type_name (str): type name
Returns:
pd.DataFrame or JSON: a Dataframe or JSON with the objects
"""
object_type_id = self.get_object_type_id_from_name(type_name)
body = json.dumps({"typeId": object_type_id})
Expand All @@ -69,7 +92,7 @@ def get_objects_of_type(self, type_name: str, return_format="dataframe"):
return self.as_dataframe(content)
return content

def get_object_descendants(self, type_name: str, obj_dataframe: pd.DataFrame, domain: str, return_format="dataframe") -> json:
def get_object_descendants(self, type_name: str, obj_dataframe: pd.DataFrame, domain: str, return_format="dataframe") -> str:
"""A function to get object descendants
Args:
Expand All @@ -78,7 +101,7 @@ def get_object_descendants(self, type_name: str, obj_dataframe: pd.DataFrame, do
domain (str): PV_Assets or PV_Serves
Returns:
pd.DataFrame: descendats data of selected object
pd.DataFrame or JSON: descendats data of selected object
"""
object_type_id = self.get_object_type_id_from_name(type_name)
id_column = [x for x in obj_dataframe if x in ['Id','DescendantId', 'AncestorId']][0]
Expand All @@ -93,7 +116,7 @@ def get_object_descendants(self, type_name: str, obj_dataframe: pd.DataFrame, do
return self.as_dataframe(content)
return content

def get_object_ancestors(self, type_name: str, obj_dataframe: pd.DataFrame, domain: str, return_format="dataframe") -> json:
def get_object_ancestors(self, type_name: str, obj_dataframe: pd.DataFrame, domain: str, return_format="dataframe") -> str:
"""Function to get object ancestors
Args:
Expand All @@ -102,7 +125,7 @@ def get_object_ancestors(self, type_name: str, obj_dataframe: pd.DataFrame, doma
domain (str): Either PV_Assets or PV_Serves
Returns:
pd.DataFrame: ancestors data of selected object
pd.DataFrame or JSON: ancestors data of selected object
"""
object_type_id = self.get_object_type_id_from_name(type_name)
id_column = [x for x in obj_dataframe if x in ['Id','AncestorId', 'DescendantId']][0]
Expand Down

0 comments on commit 7038c49

Please sign in to comment.