Skip to content

Commit 10d295a

Browse files
committed
Python Serialize: Apply TR Feedback
1 parent c3a470b commit 10d295a

File tree

7 files changed

+15
-30
lines changed

7 files changed

+15
-30
lines changed

python-serialize/executable-code/digital-signature/trustworthy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def safe_load(file, secret_key):
1616
serialized_data = file.read()
1717
if signature == sign(serialized_data, secret_key):
1818
return dill.loads(serialized_data)
19-
raise dill.UnpicklingError("Invalid digital signature")
19+
raise dill.UnpicklingError("invalid digital signature")
2020

2121

2222
def sign(message, secret_key, algorithm=hashlib.sha256):

python-serialize/http-payload/django-rest-api/rest_api/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from . import views
44

5-
urlpatterns = [path("", views.view_users)]
5+
urlpatterns = [path("", views.handle_users)]

python-serialize/http-payload/django-rest-api/rest_api/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@api_view(["GET", "POST"])
10-
def view_users(request):
10+
def handle_users(request):
1111
if request.method == "GET":
1212
users = User.objects.all()
1313
serializer = UserSerializerOut(users, many=True)

python-serialize/http-payload/flask-rest-api/main.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
"""
2-
GET Example:
3-
$ curl http://127.0.0.1:5000/users
4-
5-
POST Example:
6-
$ curl -X POST http://127.0.0.1:5000/users \
7-
-H 'Content-Type: application/json' \
8-
--data '{"name": "Frank"}'
9-
{
10-
"created_at": "Sun, 05 Nov 2023 17:34:19 GMT",
11-
"id": "6f3530f4-2251-49ad-899b-df4c4f47009d",
12-
"name": "Frank"
13-
}
14-
"""
15-
161
from dataclasses import dataclass
172
from datetime import datetime
183
from uuid import UUID, uuid4

python-serialize/http-payload/pydantic-demo/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class User(Metadata):
1515

1616
@field_validator("name")
1717
def check_user_name(cls, name):
18-
if name.startswith(name.title()):
18+
if name[0].isupper():
1919
return name
20-
raise ValueError("Name must start with an uppercase letter")
20+
raise ValueError("name must start with an uppercase letter")
2121

2222

2323
if __name__ == "__main__":

python-serialize/python-objects/standard-python/marshal_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import marshal
22
import sysconfig
3+
from importlib.util import cache_from_source
34
from pathlib import Path
45

56

67
def main():
7-
py_version = sysconfig.get_python_version().replace(".", "")
8-
cache_dir = Path(sysconfig.get_path("stdlib")) / "__pycache__"
9-
module_path = cache_dir / f"decimal.cpython-{py_version}.pyc"
8+
stdlib_dir = Path(sysconfig.get_path("stdlib"))
9+
module_path = stdlib_dir / Path(cache_from_source("decimal.py"))
1010
import_pyc(module_path)
1111
print(Decimal(3.14)) # noqa
1212

python-serialize/schema-based/protocol-buffers-demo/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from models import Language, User
2-
from users_pb2 import Language as LanguageDao
3-
from users_pb2 import User as UserDao
4-
from users_pb2 import Users as UsersDao
2+
from users_pb2 import Language as LanguageDAO
3+
from users_pb2 import User as UserDAO
4+
from users_pb2 import Users as UsersDAO
55

66

77
def main():
@@ -13,20 +13,20 @@ def main():
1313

1414
def serialize():
1515
users = [User.fake() for _ in range(5)]
16-
users_dao = UsersDao()
16+
users_dao = UsersDAO()
1717
for user in users:
18-
user_dao = UserDao()
18+
user_dao = UserDAO()
1919
user_dao.id = user.id
2020
user_dao.name = user.name
2121
user_dao.email = user.email
22-
user_dao.language = LanguageDao.Value(user.language.name)
22+
user_dao.language = LanguageDAO.Value(user.language.name)
2323
user_dao.registered_at.FromDatetime(user.registered_at)
2424
users_dao.users.append(user_dao)
2525
return users_dao.SerializeToString()
2626

2727

2828
def deserialize(buffer):
29-
users_dao = UsersDao()
29+
users_dao = UsersDAO()
3030
users_dao.ParseFromString(buffer)
3131
return [
3232
User(

0 commit comments

Comments
 (0)