So, I was reading a thread over in MUSHclient/General and Nick mentioned a var.lua file that shipped with MUSHclient.
This particular file introduces a global object var that is a proxy for the world.GetVariable/world.SetVariable functions and since the script file for my current world has one of these functions on every other line, I thought this was Cool. (Save me half my typing, nearly :D)
So, I wrote a Python version. It's more verbose than the Lua version because I went through the Python manual and glued every feature on to it that I could find.
As an example:
All work. In fact, you can pretty much treat var as a dict :) (In hindsight, being able to iterate over all the variables is overkill. Like I said, every feature I could find in the manual.)
I also added functionality for GetPluginVariable/... so:
Should also work. (Not completely tested since I don't have any plugins ... everything is in my main script file :-/ )
If you don't want to paste it directly into your main script file, you can put it in a separate .py file in your script directory and then import it. (I have it in my mushclient.py with all the error codes and other utility odds-and-ends.)
Of course WSH's support of Python's import statement is a bit broken, so you may need to have the following above the 'from var import *' line:
Replacing the pathname with the appropriate value for your installation.
EDIT:Erm. Ignore that. I forgot you can't find world from an imported .py. *sigh*
EDIT-2:BUT you can find world if you execfile() the .py/.pys file instead. So my main script file is down to 80 lines or so with another thousand included with execfile()
Oh and lacking any obvious way of determining the difference between a string that is a pickled object and a string that might be a pickled object I prefixed pickles with PICKLE_SIGNATURE which is set to "~~~" below. If you intend to put a string starting with ~~~ into a variable then you will need to change the value of PICKLE_SIGNATURE. (It can be anything, including "PICKLE_SIGNATURE" if you wanted to... just be wary of the overhead involved if you set it to something long and then pickle lots of objects.)
By default it uses Python's pickle0 protocol, which is pure us-ascii (no \x00, nothing with bit7 set) and is probably overly-safe for MUSHclient's variables.
... having said that, it avoids pickling obvious strings (ie str and unicode objects) allowing them to be used in @expansions so they still have the same line-ending confusion as per usual. bools, ints, floats and complexes should also all be stored in a fashion conducive to both @expansion and them coming back with the same type they went in with (so no need to coerce them back to the proper type when getting them).
And I avoided the bool(str(False)) == True issue too :D
(Code deferred to followup since it's 5757 characters by itself.)
This particular file introduces a global object var that is a proxy for the world.GetVariable/world.SetVariable functions and since the script file for my current world has one of these functions on every other line, I thought this was Cool. (Save me half my typing, nearly :D)
So, I wrote a Python version. It's more verbose than the Lua version because I went through the Python manual and glued every feature on to it that I could find.
As an example:
for k, v in var.iteritems():
world.Note("%s = %s" % (k, v))
var.stuff = { 'a': 1, 'b': 2 }
world.Note(var['stuff']['a'])
All work. In fact, you can pretty much treat var as a dict :) (In hindsight, being able to iterate over all the variables is overkill. Like I said, every feature I could find in the manual.)
I also added functionality for GetPluginVariable/... so:
pvar = PluginVar("a42fbf74f394d8129df956ae")
pvar.hp = 12
Should also work. (Not completely tested since I don't have any plugins ... everything is in my main script file :-/ )
If you don't want to paste it directly into your main script file, you can put it in a separate .py file in your script directory and then import it. (I have it in my mushclient.py with all the error codes and other utility odds-and-ends.)
Of course WSH's support of Python's import statement is a bit broken, so you may need to have the following above the 'from var import *' line:
import sys, os
cwd = r"C:\Program Files\MUSHclient\scripts"
os.chdir(cwd)
if not cwd in sys.path:
sys.path.append(cwd)
Replacing the pathname with the appropriate value for your installation.
EDIT:Erm. Ignore that. I forgot you can't find world from an imported .py. *sigh*
EDIT-2:BUT you can find world if you execfile() the .py/.pys file instead. So my main script file is down to 80 lines or so with another thousand included with execfile()
Oh and lacking any obvious way of determining the difference between a string that is a pickled object and a string that might be a pickled object I prefixed pickles with PICKLE_SIGNATURE which is set to "~~~" below. If you intend to put a string starting with ~~~ into a variable then you will need to change the value of PICKLE_SIGNATURE. (It can be anything, including "PICKLE_SIGNATURE" if you wanted to... just be wary of the overhead involved if you set it to something long and then pickle lots of objects.)
By default it uses Python's pickle0 protocol, which is pure us-ascii (no \x00, nothing with bit7 set) and is probably overly-safe for MUSHclient's variables.
... having said that, it avoids pickling obvious strings (ie str and unicode objects) allowing them to be used in @expansions so they still have the same line-ending confusion as per usual. bools, ints, floats and complexes should also all be stored in a fashion conducive to both @expansion and them coming back with the same type they went in with (so no need to coerce them back to the proper type when getting them).
And I avoided the bool(str(False)) == True issue too :D
(Code deferred to followup since it's 5757 characters by itself.)