8000 stdlib/string: first import · go-python/gpython@2cc4648 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cc4648

Browse files
committed
stdlib/string: first import
1 parent 51a6831 commit 2cc4648

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

stdlib/stdlib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
_ "github.com/go-python/gpython/stdlib/builtin"
2222
_ "github.com/go-python/gpython/stdlib/math"
23+
_ "github.com/go-python/gpython/stdlib/string"
2324
_ "github.com/go-python/gpython/stdlib/sys"
2425
_ "github.com/go-python/gpython/stdlib/time"
2526
)

stdlib/string/string.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package string provides the implementation of the python's 'string' module.
6+
package string
7+
8+
import "github.com/go-python/gpython/py"
9+
10+
func init() {
11+
methods := []*py.Method{
12+
// py.MustNewMethod("get_clock_info", time_get_clock_info, 0, get_clock_info_doc),
13+
}
14+
15+
py.RegisterModule(&py.ModuleImpl{
16+
Info: py.ModuleInfo{
17+
Name: "string",
18+
Doc: module_doc,
19+
},
20+
Methods: methods,
21+
Globals: py.StringDict{
22+
"whitespace": whitespace,
23+
"ascii_lowercase": ascii_lowercase,
24+
"ascii_uppercase": ascii_uppercase,
25+
"ascii_letters": ascii_letters,
26+
"digits": digits,
27+
"hexdigits": hexdigits,
28+
"octdigits": octdigits,
29+
"punctuation": punctuation,
30+
"printable": printable,
31+
},
32+
})
33+
}
34+
35+
const module_doc = `A collection of string constants.
36+
37+
Public module variables:
38+
39+
whitespace -- a string containing all ASCII whitespace
40+
ascii_lowercase -- a string containing all ASCII lowercase letters
41+
ascii_uppercase -- a string containing all ASCII uppercase letters
42+
ascii_letters -- a string containing all ASCII letters
43+
digits -- a string containing all ASCII decimal digits
44+
hexdigits -- a string containing all ASCII hexadecimal digits
45+
octdigits -- a string containing all ASCII octal digits
46+
punctuation -- a string containing all ASCII punctuation characters
47+
printable -- a string containing all ASCII characters considered printable
48+
`
49+
50+
var (
51+
whitespace = py.String(" \t\n\r\x0b\x0c")
52+
ascii_lowercase = py.String("abcdefghijklmnopqrstuvwxyz")
53+
ascii_uppercase = py.String("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
54+
ascii_letters = ascii_lowercase + ascii_uppercase
55+
digits = py.String("0123456789")
56+
hexdigits = py.String("0123456789abcdefABCDEF")
57+
octdigits = py.String("01234567")
58+
punctuation = py.String("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
59+
printable = py.String("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c")
60+
)

stdlib/string/string_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package string_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/go-python/gpython/pytest"
11+
)
12+
13+
func TestString(t *testing.T) {
14+
pytest.RunScript(t, "./testdata/test.py")
15+
}

stdlib/string/testdata/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
import string
6+
7+
print("globals:")
8+
for name in ("whitespace",
9+
"ascii_lowercase",
10+
"ascii_uppercase",
11+
"ascii_letters",
12+
"digits",
13+
"hexdigits",
14+
"octdigits",
15+
"punctuation",
16+
"printable"):
17+
v = getattr(string, name)
18+
print("\nstring.%s:\n%s" % (name,repr(v)))
19+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
globals:
2+
3+
string.whitespace:
4+
' \t\n\r\x0b\x0c'
5+
6+
string.ascii_lowercase:
7+
'abcdefghijklmnopqrstuvwxyz'
8+
9+
string.ascii_uppercase:
10+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11+
12+
string.ascii_letters:
13+
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
14+
15+
string.digits:
16+
'0123456789'
17+
18+
string.hexdigits:
19+
'0123456789abcdefABCDEF'
20+
21+
string.octdigits:
22+
'01234567'
23+
24+
string.punctuation:
25+
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
26+
27+
string.printable:
28+
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

0 commit comments

Comments
 (0)
0