Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks (part 3)

Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks (part 3)



Часть 1
Часть 2


Оглавление:

·       Working With Common Built-in Functions and Packages

·       Working With pip (Package Management)

Working With Common Built-in Functions and Packages

1. os - Operating System Interface

To interact with the operating system:


import os
current_directory = os.getcwd()  # Get the current working directory

2. sys - System-specific Parameters and Functions

To access system-specific parameters and functions:


import sys
sys.exit()  # Exit the script

3. datetime - Basic Date and Time Types

To work with dates and times:


from datetime import datetime
now = datetime.now()  # Current date and time

4. math - Mathematical Functions

To perform mathematical operations:


import math
result = math.sqrt(16)  # Square root

5. random - Generate Pseudo-random Numbers

To generate pseudo-random numbers:


import random
number = random.randint(1, 10)  # Random integer between 1 and 10

6. json - JSON Encoder and Decoder

To parse and generate JSON data:


import json
json_string = json.dumps({'name': 'Alice', 'age': 30})  # Dictionary to JSON string

7. re - Regular Expressions

To work with regular expressions:


import re
match = re.search('Hello', 'Hello, world!')  # Search for 'Hello' in the string

8. urllib - URL Handling Modules

To work with URLs:


from urllib.request import urlopen
content = urlopen('http://example.com').read()  # Fetch the content of a webpage

9. http - HTTP Modules

To create HTTP servers and work with HTTP requests:


from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b'<html><head><title>Python HTTP Server</title></head>')
        self.wfile.write(b'<body><h1>Hello from a simple Python HTTP server!</h1></body></html>')
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
    server_address = ('', 8000)  # Serve on all addresses, port 8000
    httpd = server_class(server_address, handler_class)
    print("Server starting on port 8000...")
    httpd.serve_forever()
if __name__ == '__main__':
    run()

10. subprocess - Subprocess Management

To spawn new processes and connect to their input/output/error pipes:


import subprocess
subprocess.run(['ls', '-l'])  # Run the 'ls -l' command

11. socket - Low-level Networking Interface

To create network clients and servers:


import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # Create a TCP/IP socket

12. threading - Thread-based Parallelism

To manage concurrent execution of code:


import threading
def worker():
    print("Worker thread executing")
thread = threading.Thread(target=worker)
thread.start()

13. multiprocessing - Process-based Parallelism

To manage concurrent processes:


from multiprocessing import Process
def worker():
    print("Worker process")
p = Process(target=worker)
p.start()

14. argparse - Parser for Command-line Options, Arguments, and Sub-commands

To parse command-line arguments:


import argparse
parser = argparse.ArgumentParser(description="Process some integers.")
args = parser.parse_args()

15. logging - Logging Facility

To log messages (debug, info, warning, error, and critical):


import logging
logging.warning('This is a warning message')

16. unittest - Unit Testing Framework

To create and run unit tests:


