Python dataclasses.asdict() Examples

The following are 30 code examples of dataclasses.asdict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dataclasses , or try the search function .
Example #1
Source File: test_rva.py    From eyeD3 with GNU General Public License v3.0 6 votes vote down vote up
def test_v23_bounds():
    f = RelVolAdjFrameV23()
    adjustments = dataclasses.asdict(RelVolAdjFrameV23.VolumeAdjustments())

    for a in adjustments.keys():
        values = dict(adjustments)
        for value, raises in [
            (65537, True), (-65537, True),
            (65536, False), (-65536, False),
            (32769, False), (-32768, False),
            (777, False), (-999, False),
            (0, False), (-0, False),
        ]:
            values[a] = value
            if raises:
                with pytest.raises(ValueError):
                    f.adjustments = RelVolAdjFrameV23.VolumeAdjustments(**values)
                    f.render()
            else:
                f.adjustments = RelVolAdjFrameV23.VolumeAdjustments(**values)
                f.render()

        assert dataclasses.asdict(f.adjustments)[a] == value 
Example #2
Source File: test_json.py    From sslyze with GNU Affero General Public License v3.0 6 votes vote down vote up
def test(self):
        # Given a completed scan for a server with the CERTIFICATE_INFO scan command
        server_location = ServerNetworkLocationViaDirectConnection.with_ip_address_lookup("www.facebook.com", 443)
        server_info = ServerConnectivityTester().perform(server_location)
        plugin_result = CertificateInfoImplementation.scan_server(server_info)
        scan_results = {ScanCommand.CERTIFICATE_INFO: plugin_result}
        scan_result = ServerScanResultFactory.create(scan_commands_results=scan_results)

        # When converting it into to JSON
        result_as_json = json.dumps(asdict(scan_result), cls=sslyze.JsonEncoder)

        # It succeeds
        assert result_as_json

        # And complex object like certificates were properly serialized
        assert "notBefore" in result_as_json
        assert "issuer" in result_as_json
        assert "subject" in result_as_json 
Example #3
Source File: test_certificate_algorithms.py    From sslyze with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_rsa_certificate(self):
        # Given a server that is configured with an RSA certificate
        with ModernOpenSslServer() as server:
            server_location = ServerNetworkLocationViaDirectConnection(
                hostname=server.hostname, port=server.port, ip_address=server.ip_address
            )
            server_info = ServerConnectivityTester().perform(server_location)

            # When running the scan, it succeeds
            scan_result = CertificateInfoImplementation.scan_server(server_info)
            assert scan_result.certificate_deployments[0].received_certificate_chain

            # And the result can be converted to JSON
            result_as_json = json.dumps(asdict(scan_result), cls=JsonEncoder)
            assert result_as_json

            # And the result can be converted to console output
            result_as_txt = CertificateInfoImplementation.cli_connector_cls.result_to_console_output(scan_result)
            assert result_as_txt 
Example #4
Source File: test_certificate_algorithms.py    From sslyze with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_ed25519_certificate(self):
        # Given a server that is configured with an ED25519 certificate
        with ModernOpenSslServer(
            server_certificate_path=Path(__file__).parent.absolute() / "server-ed25519-cert.pem",
            server_key_path=Path(__file__).parent.absolute() / "server-ed25519-key.pem",
        ) as server:
            server_location = ServerNetworkLocationViaDirectConnection(
                hostname=server.hostname, port=server.port, ip_address=server.ip_address
            )
            server_info = ServerConnectivityTester().perform(server_location)

            # When running the scan, it succeeds
            scan_result = CertificateInfoImplementation.scan_server(server_info)
            assert scan_result.certificate_deployments[0].received_certificate_chain

            # And the result can be converted to JSON
            result_as_json = json.dumps(asdict(scan_result), cls=JsonEncoder)
            assert result_as_json

            # And the result can be converted to console output
            result_as_txt = CertificateInfoImplementation.cli_connector_cls.result_to_console_output(scan_result)
            assert result_as_txt 
Example #5
Source File: schemes.py    From wechatpy with MIT License 6 votes vote down vote up
def to_json(self) -> str:
        d = {k: v for k, v in dataclasses.asdict(self).items() if v}
        return json.dumps(d, ensure_ascii=False) 
