<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: An Vo</title>
    <description>The latest articles on Forem by An Vo (@anvo0000).</description>
    <link>https://forem.com/anvo0000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2679940%2Fcbd74269-2fe5-40c4-96ae-15d849dca062.png</url>
      <title>Forem: An Vo</title>
      <link>https://forem.com/anvo0000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anvo0000"/>
    <language>en</language>
    <item>
      <title>be-confused.com</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Sun, 12 Apr 2026 14:13:56 +0000</pubDate>
      <link>https://forem.com/anvo0000/be-confusedcom-59l8</link>
      <guid>https://forem.com/anvo0000/be-confusedcom-59l8</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for the &lt;a href="https://dev.to/challenges/aprilfools-2026"&gt;DEV April Fools Challenge&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;I built &lt;code&gt;be-confused.com&lt;/code&gt;, a deliberately useless local web app powered by &lt;code&gt;Teapot Intelligence&lt;/code&gt; and fully compliant with &lt;code&gt;HTTP 418&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At first glance, it looks like a polished chat product. In reality, it is a teapot-themed nonsense machine called &lt;code&gt;Teabot 418&lt;/code&gt; that confidently delivers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;surreal breaking news&lt;/li&gt;
&lt;li&gt;absurd quizzes with ceremonial grading&lt;/li&gt;
&lt;li&gt;fake serious explanations of &lt;code&gt;418 I'm a teapot&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;harmless nonsense about capybaras, turtles, crocodiles, kettles, and beverage protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The whole joke is that it has the interface and confidence of a serious software product, but none of the usefulness. Sometimes it even has a tiny tea hiccup in the middle of a reply, says something ridiculous like it briefly became a pancake wizard, apologizes, and then continues as if nothing happened.&lt;/p&gt;

&lt;p&gt;I wanted it to feel like an internet protocol joke that accidentally became a startup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://youtu.be/Jo9CcmoHSkc" rel="noopener noreferrer"&gt;https://youtu.be/Jo9CcmoHSkc&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/anvo0000/April-Fools-Challenge" rel="noopener noreferrer"&gt;https://github.com/anvo0000/April-Fools-Challenge&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How I Built It
&lt;/h2&gt;

&lt;p&gt;I built this with a very small stack on purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Python&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FastAPI&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Jinja2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;vanilla &lt;code&gt;HTML&lt;/code&gt;, &lt;code&gt;CSS&lt;/code&gt;, and &lt;code&gt;JavaScript&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The app has a single page and a single chat endpoint. On the backend, I used lightweight keyword routing to classify prompts into modes like news, quiz, teapot refusal, info, or general confusion. Responses are generated from template banks with randomized phrases, so the app feels playful and a little unpredictable without needing a real model.&lt;/p&gt;

&lt;p&gt;On the frontend, I added fake streaming text, fake model labels, a polished teapot-themed interface, and quick prompt cards so it feels like a serious &lt;code&gt;Teapot Intelligence&lt;/code&gt; product for about three seconds before the nonsense begins.&lt;/p&gt;

&lt;p&gt;One of my favorite additions was the tea hiccup flow. Occasionally the bot interrupts itself with a harmless absurd sentence, apologizes, and goes back to the original answer. That tiny glitch made the experience feel much sillier and more alive.&lt;/p&gt;

&lt;p&gt;This project was intentionally scoped to stay local, simple, and fast to build while still feeling polished enough to sell the joke.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prize Category
&lt;/h2&gt;

&lt;p&gt;I am submitting this for &lt;strong&gt;Best Ode to Larry Masinter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The core concept is built around the &lt;code&gt;HTCPCP&lt;/code&gt; joke and the legacy of &lt;code&gt;418 I'm a teapot&lt;/code&gt;. Instead of using the teapot reference as a one-line easter egg, I made it the entire product identity. The chatbot is literally framed as a beverage-protocol-compliant assistant that refuses coffee requests, explains &lt;code&gt;418&lt;/code&gt; with fake authority, and turns protocol absurdity into the whole user experience.&lt;/p&gt;

