Broden222 commented on code in PR #44:
URL: https://github.com/apache/flagon-distill/pull/44#discussion_r1664431330


##########
examples/labels.py:
##########
@@ -0,0 +1,108 @@
+from typing import Any, Dict, List, Callable
+import pandas as pd
+
+
+class FeatureDefinition:
+    # Implement class logic
+    # TODO: Add a very specific type hint to the rule object:
+    # see: 
https://docs.python.org/3/library/typing.html#annotating-callable-objects
+    def __init__(self, label: str, rule: Callable[[Dict[str, Any]], bool]):
+        if not callable(rule):
+            raise TypeError("Rule not callable")
+        
+        if not isinstance(label, str):
+            raise TypeError("Label is not a string")
+        # Immediately validate the rule, so you can error
+        # out/exit early if it's invalid
+            # TODO: raise an informative error to the user
+            # see:
+            # - https://www.geeksforgeeks.org/python-exception-handling/
+            # - 
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
+
+        self.label = label
+        self._rule = rule
+
+    # This is a wrapper method around the private rule attribute we
+    # store on self during init.
+    #
+    # Q: Why make the rule private and
+    # wrap the call to it in another method?
+    # A: This encapsulation allows us to expose a nicer set of behavior
+    # and naming conventions to both the user and ourselves as developers.
+    # In `label_features` below, you see that we can then check whether
+    # a log `matches` the definition which reads more like plain english
+    # and is an important part of writing clean, idiomatic python code.
+    # TODO: Implement this wrapper function by using the _rule attribute
+    def matches(self, log: Dict[str, Any]) -> bool:
+            return self._rule(log)
+
+
+def label_features(
+    logs: List[Dict[str, Any]], definitions: List[FeatureDefinition]
+) -> List[Dict[str, Any]]:
+    # Iterate through all the logs
+    for log in logs:
+        # Check whether the log matches the definition
+        # for each definition supplied in the defintions list
+        for definition in definitions:
+            # NOTE: This reads much like an English sentence
+            # and is self-explanatory. I don't need to read the
+            # implementation logic to get a sense of what's happening
+            if definition.matches(log):
+                # NOTE: Since we're mutating the log itself and interacting
+                # with a field that may (does) not already exists, we need
+                # to first check if it is present in our log and instantiate
+                # it if not.
+                if "labels" not in log:
+                    log.update({"labels": list()})
+                log["labels"].append(definition.label)
+    return logs

Review Comment:
   Should I create the process.py in the distill/process directory. Also, 
should the function remain in the transform.py as well? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@flagon.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@flagon.apache.org
For additional commands, e-mail: notifications-h...@flagon.apache.org

Reply via email to