进行到一半时Architecture Patterns with Python,我有两个关于应该如何构造和实例化领域模型类的问题。假设在我的领域模型上我有这样的课程DepthMap:
class DepthMap:
def __init__(self, map: np.ndarray):
self.map = map
Run Code Online (Sandbox Code Playgroud)
根据我从书中的理解,这个类是不正确的,因为它依赖于Numpy,它应该只依赖于 Python 原语,因此问题是:域模型类应该只依赖于 Python 原语,还是有例外?
假设上一个问题的答案是类应该完全依赖于基元,那么从 Numpy 数组创建 DepthMap 的正确方法是什么?假设现在我有更多格式可以制作 DepthMap 对象。
class DepthMap:
def __init__(self, map: List):
self.map = map
@classmethod
def from_numpy(cls, map: np.ndarray):
return cls(map.tolist())
@classmethod
def from_str(cls, map: str):
return cls([float(i) for i in s.split(',')])
Run Code Online (Sandbox Code Playgroud)
或工厂:
class DepthMapFactory:
@staticmethod
def from_numpy(map: np.ndarray):
return DepthMap(map.tolist())
@staticmethod
def from_str(map: str):
return DepthMap([float(i) for i in s.split(',')]) …Run Code Online (Sandbox Code Playgroud) python architecture oop domain-driven-design repository-pattern
我想下载 Google 存储桶上的 blob。客户拥有的方法get_bucket和方法有什么区别?bucket为什么他们的权限不同?两者都可以用来下载 blob 吗?
predict_step为什么张量流在a 函数内禁用急切执行tf.keras.Model?也许我弄错了,但这里有一个例子:
from __future__ import annotations
from functools import wraps
import tensorflow as tf
def print_execution(func):
@wraps(func)
def wrapper(self: SimpleModel, data):
print(tf.executing_eagerly()) # Prints False
return func(self, data)
return wrapper
class SimpleModel(tf.keras.Model):
def __init__(self):
super().__init__()
def call(self, inputs, training=None, mask=None):
return inputs
@print_execution
def predict_step(self, data):
return super().predict_step(data)
if __name__ == "__main__":
x = tf.random.uniform((2, 2))
print(tf.executing_eagerly()) # Prints True
model = SimpleModel()
pred = model.predict(x)
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?有没有办法强制predict_step以急切模式运行?