filtered, `values`. For example:

Np Ms Office 365/Excel 2016 I Ntermed
1st Edition
ISBN:9781337508841
Author:Carey
Publisher:Carey
Chapter5: Working With Excel Tables, Pivottables, And Pivotcharts
Section: Chapter Questions
Problem 3.3CP
icon
Related questions
Question
The acclaimed _NumPy_ library, that `Simpy` is inspired by, allows you to use the subscription operator not just to select individual items, but also to filter with a mask. It adds a second usage to the operator, just like you have had two uses of many of the operators above, such as adding two `Simpy`s or a `Simpy` and a `float`.

The big idea of the second usage of subscription notation is that if instead of giving an `int` inside the subscription brackets you give a `list[bool]`, the expression will return a new `Simpy` object containing only the _masked_, or filtered, `values`. For example:

~~~python
a = Simpy([1.0, 2.0, 3.0, 4.0, 2.0, 1.0])
mask = a > 2.0
print(mask)  # Output: [False, False, True, True, False, False]
b = a[mask]
print(b) # Output: Simpy([3.0, 4.0])  
~~~

The above example did not need to establish a separate variable named `mask`, it only did so to make it obvious what the `mask` values were. More conventionally, the above example would be written in a shorter-form notation such as:

~~~python
a = Simpy([1.0, 2.0, 3.0, 4.0, 2.0, 1.0])
b = a[a > 2.0]
print(b) # Output: Simpy([3.0, 4.0])
~~~

Take a moment to reflect on how _expressive_ that second line is! For this expression to evaluate, first the `__gt__` method call must evaluate and finally a `__getitem__` method call whose behavior you will need to extend. Notice how natural this expression feels in English: from the `Simpy` object `a`, select the values that greater than `2.0`.

Because of these two distinct use cases, notice not only the parameter given to `__getitem__` can either be an `int` in the first usage and a `list[bool]` in the second usage, but the return type will also be two different types: either a single `float` value or a new `Simpy` object.

Thus, you will need to modify the signature of your `__getitem__` method and implement logic to test for whether `rhs` is an instance of an `int` or not.

~~~python
def __getitem__(self, rhs: Union[int, list[bool]]) -> Union[float, Simpy]:
~~~
Part 10. Overloading the Subscription Notation to Filter with a Mask
The acclaimed Numpy library, that Simpy is inspired by, allows you to use the subscription operator not just to select individual items, but also to filter with a mask. It adds
a second usage to the operator, just like you have had two uses of many of the operators above, such as adding two Simpys or a Simpy and a float.
The big idea of the second usage of subscription notation is that if instead of giving an int inside the subscription brackets you give a list [bool], the expression will
return a new Simpy object containing only the masked, or filtered, values. For example:
a = Simpy ([1.0, 2.0, 3.0, 4.0, 2.0, 1.0])
mask = a > 2.0
print (mask) # Output: [False, False, True, True, False, False]
b = a[mask]
print(b) # Output: Simpy([3.0, 4.0])
The above example did not need to establish a separate variable named mask, it only did so to make it obvious what the mask values were. More conventionally, the above
example would be written in a shorter-form notation such as:
a = Simpy ([1.0, 2.0, 3.0, 4.0, 2.0, 1.0])
b = a[a > 2.0]
print(b) # Output: Simpy([3.0, 4.0])
Take a moment to reflect on how expressive that second line is! For this expression to evaluate, first the __gt___ method call must evaluate and finally a ___getitem___
method call whose behavior you will need to extend. Notice how natural this expression feels in English: from the Simpy object a, select the values that greater than 2.0.
Because of these two distinct use cases, notice not only the parameter given to _getitem___ can either be an int in the first usage and a list [bool] in the second
usage, but the return type will also be two different types: either a single float value or a new Simpy object.
Thus, you will need to modify the signature of your _getitem___ method and implement logic to test for whether rhs is an instance of an int or not.
def _getitem_(self, rhs: Union[int, list[bool]]) -> Union [float, Simpy]:
Transcribed Image Text:Part 10. Overloading the Subscription Notation to Filter with a Mask The acclaimed Numpy library, that Simpy is inspired by, allows you to use the subscription operator not just to select individual items, but also to filter with a mask. It adds a second usage to the operator, just like you have had two uses of many of the operators above, such as adding two Simpys or a Simpy and a float. The big idea of the second usage of subscription notation is that if instead of giving an int inside the subscription brackets you give a list [bool], the expression will return a new Simpy object containing only the masked, or filtered, values. For example: a = Simpy ([1.0, 2.0, 3.0, 4.0, 2.0, 1.0]) mask = a > 2.0 print (mask) # Output: [False, False, True, True, False, False] b = a[mask] print(b) # Output: Simpy([3.0, 4.0]) The above example did not need to establish a separate variable named mask, it only did so to make it obvious what the mask values were. More conventionally, the above example would be written in a shorter-form notation such as: a = Simpy ([1.0, 2.0, 3.0, 4.0, 2.0, 1.0]) b = a[a > 2.0] print(b) # Output: Simpy([3.0, 4.0]) Take a moment to reflect on how expressive that second line is! For this expression to evaluate, first the __gt___ method call must evaluate and finally a ___getitem___ method call whose behavior you will need to extend. Notice how natural this expression feels in English: from the Simpy object a, select the values that greater than 2.0. Because of these two distinct use cases, notice not only the parameter given to _getitem___ can either be an int in the first usage and a list [bool] in the second usage, but the return type will also be two different types: either a single float value or a new Simpy object. Thus, you will need to modify the signature of your _getitem___ method and implement logic to test for whether rhs is an instance of an int or not. def _getitem_(self, rhs: Union[int, list[bool]]) -> Union [float, Simpy]:
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage