lots of docstring updates, for better epydoc html.

This commit is contained in:
Arjan Molenaar 2007-07-31 08:10:06 +00:00
parent 46c9b5cc5c
commit 581f860132
7 changed files with 258 additions and 232 deletions

View File

@ -9,7 +9,7 @@ verbosity: 1
docformat: restructuredtext
parse: yes
introspect: yes
#exclude: .*\.tests.*
exclude: .*\.tests.*
inheritance: listed
private: no
imports: no
@ -21,5 +21,5 @@ output: html
#css: doc/style/epydoc.css
top: gaphas
frames: no
sourcecode: no
sourcecode: yes

View File

@ -19,14 +19,14 @@ class Context(object):
"""
Context used for updating and drawing items in a drawing canvas.
>>> c=Context(one=1,two='two')
>>> c.one
1
>>> c.two
'two'
>>> try: c.one = 2
... except: 'got exc'
'got exc'
>>> c=Context(one=1,two='two')
>>> c.one
1
>>> c.two
'two'
>>> try: c.one = 2
... except: 'got exc'
'got exc'
"""
def __init__(self, **kwargs):
self.__dict__.update(**kwargs)
@ -38,11 +38,6 @@ class Context(object):
class Canvas(object):
"""
Container class for items.
Attributes:
- projector: canvas constraint projector between item and canvas
coordinates
- sorter: items sorter in order used to add items to canvas
"""
def __init__(self):
@ -62,16 +57,16 @@ class Canvas(object):
@observed
def add(self, item, parent=None):
"""
Add an item to the canvas
Add an item to the canvas.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> len(c._tree.nodes)
1
>>> i._canvas is c
True
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> len(c._tree.nodes)
1
>>> i._canvas is c
True
"""
assert item not in self._tree.nodes, 'Adding already added node %s' % item
item.canvas = self
@ -99,17 +94,16 @@ class Canvas(object):
def remove(self, item):
"""
Remove item from the canvas
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.remove(i)
>>> c._tree.nodes
[]
>>> i._canvas
Remove item from the canvas.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.remove(i)
>>> c._tree.nodes
[]
>>> i._canvas
"""
for child in reversed(self.get_children(item)):
self.remove(child)
@ -138,16 +132,16 @@ class Canvas(object):
def get_all_items(self):
"""
Get a list of all items
>>> c = Canvas()
>>> c.get_all_items()
[]
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.get_all_items() # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
Get a list of all items.
>>> c = Canvas()
>>> c.get_all_items()
[]
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.get_all_items() # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
"""
return self._tree.nodes
@ -155,16 +149,16 @@ class Canvas(object):
"""
Return the root items of the canvas.
>>> c = Canvas()
>>> c.get_all_items()
[]
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> c.get_root_items() # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> c = Canvas()
>>> c.get_all_items()
[]
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> c.get_root_items() # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
"""
return self._tree.get_children(None)
@ -177,103 +171,107 @@ class Canvas(object):
def get_parent(self, item):
"""
See tree.Tree.get_parent()
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> c.get_parent(i)
>>> c.get_parent(ii) # doctest: +ELLIPSIS
<gaphas.item.Item ...>
See `tree.Tree.get_parent()`.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> c.get_parent(i)
>>> c.get_parent(ii) # doctest: +ELLIPSIS
<gaphas.item.Item ...>
"""
return self._tree.get_parent(item)
def get_ancestors(self, item):
"""
See tree.Tree.get_ancestors()
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_ancestors(i))
[]
>>> list(c.get_ancestors(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_ancestors(iii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>, <gaphas.item.Item ...>]
See `tree.Tree.get_ancestors()`.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_ancestors(i))
[]
>>> list(c.get_ancestors(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_ancestors(iii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>, <gaphas.item.Item ...>]
"""
return self._tree.get_ancestors(item)
def get_children(self, item):
"""
See tree.Tree.get_children()
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_children(iii))
[]
>>> list(c.get_children(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_children(i)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
See `tree.Tree.get_children()`.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_children(iii))
[]
>>> list(c.get_children(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_children(i)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
"""
return self._tree.get_children(item)
def get_all_children(self, item):
"""
See tree.Tree.get_all_children()
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_all_children(iii))
[]
>>> list(c.get_all_children(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_all_children(i)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>, <gaphas.item.Item ...>]
See `tree.Tree.get_all_children()`.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> iii = item.Item()
>>> c.add(iii, ii)
>>> list(c.get_all_children(iii))
[]
>>> list(c.get_all_children(ii)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>]
>>> list(c.get_all_children(i)) # doctest: +ELLIPSIS
[<gaphas.item.Item ...>, <gaphas.item.Item ...>]
"""
return self._tree.get_all_children(item)
def get_connected_items(self, item):
"""
Return a set of items that are connected to @item.
Return a set of items that are connected to ``item``.
The list contains tuples (item, handle). As a result an item may be
in the list more than once (depending on the number of handles that
are connected). If @item is connected to itself it will also appear
are connected). If ``item`` is connected to itself it will also appear
in the list.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Line()
>>> c.add(i)
>>> ii = item.Line()
>>> c.add(ii)
>>> iii = item.Line()
>>> c.add (iii)
>>> i.handles()[0].connected_to = ii
>>> list(c.get_connected_items(i))
[]
>>> ii.handles()[0].connected_to = iii
>>> list(c.get_connected_items(ii)) # doctest: +ELLIPSIS
[(<gaphas.item.Line ...>, <Handle object on (0, 0)>)]
>>> list(c.get_connected_items(iii)) # doctest: +ELLIPSIS
[(<gaphas.item.Line ...>, <Handle object on (0, 0)>)]
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Line()
>>> c.add(i)
>>> ii = item.Line()
>>> c.add(ii)
>>> iii = item.Line()
>>> c.add (iii)
>>> i.handles()[0].connected_to = ii
>>> list(c.get_connected_items(i))
[]
>>> ii.handles()[0].connected_to = iii
>>> list(c.get_connected_items(ii)) # doctest: +ELLIPSIS
[(<gaphas.item.Line ...>, <Handle object on (0, 0)>)]
>>> list(c.get_connected_items(iii)) # doctest: +ELLIPSIS
[(<gaphas.item.Line ...>, <Handle object on (0, 0)>)]
"""
connected_items = set()
for i in self.get_all_items():
@ -284,13 +282,15 @@ class Canvas(object):
def get_matrix_i2c(self, item, calculate=False):
"""
Get the Item to Canvas matrix for @item.
Get the Item to Canvas matrix for ``item``.
item: The item who's item-to-canvas transformation matrix should be
found
calculate: True will allow this function to actually calculate it,
in stead of raising an AttributeError when no matrix is present
yet. Note that out-of-date matrices are not recalculated.
item:
The item who's item-to-canvas transformation matrix should be
found
calculate:
True will allow this function to actually calculate it,
in stead of raising an AttributeError when no matrix is present
yet. Note that out-of-date matrices are not recalculated.
"""
if item._matrix_i2c is None or calculate:
self.update_matrix(item)
@ -299,8 +299,8 @@ class Canvas(object):
def get_matrix_c2i(self, item, calculate=False):
"""
Get the Canvas to Item matrix for @item.
See get_matrix_i2c().
Get the Canvas to Item matrix for ``item``.
See `get_matrix_i2c()`.
"""
if item._matrix_c2i is None or calculate:
self.update_matrix(item)
@ -312,17 +312,17 @@ class Canvas(object):
"""
Set an update request for the item.
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> ii = item.Item()
>>> c.add(i)
>>> c.add(ii, i)
>>> len(c._dirty_items)
0
>>> c.update_now()
>>> len(c._dirty_items)
0
>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> ii = item.Item()
>>> c.add(i)
>>> c.add(ii, i)
>>> len(c._dirty_items)
0
>>> c.update_now()
>>> len(c._dirty_items)
0
"""
if update:
self._dirty_items.add(item)
@ -341,19 +341,19 @@ class Canvas(object):
def require_update(self):
"""
Returns True or False depending on if an update is needed.
Returns ``True`` or ``False`` depending on if an update is needed.
>>> c=Canvas()
>>> c.require_update()
False
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.require_update()
False
>>> c=Canvas()
>>> c.require_update()
False
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.require_update()
False
Since we're not in a GTK+ mainloop, the update is not scheduled
asynchronous. Therefor require_update() returns False.
asynchronous. Therefore ``require_update()`` returns ``False``.
"""
return bool(self._dirty_items)
@ -529,6 +529,7 @@ class Canvas(object):
movement.
For example having an item
>>> from item import Element
>>> c = Canvas()
>>> e = Element()
@ -539,11 +540,13 @@ class Canvas(object):
[<Handle object on (0, 0)>, <Handle object on (10, 0)>, <Handle object on (10, 10)>, <Handle object on (0, 10)>]
and moving its first handle a bit
>>> e.handles()[0].x += 1
>>> map(float, e.handles()[0].pos)
[1.0, 0.0]
After normalization
>>> c._normalize([e]) # doctest: +ELLIPSIS
set([<gaphas.item.Element object at ...>])
>>> e.handles()
@ -601,8 +604,8 @@ class Canvas(object):
canvas. By lack of registered views, a PNG image surface is created
that is used to create a context.
>>> c = Canvas()
>>> c.update_now()
>>> c = Canvas()
>>> c.update_now()
"""
for view in self._registered_views:
try:
@ -616,7 +619,7 @@ class Canvas(object):
class VariableProjection(solver.Projection):
"""
Project a single gaphas.solver.Variable to another space/coordinate system.
Project a single `solver.Variable` to another space/coordinate system.
The value has been set in the "other" coordinate system. A callback is
executed when the value changes.

