Clean Code: Python Application Flashcards

1
Q

What is Encapsulation?

A

Groups data and methods –> Basically a Class does that!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Polymorphism?

A

Different classes implement the same method but have different outputs.

Example: Both the Dog and Cat classes inherit from the Animal class and implement their own version of the make_sound() method.

Creating objects
my_dog = Dog(“Buddy”) # Object of type Dog
my_cat = Cat(“Whiskers”) # Object of type Cat
# Using methods
print(f”{my_dog.name} says {my_dog.make_sound()}”)
# Output: Buddy says Woof!
print(f”{my_cat.name} says {my_cat.make_sound()}”)
# Output: Whiskers says Meow!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain what is the correct naming convention

A
  • Choose a descriptive name that reflects the code’s purpose.
  • Underscores are recommended for package names, modules, functions, and variables(my_function)
  • Uppercase letters are recommended for classes(MyClass)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a package manager do, what package manager does Python use?

A
  • Automates installation, updates, configuration, and removal of software
  • In Python we use pip as a package manager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the benefits of using a virtual environment?

A
  • Isolate Projects: Keeps your project separate from others.
  • Prevent Conflicts: Avoids package conflicts and version mismatches.
  • Manage Dependencies: Create a dedicated environment for project-specific libraries.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is venv? How do we set it up?

A
  • Built-in Tool: Comes with Python to create virtual environments.
  • Encapsulated Environment: Allows separate package installations per project.
  • Easy Setup: Use ‘python -m venv <env_name>' to create a new environment.</env_name>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Linting? What tool does python use for linting and how do we initialize it?

A

Linting is the process of analyzing source code to identify errors, style issues and inconsistencies.
We use Flake8 tool that checks code against Pep8 python style guide
To initialize flake8 we write python flake8 my_file.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Docker?

A

Docker is a tool used for code sharing and development

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain how the Simple Module Structure looks like

A
  • It is used for single module python projects

my_project/ # Project directory (can use hyphens in the directory name)

├── README.md # Optional: Project description and documentation
├── my_module.py # The single Python module (use underscores for module names)
├── requirements.txt # Optional: List of dependencies (if any)
└── LICENSE # Optional: License file for your project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain how the Package module looks like

A

Multiple modules and packages
my_project/

├── my_package/ # Main package directory
│ ├── __init__.py # Initializes the package
│ ├── module1.py # First module
│ └── module2.py # Second module

├── tests/ # Directory for test scripts
│ └── test_module1.py # Test for module1

├── data/ # Directory for data files (optional, if needed)
│ └── sample_data.csv # Example data file (optional)

├── docs/ # Directory for documentation
│ └── index.md # Main documentation file

├── README.md # Project documentation (main)
├── requirements.txt # List of dependencies
├── setup.py # Packaging script for distribution
├── LICENSE # License for the project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain how the Monolith structure looks like

A

Multiple packages, modules, supporting files and resources.
my_project/

├── src/
│ └── my_package/
│ ├── __init__.py
│ ├── main.py
│ ├── module1.py
│ ├── module2.py
│ └── config/
│ ├── settings.yaml
│ └── additional_settings.yaml

├── tests/
│ ├── test_module1.py
│ ├── test_module2.py
│ └── …

├── data/
│ └── input.csv

├── docs/
│ └── index.md

├── README.md
├── requirements.txt
└── LICENSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are __init__.py files used for?

A
  • Used within a package, tells python that directory should be treated as a package.
    Project example:
    my-project/
    __init__.py
    module1.py
    subpackage/
    __init__.py

In file:
# my-project/__init__.py
from .module1 import some_function1
from .subpackage.submodule import some_function2

Now we can import like this in the main:
import my_project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does the Config.cfg file look like?

A

[database]
host = localhost
port = 5432
username = user
password = secret

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does the Config.yaml look like?

A

database:
host: localhost
port: 5432
username: user
password: secret

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does the Config.json look like?

A

{
“database”: {
“host”: “localhost”,
“port”: 5432,
“username”: “user”,
“password”: “secret”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an .md file?

A

It is a markdown file, Markdown is a lightweight markup language that lets you format text using plain text