Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement set repr #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func init() {
"object": py.ObjectType,
"range": py.RangeType,
// "reversed": py.ReversedType,
"set": py.SetType,
"slice": py.SliceType,
"set": py.SetType,
"slice": py.SliceType,
"staticmethod": py.StaticMethodType,
"str": py.StringType,
// "super": py.SuperType,
Expand Down
Empty file added coverage.txt
Empty file.
2 changes: 1 addition & 1 deletion parser/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func approxEq(a, b float64) bool {
log.Printf("ApproxEq(a = %#v, b = %#v)", a, b)
diff := a - b
log.Printf("ApproxEq(diff = %e)", diff)
if math.Abs(diff) > 1E-10 {
if math.Abs(diff) > 1e-10 {
log.Printf("ApproxEq(false)")
return false
}
Expand Down
7 changes: 3 additions & 4 deletions py/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ func computeRangeSlice(r *Range, s *Slice) (Object, error) {
sliceLength = computeRangeLength(startIndex, stopIndex, stepIndex)

return &Range{
Start: startIndex,
Stop: stopIndex,
Step: stepIndex,
Start: startIndex,
Stop: stopIndex,
Step: stepIndex,
Length: sliceLength,
}, nil
}
Expand All @@ -238,7 +238,6 @@ var _ I__getitem__ = (*Range)(nil)
var _ I__iter__ = (*Range)(nil)
var _ I_iterator = (*RangeIterator)(nil)


func (a *Range) M__eq__(other Object) (Object, error) {
b, ok := other.(*Range)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion py/range_repr110.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ func (r *Range) repr() (Object, error) {
b.WriteString(")")

return String(b.String()), nil
}
}
2 changes: 1 addition & 1 deletion py/range_repr19.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ func (r *Range) repr() (Object, error) {
b.WriteString(")")

return String(b.String()), nil
}
}
21 changes: 21 additions & 0 deletions py/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package py

import "bytes"

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

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

func (s *Set) M__repr__() (Object, error) {
var out bytes.Buffer
out.WriteRune('{')
spacer := false
for item := range s.items {
if spacer {
out.WriteString(", ")
}
str, err := ReprAsString(item)
if err != nil {
return nil, err
}
out.WriteString(str)
spacer = true
}
out.WriteRune('}')
return String(out.String()), nil
}

func (s *Set) M__iter__() (Object, error) {
items := make(Tuple, 0, len(s.items))
for item := range s.items {
Expand Down
11 changes: 10 additions & 1 deletion py/tests/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
d = a ^ b
assert 1 in c

doc="__repr__"
a = {1, 2, 3}
b = a.__repr__()
assert "{" in b
assert "1" in b
assert "2" in b
assert "3" in b
assert "}" in b

doc="set"
a = set([1,2,3])
b = set("set")
Expand Down Expand Up @@ -83,4 +92,4 @@
assert a.__eq__({1,2,3}) == True
assert a.__ne__({1,2,3}) == False

doc="finished"
doc="finished"
2 changes: 1 addition & 1 deletion symtable/symtable_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ var symtableTestData = []struct {
Children: Children{},
},
},
}, nil,""},
}, nil, ""},
{"def fn(a):\n global b\n global b\n return b", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Expand Down
4 changes: 2 additions & 2 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.`

func time_time(self py.Object) (py.Object, error) {
return py.Float(time.Now().UnixNano()) / 1E9, nil
return py.Float(time.Now().UnixNano()) / 1e9, nil
}

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

Expand Down