我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用string.ljust()。
def build_model(): metadata_dir = utils.get_dir_path('models', pathfinder.METADATA_PATH) metadata_path = utils.find_model_metadata(metadata_dir, patch_class_config.__name__.split('.')[-1]) metadata = utils.load_pkl(metadata_path) print 'Build model' model = patch_class_config.build_model() all_layers = nn.layers.get_all_layers(model.l_out) num_params = nn.layers.count_params(model.l_out) print ' number of parameters: %d' % num_params print string.ljust(' layer output shapes:', 36), print string.ljust('#params:', 10), print 'output shape:' for layer in all_layers: name = string.ljust(layer.__class__.__name__, 32) num_param = sum([np.prod(p.get_value().shape) for p in layer.get_params()]) num_param = string.ljust(num_param.__str__(), 10) print ' %s %s %s' % (name, num_param, layer.output_shape) nn.layers.set_all_param_values(model.l_out, metadata['param_values']) return model
def build_model(): metadata_dir = utils.get_dir_path('models', pathfinder.METADATA_PATH) metadata_path = utils.find_model_metadata(metadata_dir, patch_config.__name__.split('.')[-1]) metadata = utils.load_pkl(metadata_path) print 'Build model' model = patch_config.build_model(patch_size=(window_size, window_size, window_size)) all_layers = nn.layers.get_all_layers(model.l_out) num_params = nn.layers.count_params(model.l_out) print ' number of parameters: %d' % num_params print string.ljust(' layer output shapes:', 36), print string.ljust('#params:', 10), print 'output shape:' for layer in all_layers: name = string.ljust(layer.__class__.__name__, 32) num_param = sum([np.prod(p.get_value().shape) for p in layer.get_params()]) num_param = string.ljust(num_param.__str__(), 10) print ' %s %s %s' % (name, num_param, layer.output_shape) nn.layers.set_all_param_values(model.l_out, metadata['param_values']) return model
def build_model(): print 'Build model' model = patch_config.build_model(patch_size=(window_size, window_size, window_size)) all_layers = nn.layers.get_all_layers(model.l_out) num_params = nn.layers.count_params(model.l_out) print ' number of parameters: %d' % num_params print string.ljust(' layer output shapes:', 36), print string.ljust('#params:', 10), print 'output shape:' for layer in all_layers: name = string.ljust(layer.__class__.__name__, 32) num_param = sum([np.prod(p.get_value().shape) for p in layer.get_params()]) num_param = string.ljust(num_param.__str__(), 10) print ' %s %s %s' % (name, num_param, layer.output_shape) return model
def build_model(): metadata_dir = utils.get_dir_path('models', pathfinder.METADATA_PATH) metadata_path = utils.find_model_metadata(metadata_dir, patch_config.__name__.split('.')[-1]) metadata = utils.load_pkl(metadata_path) print 'Build model' model = patch_config.build_model() all_layers = nn.layers.get_all_layers(model.l_out) num_params = nn.layers.count_params(model.l_out) print ' number of parameters: %d' % num_params print string.ljust(' layer output shapes:', 36), print string.ljust('#params:', 10), print 'output shape:' for layer in all_layers: name = string.ljust(layer.__class__.__name__, 32) num_param = sum([np.prod(p.get_value().shape) for p in layer.get_params()]) num_param = string.ljust(num_param.__str__(), 10) print ' %s %s %s' % (name, num_param, layer.output_shape) nn.layers.set_all_param_values(model.l_out, metadata['param_values']) return model
def encode_name(name, type, scope): if name == '*': name += '\0' * 15 elif len(name) > 15: name = name[:15] + chr(type) else: name = string.ljust(name, 15) + chr(type) encoded_name = chr(len(name) * 2) + re.sub('.', _do_first_level_encoding, name) if scope: encoded_scope = '' for s in string.split(scope, '.'): encoded_scope = encoded_scope + chr(len(s)) + s return encoded_name + encoded_scope + '\0' else: return encoded_name.encode('ascii') + '\0' # Internal method for use in encode_name()
def encode_name(name, type, scope): if name == '*': name = name + '\0' * 15 elif len(name) > 15: name = name[:15] + chr(type) else: name = string.ljust(name, 15) + chr(type) encoded_name = chr(len(name) * 2) + re.sub('.', _do_first_level_encoding, name) if scope: encoded_scope = '' for s in string.split(scope, '.'): encoded_scope = encoded_scope + chr(len(s)) + s return encoded_name + encoded_scope + '\0' else: return encoded_name + '\0' # Internal method for use in encode_name()
def encode_name(name, type, scope = None): """ Perform first and second level encoding of name as specified in RFC 1001 (Section 4) """ if name == '*': name = name + '\0' * 15 elif len(name) > 15: name = name[:15] + chr(type) else: name = string.ljust(name, 15) + chr(type) def _do_first_level_encoding(m): s = ord(m.group(0)) return string.uppercase[s >> 4] + string.uppercase[s & 0x0f] encoded_name = chr(len(name) * 2) + re.sub('.', _do_first_level_encoding, name) if scope: encoded_scope = '' for s in string.split(scope, '.'): encoded_scope = encoded_scope + chr(len(s)) + s return encoded_name + encoded_scope + '\0' else: return encoded_name + '\0'
def send_fake_update(p, peer_ip, ts, rib, encoding, socket): # if it is an advertisement if rib is not None: # Make the second part of the v_mac (the part where the as-path is encoded) v_mac = '' deep = 1 aspath = '' for asn in rib.rib[p]: if deep in encoding.mapping: depth_value = encoding.mapping[deep].get_mapping_string(asn) v_mac += ''+depth_value deep += 1 aspath += str(asn)+' ' aspath = aspath[:-1] v_mac = string.ljust(v_mac, encoding.max_bytes, '0') socket.send(peer_ip+'|'+p+'|'+str(ts)+'|'+aspath+'|'+v_mac+'\n') else: # If it is a withdrawal socket.send(peer_ip+'|'+p+'|'+str(ts)+'\n')
def encode_name(name, type, scope): if name == '*': name += '\0' * 15 elif len(name) > 15: name = name[:15] + chr(type) else: name = string.ljust(name, 15) + chr(type) encoded_name = chr(len(name) * 2) + re.sub('.', _do_first_level_encoding, name) if scope: encoded_scope = '' for s in string.split(scope, '.'): encoded_scope = encoded_scope + chr(len(s)) + s return encoded_name + encoded_scope + '\0' else: return encoded_name + '\0' # Internal method for use in encode_name()
def load_read(self, bytes): # # load all image data in one chunk xsize, ysize = self.size s = [None] * ysize for i in range(ysize): s[i] = string.ljust(self.fp.readline()[1:xsize+1], xsize) self.fp = None return string.join(s, "") # # Registry
def pretty_print(agent_cnt, prefix, suffix): import string # Pretty print the prefix and suffix_cycle on team_ts hdr_line_1 = '' hdr_line_2 = '' for i in range(0,agent_cnt): hdr_line_1 += string.ljust('Robot-%d' % (i+1), 20) hdr_line_2 += string.ljust('-------', 20) logger.info(hdr_line_1) logger.info(hdr_line_2) logger.info('*** Prefix: ***') for s in prefix: line = '' for ss in s: line += string.ljust('%s' % (ss,), 20) logger.info(line) logger.info('*** Suffix: ***') for s in suffix: line = '' for ss in s: line += string.ljust('%s' % (ss,), 20) logger.info(line)
def __str__(self): classStr = '' for name, value in self.__class__.__dict__.items( ) + self.__dict__.items( ): classStr += string.ljust( name, 15 ) + '\t' + str( value ) + '\n' return classStr
def list_parsers(self, *args): """Return a list of available parsing modules""" print('==== Available parsing modules: ====\n') for parser in sorted(self.parse_modules): print(string.ljust(self.parse_modules[parser].name, 16) + \ ': ' + self.parse_modules[parser].desc) sys.exit(0)
def cat(self): """ Prints a catalogue of the files stored in the UEF file. """ # Catalogue command if self.contents == []: print 'No files' else: print 'Contents:' file_number = 0 for file in self.contents: # Converts non printable characters in the filename # to ? symbols new_name = self.printable(file['name']) print string.expandtabs(string.ljust(str(file_number), 3)+': '+ string.ljust(new_name, 16)+ string.upper( string.ljust(hex(file['load'])[2:], 10) +'\t'+ string.ljust(hex(file['exec'])[2:], 10) +'\t'+ string.ljust(hex(len(file['data']))[2:], 6) ) +'\t'+ 'chunks %i to %i' % (file['position'], file['last position']) ) file_number = file_number + 1
def insert_primary_rules(self, primary_ip): primary_tag = self.tag_dic[primary_ip] tmp_mac = bin(primary_tag)[2:] tmp_mac = tmp_mac.zfill(self.nexthops_nb_bits) tmp_mac = string.ljust(tmp_mac, 48, '0') bitmask = string.ljust('', self.nexthops_nb_bits, '1') bitmask = string.ljust(bitmask, 48, '0') try: real_mac = self.mapping_real[primary_ip][0] outport = self.mapping_real[primary_ip][1] except KeyError: real_mac = 'unknown' outport = 'unknown' # Binary to Hexadecimal conversion tmp_mac = hex(int(tmp_mac, 2))[2:].zfill(12) bitmask = hex(int(bitmask, 2))[2:].zfill(12) tmp_mac = ':'.join(s.encode('hex') for s in tmp_mac.decode('hex')) bitmask = ':'.join(s.encode('hex') for s in bitmask.decode('hex')) self.fd_rules.write('ovs-ofctl add-flow s1 priority=10,dl_dst='+tmp_mac+'/'+bitmask+',actions=mod_dl_dst:'+real_mac+',output:'+outport+'\n') os.system('ovs-ofctl add-flow s1 priority=10,dl_dst='+tmp_mac+'/'+bitmask+',actions=mod_dl_dst:'+real_mac+',output:'+outport)
def __init__(self, nbname, nametype, flags): self.__nbname = string.ljust(nbname,17) self.__nametype = nametype self.__flags = flags self.__isgroup = flags & 0x8000 self.__nodetype = flags & 0x6000 self.__deleting = flags & 0x1000 self.__isconflict = flags & 0x0800 self.__isactive = flags & 0x0400 self.__ispermanent = flags & 0x0200
def set_nbname(self, name): self.__nbname = string.ljust(name,17)
def set_ext_header_pas (self) : ext = {} self.extended_header = segy_h.Passcal () cor_low, cor_high, sort_start_time = self._cor () if cor_high < -MAX_16 or cor_high > MAX_16 : #print cor_high cor_high = int (MAX_16) ext['totalStaticHi'] = cor_high ext['num_samps'] = int (self.length_points) ext['max'] = numpy.max (self.data) ext['min'] = numpy.min (self.data) ext['samp_rate'] = int ((1.0 / self.sample_rate) * 1000000.0) ext['data_form'] = 1 # 32 bit # *XXX* Is this also set where the trace is written? ext['scale_fac'] = float (self.response_t['bit_weight/value_d']) #ext['scale_fac'] = 1. / 184128195.173 corrected_start_time = self.cut_start_epoch + (cor_low / 1000.0) m_secs = int (math.modf (corrected_start_time)[0] * 1000.0) ext['m_secs'] = m_secs try : ttuple = time.gmtime ([self.event_t['time/epoch_l']]) ext['trigyear'] = ttuple[0] ext['trigday'] = ttuple[7] ext['trighour'] = ttuple[3] ext['trigminute'] = ttuple[4] ext['trigsecond'] = ttuple[5] ext['trigmills'] = int (self.event_t['time/micro_seconds_i'] / 1000.0) except : pass try : try : ext['inst_no'] = int (self.array_t['das/serial_number_s']) & 0xFFFF except ValueError : ext['inst_no'] = int (self.array_t['das/serial_number_s'], 16) except : ext['inst_no'] = 0 try : ext['station_name'] = string.ljust (string.strip (self.array_t['id_s']), 6) except : ext['station_name'] = string.ljust (string.strip (self.array_t['das/serial_number_s']), 6) return ext
def _SplitAndPad(s): """Splits a string on newlines, and pads the lines to be the same width.""" split_string = s.split('\n') width = max(len(stringpiece) for stringpiece in split_string) + 1 padded_strings = [string.ljust(stringpiece, width, ' ') for stringpiece in split_string] return padded_strings
def set_ext_header_pas (self) : ext = {} self.extended_header = segy_h.Passcal () cor_low, cor_high, sort_start_time = self._cor () if cor_high < -MAX_16 or cor_high > MAX_16 : #print cor_high cor_high = int (MAX_16) ext['totalStaticHi'] = cor_high ext['num_samps'] = int (self.length_points) ext['max'] = numpy.max (self.data) ext['min'] = numpy.min (self.data) ext['samp_rate'] = int ((1.0 / self.sample_rate) * 1000000.0) ext['data_form'] = 1 # 32 bit ext['scale_fac'] = float (self.response_t['bit_weight/value_d']) corrected_start_time = self.cut_start_epoch + (cor_low / 1000.0) m_secs = int (math.modf (corrected_start_time)[0] * 1000.0) ext['m_secs'] = m_secs try : ttuple = time.gmtime ([self.event_t['time/epoch_l']]) ext['trigyear'] = ttuple[0] ext['trigday'] = ttuple[7] ext['trighour'] = ttuple[3] ext['trigminute'] = ttuple[4] ext['trigsecond'] = ttuple[5] ext['trigmills'] = int (self.event_t['time/micro_seconds_i'] / 1000.0) except : pass try : try : ext['inst_no'] = int (self.array_t['das/serial_number_s']) except ValueError : ext['inst_no'] = int (self.array_t['das/serial_number_s'], 16) except : ext['inst_no'] = 0 try : ext['station_name'] = string.ljust (string.strip (self.array_t['id_s']), 6) except : ext['station_name'] = string.ljust (string.strip (self.array_t['das/serial_number_s']), 6) return ext