challenges package

Submodules

challenges.challenge module

Core module of challenges

This module holds the base class of all challenges.

class challenges.challenge.Challenge[source]

Bases: object

Base class of all challenges

Design concept is the Template Method Design Pattern (GOF).

Attributes:

Sample:The input of the challenge.
Output:The output of the challenge

Workflow:

The main method controls the overall workflow by calling the worker methods. This is the common character of all challenges. The base class controls the workflow of the derived workers.

Workers:

The worker methods need to be implemented by the inheriting class.

Read:Read the input into a list of lines.
Build:Build the data model from the lines.
Calc:Run the main algorithm of the challenge.
Format:Create the output string required by the grader.

Library:

The other methods support the implementation of the workers. They address the extraction of data from the input lines or the formatting of the output.

Sample:

The attribute sample is both used as class and as instance attribute. When the instance attribute is injected it shadows the class attribute. By this the class attribute sets a tiny but useful default.

When the challenge runner is executed with the option –klass no instance variable is injected and the sample from the class is used:

prompt> challenge MyChallenge --klass

When the runner is executed with the option –file the files content is injected:

prompt> challenge MyChallenge --file ~/Downloads/data.txt
br = '\n'

Line breaks as expected by the most graders.

build()[source]

Set up the model from the input lines.

This method must be implemented. Reads from self.lines. Fills self.model.

calc()[source]

Main algorithm of the challenge.

This method must be implemented. Here the interesting stuff happens. Best practice is to delegate to functions, that are named by the algorithms used or even to other classes that implement the algorithm.

Reads from self.model. Fills self.result.

edge_pattern = '^(\\d+)->(\\d+)(:(\\d+))?$'

Reg expression to extract edges of a graph.

A default setting used by methods that extract edges from input lines. May need adjustment for different kind of edge input formats.

edges(start=0, stop=None)[source]

Generator to read edges from lines.

Reads a range of lines, one edge per line, and yields the edges.

By the start and stop parameters a range can be given. The stop parameter is the index behind the last line to use.

The line to start is set by the parameter start. It defaults to zero. The line to stop is set by the parameter stop. When it is not provided lines are used as long as they match the edge_pattern reg expression. The match behaviour can be adjusted by the self.edge_pattern.

fasta(start=0, stop=None)[source]

Generator to read FASTA formatted samples.

Reads multiple fasta sequences and yields them.

By the start and stop parameters a range can be given. The stop parameter is the index behind the last line to use.

The line to start is set by the parameter start. It defaults to zero. The line to stop is set by the parameter stop. When it is not provided lines are used as long as they match the FASTA format. The match behaviour can be adjusted by the self.fasta_pattern.

fasta_pattern = '^[\\-\\*A-Z]+$'

Reg expression for FASTA sequences.

Matches lines holding FASTA sequences.

format()[source]

Format the output string.

In simple cases this method can be used as is. In other cases it needs to be reimplemented.

Reads from self.result. Fills self.output.

format_list_of_integers(integers, joint=', ')[source]

Join a list of integers to a string

Use the given joint.

format_path(integers, backwards=False)[source]

Join a list of integers to path of nodes.

The joint is -> by default. If the parameter backwards is True the joint is <-.

line(number)[source]

Return one line by the given number.

line_to_edge(nr)[source]

Convert one line to an edge.

The number of the line is selected by line_nr. The split behaviour can be adjusted by changing self.edge_pattern.

line_to_floats(line_nr)[source]

Split one line into a list of floats.

The number of the line is selected by line_nr. The split behaviour can be adjusted by changing self.split_pattern.

line_to_integers(line_nr)[source]

Split one line into a list of integers.

The number of the line is selected by line_nr. The split behaviour can be adjusted by changing self.split_pattern.

lines = None

A list of lines that will be filled by the method read().

main()[source]

Control the workflow of the challenge.

Usually this method doesn’t need to be overwritten.

The workers share data via instance variables. The overall input is injected into self.sample. The overall output is read from self.result.

model = None

The imported data model.

A flexible namespace object to take up any kind of data. In simple cases this may be completely overwritten, i.e by a list or dict.

output = None

The output string.

The string representation of the resulting model as expected by the grader.

read()[source]

Extract the input string self.sample into self.lines.

Typically this method can be used as is.

result = None

The resulting data model.

A flexible namespace object to take up any kind of data. In simple cases this may be completely overwritten, i.e by a list or dict.

sample = 'sample'

Holds a minimal example of the input.

This class variable should always be preset with a tiny sample of input.

split_pattern = '\\s+|\\s?,\\s?'

Reg expression to split input lines.

Used by some of the input parsing functions. The default splits by whitespace and/or comma. If the input is separated differently like colons or semicolons it needs adjustment in the inheriting class.

challenges.conf module

class challenges.conf.Conf[source]

Bases: object

get_challenge()[source]
get_challenge_dir()[source]
get_challenge_file()[source]
get_challenge_init_file()[source]
get_challenge_name()[source]
get_challenges()[source]
static get_class(class_)[source]
get_full_qualified_challenge_name()[source]
get_full_qualified_unittest_name()[source]
get_input_file()[source]
get_latest_at_root()[source]
get_latest_file()[source]
get_result_file()[source]
get_sample_file()[source]
get_unittest()[source]
get_unittest_file()[source]
parse_arguments()[source]
print_help()[source]

challenges.graph module

class challenges.graph.Edge(tail, head)[source]

Bases: object

head

Return the head node.

tail

Return the tail node.

class challenges.graph.Graph[source]

Bases: object

count()[source]

Return count of nodes.

create_edge(tail, head)[source]

Add an edge and return it.

Create nodes as necessary.

Tail:Node or id of node
Head:Node or id of node
create_node(id)[source]

Add a standalone node to the graph and return it.

If a node of this id already exists, it is just returned.

keys()[source]

Return keys of nodes in sorted order.

node(id)[source]

Return a node by id.

nodes()[source]

Return all nodes sorted by node id.

class challenges.graph.Node(id)[source]

Bases: object

add_edge(edge)[source]

Add an edge to the node.

Not for direct usage. To create node and edges use:

  • Graph.create_node()
  • Graph.create_edge()
antecessors()[source]

Return the incoming NODES of the node.

The tail nodes of all incoming edges.

id

Return the nodes id.

incoming

Return the incoming EDGES of the node.

outgoing

Return the outgoing EDGES of the node.

successors()[source]

Return the outgoing NODES of the node.

The head nodes of all outgoing edges.

challenges.main module

challenges.main.main()[source]

challenges.runner module

class challenges.runner.Runner(conf)[source]

Bases: object

list_challenges()[source]
main()[source]
read_file()[source]
run_challenge()[source]
run_unittest()[source]
set_sample(challenge)[source]
write(challenge)[source]

challenges.scaffold module

class challenges.scaffold.Scaffold(conf)[source]

Bases: object

get_class_content()[source]
get_unittest_content()[source]
scaffold()[source]

Module contents