Skip to main content
formatting
Source Link
NaN
  • 218
  • 3
  • 9
>>> 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__']
    >>> 
>>> 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__']
    >>> 
solution
Source Link
NaN
  • 218
  • 3
  • 9
"""
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

EDIT the "name" property wasn't listed where I thought it would be, so if the object has a "format" property, check there.

"""
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 = [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

"""
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

EDIT the "name" property wasn't listed where I thought it would be, so if the object has a "format" property, check there.

additional information, formatting
Source Link
NaN
  • 218
  • 3
  • 9
Object          Empty  Type      
[]              True   list      
[1]             False  list      
()              True   tuple     
1               False  int       
{}              True   dict      
{1: 'one'}      False  dict      
                True   str   
.... etc   

.... etc

I have tried ....

Object          Empty  Type      
[]              True   list      
[1]             False  list      
()              True   tuple     
1               False  int       
{}              True   dict      
{1: 'one'}      False  dict      
                True   str   

.... etc

tried ....

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 ....

additional information
Source Link
NaN
  • 218
  • 3
  • 9
Loading
additional information
Source Link
NaN
  • 218
  • 3
  • 9
Loading
Source Link
NaN
  • 218
  • 3
  • 9
Loading