本文整理汇总了Python中collections.ChainMap.items方法的典型用法代码示例。如果您正苦于以下问题:Python ChainMap.items方法的具体用法?Python ChainMap.items怎么用?Python ChainMap.items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.ChainMap
的用法示例。
在下文中一共展示了ChainMap.items方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_settings
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
def load_settings(paths, default_config, oldsettings,
setting_callbacks, setting_types, refresh_only):
""" Load settings from the main config file. """
auto_settings, manual_settings\
= read_config(paths['config_file'], default_config)
settings = ChainMap(auto_settings, manual_settings)
def revert_setting(key):
if key in oldsettings:
reverted_value = oldsettings[key]
else:
reverted_value = default_config[key]
if key in auto_settings:
auto_settings[key] = reverted_value
elif key in manual_settings:
manual_settings[key] = reverted_value
error = False
# Make sure the settings aren't fucked up yo
for key, value in settings.items():
if refresh_only and oldsettings.get(key) == value:
continue
# First make a simple check to see if the value is the right type
if not valid_setting(key, value, setting_types):
print('Invalid type for setting: "{}"'.format(key))
error = True
revert_setting(key)
# Then do a live update and see if things blow up
try:
update_runtime_setting(key, value, setting_callbacks)
except SettingsError as e:
print(str(e))
error = True
revert_setting(key)
error_text = 'Errors while reading the config. Check terminal output.'
return settings, error_text if error else None
示例2: __init__
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
def __init__(self, **kwargs):
# get dict fieldname:Field received as parameter
parameters = {
field: Field(value=Configuration.typed(field, value),
type=Configuration.ALL_FIELDS[field].type)
for field, value in kwargs.items()
}
# take parameters in priority, else the ALL_FIELDS dict
prioritized_fields = ChainMap(parameters, Configuration.ALL_FIELDS)
# save values as fields
for varname, field in prioritized_fields.items():
value, ftype = field
setattr(self, '_' + varname, ftype(value))
# access and setter definition
def field_access(instance, field):
return getattr(instance, '_' + field)
def field_setter(instance, value, field):
ftype = Configuration.ALL_FIELDS[field].type
if field in self.UNMUTABLE_FIELDS:
LOGGER.warning('CONFIG: The unmutable field ' + field
+ ' have been modified to ' + str(value))
elif field in self.GENERATED_FIELDS:
LOGGER.warning('CONFIG: The generated field ' + field
+ ' have been modified to ' + str(value))
setattr(instance, '_' + field, ftype(value))
# regenerate generated data
# add values as properties, eventually with a setter if field is mutable
for varname in Configuration.ALL_FIELDS:
new_prop = property(partial(field_access, field=varname))
setattr(Configuration, varname, new_prop)
if varname in Configuration.MUTABLE_FIELDS:
setter = new_prop.setter(partial(field_setter, field=varname))
setattr(Configuration, varname, setter)
# add other data
self.postprocess_data()
示例3: instantiate_related_objects
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
def instantiate_related_objects(self, related_model, related_objects, meta_attrs):
new_objects = []
for obj_attrs in related_objects:
new_obj = related_model()
input_attrs = ChainMap(obj_attrs, meta_attrs)
for field_name, field_value in input_attrs.items():
f = self.interpolate(field_name, meta_attrs)
setattr(new_obj, field_name, f(field_value))
new_objects.append(new_obj)
return new_objects
示例4: scan
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
#.........这里部分代码省略.........
target=target.target
flw=follower(name,block,code,target)
self.AddToList(flw)
def AddMatrix(self,name,block):
pass
def Sample(self,**keys):
for p in self.free_parameter_list.values():
p.Generate(**keys)
return copy.deepcopy(self)
#========================
def AddMcmcMatrix(self,name,block,shape,free_element={},minimum=None,maximum=None,pace='normal',step_width=None,element_list={}):
block=block.upper()
# set one matrix
mtx=matrix(name,block,shape,element_list={})
if type(free_element) is dict:
mtx.element_list.update(free_element)
elif type(free_element) is list or type(free_element) is tuple:
if all( type(coords) is tuple for coords in free_element ):
mtx.element_list.update(dict.fromkeys(free_element))
mtx.minimum=minimum
mtx.maximum=maximum
mtx.pace=pace
if step_width:
mtx.step_width=step_width
else:
try:
mtx.step_width=(mtx.maximum-mtx.minimum)/100.
except TypeError:
Caution(
" Both maximum and minimum of '%s' should be given with pace='%s'"%(name,pace[0])
+ " if step width is not specified. Later, step width will be 1% of its initial value")
# add matrix into lists
if name in self.variable_list.keys():
Caution("parameter '%s' overridden"%name)
self.variable_list[name]=mtx
self.matrix_list[name]=mtx
if block in self.block_list.keys():
Caution("matrix '%s' overridden"%name)
self.block_list[block]=mtx
def GetNewPoint_mcmc(self,step_factor=1.):
new_point=copy.deepcopy(self)
for name in self.scalar_list.keys():
old=self.scalar_list[name]
v=getattr(Random,old.pace)(old.value , old.minimum , old.maximum ,step_factor)
new_point.scalar_list[name].value=v
for name in self.follower_list.keys():
par=new_point.follower_list[name]
par.value=new_point.variable_list[par.follow].value
for name in self.matrix_list.keys():
old=self.matrix_list[name]
for coords in old.element_list.keys():
v=getattr(Random,old.pace)(old.element_list[coords] , old.minimum , old.maximum ,step_factor)
new_point.matrix_list[name].element_list[coords]=v
return new_point
def Print(self):
for name,par in sorted(self.variable_list.items()):
print(f'{name:>10}: {par.value}')
# print('\t',name,self.scalar_list[name].value)
# for name in self.follower_list.keys():
# print('\t',name,self.follower_list[name].value)
# for name in self.matrix_list.keys():
# print('\t',name,self.matrix_list[name].element_list)
def GetValue(self,file_name,mapping=None,ignore=None,Read=None):
'''Arguments
Required:
file_name: SLHA file that contain needed parameters;
Optional:
mapping : A dict which holds a map of different block names:
{ name in object : name in SLHA file }
ignore : parameters with name in this will not be read
Read : The function that will analyse the SLHA file
'''
if Read is None:
Read=ReadSLHAFile
target=Read(file_name)
# print(mapping)
if mapping is None:
mapping={}
if ignore is None:
ignore=[]
for par in self.free_parameter_list.values():
if par.name in ignore:continue
block=getattr(target,par.block
,getattr(target,mapping.get(par.block,par.block))
)
par.value=block.get(par.code)
# for par in self.follower_list.values():
# par.value=par.target.value
示例5: print
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
#合并多个字典或映射
from collections import ChainMap
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
c=ChainMap(a,b)#有优先级的区别,#逻辑上合并,内部并没有合并
print(c['x'],c['y'],c['z'])
print(len(c))
print(list(c.keys()))
print(list(c.items()))
print(list(c.values()))
#更新或删除操作只是影响第一个字典中的值
#del(c['y']) 此语句报错s
values = ChainMap()#神奇的new_child和parent方法
values['x'] = 1
# Add a new mapping
values = values.new_child()
values['x'] = 2
# Add a new mapping
values = values.new_child()
values['x'] = 3
print(values)
print(values['x'])
# Discard last mapping
values = values.parents
print(values['x'])
# Discard last mapping
values = values.parents
print(values['x'])
print(values)
示例6: ChainMap
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
# 1.20. Combining Multiple Mappings into a Single Mapping
from collections import ChainMap
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
# 返回第一次出现的key
merged = ChainMap(a, b)
print(list(merged.items()))
# 动态更新
a['x'] = 42
print(list(merged.items()))
示例7: ChainMap
# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import items [as 别名]
}
sysex_commands = {
0x61: ('encoder_data', ()),
0x69: ('analog_mapping_query', ()),
0x6A: ('analog_mapping_response', ()),
0x6B: ('capability_query', ()),
0x6C: ('capability_response', ()),
0x6D: ('pin_state_query', ()),
0x6E: ('pin_state_response', ()),
0x6F: ('extended_analog', ()),
0x70: ('servo_config', ()),
0x71: ('string_data', ()),
0x72: ('stepper_data', ()),
0x73: ('onewire_data', ()),
0x75: ('shift_data', ()),
0x76: ('i2c_request', ()),
0x77: ('i2c_reply', ()),
0x78: ('i2c_config', ()),
0x79: ('report_firmware', ()),
0x7A: ('sampling_interval', ()),
0x7B: ('scheduler_data', ()),
0x7E: ('sysex_non_realtime', ()),
0x7F: ('sysex_realtime', ()),
}
# Code => Name mapping for all types
command_names = ChainMap(nibble_commands, byte_commands, sysex_commands)
# Name => Code mapping for all types
command_lookup = {v[0]: k for k, v in command_names.items()}