Skip to content

Commit 147d6a1

Browse files
author
Alvaro Arroyo Parejo
committed
optimizing requested function times and physical_properties
1 parent 48d5afe commit 147d6a1

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

ESANEOCC/tabs.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,15 @@ def __init__(self):
534534
# Sources
535535
self.sources = []
536536

537-
@staticmethod
538-
def _get_prop_sources(url):
537+
@staticmethod
538+
def _get_prop_sources(contents):
539539
"""
540540
Obtain the sources parsed
541541
542542
Parameters
543543
----------
544-
url : str
545-
Complete url for physical properties
544+
contents : object
545+
Content of the requested url
546546
547547
Returns
548548
-------
@@ -551,7 +551,7 @@ def _get_prop_sources(url):
551551
"""
552552
# Read html for obtaining different tables from the portal
553553
try:
554-
df_p = pd.read_html(url, keep_default_na=False)
554+
df_p = pd.read_html(contents, keep_default_na=False)
555555
except df_p.DoesNotExist as df_not_exist:
556556
logging.warning('Object not found: the name of the '
557557
'object is wrong or misspelt')
@@ -565,14 +565,14 @@ def _get_prop_sources(url):
565565
return sources
566566

567567
@staticmethod
568-
def _get_property_names(url):
568+
def _get_property_names(contents):
569569
"""
570570
Check and obtain dataframe names
571571
572572
Parameters
573573
----------
574-
url : str
575-
Complete url for physical properties
574+
contents : object
575+
Content of the requested url
576576
577577
Returns
578578
-------
@@ -581,8 +581,6 @@ def _get_property_names(url):
581581
parsed_html : object
582582
BeautifulSoup object with the requested information
583583
"""
584-
# Get contents from url
585-
contents = requests.get(url, timeout=TIMEOUT).content
586584
# Parse html using BS
587585
parsed_html = BeautifulSoup(contents, 'lxml')
588586
# Check if there is a tag sub for term A1 and A2 if so,
@@ -607,7 +605,6 @@ def _get_property_names(url):
607605
'object. Re-attempting...')
608606
# Wait and re-try
609607
time.sleep(5)
610-
contents = requests.get(url, timeout=TIMEOUT).content
611608
parsed_html = BeautifulSoup(contents, 'lxml')
612609
for i in range(2):
613610
subtag = parsed_html.find('sub')
@@ -634,7 +631,7 @@ def _get_property_names(url):
634631
return parsed_html, df_names
635632

636633
@staticmethod
637-
def _get_physical_props(url):
634+
def _get_physical_props(contents):
638635
"""
639636
Obtain the physical properties from the portal
640637
@@ -649,8 +646,9 @@ def _get_physical_props(url):
649646
Data structure containing the physical properties
650647
"""
651648
# Obtain DataFrame with the obtained properties and html parsed
652-
parsed_html = PhysicalProperties._get_property_names(url)[0]
653-
df_names = PhysicalProperties._get_property_names(url)[1]
649+
parsed_html, df_names = PhysicalProperties\
650+
._get_property_names(contents)
651+
#df_names = PhysicalProperties._get_property_names(url)[1]
654652
# Search for property values, units and sources
655653
props_value = parsed_html.find_all("div",
656654
{"class": "col-12"})
@@ -721,10 +719,11 @@ def _phys_prop_parser(self, name):
721719
# Final url = PROPERTIES_URL + desig in which white spaces,
722720
# if any, are replaced by %20 to complete the designator
723721
url = PROPERTIES_URL + str(name).replace(' ', '%20')
724-
722+
# Get contents to optimize times
723+
contents = requests.get(url).content
725724
# Sources
726-
self.physical_properties = self._get_physical_props(url)
727-
self.sources = self._get_prop_sources(url)
725+
self.physical_properties = self._get_physical_props(contents)
726+
self.sources = self._get_prop_sources(contents)
728727

729728

730729
class AsteroidObservations:
@@ -1135,15 +1134,15 @@ def _ast_obs_parser(self, data_obj):
11351134
if not get_indexes(df_p, '! Object'):
11361135
# Set length of asteriod observations to zero
11371136
diff = 0
1137+
# Get observations
1138+
total_observations = self._get_opt_info(data_obj, diff,
1139+
head)
11381140
# Set attributes
1139-
self.optical_observations = self._get_opt_info(data_obj, diff,
1140-
head)[0]
1141+
self.optical_observations = total_observations[0]
11411142
self.radar_observations = 'There is no relevant radar '\
11421143
'information'
1143-
self.roving_observations = self._get_opt_info(data_obj, diff,
1144-
head)[1]
1145-
self.sat_observations = self._get_opt_info(data_obj, diff,
1146-
head)[2]
1144+
self.roving_observations = total_observations[1]
1145+
self.sat_observations = total_observations[2]
11471146

11481147
else:
11491148
# # Decode data for optical and radar observations
@@ -1152,14 +1151,14 @@ def _ast_obs_parser(self, data_obj):
11521151
index = get_indexes(df_p, '! Object')
11531152
# Set lenght of radar obsrevations to remove footer
11541153
diff = len(df_p) - index[0][0]
1154+
# Get observations
1155+
total_observations = self._get_opt_info(data_obj, diff,
1156+
head)
11551157
# Set attributes
1156-
self.optical_observations = self._get_opt_info(data_obj, diff,
1157-
head)[0]
1158+
self.optical_observations = total_observations[0]
11581159
self.radar_observations = self._get_rad_info(df_rad, index)
1159-
self.roving_observations = self._get_opt_info(data_obj, diff,
1160-
head)[1]
1161-
self.sat_observations = self._get_opt_info(data_obj, diff,
1162-
head)[2]
1160+
self.roving_observations = total_observations[1]
1161+
self.sat_observations = total_observations[2]
11631162

11641163

11651164
class OrbitProperties:
@@ -2022,12 +2021,14 @@ def _ephem_parser(self, name, observatory, start, stop, step, step_unit):
20222021
'-The Sky plane error with the long axis (Err1),'
20232022
' short axis (Err2) and Position Angle (PA) '
20242023
'values')
2024+
# Get header data
2025+
header_date = self._get_head_ephem(data_obj)
20252026
# Assign attributes
20262027
self.ephemerides = ephem
2027-
self.observatory = self._get_head_ephem(data_obj)[0]
2028-
self.tinit = self._get_head_ephem(data_obj)[1]
2029-
self.tfinal = self._get_head_ephem(data_obj)[2]
2030-
self.tstep = self._get_head_ephem(data_obj)[3]
2028+
self.observatory = header_date[0]
2029+
self.tinit = header_date[1]
2030+
self.tfinal = header_date[2]
2031+
self.tstep = header_date[3]
20312032

20322033

20332034
class Summary:

0 commit comments

Comments
 (0)