1
from fastapi import FastAPI
import person

app = FastAPI()

local_database = []

@app.get('/')
def index():
    """Return the todo"""
    return "Use: URL/docs to access API documentation"

@app.get('/people')
def get_people():
    if not local_database:    
        return 'Database empty'
    
    people_info = [person for person in local_database]
    return people_info

@app.post('/people')
def create_person(person: person.Person):
    local_database.append(person)
    return 'Added new person to database'

@app.get("/people/{person_id}")
def read_all_infos(person_id: int):
    try:
        return local_database[person_id]
    except IndexError as e:
        return repr(e)
        # This is much better than any str()-like solutions, because it actually includes the type of exception.
        
@app.get("/people/information/salary")
def read_salary(name: str, gender: str, age: int, password: str):    
    
    for pers in local_database:
        if (    pers.name == name 
            and pers.gender == gender 
            and password == "123456"
            ):
            return pers.salary
    
    return 'Could not find your person'

@app.get("/people/information/gender")
def read_gender(name: str):
    print(name)
    for pers in local_database:
        if pers.name == name:
            return pers

    return 'Could not find your person'

# maybe needed
def populate_database():
    dummy_person1 = person.Person(salary = 55000, gender="male", name ="Jack", age = 22)
    dummy_person2 = person.Person(salary = 120000, gender="female", name ="Rose", age = 42)
    local_database.append(dummy_person1)    
    local_database.append(dummy_person2)    

if __name__ == '__main__':
    populate_database()
    

My goal is to use FastAPI to communicate with a local in-memory database for testing purposes. However, in my main I'd like to run populate_database() to add some instances of the class Person to the list.

Then I want to use a HTTP request GET to receive the local_data. Unfortunately the local_database list is not populated. I would expect 2 instances of Person inside the database. Any idea why the local_database list is not populated?

1 Answer 1

1

If the module is run from standard input, a script, or from an interactive prompt its __name__ is being set as "__main__". 1

Effectively, populate_database is only executed when you're running it via python <filename> but you're probably running it using uvicorn or another runner which executes it as a normal module.

Try moving populate_database call outside the if __name__ = "__main__" guard.

Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely right. Thanks for the fast reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.