当前位置: 首页>>代码示例>>Python>>正文


Python Policy.add方法代码示例

本文整理汇总了Python中lsst.pex.policy.Policy.add方法的典型用法代码示例。如果您正苦于以下问题:Python Policy.add方法的具体用法?Python Policy.add怎么用?Python Policy.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lsst.pex.policy.Policy的用法示例。


在下文中一共展示了Policy.add方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BigBoolTestCase

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
class BigBoolTestCase(unittest.TestCase):

    def setUp(self):
        self.policy = Policy()

    def tearDown(self):
        del self.policy

    def testBigBoolArray(self):
        biglen = 1000
        self.policy.isBool("true")
        for i in range(biglen):
            self.policy.add("true", True)

        v = self.policy.getArray("true")
        self.assertEqual(len(v), biglen, "wrong big number of values in array")
        for i in range(biglen):
            self.assertTrue(v[i], "big array with bad value")

        del v
        self.assertTrue(True, "Blew up True")

        fd = open("/dev/null", "w")
        print("look: %s" % True, file=fd)
        fd.close()
开发者ID:HyperSuprime-Cam,项目名称:pex_policy,代码行数:27,代码来源:test_BigBool.py

示例2: testFromPolicy

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
    def testFromPolicy(self):
        p = Policy()
        p.set("name", "visit")
        idf = id.IntegerIDFilter.fromPolicy(p)
        self.testNoConstraints(idf)

        idf = id.IDFilter.fromPolicy(p)
        self.assert_(isinstance(idf, id.StringIDFilter))
        p.set("className", "String")
        idf = id.IDFilter.fromPolicy(p)
        self.assert_(isinstance(idf, id.StringIDFilter))
        self.testNoConstraints(idf)
        p.set("className", "StringIDFilter")
        idf = id.IDFilter.fromPolicy(p)
        self.assert_(isinstance(idf, id.StringIDFilter))
        self.testNoConstraints(idf)

        p.set("className", "lsst.ctrl.sched.joboffice.id.StringIDFilter")
        self.assertRaises(RuntimeError, id.IDFilter.fromPolicy, p)
        p.set("className", "String")
        
        p.set("value", "-8")
        p.add("value", "r")
        p.add("value", "6,0")
        idf = id.IDFilter.fromPolicy(p)
        self.assert_(isinstance(idf, id.StringIDFilter))
        self.testValues2(idf)
开发者ID:jonathansick-shadow,项目名称:ctrl_sched,代码行数:29,代码来源:testIDFilter.py

示例3: generateDict

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
    def generateDict(self, policy, dictionary, prefix=""):
        definitions = Policy()
        dictionary.set("definitions", definitions)
        names = policy.names(True)
        for name in names:
            values = policy.getArray(name)
            defin = Policy()
            definitions.set(name, defin)
            defin.set("type", policy.getTypeName(name))
            defin.set("minOccurs", 1)
            defin.set("maxOccurs", len(values))
            type = policy.getValueType(name)

            if type == Policy.POLICY:
                newPrefix = prefix + "." + name
                subdict = Policy()
                defin.set("dictionary", subdict)
                if len(values) > 1:
                    print("# Warning: only using first value of", newPrefix)
                self.generateDict(values[0], subdict, newPrefix)
            else:
                allowed = Policy()
                defin.set("allowed", allowed)
                orderable = type == Policy.INT or type == Policy.DOUBLE \
                    or type == Policy.STRING
                if orderable:
                    allowed.set("min", min(values))
                    allowed.set("max", max(values))
                for value in values:
                    defin.add("default", value)
                    allowed.add("value", value)
开发者ID:HyperSuprime-Cam,项目名称:pex_policy,代码行数:33,代码来源:prototype_dictionary.py

示例4: setUp

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
 def setUp(self):
     p = Policy()
     p.set("foo", "bar")
     p.set("count", 3)
     p.set("files", "goob")
     p.add("files", "gurn")
     impl = bb.PolicyBlackboardItem()
     impl._props = p
     self.bbi = bb.ImplBlackboardItem(impl)
         
     self.initCount = 3
开发者ID:jonathansick-shadow,项目名称:ctrl_sched,代码行数:13,代码来源:testBlackboardItem.py

