Continue with the tutorial.

communication
junos 2020-12-24 16:57:34 +01:00
parent bc27c71041
commit 6dcb0daae7
1 changed files with 326 additions and 13 deletions

View File

@ -90,15 +90,15 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"2020-12-24 16:40:37,230 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1\n", "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:40:37,236 INFO sqlalchemy.engine.base.Engine ()\n", "2020-12-24 16:56:29,386 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:40:37,239 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 SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1\n",
"2020-12-24 16:40:37,240 INFO sqlalchemy.engine.base.Engine ()\n", "2020-12-24 16:56:29,388 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:40:37,242 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info(\"users\")\n", "2020-12-24 16:56:29,390 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info(\"users\")\n",
"2020-12-24 16:40:37,242 INFO sqlalchemy.engine.base.Engine ()\n", "2020-12-24 16:56:29,391 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:40:37,244 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info(\"users\")\n", "2020-12-24 16:56:29,392 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info(\"users\")\n",
"2020-12-24 16:40:37,245 INFO sqlalchemy.engine.base.Engine ()\n", "2020-12-24 16:56:29,394 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:40:37,246 INFO sqlalchemy.engine.base.Engine \n", "2020-12-24 16:56:29,395 INFO sqlalchemy.engine.base.Engine \n",
"CREATE TABLE users (\n", "CREATE TABLE users (\n",
"\tid INTEGER NOT NULL, \n", "\tid INTEGER NOT NULL, \n",
"\tname VARCHAR, \n", "\tname VARCHAR, \n",
@ -108,8 +108,8 @@
")\n", ")\n",
"\n", "\n",
"\n", "\n",
"2020-12-24 16:40:37,247 INFO sqlalchemy.engine.base.Engine ()\n", "2020-12-24 16:56:29,396 INFO sqlalchemy.engine.base.Engine ()\n",
"2020-12-24 16:40:37,248 INFO sqlalchemy.engine.base.Engine COMMIT\n" "2020-12-24 16:56:29,400 INFO sqlalchemy.engine.base.Engine COMMIT\n"
] ]
} }
], ],
@ -119,7 +119,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -141,7 +141,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -149,6 +149,319 @@
"Session = sessionmaker(bind=engine)\n", "Session = sessionmaker(bind=engine)\n",
"session = Session()" "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": { "metadata": {