Python
Contents
Setup
How to Upgrade Python 3.5 to 3.7 on Debian 9
https://exitcode0.net/debian-9-how-to-upgrade-python-3-5-to-python-3-7/ sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget cd ~/Download wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz tar xf Python-3.7.3.tar.xz cd ./Python-3.7.3 ./configure --enable-optimizations nproc make -j 8 sudo make install sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 10
Then to fix pip:
sudo ln -s /usr/share/pyshared/lsb_release.py /usr/local/lib/python3.7/site-packages/lsb_release.py sudo pip3 install --upgrade pip
How to Install Python 3.7 on Debian 9
https://linuxize.com/post/how-to-install-python-3-7-on-debian-9/
sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget cd ~/Download curl -O https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz tar -xf Python-3.7.3.tar.xz cd Python-3.7.3 ./configure --enable-optimizations nproc (see how many processors you have) make -j 6 sudo make altinstall
Setup Python3 in Windows
Download and install Python for windows. If pip does not get installed with that, do the following:
Step 1: Download PIP get-pip.py Before installing PIP, download the get-pip.py file: get-pip.py on pypa.io. https://bootstrap.pypa.io/get-pip.py Open cmd python get-pip.py
I had to add my pip and python folders to my path. I had to copy/paste rename python.exe to python3.exe
Change python version system-wide
To change python version system-wide we can use update-alternatives command. Logged in as a root user, first list all available python alternatives:
update-alternatives --list python update-alternatives: error: no alternatives for python
The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
sudo update-alternatives --config python
Some error that I got
: No such file or directory
: No such file or directory
From the comments above it looks like you have dos line endings, and so the hashbang line is not properly processed.
Line ending style are not shown with :set list in Vim because that option is only used when reading/writing the file. In memory line endings are always that, line-endings. The line ending style used for a file is kept in a Vim per-file option, weirdly called fileformat.
To see/change the line ending style from Vim, you can use the following commands:
:set fileformat :set ff
It will show dos or unix. You want unix, of course ;-).
To change it quickly you can save the file with:
:w ++ff=unix
or maybe more permenantly
tr -d '\r' < input.file > output.file
Edit config files
vim: my_config_updater.py |
from configobj import ConfigObj
config = ConfigObj(r".bashrc")
global updated
updated = False
def update_me(key, value):
if config[key] <> value:
config[key] = value
updated = True
return updated
updated = update_me("alias ls", "ls --color=AUTO")
#updated = update_me("alias new", "new --color=AUTO")
updated = update_me("export EDITOR", "vim")
if updated == True:
config.write()
updated = False
print("Config file updated...")
#print(config["alias ls"])
|
virtualenv
Install Python virtualenv.
Install
sudo apt install python3 python3-venv sudo apt install virtualenv python3-virtualenv
run virtualenv
python3 -m venv /path/to/virtual/environment python3 -m venv ./python_venv
source my_project/bin/activate source ./python_venv/bin/activate
Maybe at this point its a good idea to make sure that your python tools are up to date...
pip install --upgrade pip (do NOT run this on Python 2.6.8!) pip install --upgrade pip==9.0.3 ( this should work on Python-2.6.8)
. . . . Do some stuff . . . .
deactivate
Code snip bits
File
is file/directory
vim: my_app.py |
import os.path
if os.path.exists(fname):
# file exists as a object
if os.path.isfile(fname):
# object is a file
if os.path.isdir(fname):
# directory exists
|
Find all files and loop
vim: my_config_updater.py |
import os
for root, dirnames, files in os.walk('/path/to/my/files/'):
for filename in files:
if filename.endswith(('.txt', '.sh', '.gif', '.png')):
print("root: {}; filename: {}".format(root, filename) )
|
run external program
vim: my_app.py |
def exec_cmd(command):
child = subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = child.communicate()
rc = child.returncode
return {"rc": rc, "stdout": stdout, "stderr": stderr
|
process
kill gracefully class
vim: some_app_somewhere.py |
import signal
import time
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
if __name__ == '__main__':
killer = GracefulKiller()
while True:
time.sleep(1)
print("doing something in a loop ...")
if killer.kill_now:
break
print("End of the program. I was killed gracefully")
|
PyParsing
word = Word(alphas) num = Word(nums) alpnum = Word(alphanums) white = White(' ') sub_word = Regex(r"[A-Za-z0-9_]*") mac = Regex(r"([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})") + Suppress(restOfLine + lineEnd) raw_ip = Combine(Word(nums, max=3) + "." + Word(nums, max=3) + "." + Word(nums, max=3) + "." + Word(nums, max=3)) # To find a word in a sentince day = Keyword("day") word = ~day + Word(alphas) [So I see this as word is all words excusing day] sentence = OneOrMore(word) sentence_end_with_happy = sentence('first') + day + sentence('last') ret = sentence_end_with_happy.parseString("hi this is a nice day and everything is ok") print ret['first'] print ret['last'] print ret
groups
Use groups to get pyparsing to create a dictionary with labels.
my_line = Group((someword + Optional(Suppress(sentence) + second('second')) + Optional(otherword)('otherword') + Suppress(restOfLine + lineEnd) + restOfLine + lineEnd))) section = bla + blabla + Optional(mac)('mac') + Optional(ZeroOrMore(my_line)('mline')) ## Loop through the results for match in section.scanString(myvar): result = match[0] print(result.keys) print(result.dump) print(result.values)
remove a word from word
second = Keyword('secondary') word2 = ~second + Word(alphas) sentence = OneOrMore(word2) # So here we will have a "sentence" without the word 'secondary'
skipto
Literal('<') + SkipTo('>', include=True)
tabulate
vim: some_app_somewhere.py |
from tabulate import tabulate
result = exec_cmd('tcpdump -D')
if result['rc'] <> 0:
log_error("Error from tcpdump. Please try again.")
return
else:
input_ints = result['stdout']
table = []
headers = (["Interface", "Status"])
for lines in input_ints.strip().split('\n'):
interfaces = lines.split('.', 1)[-1].split(' ', 1)[0]
status = lines.split('.', 1)[-1].split(' ', 1)[1]
# Build Table
table.append(([interfaces, status]))
print(tabulate(table, headers))
|
Django
Use python manage.py runserver <ip>:<port>
For example,my IP is 192.168.0.100 and I want to run django app on port 80,I have to do
[sudo] python manage.py runserver 192.168.0.100:80
Heruko
on local pc
sudo apt install snapd (or snap is Debian Jessie)