def cregex(input_, doquote=True): """ Returns a regular expression safe for use in SQL. If None is passed if will raise an exception as None is not a valid regular expression. The intented use is with regex based SQL expressions. @param input_: Value to evaluate @type input_: str @param doquote: I{OPTIONAL}: Wrapped in single quotes, defaults to B{True} @type doquote: bool @return: str """ if data.isregex(input_): if doquote: return data.wrap(input_, "'") else: return input_ else: raise error.TypeConversionError(input_, 'sql regex')
def cregex(string, doquote=True): """ Returns a regular expression safe for use in SQL. If :class:`None` is passed if will raise an exception as None is not a valid regular expression. The intented use is with regex based SQL expressions. :param string: Value to evaluate :type string: :class:`str` :param doquote: optionally wrap in single quotes, default is :class:`True` :type doquote: :class:`bool` :rtype: :class:`str` """ if data.isregex(string): if doquote: return data.wrap(string, "'") else: return string else: raise error.TypeConversionError(string, 'sql regex')
def test_isregex(self): self.assertEqual(data.isregex(r"abc[a-z]"), True) self.assertEqual(data.isregex(r"("), False)