def builtin_evaluate(agent, value): if (isSymbol(value)): raise AssertionError, \ "A naked symbol is not evaluable" if isString(value): return value elif isList(value): elements = [builtin_evaluate(agent, v) for v in value] return List(elements) elif isInteger(value): return value elif isFloat(value): return value elif isStructure(value): sym = value.functor if sym == BACKQUOTE_SYMBOL: return builtin_quoted(agent, value[0]) else: argvalues = [builtin_evaluate(agent, v) for v in value] fullsym = Symbol(BUILTIN_PACKAGE_NAME + "." + sym.id) imp = agent.getImp(fullsym) #if not (isinstance(imp, FunImpInt)): raise AssertionError, \ # "Not a function: %s"%sym b, z = valuesBZ(argvalues) result = imp.call(agent, b, z) return result else: return value
def builtin_evaluate(agent, value): if isSymbol(value): raise AssertionError, "A naked symbol is not evaluable" if isString(value): return value elif isList(value): elements = [builtin_evaluate(agent, v) for v in value] return List(elements) elif isInteger(value): return value elif isFloat(value): return value elif isStructure(value): sym = value.functor if sym == BACKQUOTE_SYMBOL: return builtin_quoted(agent, value[0]) else: argvalues = [builtin_evaluate(agent, v) for v in value] fullsym = Symbol(BUILTIN_PACKAGE_NAME + "." + sym.id) imp = agent.getImp(fullsym) # if not (isinstance(imp, FunImpInt)): raise AssertionError, \ # "Not a function: %s"%sym b, z = valuesBZ(argvalues) result = imp.call(agent, b, z) return result else: return value
def setTimeSpeed(simtime, simspeed): """Set the simlated time speed and the current simulated time. A simspeed of 10 means simulated time progresses at 10x realtime. A simspeed of 0 or less means leave the simspeed the same as it was. If simtime is None or 'NOW', the simulated tme does not change.""" global _TIMEWARP realtime = _time.time() if simtime is None or simtime == SIMULATED_TIME: simtime = _realtimeToSimtime(realtime) elif simtime is REAL_TIME: simtime = realtime elif isString(simtime): simtime = getTime(simtime) elif isFloat(simtime): pass elif isInteger(simtime): simtime = float(simtime) else: raise LowError('Invalid time argument') if simspeed <= 0: simspeed = _TIMEWARP[2] else: simspeed = float(simspeed) if realtime == simtime and simspeed == 1.0: _TIMEWARP = NULL_TIMEWARP else: _TIMEWARP = (realtime, simtime, simspeed)
def value_km_str(x): "convert x to a KM string" # TODO: make this work for real if isString(x) or isInteger(x) \ or isFloat(x) or isSymbol(x): return value_str(x) else: return None
def getTime(arg=None): """Get the time as a floating point number arg=None or arg="NOW": get the current simulated time arg="REAL": get the current real time arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>]': get from localtime string arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>]Z': get from UT string arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>][+-]<HH>:<MM>': use given timezone arg='<yyyy>-<mm>-<dd>; represent date by 00:00 UT arg='P<d>D<H>H<M>M<S>[.<frac>]' treat as a duration arg='-P<d>D<H>H<M>M<S>[.<frac>]' treat as a negative duration arg is a float: leave it unchanged Note: the - and : separators and some unused components can be left out """ if arg is None or arg == SIMULATED_TIME: return _realtimeToSimtime(_time.time()) elif arg == REAL_TIME: return _time.time() elif isString(arg): match = DURATION.match(arg) if match: (neg, d,h,m,s) = match.groups() dur = ((optInt(d)*24 + optInt(h))*60 + optInt(m))*60 + optFloat(s) if neg: return -dur else: return dur match = DATE_TIME.match(arg) if match: (Y, M, D, h, m, s, z, tzh, tzm) = match.groups() year = int(Y) month = int(M) day = int(D) hour = optInt(h) min = optInt(m) sec = optFloat(s) if z or h == None: # explicit GMT suffix or date without time return calendar.timegm((year,month,day,hour,min,0,0,1,0)) + sec elif tzh: offset = (int(tzh)*60 + optInt(tzm))*60 gmt = calendar.timegm((year,month,day,hour,min,0,0,1,0)) + sec return gmt - offset else: return _time.mktime((year,month,day,hour,min,0,0,1,-1)) + sec else: raise LowError('Invalid datetime string: %s'%arg) # try: # if arg[8] != "T": # raise LowError("Invalid datetime string: %s"%arg) # year = int(arg[0:4]) # month = int(arg[4:6]) # day = int(arg[6:8]) # hour = int(arg[9:11]) # min = int(arg[11:13]) # if arg.endswith('Z'): # UT # sec = float(arg[13:-1] or 0) # return calendar.timegm((year,month,day,hour,min,0,0,1,0)) + sec # else: # localtime # sec = float(arg[13:] or 0) # return _time.mktime((year,month,day,hour,min,0,0,1,-1)) + sec # except (ValueError, IndexError): # raise LowError('Invalid datetime string: %s'%arg) elif isFloat(arg): return arg elif isInteger(arg): return float(arg) else: raise LowError('Invalid time argument of type %s: %r'%(type(arg), arg))
def getTime(arg=None): """Get the time as a floating point number arg=None or arg="NOW": get the current simulated time arg="REAL": get the current real time arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>]': get from localtime string arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>]Z': get from UT string arg='<yyyy>-<mm>-<dd>T<HH>:<MM>:<SS>[.<frac>][+-]<HH>:<MM>': use given timezone arg='<yyyy>-<mm>-<dd>; represent date by 00:00 UT arg='P<d>D<H>H<M>M<S>[.<frac>]' treat as a duration arg='-P<d>D<H>H<M>M<S>[.<frac>]' treat as a negative duration arg is a float: leave it unchanged Note: the - and : separators and some unused components can be left out """ if arg is None or arg == SIMULATED_TIME: return _realtimeToSimtime(_time.time()) elif arg == REAL_TIME: return _time.time() elif isString(arg): match = DURATION.match(arg) if match: (neg, d, h, m, s) = match.groups() dur = ((optInt(d) * 24 + optInt(h)) * 60 + optInt(m)) * 60 + optFloat(s) if neg: return -dur else: return dur match = DATE_TIME.match(arg) if match: (Y, M, D, h, m, s, z, tzh, tzm) = match.groups() year = int(Y) month = int(M) day = int(D) hour = optInt(h) min = optInt(m) sec = optFloat(s) if z or h == None: # explicit GMT suffix or date without time return calendar.timegm( (year, month, day, hour, min, 0, 0, 1, 0)) + sec elif tzh: offset = (int(tzh) * 60 + optInt(tzm)) * 60 gmt = calendar.timegm( (year, month, day, hour, min, 0, 0, 1, 0)) + sec return gmt - offset else: return _time.mktime( (year, month, day, hour, min, 0, 0, 1, -1)) + sec else: raise LowError('Invalid datetime string: %s' % arg) # try: # if arg[8] != "T": # raise LowError("Invalid datetime string: %s"%arg) # year = int(arg[0:4]) # month = int(arg[4:6]) # day = int(arg[6:8]) # hour = int(arg[9:11]) # min = int(arg[11:13]) # if arg.endswith('Z'): # UT # sec = float(arg[13:-1] or 0) # return calendar.timegm((year,month,day,hour,min,0,0,1,0)) + sec # else: # localtime # sec = float(arg[13:] or 0) # return _time.mktime((year,month,day,hour,min,0,0,1,-1)) + sec # except (ValueError, IndexError): # raise LowError('Invalid datetime string: %s'%arg) elif isFloat(arg): return arg elif isInteger(arg): return float(arg) else: raise LowError('Invalid time argument of type %s: %r' % (type(arg), arg))