The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Why use Python/Perl?

I'm looking for some idea of how others have used scripting languages as a supplement to their main development tasks. My group is almost 100% .Net using TFS, Team Build, and Team System to develop websites.

I know many shops use some sort of scripting language to help them get things done quickly but so far I worry I am so .Net jaded that I am missing out on some of the key reasons to do this.

Anybody wanna throw out some issues they've had and how/why they felt a scripting language was the most appropriate path to fulfill their goal(s)?

Thanks!
Justin Bozonier Send private email
Monday, September 17, 2007
 
 
Take a look at PowerShell.
*myName Send private email
Monday, September 17, 2007
 
 
Thanks for the quick reply!

Yeah I have actually. Looks good... But it still is a solution in search of a problem for me. I'm sure I have problems that could be better solved this way, I'd just like some direction/guidance on what I'm not seeing.

Monday, September 17, 2007
 
 
We often don't need the extra performance, long term "maintenance" capabilities, a whole project in the IDE consisting of one short file, intellisense to remember which API to use and what parameters, and so on, because with a powerful scripting language and with a minimum level of experience with it things can be done anywhere, anytime, in a few minutes at most, and the script file turns out as a small program which can be reuse again and again just like a GUI executable would. :-)

That said, things on Windows don't work as well as on a proper Unix system though, because while one hates the command line of Windows, justifiably so, one generally loves the command line of Unix. That is, even one doesn't have a proper editor or proper IDE handy on a Unix system, the command line and a text mode editor is all one needs to edit or create a scripting file.

