Mike Driscoll Profile picture
Nov 23 β€’ 8 tweets β€’ 4 min read
Today is a great day to learn about #Python's `argparse` module!

The argparse module makes it easy to write user-friendly command-line interfaces.

#PythonStdLib 🐍πŸ”₯

πŸ§΅πŸ‘‡
Here is an example of creating an argument parser with no arguments.

Why would you do that? Well, to find out what `argparse` gives you out of the box πŸ“¦!

You can see here that `argparse` gives you some help text and prints out your provided description Image
A common coding pattern for argument parsing is to put the argument parsing code into its own function or method and then return the parsed args.

Here's an example: Image
Let's add some arguments to our argument parser! In this example, you add one required argument and two optional arguments.

One of the optional arguments shows you how to apply a default value while the other only accepts integers ImageImage
You can use short argument flags or long argument flags with #Python's `argparse` module 🐍πŸ”₯ Image
This thread only scratches the surface of what you can do with Python's `argparse` module.

Check out the docs for lots more detail:

docs.python.org/3/library/argp…
You can also learn more about argparse @mousevspython

blog.pythonlibrary.org/2015/10/08/a-i…
Thanks for checking out my thread on argument parsing with #Python πŸπŸš€

Follow me to learn more about the Python programming language!

β€’ β€’ β€’

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 23
Did you know you can create mobile applications with #Python and @kivyframework?

You can! I wrote an article about the process @realpython

realpython.com/mobile-app-kiv…

Let's look at a few examples from the article!

πŸ§΅πŸπŸ‘‡
This is a "Hello World" type example using the @kivyframework in #Python

It basically creates a window with a label on it:
Labels are useful, but a picture is worth a thousand labels!

In this code snippet, you add a picture to your Kivy application instead of a label! 🐍πŸ”₯
Read 5 tweets
Nov 21
One of the harder concepts to learn in #Python are decorators.

So let's take a few moments and learn about them!

πŸ§΅πŸπŸ‘‡
It's always good to start with a regular function. Why?

Because you create a decorator using functions!

This function, `doubler()` takes in a number and doubles it. That's it! Image
But wait! #Python functions are also objects. You can get their `__name__` and their docstring (`__doc__`), if they have one.

You can also get a listing of their other methods with `dir()`

What all this means is that you can pass a function to another function! Image
Read 11 tweets
Nov 21
Let's talk some more about sets in #Python in more detail!

The first thing to talk about is how to create a set. One way to do so is with curly braces.

Don't confuse this with dictionary creation though!

πŸ§΅πŸπŸ‘‡
You can also create a set by using Python's `set()` class, which is built-in.

Here you use `set()` to cast a list to a set! 🐍πŸ”₯

Note: Sets remove duplicate!
You can test if an item is in a #Python set by using Python's `in` operator:
Read 15 tweets
Nov 20
Let's talk about "unpacking" lists, tuples, and more with #Python!

The first unpacking example I want to show is how you can use a single asterisk to unpack all these different types!

🐍πŸ”₯
You can also use the double-asterisk to unpack dictionaries.

In this example, you unpack a nested dictionary into another dictionary. Note that both dictionaries have the same key.

Since this is executed from left-to-right, you unpack and then overwrite the "x" key with 1
Here's a more complicated example of dictionary unpacking.

You may need to re-read this a few times to fully understand what is going on here.
Read 4 tweets
Nov 20
#Python has many different GUI Toolkits. In this mini-thread, I'm going to show you how to write a "simple" plot with #wxPython

πŸ§΅πŸπŸ‘‡
Creating a plot with #wxPython doesn't take a TON of code, but most GUI frameworks do require a LOT more code than simple scripts.

Here is a pretty stripped down plotting example: Image
When you run the code in the previous tweet, your #Python graph will look like this:

#wxPython πŸ”₯ Image
Read 4 tweets
Nov 15
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
Read 6 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!

:(