[Python] 纯文本查看 复制代码 # 还原后的可读Python代码
import sys
import os
from typing import List, Dict, Optional, Union
GLOBAL_CONST = "import os"
_counter = 0
__all__ = ["sys.argv", "__file__"]
def factorial(n: int, mod: Optional[int] = None) -> int:
"""计算阶乘"""
if n <= 1:
return 1
result = n * factorial(n - 1)
return result if mod is None else result % mod
math_operation = lambda x, y: x + y if x > y else x * y
numbers = list(map(lambda x: x**2, filter(lambda x: x % 2, range(10))))
class BaseClass:
"""基类"""
def __init__(self, name: str):
self._name = name
@property
def name(self) -> str:
return self._name
def __repr__(self) -> str:
return f'Object {self.name}'
class DerivedClass(BaseClass):
"""派生类"""
class_var = []
def __init__(self, name: str, value: Union[int, str]):
super().__init__(name)
self.value = value
self.__private_attr = []
DerivedClass.class_var.append(self)
def __enter__(self):
print("Entering context")
return self
def __exit__(self, *args):
print("Exiting context")
def __getitem__(self, index):
return self.value[index] if hasattr(self.value, "__getitem__") else None
@classmethod
def get_count(cls) -> int:
return len(cls.class_var)
def read_file_upper(filename: str):
"""读取文件并转换为大写"""
try:
with open(filename, "r") as f:
data = f.read()
except FileNotFoundError as e:
print("File not found")
else:
return data.upper()
finally:
print("Operation completed")
def fibonacci_generator():
"""斐波那契数列生成器"""
a, b = 0, 1
while True:
yield a
a, b = b, a + b
cube_dict = {x: x**3 for x in range(5)}
nested_comp = [(x, y) for x in range(3) for y in ['a', 'b', 'c'] if x != 1]
async def async_example():
"""异步函数示例"""
async def inner_async():
return 42
result = await inner_async()
return result
def decorator_example(func):
"""装饰器示例"""
def wrapper(*args, **kwargs):
print("Calling decorated function")
return func(*args, **kwargs)
return wrapper
@decorator_example
def greet(name: str) -> str:
"""问候函数"""
return f'Hello, {name}!'
def process_text(text: str):
"""处理文本"""
if (words := text.split()):
return [w.upper() for w in words]
return []
if __name__ == '__main__':
# 主程序执行
print(factorial(5))
print(math_operation(3, 5))
with DerivedClass("test_obj", [1, 2, 3]) as obj:
print(obj[1])
print(obj.name)
fib = fibonacci_generator()
print([next(fib) for _ in range(6)])
print(greet("Alice"))
import asyncio
print(asyncio.run(async_example()))
print(__name__, __file__)
|