Skip to content

Commit

Permalink
fix(python): add docs + newline fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunSHamilton committed Dec 18, 2023
1 parent e860487 commit 3135fbb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 17 deletions.
76 changes: 67 additions & 9 deletions docs/python.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Python

## Browser

Testing a function:
## `getDef`

```javascript,mdbook-runnable,hidelines=#
# {{#rustdoc_include tools/helpers.js:1}}
Expand All @@ -11,9 +9,9 @@ a = 1
b = 2
def add(x, y):
result = x + y
print(f"{x} + {y} = {result}")
return result
result = x + y
print(f"{x} + {y} = {result}")
return result
`;
Expand All @@ -28,6 +26,66 @@ def add(x, y):
}
```

## `getBlock`

```javascript,mdbook-runnable,hidelines=#
# {{#rustdoc_include tools/helpers.js:1}}
const code = `
a = 1
b = 2
def add_or_subtract(a, b, add=True):
if add:
return a + b
else:
return a - b
`;
{
const equivalentPatterns = [
"if add",
/(if|elif) add/,
];
for (const pattern of equivalentPatterns) {
const ifBlock = __helpers.python.getBlock(code, pattern);
const { block_body, block_indentation, block_condition } = ifBlock;
console.assert(block_indentation === 4);
console.assert(block_condition === "if add");
console.assert(block_body.match(/return a \+ b/));
console.log(ifBlock);
}
}
```

## `removeComments`

```javascript,mdbook-runnable,hidelines=!!!!
!!!!{{!!!!#rustdoc_include tools/helpers.js}}
const code = `
a = 1
# comment
def b(d, e):
a = 2
# comment
return a #comment
`;
{
const commentlessCode = __helpers.python.removeComments(code);
console.log(commentlessCode);
console.assert(commentlessCode === `
a = 1
def b(d, e):
a = 2
return a
`);
}
```

## `__pyodide.runPython`

Running the code of a singluar function to get the output:

```javascript,mdbook-runnable,hidelines=#
Expand Down Expand Up @@ -69,8 +127,8 @@ assert add(a, b) == 300

```python
def
add(x, y):
return x + y
add(x, y):
return x + y
```

- Python **does** allow newline characters between function arguments. E.g:
Expand All @@ -80,5 +138,5 @@ def add(
x,
y
):
return x + y
return x + y
```
38 changes: 36 additions & 2 deletions docs/tools/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const __helpers = {
python: {
getDef: (code, functionName) => {
const regex = new RegExp(
`\\n(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
"s"
`^(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
"ms"
);

const matchedCode = regex.exec(code);
Expand All @@ -25,5 +25,39 @@ const __helpers = {

return null;
},
getBlock: (code, blockPattern) => {
const escapedBlockPattern =
blockPattern instanceof RegExp
? blockPattern.source
: blockPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

const regex = new RegExp(
`^(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*:\\n(?<block_body>.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
"ms"
);

const matchedCode = regex.exec(code);
if (matchedCode) {
/* eslint-disable camelcase */
const { block_body, block_indentation, block_condition } =
matchedCode.groups;

const blockIndentationSansNewLine = block_indentation.replace(
/\n+/g,
""
);
return {
block_body,
block_condition,
block_indentation: blockIndentationSansNewLine.length,
};
/* eslint-enable camelcase */
}

return null;
},
removeComments: (code) => {
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(#.*$)/gm, "");
},
},
};
11 changes: 5 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const getIsDeclaredAfter = (styleRule: CSSStyleRule) => (selector: string) => {
export module python {
export function getDef(code: string, functionName: string) {
const regex = new RegExp(
`\\n(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
"s"
`^(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
"ms"
);

const matchedCode = regex.exec(code);
Expand All @@ -113,8 +113,7 @@ export module python {
""
);
return {
// Entire function definition without additional \n
def: matchedCode[0].slice(1),
def: matchedCode[0],
function_parameters,
function_body,
function_indentation: functionIndentationSansNewLine.length,
Expand Down Expand Up @@ -142,8 +141,8 @@ export module python {
: blockPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

const regex = new RegExp(
`\\n(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*:\\n(?<block_body>.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
"s"
`^(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*:\\n(?<block_body>.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
"ms"
);

const matchedCode = regex.exec(code);
Expand Down

0 comments on commit 3135fbb

Please sign in to comment.