Package provides classes: FileList, FileDeque that behaves like bulltins but keeps items at disk.
Project description
Module contains class with extended python list that stores items at disk. By default items before save are pickled and compressed. Use that list as usual list!
In addition, there is implemented extended python deque with disk storage and same behaviour as collections.deque.
Intend of package was to create generic iterables that stores really big collection of items that does not fit in memory and to avoid usage of external cache and local database storages.
>>> from diskcollections.iterables import FileList
>>> flist = FileList()
>>> flist.extend([1, 2, 3])
>>> flist.append(4)
>>> flist
[1, 2, 3, 4]
>>> flist[2]
3
>>> flist2 = flist[:] # copy makes new FileList
>>> my_list = list(flist) # now its simple list
>>> from diskcollections.iterables import FileDeque
>>> fdeque = FileDeque()
>>> fdeque.extend([1, 2, 3])
>>> fdeque.append(4)
>>> fdeque
FileDeque([1, 2, 3, 4])
>>> fdeque.pop()
4
>>> fdeque.appendleft(0)
>>> fdeque.popleft()
0
There are available more ways to serialize items.
>>> from diskcollections.iterables import FileList, FileDeque
>>> from diskcollections.serializers import (
PickleSerializer, # pickle items
PickleZLibSerializer, # pickle + compress items
JsonSerializer, # convert to json items
JsonZLibSerializer # convert to json + compress items
)
>>> from functools import partial
>>> JsonFileList = partial(FileList, serializer_class=JsonHandler)
>>> flist = JsonFileList()
>>> flist.append({'a': 1, 'b': 2, 'c': 3})
>>> flist[0]
{u'a': 1, u'b': 2, u'c': 3}
Installation
To install package type
$ pip install python-disk-collections
How it works
In order to implement your serializer create class with methods: dumps and loads or import interface.
>>> from diskcollections.interfaces import ISerializer
class ISerializer:
@staticmethod
def dumps(obj):
"""Converts object to string.
:param obj: any python object
:return: dumped string
"""
raise NotImplementedError
@staticmethod
def loads(obj):
"""Restored dumped string into python object.
:param obj: Object stored as string
:return: python object restored from dump
"""
raise NotImplementedError
All serializers from example above implements interface ISerializer.
Under the hood, FileList for storage items uses tempfile.mktemp (in python2) or tempfile.TemporaryDirectory (in python3). It means, that every list has own unique directory, placed likely in /tmp/. When list is removed by garbage collector, all items that was stored are lost.
For FileDeque stores items in the same way as FileList.
Contribute
Fork repository on GitHub to start making your changes to the master branch (or branch off of it).
Write tests that prove that bug or future works as expected
Check your code and tests with tox
Send a pull request!
License
Python-Disk-Collection is under MIT license, see LICENSE for more details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python-disk-collections-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f982c2a2d3ae4fa061249fa4c6c452ed9841acfe925befd653d774777d80eff6 |
|
MD5 | a7ebcbc653e9522209ee9ab256fb87e2 |
|
BLAKE2b-256 | b9710cbeed527ee3f9a764ae0c285ce65a3ef9eb80a189ead10d39590eeae7c8 |
Hashes for python_disk_collections-0.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce307359226f8f04b9e99673b39e48430f5919eb7d298b7badc509f702f17e44 |
|
MD5 | c8a1907404e33c68476f3437c14f9da3 |
|
BLAKE2b-256 | b253b30d0eef98d63762b94bbd68d3e2bc04bff865d679a6cbc80d66c9e74d8d |