View File

@ -1,16 +1,30 @@
"""
Module ``gaphas.constraint`` contains several flavors of constraint solver
classes (constraints for short), for example
- equality constraint - two variables should have the same value
- less than constraint - keep one variables smaller than other
This module contains several flavors of constraint classes.
Each has a method `Constraint.solve_for(name)` and a method
`Constraint.mark_dirty(v)`. These methods are used by the constraint solver
(`solver.Solver`) to set the variables.
Variables should be of type ``gaphas.solver.Variable``.
Variables should be of type `solver.Variable`.
See classes' documentation below for constraints description and for
examples of their usage.
EqualsConstraint
Make 'a' and 'b' equal.
LessThanConstraint
Ensure one variable stays smaller than the other.
CenterConstraint
Ensures a Variable is kept between two other variables.
EquationConstraint
Solve a linear equation.
BalanceConstraint
Keeps three variables in line, maintaining a specific ratio.
LineConstraint
Solves the equation where a line is connected to a line or side at a
specific point.
New constraint class should derive from Constraint class abstract class and
implement Constraint.solve_for(Variable) method to update a variable with
implement `Constraint.solve_for(Variable)` method to update a variable with
appropriate value.
"""
@ -178,7 +192,7 @@ class CenterConstraint(Constraint):
class LessThanConstraint(Constraint):
"""
Ensure @smaller is less than @bigger. The variable that is passed
Ensure ``smaller`` is less than ``bigger``. The variable that is passed
as to-be-solved is left alone (cause it is the variable that has not
been moved lately). Instead the other variable is solved.
@ -194,6 +208,7 @@ class LessThanConstraint(Constraint):
(Variable(0.8, 20), Variable(0.8, 20))
Also minimal delta between two values can be set
>>> a, b = Variable(10.0), Variable(8.0)
>>> lt = LessThanConstraint(smaller=a, bigger=b, delta=5)
>>> lt.solve_for(a)
@ -224,7 +239,7 @@ class EquationConstraint(Constraint):
Takes a function, named arg value (opt.) and returns a Constraint object
Calling EquationConstraint.solve_for will solve the equation for
variable @arg, so that the outcome is 0.
variable ``arg``, so that the outcome is 0.
>>> from solver import Variable
>>> a, b, c = Variable(), Variable(4), Variable(5)
@ -358,10 +373,11 @@ class EquationConstraint(Constraint):
class BalanceConstraint(Constraint):
"""
Ensure that a variable @v is between values specified by @band
and in distance proportional from @band[0].
Ensure that a variable ``v`` is between values specified by ``band``
and in distance proportional from ``band[0]``.
Consider
>>> from solver import Variable, WEAK
>>> a, b, c = Variable(2.0), Variable(3.0), Variable(2.3, WEAK)
>>> bc = BalanceConstraint(band=(a,b), v=c)
@ -372,7 +388,8 @@ class BalanceConstraint(Constraint):
>>> a, b, c
(Variable(2, 20), Variable(3, 20), Variable(2.3, 10))
Band does not have to be band[0] < band[1]
Band does not have to be ``band[0] < band[1]``
>>> a, b, c = Variable(3.0), Variable(2.0), Variable(2.45, WEAK)
>>> bc = BalanceConstraint(band=(a,b), v=c)
>>> c.value = 2.50
@ -462,6 +479,7 @@ class LineConstraint(Constraint):
def _solve(self):
"""
Solve the equation for the connected_handle.
>>> from gaphas.solver import Variable
>>> line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
>>> point = (Variable(15), Variable(4))

View File

@ -17,59 +17,60 @@ class async(object):
priority. This requires the async'ed method to be called from within
the GTK main loop. Otherwise the method is executed directly.
Note: the current implementation of async single mode only works for
methods, not functions.
Note:
the current implementation of async single mode only works for
methods, not functions.
Calling the async function from outside the gtk main loop will yield
imediate execution:
async just works on functions (as long as single=False):
async just works on functions (as long as ``single=False``):
>>> a = async()(lambda: 'Hi')
>>> a()
'Hi'
>>> a = async()(lambda: 'Hi')
>>> a()
'Hi'
Simple method:
>>> class A(object):
... @async(single=False, priority=gobject.PRIORITY_HIGH)
... def a(self):
... print 'idle-a', gobject.main_depth()
>>> class A(object):
... @async(single=False, priority=gobject.PRIORITY_HIGH)
... def a(self):
... print 'idle-a', gobject.main_depth()
Methods can also set sinle mode to True (the method is only scheduled one).
>>> class B(object):
... @async(single=True)
... def b(self):
... print 'idle-b', gobject.main_depth()
>>> class B(object):
... @async(single=True)
... def b(self):
... print 'idle-b', gobject.main_depth()
This is a helper function used to test classes A and B from within the GTK+
main loop:
>>> def delayed():
... print 'before'
... a = A()
... b = B()
... a.a()
... b.b()
... a.a()
... b.b()
... a.a()
... b.b()
... print 'after'
... gobject.timeout_add(100, gtk.main_quit)
>>> gobject.timeout_add(1, delayed) > 0 # timeout id may vary
True
>>> import gtk
>>> gtk.main()
before
after
idle-a 1
idle-a 1
idle-a 1
idle-b 1
>>> def delayed():
... print 'before'
... a = A()
... b = B()
... a.a()
... b.b()
... a.a()
... b.b()
... a.a()
... b.b()
... print 'after'
... gobject.timeout_add(100, gtk.main_quit)
>>> gobject.timeout_add(1, delayed) > 0 # timeout id may vary
True
>>> import gtk
>>> gtk.main()
before
after
idle-a 1
idle-a 1
idle-a 1
idle-b 1
As you can see, although b.b() has been called three times, it's only
As you can see, although ``b.b()`` has been called three times, it's only
executed once.
"""
@ -110,15 +111,15 @@ def nonrecursive(func):
"""
Enforce a function or method is not executed recursively:
>>> class A(object):
... @nonrecursive
... def a(self, x=1):
... print x
... self.a(x+1)
>>> A().a()
1
>>> A().a()
1
>>> class A(object):
... @nonrecursive
... def a(self, x=1):
... print x
... self.a(x+1)
>>> A().a()
1
>>> A().a()
1
"""
def wrapper(*args, **kwargs):
"""

View File

@ -474,8 +474,9 @@ def rectangle_intersects(recta, rectb):
def rectangle_clip(recta, rectb):
"""
Return the clipped rectangle of recta and rectb. If they do not intersect,
None is returned.
Return the clipped rectangle of ``recta`` and ``rectb``. If they do not
intersect, ``None`` is returned.
>>> rectangle_clip((0, 0, 20, 20), (10, 10, 20, 20))
(10, 10, 10, 10)
"""

View File

@ -8,12 +8,12 @@ Variables change and at some point of time we want to make all constraints
valid again. This process is called solving constraints.
Gaphas' solver allows to define constraints between Variable instances.
Constraint classes are defined in gaphas.constraint module.
Constraint classes are defined in `gaphas.constraint` module.
How It Works
============
------------
Every constraint contains list of variables and has to be registered in
solver object. Variables change (Variables.dirty, Solver.request_resolve
solver object. Variables change (`Variable.dirty()`, `Solver.request_resolve()`
methods) and their constraints are marked by solver as dirty. To solve
constraints, solver loops through dirty constraints and asks constraint for
a variable (called weakest variable), which
@ -26,9 +26,9 @@ a variable (called weakest variable), which
(weakest variable invariants defined above)
Having weakest variable (Constraint.weakest method) every constraint is
being asked to solve itself (Constraint.solv_for method) changing
appropriate variables to make the constraint valid again.
Having weakest variable (`constraint.Constraint.weakest()` method) every
constraint is being asked to solve itself (`constraint.Constraint.solve_for()`
method) changing appropriate variables to make the constraint valid again.
"""
from __future__ import division
@ -231,7 +231,6 @@ class Variable(object):
def __truediv__(self, other):
"""
#>>> from __future__ import division
>>> Variable(5.) / 4
1.25
>>> 10 / Variable(5.)
@ -301,7 +300,6 @@ class Variable(object):
def __rtruediv__(self, other):
"""
#>>> from __future__ import division
>>> 5. / Variable(4)
1.25
"""
@ -354,9 +352,10 @@ class Solver(object):
If projections_only is set to True, only constraints using the
variable through a Projection instance (e.i. variable itself is not
in Constraint.variables()) are marked.
in `constraint.Constraint.variables()`) are marked.
Example:
>>> from constraint import EquationConstraint
>>> a, b, c = Variable(1.0), Variable(2.0), Variable(3.0)
>>> s = Solver()
@ -400,6 +399,7 @@ class Solver(object):
later on.
Example:
>>> from constraint import EquationConstraint
>>> s = Solver()
>>> a, b = Variable(), Variable(2.0)
@ -428,7 +428,9 @@ class Solver(object):
@observed
def remove_constraint(self, constraint):
""" Remove a constraint from the solver
"""
Remove a constraint from the solver
>>> from constraint import EquationConstraint
>>> s = Solver()
>>> a, b = Variable(), Variable(2.0)
@ -442,8 +444,8 @@ class Solver(object):
set([])
Removing a constraint twice has no effect:
>>> s.remove_constraint(c)
>>> s.remove_constraint(c)
"""
for v in constraint.variables():
while isinstance(v, Projection):
@ -459,7 +461,7 @@ class Solver(object):
"""
Return an iterator of constraints that work with variable.
The variable in question should be exposed by the constraints
variables() method.
`constraint.Constraint.variables()` method.
>>> from constraint import EquationConstraint
>>> s = Solver()
@ -485,6 +487,7 @@ class Solver(object):
def solve(self):
"""
Example:
>>> from constraint import EquationConstraint
>>> a, b, c = Variable(1.0), Variable(2.0), Variable(3.0)
>>> s = Solver()

View File

@ -25,8 +25,8 @@ import types, inspect
from decorator import decorator
# This string is added to each docstring in order to denote is's observed
OBSERVED_DOCSTRING = \
'\n\nThis method is @observed. See gaphas.state for extra info.\n'
#OBSERVED_DOCSTRING = \
# '\n\n This method is @observed. See gaphas.state for extra info.\n'
# Tell @observed to dispatch invokation messages by default
# May be changed (but be sure to do that right at the start of your
@ -62,7 +62,7 @@ def observed(func):
return func(*args, **kwargs)
dec = decorator(wrapper, func)
dec.__doc__ = (dec.__doc__ or '') + OBSERVED_DOCSTRING
#dec.__doc__ = (dec.__doc__ or '') + OBSERVED_DOCSTRING
func.__observer__ = dec
if DISPATCH_BY_DEFAULT:
dec.__dispatched__ = True