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