1
0
Fork 0
mirror of https://github.com/Luzifer/mqtt2influx.git synced 2024-10-18 05:44:19 +00:00
mqtt2influx/yaml_lambda.py

25 lines
596 B
Python
Raw Permalink Normal View History

2020-07-11 16:20:49 +00:00
import json
import yaml
class YAMLLambda(yaml.YAMLObject):
yaml_tag = '!lambda'
def __init__(self, lambda_code):
self.code = lambda_code
def run(self, in_value):
return eval('lambda {}'.format(self.code))(in_value)
@classmethod
def from_yaml(cls, loader, node):
return YAMLLambda(node.value)
@classmethod
def to_yaml(cls, dumper, data):
return dumper.represent_scalar(cls.yaml_tag, data.code)
yaml.SafeLoader.add_constructor('!lambda', YAMLLambda.from_yaml)
yaml.SafeDumper.add_multi_representer(YAMLLambda, YAMLLambda.to_yaml)