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

Fixed slicing in Python interface #202

Merged
merged 3 commits into from
Dec 6, 2016
Merged
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions python/_dynet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,16 @@ cdef class Expression: #{{{
return "expression %s/%s" % (<int>self.vindex, self.cg_version)

# __getitem__ and __getslice__ in one for python 3 compatibility
def __getitem__(self, object index):
if isinstance(index, int):
return pick(self, index)

return pickrange(self, index[0], index[1])
def __getitem__(self, index):
assert isinstance(index, (int, slice))
if isinstance(index, int):
return pick(self, index)
else:
if index.start is None or index.stop is None:
raise ValueError("Default start and stop indices not yet supported.")
if index.step is not None:
raise ValueError("Step sizes not yet supported.")
return pickrange(self, index.start, index.stop)

cpdef scalar_value(self, recalculate=False):
if self.cg_version != _cg._cg_version: raise RuntimeError("Stale Expression (created before renewing the Computation Graph).")
Expand Down