diff options
Diffstat (limited to 'python/examples')
| -rw-r--r-- | python/examples/console.py | 38 | ||||
| -rw-r--r-- | python/examples/here-now.py | 3 | ||||
| -rw-r--r-- | python/examples/history.py | 5 | ||||
| -rw-r--r-- | python/examples/publish.py | 7 | ||||
| -rw-r--r-- | python/examples/subscribe.py | 11 | 
5 files changed, 35 insertions, 29 deletions
| diff --git a/python/examples/console.py b/python/examples/console.py index 084c49a..342c70f 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -37,12 +37,16 @@ color = Cmd()  stop = None +full_date = False +  def stop_2(th):      th._Thread__stop()  def stop_3(th):      th._stop() + +  def print_console_2(of,message):      print >>of, message  @@ -63,11 +67,20 @@ else:          print_console = print_console_3          stop = stop_3 + +def get_date(): +    if full_date is True: +        return color.colorize(datetime.now().strftime( +            '%Y-%m-%d %H:%M:%S'), "magenta") +    else:  +        return color.colorize(datetime.now().strftime( +            '%H:%M:%S'), "magenta")   + +  def print_ok(msg, channel=None):      if msg is None:          return -    chstr = color.colorize("[" + datetime.now().strftime( -        '%Y-%m-%d %H:%M:%S') + "] ","magenta") +    chstr = color.colorize("[" + get_date() + "] ","magenta")      chstr += color.colorize("[Channel : " + channel + \          "] " if channel is not None else "", "cyan")      try: @@ -79,8 +92,7 @@ def print_ok(msg, channel=None):  def print_error(msg, channel=None):      if msg is None:          return -    chstr = color.colorize("[" + datetime.now().strftime( -        '%Y-%m-%d %H:%M:%S') + "] ", "magenta") +    chstr = color.colorize("[" + get_date() + "] ", "magenta")      chstr += color.colorize("[Channel : " + channel + \          "] " if channel is not None else "", "cyan")      try: @@ -237,15 +249,6 @@ def kill_all_threads():              stop(thread) -def get_date(full=False): -    if full is True: -        return color.colorize("[" + datetime.now().strftime( -            '%Y-%m-%d %H:%M:%S') + "] ", "magenta") -    else:  -        return color.colorize("[" + datetime.now().strftime( -            '%H:%M:%S') + "] ", "magenta")   - -  class DevConsole(Cmd):      def __init__(self): @@ -255,7 +258,6 @@ class DevConsole(Cmd):          self.default_channel = None          self.async = False          pubnub = Pubnub("demo", "demo") -        self.full_date = False          self.channel_truncation = 3          self.prompt = self.get_prompt() @@ -279,7 +281,7 @@ class DevConsole(Cmd):      def get_prompt(self): -        prompt = get_date(self.full_date) +        prompt = "[" + get_date() + "]"          if self.default_channel is not None and len(self.default_channel) > 0:              prompt += " [default channel: " + color.colorize(self.default_channel,"bold") + "]" @@ -352,15 +354,17 @@ class DevConsole(Cmd):          self.prompt = self.get_prompt()      def do_set_full_date(self, command): +        global full_date          """do_set_full_date          Set Full Date""" -        self.full_date = True +        full_date = True          self.prompt = self.get_prompt()      def do_unset_full_date(self, command): +        global full_date          """do_unset_full_date          Unset Full Date""" -        self.full_date = False +        full_date = False          self.prompt = self.get_prompt()      @options([make_option('-c', '--channel', action="store", help="Default Channel") diff --git a/python/examples/here-now.py b/python/examples/here-now.py index f573389..d03a110 100644 --- a/python/examples/here-now.py +++ b/python/examples/here-now.py @@ -23,13 +23,12 @@ pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,  channel = 'hello_world' -  # Synchronous usage -  print pubnub.here_now(channel)  # Asynchronous usage +  def callback(message):      print(message) diff --git a/python/examples/history.py b/python/examples/history.py index 3d6addf..7f7466b 100644 --- a/python/examples/history.py +++ b/python/examples/history.py @@ -20,7 +20,7 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  ## -----------------------------------------------------------------------  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,                  secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) -channel  = 'a' +channel = 'a'  # Synchronous usage @@ -28,7 +28,8 @@ print pubnub.history(channel, count=2)  # Asynchronous usage +  def callback(message):      print(message) -pubnub.history(channel, count=2 , callback=callback, error=callback) +pubnub.history(channel, count=2, callback=callback, error=callback) diff --git a/python/examples/publish.py b/python/examples/publish.py index 029c17a..a1b913b 100644 --- a/python/examples/publish.py +++ b/python/examples/publish.py @@ -20,17 +20,16 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  ## -----------------------------------------------------------------------  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,                  secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) -channel  = 'hello_world' -message  = 'Hello World !!!' - +channel = 'hello_world' +message = 'Hello World !!!'  # Synchronous usage -  print pubnub.publish(channel, message)  # Asynchronous usage +  def callback(message):      print(message) diff --git a/python/examples/subscribe.py b/python/examples/subscribe.py index a410297..6cf93b1 100644 --- a/python/examples/subscribe.py +++ b/python/examples/subscribe.py @@ -21,26 +21,29 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,                  secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) -channel  = 'a' - +channel = 'a'  # Asynchronous usage -  def callback(message, channel):      print(message) +  def error(message):      print("ERROR : " + str(message)) +  def connect(message):      print("CONNECTED") +  def reconnect(message):      print("RECONNECTED") +  def disconnect(message):      print("DISCONNECTED") -pubnub.subscribe(channel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect) +pubnub.subscribe(channel, callback=callback, error=callback, +                 connect=connect, reconnect=reconnect, disconnect=disconnect) | 
