Skip to content
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

SchemaView object should be instantiated when needed & not globally. #323

Closed
wants to merge 7 commits into from

Conversation

hrshdhgd
Copy link
Contributor

@hrshdhgd hrshdhgd commented Oct 21, 2022

Fixes #322

This is because it invokes a network call when sssom-py is imported. This is unnecessary.

@hrshdhgd hrshdhgd changed the title Moved sssom_schema iports to a new schema.py Moved sssom_schema imports and objects to a new schema.py Oct 21, 2022
sssom/constants.py Outdated Show resolved Hide resolved
@hrshdhgd hrshdhgd changed the title Moved sssom_schema imports and objects to a new schema.py SchemaView object should be instantiated when needed & not globally. Nov 3, 2022
sssom/cli.py Outdated Show resolved Hide resolved
sssom/parsers.py Outdated Show resolved Hide resolved
sssom/util.py Outdated Show resolved Hide resolved
sssom/util.py Outdated Show resolved Hide resolved
tests/test_sort.py Outdated Show resolved Hide resolved
@@ -628,7 +627,7 @@ def decorator(f):
@main.command()
@input_argument
@output_option
@dynamically_generate_sssom_options(MAPPING_SLOTS)
@dynamically_generate_sssom_options(SSSOMSchemaView().mapping_slots)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if this will defeat the purpose of the refactoring. I should have thought of this before.

I am wondering now if we need to find a way to skip network access in SchemaView()?


entity_reference = "EntityReference"
yaml = pkg_resources.resource_filename("sssom_schema", "schema/sssom_schema.yaml")
view = SchemaView(yaml)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure, but you may have to use init() to avoid this code being run whenever the class is referenced:

https://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python

But not 100% sure.

sssom/util.py Outdated
@@ -1040,6 +1036,18 @@ class NoCURIEException(ValueError):


CURIE_RE = re.compile(r"[A-Za-z0-9_.]+[:][A-Za-z0-9_]")
schema_view_object = SSSOMSchemaView()
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is again in the global context I think.. wont this be invoked any time anyone imports anything from utils package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will have to repeat this code in 4 different lines in the same file. Seems messy.

@@ -284,7 +284,7 @@ def to_rdf_graph(msdf: MappingSetDataFrame) -> Graph:
# os.remove("sssom.ttl") # remove the intermediate file.
graph = rdflib_dumper.as_rdf_graph(
element=doc.mapping_set,
schemaview=SchemaView(SCHEMA_YAML),
schemaview=SchemaView(SSSOMSchemaView().yaml),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit weird... :D

def view(self) -> SchemaView:
"""Return SchemaView object."""
if self._view is None:
self._view = SchemaView(self.yaml)
Copy link
Collaborator

Choose a reason for hiding this comment

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

smart, so, whenever self.view is accessed as a variable, this function is executed, effectively deferring the SchemaView() instantiation to the first time its executed? Cool!

sssom/util.py Show resolved Hide resolved

def test_slots(self) -> None:
"""Test slots."""
self.assertEqual(len(self.sv.mapping_slots), 39)
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably better test self.assertTrue(len(self.sv.mapping_slots)>=39) because changes to the metadata model will break this test every time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, changed!

@@ -52,7 +52,7 @@ description = Run the flake8 code quality checker.
[testenv:mypy]
deps = mypy
skip_install = true
commands = mypy --install-types --non-interactive --ignore-missing-imports sssom/ setup.py
commands = mypy --install-types --non-interactive --ignore-missing-imports --implicit-optional sssom/ setup.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does that do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In its absence mypy throws a bunch of errors.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nothing you think is serious enough to be fixed though? I will trust you on this, just doing due diligence

@@ -19,6 +19,8 @@ def test_sort(self):
"""Test sorting of columns."""
new_df = sort_df_rows_columns(self.msdf.df)
column_sequence = [
col for col in SCHEMA_DICT["slots"].keys() if col in new_df.columns
col
for col in SSSOMSchemaView().dict["slots"].keys()
Copy link
Collaborator

Choose a reason for hiding this comment

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

so weird SchemaView() has no "get_slots()".

@hrshdhgd
Copy link
Contributor Author

See #324

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.

constants.py should not invoke schemaview
2 participants