FileRField
Abstract base class for Resource fields that persist data to files.
FileRField provides a framework for RFields that store their data in files on disk. The field stores a file path reference, and subclasses implement custom logic for reading from and writing to these files.
When a Resource is saved:
- The dump_to_file method is called with the field value and file path
- The file path is stored in the KV_STORE for later retrieval
When a Resource is loaded:
- The file path is retrieved from storage
- The load_from_file method is called to read and deserialize the data
This is useful for: - Large data that shouldn't be stored in database/KV_STORE directly - Binary data (images, audio, video, archives) - Data formats that benefit from file-based storage (HDF5, Parquet, etc.) - Custom serialization formats
Note: - Fields are never included in dict views (include_in_dict_view=False) - Storage is always KV_STORE (stores file path reference) - Subclasses must implement both load_from_file and dump_to_file methods
Example: ```python import pickle
class PickleRField(FileRField):
'''RField that stores data as pickle files'''
def load_from_file(self, file_path: str) -> Any:
with open(file_path, 'rb') as f:
return pickle.load(f)
def dump_to_file(self, r_field_value: Any, file_path: str) -> None:
with open(file_path, 'wb') as f:
pickle.dump(r_field_value, f)
class MyResource(Resource):
large_data = PickleRField()
```
include_in_dict_view: boolstorage: RFieldStorageInitialize a FileRField with automatic file-based persistence.
The field is configured to store file path references in KV_STORE and is excluded from dict views since the actual data is in files.
AnyDeserialize field value by loading data from the file path.
This method is marked as final and cannot be overridden. It delegates to
the load_from_file method which must be implemented by subclasses.
strAnySerialize and save the field value to a file.
This abstract method must be implemented by subclasses to define how data is serialized and written to the file. It is called automatically when the Resource is saved.
The data MUST be written to the exact file path provided. The directory containing the file is guaranteed to exist.
AnystrGet the default value for this field.
If the default value is a Type or Callable, it will be called to generate a new default value. Otherwise, the default value is returned directly.
This ensures that mutable defaults (like lists or dicts) are not shared between Resource instances.
AnyLoad and deserialize the field value from a file.
This abstract method must be implemented by subclasses to define how data is read and deserialized from the file. It is called automatically when the Resource is loaded.
The file at file_path is guaranteed to exist when this method is called.
strAnySerialize method not used for FileRField.
The actual serialization is handled by dump_to_file which is called
by the resource persistence layer. This method exists for interface
compatibility but should be ignored.
Anystr