liveBook · Manning

Get method

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

The get method is a mapping operation that returns the value for a key or a specified default when the key is absent.

Use the mapping get method to read a value by key without failing when that key is absent. It returns the stored value when present; otherwise it returns None, or a default you provide. This makes optional configuration and partially mapped input safe to handle. section 3.5 section 7.5.3

Keep motor calibration optional

Motor adjustments can come from environment variables while still allowing the robot to run with neutral power. Convert the result after get returns either the configured string or the fallback value. DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1')) DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1')) If no adjustment is set, each motor uses 1; if an adjustment is set, such as 0.8, that value is used instead. section 3.5

>>> os.environ.get('ROBO_DC_ADJUST_L', '1')
'1'
>>> os.environ.get('ROBO_DC_ADJUST_R', '1')
'0.8'

Compare an absent left-motor variable with a configured right-motor variable: the same get call supplies a usable value in both cases. section 3.5

Ignore unmapped joystick controls

Use get when only selected buttons and axes have names. An event number outside the mapping produces None rather than an error, so the event can still be printed or handled safely. name = BUTTON.get(event.number) The same pattern retrieves axis names from AXIS. section 7.5.3

Do not confuse mapping get with the HTTP GET method. HTTP GET is used for web-service calls that do not change robot state, while movement requests use POST. section 5.5

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Demonstrates defaulted and default-None lookups in runnable configuration and event-handling code without detailing mapping internals.
Examples
Uses motor-adjustment variables and button and axis dictionaries for joystick event output.
What it assumes
Assumes basic Python code execution while showing get directly in environment-variable and dictionary expressions.
What you can do after
Apply it via Python mappings to supply configuration defaults and safely label selected input events.
How it connects
Connects environment variables, dictionaries, motor throttle adjustment, joystick events, and HTTP methods.

Also explained in

Compare how 2 books explain get method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.