OWASP O2 Platform Blog

Decomposing an Lamba method used in an O2 Script

I just spent some time with Sarah Baso explaining how a particular script works (she is trying to automate the creation of pdfs with OWASP Summit certificates using O2 (similar with to O2 Script – Creating PDFs with OWASP AppSec Brazil Certificates ))

I used an etherpad during this session, which at the moment can be seen here: http://primarypad.com/jnfLB22s53

This is the function that loads up the file from the local computer and returns a list of names           

Func<string, List<string>> getNames =
     (dataFile) => {
                             return (from line in dataFile.fileContents().trim().split_onLines().remove(0)
                                     select line.split(",")[1].removeFirstChar().removeLastChar()
                                    ).toList();
                        };

Here is the same function shown above, but this time in a format that is probably be easy to read

Func<string, List<string>> getNames =
     (dataFile) => {
                             return (
                                         from line in dataFile.fileContents()
                                                                     .trim()
                                                                     .split_onLines()
                                                                     .remove(0)
                                         select line.split(",")[1]
                                                        .removeFirstChar()
                                                        .removeLastChar()
                                       ).toList();
                        };

And here is a ful rewrite of it , with the same functionality but writen in a very explicity way (and not taking advantage of the fact that most O2 Extension methods will return an object that can be used on the next extension method) 

Func<string, List<string>> getNames =
     (dataFile) => {
                             var fileContents = dataFile.fileContents();   // get file contents from disk
                             fileContents = fileContents.trim();              // remote extra spaces or enters
                             var lines = splitedContents. split_onLines();  // get a list splitted by line
                             splitedContents = splitedContents.remove(0); // remove the first entry which is the names of the columns
                             var results = new List<string>();
                             foreach(var line in lines)
                             {
                                    var cells = line.split(",");           // get a list splitted by ,  (comma)
                                    var name =  cells[0];
                                    name = name. removeFirstChar(); // Need to do this because the original data was padded with ' (single quotes)
                                    results = name. removeLastChar();  // if it were spaces I could had used ->   name = name.trim();                                    results.add(name);
                             }
                             return name;
                         }

March 24, 2011 - Posted by | .NET, O2 Internals

2 Comments »

  1. […] next step for Sarah on her quest to create OWASP Summit Certificates (see Decomposing an Lamba method used in an O2 Script) is to consume the data she has on a Excel […]

    Pingback by O2Platform.com for Developers | March 24, 2011 | Reply

  2. […] next step for Sarah on her quest to create OWASP Summit Certificates (see Decomposing an Lamba method used in an O2 Script) is to consume the data she has on a Excel […]

    Pingback by O2 Script: loading data from an Xslx (OpenXml) file « O2Platform.com for Developers | March 24, 2011 | Reply


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: