Categories
Python Answers

How to detect keyboard input in a script from the terminal with Python?

Sometimes, we want to detect keyboard input in a script from the terminal with Python.

In this article, we’ll look at how to detect keyboard input in a script from the terminal with Python.

How to detect keyboard input in a script from the terminal with Python?

To detect keyboard input in a script from the terminal with Python, we can use pynput.

To install it, we run

pip install pynput

Then we use it by writing

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        return False

with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

to define the on_press and on_release functions to listen for keypresses and key up events.

We get the value pressed from the key parameter.

And then we add the listeners with the keyboard.Listener class with on_press set to on_press and on_release set to on_release.

Then we call listener.join to add them as listeners.

Conclusion

To detect keyboard input in a script from the terminal with Python, we can use pynput.

Categories
Python Answers

How to use proxy with Python urllib2?

Sometimes, we want to use proxy with Python urllib2.

In this article, we’ll look at how to use proxy with Python urllib2.

How to use proxy with Python urllib2?

To use proxy with Python urllib2, we can use the ProxyHandler class.

For instance, we write

proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com')

to create a ProxyHandler object.

Then we call build_opened with proxy to use the proxy.

And then we call install_opener with opener to use the opener.

Finally, we call urlopen with the URL to make a GET request via the proxy.

Conclusion

To use proxy with Python urllib2, we can use the ProxyHandler class.

Categories
Python Answers

How to use Python Pandas read_csv with URL?

Sometimes, we want to use Python Pandas read_csv with URL.

In this article, we’ll look at how to use Python Pandas read_csv with URL.

How to use Python Pandas read_csv with URL?

To use Python Pandas read_csv with URL, we can call read_csv directly with a url.

For instance, we write

import pandas as pd

url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c = pd.read_csv(url)

to call read_csv with the url with the csv to read it into a data frame.

Conclusion

To use Python Pandas read_csv with URL, we can call read_csv directly with a url.

Categories
Python Answers

How to find differences between elements of a list with Python?

Sometimes, we want to find differences between elements of a list with Python.

In this article, we’ll look at how to find differences between elements of a list with Python.

How to find differences between elements of a list with Python?

To find differences between elements of a list with Python, we can use the zip.

For instance, we write

t = [1, 3, 6]
diffs =  [j-i for i, j in zip(t[:-1], t[1:])]

to call zip with t[:-1] and t[1:] to zip the items in t that are next to each other together.

Then we subtract them with j - i.

And we put the differences of the values in a list.

Conclusion

To find differences between elements of a list with Python, we can use the zip.

Categories
Python Answers

How to get list of values for list of keys with Python dictionaries?

Sometimes, we want to get list of values for list of keys with Python dictionaries.

In this article, we’ll look at how to get list of values for list of keys with Python dictionaries.

How to get list of values for list of keys with Python dictionaries?

To get list of values for list of keys with Python dictionaries, we can use list comprehension.

For instance, we write

l = [mydict[x] for x in mykeys]

to get the keys from mykeys and get the values of each key x in the mydict dict with mydict[x].

And then we put the values in a list.

Conclusion

To get list of values for list of keys with Python dictionaries, we can use list comprehension.