@@ -20,8 +20,26 @@ Install ``versionedobj`` using pip:
20
20
21
21
pip install versionedobj
22
22
23
- Example-- VersionedObject as a configuration file
24
- -------------------------------------------------
23
+ Getting started
24
+ ---------------
25
+
26
+ Object definition
27
+ *****************
28
+
29
+ Define objects by creating a new class that inherits from ``VersionedObject ``,
30
+ and set class attributes to define your object attribubtes:
31
+
32
+ .. code :: python
33
+
34
+ from versionedobj import VersionedObjbect
35
+
36
+ class UserConfig (VersionedObject ):
37
+ version = " v1.0.0"
38
+ username = " john smith"
39
+ friend_list = [" user1" , " user2" , " user3" ]
40
+
41
+ You can also nest VersionedObjects by simply assigning another ``VersionedObject ``
42
+ class or instance object to a class attribute:
25
43
26
44
.. code :: python
27
45
@@ -39,132 +57,95 @@ Example-- VersionedObject as a configuration file
39
57
friend_list = [" user1" , " user2" , " user3" ]
40
58
display_config = DisplayConfig() # VersionedObjects can be nested
41
59
42
- # Create an instance of your object (instance attributes will match class attributes,
43
- # and the initial values will be whatever values you set on the class attributes)
44
- cfg = UserConfig()
60
+ # Nested VersionedObjects can be a class object, or an instance of the
61
+ # class, either way will behave the same
45
62
46
- # Change some values on the object instance
47
- cfg.display_config.volume = 1.0
48
- cfg.username = " jane doe"
63
+ # display_config = DisplayConfig
49
64
50
- # Save object instance to JSON file
51
- cfg.to_file( ' user_config.json ' , indent = 4 )
65
+ Creating object instances and accessing object attributes
66
+ *********************************************************
52
67
53
- # Load object instance from JSON file
54
- cfg.from_file(' user_config.json' )
68
+ The values you set on the class attributes of a ``VersionedObject `` serve as the default
69
+ values for that object. When you create an instance of your ``VersionedObject `` class,
70
+ instance attributes will automatically be created to match the class attributes, and
71
+ the values of the class attributes will be copied over to the instance attributes:
55
72
73
+ .. code :: python
56
74
57
- You can also save/load object data as a JSON string:
75
+ obj = UserConfig()
58
76
59
- .. code :: python
77
+ print (obj.friend_list)
78
+ # Output looks like this: ["user1", "user2", "user3"]
60
79
61
- >> > obj_as_json = cfg.to_json(indent = 4 ) # Serialize to JSON string
62
- >> > obj_as_json # Print JSON string
63
-
64
- {
65
- " version" : " v1.0.0" ,
66
- " username" : " jane doe" ,
67
- " friend_list" : [
68
- " user1" ,
69
- " user2" ,
70
- " user3"
71
- ],
72
- " display_config" : {
73
- " display_mode" : " windowed" ,
74
- " resolution" : " 1920x1080" ,
75
- " volume" : 1.0
76
- }
77
- }
78
-
79
- >> > cfg.from_json(obj_as_json) # Load from JSON string
80
+ print (obj.display_config.display_mode)
81
+ # Output looks like this: "windowed"
80
82
81
- Or, as a dict:
83
+ As well as regular dot notation, you can also treat an object instance like a dict,
84
+ and access individual attribubtes using their full dot name as the key:
82
85
83
86
.. code :: python
84
87
85
- >> > obj_as_dict = cfg.to_dict() # Serialize to dict
86
- >> > obj_as_dict # Print dict
88
+ print (obj[ ' friend_list ' ])
89
+ # Output looks like this: ["user1", "user2", "user3"]
87
90
88
- {' version' : ' 1.0.0' , ' username' : ' jane doe' , ' friend_list' : [' user1' , ' user2' , ' user3' ], ' display_config' : {' display_mode' : ' windowed' , ' resolution' : ' 1920x1080' , ' volume' : 1.0 }}
91
+ print (obj[' display_config.display_mode' ])
92
+ # Output looks like this: "windowed"
89
93
90
- >> > cfg.from_dict(obj_as_dict) # Load from dict
94
+ # Change the value of an instance attribute
95
+ obj[' display_config.display_mode' ] = " fullscreen"
91
96
92
- Accessing versioned object instance attributes
93
- ----------------------------------------------
97
+ print (obj[ ' display_config.display_mode ' ])
98
+ # Output looks like this: "fullscreen"
94
99
95
- When you create an instance of your VersionedObject class, the instance attributes
96
- will be automatically populated to match the class attributes you have created :
100
+ You can also use the `` object_attributes() `` method to iterate over all object attribute
101
+ names and values :
97
102
98
103
.. code :: python
99
104
100
- from versionedobj import VersionedObject
101
-
102
- class AccountInfo (VersionedObject ):
103
- user_name = " john"
104
- user_id = 11223344
105
-
106
- class Session (VersionedObject ):
107
- ip_addr = " 255.255.255.255"
108
- port = 22
109
- account_info = AccountInfo()
110
-
111
- session = Session()
112
-
113
- print (session.ip_addr)
114
- # "255.255.255.255"
115
-
116
- print (session.account_info.user_name)
117
- # "john"
105
+ for attr_name, attr_value in obj.object_attributes():
106
+ print (f " { attr_name} : { attr_value} " )
118
107
119
- session.account_info.user_name = " jane"
108
+ # Output looks like this:
109
+ #
110
+ # version: v1.0.0
111
+ # username: john smith
112
+ # friend_list: ["user1", "user2", "user3"]
113
+ # display_config.display_mode: windowed
114
+ # display_config.resolution: 1920x1080
115
+ # display_config.volume: 0.66
120
116
121
- print (session.account_info.user_name)
122
- # "jane"
117
+ Serializing and de-serializing
118
+ ******************************
123
119
124
- Alternatively, you can treat a VersionedObject instance as a dict, and access
125
- attributes by passing their full name as the key:
120
+ Use the ``to_file `` and ``from_file `` methods to serialize/deserialize data to/from a JSON file:
126
121
127
122
.. code :: python
128
123
129
- print (session[' account_info.user_name' ])
130
- # "jane"
131
-
132
- session[' account_info.user_name' ] = " jack"
133
-
134
- print (session[' account_info.user_name' ])
135
- # "jack"
124
+ # Save object instance to JSON file
125
+ obj.to_file(' user_config.json' , indent = 4 )
136
126
137
- Iterating over versioned object instance attributes
138
- ---------------------------------------------------
127
+ # Load object instance from JSON file
128
+ obj.from_file( ' user_config.json ' )
139
129
140
- If you want to enumerate all attribute names & values on a versioned object instance,
141
- you can use the ``object_attributes() `` method, which returns a generator for all instance
142
- attributes:
130
+ You can also save/load object data as a JSON string:
143
131
144
132
.. code :: python
145
133
146
- from versionedobj import VersionedObject
134
+ # Save object instance to JSON string
135
+ obj_as_json = obj.to_json(indent = 4 )
147
136
148
- class AccountInfo (VersionedObject ):
149
- user_name = " john"
150
- user_id = 11223344
137
+ # Load object instance from JSON string
138
+ obj.from_json(obj_as_json)
151
139
152
- class Session (VersionedObject ):
153
- ip_addr = " 255.255.255.255"
154
- port = 22
155
- account_info = AccountInfo()
140
+ Or, as a dict:
156
141
157
- session = Session()
142
+ .. code :: python
158
143
159
- for attr_name, attr_value in session.object_attributes():
160
- print ( f " { attr_name } : { attr_value } " )
144
+ # Save object instance to dict
145
+ obj_as_dict = obj.to_dict( )
161
146
162
- # Output looks like this:
163
- #
164
- # ip_addr: 255.255.255.255
165
- # port: 22
166
- # account_info.user_name: john
167
- # account_info.user_id: 11223344
147
+ # Load object instance from dict
148
+ obj.from_dict(obj_as_dict)
168
149
169
150
Filtering serialization/deserialization output
170
151
----------------------------------------------
0 commit comments