From 8effd5614f9c2d777ca7be1480c4d24700bd1424 Mon Sep 17 00:00:00 2001 From: Tunde Akinyanmi Date: Fri, 13 Oct 2023 18:14:11 +0100 Subject: [PATCH 1/8] remove cast operation from str to float. It cause loss of the bookmark data --- lms/lms/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/lms/utils.py b/lms/lms/utils.py index f7dbe489..a0065dea 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -150,7 +150,7 @@ def get_lesson_details(chapter): ], as_dict=True, ) - lesson_details.number = flt(f"{chapter.idx}.{row.idx}") + lesson_details.number = f"{chapter.idx}.{row.idx}" lesson_details.icon = get_lesson_icon(lesson_details.body) lessons.append(lesson_details) return lessons From a70290921644ad4711cdec84feda10e09cc0e37f Mon Sep 17 00:00:00 2001 From: Tunde Akinyanmi Date: Fri, 13 Oct 2023 18:32:15 +0100 Subject: [PATCH 2/8] add `LessonBookmark` which is a data structure that represents a bookmark. While the underlying data structure is a tuple, it makes it easy to abstract most of the logic we need and therefore allow the code to be more readable. --- lms/www/batch/learn.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index c77233b7..72499273 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -105,6 +105,57 @@ def get_page_extensions(context): e.set_context(context) return extensions +class LessonBookmark: + """ + This represents a simple data structure to represent a lesson bookmark. + """ + def __init__(self, lesson_number: str) -> None: + self.__test_param_or_raise_exception(lesson_number) + _lesson_number = f"{lesson_number}." if not "." in lesson_number else lesson_number + first, second = _lesson_number.split(".") + + self.__value = (int(first), int(second) or 0) # second would be "" if `lesson_number` is something like "7" + + def __eq__(self, other: object) -> bool: + if isinstance(other, self.__class__): + return self.__value == other.value + return NotImplemented + + def __gt__(self, other: object) -> bool: + if isinstance(other, self.__class__): + return self.__value > other.value + return False + + def __repr__(self) -> str: + return f"{self.__value[0]}.{self.__value[1]}" + + def __test_param_or_raise_exception(self, string: str) -> None: + """ + Tests that a given string is in the format "n" or "n.n" where n is a numeric + character. If the string does not match the expected format, `TypeError` is + raised. + """ + import re + import sys + expected_format = r'^\d+\.?\d*$' + + try: + if not re.match(expected_format, string): + raise TypeError("""Expected a 'str' in the format 'n' or 'n.n' where n + is a numeric character. Example: '7' or '7.10""") + except TypeError as e: + tb = sys.exc_info()[2] + raise TypeError("""Expected a 'str' in the format 'n' or 'n.n' where n + is a numeric character. Example: '7' or '7.10""").with_traceback(tb) + + @property + def value(self): + return self.__value + + @property + def readable_value(self): + return self.__repr__() + def get_neighbours(current, lessons): current = flt(current) From 038a7463e1f870d0dddbb3ea4687e5963bf8e0f5 Mon Sep 17 00:00:00 2001 From: Tunde Akinyanmi Date: Fri, 13 Oct 2023 18:39:11 +0100 Subject: [PATCH 3/8] update `get_neighbours` to use `LessonBookmark` and return the correct bookmark string --- lms/www/batch/learn.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index 72499273..a1030a28 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -158,10 +158,10 @@ class LessonBookmark: def get_neighbours(current, lessons): - current = flt(current) - numbers = sorted(lesson.number for lesson in lessons) - index = numbers.index(current) + _current = LessonBookmark(current) + numbers = sorted([LessonBookmark(lesson.number) for lesson in lessons]) + index = numbers.index(_current) return { - "prev": numbers[index - 1] if index - 1 >= 0 else None, - "next": numbers[index + 1] if index + 1 < len(numbers) else None, + "prev": numbers[index - 1].readable_value if index - 1 >= 0 else None, + "next": numbers[index + 1].readable_value if index + 1 < len(numbers) else None, } From 7b3f4c29d8e55c682dc9c1a07089decc5b5b1862 Mon Sep 17 00:00:00 2001 From: Tunde Akinyanmi Date: Fri, 27 Oct 2023 12:00:44 +0100 Subject: [PATCH 4/8] remove LessonBookmark abstraction. --- lms/www/batch/learn.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index a1030a28..ef1668ef 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -158,10 +158,13 @@ class LessonBookmark: def get_neighbours(current, lessons): - _current = LessonBookmark(current) - numbers = sorted([LessonBookmark(lesson.number) for lesson in lessons]) - index = numbers.index(_current) + numbers = [lesson.number for lesson in lessons] + tuples_list = [tuple(map(int, s.split('.'))) for s in numbers] + sorted_tuples = sorted(tuples_list) + sorted_numbers = ['.'.join(map(str, t)) for t in sorted_tuples] + index = sorted_numbers.index(current) + return { - "prev": numbers[index - 1].readable_value if index - 1 >= 0 else None, - "next": numbers[index + 1].readable_value if index + 1 < len(numbers) else None, + "prev": sorted_numbers[index - 1] if index - 1 >= 0 else None, + "next": sorted_numbers[index + 1] if index + 1 < len(sorted_numbers) else None, } From d67faa161036b836bbb5cbb9eb4b65ddba83e2f7 Mon Sep 17 00:00:00 2001 From: Tunde Akinyanmi Date: Fri, 27 Oct 2023 12:05:37 +0100 Subject: [PATCH 5/8] forgot to remove the `LessonBookmark` class --- lms/www/batch/learn.py | 51 ------------------------------------------ 1 file changed, 51 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index ef1668ef..cafeb5be 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -105,57 +105,6 @@ def get_page_extensions(context): e.set_context(context) return extensions -class LessonBookmark: - """ - This represents a simple data structure to represent a lesson bookmark. - """ - def __init__(self, lesson_number: str) -> None: - self.__test_param_or_raise_exception(lesson_number) - _lesson_number = f"{lesson_number}." if not "." in lesson_number else lesson_number - first, second = _lesson_number.split(".") - - self.__value = (int(first), int(second) or 0) # second would be "" if `lesson_number` is something like "7" - - def __eq__(self, other: object) -> bool: - if isinstance(other, self.__class__): - return self.__value == other.value - return NotImplemented - - def __gt__(self, other: object) -> bool: - if isinstance(other, self.__class__): - return self.__value > other.value - return False - - def __repr__(self) -> str: - return f"{self.__value[0]}.{self.__value[1]}" - - def __test_param_or_raise_exception(self, string: str) -> None: - """ - Tests that a given string is in the format "n" or "n.n" where n is a numeric - character. If the string does not match the expected format, `TypeError` is - raised. - """ - import re - import sys - expected_format = r'^\d+\.?\d*$' - - try: - if not re.match(expected_format, string): - raise TypeError("""Expected a 'str' in the format 'n' or 'n.n' where n - is a numeric character. Example: '7' or '7.10""") - except TypeError as e: - tb = sys.exc_info()[2] - raise TypeError("""Expected a 'str' in the format 'n' or 'n.n' where n - is a numeric character. Example: '7' or '7.10""").with_traceback(tb) - - @property - def value(self): - return self.__value - - @property - def readable_value(self): - return self.__repr__() - def get_neighbours(current, lessons): numbers = [lesson.number for lesson in lessons] From b4af82acbc68df5bce4501c21ef22d9f0ad8c6fd Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 27 Oct 2023 17:21:28 +0530 Subject: [PATCH 6/8] chore: fix linters --- lms/www/batch/learn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index cafeb5be..0e233aac 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -108,9 +108,9 @@ def get_page_extensions(context): def get_neighbours(current, lessons): numbers = [lesson.number for lesson in lessons] - tuples_list = [tuple(map(int, s.split('.'))) for s in numbers] + tuples_list = [tuple(map(int, s.split("."))) for s in numbers] sorted_tuples = sorted(tuples_list) - sorted_numbers = ['.'.join(map(str, t)) for t in sorted_tuples] + sorted_numbers = [".".join(map(str, t)) for t in sorted_tuples] index = sorted_numbers.index(current) return { From 1af547288cc79d5acdaca9e1ab41546d6cf4f73f Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 27 Oct 2023 17:53:35 +0530 Subject: [PATCH 7/8] chore: fix linters --- lms/www/batch/learn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index 0e233aac..a3b9566c 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -108,9 +108,9 @@ def get_page_extensions(context): def get_neighbours(current, lessons): numbers = [lesson.number for lesson in lessons] - tuples_list = [tuple(map(int, s.split("."))) for s in numbers] + tuples_list = [tuple(map(int, s.split("."))) for s in numbers] # noqa sorted_tuples = sorted(tuples_list) - sorted_numbers = [".".join(map(str, t)) for t in sorted_tuples] + sorted_numbers = [".".join(map(str, t)) for t in sorted_tuples] # noqa index = sorted_numbers.index(current) return { From 05282178dd639ae20eb0320a462b64a71d1ffc00 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 27 Oct 2023 18:30:45 +0530 Subject: [PATCH 8/8] fix: removed functional programing code --- lms/www/batch/learn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/www/batch/learn.py b/lms/www/batch/learn.py index a3b9566c..d51751ac 100644 --- a/lms/www/batch/learn.py +++ b/lms/www/batch/learn.py @@ -108,9 +108,9 @@ def get_page_extensions(context): def get_neighbours(current, lessons): numbers = [lesson.number for lesson in lessons] - tuples_list = [tuple(map(int, s.split("."))) for s in numbers] # noqa + tuples_list = [tuple(int(x) for x in s.split(".")) for s in numbers] sorted_tuples = sorted(tuples_list) - sorted_numbers = [".".join(map(str, t)) for t in sorted_tuples] # noqa + sorted_numbers = [".".join(str(num) for num in t) for t in sorted_tuples] index = sorted_numbers.index(current) return {