Categories
Python Answers

How to use raw_input without pressing enter in Python?

Sometimes, we want to use raw_input without pressing enter in Python.

In this article, we’ll look at how to use raw_input without pressing enter in Python.

How to use raw_input without pressing enter in Python?

To use raw_input without pressing enter in Python, we can use the pynput library.

To install it, we run

pip install pynput

Then we use it by writing

from pynput import keyboard

print('Press s or n to continue:')

with keyboard.Events() as events:
    event = events.get(1e6)
    if event.key == keyboard.KeyCode.from_char('s'):
        print("hello")

We use keyboard.Events to create an event object.

Then we get the keyboard event with

event = events.get(1e6)

We then check if the s key pressed with

event.key == keyboard.KeyCode.from_char('s')

If it is, then we print 'hello'.

Otherwise, pynput will block the program until s is pressed.

Conclusion

To use raw_input without pressing enter in Python, we can use the pynput library.

Categories
Python Answers

How to access command line arguments with Python?

Sometimes, we want to access command line arguments with Python.

In this article, we’ll look at how to access command line arguments with Python.

How to access command line arguments with Python?

To access command line arguments with Python, we can use the sys.argv property.

For instance, we write

import sys

print(sys.argv)

to print out all the command line arguments in a list with print and sys.argv.

Conclusion

To access command line arguments with Python, we can use the sys.argv property.

Categories
Python Answers

How to obfuscate Python code?

Sometimes, we want to obfuscate Python code.

In this article, we’ll look at how to obfuscate Python code.

How to obfuscate Python code?

To obfuscate Python code, we can use the py_compile option with python.

To use it, we run

python -OO -m py_compile foo.py

to run python with py_compile to compile foo.py into a .pyo file.

A .pyo file is a Python script file that has the obfuscated version of the original code.

Some of the variables names can be recognized in the file and comments and docstrings are stripped out.

Conclusion

To obfuscate Python code, we can use the py_compile option with python.

Categories
Python Answers

How to get multiline input from user with Python?

Sometimes, we want to get multiline input from user with Python

In this article, we’ll look at how to get multiline input from user with Python.

How to get multiline input from user with Python?

To get multiline input from user with Python, we can call input in an infinite loop.

For instance, we write

contents = []
while True:
    try:
        line = input()
    except EOFError:
        break
    contents.append(line)

to call input in the infinite while loop to get its input value.

Then we catch the EOFError and stop the look with break if there’s an EOFError raised.

The error will be raised if Ctrl-D or Ctrl-Z is pressed.

Outside the try-except statement, we call contents.append to append the entered line into the contents list.

Conclusion

To get multiline input from user with Python, we can call input in an infinite loop.

Categories
Python Answers

How to slice 2d array into smaller 2d arrays with Python?

Sometimes, we want to slice 2d array into smaller 2d arrays with Python.

In this article, we’ll look at how to slice 2d array into smaller 2d arrays with Python.

How to slice 2d array into smaller 2d arrays with Python?

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy split method.

For instance, we write

a = np.arange(30).reshape([5,6])
a1 = np.split(a,3,axis=1) 

to create an array with arange.

Then we call reshape to reshape the returned array to a 5×6 2d array.

Next, we call split with array a, 3 and the axis` argument set to 1 to get a list of 3 5×2 arrays.

Conclusion

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy reshape method.