news.only-4-geeks.com Forum Index » Python | Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129 Next |
|  | IDE for Python |  | |
| | | server |  |
| Posted: Fri May 16, 2008 6:08 pm Post subject: IDE for Python |  |
| |
| | | Joshua Kugler |  |
| Posted: Fri May 16, 2008 6:08 pm Post subject: Re: IDE for Python |  |
Jonathan Barbero wrote:
| Quote: | I´m newbie with Python and to learn it better I want to use a good IDE to concentrate on Python power. There is any IDE like Eclipse for Java for Python? If not, what is the best Python´s IDE for you?
|
This is an EFAQ (extremely frequently asked question).
LINK
j |
| |
| | | Arnaud Delobelle |  |
| Posted: Fri May 16, 2008 7:02 pm Post subject: Re: namespaces and eval |  |
| |  | |
dave.g1234@gmail.com writes:
| Quote: | Sorry for the repost, didnt' quite finish
Suppose I have a function in module X that calls eval e.g,
X.py _______ Def foo(bar): Eval(bar) _______
Now eval will be called using the default eval(bar,globals(),locals()) and globals will pull in anything in module X.
Now I have module Y that calls bar like so Y.py ________ from x import * def biz(): print "Im a silly example!" Foo("biz()") _______
Python will complain that it cannot find biz because it's not the X module.
My question - is there any way to a) get a listing of active namespaes search ALL active namespaces for a particular variable
|
sys.modules gives you all the live modules.
| Quote: | b) get the namespace of the caller withing passing it in (here, Y)
|
sys._getframe(1).f_globals (resp. sys._getframe(1).f_locals) gives you the caller's globals (resp. locals).
| Quote: | c) do anything else to get biz without passing foo("biz()",locals(),globals()) or hacking biz into the __builtin__ namespace(don't know if it's even possible, but it defeats the purpose of what I'm trying to do) ?
|
Functions in Python are objects like any other, and can be passed as parameters. I.e:
x.py ---------- def foo(bar): bar()
| Quote: | from x import foo def biz(): print "Boo!" foo(biz) Boo!
|
| Quote: | I realize this is here for a reason, but I'm working on something that's kind of a hack already
Thanks dave
More gratuitous context. Long story I'm trying to write a hack for a concise way of adding arbitrary methods to objects for JPA/JQuery like chaning eg.
def foo(self): print "foo" return self;
wrap(x).foo().foo().foo().foo() etc....
|
What about a composition function instead? I.e.
compose(foo, foo, foo, foo)(x)
Here's one that I made earlier: LINK
[snip]
-- Arnaud |
| |
| | | Hans Nowak |  |
| Posted: Fri May 16, 2008 7:28 pm Post subject: Re: can't delete from a dictionary in a loop |  |
Dan Upton wrote:
| Quote: | for pid in procs_dict: if procs_dict[pid].poll() != None # do the counter updates del procs_dict[pid]
The problem:
RuntimeError: dictionary changed size during iteration
|
I don't know if the setup with the pids in a dictionary is the best way to manage a pool of processes... I'll leave it others, presumably more knowledgable, to comment on that. But I can tell you how to solve the immediate problem:
for pid in procs_dict.keys(): ...
Hope this helps!
--Hans |
| |
| | | Jan Claeys |  |
| Posted: Fri May 16, 2008 8:03 pm Post subject: Re: downloading a link with javascript in it.. |  |
Op Mon, 12 May 2008 22:06:28 +0200, schreef Diez B. Roggisch:
| Quote: | There is no way to interpret the JS in Python,
|
There is at least one way: <http://wwwsearch.sourceforge.net/python-spidermonkey/>
-- JanC |
| |
| | | Eduardo O. Padoan |  |
| Posted: Fri May 16, 2008 8:38 pm Post subject: Re: can't delete from a dictionary in a loop |  |
| |  | |
On Fri, May 16, 2008 at 6:40 PM, Gary Herron <gherron@islandtraining.com> wrote:
| Quote: | bruno.desthuilliers@gmail.com wrote:
On 16 mai, 23:28, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com> wrote:
Dan Upton wrote:
for pid in procs_dict: if procs_dict[pid].poll() != None # do the counter updates del procs_dict[pid] The problem: RuntimeError: dictionary changed size during iteration
I don't know if the setup with the pids in a dictionary is the best way to manage a pool of processes... I'll leave it others, presumably more knowledgable, to comment on that. But I can tell you how to solve the immediate problem:
for pid in procs_dict.keys():
No, keys() produces a list (which is what is wanted here). It's iterkeys() that produces an iterator which would reproduce the OP's problem.
And then, in Python3, keys() produces something else altogether (call a view of the dictionary) which would provoke the same problem, so yet another solution would have to be found then.
|
In Python 3.0, list(procs_dict.keys()) would have the same effect.
| Quote: | Gary Herron
I'm afraid this will do the same exact thing. A for loop on a dict iterates over the dict keys, so both statements are strictly equivalent from a practical POV. -- LINK
-- LINK
|
-- Eduardo de Oliveira Padoan LINK LINK Bookmarks: LINK |
| |
| | | Lie |  |
| Posted: Fri May 16, 2008 8:56 pm Post subject: Re: Scanning through Windows registry... |  |
| |  | |
On May 17, 2:06 am, Lie <Lie.1...@gmail.com> wrote:
| Quote: | On May 9, 7:36 pm, Unknown Hero <unknown_hero...@hotmail.com> wrote:> Ah, never mind, got it to work. Here's the code now. I hope I won't run into another problems later :D
code snippet #Goes through all keys and subkeys in the selected hive (defined as root) and replaces the value 'old' with the value 'new' # #IMPORTANT! You should always back up the registry before attempting to modify it. #The author of this script CANNOT BE HELD RESPONSIVE for any damage caused by running this script.
(snip)
One thing though, the disclaimer should not said the author cannot be held responsive, or you would be Windows that is not responsive all the times. I think it should say "responsible".
I'm quite confused though, the code could be made simpler by dozens using Python's high-level functionalities. Particularly the lengthy code that checked whether the string contained a substring and replace the substring with new could be done easily (and faster) using python's in and replace function. I'll post the code later when I finished checking places where the codes could be simplified, and I'll also polish the entry code and a few other things (and pythonify the code according to the PEP 8's guide lines).
|
The tidied code ++
''' Goes through all keys and subkeys in the selected hive (defined as root) and replaces the value 'old' with the value 'new'
IMPORTANT! You should always back up the registry before attempting to modify it. The author of this script CANNOT BE HELD RESPONSIBLE for any damage caused by running this script.
You can call the script from a command line and pass two or three values: HIVE, OLD, and NEW.
OLD and NEW can be any string value HIVE has to be one of the following:
HKEY_LOCAL_MACHINE / HKLM HKEY_CURRENT_USER / HKCU HKEY_CLASSES_ROOT / HKCR HKEY_USERS / HKU HKEY_CURRENT_CONFIG / HKCC
If NEW is not specified, values that matches OLD will be replaced with empty string (i.e. deleted) '''
import _winreg import sys
HIVES = { "HKEY_LOCAL_MACHINE" : _winreg.HKEY_LOCAL_MACHINE, "HKEY_CURRENT_USER" : _winreg.HKEY_CURRENT_USER, "HKEY_CLASSES_ROOT" : _winreg.HKEY_CLASSES_ROOT, "HKEY_USERS" : _winreg.HKEY_USERS, "HKEY_CURRENT_CONFIG" : _winreg.HKEY_CURRENT_CONFIG, }
AccessError = False
class RegKey(object): def __init__ (self, name, key): self.name = name self.key = key
def __str__ (self): return self.name
def walk(top): """ Walk through each key, subkey, and values
Walk the registry starting from the key top in the form HIVE\\key\\subkey\\..\\subkey and generating key, subkey_names, values at each level.
key is a lightly wrapped registry key, including the name and the HKEY object. subkey_names are simply names of the subkeys of that key values are 3-tuples containing (name, data, data-type). See the documentation for _winreg.EnumValue for more details. """
try: if "\\" not in top: top += "\\" root, subkey = top.split ("\\", 1) key = _winreg.OpenKey(HIVES[root], subkey, 0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE)
subkeys = [] i = 0 while True: try: subkeys.append(_winreg.EnumKey(key, i)) i += 1 except EnvironmentError: break
values = [] i = 0 while True: try: values.append(_winreg.EnumValue(key, i)) i += 1 except EnvironmentError: break
yield RegKey(top, key), subkeys, values
for subkey in subkeys: for result in walk(top + "\\" + subkey): yield result
except WindowsError: global AccessError AccessError = True
def main(start, startHandle, old, new): basickeys = [] i = 0 while True: try: basickeys.append(_winreg.EnumKey(startHandle, i)) i += 1 except EnvironmentError: break
for x in basickeys: for key, subkey_names, values in walk(start + "\\" + x): for (name, data, type) in values: if type == _winreg.REG_SZ: if old in data: winreg.SetValueEx(key.key, name, 0, type, data.replace(old, new)) print key.key, name, 0, type, data.replace(old, new)
def help(): #Show help print ''' USAGE: Registry.py HIVE OLD [NEW] HIVE: The root key in registry you want to go through. HKEY_CLASSES_ROOT / HKCR HKEY_CURRENT_USER / HKCU HKEY_LOCAL_MACHINE / HKLM HKEY_USERS / HKU HKEY_CURRENT_CONFIG / HKCC
OLD: The value to search for. Wrap multiword strings with "".
NEW: The value which will replace OLD. Wrap multiword strings with "". If not supplied, it default to empty string which means delete all occurence of OLD in the registry.
''' if __name__ == '__main__': if len(sys.argv) < 3 or len(sys.argv) > 4: print 'Invalid Number of Arguments' help() exit()
## Root Hive try: start = { 'HKCU' : 'HKEY_CURRENT_USER', 'HKLM' : 'HKEY_LOCAL_MACHINE', 'HKCR' : 'HKEY_CLASSES_ROOT', 'HKU' : 'HKEY_USERS', 'HKCC' : 'HKEY_CURRENT_CONFIG', }[sys.argv[1].upper()] except KeyError: start = sys.argv[1].upper() try:
startHandle = { 'HKEY_CURRENT_USER' : _winreg.HKEY_CURRENT_USER, 'HKEY_LOCAL_MACHINE' : _winreg.HKEY_LOCAL_MACHINE, 'HKEY_CLASSES_ROOT' : _winreg.HKEY_CLASSES_ROOT, 'HKEY_USERS' : _winreg.HKEY_USERS, 'HKEY_CURRENT_CONFIG' : _winreg.HKEY_CURRENT_CONFIG, }[start] except KeyError: print >> sys.stderr, 'Invalid Hive' help() exit()
## The substring to be replaced old = sys.argv[2]
## The replacement string try: new = sys.argv[3] except: new = '' main(start, startHandle, old, new) if AccessError: print ''' Some keys cannot be changed because you don't have the appropriate permission to modify those keys, please try again with a suitable user account. ''' |
| |
| | | bruno.desthuilliers@gmail |  |
| Posted: Fri May 16, 2008 9:08 pm Post subject: Re: namespaces and eval |  |
| |  | |
On 16 mai, 20:44, dave.g1...@gmail.com wrote:
| Quote: | Suppose I have a function in module X that calls eval e.g,
X.py _______ Def foo(bar): Eval(bar) _______
Now eval will be called using the default eval(bar,globals(),locals()) and globals will pull in anything in module X.
Now I have module Y that calls bar like so Y.py ________ from x import * def biz(): print "Im a silly example!" Foo("biz()") _______
Python will complain that it cannot find biz because it's not the X module.
|
I'm afraid you're using the wrong tool here. Just try this:
# module x def foo(bar): bar()
# module y from x import foo def baaz(): print "in y.baaz"
foo(baaz)
Remember that in Python, everything (at least, everything you can bind to a nme) is an object, including functions, classes and modules. And that every name lives in a namespace, that can be inspected somehow (using globals() and locals(), but also using getattr() or some function from the inspect module). IOW, when you think that eval() (or exec()) is what you need, odds are there's a way better solution. FWIW, I must have used exec twice and eval thrice in 7+ years of Python programming - and believe me, I'm not afraid of wizardry and black magic. |
| |
| | | jay graves |  |
| Posted: Fri May 16, 2008 9:21 pm Post subject: Re: save dictionary for later use? |  |
| |  | |
On May 16, 3:24 pm, globalrev <skanem...@yahoo.se> wrote:
| Quote: | On 16 Maj, 21:22, jay graves <jaywgra...@gmail.com> wrote: On May 16, 2:17 pm, globalrev <skanem...@yahoo.se> wrote: i extract info from one file and put it into a dictionary. i want to save that dictionary for later use, how do i do that? might save a list of dictionaries or a list of classobjects too if there is any difference. use the 'pickle' module.http://docs.python.org/lib/module-pickle.html
pickle.dumps(mg) pickle.load(mg)
'dict' object has no attribute 'readline' dumps load(well i dont know but i get no complaint but running load generates that error)
|
It's best to post a minimal set of code that exhibits your error. You aren't saving the output of pickle.dumps and you are using pickle.load instead of pickle.loads.
Sample loading to and from a string which you can tuck away in a file.
| Quote: | import pickle test = {'a':1,'b':2} picklestr = pickle.dumps(test) test2 = pickle.loads(picklestr) test == test2 True
|
Sample using an open file.
| Quote: | import pickle test = {'a':1,'b':2} pfile = open('pickletest','wb') pickle.dump(test,pfile) pfile.close() pfile = open('pickletest','rb') test2 = pickle.load(pfile) pfile.close() test == test2 True
|
.... Jay Graves |
| |
| | | Guest |  |
| Posted: Fri May 16, 2008 9:23 pm Post subject: Re: namespaces and eval |  |
Thanks for the responses. I'm well aware that the function can be passed in the parameters, passing in the functino as an arg defeats the purpose of what I'm going after.
@ Arnaud - Nice. I'm not sure what the performance of mine vs. yours, but a perfunctory glance looks like we're doing the close to the same thing. Maybe one advanage to doing it wrap(x).foo().... is that you can pass in other parameters to foo. Of course, getting the callers locals/globals is bad in of itself.
best, dave |
| |
| Page 1 of 129 .:. Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129 Next | |
|
|