MCPcopy Create free account
hub / github.com/numpy/numpy / __getitem__

Method __getitem__

numpy/lib/index_tricks.py:326–425  ·  view source on GitHub ↗
(self, key)

Source from the content-addressed store, hash-verified

324 self.ndmin = ndmin
325
326 def __getitem__(self, key):
327 # handle matrix builder syntax
328 if isinstance(key, str):
329 frame = sys._getframe().f_back
330 mymat = matrixlib.bmat(key, frame.f_globals, frame.f_locals)
331 return mymat
332
333 if not isinstance(key, tuple):
334 key = (key,)
335
336 # copy attributes, since they can be overridden in the first argument
337 trans1d = self.trans1d
338 ndmin = self.ndmin
339 matrix = self.matrix
340 axis = self.axis
341
342 objs = []
343 # dtypes or scalars for weak scalar handling in result_type
344 result_type_objs = []
345
346 for k, item in enumerate(key):
347 scalar = False
348 if isinstance(item, slice):
349 step = item.step
350 start = item.start
351 stop = item.stop
352 if start is None:
353 start = 0
354 if step is None:
355 step = 1
356 if isinstance(step, (_nx.complexfloating, complex)):
357 size = int(abs(step))
358 newobj = linspace(start, stop, num=size)
359 else:
360 newobj = _nx.arange(start, stop, step)
361 if ndmin > 1:
362 newobj = array(newobj, copy=False, ndmin=ndmin)
363 if trans1d != -1:
364 newobj = newobj.swapaxes(-1, trans1d)
365 elif isinstance(item, str):
366 if k != 0:
367 raise ValueError("special directives must be the "
368 "first entry.")
369 if item in ('r', 'c'):
370 matrix = True
371 col = (item == 'c')
372 continue
373 if ',' in item:
374 vec = item.split(',')
375 try:
376 axis, ndmin = [int(x) for x in vec[:2]]
377 if len(vec) == 3:
378 trans1d = int(vec[2])
379 continue
380 except Exception as e:
381 raise ValueError(
382 "unknown special directive {!r}".format(item)
383 ) from e

Callers

nothing calls this directly

Calls 6

linspaceFunction · 0.90
absFunction · 0.85
ndimMethod · 0.80
makematMethod · 0.80
arrayFunction · 0.50
splitMethod · 0.45

Tested by

no test coverage detected