forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpxt-python-helpers.ts
More file actions
263 lines (208 loc) · 6.42 KB
/
pxt-python-helpers.ts
File metadata and controls
263 lines (208 loc) · 6.42 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
namespace _py {
export const ATTRIBUTE_ERROR: string = "AttributeError";
export const INDEX_ERROR: string = "IndexError";
export const VALUE_ERROR: string = "ValueError";
export function py_string_capitalize(str: string): string {
nullCheck(str);
return str;
}
export function py_string_casefold(str: string): string {
nullCheck(str);
return str;
}
export function py_string_center(str: string, width: number, fillChar?: string): string {
nullCheck(str);
return str;
}
export function py_string_count(str: string, sub: string, start?: number, end?: number): number {
nullCheck(str);
return 0;
}
export function py_string_endswith(str: string, suffix: string, start?: number, end?: number): boolean {
nullCheck(str);
return false;
}
export function py_string_find(str: string, sub: string, start?: number, end?: number): number {
nullCheck(str);
return 0;
}
export function py_string_index(str: string, sub: string, start?: number, end?: number): number {
nullCheck(str);
return 0;
}
export function py_string_isalnum(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isalpha(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isascii(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isdigit(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isnumeric(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isspace(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isdecimal(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isidentifier(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_islower(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isprintable(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_istitle(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_isupper(str: string): boolean {
nullCheck(str);
return false;
}
export function py_string_join(str: string, iterable: any[]): string {
nullCheck(str);
return str;
}
export function py_string_ljust(str: string, width: number, fillChar?: string): string {
nullCheck(str);
return str;
}
export function py_string_lower(str: string): string {
nullCheck(str);
return str;
}
export function py_string_lstrip(str: string, chars?: string): string {
nullCheck(str);
return str;
}
export function py_string_replace(str: string, oldString: string, newString: string, count?: number): string {
nullCheck(str);
return str;
}
export function py_string_rfind(str: string, sub: string, start?: number, end?: number): number {
nullCheck(str);
return 0;
}
export function py_string_rindex(str: string, sub: string, start?: number, end?: number): number {
nullCheck(str);
return 0;
}
export function py_string_rjust(str: string, width: number, fillChar?: string): string {
nullCheck(str);
return str;
}
export function py_string_rsplit(str: string, sep?: string, maxSplit?: number): string[] {
nullCheck(str);
return [];
}
export function py_string_rstrip(str: string, chars?: string): string {
nullCheck(str);
return str;
}
export function py_string_split(str: string, sep?: string, maxsplit?: number): string[] {
nullCheck(str);
return [];
}
export function py_string_splitlines(str: string, keepends?: boolean): string[] {
nullCheck(str);
return [];
}
export function py_string_startswith(str: string, prefix: string, start?: number, end?: number): boolean {
nullCheck(str);
return false;
}
export function py_string_strip(str: string, chars?: string): string {
nullCheck(str);
return str;
}
export function py_string_swapcase(str: string): string {
nullCheck(str);
return str;
}
export function py_string_title(str: string): string {
nullCheck(str);
return str;
}
export function py_string_upper(str: string): string {
nullCheck(str);
return str;
}
export function py_string_zfill(str: string, width: number): string {
nullCheck(str);
return str;
}
export function py_array_pop(arr: any[], index?: number): any {
nullCheck(arr);
if (arr.length === 0) {
throw INDEX_ERROR;
}
if (index == undefined) {
return arr.pop();
}
else if (index > 0 && index < arr.length) {
return arr.removeAt(index | 0);
}
throw INDEX_ERROR;
}
export function py_array_clear(arr: any[]): void {
nullCheck(arr);
arr.length = 0;
}
export function py_array_index(arr: any[], value: any, start?: number, end?: number): number {
nullCheck(arr);
start = fixIndex(arr, start);
end = fixIndex(arr, end);
if (start == null) {
start = 0;
}
if (end == null) {
// end is exclusive
end = arr.length;
}
for (let i = start; i < end; i++) {
if (arr[i] === value) {
return i;
}
}
throw VALUE_ERROR;
}
export function py_array_count(arr: any[], value: any): number {
nullCheck(arr);
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) count++;
}
return count;
}
function nullCheck(arg: any) {
if (arg == null) {
throw ATTRIBUTE_ERROR;
}
}
function fixIndex(arr: any[], index: number) {
if (index != null && arr.length) {
index = index | 0;
while (index < 0) index += arr.length;
}
return index;
}
}