Categories
Python Answers

How to add a progress bar with Python?

Sometimes, we want to add a progress bar with Python.

In this article, we’ll look at how to add a progress bar with Python.

How to add a progress bar with Python?

To add a progress bar with Python, we can use the tqdm package.

To install it, we run:

pip install tqdm

Then we can use it by writing:

from time import sleep
from tqdm import tqdm
for i in tqdm(range(10)):
    sleep(3)

We call tqdm with the range(10) iterator to print out the progress 10 times.

Therefore, we should see the latest progress printed every 3 seconds.

Conclusion

To add a progress bar with Python, we can use the tqdm package.

Categories
Python Answers

How to set environment variables in Python?

Sometimes, we want to set environment variables in Python.

In this article, we’ll look at how to set environment variables in Python.

How to set environment variables in Python?

To set environment variables in Python, we can put an entry the os.environ dictionary.

For instance, we write:

import os

os.environ["DEBUG"] = "1"
print(os.environ["DEBUG"])

to set the DEBUG environment variable to '1'.

And to get the value of the DEBUG environment variable, we write:

print(os.environ["DEBUG"])

and we should see '1' printed.

Conclusion

To set environment variables in Python, we can put an entry the os.environ dictionary.

Categories
Python Answers

How to take subarrays from Python numpy array with given stride/step size?

Sometimes, we want to take subarrays from Python numpy array with given stride/step size.

In this article, we’ll look at how to take subarrays from Python numpy array with given stride/step size.

How to take subarrays from Python numpy array with given stride/step size?

To take subarrays from Python numpy array with given stride/step size, we can use the lib.atride_ticks.as_strided method.

For instance, we write:

import numpy as np


def strided_app(a, L, S):
    nrows = ((a.size - L) // S) + 1
    n = a.strides[0]
    return np.lib.stride_tricks.as_strided(a,
                                           shape=(nrows, L),
                                           strides=(S * n, n))


a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
s = strided_app(a, L=5, S=3)
print(s)

We create the strided_app function that takes the array a.

L is the length of the chunk.

And S is the stride or step size.

We compute the number of rows with ((a.size - L) // S) + 1.

Then we get the first chunk with a.strides[0].

And then we call np.lib.stride_tricks.as_strided to compute the chunks with the shape of the nested array and the strides set to the start and end index of the range of items from the original array used to form the chunks in the new array.

Therefore, s is:

[[ 1  2  3  4  5]
 [ 4  5  6  7  8]
 [ 7  8  9 10 11]]

Conclusion

To take subarrays from Python numpy array with given stride/step size, we can use the lib.atride_ticks.as_strided method.

Categories
Python Answers

How to extract an attribute value with Python BeautifulSoup?

Sometimes, we want to extract an attribute value with Python BeautifulSoup.

In this article, we’ll look at how to extract an attribute value with Python BeautifulSoup.

How to extract an attribute value with Python BeautifulSoup?

To extract an attribute value with Python BeautifulSoup, we can use the find_all method.

For instance, we write:

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.crummy.com/software/BeautifulSoup/bs4/doc/")
soup = BeautifulSoup(r.text, 'html.parser')

res = soup.find_all(attrs={"class": 'document'})

print(res)

We make a GET request to get the content of https://www.crummy.com/software/BeautifulSoup/bs4/doc/.

Then we get the HTML text with r.text and use that as the argument of the BeautifulSoup constructor.

Then we find all the elements with the class attribute set to document with:

a = soup.find_all(attrs={"class": 'document'})

Conclusion

To extract an attribute value with Python BeautifulSoup, we can use the find_all method.

Categories
Python Answers

How to test if a string contains one of the substrings in a list in Python Pandas?

Sometimes, we want to test if a string contains one of the substrings in a list in Python Pandas.

In this article, we’ll look at how to test if a string contains one of the substrings in a list in Python Pandas.

How to test if a string contains one of the substrings in a list in Python Pandas?

To test if a string contains one of the substrings in a list in Python Pandas, we can use the str.contains method with a regex pattern to find all the matches.

For instance, we write:

import pandas as pd

s = pd.Series(['cat', 'hat', 'dog', 'fog', 'pet'])
df = pd.DataFrame([('cat', 1000.0), ('hat', 2000000.0), ('dog', 1000.0),
                   ('fog', 330000.0), ('pet', 330000.0)],
                  columns=['col1', 'col2'])
r = df[s.str.contains('cat|pet')]
print(r)

We create a series with the pd.Series constructor.

Then we create a DataFrame with the pd.DataFrame constructor.

Next, we call s.str.contains with the words we’re looking for separated by a |.

And then we assign the matches to r.

Therefore, r is:

  col1      col2
0  cat    1000.0
4  pet  330000.0

Conclusion

To test if a string contains one of the substrings in a list in Python Pandas, we can use the str.contains method with a regex pattern to find all the matches.