&lt;p&gt;This project is basically my attempt to take an internet joke RFC far too seriously and turn it into a useless product demo.&lt;/p&gt;

</description>
      <category>devchallenge</category>
      <category>418challenge</category>
      <category>showdev</category>
    </item>
    <item>
      <title>GenAI #2: What are Vector Databases?</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Tue, 27 Jan 2026 15:39:21 +0000</pubDate>
      <link>https://forem.com/anvo0000/genai-2-what-are-vector-databases-27n3</link>
      <guid>https://forem.com/anvo0000/genai-2-what-are-vector-databases-27n3</guid>
      <description>&lt;h2&gt;
  
  
  What is a Vector?
&lt;/h2&gt;

&lt;p&gt;A vector is a list of numbers that represents something (text, image, audio, user, product, etc.) in a way computers can compare.&lt;/p&gt;

&lt;p&gt;Think of a vector as a “numerical fingerprint.”&lt;br&gt;
A vector might look like: &lt;code&gt;[0.48, -0.87, 1.04, 0.33, ...]&lt;/code&gt;&lt;br&gt;
Each number captures some aspect of meaning or features.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why vectors matter in GenAI
&lt;/h2&gt;

&lt;p&gt;AI models can’t directly understand words or images — they understand numbers. So we convert things into vectors using embedding models.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;"I love dogs" → vector A&lt;/p&gt;

&lt;p&gt;"I like puppies" → vector B&lt;/p&gt;

&lt;p&gt;Those vectors will be close together in vector space because they mean similar things.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are Vector Databases?
&lt;/h2&gt;

&lt;p&gt;A vector database stores vectors and lets you quickly find the most similar ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why regular databases aren’t enough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Traditional databases are great for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Exact matches &lt;code&gt;(id = 123)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filters &lt;code&gt;(price &amp;gt; 50)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But GenAI needs:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;“Find text similar to this”&lt;/p&gt;

&lt;p&gt;“Find documents that mean the same thing”&lt;/p&gt;

&lt;p&gt;“Find images that look alike”&lt;/p&gt;

&lt;p&gt;That’s where vector databases come in.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does a Vector Database do?
&lt;/h2&gt;

&lt;p&gt;A vector database:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Stores vectors (embeddings)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Indexes them efficiently&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performs similarity search (usually cosine similarity or distance)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Typical GenAI Workflow (RAG-style)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You start with data such as PDFs, notes, system logs, customer support chat logs, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These documents are split into smaller chunks and converted into vectors (embeddings) using an embedding model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The vectors are stored in a Vector Database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A user asks a question.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The question is converted into a query vector using the same embedding model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Vector Database performs a similarity search to find the vectors (document chunks) most relevant to the query vector.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The retrieved text chunks are fed into an LLM as context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The LLM uses this context to understand, summarize, and synthesize the information—following the system prompt—and returns a natural-language response to the end user.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Popular Vector Databases:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pinecone&lt;/li&gt;
&lt;li&gt;Weaviate&lt;/li&gt;
&lt;li&gt;Milvus&lt;/li&gt;
&lt;li&gt;Chroma&lt;/li&gt;
&lt;li&gt;FAISS (library, not a DB)&lt;/li&gt;
&lt;li&gt;PostgreSQL + pgvector (Relational database with vector support)&lt;/li&gt;
&lt;li&gt;Qdrant&lt;/li&gt;
&lt;li&gt;AWS: 

&lt;ul&gt;
&lt;li&gt;Individual Vector DB:  Amazon Kendra, OpenSearch Service, and RDS for PostgreSQL with pgvector.&lt;/li&gt;
&lt;li&gt;Managed service via Amazon Bedrock Knowledge Bases: Aurora PostgreSQL, Neptune Analytics, OpenSearch Serverless, Pinecone, and Redis Enterprise Cloud.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Vector Index Optimization:
&lt;/h2&gt;

