astar666
asked on
python style review
Ah, finally hacked it enough so it works. This is generated code. Perhaps style comments please. The actual product is the imported files. Enumerations.py is there to test the imported files.
# ISOWeekDays.py
#
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Mon Feb 28 17:35:30 2005
import sys
class eISOWeekDays:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Min = Monday
Max = Sunday
def ISOWeekDay_Literal(eDay):
if eDay == eISOWeekDays.Monday:
strTarget = "Monday"
elif eDay == eISOWeekDays.Tuesday:
strTarget = "Tuesday"
elif eDay == eISOWeekDays.Wednesday:
strTarget = "Wednesday"
elif eDay == eISOWeekDays.Thursday:
strTarget = "Thursday"
elif eDay == eISOWeekDays.Friday:
strTarget = "Friday"
elif eDay == eISOWeekDays.Saturday:
strTarget = "Saturday"
elif eDay == eISOWeekDays.Sunday:
strTarget = "Sunday"
else:
raise "ValueError","eDay out of range."
return strTarget
def Test_eISOWeekDays():
for eDay in range(eISOWeekDays.Min,eIS OWeekDays. Max+1):
try:
print "Day: ",ISOWeekDay_Literal(eDay)
except:
print sys.exc_type,": ",sys.exc_value
# enumerations.py
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Mon Feb 28 17:35:30 2005
import sys
from LanguageCodes import *
from WorldLanguages import *
from CountryCodes import *
from WorldCurrencies import *
from WorldCountries import *
from NAWeekDays import *
from ISOWeekDays import *
from CurrencyCodes import *
from YearMonths import *
Test_eLanguageCodes()
Test_eWorldLanguages()
Test_eCountryCodes()
Test_eWorldCurrencies()
Test_eWorldCountries()
Test_eNAWeekDays()
Test_eISOWeekDays()
Test_eCurrencyCodes()
Test_eYearMonths()
sys.exit(0)
# ISOWeekDays.py
#
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Mon Feb 28 17:35:30 2005
import sys
class eISOWeekDays:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Min = Monday
Max = Sunday
def ISOWeekDay_Literal(eDay):
if eDay == eISOWeekDays.Monday:
strTarget = "Monday"
elif eDay == eISOWeekDays.Tuesday:
strTarget = "Tuesday"
elif eDay == eISOWeekDays.Wednesday:
strTarget = "Wednesday"
elif eDay == eISOWeekDays.Thursday:
strTarget = "Thursday"
elif eDay == eISOWeekDays.Friday:
strTarget = "Friday"
elif eDay == eISOWeekDays.Saturday:
strTarget = "Saturday"
elif eDay == eISOWeekDays.Sunday:
strTarget = "Sunday"
else:
raise "ValueError","eDay out of range."
return strTarget
def Test_eISOWeekDays():
for eDay in range(eISOWeekDays.Min,eIS
try:
print "Day: ",ISOWeekDay_Literal(eDay)
except:
print sys.exc_type,": ",sys.exc_value
# enumerations.py
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Mon Feb 28 17:35:30 2005
import sys
from LanguageCodes import *
from WorldLanguages import *
from CountryCodes import *
from WorldCurrencies import *
from WorldCountries import *
from NAWeekDays import *
from ISOWeekDays import *
from CurrencyCodes import *
from YearMonths import *
Test_eLanguageCodes()
Test_eWorldLanguages()
Test_eCountryCodes()
Test_eWorldCurrencies()
Test_eWorldCountries()
Test_eNAWeekDays()
Test_eISOWeekDays()
Test_eCurrencyCodes()
Test_eYearMonths()
sys.exit(0)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Happy to help!
ASKER
1) I configured my editor.
2) I went to a dictionary for my literals. In other languages, I have avoid arrays, since they may be large and sparse, but the python dictionary eliminates this concern. The decision simplifes my generating code.
3) Thank you for the appreciation of the test code. I cannot take too much credit though, since working with multiple languages does not easily allow the conceit that the code is correct by inspection. I lookied at pyUnit and decided it did not offer much for what I was doing. Since the generation code creates both the modules and the test program and even executes it as one step, I thought I was ahead of the game.
4) I changed the import statements.
5) I dropped sys.exit(0)
6) I used the test for __main__
Thank you.
# ISOWeekDays.py
#
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Tue Mar 1 10:46:17 2005
import sys
class eISOWeekDays:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Min = Monday
Max = Sunday
_ISOWeekDay_Literals = {
eISOWeekDays.Monday:"Monda
eISOWeekDays.Tuesday:"Tues
eISOWeekDays.Wednesday:"We
eISOWeekDays.Thursday:"Thu
eISOWeekDays.Friday:"Frida
eISOWeekDays.Saturday:"Sat
eISOWeekDays.Sunday:"Sunda
}
def ISOWeekDay_Literal(eDay):
return _ISOWeekDay_Literals[eDay]
def Test_eISOWeekDays():
for eDay in range(eISOWeekDays.Min,eIS
try:
print "Day:", ISOWeekDay_Literal(eDay)
except KeyError:
print "eDay out of range."
# enumerations.py
# Copyright (C) 2004-2005 Max M. Stalnaker
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Max M. Stalnaker
# PO Box 64746
# University Place, WA 98464
# astar@spamcop.net
# generated on Tue Mar 1 10:46:17 2005
from LanguageCodes import Test_eLanguageCodes
from WorldLanguages import Test_eWorldLanguages
from CountryCodes import Test_eCountryCodes
from WorldCurrencies import Test_eWorldCurrencies
from WorldCountries import Test_eWorldCountries
from NAWeekDays import Test_eNAWeekDays
from ISOWeekDays import Test_eISOWeekDays
from CurrencyCodes import Test_eCurrencyCodes
from YearMonths import Test_eYearMonths
def runTests():
Test_eLanguageCodes()
Test_eWorldLanguages()
Test_eCountryCodes()
Test_eWorldCurrencies()
Test_eWorldCountries()
Test_eNAWeekDays()
Test_eISOWeekDays()
Test_eCurrencyCodes()
Test_eYearMonths()
if __name__ == '__main__':
runTests()