name-spaces and loading a file

Alex Shinn foof at eyeofdog.com
Sat Feb 19 15:37:20 EST 2000


>>>>> "Terry" == Terry Reedy <tjreedy at udel.edu> writes:

    Terry> "Alex Shinn" <foof at eyeofdog.com> wrote in message

    >> I'm working on an interactive story engine with the primary
    >> purpose of making it easy for non-programmers to write story
    >> modules.  A story would be comprised of a collection of files,
    >> each describing a scene, like the following:

    Terry> ...

    Terry> Suggestions (emailed and posted):

    Terry> 1. Separate initial processing of input text from run-time
    Terry> use of compiled internal version so you can optimize
    Terry> different data forms for different operations (writing
    Terry> versus running).

I'm thinking this may be a good way to go, but I'll have to change my
concept of a scene file, which was arbitrary python code that got
executed when you visited a scene, not just setting of variables.  But
I was going to have to come up with a way to distinguish between the
first and later times that you visit a scene anyway... probably let
the author define first(), second() and later() functions, etc.

Can you pickle a function (I just tried and it didn't work, but I may
be missing something)?  Perhaps when they needed to define functions
they could specify a separate file to get them from.

    Terry> 2. Allow more than one scene per system file (easy if you
    Terry> do 1.); little files take up extra space (especially on
    Terry> Win95 with 32K disk blocks) and can be a nuisance to work
    Terry> with.  Some authors may prefer to have related scenes, if
    Terry> not all, in one file for easier editing.

I happen to like working with little files (convenient for scripting),
but this should probably be an option.

    Terry> 3. Compile input text to either dictionary or scene class
    Terry> instance (former may seem initially easier, but latter
    Terry> bundles scene methods within scene class).  In other words,
    Terry> exec just once before running.

I was using a scene class, translating variables from the scene module
into the class.  The current code looks like:

class Scene:
    def __init__(self, game, name):
        "Initialize a Scene from a file"
        # Reference the parent game
        self.game = game
        # Load the scene and adjust the history
        if game.history.has_key(name):
            exec 'reload(scenes.'+name+')'
            game.history[name] = game.history[name] + 1
        else:
            scene = __import__('scenes.'+name)
            game.history[name] = 1
        # Translate the module variables into class variables
        exec 'vars = dir(scenes.'+name+')'
        for v in vars:
            if not v[0] == '_':
                exec 'self.'+v+' = scenes.'+name+'.'+v

and yes, I know it's ugly and inefficient, which is why I'm asking for
advice :).  I guess the "load a file for a scene" idea was just bad.
As long as there's a simple tool for converting scene files to data
files, it should still be easy to write stories.  And we were planning
on a graphical tool anyway.

Thanks everyone who responded!


-- 
Alex Shinn



More information about the Python-list mailing list