refamonkeys.blogg.se

Python ascii art text generator
Python ascii art text generator




  1. #Python ascii art text generator how to#
  2. #Python ascii art text generator generator#
  3. #Python ascii art text generator code#

This, however, assumes that table has all rows of the same length. Second, when dealing with structures like table holding a list of lines, you can easily get a list of columns using zip(*table). widthsįirst of, never write anything other than a comment after a colon: it impairs readability. Thaaat's… not a side effect I would expect from such function. We can see that, when computing the length of a column, you account for varying number of columns (with your if len(row) > i and if len(header) > i) and add missing items as you go on the lines you iterate over latter on. But before we do that, let's analyze how you do things a bit. So, onto simplifying the building of your lists as a whole. List-comprehensions and generator-expressions

#Python ascii art text generator generator#

You can also provide a wrapper around the generator that '\n'.join the lines yielded. The caller will be responsible for joining them or can print them as they are generated. Instead of building a list of each line to print and then joining them, you could turn your function into a generator and yield each line as you compute it. The same kind of optimization can be applied to your function as a whole. An since join accept any iterable (meaning: anything a for loop can work with) and not only lists, you can just remove the brackets to turn the list-comprehension into a generator expression that will happily be consumed by join but not take nearly as much memory as the list. Then, once you have a nice list-comprehension you can feed it to join instead of using an intermediate variable.

#Python ascii art text generator how to#

But for it to be readable, we will need to work on how to write it. Since you build these lists using append in a for loop, you could use a list-comprehension instead. There is just widths that you keep all along.

#Python ascii art text generator code#

One of the most obvious inefficiency in your code is that you build a list only to feed it to join. There are a lot of duplicated code, inefficiencies and redundancies (wait…) but there is that one thing that you are doing right: you use join instead of trying to manually insert separators between each piece of text. > ascii = ascii_table(table, header=header, align='center', border=True) > table = list(map(lambda x: x.split(), )) If i > 0 and i 0 and i > from asciiart import ascii_table If border: printable.append('│ ' + ' │ '.join(printrow) + ' │')Įlse: printable.append(' │ '.join(printrow)) If align = 'center': printrow.append(header.center(widths))Įlif align = 'left': printrow.append(header.ljust(widths))Įlif align = 'right': printrow.append(header.rjust(widths)) Can I improve or shorten some parts of the code? Any other advice? def ascii_table (table, **k):įor i in range(max(map(len, table))): widths.append(max(max(map(len, for row in table if len(row) > i])), len(header) if len(header) > i else 0)) I'm quite new to Python, so I wanted to know about whether my writing style is OK. Table cells have auto computed lengths by the longest cell of their column, rows that have a small amount of elements get filled by blank cells to adjust their length. I've also added the options of adding a header, determining the in-cell alignment, and adding a border to the table. Out = pyfiglet.As a part of a console utilities module, I created a function that takes a table in the form of an array of arrays, and generates an ASCII table with the given contents. Running the above code gives us the following result − _ _ _ _ _ Out = pyfiglet.figlet_format("Point", font="bubble") Out = pyfiglet.figlet_format("Point", font="dotmatrix") Out = pyfiglet.figlet_format("Point", font="alligator") Out = pyfiglet.figlet_format("Point", font="3x5") Out = pyfiglet.figlet_format("Point", font="3-d")

python ascii art text generator

Out = pyfiglet.figlet_format("Point", font="slant") Running the above code gives us the following result − In the below program we see various results by choosing various font types. After installing this module we can use it to control the font that can be used to display the result. The ASCII text can be used to display many stylish texts by using the module pyfiglet.






Python ascii art text generator