-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent use of locale #230
base: master
Are you sure you want to change the base?
Conversation
…or `.` to the locale one when the value being shown is a float
…tor `.` by the locale one when the value being shown is a float
Hi @gu1lhermelp , Here is some info that we can discuss a little bit: Python string format has the
Based on that and some tests I can see that it works but there is a small detail... We can't easily specify the number of decimal places (precision) without a small hack: import locale
# an example value
val = 1235.21234
# let's switch the locale to pt_BR
locale.setlocale(locale.LC_ALL, "pt_BR")
print("{:.3n}".format(val)) # Will give us '1,24e+03'
# But if we do:
print("{:.7n}".format(val)) # This will give us '1.235,212', which is the desired output.
# So for a general term we would need to always be generating the mask based on the "size" of the value. Something like:
fmt = "{:."+str(len(str(int(val))) + prec) + "}"
print(fmt.format(val)) # This will give us '1.235,212', One other approach is to change the way the data is formatted at PyDM and provide a better and more uniform way to do so. |
@hhslepicka , your simple-approach solution example worked within ipython in my system. I set locale to fmt = "{:."+str(len(str(int(val))) + prec) + "n}" I did not know about |
…er when the value is an int or float
@hhslepicka nice suggestion! I added a new commit using the 'n' specifier instead of the old solution. |
@fernandohds564 just pointed out that |
this PR has problems with PV values between (-1,1)... we need to think more about it. |
I cannot reproduce this issue, would you mind to clarify what is going on with those values? |
Hello Hugo here is an example:
|
What is happening is that for values starting with 0 the formatting is counting from the first non-zero digit... I will think about it too and if I come up with some ideas I will send it here. |
@hhslepicka , we have an idea on how to generalize string representations of PV values properly taking into account locale. We will only have time to work on this probably next week. |
Perhaps something like: from decimal import Decimal
'{:n}'.format(Decimal('{:.5f}'.format(0.000123456))) In [44]: '{:n}'.format(decimal.Decimal('{:.5f}'.format(0.000123456)))
Out[44]: '0.00012'
In [45]: import locale
In [46]: locale.setlocale(locale.LC_ALL, 'pt_br')
Out[46]: 'pt_br'
In [47]: '{:n}'.format(decimal.Decimal('{:.5f}'.format(0.000123456)))
Out[47]: '0,00012' |
Hello all, in the last commits I am sending a new solution so you guys can evaluate it. Basically, I removed the class member As the I tested the six widgets that were altered and they seem to be working fine. I understand this is a major change as it alters the |
Hi guys! Thanks. |
Hi @fernandohds564.. sorry about that! |
This PR addresses the issue #229.
The changes were made in the set_display module of the PyDMLineEdit and PyDMLabel widgets.
The change consists of simply replacing the standard decimal separator '.' by the one defined in the locale settings.
Perhaps a more elegant solution would be to make this changes in the the PyDMWidget's update_format_string method, since that would affect all widgets with a similar behavior. However, at this time I could not think of an easy way to implement this changes, as that would entail having to use the locale module formatter instead of the .format currently being used.
Anyhow it seems to be working.
cheers,
Guilherme