znslice


Nameznslice JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryCache, advanced slicing and lazy loading for __getitem__
upload_time2023-02-06 13:16:12
maintainer
docs_urlNone
authorzincwarecode
requires_python>=3.8,<4.0
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://badge.fury.io/py/znslice.svg)](https://badge.fury.io/py/znslice)
[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnSlice/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnSlice?branch=main)
[![zincware](https://img.shields.io/badge/Powered%20by-zincware-darkcyan)](https://github.com/zincware)
# ZnSlice

A lightweight library  (without external dependencies) for:
- advanced slicing.
- cache `__getitem__(self, item)`
- lazy load `__getitem__(self, item)`

# Installation

```bash
pip install znslice
```

# Usage

## Advanced Slicing and Cache
Convert List to `znslice.LazySequence` to allow advanced slicing.
```python
import znslice

lst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])
print(lst[[0, 1]].tolist())  # [1, 3]
```


```python
import znslice
import collections.abc

class MapList(collections.abc.Sequence):
    def __init__(self, data, func):
        self.data = data
        self.func = func
    
    @znslice.znslice
    def __getitem__(self, item: int):
        print(f"Loading item = {item}")
        return self.func(self.data[item])
    
    def __len__(self):
        return len(self.data)

data = MapList([0, 1, 2, 3, 4], lambda x: x ** 2)

assert data[0] == 0
assert data[[1, 2, 3]] == [1, 4, 9]
# calling data[:] will now only compute data[4] and load the remaining data from cache
assert data[:] == [0, 1, 4, 9, 16]
```

## Lazy Database Loading

You can use `znslice` to lazy load data from a database. This is useful if you have a large database and only want to load a small subset of the data.

In the following we will use the `ase` package to generate `Atoms` objects stored in a database and load them lazily.

```python 
import ase.io
import ase.db
import znslice
import tqdm
import random

# create a database
with ase.db.connect("data.db", append=False) as db:
    for _ in range(10):
        atoms = ase.Atoms('CO', positions=[(0, 0, 0), (0, 0, random.random())])
        db.write(atoms, group="data")
        
# load the database lazily
class ReadASEDB:
    def __init__(self, file):
        self.file = file
    
    @znslice.znslice(
        advanced_slicing=True, # this getitem supports advanced slicingn
        lazy=True # we want to lazy load the data
    )
    def __getitem__(self, item):
        data = []
        with ase.db.connect(self.file) as database:
            if isinstance(item, int):
                print(f"get {item = }")
                return database[item + 1].toatoms()
            for idx in tqdm.tqdm(item):
                data.append(database[idx + 1].toatoms())
        return data
            
    def __len__(self):
        with ase.db.connect(self.file) as db:
            return len(db)

db = ReadASEDB("data.db")

data = db[::2] # LazySequence([<__main__.ReadASEDB>], [[0, 2, 4, 6, 8]])
data.tolist() # list[ase.Atoms] 

# supports addition, advanced slicing, etc.
data = db[::2] + db[1::2]
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "znslice",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "zincwarecode",
    "author_email": "zincwarecode@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ce/b8/c657c1317ec4143dc292231a8b2dd53b2d14e72477d0f7439978d26c3d0f/znslice-0.1.2.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://badge.fury.io/py/znslice.svg)](https://badge.fury.io/py/znslice)\n[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnSlice/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnSlice?branch=main)\n[![zincware](https://img.shields.io/badge/Powered%20by-zincware-darkcyan)](https://github.com/zincware)\n# ZnSlice\n\nA lightweight library  (without external dependencies) for:\n- advanced slicing.\n- cache `__getitem__(self, item)`\n- lazy load `__getitem__(self, item)`\n\n# Installation\n\n```bash\npip install znslice\n```\n\n# Usage\n\n## Advanced Slicing and Cache\nConvert List to `znslice.LazySequence` to allow advanced slicing.\n```python\nimport znslice\n\nlst = znslice.LazySequence.from_obj([1, 2, 3], indices=[0, 2])\nprint(lst[[0, 1]].tolist())  # [1, 3]\n```\n\n\n```python\nimport znslice\nimport collections.abc\n\nclass MapList(collections.abc.Sequence):\n    def __init__(self, data, func):\n        self.data = data\n        self.func = func\n    \n    @znslice.znslice\n    def __getitem__(self, item: int):\n        print(f\"Loading item = {item}\")\n        return self.func(self.data[item])\n    \n    def __len__(self):\n        return len(self.data)\n\ndata = MapList([0, 1, 2, 3, 4], lambda x: x ** 2)\n\nassert data[0] == 0\nassert data[[1, 2, 3]] == [1, 4, 9]\n# calling data[:] will now only compute data[4] and load the remaining data from cache\nassert data[:] == [0, 1, 4, 9, 16]\n```\n\n## Lazy Database Loading\n\nYou can use `znslice` to lazy load data from a database. This is useful if you have a large database and only want to load a small subset of the data.\n\nIn the following we will use the `ase` package to generate `Atoms` objects stored in a database and load them lazily.\n\n```python \nimport ase.io\nimport ase.db\nimport znslice\nimport tqdm\nimport random\n\n# create a database\nwith ase.db.connect(\"data.db\", append=False) as db:\n    for _ in range(10):\n        atoms = ase.Atoms('CO', positions=[(0, 0, 0), (0, 0, random.random())])\n        db.write(atoms, group=\"data\")\n        \n# load the database lazily\nclass ReadASEDB:\n    def __init__(self, file):\n        self.file = file\n    \n    @znslice.znslice(\n        advanced_slicing=True, # this getitem supports advanced slicingn\n        lazy=True # we want to lazy load the data\n    )\n    def __getitem__(self, item):\n        data = []\n        with ase.db.connect(self.file) as database:\n            if isinstance(item, int):\n                print(f\"get {item = }\")\n                return database[item + 1].toatoms()\n            for idx in tqdm.tqdm(item):\n                data.append(database[idx + 1].toatoms())\n        return data\n            \n    def __len__(self):\n        with ase.db.connect(self.file) as db:\n            return len(db)\n\ndb = ReadASEDB(\"data.db\")\n\ndata = db[::2] # LazySequence([<__main__.ReadASEDB>], [[0, 2, 4, 6, 8]])\ndata.tolist() # list[ase.Atoms] \n\n# supports addition, advanced slicing, etc.\ndata = db[::2] + db[1::2]\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Cache, advanced slicing and lazy loading for __getitem__",
    "version": "0.1.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf361b0bf380415cc07e53d86bd969a7cbd226ec6f73a535191f67396c68a1ae",
                "md5": "d3b12557d0727dadc567a0205adb3ac9",
                "sha256": "f07912398075d3967a7cc7ab1c00fcac1ca9a4389af8274a140dd00b04065b49"
            },
            "downloads": -1,
            "filename": "znslice-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d3b12557d0727dadc567a0205adb3ac9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 5417,
            "upload_time": "2023-02-06T13:16:11",
            "upload_time_iso_8601": "2023-02-06T13:16:11.015159Z",
            "url": "https://files.pythonhosted.org/packages/cf/36/1b0bf380415cc07e53d86bd969a7cbd226ec6f73a535191f67396c68a1ae/znslice-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ceb8c657c1317ec4143dc292231a8b2dd53b2d14e72477d0f7439978d26c3d0f",
                "md5": "3ef3405d920da133a19b1a2a6499bc25",
                "sha256": "c2edb2fe1cdf7ac52f42bd0cf63c74d25c8dbdce0ded3a9d245135641d175255"
            },
            "downloads": -1,
            "filename": "znslice-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "3ef3405d920da133a19b1a2a6499bc25",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5435,
            "upload_time": "2023-02-06T13:16:12",
            "upload_time_iso_8601": "2023-02-06T13:16:12.662968Z",
            "url": "https://files.pythonhosted.org/packages/ce/b8/c657c1317ec4143dc292231a8b2dd53b2d14e72477d0f7439978d26c3d0f/znslice-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-06 13:16:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "znslice"
}
        
Elapsed time: 0.05217s