Skip to content

Commit 0c23b14

Browse files
xarus01corona10
authored andcommitted
Implement set repr (#117)
* Implemented __repr__ of set * Gofmt * Fixed test error * Fixed test error
1 parent 33327c5 commit 0c23b14

File tree

10 files changed

+42
-13
lines changed

10 files changed

+42
-13
lines changed

builtin/builtin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func init() {
8787
"object": py.ObjectType,
8888
"range": py.RangeType,
8989
// "reversed": py.ReversedType,
90-
"set": py.SetType,
91-
"slice": py.SliceType,
90+
"set": py.SetType,
91+
"slice": py.SliceType,
9292
"staticmethod": py.StaticMethodType,
9393
"str": py.StringType,
9494
// "super": py.SuperType,

coverage.txt

Whitespace-only changes.

parser/lexer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func approxEq(a, b float64) bool {
574574
log.Printf("ApproxEq(a = %#v, b = %#v)", a, b)
575575
diff := a - b
576576
log.Printf("ApproxEq(diff = %e)", diff)
577-
if math.Abs(diff) > 1E-10 {
577+
if math.Abs(diff) > 1e-10 {
578578
log.Printf("ApproxEq(false)")
579579
return false
580580
}

py/range.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ func computeRangeSlice(r *Range, s *Slice) (Object, error) {
226226
sliceLength = computeRangeLength(startIndex, stopIndex, stepIndex)
227227

228228
return &Range{
229-
Start: startIndex,
230-
Stop: stopIndex,
231-
Step: stepIndex,
229+
Start: startIndex,
230+
Stop: stopIndex,
231+
Step: stepIndex,
232232
Length: sliceLength,
233233
}, nil
234234
}
@@ -238,7 +238,6 @@ var _ I__getitem__ = (*Range)(nil)
238238
var _ I__iter__ = (*Range)(nil)
239239
var _ I_iterator = (*RangeIterator)(nil)
240240

241-
242241
func (a *Range) M__eq__(other Object) (Object, error) {
243242
b, ok := other.(*Range)
244243
if !ok {

py/range_repr110.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ func (r *Range) repr() (Object, error) {
3535
b.WriteString(")")
3636

3737
return String(b.String()), nil
38-
}
38+
}

py/range_repr19.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ func (r *Range) repr() (Object, error) {
3535
b.WriteString(")")
3636

3737
return String(b.String()), nil
38-
}
38+
}

py/set.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package py
1010

11+
import "bytes"
12+
1113
var SetType = NewTypeX("set", "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements.", SetNew, nil)
1214

1315
type SetValue struct{}
@@ -102,6 +104,25 @@ func (s *Set) M__bool__() (Object, error) {
102104
return NewBool(len(s.items) > 0), nil
103105
}
104106

107+
func (s *Set) M__repr__() (Object, error) {
108+
var out bytes.Buffer
109+
out.WriteRune('{')
110+
spacer := false
111+
for item := range s.items {
112+
if spacer {
113+
out.WriteString(", ")
114+
}
115+
str, err := ReprAsString(item)
116+
if err != nil {
117+
return nil, err
118+
}
119+
out.WriteString(str)
120+
spacer = true
121+
}
122+
out.WriteRune('}')
123+
return String(out.String()), nil
124+
}
125+
105126
func (s *Set) M__iter__() (Object, error) {
106127
items := make(Tuple, 0, len(s.items))
107128
for item := range s.items {

py/tests/set.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@
5656
d = a ^ b
5757
assert 1 in c
5858

59+
doc="__repr__"
60+
a = {1, 2, 3}
61+
b = a.__repr__()
62+
assert "{" in b
63+
assert "1" in b
64+
assert "2" in b
65+
assert "3" in b
66+
assert "}" in b
67+
5968
doc="set"
6069
a = set([1,2,3])
6170
b = set("set")
@@ -83,4 +92,4 @@
8392
assert a.__eq__({1,2,3}) == True
8493
assert a.__ne__({1,2,3}) == False
8594

86-
doc="finished"
95+
doc="finished"

symtable/symtable_data_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ var symtableTestData = []struct {
514514
Children: Children{},
515515
},
516516
},
517-
}, nil,""},
517+
}, nil, ""},
518518
{"def fn(a):\n global b\n global b\n return b", "exec", &SymTable{
519519
Type: ModuleBlock,
520520
Name: "top",

time/time.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Return the current time in seconds since the Epoch.
1818
Fractions of a second may be present if the system clock provides them.`
1919

2020
func time_time(self py.Object) (py.Object, error) {
21-
return py.Float(time.Now().UnixNano()) / 1E9, nil
21+
return py.Float(time.Now().UnixNano()) / 1e9, nil
2222
}
2323

2424
// func floatclock(_Py_clock_info_t *info) (py.Object, error) {
@@ -141,7 +141,7 @@ func time_sleep(self py.Object, args py.Tuple) (py.Object, error) {
141141
if secs < 0 {
142142
return nil, py.ExceptionNewf(py.ValueError, "sleep length must be non-negative")
143143
}
144-
time.Sleep(time.Duration(secs * 1E9))
144+
time.Sleep(time.Duration(secs * 1e9))
145145
return py.None, nil
146146
}
147147

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy