def get_resrange(resrange): '''return resrange as a string Examples -------- >>> get_resrange('1-3') 'resrange 1-3' >>> get_resrange(0) 'resrange 1' >>> get_resrange(range(3)) 'resrange 1,2,3' >>> get_resrange([2, 5, 7]) 'resrange 3,6,8' >>> get_resrange(None) '' ''' from pytraj.utils import convert, is_int if resrange is not None: if is_int(resrange): resrange = [ resrange, ] if isinstance(resrange, string_types): _resrange = "resrange " + resrange else: _resrange = convert.array_to_cpptraj_range(resrange) _resrange = "resrange " + str(_resrange) else: _resrange = "" return _resrange
def __getitem__(self, idx): """return a DataSet instance. Memory view is applied (which mean this new insance is just alias of self[idx]). Should we use a copy instead? Examples -------- >>> import pytraj as pt >>> traj = pt.datafiles.load_tz2_ortho() >>> dslist = pt.multidihedral(traj) >>> d0 = dslist[0] >>> d1 = dslist['phi:3'] >>> d2 = dslist[:6:2] >>> d3 = dslist[[0, 3, 8]] >>> d4 = dslist.__getslice__(0, 3) >>> d5 = d3.__class__() >>> d5[0] Traceback (most recent call last): ... ValueError: size = 0: can not index >> # dummy >>> d6 = dslist[pt.Frame] Traceback (most recent call last): ... ValueError: index must be int, string, slice or array-like """ if self.size == 0: raise ValueError("size = 0: can not index") if is_int(idx): return super(DatasetList, self).__getitem__(idx) elif isinstance(idx, string_types): for d0 in self: if d0.key.upper() == idx.upper(): d0._base = self return d0 elif isinstance(idx, slice): # return new view of `self` start, stop, step = idx.indices(self.size) new_dslist = self.__class__() for _idx in range(start, stop, step): new_dslist.append(self[_idx], copy=False) return new_dslist elif is_array(idx) or isinstance( idx, list) and not isinstance(idx[0], bool): new_dslist = self.__class__() for _idx in idx: new_dslist.append(self[_idx], copy=False) return new_dslist else: raise ValueError('index must be int, string, slice or array-like')
def __getitem__(self, idx): """return a DataSet instance. Memory view is applied (which mean this new insance is just alias of self[idx]). Should we use a copy instead? Examples -------- >>> import pytraj as pt >>> traj = pt.datafiles.load_tz2_ortho() >>> dslist = pt.multidihedral(traj) >>> d0 = dslist[0] >>> d1 = dslist['phi:3'] >>> d2 = dslist[:6:2] >>> d3 = dslist[[0, 3, 8]] >>> d4 = dslist.__getslice__(0, 3) >>> d5 = d3.__class__() >>> d5[0] Traceback (most recent call last): ... ValueError: size = 0: can not index >> # dummy >>> d6 = dslist[pt.Frame] Traceback (most recent call last): ... ValueError: index must be int, string, slice or array-like """ if self.size == 0: raise ValueError("size = 0: can not index") if is_int(idx): return super(DatasetList, self).__getitem__(idx) elif isinstance(idx, string_types): for d0 in self: if d0.key.upper() == idx.upper(): d0._base = self return d0 elif isinstance(idx, slice): # return new view of `self` start, stop, step = idx.indices(self.size) new_dslist = self.__class__() for _idx in range(start, stop, step): new_dslist.append(self[_idx], copy=False) return new_dslist elif is_array(idx) or isinstance(idx, list) and not isinstance(idx[0], bool): new_dslist = self.__class__() for _idx in idx: new_dslist.append(self[_idx], copy=False) return new_dslist else: raise ValueError('index must be int, string, slice or array-like')