mafw.examples.loop_modifier

The module provides examples on how the user can change the looping structure in a looping processor using the looping status.

Functions

is_prime(n)

Check if n is a prime number.

Classes

FindNPrimeNumber(*args, **kwargs)

An example of Processor to search for N prime numbers starting from a given starting integer.

FindPrimeNumberInRange(*args, **kwargs)

An example processor to find prime numbers in the defined interval from start_from to stop_at.

ModifyLoopProcessor(*args, **kwargs)

Example processor demonstrating how it is possible to change the looping structure.

class mafw.examples.loop_modifier.FindNPrimeNumber(*args: Any, **kwargs: Any)[source]

Bases: Processor

An example of Processor to search for N prime numbers starting from a given starting integer.

This processor is meant to demonstrate the use of a while_loop execution workflow.

Let us say we need to find 1000 prime numbers starting from 12347. One possible brute force approach to solve this problem is to start checking if the initial value is a prime number. If this is not the case, then check the next odd number. If it is the case, then add the current number to the list of found prime numbers and continue until the size of this list is 1000.

This is a perfect application for a while loop execution workflow.

Processor parameters

  • prime_num_to_find: How many prime number we have to find (default: 100)

  • start_from: From which number to start the search (default: 50)

Processor parameters:

Parameters:
  • prime_num_to_find (int) – The number of prime numbers to be found.

  • start_from (int) – The initial integer number from where to start the search.

finish() None[source]

Overload of the finish method.

Remember: The finish method is called only once just after the last loop interaction. Always put a call to its super when you overload finish.

The loop is over, it means that the while condition was returning false, and now we can do something with our list of prime numbers.

format_progress_message() None[source]

Customizes the progress message with information about the current item.

The user can overload this method in order to modify the message being displayed during the process loop with information about the current item.

The user can access the current value, its position in the looping cycle and the total number of items using Processor.item, Processor.i_item and Processor.n_item.

process() None[source]

The overload of the process method.

Remember: The process method is called inside the while loop. It has access to the looping parameters: Processor.i_item, Processor.item and Processor.n_item.

In our specific case, the process contains another while loop. We start by checking if the current Processor.item is a prime number or not. If so, then we have found the next prime number, we add it to the list, we increment by two units the value of Processor.item and we leave the process method ready for the next iteration.

If Processor.item is not prime, then increment it by 2 and check it again.

start() None[source]

The overload of the start method.

Remember: The start method is called just before the while loop is started. So all instructions in this method will be executed only once at the beginning of the process execution. Always put a call to its super when you overload start.

First, we empty the list of found prime numbers. It should not be necessary, but it makes the code more readable. Then set the Processor.n_item to the total number of prime numbers we need to find. In this way, the progress bar will display useful progress.

If the start value is smaller than 2, then let’s add 2 to the list of found prime number and set our first item to check at 3. In principle, we could already add 3 as well, but maybe the user wanted to find only 1 prime number, and we are returning a list with two, that is not what he was expecting.

Since prime numbers different from 2 can only be odd, if the starting number is even, increment it already by 1 unit.

while_condition() bool[source]

Define the while condition.

First, it checks if the prime_num_to_find is positive. Otherwise, it does not make sense to start. Then it will check if the length of the list with the already found prime numbers is enough. If so, then we can stop the loop return False, otherwise, it will return True and continue the loop.

Differently from the for_loop execution, we are responsible to assign the value to the looping variables Processor.i_item, Processor.item and Processor.n_item.

In this case, we will use the Processor.i_item to count how many prime numbers we have found and Processor.n_item will be our target. In this way, the progress bar will work as expected.

In the while condition, we set the Processor.i_item to the current length of the found prime number list.

Returns:

True if the loop has to continue, False otherwise

prime_num_found: list[int]

The list with the found prime numbers

class mafw.examples.loop_modifier.FindPrimeNumberInRange(*args: Any, **kwargs: Any)[source]

Bases: Processor

An example processor to find prime numbers in the defined interval from start_from to stop_at.

This processor is meant to demonstrate the use of a for_loop execution workflow.

Let us say we want to select only the prime numbers in a user defined range. One possible brute force approach is to generate the list of integers between the range extremes and check if it is prime or not. If yes, then add it to the list of prime numbers, if not continue with the next element.

This is a perfect application for a loop execution workflow.

Processor parameters

  • start_from: From which number to start the search (default: 50)

  • stop_at: At which number to stop the search (default: 100)

Processor parameters:

Parameters:
  • start_from (int) – First element of the range under investigation.

  • stop_at (int) – Last element of the range under investigation.

finish() None[source]

Overload of the finish method.

Remember: to call the super method when you overload the finish method.

In this case, we just print out some information about the prime number found in the range.

format_progress_message() None[source]

Customizes the progress message with information about the current item.

The user can overload this method in order to modify the message being displayed during the process loop with information about the current item.

The user can access the current value, its position in the looping cycle and the total number of items using Processor.item, Processor.i_item and Processor.n_item.

get_items() Collection[Any][source]

Overload of the get_items method.

This method must be overloaded when you select a for loop workflow.

Here we generate the list of odd numbers between the start and stop that we need to check. We also check that the stop is actually larger than the start, otherwise we print an error message, and we return an empty list of items.

Returns:

A list of odd integer numbers between start_from and stop_at.

Return type:

list[int]

process() None[source]

The process method.

In this case, it is very simple. We check if Processor.item is a prime number, if so we added to the list, otherwise we let the loop continue.

start() None[source]

Overload of the start method.

Remember: to call the super method when you overload the start.

In this specific case, we just make sure that the list of found prime numbers is empty.

prime_num_found: list[int]

The list with the found prime numbers

class mafw.examples.loop_modifier.ModifyLoopProcessor(*args: Any, **kwargs: Any)[source]

Bases: Processor

Example processor demonstrating how it is possible to change the looping structure.

It is a looping processor where some events will be skipped, and at some point one event will trigger an abort.

Processor parameters

  • item_to_abort: Item to abort (default: 65)

  • items_to_skip: List of items to be skipped. (default: [12, 16, 25])

  • total_item: Total item in the loop. (default: 100)

Processor Parameters:

Parameters:
  • total_item (int) – The total number of items

  • items_to_skip (list[int]) – A list of items to skip.

  • item_to_abort (int) – The item where to trigger an abort.

get_items() list[int][source]

Returns the list of items, the range from 0 to total_item.

process()[source]

Processes the item

skip_item()[source]

Add skipped item to the skipped item list.

start()[source]

Resets the skipped item container.

skipped_items: [list[int]]

A list with the skipped items.

mafw.examples.loop_modifier.is_prime(n: int) bool[source]

Check if n is a prime number.

Parameters:

n (int) – The integer number to be checked.

Returns:

True if n is a prime number. False, otherwise.

Return type:

bool