From a3eed28f425d6c7ac7c08bcea36007cd6135a9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20Emir=20=C3=96zkan?= Date: Mon, 10 Aug 2020 05:39:56 +0300 Subject: [PATCH] Reverted fetch_from, added fetch_equals for better documentation and simplicity. --- README.md | 15 ++++++++------- datalite/__init__.py | 15 ++++++++++++++- test/main_tests.py | 6 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3fcee99..92196a2 100644 --- a/README.md +++ b/README.md @@ -62,22 +62,23 @@ inserted onto the system. > :warning: **Limitation! Fetch can only fetch limited classes correctly**: int, float and str! Finally, you may wish to recreate objects from a table that already exist, for -this purpose we have the function `fetch_from(class_, value, field)` as well +this purpose we have the function `fetch_from(class_, obj_id)` as well as `is_fetchable(className, object_id)` former fetches a record from the -SQL database given a field and value. If only the value is provided, - field defaults to 'obj_id' which is unique for all objects, - whereas the latter checks if it is fetchable (most likely to check if it exists.) +SQL database given its unique object_id whereas the latter checks if it +is fetchable (most likely to check if it exists.) ```python >>> fetch_from(Student, 2) Student(student_id=10, student_name='Albert Einstein') ``` -We have three helper methods, `fetch_range(class_, range_)` and +We have four helper methods, `fetch_range(class_, range_)` and `fetch_all(class_)` are very similar: the former fetches the records fetchable from the object id range provided by the user, whereas the latter fetches all records. Both return a tuple of `class_` objects. -The last helper method, `fetch_if(class_, condition)` fetches all +The last two helper methods, `fetch_if(class_, condition)` fetches all the records of type `class_` that fit a certain condition. Here conditions -must be written is SQL syntax. Be careful about the string espacially. \ No newline at end of file +must be written is SQL syntax. For easier, only one conditional checks, there +is `fetch_equals(class_, field, value)` that checks the value of only one `field` +and returns the object whose `field` equals the provided `value`. \ No newline at end of file diff --git a/datalite/__init__.py b/datalite/__init__.py index fc72f89..d8fd205 100644 --- a/datalite/__init__.py +++ b/datalite/__init__.py @@ -171,7 +171,7 @@ def _get_table_cols(cur: sql.Cursor, table_name: str) -> List[str]: return [row_info[1] for row_info in cur.fetchall()][1:] -def fetch_from(class_: type, value: Any, field: str = 'obj_id') -> Any: +def fetch_equals(class_: type, field: str, value: Any, ) -> Any: """ Fetch a class_ type variable from its bound db. :param class_: Class to fetch. @@ -191,6 +191,19 @@ def fetch_from(class_: type, value: Any, field: str = 'obj_id') -> Any: return obj +def fetch_from(class_: type, obj_id: int) -> Any: + """ + Fetch a class_ type variable from its bound dv. + :param class_: Class to fetch from. + :param obj_id: Unique object id of the object. + :return: The fetched object. + """ + if not is_fetchable(class_, obj_id): + raise KeyError(f"An object with {obj_id} of type {class_.__name__} does not exist, or" + f"otherwise is unreachable.") + return fetch_equals(class_, 'obj_id', obj_id) + + def _convert_record_to_object(class_: type, record: Tuple[Any], field_names: List[str]) -> Any: """ Convert a given record fetched from an SQL instance to a Python Object of given class_. diff --git a/test/main_tests.py b/test/main_tests.py index 75c478b..97a0a32 100644 --- a/test/main_tests.py +++ b/test/main_tests.py @@ -1,5 +1,5 @@ import unittest -from datalite import datalite, fetch_if, fetch_all, fetch_range, fetch_from +from datalite import datalite, fetch_if, fetch_all, fetch_range, fetch_from, fetch_equals from sqlite3 import connect from dataclasses import dataclass, asdict from os import remove @@ -82,8 +82,8 @@ class DatabaseFetchCalls(unittest.TestCase): t_obj = fetch_from(FetchClass, self.objs[0].obj_id) self.assertEqual(self.objs[0], t_obj) - def testFetchFromDif(self): - t_obj = fetch_from(FetchClass, self.objs[0].str_, 'str_') + def testFetchEquals(self): + t_obj = fetch_equals(FetchClass, 'str_', self.objs[0].str_) self.assertEqual(self.objs[0], t_obj) def testFetchAll(self):