Repetative routines for python
Version 1.0.5 (9.74 KB) by
Dmytro Kolenov
Summary of some repetative action one does in python
Summary of some repetative action one does in python. Updated ocasionally.
1) Take a snapshot of current enviroment from script
import subprocess
with open("requirements.txt", "w") as f:
subprocess.run(["pip", "freeze"], stdout=f)
2) Framework to load pkl files from folder.
import os
import pickle
folder_path = "path/to/your/folder"
# Version 1: List comprehension (compact)
# pkl_files = [f for f in os.listdir(folder_path) if f.endswith('.pkl')]
# Version 2: Traditional loop (more readable)
pkl_files = []
for f in os.listdir(folder_path):
if f.endswith('.pkl'):
pkl_files.append(f)
# Sort the file list for consistent indexing (optional)
pkl_files.sort()
# Full paths to the .pkl files
pkl_paths = [os.path.join(folder_path, f) for f in pkl_files]
def load_pickle_by_index(index):
if 0 <= index < len(pkl_paths):
with open(pkl_paths[index], 'rb') as f:
return pickle.load(f)
else:
raise IndexError("Index out of range.")
# Example usage
print(f"Found {len(pkl_paths)} .pkl files.")
# obj = load_pickle_by_index(0)
print(type(obj)) # Uncomment to test loading
% run_make_test_pptx_demo.m
% Creates synthetic test results, then calls make_test_pptx
% --- synthetic results you’d normally compute in your pipeline
results = struct();
results.timestamp = datestr(now, 'yyyy-mm-dd HH:MM:ss');
results.tester = 'Automated Rig 3';
N = 10000; % or N = 1e4;
results.timeSec = linspace(0, 20, N);
results.signal = 0.7*sin(2*pi*0.6*results.timeSec) + 0.05*randn(1,N);
results.latencyMs = 12 + 3*randn(1, 2000); % pretend 2k transactions
% KPIs
results.nRuns = 25;
results.metrics.meanLatency = mean(results.latencyMs);
results.metrics.stdLatency = std(results.latencyMs);
results.metrics.p95Latency = prctile(results.latencyMs,95);
results.metrics.maxOvershoot = max(results.signal) - 0.7;
results.passRate = 0.96;
results.pass = results.metrics.p95Latency < 18; % example rule
% Short log
results.log = [
"Init hardware: OK"
"Loaded config profile: /configs/prod-A.json"
"Self-test: PASS"
"Run sweep: 25 iterations"
sprintf("Anomalies detected: %d", sum(results.latencyMs>20))
];
% --- build the deck (creates a NEW .pptx; no template file required)
outfile = make_test_pptx(results, "reports"); % returns full path
fprintf("Wrote: %s\n", outfile);
function pptxPath = make_test_pptx(results, outdir)
% make_test_pptx Build a brand-new PowerPoint summary deck for a test run.
% Creates a fresh .pptx using MATLAB's built-in default presentation.
% Does NOT rely on any existing .pptx or .potx on disk.
% ---- dependency check
if ~exist('mlreportgen.ppt.Presentation','class')
error(['mlreportgen.ppt (MATLAB Report Generator) not found.\n' ...
'Install MATLAB Report Generator or use a File Exchange alternative (e.g., exportToPPTX).']);
end
import mlreportgen.ppt.*
if nargin < 2 || strlength(outdir)==0
outdir = "reports";
end
if ~exist(outdir, 'dir'), mkdir(outdir); end
pptxPath = fullfile(outdir, "test_summary.pptx");
% Create a NEW presentation using MATLAB's built-in default template in memory.
% This does NOT require any existing file on disk.
pres = Presentation(pptxPath); % no template path -> built-in default
open(pres);
% ---------- Slide 1: Title ----------
s1 = add(pres, "Title Slide");
replace(s1, "Title", "Test Summary");
replace(s1, "Subtitle", sprintf("%s • %s", results.tester, results.timestamp));
% ---------- Slide 2: KPIs ----------
s2 = add(pres, "Title and Content");
replace(s2, "Title", "Key Performance Indicators");
kpi = {
"Metric", "Value";
"Runs", num2str(results.nRuns);
"Pass rate (%)", sprintf("%.1f", 100*results.passRate);
"Mean latency (ms)", sprintf("%.2f", results.metrics.meanLatency);
"Std latency (ms)", sprintf("%.2f", results.metrics.stdLatency);
"p95 latency (ms)", sprintf("%.2f", results.metrics.p95Latency);
"Max overshoot", sprintf("%.3f", results.metrics.maxOvershoot);
"Overall status", string(results.pass * "PASS" + ~results.pass * "FAIL")
};
tbl = Table(kpi);
replace(s2, "Content", tbl);
% ---------- Prep images (export figures) ----------
latFig = figure('Visible','off');
histogram(results.latencyMs);
xlabel('Latency (ms)'); ylabel('Count'); title('Latency Distribution');
latPng = fullfile(outdir,'latency_hist.png');
exportgraphics(latFig, latPng, 'Resolution', 150); close(latFig);
sigFig = figure('Visible','off');
plot(results.timeSec, results.signal); grid on
xlabel('Time (s)'); ylabel('Signal'); title('Signal vs Time');
sigPng = fullfile(outdir,'signal_timeseries.png');
exportgraphics(sigFig, sigPng, 'Resolution', 150); close(sigFig);
% ---------- Slide 3: Latency histogram ----------
s3 = add(pres, "Title and Content");
replace(s3, "Title", "Latency Distribution");
replace(s3, "Content", Picture(latPng));
% ---------- Slide 4: Signal snapshot ----------
s4 = add(pres, "Title and Content");
replace(s4, "Title", "Signal Snapshot");
replace(s4, "Content", Picture(sigPng));
% ---------- Slide 5: Short log (bullets) ----------
s5 = add(pres, "Title and Content");
replace(s5, "Title", "Run Notes / Log (truncated)");
paras = arrayfun(@(i) Paragraph(results.log(i)), 1:numel(results.log), 'uni', false);
tbx = TextBox();
for i = 1:numel(paras), append(tbx, paras{i}); end
replace(s5, "Content", tbx);
% Finish (creates/overwrites the pptx at pptxPath)
close(pres);
end
Cite As
Dmytro Kolenov (2026). Repetative routines for python (https://www.mathworks.com/matlabcentral/fileexchange/180922-repetative-routines-for-python), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2024b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.5 | wqwe |
|
|
| 1.0.4 | bla |
|
|
| 1.0.3 | more details |
|
|
| 1.0.2 | Description added |
|
|
| 1.0.1 | directly with .m extension |
|
|
| 1.0.0 |
