Useful tools for Python

Python Tutor

Free online Python code visualization, Python learning tool.

Python Tutor
Python Tutor

http://pythontutor.com/

Anaconda

Easy package management

Packed with many useful Python tools

Anaconda
Anaconda
Anaconda - Software
Anaconda – Software

https://www.anaconda.com/distribution/#download-section

Jupyter Notebook

Jupyter notebook is like a magic notebook for Python. It can be used to share Notes, algebra, data analytics, code etc easily.

Jupyter Notebook
Jupyter Notebook

https://jupyter.org/install

(We can use Anaconda to install it easily)

IPython

IPython is a interactive shell for Python.

Supports Automatic indenting, bash shell commands, many built-in functions etc.

https://ipython.org/

Skulpt

Skulpt is a online Python environment built via javascript. Use with CodeMirror, we can do basic Python programming.

Skulpt
Skulpt

http://skulpt.org/


Simple ways to improve/optimize Python performance

1 Use built-in function when available

Use built-in function when available
Use built-in function when available

2 Use List comprehensions to Filter a list

Loop
Loop
Filter
Filter
List comprehensions
List comprehensions

3 Use “try” accordingly

# Slow
class Fruit(object):
    name='Apple'
    
fruit=Fruit()
    if hasattr(fruit,'name'):
        fruit.name
 
 
# Quicker
class Fruit(object):
    name='Apple'
    
fruit=Fruit()
    try:
        fruit.name
    except AttributeError:
        pass

4 Use “in” to check list rather than if (Use built-in function)

in vs for loop and if
in vs for loop and if

5 Deduplication

For loop and if vs set
For loop and if vs set

6 Sort

sorted
sorted
.sort()
.sort()

7 Put iterative loop into function

Iterative loop vs iterative loop in function
Iterative loop vs iterative loop in function

8 Check boolean

if var:
 
# Is faster than
 
if var is True:
 
# Is faster than
 
if var==True:

9 Check is the list is empty

if not list:
 
# Is faster than 
 
if list==[]:
 
# Is faster than
 
if len(list)==0:

10 Object vs list

list() and dict() are slower than [] directly returning a list.


How to: Convert string/text to ASCII in python & How to: Convert ASCII to string/text in python

Method 1

#String we want to convert to ASCII
my_string = "AAAasdasfd, a[sdfi       94q,p asf "
 
#Convert to ASCII
results_in_ASCII = list(my_string.encode())
 
#Convert back to string
results_in_string = bytes(results_in_ASCII).decode()
 
#Print ASCII
print(results_in_ASCII)
 
print()
 
#Print String
print(results_in_string)
Method 1
Method 1

Method 2

#String we want to convert to ASCII
my_string = "Hello, world!"
 
#Convert to ASCII
results_in_ASCII = [ord(x) for x in my_string]
 
#Convert back to string
results_in_string = ''.join(chr(i) for i in results_in_ASCII)
 
#Print ASCII
print(results_in_ASCII)
 
print()
 
#Print String
print(results_in_string)
Method 2
Method 2

Resource

How do I convert a list of ascii values to a string in python?


Kali Linux – pip/pip3 install -r requirements.txt fail

The Error

When executing following command in Kali Linux

pip install -r requirements.txt
 
OR
 
pip3 install -r requirements.txt

We get error

Command “python setup.py egg_info” failed with error code 1 in ….

Or other errors

We can give following fix a try

The Fix

Use following command instead

pip install --upgrade --force-reinstall -r requirements.txt
 
OR
 
pip3 install --upgrade --force-reinstall -r requirements.txt 

How to: upgrade/update Anaconda

Keywords: Python, anaconda, conda

Upgrade Anaconda & all packages to latest version:

1 Find and launch Anaconda Prompt

Anaconda from Windows start menu
Anaconda from Windows start menu

2 Using following command to update all packages including Anaconda itself

conda update --all
Anaconda Prompt -> conda update --all
Anaconda Prompt -> conda update –all
Anaconda Prompt -> conda update --all -> finished
Anaconda Prompt -> conda update –all -> finished

(Note: This way, it will drop all the version constraints from the history and tries to make everything as new as it can)

Bonus:

We can also use “conda update anaconda=VersionNumber” to update Anaconda to specified version

e.g.

conda update anaconda=1.9.7