adding all files done so far
This commit is contained in:
32
learning/super/class_1.py
Normal file
32
learning/super/class_1.py
Normal file
@@ -0,0 +1,32 @@
|
||||
class Celsius:
|
||||
def __init__(self, temperature=0):
|
||||
print('initalising')
|
||||
self.temperature = temperature
|
||||
|
||||
def __repr__(self):
|
||||
return(f'current temperature is {self.temperature} degrees C')
|
||||
|
||||
def to_fahrenheit(self):
|
||||
return (self.temperature * 1.8) + 32
|
||||
|
||||
def get_temperature(self):
|
||||
print("Getting value")
|
||||
return self._temperature
|
||||
|
||||
def set_temperature(self, value):
|
||||
if value < -273:
|
||||
raise ValueError("Temperature below -273 is not possible")
|
||||
print("Setting value")
|
||||
self._temperature = value
|
||||
return value
|
||||
|
||||
temperature = property(get_temperature, set_temperature)
|
||||
|
||||
|
||||
# c = Celsius(20)
|
||||
# print(Celsius(20).get_temperature)
|
||||
|
||||
# print(Celsius(20).set_temperature(25))
|
||||
# Celsius(20).get_temperature()
|
||||
c = Celsius()
|
||||
print(c.to_fahrenheit())
|
||||
33
learning/super/notes.txt
Normal file
33
learning/super/notes.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
children precede their parents and the order of appearance in __bases__ is respected.
|
||||
|
||||
why not to use super in object classes: The problem with incompatible signatures https://www.artima.com/weblogs/viewpost.jsp?thread=281127
|
||||
|
||||
why (swap the print and super in each command)
|
||||
class First(object):
|
||||
def __init__(self):
|
||||
super(First, self).__init__()
|
||||
print("first")
|
||||
|
||||
class Second(object):
|
||||
def __init__(self):
|
||||
super(Second, self).__init__()
|
||||
print("second")
|
||||
|
||||
class Third(First, Second):
|
||||
def __init__(self):
|
||||
super(Third, self).__init__()
|
||||
print("third")
|
||||
|
||||
Third()
|
||||
print(Third.__mro__)
|
||||
|
||||
second
|
||||
first
|
||||
third
|
||||
(<class '__main__.Third'>, <class '__main__.First'>, <class '__main__.Second'>, <class 'object'>)
|
||||
[Finished in 0.0s]
|
||||
|
||||
|
||||
https://www.datacamp.com/community/data-science-cheatsheets
|
||||
|
||||
https://www.datacamp.com/community/tutorials/decorators-python - functions returning other functions?
|
||||
97
learning/super/rectangle-square.py
Normal file
97
learning/super/rectangle-square.py
Normal file
@@ -0,0 +1,97 @@
|
||||
#!~/.virtualenvs/learning/bin/python
|
||||
|
||||
|
||||
class Rectangle(object):
|
||||
"""calculates the area and perimeter of a rectangle"""
|
||||
|
||||
def __init__(self, length, width, **kwargs):
|
||||
super(Rectangle, self).__init__(**kwargs)
|
||||
self.length = length
|
||||
self.width = width
|
||||
|
||||
def area(self):
|
||||
return(self.length * self.width)
|
||||
|
||||
def perimeter(self):
|
||||
return(2 * self.length + 2 * self.width)
|
||||
|
||||
|
||||
# Here we can declare the superclass for the subclass Square
|
||||
|
||||
class Square(Rectangle):
|
||||
"""calculates the area and perimeter of a square"""
|
||||
|
||||
def __init__(self, length, **kwargs):
|
||||
super(Square, self).__init__(length=length, width=length, **kwargs)
|
||||
|
||||
|
||||
class Cube(Square):
|
||||
"""calculates the surface area and volume of a cube"""
|
||||
|
||||
def __init__(self, length, **kwargs):
|
||||
super(Cube, self).__init__(length=length, **kwargs)
|
||||
self.length = length
|
||||
|
||||
def surface_area(self):
|
||||
face_area = super(Cube, self).area()
|
||||
return(6 * face_area)
|
||||
|
||||
def volume(self):
|
||||
face_area = super(Cube, self).area()
|
||||
return(face_area * self.length)
|
||||
|
||||
def area(self):
|
||||
return(self.length * self.length + 1)
|
||||
# return(super(Cube, self))
|
||||
|
||||
|
||||
class Triangle(object):
|
||||
"""calculates the area of a triangle"""
|
||||
|
||||
def __init__(self, base, height, **kwargs):
|
||||
self.base = base
|
||||
self.height = height
|
||||
super(Triangle, self).__init__(**kwargs)
|
||||
|
||||
def tri_area(self):
|
||||
return 0.5 * self.base * self.height
|
||||
|
||||
|
||||
class RightPyramid(Square, Triangle):
|
||||
"""calculates the surface area of a right pyramid"""
|
||||
|
||||
def __init__(self, base, slant_height, **kwargs):
|
||||
kwargs['height'] = slant_height
|
||||
kwargs['length'] = base
|
||||
super(RightPyramid, self).__init__(base=base, **kwargs)
|
||||
self.base = base
|
||||
self.slant_height = slant_height
|
||||
|
||||
def area(self):
|
||||
base_area = super(RightPyramid, self).area()
|
||||
perimeter = super(RightPyramid, self).perimeter()
|
||||
return(0.5 * perimeter * self.slant_height + base_area)
|
||||
|
||||
def area_2(self):
|
||||
base_area = super(RightPyramid, self).area()
|
||||
triangle_area = super(RightPyramid, self).tri_area()
|
||||
return(triangle_area * 4 + base_area)
|
||||
|
||||
|
||||
class new_cube(Cube):
|
||||
"""docstring for new_cube"""
|
||||
def __init__(self, new_length):
|
||||
super(new_cube, self).__init__(length=new_length)
|
||||
self.new_length = new_length
|
||||
|
||||
|
||||
# print(RightPyramid.__mro__)
|
||||
pyramid = RightPyramid(2, 4)
|
||||
print(pyramid.area())
|
||||
print(pyramid.area_2())
|
||||
|
||||
nc = new_cube(4)
|
||||
print(nc.surface_area())
|
||||
|
||||
square = Square(3)
|
||||
print(square.perimeter())
|
||||
0
learning/super/temp.py
Normal file
0
learning/super/temp.py
Normal file
20
learning/super/triangle.py
Normal file
20
learning/super/triangle.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# ~/.virtualenvs/learning/python
|
||||
|
||||
|
||||
def convert_to_uppercase(function):
|
||||
""" will convert to uppercase """
|
||||
def wrapper():
|
||||
func = function()
|
||||
make_uppercase = func.upper()
|
||||
return make_uppercase
|
||||
|
||||
|
||||
# @convert_to_uppercase
|
||||
def hello():
|
||||
""" print hello world """
|
||||
return 'hello world'
|
||||
|
||||
|
||||
say_HELLO = convert_to_uppercase(hello)
|
||||
|
||||
print(say_HELLO())
|
||||
Reference in New Issue
Block a user