- Python
- Regex
- Kahoot!
-
Conda is an open source package management system and environment management system that runs on Windows, macOS, Linux and z/OS. Conda quickly installs, runs and updates packages and their dependencies.
-
Reference: https://docs.conda.io/en/latest/
- Terminal
- PyCharm
- VS Code
-
Emacs
- Last week I was doing my Haskell homework on Emacs
- And I installed a
haskell-mode
- And I installed a
- Emacs has a built-in
python-mode
- Syntax highlighting
- Indentation
- ...
- Last week I was doing my Haskell homework on Emacs
>>> import cmath
>>> cmath.e ** (cmath.pi * complex(0,1)) + 1 # hopefully 0?
- It is a package for Python3
- Similar with
optparse
- Reference: https://docs.python.org/3/library/argparse.html
- The first step in using the
argparse
is creating anArgumentParser
object:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
parser.add_argument('-r', '--repeat', action="store_true", help = "output lines can be repeated")
args = parser.parse_args()
$ python shuf.py -i 1-10
- a simple
Namespace
object will be built up from attributes parsed out of the command line
>>> args
Namespace(accumulate=<built-in function max>, repeat=False)
- Used in Deep Learning
- Part of the Pytorch MNIST Example:
def main():
# Create a Parser
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
# Add Arguments
parser.add_argument('--epochs', type=int, default=14, metavar='N',
help='number of epochs to train (default: 14)')
parser.add_argument('--lr', type=float, default=1.0, metavar='LR',
help='learning rate (default: 1.0)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--no-mps', action='store_true', default=False,
help='disables macOS GPU training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
# Parsing Arguments
args = parser.parse_args()
# Body
use_cuda = not args.no_cuda and torch.cuda.is_available()
use_mps = not args.no_mps and torch.backends.mps.is_available()
torch.manual_seed(args.seed)
-
I'm not going to cover it this time
because I forget most of it
-
Learn how to learn things quickly
- read docs and tutorials
-
Divide and Conquer
-
arguments can be parsed correctly
-
manage input
-
different cases, right?
What might be useful?
structural pattern matching (only in Python 3.10)
-
-
Shuffle
-
manage output
-
-
Read the documents and examples
-
Good luck!
Reference:
https://cheatography.com/davechild/cheat-sheets/regular-expressions/
Yuxing's Slide in Winter 2022
grep
has two modes:- basic
- extended (with option -E)
grep -E
-
^
Start of the string-
^bash
matchesbash_profile
but notI like bash
-
-
$
End of the string-
shell$
matchesbash shell
but notshell scripting
-
- ...
google is your friend if you are interested in learning more
*
0 or morebash*
matchesbas
,bash
,bashhhhhhhhh
+
1 or morebash*
matchesbash
,bashhh
, but notbas
?
0 or 1bash*
matchesbas
,bash
, but notbashhh
{3}
exactly 3bash{3}
matchesbashhh
{3,}
3 or morebash{3,}
matchesbashhh
,bashhhhhhhhhh
, ...
{1,3}
1, 2, or 3bash{1,3}
matchesbash
,bashh
, andbashhh
-
.
any single character (except newline\n
)s.t
matchessit
,sat
,set
, ....
-
[]
any signle character contained[abc]
matchesa
,b
, orc
[a-z]
matches any letter froma
toz
[0-9A-Za-z]
matches any number from 0-9 and any letter
-
[^a]
any single character that is NOT a- Caret inside a brackets means not
[^0-9]
means no number
-
()
groupa(bc)*
matchesa
,abc
,abcbc
, ... but notabbc
-
(a|b)
a or b-
(abc|def)
matchesabc
,def
-
$ grep -E '(abc|def)' RET
-
-
\
add it to the front of metacharacters to escape^ [ . $ { * ( \ + ) | ? < >
-
In your project, you may want users to input valid phone numbers
-
10-digit, ...
-
Enter a phone number:
Format: 123-45-678
submit -
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
-
123-456-7890 (123) 456-7890 123 456 7890 123.456.7890 +91 (123) 456-7890
-
-
Email address?
Week 3 Regex worksheet
Write a basic regular expression that causes grep to match lines ending in the characters ‘dog’.
It should match:
‘bulldog’
‘smol dog’
‘hotdog’
It should not match:
‘doge’
‘dogecoin’
‘doggo’
‘woof dogs’
‘sinnoh remakes’
dog$ <-- Answer
Write a basic regular expression that checks whether or not a vowel is present
[aeiou] <-- Solution1
[aeiou]\|y <-- Solution2
Write a basic regular expression that causes grep to match lines solely consisting of alphabetical characters (A-Za-z) and digit characters (0-9), where no alphabetical characters appear before digit characters.
e.g.: 123abc
^[0-9][A-Za-z]$ <-- it will only match a single number/letter
^[0-9]*[A-Za-z]*$ <-- Correct
^[0-9]*.[A-Za-z]*$ <-- it will match any other symbol like $
^[[:digit:]]*[[:alpha:]]*$ <-- Correct
Write an extended regular expression that causes grep -E to match lines that either contain at least one digit or the string “flower”. It should be as short as possible. The optimal solution is 12 characters long. (If you like this sort of thing, check out code golfing!)
([0-9]|flower)
<-- Correct!
[0-9f](lower)? <-- Will match a single f
(0-9|flower) <-- Will only match 0-9
instead of a single number
[0-9] | (flower)
<-- Optimal Solution!!!