-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3a73b8
commit ff34225
Showing
9 changed files
with
890 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
docs/source/notebooks/03_python_module/02-from_notebooks.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "93f4ef0d-1cce-43fc-baa5-85ea4e6592ec", | ||
"metadata": {}, | ||
"source": [ | ||
"# Transitioning from Jupyter Notebooks to Jupyter Notebooks\n", | ||
"\n", | ||
"Moving code from Jupyter notebooks to a Python module is a structured process that involves organizing your code efficiently. This transition is crucial for enhancing readability, maintainability, and collaboration.\n", | ||
"\n", | ||
"1. **Isolate Logic**: \n", | ||
" - **Identify Core Components**: Go through your Jupyter notebook and pinpoint the core logic, functions, and classes. These are the elements you'll want to transfer to your Python module.\n", | ||
" - **Separate Concerns**: Ensure each function or class has a single responsibility. This approach simplifies maintenance and improves code clarity.\n", | ||
" \n", | ||
"2. **Code Cleanup**:\n", | ||
" - **Follow PEP 8 Standards**: Adhere to [PEP 8](https://www.python.org/dev/peps/pep-0008/) guidelines to ensure code consistency and readability. This includes proper naming conventions, indentation, and spacing.\n", | ||
" - **Refactor and Comment**: Simplify complex code blocks and add comments where necessary. Comments should explain the why, not just the how, providing context for your code.\n", | ||
" - **Remove Redundancies**: Eliminate any redundant or unnecessary code that does not contribute to the core functionality.\n", | ||
"\n", | ||
"3. **File Creation**:\n", | ||
" - **Setup Module Directory**: Create the `gcpds` directory and the chosen submodule directory within it.\n", | ||
" - **Create Python Files**: Based on the complexity, create either a single `__init__.py` file for simple submodules or multiple files like `optional_file_1.py` for more complex ones.\n", | ||
"\n", | ||
"4. **Code Transfer**:\n", | ||
" - **Extract Code from Notebook**: Convert the code cells from your Jupyter notebook into Python script format.\n", | ||
" - **Organize by Functionality**: Group related functions or classes together. If they are part of a common theme or functionality, they should reside in the same file.\n", | ||
" - **Adjust for Script Format**: Transform interactive Jupyter code (like inline plots or progress bars) into a format suitable for scripts.\n", | ||
"\n", | ||
"5. **Testing**:\n", | ||
" - **Unit Tests**: Write unit tests for each function and class to ensure they behave as expected. This is crucial for identifying bugs early.\n", | ||
" - **Test as Standalone Components**: Ensure that each part of your code can function independently. This improves modularity and reusability.\n", | ||
" - **Iterative Testing**: Test your module iteratively as you transfer code. This helps in identifying issues early in the transition process.\n", | ||
"\n", | ||
"6. **Documentation**:\n", | ||
" - **Docstrings**: Include docstrings for each function and class to explain their purpose, parameters, and return values.\n", | ||
" - **Module Documentation**: Provide a README file or equivalent documentation within the module, detailing its purpose, contents, and usage instructions.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "502b4aa9-f29e-4dfc-bc9b-a9782f2dc561", | ||
"metadata": {}, | ||
"source": [ | ||
"# Using Jupyter Notebooks for Testing and Exploration\n", | ||
"\n", | ||
"In the development workflow, Jupyter Notebooks play a crucial role in the initial stages of testing and exploration. However, for creating a robust, maintainable, and reusable codebase, it's essential to transition the main code to a Python package. This section outlines the reasons and the best practices for this approach.\n", | ||
"\n", | ||
"## The Role of Jupyter Notebooks\n", | ||
"\n", | ||
"1. **Exploratory Analysis**:\n", | ||
" - **Interactive Environment**: Jupyter Notebooks offer an interactive environment, making them ideal for exploratory data analysis, quick tests, and prototyping.\n", | ||
" - **Visualization**: They are excellent for visualizations and seeing immediate outputs, which aids in understanding data and debugging.\n", | ||
"\n", | ||
"2. **Limitations for Production**:\n", | ||
" - **Not Ideal for Large Codebases**: Notebooks can become cumbersome for managing larger codebases.\n", | ||
" - **Version Control Challenges**: Notebooks often pose challenges with version control systems, making collaboration and code tracking more difficult.\n", | ||
"\n", | ||
" \n", | ||
"## Transition to Python Packages\n", | ||
"\n", | ||
"1. **Structured Codebase**:\n", | ||
" - **Maintainability**: A Python package structure allows for better organization of code into modules and submodules, enhancing readability and maintainability.\n", | ||
" - **Scalability**: It's easier to scale and extend functionalities in a package format compared to a notebook.\n", | ||
"\n", | ||
"2. **Installation and Reusability**:\n", | ||
" - **Easy Distribution**: Packages can be easily distributed and installed using tools like `pip`. This makes sharing your code with others more straightforward.\n", | ||
" - **Reuse Across Projects**: Once packaged, your code can be imported and reused across different projects, saving time and effort.\n", | ||
"\n", | ||
"3. **Testing and Deployment**:\n", | ||
" - **Unit Testing**: Packages allow for comprehensive unit testing of functions and classes, ensuring code reliability.\n", | ||
" - **Deployment Ready**: Code in a package is more deployment-ready for production environments. " | ||
] | ||
} | ||
], | ||
"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.11.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.