|
| 1 | +*********************************** |
| 2 | +Converting Between Sequence Formats |
| 3 | +*********************************** |
| 4 | + |
| 5 | +Different applications and use cases can require different formats of |
| 6 | +sequence strings. |
| 7 | +PathSeq supports converting a path sequence to other formats by overriding the |
| 8 | +:class:`~pathseq.Formatter` class. |
| 9 | + |
| 10 | +Without any changes, the Formatter class will format |
| 11 | +a path sequence into a string without any changes. |
| 12 | + |
| 13 | +.. code-block:: pycon |
| 14 | +
|
| 15 | + >>> seq = PathSequence("image.1-5#.exr") |
| 16 | + >>> pathseq.Formatter().format(seq.parsed) |
| 17 | + 'image.1-5#.exr' |
| 18 | +
|
| 19 | +To convert to another format, the methods of a Formatter can be overriden. |
| 20 | +For example, a basic formatter for Houdini strings might look like this: |
| 21 | + |
| 22 | +.. code-block:: pycon |
| 23 | +
|
| 24 | + >>> class HoudiniFormatter(pathseq.Formatter): |
| 25 | + ... def range(self, range_): |
| 26 | + ... return "$F" |
| 27 | + ... |
| 28 | + >>> seq = PathSequence("image.1-5#.exr") |
| 29 | + >>> HoudiniFormatter().format(seq.parsed) |
| 30 | + 'image.$F.exr' |
| 31 | +
|
| 32 | +There is a method on the Formatter for each attribute on a parsed sequence. |
| 33 | +So any part of a path sequence can be changed. |
| 34 | + |
| 35 | +.. code-block:: pycon |
| 36 | +
|
| 37 | + >>> class DemoFormatter(pathseq.Formatter): |
| 38 | + ... def stem(self, stem): |
| 39 | + ... return "stem" |
| 40 | + ... def prefix(self, prefix): |
| 41 | + ... return "|prefix" |
| 42 | + ... def range(self, range_): |
| 43 | + ... return "|range" |
| 44 | + ... def inter_range(self, inter_range): |
| 45 | + ... return "|inter_range" |
| 46 | + ... def postfix(self, postfix): |
| 47 | + ... return "|postfix" |
| 48 | + ... def suffixes(self, suffixes): |
| 49 | + ... return "|suffixes" |
| 50 | + ... |
| 51 | + >>> seq = PathSequence("image.1-5#.exr") |
| 52 | + >>> DemoFormatter().format(seq.parsed) |
| 53 | + 'stem|prefix|range|suffixes' |
| 54 | +
|
| 55 | +The order of the parts in a :ref:`loose <loose-format>` can even be changed |
| 56 | +by overriding the :meth:`~pathseq.Formatter.format` or |
| 57 | +:meth:`~pathseq.Formatter.ranges` methods. |
| 58 | +For example, a naive formatter to convert a loose format range into |
| 59 | +a :ref:`simple <simple-format>` might look like this: |
| 60 | + |
| 61 | +.. code-block:: pycon |
| 62 | +
|
| 63 | + >>> class ToSimpleFormatter(pathseq.Formatter): |
| 64 | + ... def format(self, seq): |
| 65 | + ... return ( |
| 66 | + ... self.stem(seq.stem) |
| 67 | + ... + self.prefix(seq.prefix or ".") |
| 68 | + ... + self.ranges(seq.ranges) |
| 69 | + ... + self.suffixes(seq.suffixes) |
| 70 | + ... ) |
| 71 | + ... |
| 72 | + >>> seq = LoosePathSequence("1-5#image.exr") |
| 73 | + >>> ToSimpleFormatter().format(seq.parsed) |
| 74 | + 'image.1-5#.exr' |
0 commit comments