Example #6
Source File: response.py    From selene-backend with GNU Affero General Public License v3.0 6 votes vote down vote up
def coerce_response(response_data):
    """Coerce response data to JSON serializable object with camelCase keys

    Recursively walk through the response object in case there are things
    like nested dataclasses that need to be reformatted

    :param response_data: data returned from the API request
    :returns: the same data but with keys in camelCase
    """
    if is_dataclass(response_data):
        coerced = {
            snake_to_camel(key): coerce_response(value)
            for key, value in asdict(response_data).items()
        }
    elif isinstance(response_data, dict):
        coerced = {
            snake_to_camel(key): coerce_response(value)
            for key, value in response_data.items()
        }
    elif isinstance(response_data, list):
        coerced = [coerce_response(item) for item in response_data]
    else:
        coerced = response_data

    return coerced 
Example #7
Source File: data.py    From dffml with MIT License 6 votes vote down vote up
def export_value(obj, key, value):
    # export and _asdict are not classmethods
    if hasattr(value, "ENTRY_POINT_ORIG_LABEL") and hasattr(value, "config"):
        obj[key] = {"plugin": value.ENTRY_POINT_ORIG_LABEL}
        export_value(obj[key], "config", value.config)
    elif inspect.isclass(value):
        obj[key] = value.__qualname__
    elif isinstance(value, (pathlib.Path, uuid.UUID)):
        obj[key] = str(value)
    elif hasattr(value, "export"):
        obj[key] = value.export()
    elif hasattr(value, "_asdict"):
        obj[key] = value._asdict()
    elif getattr(type(value), "__module__", None) == "numpy" and isinstance(
        getattr(value, "flatten", None), collections.Callable
    ):
        obj[key] = tuple(value.flatten())
    elif dataclasses.is_dataclass(value):
        obj[key] = export_dict(**dataclasses.asdict(value))
    elif getattr(value, "__module__", None) == "typing":
        obj[key] = STANDARD_TYPES.get(
            str(value).replace("typing.", ""), "generic"
        ) 
Example #8
Source File: simulator.py    From whynot with MIT License 6 votes vote down vote up
def set_config(js_context, config, intervention):
    """Set the non-state variables of the world3 simulator."""
    # Set global simulator parameters
    js_context.eval(f"startTime = {config.start_time}")
    js_context.eval(f"stopTime = {config.end_time}")
    js_context.eval(f"dt = {config.delta_t}")

    if intervention:
        intervention_config = config.update(intervention)
        js_context.eval(f"policyYear = {intervention.time}")
    else:
        intervention_config = config
        js_context.eval(f"policyYear = {config.end_time}")

    intervention_config = dataclasses.asdict(intervention_config)
    for parameter, before in dataclasses.asdict(config).items():
        if parameter in ["policy_year", "start_time", "end_time", "delta_t"]:
            continue
        after = intervention_config[parameter]
        js_context.eval(f"{to_camel_case(parameter)}.before = {before}")
        js_context.eval(f"{to_camel_case(parameter)}.after = {after}")
    js_context.eval("resetModel()") 
Example #9
Source File: test_metar.py    From avwx-engine with MIT License 6 votes vote down vote up
def test_metar_ete(self):
        """
        Performs an end-to-end test of all METAR JSON files
        """
        for ref, icao, issued in get_data(__file__, "metar"):
            station = metar.Metar(icao)
            raw = ref["data"]["raw"]
            self.assertEqual(station.sanitize(raw), ref["data"]["sanitized"])
            self.assertIsNone(station.last_updated)
            self.assertIsNone(station.issued)
            self.assertTrue(station.update(raw, issued=issued))
            self.assertIsInstance(station.last_updated, datetime)
            self.assertEqual(station.issued, issued)
            self.assertEqual(asdict(station.data), ref["data"])
            self.assertEqual(asdict(station.translations), ref["translations"])
            self.assertEqual(station.summary, ref["summary"])
            self.assertEqual(station.speech, ref["speech"]) 
