Instruction Module

Knitting patterns consist of instructions.

The instructions. that are used in the knitting patterns can be foudn in this module. They have certain attributes in common.

class knittingpattern.Instruction.Instruction(specification, inherited_values=())[source]

Bases: knittingpattern.Prototype.Prototype

Instructions specify what should be done during knitting.

This class represents the basic interface for instructions.

It is based on the Prototype which allows creating instructions based on other instructions so they can inherit their attributes.

You can create new instructions by passing a specification to them which can consist of a dictionary or an other prototype. For such specifications see the InstructionLibrary.

color

The color of the instruction.

Returns:the color of the instruction or None if none is specified.
colors

All the colors that an instruction has.

Returns:a list of colors of the instruction. If the instruction has no color, this is [None].
Return type:list
consumes_meshes()[source]

Whether this instruction consumes meshes.

Returns:whether this instruction consumes any meshes
Return type:bool
description

The description of the instruction.

Returns:the description of the instruction or None if none is specified.
does_knit()[source]

Whether this instruction is a knit instruction.

Returns:whether this instruction is a knit instruction
Return type:bool
does_purl()[source]

Whether this instruction is a purl instruction.

Returns:whether this instruction is a purl instruction
Return type:bool
has_color()[source]

Whether this instruction has a color.

Returns:whether a color is specified
Return type:bool
hex_color

The color in “#RRGGBB” format.

Returns:the color in “#RRGGBB” format or none if no color is given
id

The id of the instruction.

Returns:the id of the instruction or None if none is specified.
number_of_consumed_meshes

The number of meshes that this instruction consumes.

Returns:the number of consumed meshes of the instruction or DEFAULT_NUMBER_OF_CONSUMED_MESHES if none is specified.
number_of_produced_meshes

The number of meshes that this instruction produces.

Returns:the number of produced meshes of the instruction or DEFAULT_NUMBER_OF_PRODUCED_MESHES if none is specified.
produces_meshes()[source]

Whether this institution produces meshes.

Returns:whether this instruction produces any meshes
Return type:bool
render_z

The z-index of the instruction when rendered.

Returns:the z-index of the instruction. Instructions with a higher z-index are displayed in front of instructions with lower z-index.
Return type:float
to_svg(converter=None)[source]

Return a SVGDumper for this instruction.

Parameters:converter – a :class:` knittingpattern.convert.InstructionSVGCache.InstructionSVGCache` or None. If None is given, the :func:` knittingpattern.convert.InstructionSVGCache.default_svg_cache` is used.
Return type:knittingpattern.Dumper.SVGDumper
type

The type of the instruction.

Returns:the type of the instruction or DEFAULT_TYPE if none is specified.
Return type:str

The type should be a string. Depending on the type, the instruction can receive additional attributes.

class knittingpattern.Instruction.InstructionInRow(row, spec)[source]

Bases: knittingpattern.Instruction.Instruction

Instructions can be placed in rows.

Then, they have additional attributes and properties.

__init__(row, spec)[source]

Create a new instruction in a row with a specification.

Parameters:
__repr__()[source]

repr(instruction) used for print().

Returns:the string representation of this object
Return type:str
color

The color of the instruction.

Returns:the color of the instruction or None if none is specified.

If no color is specified in the instruction, it is inherited form the row.

consumed_meshes

The meshes consumed by this instruction

Returns:a list of meshes that this instruction consumes
Return type:list
assert len(inst.consumed_meshes) == inst.number_of_consumed_meshes
assert all(mesh.is_consumed() for mesh in inst.consumed_meshes)
consuming_instructions

Instructions that consume the meshes that this instruction produces.

Returns:a list of instructions
Return type:list
first_consumed_mesh

The first consumed mesh.

Returns:the first consumed mesh
Return type:knittingpattern.Mesh.Mesh
Raises:IndexError – if no mesh is consumed
first_produced_mesh

The first produced mesh.

Returns:the first produced mesh
Return type:knittingpattern.Mesh.Mesh
Raises:IndexError – if no mesh is produced
get_index_in_row()[source]

Index of the instruction in the instructions of the row or None.

Returns:index in the row‘s instructions or None, if the instruction is not in the row
Return type:int
index_in_row

Index of the instruction in the instructions of the row.

Returns:index in the row‘s instructions
Return type:int
Raises:knittingpattern.Instruction.InstructionNotFoundInRow – if the instruction is not found at the index
index = instruction.index_in_row
assert instruction.row.instructions[index] == instruction
index_of_first_consumed_mesh_in_row

