Skip to main content

Code example

Project description

example-code

This readme is intended as a guide on how Python code in github.com/equinor/ops-py should be written, documented and distributed.

It also includes an example on how to use such a Python library (PIP package) in other Python code/projects, in GitHub Action/Azure DevOps Pipelines etc.

The files included in the code/example-code-template/src directory may be used as a template in other projects.

├─ code/example-code-template/src/
│  ├─ readme.md
│  ├─ simple_service/
│  │  ├─ __init__.py
│  │  ├─ simple_service.py

The additional files, neeeded by setuptools, will be generated by ops-py-generate-pyproject during CI/CD pipeline run.

If the ops-py-generate-pyproject is not used, please refer to the tools/build.sh script.

What needs to be done before start coding

Create a directory where the project files, code files and directories will reside. The files and subfolder within this directory must be structured as the example-code-template directory tree above.

Run tools/create_venv.sh
This will create a hidden directory .venv within the src directory. This will be the virtual environment for the code to use and where packages will be installed during development.

Notes from tools/create_venv.sh:

Instead of installing all the needed pip packages
on to your system, the packages should be
installed to an isolated environment instead.
Use this script to create a virtual environment
before you start coding.

Only the needed packages, and verified version, should
be installed during deploy. This is ensured by generating a
requirements.txt file when ready to distribute
and deploy. (Please refer to the create_requirements.sh
script.)

Create a directory for your project. Make sure to also
create a directory named 'src' within the project directory.
All the Python code will reside in the 'src' directory.
The virtual enviroment will be created in a '.venv' directory,
within the 'src' 'directory.

Execute the script followed by the path to the project as an
argument, e.g: tools/create_venv.sh code/my-fabulous-project

Specify this '.venv' dir as the interpreter for you IDE project.
To activate it from shell:
cd code/my-fabulous-project/src
source .venv/bin/activate

When ready to start coding

Make sure the venv is activated. Please refer to your IDE or activate it from command line using source .venv/bin/activate

The code must be placed inn subfolder(s) within the src directory - like with the simple_service diretory from the template / example.

How the code should be written and documented

The code should include comments and proper documentation. Use Docstrings Please refer to the documentation in simple_service.py and to Documenting Python Code: A Complete Guide

To write proper and understandable code, for you and for others to read, please refer to How to Write Beautiful Python Code With PEP 8

What needs to be done prior to build and distribution of the code

  • src/my_code_dir/__init__.py
    This file MUST include the version of the project.
    Example from the template_simple_service:
__version__ = '0.0.1'
__author__ = "Svein Tore Eikeskog"
__email__ = "svte@equinor.com"
__description__ = "A simple service to calculate number of atoms in the universe"

NOTE: Next time the project is pushed to pypi the version must be bumped. Edit the __init__py file accordingly.

  • src/requirements.txt
    If pip packages are installed during develompent, a requirements.txt file must be checked in and always follow the code. You may use the tools/create_requirements.sh to create one. If new packages are installed or updated during the development, the requirements.txt must be regenerated. Use the tools/create_requirements.sh frequently/whenever needed.

  • src/readme.md

Usage example:

Create a new directory for your project. Create a new virtual venv and source it:

mkdir my_new_project`
cd my_new_project
python3 -m venv venv
source venv/bin/activate

Install the simple service from the example:

pip install ops-py-example-code

Start the Python interpreter

python

Example code:

from simple_service import simple_service
wan_service = simple_service.SimpleService()
wan_service.fetch_wan_ip()
my_ip = wan_service.get_wan_ip()
print(my_ip)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ops-py-example-code-1.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ops_py_example_code-1.0.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file ops-py-example-code-1.0.1.tar.gz.

File metadata

  • Download URL: ops-py-example-code-1.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for ops-py-example-code-1.0.1.tar.gz
Algorithm Hash digest
SHA256 709952adae60a9f977e605c4826fd84acb9d06193ea8e7f7a163af93017d9425
MD5 66a6cf7596fb3469c32cb704a7bd5382
BLAKE2b-256 b0d61c819e6b5158ec173689e93501201029dea397c865ad1d74613e04e3c1cd

See more details on using hashes here.

File details

Details for the file ops_py_example_code-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ops_py_example_code-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c68e5f6cd827c8f4502bfb272e74ba842e7025faaea0198e4650abed9a0192fe
MD5 45cf3ce62dcb06330f27ac8352e877ce
BLAKE2b-256 52b79c739f3c4110c0e7ca4756171f0a65e26b765d28e29721476341132edea8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page