Quantcast
Channel: Planet Python
Viewing all 22873 articles
Browse latest View live

Python Sweetness: Mitogen v0.2.8 released

$
0
0

Mitogen for Ansible v0.2.8 has been released. This version (finally) supports Ansible 2.8, comes with a supercharged replacement fetch module, and includes roughly 85% of what is needed to implemement fully asynchronous connect.

As usual a huge slew of fixes are included. This is a bumper release, running to over 20k lines of diff. Get it while it's hot, and as always, bug reports are welcome!


William Minchin: Image Process Plugin 1.2.0 for Pelican Released

$
0
0

Image Process is a plugin for Pelican, a static site generator written in Python.

Image Process let you automate the processing of images based on their class attribute. Use this plugin to minimize the overall page weight and to save you a trip to Gimp or Photoshop each time you include an image in your post.

Image Process is used by this blog’s theme to resize the source images so they are the correct size for thumbnails on the main index page and the larger size they are displayed at on top of the articles.

This Release

Version 1.2.0 of the plugin has been released and posted PyPI.

The biggest change this version brings is support for Pelican version 4. Thanks to Nick Perkins for reporting the issue, and to Therry van Neerven for providing a Pull Request I could crib a solution from.

I’ve also made some improvements in the test suite. It still fails on Windows due to issues with filepath separators, but most tests now pass on Travis. The remaining failing test appears to be due to some changes in exactly how Pillow (the image processing library used here) transforms the images.

Upgrading

To upgrade simply use pip:

pip install minchin.pelican.plugins.image_process --upgrade

Mike Driscoll: PyDev of the Week: Paul Ganssle

$
0
0

This week we welcome Paul Ganssle (@pganssle) as our PyDev of the Week. Paul is the maintainer of the dateutil package and also a maintainer of the setuptools project. You can catch up with Paul on his website or check out some of his talks. Let’s take a few moments to get to know Paul better!

Can you tell us a little about yourself (hobbies, education, etc):

One thing that sometimes surprises people is that I started out my career as a chemist. I have a bachelor’s degree in Chemistry from the University of Massachusetts, Amherst and a Ph.D in Physical Chemistry from the University of California, Berkeley. After that I worked for two years building NMR (nuclear magnetic resonance) devices for use in oil wells. In 2015 I was looking for a career with a bit more flexibility in terms of location and I made the switch to software development; one thing that is nice about the software industry is that tech companies are not afraid to hire people with non-traditional backgrounds if they know how to code.

Paul Ganssle

I have the typical assortment of “hacker” and “autodidact” hobbies – learning languages, picking locks, electronics projects, etc. One of my favorite projects (which has unfortunately fallen a bit by the wayside) is my HapticapMag, a haptic compass that I built into a hat. I had it up and working for 2 or 3 weeks, but some parts broke and I never got around to fixing it. My tentative plan is to start up some new electronics projects in 4-5 years, when my son is old enough to be interested in that sort of thing.

Why did you start using Python?

I have two origin stories for this, actually. The more boring one is that around 2008 a friend of mine told me about this cool and increasingly popular programming language called Python that I should definitely learn, and I sort of picked it up and started using it for little system automation tasks.

What really got me into Python, though, was when I illustrated some point I was making in a forum post using a graph that I had made in Matlab and someone complained about the terrible aliasing in the plot and suggested I use matplotlib instead. I tried it out and the plots were so much better that I was instantly hooked. After that, I moved everything I could over from Matlab to Python and never looked back.

What other programming languages do you know and which is your favorite?

It’s hard to say when you “know” a programming language, but the programming languages I’m most confident with are C++, C and Rust (and probably some others like Matlab that I haven’t used in years but once knew pretty well). I can write enough Javascript to get by, but to say I know it would be kind of like saying I speak Spanish because I can order a beer and ask where the bathroom is.

At the moment, I’m very excited about Rust, which is a memory-safe systems programming language targeting the use cases where C and C++ currently predominate. One of the very nice things about Rust is that there is a very enthusiastic community out there and it already has a flourishing ecosystem of third party packages, which I think is one reason there’s a lot of excitement about Rust in the Python community.

What projects are you working on now?

I do a lot of maintenance tasks that might not be considered “working on a project” for packages I maintain or help maintain like dateutil, setuptools and CPython (and I try to monitor the PyO3 issue tracker, though I have no official standing in that project); things like reviewing PRs, commenting on issues and participating in discussions.

In terms of features and other improvements, I’ve been trying to prepare a proof of concept for PEP 517-compatible editable installations and I’ve started working a bit on adding time zones to the standard library. I also have a few smaller projects in various states of completion that I occasionally work on, like my library variants, which I’ve given a few talks about, or my only half-complete library pyminimp3 – Python bindings around the C minimp3 library for processing MP3 files.

Which Python libraries are your favorite (core or 3rd party)?

I am a big fan of hypothesis, the property-based testing framework. With hypothesis, you write assertions about a property that your code has (e.g. “this parse operation is the inverse of this format operation”), given a domain of inputs (integers, datetimes, etc), and hypothesis randomly generates example inputs for you. I was originally very uneasy about the fact that the tests you write with it are non-deterministic, but I got over that very quickly; in reality, if there’s a bug in your code, it’s very rare for hypothesis to miss it on the first try, particularly if you are running tests for multiple platforms and multiple interpreters on each PR. The bigger problem I’ve had introducing hypothesis into a code base is that it finds a bunch of obscure edge cases that you haven’t handled, so you need to fix a bunch of bugs just to be able to start using it!

How did you become a core developer of Python?

I have been peripherally involved in Python development since I was asked to comment on PEP 495 (adding the fold attribute) as maintainer of dateutil. In late 2017 I started contributing bug fixes and features more actively, and monitoring the issue tracker for datetime-related issues. One of the biggest bottlenecks in the CPython development process is high-quality reviews, which is something I’m used to doing from years of maintaining open source packages, so I stepped in and started reviewing PRs as well.

In terms of the actual process of becoming a core dev, I took an increasingly common path for newer core devs, which is that Victor Stinner noticed my contributions and asked me if I was interested in eventually becoming a core dev; after I said yes, he and Pablo Galindo Salgado agreed to mentor me through the process of becoming a bug triager and eventually core developer.

Which modules do you work on and why?

My main expertise is with the datetime and related modules (e.g. time); this is partially a result of my randomly stumbling into maintaining dateutil back in early 2015 and partially down to the fact that most other people reallywould prefer not to work on datetimes. I’ve also been involved in packaging as well (as a maintainer of setuptools), but these days distutils doesn’t see much improvement because it is almost always preferable to make the improvements in setuptools instead.

Do you have any advice for others who would like to contribute to Python core?

If you have the opportunity to attend a sprint (as part of a conference or otherwise), that is usually the best way to make a first contribution to any open source project. If not, I think the best advice I can give is the same I would give for contributing to any open source project:

  1. Make a small PR with tightly scoped changes: these are much easier to review and merge.
  2. Minimize or eliminate changes to the public API: every change to the public API is something that the maintainers of that module will have to support indefinitely. Behind-the-scenes changes are a lot easier to accept because they’re a lot easier to undo.
  3. Add tests! Good tests are often the hardest part of a PR to write, so it’s very unlikely that your contribution will be accepted without them. They also make a contribution much easier to review, because they demonstrate exactly what your code is supposed to do and they enforce the behavior!

If you’re already a skilled reviewer, I also recommend looking around in the issue tracker for unreviewed PRs and giving your comments. This will really help the project and is a sure fire way to build a reputation as a solid contributor.

Is there anything else you’d like to say?

Thank you for asking me to do this interview, and thanks to all the readers who’ve indulged my verbosity by reading all the way to the end.

Thanks for doing the interview, Paul!

The post PyDev of the Week: Paul Ganssle appeared first on The Mouse Vs. The Python.

Reuven Lerner: Weekly Python Exercise A3 (beginner objects) is open

$
0
0
Image

If you’ve been programming in Python for any length of time, then you’ve undoubtedly heard that “everything is an object.”

But what does that mean? And who cares?  And what effect does that have on you as a developer — or on Python, as a language?

Indeed, how can (and should) you take advantage of Python’s object-oriented facilities to make your code more readable, maintainable, standard, and (dare I say it) Pythonic?

If you’re relatively new to Python, and have been struggling with some of these same questions, or if you’re just wondering about the differences between instances, classes, methods, and attributes, then I have good news for you: The upcoming cohort of Weekly Python Exercise is all about object-oriented programming.

In this 15-week course, you’ll learn in the best way I know, by solving problems and discussing them with others. As you work through the exercises, you’ll get a better understanding of:

  • Instances and classes
  • Attributes, at both the attribute and class level
  • Methods
  • Composition of objects
  • Inheritance
  • Magic methods

Weekly Python Exercise, of course, is a family of 15-week classes designed to help improve your Python fluency.  Each course works the same:

  • On Tuesday, you receive a problem (along with “pytest” tests)
  • On the following Monday, you receive my detailed solution. 
  • In between, we can discuss it in the forum.
  • I also have live office hours, when people can ask any questions that they might have.

WPE A-level courses are for beginners, while B-level courses are for more advanced Python developers. But you can take any or all of them, in any order — and there’s no overlap between the exercises in these classes and any of the previous books/courses I’ve given.

This new cohort (A3) will be starting on Tuesday, September 17th.  To join, you must sign up before September 10th.  But if you sign up by September 3rd, you’ll get the early-bird discount, bringing the price down to $80 — more than $20 off the full price.

I won’t be offering these exercises for at least one more year. So if you want to sharpen your OO skills before the autumn of 2020, then you should act now.

As always, you can get an even better price if you’re a student, pensioner/retiree/senior citizen, or living permanently outside of the world’s 30 richest countries. Just reply to this e-mail, and I”ll send you the appropriate coupon code.

