Skip to content
Open
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
48 changes: 24 additions & 24 deletions neuroshare/File.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@

from .Library import Library
from .Entity import EntityType
from .EventEntity import EventEntity
from .AnalogEntity import AnalogEntity
from .SegmentEntity import SegmentEntity
from .NeuralEntity import NeuralEntity


class EntityProxy(object):
def __init__(self, nsfile):
def __init__(self, nsfile=None):
self._nsfile = nsfile

def __getitem__(self, key):
def __getitem__(self, key=None):
return self._nsfile.get_entity(key)

def __iter__(self):
Expand All @@ -30,8 +28,7 @@ class File(object):
Individual entities can be opened via the :func:`get_entity` function or
the :func:`entities` property. NB: The first entity index is **0**
"""

def __init__(self, filename, library=None):
def __init__(self, filename=None, library=None):
self._handle = None
self._filename = filename
if not library:
Expand All @@ -40,7 +37,7 @@ def __init__(self, filename, library=None):
self._lib = library
(handle, info) = self._lib._open_file(filename)
self._handle = handle
self._info = info
self._info = info
self._eproxy = EntityProxy(self)

def __del__(self):
Expand Down Expand Up @@ -96,7 +93,6 @@ def ctime(self):
Returns a :py:class:`datetime.datetime` object.
"""
from datetime import datetime

year = self._info['Time_Year']
month = self._info['Time_Month']
day = self._info['Time_Day']
Expand All @@ -113,23 +109,27 @@ def get_entity(self, entity_id):
"""Open the entity at the given index."""
info = self._lib._get_entity_info(self, entity_id)
entity_type = info['EntityType']

if entity_type == EntityType.Event:
entity = EventEntity(self, entity_id, info)
elif entity_type == EntityType.Analog:
entity = AnalogEntity(self, entity_id, info)
elif entity_type == EntityType.Segment:
entity = SegmentEntity(self, entity_id, info)
elif entity_type == EntityType.Neural:
entity = NeuralEntity(self, entity_id, info)
else:

try:
if entity_type == EntityType.Event:
entity = EventEntity(self, entity_id, info)
elif entity_type == EntityType.Analog:
entity = AnalogEntity(self, entity_id, info)
elif entity_type == EntityType.Segment:
entity = SegmentEntity(self, entity_id, info)
elif entity_type == EntityType.Neural:
entity = NeuralEntity(self, entity_id, info)
except ValueError:
print("Entity id not correct.")
return None # should not happen, throw exception?

return entity

def list_entities(self, start=0, end=-1):
"""List all entities. The range can be limited
via the ``start`` and ``end`` parameters."""
"""
List all entities. The range can be limited
via the ``start`` and ``end`` parameters.
"""
if end == -1:
end = self.entity_count

Expand All @@ -138,10 +138,10 @@ def list_entities(self, start=0, end=-1):

@property
def entities(self):
"""Property that returns a proxy object to allow the opening of
entities in a via indexing, ie::

entity = datafile.entities[10] #retrieve the entity with at 10
"""
Property that returns a proxy object to allow the opening of
entities in a via indexing, ie
entity = datafile.entities[10] #retrieve the entity at 10
"""
return self._eproxy

Expand Down