- **函数级的封装**:
```python
import random
def generate_random_data(data_structure):
if isinstance(data_structure, int):
return random.randint(0, data_structure)
elif isinstance(data_structure, float):
return random.uniform(0, data_structure)
elif isinstance(data_structure, list):
return [generate_random_data(item) for item in data_structure]
elif isinstance(data_structure, dict):
return {key: generate_random_data(value) for key, value in data_structure.items()}
else:
return None
使用修饰器进行扩展功能
def add_prefix(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return f"Random Data: {result}"
return wrapper
@add_prefix
def generate_random_data_wrapper(data_structure):
return generate_random_data(data_structure)
测试函数
data_structure = [1, 2, {'a': 3, 'b': [4, 5]}, 6]
random_data = generate_random_data_wrapper(data_structure)
print(random_data)
```
- **类的封装**:
```python
import random
class RandomDataGenerator:
def init(self):
pass
def generate_random_data(self, data_structure):
if isinstance(data_structure, int):
return random.randint(0, data_structure)
elif isinstance(data_structure, float):
return random.uniform(0, data_structure)
elif isinstance(data_structure, list):
return [self.generate_random_data(item) for item in data_structure]
elif isinstance(data_structure, dict):
return {key: self.generate_random_data(value) for key, value in data_structure.items()}
else:
return None
使用迭代器进行数据生成
def generator(self, data_structure):
if isinstance(data_structure, (list, dict)):
for item in data_structure:
yield self.generate_random_data(item)
测试类
data_structure = {'a': 1, 'b': [2, 3]}
generator = RandomDataGenerator()
random_data = generator.generate_random_data(data_structure)
print(random_data)
测试迭代器
for data in generator.generator(data_structure):
print(data)
```
- **包的封装**:
添加一个名为`random_generator`的包,结构如下:
```
random_generator/
init.py
```
`core.py`文件:
```python
import random
def generate_random_data(data_structure):
if isinstance(data_structure, int):
return random.randint(0, data_structure)
elif isinstance(data_structure, float):
return random.uniform(0, data_structure)
elif isinstance(data_structure, list):
return [generate_random_data(item) for item in data_structure]
elif isinstance(data_structure, dict):
return {key: generate_random_data(value) for key, value in data_structure.items()}
else:
return None
```
`generators.py`文件:
```python
from .core import generate_random_data
class RandomDataGenerator:
def init(self):
pass
def generate_random_data(self, data_structure):
return generate_random_data(data_structure)
def generator(self, data_structure):
if isinstance(data_structure, (list, dict)):
for item in data_structure:
yield generate_random_data(item)
```
`decorators.py`文件:
```python
def add_prefix(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return f"Random Data: {result}"
return wrapper
```
`init.py`文件:
```python
from .core import generate_random_data
from .generators import RandomDataGenerator
from .decorators import add_prefix
```
在包外调用:
```python
from random_generator import generate_random_data, RandomDataGenerator, add_prefix
data_structure = {'a': [1, 2], 'b': {'c': 3, 'd': 4}}
random_data = generate_random_data(data_structure)
print(random_data)
generator = RandomDataGenerator()
for data in generator.generator(data_structure):
print(data)
@add_prefix
def generate_random_data_with_prefix(data_structure):
return generate_random_data(data_structure)
random_data_prefixed = generate_random_data_with_prefix(data_structure)
print(random_data_prefixed)