FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

String and List Methods by kellrott · Pull Request #61 · go-python/gpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (2) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions py/list.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ type List struct {
Items []Object
}

func init() {
ListType.Dict["append"] = MustNewMethod("append", func(self Object, args Tuple) (Object, error) {
listSelf := self.(*List)
if len(args) != 1 {
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
}
listSelf.Items = append(listSelf.Items, args[0])
return NoneType{}, nil
}, 0, "append(item)")

ListType.Dict["extend"] = MustNewMethod("extend", func(self Object, args Tuple) (Object, error) {
listSelf := self.(*List)
if len(args) != 1 {
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
}
if oList, ok := args[0].(*List); ok {
listSelf.Items = append(listSelf.Items, oList.Items...)
}
return NoneType{}, nil
}, 0, "extend([item])")

}

// Type of this List object
func (o *List) Type() *Type {
return ListType
Expand Down
59 changes: 59 additions & 0 deletions py/string.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,65 @@ func init() {
}
return &o, nil
}, 0, "split(sub) -> split string with sub.")

StringType.Dict["startswith"] = MustNewMethod("startswith", func(self Object, args Tuple) (Object, error) {
selfStr := string(self.(String))
prefix := []string{}
if len(args) > 0 {
if s, ok := args[0].(String); ok {
prefix = append(prefix, string(s))
} else if s, ok := args[0].(Tuple); ok {
for _, t := range s {
if v, ok := t.(String); ok {
prefix = append(prefix, string(v))
}
}
} else {
return nil, ExceptionNewf(TypeError, "startswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
}
} else {
return nil, ExceptionNewf(TypeError, "startswith() takes at least 1 argument (0 given)")
}
if len(args) > 1 {
if s, ok := args[1].(Int); ok {
selfStr = selfStr[s:len(selfStr)]
}
}

for _, s := range prefix {
if strings.HasPrefix(selfStr, s) {
return Bool(true), nil
}
}
return Bool(false), nil
}, 0, "startswith(prefix[, start[, end]]) -> bool")

StringType.Dict["endswith"] = MustNewMethod("endswith", func(self Object, args Tuple) (Object, error) {
selfStr := string(self.(String))
suffix := []string{}
if len(args) > 0 {
if s, ok := args[0].(String); ok {
suffix = append(suffix, string(s))
} else if s, ok := args[0].(Tuple); ok {
for _, t := range s {
if v, ok := t.(String); ok {
suffix = append(suffix, string(v))
}
}
} else {
return nil, ExceptionNewf(TypeError, "endswith first arg must be str, unicode, or tuple, not %s", args[0].Type())
}
} else {
return nil, ExceptionNewf(TypeError, "endswith() takes at least 1 argument (0 given)")
}
for _, s := range suffix {
if strings.HasSuffix(selfStr, s) {
return Bool(true), nil
}
}
return Bool(false), nil
}, 0, "endswith(suffix[, start[, end]]) -> bool")

}

// Type of this object
Expand Down
11 changes: 11 additions & 0 deletions py/tests/list.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from libtest import assertRaises

doc="str"
assert str([]) == "[]"
assert str([1,2,3]) == "[1, 2, 3]"
Expand All @@ -22,4 +24,13 @@
assert idxs[idx] == a[idx][0]
assert values[idx] == a[idx][1]

doc="append"
a = [1,2,3]
a.append(4)
assert repr(a) == "[1, 2, 3, 4]"
a = ['a', 'b', 'c']
a.extend(['d', 'e', 'f'])
assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']"
assertRaises(TypeError, lambda: [].append())

doc="finished"
11 changes: 11 additions & 0 deletions py/tests/string.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class C():
assert not( 1 == "potato")
assert 1 != "potato"

doc="startswith"
assert "HELLO THERE".startswith("HELL")
assert not "HELLO THERE".startswith("THERE")
assert "HELLO".startswith("LLO", 2)
assert "HELLO THERE".startswith(("HERE", "HELL"))

doc="endswith"
assert "HELLO THERE".endswith("HERE")
assert not "HELLO THERE".endswith("HELL")
assert "HELLO THERE".endswith(("HELL", "HERE"))

doc="bool"
assert "true"
assert not ""
Expand Down

Back | FazBrowse Home | New Git URL