Python
code snippets and stuff for Python
Programm Setup
Install pyenv and set it to the version you want to use Install virtualenv, using pyenv is not necessary but I like it to keep my python versions managed. Install python3.10-venv
if an error message tells to.
curl https://pyenv.run | bash
pyenv install 3.11
pyenv local 3.11
apt install python3.10-venv #might not be needed with pyenv
pyenv local 3.9.0
python -m venv
Shebang and program layout
#!/usr/bin/env python3
def main(kwargs**, args**):
pass
if __name__ == '__main__':
main()
Beautiful Soup
Use beautiful soup for scraping and manipulating html content
pip install bs4
from bs4 import BeautifulSoup
Commandline execution
The -c
flag tells python to run the appended string as a command. For example
➜ python3 -c "import datetime; print('It is: ',datetime.datetime.now())"
It is: 2022-03-29 13:37:50.057810
Help Function
adding an explantion string to a function can be used by the help()
function to give explanation into what it is doing.
def adder(x,y):
"""
adds to numbers x and y and returns the sum
"""
return x+y
help(adder)
for a class this looks like:
class person():
"""
A new person is born
"""
def __init__ (self, name):
"""
Give the person a name
"""
self.name = name
and the output from interactive session is:
PS C:\Users\920280\OneDrive - Cognizant\Documents> python.exe -i person.py
>>> help(person)
Help on class person in module __main__:
class person(builtins.object)
| person(name)
|
| A new person is born
|
| Methods defined here:
|
| __init__(self, name)
| Give the person a name
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
>>> help(person.__init__)
Help on function __init__ in module __main__:
__init__(self, name)
Give the person a name
>>>
CLI Arguments
Use the argparse
Library to read cli arguments.
import argparse
def read_cli_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="Execute on this File.", type=str)
args = parser.parse_args()
argv = vars(args)
argv
is a dictionary like {'file' : 'CliArgument'}.
args` is the Namespace for all Cli Arguments.
String Formatting
Filling {}
with the variable `Name'
Name = "Jacen Solo"
this_string = "Hello World I am {}".format(Name)
More complex solution by definen the positional arguments of the format function
age = 32
Name = "Nom Anor"
this_string = "Hello World. I am {1} and {0} years alive.".format(age, Name)
This puts Name
in {1}
because it is the second positional argument (0-based indexing) and age
in {0}
.
IF-Else Oneliner
this_dict['key'] = other_dict['key'] if other_dict['key'] else this_dict['key']
Read this_dict['key']
is other_dict['key']
if that key exists (evalutes to true
) otherwise keep the origial value.
Logging
the logger object takes several arguments, the first on is the message that needs to be one string. Not like the print function that automatically concatenates its arguments to a string
import logging
LOGGING_LEVEL = os.environ["LOGGING_LEVEL"]
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(LOGGING_LEVEL)
logger.info('This will get logged'+' this')
Decorators
Decorators can only be applied to functions and classes see
def outer(func):
def wrapperFunc():
return("Hello " + str(func()))
return(wrapperFunc)
@outer
def func():
print("Logic here")
return("Return here")
a = func()
print(a)
Last updated