diff --git a/python-serialize/executable-code/digital-signature/trustworthy.py b/python-serialize/executable-code/digital-signature/trustworthy.py index f5c3e1fbf4..751c84a6d7 100644 --- a/python-serialize/executable-code/digital-signature/trustworthy.py +++ b/python-serialize/executable-code/digital-signature/trustworthy.py @@ -16,7 +16,7 @@ def safe_load(file, secret_key): serialized_data = file.read() if signature == sign(serialized_data, secret_key): return dill.loads(serialized_data) - raise dill.UnpicklingError("Invalid digital signature") + raise dill.UnpicklingError("invalid digital signature") def sign(message, secret_key, algorithm=hashlib.sha256): diff --git a/python-serialize/http-payload/django-rest-api/rest_api/urls.py b/python-serialize/http-payload/django-rest-api/rest_api/urls.py index 36be1d3600..9f96dab657 100644 --- a/python-serialize/http-payload/django-rest-api/rest_api/urls.py +++ b/python-serialize/http-payload/django-rest-api/rest_api/urls.py @@ -2,4 +2,4 @@ from . import views -urlpatterns = [path("", views.view_users)] +urlpatterns = [path("", views.handle_users)] diff --git a/python-serialize/http-payload/django-rest-api/rest_api/views.py b/python-serialize/http-payload/django-rest-api/rest_api/views.py index b5ed2bac46..0adf175b9f 100644 --- a/python-serialize/http-payload/django-rest-api/rest_api/views.py +++ b/python-serialize/http-payload/django-rest-api/rest_api/views.py @@ -7,7 +7,7 @@ @api_view(["GET", "POST"]) -def view_users(request): +def handle_users(request): if request.method == "GET": users = User.objects.all() serializer = UserSerializerOut(users, many=True) diff --git a/python-serialize/http-payload/flask-rest-api/main.py b/python-serialize/http-payload/flask-rest-api/main.py index 7a363e0dce..1841c92e9a 100644 --- a/python-serialize/http-payload/flask-rest-api/main.py +++ b/python-serialize/http-payload/flask-rest-api/main.py @@ -1,18 +1,3 @@ -""" -GET Example: -$ curl http://127.0.0.1:5000/users - -POST Example: -$ curl -X POST http://127.0.0.1:5000/users \ - -H 'Content-Type: application/json' \ - --data '{"name": "Frank"}' -{ - "created_at": "Sun, 05 Nov 2023 17:34:19 GMT", - "id": "6f3530f4-2251-49ad-899b-df4c4f47009d", - "name": "Frank" -} -""" - from dataclasses import dataclass from datetime import datetime from uuid import UUID, uuid4 diff --git a/python-serialize/http-payload/pydantic-demo/main.py b/python-serialize/http-payload/pydantic-demo/main.py index 32900ba182..df385057ef 100644 --- a/python-serialize/http-payload/pydantic-demo/main.py +++ b/python-serialize/http-payload/pydantic-demo/main.py @@ -15,9 +15,9 @@ class User(Metadata): @field_validator("name") def check_user_name(cls, name): - if name.startswith(name.title()): + if name[0].isupper(): return name - raise ValueError("Name must start with an uppercase letter") + raise ValueError("name must start with an uppercase letter") if __name__ == "__main__": diff --git a/python-serialize/python-objects/standard-python/marshal_demo.py b/python-serialize/python-objects/standard-python/marshal_demo.py index 4f70bfe61f..5ea59a59b9 100644 --- a/python-serialize/python-objects/standard-python/marshal_demo.py +++ b/python-serialize/python-objects/standard-python/marshal_demo.py @@ -1,12 +1,12 @@ import marshal import sysconfig +from importlib.util import cache_from_source from pathlib import Path def main(): - py_version = sysconfig.get_python_version().replace(".", "") - cache_dir = Path(sysconfig.get_path("stdlib")) / "__pycache__" - module_path = cache_dir / f"decimal.cpython-{py_version}.pyc" + stdlib_dir = Path(sysconfig.get_path("stdlib")) + module_path = stdlib_dir / Path(cache_from_source("decimal.py")) import_pyc(module_path) print(Decimal(3.14)) # noqa diff --git a/python-serialize/schema-based/protocol-buffers-demo/main.py b/python-serialize/schema-based/protocol-buffers-demo/main.py index 84f55da40f..96e0c7e426 100644 --- a/python-serialize/schema-based/protocol-buffers-demo/main.py +++ b/python-serialize/schema-based/protocol-buffers-demo/main.py @@ -1,7 +1,7 @@ from models import Language, User -from users_pb2 import Language as LanguageDao -from users_pb2 import User as UserDao -from users_pb2 import Users as UsersDao +from users_pb2 import Language as LanguageDAO +from users_pb2 import User as UserDAO +from users_pb2 import Users as UsersDAO def main(): @@ -13,20 +13,20 @@ def main(): def serialize(): users = [User.fake() for _ in range(5)] - users_dao = UsersDao() + users_dao = UsersDAO() for user in users: - user_dao = UserDao() + user_dao = UserDAO() user_dao.id = user.id user_dao.name = user.name user_dao.email = user.email - user_dao.language = LanguageDao.Value(user.language.name) + user_dao.language = LanguageDAO.Value(user.language.name) user_dao.registered_at.FromDatetime(user.registered_at) users_dao.users.append(user_dao) return users_dao.SerializeToString() def deserialize(buffer): - users_dao = UsersDao() + users_dao = UsersDAO() users_dao.ParseFromString(buffer) return [ User(