Search code examples
pythonpython-2.7python-2.xjython

Human-readable, table-like structure (Python 2.7.1; standard library only)


I have a COTS system called IBM Maximo Asset Management 7.6.1.2 where I can write Python scripts.

The system has a number of unfortunate limitations:

  • The Python version is 2.7.1 (ancient). Technically, it's Jython, not true Python.
  • I can't import Python libraries; I only have access to a subset of the standard Python library.
  • I can't store files on the server (i.e. CSV or text files).
  • In this case, I can't create custom tables in the database.

I want to store some table-like data in a Python library script.

Table name: DIVISION_SETTINGS

DIVISION    FLOW_CONTROL_REQ    ACTUAL_COSTS_REQ    SETTING3    SETTING4   SETTING5
FLEET       1                   1                   0           1          A
ROADS       0                   1                   0           2          B
PARKS       0                   1                   1           3          C
DIV4        1                   0                   0           4          D
DIV5        0                   0                   0           5          E
DIV6        0                   1                   1           6          F
DIV7        0                   1                   0           7          G

I would write a Python function that would let me access the data in the script, similar to accessing data from a database table.


I've poked around the Python docs and found mechanisms like lists, collections, etc., but to my untrained eye, it looks like it's hard for people to read the data in those mechanisms.

Ideally, I'd use a Python mechanism that would let me write the data/text in a human readable format, so that it would be easy for IT staff to go into the script and update the values when needed. If the data were easy to read, and in a columnar format, then I think that would help us avoid mistakes when making changes.


Question:

Is there a way to store human-readable data in Python 2.7.1 (without adding any additional libraries, using files, or using database tables)?

(I know that's a tall order, but I thought it might be worthwhile to ask, in case there happens to be a suitable option.)


Solution

  • Seems like a pretty odd set of requirements...

    You could start the .py file with a multiline string, and the script could parse this string and turn it into some useful structure.

    The very first string in a python module is __doc__, so the script could do something like:

    """
    data    in  whatever    format  you     want
    
    """
    ############################################# 
    lines = __doc__.replace('\t',' ').split('\n')
    lines = filter(None, lines)
    table = []
    for line in lines:
        table.append(list(filter(None, line.split(' '))))
    
    #at this point, table is a list of lists of word tokens (for each row)
    

    This is just an example of turning the text into a data structure, you'll have to decide what structure makes the most sense for your data.