示例5: testPolicySetget

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
    def testPolicySetget(self):
        p = Policy()
        self.assert_(not p.exists("foo"), "empty existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")

        self.assertRaises(NameNotFound, p.getTypeInfo, "foo")

        p.set("doall", "true")

        # non-existence tests on a non-empty policy
        self.failUnless(not p.exists("foo"),
                        "non-empty non-existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")
        self.failUnless(not p.isInt("foo"),
                        "non-empty non-existence type test failed")
        self.assertRaises(NameNotFound, p.getTypeInfo, "foo")

        # existence tests
        self.assert_(p.exists("doall"), "non-empty existence test failed")
        self.assertEquals(p.valueCount("doall"), 1,
                          "single valueCount test failed")

        self.assertRaises(Exception, p.getInt, "doall")
        self.assertRaises(Exception, p.getDoubleArray, "doall")

        self.assertEquals(p.get("doall"), "true",
                          "top-level getString failed")
        p.set("doall", "duh")
        self.assertEquals(p.get("doall"), "duh",
                          "top-level getString failed")
        
        # test array access
        ary = p.getArray("doall")
        self.assertEquals(len(ary), 1,
                          "scalar property has more than one value")


        self.assertEquals(ary[0], "duh", "scalar access via array failed")

        p.add("doall", "never")
        self.assertEquals(p.valueCount("doall"), 2,
                          "2-elem. valueCount test failed")
        self.assertEquals(p.get("doall"), "never", "top-level add failed")
        ary = p.getArray("doall")
        self.assertEquals(len(ary), 2,
                          "scalar property has wrong number of values")
        self.assertEquals(ary[0], "duh",
                          "scalar access via (2-el) array failed")
        self.assertEquals(ary[-1], "never",
                          "scalar access via (2-el) array failed")

        # test hierarchical access

        # list names

        # test types
        p.set("pint", 5)
        self.assertEquals(p.getInt("pint"), 5, "support for type int failed")
        self.assertEquals(type(p.get("pint")), type(5),
                          "auto-typing for int failed")
        p.set("pdbl", 5.1)
        self.assertAlmostEquals(p.getDouble("pdbl"), 5.1, 7, 
                                "support for type double failed")
        self.assertEquals(type(p.get("pdbl")), type(5.1),
                          "auto-typing for double failed")
        p.set("pbool", True)
        self.assert_(p.getBool("pbool"), "support for type bool failed")
        self.assertEquals(type(p.get("pbool")), type(True),
                          "auto-typing for bool failed")
        p.add("pbool", False)

        # test shallow & deep copies

        # test raise NameNotFound if not present
        try:
            p.get("nonexistent")
            self.fail() # should never reach here
        except Exception, e:
            self.assert_(isinstance(e, NameNotFound))
开发者ID:lsst-dm,项目名称:bp,代码行数:83,代码来源:Policy_1.py

示例6: testMergeDefaults

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
    def testMergeDefaults(self):
        # from a non-trivial dictionary
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.set("required", "foo")
        d = Dictionary(self.getTestDictionary("defaults_dictionary_good.paf"))
        d.loadPolicyFiles(self.getTestDictionary(), True)
        self.assert_(p.nameCount() == 2)
        p.mergeDefaults(d)
        self.assert_(p.valueCount("int_range_count") == 3)
        self.assert_(p.nameCount() == 7)

        # from a policy that's really a dictionary
        p = Policy()
        pd = Policy(self.getTestDictionary("defaults_dictionary_indirect.paf"))
        p.mergeDefaults(pd)
        self.assert_(p.getString("string_type") == "foo")
        self.assert_(p.getDictionary().isDictionary())
        
        # from a policy that's really a non-trivial dictionary
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.set("required", "foo")
        pd = Policy(self.getTestDictionary("defaults_dictionary_policy.paf"))
        pd.loadPolicyFiles(self.getTestDictionary(), True)
        self.assert_(p.nameCount() == 2)
        p.mergeDefaults(pd)
        self.assert_(p.valueCount("int_range_count") == 3)
        self.assert_(p.nameCount() == 5)

        # ensure post-load validation
        p.set("int_range_count", -5)
        self.assertValidationError(ValidationError.UNKNOWN_NAME,
                                   p.add, "unknown", 0)

        # test throwing validation
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        try:
            p.mergeDefaults(pd)
        except ValidationError as ve:
            self.assert_(ve.getErrors("required")
                         == ValidationError.MISSING_REQUIRED)

        # non-throwing validation
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        ve = ValidationError("Dictionary_1.py", 1, "testMergeDefaults")
        p.mergeDefaults(pd, False, ve.cpp)
        self.assert_(ve.getErrors("required") == ValidationError.MISSING_REQUIRED)
        self.assert_(ve.getParamCount() == 1)

        # non-retention
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.set("required", "foo")
        p.mergeDefaults(pd, False)
        # make sure validate() fails gracefully when no dictionary present
        self.assertRaiseLCE(DictionaryError, "No dictionary",
                            p.validate, "No dictionary assigned")
        p.add("unknown", 0) # would be rejected if dictionary was kept

        # deep merge from a Policy that's not a Dictionary
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf")))
        self.assert_(p.nameCount() == 3)
        self.assert_(p.getBool("bool_set_count") == True)
        self.assert_(p.getString("indirect.string_type") == "bar")

        # propagation of a Dictionary from one Policy to another via mergeDefaults
        d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf"))
        d.loadPolicyFiles(self.getTestDictionary())
        pEmpty = Policy()
        pEmpty.mergeDefaults(d)
        self.assert_(pEmpty.canValidate())
        pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        pPartial.mergeDefaults(pEmpty)
        self.assert_(pPartial.canValidate(), "Dictionary handed off via mergeDefaults.")
