Skip to content

Commit 0c4045c

Browse files
Fix: Add missing null checks for .find() using getattr
1 parent eb02faf commit 0c4045c

10 files changed

Lines changed: 50 additions & 50 deletions

File tree

rocketserializer/components/environment.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ def search_environment(bs):
2323
"""
2424
settings = {}
2525

26-
latitude = float(bs.find("launchlatitude").text)
27-
longitude = float(bs.find("launchlongitude").text)
28-
elevation = float(bs.find("launchaltitude").text)
29-
wind_average = float(bs.find("windaverage").text)
30-
wind_turbulence = float(bs.find("windturbulence").text)
31-
geodetic_method = bs.find("geodeticmethod").text
26+
latitude = float(getattr(bs.find("launchlatitude"), "text", "0"))
27+
longitude = float(getattr(bs.find("launchlongitude"), "text", "0"))
28+
elevation = float(getattr(bs.find("launchaltitude"), "text", "0"))
29+
wind_average = float(getattr(bs.find("windaverage"), "text", "0"))
30+
wind_turbulence = float(getattr(bs.find("windturbulence"), "text", "0"))
31+
geodetic_method = getattr(bs.find("geodeticmethod"), "text", "")
3232
logger.info(
3333
"Collected first environment settings: latitude, "
3434
+ "longitude, elevation, wind_average, wind_turbulence, geodetic_method"
3535
)
3636
try:
37-
base_temperature = float(bs.find("basetemperature").text)
37+
base_temperature = float(getattr(bs.find("basetemperature"), "text", "0"))
3838
logger.info(
3939
"The base temperature was found in the .ork file. It is %f °C.",
4040
base_temperature,
@@ -46,7 +46,7 @@ def search_environment(bs):
4646
)
4747
base_temperature = None
4848
try:
49-
base_pressure = float(bs.find("basepressure").text)
49+
base_pressure = float(getattr(bs.find("basepressure"), "text", "0"))
5050
logger.info(
5151
"The base pressure was found in the .ork file. It is %f Pa.",
5252
base_pressure,

rocketserializer/components/fins.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def search_trapezoidal_fins(bs, elements):
3838
"Starting collecting the settings for the trapezoidal fin set number '%d'",
3939
idx,
4040
)
41-
label = fin.find("name").text
41+
label = getattr(fin.find("name"), "text", "")
4242
try:
4343

4444
def get_element_by_name(name):
@@ -57,32 +57,32 @@ def get_element_by_name(name):
5757
logger.error(message)
5858
raise KeyError(message)
5959

60-
n_fin = int(fin.find("fincount").text)
60+
n_fin = int(getattr(fin.find("fincount"), "text", "0"))
6161
logger.info("Number of fins retrieved: %d", n_fin)
6262

63-
root_chord = float(fin.find("rootchord").text)
63+
root_chord = float(getattr(fin.find("rootchord"), "text", "0"))
6464
logger.info("Root chord retrieved: %f", root_chord)
6565

66-
tip_chord = float(fin.find("tipchord").text)
66+
tip_chord = float(getattr(fin.find("tipchord"), "text", "0"))
6767
logger.info("Tip chord retrieved: %f", tip_chord)
6868

69-
span = float(fin.find("height").text)
69+
span = float(getattr(fin.find("height"), "text", "0"))
7070
logger.info("Span retrieved: %f", span)
7171

7272
sweep_length = (
73-
float(fin.find("sweeplength").text) if fin.find("sweeplength") else None
73+
float(getattr(fin.find("sweeplength"), "text", "0")) if fin.find("sweeplength") else None
7474
)
7575
sweep_angle = (
76-
float(fin.find("sweepangle").text) if fin.find("sweepangle") else None
76+
float(getattr(fin.find("sweepangle"), "text", "0")) if fin.find("sweepangle") else None
7777
)
7878
logger.info(
7979
"Sweep length and angle retrieved: %s, %s", sweep_length, sweep_angle
8080
)
8181

82-
cant_angle = float(fin.find("cant").text)
82+
cant_angle = float(getattr(fin.find("cant"), "text", "0"))
8383
logger.info("Cant angle retrieved: %f", cant_angle)
8484

85-
section = fin.find("crosssection").text
85+
section = getattr(fin.find("crosssection"), "text", "")
8686
logger.info("Crosssection format retrieved")
8787

8888
fin_settings = {
@@ -145,7 +145,7 @@ def search_elliptical_fins(bs, elements):
145145
"Starting collecting the settings for the elliptical fin set number '%d'",
146146
idx,
147147
)
148-
label = fin.find("name").text
148+
label = getattr(fin.find("name"), "text", "")
149149
try:
150150

151151
def get_element_by_name(name):
@@ -164,19 +164,19 @@ def get_element_by_name(name):
164164
logger.error(message)
165165
raise KeyError(message)
166166

167-
n_fin = int(fin.find("fincount").text)
167+
n_fin = int(getattr(fin.find("fincount"), "text", "0"))
168168
logger.info("Number of fins retrieved: %d", n_fin)
169169

170-
root_chord = float(fin.find("rootchord").text)
170+
root_chord = float(getattr(fin.find("rootchord"), "text", "0"))
171171
logger.info("Root chord retrieved: %f", root_chord)
172172

173-
span = float(fin.find("height").text)
173+
span = float(getattr(fin.find("height"), "text", "0"))
174174
logger.info("Span retrieved: %f", span)
175175

176-
cant_angle = float(fin.find("cant").text)
176+
cant_angle = float(getattr(fin.find("cant"), "text", "0"))
177177
logger.info("Cant angle retrieved: %f", cant_angle)
178178

179-
section = fin.find("crosssection").text
179+
section = getattr(fin.find("crosssection"), "text", "")
180180
logger.info("Crosssection format retrieved")
181181

182182
fin_settings = {

rocketserializer/components/flight.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def search_launch_conditions(bs):
2121
"""
2222
settings = {}
2323

