<?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>DEV Community: George Brockman</title>
    <description>The latest articles on DEV Community by George Brockman (@georgefbrockman).</description>
    <link>https://dev.to/georgefbrockman</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%2F1191586%2F99452b9c-086d-434f-9545-bf4894f5d495.png</url>
      <title>DEV Community: George Brockman</title>
      <link>https://dev.to/georgefbrockman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/georgefbrockman"/>
    <language>en</language>
    <item>
      <title>An Introduction to Tailwind CSS</title>
      <dc:creator>George Brockman</dc:creator>
      <pubDate>Thu, 15 Aug 2024 07:26:37 +0000</pubDate>
      <link>https://dev.to/georgefbrockman/an-introduction-to-tailwind-css-5fdb</link>
      <guid>https://dev.to/georgefbrockman/an-introduction-to-tailwind-css-5fdb</guid>
      <description>&lt;p&gt;In the ever-evolving landscape of web development, Tailwind CSS has emerged as a game-changer for both seasoned developers and newcomers alike. If you’ve been following trends in CSS frameworks or exploring ways to streamline your design workflow, you might have encountered this powerful tool. But what exactly is Tailwind CSS, and why is it capturing so much attention in the web development community?&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Tailwind CSS?
&lt;/h3&gt;

&lt;p&gt;Tailwind CSS is a utility-first CSS framework that provides a collection of low-level utility classes to help you build custom designs all within your HTML. Unlike traditional CSS frameworks like Bootstrap or Foundation that come with predefined components and styles, Tailwind allows you to style your elements directly in your HTML with a series of utility classes.&lt;/p&gt;

&lt;p&gt;For example, instead of writing custom CSS for a button, you can use Tailwind's utility classes to quickly style it right in the HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"bg-blue-500 text-white font-bold py-2 px-4 rounded"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Click Me
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this snippet, &lt;code&gt;bg-blue-500&lt;/code&gt; sets the background color, &lt;code&gt;text-white&lt;/code&gt; changes the text color, &lt;code&gt;font-bold&lt;/code&gt; makes the text bold, &lt;code&gt;py-2&lt;/code&gt; and &lt;code&gt;px-4&lt;/code&gt; control the padding, and &lt;code&gt;rounded&lt;/code&gt; adds rounded corners. Each of these classes is part of Tailwind’s utility class system, allowing you to compose complex styles from simple, reusable pieces.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Choose Tailwind CSS?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Utility-First Approach&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Tailwind’s utility-first approach is a departure from the component-based or semantic styles of traditional CSS frameworks. This method encourages using small, single-purpose classes that are highly composable. This means you can quickly build unique designs by combining these utility classes, rather than being constrained by predefined components.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Customization and Flexibility&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;One of the standout features of Tailwind is its flexibility. The framework is highly customizable through its configuration file (&lt;code&gt;tailwind.config.js&lt;/code&gt;). You can define your own color palette, spacing units, breakpoints, and more. This level of customization ensures that your design remains consistent and tailored to your project’s needs without having to write additional CSS.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;Responsive Design&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Tailwind comes with built-in responsive design support. By utilizing responsive utility variants, you can apply different styles at various breakpoints. This makes it easy to create designs that work across all screen sizes without needing to write media queries manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"p-4 sm:p-6 lg:p-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Content here --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the padding will adjust according to the screen size, with different values for small (&lt;code&gt;sm&lt;/code&gt;), medium (&lt;code&gt;md&lt;/code&gt;), and large (&lt;code&gt;lg&lt;/code&gt;) screens.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Rapid Prototyping&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Tailwind’s utility classes facilitate rapid prototyping. Because you can quickly apply and adjust styles directly in your HTML, you can iterate on designs faster. This immediate feedback loop helps in refining and finalizing designs efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does Tailwind CSS Work?
&lt;/h3&gt;

&lt;p&gt;Tailwind CSS works by using a build process that generates a CSS file containing all the utility classes you need. When you use Tailwind, you typically set up a build tool like PostCSS to process your CSS and produce a final output file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started with Tailwind CSS
&lt;/h3&gt;

