generated from opensafely-core/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2224 from opensafely-core/debug
Add a debug ehrQL command
- Loading branch information
Showing
17 changed files
with
1,231 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import inspect | ||
import sys | ||
|
||
from ehrql.renderers import truncate_table | ||
from ehrql.utils.docs_utils import exclude_from_docs | ||
|
||
|
||
@exclude_from_docs | ||
def show( | ||
element, | ||
*other_elements, | ||
label: str | None = None, | ||
head: int | None = None, | ||
tail: int | None = None, | ||
): | ||
""" | ||
Show the output of the specified element within a dataset definition | ||
_element_<br> | ||
Any element within the dataset definition file; can be a string, constant value etc, | ||
but will typically be a dataset variable (filtered table, column, or a dataset itself.) | ||
_label_<br> | ||
Optional label which will be printed in the debug output. | ||
_head_<br> | ||
Show only the first N lines. If the output is an ehrQL column, table or dataset, it will | ||
print only the first N lines of the table. | ||
_tail_<br> | ||
Show only the last N lines. If the output is an ehrQL column, table or dataset, it will | ||
print only the last N lines of the table. | ||
head and tail arguments can be combined, e.g. to show the first and last 5 lines of a table: | ||
show(<table>, head=5, tail=5) | ||
""" | ||
line_no = inspect.getframeinfo(sys._getframe(1))[1] | ||
elements = [element, *other_elements] | ||
element_reprs = [repr(el) for el in elements] | ||
if head or tail: | ||
element_reprs = [ | ||
truncate_table(el_repr, head, tail) for el_repr in element_reprs | ||
] | ||
label = f" {label}" if label else "" | ||
print(f"Debug line {line_no}:{label}", file=sys.stderr) | ||
for el_repr in element_reprs: | ||
print(el_repr, file=sys.stderr) | ||
|
||
|
||
def stop(*, head: int | None = None, tail: int | None = None): | ||
""" | ||
Stop loading the dataset definition and show the contents of the dataset at this point. | ||
_head_<br> | ||
Show only the first N lines of the dataset. | ||
_tail_<br> | ||
Show only the last N lines of the dataset. | ||
head and tail arguments can be combined, e.g. to show the first and last 5 lines of the dataset: | ||
stop(head=5, tail=5) | ||
""" | ||
line_no = inspect.getframeinfo(sys._getframe(1))[1] | ||
print(f"Stopping at line {line_no}", file=sys.stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.