Skip to content

Commit

Permalink
Merge pull request beeware#690 from cnlenzc/bytes_partition
Browse files Browse the repository at this point in the history
create function Bytes.partition
  • Loading branch information
eliasdorneles committed Nov 2, 2017
2 parents 90fc073 + 28073b7 commit 6acc30b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
21 changes: 17 additions & 4 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,23 @@ public org.python.Object maketrans(java.util.List<org.python.Object> args, java.
}

@org.python.Method(
__doc__ = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytes objects."
)
public org.python.Object partition(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.partition has not been implemented.");
__doc__ = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytes objects.",
args = {"sep"}
)
public org.python.Object partition(org.python.Object sep) {
Tuple result = new Tuple();
int pos = (int) this.find(sep, null, null).value;
if (pos < 0) {
result.value.add(this);
result.value.add(new Bytes(""));
result.value.add(new Bytes(""));
}
else {
result.value.add(new Bytes(Arrays.copyOfRange(this.value, 0, pos)));
result.value.add(sep);
result.value.add(new Bytes(Arrays.copyOfRange(this.value, pos + ((Bytes)sep).value.length, this.value.length)));
}
return result;
}

@org.python.Method(
Expand Down
14 changes: 14 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ def test_capitalize(self):
print(b'\xc8'.capitalize())
""")

def test_partition(self):
self.assertCodeExecution(r"""
print(b'hello, world'.partition(b','))
print(b'hello, world'.partition(b'h'))
print(b'hello, world'.partition(b'd'))
print(b'hello, world'.partition(b', '))
print(b'hello, world'.partition(b'l'))
print(b'2015638687'.partition(b'a'))
print(b'\xc8'.partition(b' '))
""")
# self.assertCodeExecution(r"""
# print(b'hello, world'.partition(None))
# """, exits_early=True)

def test_repr(self):
self.assertCodeExecution(r"""
print(repr(b'\xc8'))
Expand Down

0 comments on commit 6acc30b

Please sign in to comment.