How to get parametrized readers/writers in dol
#31
thorwhalen
started this conversation in
Enhancement
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
dol
helps you make facades for the storage concern. It limits the methods to the base builtin collections methods (those ofCollection
,Mapping
andMutableMapping
types).What do you do, though when you need to have more control over your storage operations?
Let's take the example of needing to do chunked reads. That is, you want to be able to say
store.get_chunks(key, chk_size)
and get an iterator of chunks ofchk_size
. TheMapping
interface doesn't give you that naturally. It just gives youstore[this]
. So you write your own new method for this (see for example this (rejected) pull request.The biggest con to that is that it's not a method that
dol
knows of, so if you use any ofdol
's tools, stuff will break.What can you do?
Is it really a need?
Sometimes the need isn't really a need: The "need" comes from constraints of approach you're taking, or have to take.
Many times your best move is to rethink your approach to fit it to the patterns/interfaces that the python gods decided to include as the fundamental methods, or write a adapter to an existing system so it will comply to those fundamental interfaces.
In the case of the
store.get_chunks(key, chk_size)
, you might, for example, realize that really, yourchk_size
will be fixed (perhaps different, but fixed for every given context). In that case you could just have the values of your store be iteratables, or iterators of chunks:tuple keys
If you really "need" to not fix your
chk_size
for a give context, to enable a user to ask for anychk_size
when they read, but while avoiding writing another method, you define your__getitem__
to be something like this:Though it is arguably "better" than writing a new method, it's not ideal. If you were only getting items, it would be fine, but a
Mapping
has other methods, and you might be misaligned how a user might expect the methods to interact.For example, that
__iter__
should give you a list of all valid keys, and that these keys can all be used to get items.What are you going to do? List all possible
(src_key, chk_size)
combinations? Certainly not!Instead, you could:
So now your
__iter__
gives you all validsrc_key
s, which you can use to get items.But still, a purist would sneer at it (and if they do, just mention
pandas
, which does do such "expanded item getting".Opinion: There's a missing
collections.abc
type: AGetter
. One that just has a__getitem__
, but not necessarily__iter__
,__len__
etc.a cleaner (but not necessarily always better) way
Make the object that
__getitem__
returns be something that has a__getitem__
itself, that will give you the chunk iterator.The usage would be something like:
Beta Was this translation helpful? Give feedback.
All reactions