def findWords(st0, opt=None): if opt is None: opt = {} # take all words of a text # and returns their indexes as a list. defOpt = {'minLen': 3, 'noEn': True} addDefaultOptions(opt, defOpt) st = st0[:] ind = [] for ch in sch: st = st.replace(ch, ' ' * len(ch)) if len(st) != len(st0): log.error( 'Error in function text_utlis.findWord. string length has been changed!' ) return [] si = [-1] + findAll(st, ' ') + [len(st)] # separatior indexes for i in range(len(si) - 1): word = st[si[i] + 1:si[i + 1]] if word.strip() == '': continue if 'word' in opt: if word != opt['word']: continue if len(word) < opt['minLen']: continue if opt['noEn']: en = False for c in word: if c in string.printable: en = True if en: continue ind.append((si[i] + 1, si[i + 1])) return ind
def findWords(st0, opt=None): if opt is None: opt = {} # take all words of a text # and returns their indexes as a list. defOpt = {'minLen':3, 'noEn':True} addDefaultOptions(opt, defOpt) st = st0[:] ind = [] for ch in sch: st = st.replace(ch, ' '*len(ch)) if len(st)!=len(st0): log.error('Error in function text_utlis.findWord. string length has been changed!') return [] si = [-1] + findAll(st, ' ') + [len(st)] # separatior indexes for i in range(len(si)-1): word = st[si[i]+1:si[i+1]] if word.strip()=='': continue if 'word' in opt: if word != opt['word']: continue if len(word) < opt['minLen']: continue if opt['noEn']: en = False for c in word: if c in string.printable: en = True if en: continue ind.append((si[i]+1, si[i+1])) return ind
def rmRebootMachineOrApp(machine, exitCode): #-------------------------------------------------- # Restart the machine/app. cmd = None if machine: log.info("REBOOTING MACHINE...") cmd = "reboot" else: log.info("RESTARTING APP...") restartScript = os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "restartSelf.sh")) params = [restartScript, ` os.getpid() `] + sys.argv if params[-1] == "&": del params[-1] cmd = '"' + '" "'.join(params) + '" &' if cmd: try: log.info(cmd) os.system(cmd) except Exception, e: log.error(e)
def go(*args, **kwargs): assert len(args) == 1 and len(kwargs) == 0 and type(args[0]) is Node try: return f(*args, **kwargs) except: log.error("Error while running %s", args[0].t) raise
def distanceBetweenGeographicCoordinatesAsKm(lat1, lon1, lat2, lon2): try: piOver180 = 0.01745329251 deltaLat = (lat1 - lat2)*piOver180 deltaLon = (lon1 - lon2)*piOver180 avgLat = (lat1 + lat2)/2*piOver180 return 6373 * sqrt(deltaLat**2 + (cos(avgLat)*deltaLon)**2) except Exception, e: log.error("Can't compute distance: %s" % e) return None
def distanceBetweenGeographicCoordinatesAsKm(lat1, lon1, lat2, lon2): try: piOver180 = 0.01745329251 deltaLat = (lat1 - lat2) * piOver180 deltaLon = (lon1 - lon2) * piOver180 avgLat = (lat1 + lat2) / 2 * piOver180 return 6373 * sqrt(deltaLat**2 + (cos(avgLat) * deltaLon)**2) except Exception, e: log.error("Can't compute distance: %s" % e) return None
def rmShutdownMachine(exitCode): cmd = "poweroff" log.info("SHUTTING DOWN MACHINE...") if cmd: try: log.info(cmd) os.system(cmd) except Exception, e: log.error(e)
def fixp_sat(f_type, val): try: # try to convert to filter implementation output type f_type(val) except ValueError: if (val > 0): val = f_type.fmax elif (val < 0): val = f_type.fmin else: log.error(f'Error when converting result {val} to {f_type}') return val
def find_program(): """Locate the binary ...""" # pth = util.program_path # pth = os.path.join(pth, _binary_name) # This is a temporary solution to call phyml without messing with other PF files pth = _binary_name # log.debug("Checking for program %s in path %s", _binary_name, pth) if not os.path.exists(pth) or not os.path.isfile(pth): log.error("No such file: '%s'", pth) raise PhymlError # log.debug("Found program %s at '%s'", _binary_name, pth) return pth
def getAlarmElapsedRealTime(): ### This method is used on Android to get the UP_TIME. elapsedTime = -1 try: alarmFile = open("/dev/alarm", 'r') if alarmFile: t = timespec() # ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME) = 0x40086134 result = fcntl.ioctl(alarmFile.fileno(), 0x40086134, t) if result == 0: elapsedTime = t.tv_sec alarmFile.close() except Exception, e: log.error(e)
def findAll(st, sub): ind = [] if isinstance(sub, basestring): i = 0 sbl = len(sub) while True: i = st.find(sub, i) if i == -1: break ind.append(i) i += sbl elif isinstance(sub, (list, tuple)): for item in sub: ind += findAll(st, item) ind.sort() else: log.error('Invailed second argument to function findAll!') return [] return ind
def findAll(st, sub): ind = [] if isinstance(sub, basestring): i = 0 sbl = len(sub) while True: i = string.find(st, sub, i) if i==-1: break ind.append(i) i += sbl elif isinstance(sub, (list, tuple)): for item in sub: ind += findAll(st, item) ind.sort() else: log.error('Invailed second argument to function findAll!') return [] return ind
def rmRebootMachineOrApp(machine, exitCode): #-------------------------------------------------- # Restart the machine/app. cmd = None if machine: log.info("REBOOTING MACHINE...") cmd = "reboot" else: log.info("RESTARTING APP...") restartScript = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "restartSelf.sh")) params = [restartScript, `os.getpid()`] + sys.argv if params[-1] == "&": del params[-1] cmd = '"' + '" "'.join(params) + '" &' if cmd: try: log.info(cmd) os.system(cmd) except Exception, e: log.error(e)
def Ec2Conn(reg, profile): try: ec2conn = AWSConn.Ec2Conn(reg, profile) except Exception, e: log.error("Cannot validate provided AWS credentials: %s" % e)