def __read_from_Tinker_xyz_file(self) : self.n_atoms = 0 self.n_qm_atoms = 0 self.n_pseudo_atoms = 0 self.n_zero_charge_atoms = 0 self.n_mm_atoms = 0 self.charge = 0 self.spin = 1 self.atoms = [] fin = open(self.__file_name, "r") line = fin.readline() assert line tmp = line.split() if len(tmp) >= 7 : self.n_atoms = atoi(tmp[0]) self.n_qm_atoms = atoi(tmp[1]) self.n_pseudo_atoms = atoi(tmp[2]) self.n_zero_charge_atoms = atoi(tmp[3]) self.n_mm_atoms = atoi(tmp[4]) self.charge = atoi(tmp[5]) self.spin = atoi(tmp[6]) self.__check_atom_numbers() elif len(tmp) >= 5 : self.n_atoms = atoi(tmp[0]) self.n_qm_atoms = atoi(tmp[1]) self.n_pseudo_atoms = atoi(tmp[2]) self.n_zero_charge_atoms = atoi(tmp[3]) self.n_mm_atoms = atoi(tmp[4]) self.__check_atom_numbers() elif len(tmp) >= 1 : self.n_atoms = atoi(tmp[0]) self.n_mm_atoms = self.n_atoms self.__check_atom_numbers() else : die(self.__file_name + ": first line error") i = 1 while i <= self.n_atoms : line = fin.readline() assert line tmp = line.split() assert len(tmp) >= 6 index = atoi(tmp[0]) assert index == i name = tmp[1] x = atof(tmp[2]) y = atof(tmp[3]) z = atof(tmp[4]) type = atoi(tmp[5]) atom = TinkerAtom(index = index, name = name, coordinate = Cartesian(x, y, z), type = type, bond_list = map(int, tmp[6:])) self.atoms.append(atom) i += 1 fin.close() assert len(self.atoms) == self.n_atoms return
def __init__(self, file_name, data_begin = 0, data_end = 2**31, temperature = 298.15, bin_minimum = -200.0, bin_maximum = -100.0, number_of_bins = 100) : if not os.path.exists(file_name) : die("Data file '" + file_name + "' does not exist") self.windows = [] fin = open(file_name, "r") while 1 : line = fin.readline() if not line : break if not is_blank_line(line) : tmp = line.split() fname = tmp[0] minimum = string.atof(tmp[1]) force_constant = string.atof(tmp[2]) self.windows.append(HarmonicWindow(file_name = fname, data_begin = data_begin, data_end = data_end, temperature = temperature, bin_minimum = bin_minimum, bin_maximum = bin_maximum, number_of_bins = number_of_bins, force_constant = force_constant, minimum = minimum)) return
def read_data_from_file(self, every = 1) : fin = open(self.__file_name, "r") n = 0 while 1 : n = n+1 if n > self.__data_begin : break line = fin.readline() if not line : break if n < self.__data_begin : die("The total lines in file '" + self.__file_name + "' is less than " + str(self.__data_begin)) #n = 0 while 1 and n <= self.__data_end : line = fin.readline() if not line : break if not n%every : if line[-1] != '\n' : continue tmp = line.split() x = None if len(tmp) == 0 : print "The data format in file '" + self.__file_name + \ "' is not correct, which should be 1 or 2 or 4 columns" pass else : x = string.atof(tmp[-1]) if x > self.__bin_minimum and x < self.__bin_maximum : self.data.append(x) n = n+1 return
def lexer(fileName): lineNum = 0 yield Token(tT=TokTT(text="!!SOF",tType="OperatorOnly"),indent=0,whiteB4=False, location=(fileName,0,0)) # should allow %\ at end of line to split long lines (or %+ at start of next ?) for line in open(fileName, "r", encoding="utf-8"): whiteB4 = True # at a new line lineNum += 1 indentM = white.match(line) indent = len(indentM[0]) # set to -1 after 1st token pos = indent if upSlash.match(line,indent): # lex command m = re.compile(r"include\s+(\S+)\n?").fullmatch(line,indent+2) if m: yield from lexer(m[1]) # recurse else: #e.g. %/token String 100 "U.unquote" (?P<token>"(\\"|\\\\|[^"\\])*") n = re.compile(r'token\s+(\w+)\s+(\d+\.?\d*)\s+("(?:[^\\"]|\\.)*")?\s*([^\n]+)')\ .match(line,indent+2) if n: #print(n[4]) tokenClass = TokenClass(tType=n[1], tokRE=re.compile(n[4]), adjust=U.evalCallable(U.unquote(n[3]))) insertTokenClass(decimal.Decimal(n[2]), tokenClass) else: raise Exception("unknown %/ cmd:"+line) else: # multiple ordinary tokens while pos!=len(line): Found = C.namedtuple('Found',['tokTT','length']) found = None for p in tokenClassPrios: if found!=None: break # must have found one at higher priority for tc in tokenClassByPrio[p]: tm = tc.tokRE.match(line,pos) if tm: tt = tm['token'] if tc.adjust==None else tc.adjust(tm['token']) tokTT = TokTT(text=tt,tType=tc.tType) if found!=None: if found!=Found(tokTT=tokTT,length=len(tm[0])): U.die("conflicting tokens: "+found.tokTT.text+" "+tm['token'], fileName,lineNum,pos) else: found = Found(tokTT=tokTT,length=len(tm[0])) if found: wm = white.match(line, pos+found.length) gotWhite = pos+found.length==len(line) or len(wm[0])>0 if found.tokTT.tType!='Comment': yield Token(tT=found.tokTT,indent=indent, whiteB4=whiteB4, location=(fileName,lineNum,pos)) whiteB4 = gotWhite indent = -1 pos += found.length+len(wm[0]) else: assert False yield Token(tT=TokTT(text="!!EOF",tType="OperatorOnly"),indent=0,whiteB4=True, location=(fileName,lineNum+1,0)) return
def validate_config(): required = { 'create': ['resource_provider_namespace', 'resource_type', 'subscription_id', 'resource_name', 'purchase_plan'], 'show': ['resource_provider_namespace', 'resource_type', 'subscription_id', 'resource_name'], 'delete': ['resource_provider_namespace', 'resource_type', 'subscription_id', 'resource_name'], 'upgrade': ['resource_provider_namespace', 'resource_type', 'subscription_id', 'resource_name', 'upgrade_plan'], 'sso': ['resource_provider_namespace', 'resource_type', 'subscription_id', 'resource_name'], 'manifest': [] } missing = map( lambda x: x.replace("_", "-"), ["--" + k for k in required[config['command']] if k not in config] ) if missing: Printer.error("The following flags are required for this command: %s" % string.join(missing, ', ')) utility.die()
def perform_request(self, uri, method, body, uri_type='base', validate_xml=True, timeout=20.0): full_url = urlparse.urljoin( self.base_uri, uri) if uri_type == 'base' else urlparse.urljoin( self.sso_uri, uri) dispatch = { 'GET': requests.get, 'PUT': requests.put, 'POST': requests.post, 'DELETE': requests.delete } Printer.info("%s on %s" % (method, full_url)) try: if method in ['PUT', 'POST']: result = dispatch[method](full_url, body, headers=self.headers) elif method in ['GET', 'DELETE']: result = dispatch[method](full_url, headers=self.headers) except requests.exceptions.ConnectionError as e: Printer.error("Could not %s on %s. Error: %s" % (method, full_url, e.message[1])) die() except requests.exceptions.Timeout as e: Printer.error("%s on %s timed out." % (method, full_url)) die() Printer.info("Server returned HTTP status code %s" % result.status_code) if result.content and validate_xml: try: t = xmlutil.get_root_element( xmlutil.get_subtree_from_xml_string(result.content)) except Exception as e: Printer.error( "Could not parse response as XML. Check for mismatched tags or missing XML header. Error: %s" % e.message) die() try: xmlutil.check_tags_alpha_ordered(t) except xmlutil.TagOrderingException as e: Printer.error( "Tags in response have to be alphabetically sorted. These tags are not alphabetically-sorted: %s" % e.message) die() return (result.status_code, result.content)
def doMCTcmd(cmd, tok): if 'operator ' == cmd[0:9]: mo = operatorRE.match(cmd[9:]) if mo: op.doOperatorCmd(U.unquote(mo[1]), mo[2]) else: U.die("invalid operator decl: " + cmd, *tok.location) #elif 'defaultOperand '==cmd[0:15]: # defaultOperand = eval(cmd[15:]) # must be an ASTree elif 'import ' == cmd[0:7]: raise Exception("import not implemented -- FIXME") elif 'package ' == cmd[0:8]: raise Exception("package not implemented -- FIXME") elif 'export ' == cmd[0:7]: raise Exception("export not implemented -- FIXME") else: U.die("unknown MCTcmd: " + cmd, *tok.location) return A.zeroTuple() #?? defaultOperand
def run_checks(): if 'manifest_path' in config: manifest_path = config['manifest_path'] else: manifest_path = os.path.join(os.getcwdu(), "manifest.xml") config['manifest_path'] = manifest_path try: with open(manifest_path) as f: pass except IOError as e: Printer.error("Manifest file %s could not be opened" % (manifest_path)) utility.die() validator = Validator(config) dispatch = { 'create': validator.create, 'show': validator.get_cloud_service, 'delete': validator.delete, 'upgrade': validator.upgrade, 'manifest': validator.manifest, 'sso': validator.sso, } dispatch[config['command']]()
def __init__(self, file_name, data_begin = 0, data_end = 2**31, temperature = 298.15, bin_minimum = -200.0, bin_maximum = -100.0, number_of_bins = 100) : if not os.path.exists(file_name) : die("Data file '" + file_name + "' does not exist") self.__file_name = file_name self.__data_begin = data_begin self.__data_end = data_end if(self.__data_end <= self.__data_begin) : die("For data file '%s', data_begin = %d, data_end = %d, *** error ***" % (self.__file_name, self.__data_begin, self.__data_end)) self.__temperature = temperature self.__bin_minimum = bin_minimum self.__bin_maximum = bin_maximum self.__number_of_bins = number_of_bins self.beta = 1.0/(kB*temperature) self.free_energy_shift = 0.0 self.data = [] self.biased_distribution = [] self.unbiased_distribution = [] self.unbiased_potential_of_mean_force = [] return
def perform_request(self, uri, method, body, uri_type='base', validate_xml=True, timeout=20.0): full_url = urlparse.urljoin(self.base_uri,uri) if uri_type == 'base' else urlparse.urljoin(self.sso_uri,uri) dispatch = { 'GET': requests.get, 'PUT': requests.put, 'POST': requests.post, 'DELETE': requests.delete } Printer.info("%s on %s" % (method, full_url)) try: if method in ['PUT','POST']: result = dispatch[method](full_url, body, headers=self.headers) elif method in ['GET', 'DELETE']: result = dispatch[method](full_url, headers=self.headers) except requests.exceptions.ConnectionError as e: Printer.error("Could not %s on %s. Error: %s" % (method, full_url, e.message[1])) die() except requests.exceptions.Timeout as e: Printer.error("%s on %s timed out." % (method, full_url)) die() Printer.info("Server returned HTTP status code %s" % result.status_code) if result.content and validate_xml: try: t = xmlutil.get_root_element(xmlutil.get_subtree_from_xml_string(result.content)) except Exception as e: Printer.error("Could not parse response as XML. Check for mismatched tags or missing XML header. Error: %s" % e.message) die() try: xmlutil.check_tags_alpha_ordered(t) except xmlutil.TagOrderingException as e: Printer.error("Tags in response have to be alphabetically sorted. These tags are not alphabetically-sorted: %s" % e.message) die() return (result.status_code, result.content)
def die(errormsg): utility.die(errormsg, LOG_FILE)