And if several people (at least five) from your company want to join together?  Let me know, and I’ll give you an additional discount, too.

There’s lots more to say about Weekly Python Exercise, now in its third year of helping Python developers from around the world to write better code — doing more in less time, and getting better jobs than before.  You can read more, and try to some sample exercises, at https://WeeklyPythonExercise.com/ .

But if you’ve always wanted to improve your fluency with Python objects, then you can just sign up at https://WeeklyPythonExercise.com/ .

Don’t wait, though! The early-bird discount ends on September 3rd.

The post Weekly Python Exercise A3 (beginner objects) is open appeared first on Reuven Lerner.

Erik Marsja: The Easiest Data Cleaning Method using Python & Pandas

$
0
0

The post The Easiest Data Cleaning Method using Python & Pandas appeared first on Erik Marsja.

In this post we are going to learn how to do simplify our data preprocessing work using the Python package Pyjanitor. More specifically, we are going to learn how to:

  • Add a column to a Pandas dataframe
  • Remove missing values
  • Remove an empty column
  • Cleaning up column names

That is, we are going to learn how clean Pandas dataframes using Pyjanitor. In all Python data manipulation examples, here we are also going to see how to carry out them using only Pandas functionality.

What is Pyjanitor?

What is Pyjanitor? Before we continue learning on how to use Pandas and Pyjanitor to clean our datasets, we will learn about this package. The python package Pyjanitor extends Pandas with a verb-based API. This easy to use API is providing us with convenient data cleaning techniques. Apparently, it started out as a port of the R package janitor. Furthermore, it is inspired by the ease-of-use and expressiveness of the r-package dplyr. Note, there are some different ways how to work with the methods and this post will not cover all of them (see the documentation).

How to install Pyjanitor

There are two easy methods to install Pyjanitor:

1. Installing Pyjanitor using Pip

pip install pyjanitor

2. Installing Pyjanitor using Conda:

conda -c install conda-forge pyjanitor

Now that we know what Pyjanitor is and how to install the package we soon can continue the Python data cleaning tutorial by learning how to remove missing values from Pandas. Note, that this Pandas tutorial will walk through each step on how to do it using Pandas and Pyjanitor. In the end, we will have a complete data cleaning example using only Pyjanitor and a link to a Jupyter Notebook with all code.

Fake Data

In the first Python data manipulation examples, we are going to work with a fake dataset. More specifically, we are going to create a dataframe, with an empty column, and missing values. In this part of the post we are, further, going to use the Python packages SciPy, and NumPy. That is, these packages also need to be installed.

In this example we are going create three columns; Subject, RT (response time), and Deg. To create the response time column, we will use SciPy norm to create data that is normally distributed.

import numpy as np
import pandas as pd
from scipy.stats import norm
from random import shuffle

import janitor

subject = ['n0' + str(i) for i in range(1, 201)]

Python Normal Distribution using Scipy

In the next code chunk we create a variable, for response time, using a normal distribution.

a = 457
rt = norm.rvs(a, size=200)

Shuffling the List and Adding Missing Values

Furthermore, we are adding some missing values and shuffling the list of normally distirbuted data:

# Shuffle the response times
shuffle(rt)
rt[4], rt[9], rt[100] = np.nan, np.nan, np.nan

Dataframe from Dictionary

Finally, we are creating a dictionary of our two variables and use the dictionary to create a Pandas dataframe.

data = {
    'Subject': subject,
    'RT': rt,
}

df = pd.DataFrame(data)

df.head()
Dataframe created from dictDataframe created from dictionary

Data Cleaning in Python with Pandas and Pyjanitor

How to Add a Column to Pandas Dataframe

Now that we have created our dataframe from a dictionary we are ready to add a column to it. In the examples, below, we are going to use Pandas and Pyjanitors method.

1. Append a Column to Pandas Dataframe

It’s quite easy to add a column to a dataframe using Pandas. In the example below we will append an empty column to the Pandas dataframe:

df['NewColumnName'] = np.nan
df.head()
How to add Single Column to DataframeColumn added to dataframe

2. Adding a Column to Pandas Dataframe using Pyjanitor

Now, we are going to use the method add_column to append a column to the dataframe. Adding an empty column is not as easy as using the method above. However, as you will see towards the end of this post, we can use all of the methods when creating our dataframe:

newcolvals = [np.nan]*len(df['Subject'])
df = df.add_column('NewColumnName2', newcolvals)
df.head()
Append column to Pandas dataframe

How to Remove Missing Values in Pandas Dataframe

It is quite common that our dataset is far from complete. This may be due to error in the measurement instruments, people forgetting, or refusing, to answer certain questions, amongst many other things. Despite the reason behind missing information these rows are called missing values. In the framework of Pandas the missing values are coded by the symbol NA, much like in R statistical environment. Pandas have the function isna() to help us identify missings in our dataset. If we want to drop missing values, Pandas have the function dropna().

1 Dropping Missing Values using Pandas dropna method

In the code example below we are dropping all rows with missing values. Note, if we want to modify the dataframe we should add the inplace parameter and set it to true.

df.dropna(subset=['RT']).head()

Dropping Missing Values from Pandas Dataframe using PyJanitor

The method to drop missing values from a Pandas Dataframe using Pyjanitor is the same the one above. That is, we are going to use the the dropna method. However, when using Pyjanitor we also use the parameter subset to select which column(s) we are going to use when removing missing data from the dataframe:

df.dropna(subset=['RT'])

How to Remove an Empty Column from Pandas Dataframe

In the next Pandas data manipulation example, we are going to remove the empty column from the dataframe. First, we are going to use Pandas to remove the empty column and, then, we are going to use Pyjanitor. Remember, towards the end of the post we will have a complete example in which we carry out all data cleaning while actually creating the Pandas Dataframe.

1. Removing an Empty Column from Pandas Dataframe

When we want to remove an empty column (e.g., with missing values) we use the Pandas method dropna again. However, we use the axis method and set it to 1 (for column). Furthermore, we also have to use the parameter how and set it to ‘all’. If we don’t it will remove any column with missing values

df.dropna(axis=1, how='all').head()
removing empty columnsRemoved empty columns

2. Deleting an Empty Column from Pandas Dataframe using Pyjanitor

It’s a bit easier to remove an empty column using Pyjanitor:

df.remove_empty()

How to Rename Columns in Pandas Dataframe

Now that we know how to remove missing values, add a column to a Pandas dataframe, and how to remove a column, we are going to continue this data cleaning tutorial learning how to rename columns.

For instance, in the post where we learned how to load data from a JSON file to a Pandas dataframe, we renamed columns to make it easier to work with the dataframe later. In the example below, we will read a JSON file, and rename columns using both Pandas dataframe method rename and Pyjanitor

import requests
from pandas.io.json import json_normalize

url = "https://datahub.io/core/s-and-p-500-companies-financials/r/constituents-financials.json"
resp = requests.get(url=url)

df = json_normalize(resp.json())
df.iloc[:,0:6].head()

More about loading data to dataframes:

1 Renaming Columns in Pandas Dataframe

As can be seen in the image above, there are some whitespaces and special characters that we want to remove. In the first renaming columns example we are going to use Pandas rename method together with regular expressions to rename the columns (i.e., we are going to replace whitespaces and \ with underscores).

import re

df.rename(columns=lambda x: re.sub('(\s|/)','_',x),
          inplace=True)
df.keys()

2. How to Rename Columns using Pyjanitor and clean_names

The task to rename a column (or many columns) is way easier using Pyjanitor. In fact, when we have imported this Python package, we can just use the clean_names method and it will give us the same result as using Pandas rename method. In fact, using clean_names we also get all letters in the column names to lowercase:

df = df.clean_names().head()
df.keys()

How to Clean Data when Loading the Data from Disk

The cool thing with using Pyjanitor to clean our data is that we can do use all of the above methods when loading our data. For instance, in the final data cleaning example we are going to add a column to the dataframe, remove empty columns, drop missing data, and clean the column names. This is what makes working with Pyjanitor our lives easier.

data_id = [1]*200
df = (
    pd.read_csv('./SimData/DF_NA_Janitor.csv',
                index_col=0)
    .add_column('data_id', data_id)
    .remove_empty()
    .dropna()
    .clean_names()
)

df.head()

Aggregating Data using Pyjanitor

In the last example we are going to use Pandas methods agg, groupby, and reset_index together with the Pyjanitor method collapse_levels to calculate the mean and standard for each sector:

df.groupby('sector').agg(['mean',
                          'std']).collapse_levels().reset_index()

More about grouping and aggregating data using Python and Pandas:

Conclusion:

In this post we have learned how to do some data cleaning methods. Specifically, we have learned how to append a column to a Pandas dataframe, remove empty columns, handling missing values, and renaming the columns (i.e., getting better column names). There are, of course, many more data cleaning methods available, both when it comes to Pandas and Pyjanitor.

In conclusion, the methods added by the Python package are both s imilar to the one of the R-package janitor and dplyr. These methods will make our lives easier when preprocessing our data.

What is your favorite data cleaning method and/or Package? It can be either using R, Python, or any other programming language. Leave a comment below!

Add a column to a Pandas dataframe > Remove missing values > Remove an empty column > Cleaning up column names And all of this using only a few lines of code (end of the post)." data-app-id="27344152" data-app-id-name="category_below_content">

The post The Easiest Data Cleaning Method using Python & Pandas appeared first on Erik Marsja.

PSF GSoC students blogs: GSoC week #9

$
0
0

Hello everyone,

In week 9, My front end changes to connect the EOS-icons with backend API was merged and it is live at https://eos-icons.eosdesignsystem.com/extended/icons-picker.html

But after this another GSoC student merged his code and some issues were happening and some part of front end seems to be broken due to inherited styling. The good thing is all the features are working and the UI looks pretty smooth too.

