Diffing
Comparison
dataclass
¶
Bases: ABC, Generic[_T]
A comparison between two objects of the same type.
Source code in splitgill/diffing.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
compare()
abstractmethod
¶
Compare the two objects and return a 2-tuple containing a DiffOp and a list of further comparisons which need to be handled. If no differences are found, then the first element of the returned 2-tuple will be None.
Returns:
| Type | Description |
|---|---|
Tuple[Optional[DiffOp], List[Comparison]]
|
A 2-tuple containing a DiffOp and a list of further Comparison objects |
Source code in splitgill/diffing.py
154 155 156 157 158 159 160 161 162 163 | |
DictComparison
dataclass
¶
Bases: Comparison[dict]
A comparison between two dicts.
Source code in splitgill/diffing.py
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 | |
compare()
¶
Compares the two dicts and return a 2-tuple containing a DiffOp and a list of further comparisons which need to be handled. If no differences are found, then the first element of the returned 2-tuple will be None.
Returns:
| Type | Description |
|---|---|
Tuple[Optional[DiffOp], List[Comparison]]
|
A 2-tuple containing a DiffOp and a list of further Comparison objects |
Source code in splitgill/diffing.py
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 | |
DiffOp
¶
Bases: NamedTuple
A namedtuple describing the differences found by the diff function.
Source code in splitgill/diffing.py
130 131 132 133 134 135 136 137 138 | |
DiffingTypeComparisonException
¶
Bases: Exception
Exception raised if the base type and the new type passed to the diff function below are not both dicts.
Source code in splitgill/diffing.py
275 276 277 278 279 280 281 | |
ListComparison
dataclass
¶
Bases: Comparison[list]
A comparison between two lists.
Source code in splitgill/diffing.py
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 | |
compare()
¶
Compares the two lists and return a 2-tuple containing a DiffOp and a list of further comparisons which need to be handled. If no differences are found, then the first element of the returned 2-tuple will be None.
Returns:
| Type | Description |
|---|---|
Tuple[DiffOp, List[Comparison]]
|
A 2-tuple containing a DiffOp and a list of further Comparison objects |
Source code in splitgill/diffing.py
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 | |
diff(base, new)
¶
Finds the differences between the two dicts, yielding DiffOps. Each DiffOp describes specific differences between the base dict and the new dict. By applying them all using the patch function below, the new dict can be recreated from the base dict.
For efficiency, the DiffOps represent all the changes at a container level (e.g. a dict or list) not each specific change to every version at a specific key or index. This saves not only database space, but also allows for a faster patch function as changes can be applied en masse instead of individually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
dict
|
the base dict |
required |
new
|
dict
|
the new version of the base dict |
required |
Returns:
| Type | Description |
|---|---|
Iterable[DiffOp]
|
yields DiffOps (if any changes are found) |
Source code in splitgill/diffing.py
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 | |
patch(base, ops)
¶
Applies the operations in the ops iterable to the base dict, returning a new dict. If there are no operations to apply, the base dict is returned unchanged.
Note that although the returned dict is new, the nested container values in it may or may not be new and could reference the same exact object as in the passed base dict. A nested container will be copied to avoid modifying the same container referenced in the base dict if there are any modifications made to it directly, or to any nested containers below it at any depth. If the nested container contains no changes to itself or its nested containers, it is not copied and the original reference to it from the base dict is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
dict
|
the starting dict |
required |
ops
|
Collection[DiffOp]
|
the DiffOps to apply to the base dict (can be pure tuples, doesn't have to be DiffOp namedtuples) |
required |
Returns:
| Type | Description |
|---|---|
dict
|
a new dict with the changes applied |
Source code in splitgill/diffing.py
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 375 | |
prepare_data(value)
¶
Prepares the given value for storage in MongoDB. Conversions are completed like so:
- None values are just returned as is
- str values have invalid characters removed and are then returned. The
characters are currently all unicode control characters except
, , and . - int, float, bool, and None values are returned with no changes made - datetime and date values are converted to strings using strftime with the specific formats DATETIME_FORMAT and DATE_FORMAT. - dict values are returned as a new dict instance, with all the keys converted to strings and all the values recursively prepared using this function. - lists, sets, and tuples are converted to lists with each element of the value prepared by this function.
:param value: the value to be stored in MongoDB
:return: None, str, int, float, bool, tuple, or dict depending on the input value
Source code in splitgill/diffing.py
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 | |
prepare_field_name(name)
¶
Cleans up a field name for ingestion into the system. There are a few steps to this:
- convert the name to a str as MongoDB only accepts str keys in objects
- remove any control characters from the str
- replace . with _ as Elasticsearch doesn't like dots in keys
- replace any name starting _ with - as _ is a reserved character in Splitgill
that we use in special cases
If after cleaning, the field name is an empty string, we return a hyphen.
This function explicitly handles the _id field by just returning it if encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Any
|
the field name |
required |
Returns:
| Type | Description |
|---|---|
str
|
a clean str field name |
Source code in splitgill/diffing.py
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 | |