2013-01-19 10 views
6

मैं अजगरअजगर मुद्रण कोई __dict__

मैं iTunes वस्तु

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

की विशेषताओं को सूचीबद्ध करने के कोशिश कर रहा हूँ का उपयोग कर

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

मैं के लिए पटकथा पुल साथ परेशानी हो रही हूँ साथ जिम्मेदार बताते हैं वापस

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

किसी को पता है कि इस के आसपास कैसे जाना है?

उत्तर

5

dir(iTunes) की कोशिश करो। यह vars के समान है, लेकिन अधिक सीधे वस्तुओं के साथ उपयोग किया जाता है।

+0

हाँ कि –

+0

@AshleyHughes यह भावना में काम करता है कि कोई अपवाद नहीं फेंक दिया जाता है काम करता है, लेकिन मैं टी वास्तव में इन अजीब लपेटनों में उपलब्ध सब कुछ सूचीबद्ध नहीं करता है। उदाहरण के लिए, 'dir (SBAplication) में 'applicationWithBundleIdentifier_' नहीं है। – mmgp

+0

मैं सिर्फ जानकारी देखने के लिए मैं देख सकता हूँ आईई कुछ खेल है, जो इसका समर्थन नहीं करता है चाहता था, लेकिन कोई नहीं trackName देता तो मैं उपयोग कर रहा हूँ कि कोको के लिए –

1

यह सुंदर देर से आता है, लेकिन एक अलग समस्या (लेकिन एक ही त्रुटि) के लिए, मेरे लिए काम किया है:

json.dumps(your_variable) 

सुनिश्चित करें कि आप इस से पहले अपनी स्क्रिप्ट में JSON आयात किया है सुनिश्चित करें।

import json 

आपको जेएसओएन को एक स्वच्छ प्रारूप में पढ़ने का तरीका ढूंढना होगा।

+0

इस तरह के एक अजगर फ़ाइल वस्तु के रूप में कई वस्तुओं, के लिए काम नहीं करेगा। आपको त्रुटि मिलती है '' '>>> json.dumps (f) ट्रेसबैक (सबसे हालिया कॉल अंतिम): फ़ाइल" ", लाइन 1, टाइप एरर: JSON serializable''' नहीं है –

1

वार्स (obj), जब obj एक dict के रूप में सुलभ नहीं है करने के लिए कुछ इसी तरह के लिए, मैं इस तरह एक kludge का उपयोग करें:

>>> 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'}

मुझे यकीन है कि इस पर सुधार किया जा सकता है कर रहा हूँ , 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} 
संबंधित मुद्दे