PSF GSoC students blogs: GSoC Weekly Checkin

$
0
0

Hello everyone!

What did I do this week?

After the Front end was connected, we noticed some bugs with the API. It wasn't working due to some packages that were not being installed in heroku. So we added an Apt file for that. Once that being done, I added extended icons version support for Icons Picker so that there are lots of icons to choose from.

What is coming up next week?

Next week I have to implement some features like, auto download after some time interval, loading animation etc.

Did I get stuck anywhere?

The only problem was figuring out why the API wasn't working right which we did take some good time.

Till next time,
Cheers!

PSF GSoC students blogs: 6th Last or Inception of new road Blog Post

$
0
0

My best three months now come close to end but I am very happy from myself because I have completed my project before time. Upto this time my all project goals are completed i.e MVP, test and refactoring of code all of them is done. Just one pr regarding the reconnecting is on verge of merging which is a extended goals for the gsoc period. What I can say about past 3 months I don't have words to describe these days, I learned a lot of things like WebSocket, Gatsby and Jest. I spent entire time learning these technology and implementing them in my GatsbyJs preview project. Websocket for handling the websocket event fired from the backend of Plone CMS. Gatsby API, docs and gatsby-source-filesystem to learn how to implement the functionality for updating content on real time. Jest for writing test of implemented feature. I big thankyou to my mentor datakurre who helped me in completing the project. He was active throughout the project and solve my doubts regarding the project. He helped me out whenever I stuck at certain point. I learned a lot of things at the end of this program which is not avialable to most developer i.e refactoring the codebase. I have flatten the codebase by splitting the large function into the smaller ones. Out project contains a util.Js file which contains all the methods and function for the entire project. I refactored this utils file into small separate function file and now our codebase look neat, clean and enhanced code readability. Splitting into different files also helps us in refactoring the codebase from js to typescript which is our future plan. During the implementation I faced a interesting problem i.e how I grouped different functions into single file and separate those which is not related. I make a proper hierarchical order and resolve this issue.

Thank you to Python Foundation to giving me such a wonderful opportunity to work on real world project which is used by different people all around the world. I learned some cool concept and get a valuable experience of my life :)


PSF GSoC students blogs: Coding period: week #12

$
0
0

This is going to be a blog post about fixing an urgent bug and splitting an old RFC patch of mine into stack to be landed. Also, finishing up something I was working for a long time.

 

What did I do this week?

My mentor introduced me to an issue[1] which is an urgent bug to get fixed. While pushing bookmarks, the bookmarks pointing to secret changesets are also pushed, but the changesets are not pushed. So, while pulling the bookmarks, the changeset it is pointing to will be unknown. We wanted to abort on pushing bookmarks which points to secret changes. I sent two patches. One[2] which demonstrates the issue which has tests and the other[3] one which fixes the issue. I also sent a patch[4] to abort on using both `--interactive` and `--keep` together with `unshelve` which got merged. I worked on two patches[5][6] to enhance the config registrar and its usage. Then, there was an old RFC patch[7] which introduces the first prototype of storing/restoring mergestate with `shelve`. I had to split that into a stack[8][9][10][11] to change its priority from RFC to patches which can be landed in code.

 

What is coming up next?

My mentor has requested some follow-ups to some of the merged patches. I shall be working on them. Also, I will be addressing the reviews to the WIP patches that are active now and cleanup the work. Then, I'll work on documentation of the work I did and the importance of that.

 

Did you get stuck anywhere?

Yes. While adding an abort on trying to exchange bookmarks which points to secret changesets, I was unable to solve a test case initially. My mentor asked me to sent a patch to the bitbucket and I did that. He gave me reviews there and it was later fixed by me and I sent the patch to the core.

PSF GSoC students blogs: Coding Period: Week 12

$
0
0

Hello everyone! This is one of the last blogposts. This week I worked on the last features I needed to add in my GSoC period i.e. hg update --abort

What did I do this week?
As stated in the previous week I worked on hg transplant --abort. As suggested by @Pulkit it was modified to --stop flag and finally got it merged [1]. I also resolved most of the hg continue series of patches.
Later this week I was finally able to deduce a logic for
hg update --abort and sent a patch for that [2]. It is still under review though.

What is coming up next?

As almost all patches are dealt with I will modify the patch for hg update --abort as suggested by the community and get that merged.
I will also the continue patches for
evolve at the beginning of the next week and get them merged too.
This issue has been one of the most requested features and I am really happy that I could work on this and get it resolved. Also in this week, I will be working on my GSoC 19 blog which is supposed to be attached as prescribed by final product submission guidelines.

Did you get stuck anywhere?
I got stuck regarding the logic of hg update --abort for long enough but a deeper understanding of mergestate and associated functions help me deduce a logic which was not functional initially but later this week I was able to make it work. I am still adding more extensive test cases for it to make the feature perfect. It is one of the most interesting features that I have worked on this summer which involved developing a deeper understanding of the merge and update workflow.

PSF GSoC students blogs: Blog post: Week 12

$
0
0

 

Hi everyone!

End of summer is close, and we are doing some interesting stuff here :)

This week we mainly focused on doing some integrity tests to our new atomic files, and finally running some TARDIS simulations with them! My objective was to ensure we're getting an identical atomic files with my new module.

Next week I'll run more simulations and polish the documentation. Also, I started to write my final evaluation for GSoC'19.

Last months have been really fun and exciting!

PSF GSoC students blogs: Week 10: Cartopy's EPSG

$
0
0

What did you do this week?

This week I started adding support to plot the projections from EPSG code natively using Cartopy's own feature `epsg([epsg code])` from its CRS class.  Apart from that I also successfully corrected the feature to disable or enable gridlines for map which was broken while migrating the UI side code to Cartopy.

What is up next? 

This, EPSG support, still needs a lot more refinement and the way UI handles it is still needs a little improvement so I will work on that in the coming week just before the final evaluation week.

Did you faced any blockers?

Yes, I did faced a couple of blockers specially with enable/disable of gridlines but eventually looking up the documentation it got solved in not much time.

PSF GSoC students blogs: Twelveth week of GSoC: Getting ready for the final week of GSoC

$
0
0

My GSoC is soon coming to an end so I took some time to write down what still needs to be done:

Making a release of MNE-BIDS

In the past months, there were substantial additions, fixes, and cosmetic changes made to the codebase and documentation of MNE-BIDS. The last release has happened in April (about 4 months ago) and we were quite happy to observe some issues and pull requests raised and submitted by new users. With the next release we can provide some new functionality for this growing user base.

Handling coordinates for EEG and iEEG in MNE-BIDS

In MNE-BIDS, the part of the code that handles the writing of sensor positions in 3D space (=coordinates) is so far restricted to MEG data. Extending this functionality to EEG and iEEG data has been on the to do list for a long time now. Fortunately, I have been learning a bit more about this topic during my GSoC, and Mainak has provided some starting points in an unrelated PR that I can use to finish this issue. (After the release of MNE-BIDS though, to avoid cramming in too much last-minute content before the release)

Writing a data fetcher for OpenNeuro to be used in MNE-Python

While working with BIDS and M/EEG data, the need for good testing data has come up time and time again. For the mne-study-template we solved this issue with a combination of DataLad and OpenNeuro. Meanwhile, MNE-BIDS has its own dataset.py module ... however, we all feel like this module is duplicating the datasets module of MNE-Python and not advancing MNE-BIDS. Rather, it is confusing the purpose of MNE-BIDS.

As a solution, we want to write a generalized data fetching function for MNE-Python that works with OpenNeuro ... without adding the DataLad (and hence Git-Annex) dependency). Once this fetching function is implemented, we can import it in MNE-BIDS and finally deprecate MNE-BIDS' dataset.py module.

Make a PR in MNE-Python that will support making Epochs for duplicate events (will fix ds001971 PR)

In MNE-Python, making data epochs is not possible, if two events share the same time. This became apparent with the dataset ds001971 that we wanted to add to the mne-study-template pipeline: https://github.com/mne-tools/mne-study-template/pull/41. There was a suggestion on how to solve this issue by merging the event codes that occurred at the same time. Once this fix is implemented in MNE-Python, we can use this to finish the PR in the mne-study-template.

Salvage / close the PR on more "read_raw_bids" additions

