humble-database
================
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
This file will become your README and also the index of your
documentation.
## Install
``` sh
pip install humble_database
```
## How to use
## Database Example
``` python
import pandas as pd
import os
from sqlalchemy import text
from urllib.request import urlretrieve
```
``` python
urlretrieve(
"http://2016.padjo.org/files/data/starterpack/census-acs-1year/acs-1-year-2015.sqlite",
filename='acs.db'
)
db = Database(drivername='sqlite',database = 'acs.db')
```
``` python
db.query_to_df("select * from sqlite_schema").head(2)
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>type</th>
<th>name</th>
<th>tbl_name</th>
<th>rootpage</th>
<th>sql</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>table</td>
<td>states</td>
<td>states</td>
<td>2</td>
<td>CREATE TABLE states (\n year INTEGER , \n ...</td>
</tr>
<tr>
<th>1</th>
<td>table</td>
<td>congressional_districts</td>
<td>congressional_districts</td>
<td>3</td>
<td>CREATE TABLE congressional_districts (\n ye...</td>
</tr>
</tbody>
</table>
</div>
``` python
db.query_to_df("""select * from states limit 5""")
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>year</th>
<th>name</th>
<th>geo_id</th>
<th>total_population</th>
<th>white</th>
<th>black</th>
<th>hispanic</th>
<th>asian</th>
<th>american_indian</th>
<th>pacific_islander</th>
<th>other_race</th>
<th>median_age</th>
<th>total_households</th>
<th>owner_occupied_homes_median_value</th>
<th>per_capita_income</th>
<th>median_household_income</th>
<th>below_poverty_line</th>
<th>foreign_born_population</th>
<th>state</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2015</td>
<td>Alabama</td>
<td>04000US01</td>
<td>4858979</td>
<td>3204076</td>
<td>1296681</td>
<td>192870</td>
<td>58918</td>
<td>19069</td>
<td>2566</td>
<td>5590</td>
<td>38.7</td>
<td>1846390</td>
<td>134100</td>
<td>44765</td>
<td>44765</td>
<td>876016</td>
<td>169972</td>
<td>01</td>
</tr>
<tr>
<th>1</th>
<td>2015</td>
<td>Alaska</td>
<td>04000US02</td>
<td>738432</td>
<td>452472</td>
<td>24739</td>
<td>51825</td>
<td>45753</td>
<td>98300</td>
<td>6341</td>
<td>2201</td>
<td>33.3</td>
<td>250185</td>
<td>259600</td>
<td>73355</td>
<td>73355</td>
<td>74532</td>
<td>58544</td>
<td>02</td>
</tr>
<tr>
<th>2</th>
<td>2015</td>
<td>Arizona</td>
<td>04000US04</td>
<td>6828065</td>
<td>3802263</td>
<td>282718</td>
<td>2098411</td>
<td>210922</td>
<td>276132</td>
<td>9963</td>
<td>6951</td>
<td>37.4</td>
<td>2463008</td>
<td>194300</td>
<td>51492</td>
<td>51492</td>
<td>1159043</td>
<td>914400</td>
<td>04</td>
</tr>
<tr>
<th>3</th>
<td>2015</td>
<td>Arkansas</td>
<td>04000US05</td>
<td>2978204</td>
<td>2174934</td>
<td>466486</td>
<td>207743</td>
<td>41932</td>
<td>18221</td>
<td>7551</td>
<td>3826</td>
<td>37.9</td>
<td>1144663</td>
<td>120700</td>
<td>41995</td>
<td>41995</td>
<td>550508</td>
<td>142841</td>
<td>05</td>
</tr>
<tr>
<th>4</th>
<td>2015</td>
<td>California</td>
<td>04000US06</td>
<td>39144818</td>
<td>14815122</td>
<td>2192844</td>
<td>15184545</td>
<td>5476958</td>
<td>135866</td>
<td>143408</td>
<td>87813</td>
<td>36.2</td>
<td>12896357</td>
<td>449100</td>
<td>64500</td>
<td>64500</td>
<td>5891678</td>
<td>10688336</td>
<td>06</td>
</tr>
</tbody>
</table>
</div>
``` python
db.query_to_records(
"select * from states limit 2",
)[0]
```
{'year': 2015, 'name': 'Alabama', 'geo_id': '04000US01', 'total_population': 4858979, 'white': 3204076, 'black': 1296681, 'hispanic': 192870, 'asian': 58918, 'american_indian': 19069, 'pacific_islander': 2566, 'other_race': 5590, 'median_age': 38.7, 'total_households': 1846390, 'owner_occupied_homes_median_value': 134100, 'per_capita_income': 44765, 'median_household_income': 44765, 'below_poverty_line': 876016, 'foreign_born_population': 169972, 'state': '01'}
## ORM Example
### SQL Alchemy Models
``` python
from pydantic import BaseModel,computed_field,field_validator,ConfigDict,Field
from sqlalchemy import ForeignKey
from sqlalchemy.orm import DeclarativeBase,Mapped, mapped_column,relationship
from typing import List
```
``` python
class Base(DeclarativeBase):
year: Mapped[int]
name: Mapped[str]
geo_id: Mapped[str]
total_population: Mapped[int]
white: Mapped[int]
black: Mapped[int]
hispanic: Mapped[int]
asian: Mapped[int]
american_indian: Mapped[int]
pacific_islander: Mapped[int]
other_race: Mapped[int]
median_age: Mapped[int]
total_households: Mapped[int]
owner_occupied_homes_median_value: Mapped[int]
per_capita_income: Mapped[int]
median_household_income: Mapped[int]
below_poverty_line: Mapped[int]
foreign_born_population: Mapped[int]
class State(Base):
__tablename__ = 'states'
state: Mapped[str] = mapped_column(primary_key=True)
total_population: Mapped[int]
places: Mapped[List['Place']] = relationship(back_populates='state_')
congressional_districts: Mapped[List['CongressionalDistrict']] = relationship(back_populates='state_')
class Place(Base):
__tablename__ = 'places'
place: Mapped[str] = mapped_column(primary_key=True)
total_population: Mapped[int]
state: Mapped[str] = mapped_column(ForeignKey("states.state"))
state_: Mapped['State'] = relationship(back_populates='places')
class CongressionalDistrict(Base):
__tablename__ = 'congressional_districts'
congressional_district: Mapped[str] = mapped_column(primary_key=True)
state: Mapped[str] = mapped_column(ForeignKey("states.state"))
state_: Mapped['State'] = relationship(back_populates='congressional_districts')
```
``` python
with db.session_scope() as session:
s = session.query(State).first()
print(s,'\n')
for place in s.places:
print(place.name,'::',place.median_household_income)
```
<__main__.State object>
Birmingham city, Alabama :: 32378
Dothan city, Alabama :: 44208
Hoover city, Alabama :: 77365
Huntsville city, Alabama :: 46769
Mobile city, Alabama :: 38678
Montgomery city, Alabama :: 41836
Tuscaloosa city, Alabama :: 44125
``` python
with db.session_scope() as session:
result = session.query(State).limit(7).all()
for state in result:
print(
state.name,
len(state.places),
len(state.congressional_districts)
)
```
Alabama 7 7
Alaska 1 1
Arizona 16 9
Arkansas 6 4
California 137 53
Colorado 16 7
Connecticut 8 5
## Pydantic Models
``` python
class ACSBase(BaseModel):
model_config = ConfigDict(from_attributes=True)
year: int = Field()
name: str = Field()
geo_id: str = Field()
total_population: Optional[int] = Field(None)
white: Optional[int] = Field(None)
black: Optional[int] = Field(None)
hispanic: Optional[int] = Field(None)
asian: Optional[int] = Field(None)
american_indian: Optional[int] = Field(None)
pacific_islander: Optional[int] = Field(None)
other_race: Optional[int] = Field(None)
median_age: float = Field()
total_households: Optional[int] = Field(None)
owner_occupied_homes_median_value: int = Field()
per_capita_income: int = Field()
median_household_income: int = Field()
below_poverty_line: Optional[int] = Field(None)
foreign_born_population: Optional[int] = Field(None)
state: int = Field()
class PlaceModel(ACSBase):
"""A Model for a record from the 'places' table"""
place: str
class CDModel(ACSBase):
"""A Model for a record from the 'congressional_districts' table"""
congressional_district: str
class StateModel(ACSBase):
"""A Model for a record from the 'states' table"""
places: List[PlaceModel]
congressional_districts: List[CDModel]
@computed_field(return_type=float,title='People per District',)
def avg_people_per_cd(self) -> float:
return sum([cd.total_population for cd in self.congressional_districts]) / len(self.congressional_districts)
```
``` python
from IPython.display import JSON
```
``` python
# mode = serialization includes computed fields
JSON(StateModel.model_json_schema(mode='serialization'))
```
<IPython.core.display.JSON object>
``` python
from humble_database.data_model import DataModel
```
``` python
ACSDataModel = DataModel[StateModel]
JSON(ACSDataModel.model_json_schema(mode='serialization'))
```
<IPython.core.display.JSON object>
``` python
with db.session_scope() as session:
orm_result = session.query(State).all()
result = ACSDataModel(data=orm_result)
result
```
<header><b>title</b>: DataModel[StateModel]
</header><header><b>description</b>: None
</header><header><b>DataFrame</b>: </header><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>year</th>
<th>name</th>
<th>geo_id</th>
<th>total_population</th>
<th>white</th>
<th>black</th>
<th>hispanic</th>
<th>asian</th>
<th>american_indian</th>
<th>pacific_islander</th>
<th>...</th>
<th>total_households</th>
<th>owner_occupied_homes_median_value</th>
<th>per_capita_income</th>
<th>median_household_income</th>
<th>below_poverty_line</th>
<th>foreign_born_population</th>
<th>state</th>
<th>places</th>
<th>congressional_districts</th>
<th>avg_people_per_cd</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2015</td>
<td>Alabama</td>
<td>04000US01</td>
<td>4858979</td>
<td>3204076</td>
<td>1296681</td>
<td>192870</td>
<td>58918</td>
<td>19069</td>
<td>2566</td>
<td>...</td>
<td>1846390</td>
<td>134100</td>
<td>44765</td>
<td>44765</td>
<td>876016.0</td>
<td>169972.0</td>
<td>1</td>
<td>[{'year': 2015, 'name': 'Birmingham city, Alab...</td>
<td>[{'year': 2015, 'name': 'Congressional Distric...</td>
<td>694139.857143</td>
</tr>
<tr>
<th>1</th>
<td>2015</td>
<td>Alaska</td>
<td>04000US02</td>
<td>738432</td>
<td>452472</td>
<td>24739</td>
<td>51825</td>
<td>45753</td>
<td>98300</td>
<td>6341</td>
<td>...</td>
<td>250185</td>
<td>259600</td>
<td>73355</td>
<td>73355</td>
<td>74532.0</td>
<td>58544.0</td>
<td>2</td>
<td>[{'year': 2015, 'name': 'Anchorage municipalit...</td>
<td>[{'year': 2015, 'name': 'Congressional Distric...</td>
<td>738432.000000</td>
</tr>
<tr>
<th>2</th>
<td>2015</td>
<td>Arizona</td>
<td>04000US04</td>
<td>6828065</td>
<td>3802263</td>
<td>282718</td>
<td>2098411</td>
<td>210922</td>
<td>276132</td>
<td>9963</td>
<td>...</td>
<td>2463008</td>
<td>194300</td>
<td>51492</td>
<td>51492</td>
<td>1159043.0</td>
<td>914400.0</td>
<td>4</td>
<td>[{'year': 2015, 'name': 'Avondale city, Arizon...</td>
<td>[{'year': 2015, 'name': 'Congressional Distric...</td>
<td>711564.777778</td>
</tr>
<tr>
<th>3</th>
<td>2015</td>
<td>Arkansas</td>
<td>04000US05</td>
<td>2978204</td>
<td>2174934</td>
<td>466486</td>
<td>207743</td>
<td>41932</td>
<td>18221</td>
<td>7551</td>
<td>...</td>
<td>1144663</td>
<td>120700</td>
<td>41995</td>
<td>41995</td>
<td>550508.0</td>
<td>142841.0</td>
<td>5</td>
<td>[{'year': 2015, 'name': 'Fayetteville city, Ar...</td>
<td>[{'year': 2015, 'name': 'Congressional Distric...</td>
<td>695398.750000</td>
</tr>
<tr>
<th>4</th>
<td>2015</td>
<td>California</td>
<td>04000US06</td>
<td>39144818</td>
<td>14815122</td>
<td>2192844</td>
<td>15184545</td>
<td>5476958</td>
<td>135866</td>
<td>143408</td>
<td>...</td>
<td>12896357</td>
<td>449100</td>
<td>64500</td>
<td>64500</td>
<td>5891678.0</td>
<td>10688336.0</td>
<td>6</td>
<td>[{'year': 2015, 'name': 'Alameda city, Califor...</td>
<td>[{'year': 2015, 'name': 'Congressional Distric...</td>
<td>735426.811321</td>
</tr>
</tbody>
</table>
<p>5 rows × 22 columns</p>
</div>
Raw data
{
"_id": null,
"home_page": "https://github.com/schlinkertc/humble-database",
"name": "humble-database",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "nbdev jupyter notebook python sqlalchemy pydantic",
"author": "Charlie Schlinkert",
"author_email": "cschlinkert@pretium.com",
"download_url": "https://files.pythonhosted.org/packages/9b/76/aef01b4edb9df6aa8fc68ef9011034a38de5cf4ee7264c0cf32d17e3e28e/humble-database-0.0.1.tar.gz",
"platform": null,
"description": "humble-database\n================\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\nThis file will become your README and also the index of your\ndocumentation.\n\n## Install\n\n``` sh\npip install humble_database\n```\n\n## How to use\n\n## Database Example\n\n``` python\nimport pandas as pd\nimport os\nfrom sqlalchemy import text\nfrom urllib.request import urlretrieve\n```\n\n``` python\nurlretrieve(\n \"http://2016.padjo.org/files/data/starterpack/census-acs-1year/acs-1-year-2015.sqlite\",\n filename='acs.db'\n)\ndb = Database(drivername='sqlite',database = 'acs.db')\n```\n\n``` python\ndb.query_to_df(\"select * from sqlite_schema\").head(2)\n```\n\n<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>type</th>\n <th>name</th>\n <th>tbl_name</th>\n <th>rootpage</th>\n <th>sql</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>table</td>\n <td>states</td>\n <td>states</td>\n <td>2</td>\n <td>CREATE TABLE states (\\n year INTEGER , \\n ...</td>\n </tr>\n <tr>\n <th>1</th>\n <td>table</td>\n <td>congressional_districts</td>\n <td>congressional_districts</td>\n <td>3</td>\n <td>CREATE TABLE congressional_districts (\\n ye...</td>\n </tr>\n </tbody>\n</table>\n</div>\n\n``` python\ndb.query_to_df(\"\"\"select * from states limit 5\"\"\")\n```\n\n<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>year</th>\n <th>name</th>\n <th>geo_id</th>\n <th>total_population</th>\n <th>white</th>\n <th>black</th>\n <th>hispanic</th>\n <th>asian</th>\n <th>american_indian</th>\n <th>pacific_islander</th>\n <th>other_race</th>\n <th>median_age</th>\n <th>total_households</th>\n <th>owner_occupied_homes_median_value</th>\n <th>per_capita_income</th>\n <th>median_household_income</th>\n <th>below_poverty_line</th>\n <th>foreign_born_population</th>\n <th>state</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2015</td>\n <td>Alabama</td>\n <td>04000US01</td>\n <td>4858979</td>\n <td>3204076</td>\n <td>1296681</td>\n <td>192870</td>\n <td>58918</td>\n <td>19069</td>\n <td>2566</td>\n <td>5590</td>\n <td>38.7</td>\n <td>1846390</td>\n <td>134100</td>\n <td>44765</td>\n <td>44765</td>\n <td>876016</td>\n <td>169972</td>\n <td>01</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2015</td>\n <td>Alaska</td>\n <td>04000US02</td>\n <td>738432</td>\n <td>452472</td>\n <td>24739</td>\n <td>51825</td>\n <td>45753</td>\n <td>98300</td>\n <td>6341</td>\n <td>2201</td>\n <td>33.3</td>\n <td>250185</td>\n <td>259600</td>\n <td>73355</td>\n <td>73355</td>\n <td>74532</td>\n <td>58544</td>\n <td>02</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2015</td>\n <td>Arizona</td>\n <td>04000US04</td>\n <td>6828065</td>\n <td>3802263</td>\n <td>282718</td>\n <td>2098411</td>\n <td>210922</td>\n <td>276132</td>\n <td>9963</td>\n <td>6951</td>\n <td>37.4</td>\n <td>2463008</td>\n <td>194300</td>\n <td>51492</td>\n <td>51492</td>\n <td>1159043</td>\n <td>914400</td>\n <td>04</td>\n </tr>\n <tr>\n <th>3</th>\n <td>2015</td>\n <td>Arkansas</td>\n <td>04000US05</td>\n <td>2978204</td>\n <td>2174934</td>\n <td>466486</td>\n <td>207743</td>\n <td>41932</td>\n <td>18221</td>\n <td>7551</td>\n <td>3826</td>\n <td>37.9</td>\n <td>1144663</td>\n <td>120700</td>\n <td>41995</td>\n <td>41995</td>\n <td>550508</td>\n <td>142841</td>\n <td>05</td>\n </tr>\n <tr>\n <th>4</th>\n <td>2015</td>\n <td>California</td>\n <td>04000US06</td>\n <td>39144818</td>\n <td>14815122</td>\n <td>2192844</td>\n <td>15184545</td>\n <td>5476958</td>\n <td>135866</td>\n <td>143408</td>\n <td>87813</td>\n <td>36.2</td>\n <td>12896357</td>\n <td>449100</td>\n <td>64500</td>\n <td>64500</td>\n <td>5891678</td>\n <td>10688336</td>\n <td>06</td>\n </tr>\n </tbody>\n</table>\n</div>\n\n``` python\ndb.query_to_records(\n \"select * from states limit 2\",\n)[0]\n```\n\n {'year': 2015, 'name': 'Alabama', 'geo_id': '04000US01', 'total_population': 4858979, 'white': 3204076, 'black': 1296681, 'hispanic': 192870, 'asian': 58918, 'american_indian': 19069, 'pacific_islander': 2566, 'other_race': 5590, 'median_age': 38.7, 'total_households': 1846390, 'owner_occupied_homes_median_value': 134100, 'per_capita_income': 44765, 'median_household_income': 44765, 'below_poverty_line': 876016, 'foreign_born_population': 169972, 'state': '01'}\n\n## ORM Example\n\n### SQL Alchemy Models\n\n``` python\nfrom pydantic import BaseModel,computed_field,field_validator,ConfigDict,Field\nfrom sqlalchemy import ForeignKey\nfrom sqlalchemy.orm import DeclarativeBase,Mapped, mapped_column,relationship\nfrom typing import List\n```\n\n``` python\nclass Base(DeclarativeBase):\n year: Mapped[int]\n name: Mapped[str]\n geo_id: Mapped[str]\n total_population: Mapped[int]\n white: Mapped[int]\n black: Mapped[int]\n hispanic: Mapped[int]\n asian: Mapped[int]\n american_indian: Mapped[int]\n pacific_islander: Mapped[int]\n other_race: Mapped[int]\n median_age: Mapped[int]\n total_households: Mapped[int]\n owner_occupied_homes_median_value: Mapped[int]\n per_capita_income: Mapped[int]\n median_household_income: Mapped[int]\n below_poverty_line: Mapped[int]\n foreign_born_population: Mapped[int]\n\nclass State(Base):\n __tablename__ = 'states'\n state: Mapped[str] = mapped_column(primary_key=True)\n total_population: Mapped[int]\n\n places: Mapped[List['Place']] = relationship(back_populates='state_')\n congressional_districts: Mapped[List['CongressionalDistrict']] = relationship(back_populates='state_')\n\nclass Place(Base):\n __tablename__ = 'places'\n place: Mapped[str] = mapped_column(primary_key=True)\n total_population: Mapped[int]\n state: Mapped[str] = mapped_column(ForeignKey(\"states.state\"))\n \n state_: Mapped['State'] = relationship(back_populates='places')\n\nclass CongressionalDistrict(Base):\n __tablename__ = 'congressional_districts'\n \n congressional_district: Mapped[str] = mapped_column(primary_key=True)\n state: Mapped[str] = mapped_column(ForeignKey(\"states.state\"))\n \n state_: Mapped['State'] = relationship(back_populates='congressional_districts')\n```\n\n``` python\nwith db.session_scope() as session:\n s = session.query(State).first()\n print(s,'\\n')\n for place in s.places:\n print(place.name,'::',place.median_household_income)\n```\n\n <__main__.State object> \n\n Birmingham city, Alabama :: 32378\n Dothan city, Alabama :: 44208\n Hoover city, Alabama :: 77365\n Huntsville city, Alabama :: 46769\n Mobile city, Alabama :: 38678\n Montgomery city, Alabama :: 41836\n Tuscaloosa city, Alabama :: 44125\n\n``` python\nwith db.session_scope() as session:\n result = session.query(State).limit(7).all()\n for state in result:\n print(\n state.name,\n len(state.places),\n len(state.congressional_districts)\n )\n```\n\n Alabama 7 7\n Alaska 1 1\n Arizona 16 9\n Arkansas 6 4\n California 137 53\n Colorado 16 7\n Connecticut 8 5\n\n## Pydantic Models\n\n``` python\nclass ACSBase(BaseModel):\n model_config = ConfigDict(from_attributes=True)\n \n year: int = Field()\n name: str = Field()\n geo_id: str = Field()\n total_population: Optional[int] = Field(None)\n white: Optional[int] = Field(None)\n black: Optional[int] = Field(None)\n hispanic: Optional[int] = Field(None)\n asian: Optional[int] = Field(None)\n american_indian: Optional[int] = Field(None)\n pacific_islander: Optional[int] = Field(None)\n other_race: Optional[int] = Field(None)\n median_age: float = Field()\n total_households: Optional[int] = Field(None)\n owner_occupied_homes_median_value: int = Field()\n per_capita_income: int = Field()\n median_household_income: int = Field()\n below_poverty_line: Optional[int] = Field(None)\n foreign_born_population: Optional[int] = Field(None) \n state: int = Field()\n\nclass PlaceModel(ACSBase):\n \"\"\"A Model for a record from the 'places' table\"\"\"\n place: str\n\nclass CDModel(ACSBase):\n \"\"\"A Model for a record from the 'congressional_districts' table\"\"\"\n congressional_district: str \n\nclass StateModel(ACSBase):\n \"\"\"A Model for a record from the 'states' table\"\"\"\n\n places: List[PlaceModel]\n congressional_districts: List[CDModel]\n \n @computed_field(return_type=float,title='People per District',)\n def avg_people_per_cd(self) -> float:\n return sum([cd.total_population for cd in self.congressional_districts]) / len(self.congressional_districts)\n```\n\n``` python\nfrom IPython.display import JSON\n```\n\n``` python\n# mode = serialization includes computed fields\nJSON(StateModel.model_json_schema(mode='serialization'))\n```\n\n <IPython.core.display.JSON object>\n\n``` python\nfrom humble_database.data_model import DataModel\n```\n\n``` python\nACSDataModel = DataModel[StateModel]\nJSON(ACSDataModel.model_json_schema(mode='serialization'))\n```\n\n <IPython.core.display.JSON object>\n\n``` python\nwith db.session_scope() as session:\n orm_result = session.query(State).all()\n result = ACSDataModel(data=orm_result)\n\nresult\n```\n\n<header><b>title</b>: DataModel[StateModel]\n</header><header><b>description</b>: None\n</header><header><b>DataFrame</b>: </header><div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>year</th>\n <th>name</th>\n <th>geo_id</th>\n <th>total_population</th>\n <th>white</th>\n <th>black</th>\n <th>hispanic</th>\n <th>asian</th>\n <th>american_indian</th>\n <th>pacific_islander</th>\n <th>...</th>\n <th>total_households</th>\n <th>owner_occupied_homes_median_value</th>\n <th>per_capita_income</th>\n <th>median_household_income</th>\n <th>below_poverty_line</th>\n <th>foreign_born_population</th>\n <th>state</th>\n <th>places</th>\n <th>congressional_districts</th>\n <th>avg_people_per_cd</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2015</td>\n <td>Alabama</td>\n <td>04000US01</td>\n <td>4858979</td>\n <td>3204076</td>\n <td>1296681</td>\n <td>192870</td>\n <td>58918</td>\n <td>19069</td>\n <td>2566</td>\n <td>...</td>\n <td>1846390</td>\n <td>134100</td>\n <td>44765</td>\n <td>44765</td>\n <td>876016.0</td>\n <td>169972.0</td>\n <td>1</td>\n <td>[{'year': 2015, 'name': 'Birmingham city, Alab...</td>\n <td>[{'year': 2015, 'name': 'Congressional Distric...</td>\n <td>694139.857143</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2015</td>\n <td>Alaska</td>\n <td>04000US02</td>\n <td>738432</td>\n <td>452472</td>\n <td>24739</td>\n <td>51825</td>\n <td>45753</td>\n <td>98300</td>\n <td>6341</td>\n <td>...</td>\n <td>250185</td>\n <td>259600</td>\n <td>73355</td>\n <td>73355</td>\n <td>74532.0</td>\n <td>58544.0</td>\n <td>2</td>\n <td>[{'year': 2015, 'name': 'Anchorage municipalit...</td>\n <td>[{'year': 2015, 'name': 'Congressional Distric...</td>\n <td>738432.000000</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2015</td>\n <td>Arizona</td>\n <td>04000US04</td>\n <td>6828065</td>\n <td>3802263</td>\n <td>282718</td>\n <td>2098411</td>\n <td>210922</td>\n <td>276132</td>\n <td>9963</td>\n <td>...</td>\n <td>2463008</td>\n <td>194300</td>\n <td>51492</td>\n <td>51492</td>\n <td>1159043.0</td>\n <td>914400.0</td>\n <td>4</td>\n <td>[{'year': 2015, 'name': 'Avondale city, Arizon...</td>\n <td>[{'year': 2015, 'name': 'Congressional Distric...</td>\n <td>711564.777778</td>\n </tr>\n <tr>\n <th>3</th>\n <td>2015</td>\n <td>Arkansas</td>\n <td>04000US05</td>\n <td>2978204</td>\n <td>2174934</td>\n <td>466486</td>\n <td>207743</td>\n <td>41932</td>\n <td>18221</td>\n <td>7551</td>\n <td>...</td>\n <td>1144663</td>\n <td>120700</td>\n <td>41995</td>\n <td>41995</td>\n <td>550508.0</td>\n <td>142841.0</td>\n <td>5</td>\n <td>[{'year': 2015, 'name': 'Fayetteville city, Ar...</td>\n <td>[{'year': 2015, 'name': 'Congressional Distric...</td>\n <td>695398.750000</td>\n </tr>\n <tr>\n <th>4</th>\n <td>2015</td>\n <td>California</td>\n <td>04000US06</td>\n <td>39144818</td>\n <td>14815122</td>\n <td>2192844</td>\n <td>15184545</td>\n <td>5476958</td>\n <td>135866</td>\n <td>143408</td>\n <td>...</td>\n <td>12896357</td>\n <td>449100</td>\n <td>64500</td>\n <td>64500</td>\n <td>5891678.0</td>\n <td>10688336.0</td>\n <td>6</td>\n <td>[{'year': 2015, 'name': 'Alameda city, Califor...</td>\n <td>[{'year': 2015, 'name': 'Congressional Distric...</td>\n <td>735426.811321</td>\n </tr>\n </tbody>\n</table>\n<p>5 rows \u00d7 22 columns</p>\n</div>\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "A simple interface for managing database connections and queries",
"version": "0.0.1",
"project_urls": {
"Homepage": "https://github.com/schlinkertc/humble-database"
},
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python",
"sqlalchemy",
"pydantic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fb3bbf38f71489dbae1cb685b65810fdc508824f7b19614358fdbbcb13b2c5ba",
"md5": "a80360e475ea8fc4d3b8cc6df217b127",
"sha256": "cd71fbf220a5c318c859685f3cea6cf50b8c24b637a822d769973b72a5b40997"
},
"downloads": -1,
"filename": "humble_database-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a80360e475ea8fc4d3b8cc6df217b127",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 13933,
"upload_time": "2023-10-02T22:52:53",
"upload_time_iso_8601": "2023-10-02T22:52:53.612230Z",
"url": "https://files.pythonhosted.org/packages/fb/3b/bf38f71489dbae1cb685b65810fdc508824f7b19614358fdbbcb13b2c5ba/humble_database-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9b76aef01b4edb9df6aa8fc68ef9011034a38de5cf4ee7264c0cf32d17e3e28e",
"md5": "da04a230f1bd5114782d125a3b96ddd9",
"sha256": "40ffe67d5ea569a8531e9cad344728bf527ad6dbc2f4ffcf1376e0c04091de79"
},
"downloads": -1,
"filename": "humble-database-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "da04a230f1bd5114782d125a3b96ddd9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 16927,
"upload_time": "2023-10-02T22:52:55",
"upload_time_iso_8601": "2023-10-02T22:52:55.263848Z",
"url": "https://files.pythonhosted.org/packages/9b/76/aef01b4edb9df6aa8fc68ef9011034a38de5cf4ee7264c0cf32d17e3e28e/humble-database-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-02 22:52:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "schlinkertc",
"github_project": "humble-database",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "humble-database"
}