示例#1
0
    def _handle_timeline_notification(self, data):
        """Handle timeline notification.

    This method handles when a user chooses to perform a custom menu optio
    (increment, decrement, reset).
    """
        # TODO: Race conditions occur if a user rapidly selects a menu item many
        #       times (concurrently reading and writing the same timeline item).
        #       Resolve this issue
        for user_action in data.get('userActions', []):
            logging.info(user_action)
            option = user_action.get('payload')
            if user_action.get(
                    'type') == 'CUSTOM' and option in PROCESS_OPTIONS:
                data = json.loads(self.request.body)
                item = self.mirror_service.timeline().get(
                    id=data['itemId']).execute()
                num = util.get_num(custom_item_fields.get(item, 'num'))
                # possible actions are functions listed in PROCESS_OPTIONS
                custom_item_fields.set(item, 'num',
                                       PROCESS_OPTIONS[option](num),
                                       TIMELINE_ITEM_TEMPLATE_URL)
                if 'notification' in item:
                    item.pop('notification')
                self.mirror_service.timeline().update(id=data['itemId'],
                                                      body=item).execute()
                # Only handle the first successful action.
                break
            else:
                logging.info(
                    "I don't know what to do with this notification: %s",
                    user_action)
  def _handle_timeline_notification(self, data):
    """Handle timeline notification.

    This method handles when a user chooses to perform a custom menu optio
    (increment, decrement, reset).
    """
    # TODO: Race conditions occur if a user rapidly selects a menu item many
    #       times (concurrently reading and writing the same timeline item).
    #       Resolve this issue
    for user_action in data.get('userActions', []):
      logging.info(user_action)
      option = user_action.get('payload')
      if user_action.get('type') == 'CUSTOM' and option in PROCESS_OPTIONS:
        data = json.loads(self.request.body)
        item = self.mirror_service.timeline().get(id=data['itemId']).execute()
        num = util.get_num(custom_item_fields.get(item, 'num'))
        # possible actions are functions listed in PROCESS_OPTIONS
        custom_item_fields.set(
            item, 'num',
            PROCESS_OPTIONS[option](num), TIMELINE_ITEM_TEMPLATE_URL)
        if 'notification' in item:
          item.pop('notification')
        self.mirror_service.timeline().update(
            id=data['itemId'], body=item).execute()
	# Only handle the first successful action.
        break
      else:
        logging.info(
            "I don't know what to do with this notification: %s", user_action)
  def _new_counter(self):
    """Insert a timeline item."""
    logging.info('Inserting timeline item')
    # Note that icons will not show up when making counters on a
    # locally hosted web interface.
    body = {
        'notification': {'level': 'DEFAULT'},
        'menuItems': [
            {
                'action': 'CUSTOM',
                'id': 'increment',
                'values': [{
                    'displayName': 'Increment',
                    'iconUrl': util.get_full_url(
                        self, '/static/images/up.png')}]
            },
            {
                'action': 'CUSTOM',
                'id': 'decrement',
                'values': [{
                    'displayName': 'Decrement',
                    'iconUrl': util.get_full_url(
                        self, '/static/images/down.png')}]
            },
            {
                'action': 'CUSTOM',
                'id': 'reset',
                'values': [{
                    'displayName': 'Set Counter to 0',
                    'iconUrl': util.get_full_url(
                        self, '/static/images/reset.png')}]
            },
            {'action': 'SHARE'},
            {'action': 'TOGGLE_PINNED'},
            {'action': 'DELETE'}
        ]
    }
    new_fields = {
        'name': self.request.get('name'),
        'num': util.get_num(self.request.get('num'))
    }
    custom_item_fields.set_multiple(
        body, new_fields, TIMELINE_ITEM_TEMPLATE_URL)

    # self.mirror_service is initialized in util.auth_required.
    self.mirror_service.timeline().insert(body=body).execute()

    # Subscribe to timeline notifications if not yet subscribed. A
    # subscription should have been made during initial OAuth grant
    # but user could have unsubscribed via /subscription for debugging.
    try:
      self._subscribe()
    except HttpError:
      return (
          'A counter was made but '
          'Notifications were not enabled because an HTTP Error occured. '
          'A common cause of this problem is not using an HTTPS connection.'
      )
    return  'A new counter has been created.'
示例#4
0
 def __enter__(self):
     o, e = self.tentacle.cmd(self.cmd + ' &', expect=self.ready_pattern, timeout=self.timeout)
     self.pid = util.get_num('\[\d+\] +(\d+)', o)
     if self.pid is None:
         util.print_error('Fail to get process id for "%s"' % self.cmd)
     self.o += o
     self.e += e
     return self
  def _update_counter(self):
    """Updates the counter to user input for given timeline item."""
    item = self.mirror_service.timeline().get(
        id=self.request.get('itemId')).execute()

    new_fields = {
        'name': self.request.get('name'),
        'num': util.get_num(self.request.get('num'))
    }
    custom_item_fields.set_multiple(
        item, new_fields, TIMELINE_ITEM_TEMPLATE_URL)

    if 'notification' in item:
      item.pop('notification')
    self.mirror_service.timeline().update(
        id=self.request.get('itemId'), body=item).execute()
    return 'Counter Updated'
示例#6
0
文件: Wilson.py 项目: cwpfautz/Math
import math as m
import util


def is_prime(n):
    """Determine whether n is prime using Wilson's Theorem"""

    return m.factorial(n - 1) % n == (-1) % n


def display_primality(n):
    """Display the primality of n"""

    if is_prime(n):
        print("{0} is a prime number".format(n))
    else:
        print("{0} is a composite number".format(n))


if __name__ == '__main__':
    num = util.get_num(lim=1)
    display_primality(num)