Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. super_list("first level", 5, ..., 9, array.move_up, array.move_up, "second level", 10, ..., 15, array.move_down, "down one level")
  2.  
  3. ['first level', 5, 6, 7, 8, 9, [['second level', 10, 11, 12, 13, 14, 15], 'down one level']]
  4.  
  5. def get_in_list(lst, indexes):
  6. """Gets an item in a nested list by a list of indexes."""
  7. return functools.reduce(operator.getitem, indexes, lst)
  8.  
  9. def super_list(*args):
  10. """Special initialization syntax for lists."""
  11. curr_index = []
  12. result = []
  13. for index, item in enumerate(args): # Iterate over args with indexes
  14. el_type = type(...) # Type of the Ellipsis object
  15. if isinstance(item, el_type): # Case: Ellipsis range generator
  16. if index == 0:
  17. get_in_list(result, curr_index).append(item)
  18. else:
  19. get_in_list(result, curr_index).extend(list(range(args[index-1]+1, args[index+1])))
  20. elif item == array.move_up: # Case: move up one level in list
  21. get_in_list(result, curr_index).append([])
  22. curr_index.append(len(get_in_list(result, curr_index))-1)
  23. elif item == array.move_down: # Case: move down one level in list
  24. try:
  25. curr_index.pop()
  26. except IndexError: # Silently catch if user tries to move down too far in the list
  27. pass
  28. else: # Case: No special syntax - regularly append item to list
  29. get_in_list(result, curr_index).append(item)
  30. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement