4
\$\begingroup\$

Out of curiosity I was discovering the potential of the Ellipsis object in Python (...), and I discovered it doesn't do a whole lot except for a handful of minor features. In an effort to make it useful, I decided to create the most fancy-pants constructor for a list that I could.

The function, super_list, takes an arbitrary number of arguments to be added to the list. Here are the features:

  • super_list(multiple, arguments, provided) -> Generates a list using the arguments provided. If an argument doesn't follow any of the special features below, it is just appended to the array at the proper nesting level. This example returns [multiple, arguments, provided], a list of the arguments.

  • super_list(5, ..., 9) -> Takes advantage of the Ellipsis object to create something that looks like "x to y", or a range. This particular example would produce a list containing [5,6,7,8,9], the number between 5 and 9.

  • super_list(arg, array.move_up, higher_arg, array.move_down, lower_arg) -> Sets the nesting level inside the list. Including array.move_up or array.move_down moves up or down one level of nesting in the list. This example produces [arg, [higher_arg], lower_arg], moving up and down the array chain with array.move_up and array.move_down.

One last big example:

super_list("first level", 5, ..., 9, array.move_up, array.move_up, "second level", 10, ..., 15, array.move_down, "down one level")

produces

['first level', 5, 6, 7, 8, 9, [['second level', 10, 11, 12, 13, 14, 15], 'down one level']]

So this is my current implementation:

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

get_in_list is a function used to get an item at a list of indexes. This means that a[0][1] == get_in_list(a, [0, 1]).

My questions:

  • Is it too messy?

  • Is anything too long and could be implemented in a shorter way?

  • Is the program too confusing, and do you think it could be more verbose?

And obviously, any other comments you may want to add are appreciated. Thanks in advance!

asked Apr 21, 2019 at 21:22
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I'm severely confused by array.move_down, because I don't see any array or anything moving anywhere. If you are doing a super_list, wouldn't it be more readable to have sublist.begin and sublist.end?

Also I don't like fail-later approach with hiding an exception. If user makes an error in nesting lists, I would fail fast instead. It's a common behavior of all the languages I know (with an exception of early/ancient HTML).

answered Apr 21, 2019 at 21:58
\$\endgroup\$
7
  • \$\begingroup\$ "Moving" is going up or down to the next level of array nesting (move_up from [1,0] is doing [1,0,[]]). \$\endgroup\$ Commented Apr 21, 2019 at 22:01
  • \$\begingroup\$ And I used array to not name-mangle with the builtin list type, because that's bad practice. \$\endgroup\$ Commented Apr 21, 2019 at 22:02
  • \$\begingroup\$ I understand your motive, but it's confusing to me. In OOP, array.move_down means I intend to move the array down - I would never have guessed that it starts a nested sublist. Oh, sorry... ends a sublist? \$\endgroup\$ Commented Apr 21, 2019 at 22:04
  • \$\begingroup\$ I understand your point. What do you think I should change it to? I'd like to keep it short, if possible. Maybe new_list and end_list? \$\endgroup\$ Commented Apr 21, 2019 at 22:07
  • \$\begingroup\$ How about sublist and endsub \$\endgroup\$ Commented Apr 21, 2019 at 22:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.