From 2008ab9d7bcbaee912b47c081b8e965d0c8e230f Mon Sep 17 00:00:00 2001 From: Amirreza A Date: Tue, 4 Jul 2023 14:27:00 +0330 Subject: [PATCH 1/2] fix #13: added command line argument capabilities. --- pyheat/commandline.py | 3 ++- pyheat/pyheat.py | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pyheat/commandline.py b/pyheat/commandline.py index 49f2bac..231ec4c 100644 --- a/pyheat/commandline.py +++ b/pyheat/commandline.py @@ -35,11 +35,12 @@ def main(): # Adding command line arguments. parser.add_argument('-o', '--out', help='Output file', default=None) parser.add_argument('pyfile', help='Python file to be profiled', default=None) + parser.add_argument('argv', help='Arguments of the python file', nargs='*', default=[]) # Parse command line arguments. arguments = parser.parse_args() if arguments.pyfile is not None: # Core functionality. - pyheat = PyHeat(arguments.pyfile) + pyheat = PyHeat(arguments.pyfile, arguments.argv) pyheat.create_heatmap() pyheat.show_heatmap(output_file=arguments.out, enable_scroll=True) pyheat.close_heatmap() diff --git a/pyheat/pyheat.py b/pyheat/pyheat.py index 3125844..d54c27d 100644 --- a/pyheat/pyheat.py +++ b/pyheat/pyheat.py @@ -23,7 +23,7 @@ class FileDetails(object): heatmaps. """ - def __init__(self, filepath): + def __init__(self, filepath, argv): """Constructor. @param filepath: Path of the file to profile. @@ -32,13 +32,14 @@ def __init__(self, filepath): self.lines = [] self.length = 0 self.data = None + self.argv = [filepath] + argv - def __init__(self, filepath): + def __init__(self, filepath, argv): """Constructor. @param filepath: Path of the file to profile. """ - self.pyfile = PyHeat.FileDetails(filepath) + self.pyfile = PyHeat.FileDetails(filepath, argv) self.line_profiler = None def create_heatmap(self): @@ -83,7 +84,7 @@ def close_heatmap(self): def __profile_file(self): """Method used to profile the given file line by line.""" self.line_profiler = pprofile.Profile() - self.line_profiler.runfile(open(self.pyfile.path, 'r'), {}, self.pyfile.path) + self.line_profiler.runfile(open(self.pyfile.path, 'r'), self.pyfile.argv, self.pyfile.path) def __get_line_profile_data(self): """Method to procure line profiles. From 46ccd809c6ca089c84bb81643ac5f9aef0b2e6c6 Mon Sep 17 00:00:00 2001 From: Amirreza A Date: Tue, 4 Jul 2023 14:53:11 +0330 Subject: [PATCH 2/2] updated the readme files --- README.md | 8 ++++++-- README.rst | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c53ab5..61b775b 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,10 @@ python pyheat/setup.py install ```bash # To view the heatmap. pyheat +# To view the heatmap of a script with arguments. +pyheat [arg1 arg2 ...] # To output the heatmap as a file. -pyheat --out image_file.png +pyheat --out image_file.png pyheat --help ``` @@ -60,7 +62,9 @@ pyheat --help ```python from pyheat import PyHeat -ph = PyHeat() +# PyHeat initializer takes two arguments: script name and the +# list of command line arguments which can be possibly empty. +ph = PyHeat(, [arg1, arg2, ...]) ph.create_heatmap() # To view the heatmap. ph.show_heatmap() diff --git a/README.rst b/README.rst index 7e32f5e..3b2b2f9 100644 --- a/README.rst +++ b/README.rst @@ -65,8 +65,10 @@ As a command # To view the heatmap. pyheat + # To view the heatmap of a script with arguments. + pyheat [arg1 arg2 ...] # To output the heatmap as a file. - pyheat --out image_file.png + pyheat --out image_file.png pyheat --help As a module @@ -75,7 +77,9 @@ As a module .. code:: python from pyheat import PyHeat - ph = PyHeat() + # PyHeat initializer takes two arguments: script name and the + # list of command line arguments which can be possibly empty. + ph = PyHeat(, [arg1, arg2, ...]) ph.create_heatmap() # To view the heatmap. ph.show_heatmap()