Skip to content

Commit 6e27301

Browse files
authored
Merge pull request #7 from ODM2/develop
Release 0.0.3
2 parents 5430379 + d2e3f2d commit 6e27301

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

src/odm2datamodels/base.py

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def from_dict(cls, attributes_dict:Dict) -> object:
5353
setattr(instance, key, value)
5454
return instance
5555

56-
def to_dict(self) -> Dict[str,Any]:
56+
def to_dict(self) -> Dict[str, Any]:
5757
"""Converts attributes into a dictionary"""
5858
columns = self.__table__.columns.keys()
5959
output_dict = {}
@@ -83,8 +83,31 @@ def __init__(self, session_maker:sqlalchemy.orm.sessionmaker) -> None:
8383

8484
def read_query(self,
8585
query: Union[Query, Select],
86-
output_format:str='json',
87-
orient:str='records') -> Union[str, pd.DataFrame]:
86+
output_format:str='dict',
87+
orient:str='records') -> Union[str, pd.DataFrame, Dict[Any, Any]]:
88+
"""
89+
Executes valid SQLAlchemy query and returns the result
90+
91+
Arguments:
92+
query:Query|Select - valid sqlalchemy.orm.Query or sqlalchemy.select instance
93+
94+
Optionals:
95+
output_format:str - string corresponding to desired return datatype:
96+
'json' - json string
97+
'dataframe' - pandas.DataFrame object
98+
'dict' (default) - python Dictionary object
99+
orient:str - string corresponding to desired output formatting.
100+
'dict' (default) : dict like {column -> {index -> value}}
101+
'list' : dict like {column -> [values]}
102+
'series' : dict like {column -> Series(values)}
103+
'split' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values]}
104+
'tight' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values], 'index_names' -> [index.names], 'column_names' -> [column.names]}
105+
'records' : list like [{column -> value}, … , {column -> value}]
106+
'index' : dict like {index -> {column -> value}}
107+
See pandas orient documentation on 'to_dict' and 'to_json' for additional information
108+
Returns:
109+
str|pandas.DataFrame|Dict based on specified output format
110+
"""
88111

89112
# guard against invalid output_format strings
90113
if output_format not in OUTPUT_FORMATS:
@@ -102,10 +125,20 @@ def read_query(self,
102125
elif output_format == 'dataframe':
103126
return df
104127
elif output_format == 'dict':
105-
return df.to_dict()
128+
return df.to_dict(orient=orient)
106129
raise TypeError("Unknown output format")
107130

108131
def insert_query(self, objs:List[object]) -> None:
132+
"""
133+
Inserts list of ODM2DataModels into the database
134+
135+
Arguments:
136+
objs:List[ODM2DataModel] - list of ODM2DataModels objects to be inserted into the database
137+
138+
Returns:
139+
None
140+
"""
141+
109142
with self.session_maker() as session:
110143
session.add_all(objs)
111144
session.commit()
@@ -128,7 +161,7 @@ def create_object(self, obj:object, preserve_pkey:bool=False) -> Union[int, str]
128161
129162
"""
130163

131-
if not perserve_pkey:
164+
if not preserve_pkey:
132165
pkey_name = obj.get_pkey_name()
133166
setattr(obj, pkey_name, None)
134167

@@ -141,7 +174,34 @@ def create_object(self, obj:object, preserve_pkey:bool=False) -> Union[int, str]
141174
def read_object(self, model:Type[Base], pkey:Union[int, str],
142175
output_format:str='dict',
143176
orient:str='records') -> Dict[str, Any]:
177+
"""
178+
Executes select query to read the requested DataModel object from the database
179+
180+
Arguments:
181+
models:ODM2DataModel - The class of the ODM2DataModel to be read from the database
182+
pkey:int|str - the primary key value of the object
183+
184+
Optionals:
185+
output_format:str - string corresponding to desired return datatype:
186+
'json' - json string
187+
'dataframe' - pandas.DataFrame object
188+
'dict' (default) - python Dictionary object
189+
orient:str - string corresponding to desired output formatting.
190+
'dict' (default) : dict like {column -> {index -> value}}
191+
'list' : dict like {column -> [values]}
192+
'series' : dict like {column -> Series(values)}
193+
'split' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values]}
194+
'tight' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values], 'index_names' -> [index.names], 'column_names' -> [column.names]}
195+
'records' : list like [{column -> value}, … , {column -> value}]
196+
'index' : dict like {index -> {column -> value}}
197+
See pandas orient documentation on 'to_dict' and 'to_json' for additional information
198+
199+
Returns:
200+
str|pandas.DataFrame|Dict based on specified output format
201+
"""
144202

203+
#TODO - this method is more complicated than it needs to be
204+
#Simplify the last if else statement
145205
with self.session_maker() as session:
146206
obj = session.get(model, pkey)
147207
pkey_name = model.get_pkey_name()
@@ -175,6 +235,19 @@ def read_object(self, model:Type[Base], pkey:Union[int, str],
175235

176236

177237
def update_object(self, model:Type[Base], pkey:Union[int,str], data:Dict[str, Any]) -> None:
238+
"""
239+
Updates data record in the database with provided dictionary of updates attributes
240+
241+
Arguments:
242+
models:ODM2DataModel - The class of the ODM2DataModel to be read from the database
243+
pkey:int|str - the primary key value of the object
244+
data:Dict[str, Any] - dictionary of updated attributes with keys corresponding to
245+
attribute name and values corresponding to new value.
246+
247+
Returns:
248+
None
249+
"""
250+
178251
if not isinstance(data, dict):
179252
data = data.dict()
180253
pkey_name = model.get_pkey_name()
@@ -187,6 +260,16 @@ def update_object(self, model:Type[Base], pkey:Union[int,str], data:Dict[str, An
187260
session.commit()
188261

189262
def delete_object(self, model:Type[Base], pkey:Union[int, str]) -> None:
263+
"""
264+
Deletes the object from the database
265+
266+
Arguments:
267+
models:ODM2DataModel - The class of the ODM2DataModel to be read from the database
268+
pkey:int|str - the primary key value of the object
269+
270+
Returns:
271+
None
272+
"""
190273
with self.session_maker() as session:
191274
obj = session.get(model, pkey)
192275
pkey_name = model.get_pkey_name()

0 commit comments

Comments
 (0)