&lt;p&gt;In a Vector Store, there can be millions or even billions of vectors.&lt;br&gt;
To retrieve relevant information efficiently, these vectors cannot be stored as a simple flat list, which would require brute-force comparison against every vector.&lt;/p&gt;

&lt;p&gt;Vector index optimization refers to selecting and configuring specialized algorithms that organize vectors into efficient data structures, enabling fast approximate similarity search while maintaining acceptable accuracy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brute-Force Comparison:&lt;/strong&gt; means comparing a query vector against every single vector in the vector store to find the most similar ones. It's guaranteeing accuracy but killing performance at scale.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assume:

- You have 1,000,000 vectors
- Each vector has 1,536 dimensions (typical OpenAI embedding size)
- When a user asks a question:
    - Convert the question into a vector
    - Compute similarity between the question vector and:
Vector 1
Vector 2
Vector 3
…
Vector 1,000,000

    - Sort the results
    - Return the top-K most similar vectors

That’s brute force.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hierarchical Navigable Small World (HNSW)&lt;/strong&gt;: It builds a graph structure connecting similar vectors. High-speed (low latency and high recall), consumes RAM, and is the best fit for RAG chatbots with end users.&lt;br&gt;
Below is an intuitive sample:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Layer 3 (top, very sparse)
    o -------- o
       \
        o

Layer 2
  o ---- o ---- o
    \      \
     o      o

Layer 1
 o -- o -- o -- o
 |    |    |    |
 o -- o -- o -- o

- Each `o` is a vector (a data point)
- Lines are connections to nearby vectors
- Higher layers have fewer nodes and longer jumps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Step 1: Start at the top layer&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Begin from a random entry point&lt;/li&gt;
&lt;li&gt;This layer helps you make big jumps across the space&lt;/li&gt;
&lt;li&gt;Goal: get close to the target region
Like using Google Maps zoomed out at the continent level&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Step 2: Move down layer by layer&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At each layer:&lt;/li&gt;
&lt;li&gt;Look at neighboring nodes&lt;/li&gt;
&lt;li&gt;Move to the neighbor that’s closer to your query&lt;/li&gt;
&lt;li&gt;Repeat until you can’t get closer&lt;/li&gt;
&lt;li&gt;Drop down to the next layer
Like zooming from continent → country → city → street&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Step 3: Final search at the bottom layer&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bottom layer is dense&lt;/li&gt;
&lt;li&gt;You now explore the local neighborhood&lt;/li&gt;
&lt;li&gt;Retrieve the nearest vectors&lt;/li&gt;
&lt;li&gt;This is where accuracy comes from.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Inverted File Index (IVF)&lt;/strong&gt;: IVF does not create paths or layers like HNSW. Instead, it:&lt;br&gt;
    Partitions space into regions&lt;br&gt;
    Stores vectors in buckets&lt;br&gt;
    Searches only the most relevant buckets&lt;br&gt;
Imagine the entire vector space as a world map:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-------------------+-------------------+
|     Region A      |     Region B      |
|   (Cluster C1)    |   (Cluster C2)    |
|   ● ● ● ● ●       |   ● ● ● ●         |
|                   |                   |
+-------------------+-------------------+
|     Region C      |     Region D      |
|   (Cluster C3)    |   (Cluster C4)    |
|   ● ● ●           |   ● ● ● ● ● ●     |
+-------------------+-------------------+
- Each region is a cluster
- Each ● is a vector (document chunk)
- Each region has a centroid (not shown yet)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Step 1: Zoom In to the Centroids (Region Representatives)&lt;br&gt;
Now imagine flags planted at the center of each region:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      🚩C1                🚩C2

      ● ● ● ●            ● ● ●

      🚩C3                🚩C4

      ● ● ●              ● ● ● ● ●
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These flags (centroids) act like: “If your query is close to me, search my region.”&lt;/p&gt;

