Skip to content

Commit

Permalink
Add logical utility functions (#8590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Feb 10, 2021
1 parent da819e3 commit d60b98b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from ....utils.common import no_op
from ....utils.functions import no_op


class LabelAggregator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ....config import is_affirmative
from ....constants import ServiceCheck
from ....errors import ConfigurationError
from ....utils.common import no_op
from ....utils.functions import no_op, return_true
from ....utils.http import RequestsWrapper
from .labels import LabelAggregator, get_label_normalizer
from .transform import MetricTransformer
Expand Down Expand Up @@ -118,7 +118,7 @@ def __init__(self, check, config):
elif exclude_metrics_by_labels:
for label, values in exclude_metrics_by_labels.items():
if values is True:
self.exclude_metrics_by_labels[label] = lambda label_value: True
self.exclude_metrics_by_labels[label] = return_true
elif isinstance(values, list):
for i, value in enumerate(values, 1):
if not isinstance(value, str):
Expand Down
4 changes: 0 additions & 4 deletions datadog_checks_base/datadog_checks/base/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ def ensure_unicode(s):
to_string = to_native_string


def no_op(*args, **kwargs):
pass


def compute_percent(part, total):
if total:
return part / total * 100
Expand Down
31 changes: 31 additions & 0 deletions datadog_checks_base/datadog_checks/base/utils/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)


def identity(obj, **kwargs):
"""
https://en.wikipedia.org/wiki/Identity_function
"""
return obj


def no_op(*args, **kwargs):
"""
https://en.wikipedia.org/wiki/NOP_(code)
"""


def predicate(assertion):
"""
https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)
"""
return return_true if bool(assertion) else return_false


def return_true(*args, **kwargs):
return True


def return_false(*args, **kwargs):
return False

0 comments on commit d60b98b

Please sign in to comment.