Advertisement
DeaD_EyE

something with numpy not very interesting

Nov 9th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. from collections import namedtuple
  2.  
  3.  
  4. import numpy as np
  5. # read more about numpy
  6.  
  7.  
  8. # sentinel
  9. # read more about the use of it
  10. # https://python-patterns.guide/python/sentinel-object/
  11. EXIT = object()
  12. TERM = object()
  13.  
  14.  
  15. def main():
  16.     # main loop
  17.     while True:
  18.         result = menu()
  19.         if result is EXIT:
  20.             # here he exits
  21.             print('Exit!')
  22.             break
  23.         elif result is TERM:
  24.             # same here
  25.             # just to show that you can handle different
  26.             # sentinel objects
  27.             print('TERM!')
  28.             break
  29.         # no exit
  30.         print_result(result)
  31.         # still in loop
  32.  
  33.  
  34. def print_result(result):
  35.     print()
  36.     print('=======')
  37.     print('Result:')
  38.     print(result)
  39.     print()
  40.  
  41.  
  42. def get_option(last_index):
  43.     while True:
  44.         try:
  45.             option = int(input("Please choose and option: "))
  46.         except ValueError:
  47.             print('Invalid value.')
  48.             continue
  49.         if not (1 <= option <= last_index):
  50.             print('Value is not in menu')
  51.             continue
  52.         return option
  53.  
  54.  
  55. def menu():
  56.     """
  57.    This is not the best implementation.
  58.    """
  59.     for index, entry in enumerate(option_mapping, start=1):
  60.         print(f'{index:<2d}. {entry.text}')
  61.     # using index, which is the last value
  62.     # yes, for-loops leaks
  63.     last_index = index
  64.     # hint, Python indicies starts with 0
  65.     option = get_option(last_index) - 1
  66.     # print('Option:', option)
  67.     entry = option_mapping[option]
  68.     if entry.function is EXIT:
  69.         return EXIT
  70.     elif entry.function is TERM:
  71.         return TERM
  72.     return entry.function()
  73.  
  74.  
  75. def array_2by2():
  76.     """
  77.    I was too lazy to find a better function
  78.    to create a 2by2 matrix.
  79.  
  80.    2x2 matricies are used for 2d graphics
  81.    3x3 for 3d
  82.    4x4 for translation matrix in 3d space
  83.    
  84.    https://en.wikipedia.org/wiki/Rotation_matrix
  85.    """
  86.     # to create an array with zeros with a 2 by 2 shape
  87.     # np.zeros((2, 2))
  88.     # np.ones((2,2))
  89.     return np.arange(1, 5).reshape((2,2))
  90.  
  91.  
  92. def square_value():
  93.     # numpy arrays support broadcast multiplication
  94.     # you can do also matrix multiplacation
  95.     # dot product, cross product, etc.
  96.     return array_2by2() ** 2
  97.  
  98.  
  99. def add_3():
  100.     """
  101.    Functions should have alsways a doctstring
  102.  
  103.    Here array_2by2 is called and 3 is added to the result.
  104.    The result returns
  105.    """
  106.     return array_2by2() + 3
  107.  
  108.  
  109. def multiply_by5():
  110.     return array_2by2() * 5
  111.  
  112.  
  113. # mapping must be defined after the functions has been created
  114. # if you change the order, you get a NameError
  115.  
  116.  
  117. menu_entry = namedtuple("Entry", "function text")
  118. option_mapping = (
  119.     menu_entry(array_2by2, "Create a 2-by-2 Array"),
  120.     menu_entry(square_value, "Square Value"),
  121.     menu_entry(add_3, "Add 3 to Elements"),
  122.     menu_entry(multiply_by5, "Multiply Elements"),
  123.     menu_entry(EXIT, "Exit"),
  124.     menu_entry(TERM, "Terminate"),
  125. )
  126.  
  127.  
  128. if __name__ == '__main__':
  129.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement