Home Section Blog Program Choice of Development Environment
Choice of Development Environment PDF Print E-mail
Blog - Program
Written by Dennis Reinhardt   
Monday, 05 January 2009 16:08

I have been contemplating changing from my Python 2.3 standard with HTML User Interface (UI) to some other environment.  The leading choices are:

  1. Python 2.3 with templating Html UI
  2. Python 2.5 with WX UI
  3. Python 3.0
  4. Delphi
  5. .net

One problem I have with my current UI is that Python is not really very good at embedding html.  Spaces are part of the Python language and embedding indented html is a mess.  I could define a global string and use triple quoting to define a multi-line snippet but variable substitution is painful.

What I want to do is collect html snippets into an external file, one that I can review in a web browser and then pull that into my program.  I looked though web fameworks and templating systems in Python and did not see what I wanted.  The closest, WASP, has a GNU license and I cannot use it in a commercial product.  So, I will write my own.  The initial specs for expansion of prototype are

Prototype expansion
-------------------
$name is a text substitution from

    1) a template file tpl/name.html

or the following entered into a global dictionary

    2) the text value for name in a global dictionary
    3) a subroutine call which when made returns text

The regex for $name is [$][a-zA-Z][a-zA-Z_0-9]*

Expansion of the prototype takes place repeatedly until no more substitutions can be made or MAXEXPANSION limit hit.  

$name that cannot be expanded are left in the prototype as is.  This allows debug and dollar amounts to not require coding.

$_ explicitly escapes to $ and $! to null. Thus Micro$oft decodes to Micro$_oft and $namemore would decode to $name$!more.  Explicit escapes imply that escape remains in prototype until all other expansions are made.  $_ is done last so that $_! decodes to $!

Using $$ as an escape is difficult in the presence of repeated substitution because the parser must backup a character to see if $name is really $$name.  Further, $name1$name2 is difficult to parse for $name2 since we have already substituted $name1.

Next step is to set up a development directory for TextToolKit and prototype this facility. [I have done this and edited the definiiton based on testing]

  : completely lock the comment area

Trackback(0)
Comments (2)Add Comment
0
Plain text templates
written by Dylan, January 06, 2009
One problem I have with my current UI is that Python is not really very good at embedding html. Spaces are part of the Python language and embedding indented html is a mess.


Python's shortcoming here isn't much of a practical disadvantage because embedding html in code is a poor idea anyway. Best practices in this area include maintaining a reasonably strict separation among content, logic, and layout.

If none of the many Python templating systems are to your liking, you might be well-enough served by using a plain text file that contains html and some %(varname)s insert points. There's no need for triple-quoting... you can just read the file in as text and perform regular string substitutions on it, eg:

file contents (test.txt)
--------------
This %(objname)s is %(status)s


code
----------
>>> template = open('test.txt','r').read()
>>> context = {'objname': 'system', 'status':'working'}
>>> template % context
'This system is working'


That's a usable templating system in almost no code at all. If your needs grow to include display logic, the value of using an existing templating system may increase.
Dennis Reinhardt
Re: Plain text templates
written by Dennis Reinhardt, January 08, 2009
Thanks, Dylan. Your example is neat and compact. I am coming from a background where I have been embedding html in code and am moving to the separation you describe.

I ended up with a similar system whereby the test.txt file looks like

This $objname is $status.

and dictionary entries for objname and status are used for performing substitution. The code is working and is a superset of the native %(...) string substitution.

In particular, the value substituted can be a text string, a callable which returns a text string, or the contents of a file. The type of substitution is controlled by what is put in the dictionary and the implementation of the substitution can vary without making any changes to the text files.

I did look at 2.5 templating before writing my own system (I am on 2.3 now and would upgrade if necessary). Because I am writing a multi-pane windowing system, my guess is that I would need to implement hierarchical dictionaries and perhaps parameter setting within files. This is not what I have now. What I have now is a flat, albeit multi-level, system to expand out a template.

Write comment
This content has been locked. You can no longer post any comments.

busy
Last Updated on Monday, 19 January 2009 10:23
 
Copyright © 2010 TextToolKit. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.