DEV Community

KALPESH
KALPESH

Posted on

Python Overview

Python Modules

  • A file with reusable Python code (functions, classes, variables).

  • Example: custom .py files or sys, math module

Python Package

  • A folder with related modules, including an __init__.py file.

  • Example: numpy or custom module directories.

  • pip is a tool in Python to install & manage Python packages or libraries from the Python Package Index (PyPI).

Python Virtual Environment

  • Isolated environment for project-specific dependencies.

  • Create: python -m venv <env_name>

  • Activate: source <env_name>/bin/activate

Command Line Args

Python in build sys module

import sys # python inbuild sys module, which is used for command line args

num1 = float(sys.argv[1])
Enter fullscreen mode Exit fullscreen mode

Environment Variables

Env vars used for sensitive data, which we can’t hardcoded:

  • API keys

  • passwords

  • tokens

  • certificates

Declare Env vars in terminal:

export password=”pass@123”

Code:

import os

pass = os.getenv("password")
Enter fullscreen mode Exit fullscreen mode

File Operation of Windows

  • Open: open() with modes (r, w, etc.), e.g., open("file.txt", "r").

  • Read: Use read(), readline(), or readlines() to fetch content.

  • Write: Use write() or writelines() in modes like w or a.

  • Close: Use close() or with for auto-closing.

Module

Requests

The requests module in Python simplifies HTTP requests to interact with web servers.

  • Purpose: Send HTTP methods (GET, POST, etc.) and handle responses via API.

  • Features: Manage headers, cookies, auth, and work with JSON or text.

  • Install: pip install requests

import requests  
response = requests.get("https://example.com")  
print(response.status_code)  # Status code  
print(response.text)         # Response body
Enter fullscreen mode Exit fullscreen mode

Boto3

  • AWS SDK for Python to interact programmatically with AWS services.

  • Ideal for automating AWS workflows and managing resources efficiently

  • Building serverless applications with services like Lambda and DynamoDB.

import boto3

# Initialize S3 client
s3 = boto3.client('s3')

# Upload a file to S3
s3.upload_file('local_file.txt', 'my-bucket', 'remote_file.txt')
Enter fullscreen mode Exit fullscreen mode

Flask

It’s a lightweight web framework in Python used to build web applications with added functionality.

Decorators:

  • It’s a special function in Python used to modify the behaviour of another function.

  • It written above function with @ symbol.

from flask import Flask

app = Flask(__name__)

@app.route("/greet")  # Flask decorator that connects a URL('/greet') route to a function
def greet():
    return "Greetings from Flask!"

if __name__ == "__main__":
    app.run()
Enter fullscreen mode Exit fullscreen mode

Constructor

It’s special method in a class that runs automatically when you create an object.

__init__()

# __name__ special build-in variable
__name__ = __main__ # Runs code only when file is executed directly
Enter fullscreen mode Exit fullscreen mode

Pytest

Tool/framework for testing
Basic Structure

my_project/
│
├── app.py              # Your actual code
├── test_app.py         # Your test file (MUST start with "test_")
Enter fullscreen mode Exit fullscreen mode
  • pytest -v — Verbose output
  • pytest -s — Show print statements

Assert Statements

They check if a condition is True. If True, test passes ✅

Fixtures

A fixture in pytest is a reusable piece of setup code that prepares data or resources for your tests.

Conftest

Special file where you define fixtures that are automatically available to ALL test files in that directory and sub directories. No imports needed!

Mocking

Create fake versions of external dependencies (APIs, databases, files) so tests run fast, reliably.

Top comments (0)