@@ -692,7 +692,51 @@ service = container.get_object("MovieLister")
692
692
]]> </programlisting >
693
693
694
694
</section >
695
-
695
+
696
+ <section id =" objects-config-xmlconfig-python-builtin-types-mappings" >
697
+ <title >XMLConfig and basic Python types</title >
698
+ </section >
699
+
700
+ <para >Objects of most commonnly used Python types
701
+ -
702
+ <classname >str</classname >,
703
+ <classname >unicode</classname >,
704
+ <classname >int</classname >,
705
+ <classname >long</classname >,
706
+ <classname >float</classname >,
707
+ <classname >decimal.Decimal</classname >,
708
+ <classname >bool</classname > and
709
+ <classname >complex</classname >
710
+ -
711
+ may be expressed in XMLConfig using a shorthand syntax which allows
712
+ for a following XMLConfig file:
713
+ </para >
714
+ <programlisting ><![CDATA[
715
+ <?xml version="1.0" encoding="UTF-8"?>
716
+ <objects xmlns="http://www.springframework.org/springpython/schema/objects/1.1"
717
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
718
+ xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects/1.1
719
+ http://springpython.webfactional.com/schema/context/spring-python-context-1.1.xsd">
720
+
721
+ <str id="MyString">My string</str>
722
+
723
+ <unicode id="MyUnicode">Zażółć gęślą jaźń</unicode>
724
+
725
+ <int id="MyInt">10</int>
726
+
727
+ <long id="MyLong">100000000000000000000000</long>
728
+
729
+ <float id="MyFloat">3.14</float>
730
+
731
+ <decimal id="MyDecimal">12.34</decimal>
732
+
733
+ <bool id="MyBool">False</bool>
734
+
735
+ <complex id="MyComplex">10+0j</complex>
736
+
737
+ </objects>
738
+ ]]> </programlisting >
739
+
696
740
</section >
697
741
698
742
<section id =" objects-config-yamlconfig" >
@@ -934,6 +978,186 @@ properties:
934
978
</note >
935
979
936
980
</section >
981
+
982
+ <section id =" objects-config-yamlconfig-python-builtin-types-mappings" >
983
+ <title >Support for Python builtin types and mappings of other types onto YAML syntax</title >
984
+ </section >
985
+ <para >
986
+ Objects of commonly used Python builtin types may be tersely expressed
987
+ in YamlConfig. Supported types are
988
+ <classname >str</classname >,
989
+ <classname >unicode</classname >,
990
+ <classname >int</classname >,
991
+ <classname >long</classname >,
992
+ <classname >float</classname >,
993
+ <classname >decimal.Decimal</classname >,
994
+ <classname >bool</classname >,
995
+ <classname >complex</classname >,
996
+ <classname >dict</classname >,
997
+ <classname >list</classname > and
998
+ <classname >tuple</classname >.
999
+ </para >
1000
+ <para >
1001
+ Here's a sample YamlConfig featuring their usage. Note that
1002
+ with the exception of <classname >decimal.Decimal</classname >,
1003
+ names of the YAML attributes are the same as the names of
1004
+ Python types.
1005
+ </para >
1006
+ <programlisting ><![CDATA[
1007
+ objects:
1008
+ - object: MyString
1009
+ str: My string
1010
+
1011
+ - object: MyUnicode
1012
+ unicode: Zażółć gęślą jaźń
1013
+
1014
+ - object: MyInt
1015
+ int: 10
1016
+
1017
+ - object: MyLong
1018
+ long: 100000000000000000000000
1019
+
1020
+ - object: MyFloat
1021
+ float: 3.14
1022
+
1023
+ - object: MyDecimal
1024
+ decimal: 12.34
1025
+
1026
+ - object: MyBoolean
1027
+ bool: False
1028
+
1029
+ - object: MyComplex
1030
+ complex: 10+0j
1031
+
1032
+ - object: MyList
1033
+ list: [1, 2, 3, 4]
1034
+
1035
+ - object: MyTuple
1036
+ tuple: ["a", "b", "c"]
1037
+
1038
+ - object: MyDict
1039
+ dict:
1040
+ 1: "a"
1041
+ 2: "b"
1042
+ 3: "c"
1043
+
1044
+ - object: MyRef
1045
+ decimal:
1046
+ ref: MyDecimal
1047
+ ]]> </programlisting >
1048
+ <para >
1049
+ Under the hood, while parsing the YAML files, Spring Python
1050
+ will translate the definitions such as the one above into
1051
+ the following one:
1052
+ </para >
1053
+ <programlisting ><![CDATA[
1054
+ objects:
1055
+ - object: MyString
1056
+ class: types.StringType
1057
+ constructor-args: ["My string"]
1058
+
1059
+ - object: MyUnicode
1060
+ class: types.UnicodeType
1061
+ constructor-args: ["Zażółć gęślą jaźń"]
1062
+
1063
+ - object: MyInt
1064
+ class: types.IntType
1065
+ constructor-args: [10]
1066
+
1067
+ - object: MyLong
1068
+ class: types.LongType
1069
+ constructor-args: [100000000000000000000000]
1070
+
1071
+ - object: MyFloat
1072
+ class: types.FloatType
1073
+ constructor-args: [3.14]
1074
+
1075
+ - object: MyDecimal
1076
+ class: decimal.Decimal
1077
+ constructor-args: ["12.34"]
1078
+
1079
+ - object: MyBoolean
1080
+ class: types.BooleanType
1081
+ constructor-args: [False]
1082
+
1083
+ - object: MyComplex
1084
+ class: types.ComplexType
1085
+ constructor-args: [10+0j]
1086
+
1087
+ - object: MyList
1088
+ class: types.ListType
1089
+ constructor-args: [[1,2,3,4]]
1090
+
1091
+ - object: MyTuple
1092
+ class: types.TupleType
1093
+ constructor-args: [["a", "b", "c"]]
1094
+
1095
+ - object: MyDict
1096
+ class: types.DictType
1097
+ constructor-args: [{1: "a", 2: "b", 3: "c"}]
1098
+
1099
+ - object: MyRef
1100
+ class: decimal.Decimal
1101
+ constructor-args: [{ref: MyDecimal}]
1102
+ ]]> </programlisting >
1103
+
1104
+ <para >
1105
+ Configuration of how YAML elements are mapped onto Python types
1106
+ is stored in the
1107
+ <classname >springpython.config.yaml_mappings</classname >
1108
+ dictionary which can be easily customized to fulfill one's needs.
1109
+ The dictionary's keys are names of the YAML elements and its
1110
+ values are the coresponding Python types, written as strings
1111
+ in the form of <classname >"package_name.module_name.class_name"</classname >
1112
+ - note that the <classname >"package_name.module_name."</classname > part is
1113
+ required, it needs to be a fully qualified name.
1114
+ </para >
1115
+ <para >
1116
+ Let's assume that in your configuration you're frequently creating
1117
+ objects of type
1118
+ <classname >interest_rate.InterestRateFrequency</classname >,
1119
+ here's how you can save yourself a lot of typing by customizing
1120
+ the mappings dictionary. First, on Python side, create an
1121
+ <classname >InterestRate</classname > class, such as:
1122
+ <programlisting ><![CDATA[
1123
+ class InterestRate(object):
1124
+ def __init__(self, value=None):
1125
+ self.value = value
1126
+ ]]> </programlisting >
1127
+
1128
+ <para >
1129
+ .. which will allow you to create such a YAML context
1130
+ </para >
1131
+
1132
+ <programlisting ><![CDATA[
1133
+ objects:
1134
+ - object: base_interest_rate
1135
+ interest_rate: "7.35"
1136
+ ]]> </programlisting >
1137
+
1138
+ <para >
1139
+ .. then, before creating the context, update the
1140
+ mappings dictionary as needed and next you'll be able to
1141
+ access the <classname >base_interest_rate</classname >
1142
+ object as if it had been defined using the standard syntax:
1143
+ </para >
1144
+
1145
+ <programlisting ><![CDATA[
1146
+ from springpython.context import ApplicationContext
1147
+ from springpython.config import YamlConfig, yaml_mappings
1148
+
1149
+ yaml_mappings.update({"interest_rate": "interest_rate.InterestRate"})
1150
+
1151
+ # .. create the context now
1152
+ container = ApplicationContext(YamlConfig("./app-ctx.yaml"))
1153
+
1154
+ # .. fetch the object
1155
+ base_interest_rate = container.get_object("base_interest_rate")
1156
+
1157
+ # .. will show "7.35", as defined in the "./app-ctx.yaml" config
1158
+ print base_interest_rate.value
1159
+ ]]> </programlisting >
1160
+ </para >
937
1161
938
1162
<section id =" objects-config-yamlconfig-constructors" >
939
1163
<title >Constructors</title >
@@ -974,7 +1198,7 @@ constructor-args:
974
1198
<para >It is also valuable to know that you can mix this up and use both.</para >
975
1199
976
1200
</section >
977
-
1201
+
978
1202
</section >
979
1203
980
1204
<section id =" objects-config-object" >
0 commit comments