Skip to content

Allow users to provide table names as pathlib.Path objects - #295

Open
rtobar wants to merge 1 commit into
casacore:masterfrom
rtobar:tablename-as-path
Open

Allow users to provide table names as pathlib.Path objects#295
rtobar wants to merge 1 commit into
casacore:masterfrom
rtobar:tablename-as-path

Conversation

@rtobar

@rtobar rtobar commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

In a few codebases we use pathlib.Path objects for denoting paths in the filesystem. However python-casacore stringly requires a str for its tablename arguments throughout. This PR adds support for providing table names as pathlib.Path objects.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>

@gmloose gmloose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have some remarks. See above.

Comment thread casacore/tables/table.py
_columnnames=[], _datatypes=[],
_oper=0, _delete=False):
"""Open or create a table."""
import pathlib

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would prefer to have imports at the top of the file, and not inside functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Me too, but I was preferred to follow the precedent already established by many other functions in this file, where imports are done within each function.

Comment on lines +54 to +56
elif hasattr(name, "__iter__"):
return [_do_remove_prefix(nm) for nm in name]
return name

@gmloose gmloose Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure why this is needed; I don't see a direct connection with the support for pathlib.Path. Having said that, IMHO, the pythonic way to do this is:

try:
    return [_do_remove_prefix(nm) for nm in name]
except TypeError:
    return name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is needed so other utility functions like tableutil.tabledelete can also take a Path object (otherwise they'd fail while trying to iterate over a Path object, which isn't supported).

I did think about having something like what you suggest, but the try/except block could potentially catch TypeErrors that are raised within _do_remove_prefix, so that seemed slightly risky. Granted, that shouldn't happen at the moment, but it would be a hidden dependency between the two functions.

Alternatively, I could also add a more specific isinstance(name, Path) and go with that, since that's the more direct behaviour that is being sought.

Comment thread tests/test_table.py
path = pathlib.Path("ttable.py_tmp.tab1")
t = table(path, maketabdesc([c1]), ack=False)
t.close()
tabledelete(path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You may want to use with table(...) as t:, which is exception-safe, and doesn't require you to call t.close().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Happy to do so; again I was following what other functions were doing already

@rtobar

rtobar commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

@gmloose thanks for the review! Please let me know how you think I should proceed with each individual note, hopefully the rationale for each of them is clearer now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants