Mike Driscoll Profile picture
Nov 15 6 tweets 4 min read
Today is a great day to talk about one of #Python's most popular SQL ORMs - #SQLAlchemy!

SQLAlchemy is an Object Relational Mapper for Python. That means that it can translate Python code to SQL.

🧵👇🐍
Let's take a look at creating a database with #Python and #SQLAlchemy

You need to create classes that model tables in your database.

Then to create the actual database, you use `Base.metadata.create_all()` Image
Now let's add some data to our database using #Python and #SQLAlchemy

The following code shows how to add data to both our tables. It also shows how to add multiple rows of data all at once! 🐍🔥 Image
Now that you have data in the database, let's find out how to write a simple query and change one of the fields!

Here are a couple of examples that demonstrate how to change two different fields in your database: Image
To wrap things up, let's look at how you might delete a record from a database using #Python and #SQLAlchemy Image
Thanks for reading my thread on Python and SQLAlchemy. Working with databases can be fun in Python!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Nov 13
It's the weekend, so now's a great time to talk about #Python and SQLite! 🐍

Python works with SQLite, a file-based database, right out of the box via its built-in `sqlite3` library.

Here's how you could connect to a SQLite database with Python: Image
When you use the `sqlite3` module in Python, you will be using regular SQL to work with it.

So to create a table, you would do something like this: Image
The following code shows how to insert one row of data into a table as well as how to insert multiple rows using SQL and sqlite3 in Python 🐍🔥 Image
Read 8 tweets
Nov 7
#Python includes several different ways to sort things. One handy way to sort is to use the built-in `sorted()` function!

Let's learn about that today!

#python_builtins_by_driscollis

🧵🐍👇
The `sorted()` function takes in an iterable and a couple of optional arguments.

In this example, you pass in a Python list and sort it from smallest to largest.

Then you set the optional `reverse` argument to True and sort from largest to smallest! 🐍🔥
The `sorted()` function also takes an optional `key` parameter. This parameter is a function that is used to modify the sorting behavior.

In this example, you use `str.lower()` to sort the words of a sentence. It will compare all the words in lowercase while sorting.
Read 5 tweets
Nov 4
Python dataclasses were added in 3.7. This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

The original definition can be found here:

python.org/dev/peps/pep-0…

🧵🐍👇
Here is a fairly simple example of creating a dataclass in #Python Image
If you need to add a #Python list or dictionary field to a dataclass, then you will need to import the `field()` function from the dataclass module

Then specify the appropriate `default_factory` to create the field Image
Read 4 tweets
Oct 27
#Python has a TON of different ways to create executables!

Here are some of the packages you can use:

🐍 PyInstaller
🐍 Py2Exe
🐍 cx_freeze
🐍 Briefcase
🐍 Nuitka
🐍 py2app

🧵🐍👇
If you want to create a Mac executable, you can use any of these:

🐍 PyInstaller
🐍 Briefcase
🐍 py2app
🐍 Nuitka
When creating a Windows executable with PyInstaller or py2exe, avoid using the single-file option.

If you use the single-file option, Windows Defender may think you are creating malware and delete it or flag it.

You can sign your executables to prevent this
Read 7 tweets
Oct 25
Python dataclasses were added in 3.7. This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

The original definition can be found here:

python.org/dev/peps/pep-0…

🧵🐍👇
Here is a fairly simple example of creating a dataclass in #Python Image
If you need to add a #Python list or dictionary field to a dataclass, then you will need to import the `field()` function from the dataclass module

Then specify the appropriate `default_factory` to create the field Image
Read 4 tweets
Oct 12
One of #Python's many built-in functions is called `id()`. You can use `id()` to return the “identity” of an object.

The id is an integer which is guaranteed to be unique and constant for this object during its lifetime.

#python_builtins_by_driscollis

🧵🐍👇
CPython implementation detail for ids: This is the address of the object in memory.

Identity and equality are NOT the same in Python!

For example, the two lists in the following example equal each other (i.e. have the same contents), but their id is different!
In CPython, there is an optimization where the first 256 integers have the same identity

This can be proven with Python's `id()` function.

So in this case, the identity and the equality are the same
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(