News for hackers. Stuff that matters.
Re:Lesser known parts of Python standard library – Trickster... by kstrauser on Thursday September 05, 2024 @02:27PM (#41459219)
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))
Re:Lesser known parts of Python standard library – Trickster... by ttyprintk on Friday September 06, 2024 @07:10AM (#41465015)
For that, you might as well conn.row_factory = sqlite3.Row
Re:Lesser known parts of Python standard library – Trickster... by kstrauser on Friday September 06, 2024 @08:58AM (#41465754)
That would be a better option, as long as the goal isn’t to demonstrate how namedtuples or dataclasses work.