Example #10
Source File: cloudsync.py    From integrations with Apache License 2.0 6 votes vote down vote up
def sync(name, metrics):
    """Write markdown docs"""
    metrics_file = Path().resolve().parent / name / "metrics.yaml"
    cur = {}

    if metrics_file.exists():
        cur = yaml.round_trip_load(metrics_file.read_text())

    for m in metrics:
        entry = cur.setdefault(m.title, asdict(m))

        # If the fetched value exists override it, otherwise leave the current one alone.
        if m.description:
            entry["description"] = m.description
        if m.brief:
            entry["brief"] = m.brief
        if m.metric_type:
            entry["metric_type"] = m.metric_type

    with metrics_file.open("wt") as f:
        yaml.round_trip_dump(cur, f) 
Example #11
Source File: buffer.py    From imitation with MIT License 6 votes vote down vote up
def store(self, transitions: types.Transitions, truncate_ok: bool = True) -> None:
        """Store obs-act-obs triples.

        Args:
          transitions: Transitions to store.
          truncate_ok: If False, then error if the length of `transitions` is
            greater than `self.capacity`. Otherwise, store only the final
            `self.capacity` transitions.

        Raises:
            ValueError: The arguments didn't have the same length.
        """
        trans_dict = dataclasses.asdict(transitions)
        # Remove unnecessary fields
        trans_dict = {k: trans_dict[k] for k in self._buffer.sample_shapes.keys()}

        if len(transitions) > self.capacity and truncate_ok:
            trans_dict = {k: v[-self.capacity :] for k, v in trans_dict.items()}

        self._buffer.store(trans_dict) 
Example #12
Source File: dataset.py    From imitation with MIT License 6 votes vote down vote up
def __init__(
        self,
        transitions: types.Transitions,
        dict_dataset_cls: Type[DictDataset] = RandomDictDataset,
        dict_dataset_cls_kwargs: Optional[Mapping] = None,
    ):
        """Adapts a `DictDataset` class to build `Transitions` `Dataset`.

        Args:
            transitions: An `Transitions` instance to be sampled from and stored
                internally as a `dict` of copied arrays. Note that `sample` will return
                the same type of `Transitions` as the type of this arg. In other words,
                if `transitions` is an instance of `TransitionsWithRew`, then
                `.sample()` also returns `TransitionsWithRew`.
            dict_dataset_cls: `DictDataset` class to be adapted.
            dict_dataset_kwargs: Optional kwargs for initializing `DictDataset` class.
        """
        data_map: Dict[str, np.ndarray] = dataclasses.asdict(transitions)
        kwargs = dict_dataset_cls_kwargs or {}
        self.transitions_cls: Type[types.Transitions] = type(transitions)
        self.simple_dataset = dict_dataset_cls(
            data_map, **kwargs  # pytype: disable=not-instantiable
        ) 
Example #13
Source File: test_nelson_siegel_svensson.py    From nelson_siegel_svensson with MIT License 6 votes vote down vote up
def test_curve_parameters(self):
        '''Test curve parameter.'''
        param = cli.Curve()
        self.assertRaises(click.BadParameter, param.convert,
                          value='', param=None, ctx=None)
        self.assertRaises(click.BadParameter, param.convert,
                          value='{}', param=None, ctx=None)
        missing_tau = '{"beta0": 0.017, "beta1": -0.023, "beta2": 0.24}'
        self.assertRaises(click.BadParameter, param.convert,
                          value=missing_tau, param=None, ctx=None)
        self.assertEqual(self.y1,
                         param.convert(json.dumps(asdict(self.y1)),
                                       None, None))
        self.assertEqual(self.y2,
                         param.convert(json.dumps(asdict(self.y2)),
                                       None, None)) 
Example #14
Source File: test_data.py    From imitation with MIT License 6 votes vote down vote up
def test_sample_sizes_and_types(
        self,
        trans_ds,
        transitions,
        trans_ds_rew,
        transitions_rew,
        max_batch_size=50,
        n_checks=30,
    ):
        """Check for correct sample shapes and dtypes."""
        for ds, trans in [(trans_ds, transitions), (trans_ds_rew, transitions_rew)]:
            trans_dict = dataclasses.asdict(trans)
            for _ in range(n_checks):
                n_samples = np.random.randint(max_batch_size) + 1
                sample = ds.sample(n_samples)
                assert isinstance(sample, type(trans))
                for k, v in dataclasses.asdict(sample).items():
                    trans_v: np.ndarray = trans_dict[k]
                    assert v.dtype == trans_v.dtype
                    assert v.shape[1:] == trans_v.shape[1:]
                    assert v.shape[0] == n_samples 
