Python Code Snippnet from Colab
Python Code Snippnet from Colab
What is snippet?
A snippet is a small, reusable piece of code designed to perform a specific task or solve a particular problem. It’s often just a few lines long and is meant to be quickly inserted into a larger program to save time or avoid re-writing commonly used functions.
Key Features of a Snippet:
- Short and specific: Snippets usually address a single functionality or task, such as reading a file, making a network request, or sorting data.
- Reusable: Snippets can be reused in multiple projects without modification.
- Pre-tested: Snippets are typically pre-tested, so you can trust them to work in many situations.
- Contextual: A snippet often relies on a specific environment or language and may need to be adapted slightly to fit your use case.
Example of a Snippet:
For example, if you frequently need to read a file in Python, you might save a snippet like this:
# A snippet to read a file
with open('file.txt', 'r') as file:
data = file.read()
print(data)
This snippet can be used in any Python project to quickly read a file.
Note: If you want, you can convert above code into a function and put inside module. But many times it does not make sense to create a function for this kind of work, rather using a code snippet makes things simple.
How Snippets Differ from Libraries, Packages, and Modules:
- Snippets are small, standalone bits of code. They are typically just individual functions or a few lines of logic.
- Libraries and packages are more structured, containing many functions and classes, often packaged for broader reuse.
- Modules are Python files containing reusable code, often more complex than snippets.
- Frameworks provide a full set of tools, including snippets but also structures and conventions for building large applications.
Where Snippets are Used:
- Code editors: Many code editors like Visual Studio Code, PyCharm, and Sublime Text offer built-in snippets that can be quickly inserted into your code.
- Online resources: Websites like StackOverflow and GitHub Gist are filled with snippets shared by developers for specific tasks.
- Google Colab: Snippets are available as pre-built, reusable blocks of code to solve common data science tasks.
Snippets save time and effort, allowing developers to focus on more complex or unique aspects of their projects.
What is Colab Code Snippet?
If you want to do anything in Colab in Python language, don’t spend time writing code from scratch. There are rich code snippets available from Google Colab that you can use to save a significant amount of time. This is especially useful for beginners who are starting their journey and struggle to write code, but it’s also helpful for experts because, after all, it takes time to think, write, and test code.
And why does it matter whether the library was created by you, a project team member, or Google? As long as you understand what you want and the code snippet helps achieve that, use it without feeling guilty about not writing the code yourself.
After all, business is not about writing code, but about solving social and business problems. If there’s a solution available in the form of libraries, snippets, or templates, using them is the wise choice. Just make sure you understand what each line of code is doing.
Colab snippets (Sep’2024)
This keeps growing.
- Adding form fields in Colab
- Apply QA_PIXEL cloud mask to Landsat 8
- Authenticate to GCP
- Calculate mean NDVI on the 10km buffer around Paris using Sentinel-2
- Camera Capture
- Cross-output communication
- Display SRTM DEM over Seattle
- display.Javascript to execute JavaScript from Python
- Downloading files or importing data from Google Drive
- Downloading files to your local file system
- Evaluate a Javascript expression from Python with eval_js
- Filter Landsat 8 to a date range
- Gemini: Connecting to Gemini
- Gemini: Creating a prompt
- Hiding code
- Importing a library that is not in Colaboratory
- Importing data directly from Google Sheets
- Importing data from Google Sheets
- Install cartopy
- Install 7zip reader libarchive
- Install GraphViz & PyDot
- Javascript to Python communication
- Jupyter Comms
- Jupyter Widgets
- Listing files in Google Drive
- Mask out Sentinel-2 pixels above 100 m using SRTM
- Mount a Cloud Storage location into the local filesystem
- Mounting Google Drive in your VM
- Open files from GCS with gsutil
- Open files from GCS with the Cloud Storage Python API
- Open files from GitHub
- Open files from Google Drive
- Open files from your local file system
- Output Handling
- Pandas DataFrame: Create from lists of values
- Pandas DataFrame: Drop duplicate rows
- Pandas DataFrame: Explode a column containing dictionary values into multiple columns
- Pandas DataFrame: Extract values using regexp (regular expression)
- Pandas DataFrame: Filter by Timestamp in DatetimeIndex using
.loc[]
- Pandas DataFrame: Filter by Timestamp using TimeDelta string
- Pandas DataFrame: Ignore one Column
- Pandas DataFrame: Intersect Indexes
- Pandas DataFrame: Query by regexp (regular expression)
- Pandas DataFrame: Query by Timestamp above a value
- Pandas DataFrame: Query by variable value
- Pandas DataFrame: Query for Timestamp between two values
- Pandas DataFrame: Query using variable value as a column name
- Pandas DataFrame: Rename multiple Columns
- Pandas DataFrame: Reshape to have 1 row per value in a list column
- Pandas DataFrame: Select all rows from A that are not in B, using the index
- Pandas DataFrame: Select rows by an attribute of a column value
- Pandas DataFrame: Sort the count of rows grouped on columns
- Pandas Timestamp: Convert string to Timestamp
- Pandas Timestamp: Convert string to Timestamp, using date only
- Pandas: Create a TimeDelta from a string
- Pandas: Create a TimeDelta using
unit
- Pandas: Create a TimeDelta using available kwargs
- Pandas: DataFrames: Group Timeseries by Frequency
- Pandas: Describe Timestamp values in a column
- Pandas: display dataframes as interactive tables
- Pandas: Replace NaN values in a Column
- Pausing output processing
- Saving data to Google Drive
- Saving data to Google Sheets
- Saving data with gsutil
- Saving data with the Cloud Storage Python API
- Serving resources
- Show a bar chart of MODIS land cover classes
- Show a line chart of CHIRPS precipitation
- Show average yearly temperatures over California
- Show NDVI from Sentinel-2 SR over Milan
- Show NDVI over Rio using Landsat 8
- Showing CV2 Images
- Tagged Outputs
- Third-party Jupyter widgets
- Use BigQuery DataFrames
- Using BigQuery with Cloud API
- Using BigQuery with Pandas API
- Visualization: Bar Plot in Altair
- Visualization: Histogram in Altair
- Visualization: Interactive Brushing in Altair
- Visualization: Interactive Scatter Plot in Altair
- Visualization: Linked Brushing in Altair
- Visualization: Linked Scatter-Plot and Histogram in Altair
- Visualization: Scatter Plot with Rolling Mean in Altair
- Visualization: Stacked Histogram in Altair
- Visualization: Time Series Line Plot in Altair
Difference between Framework, Package, Library, Module
Here’s a detailed comparison table of module, library, package, and framework based on different variables:
Variable | Module | Library | Package | Framework |
---|---|---|---|---|
Made of | A single file containing functions and classes. | Multiple files with functions and classes; serves a specific purpose. | Multiple libraries and/or modules grouped together. | A collection of packages, libraries, and modules organized for building applications. |
Purpose | Used for small, specific functionality. | Provides specific functionalities to be used when needed. | Organizes related functionality and can be reused across projects. | Provides a full structure for building applications with rules and guidelines. |
Installation | Typically just copied or included in the same project. | Installed using package managers like pip or npm. | Installed using package managers like pip or npm. | Installed using package managers like pip, npm, or specialized tools. |
Control | The code calls the module whenever needed. | The user calls the library functions whenever necessary. | The user imports and uses packages across multiple libraries. | The framework controls the flow of the project; your code fits within the framework’s structure. |
Structure | No predefined structure; just a file. | No specific structure required; can be used anywhere in the project. | May require a specific directory structure for complex packages. | Requires a well-defined structure, often with prebuilt directories and files like views , models . |
Dependency | May depend on other modules but generally self-contained. | Can depend on other libraries; dependencies managed via pip. | Can include multiple libraries and modules with interdependencies. | May have many dependencies and third-party packages included. |
Reusability | Reusable but mostly within the same project. | Highly reusable across multiple projects. | Reusable across multiple projects, typically contains more functionality than libraries. | Framework code can be reused within projects but often requires adherence to framework-specific guidelines. |
Flexibility | Extremely flexible and can be used anywhere. | Flexible, usable in any part of the project, without structural constraints. | Mostly flexible but organized, sometimes following naming conventions. | Less flexible, as the framework imposes a certain structure and rules. |
Inversion of Control (IoC) | No inversion of control. You are in full control of the flow. | No inversion of control. You control the code flow. | You control the code flow, but packages can have entry points. | The framework controls when and where your code is executed. |
Scale | Small-scale, limited to specific functionality within a project. | Medium-scale, provides specific functionality but doesn’t organize a project. | Medium to large-scale, organizes functionalities and libraries. | Large-scale, providing everything needed to organize and manage an entire project or application. |
Examples | A single Python file defining utility functions. | matplotlib , requests , NumPy |
scikit-learn , pandas , TensorFlow |
Django , Flask , React , Angular |
Additional Notes:
- Modules: Small and self-contained, often written directly as part of a project.
- Libraries: More general-purpose and can be applied to a wide variety of projects without imposing structure.
- Packages: More organized collections that can be shared across multiple projects, combining multiple related libraries.
- Frameworks: Full application structures that provide a skeleton for the entire project and guide development. .
A Newest snippet added in Sep’2024
Gemini: Creating a prompt.
This rich example shows how you can create and configure complex prompts for Gemini.
# @title Create a prompt
import google.generativeai as genai
from google.colab import userdata
api_key_name = 'GOOGLE_API_KEY' # @param {type: "string"}
prompt = 'What is the velocity of an unladen swallow?' # @param {type: "string"}
system_instructions = 'You have a tendency to speak in riddles.' # @param {type: "string"}
model = 'gemini-1.5-flash' # @param {type: "string"} ["gemini-1.0-pro", "gemini-1.5-pro", "gemini-1.5-flash"]
temperature = 0.5 # @param {type: "slider", min: 0, max: 2, step: 0.05}
stop_sequence = '' # @param {type: "string"}
if model == 'gemini-1.0-pro' and system_instructions is not None:
system_instructions = None
print('\x1b[31m(WARNING: System instructions ignored, gemini-1.0-pro does not support system instructions)\x1b[0m')
if model == 'gemini-1.0-pro' and temperature > 1:
temperature = 1
print('\x1b[34m(INFO: Temperature set to 1, gemini-1.0-pro does not support temperature > 1)\x1b[0m')
if system_instructions == '':
system_instructions = None
api_key = userdata.get(api_key_n
Leave a comment