Skip to content

Instantly share code, notes, and snippets.

View Ytosko's full-sized avatar
🌴
Some other day?

Saiki Sarkar Ytosko

🌴
Some other day?
View GitHub Profile
@Ytosko
Ytosko / building_a_simple_token_bucket_rate_limiter_in_python.md
Created September 22, 2025 22:01
Building a Simple Token Bucket Rate Limiter in Python

Instructions

  1. Find the TokenBucket settings in your current simulation.
  2. Increase the capacity to a new, higher value (e.g., double its current value, or set to 200 tokens).
  3. Decrease the fill_rate to a new, lower value (e.g., half its current value, or set to 5 tokens per second).
  4. Run the simulation again using these modified TokenBucket settings.
  5. Report the total number of allowed requests and rate-limited requests from this new simulation run.
  6. Clearly explain how increasing the capacity and decreasing the fill_rate specifically changed the number of allowed versus rate-limited requests compared to the original simulation.
@Ytosko
Ytosko / mastering_resource_management_custom_context_managers_in_python.md
Created September 22, 2025 10:01
Mastering Resource Management: Custom Context Managers in Python

Modify the following Python TimerContext class to handle exceptions:

import time

class TimerContext:
    def __enter__(self):
        self.start_time = time.time()
        return self
@Ytosko
Ytosko / managing_transient_data_mastering_pythons_tempfile_module.md
Created September 21, 2025 22:01
Managing Transient Data: Mastering Python's `tempfile` Module

Python Temporary File Persistence Example

This script demonstrates how to create a temporary file using tempfile.NamedTemporaryFile that does not get automatically deleted upon closing. It then writes data, closes the file, reopens it by its path to read the data, and finally manually deletes it.

Instructions Implemented:

  • Create a temporary file that will not be deleted automatically when closed. Uses tempfile.NamedTemporaryFile and sets delete=False.
  • Write the text "This data should persist." into this temporary file.
  • Get the full path (name) of the temporary file.
  • Close the temporary file.
@Ytosko
Ytosko / building_a_python_cli_tool_with_configurable_defaults_using_argparse_and_configparser.md
Created September 21, 2025 10:01
Building a Python CLI Tool with Configurable Defaults using `argparse` and `configparser`

Write a Python script that uses configparser

This guide demonstrates how to use Python's configparser module to read configuration from an INI file.

  1. Create a file named config.ini. Inside config.ini, add the following content:

    [DEFAULT]
    name = Guest
@Ytosko
Ytosko / crafting_custom_python_context_managers_for_robust_resource_handling.md
Created September 20, 2025 22:01
Crafting Custom Python Context Managers for Robust Resource Handling

Enhanced DBConnection Context Manager

This document demonstrates the modifications to the DBConnection context manager, fulfilling the requirements to accept connection parameters, display them during connection, and an example of its usage.

1. & 2. Modified DBConnection Class

The DBConnection class has been updated:

  • The __init__ method now accepts host, user, and password.
  • The __enter__ method prints a clear message including these parameters as the connection is established.
@Ytosko
Ytosko / building_a_python_cli_tool_to_process_text_files_with_argparse_and_custom_logic.md
Created September 20, 2025 10:01
Building a Python CLI tool to process text files with `argparse` and custom logic

Modify Script: Add Optional Output Feature

Follow these steps to modify your existing script to include an optional output feature:

  1. Add an optional command-line argument: Create an optional argument named --output (or -o) that accepts a file path as its value.
  2. Check for the argument: After processing the script's content, check if the --output argument was provided by the user.
  3. Conditional output:
    • If the --output argument was provided: Take the processed content and write it to the file path specified by the argument. Create the file if it doesn't exist, or overwrite it if it does.
    • If the --output argument was not provided: Print the processed content to the console (standard output), just like the script currently does.
  4. Maintain core functionality: The script should continue to perform its original content processing (e.g., making text uppercase, adding prefixes) exactly as it did before. This modification only changes where the final proce
@Ytosko
Ytosko / automating_precommit_checks_and_code_quality_with_git_hooks.md
Created September 19, 2025 22:01
Automating Pre-Commit Checks and Code Quality with Git Hooks

Implement a Git Pre-Push Hook to Run Unit Tests

This guide provides a detailed, step-by-step process to implement a Git pre-push hook. This hook will automatically run your unit tests before allowing a push to a remote repository. If any tests fail, the push will be blocked, helping to ensure that only passing code reaches your remote branches.

1. Finding the Git hooks directory

All Git hooks for a repository are located in a special directory within that repository.

  • Navigate to your repository's root: Open your terminal or command prompt and change your current directory to the main folder of your Git project.
  • Locate the .git/hooks/ folder: Inside your repository's root, you'll find a hidden .git directory. The hooks are within a subfolder called hooks.
@Ytosko
Ytosko / mastering_pythons_pathlib_for_elegant_file_system_operations.md
Created September 19, 2025 10:01
Mastering Python's `pathlib` for Elegant File System Operations

Using Python's pathlib Module for File Paths

The pathlib module in Python provides an object-oriented way to work with file system paths. It makes common path operations cleaner and more intuitive than the older os.path module.

Here's how to use it step-by-step:

1. Import the Path Object

First, you need to import the Path class from the pathlib module.

@Ytosko
Ytosko / building_a_basic_inmemory_rate_limiter_in_python_using_the_token_bucket_algorithm.md
Created September 18, 2025 22:01
Building a Basic In-Memory Rate Limiter in Python using the Token Bucket Algorithm

Write Python code to simulate and test a Token Bucket Rate Limiter.

  1. Define a TokenBucketRateLimiter class that has:
    • An __init__ method to set capacity (maximum tokens) and fill_rate (tokens per second).
    • A method (e.g., allow_request) that tries to consume one token. It should return True if a token was available and consumed, False otherwise. Tokens should refill over time based on the fill_rate.
  2. Create an instance of this limiter with capacity=5 and fill_rate=1 token/second.
  3. Execute 10 requests in a rapid loop. For each request, call the allow_request method on the limiter and print whether the request was "Allowed" or "Denied".
  4. Pause execution for 3 seconds using time.sleep(3).
  5. Execute 5 more requests in a rapid loop. For each request, call the allow_request method and print whether the request was "Allowed` or "Denied".
  6. After the code runs, provide a clear explanation of the entire output, detailing why certain requests were allowed or de
@Ytosko
Ytosko / building_a_commandline_utility_to_monitor_file_system_changes_with_python.md
Created September 18, 2025 10:01
Building a Command-Line Utility to Monitor File System Changes with Python

Modifying a File System Monitoring Script (watchdog)

This guide provides step-by-step instructions to modify an existing Python watchdog script to react only to file creation and file modification events, explicitly ignoring directory events and other file system changes.

Goal

To configure the watchdog script to:

  • Process events when a file is created.
  • Process events when a file is modified.
  • Ignore all events related to directories (created, modified, deleted).