pygx.io¶
Pluggable IO with unified interfaces.
io
¶
Pluggable IO with unified interfaces.
pygx.io defines abstract FileSystem and File interfaces, plus a
Sequence abstraction for append-only record streams. Module-level helpers
(open, readfile, writefile, mkdirs, open_sequence, ...) dispatch
each path to the registered FileSystem whose prefix matches it, so the same
call works on local paths, in-memory paths (/mem/...) and remote URIs.
Implementations include:
- An in-memory backend (
MemoryFile,MemoryFileSystem,MemorySequence) useful for tests and ephemeral fixtures. - A
fsspecbridge (FsspecFile,FsspecFileSystem) that delegates to any filesystem registered with fsspec (local, GCS, S3, etc.). - A line-based sequence type (
LineSequence,LineSequenceIO) used by JSONL serialization inpg.open_jsonl.
External code can register additional FileSystem implementations and have the rest of PyGX (save/load/open_jsonl) pick them up transparently.
File
¶
Interface for a file.
read
abstractmethod
¶
readline
abstractmethod
¶
write
abstractmethod
¶
seek
abstractmethod
¶
Changes the current position of the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
Offset from the position to a reference point. |
required |
whence
|
Literal[0, 1, 2]
|
The reference point, with 0 meaning the beginning of the file, 1 meaning the current position, or 2 meaning the end of the file. |
0
|
Returns:
| Type | Description |
|---|---|
int
|
The position from the beginning of the file. |
Source code in pygx/io/_file_system.py
tell
abstractmethod
¶
flush
abstractmethod
¶
FileSystem
¶
Interface for a file system.
chmod
abstractmethod
¶
exists
abstractmethod
¶
glob
abstractmethod
¶
listdir
abstractmethod
¶
isdir
abstractmethod
¶
getctime
abstractmethod
¶
getmtime
abstractmethod
¶
mkdir
abstractmethod
¶
mkdirs
abstractmethod
¶
rm
abstractmethod
¶
rename
abstractmethod
¶
rmdir
abstractmethod
¶
rmdirs
abstractmethod
¶
copy
abstractmethod
¶
StdFile
¶
StdFileSystem
¶
MemoryFile
¶
MemoryFileSystem
¶
Bases: FileSystem
The in-memory file system.
Source code in pygx/io/_file_system.py
FsspecFile
¶
FsspecFileSystem
¶
Sequence
¶
Sequence(
perms: int | None = None,
serializer: Callable[[Any], bytes | str] | None = None,
deserializer: Callable[[bytes | str], Any] | None = None,
)
Interface for a sequence of records.
Source code in pygx/io/_sequence.py
add
¶
Appends a record to the sequence.
The record is first converted with the serializer given at open
time, when one was provided; the result must be a str or bytes.
Source code in pygx/io/_sequence.py
close
abstractmethod
¶
SequenceIO
¶
Interface for a record IO system.
MemorySequence
¶
MemorySequence(
path: str | PathLike[str],
mode: str,
records: list[str | bytes],
*,
perms: int | None,
serializer: Callable[[Any], bytes | str] | None,
deserializer: Callable[[bytes | str], Any] | None
)
Bases: Sequence
An in-memory sequence.
Source code in pygx/io/_sequence.py
MemorySequenceIO
¶
Bases: SequenceIO
Memory-based record IO.
Source code in pygx/io/_sequence.py
open
¶
open(
path: str | PathLike[str],
mode: str,
*,
perms: int | None,
serializer: Callable[[Any], bytes | str] | None,
deserializer: Callable[[bytes | str], Any] | None,
**kwargs
) -> Sequence
Opens a reader for a sequence.
Source code in pygx/io/_sequence.py
LineSequence
¶
LineSequence(
path: str | PathLike[str],
mode: str,
perms: int | None,
serializer: Callable[[Any], bytes | str] | None,
deserializer: Callable[[bytes | str], Any] | None,
)
Bases: Sequence
A sequence of records stored as newline-delimited lines in a file.
Source code in pygx/io/_sequence.py
LineSequenceIO
¶
Bases: SequenceIO
Line-based record IO.
open
¶
open(
path: str | PathLike[str],
mode: str,
*,
perms: int | None,
serializer: Callable[[Any], bytes | str] | None,
deserializer: Callable[[bytes | str], Any] | None,
**kwargs
) -> Sequence
Opens a reader for a sequence.
Source code in pygx/io/_sequence.py
add_file_system
¶
add_file_system(prefix: str, fs: FileSystem) -> None
Registers a file system for paths that start with a prefix.
Every pygx.io helper (open, readfile, mkdirs, ...) dispatches to
the registered file system with the longest prefix matching the path.
Source code in pygx/io/_file_system.py
chmod
¶
readfile
¶
readfile(
path: str | PathLike[str],
mode: str = "r",
nonexist_ok: bool = False,
**kwargs
) -> bytes | str | None
Reads content from a file.
Returns None instead of raising FileNotFoundError when nonexist_ok
is True and the path does not exist.
Source code in pygx/io/_file_system.py
writefile
¶
writefile(
path: str | PathLike[str],
content: str | bytes,
*,
mode: str = "w",
perms: int | None = 436,
**kwargs
) -> None
Writes content to a file.
After writing, the file's permission bits are set to perms
(0o664 — world-readable — by default) unless perms is None.
Source code in pygx/io/_file_system.py
rename
¶
rm
¶
copy
¶
path_exists
¶
glob
¶
listdir
¶
Lists all files or sub-directories under a dir.
Source code in pygx/io/_file_system.py
isdir
¶
getctime
¶
getmtime
¶
mkdir
¶
mkdirs
¶
Makes a directory chain.
Source code in pygx/io/_file_system.py
rmdir
¶
rmdirs
¶
add_sequence_io
¶
add_sequence_io(extension: str, sequence_io: SequenceIO) -> None
Registers a record IO system for a file extension.
open_sequence picks the IO system by the path's extension (ignoring
shard suffixes like @10 or -00001-of-00010); paths with an
unregistered extension use line-based records (LineSequenceIO).
Source code in pygx/io/_sequence.py
open_sequence
¶
open_sequence(
path: str | PathLike[str],
mode: str = "r",
*,
perms: int | None = 436,
serializer: Callable[[Any], bytes | str] | None = None,
deserializer: Callable[[bytes | str], Any] | None = None,
make_dirs_if_not_exist: bool = True
) -> Sequence
Opens a sequence of records for reading or writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
The path to the sequence. Its file extension selects the record
IO system (see |
required |
mode
|
str
|
The IO mode: 'r' (read), 'w' (write) or 'a' (append). |
'r'
|
perms
|
int | None
|
(Optional) The file permission bits to apply when writing. |
436
|
serializer
|
Callable[[Any], bytes | str] | None
|
(Optional) A serializer function for converting a structured object to a string or bytes. |
None
|
deserializer
|
Callable[[bytes | str], Any] | None
|
(Optional) A deserializer function for converting a string or bytes to a structured object. |
None
|
make_dirs_if_not_exist
|
bool
|
(Optional) Whether to create the directories if they do not exist. Applicable when opening in write or append mode. |
True
|
Returns:
| Type | Description |
|---|---|
Sequence
|
A sequence for reading or writing. |
Source code in pygx/io/_sequence.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2