Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try except in summartStar #2

Open
sousasag opened this issue May 22, 2020 · 2 comments
Open

try except in summartStar #2

sousasag opened this issue May 22, 2020 · 2 comments

Comments

@sousasag
Copy link
Collaborator

sousasag commented May 22, 2020

try:
fileName = np.array(search['ARCFILE'])
spectrograph = np.array(search['Instrument'])
obsDate = np.array(search['Date Obs'])
snr = np.array(search['SNR (spectra)'])
#number of spectra
print('Total number of spectra found: {0}'.format(snr.size), file = f)
value, count = np.unique(spectrograph, return_counts=True)
for i in range(value.size):
specSNR = search[search['Instrument']==value[i]]['SNR (spectra)']
quadSum = np.sqrt(np.sum(specSNR**2))
print('\n{0} spectra: {1}'.format(value[i], count[i]), '|',
'SNR Quadradratic Sum: {0}'.format(quadSum), file = f)
comparison = search[search['Instrument']==value[i]]['SNR (spectra)']
maxSNRpos = np.argmax(comparison)
print('Maximum SNR: {0}'.format(comparison[maxSNRpos]), file=f)
minSNRpos = np.argmin(comparison)
print('Minimum SNR: {0}'.format(comparison[minSNRpos]), file=f)
#spectra found
if printFiles:
print('ARCFILE\tInstrument\tObservationDate\tSNR', file = f)
for i, j in enumerate(fileName):
print('{0}\t{1}\t{2}\t{3}'.format(j, spectrograph[i],
obsDate[i], snr[i]), file = f)
if saveFile:
f.close()
except:
print('{0} not found in archive\n'.format(star), file = f)

Comment on this part of the code:
This try except is a "bit" to large, the exception catch everything, the message might be misleading.

You need to include the try only where the specific problem might occur, or alternative, you have to catch the correct exception...

@Kamuish
Copy link
Collaborator

Kamuish commented May 25, 2020

The files does not close if the code enters the exception.
Furthermore, instead of manually opening and closing files, I would suggest the use of a context manager (it makes sure that your file is closed, even if an exception occurs - read this more more info: https://towardsdatascience.com/https-medium-com-sinister-why-using-a-context-manager-is-a-better-choice-55ccfcecddb8)

The syntax is something like this :

with open( ... ) as file:

Another detail, you can print the exact error message:

try:
< code>
except Exception as e:
print(e)

this way your "e" will be exact exception that is thrown in the "try" block. You can switch it with other Exception types, i.e. SyntaxError, ...

@Kamuish
Copy link
Collaborator

Kamuish commented May 25, 2020

There is another issue. When you enter the exception, you will overwrite the "f" file. Npot sure if .write appends or writes over everything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants