Hacker News new | past | comments | ask | show | jobs | submit login

You don't need `_make()` with dataclasses, and you get `asdict()` as a stand-alone function so it doesn't clash with each class's namespace. Here's what your code might look like with them:

    import sqlite3
    from dataclasses import asdict, dataclass
    
    @dataclass
    class EmployeeRecord:
        name: str
        age: int
        title: str
        department: str
        paygrade: str
    
    conn = sqlite3.connect("/companydata")
    cursor = conn.cursor()
    cursor.execute("SELECT name, age, title, department, paygrade FROM employees")
    for emp in (EmployeeRecord(*row) for row in cursor.fetchall()):
        print(emp.name, emp.title)
        print(asdict(emp))



For that, you might as well conn.row_factory = sqlite3.Row


That would be a better option, as long as the goal isn’t to demonstrate how namedtuples or dataclasses work.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: