Python¶
flake8 skip lines¶
If specific lines should not be linted i.e. long URLs over 80 chars
just append following comment to your line that should be ignored
# noqa
argparse¶
import sys
import argparse
parser = argparse.ArgumentParser(description='Put some different arguments')
parser.add_argument('--storstring', '-s', action='store', dest='string', required=True,
help='Put some string in here. This value is also required/mandatory')
parser.add_argument('--true', '-t', action='store_true', dest='bool', default=False,
help='If set variable is set to true')
parser.add_argument('--append', '-a', action='append', dest='append', default=[]
help='Put strins into array. this can be used multiple times')
args = parser.parse_args()
# Values can be accesed via args.string, args.bool, args.append
# Print help when no args given
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
system call¶
import os
com = os.popen("<SHELL COMMAND>").read()
for c in com:
print(c)
ternary operator¶
variable = variable if variable else self.variable
merge dict¶
by updating the dictionary:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
x.update(y)
print(x)
Using ** operator:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
print(z)
mixed:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
print({**x, 'foo': 1, 'bar': 2, **y})
## Ensure OpenStack env is set
```python
if not os.environ.get('OS_AUTH_URL') or not os.environ.get('OS_DOMAIN_NAME') or not os.environ.get('OS_PROJECT_NAME') or not os.environ.get('OS_PASSWORD') or not os.environ.get('OS_USERNAME'):
print("missing OpenStack Environment variables! Plase source openrc")
exit(1)
Build python package¶
Directory structure¶
/python-my-package
/my-package
__init__.py
__main__.py
submodule1.py
submodule2.py
submodule3.py
.gitignore
.gitlab-ci.yml
README.md
setup.py
.gitignore¶
build/
dist/
*.egg-info
__pycache__
setup.py¶
import setuptools, os
with open("README.md", "r") as fh:
long_description = fh.read()
def get_version(version='9999.999.99-devel'):
if 'CI_COMMIT_SHORT_SHA' in os.environ:
version = os.environ['CI_COMMIT_SHORT_SHA']
if 'CI_COMMIT_TAG' in os.environ:
version = os.environ['CI_COMMIT_TAG']
if 'CUSTOM_VERSION' in os.environ:
version = os.environ['CUSTOM_VERSION']
return version
setuptools.setup(
name='python-my-package',
version=get_version(),
author="Me Myself And I",
author_email="me@myself.and.i",
description="My first python package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gitlab.der-jd.de/python/python-my-package",
license='MIT',
packages=setuptools.find_packages(),
install_requires=['requests', 'ptable', 'argparse', 'configparser'],
entry_points={"console_scripts": ["my-package = my-package.cli:main"]},
classifiers=[
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"License :: OSI Approved :: MIT License",
],
)
build package¶
python setup.py sdist
.gitlab-ci.yml¶
---
default:
image: python:latest
stages:
- build
- test
- release
artifacts:
when: on_success
paths:
- dist/*.tar.gz
# build whl, deb and rpm
build:pip:
stage: build
script:
- pip install setuptools wheel
- python setup.py sdist
# build:deb:
# stage: build
# scripts:
# - debian build
# build:rpm:
# stage: build
# scripts:
# - rpm build
test:
stage: test
scripts:
- pip install ./
# - dpkg install *.deb
# - yum install *.rpm
release:
stage: release
only:
- tags
Last update:
January 15, 2021