I'm creating a python file to read in some json data. Then assigning that data to my protobuf messages. I want to pass in "start_hour": 8 and receive 8 and not EIGHT from my enum. Perhaps I'm missing something completely, or can I do this in a different way. I'm using an enum because I want to limit the values to 0 - 24.
scheduler.proto file:
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;
}
scheduler.py file
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)
schedule.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
}
]
}
output looks like this:
day_of_the_week: MONDAY
start_hour: EIGHT
employee_id: "E00001"
base_pay: 10
I want output to look like this:
day_of_the_week: MONDAY
start_hour: 8
employee_id: "E00001"
base_pay: 10