-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerpy.ps1
246 lines (208 loc) · 5.64 KB
/
powerpy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Load all the PowerPy library files
. sys\file.ps1
. sys\stdout.ps1
. request\requst.ps1
. os\os.ps1
. sys\sys.ps1
# Regex for any numbers at the end of a line
$numbers = "^\d+$"
# This resets the stdout whenever powerpy is loaded
[stdout]::stdout = [stdout]::new()
# This function is used to print out any data to the console/terminal
function print($data, $end="", $sep=", ")
{
# Assigns the result value
$result = $data
# Checks if the data given is an array, if so prints in perferred array format
if($data -is [array])
{
$tempResult = "["
foreach($entry in $data){
$tempResult += [string]$entry + $sep
}
$result = $tempResult.Substring(0,$tempResult.length -2) + "]"
}
# Writes the output to the console/log/thing that is listening
Write-Output "$result$end"
}
# This allows for the length of a given variable to be found in a pythonic way
function len($data)
{
if($data -is [string]){
return $data.length
}
elseif($data -is [array]){
return $data.length
}else{
return $data.length
}
}
# This will allow for the int version of a string to be returned
function int($data)
{
if([string]$data -match $numbers)
{
return [int] $data
}
}
# Converts a native array into a list
function list($array){
$list = [list]::new()
$list.data = $array
return $list
}
function type_of($object){
return $object.GetType()
}
# This is a wrapper class around the native array class providing additional functionality
Class list{
[array] $data = @()
# Adds an object to the list
add($object){
if($this.data -ne @()){
$this.data += $object
}else{
$this.data = @($object)
}
}
# Removes either the object at the given index OR the object in the list with that value
remove($index){
$newList = @()
if($this.contains($index)){
foreach($entry in $this.data){
if($entry -ne $index){
$newList += $entry
}
}
}else{
$indexPos = 0
foreach($entry in $this.data){
if ($indexPos -ne $index){
$newList += $entry
}
$indexPos += 1
}
}
$this.data = $newList
}
remove_index($index){
$newList = @()
$indexPos = 0
foreach($entry in $this.data){
if ($indexPos -ne $index){
$newList += $entry
}
$indexPos += 1
}
$this.data = $newList
}
# Gets an object at the given index
[string] get($index)
{
if($index -lt (len($this.data))){
return [string] $this.data[$index]
}
return [string] "null"
}
# Checks if the object is in the list
[bool] contains($object){
foreach($entry in $this.data){
if($entry -eq $object){
return $true
}
}
return $false
}
# Gets the index of an object if in the list
[int] index($object){
$index = 0
foreach($entry in $this.data){
if($entry -eq $object){
return $index
}
$index += 1
}
return -1
}
[string] ToString(){
$string = "["
$isFirst = $true
foreach ($entry in $this.data){
if (!($isFirst)){
$string += ", "
}
$isFirst = $false
$string += "$($entry)"
}
$string += "]"
return $string
}
[string] GetType(){
return "list"
}
}
Class dict{
# Creates the key and value lists for the dict class
[list] $keys = [list]::new()
[list] $values = [list]::new()
# Sets the key-value pair
set($key, $value){
# Checks if a value with the given key already exists
if(!($this.keys.contains($key))){
$this.keys.add($key)
$this.values.add($value)
# If exists removes and replaces existing pair with provided pair
}else{
$index = $this.keys.index($key)
$this.keys.remove($key)
$this.values.remove($index)
$this.keys.add($key)
$this.values.add($value)
}
}
# Gets the value for a given key
[object] get($key){
if ($this.keys.contains($key)){
$index = $this.keys.index($key)
return $this.values.get($index)
}
return $null
}
# Removes the key-value pair for the given key if exists
remove($key){
if($this.keys.contains($key)){
$index = $this.keys.index($key)
$this.keys.remove($key)
$this.values.remove_index($index)
}
}
# Removes the key-value pair from the dict and returns the value for the given key if exists
[object] pop($key){
if($this.keys.contains($key)){
$value = $this.values.get($this.keys.index($key))
$this.remove($key)
return $value
}
return $null
}
# Converts the dict into a string
[string] ToString(){
$string = "{"
$isFirst = $true
foreach($key in $this.keys.data){
if(!($isFirst)){
$string += ", "
}
$isFirst = $false
$index = $this.keys.index($key)
$keyStr = $this.keys.get($index)
$value = $this.values.get($index)
$string += "$($keyStr):$($value)"
}
$string += "}"
return $string
}
[string] GetType(){
return "dict"
}
}