This assignment is based on the concepts of "Sequence Types - 2 ". Here we went through iterators which are objects that implement the __iter__ and __next__ methods while a class only implementing __iter__ is an iterable. We can use iterators to parse through un-ordered types like sets and Dictionary aprt from ordered types like List and Tuple are sequence type. We have refactored the classes we developed in session13 with following objectives-
- Refactor the Polygon class so that all the calculated properties are lazy properties, i.e. they should still be calculated properties, but they should not have to get recalculated more than once (since we made our Polygon class "immutable").
- Refactor the Polygons (sequence) type, into an iterable. Make sure also that the elements in the iterator are computed lazily - i.e. you can no longer use a list as an underlying storage mechanism for your polygons. Implement both an iterable, and an iterator.
Solution:-
Class is implemented in Module regular_poly.py
Class is implemented in Module regular_poly.py
Ipynb file to test the above mentioned Modules
- We have implemented a Regular Polygon class which takes number or vertices and circumradius and gives a polygon class object with following properties-
- Create a Polygon Class:
-
- where initializer takes in:
-
- number of edges/vertices
-
- circumradius
-
- it provide following properties:
-
- number of edges
-
- number of vertices
-
- interior angle
-
- edge length
-
- apothem
-
- area
-
- perimeter
-
- that has these functionalities:
-
- a proper __repr__ function
-
- implements equality (==) based on # vertices and circumradius (__eq__)
-
- implements > based on number of vertices only (__gt__)
-
We have implemented the concepts of lazy loading where in the first case, the class properties are evaluated only once till the radius or number of vertices is changed. These are evaluated only when these properties are referenced.
- Implement a Custom Polygon iterable class which has got a class for iterator:
-
- where initializer takes in:
-
- number of vertices for largest polygon in the sequence
-
- common circumradius for all polygons
-
- that has these functionalities:
-
- method for iterable (__iter__)
-
- class for iterable having the __iter__ and __next__ methods