Categories
Python Answers

How to bind an unbound method with Python?

Sometimes, we want to bind an unbound method with Python.

In this article, we’ll look at how to bind an unbound method with Python.

How to bind an unbound method with Python?

To bind an unbound method with Python, we can use the types.MethodType method.

For instance, we write:

import types


def f(self):
    print(self)


class C:
    pass


meth = types.MethodType(f, C)
meth()

We have the f function that prints the value of self.

Then we set self to C by using types.MethodType(f, C) and assign the returned function to meth.

Therefore, when we call meth, we see:

<class '__main__.C'>

printed.

Conclusion

To bind an unbound method with Python, we can use the types.MethodType method.

Categories
Python Answers

How to filter a dictionary according to an arbitrary condition function with Python?

Sometimes, we want to filter a dictionary according to an arbitrary condition function with Python

In this article, we’ll look at how to filter a dictionary according to an arbitrary condition function with Python

How to filter a dictionary according to an arbitrary condition function with Python?

To filter a dictionary according to an arbitrary condition function with Python, we can use dictionary comprehension.

For instance, we write:

points = {'a': (3, 4), 'b': (1, 2), 'c': (5, 5), 'd': (3, 3)}
new_points = {k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}
print(new_points)

We define the points dictionary and we want to return a new dictionary with the tuples entries all less than 5.

To do this, we write:

new_points = {k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}

v is the dictionary entry’s value being looped through, which is the tuple.

We assign that to new_points and we get that it’s:

{'a': (3, 4), 'b': (1, 2), 'd': (3, 3)}

Conclusion

To filter a dictionary according to an arbitrary condition function with Python, we can use dictionary comprehension.

Categories
Python Answers

How to calculate a directory’s size using Python?

Sometimes, we want to calculate a directory’s size using Python.

In this article, we’ll look at how to calculate a directory’s size using Python.

How to calculate a directory’s size using Python?

To calculate a directory’s size using Python, we can use the pathlib’s glob method to get all entries in the file system from the root.

And then we can use the is_file method to check if each file is a file.

If it is, then we use the stat method to get the file size.

And we use the sum method to sum all the file sizes together.

For instance, we write:

from pathlib import Path

root_directory = Path('.')
s = sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())
print(s)

We set root_directory to the root path object.

Then we call root_directory.glob with '**/*' to get everything under root_directory.

And we filter out non-files with if f.is_file().

Then we get the size of each file with f.stat().st_size.

And we call sum to add the file sizes together.

Therefore, s is a number in bytes like 16629.

Conclusion

To calculate a directory’s size using Python, we can use the pathlib’s glob method to get all entries in the file system from the root.

And then we can use the is_file method to check if each file is a file.

If it is, then we use the stat method to get the file size.

And we use the sum method to sum all the file sizes together.

Categories
Python Answers

How to make requests via a proxy with Python?

Sometimes, we want to make requests via a proxy with Python.

In this article, we’ll look at how to make requests via a proxy with Python.

How to make requests via a proxy with Python?

To make requests via a proxy with Python, we can use the requests module.

For instance, we write:

import requests

url = 'http://www.google.com'
proxy = '206.253.164.108'
r = requests.get(url, proxies={"http": proxy})
print(r)

We call requests.get with the url to make the request to and a dictionary with an entry with the 'http' key set to a string with the proxy’s address.

Therefore, if the request is successful, we get something like:

<Response [200]>

returned.

Conclusion

To make requests via a proxy with Python, we can use the requests module.

Categories
Python Answers

How to add N seconds to datetime.time in Python?

Sometimes, we want to add N seconds to datetime.time in Python.

In this article, we’ll look at how to add N seconds to datetime.time in Python.

How to add N seconds to datetime.time in Python?

To add N seconds to datetime.time in Python, we can add a timedelta object to a datetime.

For instance, we write:

import datetime

a = datetime.datetime(100, 2, 2, 11, 30, 59)
b = a + datetime.timedelta(0, 3)
print(a.time())
print(b.time())

We create a datetime object with a = datetime.datetime(100, 2, 2, 11, 30, 59).

And then we add 3 seconds with a + datetime.timedelta(0, 3).

We then get the time value in hours:minutes:seconds format with the time method and print the returned result.

Therefore, we have:

11:30:59
11:31:02

from the print output.

Conclusion

To add N seconds to datetime.time in Python, we can add a timedelta object to a datetime.