example_code package

Submodules

example_code.addable module

class example_code.addable.Addable[source]

Bases: ABC

example_code.euclid module

example_code.euclid.gcd(a, b)[source]

Return the greatest common divisor of a and b using a recursive implementation of Euclid’s algorithm.

example_code.expression_tools module

example_code.expression_tools.evaluate(expr, *o, **kwargs)[source]
example_code.expression_tools.evaluate(expr: Number, *o, **kwargs)
example_code.expression_tools.evaluate(expr: Symbol, *o, symbol_map, **kwargs)
example_code.expression_tools.evaluate(expr: Add, *o, **kwargs)
example_code.expression_tools.evaluate(expr: Sub, *o, **kwargs)
example_code.expression_tools.evaluate(expr: Mul, *o, **kwargs)
example_code.expression_tools.evaluate(expr: Div, *o, **kwargs)
example_code.expression_tools.evaluate(expr: Pow, *o, **kwargs)

Evaluate an expression node.

Parameters:
  • expr (Expression) – The expression node to be evaluated.

  • *o (numbers.Number) – The results of evaluating the operands of expr.

  • **kwargs – Any keyword arguments required to evaluate specific types of expression.

  • symbol_map (dict) –

    A dictionary mapping Symbol names to numerical values, for example:

    {‘x’: 1}

example_code.expression_tools.postvisitor(expr, fn, **kwargs)[source]

Visit an Expression in postorder applying a function to every node.

Parameters:
  • expr (Expression) – The expression to be visited.

  • fn (function(node, *o, **kwargs)) – A function to be applied at each node. The function should take the node to be visited as its first argument, and the results of visiting its operands as any further positional arguments. Any additional information that the visitor requires can be passed in as keyword arguments.

  • **kwargs – Any additional keyword arguments to be passed to fn.

example_code.graphs module

A simple tree implementation with basic pre- and post-visitors.

class example_code.graphs.TreeNode(value, *children)[source]

Bases: object

A basic tree implementation.

A tree is simply a collection of connected TreeNodes.

Parameters:
  • value – An arbitrary value associated with this node.

  • children – The TreeNodes which are the children of this node.

example_code.graphs.postvisitor(tree, fn)[source]

Traverse tree in postorder applying a function to every node.

Parameters:
  • tree (TreeNode) – The tree to be visited.

  • fn (function(node, *fn_children)) – A function to be applied at each node. The function should take the node to be visited as its first argument, and the results of visiting its children as any further arguments.

example_code.graphs.previsitor(tree, fn, fn_parent=None)[source]

Traverse tree in preorder applying a function to every node.

Parameters:
  • tree (TreeNode) – The tree to be visited.

  • fn (function(node, fn_parent)) – A function to be applied at each node. The function should take the node to be visited as its first argument, and the result of visiting its parent as the second.

example_code.groups module

A module implementing the basic functionality of mathematical groups.

This version of the module uses inheritance.

class example_code.groups.CyclicGroup(n)[source]

Bases: Group

A cyclic group represented by integer addition modulo n.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is addition modulo n.

symbol = 'C'
class example_code.groups.Element(group, value)[source]

Bases: object

An element of the specified group.

Parameters:
  • group (Group) – The group of which this is an element.

  • value – The value of this entity. Valid values depend on the group.

class example_code.groups.GeneralLinearGroup(n)[source]

Bases: Group

The general linear group represented by n x n matrices.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is matrix multiplication.

symbol = 'G'
class example_code.groups.Group(n)[source]

Bases: object

A base class containing methods common to many groups.

Each subclass represents a family of parametrised groups.

Parameters:

n (int) – The primary group parameter, such as order or degree. The precise meaning of n changes from subclass to subclass.

example_code.groups_abc module

A module implementing the basic functionality of mathematical groups.

This version of the module uses inheritance and defines the base class as an abc.ABC.

class example_code.groups_abc.CyclicGroup(n)[source]

Bases: Group

A cyclic group represented by integer addition modulo n.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is addition modulo n.

symbol = 'C'
class example_code.groups_abc.Element(group, value)[source]

Bases: object

An element of the specified group.

Parameters:
  • group (Group) – The group of which this is an element.

  • value – The value of this entity. Valid values depend on the group.

class example_code.groups_abc.GeneralLinearGroup(n)[source]

Bases: Group

The general linear group represented by n x n matrices.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is matrix multiplication.

symbol = 'G'
class example_code.groups_abc.Group(n)[source]

Bases: ABC

A base class containing methods common to many groups.

Each subclass represents a family of parametrised groups.

Parameters:

n (int) – The primary group parameter, such as order or degree. The precise meaning of n changes from subclass to subclass.

abstract operation(a, b)[source]

Return a ∘ b using the group operation ∘.

abstract property symbol

Represent the group name as a character.

example_code.groups_basic module

A module implementing the basic functionality of mathematical groups.

class example_code.groups_basic.CyclicGroup(order)[source]

Bases: object

A cyclic group represented by addition modulo group order.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is addition modulo n.

class example_code.groups_basic.Element(group, value)[source]

Bases: object

An element of the specified group.

Parameters:
  • group – The group of which this is an element.

  • value – The individual element value.

class example_code.groups_basic.GeneralLinearGroup(degree)[source]

Bases: object

The general linear group represented by degree square matrices.

operation(a, b)[source]

Perform the group operation on two values.

The group operation is matrix multiplication.

example_code.hello module

example_code.linked_list module

Bases: object

insert(link)[source]

Insert a new link after the current one.

class example_code.linked_list.LinkIterator(link)[source]

Bases: object

example_code.linked_list.byte_size(n)[source]

Print the size in bytes of lists up to length n.

example_code.polynomial module

class example_code.polynomial.Polynomial(coefs)[source]

Bases: object

degree()[source]

example_code.shapes module

A module containing some very simple shape classes to illustrate the use of super().

class example_code.shapes.Rectangle(length, width)[source]

Bases: object

area()[source]
class example_code.shapes.Square(length)[source]

Bases: Rectangle

example_code.simple_classes module

A very elementary set of classes and objects for one of the week 3 quiz questions.

class example_code.simple_classes.a(b)[source]

Bases: object

c()[source]
example_code.simple_classes.d

alias of a

example_code.simple_classes.g()

example_code.square module

example_code.square.square(x)[source]

example_code.try_except module

example_code.try_except.except_demo(n)[source]

Demonstrate all the clauses of a try block.

Module contents