Comments (5)
Hello,
Someone recently tried to do a very similar thing based on xe.com. It contains a very useful piece of code that gives you the latest rate for any currency. You can take a look at PR #12 for more details. I saw your PR #15, but I am only going to answer here since the issue and the PR are related.
The idea behind the current implementation (this might not be well documented) is to load the rates once when creating the CurrencyConverter
object (about 400ms), then get near-instantaneous results when performing conversions (about 7ยตs). By comparison, just doing an HTTP request will probably take minimum 100ms (per conversion). This might not be a problem if you do not plan to perform thousands of conversions, but this is my use case.
I took a look at the API you are using in the PR, the XML returned is not updated more than once a day I think, so actually you do not need to call it more than once a day, it effectively contains the same information as the CSV file.
Another point, since you mentioned extensibility: the format the currency converter uses is the ECB format. It is a very simple CSV format, and I think people can very easily generate a custom file containing rates from another source, then use c = CurrencyConverter('file.csv')
. I believe this is more extensible than having to override the loading code to a specific API that uses a different XML grammar.
from currencyconverter.
Hi Alex,
thank you for the feedback, especially since this is my first contribution on github.
It was just an idea of making the tool itself more lightweight in terms of memory and/or disk space, which is nonsense given your use case. This works under the assumption that the user does not need the entire data set which is shipped in the first place. I should have thought of your last point right away, manipulating the input rather than the source.
For a different approach of custom input: I stored the same data as your zip in a file storage system by currency (sample: DarioHett/centralbankdata). I would previously fetch the required data and then feed the custom list to CurrencyConverter() such that I would skip load_file() and start with load_lines() - is there some way to achieve this? Otherwise I would just write a customized load_file() which passes a list right through to load_lines().
from currencyconverter.
Well, congratulations for you first contribution ๐ You are doing great!
If you do not need the full dataset, I suggest to just use head
on the file to keep only the first lines, it should work well and reduce the disk usage. Note that the full dataset is only 1Mo uncompressed though. But I understand if you need to cut down on the initial load time of 400ms.
I am not sure I understand your last point but I will try to answer it. You can pass currency_file=None
during __init__
to avoid calling load_file
. Then you can call load_lines
with your custom data (it expects the first line to be the header).
c = CurrencyConverter(currency_file=None)
c.load_lines(open('eurofxref-hist.csv'))
from currencyconverter.
Your answer nailed it despite the lack of precision in my question.
Two more things:
(i) Have you thought about adding control flow for passing further data in load_lines/load_file when the object has already been initialized?
Obviously my idea stems from the mentioned file storage and keeping it extensible without previously creating a single input file. Just initialize with the *.zip and then pass it another *.csv to add a 'column' if one wishes - that is the idea. It also adds to functionality, since your currency converter may as well be used to discount values simply by attaching another list of interest rates to the existing _rates dictionary.
(ii) Several currencies have been assigned another identifier once they were redenominated (ie TRL to TRY); should there be a second 'mode' where these attach to one another? I assume you did not concatenate those lists because of your own use case.
from currencyconverter.
I have never had those use cases so I did not really think about it.
I guess the easiest way to extend an object with new data is actually to create it from scratch again with the previous lines concatenated with new additional lines (custom code to handle this use case would have to be justified, since we are only talking about a few ms of processing time).
lines = list(open('eurofxref-hist.csv'))
c1 = CurrencyConverter(currency_file=None)
c1.load_lines(lines)
# Get other_lines from somewhere else
c2 = CurrencyConverter(currency_file=None)
c2.load_lines(lines + other_lines)
For your second point, I did not concatenate the lists because they were not concatenated in the raw data set. I guess you could merge them manually if needed, either in the CSV, or using the _rates
, but I think the current system works well, e.g. to have them as two separate currencies.
from currencyconverter.
Related Issues (20)
- CurrencyConverter creation sometimes fails when creating with up to date full history url
- Adding ECB urls to the code HOT 3
- Documentation not working HOT 4
- fallback option not working HOT 1
- Human readable flag HOT 4
- ModuleNotFoundError: No module named 'currency_converter' HOT 2
- Data is wrong! HOT 1
- Doesnt Support TWD to EUR HOT 1
- Not supported currency HOT 1
- Specifying default currency & string output HOT 1
- Is there a list with all supported currencys? HOT 1
- Use the worldbank dataset HOT 2
- Take current date is no date data is provided. HOT 1
- How can I access the actual date and rate used when a fallback method is specified? HOT 1
- Detecting target currency based on user's locale HOT 2
- QAR conversion is not supported. HOT 1
- Actual date in rates HOT 1
- Run
- custom file is not available
- Incorrect coversion rate found HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from currencyconverter.