From df14413f3d838ccca204ae40012144f297d4834c Mon Sep 17 00:00:00 2001 From: Sarfaraz Ghulam Iraqui Date: Tue, 6 Mar 2018 16:40:08 +0530 Subject: [PATCH 1/4] implement bytearray.isalnum() --- python/common/org/python/types/ByteArray.java | 12 ++++++++++-- python/common/org/python/types/Bytes.java | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/common/org/python/types/ByteArray.java b/python/common/org/python/types/ByteArray.java index 13e5f91f27..3b88f5aecb 100644 --- a/python/common/org/python/types/ByteArray.java +++ b/python/common/org/python/types/ByteArray.java @@ -761,8 +761,16 @@ public org.python.Object index(java.util.List args, java.util @org.python.Method( __doc__ = "B.isalnum() -> bool\n\nReturn True if all characters in B are alphanumeric\nand there is at least one character in B, False otherwise." ) - public org.python.Object isalnum(java.util.List args, java.util.Map kwargs, java.util.List default_args, java.util.Map default_kwargs) { - throw new org.python.exceptions.NotImplementedError("bytearray.isalnum has not been implemented."); + public org.python.Object isalnum() { + if (this.value.length == 0) { + return new org.python.types.Bool(false); + } + for (byte ch: this.value) { + if (!Bytes._isalpha(ch) && !Bytes._isnum(ch)) { + return new org.python.types.Bool(false); + } + } + return new org.python.types.Bool(true); } @org.python.Method( diff --git a/python/common/org/python/types/Bytes.java b/python/common/org/python/types/Bytes.java index 9da9e7573d..6b0a431c54 100644 --- a/python/common/org/python/types/Bytes.java +++ b/python/common/org/python/types/Bytes.java @@ -1086,6 +1086,10 @@ public org.python.Object isalpha() { return new Bool(_isalpha(this.value)); } + public static boolean _isnum(byte ch) { + return (ch >= '0' && ch <= '9'); + } + @org.python.Method( __doc__ = "B.isdigit() -> bool\n\nReturn True if all characters in B are digits\nand there is at least one character in B, False otherwise." ) From 80c55c87a90dad6729a3d18e7f12a408f0358ab8 Mon Sep 17 00:00:00 2001 From: Sarfaraz Ghulam Iraqui Date: Tue, 6 Mar 2018 23:44:48 +0530 Subject: [PATCH 2/4] tests for bytearray isalnum() and isdigit() --- tests/datatypes/test_bytearray.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/datatypes/test_bytearray.py b/tests/datatypes/test_bytearray.py index 8f69a661d1..6b2b556abe 100644 --- a/tests/datatypes/test_bytearray.py +++ b/tests/datatypes/test_bytearray.py @@ -228,6 +228,33 @@ def test_startswith(self): print(bytearray(b'').startswith(b'')) """) + def test_isalnum(self): + self.assertCodeExecution(""" + print(bytearray(b'0').isalnum()) + print(bytearray(b'9').isalnum()) + print(bytearray(b'1234567890').isalnum()) + print(bytearray(b'89A23gM23z').isalnum()) + print(bytearray(b':923').isalnum()) + print(bytearray(b'\\923').isalnum()) + print(bytearray(b' jdf fhd 33').isalnum()) + print(bytearray(b'@#$%^&*').isalnum()) + print(bytearray(b'"478\t47ads:').isalnum()) + print(bytearray(b'AbZ').isalnum()) + """) + + def test_isdigit(self): + self.assertCodeExecution(""" + print(bytearray(b'0').isdigit()) + print(bytearray(b'9').isdigit()) + print(bytearray(b'1234567890').isdigit()) + print(bytearray(b'8923g23823').isdigit()) + print(bytearray(b'923').isdigit()) + print(bytearray(b'\\923').isdigit()) + print(bytearray(b'000').isdigit()) + print(bytearray(b'@#$%^&*').isdigit()) + print(bytearray(b'"478\t47ads:').isdigit()) + print(bytearray(b'AbZ').isdigit()) + """) class UnaryBytearrayOperationTests(UnaryOperationTestCase, TranspileTestCase): data_type = 'bytearray' From 6ca14b80de1d193b936a7e2a648f79b6c098973b Mon Sep 17 00:00:00 2001 From: Sarfaraz Ghulam Iraqui Date: Tue, 6 Mar 2018 23:46:16 +0530 Subject: [PATCH 3/4] implement bytearray isdigit() --- python/common/org/python/types/ByteArray.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python/common/org/python/types/ByteArray.java b/python/common/org/python/types/ByteArray.java index 3b88f5aecb..a06698e985 100644 --- a/python/common/org/python/types/ByteArray.java +++ b/python/common/org/python/types/ByteArray.java @@ -783,8 +783,16 @@ public org.python.Object isalpha() { @org.python.Method( __doc__ = "B.isdigit() -> bool\n\nReturn True if all characters in B are digits\nand there is at least one character in B, False otherwise." ) - public org.python.Object isdigit(java.util.List args, java.util.Map kwargs, java.util.List default_args, java.util.Map default_kwargs) { - throw new org.python.exceptions.NotImplementedError("bytearray.isdigit has not been implemented."); + public org.python.Object isdigit() { + if (this.value.length == 0) { + return new org.python.types.Bool(false); + } + for (byte ch: this.value) { + if (!Bytes._isnum(ch)) { + return new org.python.types.Bool(false); + } + } + return new org.python.types.Bool(true); } @org.python.Method( From 8b4324ca5526d57a41e592672db10cc2de7c0bde Mon Sep 17 00:00:00 2001 From: Sarfaraz Ghulam Iraqui Date: Wed, 7 Mar 2018 00:19:44 +0530 Subject: [PATCH 4/4] use Bytes isalnum() and isdigit() in ByteArray --- python/common/org/python/types/ByteArray.java | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/python/common/org/python/types/ByteArray.java b/python/common/org/python/types/ByteArray.java index a06698e985..74811b91f6 100644 --- a/python/common/org/python/types/ByteArray.java +++ b/python/common/org/python/types/ByteArray.java @@ -762,15 +762,7 @@ public org.python.Object index(java.util.List args, java.util __doc__ = "B.isalnum() -> bool\n\nReturn True if all characters in B are alphanumeric\nand there is at least one character in B, False otherwise." ) public org.python.Object isalnum() { - if (this.value.length == 0) { - return new org.python.types.Bool(false); - } - for (byte ch: this.value) { - if (!Bytes._isalpha(ch) && !Bytes._isnum(ch)) { - return new org.python.types.Bool(false); - } - } - return new org.python.types.Bool(true); + return (new Bytes(this.value)).isalnum(); } @org.python.Method( @@ -784,15 +776,7 @@ public org.python.Object isalpha() { __doc__ = "B.isdigit() -> bool\n\nReturn True if all characters in B are digits\nand there is at least one character in B, False otherwise." ) public org.python.Object isdigit() { - if (this.value.length == 0) { - return new org.python.types.Bool(false); - } - for (byte ch: this.value) { - if (!Bytes._isnum(ch)) { - return new org.python.types.Bool(false); - } - } - return new org.python.types.Bool(true); + return (new Bytes(this.value)).isdigit(); } @org.python.Method(