def get(name,return_type): """Return a variable from hoc. name can be a hoc variable (int, float, string) or a function/method that returns such a variable. """ fmt_dict = {'int' : '%d', 'float' : '%g', 'string' : '\\"%s\\"'} hoc_commands = ['strdef cmd', '{sprint(cmd,"HocToPy.hocvar = %s",%s)}' % (fmt_dict[return_type],name), '{nrnpython(cmd)}'] for cmd in hoc_commands: hoc.execute(cmd) return HocToPy.hocvar
def hoc_execute(hoc_commands, comment=None): assert isinstance(hoc_commands,list) if comment: logging.debug(comment) for cmd in hoc_commands: logging.debug(cmd) success = hoc.execute(cmd) if not success: raise HocError('Error produced by hoc command "%s"' % cmd)
def get(name,return_type): """Return a variable from hoc. name can be a hoc variable (int, float, string) or a function/method that returns such a variable. """ fmt_dict = {'int' : '%d', 'float' : '%g', 'string' : '\\"%s\\"'} hoc_commands = ['strdef cmd', '{sprint(cmd,"HocToPy.hocvar = %s",%s)}' % (fmt_dict[return_type],name), '{nrnpython(cmd)}'] for cmd in hoc_commands: hoc.execute(cmd) return HocToPy.hocvar get = staticmethod(get) hoc.execute("a = 2") hoc.execute("pi = 3.14159") hoc.execute("strdef hello") hoc.execute('hello = "Hello World"') a = HocToPy.get('a','int') pi = HocToPy.get('pi','float') hello = HocToPy.get('hello','string') print("a = ", a) print("pi = ", pi) print(hello) hoc.execute('create soma') hoc.execute('{finitialize(-70)}') v = HocToPy.get('soma.v(.5)', 'float')
import hoc hoc.execute("hoc_ac_ = PI") pi = hoc.hoc_ac() print(pi)
from hoc import execute execute('load_file("nrngui.hoc")') secnames = [] execute(''' create soma, axon, dend[3] strdef str forall { \ sprint(str, "secnames.append('%s')", secname()) \ nrnpython(str) \ } ''') print(secnames)
import hoc hoc.execute('print "scalar get tests"') h = hoc.HocObject() print(h) h('print "goodbye"') h('x = 2') print(h.x) h('strdef str') h('str = "test hoc string"') print(h.str) h('objref obj') h('obj = new Vector(10)') a = h.obj print(a) h('print "scalar set tests"') h.x = 5 print(h.x) h.str = 'from python' print(h.str) h('objref o2') h('o2 = new List()') h.obj = h.o2 print(h.obj) h('print "function tests"') f = h.printf