本文整理汇总了Python中yaml.unsafe_load方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.unsafe_load方法的具体用法?Python yaml.unsafe_load怎么用?Python yaml.unsafe_load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.unsafe_load方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_from_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def merge_from_file(self, cfg_filename, allow_unsafe = False):
"""
Merge configs from a given yaml file.
Args:
cfg_filename: the file name of the yaml config.
allow_unsafe: whether to allow loading the config file with
`yaml.unsafe_load`.
"""
loaded_cfg = CfgNode.load_yaml_with_base(
cfg_filename, allow_unsafe=allow_unsafe
)
loaded_cfg = type(self)(loaded_cfg)
self.merge_from_other_cfg(loaded_cfg)
# Forward the following calls to base, but with a check on the BASE_KEY.
示例2: MakeYml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def MakeYml(self):
f = open(self.YmlFilePathBase, "r")
f2 = open(self.YmlFilePathOut, 'w')
for l in f:
f2.write(l)
# now parse as yaml
f.seek(0)
# yaml.load(f)
# IMPORTANT NOTE: This call to "unsafe_load()" is only acceptable because we control the yml file being loaded.
# Unsafe load is required to support some configuration options for pymdownx.
if "extra" in yaml.unsafe_load(f):
raise Exception(
"extra: member not allowed in mkdocs_base.yml. Please add the contents to DocBuild constructor instead. ")
f.close()
self.Yml = f2
示例3: merge_from_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def merge_from_file(self, cfg_filename: str, allow_unsafe: bool = False) -> None:
"""
Merge configs from a given yaml file.
Args:
cfg_filename: the file name of the yaml config.
allow_unsafe: whether to allow loading the config file with
`yaml.unsafe_load`.
"""
loaded_cfg = CfgNode.load_yaml_with_base(
cfg_filename, allow_unsafe=allow_unsafe
)
loaded_cfg = type(self)(loaded_cfg)
self.merge_from_other_cfg(loaded_cfg)
# Forward the following calls to base, but with a check on the BASE_KEY.
示例4: merge_from_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def merge_from_file(self, cfg_filename: str, allow_unsafe: bool = False):
"""
Merge configs from a given yaml file.
Args:
cfg_filename: the file name of the yaml config.
allow_unsafe: whether to allow loading the config file with
`yaml.unsafe_load`.
"""
loaded_cfg = CfgNode.load_yaml_with_base(
cfg_filename, allow_unsafe=allow_unsafe
)
loaded_cfg = type(self)(loaded_cfg)
self.merge_from_other_cfg(loaded_cfg)
# Forward the following calls to base, but with a check on the BASE_KEY.
示例5: load_yaml_with_base
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def load_yaml_with_base(filename: str, allow_unsafe: bool = False):
"""
Just like `yaml.load(open(filename))`, but inherit attributes from its
`_BASE_`.
Args:
filename (str): the file name of the current config. Will be used to
find the base config file.
allow_unsafe (bool): whether to allow loading the config file with
`yaml.unsafe_load`.
Returns:
(dict): the loaded yaml
"""
with PathManager.open(filename, "r") as f:
try:
cfg = yaml.safe_load(f)
except yaml.constructor.ConstructorError:
if not allow_unsafe:
raise
logger = logging.getLogger(__name__)
logger.warning(
"Loading config {} with yaml.unsafe_load. Your machine may "
"be at risk if the file contains malicious content.".format(
filename
)
)
f.close()
with open(filename, "r") as f:
cfg = yaml.unsafe_load(f)
def merge_a_into_b(a, b):
# merge dict a into dict b. values in a will overwrite b.
for k, v in a.items():
if isinstance(v, dict) and k in b:
assert isinstance(
b[k], dict
), "Cannot inherit key '{}' from base!".format(k)
merge_a_into_b(v, b[k])
else:
b[k] = v
if BASE_KEY in cfg:
base_cfg_file = cfg[BASE_KEY]
if base_cfg_file.startswith("~"):
base_cfg_file = os.path.expanduser(base_cfg_file)
if not any(
map(base_cfg_file.startswith, ["/", "https://", "http://"])
):
# the path to base cfg is relative to the config file itself.
base_cfg_file = os.path.join(
os.path.dirname(filename), base_cfg_file
)
base_cfg = CfgNode.load_yaml_with_base(
base_cfg_file, allow_unsafe=allow_unsafe
)
del cfg[BASE_KEY]
merge_a_into_b(cfg, base_cfg)
return base_cfg
return cfg
示例6: load_yaml_with_base
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def load_yaml_with_base(filename: str, allow_unsafe: bool = False) -> None:
"""
Just like `yaml.load(open(filename))`, but inherit attributes from its
`_BASE_`.
Args:
filename (str): the file name of the current config. Will be used to
find the base config file.
allow_unsafe (bool): whether to allow loading the config file with
`yaml.unsafe_load`.
Returns:
(dict): the loaded yaml
"""
with PathManager.open(filename, "r") as f:
try:
cfg = yaml.safe_load(f)
except yaml.constructor.ConstructorError:
if not allow_unsafe:
raise
logger = logging.getLogger(__name__)
logger.warning(
"Loading config {} with yaml.unsafe_load. Your machine may "
"be at risk if the file contains malicious content.".format(
filename
)
)
f.close()
with open(filename, "r") as f:
cfg = yaml.unsafe_load(f) # pyre-ignore
# pyre-ignore
def merge_a_into_b(a: Dict[Any, Any], b: Dict[Any, Any]) -> None:
# merge dict a into dict b. values in a will overwrite b.
for k, v in a.items():
if isinstance(v, dict) and k in b:
assert isinstance(
b[k], dict
), "Cannot inherit key '{}' from base!".format(k)
merge_a_into_b(v, b[k])
else:
b[k] = v
if BASE_KEY in cfg:
base_cfg_file = cfg[BASE_KEY]
if base_cfg_file.startswith("~"):
base_cfg_file = os.path.expanduser(base_cfg_file)
if not any(map(base_cfg_file.startswith, ["/", "https://", "http://"])):
# the path to base cfg is relative to the config file itself.
base_cfg_file = os.path.join(os.path.dirname(filename), base_cfg_file)
base_cfg = CfgNode.load_yaml_with_base(
base_cfg_file, allow_unsafe=allow_unsafe
)
del cfg[BASE_KEY]
# pyre-fixme[6]: Expected `Dict[typing.Any, typing.Any]` for 2nd param
# but got `None`.
merge_a_into_b(cfg, base_cfg)
return base_cfg
return cfg
示例7: load_ote_from_deployment_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def load_ote_from_deployment_yaml(deployments_file, out_dir, save=True):
"""Create a WebbPSF adjustable OTE object representing a perturbed OTE
mirror state using mirror deployments defined in a YAML file.
Parameters
----------
deployments_file : str
Path to YAML file containing deployments data. Expects format as
produced by ``mirage.psf.deployments.generate_deployment_errors``
out_dir : str
Path to directory in which to save OPD FITS files
save : bool, optional
Denotes whether to save out the OPD (with and without tip/tilt)
as FITs files
Returns
-------
ote : webbpsf.opds.OTE_Linear_Model_WSS object
WebbPSF OTE object describing perturbed OTE state with tip and tilt removed
segment_tilts : numpy.ndarray
List of X and Y tilts for each mirror segment, in microradians
ote_opd_with_tilts : numpy.ndarray
Array representing perturbed OTE OPD pupil before tip/tilt is removed
"""
# Make an adjustable OTE object with WebbPSF
nc = webbpsf.NIRCam()
nc, ote = webbpsf.enable_adjustable_ote(nc)
# Open existing file with previous deployments
with open(deployments_file) as f:
deployment_errors = yaml.unsafe_load(f)
# Create OTE object and list of segment tilts
ote, segment_tilts = apply_deployment_errors(
ote, deployment_errors, out_dir=out_dir, save=save
)
ote_opd_with_tilts = ote.opd # Save array to plot below
# Remove tip and tilt
ote = remove_piston_tip_tilt(ote, out_dir=out_dir, save=save)
return ote, segment_tilts, ote_opd_with_tilts
示例8: load_yaml_with_base
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import unsafe_load [as 别名]
def load_yaml_with_base(filename, allow_unsafe = False):
"""
Just like `yaml.load(open(filename))`, but inherit attributes from its
`_BASE_`.
Args:
filename (str): the file name of the current config. Will be used to
find the base config file.
allow_unsafe (bool): whether to allow loading the config file with
`yaml.unsafe_load`.
Returns:
(dict): the loaded yaml
"""
with PathManager.open(filename, "r") as f:
try:
cfg = yaml.safe_load(f)
except yaml.constructor.ConstructorError:
if not allow_unsafe:
raise
logger = logging.getLogger(__name__)
logger.warning(
"Loading config {} with yaml.unsafe_load. Your machine may "
"be at risk if the file contains malicious content.".format(
filename
)
)
f.close()
with open(filename, "r") as f:
cfg = yaml.unsafe_load(f)
def merge_a_into_b(a, b):
# merge dict a into dict b. values in a will overwrite b.
for k, v in a.items():
if isinstance(v, dict) and k in b:
assert isinstance(
b[k], dict
), "Cannot inherit key '{}' from base!".format(k)
merge_a_into_b(v, b[k])
else:
b[k] = v
if BASE_KEY in cfg:
base_cfg_file = cfg[BASE_KEY]
if base_cfg_file.startswith("~"):
base_cfg_file = os.path.expanduser(base_cfg_file)
if not any(
map(base_cfg_file.startswith, ["/", "https://", "http://"])
):
# the path to base cfg is relative to the config file itself.
base_cfg_file = os.path.join(
os.path.dirname(filename), base_cfg_file
)
base_cfg = CfgNode.load_yaml_with_base(
base_cfg_file, allow_unsafe=allow_unsafe
)
del cfg[BASE_KEY]
merge_a_into_b(cfg, base_cfg)
return base_cfg
return cfg