-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Python-World/w34
w34 added
- Loading branch information
Showing
2 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters