Skip to content
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
73 changes: 65 additions & 8 deletions nixio/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@
from .data_array import DataArray
from .link_type import LinkType
from six import string_types
from .util import util


class Feature(Entity):
class Feature(object):

def __init__(self, nixparent, h5group):
super(Feature, self).__init__(nixparent, h5group)
util.check_entity_id(h5group.get_attr("entity_id"))
self._h5group = h5group
self._parent = nixparent

@classmethod
def _create_new(cls, nixparent, h5parent, data, link_type):
newentity = super(Feature, cls)._create_new(nixparent, h5parent)
newentity.link_type = link_type
newentity.data = data
return newentity
id_ = util.create_id()
h5group = h5parent.open_group(id_)
h5group.set_attr("entity_id", id_)
newfeature = cls(nixparent, h5group)
newfeature.link_type = link_type
newfeature.data = data
newfeature._h5group.set_attr("created_at",
util.time_to_str(util.now_int()))
newfeature._h5group.set_attr("updated_at",
util.time_to_str(util.now_int()))

return newfeature

@property
def id(self):
Expand All @@ -39,7 +50,8 @@ def link_type(self, lt):
lt = LinkType(lt)
self._h5group.set_attr("link_type", lt.value)
if self._parent._parent._parent.time_auto_update:
self.force_updated_at()
t = util.now_int()
self._h5group.set_attr("updated_at", util.time_to_str(t))

@property
def data(self):
Expand All @@ -59,4 +71,49 @@ def data(self, da):
del self._h5group["data"]
self._h5group.create_link(da, "data")
if self._parent._parent._parent.time_auto_update:
self.force_updated_at()
t = util.now_int()
self._h5group.set_attr("updated_at", util.time_to_str(t))

@property
def created_at(self):
"""
The creation time of the entity. This is a read-only property.
Use `force_created_at` in order to change the creation time.

:rtype: int
"""
return util.str_to_time(self._h5group.get_attr("created_at"))

@property
def updated_at(self):
"""
The time of the last update of the entity. This is a read-only
property. Use `force_updated_at` in order to change the update
time.

:rtype: int
"""
return util.str_to_time(self._h5group.get_attr("updated_at"))

def __eq__(self, other):
"""
Two Entities are considered equal when they have the same id.
"""
if hasattr(other, "id"):
return self.id == other.id
return False

def __hash__(self):
"""
overwriting method __eq__ blocks inheritance of __hash__ in Python 3
hash has to be either explicitly inherited from parent class,
implemented or escaped
"""
return hash(self.id)

def __str__(self):
return "Feature: {{data = {}, link_type = {} }}".\
format(self.data.name, self.link_type)

def __repr__(self):
return self.__str__()
2 changes: 2 additions & 0 deletions nixio/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __getitem__(self, item):
raise ke

def __contains__(self, item):
if isinstance(item, Feature):
item = item.id
if not Container.__contains__(self, item):
# check if it contains a Feature whose data matches 'item'
for feat in self:
Expand Down