&lt;p&gt;Here’s a basic guide to get you started with Tailwind CSS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Install Tailwind CSS:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;tailwindcss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   yarn add tailwindcss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Create Your Configuration File:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Generate a configuration file using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npx tailwindcss init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Configure Your CSS:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create a CSS file (e.g., &lt;code&gt;styles.css&lt;/code&gt;) and include Tailwind’s directives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Build Your CSS:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the build process to generate your final CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npx tailwindcss build styles.css &lt;span class="nt"&gt;-o&lt;/span&gt; output.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Link Your CSS:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Link the generated &lt;code&gt;output.css&lt;/code&gt; file in your HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"output.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Tailwind CSS offers a refreshing take on web design by providing a utility-first approach that simplifies the process of styling and customizing web pages. Its flexibility, ease of use, and powerful features make it a compelling choice for modern web development. Whether you’re building a new project or looking to streamline your workflow, Tailwind CSS is worth exploring for its potential to enhance both your design process and end results.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SQLAlchemy Constraints and Validations</title>
      <dc:creator>George Brockman</dc:creator>
      <pubDate>Mon, 22 Jul 2024 22:50:21 +0000</pubDate>
      <link>https://dev.to/georgefbrockman/sqlalchemy-constraints-and-validations-4dgn</link>
      <guid>https://dev.to/georgefbrockman/sqlalchemy-constraints-and-validations-4dgn</guid>
      <description>&lt;p&gt;When building robust applications that interact with databases, ensuring data integrity is paramount. SQLAlchemy, a powerful Python SQL toolkit and Object-Relational Mapping (ORM) library, provides a straightforward yet flexible way to manage databases through the use of constraints and validations directly from your Python code. In this guide, we'll explore how to leverage SQLAlchemy's capabilities for constraints and validations effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Constraints
&lt;/h3&gt;

&lt;p&gt;Constraints in a database enforce rules regarding the data that can be inserted, updated, or deleted in tables. SQLAlchemy allows us to define these constraints both at the database schema level and within our application code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Database-Level Constraints:
&lt;/h3&gt;

&lt;p&gt;SQLAlchemy supports the full spectrum of database constraints such as &lt;code&gt;NOT NULL&lt;/code&gt;, &lt;code&gt;UNIQUE&lt;/code&gt;, &lt;code&gt;PRIMARY KEY&lt;/code&gt;, &lt;code&gt;FOREIGN KEY&lt;/code&gt;, &lt;code&gt;CHECK&lt;/code&gt;, and &lt;code&gt;DEFAULT&lt;/code&gt;. These constraints are defined when creating the table schema using SQLAlchemy's declarative syntax.&lt;/p&gt;

&lt;p&gt;Let's start with an example database in which we have a User class and an associated Address class. In this case, we're going to set up constraints that require incoming data to follow certain rules. In our User model, the &lt;code&gt;username&lt;/code&gt; has to not be &lt;code&gt;null&lt;/code&gt; and must be a unique value, the &lt;code&gt;email&lt;/code&gt; must also not be &lt;code&gt;null&lt;/code&gt;, and the &lt;code&gt;address_id&lt;/code&gt; has to be a &lt;code&gt;Foreign Key&lt;/code&gt;. Meanwhile, our Address model is going to require that &lt;code&gt;street&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt; be not &lt;code&gt;null&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(metadata=metadata)

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, nullable=False, unique=True)
    email = Column(db.String, nullable=False)

    address_id = Column(db.Integer, ForeignKey('addresses.id'))

    address = db.relationship("Address", back_populates="users")

class Address(db.Model):
    __tablename__ = 'addresses'

    id = db.Column(db.Integer, primary_key=True)
    street = db.Column(db.String, nullable=False)
    city = db.Column(db.String, nullable=False)

    users = relationship("User", back_populates="address")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nullable=False&lt;/code&gt; ensures fields are required (NOT NULL).&lt;br&gt;
&lt;code&gt;unique=True&lt;/code&gt; enforces uniqueness (UNIQUE constraint).&lt;br&gt;
&lt;code&gt;ForeignKey&lt;/code&gt; establishes relationships (FOREIGN KEY constraint).&lt;/p&gt;
&lt;h3&gt;
  
  
  Application-Level Constraints:
&lt;/h3&gt;

&lt;p&gt;While database constraints are crucial, certain situations demand validation beyond what the database can enforce. SQLAlchemy allows us to define custom validators using Python's native capabilities or third-party libraries, enhancing flexibility in data validation.&lt;/p&gt;

&lt;p&gt;Just like our constraints, validation ensures that data entering the database meets specific criteria, preventing erroneous or malicious data from compromising the integrity of our application.&lt;/p&gt;

