HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Pulling information from user input

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pullinguserinputfrominformation

Problem

This code works by taking my user input and correctly pulling the information I'm giving it.

I'm having difficulty understanding the implementation of methods in classes. I'm pretty sure I'm not doing this the correct way. Can anyone show me the way it should be done?

```
class prog_setup:

def __init__(self):
"""Initiates the below modules/variables."""

self.get_starlist()
self.get_InputKey()
self.define_listsANDarrays()
self.determine_normalizationpoint()
self.create_fobs_fobserr()
#self.calculate_taumag()
self.search_DUSTYmodels()
self.calculate_chi2()
self.delete_null_components()
self.dust_whittet_sub()

def get_starlist(self):
"""Imports the list of stars that you would like information on."""

self.star_list=np.array([])

try:
get_star_list=raw_input('Input pathname of the list of stars to be studied: ')
incoming_stars=open(get_star_list,'rb')
for line in incoming_stars:
x=line.split()
self.star_list=np.append(self.star_list,x) #Appends the individual star-IDs to the empty array star_list.
print 'Stars imported successfully.'
except IOError:
print 'Star import unsuccessful. Check that the star catalog file-path is correct. Program exiting now.'
sys.exit()
self.indices_of_star_list=range(len(self.star_list))

def get_InputKey(self):
"""Imports the Input (.inp) file. Historically, InputUttenthaler.inp. Allows for definition
of parameters which are utilized later in the script."""

input_script=raw_input('Pathname of .inp file: ')
InputKey=[]
self.band=''
self.Ak_scalefactor=''
try:
with open(input_script) as input_key:
for line in input_key.readlines():
x=[item for item in line.split()]
InputKey.append(x)
if InputKey[0][0]=='1' or InputKey[0][0]=='2': #Checks to see if .inp file is valid or not by checking the first row/column which should be 1 or 2.
print 'Your .inp file was successfully imported'
else:
print 'Your .inp file import failed. Check the val

Solution

If you decide to implement this without using a class you can simply write:

def get_starlist():
    ...
def get_inputkey():
    ...

if __name__ == "__main__":
    get_starlist()
    get_input_key()
    define_lists_and_arrays()  
    determine_normalization_point()
    create_fobs_fobserr()
    search_DUSTY_models()
    calculate_chi2()
    delete_null_components()
    dust_whittet_sub()


But if you find that some of your functions are sharing data a lot you may want to attach them to a class, or pass instances of a class (objects) between the different functions. It's hard to say more without knowing what you're trying to do.

Code Snippets

def get_starlist():
    ...
def get_inputkey():
    ...

if __name__ == "__main__":
    get_starlist()
    get_input_key()
    define_lists_and_arrays()  
    determine_normalization_point()
    create_fobs_fobserr()
    search_DUSTY_models()
    calculate_chi2()
    delete_null_components()
    dust_whittet_sub()

Context

StackExchange Code Review Q#29571, answer score: 3

Revisions (0)

No revisions yet.