8000 Add Bytes parameter by philippjfr · Pull Request #542 · holoviz/param · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add Bytes parameter #542

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 4 commits into from
Jan 7, 2023
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
36 changes: 36 additions & 0 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,42 @@ def get_soft_bounds(bounds, softbounds):
return (l, u)


class Bytes(Parameter):
"""
A Bytes Parameter, with a default value and optional regular
expression (regex) matching.

Similar to the String parameter, but instead of type basestring
this parameter only allows objects of type bytes (e.g. b'bytes').
"""

__slots__ = ['regex']

def __init__(self, default=b"", regex=None, allow_None=False, **kwargs):
super(Bytes, self).__init__(default=default, allow_None=allow_None, **kwargs)
self.regex = regex
self.allow_None = (default is None or allow_None)
self._validate(default)

def _validate_regex(self, val, regex):
if (val is None and self.allow_None):
return
if regex is not None and re.match(regex, val) is None:
raise ValueError("Bytes parameter %r value %r does not match regex %r."
% (self.name, val, regex))

def _validate_value(self, val, allow_None):
if allow_None and val is None:
return
if not isinstance(val, bytes):
raise ValueError("Bytes parameter %r only takes a byte string value, "
"not value of type %s." % (self.name, type(val)))

8000 def _validate(self, val):
self._validate_value(val, self.allow_None)
self._validate_regex(val, self.regex)


class Number(Dynamic):
"""
A numeric Dynamic Parameter, with a default value and optional bounds.
Expand Down
80 changes: 80 additions & 0 deletions tests/API1/testbytesparam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Unit test for Bytes parameters
"""
import sys

import pytest

from . import API1TestCase

import param


ip_regex = br'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

class TestBytesParameters(API1TestCase):

def test_bytes_default_type(self):
if sys.version_info.major < 3:
pytest.skip()

with pytest.raises(ValueError):
class A(param.Parameterized):
s = param.Bytes('abc')

def test_bytes_value_type(self):
if sys.version_info.major < 3:
pytest.skip()

class A(param.Parameterized):
s = param.Bytes()

with pytest.raises(ValueError):
A(s='abc')


def test_regex_ok(self):
class A(param.Parameterized):
s = param.Bytes(b'0.0.0.0', ip_regex)

a = A()
a.s = b'123.123.0.1'

def test_reject_none(self):
class A(param.Parameterized):
s = param.Bytes(b'0.0.0.0', ip_regex)

a = A()

cls = 'class' if sys.version_info.major > 2 else 'type'
exception = "Bytes parameter 's' only takes a byte string value, not value of type <%s 'NoneType'>." % cls
with self.assertRaisesRegex(ValueError, exception):
a.s = None # because allow_None should be False

def test_default_none(self):
class A(param.Parameterized):
s = param.Bytes(None, ip_regex)

a = A()
a.s = b'123.123.0.1'
a.s = None # because allow_None should be True with default of None

def test_regex_incorrect(self):
class A(param.Parameterized):
s = param.Bytes(b'0.0.0.0', regex=ip_regex)

a = A()

b = 'b' if sys.version_info.major > 2 else ''
exception = "Bytes parameter 's' value %s'123.123.0.256' does not match regex %r." % (b, ip_regex)
with self.assertRaises(ValueError) as e:
a.s = b'123.123.0.256'
self.assertEqual(str(e.exception), exception)

def test_regex_incorrect_default(self):
b = 'b' if sys.version_info.major > 2 else ''
exception = "Bytes parameter None value %s'' does not match regex %r." % (b, ip_regex)
with self.assertRaises(ValueError) as e:
class A(param.Parameterized):
s = param.Bytes(regex=ip_regex) # default value '' does not match regular expression
self.assertEqual(str(e.exception), exception)
0