with open('data-dev/jsondict-test.json', 'r') as fp:
d = json.load(fp)
d
d.items()dict_items([('b', 2), ('c', 3), ('d', 4)])
coreClasses to handle data structure more easily
dict_items([('b', 2), ('c', 3), ('d', 4)])
Dictionary whose current value is mirrored in a json file and can be initated from a json file
JsonDict requires a path to json file at creation. An optional dict can be passed as argument.
Behavior at creation:
JsonDict(p2json, dict) will create a JsonDict with key-values from dict, and mirrored in p2jsonJsonDict(p2json) will create a JsonDict with empty dictionary and load json content if file existsOnce created, JsonDict instances behave exactly as a dictionary
Create a new dictionary mirrored to a JSON file:
{'a': 1, 'b': 2, 'c': 3}
dict mirrored in /home/vtec/projects/ec-packages/eccore/nbs-dev/data-dev/jsondict-test.json
Once created, the JsonFile instance behaves exactly like a dictionary, with the added benefit that any change to the dictionary is automatically saved to the JSON file.
key: a; value: 1
key: b; value: 2
key: c; value: 3
Adding or removing a value from the dictionary works in the same way as for a normal dictionary. But the json file is automatically updated.
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
dict mirrored in /home/vtec/projects/ec-packages/eccore/nbs-dev/data-dev/jsondict-test.json
{'b': 2, 'c': 3, 'd': 4}
dict mirrored in /home/vtec/projects/ec-packages/eccore/nbs-dev/data-dev/jsondict-test.json
Validate that obj is of type obj_type. Raise error in the negative when raise_error is True
Functions to ensure path are properly formated and point to a real file or directory.
Validate that path is a Path or str and points to a real file or directory
Raised an ValueError exception with error message:
No file at /home/vtec/projects/ec-packages/eccore/nbs-dev/../data/img/IIIMG_001_512px.jpg. Check the path
Return a Path object when given a valid path as a str or a Path, raise error otherwise
Path('data-dev/jsondict-test.json')
Raised an ValueError exception with error message:
No file or directory at /home/vtec/projects/ec-packages/eccore/nbs-dev/data-dev/no-file-here.json. Check the path
Returns the value corresponding to the key-value pair in the configuration file (configparser format)
When no path_to_config_file is provided, the function will try to find the file in: the system’s home, the parent directory of the current directory, and the Google drive directory mounted to the Colab environment.
By defaults (path_to_config_file is None), it is assumed that the configuration file is located in: - the local package config directory (home/.eccore/) - the working directory - the folder above the working directory - the private-accross-accounts directory on google drive.
File names are expected to be either config-api-keys.cfg or config-sample.cfg.
If not, a path to the file (Path or str) must be provided.
The configuration file is expected to be in the format used by the standard module configparser documentation
[DEFAULT]
key = value
[section_name]
key = value
[section_name]
key = value
found /home/vtec/projects/ec-packages/eccore/config-api-keys.cfg
Using config file at /home/vtec/projects/ec-packages/eccore/config-api-keys.cfg
'Etienne Charlier'
/home/vtec/projects/ec-packages/eccore/config-sample.cfg
[azure]
azure-api-key= dummy_api_key_for_azure
[github]
git_name = not_my_real_github_name
git_email = not_my_real_git_email
github_username = not_my_real_git_username
[kaggle]
kaggle_username = not_my_real_kaggle_name
kaggle_key = dummy_api_key_for_kaggle
[wandb]
api_key = dummy_api_key_for_wandb
Using config file at /home/vtec/projects/ec-packages/eccore/config-sample.cfg
Using config file at /home/vtec/projects/ec-packages/eccore/config-sample.cfg
Using config file at /home/vtec/projects/ec-packages/eccore/config-sample.cfg
Callable class representing the current machine. When called, instance return a dict all attrs:
os: the operating system running on the machinehome: path to home on the machineis_local, is_colab, is_kaggle: whether the machine is running locally or notp2config: path to the config filepackage_root: path to the package root directoryCurrentMachine is a singleton class.
{'os': 'linux',
'home': Path('/home/vtec'),
'is_local': True,
'is_colab': False,
'is_kaggle': False,
'p2config': Path('/home/vtec/.ecutilities/ecutilities.cfg'),
'package_root': Path('/home/vtec/projects/ec-packages/eccore')}
This machine is not registered a local machine, but is also not running in the cloud. We should register it as a local machine with register_as_local
Update the configuration file to register the machine as local machine
Use this method to register the current machine as local machine. Only needs to be used once on a machine. Do not use on cloud VMs
(True, False, False)
Technical Note:
The configuration file is located at a standard location, which varies depending on the OS:
- Windows:
- home is
C:\Users\username- application data in
C:\Users\username\AppData/Local/...orC:\Users\username\AppData\Roaming\...(see StackExchange)- application also can be loaded under a dedicated directory under
C:\Users\usernamelikeC:\Users\username\.conda\...- Linux:
- home is
/home/username- application data in a file or dedicated directory
/home/username/s.a.:
- file in home directory, e.g.
.gitconfig- file in an application dedicated directory, e.g.
/home/username/.conda/...
ecutilitiesplaces the configuration file in a dedicated directory in the home directory: -C:\Users\username\.ecutilities\ecutilities.cfg-/home/username/.ecutilities/ecutilities.cfgRetrieve the OS:
win32 with Windows linux with linux darwin with macOsAccessing the correct path depending on the OS:
WindowsPath('C:/Users/username') with Windows Path('/home/username') with linux
Class representing the project file system and key subfolders (data, nbs, src)
Set paths to key directories, according to whether the code is running locally or in the cloud. Give access to path to these key folders and information about the environment.
{'os': 'linux',
'home': Path('/home/vtec'),
'is_local': True,
'is_colab': False,
'is_kaggle': False,
'p2config': Path('/home/vtec/.ecutilities/ecutilities.cfg'),
'package_root': Path('/home/vtec/projects/ec-packages/eccore')}
Create a standard project file system with the following structure:
project_root
|--- data all data files
|--- nbs all notebooks for work and experiments
|--- src all scripts and code
Setup logging to console and to file if logfile is not None
Logs all elements passed to logs
Highlights when function in entered to and exited from
After setting up the logging, it is easy to create log entries:
Logging to console and to /home/vtec/projects/ec-packages/eccore/nbs-dev/data-dev/dev.log.
Logging setup finished
2026-07-24 10:34:27: Logging manually as warning, shows in logfile and console
See logfile content:
Log file content:
2026-07-24 10:34:27: Logging manually as info, only shows in logfile
2026-07-24 10:34:27: Logging manually as warning, shows in logfile and console
2026-07-24 10:34:27: Using log function, as info, only shows in the log file
Create a function decorated with @monitor_fn to monitor function calls, i.e. when function in entered and exited.
function output is 3
Log file content:
2026-07-24 10:34:27: Logging manually as info, only shows in logfile
2026-07-24 10:34:27: Logging manually as warning, shows in logfile and console
2026-07-24 10:34:27: Using log function, as info, only shows in the log file
2026-07-24 10:34:30: Entering `a_function`
2026-07-24 10:34:30: Exiting `a_function`
List files in directory and its subdiretories, print tree starting from parent directory
/home/vtec/projects/ec-packages/eccore/nbs-dev
eccore
|--nbs-dev
| |--index.ipynb (0)
| |--0_02_plotting.ipynb (1)
| |--styles.css (2)
| |--9_01_dev_utils.ipynb (3)
| |--nbdev.yml (4)
| |--_quarto.yml (5)
| |--0_00_core.ipynb (6)
| |--0_01_ipython.ipynb (7)
| |--sidebar.yml (8)
| |--.last_checked (9)
| |--data-dev
| | |--ten-blobs-6-cols-y.npy (10)
| | |--ten-blobs-6-cols-X.npy (11)
| | |--jsondict-test.json (12)
| | |--ten-blobs-6-cols-clusters.npy (13)
List of 14 files when unfiltered
Use pattern to filter the paths to return (using glob syntax)
eccore
|--nbs-dev
| |--index.ipynb (0)
| |--0_02_plotting.ipynb (1)
| |--9_01_dev_utils.ipynb (2)
| |--0_00_core.ipynb (3)
| |--0_01_ipython.ipynb (4)
| |--data-dev
List of 5 files when filtered
Climb directory tree up to a directory starting with pattern, and return its path.
When no directory is found in the tree starting with pattern, return the current directory path.
It is possible to pass a path as starting path to climb from.
Path('/home/vtec/projects/ec-packages/eccore/nbs-dev')