4 ms·
How many errors?
- jheriko 11y agothat code stinks on many levels. matching test in a string with a regex is so far away from being part of a good solution to this problem on any level imo... its a recipe for pain. i'm sorry anyone has to work in that codebase.
- eridius 11y agoYou're taking regex hate way too far. This is actually pretty much an ideal use-case for regular expressions. The correct regular expression looks like /\b[Tt]est/, which is trivial to write by anyone who has a passing familiarity by regex, easy to read, and will do exactly what it's trying to do.
- jheriko 11y agoits nothing to do with the regex. its the algorithm and its suitability 'any function containing test in its name' being a test is a stinky practice. using something well defined is what i would expect from any programmer at any level. this is what we call a 'hack'... and its a particularly revolting one.
- eridius 11y agoI'm going to go out on a wild limb here and guess that you have no clue whatsoever what a regex is, because your comment makes absolutely no sense. The original regex from the article is wildly broken, yes. But the trival regex /\b[Tt]est/ does not match "any function containing test in its name". And this regex is extremely well-defined. The fact that you can't understand it, and the fact that you are making no effort whatsoever to learn even the most basic stuff about regexes, does not make this a 'hack', it just makes you one.
- jheriko 11y agonope. use them every day, but not as a pro expert because its rarely necessary. i don't know what \b is off the top of my head but i'd guess at word boundary so its only checking for functions starting with test, which is equally bad... matching strings contents of function names is not a good way to work. using well defined constructs is. thank you for the hate though.
- TheLoneWolfling 11y agoYou have a problem. You use a regex. Now you have two problems.
- miander 11y agoPlease do not use that phrase... http://regex.info/blog/2006-09-15/247 http://regex.info/blog/2006-09-15/247 EDIT: I take that back, you're actually using it in its original context :)
- monochromatic 11y agoI'm not 100% sure what the intent of the code is, but it sounds like something that doesn't need a regex at all. Would 'test' in x.lower() work? Or maybe x[:4].lower() == 'test' if you want it to be at the beginning.
- dualbus 11y agox[:4].lower() = 'test' ?
- monochromatic 11y agoWhoops, forgot the double equal. As penance, I'll quote some Steve Yegge. > Error Prone == Evil > Although this concept is obvious to 99.999% of the general population, it's only accepted by 2% of computer programmers. The remaining 98% subscribe to a competing philosophy, namely: "Error Prone = Manly". Never mind that they just assigned "Manly" to the variable "Error Prone", and the expression always returns true; that happens to be the correct value in this case, so it's an acceptable hack.
- icebraining 11y agoExcept it'd just error out with "can't assign to function call". You also can't assign in if/while (they expect an expression, while assignment makes it a statement). So, all-in-all, in Python it's not really error prone unless you never run the code at all.
- monochromatic 11y agoIn other news, sometimes jokes are not literally true.
- lgas 11y agox.startswith("test")
- 11y ago
- thaumasiotes 11y ago> The regex here is using an r"" string, as all regexes should. I don't understand how \ ever became the escape character for regular expressions (and JSON!) in the first place. Every language gets its own unique escape character -- that way, you don't need an exponential number of characters in the source code to represent a single character in the model. This seems to have been understood early on; C uses \ as the escape character for strings and % as the escape character for printf directives. Common Lisp uses \ for strings and ~ for format directives. HTML uses &. When I wrote a regex parser, I made the escape character /, making a clear distinction between \n (a raw newline - just character data to the regex parser) and /n (an escaped n - special to the regex parser). But somehow every mainstream regex library uses \, gratuitously adding a lot of mental load to figuring out how much escaping you need. String.split("\\s+") is a usability failure, not something to be proud of, and definitely not something to imitate. What happened? Imagine you're looking for use of a single backslash in some JSON data. It'll be escaped, appearing literally in the data as \\. If (because this is an example to show why sharing escape characters between languages is idiotic) you want to search for it with a regex, you need \\\\. But to get that regex, you need to escape each of those four backslashes yet again, because you construct them from strings. So you've got re.compile('\\\\\\\\') to match what is notionally a single backslash in the data you're searching through. Compare a hypothetical world where \ is the escape character for strings, @ is the escape character for regexes, and + is the escape character for JSON: re.compile('++') # matches escaped + in the data re.compile('@@') # matches @ in the data re.compile('\\') # matches \ in the data
- eridius 11y agoWhy are you using regexes to search through raw JSON? That's pretty messy. The short answer for "why does everything use \" is because using a different escape character for each different grammar very quickly exhausts the potential space of uncommon punctuation, and becomes exceedingly confusing as anyone who works with more than one of these grammars now has to remember which one uses which escape character.
- thaumasiotes 11y ago> Why are you using regexes to search through raw JSON? >> (because this is an example to show why sharing escape characters between languages is idiotic) I turned one backslash into eight backslashes with a contrived example. While it's not common to go through so many layers, this problem is very real, and, in the article, is directly responsible for a bug. > using a different escape character for each different grammar [...] becomes exceedingly confusing as anyone who works with more than one of these grammars now has to remember which one uses which escape character Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)? I'd say that C strings and C format directives use different escape characters specifically because they're likely to be used together. But regexes are intended to be used with strings, too.
- js2 11y agoComment on the post links to https://github.com/nose-devs/nose/issues/11 https://github.com/nose-devs/nose/issues/11 which reported the issue in 2011. Ugh.
- bobwaycott 11y agoThat is deceptive, as it is a mirrored issue from when the project was brought to Github (which is why it reads so oddly, all from the same author). It'd been reported nearly 20 months prior on googlecode: https://code.google.com/p/python-nose/issues/detail?id=335 https://code.google.com/p/python-nose/issues/detail?id=335 This issue has been open since April 2010. What a shame.