Skip to content

Commit

Permalink
[New] SaveframeReader class
Browse files Browse the repository at this point in the history
**Background:**
In #356, we added a new utility named `saveframe` in Pyflyby, that can
be used to save information for debugging / reproducing an issue.

**Issue:**
The `saveframe` utility saves data as a pickled Python dictionary.
Reading this raw data and extracting values of specific variables or metadata
fields can be complex.

This commit adds a new class named `SaveframeReader`, for reading data saved by
the `saveframe` utility.

The `SaveframeReader` class provides an easy and efficient way to read the
raw data and extract specific items. This class has a user-friendly `repr`
for visualizing the data and provides various helpful methods to extract
different items.

**Usage Example:**

**Creating an instance**

First, create an instance of this class by passing the path of the file that
contains the `saveframe` data.
```
>>> from pyflyby import SaveframeReader
>>> reader = SaveframeReader('/path/to/file')
```

**Extracting all available metadata fields**

To extract all available metadata fields, use the `SaveframeReader.metadata`
property. Example:
```
>>> reader.metadata
['frame_index', 'filename', 'lineno', 'function_name', 'function_qualname',
'function_object', 'module_name', 'code', 'frame_identifier',
'exception_string', 'exception_full_string', 'exception_class_name',
'exception_class_qualname', 'exception_object', 'traceback']
```

**Extracting all stored local variables**

To extract the names of all local variables stored in the frames, use the
`SaveframeReader.variables` property. Example:
```
>>> reader.variables
{
   1: ['var1', 'var2', ...],
   2: ['var5', 'var8', 'var9', ...],
   ...
}
```

**Extracting the value of a specific metadata field**

To extract the value of a specific metadata field, use the
`SaveframeReader.get_metadata` method. Example:
```
>> reader.get_metadata("filename")
{1: '/dir1/mod1.py', 2: '/dir2/mod2.py', ...}

>> reader.get_metadata("filename", frame_idx=2)
'/dir2/mod2.py'

>> reader.get_metadata("exception_string")
"Error is raised"
```

**Extracting the value of specific local variables**

To extract the value of specific local variable(s), use the
`SaveframeReader.get_variables` method. Example:
```
>> reader.get_variables('var1')
{2: var1_value2, 4: var1_value4}

>>  reader.get_variables('var1', frame_idx=4)
var1_value4

>> reader.get_variables('var2')
var2_value3

>> reader.get_variables(['var1', 'var3'])
{2: {'var1': var1_value2, 'var3': var3_value2},
4: {'var1': var1_value4}, 5: {'var3': var3_value5}}

>> reader.get_variables(['var1', 'var3'], frame_idx=2)
{'var1': var1_value2, 'var3': var3_value2}
```

**NOTE:** Raw data can be extracted using `SaveframeReader.data` property.

Request: PyInf#13916
  • Loading branch information
Sachin Saharan committed Dec 10, 2024
1 parent f2a05eb commit 734f14b
Show file tree
Hide file tree
Showing 4 changed files with 1,173 additions and 1 deletion.
2 changes: 1 addition & 1 deletion etc/pyflyby/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pexpect
import pstats
import pyflyby
from pyflyby import saveframe, xreload
from pyflyby import SaveframeReader, saveframe, xreload
import pylab
import pyodbc
import pysvn
Expand Down
2 changes: 2 additions & 0 deletions lib/python/pyflyby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from pyflyby._log import logger
from pyflyby._parse import PythonBlock, PythonStatement
from pyflyby._saveframe import saveframe
from pyflyby._saveframe_reader \
import SaveframeReader
from pyflyby._version import __version__

# Deprecated:
Expand Down
Loading

0 comments on commit 734f14b

Please sign in to comment.