-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
125 lines (91 loc) · 2.49 KB
/
examples.py
File metadata and controls
125 lines (91 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# EAFP: Easier to Ask Forgiveness Than Permission
# LBYL: Look Before You Leap
# Example 1: Input Validation
user_input = input("Enter a number: ")
try:
num = int(user_input)
except ValueError:
print("Invalid input: Please enter a valid number.")
# Example 2: Index checking
my_list = [1, 2, 3]
index = 5
if 0 <= index < len(my_list):
item = my_list[index]
else:
print("Index out of range.")
# Example 3: File Existence
import os
file_path = "data.txt"
if os.path.exists(file_path):
with open(file_path, "r") as file:
data = file.read()
else:
print("File not found.")
# Example 4: Key Existence in Dictionary
my_dict = {"key1": "value1", "key2": "value2"}
key = "key3"
if key in my_dict:
value = my_dict[key]
else:
print(f"Key '{key}' not found.")
# Example 5: Attribute Existence
class MyClass:
def __init__(self, value):
self.value = value
my_instance = MyClass(42)
if hasattr(my_instance, "value"):
print(my_instance.value)
else:
print("Attribute 'value' not found.")
# Example 6: Error Handling with try and except
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
else:
print("Execute if no exception")
finally:
print("Always executed.")
# Example 7: Timeouts
import urllib.request
import socket
try:
response = urllib.request.urlopen("http://example.com", timeout=5)
except (urllib.error.URLError, socket.timeout) as e:
print(f"Error: {e}")
# Example 8: Concurrency Issues
import threading
lock = threading.Lock()
def update_shared_variable():
with lock:
# Code to update shared variable
pass
# Example 9: Recursion depth check to avoid stack overflow / maximum recursion depth exceeded
import sys
def factorial(n):
if n < sys.getrecursionlimit():
return n * factorial(n - 1)
else:
print("Recursion limit exceeded.")
# Example 10: Database connection / context manager
import sqlite3
conn = sqlite3.connect("mydb.db")
try:
cursor = conn.cursor()
# Database operations
finally:
conn.close()
# Example 11: Graceful Degradation / Fallback
import requests
try:
response = requests.get("https://api.example.com/data")
data = response.json()
except (requests.ConnectionError, requests.Timeout):
# Use cached data or provide a default response.
pass
# Example 12: Memory Leak Detection
import tracemalloc
tracemalloc.start()
# Code with potential memory leaks
snapshot = tracemalloc.take_snapshot()
# Analyze snapshot for memory leaks