Python Assignment

The Oracle

Object modeling & structured queries

01 — What is the Oracle?

Pretend there is a black box. You can ask it any question, and it will return a response in a particular form. However, you are only allowed to ask types of questions.

A question type is a clearly defined query that accepts one or more objects as input. Each class (object specification) must be defined with typed fields before you can use it.

The rule

Every question type takes objects as input. Every response is also a structured object. You define both.

02 — Example: A Stock Oracle

Here is an example. The oracle answers questions about stocks. First, define your questions:

  • How will STOCK perform?
  • I have STOCK_PURCHASE what should I do with it?

Then define your input classes and response classes:

from pydantic import BaseModel
from typing import Optional
from enum import Enum


# The objects (inputs)

class Stock(BaseModel):
    symbol: str
    market: str

class StockPurchase(BaseModel):
    stock: Stock
    quantity: float
    price: float


# The response (output)

class Status(Enum):
    SELL = "SELL"
    HOLD = "HOLD"
    BUY  = "BUY"

class Response(BaseModel):
    status: Optional[Status]
    by: Optional[float]  # percentage/amount
    reason: str

03 — Your Assignment

Design your law oracle.

What to create

  1. Write what questions you want the oracle to answer
  2. Define at least 2 objects with typed fields (one should reference the other)
  3. Define Response objects for each question

You do not need to write the specifications in Python. I got AI to do that for me from natural language.

04 — Object Diagram

Here is how the objects relate to each other and to the oracle:

THE ORACLE — STOCK EXAMPLE Stock symbol str market str StockPurchase stock Stock quantity float price float references Status SELL "SELL" HOLD "HOLD" BUY "BUY" Response status Status? by float? reason str ? = Optional references Oracle def analyze_stock stock: Stock → Response def evaluate_purchase purchase: StockPurchase → Response input references returns input object output object field name primitive type object type enum value
THE ORACLE Python Assignment Object Modeling
Tarea de Python

El Oráculo

Modelado de objetos y consultas estructuradas

01 — ¿Qué es el Oráculo?

Imagina que exista una caja negra. Puedes preguntarle cualquier cosa y te devolverá una respuesta en una forma particular. Sin embargo, el oráculo solo puede entender tipos de preguntas.

Un tipo de pregunta es una consulta claramente definida que acepta uno o más objetos como entrada. Cada objeto debe definirse en una clase con variables tipados.

La regla

Cada tipo de pregunta recibe objetos como entrada. Cada respuesta también es un objeto estructurado. Tú defines ambos.

02 — Ejemplo: Un Oráculo de Acciones

Aquí hay un ejemplo. El oráculo responde preguntas sobre acciones bursátiles. Primero, define tus preguntas:

  • ¿Cómo le irá a ACCION?
  • Tengo COMPRA_DE_ACCION, ¿qué debo hacer con ella?

Luego define tus clases de entrada y de respuesta:

from pydantic import BaseModel
from typing import Optional
from enum import Enum


# Los objetos (entradas)

class Accion(BaseModel):
    simbolo: str
    mercado: str

class CompraDeAccion(BaseModel):
    accion: Accion
    cantidad: float
    precio: float


# La respuesta (salida)

class Estado(Enum):
    VENDER = "VENDER"
    MANTENER = "MANTENER"
    COMPRAR = "COMPRAR"

class Respuesta(BaseModel):
    estado: Optional[Estado]
    por: Optional[float]  # porcentaje/cantidad
    razon: str

03 — Tu Tarea

Diseña tu oráculo de leyes.

Qué hacer

  1. Escribe qué preguntas quieres que el oráculo responda
  2. Define al menos 2 objetos con variables tipados (uno debe referenciar al otro)
  3. Define objetos Respuesta para cada pregunta

No necesitas escribir las especificaciones en Python. Yo usé IA para hacerlo desde lenguaje natural.

04 — Diagrama de Objetos

Así es como los objetos se relacionan entre ellos y con el oráculo:

EL ORÁCULO — EJEMPLO DE ACCIONES Accion simbolo str mercado str CompraDeAccion accion Accion cantidad float precio float referencia Estado VENDER "VENDER" MANTENER "MANTENER" COMPRAR "COMPRAR" Respuesta estado Estado? por float? razon str ? = Opcional referencia Oráculo def analizar_accion accion: Accion → Respuesta def evaluar_compra compra: CompraDeAccion → Respuesta entrada referencia devuelve objeto entrada objeto salida nombre de variable tipo primitivo tipo objeto valor enum