sdcflows.utils.bimap module

A bidirectional hashmap.

class sdcflows.utils.bimap.EstimatorRegistry(*args, **kwargs)[source]

Bases: bidict

A specialized bidict to track FieldmapEstimation.

Examples

>>> estimators = EstimatorRegistry()
>>> _ = estimators.add(("file3.txt", "file4.txt"))
>>> estimators.sources
['file3.txt', 'file4.txt']
>>> _ = estimators.add(("file1.txt", "file2.txt"))
>>> estimators.sources
['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt']
>>> _ = estimators.add(("file3.txt", "file2.txt"))
>>> estimators.sources
['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt']
>>> estimators.get_key("file3.txt")
('auto_00000', 'auto_00002')
>>> estimators.get_key("file5.txt")
()
get_key(value)[source]

Get the key(s) containing a particular value.

property sources

Return a flattened list of fieldmap sources.

class sdcflows.utils.bimap.bidict(*args, **kwargs)[source]

Bases: dict

A bidirectional hashmap.

>>> d = bidict({"a": 1, "b": 2}, c=3)
>>> d["a"]
1
>>> d[1]
'a'
>>> 2 in d
True
>>> "b" in d
True
>>> d["d"] = 4
>>> del d["d"]
>>> d["d"] = 4
>>> del d[4]
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> d["d"] = "d"  
Traceback (most recent call last):
TypeError: 'd' <> 'd' is a self-mapping
>>> d["d"]  
Traceback (most recent call last):
KeyError: 'd'
>>> d["b"] = None  
Traceback (most recent call last):
KeyError: 'b' is already in mapping
>>> d[1] = None  
Traceback (most recent call last):
KeyError: '1' is already a value in mapping
>>> d["d"] = 1  
Traceback (most recent call last):
ValueError: '1' is already in mapping
>>> d["d"] = "a"  
Traceback (most recent call last):
ValueError: 'a' is already a key in mapping
>>> d["unhashable val"] = []  
Traceback (most recent call last):
TypeError: value '[]' of unhashable type: 'list'
>>> d[list()] = 1  
Traceback (most recent call last):
TypeError: key '[]' of unhashable type: 'list'
>>> d.add("a new value")
'auto_00000'
>>> d["auto_00000"]
'a new value'
>>> d["auto_00001"] = "another value"
>>> d.add("a new value")  
Traceback (most recent call last):
ValueError: 'a new value' is already in mapping
>>> d.add("third value")
'auto_00002'
>>> d == bidict(reversed(list(d.items())))
True
>>> bidict({"a": 1, "b": 1})  
Traceback (most recent call last):
TypeError: Bidirectional dictionary cannot contain repeated values
>>> del d["e"]  
Traceback (most recent call last):
KeyError: 'e'
>>> list(d)
['a', 'b', 'c', 'auto_00000', 'auto_00001', 'auto_00002']
>>> list(d.values())
[1, 2, 3, 'a new value', 'another value', 'third value']
>>> d.clear()
>>> d
{}
add(value)[source]

Insert a new value in the bidict, generating an automatic key.

clear()[source]

Empty of all key/value pairs.