forked from PanDAWMS/pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExperimentFactory.py
87 lines (72 loc) · 2.61 KB
/
ExperimentFactory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Class definition:
# ExperimentFactory
# This class is used to generate Experiment class objects corresponding to a given "experiment"
# Based the on Factory Design Pattern
# Note: not compatible with Singleton Design Pattern due to the subclassing
from types import TypeType
from Experiment import Experiment
from ATLASExperiment import ATLASExperiment
from CMSExperiment import CMSExperiment
from OtherExperiment import OtherExperiment
from AMSTaiwanExperiment import AMSTaiwanExperiment
from NordugridATLASExperiment import NordugridATLASExperiment
class ExperimentFactory(object):
def newExperiment(self, experiment):
""" Generate a new site information object """
# get all classes
experimentClasses = [j for (i,j) in globals().iteritems() if isinstance(j, TypeType) and issubclass(j, Experiment)]
# loop over all subclasses
for experimentClass in experimentClasses:
si = experimentClass()
# return the matching experiment class
if si.getExperiment() == experiment:
return experimentClass
# if no class was found, raise an error
raise ValueError('ExperimentFactory: No such class: "%s"' % (experiment))
if __name__ == "__main__":
factory = ExperimentFactory()
print "\nAttempting to get ATLAS"
try:
experimentClass = factory.newExperiment('ATLAS')
except Exception, e:
print e
else:
si = experimentClass()
print 'got experiment:',si.getExperiment()
del experimentClass
print "\nAttempting to get Nordugrid-ATLAS"
try:
experimentClass = factory.newExperiment('Nordugrid-ATLAS')
except Exception, e:
print e
else:
si = experimentClass()
print 'got experiment:',si.getExperiment()
del experimentClass
print "\nAttempting to get Other"
try:
experimentClass = factory.newExperiment('Other')
except Exception, e:
print e
else:
si = experimentClass()
print 'got experiment:',si.getExperiment()
del experimentClass
print "\nAttempting to get AMSTaiwan"
try:
experimentClass = factory.newExperiment('AMSTaiwan')
except Exception, e:
print e
else:
si = experimentClass()
print 'got experiment:',si.getExperiment()
del experimentClass
print "\nAttempting to get Dummy"
try:
experimentClass = factory.newExperiment('Dummy')
except Exception, e:
print e
else:
si = experimentClass()
print 'got experiment:',si.getExperiment()
del experimentClass