-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python.py
More file actions
132 lines (99 loc) · 2.86 KB
/
Copy pathtest_python.py
File metadata and controls
132 lines (99 loc) · 2.86 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
126
127
128
129
130
131
132
"""
Test Python file for sensegrep multilingual support.
"""
from dataclasses import dataclass
from typing import Protocol, TypedDict, Optional
from enum import Enum
import hashlib
class UserRole(Enum):
"""User role enumeration."""
ADMIN = "admin"
USER = "user"
GUEST = "guest"
class Hashable(Protocol):
"""Protocol for hashable objects."""
def get_hash(self) -> str:
...
class UserConfig(TypedDict):
"""Configuration for user settings."""
theme: str
language: str
notifications: bool
@dataclass
class User:
"""User entity with authentication support."""
id: str
name: str
email: str
role: UserRole
def get_display_name(self) -> str:
"""Get formatted display name."""
return f"{self.name} ({self.role.value})"
@property
def is_admin(self) -> bool:
"""Check if user has admin privileges."""
return self.role == UserRole.ADMIN
@staticmethod
def generate_id() -> str:
"""Generate a unique user ID."""
import uuid
return str(uuid.uuid4())
@classmethod
def create_guest(cls) -> "User":
"""Factory method to create a guest user."""
return cls(
id=cls.generate_id(),
name="Guest",
email="guest@example.com",
role=UserRole.GUEST
)
def calculate_hash(data: str) -> str:
"""Calculate SHA256 hash of data."""
return hashlib.sha256(data.encode()).hexdigest()
async def fetch_user_data(user_id: str) -> Optional[dict]:
"""
Fetch user data from remote API.
This is an async function that simulates API call.
"""
import asyncio
await asyncio.sleep(0.1) # Simulate network delay
if user_id == "invalid":
return None
return {
"id": user_id,
"name": "Test User",
"email": "test@example.com"
}
def _private_helper(x: int) -> int:
"""Private helper function (not exported)."""
return x * 2
def complex_validation(data: dict) -> bool:
"""
Complex validation with multiple conditions.
This function has high cyclomatic complexity.
"""
if not data:
return False
if "name" not in data:
return False
if "email" not in data:
return False
if len(data["name"]) < 2:
return False
if "@" not in data["email"]:
return False
if "role" in data:
if data["role"] not in ["admin", "user", "guest"]:
return False
if data["role"] == "admin":
if "admin_key" not in data:
return False
for key, value in data.items():
if value is None:
return False
if isinstance(value, str) and len(value) > 1000:
return False
return True
# Module-level constant
MAX_USERS = 1000
API_VERSION = "v2"