Still have to prepend "default" when assigning variables to this class, but it now allows val = inst.vaule: - """Encapuslate a Default Values Object and Config File"""
-
-
from inspect import getmembers
-
import wx
-
-
class DefaultValueHolder(object):
-
"""Intended for use with wxConfig (or maybe _winreg) to set up and/or get
-
registry key names and values. Name attrs as default*. "default"
-
will be stripped of when reading and writing to the config file.
-
You may not use the name varDict as one of the variable names."""
-
-
def __init__(self, appName, grpName):
-
"""Open or create the application key"""
-
self.appName = appName
-
self.grpName = grpName # if the key or group doesn't exit, it will be created
-
self.config = wx.Config(appName) # Open the file (HKCU in windows registry)
-
-
def __getattribute__(self, name):
-
try:
-
return object.__getattribute__(self, "default%s" %name)
-
except AttributeError:
-
return object.__getattribute__(self, name)
-
-
def GetVariables(self):
-
return [{"name":var[0][7:], "value":var[1], "type":type(var[1])}
-
for var in getmembers(self) if var[0].startswith('default')]
-
-
def SetVariables(self, varDict={}, **kwargs):
-
kwargs.update(varDict)
-
for name, value in kwargs.items():
-
setattr(self, "default%s" %name, value)
-
-
def InitFromConfig(self):
-
config = self.config
-
group = self.grpName
-
-
if not config.Exists(group):
-
self.WriteRegistryGroup(group)
-
-
else:
-
config.SetPath(group)
-
for var in self.GetVariables():
-
name = var['name']
-
if config.Exists(name):
-
value = self.ReadRegistry(name, var['type'])
-
self.SetVariables({name:value})
-
else:
-
self.WriteRegistry(name, var['value'], var['type'])
-
config.SetPath("")
-
-
def WriteRegistryGroup(self, group):
-
self.config.SetPath(group)
-
for var in self.GetVariables():
-
self.WriteRegistry(var['name'], var['value'], var['type'])
-
self.config.SetPath("")
-
-
def UpdateConfig(self):
-
self.WriteRegistryGroup(self.grpName)
-
-
def ReadRegistry(self, name, type):
-
value = None
-
if type == str:
-
value = self.config.Read(name)
-
elif type in (int, long):
-
value = self.config.ReadInt(name)
-
elif type == float:
-
value = self.config.ReadFloat(name)
-
return value
-
-
def WriteRegistry(self, name, value, type):
-
if type == str:
-
self.config.Write(name, value)
-
elif type in (int, long):
-
self.config.WriteInt(name, value)
-
elif type == float:
-
self.config.WriteFloat(name, value)
-
-
-
if __name__ == "__main__":
-
## class SetSelfFromDefaults(object):
-
## def __init__(self):
-
## self.test = test = DefaultValueHolder("HETAP Pro 2.00", "Database")
-
## test.SetVariables(UserName="peter", Password="pan", ServerName="MyServer", database="")
-
## test.InitFromConfig()
-
##
-
## self.SetupLoginVars(test)
-
##
-
## def SetupLoginVars(self, defaultHolder):
-
## """Cool tricks, use somewhere, someday."""
-
## for var in defaultHolder.GetVariables():
-
## execStr = "self.%s = %s" %(var['name'], (repr(var['value']), var['value'])[var['type'] != str])
-
## exec(execStr) in locals()
-
##
-
## testClass = SetSelfFromDefaults()
-
## print testClass.UserName
-
-
test = DefaultValueHolder("HETAP Pro 2.00", "Database")
-
test.SetVariables(UserName = "peter", Password = "pan", ServerName = "MyServer", database="")
-
test.InitFromConfig()
-
print test.UserName
-
test.defaultvar1=77
-
print test.GetVariables()
-
#### this also works:
-
## test.defaultUserName = "joe"
1 5680
I cleaned this up too: -
-
def GetVariables(self):
-
return [{"name":name[7:], "value":value, "type":type(value)}
-
for name, value in getmembers(self) if name.startswith('default')]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ruud de Jong |
last post by:
I have the situation where I need to construct the name
of a static method, and then retrieve the corresponding
function from a class object.
I thought I could just use __getattribute__ for this...
|
by: Daniel Schüle |
last post by:
Hello NG
is this kind of usage possible for classes
l =
l.__getattribute__("count")(1)
for example
f = foo()
def bar(t, m, p):
|
by: Gigi |
last post by:
Hi,
In the Python documentation regarding __getattribute__ (more attribute
access for new style classes) it is mentioned that if __getattribute__
is defined __getattr__ will never be called...
|
by: Sylvain Ferriol |
last post by:
hello
when i define __getattribute__ in a class, it is for the class instances
but if i want to have a __getattribute__ for class attributes
how can i do that ?
sylvain
|
by: Stefan Sonnenberg-Carstens |
last post by:
Hi there,
I'm facing some strange things - but maybe only me is strange - anyway...
i wrote the following code:
+++
class T(object):
def __init__(self,name='',port=80):
self.name=name
|
by: Barry Kelly |
last post by:
I'm running this version of Python:
Python 2.4.3 (#1, May 18 2006, 07:40:45)
on cygwin
I read in the documentation that these two expressions are
interchangeable:
...
|
by: Pedro Werneck |
last post by:
Hi all
I noticed something strange here while explaining decorators to someone.
Not any real use code, but I think it's worth mentioning.
When I access a class attribute, on a class with a...
|
by: Adam Donahue |
last post by:
As an exercise I'm attempting to write a metaclass that causes an
exception to be thrown whenever a user tries to access
'attributes' (in the traditional sense) via a direct reference.
Consider:...
|
by: bukzor |
last post by:
I want to make a MixIn class that waits to initialize its super-
classes until an attribute of the object is accessed. Not generally
useful, but desirable in my case. I've written this, and it...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |