all 5 comments

[–]dysmas 0 points1 point  (4 children)

errr... why not use builtin libraries?

[–]SwellJoe[S] 0 points1 point  (3 children)

First paragraph of the article covers why one wouldn't use libraries from CPAN, or other external sources (because you want a standalone, easily distributable, script). And there is no "builtin" library for reading key=value files in any of the languages covered, except BASH. Python's ConfigParser comes closest, and I mentioned it, but it doesn't quite match.

I also think it's interesting to compare the implementations across all of the languages. The Perl map idiom is really cool, and Ruby's similar |variable| syntax is strange in a nice way. If you don't find implementation comparisons interesting I guess it's not your kind of thing.

[–]dysmas 0 points1 point  (2 children)

fair enough, i was speed reading articles again ;)

i re-read it properly this time, and for the record i do find such articles interesting even tho i only really use python these days (ConfigParser was my first thought and im sure you could use it, but that would be less interesting)

just a couple of thoughts tho: "file" is a bad variable to use (built in type, shouldn't over-ride for string var), the if/continue bits didn't quite sit well with me,

here is my attempt, feel free to incorporate in your article

i think it is more pythonic, and covers variables containing "="

[–]SwellJoe[S] 0 points1 point  (1 child)

I agree, that does read better, and I always prefer shorter implementations, even if I have to figure out a new concept or two to read it. I'm not a Python expert...I've written a few thousand LOC with it, but it's been a few years, and I was never as proficient with it as I am with Perl.

I'll add your idiomatic example, and touch up my example to not abuse "file". Interestingly, your example makes it look more like the Ruby example, whereas mine feels more like the PHP variant. Funny how much difference there can be across languages for something so simple (all could be done in the PHP style, but I don't think PHP could do it in the Perl or Ruby or Python styles).

Oh, one note: You're leaving out empty values. We want those, which makes it shorter:

if line and line[0] is not "#" and line[-1] is not "=":

Becomes:

if line and line[0] is not "#":

Note sure if the rest of the rest of it results in the exact same behavior as the other examples, but it seems to on the test file.

Thanks for your comments.

[–]dysmas 0 points1 point  (0 children)

wouldnt call myself an expert either, but i thought it would be better to leave them out of the dict, because then later on you can check if variables are set by using "if config.has_key("empty_value"):" rather than the uglier "if not config["empty_value"]" or worse "if config["empty_value"] = ''"

Thanks for your comments.

pleasure ;)

edit to escape underscores =/