コード例 #1
0
    def write(self, data, options):
        output_options = self.DEFAULT_OPTIONS \
            if options.custom_options is None \
            else merge_two_dicts(
                first=self.DEFAULT_OPTIONS,
                second=options.custom_options
            )

        if options.should_write_to_file():
            with open(options.file_path, options.mode.value, newline="") as json_file:
                return json.dump(data, json_file, cls=self.encoder, **output_options)

        return json.dumps(data, cls=self.encoder, **output_options)
コード例 #2
0
    def write(self, data, options):
        output_options = merge_two_dicts(DEFAULT_JSON_OPTIONS,
                                         options.formatting_options)
        if options.file_options.should_write_to_file:
            with open(options.file_options.path,
                      options.file_options.mode.value,
                      newline="",
                      encoding="utf8") as json_file:
                return json.dump(
                    data,
                    json_file,
                    cls=self.value_formatter,
                    **output_options,
                )

        return json.dumps(
            data,
            cls=self.value_formatter,
            **output_options,
        )
コード例 #3
0
    def of(file_options, output_type, json_options=None, csv_options=None):
        if output_type == OutputType.JSON:
            if json_options is None:
                formatting_options = DEFAULT_JSON_OPTIONS
            else:
                formatting_options = merge_two_dicts(DEFAULT_JSON_OPTIONS,
                                                     json_options)
        elif output_type == OutputType.CSV:
            formatting_options = csv_options
        elif output_type is None:
            return OutputOptions(file_options=None,
                                 formatting_options={},
                                 output_type=None)
        else:
            raise ValueError("Unknown output type: {output_type}".format(
                output_type=output_type))

        return OutputOptions(
            file_options=file_options,
            formatting_options=formatting_options,
            output_type=output_type,
        )
コード例 #4
0
 def test_merge_non_empty_dicts_with_shared_keys(self):
     self.assertEqual({"jae": "baebae2"},
                      merge_two_dicts({"jae": "baebae"},
                                      {"jae": "baebae2"}))
コード例 #5
0
 def test_merge_non_empty_dicts_with_unique_keys(self):
     self.assertEqual({
         "jae": "baebae",
         "bae": "jadley"
     }, merge_two_dicts({"jae": "baebae"}, {"bae": "jadley"}))
コード例 #6
0
 def test_merges_non_empty_dict_with_empty_dict(self):
     self.assertEqual({"jae": "baebae"},
                      merge_two_dicts({"jae": "baebae"}, {}))
コード例 #7
0
 def test_merges_two_empty_dicts(self):
     self.assertEqual({}, merge_two_dicts({}, {}))