API key authentication is a common method for securing APIs by controlling access to them. It’s important to note that API keys are great for authentication, but further development should be made to ensure proper authorization at the business level. API keys do not ensure that the correct permissions are being enforced, only that the user has access to the API. Regardless, let’s get started! In this post, we’re going to touch on a few services:| how.wtf
On May 30th, 2024, Amazon announced the release of the Bedrock Converse API. This API is designed to provide a consistent experience for “conversing” with Amazon Bedrock models. The API supports: Conversations with multiple turns System messages Tool use Image and text input In this post, we’ll walk through how to use the Amazon Bedrock Converse API with the Claude Haiku foundational model. Please keep in mind that not all foundational models may support all the features of the Converse...| how.wtf
DynamoDB supports an UpdateItem operation that modifies an existing or creates a new item. The UpdateItem accepts an UpdateExpression that dictates which operations will occur on the specified item. In this post, we’ll explore how to perform multiple operations including within a clause/keyword and with multiple clauses/keywords. What is an update expression An update expression supports four keywords: SET (modify or add item attributes) REMOVE (deleting attributes from an item) ADD (updati...| how.wtf
Typically, AWS recommends leveraging an existing service offering such as Amazon Comprehend to detect and redact PII. However, this post explores an alternative solution using Amazon Bedrock. This is possible using the Claude, Anthropic’s large langauge model, and their publicly available prompt library. In our case, we’ll leverage the PII purifier prompt that is maintained by their prompt engineers. How to extract PII using Amazon Bedrock in Python This demo showcases how to invoke the A...| how.wtf
On April 4th, 2024, Anthropic released official support for tool use through their API. This feature allows developers to define one or more tools that include parameters, descriptions, and schema definitions for Claude to process. In this post, we’ll go over how to leverage these tools using the Python Anthropic SDK. Claude with tools example in Python Firstly, ensure that your Anthropic API key is available as an environment variable in your shell:| how.wtf
The primary motive for writing this article is to address the common error I repeatedly received while troubleshooting event filters: Invalid filter pattern definition. (Service: AWSLambda; Status Code: 400; Error Code: InvalidParameterValueException For my case, the goal was to invoke an AWS Lambda function via a DynamoDB stream. What are Lambda Event Filters Lambda event filters allow developers to specify which types of records from a stream or queue are submitted to a Lambda.| how.wtf
In November 2023, I wrote a post describing how to deploy a lambda function with a function url in Python. For this post, I want to showcase how streamlined and practical it is to deploy a “Lambdalith” (a single Lambda function) that contains an entire API. What this means: No API Gateway API requests can take longer than 30 seconds Faster deployments Local testing without cloud deployment Reduced costs * Easy management * = Depends on the use-case How to deploy a serverless API using Fas...| how.wtf
With the recent release of the Claude 3 family, this was the perfect opportunity to use the Claude API access that I was recently granted. The Claude 3 family boasts a huge improvement from the prior family, Claude 2. The introductory post goes into greater detail. In this post, we’ll explore how to invoke Claude 3 Opus using the Anthropic SDK. Getting started For the purposes of this post, we’ll leverage the Python Anthropic SDK.| how.wtf
On March 4th, 2024, the Claude 3 Sonnet foundational model was made available in Amazon Bedrock. Not only is the model a major improvement over the Claude 2.0 family, but it allows for vision input. In the post, we’ll explore how to invoke Claude 3 Sonnet using Amazon’s boto3 library in Python. Getting started For those unfamiliar with the Bedrock runtime API, I have a post that demonstrates how to invoke it using Claude 2.| how.wtf
Using LangChain, we can create a prompt to output a bash, which is then executed via the BashProcess tool. All of this is as easy as a few lines of Python code! How to generate bash scripts using LangChain First, let’s install the necessary dependencies: 1pip install langchain-core langchain-experimental langchain-openaiNow, let’s write a simplistic prompt to get OpenAI to output bash code. For this example, we’ll target a script that outputs:| how.wtf
Unix provides a utility named date. As its name implies, it allows you to fetch your system’s date. Display the current date Displaying the current date is simple: 1echo $(date)Output: 1Sat Feb 10 21:29:37 EST 2024The system date was printed with the timezone information. Display the current date in UTC Similarly, we can display the current date in UTC (Coordinated Universal Time) using the -u flag: 1echo $(date -u)Output: 1Sun Feb 11 02:31:06 UTC 2024The datetime was printed with the UTC t...| how.wtf
Last year, I wrote a post showcasing how to tokenize words using the Anthropic SDK to track token usage. While this method continues to work, it’s not necessary since the Amazon Bedrock team added token metrics to the SDK response metadata. In this guide, we’ll explore the SDK and how to get those token counts. Install the AWS SDK To begin, install boto3, the AWS SDK for Python, using pip:| how.wtf
In this article, we’ll go over how to properly test exit codes in Bash scripts. Testing exit codes with $? When I first started writing Bash, I often wrote the following code to test whether a program’s execution was successful: 1some_command 2if [ "$?" -eq 0 ]; then 3 echo "It worked!" 4fiHowever, this introduces complexity in the flow of the script. For example, one minor mistake like adding en echo statement:| how.wtf
LangChain is a framework that streamlines developing LLM applications. In a nutshell, it provides abstractions that help build out common steps and flows for working with LLMs. In this post, we’ll go over how to integrate Embedchain, an open source RAG framework, with AWS Bedrock! What is Embedchain Embedchain is an open source RAG framework aimed at allowing developers to quickly gather necessary knowledge for LLM context when answering user prompts.| how.wtf
Retrieval-Augmented Generation (RAG) is a technique for including contextual information from external sources in a large language model’s (LLM) prompt. In other terms, RAG is used to supplement an LLM’s answer by providing more information in the prompt; thus, removing the need to retrain or fine-tune a model. For the purposes of this post, we will implement RAG by using Chroma DB as a vector store with the Nobel Prize data set.| how.wtf
AWS introduced a PartiQL in 2019 as a “query language for all your data” that resembles SQL-like expressions. In November 2020, it was natively integrated with DynamoDB. In this post, we’ll explore using PartiQL with DynamoDB. What is PartiQL PartiQL is an open source initiative that is maintained by Amazon under the Apache 2.0 license. Its goal is to be a SQL-compatible query language for accessing relational, semi-structured, and nested data.| how.wtf
Historically in Python, adding explicit typing for kwargs was not directly supported; however, Python 3.12 added that ability. In this post, we’ll go over methods for adding types to the kwargs argument. Adding type annotations to kwargs Python 3.12 released a new method for explicitly typing kwargs: using a TypedDict + Unpack. 1from typing import NotRequired, TypedDict, Unpack 2 3 4class Parameters(TypedDict): 5 foo: int 6 bar: str 7 baz: NotRequired[str] 8 9 10def some_function(**kwargs: ...| how.wtf
Postman used to be my favorite API testing tool, but the company’s decision to phase out the final local collection storage feature, Scratchpad, in favor of mandatory cloud services, has changed that. What is the best Postman alternative? Finding an alternative means it must satisfy the following requirements: Local collection storage Git-friendly collection definitions for my team members CLI that can invoke collections Beautiful desktop GUI No forced account login with a nice-to-have bein...| how.wtf
Tags are key/value metadata that enable users to categorize their AWS infrastructure. This is useful for identifying groups of resources, monitoring cloud spending, creating reports, etc. In this post, we’ll explore different methods for tagging infrastructure using the AWS CDK. The code samples are in Python, but the techniques are applicable to all languages. Tagging constructs Constructs are the basic “building blocks” of the AWS CDK. They act as components and encapsulate necessary ...| how.wtf
Last year, I wrote a post describing the difference between aws cloudformation deploy and aws cloudformation create-stack. I concluded that the easiest method for deploying CloudFormation templates was cloudformation deploy since it handled change sets on your behalf. My opinion has changed; I discovered a new tool named Rain that is maintained by AWS. What is Rain For folks needing to deploy raw CloudFormation templates, rain is your hero. The tool brings you a lot of power:| how.wtf
Amazon DynamoDB is a fully managed service provided by AWS that enables developers to quickly store data for their applications. In this article, I will showcase how to implement version control in DynamoDB for recording changes to data over time. What is version control in DynamoDB DynamoDB does not support native version control on a per-item basis. If you need to record changes to your data over time, it must be handled via the application.| how.wtf
I need to create an IAM role that is assumable by multiple principals, but the AWS CDK role creation documentation only lists the following example: 1lambda_role = iam.Role(self, "Role", 2 assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"), 3 description="Example role..." 4) 5 6stream = kinesis.Stream(self, "MyEncryptedStream", 7 encryption=kinesis.StreamEncryption.KMS 8) 9 10stream.grant_read(lambda_role)That works great, but how do I list multiple principals? Specifying multiple princi...| how.wtf
By the end of this article, you’ll have a Python app deployed with FastAPI using AWS Lambda and API Gateway that is serving requests. In addition, you will have the infrastructure defined in code. What is AWS Lambda and API Gateway AWS Lambda and Amazon API Gateway are two services offered by AWS that enable developers to quickly spin up infrastructure without the hassle of provisioning or maintaining servers. In our case, we’ll host the FastAPI app in a Lambda function and the API Gatewa...| how.wtf
Amazon API Gateway + AWS Lambda is a powerful duo for creating easy-to-deploy scalable infrastructure that is immediately accessible by thousands of users. For this post, we will create an API Gateway with an lambda proxy integration using the AWS Lambda Powertools library and the AWS CDK. In just a few lines of code, we’ll have our API deployed and ready to go! This tutorial assumes you know what the Amazon API Gateway and AWS Lambda services are.| how.wtf
For this post, I will extend the AWS documentation for ‘Building a custom runtime’ by additionally using the AWS CDK in Python to deploy the Bash custom runtime for AWS Lambda. How to deploy a Lambda custom runtime using AWS CDK Follow the instructions below to deploy an AWS Lambda Function with a Bash custom runtime! Create a /handler folder with a bootstrap file AWS provides a bootstrap file that creates the custom runtime.| how.wtf
Going into 2024, I wanted to minimize the footprint of how.wtf. This included: Removing ads Removing giscus, the commenting system Removing Google Analytics Reducing the website’s size to less than 20kb Changing the theme to be super minimal with a focus on content I have nothing against Google Analytics; I simply wanted something with less impact on my website’s loading speeds. Additionally, GDPR compliance requires companies using Google Analytics to gain explicit user consent for data ...| how.wtf
Retrieving the top 10 used commands in Linux is simple using the history command. What is the history command The history command, as its name implies, is used to view previously executed commands. It’s API is minimal and easy to use. How to view top used commands in Linux Using a combination of history and awk, we can pull the top 10 commands like this: 1history | 2 awk '{CMD[$1]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | 3 grep -v ".| how.wtf
For a side project, I needed to wait for multiple threads to complete before proceeding with the next step. In other words, I needed threads to behave like the Promise.all() functionality of JavaScript. Wait for threads in Python In the sections below, we’ll discuss different methods for waitin on threads to complete in Python. Wait for all threads to complete using start / join In simple terms, we can create the threads, append them to a list, then start and join them.| how.wtf
Amazon Bedrock is a managed service provided by AWS that allows users to invoke models. For more information about the service, please refer to their user guide here. For this article, we’re going to focus on how to invoke the Stable Diffusion model using the Python SDK: boto3. If you want to learn about the Amazon Bedrock SDK in general or how to invoke Claude with it, I have an article here that goes into detail.| how.wtf
AWS released an optimized integration for Amazon Bedrock. It allows StepFunction statemachines to directly call the Amazon Bedrock API without needing to write a Lambda function. How to deploy a StepFunction with Amazon Bedrock integration using AWS CDK Follow the instructions below to deploy a StepFunction with Amazon Bedrock integration using the AWS CDK. The following tutorial uses Python, but the same principles can be used with any language.| how.wtf
Amazon Bedrock is a managed service provided by AWS that provides foundational models at your fingertips through a unified API. The service offers a range of features including foundational model invocations, fine-tuning, agents, guardrails, knowledge base searching, and more! To read more about the service offerings, refer to its documentation. What is Amazon Bedrock Runtime? For this article, we’ll dive into using the Python AWS SDK, boto3, to call foundational models.| how.wtf
Before typing was released for Python, the ABC class reigned as champion for describing shape and behaviors of classes. After type annotations, ABC and @abstractmethod were still used to describe the behaviors: they felt ‘interface-like’. Then, Protocol was released and introduced a new way for declaring class behaviors. Should you use ABC or Protocol? Before describing why or why not you should choose one over the other, let’s delve into what each of them are.| how.wtf
Retrieval-Augmented Generation (RAG) is a technique for improving an LLM’s response by including contextual information from external sources. In other terms, it helps a large language model answer a question by providing facts and information for the prompt. For the purposes of this tutorial, we will implement RAG by leveraging a Chroma DB as a vector store with the FDIC Failed Bank List dataset. Langchain with CSV data in a vector store A vector store leverages a vector database, like Chr...| how.wtf
After pushing changes to a remote repository, a common next step is creating a pull request for your team members to review. In this article, we’ll discuss how to open a pull request from the command line using the GitHub CLI. What is the GitHub CLI The GitHub CLI is a tool that allows users to interact with GitHub directly from the terminal. It simplifies and streamlines GitHub workflows, enabling users to issue pull requests, check issues, clone repositories, etc.| how.wtf
Amazon DynamoDB’s Time to Live (TTL) feature allows for automatic item deletion after a specified timestamp. It comes at no extra cost and is useful for removing outdated data. TTL use-cases Remove stale data and save on DynamoDB memory usage Retain sensitive data only up to contractual or regulatory obligations Trigger processes using DynamoDB Streams based on TTL deletions since the deletion stream is marked as “system” rather than normal How to setup TTL in DynamoDB step-by-step This...| how.wtf
In this article, we’ll discuss how to use NordVPN on Linux. This includes installation, authentication, and general usage of the CLI in Linux. Please note, the links provided for NordVPN are affiliate links. This means if you click on them and make a purchase, I may receive a commission at no extra cost to you. This helps support the blog and allows me to continue creating content like this.| how.wtf
Docker Desktop was my de facto choice on MacOS due to its ease of installation and container management. This changed after Docker updated their subscription service agreement. Step-by-step guide to installing Docker without Docker Desktop The following tutorial assumes that you use brew as your package manager. Install docker Firstly, install docker and docker-credential-helper. 1brew install docker-credential-helper dockerdocker-credential-helper provides a way for Docker to use the MacOS K...| how.wtf
AWS Lambda is an easy-to-use serverless offering that enables developers to quickly deploy code without worrying about maintenance, orchestration, scaling, etc. It’s simple to get started and its free tier is generous! Golang project structure GO does not have a recommended project structure. In fact, keeping a project structure flat is preferrable for small apps: 1project/ 2├── go.mod 3├── go.sum 4├── user.go 5└── main.goMy personal preference is to use the flat str...| how.wtf
Tracking Amazon Bedrock Claude token usage is simple using Langchain! How to track tokens using Langchain For OpenAI models, Langchain provides a native Callback handler for tracking token usage as documented here. 1from langchain.callbacks import get_openai_callback 2from langchain.llms import OpenAI 3 4llm = OpenAI(temperature=0) 5with get_openai_callback() as cb: 6 llm("What is the square root of 4?") 7 8total_tokens = cb.total_tokens 9assert total_tokens > 0However, other model families d...| how.wtf
Monitoring token consumption in Anthropic-based models can be straightforward and hassle-free. In fact, Anthropic offers a simple and effective method for accurately counting tokens using Python! In this guide, I’ll show you how to count tokens for Amazon Bedrock Anthropic models. Installing the Anthropic Bedrock Python Client To begin, install the Amazon Bedrock Python client using pip: 1pip install anthropic-bedrockFor more information about the library, visit the technical documentation ...| how.wtf
DALL-E is an OpenAI model that generates images from textual descriptions. It combines elements of creativity and technology to produce unique imagery based on human prompts. Tip 1 - DALL-E Prompt Book To kick things off, I recommend reviewing the DALL-E prompt book released by the dallery gallery. It contains many useful techniques for describing tone, emotion, angles, lightning, illustration style, etc. It has been a wonderful resource for myself and will likely give you enough information ...| how.wtf
OpenAI has two products that are similar: Assistants and GPTs. In this article, we’ll discuss the differences between the two. What are OpenAI GPTs? GPTs (custom versions of ChatGPT) are specialized tools designed to cater to specific needs and preferences. They enable users, including those without coding skills, to tailor ChatGPT for distinct tasks or interests. Users can create GPTs for private or public use, with the forthcoming GPT Store showcasing creations from verified builders in v...| how.wtf
In this article, we’ll cover how to easily leverage Amazon Bedrock in Langchain. What is Amazon Bedrock? Amazon Bedrock is a fully managed service that provides an API to invoke LLMs. At the time of writing, Amazon Bedrock supports: Anthropic Claude Meta Llama Cohere Command Stability AI SDXL A121 Labs Jurassic with many more slated to come out! How to use Amazon Bedrock with Langchain Langchain easily integrates with Amazon Bedrock via the langchain.| how.wtf
Hosting large langage models (LLMs) locally is simple using LocalAI. What is LocalAI LocalAI is an open source alternative to OpenAI. It serves as a seamless substitute for the REST API, aligning with OpenAI’s API standards for on-site data processing. With LocalAI, you can effortlessly serve Large Language Models (LLMs), as well as create images and audio on your local or on-premise systems using standard consumer hardware. It supports a variety of model families that work with the ggml fo...| how.wtf
Vector databases have seen an increase in popularity due to the rise of Generative AI and Large Language Models (LLMs). Vector databases can be used in tandem with LLMs for Retrieval-augmented generation (RAG) - i.e. a framework for improving the quality of LLM responses by grounding prompts with context from external systems. What is Chroma DB? Chroma is an open-source embedding database that enables retrieving relevant information for LLM prompting.| how.wtf
What are the differences between Langchain and LlamaIndex and what are their use cases? In this article, we’ll explore these differences, helping you choose the most suitable library for your needs. What is Langchain? As the documentation outlines, LangChain is a framework for developing applications powered by language models. Langchain is a framework that enables developers to build solutions powered by language models. Some use-case examples include: Retrieval-augmented generation (RAG) ...| how.wtf
Reverting a submodule in Git is simple. How to revert changes to a Git submodule The cleanest / easiest method of reverting changes to one or many git submodules is by using the following two commands: 1git submodule deinit -f . 2git submodule update --initThe git submodule deinit -f . command “deinitializes” all submodules in the repository. The git submodule update --init command does a new checkout of them.| how.wtf
Deploying Lambda Function URLs with the AWS CDK is simple. How to deploy Function URLs using AWS Python CDK Follow the instructions below to deploy an AWS Lambda Function with a URL. Create a requirements.txt For this tutorial, version 2.106.1 of the AWS CDK was used. 1aws-cdk-lib==2.106.1 2constructs>=10.0.0,<11.0.0Install the dependencies 1pip3 install -r requirements.txtCreate /handler folder with an index.py file 1def handler(event, context): 2 return { 3 "statusCode": 200, 4 "body": {"me...| how.wtf
Implementing custom memory in Langchain is dead simple using the ChatMessageHistory class. How to implement custom memory in Langchain (including LCEL) One of the easiest methods for storing and retrieving messages with Langchain is using the ChatMessageHistory class that is provided from the langchain.memory module. It’s simple to get started with: 1from langchain.memory import ChatMessageHistory 2 3history = ChatMessageHistory() 4 5history.add_user_message("Hello!") 6 7history.add_ai_mess...| how.wtf
Learn the simple process of iterating through a JSON array in bash with the help of jq. Iterate through a JSON array using jq Scenario: You have a file named projects.json contains an array named “projects” that you want to iterate through. 1{ 2 "projects": [ 3 { 4 "name": "project 1", 5 "owner": "owner 1", 6 "id": 1, 7 "version": "2.3.0" 8 }, 9 { 10 "name": "project 2", 11 "owner": "owner 1", 12 "id": 2, 13 "version": "1.| how.wtf
When working with Amazon DynamoDB, the design of the primary key plays a crucial role. Unlike traditional relational databases, DynamoDB does not natively support auto-incrementing primary keys. However, there are alternative approaches that provide greater flexibility and scalability. In this article, we will explore the drawbacks of auto-incrementing primary keys in DynamoDB and present UUIDs as a recommended alternative. The Problem with Auto-Incrementing Keys Auto-incrementing keys can re...| how.wtf
When it comes to lightweight text editors, Pico and Nano are viable options that are available on Unix-based systems. While they share similarities, understanding their differences will enable users to make an informed choice. What is Pico? Pico (Pine composer) is a user-friendly text editor known for its simplicity. It features a straightforward interface with an accessible on-screen menu. The intentions of the software are clear: Move the cursor by using arrow keys Insert a character by typ...| how.wtf
Using a virtual environment with a Jupyter notebook is easy! Install and use virtualenv For instructions on installing, using, and activating virtualenv, refer to my blog article here. NOTE: You may use your virtual environment of choice. Add virtualenv environment to Jupyter notebook Activate the virtual environment Install ipykernel 1pip3 install ipykernel Use the ipykernel install command 1python3 -m ipykernel install --user --name=venv # this name can be anything Open jupyter-lab and clic...| how.wtf
If you need a quick one-liner HTTP server from the command line, Python comes in handy. How to create an HTTP server in one line Python enables users to run a simple command to spin up an HTTP server: 1python -m http.server <PORT>Example: 1python -m http.server 4000By default, this command will serve the files in the current directory using a single-threaded HTTP server bound to localhost. In addition, users may specify a different directory:| how.wtf
What is the shuf command in Linux? What is the shuf command? The shuf command in Linux is a utility for shuffling lines of input files. It allows users to shuffle contents of a file or produce random output based on supplied arguments. How is the shuf command used? Given a file named quotes.txt with the following contents: 1"The only way to do great work is to love what you do.| how.wtf
What is the rev command in Linux? What is the rev command? As the name implies, the rev command (short for reverse) allows users to reverse a line of characters or a file. How is the rev command used? The rev command is typically supplied a string of characters or a file input. rev with a file If a file named example.txt contains: 1This is a line of text 2txet fo enil a si siht 3another oneand rev is supplied example.| how.wtf
Undo and redo operations are seamless using Vim. How to undo changes in Vim To undo changes in Vim, press u. In addition, using any combination of the :u[ndo] command: :u :un :und :undo How to redo changes in Vim To redo changes in Vim, press CTRL-R. In addition, using any combination :red[o] command: :red :redo| how.wtf
When deploying apex classes, a user may run into an odd error: Error: Compile Error: Invalid constructor name. How to solve Invalid constructor name Upon initial inspection, this error may be misleading. The class in question contains a working constructor or a new method was added that is not a constructor. What gives? Normally, this error indicates that a new method was added without adding the return type. Example: 1public class Foo { 2 3 private String bar; 4 5 public Foo(String bar) { 6 ...| how.wtf
Recently, I was made aware of the apropos command in Linux. What is it? What is it used for? What is the apropos command? apropos enables users to search terminal commands based on a keywords or descriptions. This is an incredibly useful tool because it quickly identifies commands based on man pages. Scenario: I forgot the command name for the qalculate CLI I installed. 1% apropos calculator 2qalc(1) - Powerful and easy to use command line calculator 3transicc(1) - little cms ColorSpace conve...| how.wtf
When developing in Python, it’s common to come across **kwargs. What is the double asterisk (**) in Python? If a function parameter explictly denotes the double asterisk: 1def foo(**kwargs): 2 print(kwargs) 3 4foo(bar="test", baz=5)kwargs will represent a dictionary of the key word arguments given to the function. NOTE: kwargs is a standard name that refers to “key word arguments”; however, any name may be used. Output: 1{'bar': 'test', 'baz': 5}| how.wtf
Sorting based on a dictionary value or key is easy using Python. Sort by value in a dictionary In Python 3.7 and above, a combination of dictionary comprehension and sorted can be used: 1foo = {"first": 1, "third": 3, "second": 2, "zeroth": 0} 2print({k: v for k, v in sorted(foo.items(), key=lambda i: i[1])})In addition, 1foo = {"first": 1, "third": 3, "second": 2, "zeroth": 0} 2print(dict(sorted(foo.items(), key=lambda i: i[1])))Output: 1{'zeroth': 0, 'first': 1, 'second': 2, 'third': 3}sort...| how.wtf
Iterating through a two lists in parallel is natively handled in Python using the zip function. Iterate through two lists using zip Python’s zip function will aggregate elements from two or more iterables. 1foo = ["x", "y", "z"] 2bar = [1, 2, 3] 3 4for f, b in zip(foo, bar): 5 print(f, b)Output: 1x 1 2y 2 3z 3This works with any number of iterables: 1foo = ["x", "y", "z"] 2bar = [1, 2, 3] 3baz = ["!| how.wtf
Installing Neovim on Debian-based distributions: Ubuntu, Mint, etc. is easy! Install Neovim on Ubuntu Neovim is available through the Debian package manager. Simply execute the following command: 1sudo apt install neovimInstall Neovim using PPA The default Neovim version bundled with apt is normally outdated. To install a newer version, a PPA (Personal Package Archive) must be used. NOTE: The Neovim PPAs are not maintained by the Neovim team Neovim stable PPA To install the latest stable Neov...| how.wtf
Finding a device that’s connected to a network on Linux is easy with an open source tool named nmap. What is nmap? nmap is a utility for network discovery and security auditing. It’s designed to rapidly scan large networks. Install nmap nmap is available through most popular package managers. For Debian-based distributions: 1sudo apt install nmapFor MacOS: 1brew install nmapUsing nmap Firstly, grab your IP address using ifconfig 1ifconfigthe output will contain an IP.| how.wtf
Merging objects in jq is handled natively since 1.14. Merge objects using jq Given a file named example.json 1{ 2 "key": "value1", 3 "anotherKey": "value2", 4 "oneMoreKey": "value3" 5}and another file named example2.json, 1{ 2 "key1": "value2", 3 "key2": "value3", 4 "key4": "value4" 5}merging the two objects can be completed using: 1jq -s '.[0] * .[1]' example.json example2.json Output: 1{ 2 "key1": "value2", 3 "key2": "value3", 4 "key4": "value4", 5 "key": "value1", 6 "anotherKey": "value2",...| how.wtf
Fetching the key names from a JSON document using jq is simple! Get key names of JSON using jq Given a file named example.json, 1{ 2 "key": "value1", 3 "anotherKey": "value2", 4 "oneMoreKey": "value3" 5}extracting the keys in alphabetical order can be completed using: 1jq 'keys' example.jsonOutput: 1[ 2 "anotherKey", 3 "key", 4 "oneMoreKey" 5]Get key names of JSON unsorted using jq Taking the previous JSON document example.json, the keys may be returned in order as they appear:| how.wtf
The AWS CLI can be installed using several methods: this article attempts to address uninstalling through many of them. Uninstall AWS CLI using Pip 1pip3 uninstall awscli -y 2# OR 3pip uninstall awscli -yUninstall AWS CLI on Ubuntu 1sudo apt-get remove --auto-remove awscliUninstall AWS CLI using Brew on MacOS 1brew uninstall awscliUninstall AWS CLI using Snap 1sudo snap rm -r aws-cliUninstall AWS CLI with Yum 1sudo yum erase awscliUninstall AWS CLI manually If all else fails, uninstalling man...| how.wtf
A full scan of DynamoDB in Python is possible by using the LastEvaluatedKey. Scanning DynamoDB using LastEvaluatedKey According to the boto3 scan documentation, If LastEvaluatedKey is present in the response, you need to paginate the result set. For more information, see Paginating the Results in the Amazon DynamoDB Developer Guide. Simply check for the LastEvaluatedKey in the response using response.get('LastEvaluatedKey'). 1import boto3 2 3dynamodb = boto3.resource( 4 'dynamodb', 5 aws_sess...| how.wtf
Python 3.10 added support for the switch keyword. Implement switch statements in Python The basic syntax for a switch statement is: 1match term: 2 case pattern1: 3 some_action 4 case pattern2: 5 some_other_action 6 case _: 7 default_actionA direct example: 1pizza_size = 0 2match pizza_size: 3 case 0: 4 print("small pizza") 5 case 1: 6 print("medium pizza") 7 case 2: 8 print("large pizza") 9 case _: 10 print("pizza size is not valid")Output:| how.wtf
rm is a great tool that gives users enough power to inadvertently erase data. While the data may be recoverable, there is a tool that gives users recycle bin behavior. What is trash-cli trash-cli is a Python tool that implements the freedesktop.org trash specification. In contrast to rm, users can recover deleted files easily. Installing trash-cli The recommended way to install trash-cli is through pip (the Python package manager): 1pip3 install trash-cliAlternatively, trash-cli is available ...| how.wtf
Checking if a variable is set is easy in Bash; however, there are two common scenarios: Checking if a variable is empty or unset Checking if a variable is unset Checking if a variable is empty or unset Firstly, a simple test may be used to determine if a variable’s value is empty or if it’s unset. 1if [[ -z "$var" ]]; then 2 echo "it's empty or unset" 3fiOR| how.wtf
Counting the number of files in Linux is made possible using the wc command! Count number of files and directories in current directory Using wc in combination with ls, the number of files and directories in the current directory can be displayed: 1ls | wc -lOutput: 1~/how.wtf/content/post$ ls | wc -l 2120To include hidden files and directories, use the -A ls option. 1ls -A | wc -lCount number of files in current directory Using find and wc, the number of files in the current directory can be...| how.wtf
man (manual documentation pages) cannot be replaced; however, there are modern CLI tools that provide simplified experiences. tldr pages tldr is a community-curated collection of simplified man pages. As the name (too long; didn’t read) implies, the focus is providing straightforward examples that demonstrate tool usage. Installing tldr On MacOS, 1brew install tldrUsing the NodeJS client, 1npm -g tldrUsing the Python client, 1pip3 install tldrUsing `tldr Using tldr is straightforward:| how.wtf
Curl is a command line utility that was created in 1998 for transferring data using URLs. Fundamentally, curl allows users to create network requests to a server by specifying a location in the form of a URL and adding optional data. curl (short for “Client URL”) is powered by libcurl – a portable client-side URL transfer library written in C. Why use the curl command? Common use cases for curl include:| how.wtf
For more information about curl, checkout the “What is curl” article. This article will discuss how to interact with an API using PATCH requests through curl. Make a simple PATCH request The basic syntax for sending a PATCH request using curl is: 1curl -X PATCH https://example.comThe -X argument accepts an HTTP method for interacting with the server. For HTTP, valid values include: GET (default), POST, PUT, DELETE, etc. NOTE: The -X flag is a shorthand for --request.| how.wtf
For more information about curl, checkout the “What is curl” article. This article will discuss how to interact with an API using DELETE requests through curl. Make a simple DELETE request The basic syntax for sending a DELETE request using curl is: 1curl -X DELETE https://example.comNOTE: The -X flag is a shorthand for --request. DELETE request example 1curl -X DELETE \ 2 https://jsonplaceholder.typicode.com/posts/1In the example above, a DELETE request is sent to the JSONPlaceholder API...| how.wtf
For more information about curl, checkout the “What is curl” article. This article will discuss how to interact with an API using PUT requests through curl. Make a simple PUT request The basic syntax for sending a PUT request using curl is: 1curl -X PUT https://example.comThe -X argument accepts an HTTP method for interacting with the server. For HTTP, valid values include: GET (default), POST, PUT, DELETE, etc. NOTE: The -X flag is a shorthand for --request.| how.wtf
For more information about curl, checkout the “What is curl” article. This article will discuss how to interact with an API using GET requests through curl. Make a simple GET request The basic syntax for sending a GET request using curl is: 1curl https://example.comOR 1curl -X GET https://example.comNOTE: The -X flag is a shorthand for --request. It’s not required because the default value is GET. Make a GET request for JSON data If the server responds differently per the Accept header,...| how.wtf
For more information about curl, checkout the “What is curl” article. This article will discuss how to interact with an API using POST requests through curl. Make a simple POST request The basic syntax for sending a POST request using curl is: 1curl -X POST https://example.comThe -X argument accepts an HTTP method for interacting with the server. For HTTP, valid values include: GET (default), POST, PUT, DELETE, etc. NOTE: The -X flag is a shorthand for --request.| how.wtf
Curl is a command line utility that was created in 1998 for transferring data using URLs. Fundamentally, curl allows users to create network requests to a server by specifying a location in the form of a URL and adding optional data. curl (short for “Client URL”) is powered by libcurl – a portable client-side URL transfer library written in C. Why use the curl command? Common use cases for curl include:| how.wtf
Building a simple CLI in Bash may seem like a herculean task; however, getopts provides an easy-to-use interface out of the box! For this tutorial, we’ll be using the https://pokeapi.co/ to build a simple CLI for fetching resources from the Pokemon world. What is getopts? Given from the documentation, The getopts utility shall retrieve options and option-arguments from a list of parameters. It shall support the Utility Syntax Guidelines 3 to 10, inclusive, described in XBD Utility Syntax Gu...| how.wtf
Retrieving the file name and extension in Bash can be completed using basename and shell parameter expansion. Get the file name Using the native unix basename command, the file name can be extracted from the path. 1file_path="/path/to/package.tar.gz" 2file_name=$(basename "$file_path") 3echo "$file_name"Output: 1package.tar.gzGet the file extension Using shell parameter expansion, the prior example can include capturing the file extension: 1file_path="/path/to/package.tar.gz" 2file_name=$(bas...| how.wtf
Here are the top 3 ways for converting a string to lowercase in Bash. Using tr One of the most popular ways for lowercasing a string is using tr. This is a POSIX standard method for accomplishing the task. 1echo "I WANT this LoWeRcAsEd" | tr '[:upper:]' '[:lower:]' 2# Outputs: i want this lowercasedUsing awk Another POXIS standard is using awk’s native toLower method. 1echo "I WANT this LoWeRcAsEd" | awk '{print tolower($0)}' 2# Outputs: i want this lowercasedUsing Bash 4.| how.wtf
Linux provides several methods for calculating expressions using the command line. Using bc — basic calculator for command line bc is a native Linux tool (originated on Unix systems in 1975) that stands for “basic calculator”. To use it, simply type bc followed by an expression. To quit, type quit. 1> bc 2Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. 3This is free software with ABSOLUTELY NO WARRANTY.| how.wtf
Installing Docker on Ubuntu-based Linux distributions: Linux Mint, Zorin, Pop OS, etc. is simple! Installing Docker from CLI Docker is available in the default repositories of apt. Install via: 1sudo apt install docker.ioAdd permissions to the docker group by adding the current user: 1sudo usermod -a -G docker $(whoami)Verify that everything worked: 1docker psOutput: 1CONTAINER ID IMAGE COMMAND CREATEDTroubleshooting If the following error occurs after docker ps: 1Got permission denied while ...| how.wtf
Testing for AWS credentials is a straightforward operation. Using get-caller-identity There is a single API call that will always work regardless of permissions: GetCallerIdentity 1aws sts get-caller-identityOutput: 1{ 2 "UserId": "AIDA...", 3 "Account": "123456789012", 4 "Arn": "arn:aws:iam::123456789012:user/thomas" 5}The Arn value depends on the type of credentials, but mostly includes the human-friendly name. In addition, checking the status is reliable: 0 for success, 255 for failure.| how.wtf
Splitting a list into evenly sized chunks or batches can be accomplished using Python. Using itertools.batched In Python 3.12, itertools.batched is available for consumption. 1import itertools 2 3lst = ['mary', 'sam', 'joseph'] 4print(list(itertools.batched(lst, 2))) # [('mary', 'sam'), ('joseph')]Using yield If the version of Python is lower than 3.12, the yield keyword may be used. 1def chunks(lst, n): 2 for i in range(0, len(lst), n): 3 yield lst[i:i + n] 4 5lst = ['mary', 'sam', 'joseph']...| how.wtf
After merging changes or working on temporary branches, users may retain a list of orphaned git branches on their local machine. Rather than deleting each branch one-by-one, running a simple script can clear them out. Delete local branches not found on remote The one-liner script to delete local branches not found one remote: 1git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do g...| how.wtf
In Python, an iterable may be traversed using a simple for in loop: 1names = ["sally", "mary", "billy"] 2for n in names: 3 print(n)However, what if the current index is desired? Access the index in a Python for loop The recommended pythonic solution is to use the built-in enumerate function. 1names = ["sally", "mary", "billy"] 2for i, n in enumerate(names): 3 print(i, n)Output: 10 sally 21 mary 32 billyFor contrast, the unidiomatic way to access the current index is by using range:| how.wtf
Killing all processes that match a certain substring or name is easy using pkill. Killing all processes using pkill pkill is a native Linux tool that provides command line access for killing or reloading processes. To kill all processes that match a pattern: 1pkill -f firefoxIf pkill is not successful, trying adding the -9 flag. 1pkill -9 -f firefox| how.wtf
Asking for user confirmation in a bash script is easy! Yes/No prompt with default no If the default is No for an invalid character or space, simply check if the prompt answer is y. Using read, user input can be limited to a single character using the -n 1 parameter. 1read -r -p "Are you sure? [y/N]" -n 1 2echo # (optional) move to a new line 3if [[ "$REPLY" =~ ^[Yy]$ ]]; then 4 echo "Operation continues" 5fiSimilarly, a case statement may be used:| how.wtf
bash 4.0 natively supports creating hash tables / maps / dictionaries. Using maps in Bash Variables in bash are loosely typed; however, declaring provides a type-like behavior. One of the supported types in bash is an associative array (-A). To declare an associative array: 1declare -A scoresTo declare and instantiate in a single line: 1declare -A scores=( ["peter"]=95 ["sally"]=98 )Add key/value pairs Adding key/value pairs to an existing associate array:| how.wtf
bash 5.0 natively supports retrieving seconds since Epoch using a variable. Native bash method 1echo $EPOCHSECONDS # 1682740449If seconds are preferred: 1echo $EPOCHREALTIME # 1682740449.252199If milliseconds are preferred: 1echo $(( ${EPOCHREALTIME/./} / 1000 )) # 1682740449252Using date In addition to using the native bash method, date may be used. According to the date manual: %s seconds since the Epoch (1970-01-01 00:00 UTC) 1echo $(date +%s) # 1682740326If milliseconds seconds are prefer...| how.wtf
Bash does not natively support a try/catch syntax for errors; however, there are options for handling errors within a script. Try and Catch errors in Bash For the first technique, simply detect if there’s a non-zero status by using the following syntax: 1if ! command_call; then 2 echo "command_call did not complete successfully" 3fiIn addition, an || (or) expression may be used instead: 1command_call || echo "command_call did not complete successfully"For explicitness, the full syntax for a...| how.wtf
Hosting a free PostgreSQL database for side projects, MVPs, learning, etc. is easy with free tiers provided by following services in this article. The following list is not comprehensive nor in any specific order. Top free PostgreSQL database hosting online Here are the top free PostgreSQL database online hosting services: Neon Neon provides a generous free tier for a fully managed PostgreSQL database. Its product offerings focus on the modern development experience: serverless architecture, ...| how.wtf
Searching for a specific text or pattern across multiple files can be completed using grep. Using grep to search file contents grep provides searching functionality out-of-the-box: 1grep -rnw '/path/to/directory' -e 'pattern'Here are a breakdown of the grep options: -r for recursiveness -n for line number -w for matching the whole word -e is the pattern used for searching The command will output the file names and text containing the pattern. If the goal is simply to display the filenames the...| how.wtf
If a user wants to write jq output to a file, the solution is simple. jq output to a file Extract all project ids from the following json payload to a text file: 1{ 2 "projects": [ 3 { 4 "id": 1 5 }, 6 { 7 "id": 2 8 } 9 ] 10} 1echo '{"projects": [{"id": 1}, {"id": 2}]}' | jq '.projects[].id' > ids.txtOutput: ids.txt: 11 22Using the redirection operator (>), the jq results are written to a file.| how.wtf
What is the equivalent for the Python string title method in JavaScript? Python’s native str.title() method returns a string with title-casing— i.e. the first letter of each word is capitalized. str.title() method in JavaScript Unfortunately, JavaScript does not contain a standardized str.title() method; however, a function can be created that implements equivalent behavior. In Python, here are a few examples of the str.title() method: 1print("WeIrD cAsInG".title()) # Weird Casing 2print(...| how.wtf