mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
CLI loader for the Project Euler excercises
git-svn-id: file:///srv/svn/euler@66 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
parent
bfa89ab07c
commit
54dedd8f7f
1 changed files with 59 additions and 0 deletions
59
loader.py
Normal file
59
loader.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
class EulerExcercise:
|
||||
def __init__(self, module):
|
||||
self.__module = None
|
||||
if type(module) == str:
|
||||
exec 'import {0}'.format(module)
|
||||
self.__module = eval(module)
|
||||
elif type(module).__name__ == 'module':
|
||||
self.__module = module
|
||||
if not self.__module:
|
||||
raise Exception('Failed to load module {0}'.format(module))
|
||||
docs = self.__module.__doc__.strip()
|
||||
self.__info = docs.split('\n')[0]
|
||||
self.__description = '\n'.join(docs.split('\n')[1:])
|
||||
def info(self):
|
||||
return self.__info
|
||||
def description(self):
|
||||
return self.__description
|
||||
def run(self):
|
||||
return self.__module.main()
|
||||
|
||||
# Load in all Project Euler exercises
|
||||
pattern = re.compile('^e\d{3}\.py$')
|
||||
modules = {}
|
||||
for file_name in os.listdir('.'):
|
||||
if not os.path.isfile(file_name) or not pattern.match(file_name):
|
||||
continue
|
||||
module_name = file_name.split('.')[0]
|
||||
module_number = int(module_name.split('e')[1])
|
||||
modules[module_number] = EulerExcercise(module_name)
|
||||
|
||||
def main_loop():
|
||||
print 'Project Euler Excercises:\n\n'
|
||||
for i in sorted(modules.keys()):
|
||||
print '{0:03d} - {1}'.format(i, modules[i].info())
|
||||
selection = None
|
||||
while not selection in modules.keys():
|
||||
input = raw_input('Select a problem, or type q to quit: ').strip().lower()
|
||||
if input == 'q':
|
||||
exit()
|
||||
try:
|
||||
selection = int(input, 10)
|
||||
except:
|
||||
selection = 0
|
||||
if selection not in modules.keys():
|
||||
print '>>>Invalid selection<<<'
|
||||
module = modules[selection]
|
||||
print '\nMODULE: e{0}'.format(selection)
|
||||
print 'INFO: {0}\n\n{1}\n'.format(module.info(), module.description())
|
||||
raw_input('Press enter to run...')
|
||||
print
|
||||
module.run()
|
||||
raw_input('Press enter to continue...')
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
main_loop()
|
Loading…
Reference in a new issue