c108.collections
Bidirectional mapping utilities providing a generic BiDirectionalMap with forward and reverse lookups.
BiDirectionalMap
Bases: Mapping[K, V], Generic[K, V]
A bidirectional mapping that maintains one-to-one correspondence between keys and values.
Implements the standardMapping protocol for forward lookups (key -> value) while providing efficient reverse lookups (value -> key). Both keys and values must be hashable and unique.
Examples:
>>> bimap = BiDirectionalMap({'a': 1, 'b': 2})
>>> bimap['a'] # Forward lookup
1
>>> bimap.get_key(1) # Reverse lookup
'a'
>>> 'a' in bimap # Key membership
True
>>> bimap.has_value(1) # Value membership
True
>>> bimap.to_dict() # Extract as standarddict
{'a': 1, 'b': 2}
>>> bimap.reversed() # Get reversed mapping
BiDirectionalMap({1: 'a', 2: 'b'})
Forward direction (key -> value): - Implements full Mapping protocol: getitem, iter, len, keys(), values(), items(), get() - Membership testing (x in bimap) applies to keys only, like dict
Reverse direction (value -> key): - get_key(value): lookup key by value (raises KeyError if not found) - has_value(value): test if value exists
Conversion and views
- to_dict(): extract as standarddict (key -> value)
- reversed(): create new BiDirectionalMap with swapped keys/values
Mutation operations maintain bidirectional consistency
- add(key, value): add new mapping, both must be unique
- set(key, value): set/update mapping for key, value must be unique
- pop(key, default=None): remove and return value for key
- delete(key): remove mapping for key
- update(mapping): bulk update with uniqueness validation
- clear(): remove all mappings
Raises:
| Type | Description |
|---|---|
ValueError
|
When attempting to add duplicate keys or values |
KeyError
|
When accessing non-existent keys or values |
TypeError
|
When keys or values are not hashable |
Source code in c108/collections.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
__contains__(key)
Test if key exists (standardMapping behavior).
Source code in c108/collections.py
135 136 137 | |
__eq__(other)
Test equality with another mapping based on key-value pairs.
Source code in c108/collections.py
370 371 372 373 374 | |
__getitem__(key)
Get value for key. Raises KeyError if key not found.
Source code in c108/collections.py
123 124 125 | |
__init__(initial=None)
Initialize bidirectional map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial
|
Mapping[K, V] | Iterable[tuple[K, V]] | None
|
Optional initial data as mapping or iterable of (key, value) pairs |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If initial data contains duplicate keys or values |
TypeError
|
If keys or values are not hashable |
Source code in c108/collections.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
__iter__()
Iterate over keys.
Source code in c108/collections.py
127 128 129 | |
__len__()
Return number of key-value pairs.
Source code in c108/collections.py
131 132 133 | |
__repr__()
Return string representation.
Source code in c108/collections.py
366 367 368 | |
add(key, value)
Add a new key-value pair. Both key and value must be unique.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
The key to add |
required |
value
|
V
|
The value to add |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If key already exists or value already exists |
TypeError
|
If key or value is not hashable |
Source code in c108/collections.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
clear()
Remove all mappings.
Source code in c108/collections.py
341 342 343 344 | |
delete(key)
Remove mapping for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
object
|
The key to remove |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If key not found |
Source code in c108/collections.py
328 329 330 331 332 333 334 335 336 337 338 339 | |
get(key, default=None)
Get value for key, returning default if key not found.
Source code in c108/collections.py
153 154 155 | |
get_key(value)
Get key for value (reverse lookup).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The value to look up |
required |
Returns:
| Type | Description |
|---|---|
K
|
The key associated with the value |
Raises:
| Type | Description |
|---|---|
KeyError
|
If value not found |
Source code in c108/collections.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
get_value(key)
Get value for key (alias for getitem).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
object
|
The key to look up |
required |
Returns:
| Type | Description |
|---|---|
V
|
The value associated with the key |
Raises:
| Type | Description |
|---|---|
KeyError
|
If key not found |
Source code in c108/collections.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
has_value(value)
Test if value exists in the mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The value to test for |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if value exists, False otherwise |
Source code in c108/collections.py
189 190 191 192 193 194 195 196 197 198 199 | |
items()
Return view of (key, value) pairs.
Source code in c108/collections.py
149 150 151 | |
keys()
Return view of keys.
Source code in c108/collections.py
141 142 143 | |
pop(key, default=MISSING)
pop(key: object) -> V
pop(key: object, default: V) -> V
Remove mapping for key and return its value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
object
|
The key to remove |
required |
default
|
V
|
Value to return if key not found |
MISSING
|
Returns:
| Type | Description |
|---|---|
V
|
The value that was associated with key, or default if key not found |
Raises:
| Type | Description |
|---|---|
KeyError
|
If key not found and no default provided |
Source code in c108/collections.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
reversed()
Create new BiDirectionalMap with keys and values swapped.
Returns a new BiDirectionalMap where the original values become keys and the original keys become values. The original mapping is unchanged.
Returns:
| Type | Description |
|---|---|
BiDirectionalMap[V, K]
|
A new BiDirectionalMap with swapped key-value pairs |
Examples:
>>> bimap = BiDirectionalMap({'a': 1, 'b': 2})
>>> rev = bimap.reversed()
>>> rev.to_dict()
{1: 'a', 2: 'b'}
>>> rev[1]
'a'
Source code in c108/collections.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
set(key, value)
Set or update mapping for key. Value must be unique across all mappings.
If key exists, its old value is released. The new value must not be used by any other key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
The key to set |
required |
value
|
V
|
The value to associate with key |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If value already exists (mapped from a different key) |
TypeError
|
If key or value is not hashable |
Source code in c108/collections.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
to_dict()
Extract as standarddictionary (key -> value mapping).
Returns a shallow copy of the forward mapping as a standarddict. Modifications to the returned dict do not affect this BiDirectionalMap.
Returns:
| Type | Description |
|---|---|
dict[K, V]
|
A new dict containing all key-value pairs |
Examples:
>>> bimap = BiDirectionalMap({'a': 1, 'b': 2})
>>> d = bimap.to_dict()
>>> d
{'a': 1, 'b': 2}
>>> d['c'] = 3 # Does not affect bimap
Source code in c108/collections.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
update(other)
Update with key-value pairs from another mapping or iterable.
Uses set() semantics: existing keys are updated, new keys are added. All values must satisfy uniqueness constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Mapping[K, V] | Iterable[tuple[K, V]]
|
Mapping or iterable of (key, value) pairs |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any value would violate uniqueness constraints |
TypeError
|
If any key or value is not hashable |
Source code in c108/collections.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
values()
Return view of values.
Source code in c108/collections.py
145 146 147 | |