Python regular expression style?

One way that would be a lot cleaner is using a dictionary: PATTERNS = {'pattern1': re. Compile('foo. *baz'), 'snake': re.

Compile('python'), 'knight': re. Compile('Aarthur|Bbedevere|Llauncelot')} That would solve your problem of having a polluted namespace, plus it's pretty obvious to anyone looking at your code what PATTERNS is and will be used for, and it satisfies the CAPS convention for globals. In addition, you can easily call re.

Match(PATTERNSpattern) or whatever it is your logic calls for.

One way that would be a lot cleaner is using a dictionary: PATTERNS = {'pattern1': re. Compile('foo. *baz'), 'snake': re.

Compile('python'), 'knight': re. Compile('Aarthur|Bbedevere|Llauncelot')} That would solve your problem of having a polluted namespace, plus it's pretty obvious to anyone looking at your code what PATTERNS is and will be used for, and it satisfies the CAPS convention for globals. In addition, you can easily call re.

Match(PATTERNSpattern), or whatever it is your logic calls for.

I like this a lot! Thanks! – orangeoctopus Jul 6 '10 at 16:48.

I also tend to use your first approach but I've never benchmarked this. One thing to note, from the documentation, is that: The compiled versions of the most recent patterns passed to re.match(), re.search() or re.compile() are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions. One worry is that you could have regular expressions that don't get used.

If you compile all expressions at module load time you could be incurring the cost of compiling the expression but never benefiting from that "optimization". I don't suppose this would matter unless you compile lots of regular expressions that never get used. One thing I do recommend is to use the re.

VERBOSE (or re. X) flag and include comments and white space to make anything beyond the most trivial regular expression more readable.

The reason I don't like my first approach is because it clogs up the namespace, and the actual code is not associated with the code that is running it. I wish there was a way to make the code easier to read. – orangeoctopus Jul 6 '10 at 16:23 If you want to make code easier to read, don't use regex.

Of course that will probably complicate your code if you're using a lot of regexes. – Wayne Werner Jul 6 '10 at 16:25 1 One way to (semi)avoid clogging up the namespace is to put one or two underscores before your module variables to avoid exporting the variable or mangle its name. – Andrew Walker Jul 6 '10 at 16:33.

I personally use your first approach where the expressions I'll reuse are compiled early on and available globally to the functions / methods that will need them. In my experience this is reliable, and reduces total compile time for them.

Needn’t worry about compiling regular expressions. One worry is that you could have regular expressions that don't get used. If you compile all expressions at module load time you could be incurring the cost of compiling the expression but never benefiting from that "optimization".

I don't suppose this would matter unless you compile lots of regular expressions that never get used. One thing I do recommend is to use the re. VERBOSE (or re.

X) flag and include comments and white space to make anything beyond the most trivial regular expression more readable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions