Add etc tasks

This commit is contained in:
Inex Code 2021-12-22 11:16:42 +03:00
parent 7d7d68ad67
commit f2bc29dad0
4 changed files with 101 additions and 0 deletions

45
etc/circle.py Normal file
View File

@ -0,0 +1,45 @@
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, delta_x, delta_y):
self.x = self.x + delta_x
self.y = self.y + delta_y
class Square(Shape):
def __init__(self, side=1, x=0, y=0):
super().__init__(x, y)
self.side = side
class Circle(Shape):
pi = 3.14159
all_circles = []
def __init__(self, radius=1, x=0, y=0):
super().__init__(x, y)
self.radius = radius
self.all_circles.append(self)
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
raise ValueError("Radius cannot be negative")
self._radius = value
@classmethod
def total_area(cls):
area = 0
for circle in cls.all_circles:
area += cls.circle_area(circle.radius)
return area
@staticmethod
def circle_area(radius):
return __class__.pi * radius * radius

12
etc/decorators.py Normal file
View File

@ -0,0 +1,12 @@
def wrap_with_html(fn):
def wrapper_func(*args):
print('<html>')
fn(*args)
print('</html>')
return wrapper_func
@wrap_with_html
def echo(param):
print(param)
echo('Hello')

33
etc/kindahtml.py Normal file
View File

@ -0,0 +1,33 @@
class HtmlElement:
tag = "html"
indent = " "
def __init__(self, content=None, **kwargs):
if content is None:
self.contents = []
else:
self.contents = [content]
self.attributes = kwargs
def append(self, new_content):
self.contents.append(new_content)
def render(self, cur_ind=""):
print(cur_ind + f"<{self.tag}>")
for content in self.contents:
try:
content.render(cur_ind + self.indent)
except AttributeError:
print(cur_ind + self.indent + content)
print(cur_ind + f"</{self.tag}>")
class Body(HtmlElement):
tag = "body"
class Paragraph(HtmlElement):
tag = "p"
para = Paragraph("hello")
body = Body(para)
body.append("world")
doc = HtmlElement(body)
doc.render()

11
etc/nasa.py Normal file
View File

@ -0,0 +1,11 @@
import requests
import json
api_key = "DEMO_KEY"
def get_weather():
url = f"https://api.nasa.gov/insight_weather/?api_key={api_key}&feedtype=json&ver=1.0"
response = requests.get(url)
return json.loads(response.text)
print(get_weather())