Last active
August 29, 2015 14:24
Revisions
-
Kei Sato renamed this gist
Jul 13, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Kei Sato revised this gist
Jul 13, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/usr/bin/env python #coding:utf-8 # Exchange usd to any currency, or any currency to usd # help: python convert_usd.py -h import os.path -
Kei Sato renamed this gist
Jul 13, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Kei Sato revised this gist
Jul 13, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ #!/usr/bin/env python #coding:utf-8 # convert usd to any currency, or any currency to usd -
Kei Sato revised this gist
Jul 13, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #coding:utf-8 # convert usd to any currency, or any currency to usd # help: python convert_usd.py -h import os.path from datetime import date, datetime -
Kei Sato revised this gist
Jul 13, 2015 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,16 +23,6 @@ def hasCache(): return False def readCache(): with open(FPATH, 'r') as f: body = f.read() @@ -44,6 +34,16 @@ def writeCache(body): f.write(body) def fetchRates(): # fetch rate list from remote response = urllib.urlopen(URL) body = response.read() writeCache(body) return body if __name__ == "__main__": parser = argparse.ArgumentParser(description='convert usd to any currency.') parser.add_argument('-p', '--price', nargs='?', type=float, help='price', default=1.0) -
kei-sato revised this gist
Jul 13, 2015 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,9 +57,6 @@ def writeCache(body): reverse = args.reverse verbosity = args.verbosity if hasCache(): body = readCache() else: -
kei-sato created this gist
Jul 13, 2015 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ #coding:utf-8 # convert usd to any currency, or vise versa # help: python usd.py -h import os.path from datetime import date, datetime import argparse import urllib import json URL = "http://api.aoikujira.com/kawase/json/usd" FPATH = "/tmp/exchange" def hasCache(): if os.path.isfile(FPATH): d = date.today() today = datetime.combine(d, datetime.min.time()) mtime = datetime.fromtimestamp(os.path.getmtime(FPATH)) if mtime > today: return True return False def fetchRates(): # fetch rate list from remote response = urllib.urlopen(URL) body = response.read() writeCache(body) return body def readCache(): with open(FPATH, 'r') as f: body = f.read() return body def writeCache(body): with open(FPATH, 'w') as f: f.write(body) if __name__ == "__main__": parser = argparse.ArgumentParser(description='convert usd to any currency.') parser.add_argument('-p', '--price', nargs='?', type=float, help='price', default=1.0) parser.add_argument('-c', '--currency', nargs='?', help='currency', default=u'JPY') parser.add_argument('-r', '--reverse', action='store_true', help='reverse the direction') parser.add_argument('-v', '--verbosity', action='count', help='increase output verbosity') args = parser.parse_args() price = args.price currency = args.currency reverse = args.reverse verbosity = args.verbosity if verbosity: print "verbosity:", verbosity if hasCache(): body = readCache() else: body = fetchRates() data = json.loads(body) rate = float(data[currency]) if reverse: if verbosity >= 1: print "{0}({2}) => {1}(USD)".format(1, 1 / rate, currency) print "{0}({2}) => {1}(USD)".format(price, price / rate, currency) else: print price / rate else: if verbosity >= 1: print "{0}(USD) => {1}({2})".format(1, rate, currency) print "{0}(USD) => {1}({2})".format(price, price * rate, currency) else: print price * rate