Skip to content
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

Defining miligram per cent (mg%) #1717

Closed
mac93pl opened this issue Feb 25, 2023 · 3 comments
Closed

Defining miligram per cent (mg%) #1717

mac93pl opened this issue Feb 25, 2023 · 3 comments

Comments

@mac93pl
Copy link

mac93pl commented Feb 25, 2023

Hi! Great package 💪

I'm currently exploring Pint possibilities in medical field and stumbled upond a problem with adding support for "mg%" measure.
It's rather depricated, but in some cases still in use ( [https://en.wikipedia.org/wiki/Milligram_per_cent] )
The problem is that I can't simply register "mg%" unit symbol, due to definition error DefinitionSyntaxError('missing unary operator "%s"' % op_text).

I tried different approaches (#1402, #379, #429) but non of them works in my case and in most cases it somehow ends up with the error above.

One thing that worked in some cases was to append the definition to default_en.txt as:
miligrampercent = mg/dL = mg%
But even then ureg checkup or .to(ureg['mg%'])doesn't work (same error).

I use the packeage to convert quantities based on provided string and in almost all cases it works perfectly except this one. Seem like I can simply replace string from "mg%" to eg "miligrampercent" before defining it in Pint and again, after obtaining the result, replace "miligrampercent" to "mg%" in some caseses, but it doesn't looks like the most elegent solution.

@lafrech
Copy link

lafrech commented Feb 27, 2023

Not sure but this might have been solved in master already while introducing % and ppm (not released yet). Would you like to try master branch to confirm?

@MichaelTiemannOSC
Copy link
Collaborator

I had quite some fun integrating currency symbols into my Pint application:

currency_dict = {
    # 'US$':'USD', NOTE: don't try to re-define USD...it leads to infinite recursion                                                                 
    '€':'EUR',
    '¥':'JPY', # 円 / 圓                                                                                                                             
    '£':'GBP',
    '元':'CNY', # ¥                                                                                                                                  
    'A$':'AUD',
    'C$':'CAD',
    'HK$':'HKD',
    'S$':'SGD',
    'SKr':'SEK',
    '₩':'KRW', # 원/ 圓                                                                                                                              
    'NKr':'NOK',
    'NZ$':'NZD',
    '₹':'INR',
    'NT$':'TWD',
    'R':'ZAR',
    'R$':'BRL',
    'DKr':'DKK',
    'zł':'PLN',
    '฿':'THB',
    '₪':'ILS',
    'Rp':'IDR',
    'Kč':'CZK',
    'إ.د':'AED',
    '₺':'TRY',
    '₴':'HRV',
    '₦':'NGN',
    'م.د.':'MAD',
    'RM':'MYR',
}

One of the challenges is that if/when the tokenizer sees a non-Pythonic character, it throws a fit (ERROR_TOKEN). So I had to use pre-processors. Granted, % is a pythonic character, but a binop, not a postfix unop. I haven't yet tried the new % handling code in the upcoming release (I'm still waiting for word on the changes I've made for handling uncertainties, and I don't want to mess up my existing PR by merging changes that don't belong to that PR). Pre-processors are a powerful way to address parsing problems like this.

@mac93pl
Copy link
Author

mac93pl commented Mar 24, 2023

Thanks @lafrech - it looks like the new version supports useage of "%" in such definitions!

@mac93pl mac93pl closed this as completed Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants