py2store.stores.local_store¶
stores to operate on local files
-
class
py2store.stores.local_store.
AutoMkDirsOnSetitemMixin
[source]¶ A mixin that will automatically create directories on setitem, when missing.
-
class
py2store.stores.local_store.
AutoMkPathformatMixin
(path_format=None, max_levels=None)[source]¶ A mixin that will choose a path_format if none given
-
class
py2store.stores.local_store.
DirStore
(rootdir)[source]¶ A store for local directories. Keys are directory names and values are subdirectory DirStores.
>>> from py2store import __file__ >>> import os >>> root = os.path.dirname(__file__) >>> s = DirStore(root) >>> assert set(s).issuperset({'stores', 'persisters', 'serializers', 'key_mappers'})
-
class
py2store.stores.local_store.
LocalBinaryStore
(path_format, max_levels=None)[source]¶ Local files store for binary data
-
class
py2store.stores.local_store.
LocalJsonStore
(path_format, max_levels=None)[source]¶ Local files store for text dataData is assumed to be a JSON string, and is loaded with json.loads and dumped with json.dumps
-
class
py2store.stores.local_store.
LocalPickleStore
(path_format, max_levels=None, fix_imports=True, protocol=None, pickle_encoding='ASCII', pickle_errors='strict', **open_kwargs)[source]¶ Local files store with pickle serialization
-
py2store.stores.local_store.
LocalStore
¶
-
class
py2store.stores.local_store.
LocalTextStore
(path_format, max_levels=None)[source]¶ Local files store for text data
-
class
py2store.stores.local_store.
MakeMissingDirsStoreMixin
[source]¶ Will make a local file store automatically create the directories needed to create a file. Should be placed before the concrete perisister in the mro but in such a manner so that it receives full paths.
-
class
py2store.stores.local_store.
PathFormatStore
(path_format, max_levels: int = inf, mode='', **open_kwargs)[source]¶ Local file store using templated relative paths.
>>> from tempfile import gettempdir >>> import os >>> >>> def write_to_key(fullpath_of_relative_path, relative_path, content): # a function to write content in files ... with open(fullpath_of_relative_path(relative_path), 'w') as fp: ... fp.write(content) >>> >>> # Preparation: Make a temporary rootdir and write two files in it >>> rootdir = os.path.join(gettempdir(), 'path_format_store_test' + os.sep) >>> if not os.path.isdir(rootdir): ... os.mkdir(rootdir) >>> # recreate directory (remove existing files, delete directory, and re-create it) >>> for f in os.listdir(rootdir): ... fullpath = os.path.join(rootdir, f) ... if os.path.isfile(fullpath): ... os.remove(os.path.join(rootdir, f)) >>> if os.path.isdir(rootdir): ... os.rmdir(rootdir) >>> if not os.path.isdir(rootdir): ... os.mkdir(rootdir) >>> >>> filepath_of = lambda p: os.path.join(rootdir, p) # a function to get a fullpath from a relative one >>> # and make two files in this new dir, with some content >>> write_to_key(filepath_of, 'a', 'foo') >>> write_to_key(filepath_of, 'b', 'bar') >>> >>> # point the obj source to the rootdir >>> s = PathFormatStore(path_format=rootdir) >>> >>> # assert things... >>> assert s._prefix == rootdir # the _rootdir is the one given in constructor >>> assert s[filepath_of('a')] == 'foo' # (the filepath for) 'a' contains 'foo' >>> >>> # two files under rootdir (as long as the OS didn't create it's own under the hood) >>> len(s) 2 >>> assert list(s) == [filepath_of('a'), filepath_of('b')] # there's two files in s >>> filepath_of('a') in s # rootdir/a is in s True >>> filepath_of('not_there') in s # rootdir/not_there is not in s False >>> filepath_of('not_there') not in s # rootdir/not_there is not in s True >>> assert list(s.keys()) == [filepath_of('a'), filepath_of('b')] # the keys (filepaths) of s >>> sorted(list(s.values())) # the values of s (contents of files) ['bar', 'foo'] >>> assert list(s.items()) == [(filepath_of('a'), 'foo'), (filepath_of('b'), 'bar')] # the (path, content) items >>> assert s.get('this key is not there', None) is None # trying to get the val of a non-existing key returns None >>> s.get('this key is not there', 'some default value') # ... or whatever you say 'some default value' >>> >>> # add more files to the same folder >>> write_to_key(filepath_of, 'this.txt', 'this') >>> write_to_key(filepath_of, 'that.txt', 'blah') >>> write_to_key(filepath_of, 'the_other.txt', 'bloo') >>> # see that you now have 5 files >>> len(s) 5 >>> # and these files contain values: >>> sorted(s.values()) ['bar', 'blah', 'bloo', 'foo', 'this'] >>> >>> # but if we make an obj source to only take files whose extension is '.txt'... >>> s = PathFormatStore(path_format=rootdir + '{}.txt') >>> >>> rootdir_2 = os.path.join(gettempdir(), 'obj_source_test_2') # get another rootdir >>> if not os.path.isdir(rootdir_2): ... os.mkdir(rootdir_2) >>> filepath_of_2 = lambda p: os.path.join(rootdir_2, p) >>> # and make two files in this new dir, with some content >>> write_to_key(filepath_of, 'this.txt', 'this') >>> write_to_key(filepath_of, 'that.txt', 'blah') >>> write_to_key(filepath_of, 'the_other.txt', 'bloo') >>> >>> ss = PathFormatStore(path_format=rootdir_2 + '{}.txt') >>> >>> assert s != ss # though pointing to identical content, o and oo are not equal since the paths are not equal!
-
py2store.stores.local_store.
PickleStore
¶
-
class
py2store.stores.local_store.
QuickBinaryStore
(path_format=None, max_levels=None)[source]¶ Local files store for binary data with default temp root and auto dir generation on write.
-
class
py2store.stores.local_store.
QuickJsonStore
(path_format=None, max_levels=None)[source]¶ Local files store for text data with default temp root and auto dir generation on write.Data is assumed to be a JSON string, and is loaded with json.loads and dumped with json.dumps
-
class
py2store.stores.local_store.
QuickLocalStoreMixin
(path_format=None, max_levels=None)[source]¶ A mixin that will choose a path_format if none given, and will automatically create directories on setitem, when missing.
-
class
py2store.stores.local_store.
QuickPickleStore
(path_format=None, max_levels=None)[source]¶ Local files store with pickle serialization with default temp root and auto dir generation on write.
-
py2store.stores.local_store.
QuickStore
¶