-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adding Until and making documentation more consistent.
- Loading branch information
James Brundage
committed
Feb 17, 2024
1 parent
440d061
commit 46aebc0
Showing
2 changed files
with
76 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
Languages/Python/Templates/Python-Template-UntilLoop.ps.ps1
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,72 @@ | ||
Template function UntilLoop.py { | ||
<# | ||
.SYNOPSIS | ||
Template for a Python `until` Loop | ||
.DESCRIPTION | ||
Template for a `until` loop in Python. | ||
.NOTES | ||
There is not a proper `do` or `until` loop in Python, so we have to be a little creative. | ||
This will produce a while loop where the `-InitialCondition` should always be true, | ||
and the `-Condition` will be checked at the end of each iteration. | ||
If the `Condition` is true, then the loop will break. | ||
.EXAMPLE | ||
Template.UntilLoop.py -Condition "True" -Body "print('This happens once')" | ||
#> | ||
param( | ||
# The Loop's Condition. | ||
# This determines if the loop should continue running. | ||
# This defaults to 0 > 1, so that the loop only occurs once | ||
[vbn()] | ||
[string] | ||
$Condition = "False", | ||
|
||
# The Loop's Initial Condition. | ||
# Since Python does not have a `do` loop, this needs to be any truthy condition for the loop to run. | ||
[vbn()] | ||
[string] | ||
$InitialCondition = 'True', | ||
|
||
# The body of the loop | ||
[vbn()] | ||
[string] | ||
$Body, | ||
|
||
# The number of spaces to indent the body. | ||
# By default, two. | ||
[vbn()] | ||
[int] | ||
$BodyIndent = 2, | ||
|
||
# The number of spaces to indent all code. | ||
# By default, zero | ||
[vbn()] | ||
[int] | ||
$Indent = 0 | ||
) | ||
|
||
process { | ||
if ($body -match '^\{') { | ||
$body = $body -replace '^\s{0,}\{' -replace '\}\s{0,}$' | ||
} | ||
if ($Condition -match '^\$') { | ||
$Condition = $Condition -replace '^\$' | ||
} | ||
'' + | ||
$(if ($Indent) {(' ' * $Indent)}) + | ||
(@" | ||
while ${InitialCondition}: | ||
$(' ' * $BodyIndent)$( | ||
$Body -split "(?>\r\n|\n)" -join ([Environment]::NewLine + $(' ' * $BodyIndent)) | ||
) | ||
$(' ' * $BodyIndent)$( | ||
"if ${Condition}:$( | ||
[Environment]::NewLine + (' ' * $BodyIndent * 2) | ||
)break" | ||
) | ||
"@ -split '(?>\r\n|\n)' -join ([Environment]::NewLine + $(' ' * $Indent))) | ||
} | ||
} | ||
|
||
|