Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magic method __str__
#9
I have a longer solution, but as your data resembles a Sudoku board, I think it might be interesting
allbox = u''.join(chr(9472 + x) for x in range(200))
box = [ allbox[i] for i in (2, 0, 12, 16, 20, 24, 44, 52, 28, 36, 60) ]
(vbar, hbar, ul, ur, ll, lr, nt, st, wt, et, plus) = box
hh = hbar * 7
nl = '\n'

topline = ul + (hh + nt) * 2 + hh + ur
midline = wt + (hh + plus) * 2 + hh + et
botline = ll + (hh + st) * 2 + hh + lr

def to_str(board):
    result = [topline + nl]
    for i, z in zip(range(0, 9, 3), (midline + nl, midline + nl, botline)):
        for row in board[i:i+3]:
            result.append(
                vbar + vbar.join(
                    ' ' + ' '.join(str(k) for k in row[j:j+3]) + ' ' for j in range(0, 9, 3)) + vbar + nl) 
        result.append(z)
    return ''.join(result)

if __name__ == '__main__':
    data = [
        ['.', '.', '.', '.', '.', 9, '.', '.', '.'],
        ['.', '.', 7, '.', 8, 6, '.', '.', '.'],
        [6, '.', '.', 3, '.', '.', '.', '.', '.'],
        ['.', 4, '.', '.', '.', 7, '.', '.', 8],
        ['.', '.', '.', '.', '.', '.', '.', 3, 2],
        ['.', '.', 3, 6, '.', 5, 1, '.', '.'],
        ['.', 6, '.', 7, '.', '.', '.', 8, '.'],
        [3, '.', 2, '.', '.', '.', 4, 9, '.'],
        ['.', 5, 4, 8, '.', '.', '.', '.', 3]]
    print(to_str(data))
Output:
┌───────┬───────┬───────┐ │ . . . │ . . 9 │ . . . │ │ . . 7 │ . 8 6 │ . . . │ │ 6 . . │ 3 . . │ . . . │ ├───────┼───────┼───────┤ │ . 4 . │ . . 7 │ . . 8 │ │ . . . │ . . . │ . 3 2 │ │ . . 3 │ 6 . 5 │ 1 . . │ ├───────┼───────┼───────┤ │ . 6 . │ 7 . . │ . 8 . │ │ 3 . 2 │ . . . │ 4 9 . │ │ . 5 4 │ 8 . . │ . . 3 │ └───────┴───────┴───────┘
Reply


Messages In This Thread
Magic method __str__ - by dan789 - Dec-18-2018, 04:11 PM
RE: Magic method __str__ - by stullis - Dec-18-2018, 04:55 PM
RE: Magic method __str__ - by Gribouillis - Dec-18-2018, 05:39 PM
RE: Magic method __str__ - by dan789 - Dec-18-2018, 07:17 PM
RE: Magic method __str__ - by buran - Dec-18-2018, 07:32 PM
RE: Magic method __str__ - by dan789 - Dec-18-2018, 08:12 PM
RE: Magic method __str__ - by buran - Dec-18-2018, 08:15 PM
RE: Magic method __str__ - by dan789 - Dec-18-2018, 08:37 PM
RE: Magic method __str__ - by Gribouillis - Dec-18-2018, 08:44 PM
RE: Magic method __str__ - by nilamo - Dec-18-2018, 09:04 PM
RE: Magic method __str__ - by dan789 - Dec-19-2018, 02:15 PM
RE: Magic method __str__ - by dan789 - Dec-22-2018, 03:55 PM
RE: Magic method __str__ - by ichabod801 - Dec-22-2018, 05:26 PM
RE: Magic method __str__ - by dan789 - Dec-22-2018, 07:43 PM
RE: Magic method __str__ - by ichabod801 - Dec-22-2018, 08:13 PM
RE: Magic method __str__ - by dan789 - Dec-23-2018, 10:11 AM
RE: Magic method __str__ - by ichabod801 - Dec-23-2018, 03:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  magic related field in Django model sonh 1 1,319 Apr-24-2022, 12:37 PM
Last Post: sonh
  Need a little help with numpy array magic. pmf71 0 1,224 Dec-01-2021, 02:51 AM
Last Post: pmf71
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,515 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,400 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  How to eliminate magic squares formed by the same numbers, but permuted frame 7 3,758 May-09-2019, 11:28 AM
Last Post: frame
  TypeError: __str__ returned non-string error jolinchewjb 5 10,247 Jan-24-2019, 07:54 AM
Last Post: jolinchewjb
  [split] Bad magic number. What is it ? Douglas 2 5,357 Oct-22-2018, 10:38 AM
Last Post: Douglas
  How to add metakernel magic for python rikaki__ 0 2,062 Jul-04-2018, 02:49 AM
Last Post: rikaki__
  Bad magic number. What is it ? sylas 4 7,904 Sep-20-2017, 05:52 PM
Last Post: sylas
  How to Create Very Very Special Class with too many magic methods ? harun2525 5 4,475 Apr-13-2017, 10:18 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020