Python
Contents
Performance analysis
To run the python profiler and store the profiling information in prof.dat
python -m cProfile -o prof.dat <prog> <args>
To start the interactive “profile statistics tool” run
python -m pstats prof.dat
You probably first want to sort the results
sort time # sort by tottime, the total time spent in the function alone
sort cumtime # sort by cumtime, the total time spent in the function plus all functions that this function called
and afterwards print those
stats # print the whole statistic
stats <N> # print the first N entries
Mail via Python
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import email.utils
import smtplib
import os.path
def sendMail(server, port, user, password, fromMailadress, fromName, toMailadress, toName, subject, message, attachments=[], replyToMailadress=""):
# create message object instance
msg = MIMEMultipart()
# setup the parameters of the message
msg['To'] = email.utils.formataddr((toName, toMailadress))
msg['From'] = email.utils.formataddr((fromName, fromMailadress))
msg['Date'] = email.utils.formatdate(localtime=True)
msg['Subject'] = subject
msg.add_header('reply-to', replyToMailadress)
# add in the message body
msg.attach(MIMEText(message, 'plain'))
# attach files
for filename in attachments:
if not os.path.isfile(filename):
continue
with open(filename, "rb") as f:
part = MIMEApplication(f.read(),Name=os.path.basename(filename))
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(filename)
msg.attach(part)
# create server connection
server = smtplib.SMTP(server + ": " + port)
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(user, password)
# send the message via the server.
try:
server.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
server.quit()
Python-venv
Installation:
apt install python3-venv
Setup:
python3 -m venv myvenv
cd myvenv
source bin/activate
pip install -U pip
Install dependency (e.g. django)
pip install django
Install dependency with specific version
pip install django==1.10
Store dependencies
pip freeze > requirements.txt
Retrieve dependencies
pip install -r requirements.txt