From a73f883613df4a9e7fa6b96af3c40c5d9072d8cd Mon Sep 17 00:00:00 2001 From: eyrei123 Date: Tue, 24 Sep 2024 15:23:32 +0100 Subject: [PATCH 1/8] Initial Commit --- numpy_examples/README.md | 3 + numpy_examples/cashed_checks.csv | 8 + numpy_examples/check_list_duplicates.csv | 12 + numpy_examples/ex1a.csv | 4 + numpy_examples/ex1b.csv | 1 + numpy_examples/ex1c.csv | 3 + numpy_examples/file1.csv | 2 + numpy_examples/file2.csv | 2 + numpy_examples/file3.csv | 2 + numpy_examples/issued_checks.csv | 11 + numpy_examples/london_temperatures.csv | 12 + numpy_examples/long_file.csv | 3 + numpy_examples/new_york_temperatures.csv | 12 + numpy_examples/passengers.csv | 22 + numpy_examples/passports.csv | 22 + numpy_examples/portfolio.csv | 7 + numpy_examples/rome_temperatures.csv | 12 + numpy_examples/share_prices-1.csv | 7 + numpy_examples/share_prices-2.csv | 7 + numpy_examples/share_prices-3.csv | 7 + numpy_examples/share_prices-4.csv | 7 + numpy_examples/share_prices-5.csv | 7 + numpy_examples/short_file.csv | 1 + numpy_examples/tutorial_code.ipynb | 576 +++++++++++++++++++++++ 24 files changed, 750 insertions(+) create mode 100644 numpy_examples/README.md create mode 100644 numpy_examples/cashed_checks.csv create mode 100644 numpy_examples/check_list_duplicates.csv create mode 100644 numpy_examples/ex1a.csv create mode 100644 numpy_examples/ex1b.csv create mode 100644 numpy_examples/ex1c.csv create mode 100644 numpy_examples/file1.csv create mode 100644 numpy_examples/file2.csv create mode 100644 numpy_examples/file3.csv create mode 100644 numpy_examples/issued_checks.csv create mode 100644 numpy_examples/london_temperatures.csv create mode 100644 numpy_examples/long_file.csv create mode 100644 numpy_examples/new_york_temperatures.csv create mode 100644 numpy_examples/passengers.csv create mode 100644 numpy_examples/passports.csv create mode 100644 numpy_examples/portfolio.csv create mode 100644 numpy_examples/rome_temperatures.csv create mode 100644 numpy_examples/share_prices-1.csv create mode 100644 numpy_examples/share_prices-2.csv create mode 100644 numpy_examples/share_prices-3.csv create mode 100644 numpy_examples/share_prices-4.csv create mode 100644 numpy_examples/share_prices-5.csv create mode 100644 numpy_examples/short_file.csv create mode 100644 numpy_examples/tutorial_code.ipynb diff --git a/numpy_examples/README.md b/numpy_examples/README.md new file mode 100644 index 0000000000..f6ed18d653 --- /dev/null +++ b/numpy_examples/README.md @@ -0,0 +1,3 @@ +These files are used in the tutorial [NumPy Practical Examples: Useful Techniques](https://realpython.com/numpy-example-useful-practical-techniques/). + +The various csv files contain data that is uses to create and populate various NumPy arrays in both the main tutorial and in the various embedded exercises. The tutorial_code.ipynb is a Jupyter Notebook containing the code used in the tutorials. \ No newline at end of file diff --git a/numpy_examples/cashed_checks.csv b/numpy_examples/cashed_checks.csv new file mode 100644 index 0000000000..577981e719 --- /dev/null +++ b/numpy_examples/cashed_checks.csv @@ -0,0 +1,8 @@ +Check_ID,Amount,Date_Cashed +1341,150.00,2024-04-12 +1342,175.00,2024-04-16 +1343,30.00,2024-04-12 +1345,65.00,2024-04-12 +1346,430.00,2024-04-08 +1349,500.00,2024-04-08 +1350,220.00,2024-04-15 \ No newline at end of file diff --git a/numpy_examples/check_list_duplicates.csv b/numpy_examples/check_list_duplicates.csv new file mode 100644 index 0000000000..02a3ddbf9d --- /dev/null +++ b/numpy_examples/check_list_duplicates.csv @@ -0,0 +1,12 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy_examples/ex1a.csv b/numpy_examples/ex1a.csv new file mode 100644 index 0000000000..e97fc94b35 --- /dev/null +++ b/numpy_examples/ex1a.csv @@ -0,0 +1,4 @@ +5,10,15,20 +25,30,35,40 +45,50,55,60 +65,70,75,80 \ No newline at end of file diff --git a/numpy_examples/ex1b.csv b/numpy_examples/ex1b.csv new file mode 100644 index 0000000000..37ffd0470b --- /dev/null +++ b/numpy_examples/ex1b.csv @@ -0,0 +1 @@ +1,3,5 \ No newline at end of file diff --git a/numpy_examples/ex1c.csv b/numpy_examples/ex1c.csv new file mode 100644 index 0000000000..cfa0b54952 --- /dev/null +++ b/numpy_examples/ex1c.csv @@ -0,0 +1,3 @@ +2 +4 +6 \ No newline at end of file diff --git a/numpy_examples/file1.csv b/numpy_examples/file1.csv new file mode 100644 index 0000000000..8dbfc4399b --- /dev/null +++ b/numpy_examples/file1.csv @@ -0,0 +1,2 @@ +1.1,1.2,1.3 +1.4,1.5,1.6 \ No newline at end of file diff --git a/numpy_examples/file2.csv b/numpy_examples/file2.csv new file mode 100644 index 0000000000..dabb7f3a8a --- /dev/null +++ b/numpy_examples/file2.csv @@ -0,0 +1,2 @@ +2.1,2.2,2.3 +2.4,2.5,2.6 \ No newline at end of file diff --git a/numpy_examples/file3.csv b/numpy_examples/file3.csv new file mode 100644 index 0000000000..497c31700f --- /dev/null +++ b/numpy_examples/file3.csv @@ -0,0 +1,2 @@ +3.1,3.2,3.3 +3.4,3.5,3.6 \ No newline at end of file diff --git a/numpy_examples/issued_checks.csv b/numpy_examples/issued_checks.csv new file mode 100644 index 0000000000..79ebe66d15 --- /dev/null +++ b/numpy_examples/issued_checks.csv @@ -0,0 +1,11 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy_examples/london_temperatures.csv b/numpy_examples/london_temperatures.csv new file mode 100644 index 0000000000..ce1d205e15 --- /dev/null +++ b/numpy_examples/london_temperatures.csv @@ -0,0 +1,12 @@ +Jan,5 +Feb,7 +Mar,9 +Apr,11 +May,14 +Jun,16 +Jul,19 +Aug,19 +Sep,17 +Oct,13 +Nov,10 +Dec,7 \ No newline at end of file diff --git a/numpy_examples/long_file.csv b/numpy_examples/long_file.csv new file mode 100644 index 0000000000..0e63b07cb6 --- /dev/null +++ b/numpy_examples/long_file.csv @@ -0,0 +1,3 @@ +4.1,4.2,4.3 +4.4,4.5,4.6 +4.7,4.8,4.9 \ No newline at end of file diff --git a/numpy_examples/new_york_temperatures.csv b/numpy_examples/new_york_temperatures.csv new file mode 100644 index 0000000000..4ef0f4d9fd --- /dev/null +++ b/numpy_examples/new_york_temperatures.csv @@ -0,0 +1,12 @@ +Jan,2 +Feb,2 +Mar,4 +Apr,11 +May,16 +Jun,22 +Jul,25 +Aug,24 +Sep,20 +Oct,14 +Nov,12 +Dec,9 \ No newline at end of file diff --git a/numpy_examples/passengers.csv b/numpy_examples/passengers.csv new file mode 100644 index 0000000000..d5b924e62b --- /dev/null +++ b/numpy_examples/passengers.csv @@ -0,0 +1,22 @@ +passenger_no,first_name,last_name +1,Olivia,Smith +2,Amelia,Jones +3,Isla,Williams +4,Ava,Taylor +5,Ivy,Brown +6,Freya,Davies +7,Lily,Evans +8,Florence,Wilson +9,Mia,Thomas +10,Willow,Johnson +11,Noah,Roberts +12,Oliver,Robinson +13,George,Thompson +14,Arthur,Wright +14,Arthur,Wright +15,Muhammad,Walker +16,Leo,White +17,Harry,Edwards +18,Oscar,Hughes +19,Archie,Green +20,Henry,Hall \ No newline at end of file diff --git a/numpy_examples/passports.csv b/numpy_examples/passports.csv new file mode 100644 index 0000000000..729d3351f9 --- /dev/null +++ b/numpy_examples/passports.csv @@ -0,0 +1,22 @@ +passport_no,passenger_no,nationality +123656489,1,British +373456789,2,British +493456399,3,American +375456228,4,American +457345975,5,Austrian +345957363,6,Norewegian +567546577,7,French +453667456,8,German +456785778,9,Danish +456785778,9,Danish +679857668,10,Dutch +456657867,11,Dutch +467775488,12,French +884393487,13,Danish +122647657,15,Dutch +534677673,16,British +786737688,17,American +654672278,18,Spanish +683637288,19,Norwegian +768357788,20,British +768357788,21,American \ No newline at end of file diff --git a/numpy_examples/portfolio.csv b/numpy_examples/portfolio.csv new file mode 100644 index 0000000000..5e1e766100 --- /dev/null +++ b/numpy_examples/portfolio.csv @@ -0,0 +1,7 @@ +Company,Sector +Company_A,technology +Company_B,finance +Company_C,healthcare +Company_D,technology +Company_E,finance +Company_F,healthcare \ No newline at end of file diff --git a/numpy_examples/rome_temperatures.csv b/numpy_examples/rome_temperatures.csv new file mode 100644 index 0000000000..d70c14343a --- /dev/null +++ b/numpy_examples/rome_temperatures.csv @@ -0,0 +1,12 @@ +Jan,8 +Feb,9 +Mar,12 +Apr,14 +May,21 +Jun,23 +Jul,26 +Aug,24 +Sep,22 +Oct,18 +Nov,13 +Dec,10 \ No newline at end of file diff --git a/numpy_examples/share_prices-1.csv b/numpy_examples/share_prices-1.csv new file mode 100644 index 0000000000..9e6342db35 --- /dev/null +++ b/numpy_examples/share_prices-1.csv @@ -0,0 +1,7 @@ +Company,mon +Company_A,100.5 +Company_B,200.1 +Company_C,50.3 +Company_D,110.5 +Company_E,200.1 +Company_F,55.3 \ No newline at end of file diff --git a/numpy_examples/share_prices-2.csv b/numpy_examples/share_prices-2.csv new file mode 100644 index 0000000000..b29208eade --- /dev/null +++ b/numpy_examples/share_prices-2.csv @@ -0,0 +1,7 @@ +Company,tue +Company_A,101.2 +Company_B,199.8 +Company_C,50.5 +Company_D,101.2 +Company_E,200.8 +Company_F,50.5 \ No newline at end of file diff --git a/numpy_examples/share_prices-3.csv b/numpy_examples/share_prices-3.csv new file mode 100644 index 0000000000..7cce3ec69a --- /dev/null +++ b/numpy_examples/share_prices-3.csv @@ -0,0 +1,7 @@ +Company,Wed +Company_A,102 +Company_B,200.5 +Company_C,51.0 +Company_D,102 +Company_E,200.5 +Company_F,53.0 \ No newline at end of file diff --git a/numpy_examples/share_prices-4.csv b/numpy_examples/share_prices-4.csv new file mode 100644 index 0000000000..c7d58df53f --- /dev/null +++ b/numpy_examples/share_prices-4.csv @@ -0,0 +1,7 @@ +Company,Thu +Company_A,101.8 +Company_B,201.0 +Company_C,50.8 +Company_D,111.8 +Company_E,211.0 +Company_F,50.8 \ No newline at end of file diff --git a/numpy_examples/share_prices-5.csv b/numpy_examples/share_prices-5.csv new file mode 100644 index 0000000000..5f6d2d7b9f --- /dev/null +++ b/numpy_examples/share_prices-5.csv @@ -0,0 +1,7 @@ +Company,Fri +Company_A,112.5 +Company_B,200.8 +Company_C,51.2 +Company_D,97.5 +Company_E,200.8 +Company_F,52.2 \ No newline at end of file diff --git a/numpy_examples/short_file.csv b/numpy_examples/short_file.csv new file mode 100644 index 0000000000..30c26e4dec --- /dev/null +++ b/numpy_examples/short_file.csv @@ -0,0 +1 @@ +4.1,4.2,4.3 diff --git a/numpy_examples/tutorial_code.ipynb b/numpy_examples/tutorial_code.ipynb new file mode 100644 index 0000000000..453c501358 --- /dev/null +++ b/numpy_examples/tutorial_code.ipynb @@ -0,0 +1,576 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c33e52ac-a587-41c3-b487-6a1533043a54", + "metadata": {}, + "source": [ + "# **Code Used In NumPy Practical Examples: Useful Techniques**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29aff3d0-0427-4dd8-b14f-bc7ce9002a76", + "metadata": {}, + "outputs": [], + "source": [ + "!python -m pip install numpy\n", + "!python -m pip install matplotlib\n", + "!python -m pip install pathlib\n", + "!python -m pip install jupyterlab" + ] + }, + { + "cell_type": "markdown", + "id": "8b740161-a80e-4d37-8c26-2dca52c68dce", + "metadata": {}, + "source": [ + "**Example 1: Creating Multi-Dimensional Arrays From Files**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e64a11c1-a0b5-49f8-a0a6-dfe0adf674f4", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from pathlib import Path\n", + "\n", + "array = np.zeros(\n", + " (\n", + " 3,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "print(array.shape)\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8de90c08-9bdc-42f3-ad4e-d7a34fca8215", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array[3, 0] = np.loadtxt(\"short_file.csv\", delimiter=\",\")\n", + "\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63775265-deb1-4cca-bd7c-547b3d49f5ba", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array = np.insert(arr=array, obj=2, values=0, axis=1)\n", + "\n", + "array[3] = np.loadtxt(\"long_file.csv\", delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "\n", + "array" + ] + }, + { + "cell_type": "markdown", + "id": "fd4544ac-53bc-449c-88a2-f2496bba8f55", + "metadata": {}, + "source": [ + "**Example 2: Reconciling Data Using Structured NumPy Arrays**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6d760bcd-1be5-4396-8eb8-d729768909d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['At The Back', 'Fast Eddie', 'Almost There'], dtype=' first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35dab65e-baf0-4a0a-b773-0ec7133b1b23", + "metadata": {}, + "outputs": [], + "source": [ + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "vectorized_in_profit = np.vectorize(in_profit)\n", + "vectorized_in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05eaa992-bed3-4af0-9508-c58be2f36381", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[vectorized_in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acf8cad6-e93a-4ee9-817c-ffffdc4b825e", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f789fa62-e8e0-45c8-973f-b33ee54e3262", + "metadata": {}, + "outputs": [], + "source": [ + "@np.vectorize\n", + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "print(in_profit(monday_prices, friday_prices))\n", + "\n", + "portfolio[in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "257d37f9-5b7f-4ad6-beef-1c8f300a8347", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfec4cdd-db86-4cfe-a325-0597ff2a4f94", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[np.where(friday_prices > monday_prices, True, False)]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 4d1bd4db40786378bbff971a91ef089aee94604d Mon Sep 17 00:00:00 2001 From: eyrei123 Date: Tue, 24 Sep 2024 15:31:38 +0100 Subject: [PATCH 2/8] Initial Versions pre TR --- .../cashed_checks.csv => cashed_checks.csv | 0 ...uplicates.csv => check_list_duplicates.csv | 0 numpy_examples/ex1a.csv => ex1a.csv | 0 numpy_examples/ex1b.csv => ex1b.csv | 0 numpy_examples/ex1c.csv => ex1c.csv | 0 numpy_examples/file1.csv => file1.csv | 0 numpy_examples/file2.csv => file2.csv | 0 numpy_examples/file3.csv => file3.csv | 0 .../issued_checks.csv => issued_checks.csv | 0 ...emperatures.csv => london_temperatures.csv | 0 numpy_examples/long_file.csv => long_file.csv | 0 ...peratures.csv => new_york_temperatures.csv | 0 {numpy_examples => numpy-examples}/README.md | 0 numpy-examples/cashed_checks.csv | 8 + numpy-examples/check_list_duplicates.csv | 12 + numpy-examples/ex1a.csv | 4 + numpy-examples/ex1b.csv | 1 + numpy-examples/ex1c.csv | 3 + numpy-examples/file1.csv | 2 + numpy-examples/file2.csv | 2 + numpy-examples/file3.csv | 2 + numpy-examples/issued_checks.csv | 11 + numpy-examples/london_temperatures.csv | 12 + numpy-examples/long_file.csv | 3 + numpy-examples/new_york_temperatures.csv | 12 + .../passengers.csv | 0 .../passports.csv | 0 .../portfolio.csv | 0 .../rome_temperatures.csv | 0 .../share_prices-1.csv | 0 .../share_prices-2.csv | 0 .../share_prices-3.csv | 0 .../share_prices-4.csv | 0 .../share_prices-5.csv | 0 .../short_file.csv | 0 .../tutorial_code.ipynb | 0 passengers.csv | 22 + passports.csv | 22 + portfolio.csv | 7 + rome_temperatures.csv | 12 + share_prices-1.csv | 7 + share_prices-2.csv | 7 + share_prices-3.csv | 7 + share_prices-4.csv | 7 + share_prices-5.csv | 7 + short_file.csv | 1 + tutorial_code.ipynb | 566 ++++++++++++++++++ 47 files changed, 737 insertions(+) rename numpy_examples/cashed_checks.csv => cashed_checks.csv (100%) rename numpy_examples/check_list_duplicates.csv => check_list_duplicates.csv (100%) rename numpy_examples/ex1a.csv => ex1a.csv (100%) rename numpy_examples/ex1b.csv => ex1b.csv (100%) rename numpy_examples/ex1c.csv => ex1c.csv (100%) rename numpy_examples/file1.csv => file1.csv (100%) rename numpy_examples/file2.csv => file2.csv (100%) rename numpy_examples/file3.csv => file3.csv (100%) rename numpy_examples/issued_checks.csv => issued_checks.csv (100%) rename numpy_examples/london_temperatures.csv => london_temperatures.csv (100%) rename numpy_examples/long_file.csv => long_file.csv (100%) rename numpy_examples/new_york_temperatures.csv => new_york_temperatures.csv (100%) rename {numpy_examples => numpy-examples}/README.md (100%) create mode 100644 numpy-examples/cashed_checks.csv create mode 100644 numpy-examples/check_list_duplicates.csv create mode 100644 numpy-examples/ex1a.csv create mode 100644 numpy-examples/ex1b.csv create mode 100644 numpy-examples/ex1c.csv create mode 100644 numpy-examples/file1.csv create mode 100644 numpy-examples/file2.csv create mode 100644 numpy-examples/file3.csv create mode 100644 numpy-examples/issued_checks.csv create mode 100644 numpy-examples/london_temperatures.csv create mode 100644 numpy-examples/long_file.csv create mode 100644 numpy-examples/new_york_temperatures.csv rename {numpy_examples => numpy-examples}/passengers.csv (100%) rename {numpy_examples => numpy-examples}/passports.csv (100%) rename {numpy_examples => numpy-examples}/portfolio.csv (100%) rename {numpy_examples => numpy-examples}/rome_temperatures.csv (100%) rename {numpy_examples => numpy-examples}/share_prices-1.csv (100%) rename {numpy_examples => numpy-examples}/share_prices-2.csv (100%) rename {numpy_examples => numpy-examples}/share_prices-3.csv (100%) rename {numpy_examples => numpy-examples}/share_prices-4.csv (100%) rename {numpy_examples => numpy-examples}/share_prices-5.csv (100%) rename {numpy_examples => numpy-examples}/short_file.csv (100%) rename {numpy_examples => numpy-examples}/tutorial_code.ipynb (100%) create mode 100644 passengers.csv create mode 100644 passports.csv create mode 100644 portfolio.csv create mode 100644 rome_temperatures.csv create mode 100644 share_prices-1.csv create mode 100644 share_prices-2.csv create mode 100644 share_prices-3.csv create mode 100644 share_prices-4.csv create mode 100644 share_prices-5.csv create mode 100644 short_file.csv create mode 100644 tutorial_code.ipynb diff --git a/numpy_examples/cashed_checks.csv b/cashed_checks.csv similarity index 100% rename from numpy_examples/cashed_checks.csv rename to cashed_checks.csv diff --git a/numpy_examples/check_list_duplicates.csv b/check_list_duplicates.csv similarity index 100% rename from numpy_examples/check_list_duplicates.csv rename to check_list_duplicates.csv diff --git a/numpy_examples/ex1a.csv b/ex1a.csv similarity index 100% rename from numpy_examples/ex1a.csv rename to ex1a.csv diff --git a/numpy_examples/ex1b.csv b/ex1b.csv similarity index 100% rename from numpy_examples/ex1b.csv rename to ex1b.csv diff --git a/numpy_examples/ex1c.csv b/ex1c.csv similarity index 100% rename from numpy_examples/ex1c.csv rename to ex1c.csv diff --git a/numpy_examples/file1.csv b/file1.csv similarity index 100% rename from numpy_examples/file1.csv rename to file1.csv diff --git a/numpy_examples/file2.csv b/file2.csv similarity index 100% rename from numpy_examples/file2.csv rename to file2.csv diff --git a/numpy_examples/file3.csv b/file3.csv similarity index 100% rename from numpy_examples/file3.csv rename to file3.csv diff --git a/numpy_examples/issued_checks.csv b/issued_checks.csv similarity index 100% rename from numpy_examples/issued_checks.csv rename to issued_checks.csv diff --git a/numpy_examples/london_temperatures.csv b/london_temperatures.csv similarity index 100% rename from numpy_examples/london_temperatures.csv rename to london_temperatures.csv diff --git a/numpy_examples/long_file.csv b/long_file.csv similarity index 100% rename from numpy_examples/long_file.csv rename to long_file.csv diff --git a/numpy_examples/new_york_temperatures.csv b/new_york_temperatures.csv similarity index 100% rename from numpy_examples/new_york_temperatures.csv rename to new_york_temperatures.csv diff --git a/numpy_examples/README.md b/numpy-examples/README.md similarity index 100% rename from numpy_examples/README.md rename to numpy-examples/README.md diff --git a/numpy-examples/cashed_checks.csv b/numpy-examples/cashed_checks.csv new file mode 100644 index 0000000000..577981e719 --- /dev/null +++ b/numpy-examples/cashed_checks.csv @@ -0,0 +1,8 @@ +Check_ID,Amount,Date_Cashed +1341,150.00,2024-04-12 +1342,175.00,2024-04-16 +1343,30.00,2024-04-12 +1345,65.00,2024-04-12 +1346,430.00,2024-04-08 +1349,500.00,2024-04-08 +1350,220.00,2024-04-15 \ No newline at end of file diff --git a/numpy-examples/check_list_duplicates.csv b/numpy-examples/check_list_duplicates.csv new file mode 100644 index 0000000000..02a3ddbf9d --- /dev/null +++ b/numpy-examples/check_list_duplicates.csv @@ -0,0 +1,12 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy-examples/ex1a.csv b/numpy-examples/ex1a.csv new file mode 100644 index 0000000000..e97fc94b35 --- /dev/null +++ b/numpy-examples/ex1a.csv @@ -0,0 +1,4 @@ +5,10,15,20 +25,30,35,40 +45,50,55,60 +65,70,75,80 \ No newline at end of file diff --git a/numpy-examples/ex1b.csv b/numpy-examples/ex1b.csv new file mode 100644 index 0000000000..37ffd0470b --- /dev/null +++ b/numpy-examples/ex1b.csv @@ -0,0 +1 @@ +1,3,5 \ No newline at end of file diff --git a/numpy-examples/ex1c.csv b/numpy-examples/ex1c.csv new file mode 100644 index 0000000000..cfa0b54952 --- /dev/null +++ b/numpy-examples/ex1c.csv @@ -0,0 +1,3 @@ +2 +4 +6 \ No newline at end of file diff --git a/numpy-examples/file1.csv b/numpy-examples/file1.csv new file mode 100644 index 0000000000..8dbfc4399b --- /dev/null +++ b/numpy-examples/file1.csv @@ -0,0 +1,2 @@ +1.1,1.2,1.3 +1.4,1.5,1.6 \ No newline at end of file diff --git a/numpy-examples/file2.csv b/numpy-examples/file2.csv new file mode 100644 index 0000000000..dabb7f3a8a --- /dev/null +++ b/numpy-examples/file2.csv @@ -0,0 +1,2 @@ +2.1,2.2,2.3 +2.4,2.5,2.6 \ No newline at end of file diff --git a/numpy-examples/file3.csv b/numpy-examples/file3.csv new file mode 100644 index 0000000000..497c31700f --- /dev/null +++ b/numpy-examples/file3.csv @@ -0,0 +1,2 @@ +3.1,3.2,3.3 +3.4,3.5,3.6 \ No newline at end of file diff --git a/numpy-examples/issued_checks.csv b/numpy-examples/issued_checks.csv new file mode 100644 index 0000000000..79ebe66d15 --- /dev/null +++ b/numpy-examples/issued_checks.csv @@ -0,0 +1,11 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy-examples/london_temperatures.csv b/numpy-examples/london_temperatures.csv new file mode 100644 index 0000000000..ce1d205e15 --- /dev/null +++ b/numpy-examples/london_temperatures.csv @@ -0,0 +1,12 @@ +Jan,5 +Feb,7 +Mar,9 +Apr,11 +May,14 +Jun,16 +Jul,19 +Aug,19 +Sep,17 +Oct,13 +Nov,10 +Dec,7 \ No newline at end of file diff --git a/numpy-examples/long_file.csv b/numpy-examples/long_file.csv new file mode 100644 index 0000000000..0e63b07cb6 --- /dev/null +++ b/numpy-examples/long_file.csv @@ -0,0 +1,3 @@ +4.1,4.2,4.3 +4.4,4.5,4.6 +4.7,4.8,4.9 \ No newline at end of file diff --git a/numpy-examples/new_york_temperatures.csv b/numpy-examples/new_york_temperatures.csv new file mode 100644 index 0000000000..4ef0f4d9fd --- /dev/null +++ b/numpy-examples/new_york_temperatures.csv @@ -0,0 +1,12 @@ +Jan,2 +Feb,2 +Mar,4 +Apr,11 +May,16 +Jun,22 +Jul,25 +Aug,24 +Sep,20 +Oct,14 +Nov,12 +Dec,9 \ No newline at end of file diff --git a/numpy_examples/passengers.csv b/numpy-examples/passengers.csv similarity index 100% rename from numpy_examples/passengers.csv rename to numpy-examples/passengers.csv diff --git a/numpy_examples/passports.csv b/numpy-examples/passports.csv similarity index 100% rename from numpy_examples/passports.csv rename to numpy-examples/passports.csv diff --git a/numpy_examples/portfolio.csv b/numpy-examples/portfolio.csv similarity index 100% rename from numpy_examples/portfolio.csv rename to numpy-examples/portfolio.csv diff --git a/numpy_examples/rome_temperatures.csv b/numpy-examples/rome_temperatures.csv similarity index 100% rename from numpy_examples/rome_temperatures.csv rename to numpy-examples/rome_temperatures.csv diff --git a/numpy_examples/share_prices-1.csv b/numpy-examples/share_prices-1.csv similarity index 100% rename from numpy_examples/share_prices-1.csv rename to numpy-examples/share_prices-1.csv diff --git a/numpy_examples/share_prices-2.csv b/numpy-examples/share_prices-2.csv similarity index 100% rename from numpy_examples/share_prices-2.csv rename to numpy-examples/share_prices-2.csv diff --git a/numpy_examples/share_prices-3.csv b/numpy-examples/share_prices-3.csv similarity index 100% rename from numpy_examples/share_prices-3.csv rename to numpy-examples/share_prices-3.csv diff --git a/numpy_examples/share_prices-4.csv b/numpy-examples/share_prices-4.csv similarity index 100% rename from numpy_examples/share_prices-4.csv rename to numpy-examples/share_prices-4.csv diff --git a/numpy_examples/share_prices-5.csv b/numpy-examples/share_prices-5.csv similarity index 100% rename from numpy_examples/share_prices-5.csv rename to numpy-examples/share_prices-5.csv diff --git a/numpy_examples/short_file.csv b/numpy-examples/short_file.csv similarity index 100% rename from numpy_examples/short_file.csv rename to numpy-examples/short_file.csv diff --git a/numpy_examples/tutorial_code.ipynb b/numpy-examples/tutorial_code.ipynb similarity index 100% rename from numpy_examples/tutorial_code.ipynb rename to numpy-examples/tutorial_code.ipynb diff --git a/passengers.csv b/passengers.csv new file mode 100644 index 0000000000..d5b924e62b --- /dev/null +++ b/passengers.csv @@ -0,0 +1,22 @@ +passenger_no,first_name,last_name +1,Olivia,Smith +2,Amelia,Jones +3,Isla,Williams +4,Ava,Taylor +5,Ivy,Brown +6,Freya,Davies +7,Lily,Evans +8,Florence,Wilson +9,Mia,Thomas +10,Willow,Johnson +11,Noah,Roberts +12,Oliver,Robinson +13,George,Thompson +14,Arthur,Wright +14,Arthur,Wright +15,Muhammad,Walker +16,Leo,White +17,Harry,Edwards +18,Oscar,Hughes +19,Archie,Green +20,Henry,Hall \ No newline at end of file diff --git a/passports.csv b/passports.csv new file mode 100644 index 0000000000..729d3351f9 --- /dev/null +++ b/passports.csv @@ -0,0 +1,22 @@ +passport_no,passenger_no,nationality +123656489,1,British +373456789,2,British +493456399,3,American +375456228,4,American +457345975,5,Austrian +345957363,6,Norewegian +567546577,7,French +453667456,8,German +456785778,9,Danish +456785778,9,Danish +679857668,10,Dutch +456657867,11,Dutch +467775488,12,French +884393487,13,Danish +122647657,15,Dutch +534677673,16,British +786737688,17,American +654672278,18,Spanish +683637288,19,Norwegian +768357788,20,British +768357788,21,American \ No newline at end of file diff --git a/portfolio.csv b/portfolio.csv new file mode 100644 index 0000000000..5e1e766100 --- /dev/null +++ b/portfolio.csv @@ -0,0 +1,7 @@ +Company,Sector +Company_A,technology +Company_B,finance +Company_C,healthcare +Company_D,technology +Company_E,finance +Company_F,healthcare \ No newline at end of file diff --git a/rome_temperatures.csv b/rome_temperatures.csv new file mode 100644 index 0000000000..d70c14343a --- /dev/null +++ b/rome_temperatures.csv @@ -0,0 +1,12 @@ +Jan,8 +Feb,9 +Mar,12 +Apr,14 +May,21 +Jun,23 +Jul,26 +Aug,24 +Sep,22 +Oct,18 +Nov,13 +Dec,10 \ No newline at end of file diff --git a/share_prices-1.csv b/share_prices-1.csv new file mode 100644 index 0000000000..9e6342db35 --- /dev/null +++ b/share_prices-1.csv @@ -0,0 +1,7 @@ +Company,mon +Company_A,100.5 +Company_B,200.1 +Company_C,50.3 +Company_D,110.5 +Company_E,200.1 +Company_F,55.3 \ No newline at end of file diff --git a/share_prices-2.csv b/share_prices-2.csv new file mode 100644 index 0000000000..b29208eade --- /dev/null +++ b/share_prices-2.csv @@ -0,0 +1,7 @@ +Company,tue +Company_A,101.2 +Company_B,199.8 +Company_C,50.5 +Company_D,101.2 +Company_E,200.8 +Company_F,50.5 \ No newline at end of file diff --git a/share_prices-3.csv b/share_prices-3.csv new file mode 100644 index 0000000000..7cce3ec69a --- /dev/null +++ b/share_prices-3.csv @@ -0,0 +1,7 @@ +Company,Wed +Company_A,102 +Company_B,200.5 +Company_C,51.0 +Company_D,102 +Company_E,200.5 +Company_F,53.0 \ No newline at end of file diff --git a/share_prices-4.csv b/share_prices-4.csv new file mode 100644 index 0000000000..c7d58df53f --- /dev/null +++ b/share_prices-4.csv @@ -0,0 +1,7 @@ +Company,Thu +Company_A,101.8 +Company_B,201.0 +Company_C,50.8 +Company_D,111.8 +Company_E,211.0 +Company_F,50.8 \ No newline at end of file diff --git a/share_prices-5.csv b/share_prices-5.csv new file mode 100644 index 0000000000..5f6d2d7b9f --- /dev/null +++ b/share_prices-5.csv @@ -0,0 +1,7 @@ +Company,Fri +Company_A,112.5 +Company_B,200.8 +Company_C,51.2 +Company_D,97.5 +Company_E,200.8 +Company_F,52.2 \ No newline at end of file diff --git a/short_file.csv b/short_file.csv new file mode 100644 index 0000000000..30c26e4dec --- /dev/null +++ b/short_file.csv @@ -0,0 +1 @@ +4.1,4.2,4.3 diff --git a/tutorial_code.ipynb b/tutorial_code.ipynb new file mode 100644 index 0000000000..8708a4e4fb --- /dev/null +++ b/tutorial_code.ipynb @@ -0,0 +1,566 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c33e52ac-a587-41c3-b487-6a1533043a54", + "metadata": {}, + "source": [ + "# **Code Used In NumPy Practical Examples: Useful Techniques**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29aff3d0-0427-4dd8-b14f-bc7ce9002a76", + "metadata": {}, + "outputs": [], + "source": [ + "!python -m pip install numpy\n", + "!python -m pip install matplotlib\n", + "!python -m pip install pathlib\n", + "!python -m pip install jupyterlab" + ] + }, + { + "cell_type": "markdown", + "id": "8b740161-a80e-4d37-8c26-2dca52c68dce", + "metadata": {}, + "source": [ + "**Example 1: Creating Multi-Dimensional Arrays From Files**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e64a11c1-a0b5-49f8-a0a6-dfe0adf674f4", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from pathlib import Path\n", + "\n", + "array = np.zeros(\n", + " (\n", + " 3,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "print(array.shape)\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8de90c08-9bdc-42f3-ad4e-d7a34fca8215", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array[3, 0] = np.loadtxt(\"short_file.csv\", delimiter=\",\")\n", + "\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63775265-deb1-4cca-bd7c-547b3d49f5ba", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array = np.insert(arr=array, obj=2, values=0, axis=1)\n", + "\n", + "array[3] = np.loadtxt(\"long_file.csv\", delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "\n", + "array" + ] + }, + { + "cell_type": "markdown", + "id": "fd4544ac-53bc-449c-88a2-f2496bba8f55", + "metadata": {}, + "source": [ + "**Example 2: Reconciling Data Using Structured NumPy Arrays**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6d760bcd-1be5-4396-8eb8-d729768909d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['At The Back', 'Fast Eddie', 'Almost There'], dtype=' first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35dab65e-baf0-4a0a-b773-0ec7133b1b23", + "metadata": {}, + "outputs": [], + "source": [ + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "vectorized_in_profit = np.vectorize(in_profit)\n", + "vectorized_in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05eaa992-bed3-4af0-9508-c58be2f36381", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[vectorized_in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acf8cad6-e93a-4ee9-817c-ffffdc4b825e", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f789fa62-e8e0-45c8-973f-b33ee54e3262", + "metadata": {}, + "outputs": [], + "source": [ + "@np.vectorize\n", + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "print(in_profit(monday_prices, friday_prices))\n", + "\n", + "portfolio[in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "257d37f9-5b7f-4ad6-beef-1c8f300a8347", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfec4cdd-db86-4cfe-a325-0597ff2a4f94", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[np.where(friday_prices > monday_prices, True, False)]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 23c6ab186de47568f1094d3bcf318c217b37909d Mon Sep 17 00:00:00 2001 From: gahjelle Date: Tue, 8 Oct 2024 15:07:41 +0200 Subject: [PATCH 3/8] Remove old files in the root directory --- cashed_checks.csv | 8 - check_list_duplicates.csv | 12 - ex1a.csv | 4 - ex1b.csv | 1 - ex1c.csv | 3 - file1.csv | 2 - file2.csv | 2 - file3.csv | 2 - issued_checks.csv | 11 - london_temperatures.csv | 12 - long_file.csv | 3 - new_york_temperatures.csv | 12 - passengers.csv | 22 -- passports.csv | 22 -- portfolio.csv | 7 - rome_temperatures.csv | 12 - share_prices-1.csv | 7 - share_prices-2.csv | 7 - share_prices-3.csv | 7 - share_prices-4.csv | 7 - share_prices-5.csv | 7 - short_file.csv | 1 - tutorial_code.ipynb | 566 -------------------------------------- 23 files changed, 737 deletions(-) delete mode 100644 cashed_checks.csv delete mode 100644 check_list_duplicates.csv delete mode 100644 ex1a.csv delete mode 100644 ex1b.csv delete mode 100644 ex1c.csv delete mode 100644 file1.csv delete mode 100644 file2.csv delete mode 100644 file3.csv delete mode 100644 issued_checks.csv delete mode 100644 london_temperatures.csv delete mode 100644 long_file.csv delete mode 100644 new_york_temperatures.csv delete mode 100644 passengers.csv delete mode 100644 passports.csv delete mode 100644 portfolio.csv delete mode 100644 rome_temperatures.csv delete mode 100644 share_prices-1.csv delete mode 100644 share_prices-2.csv delete mode 100644 share_prices-3.csv delete mode 100644 share_prices-4.csv delete mode 100644 share_prices-5.csv delete mode 100644 short_file.csv delete mode 100644 tutorial_code.ipynb diff --git a/cashed_checks.csv b/cashed_checks.csv deleted file mode 100644 index 577981e719..0000000000 --- a/cashed_checks.csv +++ /dev/null @@ -1,8 +0,0 @@ -Check_ID,Amount,Date_Cashed -1341,150.00,2024-04-12 -1342,175.00,2024-04-16 -1343,30.00,2024-04-12 -1345,65.00,2024-04-12 -1346,430.00,2024-04-08 -1349,500.00,2024-04-08 -1350,220.00,2024-04-15 \ No newline at end of file diff --git a/check_list_duplicates.csv b/check_list_duplicates.csv deleted file mode 100644 index 02a3ddbf9d..0000000000 --- a/check_list_duplicates.csv +++ /dev/null @@ -1,12 +0,0 @@ -Check_ID,Payee,Amount,Date_Issued -1341,K.Starmer,150.00,2024-03-29 -1342,R Sunak,175.00,2024-03-29 -1343,L Truss,30.00,2024-03-29 -1344,B Johnson,45.00,2024-03-22 -1344,B Johnson,45.00,2024-03-22 -1345,T May,65.00,2024-03-22 -1346,D Cameron,430.00,2024-03-22 -1347,G Brown,100.00,2024-03-15 -1348,T Blair,250.00,2024-03-15 -1349,J Major,500.00,2024-03-15 -1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/ex1a.csv b/ex1a.csv deleted file mode 100644 index e97fc94b35..0000000000 --- a/ex1a.csv +++ /dev/null @@ -1,4 +0,0 @@ -5,10,15,20 -25,30,35,40 -45,50,55,60 -65,70,75,80 \ No newline at end of file diff --git a/ex1b.csv b/ex1b.csv deleted file mode 100644 index 37ffd0470b..0000000000 --- a/ex1b.csv +++ /dev/null @@ -1 +0,0 @@ -1,3,5 \ No newline at end of file diff --git a/ex1c.csv b/ex1c.csv deleted file mode 100644 index cfa0b54952..0000000000 --- a/ex1c.csv +++ /dev/null @@ -1,3 +0,0 @@ -2 -4 -6 \ No newline at end of file diff --git a/file1.csv b/file1.csv deleted file mode 100644 index 8dbfc4399b..0000000000 --- a/file1.csv +++ /dev/null @@ -1,2 +0,0 @@ -1.1,1.2,1.3 -1.4,1.5,1.6 \ No newline at end of file diff --git a/file2.csv b/file2.csv deleted file mode 100644 index dabb7f3a8a..0000000000 --- a/file2.csv +++ /dev/null @@ -1,2 +0,0 @@ -2.1,2.2,2.3 -2.4,2.5,2.6 \ No newline at end of file diff --git a/file3.csv b/file3.csv deleted file mode 100644 index 497c31700f..0000000000 --- a/file3.csv +++ /dev/null @@ -1,2 +0,0 @@ -3.1,3.2,3.3 -3.4,3.5,3.6 \ No newline at end of file diff --git a/issued_checks.csv b/issued_checks.csv deleted file mode 100644 index 79ebe66d15..0000000000 --- a/issued_checks.csv +++ /dev/null @@ -1,11 +0,0 @@ -Check_ID,Payee,Amount,Date_Issued -1341,K.Starmer,150.00,2024-03-29 -1342,R Sunak,175.00,2024-03-29 -1343,L Truss,30.00,2024-03-29 -1344,B Johnson,45.00,2024-03-22 -1345,T May,65.00,2024-03-22 -1346,D Cameron,430.00,2024-03-22 -1347,G Brown,100.00,2024-03-15 -1348,T Blair,250.00,2024-03-15 -1349,J Major,500.00,2024-03-15 -1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/london_temperatures.csv b/london_temperatures.csv deleted file mode 100644 index ce1d205e15..0000000000 --- a/london_temperatures.csv +++ /dev/null @@ -1,12 +0,0 @@ -Jan,5 -Feb,7 -Mar,9 -Apr,11 -May,14 -Jun,16 -Jul,19 -Aug,19 -Sep,17 -Oct,13 -Nov,10 -Dec,7 \ No newline at end of file diff --git a/long_file.csv b/long_file.csv deleted file mode 100644 index 0e63b07cb6..0000000000 --- a/long_file.csv +++ /dev/null @@ -1,3 +0,0 @@ -4.1,4.2,4.3 -4.4,4.5,4.6 -4.7,4.8,4.9 \ No newline at end of file diff --git a/new_york_temperatures.csv b/new_york_temperatures.csv deleted file mode 100644 index 4ef0f4d9fd..0000000000 --- a/new_york_temperatures.csv +++ /dev/null @@ -1,12 +0,0 @@ -Jan,2 -Feb,2 -Mar,4 -Apr,11 -May,16 -Jun,22 -Jul,25 -Aug,24 -Sep,20 -Oct,14 -Nov,12 -Dec,9 \ No newline at end of file diff --git a/passengers.csv b/passengers.csv deleted file mode 100644 index d5b924e62b..0000000000 --- a/passengers.csv +++ /dev/null @@ -1,22 +0,0 @@ -passenger_no,first_name,last_name -1,Olivia,Smith -2,Amelia,Jones -3,Isla,Williams -4,Ava,Taylor -5,Ivy,Brown -6,Freya,Davies -7,Lily,Evans -8,Florence,Wilson -9,Mia,Thomas -10,Willow,Johnson -11,Noah,Roberts -12,Oliver,Robinson -13,George,Thompson -14,Arthur,Wright -14,Arthur,Wright -15,Muhammad,Walker -16,Leo,White -17,Harry,Edwards -18,Oscar,Hughes -19,Archie,Green -20,Henry,Hall \ No newline at end of file diff --git a/passports.csv b/passports.csv deleted file mode 100644 index 729d3351f9..0000000000 --- a/passports.csv +++ /dev/null @@ -1,22 +0,0 @@ -passport_no,passenger_no,nationality -123656489,1,British -373456789,2,British -493456399,3,American -375456228,4,American -457345975,5,Austrian -345957363,6,Norewegian -567546577,7,French -453667456,8,German -456785778,9,Danish -456785778,9,Danish -679857668,10,Dutch -456657867,11,Dutch -467775488,12,French -884393487,13,Danish -122647657,15,Dutch -534677673,16,British -786737688,17,American -654672278,18,Spanish -683637288,19,Norwegian -768357788,20,British -768357788,21,American \ No newline at end of file diff --git a/portfolio.csv b/portfolio.csv deleted file mode 100644 index 5e1e766100..0000000000 --- a/portfolio.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,Sector -Company_A,technology -Company_B,finance -Company_C,healthcare -Company_D,technology -Company_E,finance -Company_F,healthcare \ No newline at end of file diff --git a/rome_temperatures.csv b/rome_temperatures.csv deleted file mode 100644 index d70c14343a..0000000000 --- a/rome_temperatures.csv +++ /dev/null @@ -1,12 +0,0 @@ -Jan,8 -Feb,9 -Mar,12 -Apr,14 -May,21 -Jun,23 -Jul,26 -Aug,24 -Sep,22 -Oct,18 -Nov,13 -Dec,10 \ No newline at end of file diff --git a/share_prices-1.csv b/share_prices-1.csv deleted file mode 100644 index 9e6342db35..0000000000 --- a/share_prices-1.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,mon -Company_A,100.5 -Company_B,200.1 -Company_C,50.3 -Company_D,110.5 -Company_E,200.1 -Company_F,55.3 \ No newline at end of file diff --git a/share_prices-2.csv b/share_prices-2.csv deleted file mode 100644 index b29208eade..0000000000 --- a/share_prices-2.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,tue -Company_A,101.2 -Company_B,199.8 -Company_C,50.5 -Company_D,101.2 -Company_E,200.8 -Company_F,50.5 \ No newline at end of file diff --git a/share_prices-3.csv b/share_prices-3.csv deleted file mode 100644 index 7cce3ec69a..0000000000 --- a/share_prices-3.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,Wed -Company_A,102 -Company_B,200.5 -Company_C,51.0 -Company_D,102 -Company_E,200.5 -Company_F,53.0 \ No newline at end of file diff --git a/share_prices-4.csv b/share_prices-4.csv deleted file mode 100644 index c7d58df53f..0000000000 --- a/share_prices-4.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,Thu -Company_A,101.8 -Company_B,201.0 -Company_C,50.8 -Company_D,111.8 -Company_E,211.0 -Company_F,50.8 \ No newline at end of file diff --git a/share_prices-5.csv b/share_prices-5.csv deleted file mode 100644 index 5f6d2d7b9f..0000000000 --- a/share_prices-5.csv +++ /dev/null @@ -1,7 +0,0 @@ -Company,Fri -Company_A,112.5 -Company_B,200.8 -Company_C,51.2 -Company_D,97.5 -Company_E,200.8 -Company_F,52.2 \ No newline at end of file diff --git a/short_file.csv b/short_file.csv deleted file mode 100644 index 30c26e4dec..0000000000 --- a/short_file.csv +++ /dev/null @@ -1 +0,0 @@ -4.1,4.2,4.3 diff --git a/tutorial_code.ipynb b/tutorial_code.ipynb deleted file mode 100644 index 8708a4e4fb..0000000000 --- a/tutorial_code.ipynb +++ /dev/null @@ -1,566 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "c33e52ac-a587-41c3-b487-6a1533043a54", - "metadata": {}, - "source": [ - "# **Code Used In NumPy Practical Examples: Useful Techniques**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "29aff3d0-0427-4dd8-b14f-bc7ce9002a76", - "metadata": {}, - "outputs": [], - "source": [ - "!python -m pip install numpy\n", - "!python -m pip install matplotlib\n", - "!python -m pip install pathlib\n", - "!python -m pip install jupyterlab" - ] - }, - { - "cell_type": "markdown", - "id": "8b740161-a80e-4d37-8c26-2dca52c68dce", - "metadata": {}, - "source": [ - "**Example 1: Creating Multi-Dimensional Arrays From Files**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e64a11c1-a0b5-49f8-a0a6-dfe0adf674f4", - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "from pathlib import Path\n", - "\n", - "array = np.zeros(\n", - " (\n", - " 3,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", - "print(id(array))\n", - "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", - " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", - "\n", - "print(id(array))\n", - "print(array.shape)\n", - "array" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8de90c08-9bdc-42f3-ad4e-d7a34fca8215", - "metadata": {}, - "outputs": [], - "source": [ - "array = np.zeros(\n", - " (\n", - " 4,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", - "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", - " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", - "\n", - "array[3, 0] = np.loadtxt(\"short_file.csv\", delimiter=\",\")\n", - "\n", - "array" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "63775265-deb1-4cca-bd7c-547b3d49f5ba", - "metadata": {}, - "outputs": [], - "source": [ - "array = np.zeros(\n", - " (\n", - " 4,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", - "print(id(array))\n", - "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", - " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", - "\n", - "array = np.insert(arr=array, obj=2, values=0, axis=1)\n", - "\n", - "array[3] = np.loadtxt(\"long_file.csv\", delimiter=\",\")\n", - "\n", - "print(id(array))\n", - "\n", - "array" - ] - }, - { - "cell_type": "markdown", - "id": "fd4544ac-53bc-449c-88a2-f2496bba8f55", - "metadata": {}, - "source": [ - "**Example 2: Reconciling Data Using Structured NumPy Arrays**" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "6d760bcd-1be5-4396-8eb8-d729768909d6", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array(['At The Back', 'Fast Eddie', 'Almost There'], dtype=' first_day:\n", - " return True\n", - " return False\n", - "\n", - "\n", - "monday_prices = portfolio[\"mon\"]\n", - "friday_prices = portfolio[\"fri\"]\n", - "\n", - "in_profit(monday_prices, friday_prices)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "35dab65e-baf0-4a0a-b773-0ec7133b1b23", - "metadata": {}, - "outputs": [], - "source": [ - "def in_profit(first_day, last_day):\n", - " if last_day > first_day:\n", - " return True\n", - " return False\n", - "\n", - "\n", - "monday_prices = portfolio[\"mon\"]\n", - "friday_prices = portfolio[\"fri\"]\n", - "\n", - "vectorized_in_profit = np.vectorize(in_profit)\n", - "vectorized_in_profit(monday_prices, friday_prices)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "05eaa992-bed3-4af0-9508-c58be2f36381", - "metadata": {}, - "outputs": [], - "source": [ - "portfolio[vectorized_in_profit(monday_prices, friday_prices)]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "acf8cad6-e93a-4ee9-817c-ffffdc4b825e", - "metadata": {}, - "outputs": [], - "source": [ - "in_profit(3, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f789fa62-e8e0-45c8-973f-b33ee54e3262", - "metadata": {}, - "outputs": [], - "source": [ - "@np.vectorize\n", - "def in_profit(first_day, last_day):\n", - " if last_day > first_day:\n", - " return True\n", - " return False\n", - "\n", - "\n", - "monday_prices = portfolio[\"mon\"]\n", - "friday_prices = portfolio[\"fri\"]\n", - "\n", - "print(in_profit(monday_prices, friday_prices))\n", - "\n", - "portfolio[in_profit(monday_prices, friday_prices)]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "257d37f9-5b7f-4ad6-beef-1c8f300a8347", - "metadata": {}, - "outputs": [], - "source": [ - "in_profit(3, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bfec4cdd-db86-4cfe-a325-0597ff2a4f94", - "metadata": {}, - "outputs": [], - "source": [ - "portfolio[np.where(friday_prices > monday_prices, True, False)]" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.12.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From b206b2f9d28dcb8ce818132aa0e11dba9dc612bc Mon Sep 17 00:00:00 2001 From: martin-martin Date: Fri, 25 Oct 2024 14:32:02 +0200 Subject: [PATCH 4/8] Add code for Interacting With Python tutorial update --- interacting-with-python/README.md | 3 +++ interacting-with-python/hello.py | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 interacting-with-python/README.md create mode 100644 interacting-with-python/hello.py diff --git a/interacting-with-python/README.md b/interacting-with-python/README.md new file mode 100644 index 0000000000..2597e710aa --- /dev/null +++ b/interacting-with-python/README.md @@ -0,0 +1,3 @@ +# Interacting With Python + +This folder provides the code examples for the article [Interacting With Python](https://realpython.com/interacting-with-python/). diff --git a/interacting-with-python/hello.py b/interacting-with-python/hello.py new file mode 100644 index 0000000000..f04a16480f --- /dev/null +++ b/interacting-with-python/hello.py @@ -0,0 +1,4 @@ +greeting = "Hello, World!" +print(greeting) +print("Printing from a file.") + From 9a495ca6888fbf561c9ff9bab1fc3a19c21107c9 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Fri, 25 Oct 2024 14:38:49 +0200 Subject: [PATCH 5/8] Remove newline --- interacting-with-python/hello.py | 1 - 1 file changed, 1 deletion(-) diff --git a/interacting-with-python/hello.py b/interacting-with-python/hello.py index f04a16480f..94742830bd 100644 --- a/interacting-with-python/hello.py +++ b/interacting-with-python/hello.py @@ -1,4 +1,3 @@ greeting = "Hello, World!" print(greeting) print("Printing from a file.") - From 07673463eb64bea124b8543fa17add5c207b5bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Zaczy=C5=84ski?= Date: Thu, 31 Oct 2024 15:35:43 +0100 Subject: [PATCH 6/8] Materials for Python Concurrency [UPDATE] --- concurrency-overview/README.md | 9 ----- concurrency-overview/cpu_mp.py | 21 ---------- concurrency-overview/cpu_non_concurrent.py | 20 ---------- concurrency-overview/cpu_threading.py | 21 ---------- concurrency-overview/io_mp.py | 35 ---------------- concurrency-overview/race_condition.py | 17 -------- concurrency-overview/requirements.txt | 35 ---------------- python-concurrency/README.md | 13 ++++++ python-concurrency/cpu_asyncio.py | 18 +++++++++ python-concurrency/cpu_non_concurrent.py | 17 ++++++++ python-concurrency/cpu_processes.py | 18 +++++++++ python-concurrency/cpu_threads.py | 18 +++++++++ .../io_asyncio.py | 32 +++++++-------- .../io_non_concurrent.py | 27 +++++++------ python-concurrency/io_processes.py | 40 +++++++++++++++++++ .../io_threads.py | 39 +++++++++--------- python-concurrency/requirements.txt | 13 ++++++ 17 files changed, 189 insertions(+), 204 deletions(-) delete mode 100644 concurrency-overview/README.md delete mode 100755 concurrency-overview/cpu_mp.py delete mode 100755 concurrency-overview/cpu_non_concurrent.py delete mode 100755 concurrency-overview/cpu_threading.py delete mode 100755 concurrency-overview/io_mp.py delete mode 100755 concurrency-overview/race_condition.py delete mode 100644 concurrency-overview/requirements.txt create mode 100644 python-concurrency/README.md create mode 100644 python-concurrency/cpu_asyncio.py create mode 100644 python-concurrency/cpu_non_concurrent.py create mode 100644 python-concurrency/cpu_processes.py create mode 100644 python-concurrency/cpu_threads.py rename {concurrency-overview => python-concurrency}/io_asyncio.py (50%) mode change 100755 => 100644 rename {concurrency-overview => python-concurrency}/io_non_concurrent.py (62%) mode change 100755 => 100644 create mode 100644 python-concurrency/io_processes.py rename concurrency-overview/io_threading.py => python-concurrency/io_threads.py (66%) mode change 100755 => 100644 create mode 100644 python-concurrency/requirements.txt diff --git a/concurrency-overview/README.md b/concurrency-overview/README.md deleted file mode 100644 index 3e3864aa13..0000000000 --- a/concurrency-overview/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Speed Up Your Python Program With Concurrency: Code Examples - -Corresponding code to the Real Python tutorial, "[Speed up your Python Program with Concurrency](https://realpython.com/python-concurrency/)." - -To run the code here, use: - - pip install -r requirements.txt - -This will ensure you have the required packages. diff --git a/concurrency-overview/cpu_mp.py b/concurrency-overview/cpu_mp.py deleted file mode 100755 index fedc305cdf..0000000000 --- a/concurrency-overview/cpu_mp.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python3 -import multiprocessing -import time - - -def cpu_bound(number): - return sum(i * i for i in range(number)) - - -def find_sums(numbers): - with multiprocessing.Pool() as pool: - pool.map(cpu_bound, numbers) - - -if __name__ == "__main__": - numbers = [5_000_000 + x for x in range(20)] - - start_time = time.time() - find_sums(numbers) - duration = time.time() - start_time - print(f"Duration {duration} seconds") diff --git a/concurrency-overview/cpu_non_concurrent.py b/concurrency-overview/cpu_non_concurrent.py deleted file mode 100755 index ec98f080bd..0000000000 --- a/concurrency-overview/cpu_non_concurrent.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -import time - - -def cpu_bound(number): - return sum(i * i for i in range(number)) - - -def find_sums(numbers): - for number in numbers: - cpu_bound(number) - - -if __name__ == "__main__": - numbers = [5_000_000 + x for x in range(20)] - - start_time = time.time() - find_sums(numbers) - duration = time.time() - start_time - print(f"Duration {duration} seconds") diff --git a/concurrency-overview/cpu_threading.py b/concurrency-overview/cpu_threading.py deleted file mode 100755 index c15ef2006c..0000000000 --- a/concurrency-overview/cpu_threading.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python3 -import concurrent.futures -import time - - -def cpu_bound(number): - return sum(i * i for i in range(number)) - - -def find_sums(numbers): - with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: - executor.map(cpu_bound, numbers) - - -if __name__ == "__main__": - numbers = [5_000_000 + x for x in range(20)] - - start_time = time.time() - find_sums(numbers) - duration = time.time() - start_time - print(f"Duration {duration} seconds") diff --git a/concurrency-overview/io_mp.py b/concurrency-overview/io_mp.py deleted file mode 100755 index 174d99fceb..0000000000 --- a/concurrency-overview/io_mp.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 -import multiprocessing -import time - -import requests - -session = None - - -def set_global_session(): - global session - if not session: - session = requests.Session() - - -def download_site(url): - with session.get(url) as response: - name = multiprocessing.current_process().name - print(f"{name}:Read {len(response.content)} from {url}") - - -def download_all_sites(sites): - with multiprocessing.Pool(initializer=set_global_session) as pool: - pool.map(download_site, sites) - - -if __name__ == "__main__": - sites = [ - "https://www.jython.org", - "http://olympus.realpython.org/dice", - ] * 80 - start_time = time.time() - download_all_sites(sites) - duration = time.time() - start_time - print(f"Downloaded {len(sites)} in {duration} seconds") diff --git a/concurrency-overview/race_condition.py b/concurrency-overview/race_condition.py deleted file mode 100755 index 7f75c9faf8..0000000000 --- a/concurrency-overview/race_condition.py +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env python3 -import concurrent.futures - -counter = 0 - - -def increment_counter(fake_value): - global counter - for _ in range(100): - counter += 1 - - -if __name__ == "__main__": - fake_data = [x for x in range(5000)] - counter = 0 - with concurrent.futures.ThreadPoolExecutor(max_workers=5000) as executor: - executor.map(increment_counter, fake_data) diff --git a/concurrency-overview/requirements.txt b/concurrency-overview/requirements.txt deleted file mode 100644 index 245c03137f..0000000000 --- a/concurrency-overview/requirements.txt +++ /dev/null @@ -1,35 +0,0 @@ -aiohttp==3.4.4 -asks==2.0.0 -astroid==2.0.4 -async-generator==1.10 -async-timeout==3.0.1 -atomicwrites==1.2.0 -attrs==18.1.0 -certifi==2018.8.13 -chardet==3.0.4 -contextvars==2.3 -h11==0.8.1 -idna==2.7 -immutables==0.6 -isort==4.3.4 -lazy-object-proxy==1.3.1 -mccabe==0.6.1 -more-itertools==4.3.0 -multidict==4.4.2 -multio==0.2.3 -outcome==0.1.0 -pathlib2==2.3.2 -pluggy==0.7.1 -py==1.6.0 -pycodestyle==2.3.1 -pytest==3.7.3 -requests==2.19.1 -six==1.11.0 -sniffio==1.0.0 -sortedcontainers==2.0.4 -tqdm==4.25.0 -trio==0.6.0 -typed-ast==1.1.0 -urllib3==1.23 -wrapt==1.10.11 -yarl==1.2.6 diff --git a/python-concurrency/README.md b/python-concurrency/README.md new file mode 100644 index 0000000000..eb59cdddb3 --- /dev/null +++ b/python-concurrency/README.md @@ -0,0 +1,13 @@ +# Speed Up Your Python Program With Concurrency + +Corresponding code to the Real Python tutorial, [Speed up your Python Program with Concurrency](https://realpython.com/python-concurrency/). + +First, create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/) to use this code. Then, install the required libraries into it: + +```sh +$ python -m venv venv/ +$ source venv/bin/activate +(venv) $ python -m pip install -r requirements.txt +``` + +This will ensure you have the required Python packages. diff --git a/python-concurrency/cpu_asyncio.py b/python-concurrency/cpu_asyncio.py new file mode 100644 index 0000000000..34efe1f72a --- /dev/null +++ b/python-concurrency/cpu_asyncio.py @@ -0,0 +1,18 @@ +import asyncio +import time + + +async def main(): + start_time = time.perf_counter() + tasks = [fib(35) for _ in range(20)] + await asyncio.gather(*tasks, return_exceptions=True) + duration = time.perf_counter() - start_time + print(f"Computed in {duration} seconds") + + +async def fib(n): + return n if n < 2 else await fib(n - 2) + await fib(n - 1) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/python-concurrency/cpu_non_concurrent.py b/python-concurrency/cpu_non_concurrent.py new file mode 100644 index 0000000000..9fd492f399 --- /dev/null +++ b/python-concurrency/cpu_non_concurrent.py @@ -0,0 +1,17 @@ +import time + + +def main(): + start_time = time.perf_counter() + for _ in range(20): + fib(35) + duration = time.perf_counter() - start_time + print(f"Computed in {duration} seconds") + + +def fib(n): + return n if n < 2 else fib(n - 2) + fib(n - 1) + + +if __name__ == "__main__": + main() diff --git a/python-concurrency/cpu_processes.py b/python-concurrency/cpu_processes.py new file mode 100644 index 0000000000..e87c26c241 --- /dev/null +++ b/python-concurrency/cpu_processes.py @@ -0,0 +1,18 @@ +import concurrent.futures +import time + + +def main(): + start_time = time.perf_counter() + with concurrent.futures.ProcessPoolExecutor() as executor: + executor.map(fib, [35] * 20) + duration = time.perf_counter() - start_time + print(f"Computed in {duration} seconds") + + +def fib(n): + return n if n < 2 else fib(n - 2) + fib(n - 1) + + +if __name__ == "__main__": + main() diff --git a/python-concurrency/cpu_threads.py b/python-concurrency/cpu_threads.py new file mode 100644 index 0000000000..8b46852f35 --- /dev/null +++ b/python-concurrency/cpu_threads.py @@ -0,0 +1,18 @@ +import concurrent.futures +import time + + +def main(): + start_time = time.perf_counter() + with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: + executor.map(fib, [35] * 20) + duration = time.perf_counter() - start_time + print(f"Computed in {duration} seconds") + + +def fib(n): + return n if n < 2 else fib(n - 2) + fib(n - 1) + + +if __name__ == "__main__": + main() diff --git a/concurrency-overview/io_asyncio.py b/python-concurrency/io_asyncio.py old mode 100755 new mode 100644 similarity index 50% rename from concurrency-overview/io_asyncio.py rename to python-concurrency/io_asyncio.py index 7a062c4977..c022668e67 --- a/concurrency-overview/io_asyncio.py +++ b/python-concurrency/io_asyncio.py @@ -1,30 +1,30 @@ -#!/usr/bin/env python3 import asyncio import time import aiohttp -async def download_site(session, url): - async with session.get(url) as response: - print("Read {0} from {1}".format(response.content_length, url)) +async def main(): + sites = [ + "https://www.jython.org", + "http://olympus.realpython.org/dice", + ] * 80 + start_time = time.perf_counter() + await download_all_sites(sites) + duration = time.perf_counter() - start_time + print(f"Downloaded {len(sites)} sites in {duration} seconds") async def download_all_sites(sites): async with aiohttp.ClientSession() as session: - tasks = [] - for url in sites: - task = asyncio.ensure_future(download_site(session, url)) - tasks.append(task) + tasks = [download_site(url, session) for url in sites] await asyncio.gather(*tasks, return_exceptions=True) +async def download_site(url, session): + async with session.get(url) as response: + print(f"Read {len(await response.read())} bytes from {url}") + + if __name__ == "__main__": - sites = [ - "https://www.jython.org", - "http://olympus.realpython.org/dice", - ] * 80 - start_time = time.time() - asyncio.get_event_loop().run_until_complete(download_all_sites(sites)) - duration = time.time() - start_time - print(f"Downloaded {len(sites)} sites in {duration} seconds") + asyncio.run(main()) diff --git a/concurrency-overview/io_non_concurrent.py b/python-concurrency/io_non_concurrent.py old mode 100755 new mode 100644 similarity index 62% rename from concurrency-overview/io_non_concurrent.py rename to python-concurrency/io_non_concurrent.py index 2fef578b0f..b48f9d799e --- a/concurrency-overview/io_non_concurrent.py +++ b/python-concurrency/io_non_concurrent.py @@ -1,12 +1,17 @@ -#!/usr/bin/env python3 import time import requests -def download_site(url, session): - with session.get(url) as response: - print(f"Read {len(response.content)} from {url}") +def main(): + sites = [ + "https://www.jython.org", + "http://olympus.realpython.org/dice", + ] * 80 + start_time = time.perf_counter() + download_all_sites(sites) + duration = time.perf_counter() - start_time + print(f"Downloaded {len(sites)} sites in {duration} seconds") def download_all_sites(sites): @@ -15,12 +20,10 @@ def download_all_sites(sites): download_site(url, session) +def download_site(url, session): + with session.get(url) as response: + print(f"Read {len(response.content)} bytes from {url}") + + if __name__ == "__main__": - sites = [ - "https://www.jython.org", - "http://olympus.realpython.org/dice", - ] * 80 - start_time = time.time() - download_all_sites(sites) - duration = time.time() - start_time - print(f"Downloaded {len(sites)} in {duration} seconds") + main() diff --git a/python-concurrency/io_processes.py b/python-concurrency/io_processes.py new file mode 100644 index 0000000000..21baa4c55a --- /dev/null +++ b/python-concurrency/io_processes.py @@ -0,0 +1,40 @@ +import atexit +import multiprocessing +import time +from concurrent.futures import ProcessPoolExecutor + +import requests + +session: requests.Session + + +def main(): + sites = [ + "https://www.jython.org", + "http://olympus.realpython.org/dice", + ] * 80 + start_time = time.perf_counter() + download_all_sites(sites) + duration = time.perf_counter() - start_time + print(f"Downloaded {len(sites)} sites in {duration} seconds") + + +def download_all_sites(sites): + with ProcessPoolExecutor(initializer=init_process) as executor: + executor.map(download_site, sites) + + +def download_site(url): + with session.get(url) as response: + name = multiprocessing.current_process().name + print(f"{name}:Read {len(response.content)} bytes from {url}") + + +def init_process(): + global session + session = requests.Session() + atexit.register(session.close) + + +if __name__ == "__main__": + main() diff --git a/concurrency-overview/io_threading.py b/python-concurrency/io_threads.py old mode 100755 new mode 100644 similarity index 66% rename from concurrency-overview/io_threading.py rename to python-concurrency/io_threads.py index 0b7458ed90..663d975ccc --- a/concurrency-overview/io_threading.py +++ b/python-concurrency/io_threads.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import concurrent.futures import threading import time @@ -8,29 +7,33 @@ thread_local = threading.local() -def get_session(): - if not hasattr(thread_local, "session"): - thread_local.session = requests.Session() - return thread_local.session +def main(): + sites = [ + "https://www.jython.org", + "http://olympus.realpython.org/dice", + ] * 80 + start_time = time.perf_counter() + download_all_sites(sites) + duration = time.perf_counter() - start_time + print(f"Downloaded {len(sites)} sites in {duration} seconds") + + +def download_all_sites(sites): + with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: + executor.map(download_site, sites) def download_site(url): - session = get_session() + session = get_session_for_thread() with session.get(url) as response: - print(f"Read {len(response.content)} from {url}") + print(f"Read {len(response.content)} bytes from {url}") -def download_all_sites(sites): - with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: - executor.map(download_site, sites) +def get_session_for_thread(): + if not hasattr(thread_local, "session"): + thread_local.session = requests.Session() + return thread_local.session if __name__ == "__main__": - sites = [ - "https://www.jython.org", - "http://olympus.realpython.org/dice", - ] * 80 - start_time = time.time() - download_all_sites(sites) - duration = time.time() - start_time - print(f"Downloaded {len(sites)} in {duration} seconds") + main() diff --git a/python-concurrency/requirements.txt b/python-concurrency/requirements.txt new file mode 100644 index 0000000000..3cb9747ebf --- /dev/null +++ b/python-concurrency/requirements.txt @@ -0,0 +1,13 @@ +aiohappyeyeballs==2.4.3 +aiohttp==3.10.10 +aiosignal==1.3.1 +attrs==24.2.0 +certifi==2024.8.30 +charset-normalizer==3.4.0 +frozenlist==1.5.0 +idna==3.10 +multidict==6.1.0 +propcache==0.2.0 +requests==2.32.3 +urllib3==2.2.3 +yarl==1.17.1 From 400b250288ed766a07874c75a15e5ee487171a95 Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 4 Nov 2024 10:12:41 -0700 Subject: [PATCH 7/8] Language Edit --- interacting-with-python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interacting-with-python/README.md b/interacting-with-python/README.md index 2597e710aa..66f393d6e7 100644 --- a/interacting-with-python/README.md +++ b/interacting-with-python/README.md @@ -1,3 +1,3 @@ # Interacting With Python -This folder provides the code examples for the article [Interacting With Python](https://realpython.com/interacting-with-python/). +This folder contains the code examples for the [Interacting With Python](https://realpython.com/interacting-with-python/) tutorial. From e99d93c391f38dea632cac8fedc0ca425f696fa2 Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:02:16 -0700 Subject: [PATCH 8/8] Language Edit --- python-concurrency/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-concurrency/README.md b/python-concurrency/README.md index eb59cdddb3..79b90e8f13 100644 --- a/python-concurrency/README.md +++ b/python-concurrency/README.md @@ -1,8 +1,8 @@ # Speed Up Your Python Program With Concurrency -Corresponding code to the Real Python tutorial, [Speed up your Python Program with Concurrency](https://realpython.com/python-concurrency/). +This folder contains the sample code for the [Speed Up Your Python Program With Concurrency](https://realpython.com/python-concurrency/) tutorial. -First, create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/) to use this code. Then, install the required libraries into it: +To use this code, first create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/). Then, install the required libraries into it: ```sh $ python -m venv venv/