&lt;p&gt;** Step 2: A user question becomes a vector&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          🔎 Query
              ●
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Step 3: Compare ONLY with centroids&lt;br&gt;
Instead of checking every ●, you check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Distance(Query, 🚩C1)
Distance(Query, 🚩C2)
Distance(Query, 🚩C3)
Distance(Query, 🚩C4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is cheap and fast.&lt;/p&gt;

&lt;p&gt;** Step 4: Pick nearest regions (nprobe)&lt;br&gt;
Say the closest centroids are C2 and C1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Search these regions only:
✅ Region B (C2)
✅ Region A (C1)

Ignore:
❌ Region C
❌ Region D

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Step 5: Search inside selected buckets&lt;br&gt;
Now you finally compare the query with vectors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Region B: ● ● ● ●
Region A: ● ● ● ● ●
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But only those — not the entire world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Curious Why It’s Called “Inverted File”??&lt;/strong&gt;&lt;br&gt;
Instead of: “Which cluster does this vector belong to?”&lt;br&gt;
You store: “Here are all vectors belonging to this cluster.”&lt;br&gt;
==&amp;gt; That’s the inversion.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This series will build toward the goal of preparing for the AWS Certified Generative AI Developer – Professional (AIP-C01) certification &lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/aws-certification/latest/examguides/ai-professional-01.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/aws-certification/latest/examguides/ai-professional-01.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Ref:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/choosing-an-aws-vector-database-for-rag-use-cases/introduction.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/choosing-an-aws-vector-database-for-rag-use-cases/introduction.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>database</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>GenAI #1: What Is AI?</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Mon, 05 Jan 2026 16:20:54 +0000</pubDate>
      <link>https://forem.com/anvo0000/ai-learning-journey-1-what-is-ai-4af5</link>
      <guid>https://forem.com/anvo0000/ai-learning-journey-1-what-is-ai-4af5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;AI often feels mysterious, but it starts with a simple idea: machines learning from data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is AI?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Humans learn from experiences, and AI learns by processing vast amounts of data to make informed decisions.&lt;/li&gt;
&lt;li&gt;Data is indispensable for AI as it aids in learning, pattern recognition, and making informed decisions or predictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Foundational AI Concepts and Terminology
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Artificial Intelligence (AI):&lt;/strong&gt; The field of building machines that can make decisions or perform tasks in ways that resemble human intelligence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Machine Learning (ML):&lt;/strong&gt; a subset of AI, uses data to train the computer. The computer will learn and improve over time.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Supervised Learning:&lt;/strong&gt; A learning process where models are trained on labeled data (data with correct answers). For example, a model can learn to classify cats and dogs after being trained on labeled images.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssntsuk0ovcvrngwyuyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssntsuk0ovcvrngwyuyw.png" alt="Supervised Learning" width="452" height="633"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unsupervised Learning:&lt;/strong&gt; is the process that uses big data without labels to train the model, it will learn and find patterns by itself. For example, Unsupervised methods can analyze physiological and behavioral data to identify mental health conditions, especially when labeled data is in short supply. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reinforcement Learning:&lt;/strong&gt; if the model returns a correct answer, then send a reward to it. If the answer is incorrect, it receives a penalty.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deep Learning&lt;/strong&gt; A subset of machine learning based on neural networks. Common architectures include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Artificial Neural Networks&lt;/strong&gt; - Inspired by the human brain, they can process massive datasets and learn patterns from them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convolutional Neural Networks (CNNs):&lt;/strong&gt; used in vision tasks, can extract context from images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recurrent Neural Networks (RNNs):&lt;/strong&gt; Efficient for sequential data and are often used for predicting text. RNNs are adept at handling time-series predictions in stock prices.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Generative Models:&lt;/strong&gt; Models that learn from existing data and generate new, similar data samples, making them useful for creating content like text, images, or music.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transformer Architecture:&lt;/strong&gt; Consisting of encoders and decoders, crucial in language translation and code generation tasks.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flxau64gq6xien9trr0pn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flxau64gq6xien9trr0pn.png" alt="Transformer Architecture" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post lays the foundation for my AI learning journey. In the next post, I’ll dive deeper into &lt;strong&gt;GenAI #2: What are Vector Database?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This series will build toward the goal of preparing for AWS Certified Generative AI Developer – Professional (AIP-C01) certification&lt;br&gt;&lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/aws-certification/latest/examguides/ai-professional-01.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/aws-certification/latest/examguides/ai-professional-01.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>genai</category>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Conda Environment Setup Guide for MacOS and Pycharm</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Sun, 25 May 2025 07:12:57 +0000</pubDate>
      <link>https://forem.com/anvo0000/conda-environment-setup-guide-for-macos-and-pycharm-3k22</link>
      <guid>https://forem.com/anvo0000/conda-environment-setup-guide-for-macos-and-pycharm-3k22</guid>
      <description>&lt;h2&gt;
  
  
  Conda Environment Setup Guide
&lt;/h2&gt;

&lt;p&gt;This guide helps you set up and use a Conda environment named &lt;code&gt;myenv3.12&lt;/code&gt; with Python 3.12.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Install Miniconda
&lt;/h2&gt;

&lt;p&gt;Download the Miniconda installer for your platform from:&lt;br&gt;&lt;br&gt;
&lt;a href="https://docs.conda.io/en/latest/miniconda.html" rel="noopener noreferrer"&gt;https://docs.conda.io/en/latest/miniconda.html&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Install via Terminal (macOS, Apple Silicon example):
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-O&lt;/span&gt; https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash Miniconda3-latest-MacOSX-arm64.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Follow the prompts and restart your terminal after installation.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Create a New Environment
&lt;/h2&gt;

&lt;p&gt;Create an environment named &lt;code&gt;myenv3.12&lt;/code&gt; with Python 3.12:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda create &lt;span class="nt"&gt;-n&lt;/span&gt; myenv3.12 &lt;span class="nv"&gt;python&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Activate the Environment
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda activate myenv3.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4. Install Packages
&lt;/h2&gt;

&lt;p&gt;Example: Install numpy and pandas&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda &lt;span class="nb"&gt;install &lt;/span&gt;numpy pandas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5. List Your Environments
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda &lt;span class="nb"&gt;env &lt;/span&gt;list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  6. Deactivate the Environment
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda deactivate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  7. Remove the Environment (if needed)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda &lt;span class="nb"&gt;env &lt;/span&gt;remove &lt;span class="nt"&gt;-n&lt;/span&gt; myenv3.12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  8. Useful Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always activate your environment before working on your project.&lt;/li&gt;
&lt;li&gt;Install packages only after activating the desired environment.&lt;/li&gt;
&lt;li&gt;To update conda itself:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  conda update conda
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  9. List out packages in a specific environment
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda activate myenv3.12
conda list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  10. Using Conda Environment in PyCharm
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In PyCharm, go to &lt;strong&gt;Preferences &amp;gt; Project &amp;gt; Python Interpreter&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add Interpreter&lt;/strong&gt; &amp;gt; &lt;strong&gt;Conda Environment&lt;/strong&gt; &amp;gt; &lt;strong&gt;Existing environment&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Browse to:
&lt;code&gt;~/miniconda3/envs/myenv3.12/bin/python&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt; to set it as your project interpreter.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  11. Create or Update the current environment with an environment.yml file
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myenv3.12&lt;/span&gt;
&lt;span class="na"&gt;dependencies&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;python=3.12&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;pandas&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;numpy&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;matplotlib&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;seaborn&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;scikit-learn&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;tensorflow&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conda env create -f backend/environment.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;conda &lt;span class="nb"&gt;env &lt;/span&gt;update &lt;span class="nt"&gt;--file&lt;/span&gt; environment.yml &lt;span class="nt"&gt;--prune&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Happy coding anvo0000!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python's walrus operator ' := '</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Sat, 12 Apr 2025 08:01:47 +0000</pubDate>
      <link>https://forem.com/anvo0000/pythons-walrus-operator--1chp</link>
      <guid>https://forem.com/anvo0000/pythons-walrus-operator--1chp</guid>
      <description>&lt;p&gt;I have just found that Python has a new operator named "Walrus", it's so interesting to deep dive and worth writing down:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Codeblock Description:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prompts the user to enter 0, 1, or 2.&lt;/li&gt;
&lt;li&gt;If the user enters something else, it keeps asking.&lt;/li&gt;
&lt;li&gt;As soon as the input is valid, the loop ends.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The original code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;list_embedding_models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0-Ollama-Nomic Embedded&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1-OpenAI Embedding&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2-Chroma DefaultEmbeddingFunction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;====Select an Embedding model:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;list_embedding_models&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;embedding_model_choice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please enter 0,1, or 2: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;embedding_model_choice&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Try again!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can apply Walrus Operator ':='&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;list_embedding_models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0-Ollama-Nomic Embedded&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1-OpenAI Embedding&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2-Chroma DefaultEmbeddingFunction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;====Select an Embedding model:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;list_embedding_models&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding_model_choice&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please enter 0,1, or 2: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Try again!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding_model_choice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;==&amp;gt; User choose &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;list_embedding_models&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding_model_choice&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's happening?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;embedding_model_choice := input(...)&lt;/code&gt; this assigns the result of &lt;code&gt;input()&lt;/code&gt; to embedding_model_choice, inside the condition of the &lt;code&gt;while&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;not in {"0", "1", "2"}&lt;/code&gt;: then checks whether the user's input is one of the allowed choices. If not, it loops again.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;print("Try again!")&lt;/code&gt;: if the user's input is invalid, it will print a message Try again! and ask for input again. In this step, you can either print a message or &lt;code&gt;pass&lt;/code&gt; it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding_model_choice&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please enter 0,1, or 2: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner, shorter loops when you want to assign and check a value in the same place.&lt;/li&gt;
&lt;li&gt;Useful in input loops, file reading, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Risk:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The walrus operator is a new syntax that is available from Python 3.8 and later.&lt;/li&gt;
&lt;li&gt;If your code needs to support legacy systems, then it will be broken, so it's better to use the classic &lt;code&gt;while ... if ... break&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Python Decorator</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Sun, 23 Mar 2025 09:39:51 +0000</pubDate>
      <link>https://forem.com/anvo0000/python-decorator-1d42</link>
      <guid>https://forem.com/anvo0000/python-decorator-1d42</guid>
      <description>&lt;h3&gt;
  
  
  SIMPLE DECORATOR SYNTAX
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1- Define a decorator
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decorator_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="c1"&gt;# Do something before the function
&lt;/span&gt;        &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="c1"&gt;# Do something after the function
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper_function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Python Decorator Function (4 lines of code)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;1- Create a normal function.&lt;br&gt;
2- Input is another normal function, the function in this case doesn't have parentheses ().&lt;br&gt;
3- Inside the decorator_function, create a wrapper_function&lt;br&gt;
   The wrapper_function() will trigger the actual input "function()" that was passed into the decorator_function().&lt;br&gt;
4- Return the wrapper_function without parentheses ().&lt;/p&gt;
&lt;h5&gt;
  
  
  Key takeaways:
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  + Decorator function is just a function that wraps another function,
  + And GIVE that function some ADDITIONAL FUNCTIONALITY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  2.1 - Using a decorator function by @ sign -&amp;gt; RECOMMENDED way.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@decorator_function&lt;/span&gt; &lt;span class="c1"&gt;#this line will trigger the decorator function for a_normal_function
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;a_normal_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2.2 - Using a decorator function by input function -&amp;gt; NOT RECOMMENDED way.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;decorated_func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decorator_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a_normal_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;decorated_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# remember to add parentheses ()
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  EXAMPLES
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Requirements:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;All functions should be delayed by 5 seconds.&lt;/li&gt;
&lt;li&gt;Avoid adding &lt;code&gt;time.sleep(5)&lt;/code&gt; to each function.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;time.sleep(5)&lt;/code&gt; once.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Implementation:
&lt;/h4&gt;

&lt;p&gt;We can modify delay_decorator to delay the execution of the wrapped function by 5 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delay_decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# wait 5 seconds before executing the next function
&lt;/span&gt;        &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper_function&lt;/span&gt;


&lt;span class="nd"&gt;@delay_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@delay_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_bye&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GoodBye!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_greeting&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How are you?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will define a main_flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main_flow&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# no delay because we don't add @delay_decorator to it
&lt;/span&gt;    &lt;span class="nf"&gt;say_greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# delay 5s before executing greeting
&lt;/span&gt;    &lt;span class="nf"&gt;say_bye&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# delay 5s before executing GoodBye
&lt;/span&gt;
&lt;span class="nf"&gt;main_flow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hello!&lt;br&gt;
&lt;em&gt;wait 5s&lt;/em&gt;&lt;br&gt;
How are you?&lt;br&gt;
&lt;em&gt;wait 5s&lt;/em&gt;&lt;br&gt;
GoodBye!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  ADVANCE DECORATOR SYNTAX
&lt;/h3&gt;

&lt;p&gt;Class and Adv Python Decorator&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Requirements:
&lt;/h4&gt;

&lt;p&gt;Users have to provide their name, and is_logged_in = True&lt;br&gt;
   Create a decorator to ensure users log in by using the "is_authenticated_decorator()"&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Define a class name User:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_logged_in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2. The function to create a new blog post:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_blog_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s new blog post.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  3. Define a decorator:
&lt;/h4&gt;

&lt;p&gt;To check whether users log in or not&lt;br&gt;
Remember the create_blog_post function requires a param name user.&lt;br&gt;
With Decorator, we cannot send param directly, in this case, we can use *args and **kwars.&lt;br&gt;
args[0] means the first argument input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_authenticated_decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_logged_in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please log in!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Main Flow:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# If is_logged_in = True, a new blog post will be created.
&lt;/span&gt;&lt;span class="n"&gt;new_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Anvo0000&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;new_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_logged_in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="nf"&gt;create_blog_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# --&amp;gt; This is Anvo0000's new blog post.
&lt;/span&gt;
&lt;span class="c1"&gt;# If no log-in:
&lt;/span&gt;&lt;span class="n"&gt;new_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_logged_in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="nf"&gt;create_blog_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# --&amp;gt; Please log in!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  P/S: if the function has a return value, remember to return that result in a wrapper.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logging_decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You called &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="si"&gt;}{&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;It returned: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="c1"&gt;# IMPORTANT: remember to return a result for the follower function.
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

&lt;span class="nd"&gt;@logging_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sum_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;sum_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>HTTP Requests</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Sat, 22 Mar 2025 09:05:18 +0000</pubDate>
      <link>https://forem.com/anvo0000/http-requests-adl</link>
      <guid>https://forem.com/anvo0000/http-requests-adl</guid>
      <description>&lt;p&gt;HTTP Requests have some common request types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;/GET: request.get(): &lt;em&gt;is used to retrieve data from a server.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://api.user.org/users/?id=AAA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;/POST: request.post(): &lt;em&gt;is used when sending data to the server to create new resources.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /api/users
Content-Type: application/json
{
  "id": "AAA",
  "name": "John Doe",
  "email": "john@testmail.com"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;/PUT: request.put(): &lt;em&gt;is used to update or replace a resource on the server.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PUT /api/users/AAA
Content-Type: application/json

{
  "name": "AAA Smith",
  "email": "aaa@testmail.com"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;/DELETE: request.delete(): &lt;em&gt;is used to remove or delete a resource on the server.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE /api/users/AAA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sample Python code:
Remember to create a .env file with TOKE, USERNAME, and ENDPOINT.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="n"&gt;PIXELA_TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PIXELA_TOKEN&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;PIXELA_USERNAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PIXELA_USERNAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;PIXELA_ENDPOINT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PIXELA_ENDPOINT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;X-USER-TOKEN&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PIXELA_TOKEN&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;user_param&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;token&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PIXELA_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;username&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PIXELA_USERNAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;agreeTermsOfService&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notMinor&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PIXELA_ENDPOINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;### {"message":"Success. Let's visit  ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>http</category>
    </item>
    <item>
      <title>Python - Small Notes</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Wed, 19 Feb 2025 09:14:16 +0000</pubDate>
      <link>https://forem.com/anvo0000/python-small-notes-ofe</link>
      <guid>https://forem.com/anvo0000/python-small-notes-ofe</guid>
      <description>&lt;ul&gt;
&lt;li&gt;1. Python allows you to change the datatype of a variable.
int -&amp;gt; string -&amp;gt; float -&amp;gt; int
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# int  
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;testing&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;# string
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;2. To prevent the datatype change in the future, apply "Type Hint" when defining a variable:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;# accepted
&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;testing&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;# Python will reject this assigning
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;3. Apply function Arrow to force the return datatype
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;# this only accept return a string.
&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="c1"&gt;# this boolean will be rejected by Python 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's different from other languages such as C#, Java, and Swift.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Python environment.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pipenv
pipenv shell &lt;span class="c"&gt;#create a new environment&lt;/span&gt;
pip i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# create a new virtual environment
python3 -m venv .venv

#activate environment
source .venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Python OOP</title>
      <dc:creator>An Vo</dc:creator>
      <pubDate>Mon, 17 Feb 2025 09:23:08 +0000</pubDate>
      <link>https://forem.com/anvo0000/python-oop-4b80</link>
      <guid>https://forem.com/anvo0000/python-oop-4b80</guid>
      <description>&lt;p&gt;&lt;u&gt;I. What is Python OOP?&lt;/u&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;OOP = Object Oriented Programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Python fully supports OOP with &lt;strong&gt;Class&lt;/strong&gt; and &lt;strong&gt;Class Inheritance&lt;/strong&gt;. A Python class is a blueprint for creating from 1 to many objects. It can have:&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attributes&lt;/strong&gt; are attached to it to maintain its state. They may be read-only or writeable.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Methods&lt;/strong&gt; to modify its state.
Syntax to define a class name "Animal":
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret_attribute&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Suyt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# __init__: constructor that runs when an object is created.
&lt;/span&gt;        &lt;span class="c1"&gt;# self: refers to the current instance of the class
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num_eyes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__secret_attribute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;secret_attribute&lt;/span&gt; &lt;span class="c1"&gt;# Private variable
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;breathe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Inhale, exhale.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_secret_attribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__secret_attribute&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;  &lt;span class="c1"&gt;# To be implemented by subclasses
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;II. What are the core concepts or elements of OOPs?&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class: a blueprint for creating objects &lt;code&gt;class Animal&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Object: an instance of a class. &lt;strong&gt;Instantiation&lt;/strong&gt; is an action that creates an instance from the class &lt;code&gt;nemo_fish = Fish()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encapsulation: restricted direct access to attributes or from the outside &lt;code&gt;self.__secret_attribute&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inheritance: Allows a child class to use methods and properties from parents' classes.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Fish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Fish class is an inheritance from Animal&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# call Animal init
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;breathe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;breathe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;#call the Animal breathe function, then modify it.
&lt;/span&gt;        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;doing this underwater.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;swim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;moving in the water&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Polymorphism&lt;/strong&gt;: allows different classes to use the same method name but different implementations.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Woof!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Meow!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;animals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;  &lt;span class="c1"&gt;# Calls the correct method based on the object type
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Abstraction&lt;/strong&gt;: hiding implementation details and only exposing essential functionalities.
For example: we have many 2 kinds of animals: cats, dogs, and fish. We know they care how they make the sound, only care that they can make a sound.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;abc&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abstractmethod&lt;/span&gt; &lt;span class="c1"&gt;# import abstract base class module
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;# Getting error if instantiate an abstract class
# dog = Animal()
# TypeError: Can't instantiate abstract class Animal
# without an implementation for abstract method 'make_sound'
&lt;/span&gt;
&lt;span class="c1"&gt;# Dog class inherits from Animal so it has to implement method make_sound
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Woof! Woof!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;perfect_dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;perfect_dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# Woof! Woof!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
