Loading experience... Please wait.
Advanced Coding Tutorial

Advanced Coding Tutorials: Regex

Image description
Section 1: Function Annotations and Type Hints+

Function annotations and type hints, introduced in Python 3, provide a way to add metadata to function arguments and return values. While they don't enforce types at runtime in standard Python, they are invaluable for static analysis tools (like MyPy), linters, and for improving code readability and maintainability by clearly indicating the expected types.

    

def greet(name: str, greeting: str = "Hello") -> str:
    """Greets a person with a specified greeting.

    Args:
        name: The name of the person to greet (string).
        greeting: The greeting message (string, default is "Hello").

    Returns:
        A formatted greeting string.
    """
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", "Greetings"))

def process_data(items: list[int]) -> dict[str, int]:
    """Processes a list of integers and returns a dictionary of counts.

    Args:
        items: A list of integers.

    Returns:
        A dictionary where keys are string representations of numbers
        and values are their counts in the input list.
    """
    counts = {}
    for item in items:
        key = str(item)
        counts[key] = counts.get(key, 0) + 1
    return counts

data = [1, 2, 2, 3, 1, 1, 4]
result = process_data(data)
print(result)

# Note: Running this code with a type checker like MyPy would allow
# you to catch potential type errors before runtime.



Section 2: Working with Pathlib for File System Operations+

The `pathlib` module offers an object-oriented way to interact with files and directories, providing a more intuitive and readable alternative to the older `os.path` module. It encapsulates file system paths as objects, with methods for performing common operations like joining paths, checking file existence, reading/writing files, and traversing directories.

    

from pathlib import Path

# Get the current working directory as a Path object
current_dir = Path("./")
print(f"Current directory: {current_dir}")

# Create a new Path object by joining parts
new_file_path = current_dir / "data" / "output.txt"
print(f"New file path: {new_file_path}")

# Create the parent directories if they don't exist
new_file_path.parent.mkdir(parents=True, exist_ok=True)

# Write some text to the file
new_file_path.write_text("This is some data written using pathlib.\n", encoding="utf-8")
print(f"File written to: {new_file_path}")

# Read the content of the file
content = new_file_path.read_text(encoding="utf-8")
print(f"Content of the file:\n{content}")

# Check if a file or directory exists
print(f"Does {new_file_path} exist? {new_file_path.exists()}")
print(f"Is {new_file_path} a file? {new_file_path.is_file()}")
print(f"Is {new_file_path.parent} a directory? {new_file_path.is_dir()}")

# Iterate over files in a directory with a specific extension
print("\nText files in the current directory:")
for file in current_dir.glob("*.txt"):
    print(file)

# Rename a file
old_name = new_file_path
new_name = new_file_path.with_name("processed_output.txt")
old_name.rename(new_name)
print(f"File renamed from {old_name} to {new_name}")

# Clean up (optional)
# new_name.unlink() # Delete the file
# new_name.parent.rmdir() # Delete the 'output.txt' directory (if empty)
# (and potentially the 'data' directory if also empty)



Section 3: Data Serialization with JSON and Pickle+

Data serialization is the process of converting Python objects into a format that can be stored or transmitted and then reconstructed later. JSON (JavaScript Object Notation) is a lightweight, human-readable format commonly used for web applications and data exchange. Pickle is a Python-specific binary format that can serialize more complex Python objects, including custom classes, but is generally not recommended for inter-system communication due to security and compatibility concerns.

    

import json
import pickle

# Sample Python dictionary
data = {
    "name": "Alice",
    "age": 30,
    "city": "New York",
    "hobbies": ["reading", "hiking"]
}

# --- JSON Serialization ---
try:
    json_string = json.dumps(data, indent=4) # Convert to JSON string with indentation
    print("JSON String:")
    print(json_string)

    # Save to a JSON file
    with open("data.json", "w") as f:
        json.dump(data, f, indent=4)
    print("\nData saved to data.json")

    # Load from a JSON file
    with open("data.json", "r") as f:
        loaded_data_json = json.load(f)
    print("\nData loaded from data.json:")
    print(loaded_data_json)

except TypeError as e:
    print(f"JSON Serialization Error: {e}")

# --- Pickle Serialization ---
try:
    pickled_data = pickle.dumps(data) # Convert to a binary pickle format
    print("\nPickled Data (binary):")
    print(pickled_data)

    # Save to a pickle file
    with open("data.pkl", "wb") as f:
        pickle.dump(data, f)
    print("\nData saved to data.pkl")

    # Load from a pickle file
    with open("data.pkl", "rb") as f:
        loaded_data_pickle = pickle.load(f)
    print("\nData loaded from data.pkl:")
    print(loaded_data_pickle)

except pickle.PicklingError as e:
    print(f"Pickling Error: {e}")

# Note: Be cautious when unpickling data from untrusted sources as it can pose security risks.



Section 4: Working with Regular Expressions (Regex)+

Regular expressions (regex) are a powerful tool for pattern matching in strings. The `re` module in Python provides functions to search, match, split, and substitute text based on defined patterns. Mastering regex can significantly simplify complex text processing tasks.

    

import re

text = "The quick brown fox jumps over the lazy dogs. Contact us at [email protected] or [email protected]."

# Searching for a pattern
pattern = r"fox"
match = re.search(pattern, text)
if match:
    print(f"Found '{match.group()}' at position {match.start()}")

# Matching at the beginning of the string
pattern_start = r"^The"
match_start = re.match(pattern_start, text)
if match_start:
    print(f"'{match_start.group()}' matches at the beginning.")

# Finding all occurrences of a pattern
pattern_all = r"\b\w{3}\b" # Find all three-letter words
all_matches = re.findall(pattern_all, text)
print(f"All three-letter words: {all_matches}")

