socket_calculator/calculator.py

38 lines
960 B
Python

class Calculator:
def __init__(self, client, string_control='\n'):
self.client = client
self.string_control = string_control
def calc(self, operation):
fixed_operation = operation + self.string_control
self.client.connect()
self.client.send(
fixed_operation
)
response = self.client.getText(
self.string_control
)
self.client.close()
result = Result(response)
return result.get_content()
class Result:
ok = 'OK'
error = 'ERROR'
separator = ':'
def __init__(self, response_text):
self.response = response_text
self.splited_response = self.response.split(self.separator)
def has_error(self):
if not self.splited_response[0] == self.ok:
return True
return False
def get_content(self):
return self.separator.join(
self.splited_response[1:]
)