Code: Select all
d = {'key1': 1, 'key2': 56}
Moderator: hines
Code: Select all
d = {'key1': 1, 'key2': 56}
Code: Select all
begintemplate String
public s
strdef s
endtemplate String
begintemplate ObjDict
public get, put, setKeys
objref keyList, valueList, nil
proc init() {
keyList = new List()
valueList = new List()
}
proc put() {localobj key
key = new String()
key.s = $s1
keyList.append(key)
valueList.append($o2)
}
obfunc get() {local i localobj key
for i=0, keyList.count()-1 {
if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
return valueList.o(i)
}
}
return nil
}
endtemplate ObjDict
begintemplate NumDict
public get, put, setKeys
objref keyList, valueList
proc init() {
keyList = new List()
valueList = new Vector()
}
proc put() {localobj key
key = new String()
key.s = $s1
keyList.append(key)
valueList.append($2)
}
func get() {local i localobj key
for i=0, keyList.count()-1 {
if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
return valueList.get(i)
}
}
}
endtemplate NumDict
objref od, nd, vv, vvv
od = new ObjDict()
vv = new Vector()
vv.insrt(0,6,3,4)
od.put("hello", vv)
vvv = od.get("hello")
print vvv.size()
nd = new NumDict()
nd.put("hello", 5)
print nd.get("hello")
Code: Select all
oc>objref py, d
oc>py = new PythonObject()
oc>d = py.dict()
oc>d.__setitem__("axon", 100)
NULLobject
oc>d.__setitem__("soma", 10)
NULLobject
oc>d.get("soma")
10
oc>d.get("axon")
100
This looks like the way to go--avoids trying to use hoc to replicate existing Python functionality.As long as Python is installed, you can use Python dictionaries from inside HOC