Re: snmpmodule and making a pretend dictionary object...

Guido.van.Rossum@cwi.nl
Sun, 07 Aug 1994 20:10:18 +0200

> Well, I've got a partially working SNMP module now - you can do
> sess=snmp.open("host","public"), sess.get("system.sysDescr.0"),
> getnext and set are also supported the same way.
> What I'd like to do now is be able to do sess["system.sysDescr.0"] for
> get, and sess["system.sysDescr.0"]="blah" for set - however, while these
> two will be the mp_subscript and the mp_ass_sub methods, I'm going to
> have to hardcode the mp_length function to just return, say, 1.
>
> First question: is this going to break things (having length returning 1)?
> Or should I just set the length function to raise a TypeError?

I would raise an exception (I guess TypeError is the most applicable
one indeed). A consequence is that testing an snmp session object's
truth value (e.g. "if sess: ...") will raise an exception as well
since for mapping objects, this is done by testing that the length is
> 0. I don't see a good reason for testing a session object's truth
value however so that would be okay. (Unless your session has a
"closed" state or something similar -- buth this isn't implemented for
files etc. either.)

> It's been suggested that it would be nice to be able
> to do session.iso.mgmt.mib-2.system.sysDescr.0 (or session.system.sysDescr.0)
> to refer to things, from poking around, it looks like I could implement this
> by munging on the getattr method for the SnmpSession type.
>
> Second question: is this the right way to do this?

Well, your example already shows a weakness of this scheme: neither
"mib-2" nor "0" are valid Python identifiers, so they can't be used as
attribute names. I've a feeling that using subscription is more
appropriate. You'll also find that it's difficult to implement your
scheme, as Python does each of the lookups in turn and requres each of
them to return an object -- so you'd have to create an object for
session.iso, one for session.iso.mgmt, etc., and define their
behaviour. I don't know anything about the SNMP protocol, but I
assume that the subscript version would pass the entire string to the
server at once, right?

> Third question: is having an object that is a dictionary, but also has
> these funky methods, going to break things? (I doubt it is, but you never
> know :)

No, that's fine. Objects are never restricted to have *only* a
standard set of methods -- though in some cases they must have *at
least* a standard set.

Small nit: although the source code is currently confused about these
things, there's a difference between "dictionaries" and "mappings",
and your session object would be a mapping, but not a dictionary. A
"dictionary" is a particular built-in object which the parser knows
about and which is written as {key: value, key: value, ...}. A
"mapping" is anything that supports subscription (x[k]), subscript
assignment (x[k] = v, optional) and length (len(x)), and which is not
a sequence (sequences support slicing and have a key space which is
[0, 1, ..., len(x)-1]). Mappings (but not sequences) also
conventionally support the keys(), values() and items() methods, but
nothing in the interpreter relies on this, and it's fine not to
support them if there's no easy way to determine the total set
of key/value pairs. If you do support keys(), len(x) should be equal
to x.keys().

(BTW, can't you implement this as a Python class?)

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>