def __getitem__(self, id): if id is None: raise exceptions.IndexError(_("Cannot dereference NULL ID")) cur = ctypes.POINTER(Token)() cur = DLL.token_table_get_next_by_id(self._tableobj, cur, id) if bool(cur): return cur.contents else: raise exceptions.IndexError(_("ID 0x%04x not found") % id)
def file_chunker(file_handler, chunk_count, chunk_index): """Access a chunk of a file @param: file_handler - the file object @param: chunk_count - the number of section to divide this file into @param: chunk_index - the index of the chunk to read from @yield: lines of the chunk identified by the chunk_index """ if 1 > chunk_count: raise exceptions.ValueError("Chunk count must be > 0 ") if chunk_index > chunk_count: raise exceptions.IndexError("Chunk index out of bound") # seek to the end of file file_handler.seek(0, 2) file_size = file_handler.tell() # in case file is too small chunk_size = max(1, file_size // chunk_count) chunk_start = chunk_index * chunk_size chunk_end = chunk_start + chunk_size # Set the file position at the start of the first line in the chunk if chunk_start == 0: file_handler.seek(0) else: file_handler.seek(chunk_start - 1) file_handler.readline() # Start yielding line within the chunk identified by the chunk index while file_handler.tell() < chunk_end: line = file_handler.readline() if not line: # End of file, readline() include new line character for blank line break # Next line yield line.strip()
def getStructureByType(self, t): cur = ctypes.POINTER(SmbiosStructure)() cur = DLL.smbios_table_get_next_struct_by_type(self._tableobj, cur, t) if not bool(cur): raise exceptions.IndexError( _("No SMBIOS structure found with type %s") % t) return cur.contents
def _get_align_idx(self, ): """ find first good detector for alignment """ for d in range(self.n_detectors): if d not in self.bad_mca_idx: if self.mca[d].total_counts >= 1: return d #return 0 raise exceptions.IndexError("No good detector index found")
def __getitem__( self, index ): # if index not in self.__no.keys() : raise exceptions.IndexError( "index out of range: %d" % (index, ) ) if index >= self.__len : raise exceptions.IndexError( "index out of range: %d" % (index, ) ) attrname = self.__no[ index ] return getattr( self, attrname )
def Ind2Sub(index,size): if np.isscalar(index): ind = np.int64(index) else: ind = index.copy() if ind.dtype is not np.dtype('int64'): ind = ind.astype(np.int64) total = 1 for i in size: total *= i if np.isscalar(ind): if ind < 0 or ind > total: raise exceptions.IndexError('BlockInd2SubWithoutBand') return np.column_stack(np.unravel_index(ind,size,order='F'))[0] else: if ind.any() < 0 or ind.any() > total: raise exceptions.IndexError('BlockInd2SubWithoutBand') return np.column_stack(np.unravel_index(ind,size,order='F'))
def unmarshal(self, dic): """ Reads a dictionary into this springbot """ self.nodes = [] self.springs = [] for node_d in dic['nodes']: newnode = Node(node_d['pos'], node_d['vel'], node_d['acc']) newnode.id = node_d['id'] self.nodes.append(newnode) for spring_d in dic['springs']: id_a = spring_d['from'] id_b = spring_d['to'] for node in self.nodes: if node.id == id_a: a = node break else: raise exceptions.IndexError("node id %d(from) not found\n" % (id_a)) # Procura id B for node in self.nodes: if node.id == id_b: b = node break else: raise exceptions.IndexError("node id %d(to) not found\n" % (id_b)) self.springs.append( Spring(a, b, spring_d['amplitude'], spring_d['offset'], spring_d['normal'])) del dic['nodes'] del dic['springs'] self._info = dict(dic) return self
def get_run_param(self, run_num=None): """ Return value of changing paramter for run 'run_num'. By default, returns value for last run.""" if run_num is None: run_num = self.run_ctr - 1 if run_num < 0 or run_num >= self.run_ctr: raise exceptions.IndexError( str(run_num) + ' not a valid run index.') param = self.tables_file.get_node(self.root_name+'/run'+str(run_num)+\ '/param')[0] return param
def _get_phases(self, first_phase, last_phase): first_index = last_index = 0 first_index = 0 if first_phase is None else self._get_phase_index( first_phase) last_index = (len(self._phases)) if last_phase is None else ( self._get_phase_index(last_phase) + 1 ) #Using len instead of -1 because of the return of a later condition if first_index == -1: raise exceptions.IndexError("First Phase %s not in phases" % first_phase) if last_index == -1: raise exceptions.IndexError("Last Phase %s not in phases" % last_phase) if last_index < first_index: raise PhaseRunnerError( "First Phase %s must be earlier in the phase list than Last Phase %s" % (first_phase, last_phase)) return self._phases[ first_index: last_index] if last_index > first_index else self._phases[ first_index]
def get_run(self, run_num=None, z_num=None): """ Return tuple of (pump, signal, idler) pulses for run # n at z index z_num . By default, returns last z step of last run.""" if run_num is None: run_num = self.run_ctr - 1 if run_num < 0 or run_num >= self.run_ctr: raise exceptions.IndexError( str(run_num) + ' not a valid run index.') if z_num is None: z_num = -1 run = self.root_name + '/run' + str(run_num) # Read in and generate pump pulse pump_field = self.tables_file.get_node(run + '/pump')[:, z_num] pump_Twind = self.tables_file.get_node(run + '/pump_Twind')[0] pump_center = self.tables_file.get_node(run + '/pump_center_nm')[0] p = Pulse() p.set_NPTS(len(pump_field)) p.set_center_wavelength_nm(pump_center) p.set_time_window_ps(pump_Twind) p.set_AW(pump_field) p.set_frep_MHz(self.frep_Hz * 1.0e-6) # Read in and generate signal pulse sgnl_field = self.tables_file.get_node(run + '/sgnl')[:, z_num] sgnl_Twind = self.tables_file.get_node(run + '/sgnl_Twind')[0] sgnl_center = self.tables_file.get_node(run + '/sgnl_center_nm')[0] s = Pulse() s.set_NPTS(len(sgnl_field)) s.set_center_wavelength_nm(sgnl_center) s.set_time_window_ps(sgnl_Twind) s.set_AW(sgnl_field) s.set_frep_MHz(self.frep_Hz * 1.0e-6) # Read in and generate idler pulse idlr_field = self.tables_file.get_node(run + '/idlr')[:, z_num] idlr_Twind = self.tables_file.get_node(run + '/idlr_Twind')[0] idlr_center = self.tables_file.get_node(run + '/idlr_center_nm')[0] i = Pulse() i.set_NPTS(len(idlr_field)) i.set_center_wavelength_nm(idlr_center) i.set_time_window_ps(idlr_Twind) i.set_AW(idlr_field) i.set_frep_MHz(self.frep_Hz * 1.0e-6) return (p, s, i)
def least_squares_slope(X1, X2=None): if X2 == None: n = len(X1) X = [i+1 for i in range(n)] Y = X1 else: X = X1 Y = X2 n = len(X) if len(Y) != n: raise exceptions.IndexError('List sizes for least_square_slope() must match!') XY = map(lambda x, y: x*y, X, Y) sumXY = sum(XY) sumX = sum(X) sumY = sum(Y) X2 = map(lambda x, y: x*y, X, X) sumX2 = sum(X2) return (n*sumXY - sumX*sumY)/(n*sumX2 - sumX**2)
def peek(self, index): """peek at the item at a given index from the bottom of the stack. raises an IndexError if the index is too high (above the top of the stack) or too low (the item has been dropped) **INPUTS** *INT index* -- the index of the desired item, relative to the (virtual) bottom of the stack, i.e. where the bottom would be if the stack were unlimited and no items had been dropped **OUTPUTS** *ANY* -- a reference to the item """ current = index - self.dropped if current < 0: raise exceptions.IndexError() else: return self.stack[current]
def init_testing(self, datetime_in, datetime_until, exchange_abbr_in, market_in): if config.backtesting_enabled: self.backtesting = True self._curr_time = t.mktime(datetime_in.timetuple()) self._end_time = t.mktime(datetime_until.timetuple()) self.exchange_abbr = exchange_abbr_in self.market = market_in if self.backtesting: with open('price_log/' + self.market + '_' + self.exchange_abbr.lower() + '.csv') as file: start_line_no = 0 self._curr_pricelog_line_no = -1 for line in file: strs = line.split(',') self._curr_pricelog_line_no = self._curr_pricelog_line_no + 1 # If starting earlier than available - finish if (float(strs[0]) > self._curr_time) and ( self._curr_pricelog_line_no == 0): raise exceptions.IndexError( 'Backtesting time earlier than available') if not start_line_no and float(strs[0]) >= self._curr_time: start_line_no = self._curr_pricelog_line_no if start_line_no: if float(strs[0]) > self._end_time: break else: pair = (float(strs[0])), float(strs[1]) self._price_history_queue.append(pair) self._curr_pricelog_line_no = start_line_no self._find_current_price()
#u8 DLL_SPEC smbios_struct_get_length(const struct smbios_struct *); DLL.smbios_struct_get_length.argtypes = [ctypes.POINTER(SmbiosStructure)] DLL.smbios_struct_get_length.restype = ctypes.c_uint8 #u16 DLL_SPEC smbios_struct_get_handle(const struct smbios_struct *); DLL.smbios_struct_get_handle.argtypes = [ctypes.POINTER(SmbiosStructure)] DLL.smbios_struct_get_handle.restype = ctypes.c_uint16 #const char * DLL_SPEC smbios_struct_get_string_from_offset(const struct smbios_struct *s, u8 offset); DLL.smbios_struct_get_string_from_offset.argtypes = [ ctypes.POINTER(SmbiosStructure), ctypes.c_uint8 ] DLL.smbios_struct_get_string_from_offset.restype = ctypes.c_char_p DLL.smbios_struct_get_string_from_offset.errcheck = errorOnNullPtrFN( lambda r, f, a: exceptions.IndexError( _("String from offset %d doesnt exist") % a[1])) #const char * DLL_SPEC smbios_struct_get_string_number(const struct smbios_struct *s, u8 which); DLL.smbios_struct_get_string_number.argtypes = [ ctypes.POINTER(SmbiosStructure), ctypes.c_uint8 ] DLL.smbios_struct_get_string_number.restype = ctypes.c_char_p DLL.smbios_struct_get_string_number.errcheck = errorOnNullPtrFN( lambda r, f, a: exceptions.IndexError( _("String number %d doesnt exist") % a[1])) #int DLL_SPEC smbios_struct_get_data(const struct smbios_struct *s, void *dest, u8 offset, size_t len); DLL.smbios_struct_get_data.argtypes = [ ctypes.POINTER(SmbiosStructure), ctypes.c_void_p, ctypes.c_uint8, ctypes.c_size_t ]