Skip to content

Commit 46aebc0

Browse files
author
James Brundage
committed
feat: Template.DoLoop.py / Until.py ( Fixes #929, Fixes #939 )
Adding Until and making documentation more consistent.
1 parent 440d061 commit 46aebc0

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

Languages/Python/Templates/Python-Template-DoLoop.ps.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ Template function DoLoop.py {
77
.NOTES
88
There is not a proper `do` loop in Python, so we have to be a little creative.
99
10-
This will produce a while loop where the `-InitialCondition` should always be true, and,
10+
This will produce a while loop where the `-InitialCondition` should always be true,
11+
and the `-Condition` will be checked at the end of each loop.
12+
13+
If the `Condition` is false, then the loop will break.
1114
.EXAMPLE
1215
Template.DoLoop.py -Condition "False" -Body "print('This happens once')"
1316
#>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Template function UntilLoop.py {
2+
<#
3+
.SYNOPSIS
4+
Template for a Python `until` Loop
5+
.DESCRIPTION
6+
Template for a `until` loop in Python.
7+
.NOTES
8+
There is not a proper `do` or `until` loop in Python, so we have to be a little creative.
9+
10+
This will produce a while loop where the `-InitialCondition` should always be true,
11+
and the `-Condition` will be checked at the end of each iteration.
12+
13+
If the `Condition` is true, then the loop will break.
14+
.EXAMPLE
15+
Template.UntilLoop.py -Condition "True" -Body "print('This happens once')"
16+
#>
17+
param(
18+
# The Loop's Condition.
19+
# This determines if the loop should continue running.
20+
# This defaults to 0 > 1, so that the loop only occurs once
21+
[vbn()]
22+
[string]
23+
$Condition = "False",
24+
25+
# The Loop's Initial Condition.
26+
# Since Python does not have a `do` loop, this needs to be any truthy condition for the loop to run.
27+
[vbn()]
28+
[string]
29+
$InitialCondition = 'True',
30+
31+
# The body of the loop
32+
[vbn()]
33+
[string]
34+
$Body,
35+
36+
# The number of spaces to indent the body.
37+
# By default, two.
38+
[vbn()]
39+
[int]
40+
$BodyIndent = 2,
41+
42+
# The number of spaces to indent all code.
43+
# By default, zero
44+
[vbn()]
45+
[int]
46+
$Indent = 0
47+
)
48+
49+
process {
50+
if ($body -match '^\{') {
51+
$body = $body -replace '^\s{0,}\{' -replace '\}\s{0,}$'
52+
}
53+
if ($Condition -match '^\$') {
54+
$Condition = $Condition -replace '^\$'
55+
}
56+
'' +
57+
$(if ($Indent) {(' ' * $Indent)}) +
58+
(@"
59+
while ${InitialCondition}:
60+
$(' ' * $BodyIndent)$(
61+
$Body -split "(?>\r\n|\n)" -join ([Environment]::NewLine + $(' ' * $BodyIndent))
62+
)
63+
$(' ' * $BodyIndent)$(
64+
"if ${Condition}:$(
65+
[Environment]::NewLine + (' ' * $BodyIndent * 2)
66+
)break"
67+
)
68+
"@ -split '(?>\r\n|\n)' -join ([Environment]::NewLine + $(' ' * $Indent)))
69+
}
70+
}
71+
72+

0 commit comments

Comments
 (0)