Show Menu
Cheatography

python Keyboard Shortcuts (DRAFT) by

main consepts of the python

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Abstract Classes

sadfas­dfa­sdffas asdfas­dfs­fsd­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eee­eeeeeee
 
from abc import ABC,
abstra­ctm­ethod

class MyClas­s(ABC):
@abstr­act­method
def some_m­eth­od(­self):
pass
 

gdsfgsdfg

 
from abc import ABC,
abstra­ctm­ethod

class MyClas­s(ABC):
@abstr­act­method
def some_m­eth­od(­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)
 

das

fasdfasdf

Unit Testing with Pytest

1. Write tests early and often
2. Keep tests small and focused:
3. Use descri­ptive test names
4. Avoid testing implem­ent­ation details
5. Use test fixtures
6. Use mocking and stubbing when necessary
7. Run tests automa­tically
8. Maintain code coverage
pip install pytest 
def test_addition():
assert 2 + 2 == 4

pytest test_m­ath.py

pip install coverage
pytest --cov=­my_­package