Пример #1
0
 def __init__(self, datatype):
     """
     Constructor.
     :param datatype: The datatype the Translator should care for.
     """
     validate.is_instance_of(datatype, Datatype)
     self.datatype = datatype
Пример #2
0
def chunk_list(l, chunk_size):
    """Partitions the given list into <chunk_size> chunks."""
    validate.is_instance_of(l, list)
    if len(l) == 0:
        return l

    return [l[i:i + chunk_size] for i in range(0, len(l), chunk_size)]
Пример #3
0
 def __init__(self, task: Task, order: Optional[int]) -> None:
     import numbers
     validate.is_in_dict_keys('expression', task.operator)
     validate.is_in_dict_keys('other', task.operator)
     validate.is_instance_of(task.operator['other'], numbers.Number)
     validate.is_in_list(task.operator['expression'],
                         ['lt', 'le', 'eq', 'ne', 'ge', 'gt'])
     super().__init__(TransformationType.FILTER, task, order)
Пример #4
0
    def cast(self, target_type):
        """
        Prepares a cast statement that is supported / accepted by the dialect.
        Do
            c = instance.cast().format(VALUE='my_value')
        :return:
        """
        validate.is_instance_of(target_type, Datatype)

        return PostgresTranslator.MAPPING[target_type.type]['cast']
Пример #5
0
def merge_dicts(dict1, dict2):
    """
    Merges two dictionaries together.
    Duplicate keys of dict2 override keys of dict1.
    """
    validate.is_instance_of(dict1, dict)
    validate.is_instance_of(dict2, dict)

    res = dict1.copy()
    res.update(dict2)
    return res
Пример #6
0
def group_by(aggregation_func, list_to_group):
    """
    Applies the aggregation_func to every list
    element to determine a grouping key
    """
    validate.is_instance_of(list_to_group, list)
    validate.is_function(aggregation_func)

    d = defaultdict(list)
    for item in list_to_group:
        d[aggregation_func(item)].append(item)

    return d
Пример #7
0
def get_value_from_dictionary(d, key, default=None, sep='.'):
    """Assume your dictionary has the following structure:
    {
        a: {a1: {a11: "a11"}, a2: "a"},
        b: "b1"
    }
    then this will happen
    > get_value_from_dictionary(d, "a.a1.a11", default=None, sep='.')
    > "a11"
    > get_value_from_dictionary(d, "a.a2", default=None, sep='.')
    > "a2"
    ...
    """
    validate.is_instance_of(d, dict)
    validate.is_str(key)

    path = key.split(sep)
    value = d
    for entry in path:
        if not isinstance(value, dict):
            return default
        value = value.get(entry, default)

    return value