pattern matching in Python, PEAK.rules rules!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import string
from peak.rules import when
from django.db import models
class MyModelManager(models.Manager):
def starts_with(self, letter):
return self.filter(name__istartswith=letter)
@when(starts_with, 'letter in string.digits')
def starts_with_digits(self, letter):
return self.filter(name__regex='^\d')
@when(starts_with, 'letter == "~"')
def starts_with_special(self, letter):
return self.filter(name__iregex='^[^\w\d]')