Skip to content

Commit

Permalink
changing items here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
leila010 committed Oct 23, 2024
1 parent 64034f8 commit 26a4449
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 1 deletion.
284 changes: 284 additions & 0 deletions _notebooks/project_lists/lists_presentation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
{
"cells": [
{
"attachments": {},
"cell_type": "raw",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"layout: post\n",
"title: Playing with Jupyter Notebooks and Python\n",
"description: GitHub pages was built with Python and Jupyter Notebooks in mind. This post is to verify tools by using Python. \n",
"categories: [DevOps, Python]\n",
"permalink: /jupyter/notebook/python\n",
"menu: nav/tools_setup.html\n",
"toc: true\n",
"comments: true\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python and Jupyter Notebooks\n",
"Python is a highly versatile and widely-used programming language, renowned for its readability and broad library support. Jupyter Notebooks, on the other hand, is an interactive computing environment that enables users to create and share documents containing live code, equations, visualizations, and narrative text. Together, they form a powerful toolkit for data analysis, scientific research, and educational purposes.\n",
"\n",
"We will play with Python and Jupyter Notebooks to get a feel for both. This is a great interactive way to start development."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Emoji Print\n",
"It is easy to add an emoji to a message in code. However, using the emoji library or other libraries often requires you to install code on your machine. Before using a library, that is not part of Python distribution, you must install with `pip`\n",
"\n",
"```bash\n",
"# terminal command to install library\n",
"$ pip install emoji\n",
"Collecting emoji\n",
" Downloading emoji-2.5.1.tar.gz (356 kB)\n",
"...\n",
"Successfully installed emoji-2.5.1\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!pip install emoji\n",
"from emoji import emojize \n",
"print(emojize(\":thumbs_up: Python is awesome! :grinning_face:\"))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extracting Data \n",
"Web sites become a lot more interesting when you are working with data, not trying to create it. Here is some code using a library called newspaper, this extracts a couple of writeups from the CNN Entertainment site.\n",
"- Learn more on [newspaper3k](https://newspaper.readthedocs.io/en/latest/)\n",
"- Learn about library for [wikipedia](https://pypi.org/project/wikipedia/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!pip install newspaper3k\n",
"from newspaper import Article\n",
"from IPython.display import display, Markdown\n",
"\n",
"\n",
"urls = [\"http://cnn.com/2023/03/29/entertainment/the-mandalorian-episode-5-recap/index.html\", \n",
" \"https://www.cnn.com/2023/06/09/entertainment/jurassic-park-anniversary/index.html\"]\n",
"\n",
"for url in urls:\n",
" article = Article(url)\n",
" article.download()\n",
" article.parse()\n",
" # Jupyter Notebook Display\n",
" # print(article.title)\n",
" display(Markdown(article.title)) # Jupyter display only\n",
" display(Markdown(article.text)) # Jupyter display only\n",
" print(\"\\n\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!pip install wikipedia\n",
"import wikipedia \n",
"from IPython.display import display, Markdown # add for Jupyter\n",
"\n",
"terms = [\"Python (programming language)\", \"JavaScript\"]\n",
"for term in terms:\n",
" # Search for a page \n",
" result = wikipedia.search(term)\n",
" # Get the summary of the first result\n",
" summary = wikipedia.summary(result[0])\n",
" print(term) \n",
" # print(summary) # console display\n",
" display(Markdown(summary)) # Jupyter display"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inspecting a Function\n",
"The inspect module can give you the output of what's inside many Python functions/objects. This can help you explore code behind what you are using.\n",
"- [Inspect](https://docs.python.org/3/library/inspect.html) documentation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import inspect \n",
"from newspaper import Article\n",
"\n",
"# inspect newspaper Article function\n",
"print(inspect.getsource(Article))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Python Data Types\n",
"Dynamic typing means that the type of the variable is determined only during runtime. Strong typing means that variables do have a type and that the type matters when performing operations. In the illustration below there are two functions\n",
"- mean... shows types required prior to calling average function\n",
"- average, average2... calculates the average of a list of numbers\n",
"\n",
"\n",
"Python has types. In the language you can use type hints, but most coders do not use them. In other languages like Java and 'C' you must specify types.\n",
"- [Python Types Cheat Sheet](https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from typing import Union\n",
"\n",
"# Define types for mean function, trying to analyze input possibilities\n",
"Number = Union[int, float] # Number can be either int or float type\n",
"Numbers = list[Number] # Numbers is a list of Number types\n",
"Scores = Union[Number, Numbers] # Scores can be single or multiple \n",
"\n",
"def mean(scores: Scores, method: int = 1) -> float:\n",
" \"\"\"\n",
" Calculate the mean of a list of scores.\n",
" \n",
" Average and Average2 are hidden functions performing mean algorithm\n",
"\n",
" If a single score is provided in scores, it is returned as the mean.\n",
" If a list of scores is provided, the average is calculated and returned.\n",
" \"\"\"\n",
" \n",
" def average(scores): \n",
" \"\"\"Calculate the average of a list of scores using a Python for loop with rounding.\"\"\"\n",
" sum = 0\n",
" len = 0\n",
" for score in scores:\n",
" if isinstance(score, Number):\n",
" sum += score\n",
" len += 1\n",
" else:\n",
" print(\"Bad data: \" + str(score) + \" in \" + str(scores))\n",
" sys.exit()\n",
" return sum / len\n",
" \n",
" def average2(scores):\n",
" \"\"\"Calculate the average of a list of scores using the built-in sum() function with rounding.\"\"\"\n",
" return sum(scores) / len(scores)\n",
"\n",
" # test to see if scores is a list of numbers\n",
" if isinstance(scores, list):\n",
" if method == 1: \n",
" # long method\n",
" result = average(scores)\n",
" else:\n",
" # built in method\n",
" result = average2(scores)\n",
" return round(result + 0.005, 2)\n",
" \n",
" return scores # case where scores is a single valu\n",
"\n",
"# try with one number\n",
"singleScore = 100\n",
"print(\"Print test data: \" + str(singleScore)) # concat data for single line\n",
"print(\"Mean of single number: \" + str(mean(singleScore)))\n",
"\n",
"print()\n",
"\n",
"# define a list of numbers\n",
"testScores = [90.5, 100, 85.4, 88]\n",
"print(\"Print test data: \" + str(testScores))\n",
"print(\"Average score, loop method: \" + str(mean(testScores)))\n",
"print(\"Average score, function method: \" + str(mean(testScores, 2)))\n",
"\n",
"print()\n",
"\n",
"badData = [100, \"NaN\", 90]\n",
"print(\"Print test data: \" + str(badData))\n",
"print(\"Mean with bad data: \" + str(mean(badData)))\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hacks\n",
"Here is a summary of some of the things learned above.\n",
"- Formatting messages with emoji\n",
"- Exploring data with newspaper and wikipedia libraries\n",
"- Finding code on how the library we used was made\n",
"- Learning about data types while writing an algorithm for mean\n",
"\n",
"> Part of Project Based learning is the idea of combining concepts to form something more interesting. Make a plan, form some ideas, brainstorm ideas with pair. Produce something that is interesting and challenging. Samples...\n",
"- Could I get input from user to look up wikipedia information? [Python input](https://www.w3schools.com/python/ref_func_input.asp), [Article on Input](https://vegibit.com/python-input-function/)\n",
"- What could I learn in Python about Stats to get Machine Learning Read? [Stats Calculations](https://docs.python.org/3/library/statistics.html)\n",
"- Could I add emoji to an extracted article? [String Find](https://www.w3schools.com/python/ref_string_find.asp), [String Methods](https://www.w3schools.com/python/python_strings_methods.asp)\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"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.11.4"
},
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion navigation/rpg.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ permalink: /rpg/
SCALE_FACTOR: 10,
STEP_FACTOR: 1000,
ANIMATION_RATE: 50,
pixels: {height: 280, width: 256},
pixels: {height: 210, width: 240},
orientation: {rows: 4, columns: 3 },
down: {row: 0, start: 0, columns: 3 },
left: {row: 1, start: 0, columns: 3 },
Expand Down
Loading

0 comments on commit 26a4449

Please sign in to comment.