4

Problem Statement:

ID value must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

I done by regular expression.

Code:

>>> import re
>>> id_value1 =  "custom-title1"
>>> id_value2 =  "1-custom-title"
>>> pattern = "[A-Za-z][\-A-Za-z0-9_:\.]*"

Code for valid ID value

>>> flag= False
>>> try:
...     if re.finadll(pattern, id_value1)[0]==id_value1:
...         flag=True
... except:
...     pass
... 
>>> print flag
False

Code for invalid ID value:

>>> flag = False
>>> try:
...     if re.findall(pattern, id_value2)[0]==id_value2:
...         flag=True
... except IndexError:
...     pass
... 
>>> print flag
False

Code for IndexError

>>> try:
...     if re.findall(pattern, "")[0]=="":
...         print "In "
... except IndexError:
...    print "Exception Index Error"
... 
Exception Index Error
>>> 

I will move above code in one function. This function will call more then 1000 times. So can anyone optimize above code?

Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • here is a thread about `re.compile`. may be interesting. http://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile – hiro protagonist Aug 04 '15 at 12:06
  • @hiroprotagonist: I will `compile` pattern first(one time process) then same instant variable(I have ID generator as a class) will use in function. – Vivek Sable Aug 04 '15 at 12:09

2 Answers2

2

You should match the end of string, compile your pattern and use re.match() instead of re.findall()

import re
id_value1 =  "custom-title1"
id_value2 =  "1-custom-title"

pattern = "[A-Za-z][\-A-Za-z0-9_:\.]*$"
compiled = re.compile(pattern)

def validate(id):
    return bool(compiled.match(id))

print validate(id_value1)
print validate(id_value2)
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
1

I have class ID Generation.

In __init__() function I compile pattern.

def __init__(self): 
  self.id_validation = re.compile("[A-Za-z][\-A-Za-z0-9_:\.]*$")

Following is function to validate class values for ID generation because we are use class values of HTML Tag to generate ID for respective HTML Tag. Sample HTML Element: <div class="custom1 123-custom>

def validateClassNames(self, element_obj):
    """ Remove class names which are ignore in id generation.
        Validate class value according to ID generation rule.
    """
    try:
        class_names = element_obj.attrib["class"]
    except:
        return False, ""
    #- Class value must be Alphabets
    class_list = [class_vlaue for class_vlaue in class_names.split(" ")\
                  if class_vlaue not in self.ignore_values and bool(self.id_validation.match(class_vlaue))]
    #- Return True if have class list.
    if len(class_list) != 0:
        return True, class_list
    return False, ""
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56