Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
The opencf-core
package provides a robust framework for handling file conversion tasks in Python. It offers a set of classes and utilities designed to simplify the process of reading from and writing to different file formats efficiently.
When using the opencf-core
, you can adopt different strategies for file conversion based on your specific requirements:
In this approach, conversion is achieved without utilizing a dedicated writer. The reader module parses the input files into a list of objects. Subsequently, the _convert
method orchestrates the writing process into a file or folder. This method is suitable for scenarios where direct manipulation of data structures suffices for conversion.
Conversely, indirect conversion employs a converter that supports a dedicated writer. Here, the convert
function's primary role is to transform the parsed list of objects into a format compatible with the writer. The actual conversion process may be executed by the writer, leveraging its capabilities. For instance, converting images to videos involves parsing images into a list of Pillow objects, which are then reformatted into a numpy array. This array, encapsulating frame dimensions and color channels, serves as input for the video writer.
The file conversion process can be dissected into three distinct instances:
Reader: Handles input-output (IO) operations, transforming files into objects. Readers are implementations of the abstract class Reader
present in io_handler.py
.
Converter: Facilitates object-to-object conversion, acting as an intermediary for data transformation. Converters are implementations of the abstract class BaseConverter
present in base_converter.py
.
Writer (Optional): Reverses the IO process, converting objects back into files. Writers are implementations of the abstract class Writer
present in io_handler.py
.
Reader
and Writer
.BaseConverter
abstract class.pip install opencf-core
The opencf-core
package can be used independently to build custom file conversion utilities or integrated into larger projects for handling file format transformations efficiently.
from opencf_core.io_handler import CsvToListReader, ListToCsvWriter
from opencf_core.base_converter import BaseConverter, ResolvedInputFile
from opencf_core.filetypes import FileType
class CSVToJSONConverter(BaseConverter):
file_reader = CsvToListReader()
file_writer = DictToJsonWriter()
@classmethod
def _get_supported_input_type(cls) -> FileType:
return FileType.CSV
@classmethod
def _get_supported_output_type(cls) -> FileType:
return FileType.JSON
def _convert(self, input_path: Path, output_file: Path):
# Implement conversion logic from CSV to JSON
pass
# Usage
input_file_path = "input.csv"
output_file_path = "output.json"
input_file = ResolvedInputFile(input_file_path, is_dir=False, should_exist=True)
output_file = ResolvedInputFile(output_file_path, is_dir=False, should_exist=False, add_suffix=True)
converter = CSVToJSONConverter(input_file, output_file)
converter.convert()
The examples
folder in this repository contains practical demonstrations of how to use the opencf-core
package for file conversion tasks. Currently, it includes the following examples:
simple_converter.py: Demonstrates a basic file converter that converts Excel (XLSX) files to CSV format. It utilizes the XLXSToCSVConverter
class defined within the opencf-core
package to perform the conversion.
cli_app_example.py: Illustrates how to build a command-line interface (CLI) application using the ConverterApp
class from the opencf-core.converter_app
module. This CLI app allows users to specify input and output files, as well as input and output file types, for performing file conversions.
These examples serve as practical demonstrations of how to leverage the capabilities of the opencf-core
package in real-world scenarios. Users can refer to these examples for guidance on building their own file conversion utilities or integrating file conversion functionality into existing projects.
You can have a more practical insight by reading the support associated to the examples
Reader
and Writer
implementations.ImageOpenCVReader
utilizes both numpy and OpenCV, the VideoWriter
can leverage the same dependencies.Contributions to the opencf-core
package are welcome! Feel free to submit bug reports, feature requests, or pull requests via the GitHub repository.
Please note that while the opencf-core
package aims to provide a versatile framework for file conversion tasks, it may not cover every possible use case or handle all edge cases. Users are encouraged to review and customize the code according to their specific requirements.
FAQs
A robust framework for handling file conversion tasks in Python
We found that opencf-core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.