Misc Ideas for Enhancements #46
Replies: 7 comments
-
RAM-sensitive
|
Beta Was this translation helpful? Give feedback.
-
Clean up explicit toolsWe have a module for "explicit" stores: dol/explicit.py. Note that a lot of the same functionalities can be taken care of, more or less, via I'd propose to have a mini project where we analyze what's there, document and cover it with test it using AI, then try to replace with wrap_kvs-based code. Also, it would be nice to have some tutorial on this theme so that people (and AI) can better understand how to use it. Things to check outIn the following discussion comment, I'm using In config2py.base, there's a |
Beta Was this translation helpful? Give feedback.
-
Forwarding methods to extend mappings?Given the "special methods" ( from collections.abc import Mapping
class MappingHooks(Mapping):
def __iter__(self):
return self.__iter()
def __len__(self):
return self.__len()
def __contains__(self, key):
return self.__contains(key)
def __getitem__(self, key):
return self.__getitem(key)
class ExampleMapping(MappingHooks, Mapping):
"""Just forwards all method calls to the wrapped dict"""
def __init__(self, d: dict):
self.d = d
def __iter(self):
return iter(self.d)
def __len(self):
return len(self.d)
def __contains(self, key):
return key in self.d
def __getitem(self, key):
return self.d[key]
m = ExampleMapping({'a': 1, 'b': 2})
assert list(m) == ['a', 'b']
assert len(m) == 2
assert 'a' in m
assert 'b' in m
assert 'c' not in m
assert m['a'] == 1
assert m['b'] == 2Notes:
|
Beta Was this translation helpful? Give feedback.
-
a
|
Beta Was this translation helpful? Give feedback.
-
A jsonbin store
There's no official python package for it. Found this package though. Would like a We could start by using this this package, but I'd prefer to use |
Beta Was this translation helpful? Give feedback.
-
Use the standard lib
|
Beta Was this translation helpful? Give feedback.
-
Standard lib support for postget and presetThere are several standard library modules that can help use implement dynamic, context-aware encoding/decoding, especially when dispatching based on file extension, key patterns, or value types. While none of them are built specifically for key/value or extension-based codecs, we can combine them with custom logic to build what we're after. 🧰 Helpful Standard Library Modules for Dynamic Serialization mimetypesMaps file extensions to MIME types, which you can use to select a codec. import mimetypes
mime, _ = mimetypes.guess_type("file.json") # 'application/json'Use this to dispatch to codecs like 'json', 'gzip', 'text/plain', etc. functools.singledispatchFunction overloading based on value type. Great for dispatching based on value type. from functools import singledispatch
@singledispatch
def encode(value):
raise NotImplementedError
@encode.register
def _(value: dict):
return codecs.encode(json.dumps(value), 'utf-8')
@encode.register
def _(value: bytes):
return value # passthroughCombine with key-based dispatch to dynamically choose both key and value logic. pathlibGives you clean access to file extensions and path components, ideal for key-based logic. from pathlib import Path
ext = Path("data/file.json.gz").suffix # '.gz'You can extract the full .suffixes for nested extensions (e.g. .json.gz). types + inspectUseful when value-based logic depends on inspecting callables, lambdas, or types. from types import FunctionType
from inspect import isfunction
if isinstance(value, FunctionType) or isfunction(value):
raise TypeError("Cannot serialize functions")json, pickle, base64, gzip, bz2While not “dispatch helpers,” these format-specific modules form your backend encoders/decoders. You can wrap them like codecs. Example: import json
import gzip
def gzip_json_encoder(value):
return gzip.compress(json.dumps(value).encode())
def gzip_json_decoder(data):
return json.loads(gzip.decompress(data).decode())Register that with your own dispatch layer or codec wrapper. 🧩 Putting It All TogetherYou can build a dynamic encoder registry like this: from pathlib import Path
from functools import singledispatch
def get_codec_from_key(key):
suffix = Path(key).suffix
if suffix == '.json':
return 'json_codec'
elif suffix == '.gz':
return 'gzip_codec'
return 'utf-8'
@singledispatch
def encode(value, key=None):
raise NotImplementedError
@encode.register
def _(value: dict, key=None):
return json.dumps(value).encode()
@encode.register
def _(value: bytes, key=None):
return value # already encoded✅ Final RecommendationUse a custom dispatch layer that: This gives you codec-like behavior far beyond what codecs supports, with full flexibility for key–value pairs. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
To collect miscellaneous ideas for enhancement, new features, interfaces, etc.
Beta Was this translation helpful? Give feedback.
All reactions