Discreet Cosine Transform

A few thoughts on ruby, video and other things

Getting Things Done in Python, Ruby or Dart

There are many, many posts out there about how to accomplish similar steps in Python or Ruby. However, I have found that few cover the exact topics I was looking for. There is a certain set of small routines I find myself doing very often in Ruby (the language I know best), and I wanted to practice and commit to muscle-memory how to do those in Python (which I have been learning for my current job).

I have also been (trying to) learn Dart, mostly for client-side script (you can also use it server-side). Admittedly, when working with it for client-side, not every use case I am thinking of applies. But, I found working with Python this way has really helped me pick up the syntax and common practices that differ between the two languages, so I am hoping there are enough that are relevant to Dart to help there too.

First One - Single-line Assignment Plus Test

Ruby

Ruby supports assignment and test in a single line, like:

1
2
3
4
5
if test = true || false
  puts test #prints true
end

puts test #prints nil

Where the variable test is scoped inside the if block.

That can be useful, not just for the elegant syntax but due to the controlled scope. Another similar style, though not exactly assignment plus test, is Ruby’s =~ comparison operator. It is not the same as assignment plus test because the scope is not controlled - last_match is a class method and available even once you leave the block that started with the original comparison. But it looks and feels much the same, and to me has the same elegance in terms of syntax and readability:

1
2
3
4
5
some_string = "Matches this expression"

if some_string =~ /matches this (\w+)/i
  puts Regexp.last_match(1)
end

Python

This is for Python 2.7.8, the version I have been learning. I understand much is different and improved in Python 3, so its very possible the following answer is not applicable there!

Python does not have single line assignment and test. There are some alternatives but none are very readable or elegant in my opinion (none that I have seen yet).

Specifically for the regular expression use case, the core Python regex methods return either a match object or None, so you can use a if statement to move on only if a match was made. That makes your code look like:

1
2
3
4
5
6
7
8
9
import re

some_string = "Matches this expression"

match = re.search(r"matches this (\w+)",some_string,re.I)

if match:
    print match.group(1)

And while again there is no special scope control here, from a garbage collection standpoint it doesn’t seem bad, as if the regex matched nothing, your variable is only None rather than a more complex (but useless) match object.

Dart

The first thing we have to talk about with Dart is that there is no cli REPL. This makes some sense, again if you think of Dart as mostly a client-side, in-browser language. Of course, you can write server-side Dart so a REPL for that use-case makes sense, and it seems like there is some vote for that within the Dart community.

The is a REPL built into Dartium, but to invoke it you need to load a page that includes Dart code, and then use that REPL in the context of that code (it has access to only the libraries imported on that page, for instance).

So perhaps the easiest way to try simple examples like this is to make a very small cli script and execute it each time with the dart interpreter.

I also tried all this against Dart 1.7.2, so like above, I am not sure if it is exactly the same in other versions.

So now to single-line assignment and test - looks like Dart doesn’t support it either (I am the least sure of this of the three though, as Dart seems new enough and me new enough to it that maybe I am just missing some way to twist the syntax into this behavior).

Similar to Python, your assignment then test code ends up like:

1
2
3
4
var test = true || false;
if(test) {
  print(test);
}

Dart has a RegExp library as part of dart:core (so you don’t need to import anything to get to it). The behavior of methods like firstMatch is to return null if no match is made, however, unlike Python, Dart is extremely strict about truthfulness. So you need to be explicit in your check for the match:

1
2
3
4
5
6
7
RegExp exp = new RegExp(r"matches this (\w+)", caseSensitive: false);

Match match = exp.firstMatch("Matches this expression");

if(match != null) {
  print(match.group(1));
}

Conclusion

Ruby’s assignment-and-test like syntax for regular expression testing and then using parts of the match is a very nice syntax. Python and Dart don’t have similar constructs, but the total complexity to execute a possible match, test results, then work with the match is still fairly low. Also, while Ruby supports true assignment-and-test, Python and Dart do not. Again, not the end of the world, just good to know when hoping between the languages.

Comments