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

hidden attributes #1091

Closed
wants to merge 17 commits into from
15 changes: 15 additions & 0 deletions datajoint/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import pyparsing as pp
import logging
from hashlib import sha1
from .errors import DataJointError, _support_filepath_types, FILEPATH_FEATURE_SWITCH
from .attribute_adapter import get_adapter
from .condition import translate_attribute
Expand Down Expand Up @@ -309,6 +310,20 @@ def declare(full_table_name, definition, context):
external_stores,
) = prepare_declare(definition, context)

metadata_attr_sql = [
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
]
attribute_sql.extend(
[
attr.format(
full_table_name=sha1(
full_table_name.replace("`", "").encode("utf-8")
).hexdigest()
)
for attr in metadata_attr_sql
]
)

if not primary_key:
raise DataJointError("Table must have a primary key")

Expand Down
4 changes: 3 additions & 1 deletion datajoint/heading.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
is_attachment=False,
is_filepath=False,
is_external=False,
is_hidden=False,
adapter=None,
store=None,
unsupported=False,
Expand Down Expand Up @@ -120,7 +121,7 @@ def table_status(self):
def attributes(self):
if self._attributes is None:
self._init_from_database() # lazy loading from database
return self._attributes
return {k: v for k, v in self._attributes.items() if not v.is_hidden}
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: this changes from direct access to the _attributes dictionary to a copy. This means that the following will raise AssertionError as of this PR, but not on master:

orig_value = my_table.heading.attributes['my_attr']
my_table.heading.attributes['my_attr'] = "foo"
assert my_table.heading.attributes['my_attr'] == "foo"
assert my_table.heading.attributes['my_attr'] != orig_value


@property
def names(self):
Expand Down Expand Up @@ -298,6 +299,7 @@ def _init_from_database(self):
store=None,
is_external=False,
attribute_expression=None,
is_hidden=attr["name"].startswith("_"),
)

if any(TYPE_PATTERN[t].match(attr["type"]) for t in ("INTEGER", "FLOAT")):
Expand Down
2 changes: 1 addition & 1 deletion tests_old/test_blob_matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def insert_blobs():

schema.connection.query(
"""
INSERT INTO {table_name} VALUES
INSERT INTO {table_name} (`id`, `comment`, `blob`) VALUES
(1,'simple string',0x6D596D00410200000000000000010000000000000010000000000000000400000000000000630068006100720061006300740065007200200073007400720069006E006700),
(2,'1D vector',0x6D596D0041020000000000000001000000000000000C000000000000000600000000000000000000000000F03F00000000000030400000000000003F4000000000000047400000000000804E4000000000000053400000000000C056400000000000805A400000000000405E4000000000000061400000000000E062400000000000C06440),
(3,'string array',0x6D596D00430200000000000000010000000000000002000000000000002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E00670031002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E0067003200),
Expand Down
15 changes: 15 additions & 0 deletions tests_old/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,18 @@ class WithSuchALongPartNameThatItCrashesMySQL(dj.Part):
definition = """
-> (master)
"""

@staticmethod
def test_hidden_attributes():
assert (
list(Experiment().heading._attributes.keys())[-1].split("_")[2]
== "timestamp"
)
assert (
len([a for a in Experiment().heading._attributes.values() if a.is_hidden])
!= 0
)
assert (
len([a for a in Experiment().heading.attributes.values() if a.is_hidden])
== 0
)