Categories
Python Answers

How to get a string after a specific substring with Python?

Sometimes, we want to get a string after a specific substring with Python.

In this article, we’ll look at how to get a string after a specific substring with Python.

How to get a string after a specific substring with Python?

To get a string after a specific substring with Python, we can use the split method.

For instance, we write

my_string = "hello world , i'm a beginner "
s = my_string.split("world", 1)[1] 

to call my_string/split with the string that we want to split by and the number that limits the number of splits.

As a result, we call split to split the string once with 'world' as the separator.

And we use [1] to get the 2nd entry from the returned string list.

Conclusion

To get a string after a specific substring with Python, we can use the split method.

Categories
Python Answers

How to take a screenshot via a Python script on Linux

Sometimes, we want to take a screenshot via a Python script on Linux.

In this article, we’ll look at how to take a screenshot via a Python script on Linux.

How to take a screenshot via a Python script on Linux

To take a screenshot via a Python script on Linux, we can use pyscreenshot.

To install it, we run

pip install Pillow pyscreenshot

Then we use it by writing

import pyscreenshot as ImageGrab

im = ImageGrab.grab()
im.show()

im = ImageGrab.grab(bbox=(10, 10, 500, 500))
im.show()

ImageGrab.grab_to_file('im.png')

to call grab to take a screenshot of the whole screen by calling it without arguments.

We call grab with the bbox argument to take the screenshot of part of the screen.

And we call grab_to_file to take a screenshot and save it to a file.

Conclusion

To take a screenshot via a Python script on Linux, we can use pyscreenshot.

Categories
Python Answers

How to fix Matplotlib savefig outputs blank image with Python?

Sometimes, we want to fix Matplotlib savefig outputs blank image with Python.

In this article, we’ll look at how to fix Matplotlib savefig outputs blank image with Python.

How to fix Matplotlib savefig outputs blank image with Python?

To fix Matplotlib savefig outputs blank image with Python, we call gcf and show before we call savefig.

For instance, we write

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('test.png', dpi=100)

to call gcf to get the current figure and save it.

Then we call show

And then we save the figure saved by calling gcf by calling savefig with the file name.

Conclusion

To fix Matplotlib savefig outputs blank image with Python, we call gcf and show before we call savefig.

Categories
Python Answers

How to run commands over ssh with Python?

Sometimes, we want to run commands over ssh with Python.

in this articl,e we’ll look at how to run commands over ssh with Python.

How to run commands over ssh with Python?

To run commands over ssh with Python, we can use the Paramiko library.

To install it, we run

pip install paramiko

Then we can use it by writing

import paramiko

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

to create an ssh client with

ssh = paramiko.SSHClient()

Then we call ssh.connect to connect to the server with the username and password.

And then we call exec_command to run the command we want.

If we use ssh keys, we write

import paramiko

k = paramiko.RSAKey.from_private_key_file(keyfilename)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)

to load the private key file with

paramiko.RSAKey.from_private_key_file(keyfilename)

Then we make the connection with connect.

Conclusion

To run commands over ssh with Python, we can use the Paramiko library.

Categories
Python Answers

How to compare object instances for equality by their attributes with Python?

Sometimes, we want to compare object instances for equality by their attributes with Python.

In this article, we’ll look at how to compare object instances for equality by their attributes with Python.

How to compare object instances for equality by their attributes with Python?

To compare object instances for equality by their attributes with Python, we can add the __eq__ method into our class.

For instance, we write

class MyClass:
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar
        
    def __eq__(self, other): 
        if not isinstance(other, MyClass):
            return NotImplemented
        return self.foo == other.foo and self.bar == other.bar

to create the MyClass class.

In it, we add the foo and bar instance variables.

And we add the __eq__ method to let us check for object equality by checking the value of each attribute.

We use

self.foo == other.foo and self.bar == other.bar

to do the check if self and other are both MyClass instances.

Then we can do the equality check with x == y where x and y are both MyClass instances.

Conclusion

To compare object instances for equality by their attributes with Python, we can add the __eq__ method into our class.