|
2 | 2 | import io |
3 | 3 | import logging |
4 | 4 | import json |
5 | | -import copy |
6 | 5 |
|
7 | | -from typing import NoReturn, Tuple |
| 6 | +from typing import NoReturn |
8 | 7 |
|
9 | | -from rocketpy import Function |
10 | 8 | from rocketpy._encoders import RocketPyEncoder |
11 | 9 | from starlette.datastructures import Headers, MutableHeaders |
12 | 10 | from starlette.types import ASGIApp, Message, Receive, Scope, Send |
13 | 11 |
|
14 | 12 | logger = logging.getLogger(__name__) |
15 | 13 |
|
16 | 14 |
|
17 | | -class DiscretizeConfig: |
18 | | - """ |
19 | | - Configuration class for RocketPy function discretization. |
20 | | -
|
21 | | - This class allows easy configuration of discretization parameters |
22 | | - for different types of RocketPy objects and their callable attributes. |
23 | | - """ |
24 | | - |
25 | | - def __init__( |
26 | | - self, bounds: Tuple[float, float] = (0, 10), samples: int = 200 |
27 | | - ): |
28 | | - self.bounds = bounds |
29 | | - self.samples = samples |
30 | | - |
31 | | - @classmethod |
32 | | - def for_environment(cls) -> 'DiscretizeConfig': |
33 | | - return cls(bounds=(0, 50000), samples=100) |
34 | | - |
35 | | - @classmethod |
36 | | - def for_motor(cls) -> 'DiscretizeConfig': |
37 | | - return cls(bounds=(0, 10), samples=150) |
38 | | - |
39 | | - @classmethod |
40 | | - def for_rocket(cls) -> 'DiscretizeConfig': |
41 | | - return cls(bounds=(0, 1), samples=100) |
42 | | - |
43 | | - @classmethod |
44 | | - def for_flight(cls) -> 'DiscretizeConfig': |
45 | | - return cls(bounds=(0, 30), samples=200) |
46 | | - |
47 | | - |
48 | | -def rocketpy_encoder(obj, config: DiscretizeConfig = DiscretizeConfig()): |
| 15 | +def rocketpy_encoder(obj): |
49 | 16 | """ |
50 | 17 | Encode a RocketPy object using official RocketPy encoders. |
51 | 18 |
|
52 | | - This function creates a copy of the object, discretizes callable Function |
53 | | - attributes on the copy, and then uses RocketPy's official RocketPyEncoder for |
54 | | - complete object serialization. The original object remains unchanged. |
| 19 | + This function uses RocketPy's official RocketPyEncoder for complete |
| 20 | + object serialization. |
55 | 21 |
|
56 | 22 | Args: |
57 | 23 | obj: RocketPy object (Environment, Motor, Rocket, Flight) |
58 | | - config: DiscretizeConfig object with discretization parameters (optional) |
59 | 24 |
|
60 | 25 | Returns: |
61 | 26 | Dictionary of encoded attributes |
62 | 27 | """ |
63 | 28 |
|
64 | | - # Create a copy to avoid mutating the original object |
65 | | - obj_copy = copy.deepcopy(obj) |
66 | | - |
67 | | - for attr_name in dir(obj_copy): |
68 | | - if attr_name.startswith('_'): |
69 | | - continue |
70 | | - |
71 | | - try: |
72 | | - attr_value = getattr(obj_copy, attr_name) |
73 | | - except Exception: |
74 | | - continue |
75 | | - |
76 | | - if callable(attr_value) and isinstance(attr_value, Function): |
77 | | - try: |
78 | | - discretized_func = Function(attr_value.source) |
79 | | - discretized_func.set_discrete( |
80 | | - lower=config.bounds[0], |
81 | | - upper=config.bounds[1], |
82 | | - samples=config.samples, |
83 | | - mutate_self=True, |
84 | | - ) |
85 | | - |
86 | | - setattr(obj_copy, attr_name, discretized_func) |
87 | | - |
88 | | - except Exception as e: |
89 | | - logger.warning(f"Failed to discretize {attr_name}: {e}") |
90 | | - |
91 | | - try: |
92 | | - json_str = json.dumps( |
93 | | - obj_copy, |
94 | | - cls=RocketPyEncoder, |
95 | | - include_outputs=True, |
96 | | - include_function_data=True, |
97 | | - ) |
98 | | - return json.loads(json_str) |
99 | | - except Exception as e: |
100 | | - logger.warning(f"Failed to encode with RocketPyEncoder: {e}") |
101 | | - attributes = {} |
102 | | - for attr_name in dir(obj_copy): |
103 | | - if not attr_name.startswith('_'): |
104 | | - try: |
105 | | - attr_value = getattr(obj_copy, attr_name) |
106 | | - if not callable(attr_value): |
107 | | - attributes[attr_name] = str(attr_value) |
108 | | - except Exception: |
109 | | - continue |
110 | | - return attributes |
| 29 | + json_str = json.dumps( |
| 30 | + obj, |
| 31 | + cls=RocketPyEncoder, |
| 32 | + include_outputs=True, |
| 33 | + include_function_data=True, |
| 34 | + discretize=True, |
| 35 | + pickle_callables=False, |
| 36 | + ) |
| 37 | + return json.loads(json_str) |
111 | 38 |
|
112 | 39 |
|
113 | 40 | class RocketPyGZipMiddleware: |
|
0 commit comments