dol.errors¶
Error objects and utils
-
exception
dol.errors.
AlreadyExists
[source]¶ To use if an object already exists (and shouldn’t; for example, to protect overwrites)
-
exception
dol.errors.
MethodNameAlreadyExists
[source]¶ To use when a method name already exists (and shouldn’t)
-
exception
dol.errors.
NotValid
[source]¶ To use to indicate when an object doesn’t fit expected properties
-
exception
dol.errors.
OperationNotAllowed
[source]¶ When a given operation is not allowed (through being disabled, conditioned, or just implemented)
-
exception
dol.errors.
OverWritesNotAllowedError
[source]¶ Error to raise when a writes to existing keys are not allowed
-
exception
dol.errors.
SetattrNotAllowed
[source]¶ An attribute was requested to be set, but some conditions didn’t apply
-
dol.errors.
items_with_caught_exceptions
(d: collections.abc.Mapping, callback=None, catch_exceptions=(<class 'Exception'>, ), yield_callback_output=False)[source]¶ Do what Mapping.items() does, but catching exceptions when getting the values for a key.
Some time your store.items() is annoying because of some exceptions that happen when you’re retrieving some value for some of the keys.
Yes, if that happens, it’s that something is wrong with your store, and yes, if it’s a store that’s going to be used a lot, you really should build the right store that doesn’t have that problem.
But now that we appeased the paranoid naysayers with that warning, let’s get to business: Sometimes you just want to get through the hurdle to get the job done. Sometimes your store is good enough, except for a few exceptions. Sometimes your store gets it’s keys from a large pool of possible keys (e.g. github stores or kaggle stores, or any store created by a free-form search seed), so you can’t really depend on the fact that all the keys given by your key iterator will give you a value without exception – especially if you slapped on a bunch of post-processing on the out-coming values.
So you can right a for loop to iterate over your keys, catch the exceptions, do something with it…
Or, in many cases, you can just use items_with_caught_exceptions.
- Parameters
d – Any Mapping
catch_exceptions – A tuple of exceptions that should be caught
callback –
A function that will be called every time an exception is caught. The signature of the callback function is required to be:
k (key), e (error obj), d (mapping), i (index)
but
- Returns
An (key, val) generator with exceptions caught
>>> from collections.abc import Mapping >>> class Test(Mapping): # a Mapping class that has keys 0..9, but raises of KeyError if the key is not even ... n = 10 ... def __iter__(self): ... yield from range(2, self.n) ... def __len__(self): ... return self.n ... def __getitem__(self, k): ... if k % 2 == 0: ... return k ... else: ... raise KeyError('Not even') >>> >>> list(items_with_caught_exceptions(Test())) [(2, 2), (4, 4), (6, 6), (8, 8)] >>> >>> def my_log(k, e): ... print(k, e) >>> list(items_with_caught_exceptions(Test(), callback=my_log)) 3 'Not even' 5 'Not even' 7 'Not even' 9 'Not even' [(2, 2), (4, 4), (6, 6), (8, 8)] >>> def my_other_log(i): ... print(i) >>> list(items_with_caught_exceptions(Test(), callback=my_other_log)) 1 3 5 7 [(2, 2), (4, 4), (6, 6), (8, 8)]