Earlier in this GSoC, I made a PR intended to improve the reading functionality of MNE-BIDS (https://github.com/mne-tools/mne-bids/pull/244). However, the PR was controversially discussed, because it was not leveraging BIDS and instead relying on introducing a dictionary as a container for keyword arguments.

After lots of discussion, we agreed to solve the situation in a different way (by leveraging BIDS) and Mainak made some initial commits into that direction. However in the further progress, the PR was dropped because other issues had higher priority.

Before finishing my GSoC, I want to salvage what's possible from this PR and then close it ... and improving the original issue report so that the next attempt at this PR can rely on a more detailed objective.

PSF GSoC students blogs: Weekly blog #6 (week 12): 12/08 to 18/08

$
0
0

Hello there! We are at the end of GSoC! Week 12 was the last week where we’d do any major coding. We still have the next week where we do a submission (and a final check-in?), so let me keep it short and tell you what I worked on this week and what I got stuck on.

 

This week I focused on my two open PR’s - the text PR and the PyTorch PR.

 

For the text PR, I did a couple of things:

  • I updated the image tutorial and image tests with new features introduced in this PR, such as explanation “modifiers”.

  • I fleshed out (padded with comments) the text tutorial so that it looks nice.

  • I opened issues on GitHub for some TODO’s and FIXME’s.

  • One of the mentors reviewed the PR so I made any appropriate changes, including:

    • API changes - instead of letting an argument take multiple types, have separate arguments for each type.

    • An interesting change - when trying to choose a hidden layer for Grad-CAM, we will now default to one of the latter layers (closer to output). This is closer to how Grad-CAM is defined (since picking earlier layers would be like using some other gradient method for explanations).

 

One interesting issue that arose as a result of the above change (defaulting to “higher level” layers) was that my integration tests broke and the tutorial explanations didn’t look as good. With the guidance of my mentor I resolved the failing tests by explicitly picking a layer that works. For the tutorials I made a comment that using earlier layers gave better results. Interesting issues with text!

 

Next for the PyTorch PR I did the following:

  • Added code that can extract an image from an input tensor (a simple transform in torchvision), like what we do for Keras.

  • Added a very short image tutorial.

 

I got stuck on the text part of PyTorch because my test model was quite large (large word embeddings). It looks like I will have to train my own embedding layer with a small vocabulary.

 

What’s next

  • Final submission coming soon.

  • Slowly work on getting text and pytorch PR’s merged as a regular open source contributor!

    • Text PR still has some TODO’s and reviews to resolve.

    • PyTorch PR needs a text tutorial, integration tests, and unit tests. We’ll scale down so hopefully this doesn’t take too much time.

 

That’s all the technical details! I think we have one more blog next week, so I can talk about GSoC in broader terms then?

 

Thanks for reading once again!

Tomas Baltrunas

Codementor: Top 7 Compelling Reasons to Hire Ukrainian Developers

$
0
0
Find out why it’s worth hiring Ukrainian developers.

Codementor: How to Find and Hire a Python/Django Development Company

$
0
0
Learn how to find and hire a Python development company.

Stack Abuse: Debugging Python Applications with the PDB Module

$
0
0

Introduction

In this tutorial, we are going to learn how to use Python's PDB module for debugging Python applications. Debugging refers to the process of removing software and hardware errors from a software application. PDB stands for "Python Debugger", and is a built-in interactive source code debugger with a wide range of features, like pausing a program, viewing variable values at specific instances, changing those values, etc.

In this article, we will be covering the most commonly used functionalities of the PDB module.

Background

Debugging is one of the most disliked activities in software development, and at the same time, it is one of the most important tasks in the software development life cycle. At some stage, every programmer has to debug his/her code, unless he is developing a very basic software application.

There are many different ways to debug a software application. A very commonly used method is using the "print" statements at different instances of your code to see what is happening during execution. However, this method has many problems, such as the addition of extra code that is used to print the variables' values, etc. While this approach might work for a small program, tracking these code changes in a large application with many lines of code, spread over different files, can become a huge problem. The debugger solves that problem for us. It helps us find the error sources in an application using external commands, hence no changes to the code.

Note: As mentioned above, PDB is a built in Python module, so there is no need to install it from an external source.

Key Commands

To understand the main commands or tools that we have at our disposal in PDB, let's consider a basic Python program, and then try to debug it using PDB commands. This way, we will see with an example what exactly each command does.

# Filename: calc.py

operators = ['+', '-', '*', '/']
numbers = [10, 20]

def calculator():
    print("Operators available: ")
    for op in operators:
        print(op)

    print("Numbers to be used: ")
    for num in numbers:
        print(num)

def main():
    calculator()

main()

Here is the output of the script above:

Operators available:
+
-
*
/
Numbers to be used:
10
20

I have not added any comments in the code above, as it is beginner friendly and involves no complex concepts or syntax at all. It's not important to try and understand the "task" that this code achieves, as its purpose was to include certain things so that all of PDB's commands could be tested on it. Alright then, let's start!

Using PDB requires use of the Command Line Interface (CLI), so you have to run your application from the terminal or the command prompt.

Run the command below in your CLI:

$ python -m pdb calc.py

In the command above, my file's name is "calc.py", so you'll need to insert your own file name here.

Note: The -m is a flag, and it notifies the Python executable that a module needs to be imported; this flag is followed by the name of the module, which in our case is pdb.

The output of the command looks like this:

> /Users/junaid/Desktop/calc.py(3)<module>()
-> operators = [ '+', '-', '*', '/' ]
(Pdb)

The output will always have the same structure. It will start with the directory path to our source code file. Then, in brackets, it will indicate the line number from that file that PDB is currently pointing at, which in our case is "(3)". The next line, starting with the "->" symbol, indicates the line currently being pointed to.

In order to close the PDB prompt, simply enter quit or exit in the PDB prompt.

A few other things to note, if your program accepts parameters as inputs, you can pass them through the command line as well. For instance, had our program above required three inputs from the user, then this is what our command would have looked like:

$ python -m pdb calc.py var1 var2 var3

Moving on, if you had earlier closed the PDB prompt through the quit or exit command, then rerun the code file through PDB. After that, run the following command in the PDB prompt:

(Pdb) list

The output looks like this:

  1     # Filename: calc.py
  2
  3  -> operators = ['+', '-', '*', '/']
  4     numbers = [10, 20]
  5
  6     def calculator():
  7         print("Operators available: ")
  8         for op in operators:
  9             print(op)
 10
 11         print("Numbers to be used: ")
(Pdb)

This will show the first 11 lines of your program to you, with the "->" pointing towards the current line being executed by the debugger. Next, try this command in the PDB prompt:

(Pdb) list 4,6

This command should display the selected lines only, which in this case are lines 4 to 6. Here is the output:

  4     numbers = [10, 20]
  5
  6     def calculator():
(Pdb)

Debugging with Break Points

The next important thing that we will learn about is the breakpoint. Breakpoints are usually used for larger programs, but to understand them better we will see how they function on our basic example. Break points are specific locations that we declare in our code. Our code runs up to that location and then pauses. These points are automatically assigned numbers by PDB.

We have the following different options to create break points:

  1. By line number
  2. By function declaration
  3. By a condition

To declare a break point by line number, run the following command in the PDB prompt:

(Pdb) break calc.py:8

This command inserts a breakpoint at the 8th line of code, which will pause the program once it hits that point. The output from this command is shown as:

Breakpoint 1 at /Users/junaid/Desktop/calc.py: 8
(Pdb)

To declare break points on a function, run the following command in the PDB prompt:

(Pdb) break calc.calculator

In order to insert a breakpoint in this way, you must declare it using the file name and then the function name. This outputs the following:

Breakpoint 2 at /Users/junaid/Desktop/calc.py:6

As you can see, this break point has been assigned number 2 automatically, and the line number i.e. 6 at which the function is declared, is also shown.

Break points can also be declared by a condition. In that case the program will run until the condition is false, and will pause when that condition becomes true. Run the following command in the PDB prompt:

(Pdb) break calc.py:8, op == "*"

This will track the value of the op variable throughout execution and only break when its value is "*" at line 8.

To see all the break points that we have declared in the form of a list, run the following command in the PDB prompt:

(Pdb) break

The output looks like this:

Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py: 8
2   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py: 6
    breakpoint already hit 1 time
3   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py: 8
    stop only if op == "*"
(Pdb)

Lastly, let's see how we can disable, enable, and clear a specific break point at any instance. Run the following command in the PDB prompt:

(Pdb) disable 2

This will disable breakpoint 2, but will not remove it from our debugger instance.

In the output you will see the number of the disabled break point.

Disabled breakpoint 2 at /Users/junaid/Desktop/calc.py:6
(Pdb)

Lets print out the list of all break points again to see the "Enb" value for breakpoint 2:

(Pdb) break

Output:

Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py:8
2   breakpoint   keep no    at /Users/junaid/Desktop/calc.py:4 # you can see here that the "ENB" column for #2 shows "no"
    breakpoint already hit 1 time
3   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py:8
    stop only if op == "*"
(Pdb)

To re-enable break point 2, run the following command:

(Pdb) enable 2

And again, here is the output:

Enabled breakpoint 2 at /Users/junaid/Desktop/calc.py:6

Now, if you print the list of all break points again, the "Enb" column's value for breakpoint 2 should show a "yes" again.

Let's now clear breakpoint 1, which will remove it all together.

(Pdb) clear 1

The output is as follows:

Deleted breakpoint 1 at /Users/junaid/Desktop/calc.py:8
(Pdb)

If we re-print the list of breakpoints, it should now only show two breakpoint rows. Let's see the "break" command's output:

Num Type         Disp Enb   Where
2   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py:4
    breakpoint already hit 1 time
3   breakpoint   keep yes   at /Users/junaid/Desktop/calc.py:8
    stop only if op == "*"

Exactly what we expected.

Before we move ahead from this section, I want to show you all what is displayed when we actually run the code until the specified breakpoint. To do that, let's clear all the previous breakpoints and declare another breakpoint through the PDB prompt:

1. Clear all breakpoints

(Pdb) clear

After that, type "y" and hit "Enter". You should see an output like this appear:

Deleted breakpoint 2 at /Users/junaid/Desktop/calc.py:6
Deleted breakpoint 3 at /Users/junaid/Desktop/calc.py:8

2. Declare a new breakpoint

What we wish to achieve is that the code should run up until the point that the value of num variable is greater than 10. So basically, the program should pause before the number "20" gets printed.

(Pdb) break calc.py:13, num > 10

3. Run the code until this breakpoint

To run the code, use the "continue" command, which will execute the code until it hits a breakpoint or finishes:

(Pdb) continue

You should see the following output:

Operators available:
+
-
*
/
Numbers to be used:
10
> /Users/junaid/Desktop/calc.py(13)calculator()
-> print(num)

This is exactly what we expected, the program runs until that point and then pauses, now it's up to us if we wish to change anything, inspect variables, or if we want to run the script until completion. To instruct it to run to completion, run the "continue" command again. The output should be the following:

20
The program finished and will be restarted
> /Users/junaid/Desktop/calc.py(3)<module>()
-> operators = [ '+', '-', '*', '/' ]

In the above output, it can be seen that the program continues from exactly where it left off, runs the remaining part, and then restarts to allow us to debug it further if we wish. Let's move to the next section now.

Important Note: Before moving forward, clear all the breakpoints by running the "clear" command, followed by typing in "y" in the PDB prompt.

Next and Step Functions

Last, but not the least, let's study the next and step functions; these will be very frequently used when you start debugging your applications, so let's learn what they do and how they can be implemented.

The step and next functions are used to iterate throughout our code line by line; there's a very little difference between the two. While iterating, if the step function encounters a function call, it will move to the first line of that function's definition and show us exactly what is happening inside the function; whereas, if the next function encounters a function call, it will run all lines of that function in a single go and pause at the next function call.

Confused? Let's see that in an example.

Re-run the program through PDB prompt using the following command:

$ python -m pdb calc.py

Now type in continue in the PDB prompt, and keep doing that until the program reaches the end. I'm going to show a section of the whole input and output sequence below, which is sufficient to explain the point. The full sequence is quite long, and would only confuse you more, so it will be omitted.

> /Users/junaid/Desktop/calc.py(1)<module>()
-> operators = [ '+', '-', '*', '/' ]
(Pdb) step
> /Users/junaid/Desktop/calc.py(2)<module>()
-> numbers = [ 10, 20 ]
.
.
.
.
> /Users/junaid/Desktop/calc.py(6)calculator()
-> print("Operators available: " )
(Pdb) step
Operators available:
> /Users/junaid/Desktop/calc.py(8)calculator()
-> for op in operators:
(Pdb) step
> /Users/junaid/Desktop/calc.py(10)calculator()
-> print(op)
(Pdb) step
+
> /Users/junaid/Desktop/calc.py(8)calculator()
-> for op in operators:
(Pdb) step
> /Users/junaid/Desktop/calc.py(10)calculator()
-> print(op)

.
.
.
.

Now, re-run the whole program, but this time, use the "next" command instead of "step". I have shown the input and output trace for that as well.

> /Users/junaid/Desktop/calc.py(3)<module>()
-> operators = ['+', '-', '*', '/']
(Pdb) next
> /Users/junaid/Desktop/calc.py(4)<module>()
-> numbers = [10, 20]
(Pdb) next
> /Users/junaid/Desktop/calc.py(6)<module>()
-> def calculator():
(Pdb) next
> /Users/junaid/Desktop/calc.py(15)<module>()
-> def main():
(Pdb) next
> /Users/junaid/Desktop/calc.py(18)<module>()
-> main()
(Pdb) next
Operators available:
+
-
*
/
Numbers to be used:
10
20
--Return--

Alright, now that we have output trace for both of these functions, let's see how they are different. For the step function, you can see that when the calculator function is called, it moves inside that function, and iterates through it in "steps", showing us exactly what is happening in each step.

However, if you see the output trace for the next function, when "main" function is called, it doesn't show us what happens inside that function (i.e. a subsequent call to the calculator function), and then directly prints out the end result in a single go/step.

These commands are useful if you are iterating through a program and want to step through certain functions, but not others, in which case you can utilize each command for its purposes.

Conclusion

In this tutorial, we learned about a sophisticated technique for debugging python applications using an built-in module named PDB. We dove into the different troubleshooting commands that PDB provides us with, including the next and step statements, breakpoints, etc. We also applied them to a basic program to see them in action.

Real Python: How to Make a Discord Bot in Python

$
0
0

In a world where video games are so important to so many people, communication and community around games are vital. Discord offers both of those and more in one well-designed package. In this tutorial, you’ll learn how to make a Discord bot in Python so that you can make the most of this fantastic platform.

By the end of this article you’ll learn:

  • What Discord is and why it’s so valuable
  • How to make a Discord bot through the Developer Portal
  • How to create Discord connections
  • How to handle events
  • How to accept commands and validate assumptions
  • How to interact with various Discord APIs

You’ll begin by learning what Discord is and why it’s valuable.

Free Bonus:Click here to get access to a chapter from Python Tricks: The Book that shows you Python's best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.

What Is Discord?

Discord is a voice and text communication platform for gamers.

Players, streamers, and developers use Discord to discuss games, answer questions, chat while they play, and much more. It even has a game store, complete with critical reviews and a subscription service. It is nearly a one-stop shop for gaming communities.

While there are many things you can build using Discord’s APIs, this tutorial will focus on a particular learning outcome: how to make a Discord bot in Python.

What Is a Bot?

Discord is growing in popularity. As such, automated processes, such as banning inappropriate users and reacting to user requests are vital for a community to thrive and grow.

Automated programs that look and act like users and automatically respond to events and commands on Discord are called bot users. Discord bot users (or just bots) have nearly unlimited applications.

For example, let’s say you’re managing a new Discord guild and a user joins for the very first time. Excited, you may personally reach out to that user and welcome them to your community. You might also tell them about your channels or ask them to introduce themselves.

The user feels welcomed and enjoys the discussions that happen in your guild and they, in turn, invite friends.

Over time, your community grows so big that it’s no longer feasible to personally reach out to each new member, but you still want to send them something to recognize them as a new member of the guild.

With a bot, it’s possible to automatically react to the new member joining your guild. You can even customize its behavior based on context and control how it interacts with each new user.

This is great, but it’s only one small example of how a bot can be useful. There are so many opportunities for you to be creative with bots, once you know how to make them.

Note: Although Discord allows you to create bots that deal with voice communication, this article will stick to the text side of the service.

There are two key steps when you’re creating a bot:

  1. Create the bot user on Discord and register it with a guild.
  2. Write code that uses Discord’s APIs and implements your bot’s behaviors.

In the next section, you’ll learn how to make a Discord bot in Discord’s Developer Portal.

How to Make a Discord Bot in the Developer Portal

Before you can dive into any Python code to handle events and create exciting automations, you need to first create a few Discord components:

  1. An account
  2. An application
  3. A bot
  4. A guild

You’ll learn more about each piece in the following sections.

Once you’ve created all of these components, you’ll tie them together by registering your bot with your guild.

You can get started by heading to Discord’s Developer Portal.

Creating a Discord Account

The first thing you’ll see is a landing page where you’ll need to either login, if you have an existing account, or create a new account:

Discord: Account Login Screen

If you need to create a new account, then click on the Register button below Login and enter your account information.

Important: You’ll need to verify your email before you’re able to move on.

Once you’re finished, you’ll be redirected to the Developer Portal home page, where you’ll create your application.

Creating an Application

An application allows you to interact with Discord’s APIs by providing authentication tokens, designating permissions, and so on.

To create a new application, select New Application:

Discord: My Applications Screen

Next, you’ll be prompted to name your application. Select a name and click Create:

Discord: Naming an Application

Congratulations! You made a Discord application. On the resulting screen, you can see information about your application:

Discord: Application General Information

Keep in mind that any program that interacts with Discord APIs requires a Discord application, not just bots. Bot-related APIs are only a subset of Discord’s total interface.

However, since this tutorial is about how to make a Discord bot, navigate to the Bot tab on the left-hand navigation list.

Creating a Bot

As you learned in the previous sections, a bot user is one that listens to and automatically reacts to certain events and commands on Discord.

For your code to actually be manifested on Discord, you’ll need to create a bot user. To do so, select Add Bot:

Discord: Add Bot

Once you confirm that you want to add the bot to your application, you’ll see the new bot user in the portal:

Discord: Bot Created Successfully

Notice that, by default, your bot user will inherit the name of your application. Instead, update the username to something more bot-like, such as RealPythonTutorialBot, and Save Changes:

Discord: Rename Bot

Now, the bot’s all set and ready to go, but to where?

A bot user is not useful if it’s not interacting with other users. Next, you’ll create a guild so that your bot can interact with other users.

Creating a Guild

A guild (or a server, as it is often called in Discord’s user interface) is a specific group of channels where users congregate to chat.

Note: While guild and server are interchangeable, this article will use the term guild primarily because the APIs stick to the same term. The term server will only be used when referring to a guild in the graphical UI.

For example, say you want to create a space where users can come together and talk about your latest game. You’d start by creating a guild. Then, in your guild, you could have multiple channels, such as:

  • General Discussion: A channel for users to talk about whatever they want
  • Spoilers, Beware: A channel for users who have finished your game to talk about all the end game reveals
  • Announcements: A channel for you to announce game updates and for users to discuss them

Once you’ve created your guild, you’d invite other users to populate it.

So, to create a guild, head to your Discord home page:

Discord: User Account Home Page

From this home page, you can view and add friends, direct messages, and guilds. From here, select the + icon on the left-hand side of the web page to Add a Server:

Discord: Add Server

This will present two options, Create a server and Join a Server. In this case, select Create a server and enter a name for your guild:

Discord: Naming a Server

Once you’ve finished creating your guild, you’ll be able to see the users on the right-hand side and the channels on the left:

Discord: Newly Created Server

The final step on Discord is to register your bot with your new guild.

Adding a Bot to a Guild

A bot can’t accept invites like a normal user can. Instead, you’ll add your bot using the OAuth2 protocol.

Technical Detail:OAuth2 is a protocol for dealing with authorization, where a service can grant a client application limited access based on the application’s credentials and allowed scopes.

To do so, head back to the Developer Portal and select the OAuth2 page from the left-hand navigation:

Discord: Application OAuth2

From this window, you’ll see the OAuth2 URL Generator.

This tool generates an authorization URL that hits Discord’s OAuth2 API and authorizes API access using your application’s credentials.

In this case, you’ll want to grant your application’s bot user access to Discord APIs using your application’s OAuth2 credentials.

To do this, scroll down and select bot from the SCOPES options and Administrator from BOT PERMISSIONS:

Discord: Application Scopes and Bot Permissions

Now, Discord has generated your application’s authorization URL with the selected scope and permissions.

Disclaimer: While we’re using Administrator for the purposes of this tutorial, you should be as granular as possible when granting permissions in a real-world application.

Select Copy beside the URL that was generated for you, paste it into your browser, and select your guild from the dropdown options:

Discord: Add Bot to a Server

Click Authorize, and you’re done!

Note: You might get a reCAPTCHA before moving on. If so, you’ll need to prove you’re a human.

If you go back to your guild, then you’ll see that the bot has been added:

Discord: Bot Added to Guild

In summary, you’ve created:

  • An application that your bot will use to authenticate with Discord’s APIs
  • A bot user that you’ll use to interact with other users and events in your guild
  • A guild in which your user account and your bot user will be active
  • A Discord account with which you created everything else and that you’ll use to interact with your bot

Now, you know how to make a Discord bot using the Developer Portal. Next comes the fun stuff: implementing your bot in Python!

How to Make a Discord Bot in Python

Since you’re learning how to make a Discord bot with Python, you’ll be using discord.py.

discord.py is a Python library that exhaustively implements Discord’s APIs in an efficient and Pythonic way. This includes utilizing Python’s implementation of Async IO.

Begin by installing discord.py with pip:

$ pip install -U discord.py

Now that you’ve installed discord.py, you’ll use it to create your first connection to Discord!

Creating a Discord Connection

The first step in implementing your bot user is to create a connection to Discord. With discord.py, you do this by creating an instance of Client:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')client=discord.Client()@client.eventasyncdefon_ready():print(f'{client.user} has connected to Discord!')client.run(token)

A Client is an object that represents a connection to Discord. A Client handles events, tracks state, and generally interacts with Discord APIs.

Here, you’ve created a Client and implemented its on_ready() event handler, which handles the event when the Client has established a connection to Discord and it has finished preparing the data that Discord has sent, such as login state, guild and channel data, and more.

In other words, on_ready() will be called (and your message will be printed) once client is ready for further action. You’ll learn more about event handlers later in this article.

When you’re working with secrets such as your Discord token, it’s good practice to read it into your program from an environment variable. Using environment variables helps you:

  • Avoid putting the secrets into source control
  • Use different variables for development and production environments without changing your code

While you could export DISCORD_TOKEN={your-bot-token}, an easier solution is to save a .env file on all machines that will be running this code. This is not only easier, since you won’t have to export your token every time you clear your shell, but it also protects you from storing your secrets in your shell’s history.

Create a file named .env in the same directory as bot.py:

# .env
DISCORD_TOKEN={your-bot-token}

You’ll need to replace {your-bot-token} with your bot’s token, which you can get by going back to the Bot page on the Developer Portal and clicking Copy under the TOKEN section:

Discord: Copy Bot Token

Looking back at the bot.py code, you’ll notice a library called dotenv. This library is handy for working with .env files. load_dotenv() loads environment variables from a .env file into your shell’s environment variables so that you can use them in your code.

Install dotenv with pip:

$ pip install -U python-dotenv

Finally, client.run() runs your Client using your bot’s token.

Now that you’ve set up both bot.py and .env, you can run your code:

$ python bot.py
RealPythonTutorialBot#9643 has connected to Discord!

Great! Your Client has connected to Discord using your bot’s token. In the next section, you’ll build on this Client by interacting with more Discord APIs.

Interacting With Discord APIs

Using a Client, you have access to a wide range of Discord APIs.

For example, let’s say you wanted to write the name and identifier of the guild that you registered your bot user with to the console.

First, you’ll need to add a new environment variable:

# .env
DISCORD_TOKEN={your-bot-token}
DISCORD_GUILD={your-guild-name}

Don’t forget that you’ll need to replace the two placeholders with actual values:

  1. {your-bot-token}
  2. {your-guild-name}

Remember that Discord calls on_ready(), which you used before, once the Client has made the connection and prepared the data. So, you can rely on the guild data being available inside on_ready():

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')GUILD=os.getenv('DISCORD_GUILD')client=discord.Client()@client.eventasyncdefon_ready():forguildinclient.guilds:ifguild.name==GUILD:breakprint(f'{client.user} is connected to the following guild:\n'f'{guild.name}(id: {guild.id})')client.run(TOKEN)

Here, you looped through the guild data that Discord has sent client, namely client.guilds. Then, you found the guild with the matching name and printed a formatted string to stdout.

Note: Even though you can be pretty confident at this point in the tutorial that your bot is only connected to a single guild (so client.guilds[0] would be simpler), it’s important to realize that a bot user can be connected to many guilds.

Therefore, a more robust solution is to loop through client.guilds to find the one you’re looking for.

Run the program to see the results:

$ python bot.py
RealPythonTutorialBot#9643 is connected to the following guild:RealPythonTutorialServer(id: 571759877328732195)

Great! You can see the name of your bot, the name of your server, and the server’s identification number.

Another interesting bit of data you can pull from a guild is the list of users who are members of the guild:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')GUILD=os.getenv('DISCORD_GUILD')client=discord.Client()@client.eventasyncdefon_ready():forguildinclient.guilds:ifguild.name==GUILD:breakprint(f'{client.user} is connected to the following guild:\n'f'{guild.name}(id: {guild.id})\n')members='\n - '.join([member.nameformemberinguild.members])print(f'Guild Members:\n - {members}')client.run(TOKEN)

By looping through guild.members, you pulled the names of all of the members of the guild and printed them with a formatted string.

When you run the program, you should see at least the name of the account you created the guild with and the name of the bot user itself:

$ python bot.py
RealPythonTutorialBot#9643 is connected to the following guild:RealPythonTutorialServer(id: 571759877328732195)Guild Members: - aronq2 - RealPythonTutorialBot

These examples barely scratch the surface of the APIs available on Discord, be sure to check out their documentation to see all that they have to offer.

Next, you’ll learn about some utility functions and how they can simplify these examples.

Using Utility Functions

Let’s take another look at the example from the last section where you printed the name and identifier of the bot’s guild:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')GUILD=os.getenv('DISCORD_GUILD')client=discord.Client()@client.eventasyncdefon_ready():forguildinclient.guilds:ifguild.name==GUILD:breakprint(f'{client.user} is connected to the following guild:\n'f'{guild.name}(id: {guild.id})')client.run(TOKEN)

You could clean up this code by using some of the utility functions available in discord.py.

discord.utils.find() is one utility that can improve the simplicity and readability of this code by replacing the for loop with an intuitive, abstracted function:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')GUILD=os.getenv('DISCORD_GUILD')client=discord.Client()@client.eventasyncdefon_ready():guild=discord.utils.find(lambdag:g.name==GUILD,client.guilds)print(f'{client.user} is connected to the following guild:\n'f'{guild.name}(id: {guild.id})')client.run(TOKEN)

find() takes a function, called a predicate, which identifies some characteristic of the element in the iterable that you’re looking for. Here, you used a particular type of anonymous function, called a lambda, as the predicate.

In this case, you’re trying to find the guild with the same name as the one you stored in the DISCORD_GUILD environment variable. Once find() locates an element in the iterable that satisfies the predicate, it will return the element. This is essentially equivalent to the break statement in the previous example, but cleaner.

discord.py has even abstracted this concept one step further with the get() utility:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')GUILD=os.getenv('DISCORD_GUILD')client=discord.Client()@client.eventasyncdefon_ready():guild=discord.utils.get(client.guilds,name=GUILD)print(f'{client.user} is connected to the following guild:\n'f'{guild.name}(id: {guild.id})')client.run(TOKEN)

get() takes the iterable and some keyword arguments. The keyword arguments represent attributes of the elements in the iterable that must all be satisfied for get() to return the element.

In this example, you’ve identified name=GUILD as the attribute that must be satisfied.

Technical Detail: Under the hood, get() actually uses the attrs keyword arguments to build a predicate, which it then uses to call find().

Now that you’ve learned the basics of interacting with APIs, you’ll dive a little deeper into the function that you’ve been using to access them: on_ready().

Responding to Events

You already learned that on_ready() is an event. In fact, you might have noticed that it is identified as such in the code by the client.eventdecorator.

But what is an event?

An event is something that happens on Discord that you can use to trigger a reaction in your code. Your code will listen for and then respond to events.

Using the example you’ve seen already, the on_ready() event handler handles the event that the Client has made a connection to Discord and prepared its response data.

So, when Discord fires an event, discord.py will route the event data to the corresponding event handler on your connected Client.

There are two ways in discord.py to implement an event handler:

  1. Using the client.event decorator
  2. Creating a subclass of Client and overriding its handler methods

You already saw the implementation using the decorator. Next, take a look at how to subclass Client:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')classCustomClient(discord.Client):asyncdefon_ready(self):print(f'{self.user} has connected to Discord!')client=CustomClient()client.run(token)

Here, just like before, you’ve created a client variable and called .run() with your Discord token. The actual Client is different, however. Instead of using the normal base class, client is an instance of CustomClient, which has an overridden on_ready() function.

There is no difference between the two implementation styles of events, but this tutorial will primarily use the decorator version because it looks similar to how you implement Bot commands, which is a topic you’ll cover in a bit.

Technical Detail: Regardless of how you implement your event handler, one thing must be consistent: all event handlers in discord.py must be coroutines.

Now that you’ve learned how to create an event handler, let’s walk through some different examples of handlers you can create.

Welcoming New Members

Previously, you saw the example of responding to the event where a member joins a guild. In that example, your bot user could send them a message, welcoming them to your Discord community.

Now, you’ll implement that behavior in your Client, using event handlers, and verify its behavior in Discord:

# bot.pyimportosimportdiscordfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')client=discord.Client()@client.eventasyncdefon_ready():print(f'{client.user.name} has connected to Discord!')@client.eventasyncdefon_member_join(member):awaitmember.create_dm()awaitmember.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')client.run(token)

Like before, you handled the on_ready() event by printing the bot user’s name in a formatted string. New, however, is the implementation of the on_member_join() event handler.

on_member_join(), as its name suggests, handles the event of a new member joining a guild.

In this example, you used member.create_dm() to create a direct message channel. Then, you used that channel to .send() a direct message to that new member.

Technical Detail: Notice the await keyword before member.create_dm() and member.dm_channel.send().

await suspends the execution of the surrounding coroutine until the execution of each coroutine has finished.

Now, let’s test out your bot’s new behavior.

First, run your new version of bot.py and wait for the on_ready() event to fire, logging your message to stdout:

$ python bot.py
RealPythonTutorialBot has connected to Discord!

Now, head over to Discord, log in, and navigate to your guild by selecting it from the left-hand side of the screen:

Discord: Navigate to Server

Select Invite People just beside the guild list where you selected your guild. Check the box that says Set this link to never expire and copy the link:

Discord: Copy Invite Link

Now, with the invite link copied, create a new account and join the guild using your invite link:

Discord: Accept Invite

First, you’ll see that Discord introduced you to the guild by default with an automated message. More importantly though, notice the badge on the left-hand side of the screen that notifies you of a new message:

Discord: Direct Message Notification

When you select it, you’ll see a private message from your bot user:

Discord: Direct Message

Perfect! Your bot user is now interacting with other users with minimal code.

Next, you’ll learn how to respond to specific user messages in the chat.

Responding to Messages

Let’s add on to the previous functionality of your bot by handling the on_message() event.

on_message() occurs when a message is posted in a channel that your bot has access to. In this example, you’ll respond to the message '99!' with a one-liner from the television show Brooklyn Nine-Nine:

@client.eventasyncdefon_message(message):ifmessage.author==client.user:returnbrooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]ifmessage.content=='99!':response=random.choice(brooklyn_99_quotes)awaitmessage.channel.send(response)

