Skip to main content

Complex and simple nested menus

Project description

Unit Tests codecov CD(PYPI)

Overview

mr-menu helps create Menus and sub-Menus all at once and helps managing them easily. mr-menu can easily execute menu and sub-menus and their conditional functions all-together.

Using mr-menu, you can create a Tree of Menus and add functionalities for each menu item. mr-menu can execute those functionalities and return results gracefully.

Table of Contents

Features

  • Nested Tree of Menu and Sub-Menus: Single Menu can have multiple sub-menus and further sub-sub-menus and so on.

  • In-built Execution of defined functions: You can define functions for each menu item and mr-menu will execute them based on which option the user chooses.

Installation

Execute in Terminal:

pip install mr-menu

Usage

Example Test Case: Suppose you want to create a simple menu which will contain two items - Add two nums and Sub two nums.

  • Simple Menu

    • import the Menu class.

      from mr-menu.simple import Menu
      
    • create functions/class methods for Add and Sub

      def add_two():
          num1 = int(input("Enter num 1: "))
          num2 = int(input("Enter num 2: "))
          return num1 + num2
      
      def sub_two():
          num1 = int(input("Enter num 1: "))
          num2 = int(input("Enter num 2: "))
          return num1 - num2
      
    • Create Menu class object

      menu = Menu(
          identifier="main", # unique menu identifier.
          menu={1: "Add two nums", 2: "Sub two nums"}, # menu in dict form
          functions={1: add_two, 2: sub_two}, # function dict with callable functions mapped to menu
      )
      
    • Handle the menu and it's functions.

      result = menu.handler(
          prompt="Enter choice: ", # to be shown to the user.
          return_execution_result=True,
          *args, # see docstring
          **kwargs, # see docstring
      )
      
      # the above code will ask the user for input with the 
      # prompt and if it is "1", it will ask for two nums and 
      # return (True, output).
      # Similarly, if "2" is selected, it will return (True, output) again.
      # if execution fails, it will return (False, None)
      

Now, Let us take an example where the first menu has two options -> Add, Sub and one extra option -> More options which expands into another menu, say, Multiply and divide.

  • Tree of Menus and submenus

    • import MenuBuilder classs

      from mr-menu.generator import MenuBuilder
      
    • create functions for Add, Sub, Multiply and divide

      def add():
          num1 = int(input("enter num 1: "))
          num2 = int(input("enter num 2: "))
          return num1 + num2
      
      def sub():
          num1 = int(input("enter num 1: "))
          num2 = int(input("enter num 2: "))
          return num1 - num2
      
      def mult():
          num1 = int(input("enter num 1: "))
          num2 = int(input("enter num 2: "))
          return num1*num2
      
      def div():
          num1 = int(input("enter num 1: "))
          num2 = int(input("enter num 2: "))
          return num1/num2 if num2 != 0 else 0
      
    • create MenuBuilder class object

      builder = MenuBuilder()
      
    • Add the first menu (main menu)

      builder.add(
          identifier="main", # Unique identifier for this particular menu
          menu={1: "Add two nums", 2: "Sub two nums", 3: "More Options"}, # menu in dict form
          functions={1: add, 2: sub, 3: None}, # functions for all the options except the one that expands a new menu (3rd)
          go_back_index=None, # It is recommended to keep this
          # None, as a new (4th) option will be automatically
          # created and handled that facilitates going back to the main menu.
      )
      
    • Add the sub-menu

      builder.add_submenu(
          parent_identifier="main", # parent menu is "main",
          parent_menu_index=3, # the index key where the menu is supposed to expand. i.e., 3 (More options)
          submenu_identifier="main-submenu-1", # the unique identifier for this sub-menu,
          submenu={1: "Multiply two nums", 2: "Divide two nums"}, # submenu in dict form.
          go_back_index=None, # again, keep this None, here a 3rd
          # option will be automatically created that helps to
          # go back to the main menu.
          replace_if_exist=True, # this means if the submenu already exists, replace the old one with this current one.
      )
      
    • Handle the menu and submenu

      If the user chooses option 1 (Add two nums), the handler will ask for two inputs and return the sum of the numbers. But when the user chooses 3rd option in the main menu, The new sub-menu will be displayed. The user can then choose Multiply and Divide.

      result = builder.handler(
          prompt="Enter your choice:", # the prompt that asks to choose an option.
          post_execution_label="The Task executed Successfully", # after a task finishes, this will be printed.
          return_execution_result=True, # returns the result in tuple form with two values - tuple[bool, Any],,
          # where bool represents execute status and Any is the result.
      )
      

Issues

Please submit any issues found here.

Pull Requests

Pull Requests are welcome and encouraged. Find it here

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mr_menu-0.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

mr_menu-0.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file mr_menu-0.1.tar.gz.

File metadata

  • Download URL: mr_menu-0.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for mr_menu-0.1.tar.gz
Algorithm Hash digest
SHA256 fde8b4256921472c6497f658411b587d3d6bc1bac2a4108874f7e10959fdd163
MD5 71334d4a6975eca815eefc2ab9cc6ac8
BLAKE2b-256 969a26e22d379dd4a5de4760b7f20b044f93a841861b166ffd32c5e30368f33b

See more details on using hashes here.

File details

Details for the file mr_menu-0.1-py3-none-any.whl.

File metadata

  • Download URL: mr_menu-0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for mr_menu-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f90ed33e93380aee669a2d1a7f1fb583c0b76d61ab93fca0074f721e76b49bb9
MD5 2dca1f78e1742573bd38b3872f109b62
BLAKE2b-256 60dd19e4d9f7f5401f60b150237ed89c716712f687397ff39c840c80fdb5ef1f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page