Skip to content

Commit 9bc336c

Browse files
Fix pylint and getattr TypeErrors
1 parent 75ca93f commit 9bc336c

8 files changed

Lines changed: 75 additions & 30 deletions

File tree

rocketserializer/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def ork2json(filepath, output=None, ork_jar=None, encoding="utf-8", verbose=Fals
217217
@click.option("--ork_jar", type=str, default=None, required=False)
218218
@click.option("--encoding", type=str, default="utf-8", required=False)
219219
@click.option("--verbose", is_flag=True, default=False, help="Enable verbose logging")
220-
def ork2notebook(filepath, output, ork_jar=None, encoding="utf-8", verbose=False):
220+
def ork2notebook(filepath, output, ork_jar=None, encoding="utf-8", verbose=False): # pylint: disable=unused-argument
221221
"""Generates a .ipynb file from the .ork file.
222222
223223
Notes

rocketserializer/components/fins.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ def get_element_by_name(name):
7070
logger.info("Span retrieved: %f", span)
7171

7272
sweep_length = (
73-
float(getattr(fin.find("sweeplength"), "text", "0")) if fin.find("sweeplength") else None
73+
float(getattr(fin.find("sweeplength"), "text", "0"))
74+
if fin.find("sweeplength")
75+
else None
7476
)
7577
sweep_angle = (
76-
float(getattr(fin.find("sweepangle"), "text", "0")) if fin.find("sweepangle") else None
78+
float(getattr(fin.find("sweepangle"), "text", "0"))
79+
if fin.find("sweepangle")
80+
else None
7781
)
7882
logger.info(
7983
"Sweep length and angle retrieved: %s, %s", sweep_length, sweep_angle

rocketserializer/components/id.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,31 @@ def search_id_info(bs, filepath):
2424
"filepath".
2525
"""
2626
settings = {}
27-
settings["rocket_name"] = bs.find(getattr("rocket").find("name"), "text", "")
28-
logger.info("Collected the rocket name: '%s'", settings["rocket_name"])
29-
30-
try:
31-
settings["comment"] = bs.find(getattr("rocket").find("comment"), "text", "").replace("\n", "")
32-
logger.info("Collected the comment saved in the file: %s", settings["comment"])
33-
except AttributeError:
34-
logger.warning("No auxiliary comment was found in the file.")
27+
rocket_tag = bs.find("rocket")
28+
if rocket_tag:
29+
settings["rocket_name"] = getattr(rocket_tag.find("name"), "text", "")
30+
logger.info("Collected the rocket name: '%s'", settings["rocket_name"])
31+
32+
comment_tag = rocket_tag.find("comment")
33+
if comment_tag and comment_tag.text:
34+
settings["comment"] = comment_tag.text.replace("\n", "")
35+
logger.info(
36+
"Collected the comment saved in the file: %s", settings["comment"]
37+
)
38+
else:
39+
logger.warning("No auxiliary comment was found in the file.")
40+
settings["comment"] = None
41+
42+
designer_tag = rocket_tag.find("designer")
43+
if designer_tag and designer_tag.text:
44+
settings["designer"] = designer_tag.text
45+
logger.info("Collected the designer name: %s", settings["designer"])
46+
else:
47+
logger.warning("No designer name was found in the file.")
48+
settings["designer"] = None
49+
else:
50+
settings["rocket_name"] = ""
3551
settings["comment"] = None
36-
try:
37-
settings["designer"] = bs.find(getattr("rocket").find("designer"), "text", "")
38-
logger.info("Collected the designer name: %s", settings["designer"])
39-
except AttributeError:
40-
logger.warning("No designer name was found in the file.")
4152
settings["designer"] = None
4253
# settings["ork_version"] = bs.attrs["creator"]
4354
settings["filepath"] = Path(filepath).as_posix()

rocketserializer/components/motor.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ def search_motor(bs, datapoints, data_labels):
3939
settings = {}
4040

4141
# retrieve motor geometry
42-
motor_length = float(bs.find(getattr("motormount").find("length"), "text", ""))
43-
motor_radius = float(bs.find(getattr("motormount").find("diameter"), "text", "")) / 2
42+
motormount = bs.find("motormount")
43+
if motormount is not None:
44+
motor_length = float(getattr(motormount.find("length"), "text", "") or "0")
45+
diam = getattr(motormount.find("diameter"), "text", "") or "0"
46+
motor_radius = float(diam) / 2
47+
else:
48+
motor_length = 0.0
49+
motor_radius = 0.0
4450
logger.info("Collected motor geometry: motor length and motor radius.")
4551

4652
# get motor mass properties
@@ -183,7 +189,9 @@ def __get_motor_mass(datapoints, data_labels):
183189
prop_mass_vector = list(prop_mass_vector)
184190
logger.info("The motor dry mass is %.3f kg.", motor_dry_mass)
185191
else:
186-
raise ValueError("Neither 'Propellant mass' nor 'Motor mass' found in data_labels.")
192+
raise ValueError(
193+
"Neither 'Propellant mass' nor 'Motor mass' found in data_labels."
194+
)
187195

188196
normalize = np.array(prop_mass_vector)
189197
normalize = normalize - normalize[np.argmin(normalize)]

rocketserializer/components/nose_cone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def search_nosecone(bs, elements=None, rocket_radius=None, just_radius=False):
3131
if not nosecone:
3232
nosecones = list(
3333
filter(
34-
lambda x: getattr(x.find("name"), "text", "") == "Nosecone", bs.find_all("transition")
34+
lambda x: getattr(x.find("name"), "text", "") == "Nosecone",
35+
bs.find_all("transition"),
3536
)
3637
)
3738
if len(nosecones) == 0:

rocketserializer/components/parachute.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def search_parachutes(bs):
3333
name = getattr(chute.find("name"), "text", "")
3434

3535
# parachute settings
36-
cd = "auto" if "auto" in getattr(chute.find("cd"), "text", "") else float(getattr(chute.find("cd"), "text", "0"))
36+
cd = (
37+
"auto"
38+
if "auto" in getattr(chute.find("cd"), "text", "")
39+
else float(getattr(chute.find("cd"), "text", "0"))
40+
)
3741
cd = search_cd_chute_if_auto(chute) if cd == "auto" else cd
3842
area = np.pi * float(getattr(chute.find("diameter"), "text", "0")) ** 2 / 4
3943
cds = cd * area
@@ -70,9 +74,11 @@ def search_parachutes(bs):
7074

7175

7276
def search_cd_chute_if_auto(bs):
73-
# if the parachute cd is set to "auto", OpenRocket defaults to 0.75 (flat) or 1.5 (dome).
74-
# Since we cannot easily deduce the type, 0.75 is the most common standard parachute CD in OR.
77+
# if the parachute cd is set to "auto", OpenRocket defaults to 0.75 (flat)
78+
# or 1.5 (dome). Since we cannot easily deduce the type, 0.75 is the
79+
# most common standard parachute CD in OR.
7580
logger.warning(
76-
"cd auto: the cd is set to 0.75 for parachute %s", getattr(bs.find("name"), "text", "Unknown")
81+
"cd auto: the cd is set to 0.75 for parachute %s",
82+
getattr(bs.find("name"), "text", "Unknown"),
7783
)
7884
return 0.75

rocketserializer/components/rocket.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ def get_rocket_radius(bs):
5555
noses = bs.find_all("nosecone")
5656
transitions = bs.find_all("transition")
5757

58-
tubes_radius = [getattr(i.find("radius"), "text", "") for i in tubes if i.find("radius")]
59-
noses_radius = [getattr(i.find("aftradius"), "text", "") for i in noses if i.find("aftradius")]
58+
tubes_radius = [
59+
getattr(i.find("radius"), "text", "") for i in tubes if i.find("radius")
60+
]
61+
noses_radius = [
62+
getattr(i.find("aftradius"), "text", "") for i in noses if i.find("aftradius")
63+
]
6064

6165
# Also collect radii from transitions (foreradius and aftradius)
6266
transition_radius = []

rocketserializer/ork_extractor.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def ork_extractor(bs, filepath, output_folder, ork):
4848
def _safe_search(func, default_ret, *args, **kwargs):
4949
try:
5050
return func(*args, **kwargs)
51-
except Exception as e:
51+
except Exception as e: # pylint: disable=broad-exception-caught
5252
logger.error("Error extracting %s: %s", func.__name__, e, exc_info=True)
5353
return default_ret
5454

@@ -71,7 +71,10 @@ def _safe_search(func, default_ret, *args, **kwargs):
7171
rocket_data = _safe_search(
7272
search_rocket,
7373
({"center_of_mass_without_propellant": 0, "mass": 0, "radius": 0}, 0),
74-
bs, datapoints, data_labels, burnout_position
74+
bs,
75+
datapoints,
76+
data_labels,
77+
burnout_position,
7578
)
7679
rocket, motor_position = rocket_data
7780
motors["position"] = motor_position
@@ -88,7 +91,11 @@ def _safe_search(func, default_ret, *args, **kwargs):
8891
elements = _safe_search(
8992
process_elements_position,
9093
{},
91-
ork.getRocket(), {}, center_of_dry_mass, rocket_mass, top_position=0
94+
ork.getRocket(),
95+
{},
96+
center_of_dry_mass,
97+
rocket_mass,
98+
top_position=0,
9299
)
93100
logger.info("The elements are:\n%s", _dict_to_string(elements, indent=23))
94101

@@ -101,7 +108,11 @@ def _safe_search(func, default_ret, *args, **kwargs):
101108
stored_results = _safe_search(
102109
search_stored_results,
103110
{},
104-
bs, datapoints, data_labels, time_vector, burnout_position
111+
bs,
112+
datapoints,
113+
data_labels,
114+
time_vector,
115+
burnout_position,
105116
)
106117

107118
# save everything to a dictionary

0 commit comments

Comments
 (0)