Find what other python types can an object convert to?

As a simple example, suppose a utility method that accepts a python object input_obj and out_type is a python type to convert (typecast) the object to

 def convert_obj(input_obj,out_type): 

-examples

 convert_obj('2','int') #returns 2 convert_obj([1,2,3],'tuple') #returns (1,2,3) 

The method only supports objects of certain types, such as str, list, tuple, and then checks whether it can be converted to the specified out_type. This rule is presented in the method:

  supported_conversions = { tuple: [str, list, set], str: [tuple, float, list, long, int, set], float: [str, long, int], list: [tuple, str, set], long: [str, float, int], dict: [tuple, str, list, set], int: [str, float, long], set: [tuple, str, list], } 

The keys in supported_conversions are valid types for input_obj.

Question Besides using try / except from the list of all possible python types to convert a sample object of each type and then look at valid ones, for example checking str conversion against [list, dict, tuple, int, tuple, set] is there a better way in python to generate dict supported_conversions given his keys?

Note. Other type conversion exceptions should be ignored. for example, “1” can be converted to the integer 1, but “XYZ” cannot be. But that still means str-> int is a valid conversion possible.

+6
source share
1 answer

I think the problem space is not well defined for such a method to exist. Some transformations will be destructive, and some may take place in more than one way. A few examples:

 >>> list(set([1,2,2,3])) [1, 2, 3] >>> list("hello") ['h', 'e', 'l', 'l', 'o'] >>> ["hello"] ['hello'] >>> list({'a':1, 'b': 2}) ['a', 'b'] >>> list({'a':1, 'b': 2}.iteritems()) [('a', 1), ('b', 2)] 

For an argument, you can also convert the string to any type of Python using eval() .

So basically it all depends on your use case.

If you really want to do a more comprehensive search, you can use the types module to get a list of built-in types, and then try to cross-convert (provided that you can get instances of each of them)

 >>> import types >>> [types.__dict__.get(t) for t in dir(types) if t.endswith('Type')] [<type 'bool'>, <type 'buffer'>, <type 'builtin_function_or_method'>, <type 'builtin_function_or_method'>, <type 'classobj'>, <type 'code'>, <type 'complex'>, <type 'dictproxy'>, <type 'dict'>, <type 'dict'>, <type 'ellipsis'>, <type 'file'>, <type 'float'>, <type 'frame'>, <type 'function'>, <type 'generator'>, <type 'getset_descriptor'>, <type 'instance'>, <type 'int'>, <type 'function'>, <type 'list'>, <type 'long'>, <type 'member_descriptor'>, <type 'instancemethod'>, <type 'module'>, <type 'NoneType'>, <type 'NotImplementedType'>, <type 'object'>, <type 'slice'>, <type 'str'>, <type 'traceback'>, <type 'tuple'>, <type 'type'>, <type 'instancemethod'>, <type 'unicode'>, <type 'xrange'>] 

I do not know if you need to create a supported_conversions dictionary in advance. Assuming you always convert intype to outtype on outtype(intype_value) , you can try this and then update the dictionary that displays (intype, outtype) -> bool and therefore it will not try to perform the conversion again if it fails for the first time.

+3
source

All Articles