Object-Oriented Programming/Validation/Python3
main.py
edit"""This program demonstrates use of the Temperature class."""
from temperature import Temperature
temperature = Temperature(celsius=37)
print(temperature.fahrenheit)
temperature = Temperature(fahrenheit=98.6)
print(temperature.celsius)
temperature = Temperature()
temperature.celsius = 37
print(temperature.fahrenheit)
temperature = Temperature()
temperature.fahrenheit = 98.6
print(temperature.celsius)
temperature = Temperature()
print(temperature.to_celsius(98.6))
print(temperature.to_fahrenheit(37))
try:
temperature = Temperature(celsius=1, fahrenheit=2)
except Exception as exception:
print(exception)
try:
temperature = Temperature(celsius='a')
except Exception as exception:
print(exception)
try:
temperature = Temperature(celsius=-500)
except Exception as exception:
print(exception)
try:
temperature = Temperature(fahrenheit='b')
except Exception as exception:
print(exception)
try:
temperature = Temperature(fahrenheit=-600)
except Exception as exception:
print(exception)
try:
temperature = Temperature()
print(temperature.to_celsius('c'))
except Exception as exception:
print(exception)
try:
temperature = Temperature()
print(temperature.to_celsius(-700))
except Exception as exception:
print(exception)
try:
temperature = Temperature()
print(temperature.to_fahrenheit('d'))
except Exception as exception:
print(exception)
try:
temperature = Temperature()
print(temperature.to_fahrenheit(-800))
except Exception as exception:
print(exception)
temperature.py
edit"""Temperature container / converter.
Examples:
from temperature import Temperature
temperature = Temperature(celsius=37)
print(temperature.fahrenheit)
temperature = Temperature(fahrenheit=98.6)
print(temperature.celsius)
temperature = Temperature()
temperature.celsius = 37
print(temperature.fahrenheit)
temperature = Temperature()
temperature.fahrenheit = 98.6
print(temperature.celsius)
temperature = Temperature()
print(temperature.to_celsius(98.6))
print(temperature.to_fahrenheit(37))
"""
class Temperature(object):
"""Supports Celsius and Fahrenheit temperatures."""
def __init__(self, celsius=None, fahrenheit=None):
"""Creates a temperature object.
Args:
celius (float): Optional Celius temperature.
fahrenheit (float): Optional Fahrenheit temperature.
Raises:
ValueError: If both celsius and fahrenheit are initialized.
"""
self._celsius = None
self._fahrenheit = None
if celsius != None and fahrenheit != None:
raise ValueError("Only initialize celsius or fahrenheit, not both.")
if celsius != None:
celsius = self._validate_celsius(celsius)
self.celsius = celsius
if fahrenheit != None:
fahrenheit = self._validate_fahrenheit(fahrenheit)
self.fahrenheit = fahrenheit
@property
def absolute_zero_celsius(self):
"""Gets absolute zero Celsius.
Returns:
float: Absolute zero Celsius temperature.
"""
return -273.15
@property
def absolute_zero_fahrenheit(self):
"""Gets absolute zero Fahrenheit.
Returns:
float: Absolute zero Fahrenheit temperature.
"""
return -459.67
@property
def celsius(self):
"""Gets Celsius temperature.
Returns:
float: Celsius temperature
"""
return self._celsius
@celsius.setter
def celsius(self, value):
"""Sets Celsius temperature.
Args:
value (float): Celsius temperature
"""
value = self._validate_celsius(value)
self._celsius = float(value)
self._fahrenheit = self.to_fahrenheit(self.celsius)
@property
def fahrenheit(self):
"""Gets Fahrenheit temperature.
Returns:
float: Fahrenheit temperature
"""
return self._fahrenheit
@fahrenheit.setter
def fahrenheit(self, value):
"""Sets Fahrenheit temperature.
Args:
value (float): Fahrenheit temperature
Raises:
TypeError: If Fahrenheit temperature is not a valid float.
ValueError: If Fahrenheit temperature is below absolute zero.
"""
value = self._validate_fahrenheit(value)
self._fahrenheit = float(value)
self._celsius = self.to_celsius(self.fahrenheit)
def to_celsius(self, fahrenheit):
"""Converts Fahrenheit temperature to Celsius.
Args:
fahrenheit (float): Fahrenheit temperature to be converted
Returns:
float: Celsius temperature
"""
fahrenheit = self._validate_fahrenheit(fahrenheit)
return (fahrenheit - 32) * 5 / 9
def to_fahrenheit(self, celsius):
"""Converts Celsius temperature to Fahrenheit.
Args:
celsius (float): Celsius temperature to be converted
Returns:
float: Fahrenheit temperature
"""
celsius = self._validate_celsius(celsius)
return celsius * 9 / 5 + 32
def _validate_celsius(self, celsius):
"""Validates Celsius temperature.
Args:
value (float): Celsius temperature
Returns:
float: validated Celsius temperature
Raises:
TypeError: If Celsius temperature is not a valid float.
ValueError: If Celsius temperature is below absolute zero.
"""
try:
celsius = float(celsius)
except ValueError:
raise TypeError("celsius must be a float. Received '" + str(celsius) + "'")
except:
raise
if celsius < self.absolute_zero_celsius:
raise ValueError(
"celsius must not be below absolute zero (%f). Received %f" % (self.absolute_zero_celsius, celsius))
return celsius
def _validate_fahrenheit(self, fahrenheit):
"""Validates Fahrenheit temperature.
Args:
value (float): Fahrenheit temperature
Returns:
float: validated Fahrenheit temperature
Raises:
TypeError: If Fahrenheit temperature is not a valid float.
ValueError: If Fahrenheit temperature is below absolute zero.
"""
try:
fahrenheit = float(fahrenheit)
except ValueError:
raise TypeError("fahrenheit must be a float. Received '" + str(fahrenheit) + "'")
except:
raise
if fahrenheit < self.absolute_zero_fahrenheit:
raise ValueError(
"fahrenheit must not be below absolute zero (%f). Received %f" % (self.absolute_zero_fahrenheit, fahrenheit))
return fahrenheit
Try It
editCopy and paste the code above into one of the following free online development environments or use your own Python3 compiler / interpreter / IDE.