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 continued (#1091) #1180

Merged
merged 24 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 0.14.3 -- TBD
- Fixed - Added encapsulating double quotes to comply with [DOT language](https://graphviz.org/doc/info/lang.html) - PR [#1177](https://github.com/datajoint/datajoint-python/pull/1177)
- Added - Ability to set hidden attributes on a table - PR [#1091](https://github.com/datajoint/datajoint-python/pull/1091)

### 0.14.2 -- Aug 19, 2024
- Added - Migrate nosetests to pytest - PR [#1142](https://github.com/datajoint/datajoint-python/pull/1142)
Expand Down
13 changes: 13 additions & 0 deletions datajoint/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -310,6 +311,18 @@ 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}

@property
def names(self):
Expand Down Expand Up @@ -300,6 +301,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/test_blob_matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def insert_blobs(schema):

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
8 changes: 8 additions & 0 deletions tests/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,11 @@ class Table_With_Underscores(dj.Manual):
dj.DataJointError, match="must be alphanumeric in CamelCase"
) as e:
schema_any(Table_With_Underscores)


def test_hidden_attributes(schema_any):
assert (
list(Experiment().heading._attributes.keys())[-1].split("_")[2] == "timestamp"
)
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())
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
9 changes: 9 additions & 0 deletions tests_old/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,12 @@ class WithSuchALongPartNameThatItCrashesMySQL(dj.Part):
definition = """
-> (master)
"""

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