The index of the first consumed mesh of this instruction in its row.

Same as index_of_first_produced_mesh_in_row but for consumed meshes.

index_of_first_produced_mesh_in_row

Index of the first produced mesh in the row that consumes it.

Returns:an index of the first produced mesh of rows produced meshes
Return type:int

Note

If the instruction produces meshes, this is the index of the first mesh the instruction produces in all the meshes of the row. If the instruction does not produce meshes, the index of the mesh is returned as if the instruction had produced a mesh.

if instruction.produces_meshes():
    index = instruction.index_of_first_produced_mesh_in_row
index_of_last_consumed_mesh_in_row

The index of the last consumed mesh of this instruction in its row.

Same as index_of_last_produced_mesh_in_row but for the last consumed mesh.

index_of_last_produced_mesh_in_row

Index of the last mesh produced by this instruction in its row.

Returns:an index of the last produced mesh of rows produced meshes
Return type:int

Note

If this instruction produces meshes, this is the index of its last produces mesh in the row. However, if this instruction does not produce meshes, this is the index before the first mesh of the instruction if it produced meshes.

is_in_row()[source]

Whether the instruction can be found in its row.

Returns:whether the instruction is in its row
Return type:bool

Use this to avoid raising and InstructionNotFoundInRow.

last_consumed_mesh

The last consumed mesh.

Returns:the last consumed mesh
Return type:knittingpattern.Mesh.Mesh
Raises:IndexError – if no mesh is consumed
last_produced_mesh

The last produced mesh.

Returns:the last produced mesh
Return type:knittingpattern.Mesh.Mesh
Raises:IndexError – if no mesh is produced
next_instruction_in_row

The instruction after this one or None.

Returns:the instruction in row_instructions after this or None if this is the last
Return type:knittingpattern.Instruction.InstructionInRow

This can be used to traverse the instructions.

previous_instruction_in_row

The instruction before this one or None.

Returns:the instruction in row_instructions before this or None if this is the first
Return type:knittingpattern.Instruction.InstructionInRow

This can be used to traverse the instructions.

produced_meshes

The meshes produced by this instruction

Returns:a list of meshes that this instruction produces
Return type:list
assert len(inst.produced_meshes) == inst.number_of_produced_meshes
assert all(mesh.is_produced() for mesh in inst.produced_meshes)
producing_instructions

Instructions that produce the meshes that this instruction consumes.

Returns:a list of instructions
Return type:list
row

The row this instruction is in.

Returns:the row the instruction is placed in
Return type:knittingpattern.Row.Row
row_instructions

Shortcut for instruction.row.instructions.

Returns:the instructions of the row the instruction is in

See also

index_in_row

transfer_to_row(new_row)[source]

Transfer this instruction to a new row.

Parameters:new_row (knittingpattern.Row.Row) – the new row the instruction is in.
exception knittingpattern.Instruction.InstructionNotFoundInRow[source]

Bases: ValueError

This exception is raised if an instruction was not found in its row.

__weakref__

list of weak references to the object (if defined)

knittingpattern.Instruction.ID = 'id'

the id key in the specification

knittingpattern.Instruction.TYPE = 'type'

the type key in the specification

knittingpattern.Instruction.KNIT_TYPE = 'knit'

the type of the knit instruction

knittingpattern.Instruction.PURL_TYPE = 'purl'

the type of the purl instruction

knittingpattern.Instruction.DEFAULT_TYPE = 'knit'

the type of the instruction without a specified type

knittingpattern.Instruction.COLOR = 'color'

the color key in the specification

knittingpattern.Instruction.NUMBER_OF_CONSUMED_MESHES = 'number of consumed meshes'

the key for the number of meshes that a instruction consumes

knittingpattern.Instruction.DEFAULT_NUMBER_OF_CONSUMED_MESHES = 1

the default number of meshes that a instruction consumes

knittingpattern.Instruction.NUMBER_OF_PRODUCED_MESHES = 'number of produced meshes'

the key for the number of meshes that a instruction produces

knittingpattern.Instruction.DEFAULT_NUMBER_OF_PRODUCED_MESHES = 1

the default number of meshes that a instruction produces

knittingpattern.Instruction.RENDER_Z = 'z'

The key to look for the z-index inside the render specification. .. seealso:: get_z(), DEFAULT_Z

knittingpattern.Instruction.RENDER = 'render'

Instructions have a default specification. In this specification the key in RENDER points to configuration for rendering.

knittingpattern.Instruction.DEFAULT_Z = 0

The default z-index, see get_z().