Python recursively queries the menu and converts it to a json instance, pythonjson

Source: Internet
Author: User

Python recursively queries the menu and converts it to a json instance, pythonjson

Recently, I need to write a menu in python, and it took two or three days to get it done. Now I want to record it here. You can use it for reference.

Note: The article references non-executable complete code and only extracts the key part of the code.

Environment

  • Database: mysql
  • Python: 3.6.

Table Structure

Create table 'tb _ menu '('id' varchar (32) not null comment 'unique identifi', 'menu _ name' varchar (40) default null comment 'menu name ', 'menu _ url' varchar (100) default null comment 'menu link', 'type' varchar (1) default null comment 'type', 'parent' varchar (32) default null comment 'parent directory id', 'del _ flag' varchar (1) not null default '0' comment' Delete flag 0: Do NOT delete 1: delete ', 'create _ time' datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time', 'Update _ time' timestamp not null default CURRENT_TIMESTAMP ON update CURRENT_TIMESTAMP COMMENT 'Update time', primary key ('id ') using btree) ENGINE = InnoDB default charset = utf8 COMMENT = 'menu table ';

Python code

In the Menu object, a submenu list references "subMenus" and its type is list.

Core code

Def set_subMenus (id, menus): "recursively sets the sub-Menu list of the parent menu at each level based on the passed parent menu id: param id: parent level id: param menus: sub-Menu list: return: If this menu does not have a sub-menu, return None; if there is a sub-menu, return to the sub-Menu list "" # record sub-Menu list subMenus = [] # traverse sub-menu for m in menus: if m. parent = id: subMenus. append (m) # recycle the sub-menu for sub in subMenus: menus2 = queryByParent (sub. id) # There is a sub menu if len (menus): sub. subMenus = set_subMenus (sub. id, menus2) # The Sub-Menu list is not empty. if len (subMenus): return subMenus else: # No sub-menu, return None

Test Method

Def test_set_subMenus (self): # level-1 menu rootMenus = queryByParent ('') for menu in rootMenus: subMenus = queryByParent (menu. id) menu. subMenus = set_subMenus (menu. id, subMenus)

Note: the basic process is: first query the level-1 menu, and then pass the level-1 menu id and the level-1 Menu Sub-Menu list to the set_subMenus method, recursively sets sub-menus in the sub-Menu list;

You can pass the menu Id to query all sub-Menus under the menu. If null characters are passed, the query starts from the root directory.

In the "rootMenus" object, you can see the complete menu tree structure.

Convert Json

The ORM framework I used is sqlalchemy, which directly queries the Menu object from the database. An error will be reported when converting to Json. You need to redefine a DTO class to convert the Menu object to a Dto object.

MenuDto

class MenuDto():  def __init__(self, id, menu_name, menu_url, type, parent, subMenus):    super().__init__()    self.id = id    self.menu_name = menu_name    self.menu_url = menu_url    self.type = type    self.parent = parent    self.subMenus = subMenus  def __str__(self):    return '%s(id=%s,menu_name=%s,menu_url=%s,type=%s,parent=%s)' % (      self.__class__.__name__, self.id, self.menu_name, self.menu_url, self.type, self.parent)  __repr = __str__

Therefore, the sub-menu setting method is redefined.

Def set_subMenuDtos (id, menuDtos): "recursively sets the sub-Menu list of the parent menu at each level based on the passed parent menu id: param id: parent id: param menuDtos: sub-Menu list: return: If this menu does not have a sub-menu, return None; if there is a sub-menu, return to the sub-Menu list "" # record sub-Menu list subMenuDtos = [] # traverse sub-menu for m in menuDtos: m. name = to_pinyin (m. menu_name) if m. parent = id: subMenuDtos. append (m) # recycle the sub-menu for sub in subMenuDtos: menus2 = queryByParent (sub. id) menusDto2 = model_list_2_dto_list (menus2, "MenuDto (id ='', menu_name = '', menu_url ='', type = '', parent = '', subMenus = '')") # sub menu if len (menuDtos): if len (menusDto2): sub. subMenus = set_subMenuDtos (sub. id, menusDto2) else: # No sub menu, delete this node sub. _ delattr _ ('submenus') # if len (subMenuDtos): return subMenuDtos else: # return None

Note:

  1. If a menu does not have a sub-menu, the "subMenus" attribute is deleted. Otherwise, a null value will appear during conversion to Json.
  2. The model_list_2_dto_list method can convert the Menu list into a MenuDto list.
  3. To_pinyin is a method for converting Chinese characters into pinyin.

View method to return Json

Def get (self): param = request. args id = param ['id'] # If the id is empty, the system queries rootMenus = queryByParent (id) rootMenuDtos = model_list_2_dto_list (rootMenus, "MenuDto (id ='', menu_name = '', menu_url ='', type = '', parent ='', subMenus = '')") # Set sub-menus for menu in rootMenuDtos: menu. name = to_pinyin (menu. menu_name) subMenus = queryByParent (menu. id) if len (subMenus): subMenuDtos = model_list_2_dto_list (subMenus, "MenuDto (id ='', menu_name = '', menu_url ='', type = '', parent = '', subMenus ='') ") menu. subMenus = set_subMenuDtos (menu. id, subMenuDtos) else: menu. _ delattr _ ('submenus') menus_json = json. dumps (rootMenuDtos, default = lambda o: o. _ dict __, sort_keys = True, allow_nan = false, skipkeys = true) # convert to a dictionary. Otherwise, the returned string will contain "\" menus_dict = json_dict (menus_json) return fullResponse (menus_dict) fullResponsefrom flask import jsonifydef fullResponse (data = '', msg ='', code = 0): if msg = '': return jsonify ({'code': code, 'data': data}) elif data = '': return jsonify ({'code': code, 'msg ': msg}) else: return jsonify ({'code': code, 'msg ': msg, 'data': data })

Note: The meaning of json in python is similar to that in the dictionary. When the last json is returned to the page, use the json_dict method to convert it to the dict type. Otherwise, the returned string contains "\".

Query Result

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.