0

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

1 Answers1

0

My guess (!) is that, if you look through the generated classes for HourOfTheDay, there will be a generated method that converts a given enum e.g. HourOfTheDay.EIGHT into its value (8).

Update

You piqued my curiosity.

NOTE

print(shift)
start_hour: EIGHT

print(shift.start_hour)
8

This is because there's probably a general-purpose __str__ (!?) function for protobuf messages.

But, you can override the default function if you wish:

Here's an example that prints day_of_the_week using the enum Name function and start_hour (as you wish) using the 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)
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Thanks @DazWilkin. I wonder though if there is a way to have HourOfTheDay.8 output 8. I would like my json data "8" to also output to be 8. While limiting the values of 0 - 24. If that makes sense? – YourMomsDataEngineer Jan 07 '22 at 20:17