Skip to content

Commit

Permalink
RSDK-3941 - Example module updates (#422)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Rewis <[email protected]>
  • Loading branch information
cheukt and benjirewis authored Sep 11, 2023
1 parent 4ec5669 commit 450068a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
36 changes: 32 additions & 4 deletions docs/examples/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,41 @@
"# Create a virtual environment to run our code\n",
"VENV_NAME=\"venv\"\n",
"PYTHON=\"$VENV_NAME/bin/python\"\n",
"\n",
"python3 -m venv $VENV_NAME\n",
"$PYTHON -m pip install -r requirements.txt -U # remove -U if viam-sdk should not be upgraded whenever possible\n",
"\n",
"ENV_ERROR=\"This module requires Python >=3.8, pip, and virtualenv to be installed.\"\n",
"\n",
"if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then\n",
" echo \"Failed to create virtualenv.\"\n",
" if command -v apt-get >/dev/null; then\n",
" echo \"Detected Debian/Ubuntu, attempting to install python3-venv automatically.\"\n",
" SUDO=\"sudo\"\n",
" if ! command -v $SUDO >/dev/null; then\n",
" SUDO=\"\"\n",
" fi\n",
"\t\tif ! apt info python3-venv >/dev/null 2>&1; then\n",
"\t\t\techo \"Package info not found, trying apt update\"\n",
"\t\t\t$SUDO apt -qq update >/dev/null\n",
"\t\tfi\n",
" $SUDO apt install -qqy python3-venv >/dev/null 2>&1\n",
" if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then\n",
" echo $ENV_ERROR >&2\n",
" exit 1\n",
" fi\n",
" else\n",
" echo $ENV_ERROR >&2\n",
" exit 1\n",
" fi\n",
"fi\n",
"\n",
"# remove -U if viam-sdk should not be upgraded whenever possible\n",
"# -qq suppresses extraneous output from pip\n",
"echo \"Virtualenv found/created. Installing/upgrading Python packages...\"\n",
"if ! $PYTHON -m pip install -r requirements.txt -Uqq; then\n",
" exit 1\n",
"fi\n",
"\n",
"# Be sure to use `exec` so that termination signals reach the python process,\n",
"# or handle forwarding termination signals manually\n",
"echo \"Starting module...\"\n",
"exec $PYTHON src/wifi-sensor.py $@\n",
"```\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/module_step2_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
raise Exception("Multiplier cannot be 0.")
else:
cls.multiplier = 1.0
return [""]
return []

async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> Mapping[str, Any]:
with open("/proc/net/wireless") as wifi_stats:
Expand Down
33 changes: 31 additions & 2 deletions examples/complex_module/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,39 @@ cd `dirname $0`
# Create a virtual environment to run our code
VENV_NAME="venv"
PYTHON="$VENV_NAME/bin/python"
ENV_ERROR="This module requires Python >=3.8, pip, and virtualenv to be installed."

python3 -m venv $VENV_NAME
$PYTHON -m pip install -r requirements.txt -U # remove -U if viam-sdk should not be upgraded whenever possible
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
echo "Failed to create virtualenv."
if command -v apt-get >/dev/null; then
echo "Detected Debian/Ubuntu, attempting to install python3-venv automatically."
SUDO="sudo"
if ! command -v $SUDO >/dev/null; then
SUDO=""
fi
if ! apt info python3-venv >/dev/null 2>&1; then
echo "Package info not found, trying apt update"
$SUDO apt -qq update >/dev/null
fi
$SUDO apt install -qqy python3-venv >/dev/null 2>&1
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
echo $ENV_ERROR >&2
exit 1
fi
else
echo $ENV_ERROR >&2
exit 1
fi
fi

# remove -U if viam-sdk should not be upgraded whenever possible
# -qq suppresses extraneous output from pip
echo "Virtualenv found/created. Installing/upgrading Python packages..."
if ! $PYTHON -m pip install -r requirements.txt -Uqq; then
exit 1
fi

# Be sure to use `exec` so that termination signals reach the python process,
# or handle forwarding termination signals manually
echo "Starting module..."
exec $PYTHON -m src.main $@
8 changes: 4 additions & 4 deletions examples/complex_module/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from viam.components.arm import Arm
from viam.components.base import Base

from .arm.my_arm import MyArm
from .base.my_base import MyBase
from .gizmo.my_gizmo import Gizmo, MyGizmo
from .summation.my_summation import SummationService, MySummationService
from src.arm.my_arm import MyArm
from src.base.my_base import MyBase
from src.gizmo import Gizmo, MyGizmo
from src.summation import MySummationService, SummationService


async def main():
Expand Down
33 changes: 31 additions & 2 deletions examples/simple_module/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,39 @@ cd `dirname $0`
# Create a virtual environment to run our code
VENV_NAME="venv"
PYTHON="$VENV_NAME/bin/python"
ENV_ERROR="This module requires Python >=3.8, pip, and virtualenv to be installed."

python3 -m venv $VENV_NAME
$PYTHON -m pip install -r requirements.txt -U # remove -U if viam-sdk should not be upgraded whenever possible
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
echo "Failed to create virtualenv."
if command -v apt-get >/dev/null; then
echo "Detected Debian/Ubuntu, attempting to install python3-venv automatically."
SUDO="sudo"
if ! command -v $SUDO >/dev/null; then
SUDO=""
fi
if ! apt info python3-venv >/dev/null 2>&1; then
echo "Package info not found, trying apt update"
$SUDO apt -qq update >/dev/null
fi
$SUDO apt install -qqy python3-venv >/dev/null 2>&1
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
echo $ENV_ERROR >&2
exit 1
fi
else
echo $ENV_ERROR >&2
exit 1
fi
fi

# remove -U if viam-sdk should not be upgraded whenever possible
# -qq suppresses extraneous output from pip
echo "Virtualenv found/created. Installing/upgrading Python packages..."
if ! $PYTHON -m pip install -r requirements.txt -Uqq; then
exit 1
fi

# Be sure to use `exec` so that termination signals reach the python process,
# or handle forwarding termination signals manually
echo "Starting module..."
exec $PYTHON src/main.py $@
2 changes: 1 addition & 1 deletion examples/simple_module/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
raise Exception("Multiplier cannot be 0.")
else:
cls.multiplier = 1.0
return [""]
return []

async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> Mapping[str, Any]:
return {"signal": 1 * self.multiplier}
Expand Down

0 comments on commit 450068a

Please sign in to comment.