https://github.com/j2kun/rote
A tiny python library for making human-in-the-loop terminal apps
https://github.com/j2kun/rote
Last synced: over 1 year ago
JSON representation
A tiny python library for making human-in-the-loop terminal apps
- Host: GitHub
- URL: https://github.com/j2kun/rote
- Owner: j2kun
- Created: 2017-04-13T20:27:59.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2020-06-09T03:29:29.000Z (about 6 years ago)
- Last Synced: 2025-01-07T06:32:07.860Z (over 1 year ago)
- Language: Python
- Size: 3.91 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rote - a tiny python library for writing human in the loop apps
Write apps that require human input, and are failsafe with the data
being processed.
## Example
```
from rote import Rote
import json
app = Rote()
names = [
'Jeremy',
'Erin',
'Tyrone',
'Cthulhu',
] # more realistically, import from a file
@app.setup
def setup():
try:
with open('labels.json', 'r') as infile:
data = json.load(infile)
except:
data = {
'female': [],
'male': [],
'other': [],
}
print("Classify the following names as male, female, or don't know:\n\n")
return data
@app.newdata
def newdata(existing_data):
all_names = set(x for k in existing_data for x in existing_data[k])
return [x for x in names if x not in all_names]
@app.teardown
def write(data):
with open('labels.json', 'w') as outfile:
outfile.write(json.dumps(data))
@app.foreach
def process(item, data):
print('Name: ' + item)
choice = input("male, female, or other? (m/f/o) ")
choice = (choice or 'o')[0].lower()
subset = data['other']
if choice == 'm':
subset = data['male']
elif choice == 'f':
subset = data['female']
subset.append(item)
app.run()
```
Output:
```
Classify the following names as male, female, or don't know:
Name: Jeremy
male, female, or other? (m/f/o) m
Name: Erin
male, female, or other? (m/f/o) f
Name: Tyrone
male, female, or other? (m/f/o) ^C
Quitting after teardown...
```
Partial results are handled by `teardown`, skipped by filtering in `newdata`.