我有从 JSON 创建的 Python 数据类(实际上有很多)。我想要一种从 JSON 创建类实例的方法。
我有这样的事情:
class FromJSONMixin:
@staticmethod
@abstractmethod
def from_json(json: Union[Dict, TypedDict], **kwargs):
raise NotImplementedError
class PatientJSON(TypedDict):
ID: str
Name: str
Description: str
BirthDate: str
@dataclass
class Patient(FromJSONMixin):
name: str
birth_date: str
description: str
@staticmethod
def from_json(json: PatientJSON, **kwargs) -> Patient:
return Patient(
name=json["Name"],
birth_date=json["BirthDate"],
description=raw_data["Description"])
Run Code Online (Sandbox Code Playgroud)
我想Patient从中创建对象PatientJSON(结构与现有数据库相关,我必须与它集成;它还进行一些名称属性翻译,如上所示)。我创建了FromJSONMixin来显式标记可以从 JSON 的相关类创建的类(例如PatientJSON)。
问题:-> Patient:我收到零件错误Unresolved reference 'Patient'。为什么?我无法在同一类的方法中键入类对象?我是否必须放弃输入返回类型?
我想在 PyTorch 和 Torchvision 中加载 MNIST 数据集,将其分为训练、验证和测试部分。到目前为止,我有:
def load_dataset():
train_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(
'/data/', train=True, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor()])),
batch_size=batch_size_train, shuffle=True)
test_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(
'/data/', train=False, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor()])),
batch_size=batch_size_test, shuffle=True)
Run Code Online (Sandbox Code Playgroud)
如果训练数据集在DataLoader. 我想使用训练数据集中的最后 10000 个示例作为验证数据集(我知道我应该做 CV 以获得更准确的结果,我只想在这里快速验证)。
我希望在我的TypedDict对象中有一个别名,以便在运行时期间避免许多 if/else 检查(TypedDict在数据加载期间创建)。
我有这样的事情:
class PatientJson(TypedDict):
id: int
name: str
Run Code Online (Sandbox Code Playgroud)
我想添加user_id别名PatientJson,这将返回id. 这将使列表推导始终指向user_id而不是检查哪个 ID 存在。
但是,使用这段代码:
class PatientJson(TypedDict):
id: int
name: str
@property
def user_id(self) -> int
return self.id
Run Code Online (Sandbox Code Playgroud)
我收到一个错误Invalid statement in TypedDict definition; expected 'field_name: field_type'。我发现 TypedDicts 不能有方法(链接 1,链接 2)。这也会改变语法,因为我必须使用patient.user_id而不是patient["user_id"],这将是很多代码更改。我怎样才能做到这一点?如果可能的话,我想避免添加冗余字段。
我正在尝试使用 Connection 对象在 Apache Airflow 中获取 S3 挂钩。
它看起来像这样:
class S3ConnectionHandler:
def __init__():
# values are read from configuration class, which loads from env. variables
self._s3 = Connection(
conn_type="s3",
conn_id=config.AWS_CONN_ID,
login=config.AWS_ACCESS_KEY_ID,
password=config.AWS_SECRET_ACCESS_KEY,
extra=json.dumps({"region_name": config.AWS_DEFAULT_REGION}),
)
@property
def s3(self) -> Connection:
return get_live_connection(self.logger, self._s3)
@property
def s3_hook(self) -> S3Hook:
return self.s3.get_hook()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Broken DAG: [...] Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/connection.py", line 282, in get_hook
return hook_class(**{conn_id_param: self.conn_id})
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/providers/amazon/aws/hooks/base_aws.py", line 354, in __init__
raise AirflowException('Either client_type or resource_type must …Run Code Online (Sandbox Code Playgroud) 我有一个由 2D 张量组成的 3D 张量,例如:
t = torch.tensor([[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]],
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]],
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]]
])
Run Code Online (Sandbox Code Playgroud)
我需要这些二维张量的总和的列表或张量,例如:sums = [3, 3, 3]。到目前为止我有:
sizes = [torch.sum(t[i]) for i in range(t.shape[0])]
Run Code Online (Sandbox Code Playgroud)
我认为这只能用 PyTorch 来完成,但我尝试使用torch.sum()所有可能的维度,并且我总是得到这些 2D 张量的各个字段的总和,例如:
[[0, 0, 3],
[0, 3, 0],
[3, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
在 PyTorch 中如何做到这一点?
我在 CI/CD (Jenkins) 中使用 Alpine Linux 作为我的 Python 应用程序的 Docker 镜像。这是针对 AWS Lambda 的,基本上唯一值得注意的要求是aws-psycopg2访问 Postgres。本地一切正常,它可以很好地使用无服务器部署并在 AWS 上运行,唯一的问题是在 Jenkins 上。我在那里收到错误:
16:24:55 ImportError: Error loading shared library libresolv.so.2: No such file or directory (needed by /usr/local/lib/python3.9/site-packages/psycopg2/../psycopg2_binary.libs/libgssapi_krb5-497db0c6.so.2.2)
Run Code Online (Sandbox Code Playgroud)
我没有以任何方式使用 Oracle,例如在这个问题中。
我的jenkinsfile.groovy:
def testInstallStage = {
stage('Install') {
sh 'apk add python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev cargo'
sh 'pip install --upgrade pip && pip install poetry'
sh 'poetry export --without-hashes --dev -f requirements.txt -o …Run Code Online (Sandbox Code Playgroud) 我想在 SQLAlchemy ORM 中创建 BigInteger Identity 列。文档没有任何 ORM Identity 或 BigInteger Identity 的示例。
Mapping[]括号内?我一直在使用 Docker 和 pipelinev 进行数据科学部署设置,现在我想更改为 Poetry。我的 Dockerfile 是:
FROM python:3.8-alpine3.13
ENV POETRY_VIRTUALENVS_CREATE=false \
POETRY_VERSION=1.1.11
RUN apk add --no-cache python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev \
&& pip install poetry
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt && sed -i 's/^-e //' requirements.txt
USER root
RUN apt-get update && apt-get install -y --no-install-recommends python3-dev gcc libc-dev musl-dev openssh-client git libpq-dev \
&& apt-get clean -y
# install dependencies from requirements.txt
RUN pip install …Run Code Online (Sandbox Code Playgroud) python ×7
docker ×2
json ×2
pytorch ×2
airflow ×1
alpine-linux ×1
amazon-s3 ×1
dictionary ×1
jenkins ×1
linux ×1
postgresql ×1
python-venv ×1
sqlalchemy ×1