Showing posts with label ASCII. Show all posts
Showing posts with label ASCII. Show all posts

Sunday, April 19, 2015

asciiflow.com: Draw flowcharts online, in ASCII

By Vasudev Ram



Saw this today: asciiflow.com

asciiflow.com is a site that allows you to draw flowcharts online, on their site, using the metaphor of a drag-and-drop paint program like MS Paint, but the flowcharts are drawn entirely using ASCII characters.

I tried it out a bit. Innovative.

One point is that to save the flowchart, it requires access to your Google Drive account.

The image at the top of this page, is of a flowchart that I created with asciiflow.com. I did not use the Save feature, but instead took a screenshot and saved it as a PNG file (using MS Paint, ha ha). The flowchart shows a diagram that illustrates the concept of a UNIX command pipeline, where the standard output of a preceding program becomes the standard input of a succeeding one (in the pipeline). (How's that for using web-based and Windows software to illustrate something about UNIX? :)

For another example of the innovative use of ASCII characters, check out this post I wrote somewhat recently, about the Python library called PrettyTable, which lets you generate visually appealing tables of data, bordered and boxed by ASCII characters:

PrettyTable to PDF is pretty easy with xtopdf

Also, since we're talking about standard input and output and UNIX pipelines, these two posts may be of interest:

1) [xtopdf] PDFWriter can create PDF from standard input

(The post at the above link also has an example of eating your own dog food.)

2) Print selected text pages to PDF with Python, selpg and xtopdf on Linux

Generalizing from a fragment of code in post 1) above, I'll also note that making a Python program usable as a component of a UNIX pipeline, can, in some cases, be as simple as having something like this in your code:
import sys
# ...
    for lin in sys.stdin:
        lin = process(lin)
        sys.stdout.write(lin)
which could be shortened to:
for lin in sys.stdin:
    sys.stdout.write(process(lin))
Due to this (being able to easily make a Python program into a component of a UNIX pipeline), you can do things like this (and more):

$ foo | bar | baz

where foo may be a built-in UNIX command (a filter) or a shell script, bar may be (for example) a Perl program that leverages some powerful Perl features, and baz may be a Python program that leverages some powerful Python features, thereby leveraging the UNIX philosophy concept of writing small programs, each of which do one thing well, or in this case, leveraging the features of different languages (each of which may do some things better than others), to write individual components in those respective languages. The possibilities are limitless ...

- Enjoy.

- Vasudev Ram - Online Python and Linux training;
freelance Python programming

Dancing Bison Enterprises

Signup to hear about new software products that I create.

Posts about Python  Posts about xtopdf

Contact Page

Wednesday, March 11, 2015

ASCII Table to PDF with xtopdf

By Vasudev Ram

Recently, I had the need for an ASCII table lookup, which I searched for and found, thanks to the folks here:

www.ascii-code.com

That gave me the idea of writing a simple program to generate an ASCII table in PDF. Here is the code for a part of that table - the first 32 (0 to 31) ASCII characters, which are the control characters:

# ASCIITableToPDF.py
# Author: Vasudev Ram - http://www.dancingbison.com
# Demo program to show how to generate an ASCII table as PDF,
# using the xtopdf toolkit for PDF creation from Python.
# Generates a PDF file with information about the 
# first 32 ASCII codes, i.e. the control characters.
# Based on the ASCII Code table at http://www.ascii-code.com/

import sys
from PDFWriter import PDFWriter

# Define the header information.
column_names = ['DEC', 'OCT', 'HEX', 'BIN', 'Symbol', 'Description']
column_widths = [4, 6, 4, 10, 7, 20]

# Define the ASCII control character information.
ascii_control_characters = \
"""
0    000    00    00000000    NUL    �         Null char
1    001    01    00000001    SOH             Start of Heading
2    002    02    00000010    STX             Start of Text
3    003    03    00000011    ETX             End of Text
4    004    04    00000100    EOT             End of Transmission
5    005    05    00000101    ENQ             Enquiry
6    006    06    00000110    ACK             Acknowledgment
7    007    07    00000111    BEL             Bell
8    010    08    00001000    BS             Back Space
9    011    09    00001001    HT    	         Horizontal Tab
10    012    0A    00001010    LF    
         Line Feed
11    013    0B    00001011    VT             Vertical Tab
12    014    0C    00001100    FF             Form Feed
13    015    0D    00001101    CR    
         Carriage Return
14    016    0E    00001110    SO             Shift Out / X-On
15    017    0F    00001111    SI             Shift In / X-Off
16    020    10    00010000    DLE             Data Line Escape
17    021    11    00010001    DC1             Device Control 1 (oft. XON)
18    022    12    00010010    DC2             Device Control 2
19    023    13    00010011    DC3             Device Control 3 (oft. XOFF)
20    024    14    00010100    DC4             Device Control 4
21    025    15    00010101    NAK             Negative Acknowledgement
22    026    16    00010110    SYN             Synchronous Idle
23    027    17    00010111    ETB             End of Transmit Block
24    030    18    00011000    CAN             Cancel
25    031    19    00011001    EM             End of Medium
26    032    1A    00011010    SUB             Substitute
27    033    1B    00011011    ESC             Escape
28    034    1C    00011100    FS             File Separator
29    035    1D    00011101    GS             Group Separator
30    036    1E    00011110    RS             Record Separator
31    037    1F    00011111    US             Unit Separator
"""

# Create and set some of the fields of a PDFWriter instance.
pw = PDFWriter("ASCII-Table.pdf")
pw.setFont("Courier", 12)
pw.setHeader("ASCII Control Characters - 0 to 31")
pw.setFooter("Generated by xtopdf: http://slid.es/vasudevram/xtopdf")

# Write the column headings to the output.
column_headings = [ str(val).ljust(column_widths[idx]) \
    for idx, val in enumerate(column_names) ]
pw.writeLine(' '.join(column_headings))

# Split the string into lines, omitting the first and last empty lines.
for line in ascii_control_characters.split('\n')[1:-1]:

    # Split the line into space-delimited fields.
    lis = line.split()

    # Join the words of the Description back into one field, 
    # since it was split due to having internal spaces.
    lis2 = lis[0:5] + [' '.join(lis[6:])]

    # Write the column data to the output.
    lis3 = [ str(val).ljust(column_widths[idx]) \
        for idx, val in enumerate(lis2) ]
    pw.writeLine(' '.join(lis3))

pw.close()
Discerning readers will notice the effect of some of the said control characters on the displayed program code :)

You can run the program with:
python ASCIITableToPDF.py
or
py ASCIITableToPDF.py
or
./ASCIITableToPDF.py
or
ASCIITableToPDF.py
Figuring what needs to be done for each of the above methods of invocation to work, is (as they say in computer textbooks), left as an exercise for the reader :)

(There is a bit of subtlety involved, at least for beginners, particularly if you want to do it on both Linux and Windows.)

And here is a screenshot of the PDF output of the program:


- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services from me.

Posts about Python  Posts about xtopdf

Contact Page