Skip to content

Commit d418a9a

Browse files
author
Benjamin Moody
committed
wfdb.io._coreio: new module.
This defines the function _open_file, which opens an input data file as a random-access file object. This module is intended to eventually replace the "streaming I/O" functions in the wfdb.io.download module (_remote_file_size, _stream_header, _stream_dat, and _stream_annotation.) Some notes on the implementation: - Contrary to many existing functions, I've deliberately made pn_dir the first argument rather than the second, since putting the prefix first seems more natural. - There's no dir_name argument; instead, callers should include the local directory name as part of file_name. I consider it a bug that some functions have a separate dir_name argument that is ignored when pn_dir is set. - This function does not do automatic version number resolution for PhysioNet projects. That's something the caller (still) needs to do and should be handled elsewhere.
1 parent 2a6ce72 commit d418a9a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

wfdb/io/_coreio.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from wfdb.io import _url
2+
3+
4+
def _open_file(
5+
pn_dir,
6+
file_name,
7+
mode="r",
8+
*,
9+
buffering=-1,
10+
encoding=None,
11+
errors=None,
12+
newline=None,
13+
check_access=False,
14+
):
15+
"""
16+
Open a data file as a random-access file object.
17+
18+
See the documentation of `open` and `wfdb.io._url.openurl` for details
19+
about the `mode`, `buffering`, `encoding`, `errors`, and `newline`
20+
parameters.
21+
22+
Parameters
23+
----------
24+
pn_dir : str or None
25+
The PhysioNet database directory where the file is stored, or None
26+
if file_name is a local path.
27+
file_name : str
28+
The name of the file, either as a local filesystem path (if
29+
`pn_dir` is None) or a URL path (if `pn_dir` is a string.)
30+
mode : str, optional
31+
The standard I/O mode for the file ("r" by default). If `pn_dir`
32+
is not None, this must be "r", "rt", or "rb".
33+
buffering : int, optional
34+
Buffering policy.
35+
encoding : str, optional
36+
Name of character encoding used in text mode.
37+
errors : str, optional
38+
Error handling strategy used in text mode.
39+
newline : str, optional
40+
Newline translation mode used in text mode.
41+
check_access : bool, optional
42+
If true, raise an exception immediately if the file does not
43+
exist or is not accessible.
44+
45+
"""
46+
if pn_dir is None:
47+
return open(
48+
file_name,
49+
mode,
50+
buffering=buffering,
51+
encoding=encoding,
52+
errors=errors,
53+
newline=newline,
54+
)
55+
else:
56+
url = posixpath.join(config.db_index_url, pn_dir, file_name)
57+
return _url.openurl(
58+
url,
59+
mode,
60+
buffering=buffering,
61+
encoding=encoding,
62+
errors=errors,
63+
newline=newline,
64+
check_access=check_access,
65+
)

0 commit comments

Comments
 (0)