Example #15
Source File: base.py    From dffml with MIT License 5 votes vote down vote up
def config_asdict(self, *args, **kwargs):
    return export_dict(**dataclasses.asdict(self, *args, **kwargs)) 
Example #16
Source File: build_tests.py    From avwx-engine with MIT License 5 votes vote down vote up
def make_metar_test(station: str) -> dict:
    """
    Builds METAR test file for station
    """
    m = avwx.Metar(station)
    m.update()
    return {
        "data": asdict(m.data),
        "translations": asdict(m.translations),
        "summary": m.summary,
        "speech": m.speech,
    } 
Example #17
Source File: test_taf.py    From avwx-engine with MIT License 5 votes vote down vote up
def test_taf_ete(self):
        """
        Performs an end-to-end test of all TAF JSON files
        """
        for ref, icao, issued in get_data(__file__, "taf"):
            station = taf.Taf(icao)
            self.assertIsNone(station.last_updated)
            self.assertIsNone(station.issued)
            self.assertTrue(station.update(ref["data"]["raw"], issued=issued))
            self.assertIsInstance(station.last_updated, datetime)
            self.assertEqual(station.issued, issued)
            self.assertEqual(asdict(station.data), ref["data"])
            self.assertEqual(asdict(station.translations), ref["translations"])
            self.assertEqual(station.summary, ref["summary"])
            self.assertEqual(station.speech, ref["speech"]) 
Example #18
Source File: test_pirep.py    From avwx-engine with MIT License 5 votes vote down vote up
def test_pirep_ete(self):
        """
        Performs an end-to-end test of all PIREP JSON files
        """
        for ref, icao, issued in get_data(__file__, "pirep"):
            station = pirep.Pireps(icao)
            self.assertIsNone(station.last_updated)
            self.assertIsNone(station.issued)
            reports = [report["data"]["raw"] for report in ref["reports"]]
            self.assertTrue(station.update(reports, issued=issued))
            self.assertIsInstance(station.last_updated, datetime)
            self.assertEqual(station.issued, issued)
            for parsed, report in zip(station.data, ref["reports"]):
                self.assertEqual(asdict(parsed), report["data"]) 
Example #19
Source File: streamable.py    From chia-blockchain with Apache License 2.0 5 votes vote down vote up
def __str__(self: Any) -> str:
        return pp.pformat(self.recurse_jsonify(dataclasses.asdict(self))) 
Example #20
Source File: test_gfs.py    From avwx-engine with MIT License 5 votes vote down vote up
def _test_gfs_ete(self, report: "Forecast"):
        """
        Performs an end-to-end test of all report JSON files
        """
        for ref, icao, issued in get_data(__file__, report.__class__.__name__.lower()):
            station = report(icao)
            self.assertIsNone(station.last_updated)
            self.assertIsNone(station.issued)
            self.assertTrue(station.update(ref["data"]["raw"]))
            self.assertIsInstance(station.last_updated, datetime)
            self.assertEqual(station.issued, issued)
            # Clear timestamp due to parse_date limitations
            self.assertEqual(asdict(station.data), ref["data"]) 
Example #21
Source File: test_main.py    From arq with MIT License 5 votes vote down vote up
def test_get_jobs(arq_redis: ArqRedis):
    await arq_redis.enqueue_job('foobar', a=1, b=2, c=3)
    await asyncio.sleep(0.01)
    await arq_redis.enqueue_job('second', 4, b=5, c=6)
    await asyncio.sleep(0.01)
    await arq_redis.enqueue_job('third', 7, b=8)
    jobs = await arq_redis.queued_jobs()
    assert [dataclasses.asdict(j) for j in jobs] == [
        {
            'function': 'foobar',
            'args': (),
            'kwargs': {'a': 1, 'b': 2, 'c': 3},
            'job_try': None,
            'enqueue_time': CloseToNow(),
            'score': AnyInt(),
        },
        {
            'function': 'second',
            'args': (4,),
            'kwargs': {'b': 5, 'c': 6},
            'job_try': None,
            'enqueue_time': CloseToNow(),
            'score': AnyInt(),
        },
        {
            'function': 'third',
            'args': (7,),
            'kwargs': {'b': 8},
            'job_try': None,
            'enqueue_time': CloseToNow(),
            'score': AnyInt(),
        },
    ]
    assert jobs[0].score < jobs[1].score < jobs[2].score
    assert isinstance(jobs[0], JobDef)
    assert isinstance(jobs[1], JobDef)
    assert isinstance(jobs[2], JobDef) 
