pubnub IoT Cloud – What is a channel and how to create?

Channels are how messages are sent and received. Clients that subscribe to a channel will receive messages that are published on that channel. Channels are very lightweight and flexible. Channels exist merely by using them. Just publish or subscribe to a channel name and nothing more. And, use as many channels as your app requires. There is no additional charge for the number of channels you use. But, you should use them in a way that makes the most sense for your use case.  And a channel is just a channel. What you use it for is completely up to your application’s requirements. For example, a channel might be used for 1 to 1 private chat, many to many group chat, 1 to many broadcasting, or many to 1 fan-in. And a channel that is being used for private chat can suddenly be used for group chat. It is just up to you to allow more users to start subscribing and publishing to that channel. So channels are just blank communication devices for which you determine the actual usage

There is no UI to "create" a channel. Channels are just strings.

# Code to create a new channel

// Subscribe to a channel

 pubnub.subscribe({
    channel: 'my_channel', message: function(m){console.log(m)} });

 

Advertisements

How do I access environment variables from Python?

Environment variables are accessed through os.environ

import os
print(os.environ['HOME'])

Or you can see a list of all the environment variables using:

os.environ

As sometimes you might need to see a complete list!

# using get will return `None` if a key is not present rather than raise a `KeyError`
print(os.environ.get('KEY_THAT_MIGHT_EXIST'))

# os.getenv is equivalent, and can also give a default value instead of `None`
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))

Python default installation on Windows is C:\Python. If you want to find out the running python on your system, try the below:

import sys
print(sys.prefix)

What is python-dev package used for?

python-dev contains the header files you need to build Python extensions. lxml is a Python C-API extension that is compiled when you do pip install lxml. The lxml sources have at least something like #include  in the code.  The compiler looks for the Python.h file during compilation, hence those files need to be on your system such that they can be found. On Linux typically, the binary libraries and header files of packages like python are separate. Hence you can have Python installed and everything works fine but when you want to build extensions you need to install the corresponding dev package.

python-dev is the package that contains the header files for the Python C API, which is used by lxml because it includes Python C extensions for high performance.

Github package link with installation steps:  https://gist.github.com/dwayne/e77f267d871244471e3b#file-install-md

Python callback function – quick understanding with simple example

A classic use of callbacks in Python (and other languages) is to assign events to UI elements.

Here is a very trivial example of the use of a callback in Python. First define two functions, the callback and the calling code, then pass the callback function into the calling code.

>>> def get_square(val):
...     """ the callback """
...     return val ** 2
...
>>> def caller(func, val):
...     return func(val)
...
>>> caller(get_square, 5)
25