123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #classmethod staticmethod
- import json
- class Example:
- def hello():
- print('hello')
- '''Не привязывается ни к экземпляру ни к классу.
- Не имеет доступа к self и не может его изменить.'''
- @staticmethod
- def static_hello():
- print('static hello')
- '''Принимае объект самого класса.
- Может создавать новые экземпляры класса.'''
- @classmethod
- def class_hello(cls):
- print(f'class hello {cls}')
- class TemperatureConverter:
- @staticmethod
- def celsius_to_fahrenheit(val):
- return val*9/5 + 32
- @staticmethod
- def fahrenheit_to_celsius(val):
- return (val - 32)*5/9
- @staticmethod
- def celsius_to_kelvin(val):
- return val + 273.15
- @staticmethod
- def kelvin_to_celsius(val):
- return val - 273.15
- @staticmethod
- def fahrenheit_to_kelvin(val):
- return round(((val - 32)*5/9 + 273.15), 2)
- @staticmethod
- def kelvin_to_fahrenheit(val):
- return round(((val - 273.15)*9/5 + 32), 2)
- @staticmethod
- def format(val, sim: str):
- return f"{val}°{sim}"
- class Circle:
- def __init__(self, radius):
- if not Circle.is_positive(radius):
- raise ValueError("Радиус должен быть положительным")
- self.radius = radius
- @classmethod
- def from_diameter(cls, value):
- return cls(value/2.0)
- @staticmethod
- def is_positive(value):
- return True if value >= 0 else False
- @staticmethod
- def area(radius):
- return 3.14*radius**2
- class AppConfig:
- __config = {}
- def __init__(self):
- pass
- @classmethod
- def load_config(cls, file_name: str):
- with open(file_name, "r") as f:
- cls.__config = json.load(f)
-
- @classmethod
- def get_config(cls, key : str):
- if '.' in key:
- external_key, internal_key = key.split('.')
- if external_key in cls.__config.keys() and internal_key in cls.__config[external_key].keys():
- return cls.__config[external_key][internal_key]
- else:
- return None
- else:
- if key in cls.__config.keys():
- return cls.__config[key]
- else:
- return None
- def main():
- AppConfig.load_config('app_config.json')
- # Получение значения конфигурации
- assert AppConfig.get_config('database') == {
- 'host': '127.0.0.1', 'port': 5432,
- 'database_name': 'postgres_db',
- 'user': 'owner',
- 'password': 'ya_vorona_ya_vorona'}
- assert AppConfig.get_config('database.user') == 'owner'
- assert AppConfig.get_config('database.password') == 'ya_vorona_ya_vorona'
- assert AppConfig.get_config('database.pass') is None
- assert AppConfig.get_config('password.database') is None
- config = AppConfig()
- assert config.get_config('max_connections') == 10
- assert config.get_config('min_connections') is None
- conf = AppConfig()
- assert conf.get_config('max_connections') == 10
- assert conf.get_config('database.user') == 'owner'
- assert conf.get_config('database.host') == '127.0.0.1'
- assert conf.get_config('host') is None
- print('Good')
- if __name__ == '__main__':
- main()
|