The bulk of this event handler looks at the message.content, checks to see if it’s equal to '99!', and responds by sending a random quote to the message’s channel if it is.

The other piece is an important one:

ifmessage.author==client.user:return

Because a Client can’t tell the difference between a bot user and a normal user account, your on_message() handler should protect against a potentially recursive case where the bot sends a message that it might, itself, handle.

To illustrate, let’s say you want your bot to listen for users telling each other 'Happy Birthday'. You could implement your on_message() handler like this:

@client.eventasyncdefon_message(message):if'happy birthday'inmessage.content.lower():awaitmessage.channel.send('Happy Birthday! 🎈🎉')

Aside from the potentially spammy nature of this event handler, it also has a devastating side effect. The message that the bot responds with contains the same message it’s going to handle!

So, if one person in the channel tells another “Happy Birthday,” then the bot will also chime in… again… and again… and again:

Discord: Happy Birthday Message Repetition

That’s why it’s important to compare the message.author to the client.user (your bot user), and ignore any of its own messages.

So, let’s fix bot.py:

# bot.pyimportosimportrandomimportdiscordfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')client=discord.Client()@client.eventasyncdefon_ready():print(f'{client.user.name} has connected to Discord!')@client.eventasyncdefon_member_join(member):awaitmember.create_dm()awaitmember.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')@client.eventasyncdefon_message(message):ifmessage.author==client.user:returnbrooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]ifmessage.content=='99!':response=random.choice(brooklyn_99_quotes)awaitmessage.channel.send(response)client.run(token)