开发者ID:jonathansick-shadow,项目名称:pex_policy,代码行数:75,代码来源:Dictionary_1.py

示例7: Policy

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
        # non-throwing validation
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        ve = ValidationError("Dictionary_1.py", 1, "testMergeDefaults")
        p.mergeDefaults(pd, False, ve)
        self.assert_(ve.getErrors("required") == ValidationError.MISSING_REQUIRED)
        self.assert_(ve.getParamCount() == 1)

        # non-retention
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.set("required", "foo")
        p.mergeDefaults(pd, False)
        # make sure validate() fails gracefully when no dictionary present
        self.assertRaisesEx(DictionaryError, "No dictionary",
                            p.validate, "No dictionary assigned")
        p.add("unknown", 0) # would be rejected if dictionary was kept

        # deep merge from a Policy that's not a Dictionary
        p = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
        p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf")))
        self.assert_(p.nameCount() == 3)
        self.assert_(p.getBool("bool_set_count") == True)
        self.assert_(p.getString("indirect.string_type") == "bar")

        # propagation of a Dictionary from one Policy to another via mergeDefaults
        d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf"))
        d.loadPolicyFiles(self.getTestDictionary())
        pEmpty = Policy()
        pEmpty.mergeDefaults(d)
        self.assert_(pEmpty.canValidate())
        pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf"))
开发者ID:lsst-dm,项目名称:bp,代码行数:32,代码来源:Dictionary_1.py

示例8: PolicyBlackboardItem

# 需要导入模块: from lsst.pex.policy import Policy [as 别名]
# 或者: from lsst.pex.policy.Policy import add [as 别名]
class PolicyBlackboardItem(BlackboardItem):
    """
    An implementation of a BlackboardItem that stores properities via a
    policy
    """

    def __init__(self, policyfile=None):
        """
        create an item with the given properties.
        @param policyfile    A policy
        """
        # the properties attached to this items
        self._props = None
        if policyfile:
            self._props = Policy.createPolicy(policyfile)
        else:
            self._props = Policy()

    def getProperty(self, name, default=None):
        """
        return the value for a property
        @param name      the property name
        @parma default   the default value to return if the name is not set
        """
        if not self._props.exists(name):
            return default
        elif self._props.isArray(name):
            return self._props.getArray(name)
        else:
            return self._props.get(name)

    def hasProperty(self, name):
        """
        return True if the property with the given name is available
        """
        return self._props.exists(name)

    def __getitem__(self, name):
        if not self._props.exists(name):
            raise KeyError(name)
        return BlackboardItem.__getitem__(self, name)

    def _setProperty(self, name, val):
        # set a property value
        if isinstance(val, list):
            self._props.set(name, val.pop(0))
            for v in val:
                self._props.add(name, v)
        else:
            self._props.set(name, val)

    def getPropertyNames(self):
        """
        return the property names that make up this item
        """
        return self._props.names()

    def _copyFrom(self, item):
        for name in item.getPropertyNames():
            self._setProperty(name, item.getProperty(name))

    @staticmethod
    def createFormatter():
        return PolicyBlackboardItem._Fmtr()

    class _Fmtr(object):
        def write(self, filename, item):
            pol = None
            if isinstance(item, PolicyBlackboardItem):
                pol = item._props
            else:
                delegate = PolicyBlackboardItem()
                delegate._copyFrom(item)
                pol = delegate._props

            writer = PAFWriter(filename)
            try:
                writer.write(pol, True)
            finally:
                writer.close()

        def openItem(self, filename):
            out = PolicyBlackboardItem()
            out._props = Policy.createPolicy(filename)
            return out

        def filenameExt(self):
            """
            return the recommended extension for the format this writes out
            """
            return "paf"
开发者ID:jonathansick-shadow,项目名称:ctrl_sched,代码行数:93,代码来源:item.py


注:本文中的lsst.pex.policy.Policy.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。