OLD | NEW |
(Empty) | |
| 1 from recipe_engine.recipe_api import Property |
| 2 from recipe_engine import config |
| 3 import math |
| 4 |
| 5 # This recipe computes all the primes up to the value up_to. |
| 6 |
| 7 DEPS = [ |
| 8 'recipe_engine/properties', |
| 9 ] |
| 10 |
| 11 PROPERTIES = { |
| 12 'up_to': Property(kind=int, default=50), |
| 13 } |
| 14 |
| 15 RETURN_SCHEMA = config.ReturnSchema( |
| 16 result=config.List(int) |
| 17 ) |
| 18 |
| 19 def is_prime(val): |
| 20 if val % 2 == 0: |
| 21 return False |
| 22 for div in range(3, math.ceil(math.sqrt(val))): |
| 23 if val % div == 0: |
| 24 return False |
| 25 return True |
| 26 |
| 27 def RunSteps(api, up_to): |
| 28 all_primes = [] |
| 29 prime = 2 |
| 30 while prime < up_to: |
| 31 all_primes.append(prime) |
| 32 res = api.depend_on('next_prime', {'curr_prime': prime}) |
| 33 prime = res.result |
| 34 if not is_prime(prime): |
| 35 raise api.StepFailure("OH NOES") |
| 36 |
| 37 return RETURN_SCHEMA(result=all_primes) |
| 38 |
| 39 def GenTests(api): |
| 40 yield ( |
| 41 api.test('basic') + |
| 42 api.properties(up_to=3) + |
| 43 api.depend_on('next_prime', {'result': 3}) |
| 44 ) |
| 45 |
| 46 yield ( |
| 47 api.test('failure') + |
| 48 api.properties(up_to=10) + |
| 49 api.depend_on('next_prime', {'result': 4}) |
| 50 ) |
OLD | NEW |