Skip to content

Commit c8de432

Browse files
committed
Add string.py
#1
1 parent f663edc commit c8de432

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

pyvalueobjects/strings/string.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pyvalueobjects.abstract.valueobject import ValueObject
2+
from pyvalueobjects.errors.ValueObjectError import ValueObjectError
3+
4+
5+
class String(ValueObject):
6+
7+
_ALLOWED_INPUT_TYPES = {str, int, float, bool}
8+
9+
def __init__(self, value: str):
10+
super().__init__(value)
11+
self._validate(value)
12+
13+
def _validate(self, value):
14+
input_type = type(value)
15+
if input_type != str:
16+
raise ValueObjectError(f'Input type should be: str.')
17+
18+
@classmethod
19+
def from_float(cls, value: float):
20+
if type(value) != float:
21+
raise ValueObjectError('Input type should be float.')
22+
23+
try:
24+
return cls(str(value))
25+
except Exception as _:
26+
raise ValueObjectError('Input type should be valid float.')
27+
28+
@classmethod
29+
def from_int(cls, value: int):
30+
if type(value) != int:
31+
raise ValueObjectError('Input type should be int.')
32+
33+
try:
34+
return cls(str(value))
35+
except Exception as _:
36+
raise ValueObjectError('Input type should be valid int.')

0 commit comments

Comments
 (0)