我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用yaml.CDumper()。
def write_configuration_file(filename, conf): """Write a config dict to the specified file, in yaml format""" import shutil try: shutil.copy2(filename, '%s.back' % filename) except IOError: pass try: os.makedirs(os.path.dirname(filename)) except OSError: pass with open(filename, 'w') as filep: yaml.dump(conf, filep, indent=4, default_flow_style=False, Dumper=Dumper)
def __write_yaml(data, dump, file): data = __escape_str(data) if file: create_file(file) with open(file, "w", encoding="utf-8") as stream: return dump(data, stream, Dumper=Dumper, default_flow_style=False, encoding="utf-8") else: return dump(data, Dumper=Dumper, default_flow_style=False)
def convert(self): """ conversion of the raml info into the word document :return: """ try: parsetree = ramlparser.load(self.inputname) except ValidationError as e: print ('validation error:', e.errors) print ("could not load file: error loading file") traceback.print_exc() return # make it a member.. self.parsetree = parsetree self.list_x_resources(parsetree) # print parsetree # output = dump(parsetree, Dumper=Dumper,default_flow_style=False) # output = dump(parsetree, Dumper=SafeDumper) # print output try: self.document = Document(docx=self.resourcedoc) except: print ("could not load file: ", self.resourcedoc) print ("make sure that docx file exist..") return self.generate_sections(parsetree, self.resource_name) self.document.save(self.resource_out) print ("document saved..", self.resource_out)
def convert(self): try: parsetree = ramlparser.load(self.inputname) except ValidationError as e: print 'validation error:', e.errors #print "could not load file: error loading file" #traceback.print_exc() return # make it a member.. self.parsetree = parsetree self.listXResources(parsetree) #print parsetree #output = dump(parsetree, Dumper=Dumper,default_flow_style=False) #output = dump(parsetree, Dumper=SafeDumper) #print output try: self.document = Document(docx=self.resourcedoc) except: print "could not load file: ", self.resourcedoc print "make sure that docx file exist in same directory as executable" return self.generateSections(parsetree, self.ResourceName) self.document.save(self.resourceout)
def save_results(self, verbose): if self.tested_frame_coutner > 0: ave_fp = self.cum_fp / self.tested_frame_coutner ave_fn = self.cum_fn / self.tested_frame_coutner ave_wfp = self.cum_wfp / self.tested_frame_coutner ave_wfn = self.cum_wfn / self.tested_frame_coutner else: ave_fp = 0. ave_fn = 0. ave_wfp = 0. ave_wfn = 0. if verbose: print("Tested frame count: {:d}".format(self.tested_frame_coutner)) print("Avg. false positives: {:.2f}\nAvg. false negatives: {:.2f}".format( ave_fp, ave_fn)) print("Avg. weighted false positives: {:.2f}\nAvg. weighted false negatives: {:.2f}".format( ave_wfp, ave_wfn)) out = {"average_false_positives": float(ave_fp), "average_false_negatives": float(ave_fn), "average_weighted_false_positives": float(ave_wfp), "average_weighted_false_negatives": float(ave_wfn), "tested_frame_count": self.tested_frame_coutner, "args": self.args_dict} out_file = open(os.path.join(self.datapath, self.output_file), "w", encoding="utf_8") dump(out, out_file, Dumper=Dumper) out_file.close() return 0
def format(self, record): return yaml.dump(vars(record), Dumper=Dumper) + "\n---\n"
def save_data_yaml(self, filename=None, *args): ''' Save or show the results in YAML ''' if filename: self.stream = open(filename, 'w') else: self.stream = sys.stdout try: for game_log, stats in args: print >> self.stream, '### Stats per second of the log file %s:\n' % game_log print >> self.stream, '# Zero is significant (count missing lines): %s' % str(self.significantzero) print >> self.stream, yaml.dump(stats, default_flow_style=False, Dumper=Dumper) print >> self.stream, '---' # YAML objects separator except: print >> self.stream, yaml.dump_all(args, default_flow_style=False, Dumper=Dumper)
def save(self, filename): '''Save the model to file in YAML format.''' with open(filename, 'w') as fout: dump(self, fout, Dumper=Dumper)
def main(): Setting.generate_missing_shorthands() defaults = Setting.generate_defaults_dict() conf_parser = \ Setting.generate_parser(defaults, console_only=True, description= "Use one or more .mp4 video files to perform calibration: " + "find the cameras' intrinsics and/or extrinsics.") # ============== STORAGE/RETRIEVAL OF CONSOLE SETTINGS ===========================================# args, remaining_argv = conf_parser.parse_known_args() defaults[Setting.save_settings.name] = args.save_settings if args.settings_file: defaults[Setting.settings_file.name] = args.settings_file if osp.isfile(args.settings_file): file_stream = open(args.settings_file, "r", encoding="utf-8") config_defaults = load(file_stream, Loader=Loader) file_stream.close() for key, value in config_defaults.items(): defaults[key] = value else: raise ValueError("Settings file not found at: {0:s}".format(args.settings_file)) parser = Setting.generate_parser(defaults, parents=[conf_parser]) args = parser.parse_args(remaining_argv) # process "special" setting values if args.folder == "!settings_file_location": if args.settings_file and osp.isfile(args.settings_file): args.folder = osp.dirname(args.settings_file) # save settings if prompted to do so if args.save_settings and args.settings_file: setting_dict = vars(args) file_stream = open(args.settings_file, "w", encoding="utf-8") file_name = setting_dict[Setting.save_settings.name] del setting_dict[Setting.save_settings.name] del setting_dict[Setting.settings_file.name] dump(setting_dict, file_stream, Dumper=Dumper) file_stream.close() setting_dict[Setting.save_settings.name] = file_name setting_dict[Setting.settings_file.name] = True app = MultiStereoApplication(args)
def main(): Setting.generate_missing_shorthands() defaults = Setting.generate_defaults_dict() conf_parser = \ Setting.generate_parser(defaults, console_only=True, description= "Test stereo algorithms on two image files.") # ============== STORAGE/RETRIEVAL OF CONSOLE SETTINGS ===========================================# args, remaining_argv = conf_parser.parse_known_args() defaults[Setting.save_settings.name] = args.save_settings if args.settings_file: defaults[Setting.settings_file.name] = args.settings_file if osp.isfile(args.settings_file): file_stream = open(args.settings_file, "r", encoding="utf-8") config_defaults = load(file_stream, Loader=Loader) file_stream.close() if config_defaults: for key, value in config_defaults.items(): defaults[key] = value else: raise ValueError("Settings file not found at: {0:s}".format(args.settings_file)) parser = Setting.generate_parser(defaults, parents=[conf_parser]) args = parser.parse_args(remaining_argv) # process "special" setting values if args.folder == "!settings_file_location": if args.settings_file and osp.isfile(args.settings_file): args.folder = osp.dirname(args.settings_file) # save settings if prompted to do so if args.save_settings and args.settings_file: setting_dict = vars(args) file_stream = open(args.settings_file, "w", encoding="utf-8") file_name = setting_dict[Setting.save_settings.name] del setting_dict[Setting.save_settings.name] del setting_dict[Setting.settings_file.name] dump(setting_dict, file_stream, Dumper=Dumper) file_stream.close() setting_dict[Setting.save_settings.name] = file_name setting_dict[Setting.settings_file.name] = True app = StereoMatcherApp(args) app.disparity2()
def main(): Setting.generate_missing_shorthands() defaults = Setting.generate_defaults_dict() conf_parser = \ Setting.generate_parser(defaults, console_only=True, description= "Use one or more .mp4 video files to perform calibration: " + "find the cameras' intrinsics and/or extrinsics.") # ============== STORAGE/RETRIEVAL OF CONSOLE SETTINGS ===========================================# args, remaining_argv = conf_parser.parse_known_args() defaults[Setting.save_settings.name] = args.save_settings if args.settings_file: defaults[Setting.settings_file.name] = args.settings_file if osp.isfile(args.settings_file): file_stream = open(args.settings_file, "r", encoding="utf-8") config_defaults = load(file_stream, Loader=Loader) file_stream.close() for key, value in config_defaults.items(): defaults[key] = value else: raise ValueError("Settings file not found at: {0:s}".format(args.settings_file)) parser = Setting.generate_parser(defaults, parents=[conf_parser]) args = parser.parse_args(remaining_argv) # process "special" setting values if args.folder == "!settings_file_location": if args.settings_file and osp.isfile(args.settings_file): args.folder = osp.dirname(args.settings_file) # save settings if prompted to do so if args.save_settings and args.settings_file: setting_dict = vars(args) file_stream = open(args.settings_file, "w", encoding="utf-8") file_name = setting_dict[Setting.save_settings.name] del setting_dict[Setting.save_settings.name] del setting_dict[Setting.settings_file.name] dump(setting_dict, file_stream, Dumper=Dumper) file_stream.close() setting_dict[Setting.save_settings.name] = file_name setting_dict[Setting.settings_file.name] = True if args.unsynced: app = ApplicationUnsynced(args) app.gather_frame_data() app.calibrate_time_reprojection(save_data=True) else: app = ApplicationSynced(args) app.gather_frame_data() app.run_calibration() return 0
def process_arguments(program_arguments_enum, program_help_description): argproc = ArgumentProcessor(program_arguments_enum) defaults = argproc.generate_defaults_dict() conf_parser = \ argproc.generate_parser(defaults, console_only=True, description= "Test stereo algorithms on two image files.") # ============== STORAGE/RETRIEVAL OF CONSOLE SETTINGS ===========================================# args, remaining_argv = conf_parser.parse_known_args() defaults[ArgumentProcessor.save_settings_name] = args.save_settings if args.settings_file: defaults[ArgumentProcessor.settings_file_name] = args.settings_file if os.path.isfile(args.settings_file): file_stream = open(args.settings_file, "r", encoding="utf-8") config_defaults = load(file_stream, Loader=Loader) file_stream.close() if config_defaults: for key, value in config_defaults.items(): defaults[key] = value else: if not args.save_settings: raise ValueError("Settings file not found at: {0:s}".format(args.settings_file)) parser = argproc.generate_parser(defaults, parents=[conf_parser]) args = parser.parse_args(remaining_argv) # process "special" setting values if args.settings_file and os.path.isfile(args.settings_file): for key in args.__dict__.keys(): if key in argproc.setting_file_location_args and args.__dict__[key] == \ Argument.setting_file_location_wildcard: args.__dict__[key] = os.path.dirname(args.settings_file) # save settings if prompted to do so if args.save_settings and args.settings_file: setting_dict = vars(args) file_stream = open(args.settings_file, "w", encoding="utf-8") file_name = setting_dict[ArgumentProcessor.save_settings_name] del setting_dict[ArgumentProcessor.save_settings_name] del setting_dict[ArgumentProcessor.settings_file_name] dump(setting_dict, file_stream, Dumper=Dumper, indent=3, default_flow_style=False) file_stream.close() setting_dict[ArgumentProcessor.save_settings_name] = file_name setting_dict[ArgumentProcessor.settings_file_name] = True return args