Home > Uncategorized > Python-Embedded XML

Python-Embedded XML

Reading Python-Embedded XML and Test Automation on the code project got me thinking.

I do agree on (and I even think it might be fundamental):

An elegant solution is to separate a piece of test program into two parts: one to model the process and the other to model the data.

fscheck makes this distinction very clear. Considering the specific solution I think it is interesting but I can’t help but thinking that the payback for this complexity is too low. My test bench will have a direct dependency to the library (that includes two dlls) which in turn has a compile time dependency to boost and runtime dependency to python.
 
If tests are written in VB.net, using embedded xml would probably be just as nice.

Imports System.Xml.Linq
 
Module Program
    Dim rnd As New System.Random
    Dim globalCount As Integer = 0
 
    Function RoundRobin(list as String()) As String
        globalCount = globalCount + 1
        RoundRobin = list(globalCount Mod list.Length)
    End Function
 
    Function ExtraCredit(prob as Double) As String
        If rnd.NextDouble() < prob Then
            Return "Yes"
        Else
            Return "No"
        End If
    End Function
 
    Sub Main()
        Dim days As String() = New String() { "Mon 7am", "Tue 2pm", "Thur 9am", "Fri 1pm" }
        Dim xml As XElement =
            <Data>
                <StudentID>SID0000<%= String.Format("{0:000}", rnd.Next(100)) %></StudentID>
                <Class><%= RoundRobin(days) %></Class>
                <ExtraCredit><%= ExtraCredit(0.2) %></ExtraCredit>
            </Data>            
        Console.WriteLine(xml.ToString())
        Console.Write("Press any key to continue . . . ")
        Console.ReadKey(True)
    End Sub
End Module

Powershell users might like:

$i = 0;
function round-robin($l)
{
    $script:i++;
    return $l[$i % $l.Length]
}
 
function extra-credit($x)
{
    $r = Get-Random -Minimum 0 -Maximum 100
    if ($r -gt $x*100) { "Yes" } else { "No" }
}
 
@"
<Data>
    <StudentID>SID0000$("{0:000}" -f (Get-Random -Minimum 0 -Maximum 100))</StudentID>
    <Class>$(round-robin ‘Mon 7am’, ‘Tue 2pm’, ‘Thur 9am’, ‘Fri 1pm’)</Class>
    <ExtraCredit>$(extra-credit 0.2)</ExtraCredit> 
</Data>
"@

Powershell script can be evaluated from C#, or .NET, using System.Management.Automation but I do not think this pays back either. Of course, this is my personal opinion and others might think differently.

Advertisements
Categories: Uncategorized
  1. Trustamind
    July 10, 2012 at 9:46 pm

    Daniel,

    This is Trustamind from code project. Thank you very much for giving my article a reference. Also both your code snippets are neat and powerful. Between them, I like powershell more, but I still feel a little bit disappointed since powershell is, in my opinion, less comprehensible to C++ or C# programmers, who are usually my customers.

    I’m interested in the F# link you provided here. I’m not familiar with F# but I can tell it’s a whole new programming paradigm based on my rather limited reading. I think “immutable” is really the key for thread-safe, parallel, and asynchronous programming. Immutable means no programmer-specified procedure and the compiler has the freedom to come up the the optimal procedure. I’d like to dig more. Please let me know if you have links to good articles about F#, immutable, threading, parallel and asynchronous programming. Thanks.

    • July 10, 2012 at 10:39 pm

      Thanks. (I started out writing a comment on your article but it got a bit lengthy so I decided to post something here instead, I hope you don’t mind.)

      Powershell is indeed quite different from C++ and C# and, for me, it took a while to get used to, However, I have learnt to appreciate many aspects of it such as the object pipeline and the dynamic nature of the languate and its compact syntax along with the productive environment that the Powershell ISE provides.

      F# is another weak spot for me. It is a multiparadigm language but its main focus is functional. F# programs can many times be both distinct and clear using features like higher order functions, pattern matching, list comprehensions, workflow syntax (monads). I recommed Expert F#, http://www.amazon.com/Expert-Hardback-Experts-Voice-NET/dp/1590598504 which is a great book that, in my opinion, is not only for experts.

      • Trustamind
        July 10, 2012 at 11:27 pm

        No I won’t. Discussion is always appreciated.

        Thanks for the link to the book. I’m thinking to get a copy.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: