def test_store_data_from_json(): original_json = open(JSON_FILE).read().strip() json_dict = from_json(original_json) store_data = to_model(StoreData, json_dict) assert store_data.name == "Acme store" assert store_data.id == 982 assert store_data.data_from == datetime(2017, 12, 18, 0, 0) assert store_data.data_to == datetime(2017, 12, 19, 23, 59, 59) assert len(store_data.days) == 2 assert isinstance(store_data.days[0].open_at, time) assert store_data.days[0].date == datetime(2017, 12, 18).date() assert store_data.days[0].open_at == time(8, 0, 0) assert store_data.days[0].closed_on == time(19, 0, 0) assert store_data.days[0].customers == int(487) assert store_data.days[0].day_type == DayType.NORMAL assert store_data.days[0].sales == float(27223.65) assert store_data.days[1].date == datetime(2017, 12, 19).date() assert store_data.days[1].open_at == time(10, 30, 0) assert store_data.days[1].closed_on == time(17, 30, 0) assert store_data.days[1].customers == int(192) assert store_data.days[1].day_type == DayType.HOLIDAY assert store_data.days[1].sales is None generated_json = to_json(store_data, suppress_empty_values=True, suppress_map_key_values=True).replace(" ", "") print(original_json) print(generated_json) assert original_json.replace(" ", "") == generated_json
def diff_metric_configs(previous_metric_configs, current_metric_configs): diff = {"added": [], "modified": [], "deleted": []} previous_metric_config_keys = [i.tag_key for i in previous_metric_configs] previous_metric_json_configs = [ related.to_json(i) for i in previous_metric_configs ] current_metric_config_keys = [i.tag_key for i in current_metric_configs] for current_metric_config in current_metric_configs: if current_metric_config.tag_key not in previous_metric_config_keys: diff["added"].append(related.to_dict(current_metric_config)) elif related.to_json( current_metric_config) not in previous_metric_json_configs: diff["modified"].append(related.to_dict(current_metric_config)) for previous_metric_config in previous_metric_configs: if previous_metric_config.tag_key not in current_metric_config_keys: diff["deleted"].append(related.to_dict(previous_metric_config)) return diff
def get_question(meta, id, format): """ Dumps a question in the specified format """ question = meta.find_question(id) if question is None: raise click.ClickException("Question with specified ID was not found") text = '' if format == 'json': text = related.to_json(question) else: text = related.to_yaml(question) click.echo(text)
def get_kwargs(self): kw = self.kwargs.copy() data = kw.get("data", None) files = data and data.get("files") if files and isinstance(files, dict): file_upload = files.pop("file_upload", None) if file_upload: files["upload-file"] = file_upload if not self.is_form: kw["data"] = related.to_json(data) # unlimited timeout if not specified kw.setdefault("timeout", None) return kw
def generate_json(self, output_path): get_logger().debug("generate json", output_path=output_path) success = False try: cucumber = Cucumber.create(self.suite_result) path = os.path.join(output_path, self.CUCUMBER_JSON) file = open(path, "w+") file.write(related.to_json(cucumber)) file.close() get_logger().debug("created cucumber json", path=path) success = True except Exception as e: # pragma: no cover get_logger().error("failed cucumber json", error=str(e)) raise e return success
def test_train_detector_for_latency_b(self): detector_config = dict(hyperparams=dict(upper_weak_multiplier=1.05, upper_strong_multiplier=1.10, hampel_window_size=10, hampel_n_sigma=3, strategy="highwatermark")) detector_class = build_detector("constant-detector", detector_config) data = read_csv("./tests/data/latency_test_b.csv", header=0, usecols=[1], squeeze=True) detector_class.train(data, "LATENCY") print("detector model data after training:\n", related.to_json(detector_class)) assert isclose( detector_class.config.params.thresholds.strong_upper_threshold, 0.612911, rel_tol=0.0001) assert isclose( detector_class.config.params.thresholds.weak_upper_threshold, 0.585052, rel_tol=0.0001)
def get_kwargs(self, is_aiohttp): kw = self.kwargs.copy() data = kw.get("data", None) # aiohttp is different from requests in handling files # http://aiohttp.readthedocs.io/en/stable/client.html # http://docs.python-requests.org/en/master/user/quickstart files = kw.pop("files", None) if is_aiohttp else None if self.is_form: if isinstance(data, dict) and isinstance(files, dict): data.update(files) else: kw['data'] = related.to_json(data) # unlimited timeout if not specified kw.setdefault("timeout", None) # add verify = False for requests if not is_aiohttp: kw['verify'] = False return kw
def to_json(self): return related.to_json(self)
def test_roundtrip_json(company): new_company = from_json(to_json(company), Company) assert new_company == company
def as_json(self) -> str: """Serialize the object as a JSON string. :rtype: str """ return to_json(self)
def json(self): return json.loads(to_json(self))