about summary refs log tree commit diff
path: root/uploader/datautils.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-09-09 14:06:31 -0500
committerFrederick Muriuki Muriithi2024-09-09 16:41:46 -0500
commit9cd33ddac3d6848c5443962d66494635feadef51 (patch)
tree385a559380f4d6a961fefb38ad410ad9ca27b052 /uploader/datautils.py
parent707c715d1e336ee45bdcced031881ed603b9297a (diff)
downloadgn-uploader-9cd33ddac3d6848c5443962d66494635feadef51.tar.gz
Initialise samples uploads
* Move existing code to new module
* Rework the UI: create new templates
* Rework the routes: Select species and populations before attempting
  an upload.
Diffstat (limited to 'uploader/datautils.py')
-rw-r--r--uploader/datautils.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/uploader/datautils.py b/uploader/datautils.py
new file mode 100644
index 0000000..b95a9e0
--- /dev/null
+++ b/uploader/datautils.py
@@ -0,0 +1,21 @@
+"""Generic data utilities: Rename module."""
+import math
+from functools import reduce
+
+def order_by_family(items: tuple[dict, ...],
+                    family_key: str = "Family",
+                    order_key: str = "FamilyOrderId") -> list:
+    """Order the populations by their families."""
+    def __family_order__(item):
+        orderval = item[order_key]
+        return math.inf if orderval is None else orderval
+
+    def __order__(ordered, current):
+        _key = (__family_order__(current), current[family_key])
+        return {
+            **ordered,
+            _key: ordered.get(_key, tuple()) + (current,)
+        }
+
+    return sorted(tuple(reduce(__order__, items, {}).items()),
+                  key=lambda item: item[0][0])