Jump to content

Unit converter (no longer maintained)


Recommended Posts

The built-in list of units is here: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt. My additions are here: https://github.com/jason0x43/jc-units/blob/master/unit_defs.txt. I just added "C" and "F" for temperature (different names) and a definition for hectare (just for fun).

 

The unit system is easy to extend, although it doesn't seem to automatically handle unit name clashes. I have some simple logic in the workflow to deal with situations like "u 1cup>oz", where the unit name "oz" refers to weight ounces, but we mean fluid ounces. Basically I have a table of alternate possibilities for some unit names. If a conversion fails and either the source or destination units are in the table, the converter tries again with the alternate units. 

 

Awesome. Awesome. Awesome! This is going to be incredibly handy.

 

Thank you again.

Link to comment

Currency conversion is back. I'm using the Yahoo! Finance API to get current rates. The workflow will download the most recent rates the first time you use it each day. Currency units are the standard 3-letter abbreviations for currency names (US dollar is USD, Australian dollar is AUD, yen is JPY, etc.).

Edited by jason0x43
Link to comment

Currency conversion is back. I'm using the Yahoo! Finance API to get current rates. The workflow will download the most recent rates the first time you use it each day. Currency units are the standard 3-letter abbreviations for currency names (US dollar is USD, Australian dollar is AUD, yen is JPY, etc.).

You seem to be missing euros (EUR).

On another note, is there an easy way to change the string that tells it to convert? I’d like to change > to to.

Link to comment

You seem to be missing euros (EUR).

On another note, is there an easy way to change the string that tells it to convert? I’d like to change > to to.

 

Oops. Euros are in there now.

 

You can now use the 'u>' command to list extra commands, including 'Open config file...'. This is a JSON formatted file. The only two options it currently understands are log_file and "separator". Change separator to "to" and you should be in good shape. If you change a value using Apple's TextEdit, make sure it doesn't change your double quotes to rich text quote marks. Those make the JSON parser cry.

Link to comment

I uploaded an update with support for this.

 

Using a space as the separator can lead to ambiguous queries. For example, "1 pint imperial pint" will be understood as "convert 1 pint imperial to pints", which isn't what was intended. To get around that, you can use underscores in the names of units that have spaces if you're using space as the separator: "1 pint imperial_pint". If you're using a unique character, like '>', you can just do "1pint>imperial pint".

Link to comment

For me the new version you uploaded doesn't work anymore at all. Whatever I plug in the configuration file, e.g. "u 10 eur usd", respectively "u 10 eur>usd" whether it's ">" or " ", I just get "Waiting for input...".

 

Sorry about that. My new query parsing code didn't handle spaces at the beginning of the query properly. Try it now.

Link to comment

The currency cache updates once a day. If you tried it before I added EUR conversions, it wouldn't refresh until the next day. (I should probably add a cache clearing command for that...)

I had actually cleared the cache directory before, so I thought that couldn’t be the problem, but just realised I deleted it before you added EUR support. It’s working now, thank you.

Link to comment
  • 1 month later...
  • 1 month later...

Hello,

 

A couple of questions/comments

 

1. For some reason the unit math is still not working for me. I just downloaded the workflow again today.  Error log is below

>>

[2014-03-07 16:31:09,166] INFO: convert: loading definitions from /Users/vikasmis/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/jc-units/currencies.txt
[2014-03-07 16:31:09,170] ERROR: alfred_convert: Error converting
Traceback (most recent call last):
  File "alfred_convert.py", line 89, in _convert
    value, text = self.converter.convert(query)
  File "convert.py", line 48, in convert
    in_val = Q_(value)
  File "pint/quantity.py", line 121, in __new__
    inst = cls._REGISTRY.parse_expression(value)
  File "pint/unit.py", line 1022, in parse_expression
    raise UndefinedUnitError(unknown)
UndefinedUnitError: 'i' is not defined in the unit registry
>>

 

2. In addition I have realized that if I have a space between the number and units, it doesn't work. For example -  

"u 5km > miles" works but "u 5 km > miles" doesn't work. Is this intentional? If so, may help to add a comment to the top post.

 

 

Regards,
Vikas
Edited by vikas.mishra
Link to comment
  • 1 month later...

Hi,

 

I'm getting this error. It is present only when I'm on LAN behind proxy server (system wide proxy server setting, http, https and socks proxy). Is there any workaround?

[ERROR: alfred.workflow.input.scriptfilter] Code 1: Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "alfred_convert.py", line 54, in __init__
    self.load_currencies()
  File "alfred_convert.py", line 79, in load_currencies
    rates = yfinance.get_rates(CURRENCIES)
  File "yfinance.py", line 22, in get_rates
    response = requests.get(API, params=params)
  File "requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "requests/adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='query.yahooapis.com', port=80): Max retries exceeded with url: /v1/public/yql?q=select+Name%2C+Rate+from+yahoo.finance.xchange+where+pair%3D%27AUD%2CBGN%2CBRL%2CCAD%2CCHF%2CCNY%2CCZK%2CDKK%2CEUR%2CGBP%2CHKD%2CHRK%2CHUF%2CIDR%2CILS%2CINR%2CJPY%2CKRW%2CLTL%2CLVL%2CMXN%2CMYR%2CNOK%2CNZD%2CPHP%2CPLN%2CRON%2CRUB%2CSEK%2CSGD%2CTHB%2CTRY%2CZAR%27&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&format=json (Caused by <class 'socket.error'>: [Errno 61] Connection refused)
Link to comment

I imagine you'll have the same problem with most workflows that access the network.

At least for Python workflows, making sure the http_proxy environmental variable is correctly set will ensure workflows use the proxy. This is a bit complicated to set, however.

On Mavericks, you need to set it in the launchd environment (see here). For earlier versions, you can use environment.plist.

If you only have a small number of workflows, you can fix it on a per-workflow basis.

For this one, you'll need to edit the two Script Filters and the Run Script action and add the following at the top of the script box:

 

import os
os.environ['http_proxy'] = 'http://192.168.0.1:3128'
changing http://192.168.0.1:3128 to whatever the IP address/hostname and port of your proxy server is. Edited by deanishe
Link to comment
  • 4 weeks later...

What part of which file would I have to modify in order for it to allow a space between the value and unit (for every query, not just currencies)?

 

For example rather than

u 20usd to aud

I'd like it be

u 20 usd to aud

Thanks.

Link to comment

What part of which file would I have to modify in order for it to allow a space between the value and unit (for every query, not just currencies)?

 

For example rather than

u 20usd to aud

I'd like it be

u 20 usd to aud

Thanks.

 

I just pushed an update that supports this. The code that processes the input is in convert.py. I do some very naive string pre-processing to allow unit names to contain spaces, and it was also combining the number with its units.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...