&lt;p&gt;For our next example, we're going to add a validation method to our User model that checks whether or not the &lt;code&gt;email&lt;/code&gt; provided is actually a valid email address. Just for simplicity's sake, we're just going to check if &lt;code&gt;email&lt;/code&gt; has an @ symbol in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy.orm import validates

class User(db.Model):
    # ... (previous code)

    @validates('email')
    def validate_email(self, key, address):
        if '@' not in address:
            raise ValueError("Failed simple email validation")
        return address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SQLAlchemy's ORM layer supports validation through @validates and @validates_schema decorators. These decorators validate individual attributes or the object as a whole before persistence.&lt;/p&gt;

&lt;p&gt;Here, @validates decorator checks the validity of the email format before committing to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;SQLAlchemy empowers developers to manage constraints and validations seamlessly, promoting data integrity and application reliability. By combining database-level constraints with application-level validations, we enforce rules at multiple layers, safeguarding our data against inconsistencies and errors.&lt;/p&gt;

&lt;p&gt;Whether you're building a small-scale application or a large enterprise system, mastering SQLAlchemy's constraint and validation mechanisms is essential for creating robust, maintainable database-driven applications. Embrace these practices to elevate your data handling capabilities and ensure your applications operate with precision and reliability.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring ORM - Object Relational Mapping</title>
      <dc:creator>George Brockman</dc:creator>
      <pubDate>Sat, 15 Jun 2024 12:48:39 +0000</pubDate>
      <link>https://dev.to/georgefbrockman/exploring-orm-object-relational-mapping-51lm</link>
      <guid>https://dev.to/georgefbrockman/exploring-orm-object-relational-mapping-51lm</guid>
      <description>&lt;p&gt;Databases are an immensely powerful tool for developers, as they allow us to store and access information across multiple uses of a program. Without them, most of the information that we create ends up becoming inaccessible once the application is run and closed. Amongst the information that would otherwise be lost is data related to class instances. Let's say you have an application that displays information about an account at a bank. One of the ways we can represent this account is by using a class that has variables for information such as the name, balance, account number, etc. &lt;/p&gt;

&lt;p&gt;In order to store this data, and eventually retrieve and reassign it to an instance, we will need to implement &lt;strong&gt;Object Relational Mapping&lt;/strong&gt;, or &lt;strong&gt;ORM&lt;/strong&gt; for short. &lt;strong&gt;ORM&lt;/strong&gt; simply refers to the method by which we set up classes to store and retrieve information to some database. For the case of this specific blog post, we will be looking at how to implement &lt;strong&gt;ORM&lt;/strong&gt; using &lt;strong&gt;Python&lt;/strong&gt; and &lt;strong&gt;SQLite&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;First and foremost, we need to do a little bit of setup. We start by installing &lt;strong&gt;sqlite3&lt;/strong&gt; into our workspace. Once that is done, we need to import it into our file and initialize our connection to our database file and a cursor on this connection to execute commands like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sqlite3

CONN = sqlite3.connect(database.db)
CURSOR = CONN.cursor()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  create_table
&lt;/h2&gt;

&lt;p&gt;With that setup done, we can move on to actually creating our mapper. Let’s continue with our earlier example of a Customer class. First, we need to actually create a table to store the data in. We can create a class method that will handle that for us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
def create_table(cls):
    sql = “””
    CREATE IF NOT EXISTS customers (
        id INTEGER PRIMARY KEY , 
        name TEXT, 
        account_number INTEGER,
        balance REAL
        )
    ”””

    CURSOR.execute(sql)
    CONN.commit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this method on our Customer class will give us a table that we can actually store and retrieve data from. &lt;/p&gt;

&lt;h2&gt;
  
  
  delete_table
&lt;/h2&gt;

&lt;p&gt;But before we start doing that, let’s make a method to delete our table in case we want to have a fresh start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
def delete_table(cls)
    sql = “””
        DROP TABLE IF EXISTS customers;
    ”””

    CURSOR.execute(sql)
    CONN.commit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of these class methods above have three main elements, the &lt;strong&gt;SQLite&lt;/strong&gt; code that we want to run, our &lt;strong&gt;CURSOR&lt;/strong&gt; to execute that code, and out &lt;strong&gt;CONN&lt;/strong&gt; to commit the changes to our database.&lt;/p&gt;

&lt;h2&gt;
  
  
  save and delete
&lt;/h2&gt;

