Collection of python (3.8+) modules developed for various projects.
To make it available in sys.path
the cloned folder must have the name ntlib. Then the script _add_path.py
can be called to add a link of this namespace package to the file mylibs.pth in site-packages or dist-packages.
Thus, it is possible that the same installation can be used on different systems of a dual-boot computer. Furthermore, this allows the development of libraries without naming issues:
FOLDER STRUCTURE:
+ path_defined_in_pth_file
+ ntlib
- imp.py
- ...
+ extern
- imp.py
- ...
import ntlib.imp as ntimp
import extern.imp as extimp
Below is a list with a brief description of the available modules. Feel free to modify the source code for your own purposes.
Provides the following methods:
-
config_log(level='INFO')
: set a basic config for logging, it is possible to set an additional formatter or a rotating log file -
options = load_config(name)
: import_confpath.py
which defines a path to a json dictionary (config.json) containingoptions
for ntlib modulename
-
reload(module)
: reload module, extends function fromimportlib
-
module = import_path(modulename, path='')
: import from specific folder -
module = import_alias(alias, modulename)
: load module from an alias path (sectionimp
in config.json)
In order for the following modules to operate correctly copy the config_sample
folder to config
and customize it or edit _confpath.py
.
Allows system commands (shell_cmd
, start_app
), and provides different classes to work with threads:
ThreadLoop
: execute a method repeatedlyQueueWorker
: parallel element processingCmpEvent
: receive data from another thread after a successful comparison
Module to call methods repeatedly. Also provides a global RptSched
(scheduler.glob_sched
) to add e.g. purge jobs.
Wrapper over the python socket to use a timeout by default and simplify initialization of udp and ipv6 sockets.
Modules to control vlc and foobar2000 from python. The paths to the applications are defined in the config.
Using modules sounddevice and soundfile to playback audio files on a specific output:
import ntlib.easysound as esound
fp = esound.new_playback('beeps.wav', device=None, channels=2, outputs=(1,), mono=True, vol=0.5)
# outputs = (1,): playback only on right channel
# outputs = (1, 0): swap right and left channel
fp.play()
#fp.stop()
fp.join()
fp.close()
It is also possible to start it from console: python3 easysound --help
for more information.
Moreover the module can call a function periodically with the input volume of a given device:
import time
import ntlib.easysound as esound
def vol_cb(vol):
print(vol)
invol = esound.InputVolume(vol_cb, device=None, delay=0.5)
# calls vol_cb approx. each 0.5 seconds
invol.start()
time.sleep(5)
invol.stop()
invol.close()
When using pulseaudio on linux, the device option should be avoided.
Helps connecting python with an open LibreOffice or OpenOffice file using uno
. Especially useful for calc.
The config contains the connection parameters.
import ntlib.sofficectl.calctools as ctools
model = calctools.connect('path.ods or name of opened file')
sheet = calctools.MiniSheet(model.Sheets[0])
sheet.set_data('A3', 'Hello World')
sheet[0,1] = 42.5 # set cell B1
print(sheet.get_data('B1'))
print(sheet[2,0]) # prints 'Hello World'
print(sheet.get_array('A1', 'B3'))
to_dtime
and from_dtime
converts office time values to datetime
.
New: type hints