wolph / alfred-converter

Alfred unit converter is a smart calculator for Alfred with support for unit conversions to make it a bit comparable to the Google Calculator and Wolfram Alpha.
93 stars 15 forks source link

How should I convert new units #27

Closed Dianelynn1 closed 3 years ago

Dianelynn1 commented 4 years ago

Since I need to calculate a lot of PP transfer fee every day, I want to use Alfred to quickly calculate the PP fee result. How can I implement this conversion procedure in your workflow?

The PP fee calculation tool I currently use is shown in the link, and the US dollar is needed. The specific value set is 4.4%+0.3USD https://www.clothnappytree.com/ppcalculator/ image

Thanks for your help!

Dianelynn1 commented 4 years ago

Because I don’t have any experience in programming, I hope you can explain in detail how I can operate or modify your workflow if you can help me.

Thanks again!

wolph commented 4 years ago

There are 2 ways to add new units to the system.

  1. By editing this bit of code: https://github.com/WoLpH/alfred-converter/blob/master/converter/extra_units.py
  2. By editing the units XML file: https://github.com/WoLpH/alfred-converter/blob/master/poscUnits22.xml

When editing the extra_units.py file I think you would need something like this as part of the register_post function:


convert.Unit(
  id='dollar',
  name='Dollar',
  annotations=['$', 'dollar'],
  conversion_params=('0', '0', '1', '0'),
).register(units)

convert.Unit(
  id='paypal-fee',
  name='PayPal fee',
  base_unit='dollar',
  conversion_params=('0.3', '0.044', '1', '0'),

Note: the conversion params work like this: (a + b * value) / (c + d * value)

Perhaps I'll add something in the future to make this easier, but for now that's the only option. You're the first to ask for a custom unit :)

Dianelynn1 commented 4 years ago

I compared your new code formats and put them in this section of the extra_units.py file.

But I still don’t seem to add them correctly, because PP-fee is not available in the Alfred 4

image

image Is this because I put the code in the wrong place or is it just that Alfred made a mistake about using the PP hotkey?

Sorry for taking your time again.

wolph commented 3 years ago

It seems I somehow missed your reply... sorry about that

In any case, it looks like my example was a bit too limited to work. I've tried a little more and I've come up with this code:

    params = dict(
        units=units,
        quantity_types=['money'],
        base_unit='dollar',
    )

    add = 0.3
    fee = 4.4
    multiplier = fee * 0.0105
    convert.Unit(
        id='dollar',
        name='Dollar',
        annotations=['$', 'dollar'],
        conversion_params=('0', '1', '1', '0'),
        **params
    ).register(units)

    convert.Unit(
        id='paypal-fee',
        name='PayPal fee',
        annotations=['pp', 'paypal'],
        conversion_params=('-%s' % add, '1', str(multiplier), '0'),
        **params
    ).register(units)

    convert.Unit(
        id='paypal-total',
        name='PayPal Total',
        annotations=['pt'],
        conversion_params=('-%s' % add, '1', str(1 + multiplier), '0'),
        **params
    ).register(units)

If needed you can easily change the 4.4% and +0.3. Note that after editing the code you will have to delete the units.pickle file which caches the calculations.

This is how it looks right now: image

Dianelynn1 commented 3 years ago

thank you very much for your help!

This is indeed very practical to me, thanks again!

On Sat, 17 Oct 2020 at 07:40, Rick van Hattem notifications@github.com wrote:

It seems I somehow missed your reply... sorry about that

In any case, it looks like my example was a bit too limited to work. I've tried a little more and I've come up with this code:

params = dict(
    units=units,
    quantity_types=['money'],
    base_unit='dollar',
)

add = 0.3
fee = 4.4
multiplier = fee * 0.0105
convert.Unit(
    id='dollar',
    name='Dollar',
    annotations=['$', 'dollar'],
    conversion_params=('0', '1', '1', '0'),
    **params
).register(units)

convert.Unit(
    id='paypal-fee',
    name='PayPal fee',
    annotations=['pp', 'paypal'],
    conversion_params=('-%s' % add, '1', str(multiplier), '0'),
    **params
).register(units)

convert.Unit(
    id='paypal-total',
    name='PayPal Total',
    annotations=['pt'],
    conversion_params=('-%s' % add, '1', str(1 + multiplier), '0'),
    **params
).register(units)

If needed you can easily change the 4.4% and +0.3. Note that after editing the code you will have to delete the units.pickle file which caches the calculations.

This is how it looks right now: [image: image] https://user-images.githubusercontent.com/270571/96322509-a269c680-1019-11eb-9dca-fbb6b6ae1c6f.png

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/WoLpH/alfred-converter/issues/27#issuecomment-710698330, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQWTXPSE4EK73OQMZVMVZBTSLDKWHANCNFSM4QIHQDPQ .