&lt;p&gt;Now that we have methods to create and delete our table, let’s create corresponding methods to create and delete table rows for our class attribute. Instead of class methods, these two methods are only meant to be called on an instance of a class, not the class object itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def save(self):
    sql = “””
        INSERT INTO customers (
            name, account_number, balance
        )
        VALUES (?, ?, ?)
    ”””

    CURSOR.execute(sql, (
        self.name, self.account_number, self.balance
    ))
    CONN.commit()

    self.id = CURSOR.lastrowid
    type(self).all[self.id] = self


def delete(self):
    sql = “””
        DELETE FROM customers
        WHERE id = ?
    “””

    CURSOR.execute(sql, (self.id,))
    CONN.commit()

    del type(self).all[self.id]
    self.id = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our save method, we are introducing a few new steps. First are the question marks in &lt;code&gt;sql&lt;/code&gt;. These are there to act as place holders for the data we want to pass through. We pass the information through when we call execute as the second parameter. You may have noticed that there is an extra comma in the delete method after &lt;code&gt;self.id&lt;/code&gt;. This is because the second argument for execute needs to be a &lt;strong&gt;tuple&lt;/strong&gt;, so we have to include the comma even if we’re only passing a single variable.&lt;/p&gt;

&lt;p&gt;After we add our information to the table, we set the &lt;code&gt;id&lt;/code&gt; of our instance equal to its corresponding &lt;code&gt;id&lt;/code&gt; in the table. We use that &lt;code&gt;id&lt;/code&gt; to save our instance to a dictionary object that is a class attribute. Our attribute, called &lt;code&gt;all&lt;/code&gt;, is defined as an empty dictionary at the top of our class and is used to keep track of all existing instances of our class. Without it, we could end up wasting a lot of memory by accidentally creating multiple identical instances. &lt;/p&gt;

&lt;p&gt;We also use &lt;code&gt;id&lt;/code&gt; to find the row in our table that we want to delete. After deleting the info from the table, we need to remove the object from our all dictionary and remove the instance’s id.&lt;/p&gt;

&lt;h2&gt;
  
  
  instance_from_db and find_by_id
&lt;/h2&gt;

&lt;p&gt;Now that we have our methods to manipulate information within the table, the last of our necessary methods are going to handle the retrieval of data from the table and creation of a new instance. Both of these methods are going to be class methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
def instance_from_db(cls, row):
    customer = cls.all.get(row[0])

    if customer:
        customer.name = row[1]
        customer.account_number = row[2]
        customer.balance = row[3]
    else:
        customer = cls(row[1], row[2], row[3])
        customer.id = row[0]
        cls.all[customer.id] = customer

    return customer 


@classmethod
def find_by_id(cls, id):
    sql = “””
        SELECT * FROM customers
        WHERE id = ?
    ”””

    row = CURSOR.execute(sql, (id,)).fetchone()
    return cls.instance_from_db(row) if row else None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break these last two methods down. Starting with &lt;code&gt;instance_from_db&lt;/code&gt;, we take our row provided from &lt;code&gt;find_by_id&lt;/code&gt; and try to find a corresponding instance that already exists in our all dictionary. If one does exist, it takes the information from the table and updates the instance to make sure they match. If it doesn’t exist, it goes ahead and makes and returns a new instance.&lt;/p&gt;

&lt;p&gt;Finally, there’s our &lt;code&gt;find_by_id&lt;/code&gt; method which takes an id as a parameter and returns a customer instance. Most of the code in it should look familiar, but there is one new thing. The &lt;code&gt;fetchone&lt;/code&gt; method at the end takes the executed code and returns a list containing the information from the first row that meets the &lt;code&gt;WHERE&lt;/code&gt; condition.&lt;/p&gt;

&lt;p&gt;We can write as many methods as there are different ways of interacting with the table using &lt;strong&gt;SQLite&lt;/strong&gt;. However, with these methods, you have the bare bones of a basic &lt;strong&gt;ORM&lt;/strong&gt;. Good luck and happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React createContext and useContext Hooks</title>
      <dc:creator>George Brockman</dc:creator>
      <pubDate>Wed, 24 Jan 2024 03:39:47 +0000</pubDate>
      <link>https://dev.to/georgefbrockman/react-createcontext-and-usecontext-hooks-26ah</link>
      <guid>https://dev.to/georgefbrockman/react-createcontext-and-usecontext-hooks-26ah</guid>
      <description>&lt;p&gt;When we want to share data between multiple React components, there are a couple of ways to do it. One of the simplest ways to accomplish this is to pass down data from a parent component to a child component as a prop. This, however, can become remarkably tedious when we want to pass data from a parent component to a grandchild component. We end up passing a prop through an entire component only so that the child of that component can use it. This problem only gets worse when we have code with deeply nested components. Luckily, React provides us with a couple of hooks that allow us to share data between any number of components without having to pass it through as props.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://react.dev/reference/react/createContext" rel="noopener noreferrer"&gt;createContext()&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The first of these hooks is &lt;strong&gt;createContext()&lt;/strong&gt;. To use this hook, we first need to import it, call it, and then assign the return value to a variable. This must be done outside of any component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createContext } from “react”;

