Extra

Arranger

class pdpy_lib.extra.arranger.Arranger(canvas, verbose=False, hstep=1.5, vstep=1, xmargin=10, ymargin=10)[source]

Bases: object

Arranger objects on a 2d surface

This class attempts to arrange objects graphically on the self.canvas. To use, simply import and call the class.

The main algorithm consists of initialization and three steps:

  1. Initialization

  2. step1: step1()

  3. step2: step2()

  4. step3: step3()

Parameters
  • canvas (pdpy.Canvas) – The canvas to arrange

  • verbose (bool) – (optional) set verbosity level (default: False)

  • hstep (float) –

    (optional) set the horizontal step factor for x-increments (default: 1.5)

    This factor multiplies the width maxima of all nodes in a canvas.

  • vstep (float) –

    (optional) set the vertical step factor for y-increments (default: 1)

    This factor multiplies the height maxima of all nodes in a canvas.

  • xmargin (int) – (optional) set the initial x margin on the canvas (default: 10)

  • xmargin – (optional) set the initial x margin on the canvas (default: 10)

Return type

None

Raises
  • Exception – if there were errors during arrangement

  • ValueError – if the Canvas has no nodes to arrange

Example

Import the PdPy and Arranger classes

>>> from .classes.pdpy import PdPy
>>> from .util.arrange import Arranger as arranger

Create the PdPy instance (and add some objects)

>>> pd = PdPy()

Call the function

>>> arranger(p)
step1()[source]

Step 1: Take the object from the object list

This step takes no arguments and performs the following instructions:

  1. take an object from the object list

  2. place the object

  3. pass the object to step2() without callback

If there are no objects on the list, it returns

Return type

None

step2(obj, relocate=False)[source]

Step 2: Take the object’s children from the object list

This step takes performs the following instructions:

  1. return if the obj has no children

  2. otherwise, pop all children from the object list

For each child, if the child is not on the object list:

  1. If the child is not placed, place it, otherwise move it

  2. If relocate is True, run the relocator on that child

If there are popped children, pass them to step3(), otherwise

  1. reset y-position

  2. and go back to step1()

Parameters
  • obj (pdpy.Object) – A patchable PdPy object based on object with a position attribute

  • relocate (callback or None) – A callback function to perform object relocation

Return type

None

step3(obj, children)[source]

Step3: Place every child

For every child in the children list:

  1. Adjust y-position

  2. Adjust x-position

  3. Place the child

  4. Pass the child to step2() with relocate=True

Parameters
  • obj (pdpy_lib.core.object.Object) – The PdPy object with position attribute

  • children (list) – The list of children whose parent is the passed obj

Translator

class pdpy_lib.extra.translator.Translator(json)[source]

Bases: Base

This class maintains and translates a pdpy Obj in memory.

This class loads a file in .pd or .json formats and keeps an internal mirror (aka, translation) between the two. The direction of the translation depends on the input file type. Use the save_* functions to write translations to disk. Alternatively, you can load a .pkl (aka, pickle) file containing a pdpy object.

Parameters

json (dict) –

A dictionary of arguments with the following keys:

  • to: the target format. Can be json, xml, pd, or pkl.

  • fro: the source format. Can be json, xml, pd, or pkl.

  • input: An input file Path, will be formated using pathlib.Path

  • output: An output file Path, will be formated using pathlib.Path

  • encoding (str, defaults: ‘utf-8’): Encoding of the input file

  • source (str, inferred from input_file): Source file type

  • reflect (bool): If set to True, performs a reflected translation