# Substituting text based on a pattern
pattern_sub = r"\s+" # Match one or more whitespace characters
new_text = re.sub(pattern_sub, "_", text)
print(f"Text with spaces replaced by underscores: {new_text}")

# Splitting a string based on a pattern
pattern_split = r"[.]\s*" # Split at a dot followed by zero or more spaces
sentences = re.split(pattern_split, text)
print(f"Sentences: {sentences}")

# Finding email addresses
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
emails = re.findall(email_pattern, text)
print(f"Email addresses found: {emails}")

# Using groups in a pattern
date_text = "Today is 2023-10-27."
date_pattern = r"(\d{4})-(\d{2})-(\d{2})" # Capture year, month, day in groups
date_match = re.search(date_pattern, date_text)
if date_match:
    year, month, day = date_match.groups()
    print(f"Date found: Year={year}, Month={month}, Day={day}")



Section 5: Working with Itertools for Efficient Iteration+

The `itertools` module provides a collection of tools for creating fast, memory-efficient iterators for various purposes, such as infinite sequences, combining iterators, and filtering data. Using `itertools` can often lead to more concise and performant code when dealing with iterable data.

    

import itertools

# Infinite iterators
count_up = itertools.count(start=10, step=2)
# print("Counting up (first 5):", [next(count_up) for _ in range(5)])

cycle_colors = itertools.cycle(['red', 'green', 'blue'])
# print("Cycling colors (first 7):", [next(cycle_colors) for _ in range(7)])

repeat_hello = itertools.repeat("hello", times=3)
print("Repeating 'hello':", list(repeat_hello))

# Combining iterators
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
combined_chain = itertools.chain(numbers, letters)
print("Chaining lists:", list(combined_chain))

zipped = itertools.zip_longest(numbers, letters, fillvalue='-')
print("Zipping (longest):", list(zipped))

product = itertools.product(numbers, letters)
print("Product of lists:", list(product))

# Filtering iterators
def is_even(n):
    return n % 2 == 0

even_numbers = filter(is_even, range(10)) # Standard filter
print("Even numbers (filter):", list(even_numbers))

even_numbers_dropwhile = itertools.dropwhile(lambda x: x < 5, [1, 3, 6, 2, 8])
print("Dropping while less than 5:", list(even_numbers_dropwhile))

even_numbers_takewhile = itertools.takewhile(lambda x: x < 5, [1, 3, 6, 2, 8])
print("Taking while less than 5:", list(even_numbers_takewhile))

# Combinatoric iterators
permutations_ab = itertools.permutations('ab', 2)
print("Permutations of 'ab' (length 2):", list(permutations_ab))

combinations_ab = itertools.combinations('abc', 2)
print("Combinations of 'abc' (length 2):", list(combinations_ab))

combinations_with_replacement = itertools.combinations_with_replacement('ab', 2)
print("Combinations with replacement of 'ab' (length 2):", list(combinations_with_replacement))



Section 6: Understanding and Using Python Modules and Packages+

Python's module and package system is fundamental for organizing and reusing code. A module is a single Python file containing definitions and statements. A package is a collection of modules (and possibly sub-packages) in a directory hierarchy, which helps structure larger projects and prevents naming conflicts.

    

# --- Creating and Importing Modules ---

# Create a file named 'my_module.py' with the following content:
"""
# my_module.py
def greet(name):
    print(f"Hello, {name} from my_module!")

PI = 3.14159
"""

# In another Python file (e.g., main.py):
import my_module

my_module.greet("User")
print(f"Value of PI from my_module: {my_module.PI}")

# Alternatively, import specific names:
from my_module import greet as my_greet, PI as MY_PI

my_greet("Another User")
print(f"Value of PI (aliased): {MY_PI}")

# --- Creating and Importing Packages ---

# Create a directory named 'my_package'
# Inside 'my_package', create an empty file named '__init__.py'
# Inside 'my_package', create a module named 'utils.py' with:
"""
# my_package/utils.py
def calculate_square(x):
    return x * x
"""

# Inside 'my_package', create another module named 'data_processing.py' with:
"""
# my_package/data_processing.py
def load_data(filename):
    with open(filename, 'r') as f:
        return f.readlines()
"""

# In your main Python file:
import my_package.utils
from my_package import data_processing

square_of_5 = my_package.utils.calculate_square(5)
print(f"Square of 5: {square_of_5}")

data = data_processing.load_data("example.txt") # Assuming 'example.txt' exists
print(f"Loaded data: {data}")

# Alternatively, import specific functions:
from my_package.utils import calculate_square as sq
from my_package.data_processing import load_data as ld

result = sq(10)
loaded_content = ld("another_file.txt") # Assuming 'another_file.txt' exists
print(f"Square of 10 (aliased): {result}")
print(f"Loaded content (aliased): {loaded_content}")

# The '__init__.py' file in a package directory is crucial. It can be empty,
# or it can contain initialization code for the package or define which modules
# are exported from the package.


Explore more about Python's standard library here: Python Standard Library



Discover Comprehensive Tutorials & Software Downloads

Unlock a wealth of tutorials and software downloads available across our platform.

Image descriptionStart Exploring
 Welcome to your all-in-one destination in the Philippines! Settle into our welcoming accommodations, then treat your taste buds at our restaurant, featuring both elegant fine dining and authentic local cuisine, including special 8-course meals. Explore the islands with our tourist tours and car rentals, or let our shuttle service handle your transport. Find unique treasures in our handcrafted clothing line, unwind with yoga and massage, or catch some waves with our surfboard rentals. Join us at the bar, order takeaway, or book us for your next celebration or corporate event – we even offer magical beach dining setups. We also have a selection of reads and software in our bookstore and software store.