Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename packages, libraries, and conda to follow naming convention #34

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This is the training overview:
[CHESS]: https://www.chess.cornell.edu/

[pe100]: ./theme1/PE100/PE100-01Introduction.ipynb
[pe101]: ./theme1/PE101/python-packages-conda.ipynb
[pe101]: ./theme1/PE101/PE101-01Packages.ipynb
[pe102]: ./theme1/PE102/PE102-02NumPy.ipynb
[pe103]: ./theme1/PE103/vcs-testing-debugging.md

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# PE101-01: Using Python Packages and Libraries\n",
"# PE101-01: Using Python Packages and Modules\n",
"\n",
"By itself, Python provides everything you need to write programs. These programs won't have a fancy user interface and they may not run very fast, but they'll work. If that's all Python offered, it might have become a popular language but it wouldn't have taken over most of the world the way it has. No, what Python has going for it is a simple way to take commonly-used chunks of code, wrap them up neatly into sharable budles, and distribute those bundles far and wide. The mechanism for doing this in Python is called **packages**.\n",
"\n",
Expand Down Expand Up @@ -40,7 +40,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"There are literally oodles of mathematical functions already implemented for you in the `math` package. To see a list of them as they stand currently, see [math - mathematical functions](https://docs.python.org/3/library/math.html) in the current Python documentation.\n",
"## Packages\n",
"\n",
"There are literally oodles of mathematical functions already implemented for you in the `math` package. To see a list of them as they stand currently, see \" [math - mathematical functions](https://docs.python.org/3/library/math.html) \" in the current Python documentation.\n",
"\n",
"Taking a look at the code above, the first thing we notice is the line `import math`. This tells the Python interpreter to find the package named \"math\" and to open it up and make its contents available to this session. The things in the package we can get to will all be named by the word \"math\", a period, and then the name of the actual part of the package to use. We would say the package \"math\" is imported into the \"math namespace\". This is the default behavior, but we can change that. Indeed:"
]
Expand All @@ -64,6 +66,53 @@
"print(rand.random())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By using the `as` keyword in our `import` statement, we're telling Python to load the \"random\" package but let us refer to everything as though its name was \"rand\". In a little more detail, we're creating a namespace \"rand\" instead of just letting Python automatically create a namespace with the same name as the package and load everything into that space.\n",
"\n",
"To see a current list of the packages that come with a standard Python installation, take a look at [this comprehensive list](https://docs.python.org/3/library/). In the first few sections it will list \"built-in\" capabilities - this is what you can do without importing anything. The rest of the page lists the available packages. Click on any of them for details.\n",
"\n",
"## Modules\n",
"\n",
"So far we've seen functions and constants placed into packages and directly accessible with just the package name. If you have a large package, or a package that has lots of custom changes to manage, it can be helpful to break things up into **modules**. Think of a module as a \"sub-package\". Package and module names are separated by periods. Let's take a look..."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"all is good.\n"
]
}
],
"source": [
"import os.path as op\n",
"\n",
"if op.exists(\"/usr/bin\"):\n",
" print(\"all is good.\")\n",
"else:\n",
" print(\"I don't even know how the server booted!\")\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We imported the \"path\" module from the \"os\" package and loaded it into a namespace called \"op\". Then we were able to use that namespace to get to the `exists()` function. We checked to see if the \"/usr/bin\" directory exists. That is, as you might suspect, a critically important directory.\n",
"\n",
"## Coming up next: Packages from the outside world\n",
"\n",
"As we keep saying, one of the biggest (if not **the** biggest) strengths of Python is the many, many thousands of packages that people have written and made publicly available. In the next unit, PE101-02, we'll take a look at how to find those packages, copy them to CHESS servers, and use them in your own notebooks."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down