from __future__ import unicode_literals, print_function

import json
import urllib2


class Box(object):
    """Downloads and parses metainformation about a Vagrant box"""
    def __init__(self, publisher, name):
        """Extract metainformation for a Vagrant box.

        publisher -- Atlas owner
        name -- Vagrant box name
        """
        json_url = ("https://atlas.hashicorp.com/{0}/boxes/{1}/"
                    .format(publisher, name))
        request = urllib2.Request(json_url, None,
                                  {'Accept': 'application/json'})
        json_file = urllib2.urlopen(request)
        self._data = json.loads(json_file.read())

    def versions(self):
        """Return a tuple with all available box versions."""
        return tuple(v['version'] for v in self._data['versions']
                     if v['status'] == 'active')

    def providers(self, version):
        """Return a list of providers for a specific box version."""
        _ver = ([v for v in self._data['versions']
                 if v['version'] == version])[0]
        return [p['name'] for p in _ver['providers']]

    def url(self, version, provider):
        """Return the download URL for a specific box version and provider."""
        _ver = ([v for v in self._data['versions']
                 if v['version'] == version])[0]
        return ([p for p in _ver['providers']
                 if p['name'] == provider])[0]['url']

from __future__ import unicode_literals, print_function

import json
import urllib2


class Box(object):
    """Downloads and parses metainformation about a Vagrant box"""
    def __init__(self, publisher, name):
        """Extract metainformation for a Vagrant box.

        publisher -- Atlas owner
        name -- Vagrant box name
        """
        json_url = ("https://atlas.hashicorp.com/{0}/boxes/{1}/"
                    .format(publisher, name))
        request = urllib2.Request(json_url, None,
                                  {'Accept': 'application/json'})
        json_file = urllib2.urlopen(request)
        self._data = json.loads(json_file.read())
        # We need to preserve the order of the versions
        self._versions = tuple(v['version'] for v in self._data['versions'])
        # Prepare a data structure for quick lookups
        self._boxes = {}
        for v in self._data['versions']:
            _version = v['version']
            self._boxes[_version] = {}
            for p in v['providers']:
                _provider = p['name']
                self._boxes[_version][_provider] = {}
                self._boxes[_version][_provider]['url'] = p['url']

    def versions(self):
        """Return a tuple with all available box versions."""
        return self._versions

    def providers(self, version):
        """Return a list of providers for a specific box version."""
        return self._boxes[version].keys()

    def url(self, version, provider):
        """Return the download URL for a specific box version and provider."""
        return self._boxes[version][provider]['url']

Index the Atlas data by version and provider
Index the Atlas data by version and provider

  from __future__ import unicode_literals, print_function
  
  import json
  import urllib2
  
  
  class Box(object):
      """Downloads and parses metainformation about a Vagrant box"""
      def __init__(self, publisher, name):
          """Extract metainformation for a Vagrant box.
  
          publisher -- Atlas owner
          name -- Vagrant box name
          """
          json_url = ("https://atlas.hashicorp.com/{0}/boxes/{1}/"
                      .format(publisher, name))
          request = urllib2.Request(json_url, None,
                                    {'Accept': 'application/json'})
          json_file = urllib2.urlopen(request)
          self._data = json.loads(json_file.read())
+         # We need to preserve the order of the versions
+         self._versions = tuple(v['version'] for v in self._data['versions'])
+         # Prepare a data structure for quick lookups
+         self._boxes = {}
+         for v in self._data['versions']:
+             _version = v['version']
+             self._boxes[_version] = {}
+             for p in v['providers']:
+                 _provider = p['name']
+                 self._boxes[_version][_provider] = {}
+                 self._boxes[_version][_provider]['url'] = p['url']
  
      def versions(self):
          """Return a tuple with all available box versions."""
+         return self._versions
-         return tuple(v['version'] for v in self._data['versions']
-                      if v['status'] == 'active')
  
      def providers(self, version):
          """Return a list of providers for a specific box version."""
+         return self._boxes[version].keys()
-         _ver = ([v for v in self._data['versions']
-                  if v['version'] == version])[0]
-         return [p['name'] for p in _ver['providers']]
  
      def url(self, version, provider):
          """Return the download URL for a specific box version and provider."""
+         return self._boxes[version][provider]['url']
-         _ver = ([v for v in self._data['versions']
-                  if v['version'] == version])[0]
-         return ([p for p in _ver['providers']
-                  if p['name'] == provider])[0]['url']
  
Index the Atlas data by version and provider
## Code Before:
from __future__ import unicode_literals, print_function

import json
import urllib2


class Box(object):
    """Downloads and parses metainformation about a Vagrant box"""
    def __init__(self, publisher, name):
        """Extract metainformation for a Vagrant box.

        publisher -- Atlas owner
        name -- Vagrant box name
        """
        json_url = ("https://atlas.hashicorp.com/{0}/boxes/{1}/"
                    .format(publisher, name))
        request = urllib2.Request(json_url, None,
                                  {'Accept': 'application/json'})
        json_file = urllib2.urlopen(request)
        self._data = json.loads(json_file.read())

    def versions(self):
        """Return a tuple with all available box versions."""
        return tuple(v['version'] for v in self._data['versions']
                     if v['status'] == 'active')

    def providers(self, version):
        """Return a list of providers for a specific box version."""
        _ver = ([v for v in self._data['versions']
                 if v['version'] == version])[0]
        return [p['name'] for p in _ver['providers']]

    def url(self, version, provider):
        """Return the download URL for a specific box version and provider."""
        _ver = ([v for v in self._data['versions']
                 if v['version'] == version])[0]
        return ([p for p in _ver['providers']
                 if p['name'] == provider])[0]['url']

## Instruction:
Index the Atlas data by version and provider
## Code After:
from __future__ import unicode_literals, print_function

import json
import urllib2


class Box(object):
    """Downloads and parses metainformation about a Vagrant box"""
    def __init__(self, publisher, name):
        """Extract metainformation for a Vagrant box.

        publisher -- Atlas owner
        name -- Vagrant box name
        """
        json_url = ("https://atlas.hashicorp.com/{0}/boxes/{1}/"
                    .format(publisher, name))
        request = urllib2.Request(json_url, None,
                                  {'Accept': 'application/json'})
        json_file = urllib2.urlopen(request)
        self._data = json.loads(json_file.read())
        # We need to preserve the order of the versions
        self._versions = tuple(v['version'] for v in self._data['versions'])
        # Prepare a data structure for quick lookups
        self._boxes = {}
        for v in self._data['versions']:
            _version = v['version']
            self._boxes[_version] = {}
            for p in v['providers']:
                _provider = p['name']
                self._boxes[_version][_provider] = {}
                self._boxes[_version][_provider]['url'] = p['url']

    def versions(self):
        """Return a tuple with all available box versions."""
        return self._versions

    def providers(self, version):
        """Return a list of providers for a specific box version."""
        return self._boxes[version].keys()

    def url(self, version, provider):
        """Return the download URL for a specific box version and provider."""
        return self._boxes[version][provider]['url']
