def get_url_title(channel, url): if url[0:4].lower()!="http": url="http://"+url; if len(url)>0 and url[-1]=='?': url=url[0:-1]; try: url_input=pietlib.get_url(url); except: piet.send(channel, "nou, dat lijkt misschien wel wat op een url, maar 't bestaat niet hoor\n"); traceback.print_exc(); return; tinyurl="dat ding"; if len(url)>60: apiurl = "http://tinyurl.com/api-create.php?url="; tinyurl = urllib.urlopen(apiurl + url).read(); try: soup = BeautifulSoup.BeautifulSoup(url_input) title = soup.html.title.string title = ''.join(title.split('\n')[:10]) title = re.sub('[ \t]+', ' ', title).strip() piet.send(channel, "de titel van "+tinyurl+" is: "+title+"\n") if url.find('divx.com'): value = dict(soup.input.attrs)['value'] value = urllib.unquote(value) value = value.replace("<", "<").replace(">", ">").replace(""", '"') print repr(value) soup2 = BeautifulSoup.BeautifulSoup(value) movieurl = dict(soup2.embed.attrs)['src'] piet.send(channel, "maar eigenlijk is dit het filmpje: " + movieurl + "\n") except: traceback.print_exc()
def do_fetch(action, actionpath, q): if len(q) > 0: q = "?" + urllib.urlencode(q) else: q = "" url = ov9292url + actionpath + action + q raw = pietlib.get_url(url) return parse_ov9292(raw, actionpath)
def do_fetch(action, actionpath, q): if len(q)>0: q="?"+urllib.urlencode(q); else: q=""; url=ov9292url+actionpath+action+q; raw=pietlib.get_url(url); return parse_ov9292(raw, actionpath);
def maps_lookup(van, naar): def fail(reason): print "google maps request FAILED:" print " - url: %s" % url print " - %s" % reason raise PietLookupFailure("could not fetch") google_args = { 'origin' : van, 'destination' : naar, 'sensor' : 'false', 'mode' : 'walking', 'language' : 'nl' } url = """http://maps.google.nl/maps/api/directions/json?""" + urllib.urlencode(google_args) try: result = simplejson.loads(pietlib.get_url(url,maxsize=10*1024*1024)) except Exception, e: fail("fetch exception %r" % e)
def location(address): def fail(reason): print "google location request FAILED:" print " - url: %s" % url print " - %s" % reason raise PietLookupFailure("could not fetch location") google_args = { 'address':address, 'sensor':'false', 'region':'nl' } url = "http://maps.google.com/maps/api/geocode/json?" + urllib.urlencode(google_args) try: result = simplejson.loads(pietlib.get_url(url, maxsize=10*1024*1024)) except Exception, e: fail("exception: %r" % e)
def location(address): def fail(reason): print "google location request FAILED:" print " - url: %s" % url print " - %s" % reason raise PietLookupFailure("could not fetch location") google_args = {'address': address, 'sensor': 'false', 'region': 'nl'} url = "http://maps.google.com/maps/api/geocode/json?" + urllib.urlencode( google_args) try: result = simplejson.loads( pietlib.get_url(url, maxsize=10 * 1024 * 1024)) except Exception, e: fail("exception: %r" % e)
def read_stations(self): namen = [] for ci in xrange(0, 26): c = chr(ord('A')+ci) dumpname = "namen_%s.txt" % c if debugpiet and os.path.exists(dumpname): page = open(dumpname).read() else: page = pietlib.get_url('http://m.ns.nl/stations.action?key='+c, headers=[('Accept','text/html')]) if debugpiet: open(dumpname,'w').write(page) namen = namen + re.findall('<p><a.*?>(.*?)</a> \((.*?)\)</p>', page) self.stations = {} out = open('stationnamen.txt', 'w') for lang,kort in namen: out.write('%s,%s\n' % (kort,lang)) self.stations[kort.lower()] = lang
def gps_lookup(params): global gps_cache params = params.strip() if params in gps_cache: return gps_cache[params] google_args = {'address': params, 'sensor': 'false', 'language': 'nl'} url = """http://maps.google.nl/maps/api/geocode/json?""" + urllib.urlencode( google_args) try: result = simplejson.loads( pietlib.get_url(url, maxsize=10 * 1024 * 1024)) except Exception, e: print "google geocode request FAILED:" print " - url: %s" % url print " - exception: %r" % e raise Exception("could not fetch")
def read_stations(self): namen = [] for ci in xrange(0, 26): c = chr(ord('A') + ci) dumpname = "namen_%s.txt" % c if debugpiet and os.path.exists(dumpname): page = open(dumpname).read() else: page = pietlib.get_url('http://m.ns.nl/stations.action?key=' + c, headers=[('Accept', 'text/html')]) if debugpiet: open(dumpname, 'w').write(page) namen = namen + re.findall('<p><a.*?>(.*?)</a> \((.*?)\)</p>', page) self.stations = {} out = open('stationnamen.txt', 'w') for lang, kort in namen: out.write('%s,%s\n' % (kort, lang)) self.stations[kort.lower()] = lang
def maps_lookup(van, naar): def fail(reason): print "google maps request FAILED:" print " - url: %s" % url print " - %s" % reason raise PietLookupFailure("could not fetch") google_args = { 'origin': van, 'destination': naar, 'sensor': 'false', 'mode': 'walking', 'language': 'nl' } url = """http://maps.google.nl/maps/api/directions/json?""" + urllib.urlencode( google_args) try: result = simplejson.loads( pietlib.get_url(url, maxsize=10 * 1024 * 1024)) except Exception, e: fail("fetch exception %r" % e)
def get_url_title(channel, url): if url[0:4].lower() != "http": url = "http://" + url if len(url) > 0 and url[-1] == '?': url = url[0:-1] try: url_input = pietlib.get_url(url) except: piet.send( channel, "nou, dat lijkt misschien wel wat op een url, maar 't bestaat niet hoor\n" ) traceback.print_exc() return tinyurl = "dat ding" if len(url) > 60: apiurl = "http://tinyurl.com/api-create.php?url=" tinyurl = urllib.urlopen(apiurl + url).read() try: soup = BeautifulSoup.BeautifulSoup(url_input) title = soup.html.title.string title = ''.join(title.split('\n')[:10]) title = re.sub('[ \t]+', ' ', title).strip() piet.send(channel, "de titel van " + tinyurl + " is: " + title + "\n") if url.find('divx.com'): value = dict(soup.input.attrs)['value'] value = urllib.unquote(value) value = value.replace("<", "<").replace(">", ">").replace(""", '"') print repr(value) soup2 = BeautifulSoup.BeautifulSoup(value) movieurl = dict(soup2.embed.attrs)['src'] piet.send(channel, "maar eigenlijk is dit het filmpje: " + movieurl + "\n") except: traceback.print_exc()
def indo_graden(f): mins = int(f) f = (f-mins) * 60 secs = int(f) f = (f-secs) * 60 return '%d:%d:%d' % (mins,secs,int(round(f))) def indo_loc_to_string(lat,lng): NS = lat >= 0 and "N" or "S" EW = lng >= 0 and "E" or "W" lat,lng = indo_graden(abs(lat)),indo_graden(abs(lng)) return "%s%s %s%s" % (lat, NS, lng, EW) url="http://www.indo.com/cgi-bin/dist?" + urllib.urlencode({ 'place1' : indo_loc_to_string(*vancoords), 'place2' : indo_loc_to_string(*naarcoords)}) result = pietlib.get_url(url) result = re.findall('[0-9]+ miles [(]([0-9]+ km)[)]', result) if not result: return "even geen kraaien beschikbaar, maar als je gaat lopen van %s naar %s dan moet je %s ver" % ( vanplaats, naarplaats, loopafstand) if not loopafstand: return "zoals een kraai vliegt van %s naar %s is het %s, en lopen is niet handig" % ( vanplaats, naarplaats, result[0]) return "zoals een kraai vliegt van %s naar %s is het %s, maar lopend is het %s" % ( vanplaats, naarplaats, result[0], loopafstand) if __name__ == '__main__': print Distance('"den haag" voorburg')
def ov9292(param, nick, channel): print("ov start") par = re.match("[ ]*(optie|keuze)?[ ]*([abcdefABCDEF123456])[ ]*$", param) if par: # probeer oude resultaten op te halen nr = par.group(2) if nr.isdigit(): nr = int(nr) - 1 elif nr >= 'a' and nr <= 'z': nr = ord(nr) - ord('a') else: nr = ord(nr) - ord('A') if not (ovresults.has_key(nick)): return "maar ik heb helemaal geen reizen opgeslagen voor jou, " + nick results = ovresults[nick] if nr >= len(results): return "d'r is vast een resultaat zoekgeraakt ofzo, kan nr " + str( nr + 1) + " niet vinden" return results[nr] print("ov zoektocht") # nieuwe zoekopdracht tz = pietlib.tijdzone_nick(nick) regex=\ '(["]([^"]*)["]|([^"][^ ]*))[ ]+(["]([^"]*)["]|([^"][^ ]*))[ ]*'+\ '(vertrek|aankomst)?[ ]*(.*)' par = re.match(regex, param) if not (par): return 'ik snap d\'r geen hout van, parameters moeten zijn: '\ '"<van>" "<naar>" vertrek|aankomst datum tijd' van = par.group(2) or par.group(3) naar = par.group(5) or par.group(6) vertrek = 'D' if par.group(7) and par.group(7).strip() == "aankomst": vertrek = 'A' tijd = time.time() + 10 * 60 # geen tijd opgegeven -> over 10 minuten tijdstr = par.group(8).strip() if len(tijdstr) > 0: try: tijd = pietlib.parse_tijd(tijdstr, tz) except: return 'De datum+tijd ' + tijdstr + ' snap ik niet, sorry' print("ov van \"" + van + "\" naar \"" + naar + "\" om " + str(tijd)) # get frontpage print("ov 1") action, actionpath, q, form = do_fetch("", "", {}) print("ov 2") # get from-radiobuttons action, actionpath, q, form = do_fetch(action, actionpath, q) print("ov 3") if van.find(',') >= 0: action, actionpath, q, form = do_plaats(van, action, actionpath, q, channel) else: action, actionpath, q, form = do_station(van, action, actionpath, q, channel) print("ov 4") if naar.find(',') >= 0: action, actionpath, q, form = do_plaats(naar, action, actionpath, q, channel) else: action, actionpath, q, form = do_station(naar, action, actionpath, q, channel) print("ov 5") # tijd/datum os.environ['TZ'] = "Europe/Amsterdam" time.tzset() tijdstruct = time.localtime(tijd) q["d"] = time.strftime("%Y-%m-%d", tijdstruct) q["u"] = time.strftime("%H", tijdstruct) q["m"] = time.strftime("%M", tijdstruct) q["v"] = vertrek # D=vertrek, A=aankomst pietlib.timezone_reset() action, actionpath, q, form = do_fetch(action, actionpath, q) regex=\ "<tr><td>(Ver[^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td [^>]*><a (href)=\"([^>]*)\">[^>]*><[/]td><[/]tr>"+\ "" results = re.findall(regex, form, re.DOTALL | re.IGNORECASE) print("ov 6 (done, fetching individual results)") if len(results) == 0: piet.send( channel, "9292mobiel site is weer stuk, blijkbaar. ik heb een leeg lijstje gekregen." ) r = [] for i in results: piet.send(channel, i[0]+": "+i[1]+", "+i[2]+": "+i[3]+", "+i[4]+": "+i[5]+", "+i[6]+\ ": "+i[7]+" (zeg \""+piet.nick()+": ov "+("ABCDEF"[len(r)])+"\")") qs = i[9] url = ov9292url + actionpath + qs.replace("&", "&") raw = pietlib.get_url(url) res=re.findall(\ "<tr><td><span class='kop'>([^<]*)<[/]span>"+\ "<span class='kopn'>([^<]*)<[/]span>"\ "</td></tr><tr><td class='kopn'>([^<]*)</td></tr>", raw, re.DOTALL|re.IGNORECASE) r.append('\n'.join([' '.join(i) for i in res])) print("ov got an individual result") ovresults[nick] = r print("ov done\n") return ""
def ov9292(param,nick,channel): print("ov start"); par=re.match("[ ]*(optie|keuze)?[ ]*([abcdefABCDEF123456])[ ]*$", param) if par: # probeer oude resultaten op te halen nr=par.group(2); if nr.isdigit(): nr=int(nr)-1; elif nr>='a' and nr<='z': nr=ord(nr)-ord('a'); else: nr=ord(nr)-ord('A'); if not(ovresults.has_key(nick)): return "maar ik heb helemaal geen reizen opgeslagen voor jou, "+nick; results=ovresults[nick]; if nr>=len(results): return "d'r is vast een resultaat zoekgeraakt ofzo, kan nr "+str(nr+1)+" niet vinden"; return results[nr]; print("ov zoektocht"); # nieuwe zoekopdracht tz=pietlib.tijdzone_nick(nick); regex=\ '(["]([^"]*)["]|([^"][^ ]*))[ ]+(["]([^"]*)["]|([^"][^ ]*))[ ]*'+\ '(vertrek|aankomst)?[ ]*(.*)'; par=re.match(regex, param); if not(par): return 'ik snap d\'r geen hout van, parameters moeten zijn: '\ '"<van>" "<naar>" vertrek|aankomst datum tijd' van=par.group(2) or par.group(3); naar=par.group(5) or par.group(6); vertrek='D'; if par.group(7) and par.group(7).strip()=="aankomst": vertrek='A'; tijd=time.time()+10*60; # geen tijd opgegeven -> over 10 minuten tijdstr=par.group(8).strip(); if len(tijdstr)>0: try: tijd=pietlib.parse_tijd(tijdstr,tz); except: return 'De datum+tijd '+tijdstr+' snap ik niet, sorry'; print("ov van \""+van+"\" naar \""+naar+"\" om "+str(tijd)); # get frontpage print("ov 1"); action,actionpath,q,form=do_fetch("", "", {}); print("ov 2"); # get from-radiobuttons action,actionpath,q,form=do_fetch(action, actionpath, q); print("ov 3"); if van.find(',')>=0: action,actionpath,q,form=do_plaats(van, action, actionpath, q, channel); else: action,actionpath,q,form=do_station(van, action, actionpath, q, channel); print("ov 4"); if naar.find(',')>=0: action,actionpath,q,form=do_plaats(naar, action, actionpath, q, channel); else: action,actionpath,q,form=do_station(naar, action, actionpath, q, channel); print("ov 5"); # tijd/datum os.environ['TZ']="Europe/Amsterdam"; time.tzset(); tijdstruct=time.localtime(tijd); q["d"]=time.strftime("%Y-%m-%d", tijdstruct); q["u"]=time.strftime("%H", tijdstruct); q["m"]=time.strftime("%M", tijdstruct); q["v"]=vertrek; # D=vertrek, A=aankomst pietlib.timezone_reset(); action,actionpath,q,form=do_fetch(action, actionpath, q); regex=\ "<tr><td>(Ver[^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td>([^&]*)[^<]*<[/]td><td[^>]*>([^<]*)<[/]td><[/]tr>[^<]*"+\ "<tr><td [^>]*><a (href)=\"([^>]*)\">[^>]*><[/]td><[/]tr>"+\ ""; results=re.findall(regex, form, re.DOTALL|re.IGNORECASE); print("ov 6 (done, fetching individual results)"); if len(results)==0: piet.send(channel, "9292mobiel site is weer stuk, blijkbaar. ik heb een leeg lijstje gekregen."); r=[]; for i in results: piet.send(channel, i[0]+": "+i[1]+", "+i[2]+": "+i[3]+", "+i[4]+": "+i[5]+", "+i[6]+\ ": "+i[7]+" (zeg \""+piet.nick()+": ov "+("ABCDEF"[len(r)])+"\")"); qs=i[9]; url=ov9292url+actionpath+qs.replace("&", "&"); raw=pietlib.get_url(url); res=re.findall(\ "<tr><td><span class='kop'>([^<]*)<[/]span>"+\ "<span class='kopn'>([^<]*)<[/]span>"\ "</td></tr><tr><td class='kopn'>([^<]*)</td></tr>", raw, re.DOTALL|re.IGNORECASE); r.append('\n'.join([' '.join(i) for i in res])); print("ov got an individual result"); ovresults[nick]=r; print("ov done\n"); return "";
def ns(regel, channel): def format_station(x_org, opmerkingen): x = { # common typo's 'rtc': 'rtd', 'utc': 'ut' }.get(x_org, x_org) if x != x_org: opmerkingen.append((x, x_org)) if (x[0] == '"' and x[-1] == '"') or (x[0] == "'" and x[-1] == "'"): x = x[1:-1] y = stations.to_short(x) if not y: return x if x != y: opmerkingen.append((y, stations.to_long(y))) return y pietlib.timezone_reset() if regel == "?": result = pietlib.get_url_soup("http://mobiel.ns.nl/storingen.action") storingen = [str(i.contents[0]) for i in result.body.findAll('h4')] storingen = [i for i in storingen if i != 'Disruptions (in Dutch)'] if len(storingen) > 8: return "het is weer's helemaal stuk, met %d gemelde storingen: %s" % ( len(storingen), pietlib.make_list(storingen)) elif storingen: return "er zijn %d storingen op:\n %s" % (len(storinger) + "\n ".join(storingen)) else: return "er zijn geen storingen" if regel.lower().startswith("afk") or regel.lower().startswith("accr"): naam = regel.split(' ', 1)[1:] if not naam: return "ja, ik wil je best afkortingen geven, ook wel van stations" s = stations.to_short(naam[0]) if not s: return "beter dan '%s' wordt het niet, daar is geen afkorting voor" % naam[ 0] return '%s (%s)' % (s, stations.to_long(s)) qs = """\s*((?:["][^"]*["])|(?:['][^']*['])|(?:[^'"]\S*))\s*""" full = ("(?:van)?" + qs + "(?:naar)?" + qs + "(?:via" + qs + ")?" + "(om|v\S*|a\S*)?\s*(\d+:\d{2})?\s*(hsl)?") r = re.match(full, regel) if not (r): return "uh, dus je wilt met de trein. verder snapte ik het allemaal niet" (van, naar, via, tijdtype, tijd, hsl) = r.groups() opmerkingen = [] van = format_station(van, opmerkingen) naar = format_station(naar, opmerkingen) via = via and format_station(via, opmerkingen) or "" datum = time.strftime("%d-%m-%Y") tijdtype = not (tijdtype) or tijdtype == "om" or tijdtype[0] == "v" if opmerkingen: opm = ["\002%s\002 voor %s" % (s, l) for s, l in opmerkingen] piet.send(channel, "%s was korter geweest" % pietlib.make_list(opm)) print "NS: van %s naar %s via %s %stijd %s" % (repr(van), repr( naar), repr(via), tijdtype and "vertrek" or "aankomst", repr(tijd)) # test ns site en haal cookies cookies = [] pietlib.get_url("http://mobiel.ns.nl/planner.action?lang=nl", outcookies=cookies, headers=[('Accept', 'text/html')]) cookies = [re.sub(';.*', '', c) for c in cookies] postdata = { 'from': van, 'to': naar, 'via': via, 'date': datum, 'time': tijd, 'departure': (tijdtype and 'true' or 'false'), 'planroute': 'Reisadvies', 'hsl': (hsl and 'true' or 'false') } for retrycount in range(1): # try max 5 times to get a correct page page = pietlib.get_url("http://mobiel.ns.nl/planner.action", postdata, incookies=cookies, headers=[('Accept', 'text/html')]) open("nsresult.html", "w").write(page) reisadvies, page, errors = ns_format_page(page) if reisadvies or errors: break if errors: return '\n'.join(errors) return page
def ns(regel, channel): def format_station(x_org, opmerkingen): x = { # common typo's 'rtc': 'rtd', 'utc': 'ut' }.get(x_org, x_org) if x != x_org: opmerkingen.append((x, x_org)) if (x[0]=='"' and x[-1]=='"') or (x[0]=="'" and x[-1]=="'"): x = x[1:-1] y = stations.to_short(x) if not y: return x if x!=y: opmerkingen.append((y,stations.to_long(y))) return y pietlib.timezone_reset() if regel == "?": result = pietlib.get_url_soup("http://mobiel.ns.nl/storingen.action") storingen = [ str(i.contents[0]) for i in result.body.findAll('h4') ] storingen = [ i for i in storingen if i !='Disruptions (in Dutch)' ] if len(storingen) > 8: return "het is weer's helemaal stuk, met %d gemelde storingen: %s" % ( len(storingen), pietlib.make_list(storingen)) elif storingen: return "er zijn %d storingen op:\n %s" % ( len(storinger) + "\n ".join(storingen)) else: return "er zijn geen storingen" if regel.lower().startswith("afk") or regel.lower().startswith("accr"): naam = regel.split(' ', 1)[1:] if not naam: return "ja, ik wil je best afkortingen geven, ook wel van stations" s = stations.to_short(naam[0]) if not s: return "beter dan '%s' wordt het niet, daar is geen afkorting voor" % naam[0] return '%s (%s)' % (s, stations.to_long(s)) qs = """\s*((?:["][^"]*["])|(?:['][^']*['])|(?:[^'"]\S*))\s*""" full = ( "(?:van)?"+qs+ "(?:naar)?"+qs+ "(?:via"+qs+")?"+ "(om|v\S*|a\S*)?\s*(\d+:\d{2})?\s*(hsl)?") r = re.match(full, regel) if not(r): return "uh, dus je wilt met de trein. verder snapte ik het allemaal niet" (van, naar, via, tijdtype, tijd, hsl) = r.groups() opmerkingen = [] van = format_station(van, opmerkingen) naar = format_station(naar, opmerkingen) via = via and format_station(via, opmerkingen) or "" datum = time.strftime("%d-%m-%Y") tijdtype = not(tijdtype) or tijdtype=="om" or tijdtype[0]=="v" if opmerkingen: opm = [ "\002%s\002 voor %s" % (s,l) for s,l in opmerkingen ] piet.send(channel, "%s was korter geweest" % pietlib.make_list(opm)) print "NS: van %s naar %s via %s %stijd %s" % (repr(van), repr(naar), repr(via), tijdtype and "vertrek" or "aankomst", repr(tijd)) # test ns site en haal cookies cookies = [] pietlib.get_url("http://mobiel.ns.nl/planner.action?lang=nl", outcookies=cookies, headers=[('Accept','text/html')]) cookies = [ re.sub(';.*', '', c) for c in cookies ] postdata = { 'from':van, 'to':naar, 'via':via, 'date':datum, 'time':tijd, 'departure':(tijdtype and 'true' or 'false'), 'planroute':'Reisadvies', 'hsl':(hsl and 'true' or 'false')} for retrycount in range(1): # try max 5 times to get a correct page page = pietlib.get_url("http://mobiel.ns.nl/planner.action", postdata, incookies=cookies, headers=[('Accept','text/html')]) open("nsresult.html", "w").write(page) reisadvies, page, errors = ns_format_page(page) if reisadvies or errors: break if errors: return '\n'.join(errors) return page
def calcM(param): functions=[ ("ceil", math.ceil), ("abs", math.fabs), ("floor", math.floor), ("exp", math.exp), ("log", math.log), ("log10", math.log10), ("sqrt", math.sqrt), ("acos", math.acos), ("asin", math.asin), ("atan", math.atan), ("cos", math.cos), ("sin", math.sin), ("tan", math.tan), ("cosh", math.cosh), ("sinh", math.sinh), ("tan", math.tanh)] for (funcname,func) in functions: funcname+='(' if param[:len(funcname)]==funcname: (error,(value,imag),param,unit,dimx,dimy)=calcS(param[len(funcname):]) if (dimx!=1) or (dimy!=1): return("Function does not work on a matrix",(0,0),"","",1,1) if (error!=""): return(error,(0,0),"","",1,1) if (param[:1]!=")"): if error=="": return("Missing )",(0,0),"","",1,1) else: return(error,(0,0),"","",1,1) chars=re.compile('[a-z]') if chars.match(param[1:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[1:]) return (error,(func.__call__(value)*result,0),param,"",1,1) return(error,(func.__call__(value),0),param[1:],"",1,1) # special matrix if (param[:1]=="i" and param[1:2]>="0" and param[1:2]<="9"): (error,(value,imag),param,unit,dimx,dimy)=calcM(param[1:]) if error!="" and error!="t unknown": return (error,(0,0),"","",1,1) if dimx!=1 or dimy!=1 or value!=math.floor(value): return ("identity matrix must have simply number",(0,0),"","",1,1) value=int(value) matrixvalue=[] matrixunit=[] for x in range(value): matrixline=[] unitline=[] for y in range(value): unitline+=[""] if x==y: matrixline+=[1] else: matrixline+=[0] matrixvalue+=matrixline matrixunit+=unitline if value==1: return("",(matrixvalue[0],0),param,matrixunit[0],1,1) return("",(matrixvalue,0),param,matrixunit,value,value) # date function if param[:10]=="dayofweek(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[10:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday(),0),param[1:],"DW^1",1,1) if param[:8]=="weekday(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[8:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday(),0),param[1:],"DW^1",1,1) if param[:4]=="dow(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[4:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday(),0),param[1:],"DW^1",1,1) if param[:4]=="day(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[4:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday(),0),param[1:],"DW^1",1,1) if param[:8]=="weekdag(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[8:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday()+7,0),param[1:],"DW^1",1,1) if param[:4]=="dag(": language="nl" (error,(value,imag),param,unit,dimx,dimy)=calcS(param[4:]) if error!="": return(error,(0,0),"","",1,1) if (dimx<3 or unit[:1]!=["D^1"]): return("DayOfWeek function works on dates only",(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) thisday=datetime.date(value[0],value[1],value[2]) return("",(thisday.weekday()+7,0),param[1:],"DW^1",1,1) # matrix functions if param[:4]=="det(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[4:]) if error!="": return(error,(0,0),"","",1,1) if (param[:1]!=")"): return("Missing )",(0,0),"","",1,1) if dimx!=dimy: return("det() expects a square matrix",(0,0),"","",1,1) (value,unit)=det(value,unit,dimx) return (error,(value,0),param[1:],unit,1,1) if param[:6]=="gauss(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[6:]) if error!="": return(error,(0,0),"","",1,1) if (param[:1]!=")"): if error=="": return("missing )",(0,0),"","",1,1) else: return(error,(0,0),"","",1,1) (value,unit)=gauss(value,unit,dimx,dimy) return (error,(value,0),param[1:],unit,dimx,dimy) if param[:4]=="len(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[4:]) if error!="": return(error,(0,0),"","",1,1) if (param[:1]!=")"): return("missing )",(0,0),"","",1,1) if (dimx!=1) and (dimy!=1): return("length only works on vectors",(0,0),"","",1,1) result=0 for i in value: result+=i*i return("",(math.sqrt(result),0),param[1:],"",1,1) if param[:1]=="|": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[1:]) if error!="": return(error,(0,0),"","",1,1) if (param[:1]!="|"): return("missing close |",(0,0),"","",1,1) if (dimx!=1) and (dimy!=1): return("length only works on vectors",(0,0),"","",1,1) if (dimx==1) and (dimy==1): return ("",(value,0),param[1:],unit,1,1) result=0 for i in value: result+=i*i return("",(math.sqrt(result),0),param[1:],"",1,1) if param[:5]=="rank(": (error,(value,imag),param,unit,dimx,dimy)=calcS(param[5:]) if error!="": return(error,(0,0),"","",1,1) if (param[:1]!=")"): if error=="": return("missing )",(0,0),"","",1,1) else: return(error,(0,0),"","",1,1) (value,unit)=gauss(value,unit,dimx,dimy) rank=0 for y in range(dimy): x=0 while x<dimx: if value[x+(y*dimx)]!=0: rank+=1 x=dimx else: x+=1 return (error,(rank,0),param[1:],"",1,1) #date and time format date=0 digits=re.compile('[0-9]+\-?'+months+'\-?[0-9]+') digitscheck = digits.match(param) if digitscheck: date=1 datestring=digitscheck.group() day=int(re.compile('[0-9]+').match(datestring).group()) month=re.compile(months).search(param).group() i=string.find(param,month) year=int(re.compile('[0-9]+').search(datestring[i:]).group()) month=monthmap[month] try: datetime.date(year,month,day) except: return("Invalide date type",(0,0),"","",1,1) param=param[digitscheck.end():] if (param[:1]!="_" or param[:3]=="_in"): return("",([year,month,day],0),param,["D^1","D^1","D^1"],3,1) param=param[1:] complexnumber=re.compile('-?((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))(\+|-)((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))?i') complexcheck=complexnumber.match(param) if complexcheck: l=len(complexcheck.group()) if ((param[l:l+1]=="") or (param[l:l+1]=="_") or (param[l:l+1]=="*") or (param[l:l+1]=="/") or (param[l:l+1]=="+") or (param[l:l+1]=="-") or (param[l:l+1]=="^")): sign=1 if param[:1]=="-": sign=-1 param=param[1:] #if I is follwed by a number it's the identity matrix realpart=re.match('((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))',param).group() (_,(realvalue,_),_,_,_,_)=calcP(realpart) realvalue*=sign param=param[len(realpart):] sign=1 if param[:1]=="-": sign=-1 param=param[1:] if re.match('((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))',param): complexpart=re.match('((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))',param).group() (_,(complexvalue,_),_,_,_,_)=calcP(complexpart) else: complexvalue=1 complexpart="" complexvalue*=sign param=param[len(complexpart)+1:] if param[:1]=="_": param=param[1:] return ("",(realvalue,complexvalue),param,"",1,1) complexnumber=re.compile('-?((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))?i') complexcheck=complexnumber.match(param) if complexcheck: l=len(complexcheck.group()) if ((param[l:l+1]=="") or (param[l:l+1]=="_") or (param[l:l+1]=="*") or (param[l:l+1]=="/") or (param[l:l+1]=="+") or (param[l:l+1]=="-") or (param[l:l+1]=="^")): sign=1 if param[:1]=="-": sign=-1 param=param[1:] #if I is follwed by a number it's the identity matrix if re.match('((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))',param): complexpart=re.match('((0x[0-9a-f]+(\.[0-9a-f]+)?)|([0-9]+(\.[0-9]+)?))',param).group() (_,(complexvalue,_),_,_,_,_)=calcP(complexpart) else: complexvalue=1 complexpart="" complexvalue*=sign param=param[len(complexpart)+1:] if param[:1]=="_": param=param[1:] print param return ("",(0,complexvalue),param,"",1,1) digits=re.compile('[0-9]+\:[0-9]+(\:[0-9]+(\.[0-9]+)?)?') # time format digitscheck= digits.match(param) if digitscheck: value=0.0 # in time format... parse that timestring=digitscheck.group() i1=string.find(timestring,":") value+=int(timestring[:i1])*60 i2=string.find(timestring,":",i1+1) if (i2>0): value+=int(timestring[i1+1:i2]) value*=60 i1=i2 value+=float(timestring[i1+1:]) else: value+=float(timestring[i1+1:]) value*=60 if (date==1): return("",([year,month,day,value],0),param[len(timestring):],["D^1","D^1","D^1","s^1"],4,1) return("",(value,0),param[len(timestring):],"s^1",1,1) # hex check if re.match("(\-)?0x([0-9]|[a-f])+",param): digits=re.match("(\-)?0x([0-9]|[a-f])+",param).group() param=param[len(digits):] if param[:1]!=".": chars=re.compile('[a-z]|\(|[0-9]|\$') if chars.match(param): (error,(value2,imag),left,unit,dimx,dimy)=calcP(param) return (error,(int(digits,16)*value2,0),left,unit,1,1) return("",(int(digits,16),0),param,"",1,1) value=1.0*int(digits,16) if re.match("([0-9]|[a-f])+",param[1:]): digits=re.match("([0-9]|[a-f])+",param[1:]).group() value+=(int(digits,16))/math.pow(16,len(digits)) param=param[len(digits)+1:] chars=re.compile('[a-z]|\(|[0-9]|\$') if chars.match(param): (error,(value2,imag),left,unit,dimx,dimy)=calcP(param) return (error,(value*value2,0),left,unit,1,1) return ("",(value,0),param,"",1,1) else: return("Reached end of line, could not parse: "+param,0,"","",1,1) digits=re.compile('(\-)?[0-9]+(\.[0-9]+)?((e\+[0-9]+)|(e\-[0-9]+)|(e[0-9]+))?') digitscheck= digits.match(param) if digitscheck: value=float(digitscheck.group()) param=string.strip(param[digitscheck.end():]) chars=re.compile('[a-z]|\(|[0-9]|\$') if chars.match(param): (error,(value2,imag),left,unit,dimx,dimy)=calcP(param) if (dimx==1 and dimy==1): return (error,(value*value2,0),left,unit,1,1) return(error,(value,0),"*"+param,"",1,1) return ("",(value,0),param,"",1,1) #matrix if (param[:1]=="["): x1=param[1:].find("]") x2=param[1:].find("[") if x1 < 0: return("Missing ]",(0,0),"","",1,1) if ((x2>0) and (x1<x2)) or (x2<0): matrixelements=param[1:x1+1].split(",") dimx=len(matrixelements) matrixvalues=[] matrixunits=[] error="" for matrixel in matrixelements: (error1,(value,imag),left,unit,_,_)=calcS(matrixel) if (error1!="") or (left!=""): error="Error reading matrix elements" matrixvalues+=[value] matrixunits+=[unit] if dimx==1: if param[x1+2:x1+3]=="t": x1+=1 return(error,(matrixvalues[0],0),param[x1+2:],matrixunits[0],1,1) return (error,(matrixvalues,0),param[x1+2:],matrixunits,dimx,1) if (x2>=0) and (x2<x1): dimx=0 error="" matrixvalues=[] matrixunits=[] x2=param[1:].find("]]") if x2 < 0: return ("Error reading matrix elements",(0,0),"","",1,1) rows=param[2:x2+1].split("],[") dimy=len(rows) for row in rows: matrixelements=row.split(",") if dimx==0: dimx=len(matrixelements) elif dimx!=len(matrixelements): return("Error reading matrix elements",(0,0),"","",1,1) for matrixel in matrixelements: (error1,(value,imag),left,unit,_,_)=calcS(matrixel) if (error1!="") or (left!=""): error=error1 matrixvalues+=[value] matrixunits+=[unit] if dimx==1 and dimy==1: if param[x2+3:x2+4]=="t": x2+=1 return(error,(matrixvalues[0],0),param[x2+3:],matrixunits[0],1,1) return (error,(matrixvalues,0),param[x2+3:],matrixunits,dimx,dimy) return ("Matrix format error",(0,0),"","",1,1) if (param[:1]=="("): (error,(value,imag),param,unit,dimx,dimy)=calcS(param[1:]) if param[:1]!=")": if error=="": return("missing )",(0,0),"","",1,1) else: return(error,(0,0),"","",1,1) chars=re.compile('[a-z]|\(|[0-9]|\$') if chars.match(param[1:]): return(error,(value,0),"*"+param[1:],unit,dimx,dimy) return(error,(value,0),param[1:],unit,dimx,dimy) #2 based prefixes prefix=[("kilob",1024), ("megab",1048576), ("gigab",1073741824), ("terrab",1099511627776), ("petab",1125899906842624), ("exab",1152921504606846976), ("zettab",1180591620717411303424), ("yottab",1208925819614629174706176), ("millib",1.0/1024), ("microb",1.0/1048576), ("nanob",1.0/1073741824), ("picob",1.0/1099511627776), ("femtob",1.0/1125899906842624), ("attob",1.0/1152921504606846976), ("zeptob",1.0/1180591620717411303424), ("yoctob",1.0/1208925819614629174706176)] for(thisprefix,power) in prefix: if param[:len(thisprefix)]==thisprefix: (error_p,(value_p,imag),param_p,unit_p,dimx_p,dimy_p)=calcM(param[len(thisprefix)-1:]) if (unit_p[:2]=="b^"): return (error_p,(value_p*power,0),param_p,unit_p,1,1) #prefixes prefix=[("yotta",1e24), ("zetta",1e21), ("exa",1e18), ("peta",1e15), ("tera",1e12), ("giga",1e9), ("mega",1e6), ("kilo",1e3), ("hecto",1e2), ("deca",1e1), ("deci",1e-1), ("centi",1e-2), ("milli",1e-3), ("micro",1e-6), ("nano",1e-9), ("pico",1e-12), ("femto",1e-15), ("atto",1e-18), ("zepto",1e-21), ("yocto",1e-24)] for(thisprefix,power) in prefix: if param[:len(thisprefix)]==thisprefix: (error,(value,imag),param,unit,dimx,dimy)=calcM(param[len(thisprefix):]) return (error,(value*power,0),param,unit,1,1) for (thisunit,convertrate,siunit) in units: if param[:len(thisunit)]==thisunit: #check for plural endpos=len(thisunit) if param[endpos:endpos+1]=="s": endpos+=1 if param[endpos:endpos+2]=="es": endpos+=1 return("",(convertrate,0),param[endpos:],siunit,1,1) #check for money currency money=[("euro","EUR"), ("dollar","USD"), ("pond","GBP"), ("gbp","GBP"), ("britsepond","GBP"), ("britishpound","GBP"), ("eur","EUR"), ("usd","usd"), ("gbp","GBP"), ("a$","AUD"), ("$a","AUD"), ("aud","AUD"), ("$sg","SGD"), # singaporese dollar ("cur_bzd","BZD"), ("colones", "CRC"), ("colon", "CRC"), ("australischedollar","AUD"), ("$c","CAD"), ("c$","CAD"), ("canadiandollar","CAD"), ("canadeesedollar","CAD"), ("cad","CAD"), ("ukp","GBP"), ("yen","JPY"), ("jpy","JPY"), ("$nz","NZD"), ("nz$","NZD"), ("newzealanddollar","NZD"), ("nieuwzeeuwsedollar","NZD"), ("nieuwzeelandsedollar","NZD"), ("nzd","NZD"), ("chf","CHF"), ("zwitsersefrank","CHF"), ("switzerlandfranc","CHF"), ("swissfranc","CHF"), ("franc","CHF"), ("$","USD"), ("dkk","DKK"), ("deensekronen","DKK"), ("deensekroon","DKK"), ("danishkronor","DKK"), ("nok","NOK"), ("noorsekronen","NOK"), ("noorsekroon","NOK"), ("norwegiankronor","NOK"), ("noorweegsekronen","NOK"), ("noorweegsekroon","NOK"), ("sek","SEK"), ("zweedsekronen","SEK"), ("zweedsekroon","SEK"), ("swedishkroner","SEK"), ("hkd","HKD"), ("hongkongdollar","HKD"), ("cur_hkd","HKD"), ("yuan","CNY"), ("cny","CNY"), ("cedi","GHC"), ("turkselire", "TRL")] for (currency,name) in money: if param[:len(currency)]==currency: endpos=len(currency) if param[endpos:endpos+1]=="s": endpos+=1 if name=="EUR": return("",(1,0),param[endpos:],"$^1",1,1) page = pietlib.get_url('http://www.xe.com/ucc/convert.cgi', postdata={ 'Amount':1, 'From':'EUR', 'To':name}) result = re.subn('<[^>]*>', '', page)[0].replace(' ', ' ') val = float(re.findall("1 EUR = ([0-9.]+)", result)[0]) return("",(1/val,0),param[endpos:],"$^1",1,1) unit="" if param[:2]=="pi": chars=re.compile('[a-z]') if chars.match(param[2:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[2:]) return (error,(3.1415926535897932384*result,0),param,unit,1,1) return ("",(3.14159265358979323846,0),param[2:],"",1,1) if param[:1]=="e": chars=re.compile('[a-z]') if chars.match(param[1:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[1:]) return (error,(2.7182818284590452353602874713*result,0),param,unit,1,1) return ("",(2.7182818284590452353602874713,0),param[1:],"",1,1) if param[:5]=="dozen": chars=re.compile('[a-z]') if chars.match(param[5:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[5:]) return (error,(12*result,0),param,unit,1,1) return ("",(12,0),param[5:],unit,1,1) if param[:6]=="dozijn": chars=re.compile('[a-z]') if chars.match(param[6:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[6:]) return (error,(12*result,0),param,unit,1,1) return ("",(12,0),param[6:],unit,1,1) if param[:5]=="gross": chars=re.compile('[a-z]') if chars.match(param[5:]): (error,(result,imag),param,unit,dimx,dimy)=calcM(param[5:]) return (error,(144*result,0),param,unit,1,1) return ("",(144,0),param[5:],unit,1,1) nametype=re.compile('[a-z]*') nametype=nametype.match(param) nametype=nametype.group() if nametype=="": return ("Parse Error",(1,0),"","",1,1) return (nametype+" unknown",(1,0),param,unit,1,1)
secs = int(f) f = (f - secs) * 60 return '%d:%d:%d' % (mins, secs, int(round(f))) def indo_loc_to_string(lat, lng): NS = lat >= 0 and "N" or "S" EW = lng >= 0 and "E" or "W" lat, lng = indo_graden(abs(lat)), indo_graden(abs(lng)) return "%s%s %s%s" % (lat, NS, lng, EW) url = "http://www.indo.com/cgi-bin/dist?" + urllib.urlencode( { 'place1': indo_loc_to_string(*vancoords), 'place2': indo_loc_to_string(*naarcoords) }) result = pietlib.get_url(url) result = re.findall('[0-9]+ miles [(]([0-9]+ km)[)]', result) if not result: return "even geen kraaien beschikbaar, maar als je gaat lopen van %s naar %s dan moet je %s ver" % ( vanplaats, naarplaats, loopafstand) if not loopafstand: return "zoals een kraai vliegt van %s naar %s is het %s, en lopen is niet handig" % ( vanplaats, naarplaats, result[0]) return "zoals een kraai vliegt van %s naar %s is het %s, maar lopend is het %s" % ( vanplaats, naarplaats, result[0], loopafstand) if __name__ == '__main__':