Beispiel #1
0
 def post(self, request, unit_system, *args, **kwargs):
     """
     Validate a formula with parameters
     :param request: HTTP request
     :param unit_system: Unit system to use for validation
     """
     if request.user and request.user.is_authenticated:
         us = UnitSystem(unit_system,
                         user=request.user,
                         key=request.data.get('key', None))
     else:
         us = UnitSystem(unit_system)
     exp = ExpressionSerializer(data=request.data)
     try:
         if exp.is_valid(unit_system=us, dimensions_only=True):
             expression = exp.create(exp.validated_data)
             return Response(DimensionalitySerializer(
                 expression.dimensionality(unit_system=us), many=True).data,
                             content_type="application/json")
         else:
             return Response(json.dumps(exp.errors),
                             status=status.HTTP_406_NOT_ACCEPTABLE,
                             content_type="application/json")
     except (UndefinedUnitError, DimensionalityError, TokenError) as e:
         return Response(str(e),
                         status=status.HTTP_400_BAD_REQUEST,
                         content_type="application/json")
Beispiel #2
0
 def validate_base_system(value: str):
     """
     Validate unit system
     :param value: unit system name
     """
     from djangophysics.units.models import UnitSystem
     if not UnitSystem.is_valid(value):
         raise serializers.ValidationError('Invalid unit system')
     return value
Beispiel #3
0
 def setUp(self) -> None:
     """
     Setup environment
     """
     self.calculator = ExpressionCalculator(unit_system='SI')
     self.unit_system = UnitSystem(system_name='SI')
     self.expressions = [{
         'expression':
         "3*{a}+15*{b}",
         'operands': [{
             "name": "a",
             "value": 0.1,
             "unit": "kg"
         }, {
             "name": "b",
             "value": 15,
             "unit": "g"
         }]
     }, {
         'expression':
         "3*{a}+15*{b}+1000*{c}",
         'operands': [
             {
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             },
             {
                 "name": "b",
                 "value": 150,
                 "unit": "g"
             },
             {
                 "name": "c",
                 "value": 250,
                 "unit": "mg"
             },
         ]
     }]
     self.expression_to_unit = [{
         'expression':
         "3*{a}+15*{b}",
         'operands': [{
             "name": "a",
             "value": 0.1,
             "unit": "kg"
         }, {
             "name": "b",
             "value": 15,
             "unit": "g"
         }],
         'out_units':
         'ton'
     }, {
         'expression':
         "3*{a}+15*{b}+1000*{c}",
         'operands': [
             {
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             },
             {
                 "name": "b",
                 "value": 150,
                 "unit": "g"
             },
             {
                 "name": "c",
                 "value": 250,
                 "unit": "mg"
             },
         ],
         'out_units':
         'hundredweight'
     }]
Beispiel #4
0
 def setUp(self) -> None:
     """
     Setup environment
     """
     self.calculator = ExpressionCalculator(unit_system='SI')
     self.unit_system = UnitSystem(system_name='SI')
     self.exp_id = str(uuid.uuid4())
     self.today_id = str(uuid.uuid4())
     self.last_week_id = str(uuid.uuid4())
     self.expressions = [
         {
             'exp_id':
             self.exp_id,
             'expression':
             "3*{a}+15*{b}",
             'operands': [{
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             }, {
                 "name": "b",
                 "value": 15,
                 "unit": "g"
             }]
         },
         {
             'expression':
             "3*{a}+15*{b}+1000*{c}",
             'operands': [
                 {
                     "name": "a",
                     "value": 0.1,
                     "unit": "kg"
                 },
                 {
                     "name": "b",
                     "value": 150,
                     "unit": "g"
                 },
                 {
                     "name": "c",
                     "value": 250,
                     "unit": "mg"
                 },
             ]
         },
         {
             'exp_id': self.today_id,
             'expression': "{a}",
             'operands': [{
                 "name": "a",
                 "value": 1,
                 "unit": "USD"
             }],
             'out_units': 'EUR'
         },
         {
             'exp_id': self.last_week_id,
             'expression': "{a}",
             'value_date': date.today() - timedelta(7),
             'operands': [{
                 "name": "a",
                 "value": 1,
                 "unit": "USD"
             }],
             'out_units': 'EUR'
         },
     ]
     self.expression_to_unit = [{
         'expression':
         "3*{a}+15*{b}",
         'operands': [{
             "name": "a",
             "value": 200,
             "unit": "kg"
         }, {
             "name": "b",
             "value": 150000,
             "unit": "g"
         }],
         'out_units':
         'pound'
     }, {
         'expression':
         "3*{a}+15*{b}+1000*{c}",
         'operands': [
             {
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             },
             {
                 "name": "b",
                 "value": 75,
                 "unit": "g"
             },
             {
                 "name": "c",
                 "value": 125,
                 "unit": "mg"
             },
         ],
         'out_units':
         'milligram'
     }]
     self.trash_expressions = [{
         'exp_id':
         self.exp_id,
         'expression':
         "3*{a}+15*{b}+",
         'operands': [{
             "name": "a",
             "value": 0.1,
             "unit": "kg"
         }, {
             "name": "b",
             "value": 15,
             "unit": "g"
         }]
     }, {
         'expression':
         "3*{a}+15*{b}+1000*{c}",
         'operands': [
             {
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             },
             {
                 "name": "b",
                 "value": 150,
                 "unit": "g"
             },
             {
                 "name": "c",
                 "value": 250,
                 "unit": "s"
             },
         ]
     }, {
         'expression':
         "3*{a}+15*{b}+1000*{c}",
         'operands': [
             {
                 "name": "a",
                 "value": 0.1,
                 "unit": "kg"
             },
             {
                 "name": "b",
                 "value": 75,
                 "unit": "g"
             },
             {
                 "name": "c",
                 "value": 125,
                 "unit": "mg"
             },
         ],
         'out_units':
         'L'
     }]
Beispiel #5
0
 def setUp(self):
     """
     Setup test environment
     """
     self.us = UnitSystem(system_name='SI')
Beispiel #6
0
 def setUp(self):
     """
     Setup unit system
     """
     self.us = UnitSystem(system_name='SI')