Small Script to get exchange rates

1 Currency Converter

I often find myself in need of a simple currency converter, since my local currency is not any of these (£€$). Being in need of this I decided it would make for a decent project to be done in one day. I guess I can either figure out a place to scrape for the relevant information or there might be an api. Ideally I would like to make this in python (hy) but if I find a good api in another language I might do it in something else.

Seems there is a decent source for the most interesting currencies from the danish central bank. https://www.nationalbanken.dk/_vti_bin/DN/DataService.svc/CurrencyRatesXML?lang=en So it seems that is the simplest thing to use. Since it provides most of the ones I use. Mostly I need to convert between Danish Kroner and USD or Eur.

So now it is simply a matter of parsing the xml.

(import requests)
(import re)
(setv rates (.get requests "https://www.nationalbanken.dk/_vti_bin/DN/DataService.svc/CurrencyRatesXML?lang=en"))
(defn rate [country]
  (setv pattern (+ "code=\"" country "\".+rate=\"([\d\.]+)\""))
  (setv m (.search re pattern (. rates text)))
  (setv r (int (float (.group m 1))))
  (float (/ r 100)))
(defn price [pris &optional [cur "USD"]]
  (* pris (rate cur)))

(rate "HUF")

1.1 Code Blocks

Okay since that is really all the code that is required for my needs right now. I should probably explain the different bits

(setv rates (.get requests "https://www.nationalbanken.dk/_vti_bin/DN/DataService.svc/CurrencyRatesXML?lang=en"))

So this is a simple request to get the data from the national bank of denmark, this contains a few exchange rates, actually much more than I need so it is very sufficient.

(. rates text)

This just returns the text of the file, technically it is not needed

(defn rate [country]
  (setv pattern (+ "code=\"" country "\".+rate=\"([\d\.]+)\""))
  (setv m (.search re pattern (. rates text)))
  (setv r (int (float (.group m 1))))
  (float (/ r 100)))

This is the function that does the most useful job, it would be very reasonable to split this into multiple parts, but then again it does not do anything terribly complicated.

(setv pattern (+ "code=\"" country "\".+rate=\"([\d\.]+)\""))

See this part of the code is the most complicated, but everything in the

(+ "code=\"" country "\".+rate=\"([\d\.]+)\"")

Parens is in fact just building a string to be used as a regular expression on the next line.

(setv m (.search re pattern (. rates text)))

This uses the re module calls search on that module, then applies the pattern to the text returned from the last expression, since the pattern does in fact include a variable it will find the correct rate based on the argument to the function. Setv just sets a value to something equavalent to '=' in regular python.

(setv r (int (float (.group m 1))))

Now this does some type casting, basically just truncating the value, The decimal points do not matter that much to me.

(float (/ r 100))

This is just returning the value in the preferred format as a number, that is it does not count it as cents, but rather as a real monetary value. Though this might introduce some imprecision, but again I don't care that much.

(defn price [pris &optional [cur "USD"]]
  (* pris (rate cur)))

I guess you wonder about the arguments to this function well doing defaults is done by using

&optional [cur "USD"]

This form of syntax the wrapping [ ] are important, since this indicates some connection. Here it means that if no argument is specified for the second argument it defaults to "USD"

That is all for now.

H2
H3
H4
3 columns
2 columns
1 column
5 Comments
Ecency