def _ConvertDiagListToDict( diag_list ):
buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
for diag in diag_list:
location = diag[ 'location' ]
buffer_number = vimsupport.GetBufferNumberForFilename(
location[ 'filepath' ] )
line_number = location[ 'line_num' ]
buffer_to_line_to_diags[ buffer_number ][ line_number ].append( diag )
for line_to_diags in itervalues( buffer_to_line_to_diags ):
for diags in itervalues( line_to_diags ):
# We want errors to be listed before warnings so that errors aren't hidden
# by the warnings.
diags.sort( key = lambda diag: ( diag[ 'kind' ],
diag[ 'location' ][ 'column_num' ] ) )
return buffer_to_line_to_diags
python类itervalues()的实例源码
def _ConnectGroupChildren( group_name_to_group ):
def GetParentNames( group ):
links_to = 'links to '
parent_names = []
for line in group.lines:
if line.startswith( links_to ):
parent_names.append( line[ len( links_to ): ] )
return parent_names
for group in itervalues( group_name_to_group ):
parent_names = GetParentNames( group )
for parent_name in parent_names:
try:
parent_group = group_name_to_group[ parent_name ]
except KeyError:
continue
parent_group.children.append( group )
def sample_dict(dictionary, sample_size):
"""Return a randomly sampled frequency dict"""
new_dict = dict()
num_to_sample = sample_size
num_to_process = sum(itervalues(dictionary))
for (k, v) in iteritems(dictionary):
for i in range(v):
if random.random() < num_to_sample / num_to_process:
new_dict[k] = new_dict.get(k, 0) + 1
num_to_sample -= 1
num_to_process -= 1
if num_to_sample == 0:
break
else:
continue
break
if num_to_sample > 0:
raise ValueError(u'Not enough elements in dictionary')
return new_dict
def run(testapp, collections=None):
app = testapp.app
root = app.root_factory(app)
if not collections:
collections = root.by_item_type.keys()
check_path(testapp, '/')
for collection_name in collections:
collection = root[collection_name]
collection_path = resource_path(collection, '')
check_path(testapp, collection_path)
failed = 0
for count, item in enumerate(itervalues(collection)):
path = resource_path(item, '')
if not check_path(testapp, path):
failed += 1
if failed:
logger.info('Collection %s: %d of %d failed to render.',
collection_path, failed, count)
else:
logger.info('Collection %s: all %d rendered ok',
collection_path, count)
def run(testapp, collections=None):
app = testapp.app
root = app.root_factory(app)
if not collections:
collections = root.by_item_type.keys()
check_path(testapp, '/')
for collection_name in collections:
collection = root[collection_name]
collection_path = resource_path(collection, '')
check_path(testapp, collection_path)
failed = 0
for count, item in enumerate(itervalues(collection)):
path = resource_path(item, '')
if not check_path(testapp, path):
failed += 1
if failed:
logger.info('Collection %s: %d of %d failed to render.',
collection_path, failed, count)
else:
logger.info('Collection %s: all %d rendered ok',
collection_path, count)
def clear(self):
'od.clear() -> None. Remove all items from od.'
try:
for node in itervalues(self.__map):
del node[:]
root = self.__root
root[:] = [root, root, None]
self.__map.clear()
except AttributeError:
pass
dict.clear(self)
def itervalues(self):
'od.itervalues -> an iterator over the values in od'
for k in self:
yield self[k]
def clear(self):
'od.clear() -> None. Remove all items from od.'
try:
for node in itervalues(self.__map):
del node[:]
root = self.__root
root[:] = [root, root, None]
self.__map.clear()
except AttributeError:
pass
dict.clear(self)
def itervalues(self):
'od.itervalues -> an iterator over the values in od'
for k in self:
yield self[k]
def _FilterDiagnostics( self, predicate ):
matched_diags = []
line_to_diags = self._buffer_number_to_line_to_diags[
vim.current.buffer.number ]
for diags in itervalues( line_to_diags ):
matched_diags.extend( list(
self._ApplyDiagnosticFilter( diags, predicate ) ) )
return matched_diags
def _UpdateSquiggles( buffer_number_to_line_to_diags ):
vimsupport.ClearYcmSyntaxMatches()
line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
for diags in itervalues( line_to_diags ):
# Insert squiggles in reverse order so that errors overlap warnings.
for diag in reversed( diags ):
location_extent = diag[ 'location_extent' ]
is_error = _DiagnosticIsError( diag )
if location_extent[ 'start' ][ 'line_num' ] <= 0:
location = diag[ 'location' ]
vimsupport.AddDiagnosticSyntaxMatch(
location[ 'line_num' ],
location[ 'column_num' ],
is_error = is_error )
else:
vimsupport.AddDiagnosticSyntaxMatch(
location_extent[ 'start' ][ 'line_num' ],
location_extent[ 'start' ][ 'column_num' ],
location_extent[ 'end' ][ 'line_num' ],
location_extent[ 'end' ][ 'column_num' ],
is_error = is_error )
for diag_range in diag[ 'ranges' ]:
vimsupport.AddDiagnosticSyntaxMatch(
diag_range[ 'start' ][ 'line_num' ],
diag_range[ 'start' ][ 'column_num' ],
diag_range[ 'end' ][ 'line_num' ],
diag_range[ 'end' ][ 'column_num' ],
is_error = is_error )
def get_perplexity(dictionary):
"""Compute the perplexity (=exp entropy) of a dictionary"""
my_sum = 0
weighted_sum_of_logs = 0
for freq in itervalues(dictionary):
if freq:
my_sum += freq
weighted_sum_of_logs += freq * math.log(freq)
return math.exp(math.log(my_sum) - weighted_sum_of_logs / my_sum)
def load_computation(self, computation_decl):
"""
Load a computation and associated storage into the current execution state.
Args:
computation_decl: A ComputationDecl for the computation.
Returns:
An executable for the computation.
"""
self.device_computation = computation_decl.device_computation
exop_block = computation_decl.exop_block
self.start_allocate_computation(computation_decl)
for input_decl in itervalues(computation_decl.op_returns):
self.device_tensor_view(input_decl.tensor_view_decl)
for exop in exop_block:
for input_decl in exop.input_decls:
self.device_tensor_view(input_decl.tensor_view_decl)
for input_decl in exop.write_args:
self.device_tensor_view(input_decl.tensor_view_decl)
for output_decl in exop.output_decls:
self.device_tensor_view(output_decl.tensor_view_decl)
# Make sure we have values for ops that got optimized out
for input_decl in computation_decl.returns.input_decls:
output_decl = input_decl.source_output_decl
if isinstance(output_decl.exop.op, TensorValueOp):
tensor_decl = exop.computation_decl.get_tensor_decl(
op=output_decl.exop.op.value_tensor)
self.device_tensor_view(
tensor_decl.get_tensor_view(output_decl.exop.op.tensor_description()))
else:
self.device_tensor_view(output_decl.tensor_view_decl)
for param in computation_decl.computation_op.parameters:
tensor_decl = computation_decl.get_tensor_decl(op=param.tensor)
self.device_tensor_view(tensor_decl.root_tensor_view_decl)
self.finish_allocate_computation(computation_decl)
self.start_define_computation(computation_decl)
for exop in exop_block:
self.generate_exop(exop)
self.finish_define_computation(computation_decl)
executor = self.finish_load_computation(computation_decl)
self.run_device_tensor_initializations()
return executor