Categories
Python Answers

How to compute the moving average or running mean with Python NumPy?

Sometimes, we want to compute the moving average or running mean with Python NumPy.

In this article, we’ll look at how to compute the moving average or running mean with Python NumPy.

How to compute the moving average or running mean with Python NumPy?

To compute the moving average or running mean with Python NumPy, we can use the SciPy uniform_filter1d method.

For instance, we write

import numpy as np
from scipy.ndimage.filters import uniform_filter1d

N = 1000
x = np.random.random(100000)
y = uniform_filter1d(x, size=N)

to create a array with

x = np.random.random(100000)

Then we calculate the running mean by calling uniform_filter1d with array x size set to N.

Conclusion

To compute the moving average or running mean with Python NumPy, we can use the SciPy uniform_filter1d method.

Categories
Python Answers

How to filter ForeignKey choices in a Django ModelForm with Python?

Sometimes, we want to filter ForeignKey choices in a Django ModelForm with Python.

In this article, we’ll look at how to filter ForeignKey choices in a Django ModelForm with Python.

How to filter ForeignKey choices in a Django ModelForm with Python?

To filter ForeignKey choices in a Django ModelForm with Python, we can use the filter method.

For instance, we write

form.rate.queryset = Rate.objects.filter(company_id=the_company.id)

to set the values of the rates field to the queryset returned by

Rate.objects.filter(company_id=the_company.id)

Conclusion

To filter ForeignKey choices in a Django ModelForm with Python, we can use the filter method.

Categories
Python Answers

How to use Selenium with scrapy for dynamic page with Python?

Sometimes, we want to use Selenium with scrapy for dynamic page with Python.

In this article, we’ll look at how to use Selenium with scrapy for dynamic page with Python.

How to use Selenium with scrapy for dynamic page with Python?

To use Selenium with scrapy for dynamic page with Python, we can create our own scrapy.Spider subclass.

For instance, we write

import scrapy
from selenium import webdriver


class ProductSpider(scrapy.Spider):
    name = "product_spider"
    allowed_domains = ["example.store"]
    start_urls = ["http://example.store"]

    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self, response):
        self.driver.get(response.url)

        while True:
            next = self.driver.find_element_by_xpath('//td[@class="pagn-next"]/a')

            try:
                next.click()
            except:
                break

        self.driver.close()

to create the ProductSpider class that’s a subclass of the scrapy.Spider class.

We initialize the Firefox webdriver in the __init__ method.

And then we add the parse method that calls driver.get method to open the page at the response.url.

Next, we loop through the td elements in the while loop.

And we call click to click on the element that’s found.

Once we’re done, we call close to close the webdriver.

Conclusion

To use Selenium with scrapy for dynamic page with Python, we can create our own scrapy.Spider subclass.

Categories
Python Answers

How to improve subplot size and spacing with many subplots in Python matplotlib?

Sometimes, we want to improve subplot size and spacing with many subplots in Python matplotlib.

In this article, we’ll look at how to improve subplot size and spacing with many subplots in Python matplotlib.

How to improve subplot size and spacing with many subplots in Python matplotlib?

To improve subplot size and spacing with many subplots in Python matplotlib, we can call tight_layout to tighten the spacing.

For instance, we write

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, ncols=4)
fig.tight_layout()

plt.show()

to call plt.suplots to create 16 subplots.

Then we call tight_layout to use tight layout for displaying our subplots.

As a result, there should be less space between subplots than the default layout.

Conclusion

To improve subplot size and spacing with many subplots in Python matplotlib, we can call tight_layout to tighten the spacing.

Categories
Python Answers

How to fix pg_config executable not found in Python?

Sometimes, we want to fix pg_config executable not found in Python

In this article, we’ll look at how to fix pg_config executable not found in Python.

How to fix pg_config executable not found in Python?

To fix pg_config executable not found in Python, we can install a few packages.

To install the missing packages, we run

sudo apt-get install libpq-dev python-dev

to install the libpq-dev and python-dev packages.

Conclusion

To fix pg_config executable not found in Python, we can install a few packages.