I am building a list of Python objects that I use fairly regularly that have a length property, (len, 'len') and I was trying to determine the best way to simplify the type/class formatter that the object uses. For example: type([]), returns "< type 'list'>" and type(collections.Counter()) returns "< class 'collections.Counter'> "
I had assumed that the formatter that was used could be accessed simply and the extraneous information removed to facility a particular documentation format that I prefer. In this situation, I had hoped to be able to remove '< type ... >' or '< class ... >' from the resultant output. The kludge that I have come up with entails parsing the string into its components and returning the portion I want. To ensure generality, for objects that I may not currently use, I had hoped for a better solution.
Insights regarding options to this issue, or more general comments would be appreciated. The line in question is flagged with a comment. Apologies, if I am not supposed to included doc information with posted scripts.
"""
Script: empty_tests.py
Modified: 2015-05-25
Purpose:
checks on objects that I use, that have a __len__ property
Notes:
- collections.OrderedDictionary and other classes in collections behave
in a similar fashion
- for NumPy arrays, use size property rather than __len__
"""
import numpy as np
import collections
c0 = collections.Counter()
c1 = collections.Counter([0])
objs = [ [],[1],(),(1),{},{1:"one"},"","1",None,True,1,False,0,c0,c1 ]
is_empty = [ True if not i else False for i in objs ]
t = [ type(i).__name__ for i in objs ] # correct based on comment
#t = [str(type(i)).split("\'")[1] for i in objs ] # line in question
print("\n{:<15} {:<6} {:<10}".format("Object","Empty","Type"))
for i in range(len(objs)):
print("{:<15} {:<6} {:<10s}".format(objs[i],str(is_empty[i]),t[i]))
Output with the above...the commented out line worked
Object Empty Type
[] True list
[1] False list
() True tuple
1 False int
{} True dict
{1: 'one'} False dict
True str
.... etc
I have tried ....
>>> type([])
<type 'type'>
>>> repr(type([]))
<type 'list'>"
>>> str(type([]))
<type 'list'>"
>>> print str(type([])), repr(type([]))
<type 'list'> <type 'list'>
>>>
EDIT the "name" property wasn't listed where I thought it would be, so if the object has a "format" property, check there.
>>> help(type(object).__format__)
Help on method_descriptor:
__format__(...)
default object formatter
>>> dir(type(object).__format__)
['__call__', '__class__', '__delattr__', '__doc__', '__format__',
'__get__', '__getattribute__', '__hash__', '__init__', '__name__',
'__new__', '__objclass__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>>
type(obj).__name__
? \$\endgroup\$dir
to show you all the special methods ("The resulting list is not necessarily complete"). The right place to look is the Data model section of the Python Language Reference. \$\endgroup\$