Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import numbers
  2. class Matrix(object):
  3. def __init__(self, seq, num_cols=1):
  4. self.m = seq
  5. self.num_cols = num_cols
  6. self.num_rows = len(seq) / num_cols
  7.  
  8. def __getitem__(self, idx):
  9. row, col = idx
  10. if isinstance(row, numbers.Integral) and isinstance(col, numbers.Integral):
  11. return self.m[self.num_cols * row + col]
  12.  
  13. elif isinstance(row, numbers.Integral) and isinstance(col, slice):
  14. start = self.num_cols * row
  15. stop = start + self.num_cols
  16. row_seq = self.m[slice(start, stop, 1)]
  17. return row_seq[col]
  18.  
  19. elif isinstance(row, slice) and isinstance(col, numbers.Integral):
  20. start = col
  21. end = len(self.m) + col - self.num_cols + 1
  22. col_seq = self.m[slice(start, end, self.num_cols)]
  23. return col_seq[row]
  24. else:
  25. raise ValueError('\n\nOnly supports access for slicing like:\nM[1, 2], M[:, 0], M[2, :]\n\n')
  26.  
  27. def __setitem__(self, key, value):
  28. raise NotImplementedError('Setting items not supported')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement