Azure / Azure/azure-functions-durable-python
Proposal: Support Class-Based Syntax for Python Entities
- Vorherrschende Sprache
- Python
- Sterne
- 157
- Forks
- 70
- Ø Merge
- 2 T. 10 Std.
- Gemergte PRs (30 T.)
- 2
Beschreibung
Currently, Entities in Python (and JavaScript) only support function-based syntax, where operations are delineated by a cascade of if-statements. In C#, a class-based syntax is supported, which allows individual operations to be split into methods belonging to a larger class that represents the Entity itself.
Implementing this alternative syntax should not be too difficult, it's mostly just a layer of indirection. Making matters simpler, we already have a community-driven example of how to do this for TypeScript here: https://scale-tone.github.io/2021/03/15/durable-mvc .
As an example for the syntax, consider the following entity function using the current implementation syntax
```Python
def entity_function(context: df.DurableEntityContext):
current_value = context.get_state(lambda: 0)
operation = context.operation_name
if operation == "add":
amount = context.get_input()
current_value += amount
elif operation == "reset":
current_value = 0
elif operation == "get":
pass
context.set_state(current_value)
context.set_result(current_value)
```
It's class-based syntax alternative would look as follows:
```Python
class Counter:
def __init__(self): # this corresponds to state initializer, or the `lambda: 0` in the function-based syntax
self.entity_state= 0
def add(self, amount):
self.entity_state+= amount
return self.entity_state
def get(self):
return self.entity_state
def reset(self):
return self.entity_state= 0
```
Finally, it makes sense to me that the `return` statement would set the value for `context.set_result`. A more difficult question is to know what value to save for `set_state`. One easy solution is to require the state to be under the name `self.entity_state` but perhaps that's too restrictive. What do others think?
Beitragsleitfaden
Rechercherichtung
Der Issue nennt keine Dateien oder Tests. Beginne damit, die bestehende funktionsbasierte Entity-Verarbeitung rund um DurableEntityContext und das verlinkte TypeScript-Beispiel zu überprüfen, und ermittle anschließend, wie Klassenmethoden, Initialisierung, zurückgegebene Ergebnisse und persistierter Zustand dem aktuellen Entity-Modell zugeordnet werden sollten. Als erledigt gilt die Aufgabe erst, wenn vor der Implementierung ein abgestimmtes Design für Syntax und Zustandsverwaltung vorliegt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100