Skip to content

Commit

Permalink
Python Serialize: Apply TR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bzaczynski committed Nov 14, 2023
1 parent c3a470b commit 10d295a
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from . import views

urlpatterns = [path("", views.view_users)]
urlpatterns = [path("", views.handle_users)]
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions python-serialize/http-payload/flask-rest-api/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions python-serialize/http-payload/pydantic-demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 7 additions & 7 deletions python-serialize/schema-based/protocol-buffers-demo/main.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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(
Expand Down

0 comments on commit 10d295a

Please sign in to comment.