objects.py revision 12339cdd90b2c98eb4f7adf794ddca8de53992b8
1# Copyright 2013 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os 16import os.path 17import sys 18import re 19import json 20import tempfile 21import time 22import unittest 23import subprocess 24 25def int_to_rational(i): 26 """Function to convert Python integers to Camera2 rationals. 27 28 Args: 29 i: Python integer or list of integers. 30 31 Returns: 32 Python dictionary or list of dictionary representing the given int(s) 33 as rationals with denominator=1. 34 """ 35 if isinstance(i, list): 36 return [{"numerator":val, "denominator":1} for val in i] 37 else: 38 return {"numerator":i, "denominator":1} 39 40def capture_request(obj): 41 """Function to wrap an object inside a captureRequest object. 42 43 Args: 44 obj: The Python dictionary object to wrap. 45 46 Returns: 47 The dictionary: {"captureRequest": obj} 48 """ 49 return {"captureRequest": obj} 50 51def capture_request_list(obj_list): 52 """Function to wrap an object list inside a captureRequestList object. 53 54 Args: 55 obj_list: The list of Python dictionary objects to wrap. 56 57 Returns: 58 The dictionary: {"captureRequestList": obj_list} 59 """ 60 return {"captureRequestList": obj_list} 61 62class __UnitTest(unittest.TestCase): 63 """Run a suite of unit tests on this module. 64 """ 65 66 # TODO: Add more unit tests. 67 68 def test_int_to_rational(self): 69 """Unit test for int_to_rational. 70 """ 71 self.assertEqual(int_to_rational(10), 72 {"numerator":10,"denominator":1}) 73 self.assertEqual(int_to_rational([1,2]), 74 [{"numerator":1,"denominator":1}, 75 {"numerator":2,"denominator":1}]) 76 77if __name__ == '__main__': 78 unittest.main() 79 80