I have a main file in python that I am hosting through Google App Engine, however, I have all my functions defined in the same class. This is obviously not as clean and not as useful for file management. How can I have all my functions in a different file and then import that file to use the functions?
Here is my file that is a basic date validator:
import webapp2
def valid_year(year):
if (year.isdigit()):
year = int(year)
if (year < 2030 and year > 1899):
return True
def valid_day(day):
if (day.isdigit()):
day = int(day)
if (day <= 31 and day > 0):
return True
def valid_month(month):
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December']
month_abbvs = dict((m[:3].lower(), m) for m in months)
if month:
short_month = month[:3].lower()
return month_abbvs.get(short_month)
form="""
<form method = "post">
<label>Month</label>
<input type = "text" name = "month">
<label>Day</label>
<input type = "text" name = "day">
<label>Year</label>
<input type = "text" name = "year"><br>
<input type="submit" value="Validate Date">
</form>
"""
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.write(form)
def post(self):
Month_Test = valid_month(self.request.get('month'))
Day_Test = valid_day(self.request.get('day'))
Year_Test = valid_year(self.request.get('year'))
if not (Month_Test and Day_Test and Year_Test):
self.response.out.write(form)
else:
self.response.out.write("Thanks! That's a totally valid day!")
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)