stress_at_work_analysis/config/ORMtutorial.ipynb

489 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.3.21'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sqlalchemy\n",
"sqlalchemy.__version__ "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy import create_engine\n",
"engine = create_engine('sqlite:///:memory:', echo=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy.ext.declarative import declarative_base\n",
"Base = declarative_base()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy import Column, Integer, String\n",
"class User(Base):\n",
" __tablename__ = 'users'\n",
"\n",
" id = Column(Integer, primary_key=True)\n",
" name = Column(String)\n",
" fullname = Column(String)\n",
" nickname = Column(String)\n",
"\n",
" def __repr__(self):\n",
" return \"<User(name='%s', fullname='%s', nickname='%s')>\" % (\n",
" self.name, self.fullname, self.nickname)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Table('users', MetaData(bind=None), Column('id', Integer(), table=<users>, primary_key=True, nullable=False), Column('name', String(), table=<users>), Column('fullname', String(), table=<users>), Column('nickname', String(), table=<users>), schema=None)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"User.__table__ "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,385 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1\n",
"2020-12-24 16:56:29,386 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:56:29,388 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1\n",
"2020-12-24 16:56:29,388 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:56:29,390 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info(\"users\")\n",
"2020-12-24 16:56:29,391 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:56:29,392 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info(\"users\")\n",
"2020-12-24 16:56:29,394 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:56:29,395 INFO sqlalchemy.engine.base.Engine \n",
"CREATE TABLE users (\n",
"\tid INTEGER NOT NULL, \n",
"\tname VARCHAR, \n",
"\tfullname VARCHAR, \n",
"\tnickname VARCHAR, \n",
"\tPRIMARY KEY (id)\n",
")\n",
"\n",
"\n",
"2020-12-24 16:56:29,396 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:56:29,400 INFO sqlalchemy.engine.base.Engine COMMIT\n"
]
}
],
"source": [
"Base.metadata.create_all(engine)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ed\n",
"edsnickname\n",
"None\n"
]
}
],
"source": [
"ed_user = User(name='ed', fullname='Ed Jones', nickname='edsnickname')\n",
"print(ed_user.name)\n",
"print(ed_user.nickname)\n",
"print(str(ed_user.id))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy.orm import sessionmaker\n",
"Session = sessionmaker(bind=engine)\n",
"session = Session()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Adding and Updating Objects"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"ed_user = User(name='ed', fullname='Ed Jones', nickname='edsnickname')\n",
"session.add(ed_user)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,442 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
"2020-12-24 16:56:29,449 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, nickname) VALUES (?, ?, ?)\n",
"2020-12-24 16:56:29,450 INFO sqlalchemy.engine.base.Engine ('ed', 'Ed Jones', 'edsnickname')\n",
"2020-12-24 16:56:29,452 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.nickname AS users_nickname \n",
"FROM users \n",
"WHERE users.name = ?\n",
" LIMIT ? OFFSET ?\n",
"2020-12-24 16:56:29,455 INFO sqlalchemy.engine.base.Engine ('ed', 1, 0)\n"
]
},
{
"data": {
"text/plain": [
"<User(name='ed', fullname='Ed Jones', nickname='edsnickname')>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"our_user = session.query(User).filter_by(name='ed').first() \n",
"our_user"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ed_user is our_user"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"session.add_all([\n",
" User(name='wendy', fullname='Wendy Williams', nickname='windy'),\n",
" User(name='mary', fullname='Mary Contrary', nickname='mary'),\n",
" User(name='fred', fullname='Fred Flintstone', nickname='freddy')])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"ed_user.nickname = 'eddie'"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"IdentitySet([<User(name='ed', fullname='Ed Jones', nickname='eddie')>])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"session.dirty"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"IdentitySet([<User(name='wendy', fullname='Wendy Williams', nickname='windy')>, <User(name='mary', fullname='Mary Contrary', nickname='mary')>, <User(name='fred', fullname='Fred Flintstone', nickname='freddy')>])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"session.new "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,513 INFO sqlalchemy.engine.base.Engine UPDATE users SET nickname=? WHERE users.id = ?\n",
"2020-12-24 16:56:29,514 INFO sqlalchemy.engine.base.Engine ('eddie', 1)\n",
"2020-12-24 16:56:29,515 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, nickname) VALUES (?, ?, ?)\n",
"2020-12-24 16:56:29,520 INFO sqlalchemy.engine.base.Engine ('wendy', 'Wendy Williams', 'windy')\n",
"2020-12-24 16:56:29,521 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, nickname) VALUES (?, ?, ?)\n",
"2020-12-24 16:56:29,521 INFO sqlalchemy.engine.base.Engine ('mary', 'Mary Contrary', 'mary')\n",
"2020-12-24 16:56:29,522 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, nickname) VALUES (?, ?, ?)\n",
"2020-12-24 16:56:29,523 INFO sqlalchemy.engine.base.Engine ('fred', 'Fred Flintstone', 'freddy')\n",
"2020-12-24 16:56:29,525 INFO sqlalchemy.engine.base.Engine COMMIT\n"
]
}
],
"source": [
"session.commit()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,534 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
"2020-12-24 16:56:29,538 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.nickname AS users_nickname \n",
"FROM users \n",
"WHERE users.id = ?\n",
"2020-12-24 16:56:29,539 INFO sqlalchemy.engine.base.Engine (1,)\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ed_user.id "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rolling back"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"ed_user.name = 'Edwardo'"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"fake_user = User(name='fakeuser', fullname='Invalid', nickname='12345')\n",
"session.add(fake_user)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,569 INFO sqlalchemy.engine.base.Engine UPDATE users SET name=? WHERE users.id = ?\n",
"2020-12-24 16:56:29,571 INFO sqlalchemy.engine.base.Engine ('Edwardo', 1)\n",
"2020-12-24 16:56:29,573 INFO sqlalchemy.engine.base.Engine INSERT INTO users (name, fullname, nickname) VALUES (?, ?, ?)\n",
"2020-12-24 16:56:29,573 INFO sqlalchemy.engine.base.Engine ('fakeuser', 'Invalid', '12345')\n",
"2020-12-24 16:56:29,576 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.nickname AS users_nickname \n",
"FROM users \n",
"WHERE users.name IN (?, ?)\n",
"2020-12-24 16:56:29,580 INFO sqlalchemy.engine.base.Engine ('Edwardo', 'fakeuser')\n"
]
},
{
"data": {
"text/plain": [
"[<User(name='Edwardo', fullname='Ed Jones', nickname='eddie')>,\n",
" <User(name='fakeuser', fullname='Invalid', nickname='12345')>]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"session.query(User).filter(User.name.in_(['Edwardo', 'fakeuser'])).all()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,591 INFO sqlalchemy.engine.base.Engine ROLLBACK\n",
"2020-12-24 16:56:29,594 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
"2020-12-24 16:56:29,596 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.nickname AS users_nickname \n",
"FROM users \n",
"WHERE users.id = ?\n",
"2020-12-24 16:56:29,597 INFO sqlalchemy.engine.base.Engine (1,)\n",
"ed\n",
"False\n"
]
}
],
"source": [
"session.rollback()\n",
"print(ed_user.name)\n",
"print(fake_user in session)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-12-24 16:56:29,607 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.nickname AS users_nickname \n",
"FROM users \n",
"WHERE users.name IN (?, ?)\n",
"2020-12-24 16:56:29,611 INFO sqlalchemy.engine.base.Engine ('ed', 'fakeuser')\n"
]
},
{
"data": {
"text/plain": [
"[<User(name='ed', fullname='Ed Jones', nickname='eddie')>]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"session.query(User).filter(User.name.in_(['ed', 'fakeuser'])).all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}