-1

So I am trying to have a file that has a bunch of object in it that looks like this:

<class 'oPlayer.oPlayer'>,123,4,<class 'CommonObject.Wall'>,175,4,<class 'CommonObject.Wall'>,25,654,<class 'CommonObject.Wall'>,1,123,<class 'CommonObject.Wall'>,55,87

(No newlines for splitting purposes)

The file holds the object name, x, and y coordinate. (Basic info) But I'm not 100% sure how to create the objects from the file. Here is what I have:

def loadRoom(self, fname):

    # Get the list of stuff
    openFile = open(fname, "r")
    data = openFile.read().split(",")
    openFile.close()

    # Loop through the list to assign said stuff
    for i in range(len(data) / 3):

        # Create the object at the position
        newObject = type(data[i * 3])
        self.instances.append(newObject(int(data[i * 3 + 1]), int(data[i * 3 + 2])))

The objects in the file all take two arguments, x and y. So I'm also confused on how that would work. What I did was grab the list with all the split strings, (Which I displayed, it came out correct. No \n's) then I loop through the list (sort of) to set all the data. I assumed that type would return the object, but it doesn't.

Any help with said topic is very appreciated.

null
  • 548
  • 2
  • 6
  • 17
  • 5
    Where did this file come from? Where are the classes defined? What's going on here?! Are you just looking for [pickle](https://docs.python.org/2/library/pickle.html)? – tzaman Apr 25 '15 at 04:40
  • try built-in type function, see my answer here http://stackoverflow.com/a/8576049/29489 – number5 Apr 25 '15 at 04:44
  • 3
    `type` is not returning an object, because you have no objects, only strings. You can't magically regenerate an object, with all of its associated properties and methods, from a string that says ``. – MattDMo Apr 25 '15 at 04:46
  • Every file/class is in the same directory. – null Apr 25 '15 at 05:01
  • Why not use an automatically parseable serialization format like json, yaml, or pickle? – thebjorn Apr 25 '15 at 18:41

1 Answers1

-1

Try the approach from Get python class object from string:

import importlib
...
for i in range(len(data) / 3):    
        # get object data
        cls = data[i * 3]
        x = int(data[i * 3 + 1])
        y = int(data[i * 3 + 2])

        # get module and class
        module_name, class_name = cls.split(".")
        somemodule = importlib.import_module(module_name)

        # instantiate
        obj = getattr(somemodule, class_name)(x, y)

        self.instances.append(obj)

Here is a complete sample (put it in a file named getattrtest.py):

import importlib

class Test1(object):
    def __init__(self, mx, my):
        self.x = mx
        self.y = my

    def printit(self):
        print type(self)
        print self.x, self.y

class Test2(Test1):
    def __init__(self, mx, my):
        # changes x and y parameters...
        self.y = mx
        self.x = my

def main():
    cls = 'getattrtest.Test2'
    # get module and class
    module_name, class_name = cls.split(".")
    somemodule = importlib.import_module(module_name)

    # instantiate
    obj = getattr(somemodule, class_name)(5, 7)
    obj.printit()

if __name__ == "__main__":
    main()

Tested with Python 2.7

Community
  • 1
  • 1
gus27
  • 2,616
  • 1
  • 21
  • 25
  • I gave your solution a try, but it appears that the object is not actually being created. I try to call one of the objects' superclass functions, but it's not letting me. How do I create the object after? (BTW thanks for the response.) – null Apr 25 '15 at 16:36
  • @Paolo: I added a complete sample to my answer which shows that it works including function calls to superclass. Maybe there's a problem in `oPlayer.oPlayer` or `CommonObject.Wall` – gus27 Apr 25 '15 at 18:19