Cheatography
https://cheatography.com
main consepts of the python
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Abstract Classes
sadfasdfasdffas asdfasdfsfsdeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
|
from abc import ABC, abstractmethod class MyClass(ABC): @abstractmethod def some_method(self): pass
|
|
|
gdsfgsdfg
from abc import ABC, abstractmethod class MyClass(ABC): @abstractmethod def some_method(self): pass
|
Data Classes
from dataclasses import dataclass,
InitVar
@dataclass
class Rectangle:
# We don't want to store
# width in the object
width: InitVar[int]
# We don't want to store height in the object
height: InitVar[int]
color: str
def __post_init__(self, width: int, height: int):
# Create a new attribute called area
# and store it in the object
self.area: int = width * height
def draw(self) -> str:
return f'Draw a {self.color} \
rectangle with area {self.area}'
rect = Rectangle(width=10, height=20, color='red')
print(rect.draw()) # Draw a red rectangle with area 200
# AttributeError: 'Rectangle' object has no attribute 'width'
print(rect.width)
# AttributeError: 'Rectangle' object has no attribute 'height'
print(rect.height)
|
|
|
Unit Testing with Pytest
1. Write tests early and often 2. Keep tests small and focused: 3. Use descriptive test names 4. Avoid testing implementation details 5. Use test fixtures 6. Use mocking and stubbing when necessary 7. Run tests automatically 8. Maintain code coverage |
pip install pytest def test_addition(): assert 2 + 2 == 4
pytest test_math.py
pip install coverage pytest --cov=my_package
|
|