Saturday, July 15, 2023

Ubuntu customizations compendium

Do this when moving into a new Ubuntu machine on Windows Subsystem for Linux:

Brighten bash colors and brighten the bash prompt:

.bashrc

LS_COLORS='rs=0:di=1;35:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS

PS1='\e[37;1m\u@\h \e[35m\w/ \e[0m\$ '

------------------------------------

 

Turn off the bells

.inputrc:

 set bell-style none

------------------------------------

 More bells, brighten colors in VIM, and fix double-characters in VIM

.vimrc

 set background=dark
set t_u7=
set belloff=all

------------------------------------

 



Sunday, July 3, 2022

Python - File handling

# Read a file

try:
    f = open("demofile.txt")
    f.close()
    print("Accessed file OK")
except FileNotFoundError:
    print("File does not exist")
finally:
    print("All done.")

"""

Syntax:
open("filename", "mode")

Default mode values:
r = read
t = text
    
These are the same
f = open("demofile.txt")
f = open("demofile.txt", "rt")

Open Modes:
r = read
a = append = Open for appending, creates if file does not exist
w = write = Open for writing, creates if file does not exist
x = create = Creates the file for writing, returns error if already exists

Handling modes:
t = text
b = binary

"""


# Open, read, and display a file
f = open("c:\\temp\\demofile.txt", "r")
print(f.read())
f.close()

# Open and read 5 characters
f = open("demofile.txt", "r")
# "This "
print(f.read(5))
f.close()


# Open and read a line
f = open("demofile.txt", "r")
# "This is"
print(f.readline())
f.close()

# Open and read two lines
f = open("demofile.txt", "r")
# "This is"
print(f.readline())
print(f.readline())
f.close()

# Open and loop through each line of the file
f = open("demofile.txt", "r")
for x in f:
    print(x)
f.close

# Append to a file
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close


# Open and loop through each line of the file
f = open("demofile2.txt", "r")
for x in f:
    print(x)
f.close

# Overwrite a file if it exists
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

# Delete a file
import os
os.remove("demofile2.txt")

# If the file exists, delete it
if os.path.exists("demofile3.txt"):
    os.remove("demofile3.txt")
else:
    print("The file does not exist")



   

Saturday, July 2, 2022

Python - String formatting

price = 49
txt = "The price is {} dollars"
# The price is 49 dollars
print(txt.format(price))

name = "Fred"

txt = "The price is {} dollars, {}."
# The price is 49 dollars, Fred.
print(txt.format(price, name))

txt = "The price is {:.2f} dollars"
#The price is 49.00 dollars
print(txt.format(price))

quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
# I want 3 pieces of item number 567 for 49.00 dollars.
print(myorder.format(quantity, itemno, price))

myorder = "I have a {carname}, it is a {model}."
# I have a Ford, it is a Mustang.
print(myorder.format(carname = "Ford", model = "Mustang"))


Python - User input

# User input for a string
username = input("Enter username:")

print("Hello, " + username)

      
# Note that this only gets a string.

# It does not scan for key presses



Python - Error handling (try, except, else, finally)

x = "hello world"


try:
    print(x)
    # This will cause an error
    #y = 3 / 0
except NameError:
    # Catch a specific error type
    print("Forgot to define x")
except:
    # Catch all other errors
    print("An exception occured")
else:
    # Run this if there was no error
    print("All is well")
finally:
    # Do this regardless of error status
    print("That's all folks")

    
    
# Throw an error on purpose
x = -1

if x < 0:
    raise Exception("Sorry, no numbers below zero")




Python - Regular Expressions

# Import the regular expression module
import re

txt = "The rain in Spain"

# This searches for:
# ^The = "The" at the beginning of the line (^)
# Spain$ = "Spain" at the end of the line ($)
x = re.search("^The.*Spain$", txt)

print(x)

# Pretty much any populated variable evaluates to True
# So if we got a match, that value is tucked into x
# Which means this will evaluate to True if we got a match
if bool(x) == True:
    print("We got a match")

else:
    print("We did not geta match")

# Split returns a list where the string has been split at each match
# So the next line returns all the words separated by a space:
# The, rain, in, Spain
words = re.split(" ", txt)
print(words)

# Sub replaces one or many matches with a string
new = re.sub("Spain", "England", txt)
# The rain in England
print(new)

# Review regular expression matching
# A quick review sheet is here:
# https://www.w3schools.com/python/python_regex.asp


Python - Math

# Built-in math functions

myValues = (5, 10, 25)

# minimum, maximum
x = min(myValues)
y = max(myValues)

# 5, 25
print(x)
print(y)

# Absolute value
x = abs(-7.25)
# 7.25
print(x)

# x to the power of y
z = pow(4,3)
# 64
print(z)

print("----")


# Imported math functions
import math

x = math.sqrt(64)
print(x)

# Ceiling = round up to nearest integer
# Floor = round down to nearest integer

x = math.ceil(1.4)
y = math.floor(1.4)

# 2, 1
print(x)
print(y)

# PI is a constant (3.14....)
x = math.pi
# 3.14....
print(x)