0

我正在创建一个 python 文件来读取一些 json 数据。然后将该数据分配给我的 protobuf 消息。我想传入 "start_hour": 8 并从我的枚举中接收 8 而不是 EIGHT。也许我完全错过了一些东西,或者我可以以不同的方式做到这一点。我正在使用枚举,因为我想将值限制为 0 - 24。

scheduler.proto 文件:

syntax = "proto3";

package scheduler;

// defines the days and hours worked
message Shift {
    DayOfTheWeek day_of_the_week = 1;
    HourOfTheDay start_hour = 2;
    HourOfTheDay end_hour = 3;
}

// defines values for days of the week
enum DayOfTheWeek {
    UKNOWN = 0;
    MONDAY = 1;
    TUESDAY = 2;
    WEDNESDAY = 3;
    THURSDAY = 4;
    FRIDAY = 5;
    SATURDAY = 6;
    SUNDAY = 7;
}

// defines hours of the day
enum HourOfTheDay {
    UNKNOWN = 0;
    ONE = 1;
    TWO = 2;
    THREE = 3;
    FOUR = 4;
    FIVE = 5;
    SIX = 6;
    SEVEN = 7;
    EIGHT = 8;
    NINE = 9;
    TEN = 10;
    ELEVEN = 11;
    TWELVE = 12;
    THIRTEEN = 13;
    FOURTEEN = 14;
    FIFTEEN = 15;
    SIXTEEN = 16;
    SEVENTEEN = 17;
    EIGHTEEN = 18;
    NINETEEN = 19;
    TWENTY = 20;
    TWENTYONE = 21;
    TWENTYTWO = 22;
    TWENTYTHREE = 23;
    TWENTYFOUR = 24;
}

// defines the employee and hours/shifts worked
message Employee {
    string employee_id = 1;
    int32 base_pay = 2;
    repeated string shift_list = 3;
    int32 weekend_bonus = 4;
    int32 night_bonus = 5;
}

调度程序.py 文件

 import scheduler_pb2
 import json


f = open("schedules.json")
data = json.load(f)


shift_message = scheduler_pb2.Shift()
shift_message.day_of_the_week = data["schedule_list"][0]["day_of_the_week"]
shift_message.start_hour = data["schedule_list"][0]["start_hour"] # this gives back EIGHT I want 8


employee_message = scheduler_pb2.Employee()
employee_message.employee_id = data["schedule_list"][0]["employee_id"]
employee_message.base_pay = data["schedule_list"][0]["base_pay"]




print(shift_message)
print(employee_message)

时间表.json:

{
"schedule_list":[
    {
        "day_of_the_week": 1,
        "start_hour": 8,
        "end_hour": 5,
        "employee_id": "E00001",
        "base_pay": 10,
        "shift_list": "Day Shift",
        "weekend_bonus": 0,
        "night_bonus": 0
    }
]

}

输出如下所示:

day_of_the_week: MONDAY
start_hour: EIGHT

employee_id: "E00001"
base_pay: 10

我希望输出看起来像这样:

day_of_the_week: MONDAY
start_hour: 8

employee_id: "E00001"
base_pay: 10
4

1 回答 1

0

我的猜测(!)是,如果您查看生成的类HourOfTheDay,将会有一个生成的方法将给定的枚举转换HourOfTheDay.EIGHT为它的值(8)。

更新

你激起了我的好奇心。

笔记

print(shift)
start_hour: EIGHT

print(shift.start_hour)
8

这是因为 protobuf 消息可能有一个通用 __str__(!?) 函数。

但是,如果您愿意,可以覆盖默认功能:

day_of_the_week这是一个使用 enumName函数和start_hour(如您所愿)使用 enum打印的示例Value

from google.protobuf import text_format
import scheduler_pb2

shift = scheduler_pb2.Shift()

shift.day_of_the_week = scheduler_pb2.FRIDAY # or 5
shift.start_hour = scheduler_pb2.EIGHT   # or 8

# print(shift)            # Returns start_hour: EIGHT
# print(shift.start_hour) # Returns 8

text = text_format.MessageToString(
    shift,
    message_formatter=lambda m, i, b: "day: {}\nhour: {}".format(
        scheduler_pb2.DayOfTheWeek.Name(m.day_of_the_week),
        m.start_hour,
    ),
)
print(text)
于 2022-01-07T19:29:03.857 回答