Created
October 25, 2022 04:24
-
-
Save iisaka51/17574aed2e29e3de2cce12656828188b to your computer and use it in GitHub Desktop.
example of class define
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [8]: # %load users_class.py | |
...: class User: | |
...: | |
...: def __init__(self, *nargs, **kwargs): | |
...: self.data = dict(*nargs, **kwargs) | |
...: self.id: int = self.data['id'] | |
...: self.name: str = self.data['name'] | |
...: self.age: int = self.data['age'] | |
...: self.belongs: str = self.data['belongs'] | |
...: | |
...: def __repr__(self): | |
...: return(f'User(id={self.id}, ' | |
...: f'name="{self.name}", ' | |
...: f'age={self.age}, ' | |
...: f'belongs="{self.belongs}")' ) | |
...: | |
...: | |
In [9]: users = io.read_contents('sqlite:///users.sqlite#users',row_type=User) | |
In [10]: users | |
Out[10]: | |
[User(id=1, name="David Coverdale", age=71, belongs="Whitesnake"), | |
User(id=2, name="Neal Schon ", age=68, belongs="Journey"), | |
User(id=3, name="Tom Scholz", age=75, belongs="Boston"), | |
User(id=4, name="David Gilmour", age=75, belongs="Pink Floyd"), | |
User(id=5, name="Ann Wilson", age=71, belongs="Heart"), | |
User(id=6, name="Nacy Wilson", age=67, belongs="Heart")] | |
In [11]: users[0].name | |
Out[11]: 'David Coverdale' | |
In [12]: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment