summaryrefslogtreecommitdiff
path: root/doc/maintref.txt
blob: 43289f4007964f177ebd7c64147b2a11bcd78958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
********************
Maintainer Reference
********************

The `API <api/index.html>`_
***************************

The `API <api/index.html>`_ for PyXB was generated using `Epydoc
<http://epydoc.sourceforge.net/>`_.  Most of the public API is commented,
though not all docstrings have been updated to use Epydoc syntax.

Coding Practices
****************

The practices described herein are *intended* to be used, but evolved over
time, and not all are followed in every situation.

Style
=====

PyXB follows generally uses a coding style consistent with those described
in the Python coding standard in :PEP:`8`.  Specific exceptions are listed
below.

Whitespace
----------

The pet peeves of PyXB's maintainer differ from the `BDFL
<http://en.wikipedia.org/wiki/Guido_van_Rossum>`_'s when it comes to
whitespace.  Spaces are used around operators to reveal precedence
assumptions, and sometimes around parenthesis to distinguish tuple value
boundaries::

    hypot2 = x*x + y*y
    pair_of_pairs = ( (a, b), (b, c) )

Definitions of methods and classes always use a space before the
parenthesis; invocations never do:

::

    def declaration (self, recurse=False):
       if recurse:
           self.declaration(False)

.. _identifier_naming:

Naming
------

PyXB heavily uses a single leading underscore as an indication of
protected/friend status, as suggested in :PEP:`8`.  Wherever possible,
double leading underscores are used to hide class member fields, restricting
access to them through protected or public accessor methods.

Class members that are specifically *class* (as opposed to *instance*)
members begin with a capital letter.

An underscore is used to separate the descriptive class or function name
from a suffix that indicates its use.  Standard suffixes are:

- ``mixin`` : A class that does not stand alone, but is a superclass of one
  or more classes

- ``csc`` : A method that uses :ref:`cooperative super calling<coding_csc>`

- ``vx``, ``vb`` : An indication that the method is expected to be
  overridden in a subclass.


Exceptions
==========

PyXB provides a `standard exception hierarchy
<api/pyxb.exceptions_-module.html>`_ that extends the one built into Python.

Where an execution branch has been identified that requires behavior that
has not yet been implemented, raise an :api:`pyxb.exceptions_.IncompleteImplementationError`.

Where the system detects that a precondition is not satisfied, processing
must stop.  If the precondition failure is due to improper use of the PyXB
internal or public API, a :api:`pyxb.exceptions_.LogicError` should be
raised.  If the precondition failure is due to invalid input from the
schema, a :api:`pyxb.exceptions_.SchemaValidationError` should be raised.

If the precondition is inexplicably false, Python's ``assert`` functionality
may be used.  Use of ``assert`` should be rare, and only in places that are
guaranteed to be exercised during the course of testing.

The exception behavior of methods SHOULD be documented.  Documentation of
asserts is not required.

Annotations
===========

Use decorators (:PEP:`318`) to mark class methods.  Note that this restricts
us to Python 2.4 or later.  Sigh with disappointment and move on.

Documentation
=============

Use the `Epytext Markup Language
<http://epydoc.sourceforge.net/manual-epytext.html>`_ for all public and
implementation-shared methods and classes.  (Formerly, this was "Use
docstrings :PEP:`257`".  Documentation in only a few modules has been
converted.)

Comments
========

Use comments copiously.  Do not duplicate detailed information from
standards, but do include pertinent summaries and a reference to the section
in which the details can be found.  The casual reader should not be forced
to open the standard to figure out what the coder intended to accomplish.

Terminology
===========

The term "attribute" has different meanings in Python and XML.

tag : Refers to the text that opens an XML element

instance : [as an adjective] Refers to a characteristic of an instance of a
Python class.

class : [as an adjective] Refers to a characteristic of a Python class
itself, shared among all instances.

field : Refers to a named attribute of a Python object (class).  When the
attribute holds a value, it is an "instance (class) variable" or "instance
(class) field".  When it holds a reference to an invokable object, it is an
"instance (class) method".

Use of new-style classes
========================

Too many things, such as clean hooking into the pickling system, require the
use of `new-style classes
<http://www.geocities.com/foetsch/python/new_style_classes.htm>`_.
Namespaces, schema components, and types (simple and complex) all use
new-style classes.

For this to work properly, if you implement an ``__init__`` method, it must take
arbitrary args and keywords, invoke ``super(Class, self).__init__(*args,**kw)``,
and extract any arguments it needs from the keywords.  If you do not need to
do anything in the init method, leave it out.  See
`this commentary <http://fuhm.net/super-harmful/>`_ for a detailed
description of the intricacies of ``super``.

Inheritance
===========

.. _mixins:

Mix-in classes
--------------

PyXB makes heavy use of multiple inheritance through `mix-in classes
<http://en.wikipedia.org/wiki/Mixin>`_.  If there are constraints on where
the mix-in must appear in the method resolution order (mro), note that
clearly in the mix-in documentation.


.. _coding_csc:

Invoking Superclass Instances
-----------------------------

`Cooperative super calling
<http://www.geocities.com/foetsch/python/new_style_classes.htm#super>`_ is a
pattern where a class may inherit multiple implementations of the same
method, and want to call all of them.  Normally this is done by invoking the
parent class implementation before or after the subclass implementation.  In
non-trivial inheritance hierarchies (as result from using many mix-ins),
it's not obvious who the next parent to call is, so the Python ``super``
function is used.  However, at some point a class will be reached that has
no more definitions for the called method, and attempting to invoke one
would produce an ``AttributeError``.

Use the following idiom to conditionally invoke superclass methods when you
are not sure the superclass has such a method.

::

    def method_csc (self, *args, **kw):
        super_fn = getattr(super(ThisClass, self), 'method_csc', lambda *a,**kw: self)
        return super_fn(*args, **kw)

Note the use of the ``_csc`` suffix to highlight that this method uses
cooperative super calling.

Class Variables At Multiple Levels
----------------------------------

There are several cases where we want to store information in a class, but
allow subclasses (not instances) to override it.  An example is in the
:api:`pyxb.binding.basis.simpleTypeDefinition` hierarchy where each class
maintains a set of :api:`pyxb.binding.facets.ConstrainingFacet` instances
that are available for constraining values of the class.  In many cases, a
subclass will not change the set of facets that affect instances, so we want
to be able to inherit the parent class map; but in other cases we may need
to add constraints that only affect the new class and its descendents.

This sort of thing is supported by implementing a private class method in
the base class which combines the dynamically-determined actual class name
with a constant identifier and uses ``getattr`` and ``setattr`` to access
the class-specific value.

Type declarations may extend a type of the same name in a different
namespace.  If they do so, their binding classes will likely have the same
name, but in different modules, while also inheriting (in exactly the
situation where we want different values).  For this reason, the constructed
attribute name should also incorporate the module or namespace, something
normally not done with the double-underscore feature of Python.