const SomeContext = createContext()

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

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;createContext()&lt;/strong&gt; function takes a default value as a parameter, and if none is provided, it will default to null. The React documentation suggests we always pass a parameter through, even if it’s null, however this is not required for the code to work. This default value is almost never used, however, since the data that we want to share is assigned in the Provider component (more on this in just a bit). &lt;strong&gt;createContext()&lt;/strong&gt; returns a context object that does not really hold any information itself. It simply points to a location from which context will be read or provided. In order to provide data to the context object, we use &lt;strong&gt;SomeContext.Provider&lt;/strong&gt;. In our code, it will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;SomeContext.Provider value={”some value”}&amp;gt;{children}&amp;lt;/SomeContext.Provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this bit of code, ‘some value’ is where we assign our data that we want to share amongst components, and ‘children’ is where we place said components. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://react.dev/reference/react/useContext" rel="noopener noreferrer"&gt;useContext()&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now to actually access the data we assigned earlier, we need to use &lt;strong&gt;useContext()&lt;/strong&gt;. Once again, we first need to import the hook and then assign the return value to a variable, but this time we create the variable inside of our component that we want to access the shared data from. Furthermore, we need our &lt;strong&gt;SomeContext&lt;/strong&gt; variable also imported so that we can pass it as a parameter to &lt;strong&gt;useContext()&lt;/strong&gt;. Doing so results in code that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from “react”;
import { SomeContext } from “/somewhere”

