Run.py is the script to initial the calculation and it calls function from dbm_utilities.py. Data_input.py is the script that needs to be executed in dbm_utilities.py in order to get the dataset for calculation.
How can we just execute Run.py and to get the results?
How to let Data_input.py run inside dbm_utilities.py based on the student ID and other variables provided in Run.py?
I just need some guidelines and suggestions about how to integrate my two little scripts (Run.py and Data_input.py) with dbm_utilities.py. Thanks.
# Run.py - the top layer that get results from dbm_utilities.py
from __future__ import (absolute_import, division, print_function)
unicode = type(u' ')
import warnings
import sys
if __name__ == '__main__':
if not sys.warnoptions:
warnings.simplefilter("ignore")
# STEP 1: Testing condition
try:
print('\nLoading testing condition ...')
quanity = 100.
adjust = 0.2
except RuntimeError:
print('\nTesting condition is not parameterized.')
else:
print('\nDone!')
# STEP: Student ID
try:
print('\nLoading student ID ...')
ID = 'Michael'
except RuntimeError:
print('\nStudent ID is not parameterized.')
else:
print('\nDone!')
# Run the calculation
try:
print('\nImporting calculation results ...')
ID, Adjusted_AveScore = dbm_utilities.get_report(ID, quanity, adjust)
print("Student Name:")
print(ID)
print("The score after adjustment:")
print(Adjusted_AveScore)
except RuntimeError:
print('\nModel is not running.')
else:
print('\nDone')
Run.py provides the parameters and student ID, and then call dbm_utilities.get_report to get the final adjusted score.
# dbm_utilities.py - the middle layer that do the calculation
from __future__ import (absolute_import, division, print_function)
# Get the ID, quanity, adjust from Run.py
def get_report(ID, quanity, adjust):
if isinstance(substance, str) or isinstance(substance, unicode):
ID, AveScore, Adjusted_AveScore = get_the_average(ID, adjust)
if quanity < 100:
print("Warning: student number is less than 100.")
return (ID, Adjusted_AveScore)
# --- Utilities ---
def get_the_average(ID, adjust):
AveScore = (Math + English + French + Sports) / 4.0
Adjusted_AveScore = AveScore * adjust
return (ID, AveScore, Adjusted_AveScore)
dbm_utilities.py gets the student ID and parameters from the Run.py for calculations and then return the results back to Run.py.
# Data_input.py - the lower layer to extract data from a database
# TODO: Get the ID from dbm_utillities.py
# For example:
# ID = "Michael"
with open("./Scores.txt", 'r') as infile:
text = infile.read().strip().split('\n')
header = text[0].split()[1:]
rows = [row.split() for row in text[1:]]
score_lib = {row[0]: [r for r in row[1:]] for row in rows}
def Math(ID):
Math = (score_lib[ID][header.index("Math")])
return float(Math)
def English(ID):
English = (score_lib[ID][header.index("English")])
return float(English)
def French(ID):
French = (score_lib[ID][header.index("French")])
return float(French)
def Sports(ID):
Sports = (score_lib[ID][header.index("Sports")])
return float(Sports)
Math = Math(ID)
English = English(ID)
French = French(ID)
Sports = Sports(ID)
Data_input.py is the script that read in the dataset and extract data based on the student ID. The student ID is provided in Run.py.
Here is the dataset:
ID Math English French Sports
Michael 100.0 57.0 73.0 90.0
Nancy 90.0 66.0 91.0 85.0
Eva 95.0 92.0 83.0 91.0
George 64.0 47.0 71.0 67.0
Emma 87.0 74.0 59.0 88.0
Scripts and dataset can be download at https://www.dropbox.com/sh/id22danjnq6gobk/AAASf5e1mCJQPKB4z9bcg_ILa?dl=0
data_in.pywantsoilIDfromutilties.py(data_in depends on utilities) but then data_in results are used by utilities.py (utilites depends on data_in). Isutilities.pya top level script that takes command line input and gets a result? Or isutilities.pya module that can be imported? ... perhaps containing functions that were designed to be called by importers like your program?