From bf819d7c752770d965fa1a03e588196344e583c5 Mon Sep 17 00:00:00 2001 From: Ravishankar Chavare Date: Sun, 27 Aug 2023 19:36:51 +0530 Subject: [PATCH] w34 added --- doc/newsletters/2023/WEEK_34.md | 230 ++++++++++++++++++++++++++++++++ doc/newsletters/index.2023.rst | 1 + 2 files changed, 231 insertions(+) create mode 100644 doc/newsletters/2023/WEEK_34.md diff --git a/doc/newsletters/2023/WEEK_34.md b/doc/newsletters/2023/WEEK_34.md new file mode 100644 index 0000000..48dce53 --- /dev/null +++ b/doc/newsletters/2023/WEEK_34.md @@ -0,0 +1,230 @@ + +# Week 34 - Augest 2023 + + +## 1. ๐Ÿ“ฐ The 'Title()' Method and Its Nuances(Limitations) + +The `title()` method is a versatile tool for enhancing text formatting. However, it's essential to recognize its limitations. The title() method isn't just about capitalizing; it also handles special characters and maintains proper casing rules. + + + Let's explore some insights: + +๐Ÿ” **Use Case 1: Letters Only** +```python +text = "pycon 2023" +formatted_text = text.title() +print(formatted_text) # Output: Pycon 2023 +``` + +๐Ÿ“‹ **Use Case 2: Alphanumeric Exploration** +```python +text = "python3.10_release" +formatted_text = text.title() +print(formatted_text) # Output: Python3.10_Release +``` + +๐Ÿ“„ **Use Case 3: Punctuation Perspective** +```python +text = "don't STOP believin'!" +formatted_text = text.title() +print(formatted_text) # Output: Don'T Stop Believin'! +``` + + +## 2. ๐Ÿ“š Run A Python Module Directly With `__main__.py` + +The `__main__.py` file empowers you to seamlessly convert your Python module into an executable script, enhancing usability and efficiency. + +Have you ever wished to run a Python module directly as if it were a script? The `__main__.py` file holds the key! Let's explore a captivating use case that showcases its remarkable potential: + +๐Ÿ” **Use Case: Transforming Modules into Scripts** +``` +my_module/ +|-- __main__.py +|-- helper_functions.py +``` + +```python +# file : helper_functions.py + +def some_function(): + return "Python World" +``` + +Imagine you have a module named `my_module` containing useful functions. By adding the following code to `__main__.py`, you can make your module directly executable: +```python +from helper_functions import * + +if __name__ == "__main__": + # Your script logic goes here + result = some_function() + print("Result:", result) +``` + +Now, executing `python my_module` directly from the command line runs the script within the `__main__.py` file, providing instant access to your module's functionality without additional steps. + + +## 3. ๐Ÿ” Unraveling Dictionary Literals and Constructors + +Dictionaries are fundamental for storing and accessing key-value data, and Python's versatility in creating them caters to various scenarios. + +Dictionaries are essential data structures in Python, and the methods to create them are fascinating. Let's explore these methods along with some captivating use cases: + +๐Ÿ” **Use Case 1: Dictionary Literals** +```python +employee = {'name': 'Alice', 'age': 30, 'department': 'HR'} +``` +Dictionary literals allow you to create dictionaries directly by listing key-value pairs enclosed in curly braces. Perfect for concise and readable code. + +๐Ÿ“‹ **Use Case 2: Dictionary Constructor** +```python +colors = dict(red='#FF0000', green='#00FF00', blue='#0000FF') +``` +The dictionary constructor lets you create dictionaries using the built-in `dict()` function. Ideal for dynamically constructing dictionaries from various sources. + +๐Ÿ“„ **Use Case 3: Merging Dictionaries** +```python +defaults = {'theme': 'light', 'font_size': 12} +user_preferences = {'font_size': 14, 'language': 'en'} +merged_preferences = {**defaults, **user_preferences} +``` +Combine dictionaries effortlessly by unpacking them into a new dictionary. A powerful technique for overriding default values. + +๐Ÿ”— **Use Case 4: Creating Dynamic Dictionaries** +```python +keys = ['a', 'b', 'c'] +values = [1, 2, 3] +dynamic_dict = dict(zip(keys, values)) +``` +Construct dictionaries dynamically by using the `zip()` function to combine lists of keys and values. + + +**๐Ÿš€ In a Nutshell:** + +Choose dictionary literals for straightforward dictionary creation, and opt for the dictionary constructor when you need more flexibility, such as when dynamically constructing dictionaries or merging multiple dictionaries. + + +## 4. ๐Ÿงน Discovering isort: The Art of Import Arrangement + +Import statements โ€“ a gateway to the world of functionality. But arranging them can be a challenge. Enter `isort`, the Python package that effortlessly arranges your imports for clarity and readability. Behold how easily you can get started: + +1. **Installation of isort** via pip: + + ``` + pip install isort + ``` + +2. **Running isort** on your Python file: + + ``` + isort your_file.py + ``` + + ๐ŸŒŸ Witness the transformation as your imports fall into a harmonious order! + +๐ŸŒ **A Symphony of Imports: Prioritize with Precision** + +Picture this: system packages, third-party modules, and local imports, all in symphonic order. `isort` brings this vision to life: + +```python +# Before isort +from flask import Flask, render_template +import os, sys +from datetime import datetime, timedelta +from my_local_module import my_function + +# After isort +import os +import sys +from datetime import datetime, timedelta + +from flask import Flask, render_template + +from my_local_module import my_function +``` + +๐Ÿงฉ **Custom Choreography: Tailoring isort to Your Tunes** + +`isort` is not just a one-size-fits-all solution; it dances to your rhythm. Craft your own dance steps with configuration options in `pyproject.toml`: + +```toml +# pyproject.toml +[tool.isort] +profile = "black" +line_length = 88 +known_third_party = ["flask", "requests"] +``` + +๐Ÿ” **isort in Your Workflow Ensemble: Effortless Integration** + +Seamlessly blend `isort` into your development routine. Set it as a pre-commit hook using tools like `pre-commit`. Feel the magic as your imports gracefully align with every commit. + + + +## 5. ๐Ÿ•ฐ๏ธ Master Time with Python's Scheduler! + +In the chaotic world of programming, managing tasks at specific intervals is a breeze with Python's built-in `sched` module. Let's jump into a simple example to showcase its power: + +```python +import sched +import time + +def greet(name): + print(f"Hello, {name}! It's time to shine.") + +# Create a scheduler instance +scheduler = sched.scheduler(time.time, time.sleep) + +# Schedule the greeting +scheduler.enter(5, 1, greet, ("Python-world",)) + +# Run the scheduler +scheduler.run() +``` + +๐Ÿš€ **Automation at Your Fingertips: Scheduled Tasks** + +Python's scheduler isn't limited to just delaying functions. You can schedule tasks at specific times, automate backups, and more. Here's a snippet to illustrate scheduling a task every hour: + +```python +import sched +import time + +def hourly_task(): + print("Performing hourly task!") + +scheduler = sched.scheduler(time.time, time.sleep) + +# Schedule the task every hour +interval = 3600 # 1 hour in seconds +while True: + scheduler.enter(interval, 1, hourly_task, ()) + scheduler.run() +``` + +โณ **Cron-like Scheduling: `schedule` Library** + +For more advanced scheduling, the `schedule` library offers a cron-like syntax. Check out how easy it is to schedule tasks with this powerful tool: + +```python +import schedule +import time + +def daily_task(): + print("Daily task is due!") + +# Schedule a task to run daily at 3:30 PM +schedule.every().day.at("15:30").do(daily_task) + +while True: + schedule.run_pending() + time.sleep(1) +``` + +๐Ÿ” **Tailoring to Your Time Needs: Versatility of Scheduling** + +Whether it's running regular maintenance scripts, sending automated emails, or managing data backups, Python's scheduler empowers you to automate tasks according to your precise timing needs. + +๐ŸŒ **Unlock the Potential of Python's Scheduler!** + +Say goodbye to manual task management and hello to efficient automation with Python's scheduler. From simple delays to intricate cron-like scheduling, Python has your back when it comes to managing time and tasks. \ No newline at end of file diff --git a/doc/newsletters/index.2023.rst b/doc/newsletters/index.2023.rst index 1a7edd9..7a55fad 100644 --- a/doc/newsletters/index.2023.rst +++ b/doc/newsletters/index.2023.rst @@ -3,6 +3,7 @@ .. toctree:: :maxdepth: 4 + Augest - Week 34 <2023/WEEK_34.md> Augest - Week 33 <2023/WEEK_33.md> Augest - Week 32 <2023/WEEK_32.md> Augest - Week 31 <2023/WEEK_31.md>