|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|
||||
I've ask in the past on Python list if you can have Variable Variables in Python like PHP and such, and the only answers I ever got were work arounds so I don't think you can do what you want.
I can't see why you'd want to pass a string in this case.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick |
|
||||
Quote:
I can use if reg == 2147483650: str = 'HKEY_LOCAL_MACHINE' But does this work on every system config, and on every Windows version (XP, Vista, 2003, 2008)? Other than that, it is _ugly_, passing two values (str and reg.HKLM/HKCU) would be better...
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
Ok, I see. Trying to write code to use across windows version now there is a headache. Personally I try and stay away from the registry. And wasn't Microsoft suppose to be moving away from the registry with Vista on. Or did that not happen?
Anyway, I'd probably write two function StringFromRegKey and RegKeyFromString which would basically be your if statement. If the registry key values are imported from a MS dll then the will probably stay the same across version. They are pretty good about not changing the lower level stuff like that.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick |
|
||||
Well, there might be a better way Carpetsmoker but this comes to mind:
since you obviously have access to the winreg object within the function, and just need to know the key and other arguments: why not pass the key as a string and use eval() to access the corresponding attribute/method through it. Since it appears to be the data structure you want to use to access it by, lol. example: Code:
>>> class Klass: ... name = "I am called Klass" ... >>> k=Klass() >>> def test(regkey, key, value): ... print( eval("k.%s" % str(regkey)) ) ... >>> test('name', 'and', 'others') I am called Klass >>>
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. Last edited by TerryP; 31st March 2009 at 10:27 PM. |
|
||||
Using eval() might be an option ... I'll check it out at work tomorrow.
Quote:
Quote:
Quote:
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
Quote:
The Qt C++ toolkit (and possibly WxWidgets) make things much less painful but all the fun stuff still requires the systems API or a library set to aggravate linking and distribution. Python is by far the least irksome language to do things with on Windows, that is also suitable for cross platform development. (Python == VERY good for writing portable code).
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
||||
It might not be the microsoft way, but I write to ini/config files. It's saved me a lot of headaches over the years. I try and use BSD type methods as much as I can while doing windows programming. I'm goof that way
@TerryP I like the eval, I keep that in mind from now on.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick |
|
||||
Quote:
But I want to change system settings, such as IE Proxy, explorer settings, automatic updates, resolution, etc.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
Oh, well guess your stuck with the registry then. Application I write for windows don't have anything to do with that sort of stuff, thankfully.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick |
|
||||
eval() works pretty good:
Code:
import _winreg as winreg SetRegistry(winreg.HKEY_LOCAL_MACHINE, key, values) def SetRegistry(reg, key, values): regobj = eval('winreg.' + reg) reghandle = winreg.ConnectRegistry(None, regobj)
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
If the language doesn't have an eval-like method, it better be able to automatically compile and load libraries!!!
It's like one of the most useful features a language can have, whether or not you use it a lot.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
|||
The problem with "eval" is that it is not secure at all. If someone or something else gets to the registry, they can insert code into a key which your code may read and execute, code which may darn well be malicious.
For example the following file "foo" could just as easily be the C:\ntldr: Code:
eval("open('foo','w').write('p0wned!')") Code:
literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. Code:
>>> import ast >>> ast.literal_eval("open('foo','w').write('p0wned!')") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.1/ast.py", line 80, in literal_eval return _convert(node_or_string) File "/usr/local/lib/python3.1/ast.py", line 79, in _convert raise ValueError('malformed string') ValueError: malformed string (And yes, I'm using an unreleased version of Python. Not advisable for general use. I'm working on moving all our code to Python 3.x, generally liking it.) Last edited by mwatkins; 5th April 2009 at 05:39 PM. Reason: Added example |
|
||||
Hmm, haven't heard about ast.literal_eval() yet, thanks. I'm most used to the restricted eval (reval) in Safe.pm - and advise a .40 S&W to the cranium before doing an [r]?eval "" on user supplied code in any language.
eval is nicer for internal trickery.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
apache with python | deadeyes | FreeBSD Ports and Packages | 1 | 27th August 2009 01:31 PM |
CSH 'mch' variable? | JMJ_coder | General software and network | 1 | 12th February 2009 11:24 PM |
wip/kdebindings4-python errors | Cloud | NetBSD Package System (pkgsrc) | 7 | 17th January 2009 06:52 PM |
py-yaml - Python 2.5 and 2.6 | iGloo | FreeBSD Ports and Packages | 0 | 20th December 2008 08:36 PM |
Using Date variable? | cwhitmore | FreeBSD General | 9 | 13th August 2008 07:16 AM |