views:

140

answers:

5

For rapidly changing business rules, I'm storing IronPython fragments in XML files. So far this has been working out well, but I'm starting to get to the point where I need more that just one-line expressions.

The problem is that XML and significant whilespace don't play well together. Before I abandon it for another language, I would like to know if IronPython has an alternative syntax.

+4  A: 

IronPython doesn't have an alternate syntax. It's an implementation of Python, and Python uses significant indentation (all languages use significant whitespace, not sure why we talk about whitespace when it's only indentation that's unusual in the Python case).

Ned Batchelder
Ok, then I guess I've got to drop it on favor of something else.
Jonathan Allen
You might also have to give your users some whitespace sensitivity training if you use ironruby: http://lucumr.pocoo.org/2008/7/1/whitespace-sensitivity
DiggyF
Damn. Thanks for the warning, that is exactly the kind of thing that is going to cause me headaches down the road.
Jonathan Allen
+1  A: 

I think you can set the xml:space="preserve" attribute or use a <![CDATA[ to avoid other issues, with for example quotes and greater equal signs.

DiggyF
That would be way too noisy.
Jonathan Allen
xml:space="preserve" is actually the default, so shouldn't need to be specified.
Ned Batchelder
Yea, tell that to the XML editor in Visual Studio.
Jonathan Allen
+2  A: 

Apart from the already mentioned CDATA sections, there's pindent.py which can, among others, fix broken indentation based on comments a la #end if - to quote the linked file:

When called as "pindent -r" it assumes its input is a Python program with block-closing comments but with its indentation messed up, and outputs a properly indented version.

...

A "block-closing comment" is a comment of the form '# end <keyword>' where is the keyword that opened the block. If the opening keyword is 'def' or 'class', the function or class name may be repeated in the block-closing comment as well. Here is an example of a program fully augmented with block-closing comments:

def foobar(a, b):
    if a == b:
        a = a+1
    elif a < b:
        b = b-1
        if b > a: a = a-1
        # end if
    else:
        print 'oops!'
    # end if
# end def foobar

It's bundeled with CPython, but if IronPython doesn't have it, just grab it from the repository.

delnan
+2  A: 
>>> from __future__ import braces
  File "<stdin>", line 1
    from __future__ import braces

                                 ^
SyntaxError: not a chance
Dino Viehland
I'd love to upvote for win, but it's not exactly helpful.
delnan
Same here. But in all seriousness, the one thing I like about JavaScript is that by using braces it allows you to put everything on one line. Too bad there isn't a production quality DLR implementaion yet.
Jonathan Allen
+2  A: 

All I want is something that will let my users write code like

Ummm... Don't do this. You don't actually want this. In the long run, this will cause endless little issues because you're trying to force too much content into an attribute.

Do this.

<Rule Name="Markup">
    <Formula>(Account.PricingLevel + 1) * .05</Formula>
</Rule>

You should try not to have significant, meaningful stuff in attributes. As a general XML design policy, you should use tags and save attributes for names and ID's and the like. When you look at well-done XSD's and DTD's, you see that attributes are used minimally.

Having the body of the rule in a separate tag (not an attribute) saves much pain. And it allows a tool to provide correct CDATA sections. Use a tool like Altova's XML Spy to assure that your tags have space preserved properly.

S.Lott
While I indend to offer that as an option to my users, that doesn't change the fact that XML editing tools will still frag the indention levels inside the Formula element.
Jonathan Allen