From 58b0bf51722787546eb29e1b5407896ea617e7c6 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Sun, 29 Sep 2019 15:48:56 +0900 Subject: [PATCH 1/2] Add __new__ function and property of slice Issue #98 --- builtin/builtin.go | 2 +- py/slice.go | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/builtin/builtin.go b/builtin/builtin.go index bb4158c4..3c32d789 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -88,7 +88,7 @@ func init() { "range": py.RangeType, // "reversed": py.ReversedType, "set": py.SetType, - // "slice": py.SliceType, + "slice": py.SliceType, "staticmethod": py.StaticMethodType, "str": py.StringType, // "super": py.SuperType, diff --git a/py/slice.go b/py/slice.go index 53b21493..827ad718 100644 --- a/py/slice.go +++ b/py/slice.go @@ -13,11 +13,11 @@ type Slice struct { Step Object } -var SliceType = NewType("slice", `slice(stop) -> slice object +var SliceType = NewTypeX("slice", `slice(stop) -> slice object "slice(stop) slice(start, stop[, step]) -Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`) +Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`, SliceNew, nil) // Type of this object func (o *Slice) Type() *Type { @@ -151,4 +151,25 @@ func (r *Slice) GetIndices(length int) (start, stop, step, slicelength int, err return } +func init() { + SliceType.Dict["start"] = &Property{ + Fget: func(self Object) (Object, error) { + selfSlice := self.(*Slice) + return selfSlice.Start, nil + }, + } + SliceType.Dict["stop"] = &Property{ + Fget: func(self Object) (Object, error) { + selfSlice := self.(*Slice) + return selfSlice.Stop, nil + }, + } + SliceType.Dict["step"] = &Property{ + Fget: func(self Object) (Object, error) { + selfSlice := self.(*Slice) + return selfSlice.Step, nil + }, + } +} + // Check interface is satisfied From a604dc80aa715164e06e19e8fbd89332d19a6df1 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Sun, 29 Sep 2019 16:08:30 +0900 Subject: [PATCH 2/2] Add tests for slice --- py/tests/slice.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 py/tests/slice.py diff --git a/py/tests/slice.py b/py/tests/slice.py new file mode 100644 index 00000000..8fb84a84 --- /dev/null +++ b/py/tests/slice.py @@ -0,0 +1,16 @@ +# Copyright 2019 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +doc="slice" +a = slice(10) +assert a.start == None +assert a.stop == 10 +assert a.step == None + +a = slice(0, 10, 1) +assert a.start == 0 +assert a.stop == 10 +assert a.step == 1 + +doc="finished" \ No newline at end of file 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