inji.tests._is_prime()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 4
nop 1
crap 4
1
#!/usr/bin/env python3
2
3
# -*- coding: utf-8 -*-
4
5
# Hold custom jinja2 tests
6
# https://jinja.palletsprojects.com/en/2.11.x/api/#custom-tests
7
# https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-tests
8
9 1
from jinja2 import is_undefined
10 1
import re
11 1
import sys
12 1
import math
13 1
import os
14
15 1
def _is_prime(n):
16 1
  if n == 2:
17 1
    return True
18 1
  for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
19 1
    if n % i == 0:
20 1
      return False
21 1
  return True
22
23 1
tests = dict(
24
25
  is_prime = ( """ Tests if a number is prime """,
26
    lambda v: _is_prime(v)
27
  ),
28
29
)
30
31 1
if 'USE_ANSIBLE_SUPPORT' in os.environ.keys():
32 1
  from .ansible import TestModule
33 1
  tests.update(TestModule().tests())
34
35 1
for k,v in tests.items():
36
  setattr(sys.modules[__name__], k, v[1])
37