2019-03-23 22:02:55 +00:00
|
|
|
class ConvertTable():
|
|
|
|
'''The class is used to store and find the right function to convert value from
|
|
|
|
one key to another'''
|
|
|
|
def __init__(self):
|
|
|
|
self.convert_functions = []
|
|
|
|
|
|
|
|
def add_function(self, function):
|
|
|
|
'''Here you can add function to ConvertTable.'''
|
|
|
|
#TODO: make this method produce new functions, that will be able to
|
|
|
|
#create converter chains
|
|
|
|
self.convert_functions.append(function)
|
|
|
|
|
2019-07-05 14:26:25 +00:00
|
|
|
def all_possible_conversions(self, from_keys):
|
|
|
|
result = set()
|
|
|
|
from_keys = set(from_keys)
|
|
|
|
for function in self.convert_functions:
|
|
|
|
input_args = set(value for key, value in
|
|
|
|
function.__annotations__.items() if
|
|
|
|
key!='return')
|
|
|
|
if input_args.issubset(from_keys):
|
|
|
|
result = result.union(set(function.__annotations__['return']))
|
|
|
|
return result
|
|
|
|
|
2019-03-23 22:02:55 +00:00
|
|
|
def get_converter(self, from_keys, to_key):
|
|
|
|
'''This function returns converter function, that can convert one key
|
|
|
|
to another'''
|
2019-07-05 14:26:25 +00:00
|
|
|
to_key = {to_key}
|
2019-03-23 22:02:55 +00:00
|
|
|
for function in self.convert_functions:
|
2019-07-05 14:26:25 +00:00
|
|
|
input_args = set(value for key, value in
|
|
|
|
function.__annotations__.items() if
|
|
|
|
key!='return')
|
|
|
|
if input_args.issubset(from_keys) and to_key.issubset(function.__annotations__['return']):
|
2019-11-13 19:17:42 +00:00
|
|
|
print("found converter for %s!!!" % to_key)
|
2019-07-05 14:26:25 +00:00
|
|
|
return input_args, function
|
|
|
|
raise Exception("There is no converter for %s to %s" % (from_keys,
|
|
|
|
to_key))
|
2019-03-23 22:02:55 +00:00
|
|
|
return None, None
|
2019-11-13 16:15:54 +00:00
|
|
|
|
|
|
|
def get_metaconverter(self, from_keys, to_keys):
|
|
|
|
'''This function constructs and returns new function used to provide fast
|
|
|
|
conversion from from_keys to to_keys'''
|
2019-11-13 19:17:42 +00:00
|
|
|
print("from_keys",from_keys)
|
|
|
|
print("to_keys",to_keys)
|
2019-11-13 16:15:54 +00:00
|
|
|
converters_args = []
|
|
|
|
converters = []
|
|
|
|
for key in to_keys:
|
|
|
|
keys_to_convert, converter = None, None
|
|
|
|
if key in from_keys:
|
2019-11-13 19:17:42 +00:00
|
|
|
print("%s is in from_keys" % key)
|
2019-11-13 16:15:54 +00:00
|
|
|
keys_to_convert = [key]
|
|
|
|
converter = lambda x : {key: x}
|
|
|
|
else:
|
2019-11-13 19:17:42 +00:00
|
|
|
print("getting converter for %s." % key)
|
2019-11-13 16:15:54 +00:00
|
|
|
keys_to_convert, converter = self.get_converter(from_keys, key)
|
2019-11-13 19:17:42 +00:00
|
|
|
print("needed keys: %s" % " ".join(keys_to_convert))
|
2019-11-13 16:15:54 +00:00
|
|
|
converters_args.append(keys_to_convert)
|
|
|
|
converters.append(converter)
|
|
|
|
def metaconverter(args_dict):
|
|
|
|
if args_dict == None:
|
|
|
|
return [None] * len(converters)
|
|
|
|
res = []
|
2019-11-13 19:17:42 +00:00
|
|
|
print(converters)
|
|
|
|
print(converters_args)
|
2019-11-13 16:15:54 +00:00
|
|
|
for i,conv in enumerate(converters):
|
2019-11-13 19:17:42 +00:00
|
|
|
print(converters_args[i])
|
|
|
|
print(args_dict)
|
2019-11-13 16:15:54 +00:00
|
|
|
args = [args_dict[arg] for arg in converters_args[i]]
|
|
|
|
res.append(*[value for key, value in conv(*args).items()])
|
|
|
|
return res
|
|
|
|
return metaconverter
|