funcprofiler 1.0.1__tar.gz → 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,118 @@
1
+ Metadata-Version: 2.4
2
+ Name: funcprofiler
3
+ Version: 1.1.0
4
+ Summary: An open-source Python library for finding bottlenecks in code. Includes function profiling, data exports, logging, and even line-by-line profiling, for more control.
5
+ Home-page: https://github.com/infinitode/funcprofiler
6
+ Author: Infinitode Pty Ltd
7
+ Author-email: infinitode.ltd@gmail.com
8
+ Classifier: Development Status :: 5 - Production/Stable
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3.6
12
+ Classifier: Programming Language :: Python :: 3.7
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Requires-Python: >=3.6
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: license-file
29
+ Dynamic: requires-python
30
+ Dynamic: summary
31
+
32
+ # FuncProfiler
33
+ ![Python Version](https://img.shields.io/badge/python-3.13-blue.svg)
34
+ [![Code Size](https://img.shields.io/github/languages/code-size/infinitode/funcprofiler)](https://github.com/infinitode/funcprofiler)
35
+ ![Downloads](https://pepy.tech/badge/funcprofiler)
36
+ ![License Compliance](https://img.shields.io/badge/license-compliance-brightgreen.svg)
37
+ ![PyPI Version](https://img.shields.io/pypi/v/funcprofiler)
38
+
39
+ An open-source Python library for identifying bottlenecks in code. It includes function profiling, data exports, logging, and line-by-line profiling for more granular control.
40
+
41
+ ## Changelog (v.1.1.0):
42
+ - Added support for 2 new export formats: `yaml` and `toml`.
43
+ - Exports now include more information: peak memory usage, timestamp, arguments, return value, filepath, line number, and docstring.
44
+ - Added `enabled` and `log_level` options to the decorators.
45
+ - Improved export formats for better readability.
46
+
47
+ ## Installation
48
+
49
+ You can install FuncProfiler using pip:
50
+
51
+ ```bash
52
+ pip install funcprofiler
53
+ ```
54
+
55
+ ## Supported Python Versions
56
+
57
+ FuncProfiler supports Python 3.6 and later.
58
+
59
+ ## Features
60
+
61
+ - **Function Profiling**: Monitor a function's memory usage and execution time.
62
+ - **Line-by-Line Profiling**: Get execution time and memory usage for each line of a function.
63
+ - **Shared Logging**: Log profiler outputs to a `.txt` file.
64
+ - **File Exports**: Export profiling data in various formats.
65
+ - **New Options**:
66
+ - `enabled`: A boolean to enable or disable profiling.
67
+ - `log_level`: Set the logging level to "info" or "debug".
68
+
69
+ ## Export Formats
70
+
71
+ | Format | `function_profile` | `line_by_line_profile` |
72
+ |--------|--------------------|------------------------|
73
+ | `txt` | ✅ | ❌ |
74
+ | `json` | ✅ | ✅ |
75
+ | `csv` | ✅ | ✅ |
76
+ | `html` | ✅ | ✅ |
77
+ | `xml` | ✅ | ✅ |
78
+ | `md` | ✅ | ✅ |
79
+ | `yaml` | ✅ | ✅ |
80
+ | `toml` | ✅ | ✅ |
81
+
82
+ ## Usage
83
+
84
+ ### Function Profiling
85
+
86
+ ```python
87
+ from funcprofiler import function_profile
88
+
89
+ @function_profile(export_format="html", shared_log=True, log_level="debug")
90
+ def some_function():
91
+ return "Hello World."
92
+
93
+ message = some_function()
94
+ ```
95
+
96
+ ### Line-by-Line Profiling
97
+
98
+ ```python
99
+ from funcprofiler import line_by_line_profile
100
+
101
+ @line_by_line_profile(shared_log=True, enabled=True)
102
+ def some_complicated_function(n):
103
+ total = 0
104
+ for i in range(n):
105
+ for j in range(i):
106
+ total += (i * j) ** 0.5
107
+ return total
108
+
109
+ total = some_complicated_function(1000)
110
+ ```
111
+
112
+ ## Contributing
113
+
114
+ Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/infinitode/funcprofiler).
115
+
116
+ ## License
117
+
118
+ FuncProfiler is released under the **MIT License (Modified)**. See the [LICENSE](https://github.com/infinitode/funcprofiler/blob/main/LICENSE) file for details.
@@ -0,0 +1,87 @@
1
+ # FuncProfiler
2
+ ![Python Version](https://img.shields.io/badge/python-3.13-blue.svg)
3
+ [![Code Size](https://img.shields.io/github/languages/code-size/infinitode/funcprofiler)](https://github.com/infinitode/funcprofiler)
4
+ ![Downloads](https://pepy.tech/badge/funcprofiler)
5
+ ![License Compliance](https://img.shields.io/badge/license-compliance-brightgreen.svg)
6
+ ![PyPI Version](https://img.shields.io/pypi/v/funcprofiler)
7
+
8
+ An open-source Python library for identifying bottlenecks in code. It includes function profiling, data exports, logging, and line-by-line profiling for more granular control.
9
+
10
+ ## Changelog (v.1.1.0):
11
+ - Added support for 2 new export formats: `yaml` and `toml`.
12
+ - Exports now include more information: peak memory usage, timestamp, arguments, return value, filepath, line number, and docstring.
13
+ - Added `enabled` and `log_level` options to the decorators.
14
+ - Improved export formats for better readability.
15
+
16
+ ## Installation
17
+
18
+ You can install FuncProfiler using pip:
19
+
20
+ ```bash
21
+ pip install funcprofiler
22
+ ```
23
+
24
+ ## Supported Python Versions
25
+
26
+ FuncProfiler supports Python 3.6 and later.
27
+
28
+ ## Features
29
+
30
+ - **Function Profiling**: Monitor a function's memory usage and execution time.
31
+ - **Line-by-Line Profiling**: Get execution time and memory usage for each line of a function.
32
+ - **Shared Logging**: Log profiler outputs to a `.txt` file.
33
+ - **File Exports**: Export profiling data in various formats.
34
+ - **New Options**:
35
+ - `enabled`: A boolean to enable or disable profiling.
36
+ - `log_level`: Set the logging level to "info" or "debug".
37
+
38
+ ## Export Formats
39
+
40
+ | Format | `function_profile` | `line_by_line_profile` |
41
+ |--------|--------------------|------------------------|
42
+ | `txt` | ✅ | ❌ |
43
+ | `json` | ✅ | ✅ |
44
+ | `csv` | ✅ | ✅ |
45
+ | `html` | ✅ | ✅ |
46
+ | `xml` | ✅ | ✅ |
47
+ | `md` | ✅ | ✅ |
48
+ | `yaml` | ✅ | ✅ |
49
+ | `toml` | ✅ | ✅ |
50
+
51
+ ## Usage
52
+
53
+ ### Function Profiling
54
+
55
+ ```python
56
+ from funcprofiler import function_profile
57
+
58
+ @function_profile(export_format="html", shared_log=True, log_level="debug")
59
+ def some_function():
60
+ return "Hello World."
61
+
62
+ message = some_function()
63
+ ```
64
+
65
+ ### Line-by-Line Profiling
66
+
67
+ ```python
68
+ from funcprofiler import line_by_line_profile
69
+
70
+ @line_by_line_profile(shared_log=True, enabled=True)
71
+ def some_complicated_function(n):
72
+ total = 0
73
+ for i in range(n):
74
+ for j in range(i):
75
+ total += (i * j) ** 0.5
76
+ return total
77
+
78
+ total = some_complicated_function(1000)
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/infinitode/funcprofiler).
84
+
85
+ ## License
86
+
87
+ FuncProfiler is released under the **MIT License (Modified)**. See the [LICENSE](https://github.com/infinitode/funcprofiler/blob/main/LICENSE) file for details.
@@ -0,0 +1,481 @@
1
+ import time
2
+ import tracemalloc
3
+ import functools
4
+ import inspect
5
+ import os
6
+ import sys
7
+ import csv
8
+ import json
9
+ from typing import Callable, List, Dict, Optional
10
+ import xml.etree.ElementTree as ET
11
+
12
+ __all__ = ['function_profile', 'line_by_line_profile', 'export_function_profile_data', 'export_profiling_data']
13
+
14
+ def function_profile(export_format: Optional[str] = None, filename: Optional[str] = None, shared_log: bool = False, enabled: bool = True, log_level: str = "info") -> Callable:
15
+ """Decorator factory to profile the execution time and memory usage of a function.
16
+
17
+ Parameters:
18
+ export_format (Optional[str]): The format to export the profiling data ('txt', 'json', 'csv', 'html').
19
+ filename (Optional[str]): The name of the output file (without extension).
20
+ shared_log (Optional[bool]): If True, log to a shared file for all profiled functions.
21
+ enabled (bool): If False, the decorator will not perform any profiling.
22
+ log_level (str): The logging level ("info" or "debug").
23
+ Returns:
24
+ Callable: The profiling wrapper or decorator function.
25
+ """
26
+ log_filename = f"func_profiler_logs_{time.strftime('%Y%m%d')}_{time.strftime('%H%M%S')}.txt"
27
+
28
+ def decorator(func: Callable) -> Callable:
29
+ @functools.wraps(func)
30
+ def wrapper(*args, **kwargs):
31
+ if not enabled:
32
+ return func(*args, **kwargs)
33
+
34
+ # Start time and memory tracking
35
+ tracemalloc.start()
36
+ start_time = time.time()
37
+
38
+ # Prepare shared logging if enabled
39
+ log_file = None
40
+ if shared_log:
41
+ log_file = open(log_filename, 'a')
42
+ log_file.write(f"Profiling log for {func.__name__}\n")
43
+ log_file.write(f"Date: {time.strftime('%Y-%m-%d')}\n")
44
+ log_file.write(f"Time: {time.strftime('%H:%M:%S')}\n\n")
45
+
46
+ # Execute the function
47
+ result = func(*args, **kwargs)
48
+
49
+ # End time and memory tracking
50
+ end_time = time.time()
51
+ current, peak = tracemalloc.get_traced_memory()
52
+ tracemalloc.stop()
53
+
54
+ # Prepare data for exporting
55
+ profiling_data = {
56
+ "function_name": func.__name__,
57
+ "execution_times": end_time - start_time,
58
+ "memory_usage": current / 10**6, # Convert to MB
59
+ "peak_memory_usage": peak / 10**6,
60
+ "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'),
61
+ "arguments": str({'args': args, 'kwargs': kwargs}),
62
+ "return_value": str(result),
63
+ "filepath": inspect.getfile(func),
64
+ "line_number": inspect.getsourcelines(func)[1],
65
+ "docstring": func.__doc__
66
+ }
67
+
68
+ if export_format:
69
+ file = filename or f"{func.__name__}_funcprofile_report"
70
+ export_function_profile_data(profiling_data, func, export_format, file)
71
+
72
+ # Display the profiling results
73
+ if log_level == "info":
74
+ print(f"[FUNCPROFILER] Function '{func.__name__}' executed in {end_time - start_time:.12f}s")
75
+ print(f"[FUNCPROFILER] Current memory usage: {current / 10**6:.12f}MB; Peak: {peak / 10**6:.12f}MB")
76
+ elif log_level == "debug":
77
+ print(f"[FUNCPROFILER-DEBUG] Function '{func.__name__}' called with args: {args}, kwargs: {kwargs}")
78
+ print(f"[FUNCPROFILER-DEBUG] Execution Time: {end_time - start_time:.12f}s")
79
+ print(f"[FUNCPROFILER-DEBUG] Memory Usage: {current / 10**6:.6f}MB; Peak: {peak / 10**6:.6f}MB")
80
+ print(f"[FUNCPROFILER-DEBUG] Return Value: {result}")
81
+
82
+ # Log the profiling data
83
+ if shared_log and log_file:
84
+ log_file.write(f"Function {func.__name__} called with args: {args}, kwargs: {kwargs}\n")
85
+ log_file.write(f"Execution Time: {end_time - start_time:.12f}s, Memory usage: {current / 10**6:.6f}MB; Peak: {peak / 10**6:.6f}MB\n")
86
+ log_file.write("-" * 40 + "\n") # Separator between calls
87
+
88
+ if shared_log and log_file:
89
+ log_file.close() # Close the log file after writing if shared_log is True
90
+
91
+ return result
92
+
93
+ return wrapper
94
+
95
+ return decorator
96
+
97
+ def export_function_profile_data(profiling_data: dict, func: Callable, export_format: str, filename: str) -> None:
98
+ """Export profiling data for the function profile to the specified format.
99
+
100
+ Parameters:
101
+ profiling_data (dict): The profiling data containing execution time and memory usage.
102
+ func (Callable): The function that was profiled.
103
+ export_format (str): The format for export ('txt', 'json', 'csv', 'html', 'xml', 'md').
104
+ filename (str): The output filename without extension.
105
+ """
106
+ if export_format == "txt":
107
+ with open(f"{filename}.txt", 'w') as f:
108
+ for key, value in profiling_data.items():
109
+ f.write(f"{key.replace('_', ' ').title()}: {value}\n")
110
+
111
+ elif export_format == "json":
112
+ output_data = {
113
+ "metadata": {
114
+ "profile_type": "function_profile",
115
+ "export_time": time.strftime('%Y-%m-%d %H:%M:%S')
116
+ },
117
+ "profile": profiling_data
118
+ }
119
+ with open(f"{filename}.json", 'w') as f:
120
+ json.dump(output_data, f, indent=4)
121
+
122
+ elif export_format == "csv":
123
+ with open(f"{filename}.csv", 'w', newline='') as f:
124
+ writer = csv.writer(f)
125
+ writer.writerow(profiling_data.keys())
126
+ writer.writerow(profiling_data.values())
127
+
128
+ elif export_format == "html":
129
+ with open(f"{filename}.html", 'w') as f:
130
+ f.write("<html><head><title>Function Profiling Report</title>")
131
+ f.write("""
132
+ <style>
133
+ body { font-family: Arial, sans-serif; }
134
+ table { border-collapse: collapse; width: 60%; margin: 20px 0; }
135
+ th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; }
136
+ th { background-color: #f2f2f2; }
137
+ </style>
138
+ """)
139
+ f.write("</head><body>")
140
+ f.write(f"<h1>Function Profiling Report: {profiling_data['function_name']}</h1>")
141
+ f.write("<table>")
142
+ f.write("<tr><th>Metric</th><th>Value</th></tr>")
143
+ for key, value in profiling_data.items():
144
+ f.write(f"<tr><td>{key.replace('_', ' ').title()}</td><td>{value}</td></tr>")
145
+ f.write("</table>")
146
+ f.write("</body></html>")
147
+
148
+ elif export_format == "xml":
149
+ root = ET.Element("FunctionProfile")
150
+ for key, value in profiling_data.items():
151
+ ET.SubElement(root, key.replace('_', ' ').title().replace(' ', '')).text = str(value)
152
+ tree = ET.ElementTree(root)
153
+ tree.write(f"{filename}.xml")
154
+
155
+ elif export_format == "md":
156
+ with open(f"{filename}.md", 'w') as f:
157
+ f.write(f"# Function Profiling Report for {profiling_data['function_name']}\n\n")
158
+ f.write("| Metric | Value |\n")
159
+ f.write("|--------|-------|\n")
160
+ for key, value in profiling_data.items():
161
+ f.write(f"| {key.replace('_', ' ').title()} | {value} |\n")
162
+
163
+ elif export_format == "yaml":
164
+ def to_yaml_string(data):
165
+ lines = []
166
+ for key, value in data.items():
167
+ if isinstance(value, str) and '\n' in value:
168
+ lines.append(f"{key}: |")
169
+ for line in value.splitlines():
170
+ lines.append(f" {line}")
171
+ else:
172
+ lines.append(f"{key}: {json.dumps(value, ensure_ascii=False)}")
173
+ return "\n".join(lines)
174
+
175
+ with open(f"{filename}.yaml", 'w') as f:
176
+ f.write(to_yaml_string(profiling_data))
177
+
178
+ elif export_format == "toml":
179
+ def to_toml_string(data):
180
+ lines = []
181
+ for key, value in data.items():
182
+ if isinstance(value, str) and '\n' in value:
183
+ lines.append(f"{key} = '''\n{value}'''")
184
+ else:
185
+ lines.append(f"{key} = {json.dumps(value, ensure_ascii=False)}")
186
+ return "\n".join(lines)
187
+
188
+ with open(f"{filename}.toml", 'w') as f:
189
+ f.write(to_toml_string(profiling_data))
190
+
191
+ else:
192
+ raise ValueError("Unsupported export format. Use 'txt', 'json', 'csv', 'html', 'xml', 'md', 'yaml', or 'toml'.")
193
+
194
+ def line_by_line_profile(
195
+ export_format: Optional[str] = None,
196
+ filename: Optional[str] = None,
197
+ shared_log: bool = False,
198
+ enabled: bool = True,
199
+ log_level: str = "info"
200
+ ) -> Callable:
201
+ """Decorator for line-by-line profiling of a function with optional data export and shared logging.
202
+
203
+ Parameters:
204
+ export_format (Optional[str]): The format to export the profiling data ('json', 'csv', 'html').
205
+ filename (Optional[str]): The name of the output file (without extension).
206
+ shared_log (Optional[bool]): If True, log to a shared file for all profiled functions.
207
+ enabled (bool): If False, the decorator will not perform any profiling.
208
+ log_level (str): The logging level ("info" or "debug").
209
+ Returns:
210
+ Callable: The profiling wrapper or decorator function.
211
+ """
212
+ log_filename = f"lbl_profiler_logs_{time.strftime('%Y%m%d')}_{time.strftime('%H%M%S')}.txt"
213
+
214
+ def decorator(func: Callable) -> Callable:
215
+ @functools.wraps(func)
216
+ def wrapper(*args, **kwargs):
217
+ if not enabled:
218
+ return func(*args, **kwargs)
219
+
220
+ line_execution_times: Dict[int, float] = {}
221
+ line_memory_usage: Dict[int, float] = {}
222
+ current_line_start_time: Optional[float] = None
223
+ timer = time.perf_counter
224
+ tracemalloc.start()
225
+
226
+ # Prepare shared logging if enabled
227
+ log_file = None
228
+ if shared_log:
229
+ log_file = open(log_filename, 'a')
230
+ log_file.write(f"Profiling log for {func.__name__}\n")
231
+ log_file.write(f"Date: {time.strftime('%Y-%m-%d')}\n")
232
+ log_file.write(f"Time: {time.strftime('%H:%M:%S')}\n\n")
233
+
234
+ def trace_lines(frame, event, arg):
235
+ nonlocal current_line_start_time
236
+ if frame.f_code.co_name == func.__name__:
237
+ lineno = frame.f_lineno
238
+ if event == 'line':
239
+ current_memory = tracemalloc.get_traced_memory()[1] / 10**6 # Convert to MB
240
+ if current_line_start_time is not None:
241
+ elapsed_time = timer() - current_line_start_time
242
+ if lineno in line_execution_times:
243
+ line_execution_times[lineno] += elapsed_time
244
+ else:
245
+ line_execution_times[lineno] = elapsed_time
246
+ line_memory_usage[lineno] = current_memory
247
+
248
+ # Log the profiling data
249
+ if shared_log and log_file:
250
+ log_file.write(f"Line {lineno}: Execution Time: {elapsed_time:.12f}s, Memory Usage: {current_memory:.12f}MB\n")
251
+
252
+ current_line_start_time = timer()
253
+ return trace_lines
254
+
255
+ sys.settrace(trace_lines)
256
+ try:
257
+ result = func(*args, **kwargs)
258
+ finally:
259
+ sys.settrace(None)
260
+ tracemalloc.stop()
261
+
262
+ # Print profiling data
263
+ if log_level == "info":
264
+ print(f"\nLine-by-Line Profiling for '{func.__name__}':")
265
+ source_lines, starting_line = inspect.getsourcelines(func)
266
+ for line_no in sorted(line_execution_times.keys()):
267
+ actual_line = line_no - starting_line + 1
268
+ source_line = source_lines[actual_line - 1].strip()
269
+ exec_time = line_execution_times[line_no]
270
+ mem_usage = line_memory_usage.get(line_no, 0)
271
+ print(f"Line {line_no} ({source_line}): "
272
+ f"Execution Time: {exec_time:.12f}s, "
273
+ f"Memory Usage: {mem_usage:.12f}MB")
274
+ elif log_level == "debug":
275
+ print(f"\n[DEBUG] Line-by-Line Profiling for '{func.__name__}':")
276
+ source_lines, starting_line = inspect.getsourcelines(func)
277
+ for line_no in sorted(line_execution_times.keys()):
278
+ actual_line = line_no - starting_line + 1
279
+ source_line = source_lines[actual_line - 1].strip()
280
+ exec_time = line_execution_times[line_no]
281
+ mem_usage = line_memory_usage.get(line_no, 0)
282
+ print(f"[DEBUG] Line {line_no} ({source_line}): "
283
+ f"Execution Time: {exec_time:.12f}s, "
284
+ f"Memory Usage: {mem_usage:.12f}MB")
285
+
286
+ # Collect the profiling data for the report
287
+ profiling_data = {
288
+ "function_name": func.__name__,
289
+ "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'),
290
+ "arguments": str({'args': args, 'kwargs': kwargs}),
291
+ "return_value": str(result),
292
+ "filepath": inspect.getfile(func),
293
+ "docstring": func.__doc__,
294
+ "line_execution_times": line_execution_times,
295
+ "line_memory_usage": line_memory_usage
296
+ }
297
+
298
+ if export_format:
299
+ file = filename or f"{func.__name__}_lblprofile_report"
300
+ export_profiling_data(profiling_data, func, export_format, file)
301
+
302
+ # Close the log file if shared logging was enabled
303
+ if shared_log and log_file:
304
+ log_file.write("\n") # Add a new line for separation
305
+ log_file.write("-" * 40 + "\n") # Separator between calls
306
+ log_file.write("\n") # Add a new line for separation
307
+ log_file.close()
308
+
309
+ return result
310
+
311
+ return wrapper
312
+
313
+ # If no arguments are provided, it means func is passed directly
314
+ if callable(export_format):
315
+ actual_func = export_format
316
+ export_format = None
317
+ return decorator(actual_func)
318
+
319
+ return decorator
320
+
321
+ def export_profiling_data(
322
+ profiling_data: Dict[str, Dict[int, float]],
323
+ func: Callable,
324
+ export_format: str,
325
+ filename: str
326
+ ) -> None:
327
+ """Export the profiling data to the specified format (JSON, CSV, HTML, XML, MD).
328
+
329
+ Parameters:
330
+ profiling_data (Dict[str, Dict[int, float]]): Profiling data to be exported.
331
+ func (Callable): The function that was profiled.
332
+ export_format (str): The format for export ('json', 'csv', 'html', 'xml', 'md').
333
+ filename (str): The output filename without extension.
334
+ """
335
+ line_execution_times = profiling_data["line_execution_times"]
336
+ line_memory_usage = profiling_data["line_memory_usage"]
337
+
338
+ # Get the source code of the function
339
+ source_lines, starting_line = inspect.getsourcelines(func)
340
+
341
+ # Prepare data for export
342
+ export_data: List[Dict[str, str]] = []
343
+ for line_no in sorted(line_execution_times.keys()):
344
+ actual_line = line_no - starting_line + 1
345
+ source_line = source_lines[actual_line - 1].strip()
346
+ exec_time = line_execution_times[line_no]
347
+ mem_usage = line_memory_usage.get(line_no, 0)
348
+
349
+ # Conditional wrapping based on the export format
350
+ wrapped_source_code = f'"{source_line}"' if export_format == 'csv' else source_line
351
+
352
+ export_data.append({
353
+ 'Function Name': func.__name__,
354
+ 'Line Number': str(line_no),
355
+ 'Source Code': wrapped_source_code, # Use wrapped source code
356
+ 'Execution Time (s)': f"{exec_time:.12f}",
357
+ 'Memory Usage (MB)': f"{mem_usage:.12f}"
358
+ })
359
+
360
+ # Handle different export formats
361
+ if export_format == 'json':
362
+ output_data = {
363
+ "metadata": {
364
+ "profile_type": "line_by_line_profile",
365
+ "export_time": time.strftime('%Y-%m-%d %H:%M:%S'),
366
+ "function_name": func.__name__,
367
+ "filepath": inspect.getfile(func),
368
+ "docstring": func.__doc__
369
+ },
370
+ "profile": export_data
371
+ }
372
+ with open(f"{filename}.json", 'w') as json_file:
373
+ json.dump(output_data, json_file, indent=4)
374
+ print(f"[PROFILER] JSON report generated at: {filename}.json")
375
+
376
+ elif export_format == 'csv':
377
+ output_path = f"{filename}.csv"
378
+ file_mode = 'a' if os.path.exists(output_path) else 'w'
379
+ with open(output_path, mode=file_mode, newline='') as csvfile:
380
+ writer = csv.DictWriter(csvfile, fieldnames=export_data[0].keys())
381
+ if file_mode == 'w':
382
+ writer.writeheader()
383
+ writer.writerows(export_data)
384
+ print(f"[PROFILER] CSV report generated at: {output_path}")
385
+
386
+ elif export_format == 'html':
387
+ output_path = f"{filename}.html"
388
+ file_mode = 'a' if os.path.exists(output_path) else 'w'
389
+ if file_mode == 'w':
390
+ html_content = f"""
391
+ <html>
392
+ <head>
393
+ <title>Line-by-Line Profiling Report</title>
394
+ <style>
395
+ body {{ font-family: Arial, sans-serif; }}
396
+ table {{ border-collapse: collapse; width: 100%; }}
397
+ th, td {{ border: 1px solid #dddddd; text-align: left; padding: 8px; }}
398
+ th {{ background-color: #f2f2f2; }}
399
+ </style>
400
+ </head>
401
+ <body>
402
+ <h2>Line-by-Line Profiling Report for {func.__name__}</h2>
403
+ <table>
404
+ <tr>
405
+ <th>Line Number</th>
406
+ <th>Source Code</th>
407
+ <th>Execution Time (s)</th>
408
+ <th>Memory Usage (MB)</th>
409
+ </tr>
410
+ """
411
+ else:
412
+ with open(output_path, 'r') as f:
413
+ html_content = f.read().replace("</table></body></html>", "")
414
+
415
+ for data in export_data:
416
+ html_content += f"""
417
+ <tr>
418
+ <td>{data['Line Number']}</td>
419
+ <td>{data['Source Code']}</td>
420
+ <td>{data['Execution Time (s)']}</td>
421
+ <td>{data['Memory Usage (MB)']}</td>
422
+ </tr>
423
+ """
424
+ html_content += "</table></body></html>"
425
+ with open(output_path, 'w') as f:
426
+ f.write(html_content)
427
+ print(f"[PROFILER] HTML report generated at: {output_path}")
428
+
429
+ elif export_format == 'xml':
430
+ root = ET.Element("LineByLineProfile")
431
+ for data in export_data:
432
+ line_element = ET.SubElement(root, "Line")
433
+ for key, value in data.items():
434
+ ET.SubElement(line_element, key.replace(' ', '')).text = str(value)
435
+ tree = ET.ElementTree(root)
436
+ tree.write(f"{filename}.xml")
437
+ print(f"[PROFILER] XML report generated at: {filename}.xml")
438
+
439
+ elif export_format == 'md':
440
+ with open(f"{filename}.md", 'w') as f:
441
+ f.write(f"# Line-by-Line Profiling Report for {func.__name__}\n\n")
442
+ f.write("| Line Number | Source Code | Execution Time (s) | Memory Usage (MB) |\n")
443
+ f.write("|-------------|-------------|--------------------|-------------------|\n")
444
+ for data in export_data:
445
+ f.write(f"| {data['Line Number']} | `{data['Source Code']}` | {data['Execution Time (s)']} | {data['Memory Usage (MB)']} |\n")
446
+ print(f"[PROFILER] Markdown report generated at: {filename}.md")
447
+
448
+ elif export_format == 'yaml':
449
+ def to_yaml_string(data):
450
+ lines = []
451
+ for item in data:
452
+ lines.append("-")
453
+ for key, value in item.items():
454
+ if isinstance(value, str) and '\n' in value:
455
+ lines.append(f" {key}: |")
456
+ for line in value.splitlines():
457
+ lines.append(f" {line}")
458
+ else:
459
+ lines.append(f" {key}: {json.dumps(value, ensure_ascii=False)}")
460
+ return "\n".join(lines)
461
+
462
+ with open(f"{filename}.yaml", 'w') as f:
463
+ f.write(to_yaml_string(export_data))
464
+
465
+ elif export_format == 'toml':
466
+ def to_toml_string(data):
467
+ lines = []
468
+ for item in data:
469
+ lines.append("[[profile]]")
470
+ for key, value in item.items():
471
+ if isinstance(value, str) and '\n' in value:
472
+ lines.append(f"{key} = '''\n{value}'''")
473
+ else:
474
+ lines.append(f"{key} = {json.dumps(value, ensure_ascii=False)}")
475
+ return "\n".join(lines)
476
+
477
+ with open(f"{filename}.toml", 'w') as f:
478
+ f.write(to_toml_string(export_data))
479
+
480
+ else:
481
+ print(f"[PROFILER] Unsupported export format: {export_format}")