Skip to content

Commit 0a49595

Browse files
Merge pull request #1180 from datajoint/hidden-1091-continued
hidden attributes continued (#1091)
2 parents e14544d + e0133d6 commit 0a49595

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 0.14.3 -- TBD
44
- 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)
5+
- Added - Ability to set hidden attributes on a table - PR [#1091](https://github.com/datajoint/datajoint-python/pull/1091)
56

67
### 0.14.2 -- Aug 19, 2024
78
- Added - Migrate nosetests to pytest - PR [#1142](https://github.com/datajoint/datajoint-python/pull/1142)

datajoint/declare.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import pyparsing as pp
88
import logging
9+
from hashlib import sha1
910
from .errors import DataJointError, _support_filepath_types, FILEPATH_FEATURE_SWITCH
1011
from .attribute_adapter import get_adapter
1112
from .condition import translate_attribute
@@ -310,6 +311,18 @@ def declare(full_table_name, definition, context):
310311
external_stores,
311312
) = prepare_declare(definition, context)
312313

314+
metadata_attr_sql = [
315+
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
316+
]
317+
attribute_sql.extend(
318+
attr.format(
319+
full_table_name=sha1(
320+
full_table_name.replace("`", "").encode("utf-8")
321+
).hexdigest()
322+
)
323+
for attr in metadata_attr_sql
324+
)
325+
313326
if not primary_key:
314327
raise DataJointError("Table must have a primary key")
315328

datajoint/heading.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
is_attachment=False,
3434
is_filepath=False,
3535
is_external=False,
36+
is_hidden=False,
3637
adapter=None,
3738
store=None,
3839
unsupported=False,
@@ -120,7 +121,7 @@ def table_status(self):
120121
def attributes(self):
121122
if self._attributes is None:
122123
self._init_from_database() # lazy loading from database
123-
return self._attributes
124+
return {k: v for k, v in self._attributes.items() if not v.is_hidden}
124125

125126
@property
126127
def names(self):
@@ -300,6 +301,7 @@ def _init_from_database(self):
300301
store=None,
301302
is_external=False,
302303
attribute_expression=None,
304+
is_hidden=attr["name"].startswith("_"),
303305
)
304306

305307
if any(TYPE_PATTERN[t].match(attr["type"]) for t in ("INTEGER", "FLOAT")):

tests/test_blob_matlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def insert_blobs(schema):
3434

3535
schema.connection.query(
3636
"""
37-
INSERT INTO {table_name} VALUES
37+
INSERT INTO {table_name} (`id`, `comment`, `blob`) VALUES
3838
(1,'simple string',0x6D596D00410200000000000000010000000000000010000000000000000400000000000000630068006100720061006300740065007200200073007400720069006E006700),
3939
(2,'1D vector',0x6D596D0041020000000000000001000000000000000C000000000000000600000000000000000000000000F03F00000000000030400000000000003F4000000000000047400000000000804E4000000000000053400000000000C056400000000000805A400000000000405E4000000000000061400000000000E062400000000000C06440),
4040
(3,'string array',0x6D596D00430200000000000000010000000000000002000000000000002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E00670031002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E0067003200),

tests/test_declare.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,11 @@ class Table_With_Underscores(dj.Manual):
360360
dj.DataJointError, match="must be alphanumeric in CamelCase"
361361
) as e:
362362
schema_any(Table_With_Underscores)
363+
364+
365+
def test_hidden_attributes(schema_any):
366+
assert (
367+
list(Experiment().heading._attributes.keys())[-1].split("_")[2] == "timestamp"
368+
)
369+
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
370+
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())

tests_old/test_blob_matlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def insert_blobs():
4040

4141
schema.connection.query(
4242
"""
43-
INSERT INTO {table_name} VALUES
43+
INSERT INTO {table_name} (`id`, `comment`, `blob`) VALUES
4444
(1,'simple string',0x6D596D00410200000000000000010000000000000010000000000000000400000000000000630068006100720061006300740065007200200073007400720069006E006700),
4545
(2,'1D vector',0x6D596D0041020000000000000001000000000000000C000000000000000600000000000000000000000000F03F00000000000030400000000000003F4000000000000047400000000000804E4000000000000053400000000000C056400000000000805A400000000000405E4000000000000061400000000000E062400000000000C06440),
4646
(3,'string array',0x6D596D00430200000000000000010000000000000002000000000000002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E00670031002F0000000000000041020000000000000001000000000000000700000000000000040000000000000073007400720069006E0067003200),

tests_old/test_declare.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,12 @@ class WithSuchALongPartNameThatItCrashesMySQL(dj.Part):
341341
definition = """
342342
-> (master)
343343
"""
344+
345+
@staticmethod
346+
def test_hidden_attributes():
347+
assert (
348+
list(Experiment().heading._attributes.keys())[-1].split("_")[2]
349+
== "timestamp"
350+
)
351+
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
352+
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())

0 commit comments

Comments
 (0)