Returns a Pattern from the given string or regular expression. Recently compiled patterns are kept in cache.
(pattern, *args, **kwargs)
| 866 | _cache = {} |
| 867 | _CACHE_SIZE = 100 # Number of dynamic Pattern objects to keep in cache. |
| 868 | def compile(pattern, *args, **kwargs): |
| 869 | """ Returns a Pattern from the given string or regular expression. |
| 870 | Recently compiled patterns are kept in cache. |
| 871 | """ |
| 872 | id, p = repr(pattern)+repr(args), None |
| 873 | if id in _cache: |
| 874 | return _cache[id] |
| 875 | if isinstance(pattern, basestring): |
| 876 | p = Pattern.fromstring(pattern, *args, **kwargs) |
| 877 | if isinstance(pattern, regexp): |
| 878 | p = Pattern([Constraint(words=[pattern], taxonomy=kwargs.get("taxonomy", TAXONOMY))], *args, **kwargs) |
| 879 | if len(_cache) > _CACHE_SIZE: |
| 880 | _cache.clear() |
| 881 | if isinstance(p, Pattern): |
| 882 | _cache[id] = p |
| 883 | return p |
| 884 | else: |
| 885 | raise TypeError, "can't compile '%s' object" % pattern.__class__.__name__ |
| 886 | |
| 887 | def match(pattern, sentence, *args, **kwargs): |
| 888 | """ Returns the first match found in the given sentence, or None. |
no test coverage detected
searching dependent graphs…