-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle_operators.py
executable file
·71 lines (62 loc) · 2.92 KB
/
particle_operators.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
#!/usr/bin/env python
import argparse
import ConfigParser
import logging
import readinput
import sys
momentum_map = {0: "AR", 1: "OA", 2: "PD", 3: "CD", 4: "OA", 5: "PSQ5", 6: "PSQ6", 7: "PSQ7", 8: "PSQ8"}
default_file = sys.path[0]+"/particles.ini"
class particleDatabase():
def __init__(self, datafile=default_file):
self.datafile = datafile
logging.debug("initializing particleDatabase object")
self.config = ConfigParser.SafeConfigParser()
logging.debug("Reading database from {}".format(self.datafile))
self.config.read(self.datafile)
def read_op(self, particle, irrep, momentum):
logging.debug("reading {}_{} from database".format(particle, momentum))
key = "{}_{}".format(particle, momentum_map[momentum])
try:
return self.config.get(key, irrep)
except ConfigParser.NoOptionError:
logging.warn("No operator found for {} {}".format(key, irrep))
sucsess = self.add_op_entry(key, irrep)
if sucsess:
logging.warn("Should have updated database, Re-trying")
return self.read_op(particle, irrep, momentum)
else:
return None
except ConfigParser.NoSectionError:
logging.warn("No particle found for {}".format(key))
self.config.add_section(key)
sucsess = self.add_op_entry(key, irrep)
if sucsess:
logging.warn("Should have updated database, Re-trying")
return self.read_op(particle, irrep, momentum)
else:
return None
def add_op_entry(self, key, irrep):
logging.info("please enter operator choice for {} {}".format(key, irrep))
op = readinput.askoperator("{} {}".format(key, irrep))
print "# user input operator", op
if op:
self.config.set(key, irrep, op)
with open(self.datafile, "wb") as configfile:
self.config.write(configfile)
return True
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple interface for accessing particle database entries")
parser.add_argument('particle', metavar='particle_name', type=str, help='particle name')
parser.add_argument('momentum', metavar='momentum', type=int, help='particle momentum')
parser.add_argument('irrep', metavar='irrep', type=str, help='particle irrep')
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
logging.debug("Verbose debuging mode activated")
else:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
mydatabase = particleDatabase()
print mydatabase.read_op(args.particle, args.irrep, args.momentum)