- Editor
- Anaconda
- Libraries
- Collections
- Best practises
- Comments and documentation
- Regular Expressions
- Files and folders
-
editor: VSCodium
-
ipython and ipython notebook
-
build-ins
- name
- doc : documentation in beginning of document
"""some doc here"""
-
setup.py building the project to restructure the whole project.
-
install tree
-
install anaconda (with python)
codewith.mu - online editor for python for beginners. pygame.org - online til at lave spil.
- http.server start og se alle filer i mappen.
- library numpy
- django for
List, Tuple, Dictionary
capitalsDict = {"paris":"France", "London":"England","Lisbon":"Portugal"} reverseKeyValueDict = {capitalsDict[key]:key for key in capitalsDict} //Will return {"France":"Paris",...etc..}
- Named tuple is used to find value by a name instead of a numeric index.
Person = collections.namedtuple('Person', 'name age gender')
andbob = Person(name='Bob', age=30, gender='male')
- Create a virtual environment for installs:
conda create -n exercises
andsource activate exercises
to start up the environment. - When conda environment is running -> install module:
conda install -c conda-forge pyperclip
conda-forge was necesary. conda-forge is a conda repository with many packages to install.
And documentation.
- Documentation: first line in function or class or module is written as documentation string with triple quotes:
"""bla bla bla """
(tripple quotes is used for multiline string).
https://en.wikibooks.org/wiki/Python_Programming/Idioms
ONLINE tester: https://pythex.org/
\n
backslash is used to escape charatersraw text
is text that does not need escaping eg.'\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d'
vsr'\d\d\d-\d\d\d-\d\d\d\d
import re
to work with regular expression.phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
phoneNumRegEx is the regex object.search()
is a method on the regex objectmatchFound = phoneNumRegex.search(someString)
matchFound.group()
matchFound.group() will be the actual phone number that was found.- Test a particular pattern on a text online: [http:// regexpal.com/](http:// regexpal.com/)
groups()
use groups to get a tuple of alle matching stringsgroup(0)
all parts of the first matchgroup(1)
first part of parenthes matchre.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
eg415
of match415-333-6545
. Notice how\(
looks for an actual parenthes.- pipe
|
is used forOR
like:re.compile(r'frog|toad')
will match either frog or toad and return the first one found. - ex:
re.compile(r'(frog|toad)leg')
to find either frogleg og toadleg. findall()
instead of search() will find all occurences.re.compile(r'Bat(wo)?man')
a question mark here means 'wo' is optionalphoneRegex = re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d\d')
the first 3 digits are optional.(\d)*
zero or more times with a digit when postfixing an asterix.(\d)+
one or more times when postfixing a plus.(\d){3}
only match 3 digits exactly(hi ){3,5}
matches 'hi hi hi' and 'hi hi hi hi' and 'hi hi hi hi hi'(hi ){3,5}?
with a question mark we get the shortest possible match rather than the default longest match. 'hi hi hi' rather than 'hi hi hi hi hi'.
short hand character | description |
---|---|
\d | Any numeric digit from 0 to 9. |
\D | Any character that is not a numeric digit from 0 to 9. |
\w | Any letter, numeric digit, or the underscore character. (Think of this as matching “word” characters.) |
\W | Any character that is not a letter, numeric digit, or the underscore character. |
\s | Any space, tab, or newline character. (Think of this as matching “space” characters.) |
\S | Any character that is not a space, tab, or newline |
- use
^
caret symbol to match only the start of the string:r'^Hello'
will mathc 'Hello you' but not 'Say Hello from Me'. - use
$
to match the end of the string - user
^
and$
to match string exactly:wholeStringIsNum = re.compile(r'^\d+$')
. - use wildcart
.
to match any charactor that is not a new line.
atRegex = re.compile(r'.at')
atRegex.findall('The cat in the hat sat on the flat mat.')
['cat', 'hat', 'sat', 'lat', 'mat']
-
Match any and all things:
.*
dot= any char and star=zero or more of them. -
re.compile('.*', re.DOTALL)
will match new lines as well. -
IGNORE CASE:
re.compile(r'robocop', re.I)
orre.IGNORECASE
-
Substitute text.
namesRegex = re.compile(r'Agent \w+')
namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
'CENSORED gave the secret documents to CENSORED.
- Making complex rexexp readable:
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', re.VERBOSE)
- Use
|
pipe character to give more parameters to the compile() function:someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL | re.VERBOSE)
since compile only accepts one parameter.
Read and write to files and folders.
import os
os.getcwd()
get current working directoryos.makedirs('new-dir-name')
creates a new directory in cwdos.chdir('existing-dir-name')
changes current working directory to the one requestedos.path.abspath('.')
returns the absolute path of a relative path.os.path.getsize('/home/thomas/desktop/somefile.txt')
returns the size of a file- ``
- ``
- ``