Category Archives: Other

Python Tutorial – Dictionaries

PythonIn my previous blog post I talked about basics of Python. Now it’s time to discuss about one of the native datatypes of Python progamming language: Dictionaries.

Python Dictionaries

Dictionary is like Hashtable┬áin Java: It’s constructed of keys and values that have one-to-one relationship. So, no duplicate keys and it’s unordered.

Here’s the simplest example of directory:

Looks familiar? Looks like JSON. You can nest same way than in JSON:

It’s not really JSON, but we can easily create one:

Previous example prints identically to standard print to dictionary:

{"username": "thatsme", "password": "letmein", "servers": {"host2": "192.168.1.67", "host1": "192.168.1.66"}}

You can access data from the dictionary:

This will print:

thatsme
192.168.1.66

Working with dictionaries

Assigning new value to a key is somewhat self explanatory:

Assigning new key-value pair is also easy:

Keys are case-sensitive. These are different entries:

Mixing datatypes is also allowed:

Deleting key from a dictionary and clearing it fully:

Get keys and values separately:

Conclusion

Python directories are very easy data structures to write and understand. This was just a small subset of methods and example usages of dictionaries. You can discover more by using autocomplete in your favourite IDE. Next blog post will introduce Lists.

Bacon.js – Bus

Bacon.jsBacon.js bus is an excellent way to publish and subscribe to event streams through one unified channel. Bus helps a developer to decouple application and makes code much cleaner.

Bacon.js Bus example

This example outputs status of each validator to html page. It shows simple example of publish/subscribe with Bacon.js.

First we need some scripts – JQuery, Bacon.js and Bacon.model.js and Bacon.query.js

This the actual Javascript code. I’m going to walk you through it.

First we define keys that are used to indicate what value is moving in the Bus. Publisher adds a key to message and subscriber listens using the key.

We’re initializing new Bacon.Bus() which is used as Bus. Were defining new dynamic property ofType for bus. The property is used to filter correct messages to who ever is listening.

Then we create form validators. Beginning is the same than in previous Bacon.js post. After that we publish name, age and button event stream values to the bus. A Message is delivered constantly while user is interacting.

After that we subscribe to name and age by using our keys NAME_MESSAGE and AGE_MESSAGE. They’re handed over as a parameter to our dynamic property ofType. We also use merge which allows a user to combine two streams into one. Then we just assing value to markup.

Final step is to subscribe to button validation which tells us if whole form is valid or not. This is also written to out the markup.

Here is static HTML that we’re using in this Bacon.js example.

Bacon Bus is very easy publish/subscribe mechanism. It can be used from full blown application bus to simple form messaging.