A package for file management operations with abstract base classes.
Project description
Frieren
This library provides a simple and flexible foundation for managing file saving and loading. It allows users to focus on coding without worrying about file paths. File locations are determined automatically based on the context, so you don't need to set paths manually.
The core of the library is built around three main components:
Metadata: Keeps track of additional information related to the files. Locators: Dynamically manage where files are stored. Operators: Handle the actual file operations like saving and loading. The design is extensible, allowing users to easily adapt it to their needs. This is especially useful in fields like data analysis, where frequent file input/output (I/O) is common. The library "freezes" (from the German word frieren, meaning "to freeze") the complexity of managing file paths, offering a structured and consistent approach. With this system, your file handling becomes as smooth and reliable as frozen operations.
The goal of this README.md is to provide a clear overview of the classes and their usage. It should help users quickly grasp the purpose and properties of the library, making file management easier and more efficient.
install
pip install frieren
Note: Since this module is still under development, we recommend specifying a version when installing to ensure compatibility. For example:
pip install frieren==0.1.0
By fixing the version, you can avoid unexpected issues that may arise from updates or changes in the library.
Structure
BaseRepository
An abstract repository class that serves as the foundation. It defines the basic structure for file operations without being dependent on any specific repository type.
Metadata
A class that represents the metadata of a file. It holds essential information for generating a filename. The following properties are available:
stem
: The base string of the filenamesuffix
: The file extensionprefix
: A prefix to be added before the filenamefilename()
: A method to generate the full filename
BaseLocator
An abstract class for locators that determine where files will be saved. Specific locator classes inherit from this class and provide the actual save destination.
save_dir()
: A property that returns the directory for savingget_path(metadata: Metadata)
: A method that generates the file path based on metadata
BaseOperator
An abstract class that manages file saving and loading operations. Specific operator classes inherit from this class and provide implementations for file operations.
LocatorClass
: The class of the locator to be usedmeta
: The metadata to be usedget_path(locator: LocatorT)
: A method to retrieve the file path based on the locatorsave_file(obj: T, path: Path)
: A method to save an object to a fileload_file(path: Path)
: A method to load an object from a filewrite(obj: T, locator: LocatorT)
: A method to save an object using the locatorread(locator: LocatorT)
: A method to load an object using the locator
UML
classDiagram
class BaseRepository {
<<abstract>>
}
class Metadata {
+stem: str
+suffix: str
+prefix: str
+filename() : str
}
class BaseLocator {
<<abstract>>
-repository: RepositoryT
+save_dir() Path
+get_path(metadata: MetadataT) Path
}
class BaseOperator {
<<abstract>>
+LocatorClass: Type[LocatorT]
+meta: MetadataT
+get_path(locator: LocatorT) Path
+save_file(obj: T, path: Path)
+load_file(path: Path) T
+write(obj: T, locator: LocatorT)
+read(locator: LocatorT) T
}
BaseOperator o-- BaseLocator
BaseOperator o-- Metadata
BaseLocator ..> Metadata : depends on
BaseLocator o-- BaseRepository
Usage Example
Below is a simple example of saving and loading a file using BaseOperator
and BaseLocator
.
from pathlib import Path
from typing import Type
from dataclasses import dataclass
from frieren import BaseOperator, BaseLocator, Metadata
@dataclass
class SimpleMetadata(Metadata):
stem: str
suffix: str
prefix: str = ""
@property
def filename(self) -> str:
return self.prefix + self.stem + self.suffix
class SimpleLocator(BaseLocator):
@property
def save_dir(self) -> Path:
return Path("data")
def get_path(self, metadata: Metadata) -> Path:
return self.save_dir / metadata.filename
class SimpleOperator(BaseOperator):
LocatorClass: Type[BaseLocator] = SimpleLocator
def save_file(self, obj, path: Path):
with open(path, "w") as f:
f.write(obj)
def load_file(self, path: Path):
with open(path, "r") as f:
return f.read()
# Usage example
metadata = SimpleMetadata(stem="example", suffix=".txt")
locator = SimpleLocator(e=None)
operator = SimpleOperator(metadata=metadata)
data = "Hello, world!"
operator.write(data, locator)
loaded_data = operator.read(locator)
print(loaded_data) # Output: Hello, world!
License
This project is distributed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file frieren-0.1.1.tar.gz
.
File metadata
- Download URL: frieren-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b8f0d1a1c5e64042bdae9461b8139b6ecba799689acb5bd422fb71fab1518e8 |
|
MD5 | 178b482dd1e867eb227cf6063da34656 |
|
BLAKE2b-256 | 59cbe5ce9c1253ea39fcfbcbbe3ae9c501f591efcc9a09e8ad3c19d8aebeddb3 |
File details
Details for the file frieren-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: frieren-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7da151fa1150cc672a03a69570ce68e19a5e808543ac83da08a75487319e9c7 |
|
MD5 | 548d3926f627b2c627bbde067366720b |
|
BLAKE2b-256 | 1d61e432161bc1d2fce04607c6fa8def73a90f6567c1416681af9953c538ec2a |