Common protocol buffer definitions used across Google APIs and client libraries
—
Fundamental data types for temporal, geographic, financial, and contact information used across Google APIs. These types provide standardized representations for common concepts, ensuring consistency across the Google API ecosystem.
Monetary amounts and currency representation with precise decimal handling.
from google.type.money_pb2 import Money
from google.type.decimal_pb2 import Decimal
from google.type.fraction_pb2 import Fraction
class Money(message.Message):
"""Monetary amount with currency code."""
currency_code: str # ISO 4217 currency code (e.g., "USD", "EUR")
units: int # Whole currency units
nanos: int # Fractional units (nano-units, 10^-9)
class Decimal(message.Message):
"""Arbitrary precision decimal number."""
value: str # Decimal value as string
class Fraction(message.Message):
"""Fraction representation."""
numerator: int # Fraction numerator
denominator: int # Fraction denominatorDate, time, and calendar representations for various temporal concepts.
from google.type.date_pb2 import Date
from google.type.datetime_pb2 import DateTime
from google.type.timeofday_pb2 import TimeOfDay
from google.type.interval_pb2 import Interval
from google.type.dayofweek_pb2 import DayOfWeek
from google.type.month_pb2 import Month
from google.type.calendar_period_pb2 import CalendarPeriod
class Date(message.Message):
"""Calendar date (year, month, day)."""
year: int # Year (e.g., 2023)
month: int # Month (1-12)
day: int # Day of month (1-31)
class DateTime(message.Message):
"""Date and time with timezone information."""
year: int # Year
month: int # Month (1-12)
day: int # Day of month (1-31)
hours: int # Hours (0-23)
minutes: int # Minutes (0-59)
seconds: int # Seconds (0-59)
nanos: int # Nanoseconds (0-999,999,999)
time_offset: DateTime.TimeOffset # Timezone offset
class TimeOffset(message.Message):
"""Timezone offset information."""
# Union field (one of):
utc_offset: Duration # UTC offset
time_zone: TimeZone # Named timezone
class TimeOfDay(message.Message):
"""Time of day (hours, minutes, seconds)."""
hours: int # Hours (0-23)
minutes: int # Minutes (0-59)
seconds: int # Seconds (0-59)
nanos: int # Nanoseconds (0-999,999,999)
class Interval(message.Message):
"""Time interval between two timestamps."""
start_time: Timestamp # Interval start time
end_time: Timestamp # Interval end time
class DayOfWeek(enum.Enum):
"""Day of week enumeration."""
DAY_OF_WEEK_UNSPECIFIED = 0
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7
class Month(enum.Enum):
"""Month enumeration."""
MONTH_UNSPECIFIED = 0
JANUARY = 1
FEBRUARY = 2
MARCH = 3
APRIL = 4
MAY = 5
JUNE = 6
JULY = 7
AUGUST = 8
SEPTEMBER = 9
OCTOBER = 10
NOVEMBER = 11
DECEMBER = 12
class CalendarPeriod(enum.Enum):
"""Calendar period enumeration."""
CALENDAR_PERIOD_UNSPECIFIED = 0
DAY = 1
WEEK = 2
FORTNIGHT = 3
MONTH = 4
QUARTER = 5
HALF = 6
YEAR = 7Location, coordinate, and spatial data representations.
from google.type.latlng_pb2 import LatLng
from google.type.quaternion_pb2 import Quaternion
class LatLng(message.Message):
"""Geographic coordinates (latitude, longitude)."""
latitude: float # Latitude in degrees (-90 to +90)
longitude: float # Longitude in degrees (-180 to +180)
class Quaternion(message.Message):
"""3D rotation representation using quaternion."""
x: float # X component
y: float # Y component
z: float # Z component
w: float # W component (scalar part)Phone numbers and postal addresses with international support.
from google.type.phone_number_pb2 import PhoneNumber
from google.type.postal_address_pb2 import PostalAddress
class PhoneNumber(message.Message):
"""Phone number representation."""
# Union field (one of):
e164_number: str # E.164 format (e.g., "+12345678900")
short_code: PhoneNumber.ShortCode # Short code for specific region
extension: str # Phone extension
class ShortCode(message.Message):
"""Short code representation."""
region_code: str # ISO 3166-1 alpha-2 region code
number: str # Short code number
class PostalAddress(message.Message):
"""Postal address representation."""
revision: int # Address revision number
region_code: str # ISO 3166-1 alpha-2 region code
language_code: str # BCP-47 language code
postal_code: str # Postal/ZIP code
sorting_code: str # Sorting code
administrative_area: str # State/province/prefecture
locality: str # City/town
sublocality: str # Sublocality/district/ward
address_lines: list[str] # Street address lines
recipients: list[str] # Recipients/addressees
organization: str # Organization nameLocalized text, color, and expression representations.
from google.type.localized_text_pb2 import LocalizedText
from google.type.color_pb2 import Color
from google.type.expr_pb2 import Expr
class LocalizedText(message.Message):
"""Localized text content."""
text: str # Text content
language_code: str # BCP-47 language code
class Color(message.Message):
"""RGBA color representation."""
red: float # Red component (0.0-1.0)
green: float # Green component (0.0-1.0)
blue: float # Blue component (0.0-1.0)
alpha: float # Alpha/transparency (0.0-1.0)
class Expr(message.Message):
"""Expression representation (CEL - Common Expression Language)."""
expression: str # CEL expression
title: str # Expression title
description: str # Expression description
location: str # Source location informationfrom google.type.money_pb2 import Money
# Create a monetary amount ($29.99 USD)
price = Money()
price.currency_code = "USD"
price.units = 29
price.nanos = 990000000 # 0.99 * 10^9
# Create from float (be careful with floating point precision)
def money_from_float(currency_code: str, amount: float) -> Money:
money = Money()
money.currency_code = currency_code
money.units = int(amount)
money.nanos = int((amount - money.units) * 1_000_000_000)
return money
price = money_from_float("EUR", 42.50) # €42.50from google.type.date_pb2 import Date
from google.type.datetime_pb2 import DateTime
from google.type.timeofday_pb2 import TimeOfDay
from google.protobuf.duration_pb2 import Duration
# Create a specific date
date = Date()
date.year = 2023
date.month = 12
date.day = 25
# Create a datetime with timezone
datetime = DateTime()
datetime.year = 2023
datetime.month = 12
datetime.day = 25
datetime.hours = 14
datetime.minutes = 30
datetime.seconds = 0
# Set UTC offset (-5 hours)
datetime.time_offset.utc_offset.seconds = -5 * 3600
# Create time of day
time = TimeOfDay()
time.hours = 14
time.minutes = 30
time.seconds = 45
time.nanos = 500000000 # 0.5 secondsfrom google.type.latlng_pb2 import LatLng
# Create coordinates for a location
location = LatLng()
location.latitude = 37.7749 # San Francisco latitude
location.longitude = -122.4194 # San Francisco longitude
# Validate coordinate ranges
def validate_coordinates(latlng: LatLng) -> bool:
return (-90 <= latlng.latitude <= 90 and
-180 <= latlng.longitude <= 180)from google.type.phone_number_pb2 import PhoneNumber
# Create E.164 format phone number
phone = PhoneNumber()
phone.e164_number = "+12125551234"
# Create short code
short_code_phone = PhoneNumber()
short_code_phone.short_code.region_code = "US"
short_code_phone.short_code.number = "911"
# Add extension
phone_with_ext = PhoneNumber()
phone_with_ext.e164_number = "+12125551234"
phone_with_ext.extension = "123"from google.type.postal_address_pb2 import PostalAddress
# Create a US postal address
address = PostalAddress()
address.revision = 1
address.region_code = "US"
address.language_code = "en"
address.postal_code = "10001"
address.administrative_area = "NY"
address.locality = "New York"
address.address_lines.extend([
"123 Main Street",
"Apt 4B"
])
address.recipients.append("John Doe")
address.organization = "Example Corp"from google.type.color_pb2 import Color
# Create RGB colors
red = Color()
red.red = 1.0
red.green = 0.0
red.blue = 0.0
red.alpha = 1.0 # Fully opaque
# Create semi-transparent blue
blue = Color()
blue.red = 0.0
blue.green = 0.0
blue.blue = 1.0
blue.alpha = 0.5 # 50% transparent
# Convert from hex color
def color_from_hex(hex_color: str) -> Color:
"""Convert hex color (#RRGGBB or #RRGGBBAA) to Color."""
hex_color = hex_color.lstrip('#')
color = Color()
color.red = int(hex_color[0:2], 16) / 255.0
color.green = int(hex_color[2:4], 16) / 255.0
color.blue = int(hex_color[4:6], 16) / 255.0
if len(hex_color) == 8: # Include alpha
color.alpha = int(hex_color[6:8], 16) / 255.0
else:
color.alpha = 1.0
return color
# Usage: color = color_from_hex("#FF5733")from google.type.expr_pb2 import Expr
# Create a CEL expression for authorization
auth_expr = Expr()
auth_expr.expression = "request.auth.claims.sub == resource.owner"
auth_expr.title = "Resource Owner Check"
auth_expr.description = "Verify that the authenticated user owns the resource"
# Create a validation expression
validation_expr = Expr()
validation_expr.expression = "request.amount > 0 && request.amount <= 1000"
validation_expr.title = "Amount Validation"
validation_expr.description = "Ensure amount is positive and within limits"from google.type.interval_pb2 import Interval
from google.protobuf.timestamp_pb2 import Timestamp
import time
# Create time interval
interval = Interval()
# Set start time (current time)
start_time = Timestamp()
start_time.GetCurrentTime()
interval.start_time.CopyFrom(start_time)
# Set end time (1 hour later)
end_time = Timestamp()
end_time.seconds = start_time.seconds + 3600 # Add 1 hour
interval.end_time.CopyFrom(end_time)
# Check if timestamp is within interval
def is_in_interval(timestamp: Timestamp, interval: Interval) -> bool:
return (interval.start_time.seconds <= timestamp.seconds <=
interval.end_time.seconds)Install with Tessl CLI
npx tessl i tessl/pypi-googleapis-common-protos