Skip to content

Commit

Permalink
Fixes initialization of pydantic model fields
Browse files Browse the repository at this point in the history
  • Loading branch information
klpanagi committed Dec 12, 2024
1 parent 6910462 commit d6e856b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions commlib/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Union
from uuid import UUID

from pydantic import BaseModel
from pydantic import BaseModel, Field

from commlib.utils import gen_timestamp

Expand All @@ -22,7 +22,7 @@ class MessageHeader(BaseModel):
msg_id: Union[int, str, UUID] = -1
node_id: Union[int, str, UUID] = ""
agent: str = "commlib-py"
timestamp: int = gen_timestamp()
timestamp: int = Field(default_factory=lambda: gen_timestamp())
properties: Dict[str, Any] = {}


Expand Down
2 changes: 1 addition & 1 deletion commlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def gen_timestamp() -> int:
int: Timestamp in integer representation. User `str()` to
transform to string.
"""
return int(1.0 * (time.time() + 0.5) * 1000)
return int(1.0 * (time.time_ns() + 0.5) * 1000)


def gen_random_id() -> str:
Expand Down
4 changes: 3 additions & 1 deletion examples/simple_pubsub/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import sys
import time

from pydantic import Field

from commlib.msg import MessageHeader, PubSubMessage
from commlib.node import Node


class SonarMessage(PubSubMessage):
header: MessageHeader = MessageHeader()
header: MessageHeader = Field(default_factory=lambda: MessageHeader())
range: float = -1
hfov: float = 30.6
vfov: float = 14.2
Expand Down

0 comments on commit d6e856b

Please sign in to comment.