Skip to content

PERF: Simplify some of loadtxt's standard converters. #19620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,16 +740,16 @@ def _floatconv(x):
raise # Raise the original exception, which makes more sense.


_CONVERTERS = [
_CONVERTERS = [ # These converters only ever get strs (not bytes) as input.
(np.bool_, lambda x: bool(int(x))),
(np.uint64, np.uint64),
(np.int64, np.int64),
(np.integer, lambda x: int(float(x))),
(np.longdouble, np.longdouble),
(np.floating, _floatconv),
(complex, lambda x: complex(asstr(x).replace('+-', '-'))),
(np.bytes_, asbytes),
(np.unicode_, asunicode),
(complex, lambda x: complex(x.replace('+-', '-'))),
(np.bytes_, methodcaller('encode', 'latin-1')),
(np.unicode_, str),
Comment on lines +751 to +752
Copy link
Member

@eric-wieser eric-wieser Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the case that the input to _CONVERTERS is always a str and never bytes? If so, can you add a comment to _getconv saying that the converters are for parsing data from str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (see also #19609). Added the comment.

]


Expand All @@ -763,7 +763,7 @@ def _getconv(dtype):
for base, conv in _CONVERTERS:
if issubclass(dtype.type, base):
return conv
return asstr
return str


# _loadtxt_flatten_dtype_internal and _loadtxt_pack_items are loadtxt helpers
Expand Down