Category Archives: python 3

komega-v08e. First complete minimal version with tests

Journal: uffmm.org,
ISSN 2567-6458,Oct 14 until  Oct-15, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:gerd@doeben-henisch.de

ABSTRACT

This is the first complete minimal version together with two tests with the German language  as well as with the English language. From now on this ‘starting point’ will be improved step wise until some point in the future.

PDF Documents

PYTHON CODE MAIN FILE

This file is importing the file kcv8e

komega-v08e-main

(Last change: Oct 11, 2020)

PYTHON USED CLASSES

kcv8e-classes

This file is importing the ‘shelve’ module. The ‘shelve’ module has a different back end with linux or windows. See the remark at the end of the kcv8e file.

(Last change: Oct 15, 2020)

EXAMPLE with GERMAN language

test-komega-v08e-GermanExample1

(Last change Oct 15,2020)

EXAMPLE with ENGLISH language

test-komega-v08e-EnglishExample1

(Last change: Oct 15, 2020)

LINUX – WINDOWS10

(Last change: Oct 13,2020)

If you want to run the program komega-v08e.py which is importing kcv8e.py which in turn is importing the shelve modul under Windows 10 then you have in the file kcv8e.py the last line, where the storage objects initializes three databases:

LINUX:

st=Storage(‘STAT1′,’RULE1′,’PV1’)

WINDOWS10:

st=Storage(‘STAT1.dat’,’RULE1.dat’,’PV1.dat’)

The reason for this is that the shelve modul is using internally a db-interface which can be differently be implemented on different systems. For windows you need this special Postfix ‘.dat’ to indicate the windows-specific implementation.

VIDEOS

These videos show the generation of cases

(Protocol of first live session, see video)

GerdIstHungrig1

(Recorded: Oct 15, 2020 8:30am)

(Protocol 2nd live session, see video)

test-komega-v08e-GermanExample2

(recorded: Oct 15, 2020 11:30 am)

KOMEGA REQUIREMENTS No.4, Version 2. Basic Application Scenario

ISSN 2567-6458, 28.August 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

As described in the uffmm eJournal  the wider context of this software project is a generative theory of cultural anthropology [GCA] which is an extension of the engineering theory called Distributed Actor-Actor Interaction [DAAI]. In  the section Case Studies of the uffmm eJournal there is also a section about Python co-learning – mainly
dealing with python programming – and a section about a web-server with
Dragon. This document will be part of the Case Studies section.

PDF DOCUMENT

requirements-no4-v2-27Aug2020

STARTING WITH PYTHON3 – The very beginning – part 9

Journal: uffmm.org,
ISSN 2567-6458, July 24-25, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:gerd@doeben-henisch.de

CONTEXT

This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.

SUBJECT

In this file you will see a first encounter between the AAI paradigm (described in the theory part of this uffmm blog) and some applications of the python programming language. A simple virtual world with objects and actors can become activated with a free selectable size, amount of objects and amount of actors. In later post lots of experiments with this virtual world will be described as well as many extensions.

SOURCE CODE
Main file: vw4.py

The main file ‘vw4.py’ describes the start of a virtual world and then allows a loop to run this world n-many times.

Import file: vwmanager.py

The main file ‘vw4.py’ is using many functions to enable the process. All these functions are collected in the file ‘vwmanager.py’. This file will automatically be loaded during run time of the program vw4.py.

COMMENTS

comment-vw4

DEMO

TEST RUN AUG 19, 2919, 12:56h

gerd@Doeben-Henisch:~/code$ python3 vw4.py
Amount of information: 1 is maximum, 0 is minimum0
Number of columns (= equal to rows!) of 2D-grid ?4
[‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’]

Percentage (as integer) of obstacles in the 2D-grid?77
Percentage (as integer) of Food Objects in the 2D-grid ?44
Percentage (as integer) of Actor Objects in the 2D-grid ?15

Objects as obstacles

[0, 2, ‘O’]

[0, 3, ‘O’]

[1, 2, ‘O’]

[2, 3, ‘O’]

Objects as food

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Objects as actor

[1, 3, ‘A’, [0, 1000, 100, 500, 0]]

[3, 2, ‘A’, [1, 1000, 100, 500, 0]]

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘A’, ‘F’]

END OF PREPARATION

WORLD CYCLE STARTS

—————————————————-
Real percentage of obstacles = 25.0
Real percentage of food = 37.5
Real percentage of actors = 12.5
—————————————————-
How many CYCLES do you want?25
Singe Step = 1 or Continous = 0?1
Length of olA 2

—————————————————–

WORLD AT CYCLE = 0

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘A’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 1000, 100, 500, -1]]

[2, 1, ‘A’, [1, 1000, 100, 500, 8]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 1

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘A’]

[‘F’, ‘A’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 900, 100, 500, -1]]

[2, 1, ‘A’, [1, 900, 100, 500, 0]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 2

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘A’]

[‘F’, ‘A’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 800, 100, 500, -1]]

[1, 1, ‘A’, [1, 1300, 100, 500, 1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 500, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 3

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 700, 100, 500, -1]]

[2, 0, ‘A’, [1, 1700, 100, 500, 6]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 600, 100]]

[2, 0, ‘F’, [2, 500, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 4

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘A’]

[‘A’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 600, 100, 500, -1]]

[1, 0, ‘A’, [1, 1600, 100, 500, 1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 700, 100]]

[2, 0, ‘F’, [2, 600, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 5

[‘F’, ‘_’, ‘O’, ‘O’]

[‘A’, ‘F’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 500, 100, 500, -1]]

[1, 1, ‘A’, [1, 2000, 100, 500, 3]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 300, 100]]

[2, 0, ‘F’, [2, 700, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 6

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 400, 100, 500, -1]]

[1, 1, ‘A’, [1, 1900, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 400, 100]]

[2, 0, ‘F’, [2, 800, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 7

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 300, 100, 500, -1]]

[1, 1, ‘A’, [1, 1800, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 500, 100]]

[2, 0, ‘F’, [2, 900, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 8

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 200, 100, 500, -1]]