function SomeComponent(){
    const information = useContext(SomeContext) 

    return (something);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we pass our &lt;strong&gt;SomeContext&lt;/strong&gt; variable as a parameter to &lt;strong&gt;useContext()&lt;/strong&gt; so that it knows where to grab data from, and it returns the information that we had previously passed to our &lt;strong&gt;&lt;/strong&gt; component in the value prop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Usage Information
&lt;/h2&gt;

&lt;p&gt;When using Context from React, there are certain rules that must be followed. First and foremost, we must remember to only use &lt;strong&gt;useContext()&lt;/strong&gt; in our children components. We can have as many children under the &lt;strong&gt;&lt;/strong&gt; parent, however. Doing so will result in code that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const SomeContext = createContext(null);

function App(){
    return(
        &amp;lt;&amp;gt;
            &amp;lt;header&amp;gt;
                &amp;lt;NavBar /&amp;gt;
            &amp;lt;/header&amp;gt;
            &amp;lt;SomeContext.Provider value={information}&amp;gt;
                &amp;lt;Child1/&amp;gt;
                &amp;lt;Child2/&amp;gt;
                &amp;lt;Child3&amp;gt;
                    &amp;lt;Child4/&amp;gt;
                &amp;lt;/Child3&amp;gt;
            &amp;lt;/SomeContext.Provider&amp;gt;
        &amp;lt;/&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;strong&gt;Child1&lt;/strong&gt;, &lt;strong&gt;Child2&lt;/strong&gt;, &lt;strong&gt;Child3&lt;/strong&gt;, and even &lt;strong&gt;Child4&lt;/strong&gt; can all access whatever value is assigned to ‘information’. Meanwhile, because NavBar is outside of the Provider component, it cannot access said data. Also, only a single variable can be passed as a prop through the context provider, meaning if we want to provide multiple pieces of information, we will have to assign it either to an Array or Object. From there we can destructure the information within our children components.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://react.dev/reference/react/useState" rel="noopener noreferrer"&gt;useState()&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We can combine React Context with the &lt;strong&gt;useState()&lt;/strong&gt; hook not only so that we can access state variables in other components but also allow multiple components to be able to update it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const SomeContext = createContext();

function App(){
    const [info, setInfo] = useState(some data)

    return(
        &amp;lt;SomeContext.Provider value={{ info, setInfo }}&amp;gt;
            &amp;lt;Child1/&amp;gt;
            &amp;lt;Child2/&amp;gt;
        &amp;lt;/SomeContext.Provider&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are able to call &lt;strong&gt;setInfo()&lt;/strong&gt; in both of the Child components, and doing so will cause the App and subsequent children components to be rerendered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Luckily, we have Context available to us through React to make our lives easier. When used properly, we have seen how it is able to let us access and alter information across multiple components, all while keeping our code clean and minimal. There are still cases where we do not want to use this to share information, however. Sometimes it is just easier to pass a value as a prop to a couple children instead of going through the entire set up process for Context. While it is not always the right choice for every job, when properly used, it is one of our most versatile tools in React. Hopefully after this short read, you can feel comfortable and ready to use it in your own code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;useContext: &lt;a href="https://react.dev/reference/react/useContext" rel="noopener noreferrer"&gt;https://react.dev/reference/react/useContext&lt;/a&gt;&lt;br&gt;
createContext: &lt;a href="https://react.dev/reference/react/createContext" rel="noopener noreferrer"&gt;https://react.dev/reference/react/createContext&lt;/a&gt;&lt;br&gt;
useState: &lt;a href="https://react.dev/reference/react/useState" rel="noopener noreferrer"&gt;https://react.dev/reference/react/useState&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Asynchronous Programming: Using Fetch and Then</title>
      <dc:creator>George Brockman</dc:creator>
      <pubDate>Wed, 20 Dec 2023 22:23:41 +0000</pubDate>
      <link>https://dev.to/georgefbrockman/asynchronous-programming-using-fetch-and-then-35hi</link>
      <guid>https://dev.to/georgefbrockman/asynchronous-programming-using-fetch-and-then-35hi</guid>
      <description>&lt;p&gt;When building a webpage, we almost always want to access information found in other places on the internet. Not only does it create a more interesting and engaging application, it also prevents us from having to have all of the information needed for our site on a single server. In order to access data remotely with JavaScript, we use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/fetch" rel="noopener noreferrer"&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetch
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;fetch()&lt;/code&gt; function is a global method implemented by &lt;strong&gt;Window&lt;/strong&gt; and &lt;strong&gt;WorkerGlobalScope&lt;/strong&gt;. Because of this, &lt;code&gt;fetch()&lt;/code&gt; is available to use in virtually every context in which you might need it. It takes time to access information online, however. Generally speaking, we usually don't want our code to have to wait for a single fetch request to finish before moving on to other steps. Luckily for us, &lt;code&gt;fetch()&lt;/code&gt; is an asynchronous function, meaning it returns a &lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;Promise&lt;/a&gt;&lt;/strong&gt; instance while the next lines of code can run. &lt;/p&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Promise&lt;/strong&gt; instance is exactly what it sounds like; it's a promise that the data will be passed to the code as soon as the information from the &lt;code&gt;fetch()&lt;/code&gt; request is received.&lt;/p&gt;

&lt;p&gt;That leaves us with another problem, however. If the rest of the code runs while the &lt;strong&gt;Promise&lt;/strong&gt; is waiting to resolve, then the information received by &lt;code&gt;fetch()&lt;/code&gt; will be left with no code to handle it. To solve this issue, there are a couple of tools we can use: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await" rel="noopener noreferrer"&gt;&lt;code&gt;await&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then" rel="noopener noreferrer"&gt;&lt;code&gt;then()&lt;/code&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Await and Then
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Await&lt;/code&gt; is an operator that is useful when attempting to assign a Promise instance to a variable, while &lt;code&gt;then()&lt;/code&gt; is used to immediately call a callback function when the &lt;strong&gt;Promise&lt;/strong&gt; resolves. For this blog, we're going to be looking at how &lt;code&gt;fetch()&lt;/code&gt; and &lt;code&gt;then()&lt;/code&gt; interact with each other and leave &lt;code&gt;await&lt;/code&gt; for another time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Fetch
&lt;/h2&gt;

&lt;p&gt;Building on what we read earlier, the &lt;code&gt;fetch()&lt;/code&gt; function can take a couple arguments but only needs one. For simplicity's sake, we will only be looking at the required argument, but keep in mind there are other options that can be added to manipulate the functionality.&lt;/p&gt;

&lt;p&gt;The first and main argument passed to the &lt;code&gt;fetch()&lt;/code&gt; function is the resource, or the URL of the information that you are trying to access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('http://localhost:3000/data')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As stated earlier, &lt;code&gt;fetch()&lt;/code&gt; will attempt to access the data, and while doing so, will return a &lt;strong&gt;Promise&lt;/strong&gt; instance that will be replaced with the real information at a later time. We call this resolved request the &lt;strong&gt;Response&lt;/strong&gt; object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Then
&lt;/h2&gt;

&lt;p&gt;Once we have a &lt;strong&gt;Promise&lt;/strong&gt; instance, it can then be passed to the &lt;code&gt;then()&lt;/code&gt; function to be used. &lt;code&gt;then()&lt;/code&gt; is a method used by &lt;strong&gt;Promise&lt;/strong&gt; instances that has one mandatory argument and an optional one: a callback function to be used on successful resolutions and/or one for rejected resolutions of the &lt;strong&gt;Promise&lt;/strong&gt;. We will start by only looking at instances of &lt;code&gt;then()&lt;/code&gt; using only one argument.&lt;/p&gt;

&lt;p&gt;Combined with a fetch request, it looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('http://localhost:3000/data')
.then(res =&amp;gt; res.json())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we took the response from the &lt;code&gt;fetch()&lt;/code&gt; request and passed it to an arrow function. This callback function changes the &lt;strong&gt;JSON&lt;/strong&gt; string that is the response and returns a usable object for later use. Remember that this response is only translated once the &lt;strong&gt;Promise&lt;/strong&gt; passed to the &lt;code&gt;then()&lt;/code&gt; function resolves.&lt;/p&gt;

&lt;p&gt;From here, we can link together at many &lt;code&gt;then()&lt;/code&gt; functions as we desire. This is because each &lt;code&gt;then()&lt;/code&gt; function immediately returns a &lt;strong&gt;Promise&lt;/strong&gt; instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch(`http://localhost:3000/data`)
.then(res =&amp;gt; res.json())
.then(response =&amp;gt; someFunction(response))
.then(someValue =&amp;gt; console.log(someValue))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As each &lt;strong&gt;Promise&lt;/strong&gt; is fulfilled, the callback functions will be called in the order that they are chained.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, we can add another call back function as an argument to a &lt;code&gt;then()&lt;/code&gt; function to be used in case our original &lt;strong&gt;Promise&lt;/strong&gt; does not resolve. In that case, we would have something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch(`http://localhost:3000/data`)
.then(res =&amp;gt; res.json(), () =&amp;gt; console.log("fetch unsuccessful"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Resolving Promises
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;then()&lt;/code&gt; function will choose either of the callback functions depending on how the &lt;strong&gt;Promise&lt;/strong&gt; passed to it resolves. These are the ways &lt;strong&gt;Promises&lt;/strong&gt; can be handled:&lt;/p&gt;

&lt;p&gt;•If a value is returned, the &lt;strong&gt;Promise&lt;/strong&gt; is fulfilled with the returned value as the value of the &lt;strong&gt;Promise&lt;/strong&gt;.&lt;br&gt;
•If no value is returned, the &lt;strong&gt;Promise&lt;/strong&gt; is fulfilled with undefined as the value.&lt;br&gt;
•If an error is thrown, the &lt;strong&gt;Promise&lt;/strong&gt; is rejected with the error being its value.&lt;br&gt;
•If a fulfilled &lt;strong&gt;Promise&lt;/strong&gt; is returned, the &lt;strong&gt;Promise&lt;/strong&gt; is fulfilled with the returned value as its own value.&lt;br&gt;
•If a rejected &lt;strong&gt;Promise&lt;/strong&gt; is returned, the &lt;strong&gt;Promise&lt;/strong&gt; is rejected and shares the returned value.&lt;br&gt;
•If another pending Promise is returned, the &lt;strong&gt;Promise&lt;/strong&gt; remains pending and becomes fulfilled as soon as the previous &lt;strong&gt;Promise&lt;/strong&gt; resolves.&lt;/p&gt;

&lt;p&gt;There are various different ways to alter the behavior of the &lt;code&gt;fetch()&lt;/code&gt; and &lt;code&gt;then()&lt;/code&gt; methods beyond what has been discussed in this blog. For example, we can add more arguments to &lt;code&gt;fetch()&lt;/code&gt; such as a method, headers, body, etc. However, for simplistic use, this blog covers most of what you need to know. Thank you for reading!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
