1

The title may be very vague, I did not know how to put the problem, so please excuse me for this.

I have a python file, say module.py

class myClass(obj):
    """
    class Description
    """
    def myMethod(args):
        """
        method Description
        """
        # method definition

I have one xml file, say module_result.xml

<testsuite errors="0" failures="0" name="module.myClass-20161215114804" skipped="0" tests="1" time="0.000">
    <testcase classname="module.myClass" name="myMethod" time="0.000"/>
    <system-out>
        something
    </system-out>
</testsuite>

I have another python file, test.py from where I have to use module_result.xml to parse module.py What I am doing in test.py (by giving hard coded path) is:

getdoc(module.myClass.myMethod)

I have to make this generic by reading names from xml, and those names are of type string

The argument type for getdoc() is object type, I am getting this error while using getddoc() with string:

str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.

working code in test.py:

import module
getdoc(module.myClass.myMethod)

But, this is hard coded, which is not acceptable in my case

I am extracting module/myClass/myMethod names from xml file

Lets say I got those names in variable by some logic as:

string_variable = "module.myClass.myMethod"

and I now want to do this:

getdoc(string_variable)

after doing this I am getting above mentioned error.

What can I do to convert string to object type?

atg
  • 204
  • 3
  • 18

1 Answers1

2

Referencing two different questions from elsewhere

here and here

Let's say you want to instantiate an instance of "myClass" from "myModule". To keep it simple, let's assume that "myModule" is already imported

import sys
import myModule
import myOtherModule

def create_object(modulename, classname):   # modulename and classname are strings
    mod = sys.modules[modulename]
    cls = getattr(mod, classname)
    inst = cls()
    return inst
Community
  • 1
  • 1
selbie
  • 100,020
  • 15
  • 103
  • 173
  • I did not get what you are trying to convey.. What is 'name' here? – atg Dec 20 '16 at 13:11
  • 1
    I thought what you were asking was this: *given a string that represents the name of a class, how do I instantiate an instance of that class?*. In any case, [this link](http://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported) might we what you are looking for. – selbie Dec 20 '16 at 18:21
  • I updated my answer again to reflect some new stuff I learned. – selbie Dec 20 '16 at 18:33
  • You got the question right the first time.. here I don't know the name of module/class/method.... They are just stored in some variable or list as string type (which was extracted from the xml file). I will edit the question to make things more clear... – atg Dec 21 '16 at 05:58
  • My updated answer is close to what you want. It assumes the module is already imported, but you have the target class in string form. e.g. `obj = create_object("myModule", "myClass")` You can use the links I reference on how to dynamically import a module by string e.g.`__import__("myModule")` – selbie Dec 21 '16 at 06:31