Don’t forget to import random at the top of the module, since the on_message() handler utilizes random.choice().

Run the program:

$ python bot.py
RealPythonTutorialBot has connected to Discord!

Finally, head over to Discord to test it out:

Discord: Quotes From Brooklyn Nine-Nine

Great! Now that you’ve seen a few different ways to handle some common Discord events, you’ll learn how to deal with errors that event handlers may raise.

Handling Exceptions

As you’ve seen already, discord.py is an event-driven system. This focus on events extends even to exceptions. When one event handler raises an Exception, Discord calls on_error().

The default behavior of on_error() is to write the error message and stack trace to stderr. To test this, add a special message handler to on_message():

# bot.pyimportosimportrandomimportdiscordfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')client=discord.Client()@client.eventasyncdefon_ready():print(f'{client.user.name} has connected to Discord!')@client.eventasyncdefon_member_join(member):awaitmember.create_dm()awaitmember.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')@client.eventasyncdefon_message(message):ifmessage.author==client.user:returnbrooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]ifmessage.content=='99!':response=random.choice(brooklyn_99_quotes)awaitmessage.channel.send(response)elifmessage.content=='raise-exception':raisediscord.DiscordExceptionclient.run(token)

The new raise-exception message handler allows you to raise a DiscordException on command.

Run the program and type raise-exception into the Discord channel:

