[Zope-dev] Constant values defined in interfaces

Chris Rossi chris at archimedeanco.com
Fri Apr 3 12:35:01 EDT 2009


Hello,

I was wondering if the Zope collective had given any consideration to
allowing constants to be defined in interfaces.  To be clear, these are
constant values that make up the protocol defined by the interface.  Just to
have a concrete example, let's say we're modeling an http response:

class IHttpResponse(Interface):
    """Models an HTTP 1.1 response.
    """
    status = Attribute("HTTP status code for this response.")

It might be useful to include in our interface spec what some proper values
for status code might be and make them available to applications as static
constants on the interface class.  A naive implementer might do something
like this:

class IHttpResponse(Interface):
    """Models an HTTP 1.1 response.
    """
    HTTP_OK = "200 Ok"
    HTTP_NOT_FOUND = "404 Not Found"

    status = Attribute("HTTP status code for this response.")

As you can see, the HTTP_OK and HTTP_NOT_FOUND constants are conceptually
part of the interface--they help define our contract for http responses.  We
might expect to then be able to do something like this in application code:

response.status = IHttpResponse.HTTP_OK

Of course, if you try this currently, Interface will complain:

InvalidInterface: Concrete attribute, HTTP_OK

I did do some poking around in the source code and in the documentation,
such as it is, and didn't see anything like constants in interfaces, but
it's possible this use case has already been addressed somehow and I am just
ignorant of how, in which case, my apologies, and thanks for telling me the
already accepted way to do this.

If this hasn't been done yet, I can envision doing something like:

from zope.interface import Constant

class IHttpResponse(Interface):
    """Models an HTTP 1.1 response.
    """
    HTTP_OK = Constant("200 Ok", "An HTTP Ok response.")
    HTTP_NOT_FOUND = Constant("404 Not Found", "An HTTP Not Found response")

    status = Attribute("HTTP status code for this response.")

Using descriptors, the results could be both static and immutable.

Does this seem useful to anyone besides me?  Anyone who's done much Java
programming will recognize that I did not have an original idea here.

If there is a general buy in, I would be happy to attempt an implementation
and submit a patch.

Thanks,
Chris

PS I did find a hack that will let you accomplish this, although it is a
hack rather than a supported feature of the component architecture:

class IHttpResponse(Interface):
    """Models an HTTP 1.1 response.
    """
    status = Attribute("HTTP status code for this response.")

IHttpResponse.HTTP_OK = "200 Ok"
IHttpResponse.HTTP_NOT_FOUND = "404 Not Found"

Because the interface class is decorated after the type is instantiated, the
InvalidInterface check is avoided.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope-dev/attachments/20090403/7c81f622/attachment.html 


More information about the Zope-Dev mailing list