-
Notifications
You must be signed in to change notification settings - Fork 3
D. Python hints
This section presents few hints for model implementation.
- Replace a method of a class instance
- Numpy arrays: Merge rows, Merge columns, Split columns
- Call external code
- Execute script in Windows file manager
Assignment of a new method to a class instance with access to self.* members. Note that the change is only active in instance foo of class Test. Other instances of class Test will not be affected.
class Test(object):
def __init__(self, x=0):
self.x = x
def execute(self):
print("ORIGINAL execute(), y=x, y:", self.x)
# defines external function execute()
def execute(self):
print("REPLACED execute(), y=2*x, y:", 2 * self.x)
.
# creates instance 'foo' of class 'Test'
foo = Test(x=4)
foo.execute() # calls ORIGINAL method
# assigns externally defined execute() to instance 'foo' of class 'Test'
foo.execute = execute.__get__(foo, Test)
foo.execute() # calls REPLACED method
Output:
-------
ORIGINAL execute(), y=x, y: 4
REPLACED execute(), y=2*x, y: 8
x = np.array([[1, 2], y = np.array([[5, 6]]) z = np.r_[x, y] ==> [[1, 2],
[3, 4]]) [3, 4],
[5, 6]]
x = np.array([[1, 2], y = np.array([[11, 22, 33], z = np.c_[x, y] ==> [[1,2, 11,22,33],
[3, 4]]) [44, 55, 66]]) [3,4, 44,55,66]]
# '1' column and '5' columns exclusive the first one
x0, x1 = np.hsplit(z, [1, 4]), x0 = [[1], x1 = [[2, 11,22,33],
[3]] [4, 44,55,66]]
# '3' columns, and '5' columns exclusive the first three ones
x0, x1 = np.hsplit(z, [3, 5]), x0 = [[1,2,11], x1 = [[22,33],
[3,4,44]] [55,66]]
This script contains both master and worker process code
---------------------------------------------------------------------------
import numpy as np
import os
import pickle
import subprocess
import sys
if __name__ == '__main__':
function = os.path.basename(__file__)
if 'worker' in sys.argv:
x2D = pickle.loads(sys.stdin.buffer.read())
y2D = []
for x in x2D:
y = x[0] + x[1] * x[2]
y2D.append(y)
sys.stdout.buffer.write(pickle.dumps(np.array(y2D)))
else:
x = np.arange(30).reshape(10, 3)
y = pickle.loads(subprocess.check_output(
[sys.executable, function, 'worker'], input=pickle.dumps(x)))
print('x:', x, '\ny:', y)
The script shown above contains both the functionality of the master and of the follower process. In the else-branch, the master creates an array X2D, dumps X2D to string s, gets its own filename function from the operating system, calls the follower with input s and program argument 'follower', and reads the output Y2D of the follower from sys.stdout :
X2D = np.arange(30).reshape(10, 3)
s = pickle.dumps(X2D)
function = os.path.basename(__file__)
result = subprocess.check_output([sys.executable, function, 'follower'], input=s)
Y2D = pickle.loads(result)
In the inner if-branch of the script, the follower reads its input as string s from sys.stdin. String s is converted to array x2D, the output y2D is computed, y2D is dumped to a string s, and string s is written to sys.stdout :
s = sys.stdin.buffer.read()
x2D = pickle.loads(s)
y2D = np.array([x[0] + x[1] * x[2] for x in x2D])
s = pickle.dumps(y2D)
sys.stdout.buffer.write(s)
-
Install Python 3.0 or newer
-
Find location of "python.exe" employing windows command where:
> where /R c:\ python*.exe -
In file manager: [Properties] -> [Change] -> [Browse] to: 'python*.exe'
-
Now this python script starts with double-click in file manager