Example #22
Source File: streamable.py    From chia-blockchain with Apache License 2.0 5 votes vote down vote up
def to_json_dict(self) -> Dict:
        return self.recurse_jsonify(dataclasses.asdict(self)) 
Example #23
Source File: cron.py    From arq with MIT License 5 votes vote down vote up
def _get_next_dt(dt_: datetime, options: Options) -> Optional[datetime]:  # noqa: C901
    for field, v in dataclasses.asdict(options).items():
        if v is None:
            continue
        if field == 'weekday':
            next_v = dt_.weekday()
        else:
            next_v = getattr(dt_, field)
        if isinstance(v, int):
            mismatch = next_v != v
        else:
            assert isinstance(v, (set, list, tuple)), v
            mismatch = next_v not in v
        # print(field, v, next_v, mismatch)
        if mismatch:
            micro = max(dt_.microsecond - options.microsecond, 0)
            if field == 'month':
                if dt_.month == 12:
                    return datetime(dt_.year + 1, 1, 1)
                else:
                    return datetime(dt_.year, dt_.month + 1, 1)
            elif field in ('day', 'weekday'):
                return (
                    dt_
                    + timedelta(days=1)
                    - timedelta(hours=dt_.hour, minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
                )
            elif field == 'hour':
                return dt_ + timedelta(hours=1) - timedelta(minutes=dt_.minute, seconds=dt_.second, microseconds=micro)
            elif field == 'minute':
                return dt_ + timedelta(minutes=1) - timedelta(seconds=dt_.second, microseconds=micro)
            elif field == 'second':
                return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
            else:
                assert field == 'microsecond', field
                return dt_ + timedelta(microseconds=options.microsecond - dt_.microsecond)
    return None 
Example #24
Source File: actions.py    From forest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def to_dict(self):
        return asdict(self) 
Example #25
Source File: schemes.py    From wechatpy with MIT License 5 votes vote down vote up
def to_dict(self) -> Dict:
        return dataclasses.asdict(self) 
Example #26
Source File: shipping.py    From clean-architecture with MIT License 5 votes vote down vote up
def get_next_package(query: GetNextPackage) -> Response:
    result = query.query()
    if not result:
        abort(404)
    return make_response(jsonify(dataclasses.asdict(result)))  # type: ignore 
Example #27
Source File: logger.py    From nekoyume with MIT License 5 votes vote down vote up
def json_dump(self):
        status = []
        for log in self.logs:
            status.append(asdict(log))
        return json.dumps({'status': status}) 
Example #28
Source File: base.py    From nekoyume with MIT License 5 votes vote down vote up
def __str__(self):
        return str(asdict(self)) 
Example #29
Source File: utils.py    From NLM with MIT License 5 votes vote down vote up
def convert_graphobj_to_dict(graphobj):
    """
    A graphobj is a GraphNode or GraphRelation
    """
    dct = asdict(graphobj)
    if "props" in dct:
        dct["props"] = json.dumps(dct["props"])
    if "start" in dct:
        start_props = dct["start"]["props"]
        dct["start"]["props"] = json.dumps(start_props)
    if "end" in dct:
        end_props = dct["end"]["props"]
        dct["end"]["props"] = json.dumps(end_props)
    return dct 
Example #30
Source File: build_tests.py    From avwx-engine with MIT License 5 votes vote down vote up
def make_pirep_test(station: str) -> [dict]:
    """
    Builds PIREP test file for station
    """
    p = avwx.Pireps(station)
    p.update()
    ret = []
    if not p.data:
        return
    for report in p.data:
        ret.append({"data": asdict(report)})
    return {"reports": ret, "station": asdict(p.station)}