Discord: Raise Exception Message

You should now see the Exception that was raised by your on_message() handler in the console:

$ python bot.py
RealPythonTutorialBot has connected to Discord!Ignoring exception in on_messageTraceback (most recent call last):  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/client.py", line 255, in _run_event    await coro(*args, **kwargs)  File "bot.py", line 42, in on_message    raise discord.DiscordExceptiondiscord.errors.DiscordException

The exception was caught by the default error handler, so the output contains the message Ignoring exception in on_message. Let’s fix that by handling that particular error. To do so, you’ll catch the DiscordException and write it to a file instead.

The on_error() event handler takes the event as the first argument. In this case, we expect the event to be 'on_message'. It also accepts *args and **kwargs as flexible, positional and keyword arguments passed to the original event handler.

So, since on_message() takes a single argument, message, we expect args[0] to be the message that the user sent in the Discord channel:

@client.eventasyncdefon_error(event,*args,**kwargs):withopen('err.log','a')asf:ifevent=='on_message':f.write(f'Unhandled message: {args[0]}\n')else:raise

If the Exception originated in the on_message() event handler, you .write() a formatted string to the file err.log. If another event raises an Exception, then we simply want our handler to re-raise the exception to invoke the default behavior.

Run bot.py and send the raise-exception message again to view the output in err.log:

$ cat err.log
Unhandled message: <Message id=573845548923224084 pinned=False author=<Member id=543612676807327754 name='alexronquillo' discriminator='0933' bot=False nick=None guild=<Guild id=571759877328732195 name='RealPythonTutorialServer' chunked=True>>>

Instead of only a stack trace, you have a more informative error, showing the message that caused on_message() to raise the DiscordException, saved to a file for longer persistence.

Technical Detail: If you want to take the actual Exception into account when you’re writing your error messages to err.log, then you can use functions from sys, such as exc_info().

Now that you have some experience handling different events and interacting with Discord APIs, you’ll learn about a subclass of Client called Bot, which implements some handy, bot-specific functionality.

Connecting a Bot

A Bot is a subclass of Client that adds a little bit of extra functionality that is useful when you’re creating bot users. For example, a Bot can handle events and commands, invoke validation checks, and more.

Before you get into the features specific to Bot, convert bot.py to use a Bot instead of a Client:

# bot.pyimportosimportrandomfromdotenvimportload_dotenv# 1fromdiscord.extimportcommandsload_dotenv()token=os.getenv('DISCORD_TOKEN')# 2bot=commands.Bot(command_prefix='!')@bot.eventasyncdefon_ready():print(f'{bot.user.name} has connected to Discord!')bot.run(token)

As you can see, Bot can handle events the same way that Client does. However, notice the differences between Client and Bot:

  1. Bot is imported from the discord.ext.commands module.
  2. The Bot initializer requires a command_prefix, which you’ll learn more about in the next section.

The extensions library, ext, offers several interesting components to help you create a Discord Bot. One such component is the Command.

Using Bot Commands

In general terms, a command is an order that a user gives to a bot so that it will do something. Commands are different from events because they are:

  • Arbitrarily defined
  • Directly called by the user
  • Flexible, in terms of their interface

In technical terms, a Command is an object that wraps a function that is invoked by a text command in Discord. The text command must start with the command_prefix, defined by the Bot object.

Let’s take a look at an old event to better understand what this looks like:

# bot.pyimportosimportrandomimportdiscordfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')client=discord.Client()@client.eventasyncdefon_message(message):ifmessage.author==client.user:returnbrooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]ifmessage.content=='99!':response=random.choice(brooklyn_99_quotes)awaitmessage.channel.send(response)client.run(TOKEN)

Here, you created an on_message() event handler, which receives the message string and compares it to a pre-defined option: '99!'.

Using a Command, you can convert this example to be more specific:

# bot.pyimportosimportrandomfromdiscord.extimportcommandsfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')bot=commands.Bot(command_prefix='!')@bot.command(name='99')asyncdefnine_nine(ctx):brooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]response=random.choice(brooklyn_99_quotes)awaitctx.send(response)bot.run(token)

There are several important characteristics to understand about using Command:

  1. Instead of using bot.event like before, you use bot.command(), passing the invocation command (name) as its argument.

  2. The function will now only be called when !99 is mentioned in chat. This is different than the on_message() event, which was executed any time a user sent a message, regardless of the content.

  3. The command must be prefixed with the exclamation point (! ) because that’s the command_prefix that you defined in the initializer for your Bot.

  4. Any Command function (technically called a callback) must accept at least one parameter, called ctx, which is the Context surrounding the invoked Command.

A Context holds data such as the channel and guild that the user called the Command from.

Run the program:

$ python bot.py

With your bot running, you can now head to Discord to try out your new command:

Discord: Brooklyn Nine-Nine Command

From the user’s point of view, the practical difference is that the prefix helps formalize the command, rather than simply reacting to a particular on_message() event.

This comes with other great benefits as well. For example, you can invoke the help command to see all the commands that your Bot handles:

Discord: Help Command

If you want to add a description to your command so that the help message is more informative, simply pass a help description to the .command() decorator:

# bot.pyimportosimportrandomfromdiscord.extimportcommandsfromdotenvimportload_dotenvload_dotenv()token=os.getenv('DISCORD_TOKEN')bot=commands.Bot(command_prefix='!')@bot.command(name='99',help='Responds with a random quote from Brooklyn 99')asyncdefnine_nine(ctx):brooklyn_99_quotes=['I\'m the human form of the 💯 emoji.','Bingpot!',('Cool. Cool cool cool cool cool cool cool, ''no doubt no doubt no doubt no doubt.'),]response=random.choice(brooklyn_99_quotes)awaitctx.send(response)bot.run(token)

