Skip to main content

Pattern matching as a function.

Project description

match_func

Pattern matching as a function.

from match_func import match

# Use ranges!
age = 20
match(age,
      {range(0, 3):   'Toddler',
       range(3, 14):  'Child',
       range(14, 18): 'Teen',
       range(18, 65): 'Adult'},
      default =       'Elder')
>> 'Adult'

# Use tuples!
name = 'Clara'
match(name,
      {('Laura', 'John', 'Luke'): 'First class',
       ('Clara', 'Nathan'):       'Business',
       ('Jason', 'Mary'):         'Economy'},
      default =                   'Non customer')
>> 'Business'

# Use conditions!
value = 5
match(value,
      {value < 10:        'Small',
       10 <= value <= 20: 'Medium',
       value > 20:        'Large'})
>> 'Small'

# Use types!
something = 'Hello'
match(something,
      {int:     'Its an integer',
       str:     'Its a string'},
      default = 'Its something else')
>> 'Its a string'

# Mix and match!
earnings = 10_000
match(earnings,
      {str:              'Invalid type: str',
       range(0, 5_000):  'Too low',
       5_000:            'Exactly 5k',
       earnings > 5_000: 'Over the top!'},
      default =         f'Invalid value: {earnings}')
>> 'Over the top!'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

match_func-1.0.0.tar.gz (9.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page