24-
launch_rod_length = float(bs.find("launchrodlength").text)
25-
launch_rod_angle = float(bs.find("launchrodangle").text)
26-
launch_rod_direction = float(bs.find("launchroddirection").text)
24+
launch_rod_length = float(getattr(bs.find("launchrodlength"), "text", "0"))
25+
launch_rod_angle = float(getattr(bs.find("launchrodangle"), "text", "0"))
26+
launch_rod_direction = float(getattr(bs.find("launchroddirection"), "text", "0"))
2727
logger.info(
2828
"Collected launch conditions: launch rod length, launch rod angle, "
2929
"launch rod direction."

rocketserializer/components/id.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ def search_id_info(bs, filepath):
2424
"filepath".
2525
"""
2626
settings = {}
27-
settings["rocket_name"] = bs.find("rocket").find("name").text
27+
settings["rocket_name"] = bs.find(getattr("rocket").find("name"), "text", "")
2828
logger.info("Collected the rocket name: '%s'", settings["rocket_name"])
2929

3030
try:
31-
settings["comment"] = bs.find("rocket").find("comment").text.replace("\n", "")
31+
settings["comment"] = bs.find(getattr("rocket").find("comment"), "text", "").replace("\n", "")
3232
logger.info("Collected the comment saved in the file: %s", settings["comment"])
3333
except AttributeError:
3434
logger.warning("No auxiliary comment was found in the file.")
3535
settings["comment"] = None
3636
try:
37-
settings["designer"] = bs.find("rocket").find("designer").text
37+
settings["designer"] = bs.find(getattr("rocket").find("designer"), "text", "")
3838
logger.info("Collected the designer name: %s", settings["designer"])
3939
except AttributeError:
4040
logger.warning("No designer name was found in the file.")

rocketserializer/components/motor.py

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

4141
# retrieve motor geometry
42-
motor_length = float(bs.find("motormount").find("length").text)
43-
motor_radius = float(bs.find("motormount").find("diameter").text) / 2
42+
motor_length = float(bs.find(getattr("motormount").find("length"), "text", ""))
43+
motor_radius = float(bs.find(getattr("motormount").find("diameter"), "text", "")) / 2
4444
logger.info("Collected motor geometry: motor length and motor radius.")
4545

4646
# get motor mass properties

rocketserializer/components/nose_cone.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def search_nosecone(bs, elements=None, rocket_radius=None, just_radius=False):
2626
"""
2727
settings = {}
2828
nosecone = bs.find("nosecone") # TODO: allow for multiple nosecones
29-
name = nosecone.find("name").text if nosecone else "nosecone"
29+
name = getattr(nosecone.find("name"), "text", "") if nosecone else "nosecone"
3030

3131
if not nosecone:
3232
nosecones = list(
3333
filter(
34-
lambda x: x.find("name").text == "Nosecone", bs.find_all("transition")
34+
lambda x: getattr(x.find("name"), "text", "") == "Nosecone", bs.find_all("transition")
3535
)
3636
)
3737
if len(nosecones) == 0:
@@ -41,9 +41,9 @@ def search_nosecone(bs, elements=None, rocket_radius=None, just_radius=False):
4141
logger.info("Multiple nosecones found, using only the first one")
4242
nosecone = nosecones[0] # only the first nosecone is considered
4343

44-
length = float(nosecone.find("length").text)
45-
kind = nosecone.find("shape").text
46-
base_radius = nosecone.find("aftradius").text
44+
length = float(getattr(nosecone.find("length"), "text", "0"))
45+
kind = getattr(nosecone.find("shape"), "text", "")
46+
base_radius = getattr(nosecone.find("aftradius"), "text", "")
4747
try:
4848
base_radius = float(base_radius)
4949
except ValueError:
@@ -83,7 +83,7 @@ def get_position(name, length):
8383
if kind == "haack":
8484
logger.info("Nosecone is a haack nosecone, searching for the shape parameter")
8585

86-
shape_parameter = float(nosecone.find("shapeparameter").text)
86+
shape_parameter = float(getattr(nosecone.find("shapeparameter"), "text", "0"))
8787
kind = "Von Karman" if shape_parameter == 0.0 else "lvhaack"
8888
logger.info("Shape parameter of the nosecone: %s", shape_parameter)
8989
else:

rocketserializer/components/parachute.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ def search_parachutes(bs):
3030

3131
for idx, chute in enumerate(chutes):
3232
logger.info("Starting to collect the settings of the parachute number %d", idx)
33-
name = chute.find("name").text
33+
name = getattr(chute.find("name"), "text", "")
3434

3535
# parachute settings
36-
cd = "auto" if "auto" in chute.find("cd").text else float(chute.find("cd").text)
36+
cd = "auto" if "auto" in getattr(chute.find("cd"), "text", "") else float(getattr(chute.find("cd"), "text", "0"))
3737
cd = search_cd_chute_if_auto(chute) if cd == "auto" else cd
38-
area = np.pi * float(chute.find("diameter").text) ** 2 / 4
38+
area = np.pi * float(getattr(chute.find("diameter"), "text", "0")) ** 2 / 4
3939
cds = cd * area
4040
logger.info("Parachute '%s' has a drag coefficient of %f", name, cd)
4141

4242
# deployment settings
43-
deploy_event = chute.find("deployevent").text
44-
deploy_delay = float(chute.find("deploydelay").text)
43+
deploy_event = getattr(chute.find("deployevent"), "text", "")
44+
deploy_delay = float(getattr(chute.find("deploydelay"), "text", "0"))
4545
deploy_altitude = (
46-
float(chute.find("deployaltitude").text)
46+
float(getattr(chute.find("deployaltitude"), "text", "0"))
4747
if deploy_event == "altitude"
4848
else None
4949
)
@@ -81,6 +81,6 @@ def search_cd_chute_if_auto(bs):
8181

8282
# simply return 1.0
8383
logger.warning(
84-
"cd auto: the cd is set to 1.0 for parachute %s", bs.find("name").text
84+
"cd auto: the cd is set to 1.0 for parachute %s", getattr(bs.find("name"), "text", "")
8585
)
8686
return 1.0

rocketserializer/components/rail_buttons.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def search_rail_buttons(bs, elements: dict) -> dict:
2525
angular_position = 0.0
2626
lugs = bs.find_all("launchlug")
2727
for lug in lugs:
28-
if lug.find("name").text == name:
29-
angular_position = float(lug.find("radialdirection").text)
28+
if getattr(lug.find("name"), "text", "") == name:
29+
angular_position = float(getattr(lug.find("radialdirection"), "text", "0"))
3030
break
3131

3232
return {

rocketserializer/components/rocket.py

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

58-
tubes_radius = [i.find("radius").text for i in tubes if i.find("radius")]
59-
noses_radius = [i.find("aftradius").text for i in noses if i.find("aftradius")]
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")]
6060

6161
# Also collect radii from transitions (foreradius and aftradius)
6262
transition_radius = []

rocketserializer/components/transition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def search_transitions(bs, elements, ork):
5454
for idx, transition in enumerate(transitions):
5555
logger.info("Starting to collect the settings of the transition number %d", idx)
5656

57-
label = transition.find("name").text
57+
label = getattr(transition.find("name"), "text", "")
5858
logger.info("Collected the name of the transition number %d", idx)
5959

6060
# Try to find matching Java transition by name or index
@@ -82,7 +82,7 @@ def search_transitions(bs, elements, ork):
8282
aft_text = aft_tag.text if aft_tag else "0"
8383
bottom_radius = 0.0 if "auto" in aft_text else float(aft_text)
8484

85-
length = float(transition.find("length").text)
85+
length = float(getattr(transition.find("length"), "text", "0"))
8686
logger.info("Collected the dimensions of the transition number %d", idx)
8787

8888
def get_position(name, length):

0 commit comments

Comments
 (0)