Skip to content

Commit

Permalink
extended 006 with metadata example and printf styling
Browse files Browse the repository at this point in the history
  • Loading branch information
giumas committed Feb 22, 2019
1 parent a766597 commit 5aba2bc
Showing 1 changed file with 340 additions and 1 deletion.
341 changes: 340 additions & 1 deletion 006_Dictionaries.ipynb
Original file line number Diff line number Diff line change
@@ -206,6 +206,341 @@
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A `dict` as a Metadata Container"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now explore the use of a `dict` as a [metadata](https://en.wikipedia.org/wiki/Metadata) container. \n",
"\n",
"Descriptive metadata provides a resource for several purposes such as data discovery and identification. Following our previous examples of experiments collecting water salinity and temperature values, we will use a `dict` to store metadata such as:\n",
"\n",
"- The author of the measures (`\"first_name\"` and `\"last_name\"`).\n",
"- The location where the measurements took place (`\"latitude\"` and `\"longitude\"`).\n",
"- The time frame when the measures happened (`\"start_timestamp\"` and `\"end_timestamp\"`)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus, a complete set of metadata will be represented by a `dict` containing the following six keys (with the corresponding value type):\n",
"\n",
"- `\"first_name\"` ➜ `str` type\n",
"- `\"last_name\"` ➜ `str` type\n",
"- `\"latitude\"` ➜ `float` type\n",
"- `\"longitude\"` ➜ `float` type\n",
"- `\"start_timestamp\"` ➜ `datetime` type\n",
"- `\"end_timestamp\"` ➜ `datetime` type"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the first time that we use the [`datetime`](https://docs.python.org/3.6/library/datetime.html?#module-datetime) type! "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
"\n",
"A variable of `datetime` type contains all the information from both a date and a time."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can read from the [Python documentation](https://docs.python.org/3.6/library/datetime.html?#datetime-objects), the `datetime` constructor is part of the `datetime` module (yes, they have both the same name!) and takes several parameters: \n",
"\n",
"- `datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the aims of this notebook, you can just ignore all the parameters after the first six. In fact, we will call the `datetime` constructor with only 6 values (from `year` to `second`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"begin_timestamp = datetime(2019, 2, 22, 12, 32, 40)\n",
"print(str(begin_timestamp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**How can the above code actually work?** It works because the parameters after the first 3 (e.g., `hour=0`) have a **default value** assigned to them (the `=0` in this specific example). This implies that, if you do *not* pass values for those parameters, Python will assign them those defined default values. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now write our `metadata`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"metadata = dict()\n",
"metadata[\"first_name\"] = \"John\"\n",
"metadata[\"last_name\"] = \"Doe\"\n",
"metadata[\"latitude\"] = 43.135555\n",
"metadata[\"longitude\"] = -70.939534\n",
"metadata[\"start_timestamp\"] = datetime(2019, 2, 22, 12, 32, 40)\n",
"metadata[\"end_timestamp\"] = datetime(2019, 2, 22, 12, 34, 14)\n",
"\n",
"print(metadata)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"source": [
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
"\n",
"Populate and print a `metadata` dictionary containing the following three keys: your `\"username\"`, the `\"begin_time\"` and the `\"end_time\"` for the execution of this exercise."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"metadata = dict()\n",
"metadata[\"username\"] = \"jdoe\"\n",
"metadata[\"start_timestamp\"] = datetime(2019, 2, 22, 12, 34, 20)\n",
"metadata[\"end_timestamp\"] = datetime(2019, 2, 22, 12, 34, 21)\n",
"\n",
"print(metadata)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More on String Formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this last section of this notebook, we will explore different mechanisms that Python provides for printing (**string formatting**) a value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At this moment, you know how to print a value with `str` type:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first name is: John\n"
]
}
],
"source": [
"metadata = dict()\n",
"metadata[\"first_name\"] = \"John\"\n",
"\n",
"print(\"The first name is: \" + metadata[\"first_name\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You also know that you can type-casting types using `str()`:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The position is: 43.135555, -70.939534\n",
"Start time: 2019-02-22 12:32:40\n"
]
}
],
"source": [
"metadata = dict()\n",
"metadata[\"latitude\"] = 43.135555\n",
"metadata[\"longitude\"] = -70.939534\n",
"metadata[\"start_timestamp\"] = datetime(2019, 2, 22, 12, 32, 40)\n",
"\n",
"print(\"The position is: \" + str(metadata[\"latitude\"]) + \", \" + str(metadata[\"longitude\"]))\n",
"print(\"Start time: \" + str(metadata[\"start_timestamp\"]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is possible to achieve the same results by using the `%` modulo operator like in the following examples:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first name is: John\n"
]
}
],
"source": [
"metadata = dict()\n",
"metadata[\"first_name\"] = \"John\"\n",
"\n",
"print(\"The first name is: %s\" % (metadata[\"first_name\"], ))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The position is: 43.135558, -70.939534\n",
"Start time: 2019-02-22 12:32:40\n"
]
}
],
"source": [
"metadata = dict()\n",
"metadata[\"latitude\"] = 43.135558\n",
"metadata[\"longitude\"] = -70.939534\n",
"metadata[\"start_timestamp\"] = datetime(2019, 2, 22, 12, 32, 40)\n",
"\n",
"print(\"The position is: %s, %s\" % (metadata[\"latitude\"], metadata[\"longitude\"]))\n",
"print(\"Start time: %s\" % (metadata[\"start_timestamp\"], ))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you look at the above examples, you will noticed the presence of `%s` as placeholders in the string. The string is followed by a `%` operator, then by one or more variables enclosed in square brackets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
"\n",
"In the above code, the values after the `%` operator that are inside the square brackets create a [`tuple`](https://docs.python.org/3.6/library/stdtypes.html?#tuples). <br>\n",
"A `tuple` is a Python container that represents an immutable sequence (thus, you cannot change the content of a `tuple`)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"String formatting using the `%` operator provides [additional printing options](https://docs.python.org/3.6/library/stdtypes.html#printf-style-string-formatting). Among them, you can decide how many decimal digits will be printed for a `float` value. \n",
"\n",
"For instance, by using `%.4f` as a placeholder, Python will print **only** the first four decimal digits: "
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The position is: 43.1356, -70.9395\n"
]
}
],
"source": [
"metadata = dict()\n",
"metadata[\"latitude\"] = 43.135558\n",
"metadata[\"longitude\"] = -70.939534\n",
"\n",
"print(\"The position is: %.4f, %.4f\" % (metadata[\"latitude\"], metadata[\"longitude\"]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -222,7 +557,11 @@
"* [The official Python 3.6 documentation](https://docs.python.org/3.6/index.html)\n",
" * [Glossary](https://docs.python.org/3.6/glossary.html)\n",
" * [Mapping Types - dict](https://docs.python.org/3.6/library/stdtypes.html#mapping-types-dict)\n",
" * [Collections - OrderedDict](https://docs.python.org/3.6/library/collections.html?highlight=ordereddict#ordereddict-objects)"
" * [Collections - OrderedDict](https://docs.python.org/3.6/library/collections.html?highlight=ordereddict#ordereddict-objects)\n",
" * [`datetime`](https://docs.python.org/3.6/library/datetime.html?#module-datetime) \n",
" * [`tuple`](https://docs.python.org/3.6/library/stdtypes.html?#tuples)\n",
"* [Hash function](https://en.wikipedia.org/wiki/Hash_function)\n",
"* [Metadata](https://en.wikipedia.org/wiki/Metadata)"
]
},
{

0 comments on commit 5aba2bc

Please sign in to comment.