Python: Difference between revisions

From David's Wiki
Line 144: Line 144:
===Numpy===
===Numpy===
===Matplotlib===
===Matplotlib===
{{main | Matplotlib}}
Matplotlib is the main library used for making graphs.<br>
Matplotlib is the main library used for making graphs.<br>
Alternatively, there are also Python bindings for ggplot2<br>
Alternatively, there are also Python bindings for ggplot2<br>
[https://matplotlib.org/examples/ Examples]<br>
[https://matplotlib.org/examples/ Examples]<br>
[https://matplotlib.org/3.1.1/gallery/index.html Gallery]
[https://matplotlib.org/3.1.1/gallery/index.html Gallery]
====Saving a plot====
<syntaxhighlight lang="python">
plt.savefig("my_figure.png", dpi=450)
</syntaxhighlight>

Revision as of 13:11, 12 December 2019


Installation

Use Anaconda.

  • Add C:\Users\[username]\Anaconda3\Scripts to your path
  • Run conda init in your bash
  • Run conda config --set auto_activate_base false

Basic Usage

How to use Python 3.

pip

Pip is the package manager for python.
Your package requirements should be written to requirements.txt
Install all requirements using pip install -r requirements.txt

Ternary Operator

Reference

is_nice = True
state = "nice" if is_nice else "not nice"

Strings

String Interpolation

Reference
Python has 3 syntax variations for string interpolation.

name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')

print("%s %s" %('Hello','World',))

name = 'world'
program ='python'
print('Hello {name}!This is{program}.'.format(name=name,program=program))

Lambda Function

lambda x: x * 2

Filesystem Read and Write

List all files in a folder

Reference

gazeDir = "Gaze_txt_files"
# List of folders in root folder
gazeFolders = [path.join(gazeDir, x) for x in os.listdir(gazeDir)]
# List of files 2 folders down
gazeFiles = [path.join(x, y) for x in gazeFolders for y in os.listdir(x)]

Read/Write entire text file into a list

Reading
[1]

with open('C:/path/numbers.txt') as f:
    lines = f.read().splitlines()

Writing
[2]

with open('your_file.txt', 'w') as f:
    f.write("\n".join(my_list))


Create Directory if it doesn't exist

Reference

def ensure_dir_exists(dir_path):
    if not os.path.exists(dir_path):
        try:
            os.makedirs(dir_path)
        except OSError as exc: # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise


# Example usage to create new_dir folder
ensure_dir_exists("new_dir")
# or
ensure_dir_exists(os.path.dirname("new_dir/my_file.txt"))

Copying or moving a file or folder

Copying
Shutil docs

import shutil

# Copy a file
shutil.copy2('original.txt', 'duplicate.txt')

# Move a file
shutil.move('original.txt', 'my_folder/original.txt')

Regular Expressions (Regex)

Reference

import re
myReg = re.compile(r'height:(\d+)cm')
myMatch = re.match(myReg, "height:33cm");
print(myMatch[1])
# 33
Notes
  • re.match will return None if there is no match

Spawning Processes

Use subprocess to spawn other programs.

import subprocess
subprocess.run(["ls", "-l"], cwd="/")


Misc

If you are writing a script with functions you want to be included in other scripts, use __name__ to detect if your script is being run or being imported. What does if __name__ == "__main__" do?

if __name__ == "__main__":

Anaconda

How to use Anaconda:

# Create an environment
conda create -n tf2 python

# Activate an environment
conda activate tf2

# Change version of Python
conda install python=3.6

Libraries

Numpy

Matplotlib

Matplotlib is the main library used for making graphs.
Alternatively, there are also Python bindings for ggplot2
Examples
Gallery