Guest User

Untitled

a guest
Dec 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "metadata": {
  5. "trusted": true
  6. },
  7. "cell_type": "code",
  8. "source": "# Kod 1\nimport sqlite3\nimport random\nimport time\nimport pandas as pd\nimport numpy as np",
  9. "execution_count": 1,
  10. "outputs": []
  11. },
  12. {
  13. "metadata": {
  14. "trusted": true
  15. },
  16. "cell_type": "code",
  17. "source": "# Kod 2\nconn = sqlite3.connect('../output/example.db')\nc = conn.cursor()",
  18. "execution_count": 2,
  19. "outputs": []
  20. },
  21. {
  22. "metadata": {
  23. "trusted": true
  24. },
  25. "cell_type": "code",
  26. "source": "# Kod 3\nc.execute('''CREATE TABLE IF NOT EXISTS readings (date text, PM25 real, PM10 real)''')",
  27. "execution_count": 3,
  28. "outputs": [
  29. {
  30. "output_type": "execute_result",
  31. "execution_count": 3,
  32. "data": {
  33. "text/plain": "<sqlite3.Cursor at 0x7f78f7ba1ab0>"
  34. },
  35. "metadata": {}
  36. }
  37. ]
  38. },
  39. {
  40. "metadata": {
  41. "trusted": true
  42. },
  43. "cell_type": "code",
  44. "source": "# Kod 4\ncounter = 0\nwhile True:\n readings = {}\n readings[\"PM25\"] = round(random.uniform(0, 121),1)\n readings[\"PM10\"] = round(random.uniform(0, 201),1)\n readings[\"datetime\"] = time.strftime('%Y-%m-%d %H:%M:%S')\n \n c.execute(\"INSERT INTO readings VALUES (?,?,?)\", (readings[\"datetime\"], readings[\"PM25\"], readings[\"PM10\"]))\n \n conn.commit()\n \n counter += 1\n if counter == 10:\n break",
  45. "execution_count": 4,
  46. "outputs": []
  47. },
  48. {
  49. "metadata": {
  50. "trusted": true
  51. },
  52. "cell_type": "code",
  53. "source": "# Kod 5\nfor row in c.execute('SELECT * FROM readings ORDER BY PM25 DESC LIMIT 10'):\n print(row)",
  54. "execution_count": 5,
  55. "outputs": [
  56. {
  57. "output_type": "stream",
  58. "text": "('2018-12-16 11:18:08', 120.2, 53.4)\n('2018-12-16 11:18:07', 117.2, 14.6)\n('2018-12-16 09:22:54', 116.6, 186.9)\n('2018-12-16 12:25:49', 109.8, 189.4)\n('2018-12-16 09:22:54', 108.8, 77.6)\n('2018-12-16 11:18:07', 100.5, 160.5)\n('2018-12-16 12:25:50', 99.5, 10.6)\n('2018-12-16 09:24:15', 92.9, 27.4)\n('2018-12-16 09:22:53', 87.1, 180.8)\n('2018-12-16 11:18:08', 85.6, 40.4)\n",
  59. "name": "stdout"
  60. }
  61. ]
  62. },
  63. {
  64. "metadata": {
  65. "trusted": true
  66. },
  67. "cell_type": "code",
  68. "source": "# Kod 6\ndf = pd.read_sql_query(\"select * from readings\", conn)",
  69. "execution_count": 6,
  70. "outputs": []
  71. },
  72. {
  73. "metadata": {
  74. "trusted": true
  75. },
  76. "cell_type": "code",
  77. "source": "# Kod 7\ndf[\"additional\"] = np.where(df[\"PM10\"] > 100, \"Alert!\", None)\ndf.to_sql(\"readings_modified\", conn, if_exists=\"replace\", index=False)\nfor row in c.execute('SELECT * FROM readings_modified ORDER BY PM25 DESC LIMIT 10'):\n print(row)",
  78. "execution_count": 7,
  79. "outputs": [
  80. {
  81. "output_type": "stream",
  82. "text": "('2018-12-16 11:18:08', 120.2, 53.4, None)\n('2018-12-16 11:18:07', 117.2, 14.6, None)\n('2018-12-16 09:22:54', 116.6, 186.9, 'Alert!')\n('2018-12-16 12:25:49', 109.8, 189.4, 'Alert!')\n('2018-12-16 09:22:54', 108.8, 77.6, None)\n('2018-12-16 11:18:07', 100.5, 160.5, 'Alert!')\n('2018-12-16 12:25:50', 99.5, 10.6, None)\n('2018-12-16 09:24:15', 92.9, 27.4, None)\n('2018-12-16 09:22:53', 87.1, 180.8, 'Alert!')\n('2018-12-16 11:18:08', 85.6, 40.4, None)\n",
  83. "name": "stdout"
  84. }
  85. ]
  86. },
  87. {
  88. "metadata": {
  89. "trusted": true
  90. },
  91. "cell_type": "code",
  92. "source": "# Kod 8\nconn.close()",
  93. "execution_count": 8,
  94. "outputs": []
  95. }
  96. ],
  97. "metadata": {
  98. "kernelspec": {
  99. "name": "conda-env-jakbadacdane.pl-py",
  100. "display_name": "Python [conda env:jakbadacdane.pl]",
  101. "language": "python"
  102. },
  103. "language_info": {
  104. "name": "python",
  105. "version": "3.6.7",
  106. "mimetype": "text/x-python",
  107. "codemirror_mode": {
  108. "name": "ipython",
  109. "version": 3
  110. },
  111. "pygments_lexer": "ipython3",
  112. "nbconvert_exporter": "python",
  113. "file_extension": ".py"
  114. },
  115. "gist": {
  116. "id": "",
  117. "data": {
  118. "description": " SQLite i Python – czy warto?",
  119. "public": true
  120. }
  121. }
  122. },
  123. "nbformat": 4,
  124. "nbformat_minor": 2
  125. }
Add Comment
Please, Sign In to add comment