Opened 6 years ago

Closed 5 years ago

#4671 enhancement closed fixed (fixed)

sometimes I want to have some constant values that are part of a set

Reported by: glyph Owned by: exarkun
Priority: low Milestone:
Component: core Keywords:
Cc: Branch: branches/constants-4671
branch-diff, diff-cov, branch-cov, buildbot
Author: exarkun


Twisted contains several piles of named constants that represent interesting constant values that belong to a set. Some examples include: HTTP methods, HTTP response codes, SMTP response codes, OSCAR SNAC codes, AMP special keys (ASK, ANSWER, COMMAND), IRC RPL codes, sftp file flags, and telnet negotiation options.

These are all represented in idiosyncratic and incompatible ways. Many of them are just integers bound to names. Many of them need some kind of bi-directional mapping between a string and an integer, or some associated descriptive text. In some cases we automate that mapping construction, in others we do it by hand.

I'd like to have a single idiom for representing this type of value, one which repr()s to the FQPN of the code's definition (or something similar) so it's easy to look up when debugging, but also contains the information relevant to the protocol (the numeric code, the bytes to send, etc) as a documented attribute.

For some of these, for example telnet negotiation options and conch's FXF_* flags, it may also be helpful to define some kind of "bitwise" operations, so that "FXF_WRITE | FXF_CREAT | FXF_TRUNC" actually looks like something meaningful, and not just '26', when printed. Of course, there should also be an easy way to get 26 (or should I say, 0x1A) out of that combined value as well as an individual one. For example, if FXF_WRITE.asInt() → 2, then (FXF_WRITE | FXF_CREAT | FXF_TRUNC).asInt() → 26. (And no, we don't necessarily need to use '|' and '&' but it may make sense to do so.)

Some other useful functionality: there should be an object representing the total set of statuses or codes or flags, so that it's easy to ask, "is this a value I understand", or, "I have an integer / some bytes / a byte, please give me the associated status object for that value".

This ticket is to implement the library which these different use-cases would use. It is probably not a good idea to try to do this in isolation, since it would be hard to get right without lots of specific reference to use-cases, but rather, to take the opportunity to implement this when some maintenance on such codes comes up.

Change History (6)

comment:1 Changed 6 years ago by glyph

Barry apparently wrote something in this vein, we should probably look at it: flufl.enum].

Or perhaps he could be convinced to relicense more permissively.

comment:2 Changed 6 years ago by <automation>

  • Owner glyph deleted

comment:3 Changed 5 years ago by exarkun

  • Owner set to exarkun
  • Status changed from new to assigned

comment:4 Changed 5 years ago by exarkun

  • Author set to exarkun
  • Branch set to branches/constants-4671

(In [32533]) Branching to 'constants-4671'

comment:5 Changed 5 years ago by exarkun

comment:6 Changed 5 years ago by exarkun

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [33677]) Merge flag-constants-5384

Author: exarkun Reviewer: therve Fixes: #5384, #4671

Add bitvector-like constant support to twisted.python.constants.

Note: See TracTickets for help on using tickets.