I find it too costly to launch a full blown IDE to create a short program. A scripting language with a good GUI library is just as handy to create a little GUI program as an IDE would be. Also, modern uses of scripting languages include things like CGI/Web programming, because they still work well for such things. Of course, they work better on Unix systems for such things as well, where there's plenty of support to them and they are considered top notch for such use without any shame. On Windows things tend to be way different in so many regards. :-) Treat your productivity badly and you will not know why things tend to suck.
Joao Pedrosa
Monday, September 17, 2007
 
 
We use Pyton to automate performance tests for a middleware written in C# and to automate small repetitive tasks
Christophe Keller Send private email
Monday, September 17, 2007
 
 
There are a lot of good reasons to use a dynamic language (that's what the cool kids are calling scripting languages these days) in lieu of a compiled language.

You typically write less code because the languages themselves are so malleable that you end up extending the language, in a sense, to meet your needs.

No compilation means no (or less) compile-run-debug loop.  In a language like Ruby you can set a breakpoint, and when you hit it you get an interactive prompt where you can fix methods or classes by redefining them right then and there.  It's not the only language that lets you redefine things at run-time, or redefine built-in elements.

When you modularize your code you can reload files at run time so you don't have to stop the app to change the way it works.

The difference is clear as soon as you write "Hello, World!"

C#:

  public class HelloWorld
  {
    public static void Main()
    {
      System.Console.WriteLine("Hello, World!");
    }
  }

Ruby:

  puts "Hello, World!"
John Cromartie Send private email
Monday, September 17, 2007
 
 
On the Windows side, I've used Python in place of batch files. Stuff like building several components, then bundling them into an ISO, then uploading them to an FTP server, then sending an e-mail notification. You *could* do it with a batch file and some utility programs but it's way easier with Python.

(You could also use something like FinalBuilder... pick your poison.)
Nate Silva Send private email
Monday, September 17, 2007
 
 
> "The difference is clear as soon as you write "Hello, World!""

Except that there are static, compiled languages that have "Hello world" programs just as short and simple as Ruby's.  Syntax and dynamicness (or whatever the word is) are unrelated.
Iago
Monday, September 17, 2007
 
 
The contrived "Hello, World!" example is bad.  It's rare that you're ever going to want to write a one liner so the C# framing code is overstated in that example and an experienced C# programmer can knock out the framing code in about 15 seconds anyway (not to mention the VS project wizard).  I see these sort of examples from the Ruby community all the time and wonder why that's the best they can come up with.
SomeBody Send private email
Tuesday, September 18, 2007
 
 
"I see these sort of examples from the Ruby community all the time and wonder why that's the best they can come up with."

The reason these examples show up often as Ruby examples is that Ruby is very concise so it doesn't take much effort to write them. For example, Python needs indentation to work, Ruby doesn't. So it's easier to paste Ruby code anywhere, even when there isn't appropriate support for code pasting, such as in these forums.

Also, being concise is one of Ruby's strengths. It takes time to create the "right" APIs, many of which are accessible by default, without the need to import/require them as they would need in other languages. Even when one needs to require some core Ruby library, this code tends to be short too, like "require 'benchmark'":

require 'benchmark'

ds = DBRules::DataSet.new
ds.interface = :PG
ds.db_str = ['127.0.0.1', 'postgres', '', 'ralip']
ds.table = 'jpblog_post'
ds.open

puts Benchmark.measure{
  100.times{|i|
    ds.append
    ds[:POST_NAME] = "any name #{i}"
    ds.commit
  }
}

Outputs:

  0.690000  0.070000  0.760000 (  1.070191)

That's an example using database library and framework of my own, but using the benchmark library of Ruby.
Joao Pedrosa
Wednesday, September 19, 2007
 
 
OK, how about converting a list of strings to integers?

C#:
  using System;
  using System.Collections.Generic;
 
  public class Example
  {
    public static void Main()
    {
      List<string> numStrings = new List<string>(new string[] { "1", "2", "3", "" });
      List<int> nums = StringsToInts(numStrings);
      foreach (int i in nums)
      {
        Console.WriteLine(i);
      }
    }
 
    public static List<int> StringsToInts(List<string> numStrings)
    {
      List<int> result = new List<int>();
      foreach (string s in numStrings)
      {
        try
        {
    result.Add(int.Parse(s));
        }
        catch (FormatException)
        {
    result.Add(0);
        }
      }
      return result;
    }
  }

Ruby:
  numStrings = ["1", "2", "3", ""]
  nums = numStrings.map { |s| s.to_i }
  puts nums

Both produce exactly the same results.
John Cromartie Send private email
Wednesday, September 19, 2007
 
 
Well, if you really insist on writing it in only a few lines of code, you could always do the following in C#:

  List<int> nums = numStrings.ConvertAll<int>(
    delegate(string s) { return int.Parse(s); });
  nums.ForEach(new Action<int>(Console.WriteLine));

Note that Visual Studio auto-completes almost all of this so this only takes a few keystrokes to actually type out.  The new version of C# adds syntax that cuts down on the verbosity even more. 

What does the Ruby code do with invalid input?  I'm going to assume from your C# implementation that it returns 0.  Why would this be desirable behavior?  It seems like silently failing and returning 0 would be very undesirable in most circumstances resulting in bad calculations.  What does the Ruby code look like when you want to explicitly handle errors?  If you really want to silently return 0 on errors in C#, you could replace the whole unnecessary try/catch block in your C# example or my anonymous method with three lines using TryParse.
SomeBody Send private email
Wednesday, September 19, 2007
 
 
I like Perl. Any kind of text file manipulation is easier in Perl. The regular expressions are tightly integrated into the language.

Plenty of dynamic features and map() and grep() functions. The grep isn't like the Unix grep program, it's more like Python's filter() function.

If you use it in a disciplined way, the code isn't that bad to read, either.
frustrated
Thursday, September 20, 2007
 
 
SomeBody, well done!

I also am sick of these contrived examples that ignore major language features of the intended "bad" language.


I like Perl and Ruby, but the fanboys do need frequent spankings.
an old fart who keeps it real
Thursday, September 20, 2007
 
 
puts "LOL" * 100
Joao Pedrosa
Thursday, September 20, 2007
 
 
Here is a real task I had to do today -- given a 250 row Excel file, create and execute SQL statements which will munge the data from the file into the appropriate columns and insert it into the table.  Its the classic string processing task.

This was literally a two minute job in AWK (fifteen lines and half of them are braces).  I bungled it eight times -- no biggie, !na (for nano, the only text editor I need for AWK) and !ga and I can try it again until I get it right.

In Java?  I have dreams of having to remember how the StreamReader works, taking a quick trip to the Java API to refresh my memory, writing some meaningless try/catch blocks for IOExceptions, compiling, and otherwise dealing with things other than solving my immediate problem.  The penalty for bungling stuff is also relatively steeper, and I'd need to do some IDE gymnastics instead of just creating a file somewhere and nuking it when I was done.

P.S. One major timesaver is to realize that since this is a throw-away script you don't need to work with the Excel file.  Just copy/paste into notepad and set your record separator to tab.  Bam, back to being productive.  That insight helps if you're working in Java, too, although most Java programmers I know would start by Googling "Java import Excel file" or, God forbid, "Java export Excel file to XML".  And I say this as a happy Java programmer myself.
Patrick McKenzie (Bingo Card Creator) Send private email
Tuesday, September 25, 2007
 
 
Patrick, why didn't you just use Excel to generate the SQL?  For example, let's say you have an Excel spreadsheet with two columns of first names and last names and you want to enter them into a SQL Names table:

="INSERT INTO Names (FirstName, LastName) VALUES ('" & A2 & "', '" & B2 & "')"

Then copy & paste that column over to your favorite SQL command executer and you're done.  Excel can be a great code generator for things like this. 

For times when Excel isn't the best tool, you're definitely correct about the power of realizing "oh gee, I don't need to do this in Excel".  One trick that I've found useful when using .NET languages is to take advantage of the Clipboard class to get and set text on the clipboard rather than using a file as an intermediary.  This eliminates the whole sequence of copying data to Notepad, saving the file, opening the file from your script, etc..  I use this trick with IronPython whenever what I'm doing gets too unwieldy for Excel formulas.
SomeBody Send private email
Tuesday, September 25, 2007
 
 
>>
Patrick, why didn't you just use Excel to generate the SQL?
>>

There was some business logic involved in the SQL generation and I was much more comfortable about my ability to capture it quickly in gawk than in Excel.  For example, I know of no convenient way to select the the A value for all Excel rows where the C value is (a particular Japanese character).  That took me about three seconds to do in gawk. 

if ($3 == theJapaneseCharacter) foo = foo " " $3

I'm sure there must be a way to do it efficiently in Excel.  But really, three seconds.  (The actual version I used didn't even have the wasted space at the start of the foo string ;) )
Patrick McKenzie (Bingo Card Creator) Send private email
Tuesday, September 25, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz