Currency classes for Python
Posted by vsbits@reddit | Python | View on Reddit | 13 comments
Monepy
A python package that implements currency classes to work with monetary values.
Target audience
I created it mostly for some data analysis tasks I usually do, and also as way to learn about project structure, documentation, github actions and how to publish packages.
I wouldn't know if it's production ready.
Comparison
After starting it I found about py-moneyed. They are quite similar, but I wanted something that looks "cleaner" when using it.
Any feedback will be appreciated.
ImprovementDeep310@reddit
Congratulations! This is a really cool project, we’ll done! Can I clarify if this converts currencies (e.g, can I convert 10 USD to EUR? If so, what is being used to convert them, where do the exchange rates come from?
vsbits@reddit (OP)
Thanks! It doesn't, yet. I will make a class method for the user to input exchange rates at first.
I might do some research on available APIs to integrate, but it might be like an optional feature.
commy2@reddit
The comparison of _Currency objects with integers is problematic:
vsbits@reddit (OP)
That's true. I thought it would be useful when working with a single currency, but I can se the problem now...
I will look into that. Tks
commy2@reddit
ye, you have to remember that the python ata model requires equality to be transitive. Additionally, for hashables, values that compare equal must have the same hash. That's not the case with USD(10) and 10 either, and you will have to keep that in mind should you ever decide to implement exchange rates into __eq__.
0ne2many@reddit
Looks like a cool project! I sure can see use of this in portfolio tracking, back testing, and quantitative analysis.
Some features I am thinking of adding; - built in option to enable currency conversions using a currency conversion API - built in option to enable inflation numbers using pre-defined data, where an instance of USD(250) can also be given a datetime USD(250, datetime('11-08-2012') would result in a lower amount when converted to 01-01-2025
Adrewmc@reddit
It good thing to learn how to do stuff like this. Like you said it more leaning the process, which is good to learn all by itself self.
Right now, I see 2 big problems.
It only works for like 5 currencies
you give me no function to tell me what they are.
vsbits@reddit (OP)
My main goal was to workaround the decimal values correctly and print in the right format. Conversion was never the scope. But it is interesting.
The problem I see would be to keep exchange rates up-to-date.
2 & 3 are in the docs.
I personally don't like the idea of adding different currencies and forcing conversion. But I might work on a function for it. Maybe something like
USD.sum([BRL(10), EUR(10)], force_conversion=True)
. But I can't imagine a practical use for it.Adrewmc@reddit
I mean like an actual function like
Would return a list of the currencies currently available. But I guess it would have something like an autofill in most IDEs. It’s just common to include a function for that somewhere.
I think conversion would be the most useful aspect for many people, but you’re right you’d need to keep the conversion up to date, this might be as easy as finding a place the user calls themselves, or imputing a common service that does it. (E.g. here’s a key you need for it) I’m not too familiar with what’s out there though.
vsbits@reddit (OP)
No, I really appreciate the input!
Not sure about handling the requests in this module yet. I might go with the approach of the user inputing the exchange rates (maybe the json response from his API of choice) to enable conversions for now. Something like:
USD.set_rates({"EUR":0.9})
And raise custom Exceptions if any is tried without them being set.
Will look into the best way to show avaliable currencies.
Adrewmc@reddit
Now that’s starting to sound like a full package.
turtle4499@reddit
You 100% don't want to be converting between currencies implicitly. Any conversion would need to be explicit and function over a single term.
Adrewmc@reddit
The OP used
As a, better, idea.