

class millipede:
    def __init__(self, size, comment=None):
        self._millipede = ""
        if comment:
            self._millipede = comment + "\n\n"

        self._millipede += "    ╚⊙ ⊙╝ \n"
        padding = 2
        direction = -1
        while (size):
            for i in range(0, padding):
                self._millipede += " "
            self._millipede += "╚═(███)═╝\n"
            padding += direction

            if padding == 0:
                direction = 1
            elif padding == 4:
                padding = 3
                direction = -1
            size -= 1

    def __str__(self):
        return self._millipede

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Millipede generator')
    parser.add_argument('size', metavar='s', type=int, help='the size of the millipede')
    parser.add_argument('comment', metavar='c', type=str, help='the comment', nargs="?")
    args = parser.parse_args()

    print(millipede(args.size, args.comment))



class millipede:

    def __init__(self, size, comment=None, reverse=False):
        self._padding_offsets = [2, 1, 0, 1, 2, 3, 4, 4, 3]

        head = "    ╔⊙ ⊙╗\n" if reverse else "    ╚⊙ ⊙╝\n"
        body = "".join([
            "{}{}\n".format(
                " " * self._padding_offsets[(x + 3) % 9 if reverse else x % 9],
                "╔═(███)═╗" if reverse else "╚═(███)═╝"
            )
            for x in range(size)
        ])

        self._millipede = ""
        if reverse:
            self._millipede += body + head
            if comment:
                self._millipede += "\n" + comment
        else:
            if comment:
                self._millipede += comment + "\n\n"
            self._millipede += head + body

    def __str__(self):
        return self._millipede

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Millipede generator')
    parser.add_argument('size', metavar='s', type=int, help='the size of the millipede')
    parser.add_argument('comment', metavar='c', type=str, help='the comment', nargs="?")
    parser.add_argument('-r', '--reverse', action='store_true', help='reverse the millipede')
    args = parser.parse_args()

    print(millipede(args.size, comment=args.comment, reverse=args.reverse))

Rewrite body generation and add reverse option
Rewrite body generation and add reverse option

  
  
  class millipede:
+ 
-     def __init__(self, size, comment=None):
+     def __init__(self, size, comment=None, reverse=False):
+         self._padding_offsets = [2, 1, 0, 1, 2, 3, 4, 4, 3]
+ 
+         head = "    ╔⊙ ⊙╗\n" if reverse else "    ╚⊙ ⊙╝\n"
+         body = "".join([
+             "{}{}\n".format(
+                 " " * self._padding_offsets[(x + 3) % 9 if reverse else x % 9],
+                 "╔═(███)═╗" if reverse else "╚═(███)═╝"
+             )
+             for x in range(size)
+         ])
+ 
          self._millipede = ""
+         if reverse:
+             self._millipede += body + head
-         if comment:
+             if comment:
+                 self._millipede += "\n" + comment
+         else:
+             if comment:
-             self._millipede = comment + "\n\n"
+                 self._millipede += comment + "\n\n"
- 
-         self._millipede += "    ╚⊙ ⊙╝ \n"
-         padding = 2
-         direction = -1
-         while (size):
-             for i in range(0, padding):
-                 self._millipede += " "
+             self._millipede += head + body
-             self._millipede += "╚═(███)═╝\n"
-             padding += direction
- 
-             if padding == 0:
-                 direction = 1
-             elif padding == 4:
-                 padding = 3
-                 direction = -1
-             size -= 1
  
      def __str__(self):
          return self._millipede
  
  if __name__ == "__main__":
      import argparse
  
      parser = argparse.ArgumentParser(description='Millipede generator')
      parser.add_argument('size', metavar='s', type=int, help='the size of the millipede')
      parser.add_argument('comment', metavar='c', type=str, help='the comment', nargs="?")
+     parser.add_argument('-r', '--reverse', action='store_true', help='reverse the millipede')
      args = parser.parse_args()
  
-     print(millipede(args.size, args.comment))
+     print(millipede(args.size, comment=args.comment, reverse=args.reverse))
  
Rewrite body generation and add reverse option
## Code Before:


class millipede:
    def __init__(self, size, comment=None):
        self._millipede = ""
        if comment:
            self._millipede = comment + "\n\n"

        self._millipede += "    ╚⊙ ⊙╝ \n"
        padding = 2
        direction = -1
        while (size):
            for i in range(0, padding):
                self._millipede += " "
            self._millipede += "╚═(███)═╝\n"
            padding += direction

            if padding == 0:
                direction = 1
            elif padding == 4:
                padding = 3
                direction = -1
            size -= 1

    def __str__(self):
        return self._millipede

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Millipede generator')
    parser.add_argument('size', metavar='s', type=int, help='the size of the millipede')
    parser.add_argument('comment', metavar='c', type=str, help='the comment', nargs="?")
    args = parser.parse_args()

    print(millipede(args.size, args.comment))

## Instruction:
Rewrite body generation and add reverse option
## Code After:


class millipede:

    def __init__(self, size, comment=None, reverse=False):
        self._padding_offsets = [2, 1, 0, 1, 2, 3, 4, 4, 3]

        head = "    ╔⊙ ⊙╗\n" if reverse else "    ╚⊙ ⊙╝\n"
        body = "".join([
            "{}{}\n".format(
                " " * self._padding_offsets[(x + 3) % 9 if reverse else x % 9],
                "╔═(███)═╗" if reverse else "╚═(███)═╝"
            )
            for x in range(size)
        ])

        self._millipede = ""
        if reverse:
            self._millipede += body + head
            if comment:
                self._millipede += "\n" + comment
        else:
            if comment:
                self._millipede += comment + "\n\n"
            self._millipede += head + body

    def __str__(self):
        return self._millipede

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Millipede generator')
    parser.add_argument('size', metavar='s', type=int, help='the size of the millipede')
    parser.add_argument('comment', metavar='c', type=str, help='the comment', nargs="?")
    parser.add_argument('-r', '--reverse', action='store_true', help='reverse the millipede')
    args = parser.parse_args()

    print(millipede(args.size, comment=args.comment, reverse=args.reverse))
