Just a very basic example how to create a Python Module that consists of a Single Class and 2 basic functions.
Our main app will can our module to print out a word, that we pass to our first function.
The Directory Setup:
Below is a tree view of my current working directory:
1 2 3 4 5 6 7 |
|
In order to make a python file a module, we need to have a blank __init__.py
file in our directory. So any files under our providers directory will be seen as modules from our main.py
file.
Our Test Module:
in our providers/test.py
file:
1 2 3 4 5 6 7 8 |
|
Then our providers/test.py
file will be blank.
Our main.py
, we will import our test module, instantiate our class, and call our function within the class that we instantiated:
1 2 3 4 5 6 |
|
instead of response = test_instance.simple_test()
, you could also do print(test_instance.simple_test()
Testing it out:
1 2 |
|
It’s very basic but will post some more topics around this in the future.
Also note, this blog is for quick posts that I come accross during my daily doings, for more details tutorials have a look at my main blog: sysadmins.co.za