Upload operators.py with huggingface_hub
Browse files1161 unmodified lines1162116311641165116611671168116911701171117211731174117511761177117811791180118138 unmodified lines1220122112221223122412251226122712281229123012311232123312341 unmodified line123612371238123912401241124242 unmodified lines1285128612871288128912901161 unmodified linesclass FilterByCondition(SingleStreamOperator):"""Filters a stream, yielding only instances for which the required values follows the required condition operator.Raises an error if a required key is missing.Args:values (Dict[str, Any]): Values that instances must match using the condition to be included in the output.condition: the name of the desired condition operator between the key and the value in values ("gt", "ge", "lt", "le", "ne", "eq")error_on_filtered_all (bool, optional): If True, raises an error if all instances are filtered out. Defaults to True.Examples:FilterByCondition(values = {"a":4}, condition = "gt") will yield only instances where "a">4FilterByCondition(values = {"a":4}, condition = "le") will yield only instances where "a"<=4FilterByCondition(values = {"a":[4,8]}, condition = "in") will yield only instances where "a" is 4 or 8FilterByCondition(values = {"a":[4,8]}, condition = "not in") will yield only instances where "a" different from 4 or 8"""38 unmodified linesdef _is_required(self, instance: dict) -> bool:for key, value in self.values.items():if key not in instance:raise ValueError(f"Required filter field ('{key}') in FilterByCondition is not found in {instance}")if self.condition == "in":if instance[key] not in value:return Falseelif self.condition == "not in":if instance[key] in value:return Falseelse:func = self.condition_to_func[self.condition]1 unmodified lineraise ValueError(f"Function not defined for condition '{self.condition}'")if not func(instance[key], value):return Falsereturn True42 unmodified linesFilterByExpression(expression = "a <= 4 and b > 5") will yield only instances where the value of field "a" is not exceeding 4 and in field "b" -- greater than 5FilterByExpression(expression = "a in [4, 8]") will yield only instances where "a" is 4 or 8FilterByExpression(expression = "a not in [4, 8]") will yield only instances where "a" is neither 4 nor 8"""1161 unmodified lines116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118638 unmodified lines122512261227122812291230123112321233123412351236123712381239124012411 unmodified line124312441245124612471248124942 unmodified lines129212931294129512961297129812991161 unmodified linesclass FilterByCondition(SingleStreamOperator):"""Filters a stream, yielding only instances in which the values in required fields follow the required condition operator.Raises an error if a required field name is missing from the input instance.Args:values (Dict[str, Any]): Field names and respective Values that instances must match according the condition, to be included in the output.condition: the name of the desired condition operator between the specified (sub) field's value and the provided constant value. Supported conditions are ("gt", "ge", "lt", "le", "ne", "eq", "in","not in")error_on_filtered_all (bool, optional): If True, raises an error if all instances are filtered out. Defaults to True.Examples:FilterByCondition(values = {"a":4}, condition = "gt") will yield only instances where field "a" contains a value > 4FilterByCondition(values = {"a":4}, condition = "le") will yield only instances where "a"<=4FilterByCondition(values = {"a":[4,8]}, condition = "in") will yield only instances where "a" is 4 or 8FilterByCondition(values = {"a":[4,8]}, condition = "not in") will yield only instances where "a" different from 4 or 8FilterByCondition(values = {"a/b":[4,8]}, condition = "not in") will yield only instances where "a" isa dict in which key "b" is mapped to a value that is neither 4 nor 8FilterByCondition(values = {"a[2]":4}, condition = "le") will yield only instances where "a" is a list whose 3-rdelement is <= 4"""38 unmodified linesdef _is_required(self, instance: dict) -> bool:for key, value in self.values.items():try:instance_key = dict_get(instance, key)except ValueError as ve:raise ValueError(f"Required filter field ('{key}') in FilterByCondition is not found in {instance}") from veif self.condition == "in":if instance_key not in value:return Falseelif self.condition == "not in":if instance_key in value:return Falseelse:func = self.condition_to_func[self.condition]1 unmodified lineraise ValueError(f"Function not defined for condition '{self.condition}'")if not func(instance_key, value):return Falsereturn True42 unmodified linesFilterByExpression(expression = "a <= 4 and b > 5") will yield only instances where the value of field "a" is not exceeding 4 and in field "b" -- greater than 5FilterByExpression(expression = "a in [4, 8]") will yield only instances where "a" is 4 or 8FilterByExpression(expression = "a not in [4, 8]") will yield only instances where "a" is neither 4 nor 8FilterByExpression(expression = "a['b'] not in [4, 8]") will yield only instances where "a" is a dict inwhich key 'b' is mapped to a value that is neither 4 nor 8"""
operators.py+19-10