2013-01-19 10 views
6

Sto avendo difficoltà con ponte di scripting per Pythonstampa pitone attributi senza __dict__

Sto cercando di elencare gli attributi dell'oggetto iTunes

iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes") 

utilizzando

>>> from pprint import pprint 
>>> from Foundation import * 
>>> from ScriptingBridge import * 
>>> iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes") 
>>> pprint (vars(iTunes)) 

ottengo back

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: vars() argument must have __dict__ attribute 

qualcuno sa come aggirare questo?

risposta

5

Prova dir(iTunes). È simile a vars, ma più direttamente utilizzato con gli oggetti.

+0

Sì che funziona –

+0

@AshleyHughes funziona nel senso che non viene generata un'eccezione, ma io t in realtà non elenca tutto disponibile in questi strani involucri. Ad esempio, non esiste alcun 'applicationWithBundleIdentifier_' in 'dir (SBApplication)'. – mmgp

+0

Volevo solo vedere quali informazioni posso vedere IE è qualcosa che suona, che non supporta ma trackName restituisce None, quindi sto usando i metodi di classe –

1

Questo avviene piuttosto tardi, ma per un problema diverso (ma lo stesso errore), i seguenti ha lavorato per me:

json.dumps(your_variable) 

Assicurarsi di aver importato JSON nello script prima di questo.

import json 

È necessario trovare un modo per leggere JSON in un formato pulito.

+0

non funzionerà per molti oggetti, come un oggetto file Python. si ottiene l'errore '' '>>> json.dumps (f) Traceback (ultima chiamata ultima): File" ", riga 1, in TypeError: non è serializzabile JSON''' –

1

per qualcosa di simile a Vars (obj), quando obj non è accessibile come un dict, io uso un kludge come questo:

>>> obj = open('/tmp/test.tmp') 
>>> print vars(obj) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: vars() argument must have __dict__ attribute 
>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_')) 

{'softspace': 0, 'encoding': None, 'flush': <built-in method flush of file object at 0xf7472b20>, 'readlines': <built-in method readlines of file object at 0xf7472b20>, 'xreadlines': <built-in method xreadlines of file object at 0xf7472b20>, 'close': <built-in method close of file object at 0xf7472b20>, 'seek': <built-in method seek of file object at 0xf7472b20>, 'newlines': None, 'errors': None, 'readinto': <built-in method readinto of file object at 0xf7472b20>, 'next': <method-wrapper 'next' of file object at 0xf7472b20>, 'write': <built-in method write of file object at 0xf7472b20>, 'closed': False, 'tell': <built-in method tell of file object at 0xf7472b20>, 'isatty': <built-in method isatty of file object at 0xf7472b20>, 'truncate': <built-in method truncate of file object at 0xf7472b20>, 'read': <built-in method read of file object at 0xf7472b20>, 'readline': <built-in method readline of file object at 0xf7472b20>, 'fileno': <built-in method fileno of file object at 0xf7472b20>, 'writelines': <built-in method writelines of file object at 0xf7472b20>, 'name': '/tmp/test.tmp', 'mode': 'r'}

Sono sicuro che questo potrebbe essere migliorata , come ad esempio per filtrare le funzioni con if not callable(getattr(obj, attr):

>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_') and not callable(getattr(obj, attr))) 
{'errors': None, 'name': '/tmp/test.tmp', 'encoding': None, 'softspace': 0, 'mode': 'r', 'closed': False, 'newlines': None}