<?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: David Stinnette</title>
    <description>The latest articles on DEV Community by David Stinnette (@dastinnette).</description>
    <link>https://dev.to/dastinnette</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%2F1202653%2Fc435fdbe-3d15-4e0a-9f46-d0bd034ca655.jpeg</url>
      <title>DEV Community: David Stinnette</title>
      <link>https://dev.to/dastinnette</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dastinnette"/>
    <language>en</language>
    <item>
      <title>How to save time using Python Decorators</title>
      <dc:creator>David Stinnette</dc:creator>
      <pubDate>Mon, 27 Nov 2023 00:01:06 +0000</pubDate>
      <link>https://dev.to/dastinnette/how-to-use-python-decorators-698</link>
      <guid>https://dev.to/dastinnette/how-to-use-python-decorators-698</guid>
      <description>&lt;p&gt;What are decorators in Python? Why are they useful?&lt;/p&gt;

&lt;p&gt;Let's say I'm building a Python project and I want to know how long it takes for each of my functions to run. I could build timing functionality within each of my functions but what if I have hundreds of functions? That would take far too long.&lt;/p&gt;

&lt;p&gt;Instead I'm going to create a function to do it for me. Let's call it &lt;code&gt;timer&lt;/code&gt; and give it another function as an argument. Inside, we'll define a function called &lt;code&gt;wrapper&lt;/code&gt;. Next let's define a variable that stores the start time. And finally we'll run the function that's passed into &lt;code&gt;timer&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;After the function has executed we can determine how long it took by taking the current time and subtracting the starting time from it. Next we can print that it took t2-t1 seconds to execute. Finally let's return the wrapper function.&lt;br&gt;
&lt;/p&gt;

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

def timer(func):
    def wrapper():
        t1 = time.time()
        func()
        t2 = time.time() - t1
        print(f'{func.__name__} ran in {t2} seconds')
    return wrapper

@timer 
def execute_this():
    time.sleep(1.3)

@timer 
def execute_that():
    time.sleep(.4)

execute_this()
execute_that()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now finally on the to decorator!&lt;/p&gt;

&lt;p&gt;Let's put &lt;code&gt;@timer&lt;/code&gt; above each of the functions we want to time. Python sees this &lt;code&gt;@&lt;/code&gt; symbol and understands the following function needs to be passed into a function named after the @ symbol, in this case &lt;code&gt;timer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;execute_this&lt;/code&gt; runs in &lt;code&gt;timer&lt;/code&gt; with the t1 and t2 variables determining the run time of each function. As you can see, reusing the &lt;code&gt;timer&lt;/code&gt; function as a decorator allows us to save time and code as we expand our project and add many more functions that need to be timed. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Getting started with sqlite3 in Python</title>
      <dc:creator>David Stinnette</dc:creator>
      <pubDate>Thu, 09 Nov 2023 22:31:41 +0000</pubDate>
      <link>https://dev.to/dastinnette/getting-started-with-sqlite3-in-python-3h0n</link>
      <guid>https://dev.to/dastinnette/getting-started-with-sqlite3-in-python-3h0n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Intro&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQLite is a user friendly, file-based SQL database. It comes bundled with Python and can be used in any of your Python apps without having to install any additional software.&lt;/p&gt;

&lt;p&gt;In this post we’ll walk through the sqlite3 module in Python. We’ll create a connection to a SQLite database, add a table, insert data into that table, and read data in that table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Connecting to the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can connect to a SQLite database using the Python sqlite3 module. &lt;code&gt;import sqlite3&lt;/code&gt; gives our Python program access to the sqlite3 module. The &lt;code&gt;sqlite3.connect()&lt;/code&gt; function returns a Connection object that we will use to interact with the SQLite database held in the file pets.db. The pets.db file is created automatically by sqlite3.connect() if pets.db does not already exist on our computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Adding data to the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have connected to the pets.db SQLite database, we can start inserting and reading data from it. In a SQL database, data is stored in tables. Tables define a set of columns, and contain 0 or more rows with data for each of the defined columns. We will create a table named dogs that tracks a dog's name, breed, and age.&lt;/p&gt;

&lt;p&gt;We can create this dogs table in SQLite using the connection we made in Step 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cursor = connection.cursor()
cursor.execute("CREATE TABLE dog (name TEXT, breed TEXT, age INTEGER)")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;connection.cursor()&lt;/code&gt; returns a Cursor object. Cursor objects allow us to send SQL statements to a SQLite database using &lt;code&gt;cursor.execute()&lt;/code&gt;. The "CREATE TABLE dog ..." string is a SQL statement that creates a table named dog with the three columns described earlier: name of type TEXT, breed of type TEXT, and age of type INTEGER.&lt;/p&gt;

&lt;p&gt;Now that we have created a table, we can insert rows of data into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cursor.execute("INSERT INTO dog VALUES ('Buck', 'shepherd', 11)")
cursor.execute("INSERT INTO dog VALUES ('Evi', 'lab', 7)")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We call &lt;code&gt;cursor.execute()&lt;/code&gt; two times: once to insert a row for the dog Buck, and once to insert a row for the lab Evi. "INSERT INTO dog VALUES ..." is a SQL statement that allows us to add rows to a table.&lt;/p&gt;

&lt;p&gt;In the next section, we will use a SQL SELECT statement to inspect the rows we just inserted into our dog table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Reading data from the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Step 2, we added two rows to a SQLite table named dog. We can retrieve those rows using a SELECT SQL statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rows = cursor.execute("SELECT name, breed, age FROM dog").fetchall()
print(rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;cursor.execute()&lt;/code&gt; function runs a SELECT statement to retrieve values for the name, breed, and age columns in the dog table. fetchall() retrieves all the results of the SELECT statement. When we print(rows) we see a list of two tuples. Each tuple has three entries; one entry for each column we selected from the dog table. The two tuples have the data we inserted in Step 2: one tuple for Buck the shepherd, and one tuple for Evi the lab.&lt;/p&gt;

&lt;p&gt;If we wanted to retrieve rows in the dog table that match a specific set of criteria, we can use a WHERE clause:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target_dog_name = "Buck"
rows = cursor.execute(
    "SELECT name, breed, age FROM dog WHERE name = ?",
    (target_dog_name,),
).fetchall()
print(rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As with the previous example, &lt;code&gt;cursor.execute(&amp;lt;SQL statement&amp;gt;).fetchall()&lt;/code&gt; allows us to fetch all the results of a SELECT statement. The WHERE clause in the SELECT statement filters for rows where the value of name is target_dog_name. Notice that we use ? to substitute our target_dog_name variable into the SELECT statement. We expect to only match one row, and indeed we only see the row for Buck the shepherd returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The sqlite3 module is a powerful part of the Python standard library. It lets us work with a full feature SQL database without installing any additional software.&lt;/p&gt;

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