Persistent variables?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.

Persistent variables?

Postby 22hn » Sat Mar 06, 2010 12:43 pm

Sometimes when using variables to store a state of something, I would like that variable to be persistent between EG sessions. I don't want to loose the state just because of an EG restart...

So let's say I have set the variable eg.globals.LightsON = True, is there a way to store that value on EG exit and reload it when EG is started again?

The best would be if I could store the whole collection eg.globals without having to specify each subname...
22hn
 
Posts: 10
Joined: Tue Jan 19, 2010 9:56 pm

Re: Persistent variables?

Postby stottle » Sat Mar 06, 2010 3:13 pm

You have to create a plugin to store variables, as it is the plugin loading that retrieves the previous values.

Within the plugin, you create a class like so:
Code: Select all
class Config(eg.PersistentData):
    position = None
    size = (550, 400)
    splitPosition = 210


The values listed above are the defaults (None, (550, 400), 210), which are overwritten when so set the value to something else.

From then on, Config.position with be whatever it was set to last, even from a previous run of EG. And you can set its value just like a normal variable.

As for storing all of eg.globals, why on earth would you want to do that? You should look at this post regarding that idea....

Good luck.

Brett
stottle
Plugin Developer
 
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: Persistent variables?

Postby 22hn » Sat Mar 06, 2010 3:18 pm

Thanks Brett, I'll give it a try...!

Hakan
22hn
 
Posts: 10
Joined: Tue Jan 19, 2010 9:56 pm

Re: Persistent variables?

Postby krambriw » Fri Mar 19, 2010 3:55 pm

Code: Select all
class Config(eg.PersistentData)


STottle, this is great, I have looked for something like this. I have a couple of plug-ins I can upgrade where I currently use Python function pickle for persistent data but this will be much, much simpler

Do you have any idea where the data actually is stored?

Best regards, Walter
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Persistent variables?

Postby stottle » Sat Mar 20, 2010 12:25 am

On Vista/Win7 it's in C:\Users\<USER>\AppData\Roaming\EventGhost. AppData is a hidden folder, btw....

I forget on XP.

Brett
stottle
Plugin Developer
 
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: Persistent variables?

Postby Livin » Sat Mar 20, 2010 11:44 pm

Would be nice to have a plug-in that created system wide variables any event could set/read and use as a condition. :D
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 749
Joined: Wed Oct 08, 2008 4:56 am

Re: Persistent variables?

Postby 22hn » Sun Mar 21, 2010 12:34 am

I've made a first version of a plugin called pVar. (persistent variables)

Works like this:

You can set a variable in two ways.
* Use the action SetGlobalsVar and enter your variable name and value
* In a python script, use eg.plugins.pVar.SetGlobalsVar('varname', value)

This will set eg.globals.varname = value and then save the var/value to yourconfigname.pvar

The next time you start EG, all variables will be restored to it's previous value.

It also checks for obsolete variables (once stored to file but no longer used in your config), and remove those from the pvar file automatically.
Attachments
pVar.zip
version 1.0.0
(4.16 KiB) Downloaded 139 times
22hn
 
Posts: 10
Joined: Tue Jan 19, 2010 9:56 pm

Re: Persistent variables?

Postby Livin » Sun Mar 21, 2010 1:07 am

22hn wrote:I've made a first version of a plugin called pVar. (persistent variables)

Works like this:

You can set a variable in two ways.
* Use the action SetGlobalsVar and enter your variable name and value
* In a python script, use eg.plugins.pVar.SetGlobalsVar('varname', value)

This will set eg.globals.varname = value and then save the var/value to yourconfigname.pvar

The next time you start EG, all variables will be restored to it's previous value.

It also checks for obsolete variables (once stored to file but no longer used in your config), and remove those from the pvar file automatically.



nice job!

I suggest an option for non-persistence on a per variable basis. I can foresee the need to have some variables clear when the session ends.
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 749
Joined: Wed Oct 08, 2008 4:56 am

Re: Persistent variables?

Postby 22hn » Sun Mar 21, 2010 1:28 am

Livin wrote:I suggest an option for non-persistence on a per variable basis. I can foresee the need to have some variables clear when the session ends.


If you don't want the variable to be persistent, don't set it with pVar...!

just set eg.globals.varname = value, then pVar won't save it.
22hn
 
Posts: 10
Joined: Tue Jan 19, 2010 9:56 pm

Re: Persistent variables?

Postby Livin » Sun Mar 21, 2010 4:39 am

For those not technically inclined (a lot of EG users) it would be nice to provide a GUI to do so and avoid a Python script object. Up to you :)
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 749
Joined: Wed Oct 08, 2008 4:56 am

Re: Persistent variables?

Postby eatmeimadanish » Tue Aug 16, 2011 9:30 pm

I get an error when setting a variable to a string. Anyone else having this issue?

Example : NameError: name 'test' is not defined
eatmeimadanish
Experienced User
 
Posts: 96
Joined: Thu Oct 01, 2009 5:11 pm

Re: Persistent variables?

Postby eatmeimadanish » Wed Aug 17, 2011 3:09 am

Nevermind, fixed your code that was broken. Here is the new version, remember to delete the .pyc and relaunch EG. This will work for both integers and strings now.
Attachments
__init__.py
pVar v1.0.1
(5.22 KiB) Downloaded 123 times
eatmeimadanish
Experienced User
 
Posts: 96
Joined: Thu Oct 01, 2009 5:11 pm


Return to Coding Corner

Who is online

Users browsing this forum: Google Adsense [Bot] and 1 guest