generate dictionary of hoc (h.) things

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
wwlytton
Posts: 65
Joined: Wed May 18, 2005 10:37 pm
Contact:

generate dictionary of hoc (h.) things

Post by wwlytton » Fri Nov 21, 2014 5:00 pm

I wanted a dictionary of hoc stuff for reasons that in retrospect was unnecessary, but having done this I thought I would share it

hdict = dict([(name,h.__getattribute__(name)) for name in dir(h) if hasattr(h,name)])

the hasattr() turned out to be the trick since for reasons obscure some things turn up in the dir() that are not accessible -- thanks to robert mcdougal for showing me that

note that this is not a dynamic dict so has to be regenerated after loading something

hdict['tstop'] # not found
h.load_file("stdrun.hoc")
hdict = dict([(name,h.__getattribute__(name)) for name in dir(h) if hasattr(h,name)])
hdict['tstop'] # 5.0

wwlytton
Posts: 65
Joined: Wed May 18, 2005 10:37 pm
Contact:

Re: generate dictionary of hoc (h.) things

Post by wwlytton » Fri Nov 21, 2014 5:09 pm

in python 2.7+ can also do dict comprehensions so

hdict = {name:h.__getattribute__(name) for name in dir(h) if hasattr(h,name)}

Post Reply