Adds JSon structure in README

Explains expected structure of the JSon messages.
Changes default message values to None

I will probably have to provide a imple way to create JSon elements.
This commit is contained in:
2012-10-25 18:18:48 +02:00
parent 051360345a
commit 328e1c1b29
4 changed files with 45 additions and 4 deletions

View File

@@ -1 +1 @@
}q(U collectorqUcoverage v3.5.3qUlinesq}q(U.c:\users\jll\perso\github\observer\observer.pyq]q(KKKK!K&K'K)K*K,K1K2K3K6K:K=KDKFKLKMKOKTKUKWKXKYK[K_K`KbKcKdKfKjKlKnKsKtKvKweU2c:\users\jll\perso\github\observer\testobserver.pyq]q (KKKKK K!K%K'K)K*K+K,K-K/K3K5K:K;K<K?K@KAKBKCKDKFKGKHKJKKKLKNKOKPKRKSKTKVKXKYKZK[K\K]K_K`KbKcKdKfKgKhKiKlKmKnKoKpKseuu. }q(U collectorqUcoverage v3.5.3qUlinesq}q(U2c:\users\jll\perso\github\observer\testobserver.pyq]q(KKKKK K!K%K'K)K*K+K,K-K/K3K5K:K;K<K?K@KAKBKCKDKFKGKHKJKKKLKNKOKPKRKSKTKVKXKYKZK[K\K]K_K`KbKcKdKfKgKhKiKlKmKnKoKpKseU.c:\users\jll\perso\github\observer\observer.pyq]q (KKKK!K&K'K)K*K,K1K2K3K6K:K=KDKFKLKMKOKTKUKWKXKYK[K_K`KbKcKdKfKjKlKnKsKtKvKweuu.

View File

@@ -39,7 +39,7 @@ class Observer():
raise TypeError("Expected string for name") raise TypeError("Expected string for name")
self.name = name self.name = name
self.message = 'message' self.message = None
def update(self, message): def update(self, message):
""" """
@@ -73,7 +73,7 @@ class Observable():
No subscriber is registered yet. No subscriber is registered yet.
Message is set to 'message by default' Message is set to 'message by default'
""" """
self.message = 'message' self.message = None
self.obs_collection = [] self.obs_collection = []
def subscribe(self, observer): def subscribe(self, observer):

View File

@@ -18,6 +18,47 @@ The notifications are sent in json format.
An Observable is an entity holding information of interest for other entities. An Observable is an entity holding information of interest for other entities.
Observers subscribe or unsubscribe to an Observable to get notified any time message is updated. Observers subscribe or unsubscribe to an Observable to get notified any time message is updated.
### JSon Structure:
As already explained, the messages sent from the observable to the observers are formatted in JSon.
This allows a simple yet efficient control of the data sent and received while staying in line with the standards.
Each transmitted package should be divided into 4 sections:
- name : The name of the Observer to send the data to. As each Observer is uniquely defined by its name, filling this section means the data will be sent to **at most** one Observer.
- group : The group of Observers to send the data to. Each Observer may belong to a group. All Observers in the group will then be notified.
- type : the type of message contained in data. This section is not processed by the Observable and is sent as is. It may be blank, or contain anything.
- data : The actual data to be transmitted to the Observers
Here is are several examples of valid JSon message :
#### Data will be sent to Bob only. No particular message type
{
"name": 'Bob',
"group": '',
"type": '',
"data": 42
}
#### Data will be sent to all Observers in the GUI group. We may assess that this is an update :).
{
"name": '',
"group": 'GUI',
"type": 'Update',
"data": 'coffee'
}
#### No group, neither name specified. All subscribers will be notified. the message is an array of value object.
{
"name": '',
"group": '',
"type": '',
"data": ["baz", null, 1.0, 2]
}
According to the [JSon specifications](http://json.org/),
_A value can be a string in double quotes, or a number, or true or false or null, or an object or an array._
## Description of the package ## Description of the package
The Observer pattern does not need any external module to work. Any simple Python installation (>= 2.6) should be enough. The Observer pattern does not need any external module to work. Any simple Python installation (>= 2.6) should be enough.

View File

@@ -60,7 +60,7 @@ class TestObserver(unittest.TestCase):
self.assertRaises(TypeError, lambda: obs.Observer(42)) self.assertRaises(TypeError, lambda: obs.Observer(42))
# tests message and update # tests message and update
self.assertEquals(self.myObserver1.message, self.default_mess) self.assertEquals(self.myObserver1.message, None)
new_message = "new_message" new_message = "new_message"
self.myObserver1.update(new_message) self.myObserver1.update(new_message)
self.assertEquals(self.myObserver1.message, new_message) self.assertEquals(self.myObserver1.message, new_message)