import unittest
class TestStringMethods(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

17. pathlib - Object-oriented Filesystem Paths

To work with filesystem paths in an object-oriented way:


from pathlib import Path
p = Path('.')

18. functools - Higher-order Functions and Operations on Callable Objects

To use higher-order functions and operations on callable objects:


from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

19. collections - Container Data Types

To use specialized container data types (deque, Counter, OrderedDict, etc.):


from collections import Counter
c = Counter('hello world')

20. itertools - Functions Creating Iterators for Efficient Looping

To construct and use iterators for efficient looping:


import itertools
for combination in itertools.combinations('ABCD', 2):
    print(combination)

21. hashlib - Secure Hash and Message Digest Algorithms

To hash data:


import hashlib
hash_object = hashlib.sha256(b'Hello World')
hex_dig = hash_object.hexdigest()

22. csv - CSV File Reading and Writing

To read from and write to CSV files:


import csv
with open('file.csv', mode='r') as infile:
    reader = csv.reader(infile)

23. xml.etree.ElementTree - The ElementTree XML API

To parse and create XML data:


import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()

24. sqlite3 - DB-API 2.0 Interface for SQLite Databases

To interact with SQLite databases:


import sqlite3
conn = sqlite3.connect('example.db')

25. tkinter - GUI Toolkit

To create GUI applications:


import tkinter as tk
root = tk.Tk()

26. pickle - Python Object Serialization

To serialize and deserialize Python object structures:


import pickle
serialized_obj = pickle.dumps(obj)

27. io - Core Tools for Working with Streams

To handle streams (file-like objects):


from io import StringIO
f = StringIO("some initial text data")

28. time - Time Access and Conversions

To access time-related functions:


import time
time.sleep(1)  # Sleep for 1 second

29. calendar - General Calendar-related Functions

To work with calendars:


import calendar
print(calendar.month(2023, 1))  # Print the calendar for January 2023

30. queue - A Synchronized Queue Class

To manage a queue, useful in multithreaded programming:


from queue import Queue
q = Queue()

31. shutil - High-level File Operations

To perform high-level file operations, like copying and archiving:


import shutil
shutil.copyfile('source.txt', 'dest.txt')

32. glob - Unix Style Pathname Pattern Expansion

To find files matching a specified pattern:


import glob
for file in glob.glob("*.txt"):
    print(file)

33. tempfile - Generate Temporary Files and Directories

To create temporary files and directories:


import tempfile
temp = tempfile.TemporaryFile()

34. bz2 - Support for Bzip2 Compression

To compress and decompress data using bzip2 compression:


import bz2
compressed = bz2.compress(b'your data here')

35. gzip - Support for Gzip Compression

To compress and decompress data using gzip compression:


import gzip
with gzip.open('file.txt.gz', 'wt') as f:
    f.write('your data here')

36. ssl - TLS/SSL Wrapper for Socket Objects

To handle TLS/SSL encryption and peer authentication for network sockets:


import ssl
ssl.wrap_socket(sock)

37. imaplib - IMAP4 Protocol Client

To access and manipulate mail over IMAP4:


import imaplib
mail = imaplib.IMAP4_SSL('imap.example.com')

38. smtplib - SMTP Protocol Client

To send mail using the Simple Mail Transfer Protocol (SMTP):


import smtplib
server = smtplib.SMTP('smtp.example.com', 587)

39. email - Managing Email Messages

To manage email messages, including MIME and other RFC 2822-based message documents:


from email.message import EmailMessage
msg = EmailMessage()

40. base64 - Base16, Base32, Base64, Base85 Data Encodings

To encode and decode data using Base64:


import base64
encoded_data = base64.b64encode(b'data to encode')

41. difflib - Helpers for Computing Deltas

To compare sequences and produce human-readable diffs:


import difflib
diff = difflib.ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
                     'ore\ntree\nemu\n'.splitlines(keepends=True))
print(''.join(diff))

42. gettext - Multilingual Internationalization Services

To internationalize your Python programs:


import gettext
gettext.install('myapp')

43. locale - Internationalization Services

To access a database of culture-specific data formats:


import locale
locale.setlocale(locale.LC_ALL, '')

44. secrets - Generate Secure Random Numbers for Managing Secrets

To generate secure random numbers for managing secrets, such as tokens or passwords:


import secrets
secure_token = secrets.token_hex(16)

45. uuid - UUID Objects According to RFC 4122

To generate universally unique identifiers (UUIDs):


import uuid
unique_id = uuid.uuid4()

46. html - HyperText Markup Language Support

To handle and manipulate HTML entities:


import html
escaped = html.escape('<a href="https://example.com">link</a>')

47. ftplib - FTP Protocol Client

To interact with and transfer files over the FTP protocol:


from ftplib import FTP
ftp = FTP('ftp.example.com')

48. tarfile - Read and Write Tar Archive Files

To work with tar archive files, allowing you to archive and compress/decompress:


import tarfile
with tarfile.open('sample.tar.gz', 'w:gz') as tar:
    tar.add('sample.txt')

Working With pip (Package Management)

1. Installing a Package

To summon a library from the vast repositories, incorporating its power into your environment:


pip install numpy

2. Listing Installed Packages

To survey the compendium of libraries that reside within your realm, noting their versions and lineage:


pip list

3. Upgrading a Package

To imbue an installed library with enhanced powers and capabilities, elevating it to its latest form:


pip install --upgrade numpy

4. Uninstalling a Package

To uninstall a package:


pip uninstall numpy

5. Searching for Packages

Searching packages:


pip search "data visualization"

6. Installing Specific Versions of a Package

To install a specific version:


pip install numpy==1.18.5

7. Generating a Requirements File

Requirements file:


pip freeze > requirements.txt

8. Installing Packages from a Requirements File

To conjure a symphony of libraries in unison, each aligned with the notations in your tome of requirements:


pip install -r requirements.txt

9. Using Virtual Environments

Create virtual Environments to manage package conflicts:


# Create a virtual environment named 'venv'
python -m venv venv

# Activate the virtual environment
# On Windows
.\venv\Scripts\activate

# On Unix or MacOS
source venv/bin/activate

10. Checking Package Dependencies

Understanding Dependencies:


pip show numpy


Оригинал статьи (доступ только через VPN)


Наверх


Report Page