At My Fingertips

Documentation

Battery Indicator

Abstraction

Have a look at these graphics:

ABCDE
ABCDimage-5.png

We could call them battery indicators. Here are some similarities:

  • They all have a rounded black outline that has a thickness of 1/10 of the battery width.
  • They all have a white gap inside the rounded black outline that has a thickness of 1/2 the thickness of the outline
  • They all have a positive terminal button at the top, with a width 1/3 of the battery width and a height corresponding to the outline thickness

Now let's focus on their differences, so we can figure out what parameters a battery_indicator function should have.

def battery_indicator(...) -> Graphic:

We can see that the battery levels differ, so we want to have a parameter to specify the level (say, with values between 0.0 for an empty battery and 1.0 for a full battery).

Besides the level, there's also the width and height that can be changed.

def battery_indicator(width: float, height: float, level: float) -> Graphic:

The remaining measures (such as the width of the positive terminal button sitting on the top) can be derived from the parameters.

Implementation

Assume that width and height are the width and height of the body (the black rounded rectangle) of the battery, and that the positive terminal button sits above (so the height of the overall graphic is height + width * 0.1).

Import the rounded_rectangle function from your toolbox (if you don't have that function yet, build it in the PyTamaro iconRounded Rectangle activity).

Besides rounded_rectangle, use rectangle, overlay, and above.

Loading...

Play With Parameters

Play with your battery_indicator function, creating indicators with different sizes and levels.

Loading...

Protect Against Unacceptable Arguments

Unacceptable Levels

You just played with some rather extreme argument values. What happens if you go beyond those?

Loading...

The code produced an error.

Unfortunately, when someone who calls your function sees that error, it is not clear who to blame. They might blame you! They might say that your battery_indicator function is buggy!

You can shift the blame to them! You can show them that it was their mistake! They called your function with the wrong arguments!

To do that, add assertions at the start of your function. Those assertions ensure that the argument values are acceptable. If the assertions fail, you can show the error message and tell the person who called your function that they violated your preconditions.

In our case, the battery_indicator function does not accept a level that is greater than 1.0.

Also, you may want to assert that the width and height have acceptable values (e.g., that they are not negative).

def battery_indicator(width: float, height: float, level: float) -> Graphic:
  assert level >= ...
  assert level >= ...
  ...

Note: You really want to fix that mistake now, because if you have any code cell in this activity that produces an error, you will not be able to move on.

Unacceptable Heights

Let's look at another possible problem.

Loading...

The code should have produced an error (if your implementation set the wall thickness to 10% of the width). Can you figure out why?

Save in Toolbox

Now save your battery_indicator function in your toolbox. It may come in handy as a component for more complex graphics in the future.


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Battery Indicator

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)