Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Correct way to call Pythonista script from within a Shortcuts workflow?
-
Hi everybody, thx for helping, I’ll buy Pyto and try.
-
@JonB said
But then pass handler directly to ObjC
how do I do that?
In the bosco solution, there is no @Block line
Édit: ok, understood, use the handler_block directly in
altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_(main_q, handler)
Thanks for your explanations
Edit2: but I need
@Block def handler(altitudeData:ObjCInstance, err:ObjCInstance) -> None: print(ObjCInstance(altitudeData).pressure)
-
I dropped the 2nd parameter (error) because it caused an exception: "item 2 in argtypes has no from_param method", so I tried running without the error parameter.
After reading the last comment by @JonB I now understand the proper use of @Block witch can be called directly.
This works for me.
# coding: utf-8 from rubicon.objc import Block, ObjCClass, ObjCInstance, py_from_ns from rubicon.objc.runtime import objc_id pressure = None @Block def handler(altitudeData:ObjCInstance, err:ObjCInstance) -> None: nspressure = ObjCInstance(altitudeData).pressure global pressure pressure = py_from_ns(nspressure) """ def bhandler(_data) -> None: nspressure = ObjCInstance(_data).pressure global pressure pressure = py_from_ns(nspressure) handler_block = Block(bhandler, None, (objc_id)) """ def get_pressure(): CMAltimeter = ObjCClass('CMAltimeter') NSOperationQueue = ObjCClass('NSOperationQueue') if not CMAltimeter.isRelativeAltitudeAvailable(): print('This device has no barometer.') return altimeter = CMAltimeter.new() main_q = NSOperationQueue.mainQueue #altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_(main_q, handler_block) altimeter.startRelativeAltitudeUpdatesToQueue_withHandler_(main_q, handler) print('Started altitude updates.') try: while pressure is None: pass finally: altimeter.stopRelativeAltitudeUpdates() print('Updates stopped.') return pressure if __name__ == '__main__': result = get_pressure() print(result) del pressure
-
-
@cvp Correct. After my morning brain fog cleared, that is what you said in your last edit. :-)
-
@bosco this, you bought Pyto and already tried...
-
@cvp I bought Pyto in 2019. I am currently running Pyto 17.1.1 I have tested barometer.py on iPhone 12 mini and iPad mini 4th gen.
-
-
-
@cvp said in Correct way to call Pythonista script from within a Shortcuts workflow?:
TypeError: unhashable type: 'list'
The "TypeError: unhashable type: 'list'" error message in Python occurs when you try to use a mutable object (such as a list) as a key in a dictionary. Since dictionaries use keys to index values, keys must be hashable objects, meaning they must be immutable (i.e. their value cannot be changed). Lists are mutable, so they cannot be used as dictionary keys. To fix the error, use an immutable object like a tuple, string, or number as the dictionary key instead.
To resolve the TypeError: unhashable type: 'list' error, you need to use an immutable object as a key in a dictionary instead of a mutable one like a list. For example, you can use tuples, strings, or numbers as keys, which are all hashable objects.
d = {[1, 2, 3]: "list_key"} //Using a list as a key in a dictionary //This will raise the TypeError: unhashable type: 'list' error d = {(1, 2, 3): "tuple_key"} //Using a tuple as a key in a dictionary //This will work fine as tuples are hashable