[1, 1, ‘A’, [1, 1700, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 600, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 2

—————————————————–

WORLD AT CYCLE = 9

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘A’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 3, ‘A’, [0, 100, 100, 500, 0]]

[1, 0, ‘A’, [1, 1600, 100, 500, 7]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 700, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 10

[‘F’, ‘_’, ‘O’, ‘O’]

[‘A’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 0, ‘A’, [1, 1500, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 800, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 11

[‘F’, ‘_’, ‘O’, ‘O’]

[‘A’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 0, ‘A’, [1, 1400, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 900, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 12

[‘F’, ‘_’, ‘O’, ‘O’]

[‘A’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 0, ‘A’, [1, 1300, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 13

[‘F’, ‘_’, ‘O’, ‘O’]

[‘A’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[2, 0, ‘A’, [1, 1700, 100, 500, 5]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 500, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 14

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘A’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 1, ‘A’, [1, 2100, 100, 500, 2]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 500, 100]]

[2, 0, ‘F’, [2, 600, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 15

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2500, 100, 500, 8]]

[0, 0, ‘F’, [0, 500, 100]]

[1, 1, ‘F’, [1, 600, 100]]

[2, 0, ‘F’, [2, 700, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 16

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2400, 100, 500, -1]]

[0, 0, ‘F’, [0, 600, 100]]

[1, 1, ‘F’, [1, 700, 100]]

[2, 0, ‘F’, [2, 800, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 17

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2300, 100, 500, -1]]

[0, 0, ‘F’, [0, 700, 100]]

[1, 1, ‘F’, [1, 800, 100]]

[2, 0, ‘F’, [2, 900, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 18

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2200, 100, 500, -1]]

[0, 0, ‘F’, [0, 800, 100]]

[1, 1, ‘F’, [1, 900, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 19

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2100, 100, 500, -1]]

[0, 0, ‘F’, [0, 900, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 20

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 2000, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 21

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 0, ‘A’, [1, 1900, 100, 500, 0]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 22

[‘A’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[0, 1, ‘A’, [1, 1800, 100, 500, 3]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 1000, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 23

[‘F’, ‘A’, ‘O’, ‘O’]

[‘_’, ‘F’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 1, ‘A’, [1, 2200, 100, 500, 5]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 500, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

Length of olA 1

—————————————————–

WORLD AT CYCLE = 24

[‘F’, ‘_’, ‘O’, ‘O’]

[‘_’, ‘A’, ‘O’, ‘_’]

[‘F’, ‘_’, ‘F’, ‘O’]

[‘F’, ‘_’, ‘_’, ‘F’]

Press key c for continuation!c
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Updated energy levels in olF and olA
[1, 1, ‘A’, [1, 2100, 100, 500, -1]]

[0, 0, ‘F’, [0, 1000, 100]]

[1, 1, ‘F’, [1, 600, 100]]

[2, 0, ‘F’, [2, 1000, 100]]

[2, 2, ‘F’, [3, 1000, 100]]

[3, 0, ‘F’, [4, 1000, 100]]

[3, 3, ‘F’, [5, 1000, 100]]

 

STARTING WITH PYTHON3 – The very beginning – part 6

Journal: uffmm.org,
ISSN 2567-6458, July 20  2019 – May 12, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:
gerd@doeben-henisch.de

CONTEXT

This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.

SUBJECT

Meanwhile I am beginning to combine elements of the python language with some applied ideas (in the last section the idea of cognitive entropy illustrated with the equalization of strings). In this section I address the idea of a simple 2-dimensional virtual world, how to represent it with python. In later sections I will use this virtual worlds for some ideas of internal representations and some kinds of learning in an artificial actor.

Remark: for a general help-information you can find a lot of helpful text directly on the python.org site: https://www.python.org/doc/.

SZENARIO

Motivation

Because we want to introduce step-wise artificial actors which can learn, show some intelligence, and can work in teams, we need a minimal virtual world to start (this virtual world is a placeholder for the real world later if applied to the real world (RW) by sensors and actors). Although there is a big difference between the real world and a virtual world a virtual world is nevertheless very helpful for introducing basic concepts. And, indeed, finally if you are applying to real world data you will not be able to do this without mathematical models which represent virtual structures. You will need lots of mappings between real and virtual and vice versa. Thus from a theoretical point of view any kind of virtual model will do, the question is only how ‘easily’ and how ‘good’ it fits.

Assumptions Virtual World (VW)

  1. A 2-dimensional world as a grid together with a world clock CLCK. The clock produces periodic events which can be used to organize a timely order.

  2. There is an x and y axis with the root (0,0) in the upper left corner

  3. A coordinate (x,y) represents a position.

  4. A position can be occupied by (i) nothing or (ii) by an object. Objects can be obstacles, energy objects (= Food), by a virtual executive actor, or even more.

  5. Only one object per position is allowed.

  6. It is assumed that there are four directions (headings): ‘North (N)’ as -Y, ‘East (E)’ as +X, ‘South (S)’ as +Y, and ‘West (W)’ as -X.

  7. Possible movements are always only in one of the main directions from one cell to the adjacent cell. Movements will be blocked by obstacle objects or actor objects.

  8. A sequence of movements realizes in the grid a path from one position to the next.

  9. The size of the assumed grids is always finite. In a strict finite world the border of the grid blocks a movement. In a finite-infinite world the borders of the grid are connected in a way that the positions in the south lead to the position in the north and the positions in the east lead to the positions in the west, and vice versa. Therefor can a path in an finite-infinite world become infinite.

  10. A grid G with its objects O will be configured at the beginning of an experiment. Without the artificial actors all objects are in a static world assumed to be permanent. In a dynamic world there can be a world function f_w inducing changes depending from the world clock.

  11. During an experiment possible changes in the world can happen through a world function f_w, if such a function has been defined. The other possible source for changes are artificial actors, which can act and which can ‘die’.

Remark: The basic idea of this kind of a virtual world I have got from a paper from S.W.Wilson from 1994 entitled ZCS: a zeroth level classifier system published in the Journal Evolutionary Computation vol. 2 number 1 pages 1-18. I have used this concept the first time in a lecture in 2012 (URL: https://www.doeben-henisch.de/fh/gbblt/node95.html). Although this concept looks at a first glance very simple, perhaps too simple, it is very powerful and allows very far reaching experiments (perhaps I can show some aspects from this in upcoming posts :-)).

ACTOR STORY
  1. There is a human executive actor as a user who uses a program as an assisting actor by an interface with inputs and outputs.

  2. The program tries to construct a 2D-virtual world in the format of a grid. The program needs the size of the grid as (m,n) = (number of columns, number of rows), which is here assumed by default as equal m=n. After the input the program will show the grid on screen.

  3. Then the program will ask for the percentage of obstacles ‘O’ and energy objects (= food) ‘F’ in the world. Randomly the grid will be filled according to these values.

  4. Then a finite number of virtual actors will be randomly inserted too. In the first version only one.

POSSIBLE EXTENSIONS

In upcoming versions the following options should be added:

  1. Allow multiple objects with free selectable strings for encoding.

  2. Allow multiple actors.

  3. Allow storage of a world specification with reloading in the beginning instead of editing.

  4. Transfer the whole world specification into an object specification. This allows the usage of different worlds in one program.

IMPLEMENTATION

vw1b.py

And with separation of support functions in an import module:

vw1c.py

gridHelper.py(The import module)

EXERCISES

m=int(input(‘Number of columns (= equal to rows!) of 2D-grid ?’))

The input() command generates as output a string object. If one wants to use as output numbers for follow-up computations one has to convert the string object into an integer object, which can be done with the int() operator.

mx=nmlist(n)

Is the call of the nmlist() function which has been defined before the main source code (see. above)(in another version all these supporting functions will again be stored as an extra import module:

printMX(mx,n)

This self-defined function assumes the existence of a matrix object mx with n=m many columns and rows. One row in the matrix can be addressed with the first index of mx like mx[i]. The ‘i’ gives the number of the row from ‘above’ starting with zero. Thus if the matrix has n-many rows then we have [0,…,n-1] as index numbers. The rows correspond to the y-axis.

mx = [[‘_’ for y in range(n)] for x in range(n)]

This expression is an example of a general programming pattern in python called list comprehension (see for example chapters 14 and 22 of the mentioned book of Mark Lutz in part 1 of this series). List comprehension has the basic idea to apply an arbitrary python expression onto an iteration like y in range(n). In the case of a numeric value n=5 the series goes from 0 to 4. Thus y takes the values from 0 to 4. In the above case we have two iterations, one for y (representing the rows) and one vor x (representing the columns). Thus this construct generates (y,x) pairs of numbers which represent virtual positions and each position is associated with a string value ‘_’. And because these instructions are enclosed in []-brackets will the result be a set of lists embedded in a list. And as you can see, it works 🙂 But I must confess that from the general idea of list comprehension to this special application is no direct way. I got this idea from the stack overflow web site (https://stackoverflow.com/questions) which offers lots of discussions around this topic.

for i in range(no):

x=rnd.randrange(n)

y=rnd.randrange(n)

mx[x][y]=obj

A simple for-loop to generate random pairs of (x,y) coordinates to place the different objects into the 2D-grid realized as a matrix object mx.

Demo

PS C:\Users\gdh\code> python vw1.py

Number of columns (= equal to rows!) of 2D-grid ?5

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

Percentage (as integer) of obstacles in the 2D-grid?45

Number of objects :

11

Position :

3 2

Position :

2 3

Position :

3 2

Position :

3 2

Position :

0 1

Position :

2 2

Position :

0 1

Position :

4 3

Position :

0 2

Position :

3 1

Position :

1 4

New Matrix :

[‘_’, ‘O’, ‘O’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘O’]

[‘_’, ‘_’, ‘O’, ‘O’, ‘_’]

[‘_’, ‘O’, ‘O’, ‘_’, ‘_’]

[‘_’, ‘_’, ‘_’, ‘O’, ‘_’]

Percentage (as integer) of Energy Objects (= Food) in the 2D-grid ?15

Number of objects :

3

Position :

3 4

Position :

0 4

Position :

4 1

New Matrix :

[‘_’, ‘O’, ‘O’, ‘_’, ‘F’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘O’]

[‘_’, ‘_’, ‘O’, ‘O’, ‘_’]

[‘_’, ‘O’, ‘O’, ‘_’, ‘F’]

[‘_’, ‘F’, ‘_’, ‘O’, ‘_’]

Default random placement of one virtual actor

Position :

2 2

New Matrix :

[‘_’, ‘O’, ‘O’, ‘_’, ‘F’]

[‘_’, ‘_’, ‘_’, ‘_’, ‘O’]

[‘_’, ‘_’, ‘A’, ‘O’, ‘_’]

[‘_’, ‘O’, ‘O’, ‘_’, ‘F’]

[‘_’, ‘F’, ‘_’, ‘O’, ‘_’]

STARTING WITH PYTHON3 – The very beginning – part 4

Journal: uffmm.org,
ISSN 2567-6458, July 15, 2019 – May 9, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:
gerd@doeben-henisch.de

Change: July 16, 2019 (Some re-arrangement of the content :-))

CONTEXT

This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.

SUBJECT

After a first clearing of the environment for python programming we have started with the structure of the python programming language, and in this section will deal with the object type string(s).

Remark: the following information about strings you can get directly from the python manuals, which you can find associated with the entry for python 3.7.3 if you press the Windows-Button, look to the list of Apps (= programs), and identify the entry for python 3.7.3. If you open the python entry by clicking you see the sub-entry python 3.7.3 Manuals. If you click on this sub-entry the python documentation will open. In this documentation you can find nearly everything you will need. For Beginners you even find a nice tutorial.

TOPIC: VALUES (OBJECTS) AS STRINGS

PROBLEM(s)

(1) When I see a single word (a string of symbols) I do not know which type this is in python. (2) If I have a statement with many words I would like to get from this a partition into all the single words for further processing.

VISION OF A SOLUTION

There is a simple software actor which can receive as input either single words or multiple words and which can respond by giving either the type of the received word or the list of the received multiple words.

ACTOR STORY (AS)

We assume a human user as executing actor (eA) and a piece of running software as an assisting actor (aA). For these both we assume the following sequence of states:

  1. The user will start the program by calling python and the name of the program.
  2. The program offers the user two options: single word or multiple words.
  3. The user has to select one of these options.
  4. After the selection the user can enter accordingly either one  or multiple words.
  5. The program will respond either with the recognized type in python or with a list of words.
  6. Finally asks the program the user whether he/she will continue or stop.
  7. Depending from the answer of the user the program will continue or stop.

IMPLEMENTATION

Here you can download the sourcecode: stringDemo1

# File stringDemo1.py
# Author: G.Doeben-Henisch
# First date: July 15, 2019

##################
# Function definition sword()

def sword(w1):
w=str(w1)
if w.islower():
print(‘Is lower\n’)
elif w.isalpha() :
print(‘Is alpha\n’)
elif w.isdecimal():
print(‘Is decimal\n’)
elif w.isascii():
print(‘Is ascii\n’)
else : print(‘Is not lower, alpha, decimal, ascii\n’)

##########################
# Main Programm

###############
# Start main loop

loop=’Y’
while loop==’Y’:

###################
# Ask for Options

opt=input(‘Single word =1 or multiple words =2\n’)

if opt==’1′:
w1=input(‘Input a single word\n’)
sword(w1) # Call for new function defined above

elif opt==’2′:
w1=input(‘Input multiple words\n’)
w2=w1.split() # Call for built-in method of class str
print(w2)

loop=input(‘To stop enter N or Y otherwise\n’) # Check whether loop shall be repeated

DEMO

Here it is assumed that the code of the python program is stored in the folder ‘code’ in my home director.

I am starting the windows power shell (PS) by clicking on the icon. Then I enter the command ‘cd code’ to enter the folder code. Then I call the python interpreter together with the demo programm ‘stringDemo1.py’:

PS C:\Users\gerd_2\code> python stringDemo1.py
Single word =1 or multiple words =2

Then I select first option ‘Single word’ with entering 1:

1
Input a single word
Abrakadabra
Is alpha

To stop enter N

After entering 1 the program asks me to enter a single word.

I am entering the fantasy word ‘Abrakadabra’.

Then the program responds with the classification ‘Is alpha’, what is correct. If I want to stop I have to enter ‘N’ otherwise it contiues.

I want o try another word, therefore I am entering ‘Y’:

Y
Single word =1 or multiple words =2

I select again ‘1’ and the new menue appears:

1
Input a single word
29282726
Is decimal

To stop enter N

I entered a sequence of digits which has been classified as ‘decimal’.

I want to contiue with ‘Y’ and entering ‘2’:

Y
Single word =1 or multiple words =2
2
Input multiple words
Hans kommt meistens zu spät
[‘Hans’, ‘kommt’, ‘meistens’, ‘zu’, ‘spät’]
To stop enter N

I have entered a German sentence with 5 words. The response of the system is to identify every single word and generate a list of the individual words.

Thus, so far, the test works fine.

COMMENTS TO THE SOURCE CODE

Before the main program a new function ‘sword()’ has been defined:

def sword(w1):

The python keyword ‘def‘ indicates that here the definition of a function  takes place, ‘sword‘ is the name of this new function, and ‘w1‘ is the input argument for this function. ‘w1’ as such is the name of a variable pointing to some memory place and the value of this variable at this place will depend from the context.

w=str(w1)

The input variable w1 is taken by the operator str and str translates the input value into a python object of type ‘string’. Thus the further operations with the object string can assume that it is a string and therefore one can apply alle the operations to the object which can be applied to strings.

if w.islower():

One of these string-specific operations is islower(). Attached to the string object ‘w’ by a dot-operator ‘.’ the operation ‘islower() will check, whether the string object ‘w’ contains lower case symbols. If yes then the following ‘print()’ operation will send this message to the output, otherwise the program continues with the next ‘elif‘ statement.

The ‘if‘ (and following the if the ‘elif‘) keyword states a condition (whether ‘w’ is of type ‘lower case symbols’). The statement closes with the ‘:’ sign. This statement can be ‘true’ or not. If it is true then the part after the ‘:’ sign will be executed (the ‘print()’ action), if false then the next condition ‘elif … :’ will be checked.

If no condition would be true then the ‘else: …’ statement would be executed.

The main program is organized as a loop which can iterate as long as the user does not stop it. This entails that the user can enter as many words or multi-words as he/ she wants.

loop=’Y’
while loop==’Y’:

In the first line the variable ‘loop’ receives as a value the string ‘Y’ (short for ‘yes’). In the next line starts the loop with the python key-word ‘while’ forming a condition statement ‘while … :’. This is similar to the condition statements above with ‘if …. :’ and ‘elif … :’.

The condition depends on the expression ‘loop == ‘Y” which means that  as long as the variable loop is logically equal == to the value ‘Y’ the loop condition  is ‘true’ and the part after the ‘:’ sign will be executed. Thus if one wants to break this loop one has to change the value of the variable ‘loop’ before the while-statement ‘while … :’ will be checked again. This check is done in the last line of the while-execution part with the input command:

loop=input(‘To stop enter N\n’)

Before the while-condition will be checked again there is this input() operator asking the user to enter a ‘N’ if he/ she wantds to stop. If the user  enters a  ‘N’  in the input line the result of his input will be stored in the variable called ‘loop’ and therefore the variable will have the value ‘==’N” which is different from ‘==’Y”. But what would happen if the user enters something different from ‘N’ and ‘Y’, because ‘Y’ is expected for repetition?

Because the user does not know that he/she has to enter ‘Y’ to continue the program will highly probably stop even if the user does not want to stop. To avoid this unwanted case one should change the code for the while-condition as follows:

while loop!=’N’:

This states that the loop will be true as long as the value of the loop variable is different != from the value ‘N’ which will explicitly asked from the user at the end of the loop.

The main part of the while-loop distinguishes two cases: single word or multiple words. This is realized by a new input() operation:

opt=input(‘Single word =1 or multiple words =2\n’)

The user can enter a ‘1’ or a ‘2’, which will be stored in the variable ‘opt’. Then a construction with an if or an elif will test which one of these both happens. Depending from the option 1 or 2 ther program asks the user again with an input() operation for the specific input (one word or multiple words).

sword(w1)

In the case of the one word input in the variable ‘w1’ w1 contains as value a string input which will be delivered as input argument to the new function ‘sword()’ (explanation see above). In case of input 2 the

w2=w1.split()

‘split()’ operation will be applied to the object ‘w1’ by the dot operator ‘.’. This operation will take every word separated by a ‘blank’ and generates a list ‘[ … ]’ with the individual words as elements.

A next possible continuation you can find HERE.

 

STARTING WITH PYTHON3 – The very beginning – part 2

Journal: uffmm.org,
ISSN 2567-6458, July 9, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:
gerd@doeben-henisch.de

CONTEXT

This is the next step in the python3 programming project. The first step can be found here. The overall context is still the python Co-Learning project.

PROGRAMMING TOOLS

First SW-tools to use for programming

In this second session we extend the overview of the possible programming tools, how they are interrelated, and how they work.

In the figure above you can see the windows 10 operating system as the root system for everything else. The win10 system communicates with the PATH-variable and uses this information for many operations. How on can edit this variable has been shown in the last session.

One can activate directly from the win10 system the power-shell with a command-line interface. Entering the right code one can activate from the power-shell either directly a python-shell for python commands or one can activate other programs like the editor ‘notepad’ or ‘notepad++’. With such editors one can edit python scripts, store them, and then run these scripts from the power-shell by calling a python-shell with these scripts as arguments (as shown in the first session).

The python shell allows the direct entering of python commands and gives immediately feedback whether it works and how. Therefore one calls this an interactive shell which is very handy to check quickly some commands and their effects.

Another tool, which we will use in this session, is the integrated script environment (IDLE). This is like the python-shell but with some additional functionalities (see below). The main usage is for editing larger python scripts with a built-in editor and for running these scripts.

THE IDLE TOOL

To use this new tool you can press the windows button to see the list of all apps (programs) available on your computer. Under ‘P’ you will find python 3.7.3 and within python you will find an entry for IDLE. By selecting this item and clicking on the right mouse-button you can select the option to attach this icon to the task bar. If it is there you can use it.

If you start the IDLE tool by clicking on the icon from the task bar it opens as a new python interactive shell with some more options.

A first thing you can do is to ask for the actual path you are in. For this you have to import the python module ‘os’ (operating system) and use the command ‘getcwd()‘ from this module. Entering ‘os.getcwd()‘ in the python command line generates the actual path as output on the next line.

>>> import os
>>> os.getcwd()
‘C:\\Users\\gerd_2\\AppData\\Local\\Programs\\Python\\Python37-32’
>>>

This reveals that the actual path is pointing to the location of the python exe module (on my pc). This is not what I want because I have created in the first session a folder with name ‘code’ in my home directory ‘\Users\gerd_2’. From inside of the IDLE tool it is not possible to change the actual path.  But python as language provides lots of options to do this. One option is described below:

The module os offers several functions. Besides the function ‘os.getcwd()’ which we have used already there is another command ‘os.chdir(pathname)‘. But to directly change the actual path one has to be cautious because the path ‘C:\\Users\gerd_2\code‘ includes the ‘\’-sign, this cannot be read directly by the os.chdir() command. You can surround this problem by using the ‘\’-sign twice: first as an ‘escape sign’ and then as the ‘object sign’, resulting in the following command format: ‘C:\\Users\\gerd_2\\code‘. Entering this nothing is given as a result, and when you repeat the question ‘os.getcwd()’ you will receive as new answer the new path. Here the dialog with the python-shell:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license()” for more information.

>>> import os

>>> os.getcwd()

‘C:\\Users\\gerd_2\\AppData\\Local\\Programs\\Python\\Python37-32’

>>> os.chdir(‘C:\\Users\\gerd_2\\code’)

>>> os.getcwd()

‘C:\\Users\\gerd_2\\code’

>>>

You can see that the python command ‘os.getcwd() has been used twice. If you want to repeat some command you can call-back the command history of the python-shell with the keystrokes ‘ALT+P‘. This recalls the past (P) of the command history.

Comment: In the command

>> os.chdir(‘C:\\Users\\gerd_2\\code’)

I have used the back-slash sign ‘\’ twice to make the string fit as argument for the ‘os.chdir()’ command. As one can learn does python allow another solution, which looks like this:

>> os.chdir(r’C:\Users\gerd_2\code’)

The solution is to use an additional ‘r’ directly before the string ‘…’ telling the python interpreter that the following string has to be understood as a raw string. This works, try it out 🙂

IDLE AND EXECUTION OF A SCRIPT

Now if we are in the target folder for my scripts we can look to all files which are in this folder actually. For this we can use the python command ‘os.listdir()’:

>>> os.listdir()

[‘savesrc.txt’, ‘script1.py’, ‘script1.pyw’, ‘script1b.py’, ‘showargs.py’, ‘threenames.py’, ‘tst1.py’, ‘what.py’, ‘what2.py’, ‘__pycache__’]

>>>

You can detect in this list the python script ‘scrpt1.py’. Entering the name of this script either with .py extension or without will not enable an execution:

>>> script1.py

Traceback (most recent call last):

File “<pyshell#6>”, line 1, in <module>

script1.py

NameError: name ‘script1’ is not defined

From the first session we know that we can start the script within the power-shell directly. For this we have to activate the powershell, have to go into the desired folder ‘code’ …

PS C:\Users\gerd_2> cd code

PS C:\Users\gerd_2\code> dir

Verzeichnis: C:\Users\gerd_2\code

Mode LastWriteTime Length Name

—- ————- —— —-

d—– 04.07.2019 19:03 __pycache__

-a—- 01.07.2019 18:44 182 savesrc.txt

-a—- 01.07.2019 18:41 92 script1.py

-a—- 24.06.2019 23:23 126 script1.pyw

-a—- 24.06.2019 22:43 128 script1b.py

-a—- 04.07.2019 18:51 56 showargs.py

-a—- 28.06.2019 00:29 162 threenames.py

-a—- 24.06.2019 21:16 120 tst1.py

-a—- 24.06.2019 22:49 126 what.py

-a—- 24.06.2019 23:56 136 what2.py

… and then we can start the python-script ‘script1.py’:

PS C:\Users\gerd_2\code> python script1.py

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

PS C:\Users\gerd_2\code>

But because we will here use the IDLE tool we proceed differently. We open the File-Menue to get the desired file script1.py:

Open file-directory for file search

Then we load  the python-script script1.py in the editor of the IDLE tool:

The text of the script

and then activate the RUN button for execution:

Activate the RUN button to execute the script

The script will then be executed and you will see the effect of the execution in the python shell. This looks the same as when you would have called the script within the power-shell calling  the python-shell.

There is still the other option to get the module running by the import command:

>>> import script1.py

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

Traceback (most recent call last):

File “<pyshell#7>”, line 1, in <module>

import script1.py

ModuleNotFoundError: No module named ‘script1.py’; ‘script1’ is not a package

>>>

The import call works, but at the same time the python-shell states some error, that ‘script1.py’ is not recognized as a true module. This has to be clarified in the next session.

One possible continuation can be found HERE.

 

 

STARTING WITH PYTHON3 – The very beginning – V2

Journal: uffmm.org,
ISSN 2567-6458, July 5, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is an update of the post ‘STARTING WITH PYTHON3 – The very beginning‘ from July-1, 2019. The original version was not ‘wrong’, but it seems, after a few days later, one can and should improve it. The main motivation for this new version is my experience as a linux programmer working with windows 10: learning by doing I detected a litle more radical approach to begin python programming with win10. This post is part of the overall  topic ‘Co-Learning with python 3‘.

OVERVIEW

In this updated version I will introduce some bits of the win10 environment which you will need for your python programming. Then I demonstrate the very first steps with some lines of code. Very important for all your own activities: in the beginning get you a helpful book to guide your start. After trying many sources I have actually the following recommendations:

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/

THE WIN10-ENVIRONMENT

When you start programming  you need a direct knowledge about your computing environment, in that case the win10 operating system. While linux is built up with an open mind and tries to make everything transparent, win10 is somehow the opposite: it is a closed box, and to work with it in direct contact with the operating system is not very well supported. Thus to work as a programmer with win10 is not what you ideally want. But now we are there and have to get the programming done.

After my first expriments with win10 I detected the following tools as very helpful to start programming: (i) The editor for the system and environment variables and (ii) the powershell. These tools allow you to define the operating system environment for your python programming  in a minimal way.

The editing of the system and environment variables you can reach by the following key-operations:

Editing System and Environment Variables

  1. Hit the windows key together with the key for ‘R’
  2. This opens a small window with a textfield-entry.
  3. In this field you enter the string ‘control’
  4. This opens a page with different topics for to manipulate some aspects of your system (because my computer is in the German-Mode I have the heading ‘Einstellungen des Computers ändern’, something like ‘Change options of the computer’).
  5. Here you select the topic ‘System and Security’
  6. Then another page opens with several topics, one is ‘System’, which you should select.
  7. On this new page you can see at the left border some more topics. The last one mentions something like ‘Extended System Options’. Select this
  8. Before you see the next page you will be asked to enter the Super-User Password. Thus you must be super-user to do this.  To be a real programmer you must have this rights to get a full understanding of what is going on.
  9. Then  the editor for system and environment variables pops up:
Editor for system and enviroment variables

 

  1. The interesting button is in the right down corner, in German: Umgebungsvariablen (‘environment variables’)
  2. Click this button and you will finally see the editor you are looking for:
Finally the editor to edit private and environment variables
  1. For the upcoming actions we need only the path-variable. One can highlight the Path-entry and select it with ‘Editing’ (German: ‘Bearbeiten’):
The window showing all the entries of the path-variable.
  1. Now you can see all the entries of the path variable nicely ordered one after the  other. This is a special service because the path-variable as such (as you will see later when using the power-shell) is one string. Here this string is splitted up into the different sub-strings. This allow you to add some new string or — also very important — to delete a substring. As You can see in the figure there is as last line an entry including the name Python . This line is the whole directory path on the drive C:\ where my actual python version is stored.

CLEAN UP BEFORE

Now, if You know where and how you can edit the basic system variables it can be a good idea to clean up the whole system with regard to older python installations. In my case I had tried  before — as a complete newbie — several python distributions like WinPython and Anaconda in different versions at different locations in the system, this accompanied with different installations of the python language. You can imagine that this caused finally some confusion what is where and what is causing which  effect.

Thus I decided to start from scratch, removing all the old stuff to get a ‘point zero’ for beginning.

The first step was to use the win10 explorer program with the find option in the small text-field up right.

Part of the win10-explorer window showing a find-operation on the whole PC

After you have entered your search-string, in this case ‘python’, you have to say (left up corner) the region, in which you want the search to happen. I have selected the whole PC.

After some while you see all locations as a complete path where the search-string python could be found. When I started my clean-up action there where lots of locations with different python installations, now there is only one left, which I am actually using.

But caution: If you want to delete the old installations you have (i) to look to the uninstall options/ programs to activate these to remove all stuff; (ii) finally you have to enter the editor for system and environment variables (see above) to delete all entries there, which are now obsolet.

GET YOUR PYTHON

Now, if you have set everything with Python to zero get your python version from the python home page: https://www.python.org/.

This page is a bit confusing. Stay cool. You can detect a field with the header Downloads and a hint to the latest version. In this case it shows 3.7.3. Click this.

Python Home Page for Downloads

The next page is completely confusing. The interesting part of this page is somewhere in the middle listing the different download files:

List of download files

We are interested in the 32-Bit version although I am working on a 64-Bit machine. For the learning the 32-Bit version is more flexibel and completely sufficient.

If you click the windows installer for python (second line from below) then a download notice will be shown asking for confirmation:

Confirm download of python installer

Select ‘datei speichern’ (download file) and the installer will be  loaded in your download directory.

In the list of files you can also see   a colum with the MD5-Checksum. This is an offer to check whether the file you have downloaded is indeed not corrupted in some way (viruses etc.) To use the MD5-Checksum code You need a MD5-software tool. I found a good description at this site: https://www.it-administrator.de/downloads/software/197801/, which points you to the following product page: https://www.novirusthanks.org/products/md5-checksum-tool/.

This looks quite OK. But if you want to apply it you have to surround some difficulties when you try to search for your file to check with the directory browser. In my case it shows the directory of the super-user first. To get my python download file I have to select the main drive C:\, there I have to select from Users my actual role as user gdh, and then I can find my download folder with the python exe file to be checked. From the web site with the files list I have copied the MD5-Checksum:

MD5-checksum tool at work

Luckily the check looks good. This encourages to install python 3.7.3 by clicking on the python installer.

Python Installer Window

Because I have already installe python I can only modify it or repair or uninstall. If you click it the first time you will be able to select the option Install. In that case it will propose a directory and it will install python. Before You say Yes You can also click the flag ‘Set Path Variable‘. I did this but the interesting point is that it had no effect when using the power shell (see below). Only when I added the path again with the aid of the powershell it took effect.

USING THE POWERSHELL

To get the powershell you have to press the windows button, then a list of Apps (programs) appears. Under the letter ‘W’ you see the windows powershell. Clicking this topic you will detect several versions of the power shell: 64-Bit and 32-Bit (indicated by (x86)), and the old standard version as well as a more enhanced version called integrated script environment (ISE).

I have started with the old, simple standard version. By right-clicking with your mouse you can add the option to attach the powershell to the task bar which makes it easy to call it up again later.

Section of my task bar including icons for normal powershell (left) and integrated script environment (right)

If you click on the powershell icon the powershell window will open:

Powershell window with the command python entered

If you would confirm this command by pressing the enter key — and you would not have set the path variable with the path of the python executing modul python.exe before — then there would appear a message that the command ‘python’ is not known (I cannot demonstrate it here because I have set the path variable on account of this message meanwhile).

There exists a workaround without setting the path variable explicitly by give the whole path of the python modul, like this:

Powershell calling python with explicit path

You see, this works fine. To make life easier I have set the environment path variable with this python execution path. Then you can enter the command python alone and a python environement opens up.

But, wait a moment. Setting the path variable with the editor for the environment variable alone (see above) this has no effect for the powershell! Also the powershell can show the content of the path-variable correctly:

Powershell showing content of path-variable

(This is the version of the path variable after I have added the path with the powershell too!)

To add the path variable for the python modul explicitly in the powershell you can enter the following  command:

PS C:\Users\gdh> $Env:PATH +=”;C:\Users\gdh\AppData\Local\Programs\Python\Python37-32″

When I did this everything worked fine. Don’t ask, why it didn’t work before with the editing of the environment variable PATH alone.  This is at a first glance like ‘Software mystik’. With much more time for research there exists perhaps an explanation. My first guess would be, that the communication between the powershell and the environment variable has some hidden factors. For more very detailed explanations about the powershell and the editing of the environment variable you can find a good document from microsoft here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-6.

PROGRAMMING PYTHON

For first programming steps look to the first version of this post beginning at the header FIRST PROGRAMMING STEPS somewhere in the middle of the page.

More coding will follow soon.

STARTING WITH PYTHON 3 – The very beginning

eJournal: uffmm.org,
ISSN 2567-6458, July 1, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

The idea is to give some advice to start with python 3 programming. In other posts I have used an integrated package using WinPython including the integrated  development package  spyder. While such an integrated package offers lots of nice tools it hides the basic structures of your system and of the language. Therefore I decided to start again at a more basic level using only the python language and the windows command shell (I myself are working with an ubuntu system for the normal python programming and for the work with a python web-framework called ‘django’). Because I myself am not too much experienced with the windows 10 environment it challenges me to investigate the win10 environment to some degree.

GETTING STARTED FROM SCRATCH: DOWNLOAD PYTHON

To get a fresh start from scratch I have removed all python stuff from my machine.  Then I have downloadad the newest pythion version for windows from https://www.python.org/. This is python version 3.7.3 in the 32-Bit version. There exists also a 64-Bit version, but because not all the different modules are already prepared for the 64-Bit version the 32-Bit version is more flexible. To learn python this is more than enough. To switch to a 64-Bit version would be simple.

LOCAL WINDOWS ENVIRONMENT

On my machine the installation path is:

C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32

To test whether python works on your windows machine you can activate the Windows Power Shell. You will find this shell by clicking on the Windows Icon in the left corner of your laptop which opens the list of all Windows-Apps (programs).  At the end of this list there are some windows-Apps including the Windows-Power-Shell. I took the first one. By clicking on the right mouse-button I selected to attach the power-shell on the task bar. This allows me to click once on the icon of the power shell and the power shell will open.

Typing the command ‘python’ the first time into the command line will cause an error message, because Windows does not know where to find the command ‘python’. You can do two things: (i) enter the whole path for python or (ii) inform the PATH-variable about the python path.

Enter the whole path worked out:

PS C:\Users\gerd_2> C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32\python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>

Because this procedure is a bit cumbersome I tried the other proposal, to edit the PATH-variable. One proposal (from the community) goes like this:

  1. Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
  2. Click the “Environment Variables…” button.
  3. Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
  4. The “Edit environment variable” UI will appear.

I could edit the PATH-variable this way, but it showed no effect. Therefore I tried another procedure, where one can modify the PATH-variable directly from the power-shell. The needed command has the following format:

$Env:PATH += “;Wanted Path”

This addresses the PATH variable, let the actual content as it is and adds after a semicolon the needed path. With the command

$Env:PATH

you can check the actual values of the variable PATH and after adding your new path

PS C:\Users\gerd_2> $Env:PATH +=”;C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32″

you can check again. It worked, I got my new path added to  the variable. But, even more important, now it worked. Entering in the power shell only ‘python’ it worked:

PS C:\Users\gerd_2> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

I also tried the ‘help-option’ like this:

>>> help()

Welcome to Python 3.7’s help utility!

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type “quit”.

To get a list of available modules, keywords, symbols, or topics, type

“modules”, “keywords”, “symbols”, or “topics”. Each module also comes

with a one-line summary of what it does; to list the modules whose name

or summary contain a given string such as “spam”, type “modules spam”.

I tried e.g.  the ‘modules option:

help> modules

Please wait a moment while I gather a list of all available modules…

__future__ _tracemalloc glob secrets

_abc _warnings gzip select

_ast _weakref hashlib selectors

_asyncio _weakrefset heapq setuptools

From this I selected the  ‘socket’-option:

help> socket

Help on module socket:

NAME

socket

DESCRIPTION

This module provides socket operations and some related functions.

On Unix, it supports IP (Internet Protocol) and Unix domain sockets.

On other systems, it only supports IP. Functions specific for a

socket are available as methods of the socket object.

Functions:

socket() — create a new socket object

socketpair() — create a pair of new socket objects [*]

fromfd() — create a socket object from an open file descriptor [*]

fromshare() — create a socket object from data received from socket.share() [*]

Thus this function works and looks promising.

FIRST PROGRAMMING STEPS

The main idea of using python is to be able to write some program code which then the machine can run. The basic processing steps are

  1. Start python in the power shell by typing  ‘python’
  2. Enter some python code you want to test.
  3. If you want to edit many python commands together then take a text editor and write some text which looks like python code.
  4. The text you enter directly into a python command line or a edited text in a file which you can load both will be handed out to the python interpreter which translate these pyton commands into a byte code which in turn will be run by the python virtual machine (PVM).

It is helpful befor you start programming to generate at least one folder where you will store your python programs. Following the proposal from Mark Lutz (see below references) I generate in my main directory a folder called ‘code’:

PS C:\> cd $HOME

PS C:\Users\gerd_2> mkdir code

Verzeichnis: C:\Users\gerd_2

Mode LastWriteTime Length Name

—- ————- —— —-

d—– 01.07.2019 18:40 code

To edit a first simple program with name ‘script1.py’ I have called the notepad editor in the power shell like this:

PS C:\Users\gerd_2\code> notepad script1.py

In the editor I have typed the following simple text:

# FILE: script1.py

import sys

print(sys.platform)

print(2**100)

x=’python’

print(x*8)

Then I have stored this file in the ‘code’ folder. Wih the command ‘dir’ one can have a look into the ‘code’ folder:

PS C:\Users\gerd_2\code> dir

Verzeichnis: C:\Users\gerd_2\code

Mode LastWriteTime Length Name

—- ————- —— —-

-a—- 01.07.2019 18:41 92 script1.py

To check what happens when I load this script with the python interpreter one can enter the following command:

PS C:\Users\gerd_2\code> python script1.py

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

STREAM REDIRECTION

One can also redirect the output of the python interpreter from the console into a file:

PS C:\Users\gerd_2\code> python script1.py >savesrc.txt

Then one can again call an editor like notepad to read the content of this file:

PS C:\Users\gerd_2\code> notepad savesrc.txt

CONTENT savesrc.txt:

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

MODULES

Like many other programming language python organizes larger programs by putting together  smaller programs like building blocks. These blocks are called modules and this strategy  can only work if one follows some rules. Mark Lutz formulates it as follows:

“… a module is mostly just a package of variable names, known as a namespace, and the names within that package are called attributes. An attribute is simply a variable name that is attached to a specific object (like a module).” (Lutz, Mark. Learning Python (S.70). O’Reilly Media. Kindle-Version)

One possibility to fetch a module is to use the import command, either directly in a python console or indirectly as command in a python program text. Thus with going direct to the console:

PS C:\Users\gerd_2\code> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> os.getcwd()
‘C:\\Users\\gerd_2\\code’

Here the module ‘os’ has been imported and within tis module exists a function ‘getcwd()’ which fetches the actual path you are in. In my case my home directory and there the folder ‘code’. Doing an import with the new file ‘script1.py’ it works like this:

PS C:\Users\gerd_2\code> python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import script1

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

Thus the import also causes a run of the script.

If one wants to know which kinds of names are used in the module one can use the command dir():

>>> dir(script1)

[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘sys’, ‘x’]

TO BE CONTINUED…

Go back to the main page.

SOME HELPFUL REFERENCES

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/

Example python3: popShow0 – simple file-reader

eJournal: uffmm.org,
ISSN 2567-6458, 6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 5th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: popShow0.py’). This program is a continuation of the program pop0e.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORT

In this program the following two libraries are used:

import numpy as np # Lib for math
from tkinter.filedialog import askopenfilename

numpy is known from previous programs while tkinter is here used to enable a file dialog to find a certain file while browing the different folders.

The program opens such a windows for browsing:

print(‘A window is asking you for a filename\n’)
infilename = askopenfilename()

The variable ‘infilename’ is a variable for strings, which in this case saves the content of the file.  This content looks like this:

# br=0.03,dr=0.019
# X-Column, Y-Column
1.000000000000000000e+00   7.466964000000000000e+06
2.000000000000000000e+00   7.549100604000000283e+06
3.000000000000000000e+00   7.632140710644000210e+06
4.000000000000000000e+00   7.716094258461084217e+06
5.000000000000000000e+00   7.800971295304155909e+06
6.000000000000000000e+00   7.886781979552501813e+06
7.000000000000000000e+00   7.973536581327578984e+06
8.000000000000000000e+00   8.061245483722181991e+06
9.000000000000000000e+00   8.149919184043126181e+06
1.000000000000000000e+01    8.239568295067600906e+06
1.100000000000000000e+01    8.330203546313344501e+06
1.200000000000000000e+01    8.421835785322790965e+06
1.300000000000000000e+01    8.514475978961341083e+06
1.400000000000000000e+01    8.608135214729916304e+06
1.500000000000000000e+01    8.702824702091945335e+06
1.600000000000000000e+01    8.798555773814957589e+06

This context will then be formatted by the following lines:

data = np.loadtxt(infilename)
x = data[:, 0]
y = data[:, 1]

The leading header will automatically be discarded and the main content will be stored in two columns. These formatted two-columns data will then be printed with:

for i in range(len(x)):
print(‘Year %d = Citizens. %9.0f \n’ % (x[i],y[i]))

For each of the values to print x[i] and y[i] there are formatting options telling that the x[i] represents a ‘year’ understood as an integer, and  that y[i] represents the population number Citizens understood as a floting point number with zero signs behind the floating point.

The output looks then like this (Date are from the UN for 2016; the simulation computes this into a possible future):

Year 1 = Citizens. 7466964

Year 2 = Citizens. 7549101

Year 3 = Citizens. 7632141

Year 4 = Citizens. 7716094

Year 5 = Citizens. 7800971

Year 6 = Citizens. 7886782

Year 7 = Citizens. 7973537

Year 8 = Citizens. 8061245

Year 9 = Citizens. 8149919

Year 10 = Citizens. 8239568

Year 11 = Citizens. 8330204

Year 12 = Citizens. 8421836

Year 13 = Citizens. 8514476

Year 14 = Citizens. 8608135

Year 15 = Citizens. 8702825

Year 16 = Citizens. 8798556

What is missing here is the information about the ‘real years’ as 1 = 2016 etc.

SOURCE CODE for popShow0.py

popShow0.py as popShow0.pdf

Example python3: pop0e – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 4-6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 4th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0e.py’). This program is a continuation of the program pop0d.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORTS

In the new version one more liibrary is used for the handling of time stamps:

import time # Lib for time

STORING DATA IN A FILE

The only extension in the new version of the small program are some lines enabling the storage of the data from the simulation in a file.

data = np.column_stack((x,pop))

This line is formating the plot-values as x and x axes written as two columns bedides each other in a file.

What comes next is a construction of a file name which includes the actual time as well as the values of the br and the dr variable:

ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)

After this construction a file is generated with the plotting data as well as an expressive name.

np.savetxt(fname+’.txt’, data,header=header)

SOURCE CODE

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
Created on Thu 4-6 April Konga (Sweden) 2019

@author: gerd doeben-henisch
Email: gerd@doeben-henisch.de
“””

##################################
# pop0e()
###################################
#
# IDEA
#
# Simple program to compute the increase/ decrease of a population with
# the parameters population number (p), birth-rate (br) and death-rate (dr).
# In this version an extension with the following features:
# – a loop to repeat the computation for n-many cycles
# – a storage of the data in an array
# – an additional automatic storage of the plotting data in a file
# with the actual time in the file-name
# – a plot of the stored data for n-many cycles
# – the overall change of the population in %

#########################################################
# IMPORTS
# As part of the distribution of Winpython there are already many
# libraries pre-installed, which can be activated by the import command.
# Other libraries outside of the distribution have to be downloaded
# with the pip command
# Lib for plotting

import matplotlib.pyplot as plt # Lib for plotting
import numpy as np # Lib for math
import time # Lib for time

###########################################################
# DEFINITION

def pop0(p,br,dr):

p=p+(p*br)-(p*dr)

return p

###################################
# INPUT OF DATA

p = int(input(‘Population number ? ‘))

br = float(input(‘Birthrate in % ? ‘))
br = br/100

dr = float(input(‘Deathrate in % ? ‘))
dr = dr/100

n = int(input(‘How many cycles ? ‘))

baseYear = int(input(‘What is your Base Year ? ‘))

#############################################
# GLOBAL VARIABLES

pop = [] # storage for the pop-numbers for plotting
pop.append(p)

#################################################
# COMPUTE

for i in range(n):
p=pop0(p,br,dr)
pop.append(p)

##################################################
# SHOW RESULTS

for i in range(n+1):
print(‘Year %5d = Citizens. %8d \n’ %(baseYear+i, pop[i]) )

x = np.linspace(1,len(pop),len(pop))

plt.plot(x, pop, ‘bo’)
plt.show()

#####################################################
# STORE VALUES ON DISK
#
# For this see the online article
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Saves the plot data automatically in a file with a header and two columns

data = np.column_stack((x,pop))
ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)
np.savetxt(fname+’.txt’, data,header=header)

###########################################
# Compute Change of POP

n1=pop[0]
n2=pop[len(pop)-1]
Increase=(n2-n1)/(n1/100)

print(“From Year %5d, until Year %5d, a change of %2.2f percent \n” % (baseYear, baseYear+n,Increase) )

plt.close()

####################################################
# REAL DATA
#
# UN Demographic Yearbook 2017
# https://unstats.un.org/unsd/demographic-social/products/dyb/dybsets/2017.pdf
#
# Basic Tables UN
# https://unstats.un.org/unsd/demographic-social/products/vitstats/seratab1.pdf
#
# UN public tables
# http://data.un.org/Explorer.aspx?d=POP
#
# UN Rate of population change
# http://data.un.org/Data.aspx?d=PopDiv&f=variableID%3a47
# https://www.un.org/en/development/desa/population/index.asp
”’
Population number ? 6958169

Birthrate in % ? 1.9

Deathrate in % ? 0.77

How many cycles ? 15

What is your Base Year ? 2010
Year 2010 = Citizens. 6958169

Year 2011 = Citizens. 7036796

Year 2012 = Citizens. 7116312

Year 2013 = Citizens. 7196726

Year 2014 = Citizens. 7278049

Year 2015 = Citizens. 7360291

Year 2016 = Citizens. 7443462

Year 2017 = Citizens. 7527573

Year 2018 = Citizens. 7612635

Year 2019 = Citizens. 7698658

Year 2020 = Citizens. 7785653

Year 2021 = Citizens. 7873630

Year 2022 = Citizens. 7962602

Year 2023 = Citizens. 8052580

Year 2024 = Citizens. 8143574

Year 2025 = Citizens. 8235596

From Year 2010, until Year 2025, a change of 18.36 percent

Real UN data for 2010 – 2015
2010 2011 2012 2013 2014 2015
6 958 169 7 043 009 7 128 177 7 213 426 7 298 453 7.383.009

########################################################
# STORING DATA
#
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Example of saved file:
file name:
hxyPTLSun-Apr–7-08-55-46-2019br=0.019-dr=0.0077.txt

# br=0.019,dr=0.0077
# X-Column, Y-Column
1.000000000000000000e+00 6.958169000000000000e+06
2.000000000000000000e+00 7.036796309700000100e+06
3.000000000000000000e+00 7.116312107999609783e+06
4.000000000000000000e+00 7.196726434820005670e+06
5.000000000000000000e+00 7.278049443533471785e+06
6.000000000000000000e+00 7.360291402245399542e+06
7.000000000000000000e+00 7.443462695090772584e+06
8.000000000000000000e+00 7.527573823545298539e+06
9.000000000000000000e+00 7.612635407751359977e+06
1.000000000000000000e+01 7.698658187858950347e+06
1.100000000000000000e+01 7.785653025381756946e+06
1.200000000000000000e+01 7.873630904568570666e+06
1.300000000000000000e+01 7.962602933790194802e+06
1.400000000000000000e+01 8.052580346942024305e+06
1.500000000000000000e+01 8.143574504862469621e+06
1.600000000000000000e+01 8.235596896767416038e+06
”’

 

 

Example python3: pop0d – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 2-4.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 3rd step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0d.py’). This program is a continuation of the program pop0.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORTS

In the new version two libraries are used:

import matplotlib.pyplot as plt # Lib for plotting
import numpy as np # Lib for math

This extends the set of possible functions by functions for plotting and some more math.

MORE INPUT DATA

In this version one can enter a base year, thus allowing a direct relation to real year numbers in the history or the future. In the example run at the end of the program I am using the official population numbers of the UN for the world population in the years 2016 and 2017, which will hypothetically be forecasted for 15 years.

MORE GLOBAL VARIABLES

As explained in the previous version there are local variables restricted in their meaning to a certain function and global variables outside a function. In this version a datastructure called pop is introduced to store information in a sequential order. In this case the first population number given in the variable p is stored on the first position by the append operation which is part of the data structure pop.

pop = [] # storage for the pop-numbers for plotting
pop.append(p)

Later a data structure x is needed  a s a sequence of consecutive numbers starting with 1, ending with the number of entries in the pop data strucure and with as many positions as pop has entries. The number of elements in pop can be computed by applying the len() operator to pop.

x = np.linspace(1,len(pop),len(pop))

EXTENDED COMPUTATION

The computation has extended a little bit by the new line appending the actual value of p to the pop storage:

for i in range(n):
       p=pop0(p,br,dr)
      pop.append(p)

Thus the pop data structure stores every new population value in p in the sequential order of its occurence. Moreover it has been here realized a loop with the for function. The variable i is running through a sequence of values provided by the range() function. This function builts an array of numbers from 0 to n-1 and repeats therefor the call of pop0() n times.

But the most important point here is that the value of the global variable p is handed over to the function pop0(), and the local value of the variable p inside the pop0() function is again handed over by the return command to the outside of the function. Outside there is waiting the global p variable which is receiving the new value by the =-operation. In the next call of pop0() pop0() receives as new input the global variable p with the new value.

SHOWING THE RESULTS

There are now three different data show actions: (i) the list of all years with their numbers, (ii) a graphical plot of data points, and (iii) a print out of the increase of the population compared the final result with the base year.

LIST OF ALL YEARS

for i in range(n+1):
print(‘Year %5d = Citizens. %8d \n’ %(baseYear+i, pop[i]) )

Again is the for function in action ranging through the number of cycles given by the variable n. The number of the population in the different years is catched from the pop data structure by indexing the individual elements of pop by the bracket command []. pop[i] represents the i-th element of pop.

PLOTING THE POPULATION VALUES

x = np.linspace(1,len(pop),len(pop))
plt.plot(x, pop, ‘bo’)
plt.show()

The plotfunction plot of the library plt needs an arry of numbers for the x-axis given here by the x data structure, an arry of numbers for the y-axis given here by the pop-data structures, and optionally some parameter for the format of the plot symbolds. Here with ‘bo’ a black small circle. After all values are prepared will the command plt.show() make the plotted data visible.

Plot with the UN data for the possible growth of the world population
Plot with the UN data for the possible growth of the world population

OVERALL PERCENTAGE OF CHANGE

n1=pop[0]
n2=pop[len(pop)-1]
Increase=(n2-n1)/(n1/100)

print(“From Year %5d, until Year %5d, a change of %2.2f percent \n” % (baseYear, baseYear+n,Increase) )

Getting the base year from pop[0] and the last year from pop[len(pop)-1] one can compute the difference as the increase translated into a percentage.

REAL DATA

Although the population program is still very simple it is usefull to compute real numbers of the real world. One example are the official data of the United Nations (UN) which are collecting world wide data since their foundation 1948.

But, as a first surprise, although the UN provides lots of data from all the countries world wide, they do not systematicall a birthrate (br) or a deathrate (dr). Thus I have compiled the br and dr by inferring it from absolute population numbers from 2016 and 2017 combined with the fertility rate for 1000 people in the year 2013 averaged over all countries. This gives an estimate of 3% for the birthrate br. Using this from 2016 to 2017 this gives an ‘overshoot’ to the real numbers of 2017. I inferred from this the deathrate dr which is clearly a very week inference. Nevertheless it works for 2016 to 2017 and gives a first simple example for the upcoming years.

SOURCE CODE OF pop0d.py

pop0d.py as pop0d.pdf

Example python3: pop0 – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 2-3.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 2nd step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit a first simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0.py’.

BASIC PROGRAMMING ELEMENTS

SOURCE FILE

The source code is stored under the name ‘pop0.py’. It is a ‘stand alone’ program not making use of any kind of a library except the built in functions of python3. The external libraries can be included by the ‘import command’.

FUNCTION DEFINITION

If one wants to use the built-in functions in some new way one can do this by telling the computer the keyword ‘def‘ which states that the following text defines a new function.

A function has always a name, some input arguments between rounded brackets followed by a colon marking the beginning of the function-body.  After the colon follows a list of built-in commands or some already defined functions. With defined functions one can make life much easier. Instead of repeating all the commands of the function-body again and again one can limit the writing to the call of the function name with its input arguments.

In the example file we have the following function definition:

def pop0(p,br,dr):
       p=p+(p*br)-(p*dr)
       return p

The name is pop0, the input arguments are (p,br,dr), the used built-in functions are +, -, *, return as well as the =-sign, and the new composition is p=p+(p*br)-(p*dr). This new composition combines known function names with new variable names to compute a certain mathematical mapping. The result of this simple mapping is stored in the variable ‘p’ and it is delivered to the outside of the function pop0 by the return-statement.

NECESSARY INPUT VALUES

To run the program one has to call the defined new function. But because the called function will need some values for the input variables one has first to enable the user to interact with the program by some input commands.

The input commands are informing the user which kind of information is asked for and the answers of the users will be stored in the variables p, br, and dr. The input values can also be ‘casted‘ into different value types like int — for integer — and float — for floating point –.

Here is the protocol of a possible input:

Number of citizens in the start year? 1000

Birthrate %? 0.82

Deathrate in %? 0.92

CALLING A DEFINED FUNCTION

After these preparations one can call the defined new function with the statement pnew = pop0(p,br,dr). Because the input variables have values received from the user the new function can start it’s mapping and can compute the follow up value for the population.

SHOW RESULTS

To show the user this new value explicitly on the screen  one has to use the print function, the counterpart to the input function:  print(‘New population number:\n’,int(pnew)). The print function prints the new value for the population number on the screen.  If you look closer to the print function you can detect some inherent structure: print() is the main structure with the function name ‘print’ and the brackets () as the placeholder for possible input arguments. In the used example the input arguments have two ‘parts’: (‘…’,v). The ‘…’-part allows some text which will be shown to the user, in our case New population number: followed by a line break caused by the symbols \n. The v-part allows the names of variables which have some values which can be printed. In the used example we have the expression int(pnew). pnew is the name of a variable with a value delivered by the pop0() function enabled by the return p function of the pop0() function. (Attention: the ‘return’ function works without ()-brackets to receive input arguments! The variable ‘p’ is an input argument) The value delivered by ‘return p’ is a floating point value. But because we have only ‘whole citizens’ we cast the non-integer parts of the float value away by making the variable ‘pnew’ an argument of another function int(). The int() function translates a floating point value into an integer value. This is the reason that we do not see ‘999.0’ but ‘999’:

New population number:
999

REMARK: Global and Local Variables

This simple example tells already something about the difference between global and local values. If one enters the variable names ‘p’ and ‘pnew’ in the python console of the spyder editor then one can see the following:

p
Out[5]: 1000

pnew
Out[6]: 999.0

After the function call to pop0() the original value of ‘p’ is unchanged, and the new value ‘pnew’ is different. That means the new value of ‘p internal in the function’ and the ‘old value of p external to the function’ are separated. The name of a variable has therefore to be distinguished with regard to the actual context: The same name  in different contexts (inside a function definition or outside) does represents different memory spaces.   The variable names inside a function definition are called local variables and the variable names external to a function definition are called global variables.

A continuation of this post you can find here.

SOURCE CODE: EXAMPLE: pop0.py

pop0.py as pop0.pdf

Co-Learning with python 3

 

eJournal: uffmm.org, ISSN 2567-6458,
First: April 1, 2019
Last: October 21, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This python programming section is part of the uffmm science blog..

OBJECTIVE

The context of this small initiative ist the Distributed-Actor-Actor Interaction (DAAI) paradigm described in this blog.  For this AAI paradigm one will need an assisting software to manage real problems with real people. In principle this software can be realized by every kind of programming language. Which one will be used for the planned overall software service will be decided by those groups, which will do the final job. But for the development of the ideas, for an open learning process, we will use here the programming language python 3. To get a first understanding of the main programming languages and the special advantages and disadvantages of python 3 compared to python 2 and to other important languages see the short overview here 2019-03-18 in apenwarr or chapters 1-2 from the excellent book by Lutz mentioned below.

VISION

There are many gifted young people around who could produce wonderfull programms, if they would find a ‘start’, how to begin, where to go from here… sometimes these are friends doing other things together — like playing computer games 🙂 — why not start doing programming real programs by themselves? Why not start together some exciting project by their own?

If they want to do it, they need some first steps to enter the scene. This is the idea behind this vision: getting young people to do first steps, helping each other, document the process, improve, making nice things…

CONTRIBUTING POSTS

Here is a list of posts which can support the Co-Learning Process.

The Komega SW-Project

See for details the ‘Case Studies Section‘ of this blog.

The most recent post is on top.

GENERAL REMARK

This code is completely experimental! The only intention is to show how  the ideas of the theory  are working, because for most people the theory is too complex to be easily understood. That’s normal life: as complexity and innovation is raising the understanding is going down …

New Series, from scratch

(The most recent post is on top)

    • STARTING WITH PYTHON3 – The very beginning – part10: Using the git system: For the further development of our first demo we have started a simple server with a git system. All the upcoming work will use this. Attention: the server ha changed! Look for a new presence in a few weeks under the address epolitics4you.org (Last change June-21, 2020)
    • STARTING WITH PYTHON3 – The very beginning – part 9: In part 9 the concept of a simple virtual world has been re-analyzed with the conceptual framework of the AI paradigm thereby enabling a first encounter of the AAI paradigm with the python programming language. It is a complete, working program, nevertheless still simple. In the next posts this will be further extended.
    • STARTING WITH PYTHON3 – The very beginning – part 8: In part 8 the world cycle is started. In this post it is described how one can manage the energy level of all important objects. In case of the actors these will be removed from the grid and from the actor list when they reach energy level <1. Thus, there is real death in this simple world. To survive you must be able to survive. This points to the mystery of life in this universe…
    • STARTING WITH PYTHON3 – The very beginning – part 7: Continuation of editing the 2D virtual world as a grid. A major improvement is the automatic collection of all objects according to their types from the grid as well as the automatic association with individual object values including an individual ID. Thus with only one line one can manage the layout of a whole world filled with individual objects and actors… you will see.
    • STARTING WITH PYTHON3 – The very beginning – part 6: Editing a 2-dimensional virtual world in a grid format for further experiments. Yes, from this post onward I start to develop a complete virtual world (2D) with actors having an increasing amount of artificial intelligence (AI) built in, not only machine learning (ML), but also more and more a cognitive architecture (CA) enabling real semiotic machines (SM).
    • STARTING WITH PYTHON3 – The very beginning – part 5: More strings, sequences, control-statements exemplified with a simple input-output actor which shows some creativity… and some glimpse of cognitive entropy.
    • STARTING WITH PYTHON3 – The very beginning – part 4: Introducing strings and some operations with them. The examples are embedded in a small python program simulating a simple input-output actor which is completely deterministic.
    • STATING WITH PYTHON3 – The very beginning – part 3: Introducing first data types and operations with them. From this post onwards I am using only the windows powershell, an ordinary editor (notepad or notepad++), and python activated from the windows powershell. This is all you need :-). By the way: transferring the used files to Linux — I am using the distribution ubuntu 18.04 LTS — you can work directly without any changes. Instead of the windows powershell you have in Linux your standard systems terminal, calling python you can distinguish between ‘python’ (2.7) ‘python 3’ (3.6), and there are many good editors (I am using jPad, but there are many more). That’s it. Thus you can switch between Linux and windows 10 without any problems.
    • STARTING WITH PYTHON3 – The very beginning – part 2: Continuing with the additional option to use the IDLE tool. … and some more little things.
    • STARTING WITH PYTHON3 – The very beginning – part 1-V2 : This post is a re-writing of the first version for the introductory part to the windows 10 environment. The python coding examples have not been changed. The first version STARTING WITH PYTHON 3 – The very beginning:  is still available.
Older Series, using WinPython with Spyder

More posts will follow in a random order depending of the questions which will arise or the ideas someone wants to test.

REFERENCES

There is a huge amount of python books, articles and online resources. I will mention here only some of the books and resources I have used. The following is therefore neither complete nor closed. I will add occasionally some titles.

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • Mark Lutz, Programming Python, 2011, 4th ed., Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/

 

Python Program Example: Simple Population Simulation

eJournal: uffmm.org, ISSN 2567-6458, 30.Dec 2018
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

In a preceding post I have described a simple way to install the python software as part of a integrated development environment. In this post I show a simple program to simulate the increase/ decrease of a population with nearly no parameters. It can be used as a starting point for further discussions and developments.

HOW TO MAKE IT

Of one has installed (in case of windows) the winpython software as described above and one has selected the ‘spyder.exe’ module from the folder of the winpython software) either directly (by double clicking) or one clicks the icon on the task bar (which one has placed there before), then one has the spyder working environment on the screen.

spyder software screen appearance
spyder software screen appearance

In the left subscreen one can now edit the program (by copy th source code below and paste it into the window) and then one can test the software by clicking on the green run button (alternatively: pressing F5).

Then the python console will be activated in the sub-window in the lower right corner. One has to enter the required values. After the input the console window will show the numbers as well as the graph.

THE PROGRAM SOURCE CODE

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
Created on Wed Jan 2 19:34:43 2019

@author: gerd doeben-henisch
Email: gerd@doeben-henisch.de
“””

##################################
# pop1()
###################################
#
# IDEA
#
# Simple program to compute the increase/ decrease of a population with
# the parameters population number (p), birth-rate (br), death-rate (dr),
# mirgration Plus (migrPlus), and migration Minus (migrMinus)
#

#######################################
# Used modules

import matplotlib.pyplot as plt
import numpy as np

#########################################
# Defining a function pop1()

def pop1(p,br,dr,migrPlus,migrMinus):

p=p+(p*br)-(p*dr)+migrPlus-migrMinus

return p

###################################
# Asking for input values
#
# input() creates a strng which has to be converted into an int()

baseYear = int(input(‘Basisjahr als Zahl ? ‘))

p = int(input(‘Bevölkerung als Zahl ‘))

br = float(input(‘Geburtenrate in % ‘))

dr = float(input(‘Sterberate in % ‘))

migrPlus = int(input(‘Zuwanderung Zahl ‘))
migrMinus = int(input(‘Abwanderung Zahl ‘))

n = int(input(‘Wieviele Jahre voraus ? ‘))

############################################
# processing the data
#
# creating a range called ‘run’ for the years to compute

run = np.arange(1, n+1, 1)

####################################
# pop is a ‘list’ to collect the pop-values for every year

pop = []

########################################
# The first element of pop is the base year
pop.append(p)

######################################
# Compute the changing values for the population p and store these in pop
# Use for this computation the function pop1() defined before

for i in run:
p=pop1(p,br,dr,migrPlus,migrMinus)
pop.append(p)

##############################################
# Print the content of pop for the user to show
# the different years with their pop-values

for i in range(n+1):
print(‘Jahr %5d = Einw. %8d \n’ %(baseYear+i, pop[i]) )

##############################################
# Make the numbers visible as a graph

plt.figure(1)
plt.axis([0, len(run)+1, 1, max(pop)])

run2 = np.arange(0, n+1, 1)
plt.plot(run2, pop, ‘bo’)

plt.show()
plt.close()

EXAMPLE RUNS

EXAMPLE 1

Shows a population with a lower birth rate than death rate but a positive migration outcome. (Bevölkerung = population, Zahl = number, Gebrtenrate = biirth rate, Sterberate = death rate, Zuwanderung = migration plus, Abwanderung = migration minus, Wieviele Jahre voraus = how many years forcasting)

Bevölkerung als Zahl 1000

Geburtenrate in % 0.15

Sterberate in % 0.17

Zuwanderung Zahl 200

Abwanderung Zahl 100

Wieviele Jahre voraus ? 20

Jahr 2019 = Einw. 1000

Jahr 2020 = Einw. 1080

Jahr 2021 = Einw. 1158

Jahr 2022 = Einw. 1235

Jahr 2023 = Einw. 1310

Jahr 2024 = Einw. 1384

Jahr 2025 = Einw. 1456

Jahr 2026 = Einw. 1527

Jahr 2027 = Einw. 1596

Jahr 2028 = Einw. 1665

Jahr 2029 = Einw. 1731

Jahr 2030 = Einw. 1797

Jahr 2031 = Einw. 1861

Jahr 2032 = Einw. 1923

Jahr 2033 = Einw. 1985

Jahr 2034 = Einw. 2045

Jahr 2035 = Einw. 2104

Jahr 2036 = Einw. 2162

Jahr 2037 = Einw. 2219

Jahr 2038 = Einw. 2275

Jahr 2039 = Einw. 2329

example 1 - increasing population
example 1 – increasing population

EXAMPLE 2

Shows a population with a lower birth rate than death rate and a negative migration outcome. (Bevölkerung = population, Zahl = number, Gebrtenrate = biirth rate, Sterberate = death rate, Zuwanderung = migration plus, Abwanderung = migration minus, Wieviele Jahre voraus = how many years forcasting)

Basisjahr als Zahl ? 2019

Bevölkerung als Zahl 1000

Geburtenrate in % 0.15

Sterberate in % 0.17

Zuwanderung Zahl 100

Abwanderung Zahl 120

Wieviele Jahre voraus ? 30
Jahr 2019 = Einw. 1000

Jahr 2020 = Einw. 960

Jahr 2021 = Einw. 920

Jahr 2022 = Einw. 882

Jahr 2023 = Einw. 844

Jahr 2024 = Einw. 807

Jahr 2025 = Einw. 771

Jahr 2026 = Einw. 736

Jahr 2027 = Einw. 701

Jahr 2028 = Einw. 667

Jahr 2029 = Einw. 634

Jahr 2030 = Einw. 601

Jahr 2031 = Einw. 569

Jahr 2032 = Einw. 538

Jahr 2033 = Einw. 507

Jahr 2034 = Einw. 477

Jahr 2035 = Einw. 447

Jahr 2036 = Einw. 418

Jahr 2037 = Einw. 390

Jahr 2038 = Einw. 362

Jahr 2039 = Einw. 335

Jahr 2040 = Einw. 308

Jahr 2041 = Einw. 282

Jahr 2042 = Einw. 256

Jahr 2043 = Einw. 231

Jahr 2044 = Einw. 206

Jahr 2045 = Einw. 182

Jahr 2046 = Einw. 159

Jahr 2047 = Einw. 135

Jahr 2048 = Einw. 113

Jahr 2049 = Einw. 90

example 2 - decreasing population
example 2 – decreasing population

LEARNING ENVIRONMENT

For an overview of all posts in this block about programming with python 3 see HERE.

SIMPLE PROGRAMMING ENVIRONMENT WITH PYTHON-SPYDER

eJournal: uffmm.org, ISSN 2567-6458, 30.Dec 2018; extension 10.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This post is part of the online book project for the AAI-paradigm. As mentioned in the text of the book the AAI paradigm will need for its practical usage appropriate software. Some preliminary (experimentally) programming is already underway. The programming language used for this programming is python. Here some bits of information how one can install a simple python environment to share these activities.

WINPYTHON OR ANACONDA WITH SPYDER
(Windows as well as Linux (ubuntu))

To work with the python programming language — here python 3 —  one needs some tools interacting with each other. For this different integrated development packages have been prepared.  In this uffmm-software project I am using the spyder development environment either as part of the winpython distribution or as part of the anaconda distribution.

WINPYTHON DISTRIBUTION

The winpython package can be found here. See also the picture below.

Website of winpython distribution
Website of winpython distribution

If one has downloaded the winpython distribution in some local folder then you will see the following files and folders (see picture below):

winpython distribution folder after installation
winpython distribution folder after installation

SPYDER INTEGRATED ENVIRONMENT

To use the integrated spyder environment one can also look to the spyder website directly (see picture below).

spyder working environment for python - website
spyder working environment for python – website

The spyder team recomments to download the spyder software as part of the bigger anaconda distribution with lots of additional options (see picture below).

spyder working environment embedded in the anaconda distribution
spyder working environment embedded in the anaconda distribution

Downloading the anaconda distribution needs much more time then the winpython distribution. But one gets a lot of stuff and the software is fairly good integrated into the windows 10 operating system. I personally recomment for the beginners not to beginn with the complex anaconda environment but to stay with the spyder integrated environment only. This can be done by activating with the windows-button the list of apps, looking to the anaconda icon, and there one can find the spyder icon (an idealized spyder web). One can click with the right mouse button on this icon and then select to ‘add to the task bar’. After this operation you can observe the spyder icon as attached to the task bar like in the picture below.

part of the task bar in windows with icons for spyder and a python environment
Part of the task bar in windows with icons for spyder (right border)  and a python environment (left from spyder)

If one activates the spyder icon a window opens showing some standard configuration of the integrated spyder development environment (see picture below).

spyder working environment with editor, console, and additional object informations
spyder working environment with editor, console, and additional object informations

The most important sub-windows are the window left from the editor and the window right-below from the console.  One can use the console to make small experiments with python commands and the editor to write larger source code.  In the header bar are many helpful icons for editing, running of programs, testing, and more.

LINUX (UBUNTU)

If one is working with linux (what I am usually are doing; I use the distribution ubuntu 18.04.1 LTS) then python is part of the system in version 2 as well in version 3 and spyder can be used too.

WINPYTHON CONSOLE

Besides the integrated development editor (IDE) spyder you can find even more programming tools. One very helpful tool is the WinPython Console (see icon within the red circle). You can attach this icon on your task bar too and then you can start the Winpython Console by clicking on it.

Figure 1: Directory path after starting the WinPython Console
Figure 1: Directory path after starting the WinPython Console

To apply this console to some python programm you have to navigate to that folder where you have the python program to be executed. If You do not know already the whole path then you have two options: (i) move the path upwards by using the command ‘cd ..’ (change directory one level upwards) or (ii) move the path downwards by using the command ‘cd DIR-NAME‘. If you are in a new folder you can use the command ‘dir‘ to list all the files and folders in the actual directory. Doing this You can reach the following folder with some python program files:

Figure 2: Folder with some python programs
Figure 2: Folder with some python programs

There is one example file pop0e.py which we can start (the whole program will be described later in detail).

Figure 3: Shows an example run with the program pop0e.py, some print outs as well as a diagram.
Figure 3: Shows an example run with the program pop0e.py, some print outs as well as a diagram.

For this last example with the Winpython Console the program can be edited by nearly any kind of a text editor. After the file has been saved with a .py ending one can use the console to start the program. In the last example (cf. figure 3) the important input was: ‘python pop0e.py‘ this states that the python interpreter shall take the program text of the file ‘pop0e.py’ and execute the program.

LEARNING ENVIRONMENT

For an overview of all posts in this block about programming with python 3 see HERE.