A package for file management operations with abstract base classes.
Project description
Frieren
This library provides an abstract foundation for managing file saving and loading operations. It abstracts metadata, locators for file storage, and operators that handle file operations, enabling an extensible design. In cases such as data analysis, where file I/O is frequent, the library allows users to focus on programming without the hassle of manually setting file paths by determining them dynamically through routing.
This sample README.md
is intended to clarify the purpose, properties, and usage of the classes, making it easier for users to understand 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>>
-e: 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.0.tar.gz
.
File metadata
- Download URL: frieren-0.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7165dfec42fc20b2fffed57eb352ac6be30f2bffdb1f44b40e6ba9119b111ed5 |
|
MD5 | 0a5927dc6df6a07a957287eba8bd3421 |
|
BLAKE2b-256 | 595683b0ccfb7208be69903f0b07620a1d3dc0ac1928f17fb87cb32267744f97 |
File details
Details for the file frieren-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: frieren-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 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 | b7316a4b2bc498d1f50fa55aa65b9ec13b18ec23ee3640811acd8d7a3b961137 |
|
MD5 | 6d38bc2218cbb253d487b7473aeac956 |
|
BLAKE2b-256 | 876c005ab8e8931fd72967cf8bc40d0de3f45dcfa6256830b88b1322f95545b7 |