Now, when the user invokes the help command, your bot will present a description of your command:

Discord: Informative Help Description

Keep in mind that all of this functionality exists only for the Bot subclass, not the Client superclass.

Command has another useful functionality: the ability to use a Converter to change the types of its arguments.

Converting Parameters Automatically

Another benefit of using commands is the ability to convert parameters.

Sometimes, you require a parameter to be a certain type, but arguments to a Command function are, by default, strings. A Converter lets you convert those parameters to the type that you expect.

For example, if you want to build a Command for your bot user to simulate rolling some dice (knowing what you’ve learned so far), you might define it like this:

@bot.command(name='roll_dice',help='Simulates rolling dice.')asyncdefroll(ctx,number_of_dice,number_of_sides):dice=[str(random.choice(range(1,number_of_sides+1)))for_inrange(number_of_dice)]awaitctx.send(', '.join(dice))

You defined roll to take two parameters:

  1. The number of dice to roll
  2. The number of sides per die

Then, you decorated it with .command() so that you can invoke it with the !roll_dice command. Finally, you .send() the results in a message back to the channel.

While this looks correct, it isn’t. Unfortunately, if you run bot.py, and invoke the !roll_dice command in your Discord channel, you’ll see the following error:

$ python bot.py
Ignoring exception in command roll_dice:Traceback (most recent call last):  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 63, in wrapped    ret = await coro(*args, **kwargs)  File "bot.py", line 40, in roll    for _ in range(number_of_dice)TypeError: 'str' object cannot be interpreted as an integerThe above exception was the direct cause of the following exception:Traceback (most recent call last):  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 860, in invoke    await ctx.command.invoke(ctx)  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 698, in invoke    await injected(*ctx.args, **ctx.kwargs)  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 72, in wrapped    raise CommandInvokeError(exc) from excdiscord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'str' object cannot be interpreted as an integer

In other words, range() can’t accept a str as an argument. Instead, it must be an int. While you could cast each value to an int, there is a better way: you can use a Converter .

In discord.py, a Converter is defined using Python 3’s function annotations:

@bot.command(name='roll_dice',help='Simulates rolling dice.')asyncdefroll(ctx,number_of_dice:int,number_of_sides:int):dice=[str(random.choice(range(1,number_of_sides+1)))for_inrange(number_of_dice)]awaitctx.send(', '.join(dice))

You added : int annotations to the two parameters that you expect to be of type int. Try the command again:

Discord: Bot Dice-Rolling Command

With that little change, your command works! The difference is that you’re now converting the command arguments to int, which makes them compatible with your function’s logic.

Note: A Converter can be any callable, not merely data types. The argument will be passed to the callable, and the return value will be passed into the Command.

Next, you’ll learn about the Check object and how it can improve your commands.

Checking Command Predicates

A Check is a predicate that is evaluated before a Command is executed to ensure that the Context surrounding the Command invocation is valid.

In an earlier example, you did something similar to verify that the user who sent a message that the bot handles was not the bot user, itself:

ifmessage.author==client.user:return

The commands extension provides a cleaner and more usable mechanism for performing this kind of check, namely using Check objects.

To demonstrate how this works, assume you want to support a command !create_channel <channel_name> that creates a new channel. However, you only want to allow administrators the ability to create new channels with this command.

First, you’ll need to create a new member role in the admin. Go into the Discord guild and select the {Server Name} → Server Settings menu:

Discord: Server Settings Screen

Then, select Roles from the left-hand navigation list:

Discord: Navigate to Roles

Finally select the + sign next to ROLES and enter the name admin and select Save Changes:

Discord: Create New Admin Role

Now, you’ve created an admin role that you can assign to particular users. Next, you’ll update bot.py to Check the user’s role before allowing them to initiate the command:

# bot.pyimportosimportdiscordfromdiscord.extimportcommandsfromdotenvimportload_dotenvload_dotenv()TOKEN=os.getenv('DISCORD_TOKEN')bot=commands.Bot(command_prefix='!')@bot.command(name='create-channel')@commands.has_role('admin')asyncdefcreate_channel(ctx,channel_name='real-python'):guild=ctx.guildexisting_channel=discord.utils.get(guild.channels,name=channel_name)ifnotexisting_channel:print(f'Creating a new channel: {channel_name}')awaitguild.create_text_channel(channel_name)bot.run(TOKEN)

In bot.py, you have a new Command function, called create_channel() which takes an optional channel_name and creates that channel. create_channel() is also decorated with a Check called has_role().

You also use discord.utils.get() to ensure that you don’t create a channel with the same name as an existing channel.

If you run this program as it is and type !create-channel into your Discord channel, then you’ll see the following error message:

$ python bot.py
Ignoring exception in command create-channel:Traceback (most recent call last):  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 860, in invoke    await ctx.command.invoke(ctx)  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 691, in invoke    await self.prepare(ctx)  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 648, in prepare    await self._verify_checks(ctx)  File "/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 598, in _verify_checks    raise CheckFailure('The check functions for command {0.qualified_name} failed.'.format(self))discord.ext.commands.errors.CheckFailure: The check functions for command create-channel failed.

This CheckFailure says that has_role('admin') failed. Unfortunately, this error only prints to stdout. It would be better to report this to the user in the channel. To do so, add the following event:

@bot.eventasyncdefon_command_error(ctx,error):ifisinstance(error,commands.errors.CheckFailure):awaitctx.send('You do not have the correct role for this command.')

This event handles an error event from the command and sends an informative error message back to the original Context of the invoked Command.

Try it all again, and you should see an error in the Discord channel:

Discord: Role Check Error

Great! Now, to resolve the issue, you’ll need to give yourself the admin role:

Discord: Grant Admin Role

With the admin role, your user will pass the Check and will be able to create channels using the command.

Note: Keep in mind that in order to assign a role, your user will have to have the correct permissions. The easiest way to ensure this is to sign in with the user that you created the guild with.

When you type !create-channel again, you’ll successfully create the channel real-python:

Discord: Navigate to New Channel

Also, note that you can pass the optional channel_name argument to name the channel to whatever you want!

With this last example, you combined a Command, an event, a Check, and even the get() utility to create a useful Discord bot!

Conclusion

Congratulations! Now, you’ve learned how to make a Discord bot in Python. You’re able to build bots for interacting with users in guilds that you create or even bots that other users can invite to interact with their communities. Your bots will be able to respond to messages and commands and numerous other events.

In this tutorial, you learned the basics of creating your own Discord bot. You now know:

  • What Discord is
  • Why discord.py is so valuable
  • How to make a Discord bot in the Developer Portal
  • How to create a Discord connection in Python
  • How to handle events
  • How to create a Bot connection
  • How to use bot commands, checks, and converters

To read more about the powerful discord.py library and take your bots to the next level, read through their extensive documentation. Also, now that you’re familiar with Discord APIs in general, you have a better foundation for building other types of Discord applications.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Wingware Blog: Moving Code with Refactoring in Wing Pro

$
0
0

In this issue of Wing Tips we explain how to quickly move functions, methods, classes, and other symbols around in Python code, using Wing Pro's MoveSymbol refactoring operation.

This operation takes care of updating all the points of reference for the symbol that is being moved. For example, if a function is moved from one module to another then Wing will update all the points of call for that function to import the module it has been moved into and invoke the function from there.

Example

Here's a simple example that moves a class from one file to another:

/images/blog/refactor-move/move-example.gif

Shown Above: Right-click to initiate Move Symbol, select target location testtarget.py, execute the move operation, select the moved function, and then select the added import statement.

Notice that Wing adds the necessary importtesttarget to testmove.py and changes the two places where the class MyTestClass is used to reference the class in its new location in testtarget.py. Using refactoring is much easier and less prone to errors than making edits of this type manually.

Try It Yourself

You can easily try this out in your copy of Wing Pro, by selecting any symbol in your Python code base, and choosing MoveSymbol from the Refactor menu. As in the above example, you will be asked to choose the target location for the move, which can either be the top level of any module, or within a class or def. An unwanted move can be backed out with the Revert button in the Refactoring tool.



That's it for now! We'll be back next soon with more Wing Tips for Wing Python IDE.

Python Software Foundation: Humble Bundle by No Starch supports the Python Software Foundation!

$
0
0
We are super excited to announce that the Python Software Foundation is featured as a charity in a Humble Bundle by No Starch Press this month.



This bundle features books such as Python Playground, Mission Python, Invent your own Computer Games with Python, and so much more.

Click here to see all the books being featured and GET THE BUNDLE before it closes! The bundle will run from August 19th to September 2nd (11am Pacific). Proceeds received help charities such as the Python Software Foundation (PSF). Once you click to get your bundle, you can also choose where your money goes if you'd like to customize the split of proceeds.



Humble Bundle sells games, ebooks, software, and other digital content. Their mission is to support charity while providing awesome content to customers at great prices. Thanks to past Humble Bundles that the PSF has been a part of, this program has helped the PSF raise more than $300,000 since 2017! The PSF and the Python community thank Humble Bundle and all of the featured products that have selected the PSF as one of their charities. This funding has had a positive impact for Pythonistas all around the world.

No Starch is a long time community contributor supporting the PSF in various Bundles and supporting Young Coder classes that happen at PyCon US.  “As one of the leading publishers of Python books worldwide, No StarchPress is very excited to support the organization at the core of the Python programming language” said No Starch Press Founder Bill Pollock. “Python is at the core of so much technical work today and very much at the core of our publishing program.”

The PSF staff and board of directors send a big "Thank You!" to everyone involved.


Viewing all 22873 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>