<commit_before># cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import sys

from cerbero import enums
from cerbero.errors import FatalError, ConfigurationError
from cerbero.utils import _, system_info, validate_packager
from cerbero.utils import messages as m

CONFIG_DIR = os.path.expanduser('~/.cerbero')
CONFIG_EXT = 'cbc'
DEFAULT_CONFIG_FILENAME = 'cerbero.%s' % CONFIG_EXT
DEFAULT_CONFIG_FILE = os.path.join(CONFIG_DIR, DEFAULT_CONFIG_FILENAME)
DEFAULT_GIT_ROOT = 'git+ssh://git.keema.collabora.co.uk/git/gst-sdk/'
DEFAULT_WIX_PREFIX = 'C:\\\\Program Files\\Windows Installer XML v3.5\\bin\\'
DEFAULT_ALLOW_PARALLEL_BUILD = False
DEFAULT_PACKAGER = "Default <default@change.me>"
CERBERO_UNINSTALLED = 'CERBERO_UNINSTALLED'

Platform = enums.Platform
Architecture = enums.Architecture
Distro = enums.Distro
DistroVersion = enums.DistroVersion

class Config (object):

    _properties = ['platform', 'target_platform', 'arch', 'target_arch',
                   'prefix', 'recipes_dir', 'host', 'build', 'target',
                   'sources', 'local_sources', 'lib_suffix', 'git_root',
                   'distro', 'target_distro', 'environ_dir', 'cache_file',
                   'toolchain_prefix', 'distro_version',
                   'target_distro_version', 'allow_system_libs',
                   'packages_dir', 'wix_prefix', 'py_prefix',
                   'install_dir', 'allow_parallel_build', 'num_of_cpus',
                   'use_configure_cache', 'packages_prefix', 'packager']

    def __init__(self, filename=None, load=True):
        self._check_uninstalled()

        for a in self._properties:
            setattr(self, a, None)

        if not load:
            return

        # First load the default configuration
        self.load_defaults()

        # Next parse the main configuration file
        if os.path.exists(DEFAULT_CONFIG_FILE):
            self.parse(DEFAULT_CONFIG_FILE)
        else:
            msg = _('Using default configuration because %s is missing') % \
                    DEFAULT_CONFIG_FILE
            m.warning(msg)

        # Next, if a config file is provided use it to override the settings
        # from the main configuration file
        if filename is not None:
            if os.path.exists(filename):
                self.parse(filename)
                self.filename = DEFAULT_CONFIG_FILE
            else:
                raise ConfigurationError(_("Configuration file %s doesn't "
                                           "exsits") % filename)

        # Next, load the platform configuration
        self._load_platform_config()

        # Finally fill the missing gaps in the config
        self._load_last_defaults()

        self.validate_properties()

        self.setup_env()
        self._create_path(self.local_sources)
        self._create_path(self.sources)

    def parse(self, filename, reset=True):
        config = {'os': os}
        if not reset:
            for prop in self._properties:
                if hasattr(self, prop):
                    config[prop] = getattr(self, prop)

        try:
            execfile(filename, config)
        except:
            raise ConfigurationError(_('Could not include config file (%s)') %
                             filename)
        for key in self._properties:
            if key in config:
                self.set_property(key, config[key], True)

    def setup_env(self):
        self._create_path(self.prefix)

        os.environ['CERBERO_PREFIX'] = self.prefix

        libdir = os.path.join(self.prefix, 'lib%s' % self.lib_suffix)
        self.libdir = libdir

        # Get paths for environment variables
        includedir = os.path.join(self.prefix, 'include')
        bindir = os.path.join(self.prefix, 'bin')
        manpathdir = os.path.join(self.prefix, 'share', 'man')
        infopathdir = os.path.join(self.prefix, 'share', 'info')
        pkgconfigdatadir = os.path.join(self.prefix, 'share', 'pkgconfig')
        pkgconfigdir = os.path.join(libdir, 'pkgconfig')
        typelibpath = os.path.join(self.libdir, 'girepository-1.0')
        xdgdatadir = os.path.join(self.prefix, 'share')
        xdgconfigdir = os.path.join(self.prefix, 'etc', 'xdg')
        xcursordir = os.path.join(self.prefix, 'share', 'icons')
        aclocal = os.environ.get('ACLOCAL', 'aclocal')
        aclocaldir = os.path.join(self.prefix, 'share', 'aclocal')
        perl5lib = self._join_path(os.path.join(self.prefix, 'lib', 'perl5'),
                os.path.join(self.prefix, 'lib', 'perl5', 'site_perl'))
        gstpluginpath = os.path.join(self.prefix, 'lib', 'gstreamer-0.10')
        gstregistry = os.path.join(os.path.expanduser('~'), '.gstreamer-0.10',
                                    '.cerbero-registry-%s' % self.target_arch)
        pythonpath = os.path.join(self.prefix, self.py_prefix, 'site-packages')

        if self.platform == Platform.LINUX:
            xdgdatadir += ":/usr/share:/usr/local/share"

        self._create_path(aclocaldir)

        # Most of these variables are extracted from jhbuild
        # FIXME: add python when needed
        env = {'LD_LIBRARY_PATH': libdir,
               'LDFLAGS': '-L%s %s' % (libdir,  os.environ.get('LDFLAGS', '')),
               'C_INCLUDE_PATH': includedir,
               'CPLUS_INCLUDE_PATH': includedir,
               'DYLD_FALLBACK_LIBRARY_PATH': libdir,
               'PATH': self._join_path('PATH', bindir),
               'MANPATH': manpathdir,
               'INFOPATH': infopathdir,
               'PKG_CONFIG_PATH': '%s' % pkgconfigdatadir,
               'PKG_CONFIG_LIBDIR': '%s' % pkgconfigdir,
               'GI_TYPELIB_PATH': typelibpath,
               'XDG_DATA_DIRS': xdgdatadir,
               'XDG_CONFIG_DIRS': xdgconfigdir,
               'XCURSOR_PATH': xcursordir,
               'ACLOCAL_FLAGS': '-I %s' % aclocaldir,
               'ACLOCAL': aclocal,
               'PERL5LIB': perl5lib,
               'MONO_PREFIX': self.prefix,
               'MONO_GAC_PREFIX': self.prefix,
               'GST_PLUGIN_PATH': gstpluginpath,
               'GST_REGISTRY': gstregistry,
               'PYTHONPATH': pythonpath
               }

        if self.platform == Platform.WINDOWS:
            # for pkg-config installed with the toolchain
            env['ACLOCAL_FLAGS'] += ' -I %s/share/aclocal' % self.toolchain_prefix
            env['ACLOCAL'] = '%s %s' % (aclocal, env['ACLOCAL_FLAGS'])

        # set all the variables
        self.env = env
        for e, v in env.iteritems():
            os.environ[e] = v

    def _check_uninstalled(self):
        self.uninstalled = int(os.environ.get(CERBERO_UNINSTALLED, 0)) == 1

    def _create_path(self, path):
        if not os.path.exists(path):
            try:
                os.makedirs(path)
            except:
                raise FatalError(_('directory (%s) can not be created') % path)

    def _join_path(self, env, path):
        if env not in os.environ:
            return path
        else:
            if self.platform == Platform.WINDOWS:
                separator = ';'
            else:
                separator = ':'
            return "%s%s%s" % (path, separator, os.environ['PATH'])

    def _load_platform_config(self):
        platform_config = os.path.join(self.environ_dir, '%s.config' %
                                       self.target_platform)
        arch_config = os.path.join(self.environ_dir, '%s_%s.config' %
                                   (self.target_platform, self.target_arch))

        for config in [platform_config, arch_config]:
            if os.path.exists(config):
                self.parse(config, reset=False)

    def _load_last_defaults(self):
        cerbero_home = os.path.expanduser('~/cerbero')
        self.set_property('prefix', os.path.join(cerbero_home, 'dist'))
        self.set_property('install_dir', self.prefix)
        self.set_property('sources', os.path.join(cerbero_home, 'sources'))
        self.set_property('local_sources',
                os.path.join(cerbero_home, 'sources', 'local'))

    def load_defaults(self):
        cerbero_home = os.path.expanduser('~/cerbero')
        self.set_property('cache_file', None)
        self.set_property('prefix', None)
        self.set_property('sources', None)
        self.set_property('local_sources', None)
        if not self.uninstalled:
            self.set_property('recipes_dir',
                              os.path.join(cerbero_home, 'recipes'))
            self.set_property('packages_dir',
                              os.path.join(cerbero_home, 'packages'))
        else:
            self.set_property('recipes_dir',
                os.path.join(os.path.dirname(__file__), '..', 'recipes'))
            self.set_property('packages_dir',
                os.path.join(os.path.dirname(__file__), '..', 'packages'))
        self.set_property('git_root', DEFAULT_GIT_ROOT)
        self.set_property('allow_parallel_build', DEFAULT_ALLOW_PARALLEL_BUILD)
        self.set_property('wix_prefix', DEFAULT_WIX_PREFIX)
        self.set_property('host', None)
        self.set_property('build', None)
        self.set_property('target', None)
        platform, arch, distro, distro_version, num_of_cpus = system_info()
        self.set_property('platform', platform)
        self.set_property('num_of_cpus', num_of_cpus)
        self.set_property('target_platform', platform)
        self.set_property('arch', arch)
        self.set_property('target_arch', arch)
        self.set_property('distro', distro)
        self.set_property('target_distro', distro)
        self.set_property('distro_version', distro_version)
        self.set_property('target_distro_version', distro_version)
        self.set_property('packages_prefix', None)
        self.set_property('packager', DEFAULT_PACKAGER)
        self.set_property('py_prefix', 'lib/python%s.%s' %
                (sys.version_info[0], sys.version_info[1]))
        self.set_property('lib_suffix', '')
        if not self.uninstalled:
            self.set_property('environ_dir', os.path.join(CONFIG_DIR))
        else:
            self.set_property('environ_dir',
                os.path.join(os.path.dirname(__file__), '..', 'config'))
        self.set_property('allow_system_libs', True)
        self.set_property('use_configure_cache', False)

    def validate_properties(self):
        if not validate_packager(self.packager):
            raise FatalError(_('packager "%s" must be in the format "Name <email>"') % self.packager)

    def set_property(self, name, value, force=False):
        if name not in self._properties:
            raise ConfigurationError('Unkown key %s' % name)
        if force or getattr(self, name) is None:
            setattr(self, name, value)
<commit_msg>config: add data_dir to config<commit_after># cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import sys

from cerbero import enums
from cerbero.errors import FatalError, ConfigurationError
from cerbero.utils import _, system_info, validate_packager
from cerbero.utils import messages as m

CONFIG_DIR = os.path.expanduser('~/.cerbero')
CONFIG_EXT = 'cbc'
DEFAULT_CONFIG_FILENAME = 'cerbero.%s' % CONFIG_EXT
DEFAULT_CONFIG_FILE = os.path.join(CONFIG_DIR, DEFAULT_CONFIG_FILENAME)
DEFAULT_GIT_ROOT = 'git+ssh://git.keema.collabora.co.uk/git/gst-sdk/'
DEFAULT_WIX_PREFIX = 'C:\\\\Program Files\\Windows Installer XML v3.5\\bin\\'
DEFAULT_ALLOW_PARALLEL_BUILD = False
DEFAULT_PACKAGER = "Default <default@change.me>"
CERBERO_UNINSTALLED = 'CERBERO_UNINSTALLED'
#FIXME change it with autotools
DATA_DIR = os.path.join(os.path.dirname(__file__), '..',  'data')

Platform = enums.Platform
Architecture = enums.Architecture
Distro = enums.Distro
DistroVersion = enums.DistroVersion

class Config (object):

    _properties = ['platform', 'target_platform', 'arch', 'target_arch',
                   'prefix', 'recipes_dir', 'host', 'build', 'target',
                   'sources', 'local_sources', 'lib_suffix', 'git_root',
                   'distro', 'target_distro', 'environ_dir', 'cache_file',
                   'toolchain_prefix', 'distro_version',
                   'target_distro_version', 'allow_system_libs',
                   'packages_dir', 'wix_prefix', 'py_prefix',
                   'install_dir', 'allow_parallel_build', 'num_of_cpus',
                   'use_configure_cache', 'packages_prefix', 'packager',
                   'data_dir']

    def __init__(self, filename=None, load=True):
        self._check_uninstalled()

        for a in self._properties:
            setattr(self, a, None)

        if not load:
            return

        # First load the default configuration
        self.load_defaults()

        # Next parse the main configuration file
        if os.path.exists(DEFAULT_CONFIG_FILE):
            self.parse(DEFAULT_CONFIG_FILE)
        else:
            msg = _('Using default configuration because %s is missing') % \
                    DEFAULT_CONFIG_FILE
            m.warning(msg)

        # Next, if a config file is provided use it to override the settings
        # from the main configuration file
        if filename is not None:
            if os.path.exists(filename):
                self.parse(filename)
                self.filename = DEFAULT_CONFIG_FILE
            else:
                raise ConfigurationError(_("Configuration file %s doesn't "
                                           "exsits") % filename)

        # Next, load the platform configuration
        self._load_platform_config()

        # Finally fill the missing gaps in the config
        self._load_last_defaults()

        self.validate_properties()

        self.setup_env()
        self._create_path(self.local_sources)
        self._create_path(self.sources)

    def parse(self, filename, reset=True):
        config = {'os': os}
        if not reset:
            for prop in self._properties:
                if hasattr(self, prop):
                    config[prop] = getattr(self, prop)

        try:
            execfile(filename, config)
        except:
            raise ConfigurationError(_('Could not include config file (%s)') %
                             filename)
        for key in self._properties:
            if key in config:
                self.set_property(key, config[key], True)

    def setup_env(self):
        self._create_path(self.prefix)

        os.environ['CERBERO_PREFIX'] = self.prefix

        libdir = os.path.join(self.prefix, 'lib%s' % self.lib_suffix)
        self.libdir = libdir

        # Get paths for environment variables
        includedir = os.path.join(self.prefix, 'include')
        bindir = os.path.join(self.prefix, 'bin')
        manpathdir = os.path.join(self.prefix, 'share', 'man')
        infopathdir = os.path.join(self.prefix, 'share', 'info')
        pkgconfigdatadir = os.path.join(self.prefix, 'share', 'pkgconfig')
        pkgconfigdir = os.path.join(libdir, 'pkgconfig')
        typelibpath = os.path.join(self.libdir, 'girepository-1.0')
        xdgdatadir = os.path.join(self.prefix, 'share')
        xdgconfigdir = os.path.join(self.prefix, 'etc', 'xdg')
        xcursordir = os.path.join(self.prefix, 'share', 'icons')
        aclocal = os.environ.get('ACLOCAL', 'aclocal')
        aclocaldir = os.path.join(self.prefix, 'share', 'aclocal')
        perl5lib = self._join_path(os.path.join(self.prefix, 'lib', 'perl5'),
                os.path.join(self.prefix, 'lib', 'perl5', 'site_perl'))
        gstpluginpath = os.path.join(self.prefix, 'lib', 'gstreamer-0.10')
        gstregistry = os.path.join(os.path.expanduser('~'), '.gstreamer-0.10',
                                    '.cerbero-registry-%s' % self.target_arch)
        pythonpath = os.path.join(self.prefix, self.py_prefix, 'site-packages')

        if self.platform == Platform.LINUX:
            xdgdatadir += ":/usr/share:/usr/local/share"

        self._create_path(aclocaldir)

        # Most of these variables are extracted from jhbuild
        # FIXME: add python when needed
        env = {'LD_LIBRARY_PATH': libdir,
               'LDFLAGS': '-L%s %s' % (libdir,  os.environ.get('LDFLAGS', '')),
               'C_INCLUDE_PATH': includedir,
               'CPLUS_INCLUDE_PATH': includedir,
               'DYLD_FALLBACK_LIBRARY_PATH': libdir,
               'PATH': self._join_path('PATH', bindir),
               'MANPATH': manpathdir,
               'INFOPATH': infopathdir,
               'PKG_CONFIG_PATH': '%s' % pkgconfigdatadir,
               'PKG_CONFIG_LIBDIR': '%s' % pkgconfigdir,
               'GI_TYPELIB_PATH': typelibpath,
               'XDG_DATA_DIRS': xdgdatadir,
               'XDG_CONFIG_DIRS': xdgconfigdir,
               'XCURSOR_PATH': xcursordir,
               'ACLOCAL_FLAGS': '-I %s' % aclocaldir,
               'ACLOCAL': aclocal,
               'PERL5LIB': perl5lib,
               'MONO_PREFIX': self.prefix,
               'MONO_GAC_PREFIX': self.prefix,
               'GST_PLUGIN_PATH': gstpluginpath,
               'GST_REGISTRY': gstregistry,
               'PYTHONPATH': pythonpath
               }

        if self.platform == Platform.WINDOWS:
            # for pkg-config installed with the toolchain
            env['ACLOCAL_FLAGS'] += ' -I %s/share/aclocal' % self.toolchain_prefix
            env['ACLOCAL'] = '%s %s' % (aclocal, env['ACLOCAL_FLAGS'])

        # set all the variables
        self.env = env
        for e, v in env.iteritems():
            os.environ[e] = v

    def _check_uninstalled(self):
        self.uninstalled = int(os.environ.get(CERBERO_UNINSTALLED, 0)) == 1

    def _create_path(self, path):
        if not os.path.exists(path):
            try:
                os.makedirs(path)
            except:
                raise FatalError(_('directory (%s) can not be created') % path)

    def _join_path(self, env, path):
        if env not in os.environ:
            return path
        else:
            if self.platform == Platform.WINDOWS:
                separator = ';'
            else:
                separator = ':'
            return "%s%s%s" % (path, separator, os.environ['PATH'])

    def _load_platform_config(self):
        platform_config = os.path.join(self.environ_dir, '%s.config' %
                                       self.target_platform)
        arch_config = os.path.join(self.environ_dir, '%s_%s.config' %
                                   (self.target_platform, self.target_arch))

        for config in [platform_config, arch_config]:
            if os.path.exists(config):
                self.parse(config, reset=False)

    def _load_last_defaults(self):
        cerbero_home = os.path.expanduser('~/cerbero')
        self.set_property('prefix', os.path.join(cerbero_home, 'dist'))
        self.set_property('install_dir', self.prefix)
        self.set_property('sources', os.path.join(cerbero_home, 'sources'))
        self.set_property('local_sources',
                os.path.join(cerbero_home, 'sources', 'local'))

    def load_defaults(self):
        cerbero_home = os.path.expanduser('~/cerbero')
        self.set_property('cache_file', None)
        self.set_property('prefix', None)
        self.set_property('sources', None)
        self.set_property('local_sources', None)
        if not self.uninstalled:
            self.set_property('recipes_dir',
                              os.path.join(cerbero_home, 'recipes'))
            self.set_property('packages_dir',
                              os.path.join(cerbero_home, 'packages'))
        else:
            self.set_property('recipes_dir',
                os.path.join(os.path.dirname(__file__), '..', 'recipes'))
            self.set_property('packages_dir',
                os.path.join(os.path.dirname(__file__), '..', 'packages'))
        self.set_property('git_root', DEFAULT_GIT_ROOT)
        self.set_property('allow_parallel_build', DEFAULT_ALLOW_PARALLEL_BUILD)
        self.set_property('wix_prefix', DEFAULT_WIX_PREFIX)
        self.set_property('host', None)
        self.set_property('build', None)
        self.set_property('target', None)
        platform, arch, distro, distro_version, num_of_cpus = system_info()
        self.set_property('platform', platform)
        self.set_property('num_of_cpus', num_of_cpus)
        self.set_property('target_platform', platform)
        self.set_property('arch', arch)
        self.set_property('target_arch', arch)
        self.set_property('distro', distro)
        self.set_property('target_distro', distro)
        self.set_property('distro_version', distro_version)
        self.set_property('target_distro_version', distro_version)
        self.set_property('packages_prefix', None)
        self.set_property('packager', DEFAULT_PACKAGER)
        self.set_property('py_prefix', 'lib/python%s.%s' %
                (sys.version_info[0], sys.version_info[1]))
        self.set_property('lib_suffix', '')
        if not self.uninstalled:
            self.set_property('data_dir', DATA_DIR)
            self.set_property('environ_dir', os.path.join(CONFIG_DIR))
        else:
            self.set_property('data_dir',
                os.path.join(os.path.dirname(__file__), '..', 'data'))
            self.set_property('environ_dir',
                os.path.join(os.path.dirname(__file__), '..', 'config'))
        self.set_property('allow_system_libs', True)
        self.set_property('use_configure_cache', False)

    def validate_properties(self):
        if not validate_packager(self.packager):
            raise FatalError(_('packager "%s" must be in the format "Name <email>"') % self.packager)

    def set_property(self, name, value, force=False):
        if name not in self._properties:
            raise ConfigurationError('Unkown key %s' % name)
        if force or getattr(self, name) is None:
            setattr(self, name, value)
<|endoftext|>


<commit_before># -*- coding: ascii -*-
#--------------------------------------------------------------
#Created on Nov 21, 2013
#
#@author: Piyush Harsh
#@contact: piyush.harsh@zhaw.ch
#@organization: ICCLab, Zurich University of Applied Sciences
#@summary: Module to interact with OS-ceilometer service
#@contributor: Tea Kolevska
#@contact: tea.kolevska@zhaw.ch
#@var username, tenant-id, password
#@requires: python 2.7
#--------------------------------------------------------------

import httplib2 as http
import sys, re
import json
from collections import namedtuple

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

def get_meter_list(token, api_endpoint):
    meter_list = [None]
    headers = {
               'Accept': 'application/json',
               'Content-Type': 'application/json;',
               'X-Auth-Token': token
    }
    path = "/v2/meters"
    target = urlparse(api_endpoint+path)
    method = 'GET'
    body = ''
    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    #converting the header of the response to json object
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside get_server_list(): Something went wrong!"
        return False, meter_list
    else:
        data = json.loads(content)
        #print "Number of meters found: %d" % len(data)
        meter_list = [None]*len(data)
        for i in range(len(data)):
            meter_list[i] = {}
            meter_list[i]["user-id"] = data[i]["user_id"]
            meter_list[i]["meter-name"] = data[i]["name"]
            meter_list[i]["resource-id"] = data[i]["resource_id"]
            meter_list[i]["meter-source"] = data[i]["source"]
            meter_list[i]["meter-id"] = data[i]["meter_id"]
            meter_list[i]["tenant-id"] = data[i]["project_id"]
            meter_list[i]["meter-type"] = data[i]["type"]
            meter_list[i]["meter-unit"] = data[i]["unit"]
        #print data
    return True, meter_list

def query():
    status=False

    period=raw_input("Do you want to define the starting and ending time? If yes, enter 'Y',else enter 'N'. ")
    if(period=="Y"):
        from_date = raw_input("Date from yyyy-mm-dd: ")
        from_time= raw_input("Time from hh:mm:ss ")
        to_date = raw_input("Date to yyyy-mm-dd: ")
        to_time= raw_input("Time to hh:mm:ss ")
        status=True
    else:
        from_date="/"
        to_date="/"
        from_time="/"
        to_time="/"
    rid=raw_input("Do you want to define the resource id? If yes, enter 'Y', else enter 'N'. ")
    if(rid=="Y"):
        resource_id=raw_input("Enter the resource id: ")
        status=True
    else:
        resource_id="/"
    pid=raw_input("Do you want to define the project id? If yes, enter 'Y', else enter 'N'. ")
    if(pid=="Y"):
        project_id=raw_input("Enter the project id: ")
        status=True
    else:
        project_id="/"
    return from_date,to_date,from_time,to_time,resource_id,project_id,status

def set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q):
    if(status_q==True):
        q='"q":['
        if (from_date not in "/"):
            q= q+'{"field": "timestamp","op": "ge","value": "'+from_date+'T'+from_time+'"},{"field": "timestamp","op": "lt","value": "'+to_date+'T'+to_time+'"}'
            if (resource_id != "/"):
                q=q+',{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
                if (project_id != "/"):
                    q=q+',{"field": "project_id","op": "eq","value": "'+project_id+'"}'
            else:
                if (resource_id != "/"):
                    q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
        else:
            if (resource_id != "/"):
                q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
                if (project_id != ""):
                    q=q+',{"field": "project_id","op": "eq","value": "'+project_id+'"}'
            else:
                if (resource_id != "/"):
                    q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
        q=q+']'
    return q

def meter_statistics(meter_id,api_endpoint,token):
    meter_stat = [None]
    headers = {
               #'Accept': 'application/json',
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/meters/"+meter_id+"/statistics"
    target = urlparse(api_endpoint+path)
    method = 'GET'
    #body = '{"q":[{"field": "timestamp","op": "ge","value": "'+from_date+'T'+from_time+'"},{"field": "timestamp","op": "lt","value": "'+to_date+'T'+to_time+'"}]}'

    #body='{"q": [{"field": "timestamp","op": "ge","value": "2013-12-28T00:00:00"},{"field": "timestamp","op": "lt","value": "2014-01-05T00:00:00"}]}'

    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body="{"+q
        period=raw_input("Do you want to define a time period? Enter 'Y' if yes, 'N' if no.")
        if(period=="Y"):
            period_def=raw_input("Enter the desired time period in seconds: ")
            body=body+',"period":'+period_def
        groupby=raw_input("Do you want to define a group by value? Enter 'Y' if yes, 'N' if no.")
        if (groupby=="Y") :
            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                    groupby_def='",groupby":['
                    groupby_def=groupby_def+'"resource_id"'
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def=groupby_def+',"project_id"'
                        groupby_def=groupby_def+']'
                        body=body+groupby_def
            else:
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def='",groupby":['
                    groupby_def=groupby_def+'"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
        body=body+"}"
    else:
        body="{"
        period=raw_input("Do you want to define a time period? Enter 'Y' if yes, 'N' if no.")
        if(period=="Y"):
            period_def=raw_input("Enter the desired time period in seconds: ")
            body=body+'"period":'+period_def

            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                groupby_def='",groupby":['
                groupby_def=groupby_def+'"resource_id"'
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def=groupby_def+',"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
                else:
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def='",groupby":['
                        groupby_def=groupby_def+'"project_id"'
                        groupby_def=groupby_def+']'
                        body=body+groupby_def
            body=body+"}"
        else:
            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                    groupby_def='"groupby":['
                    groupby_def=groupby_def+'"resource_id"'
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def=groupby_def+',"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
            else:
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def='"groupby":['
                    groupby_def=groupby_def+'"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
            body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside meter_statistics(): Something went wrong!"
        return False, meter_stat
    else:
        data = json.loads(content)
        meter_stat = [None]*len(data)
        for i in range(len(data)):
            meter_stat[i]={}
            meter_stat[i]["average"] = data[i]["avg"]
            meter_stat[i]["count"] = data[i]["count"]
            meter_stat[i]["duration"] = data[i]["duration"]
            meter_stat[i]["duration-end"] = data[i]["duration_end"]
            meter_stat[i]["duration-start"] = data[i]["duration_start"]
            meter_stat[i]["max"] = data[i]["max"]
            meter_stat[i]["min"] = data[i]["min"]
            meter_stat[i]["period"] = data[i]["period"]
            meter_stat[i]["period-end"] = data[i]["period_end"]
            meter_stat[i]["period-start"] = data[i]["period_start"]
            meter_stat[i]["sum"] = data[i]["sum"]
            meter_stat[i]["unit"] = data[i]["unit"]
            meter_stat[i]["group-by"] = data[i]["groupby"]
        return True, meter_stat

def get_meter_samples(meter_name,api_endpoint,token):
    meter_samples=[None]
    headers = {
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/meters/"+meter_name
    target = urlparse(api_endpoint+path)
    method = 'GET'
    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body="{"+q
        limit=raw_input("Do you want to set a limit to the number of samples that gets returned? Enter 'Y' if yes, 'N' if no.")
        if(limit=="Y"):
            limit_def=raw_input("Enter the desired limit for the number of samples: ")
            body=body+',"limit":'+limit_def
        body=body+"}"
    else:
        body="{"
        limit=raw_input("Do you want to set a limit to the number of samples that gets returned? Enter 'Y' if yes, 'N' if no.")
        if(limit=="Y"):
            limit_def=raw_input("Enter the desired limit for the number of samples: ")
            body=body+'"limit":'+limit_def
        body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside meter_samples(): Something went wrong!"
        return False, meter_samples
    else:
        data = json.loads(content)
        meter_samples = [None]*len(data)

        for i in range(len(data)):
            meter_samples[i]={}
            meter_samples[i]["counter-name"] = data[i]["counter_name"]
            meter_samples[i]["counter-type"] = data[i]["counter_type"]
            meter_samples[i]["counter-unit"] = data[i]["counter_unit"]
            meter_samples[i]["counter-volume"] = data[i]["counter_volume"]
            meter_samples[i]["message-id"] = data[i]["message_id"]
            meter_samples[i]["project-id"] = data[i]["project_id"]
            meter_samples[i]["resource-id"] = data[i]["resource_id"]
            catalog=data[i]["resource_metadata"]
            #for key,value in catalog.iteritems():
            #    meter_samples[i]["resource-metadata"][catalog[key]] = catalog[key]
            #meter_samples[i]["resource-metadata"]=data[i]["resource_metadata"]
            cat_pom = json.dumps(catalog)
            cat_pom=cat_pom.translate(None,'"{}')
            #meter_samples[i]["resource-metadata"] = dict(e.split(': ') for e in cat_pom.split(','))
            meter_samples[i]["resource-metadata"]=cat_pom
            meter_samples[i]["source"] = data[i]["source"]
            meter_samples[i]["timestamp"] = data[i]["timestamp"]
            meter_samples[i]["user-id"] = data[i]["user_id"]
        return True, meter_samples

    <commit_msg>get resources<commit_after># -*- coding: ascii -*-
#--------------------------------------------------------------
#Created on Nov 21, 2013
#
#@author: Piyush Harsh
#@contact: piyush.harsh@zhaw.ch
#@organization: ICCLab, Zurich University of Applied Sciences
#@summary: Module to interact with OS-ceilometer service
#@contributor: Tea Kolevska
#@contact: tea.kolevska@zhaw.ch
#@var username, tenant-id, password
#@requires: python 2.7
#--------------------------------------------------------------

import httplib2 as http
import sys, re
import json
from collections import namedtuple

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

def get_meter_list(token, api_endpoint):
    meter_list = [None]
    headers = {
               'Accept': 'application/json',
               'Content-Type': 'application/json;',
               'X-Auth-Token': token
    }
    path = "/v2/meters"
    target = urlparse(api_endpoint+path)
    method = 'GET'
    body = ''
    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    #converting the header of the response to json object
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside get_server_list(): Something went wrong!"
        return False, meter_list
    else:
        data = json.loads(content)
        #print "Number of meters found: %d" % len(data)
        meter_list = [None]*len(data)
        for i in range(len(data)):
            meter_list[i] = {}
            meter_list[i]["user-id"] = data[i]["user_id"]
            meter_list[i]["meter-name"] = data[i]["name"]
            meter_list[i]["resource-id"] = data[i]["resource_id"]
            meter_list[i]["meter-source"] = data[i]["source"]
            meter_list[i]["meter-id"] = data[i]["meter_id"]
            meter_list[i]["tenant-id"] = data[i]["project_id"]
            meter_list[i]["meter-type"] = data[i]["type"]
            meter_list[i]["meter-unit"] = data[i]["unit"]
        #print data
    return True, meter_list

def query():
    status=False

    period=raw_input("Do you want to define the starting and ending time? If yes, enter 'Y',else enter 'N'. ")
    if(period=="Y"):
        from_date = raw_input("Date from yyyy-mm-dd: ")
        from_time= raw_input("Time from hh:mm:ss ")
        to_date = raw_input("Date to yyyy-mm-dd: ")
        to_time= raw_input("Time to hh:mm:ss ")
        status=True
    else:
        from_date="/"
        to_date="/"
        from_time="/"
        to_time="/"
    rid=raw_input("Do you want to define the resource id? If yes, enter 'Y', else enter 'N'. ")
    if(rid=="Y"):
        resource_id=raw_input("Enter the resource id: ")
        status=True
    else:
        resource_id="/"
    pid=raw_input("Do you want to define the project id? If yes, enter 'Y', else enter 'N'. ")
    if(pid=="Y"):
        project_id=raw_input("Enter the project id: ")
        status=True
    else:
        project_id="/"
    return from_date,to_date,from_time,to_time,resource_id,project_id,status

def set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q):
    if(status_q==True):
        q='"q":['
        if (from_date not in "/"):
            q= q+'{"field": "timestamp","op": "ge","value": "'+from_date+'T'+from_time+'"},{"field": "timestamp","op": "lt","value": "'+to_date+'T'+to_time+'"}'
            if (resource_id != "/"):
                q=q+',{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
                if (project_id != "/"):
                    q=q+',{"field": "project_id","op": "eq","value": "'+project_id+'"}'
            else:
                if (resource_id != "/"):
                    q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
        else:
            if (resource_id != "/"):
                q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
                if (project_id != "/"):
                    q=q+',{"field": "project_id","op": "eq","value": "'+project_id+'"}'
            else:
                if (resource_id != "/"):
                    q=q+'{"field": "resource_id","op": "eq","value": "'+resource_id+'"}'
        q=q+']'
    return q

def meter_statistics(meter_id,api_endpoint,token):
    meter_stat = [None]
    headers = {
               #'Accept': 'application/json',
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/meters/"+meter_id+"/statistics"
    target = urlparse(api_endpoint+path)
    method = 'GET'
    #body = '{"q":[{"field": "timestamp","op": "ge","value": "'+from_date+'T'+from_time+'"},{"field": "timestamp","op": "lt","value": "'+to_date+'T'+to_time+'"}]}'

    #body='{"q": [{"field": "timestamp","op": "ge","value": "2013-12-28T00:00:00"},{"field": "timestamp","op": "lt","value": "2014-01-05T00:00:00"}]}'

    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body="{"+q
        period=raw_input("Do you want to define a time period? Enter 'Y' if yes, 'N' if no.")
        if(period=="Y"):
            period_def=raw_input("Enter the desired time period in seconds: ")
            body=body+',"period":'+period_def
        groupby=raw_input("Do you want to define a group by value? Enter 'Y' if yes, 'N' if no.")
        if (groupby=="Y") :
            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                    groupby_def='",groupby":['
                    groupby_def=groupby_def+'"resource_id"'
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def=groupby_def+',"project_id"'
                        groupby_def=groupby_def+']'
                        body=body+groupby_def
            else:
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def='",groupby":['
                    groupby_def=groupby_def+'"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
        body=body+"}"
    else:
        body="{"
        period=raw_input("Do you want to define a time period? Enter 'Y' if yes, 'N' if no.")
        if(period=="Y"):
            period_def=raw_input("Enter the desired time period in seconds: ")
            body=body+'"period":'+period_def

            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                groupby_def='",groupby":['
                groupby_def=groupby_def+'"resource_id"'
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def=groupby_def+',"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
                else:
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def='",groupby":['
                        groupby_def=groupby_def+'"project_id"'
                        groupby_def=groupby_def+']'
                        body=body+groupby_def
            body=body+"}"
        else:
            rid=raw_input("Do you want to group by the resource id? If yes, enter 'Y', else enter 'N'. ")
            if(rid=="Y"):
                    groupby_def='"groupby":['
                    groupby_def=groupby_def+'"resource_id"'
                    pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                    if(pid=="Y"):
                        groupby_def=groupby_def+',"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
            else:
                pid=raw_input("Do you want to group by the project id? If yes, enter 'Y', else enter 'N'. ")
                if(pid=="Y"):
                    groupby_def='"groupby":['
                    groupby_def=groupby_def+'"project_id"'
                    groupby_def=groupby_def+']'
                    body=body+groupby_def
            body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside meter_statistics(): Something went wrong!"
        return False, meter_stat
    else:
        data = json.loads(content)
        meter_stat = [None]*len(data)
        for i in range(len(data)):
            meter_stat[i]={}
            meter_stat[i]["average"] = data[i]["avg"]
            meter_stat[i]["count"] = data[i]["count"]
            meter_stat[i]["duration"] = data[i]["duration"]
            meter_stat[i]["duration-end"] = data[i]["duration_end"]
            meter_stat[i]["duration-start"] = data[i]["duration_start"]
            meter_stat[i]["max"] = data[i]["max"]
            meter_stat[i]["min"] = data[i]["min"]
            meter_stat[i]["period"] = data[i]["period"]
            meter_stat[i]["period-end"] = data[i]["period_end"]
            meter_stat[i]["period-start"] = data[i]["period_start"]
            meter_stat[i]["sum"] = data[i]["sum"]
            meter_stat[i]["unit"] = data[i]["unit"]
            meter_stat[i]["group-by"] = data[i]["groupby"]
        return True, meter_stat

def get_meter_samples(meter_name,api_endpoint,token):
    meter_samples=[None]
    headers = {
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/meters/"+meter_name
    target = urlparse(api_endpoint+path)
    method = 'GET'
    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body="{"+q
        limit=raw_input("Do you want to set a limit to the number of samples that gets returned? Enter 'Y' if yes, 'N' if no.")
        if(limit=="Y"):
            limit_def=raw_input("Enter the desired limit for the number of samples: ")
            body=body+',"limit":'+limit_def
        body=body+"}"
    else:
        body="{"
        limit=raw_input("Do you want to set a limit to the number of samples that gets returned? Enter 'Y' if yes, 'N' if no.")
        if(limit=="Y"):
            limit_def=raw_input("Enter the desired limit for the number of samples: ")
            body=body+'"limit":'+limit_def
        body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside meter_samples(): Something went wrong!"
        return False, meter_samples
    else:
        data = json.loads(content)
        meter_samples = [None]*len(data)

        for i in range(len(data)):
            meter_samples[i]={}
            meter_samples[i]["counter-name"] = data[i]["counter_name"]
            meter_samples[i]["counter-type"] = data[i]["counter_type"]
            meter_samples[i]["counter-unit"] = data[i]["counter_unit"]
            meter_samples[i]["counter-volume"] = data[i]["counter_volume"]
            meter_samples[i]["message-id"] = data[i]["message_id"]
            meter_samples[i]["project-id"] = data[i]["project_id"]
            meter_samples[i]["resource-id"] = data[i]["resource_id"]
            catalog=data[i]["resource_metadata"]
            #for key,value in catalog.iteritems():
            #    meter_samples[i]["resource-metadata"][catalog[key]] = catalog[key]
            #meter_samples[i]["resource-metadata"]=data[i]["resource_metadata"]
            cat_pom = json.dumps(catalog)
            cat_pom=cat_pom.translate(None,'"{}')
            #meter_samples[i]["resource-metadata"] = dict(e.split(': ') for e in cat_pom.split(','))
            meter_samples[i]["resource-metadata"]=cat_pom
            meter_samples[i]["source"] = data[i]["source"]
            meter_samples[i]["timestamp"] = data[i]["timestamp"]
            meter_samples[i]["user-id"] = data[i]["user_id"]
        return True, meter_samples

def get_resources(api_endpoint,token):
    resources_list=[None]
    headers = {
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/resources"
    target = urlparse(api_endpoint+path)
    method = 'GET'
    body="{"
    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body=body+q
    body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside resources_list(): Something went wrong!"
        return False, resources_list
    else:
        data = json.loads(content)
        resources_list = [None]*len(data)
        links_list=[None]

        for i in range(len(data)):
            resources_list[i]={}
            links_list = [None]*len(data[i]["links"])
            for j in range(len(data[i]["links"])):
                links_list[j]={}
                links_list[j]["href"]=data[i]["links"][j]["href"]
                links_list[j]["rel"]=data[i]["links"][j]["rel"]
            resources_list[i]["links"] = links_list
            catalog=data[i]["metadata"]
            cat_pom = json.dumps(catalog)
            cat_pom=cat_pom.translate(None,'"{}')
            resources_list[i]["metadata"]=cat_pom
            resources_list[i]["project-id"] = data[i]["project_id"]
            resources_list[i]["resource-id"] = data[i]["resource_id"]
            resources_list[i]["source"] = data[i]["source"]

            resources_list[i]["user-id"]=data[i]["user_id"]
        return True, resources_list

def get_resources_by_id(api_endpoint,token,rid):
    resources_list=[None]
    headers = {
               'Content-Type': 'application/json;',
               'X-Auth-Token': token

    }
    from_date,to_date,from_time,to_time,resource_id,project_id,status_q=query()
    path = "/v2/resources/"+rid
    target = urlparse(api_endpoint+path)
    method = 'GET'
    body="{"
    if(status_q==True):
        q=set_query(from_date,to_date,from_time,to_time,resource_id,project_id,status_q)
        body=body+q
    body=body+"}"

    h = http.Http()
    response, content = h.request(target.geturl(),method,body,headers)
    header = json.dumps(response)
    json_header = json.loads(header)

    server_response = json_header["status"]
    if server_response not in {'200'}:
        print "Inside resources_list(): Something went wrong!"
        return False, resources_list
    else:
        data = json.loads(content)
        resources_list = [None]*len(data)
        links_list=[None]

        for i in range(len(data)):
            resources_list[i]={}
            links_list = [None]*len(data[i]["links"])
            for j in range(len(data[i]["links"])):
                links_list[j]={}
                links_list[j]["href"]=data[i]["links"][j]["href"]
                links_list[j]["rel"]=data[i]["links"][j]["rel"]
            resources_list[i]["links"] = links_list
            catalog=data[i]["metadata"]
            cat_pom = json.dumps(catalog)
            cat_pom=cat_pom.translate(None,'"{}')
            resources_list[i]["metadata"]=cat_pom
            resources_list[i]["project-id"] = data[i]["project_id"]
            resources_list[i]["resource-id"] = data[i]["resource_id"]
            resources_list[i]["source"] = data[i]["source"]

            resources_list[i]["user-id"]=data[i]["user_id"]
        return True, resources_list

    <|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from decimal import Decimal as D

import json
import importlib

from django.db import models
from django.conf import settings
from django.contrib import messages
from django.utils.html import format_html_join
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse_lazy
from django.template.loader import render_to_string

from oscar.apps.shipping.abstract_models import AbstractWeightBased
from oscar.core import prices, loading

from .packers import Packer
from .exceptions import ( OriginCityNotFoundError,
                          CityNotFoundError,
                          ApiOfflineError,
                          TooManyFoundError,
                          CalculationError)

weight_precision = getattr(settings, 'OSCAR_SHIPPING_WEIGHT_PRECISION', D('0.000'))
volume_precision = getattr(settings, 'OSCAR_SHIPPING_VOLUME_PRECISION', D('0.000'))

Scale = loading.get_class('shipping.scales', 'Scale')

DEFAULT_ORIGIN = getattr(settings, 'OSCAR_SHIPPING_DEFAULT_ORIGIN', u'Санкт-Петербург')

API_ENABLED = getattr(settings, 'OSCAR_SHIPPING_API_ENABLED', ['pecom', 'emspost'])

API_AVAILABLE = {'pecom': _('PEC API ver. 1.0'),
                 'emspost' :_('EMS Russian Post REST API'),
                 'dhl' : _('DHL API (not ready yet)'),
                 'usps' : _('USPS API (not ready yet)'),
                 }
ONLINE, OFFLINE, DISABLED = 'online','offline','disabled'
API_STATUS_CHOICES = (
    (ONLINE, _('Online')),
    (OFFLINE, _('Offline')),
    (DISABLED, _('Disabled')),
)

def get_api_modules():
    res = {}
    for name in API_AVAILABLE.keys():
        try:
            res[name] = importlib.import_module(".facade.%s" % name, __package__)
        except ImportError:
            pass
    return res

api_modules_pool = get_api_modules()

def get_enabled_api():
    #mods = api_modules_pool
    return [(a, API_AVAILABLE[a]) for a in (API_ENABLED and api_modules_pool.keys()) ]

class ShippingCompanyManager(models.Manager):
    def get_queryset(self):
        """
        Just return original queryset
        """
        return super(ShippingCompanyManager, self).get_queryset()

class AvailableCompanyManager(ShippingCompanyManager):

    def get_queryset(self):
        """
        Filter out inactive methods (shipping companies with outdated contracts etc)
        """
        return super(AvailableCompanyManager, self).get_queryset().filter(is_active=True)

    def for_address(self, addr):
        """
        Prepopulate destination field with the given address
        for charge calculating
        """
        methods = self.get_queryset()
        for m in methods:
            m.set_destination(addr)
        return methods

class ShippingCompany(AbstractWeightBased):
    """Shipping methods based on cargo companies APIs.
    """
    size_attributes = ('width' , 'height', 'lenght')

    destination = None # not stored field used for charge calculation

    errors = ''   # There is an issue with iterables and arrays as class properties and it scopes for class and instance
    messages = '' # so just a one string messages per calculate() method call

    api_user = models.CharField(_("API username"), max_length=64, blank=True)
    api_key = models.CharField(_("API key"), max_length=255, blank=True)
    api_type = models.CharField(verbose_name=_('API type'),
                                max_length=10,
                                choices=get_enabled_api(),
                                blank=True)
    origin = models.CharField(_("City of origin"), max_length=255, blank=True, default=DEFAULT_ORIGIN)
    is_active = models.BooleanField(_('active'), default=False,
                                        help_text=_('Use this method in checkout?'))
    status = models.CharField(verbose_name=_('status'),
                                max_length=10,
                                choices=API_STATUS_CHOICES,
                                blank=True)
    containers = models.ManyToManyField("ShippingContainer",
                                        blank=True,
                                        related_name='containers',
                                        verbose_name=_('Containers or boxes'),
                                        help_text=_('Containers or boxes could be used for packing')
                                        )

    objects = ShippingCompanyManager()
    available = AvailableCompanyManager()

    def __init__(self, *args, **kwargs):
        super(ShippingCompany, self).__init__(*args, **kwargs)
        if self.api_type:
            self.facade = api_modules_pool[self.api_type].ShippingFacade(self.api_user, self.api_key)

    def calculate(self, basket, options=None):

        results = []
        charge = D('0.0')
        self.messages = ''
        self.errors = ''
        # Note, when weighing the basket, we don't check whether the item
        # requires shipping or not.  It is assumed that if something has a
        # weight, then it requires shipping.
        scale = Scale(attribute_code=self.weight_attribute,
                      default_weight=self.default_weight)
        packer = Packer(self.containers,
                        attribute_codes=self.size_attributes,
                        weight_code=self.weight_attribute,
                        default_weight=self.default_weight)
        weight = scale.weigh_basket(basket)
        # Should be a list of dicts { 'weight': weight, 'container' : container }
        packs = packer.pack_basket(basket)
        facade = self.facade
        if not self.destination:
            self.errors+=_("ERROR! There is no shipping address for charge calculation!\n")
        else:
            self.messages+=_("""Approximated shipping price
                                for %{weight}d kg from %{origin}s
                                to %{destination}s\n""" % {'weight': weight,
                                                            'origin': self.origin,
                                                            'destination': self.destination.city})

            # Assuming cases like http protocol suggests:
            # e=200  - OK. Result contains charge value and extra info such as Branch code, etc
            # e=404  - Result is empty, no destination found via API, redirect
            #          to address form or prompt to API city-codes selector
            # e=503  - API is offline. Skip this method.
            # e=300  - Too many choices found, Result contains list of charges-codes.
            #          Prompt to found dest-codes selector

            # an URL for AJAXed city-to-city charge lookup
            details_url = reverse_lazy('shipping:charge-details', kwargs={'slug': self.code})
            # an URL for AJAXed code by city lookup using Select2 widget
            lookup_url=reverse_lazy('shipping:city-lookup', kwargs={'slug': self.code})

            # if options set make a short call to API for final calculation
            if options:
                errors = None
                try:
                    results, errors = facade.get_charge(options['senderCityId'],
                                                options['receiverCityId'],
                                                packs)
                except CalculationError as e:
                    self.errors = "Post-calculation error: %s" % e.errors
                    self.messages = e.title
                except:
                    raise
                if not errors:
                    (charge, self.messages,
                     self.errors, self.extra_form) = facade.parse_results(results,
                                                                          options=options)
                else:
                    raise CalculationError("%s -> %s" % (options['senderCityId'],
                                                         options['receiverCityId']),
                                           errors)
            else:
                try:
                    results = facade.get_charges(weight, packs, self.origin, self.destination)
                except ApiOfflineError:
                    self.errors += _("""%s API is offline. Cant
                                        calculate anything. Sorry!""") % self.name
                    self.messages = _("Please, choose another shipping method!")
                except OriginCityNotFoundError as e:
                    # Paranoid mode as ImproperlyConfigured should be raised by facade
                    self.errors += _("""City of origin '%s' not found
                                      in the shipping company
                                      postcodes to calculate charge.""") % e.title
                    self.messages = _("""It seems like we couldnt find code
                                        for the city of origin (%s).
                                        Please, select it manually, choose another
                                        address or another shipping method.
                                    """) % e.title
                except ImproperlyConfigured as e: # upraised error handling
                    self.errors += "ImproperlyConfigured error (%s)" % e.message
                    self.messages = "Please, select another shipping method or call site administrator!"
                except CityNotFoundError as e:
                    self.errors += _("""Cant find destination city '%{title}s'
                                      to calculate charge.
                                      Errors: %{errors}s""" % {'title': e.title, 'errors': e.errors})
                    self.messages = _("""It seems like we cant find code
                                        for the city of destination (%s).
                                        Please, select it manually, choose
                                        another address or another shipping method.
                                    """) % e.title
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            lookup_url=lookup_url,
                                                            details_url=details_url)
                except TooManyFoundError as e:
                    self.errors += _("Found too many destinations for given city (%s)") % e.title
                    self.messages = _("Please refine your shipping address")
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            choices=e.results,
                                                            details_url=details_url)
                except CalculationError as e:
                    self.errors += _("""Error occured during charge
                                        calculation for given city (%s)""") % e.title
                    self.messages = _("API error was: %s") % e.errors
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            details_url=details_url,
                                                            lookup_url=lookup_url)
                except:
                    raise
                else:
                    (charge, self.messages,
                     self.errors, self.extra_form) = facade.parse_results(results,
                                                                            origin=self.origin,
                                                                            dest=self.destination,
                                                                            weight=weight,
                                                                            packs=packs)

        # Zero tax is assumed...
        return prices.Price(
            currency=basket.currency,
            excl_tax=charge,
            incl_tax=charge)

    def set_destination(self, addr):
        self.destination = addr

    class Meta(AbstractWeightBased.Meta):
        abstract = False
        app_label = 'shipping'
        verbose_name = _("API-based Shipping Method")
        verbose_name_plural = _("API-based Shipping Methods")

@python_2_unicode_compatible
class ShippingContainer(models.Model):
    name = models.CharField(_("Name"), max_length=128, unique=True)
    description = models.TextField(_("Description"), blank=True)
    image = models.ImageField(
        _("Image"), upload_to=settings.OSCAR_IMAGE_FOLDER, max_length=255, blank=True)
    height = models.DecimalField(
        _("Height, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    width = models.DecimalField(
        _("Width, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    lenght = models.DecimalField(
        _("Lenght, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    max_load = models.DecimalField(
        _("Max loading, kg"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])

    def __str__(self):
        return self.name

    @property
    def volume(self):
        return D(self.height*self.width*self.lenght).quantize(volume_precision)

    class Meta():
        app_label = 'shipping'
        verbose_name = _("Shipping Container")
        verbose_name_plural = _("Shipping Containers")    <commit_msg>Oops. Fix after. Do not commit untested code :(<commit_after># -*- coding: utf-8 -*-
from decimal import Decimal as D

import json
import importlib

from django.db import models
from django.conf import settings
from django.contrib import messages
from django.utils.html import format_html_join
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse_lazy
from django.template.loader import render_to_string

from oscar.apps.shipping.abstract_models import AbstractWeightBased
from oscar.core import prices, loading

from .packers import Packer
from .exceptions import ( OriginCityNotFoundError,
                          CityNotFoundError,
                          ApiOfflineError,
                          TooManyFoundError,
                          CalculationError)

weight_precision = getattr(settings, 'OSCAR_SHIPPING_WEIGHT_PRECISION', D('0.000'))
volume_precision = getattr(settings, 'OSCAR_SHIPPING_VOLUME_PRECISION', D('0.000'))

Scale = loading.get_class('shipping.scales', 'Scale')

DEFAULT_ORIGIN = getattr(settings, 'OSCAR_SHIPPING_DEFAULT_ORIGIN', u'Санкт-Петербург')

API_ENABLED = getattr(settings, 'OSCAR_SHIPPING_API_ENABLED', ['pecom', 'emspost'])

API_AVAILABLE = {'pecom': _('PEC API ver. 1.0'),
                 'emspost' :_('EMS Russian Post REST API'),
                 'dhl' : _('DHL API (not ready yet)'),
                 'usps' : _('USPS API (not ready yet)'),
                 }
ONLINE, OFFLINE, DISABLED = 'online','offline','disabled'
API_STATUS_CHOICES = (
    (ONLINE, _('Online')),
    (OFFLINE, _('Offline')),
    (DISABLED, _('Disabled')),
)

def get_api_modules():
    res = {}
    for name in API_AVAILABLE.keys():
        try:
            res[name] = importlib.import_module(".facade.%s" % name, __package__)
        except ImportError:
            pass
    return res

api_modules_pool = get_api_modules()

def get_enabled_api():
    #mods = api_modules_pool
    return [(a, API_AVAILABLE[a]) for a in (API_ENABLED and api_modules_pool.keys()) ]

class ShippingCompanyManager(models.Manager):
    def get_queryset(self):
        """
        Just return original queryset
        """
        return super(ShippingCompanyManager, self).get_queryset()

class AvailableCompanyManager(ShippingCompanyManager):

    def get_queryset(self):
        """
        Filter out inactive methods (shipping companies with outdated contracts etc)
        """
        return super(AvailableCompanyManager, self).get_queryset().filter(is_active=True)

    def for_address(self, addr):
        """
        Prepopulate destination field with the given address
        for charge calculating
        """
        methods = self.get_queryset()
        for m in methods:
            m.set_destination(addr)
        return methods

class ShippingCompany(AbstractWeightBased):
    """Shipping methods based on cargo companies APIs.
    """
    size_attributes = ('width' , 'height', 'lenght')

    destination = None # not stored field used for charge calculation

    errors = ''   # There is an issue with iterables and arrays as class properties and it scopes for class and instance
    messages = '' # so just a one string messages per calculate() method call

    api_user = models.CharField(_("API username"), max_length=64, blank=True)
    api_key = models.CharField(_("API key"), max_length=255, blank=True)
    api_type = models.CharField(verbose_name=_('API type'),
                                max_length=10,
                                choices=get_enabled_api(),
                                blank=True)
    origin = models.CharField(_("City of origin"), max_length=255, blank=True, default=DEFAULT_ORIGIN)
    is_active = models.BooleanField(_('active'), default=False,
                                        help_text=_('Use this method in checkout?'))
    status = models.CharField(verbose_name=_('status'),
                                max_length=10,
                                choices=API_STATUS_CHOICES,
                                blank=True)
    containers = models.ManyToManyField("ShippingContainer",
                                        blank=True,
                                        related_name='containers',
                                        verbose_name=_('Containers or boxes'),
                                        help_text=_('Containers or boxes could be used for packing')
                                        )

    objects = ShippingCompanyManager()
    available = AvailableCompanyManager()

    def __init__(self, *args, **kwargs):
        super(ShippingCompany, self).__init__(*args, **kwargs)
        if self.api_type:
            self.facade = api_modules_pool[self.api_type].ShippingFacade(self.api_user, self.api_key)

    def calculate(self, basket, options=None):

        results = []
        charge = D('0.0')
        self.messages = ''
        self.errors = ''
        # Note, when weighing the basket, we don't check whether the item
        # requires shipping or not.  It is assumed that if something has a
        # weight, then it requires shipping.
        scale = Scale(attribute_code=self.weight_attribute,
                      default_weight=self.default_weight)
        packer = Packer(self.containers,
                        attribute_codes=self.size_attributes,
                        weight_code=self.weight_attribute,
                        default_weight=self.default_weight)
        weight = scale.weigh_basket(basket)
        # Should be a list of dicts { 'weight': weight, 'container' : container }
        packs = packer.pack_basket(basket)
        facade = self.facade
        if not self.destination:
            self.errors+=_("ERROR! There is no shipping address for charge calculation!\n")
        else:
            self.messages+=_(u"""Approximated shipping price
                                for {weight} kg from {origin}
                                to {destination}\n""").format(weight=weight,
                                                            origin=self.origin,
                                                            destination=self.destination.city)

            # Assuming cases like http protocol suggests:
            # e=200  - OK. Result contains charge value and extra info such as Branch code, etc
            # e=404  - Result is empty, no destination found via API, redirect
            #          to address form or prompt to API city-codes selector
            # e=503  - API is offline. Skip this method.
            # e=300  - Too many choices found, Result contains list of charges-codes.
            #          Prompt to found dest-codes selector

            # an URL for AJAXed city-to-city charge lookup
            details_url = reverse_lazy('shipping:charge-details', kwargs={'slug': self.code})
            # an URL for AJAXed code by city lookup using Select2 widget
            lookup_url=reverse_lazy('shipping:city-lookup', kwargs={'slug': self.code})

            # if options set make a short call to API for final calculation
            if options:
                errors = None
                try:
                    results, errors = facade.get_charge(options['senderCityId'],
                                                options['receiverCityId'],
                                                packs)
                except CalculationError as e:
                    self.errors = "Post-calculation error: %s" % e.errors
                    self.messages = e.title
                except:
                    raise
                if not errors:
                    (charge, self.messages,
                     self.errors, self.extra_form) = facade.parse_results(results,
                                                                          options=options)
                else:
                    raise CalculationError("%s -> %s" % (options['senderCityId'],
                                                         options['receiverCityId']),
                                           errors)
            else:
                try:
                    results = facade.get_charges(weight, packs, self.origin, self.destination)
                except ApiOfflineError:
                    self.errors += _(u"""%s API is offline. Cant
                                        calculate anything. Sorry!""") % self.name
                    self.messages = _(u"Please, choose another shipping method!")
                except OriginCityNotFoundError as e:
                    # Paranoid mode as ImproperlyConfigured should be raised by facade
                    self.errors += _(u"""City of origin '%s' not found
                                      in the shipping company
                                      postcodes to calculate charge.""") % e.title
                    self.messages = _(u"""It seems like we couldnt find code
                                        for the city of origin (%s).
                                        Please, select it manually, choose another
                                        address or another shipping method.
                                    """) % e.title
                except ImproperlyConfigured as e: # upraised error handling
                    self.errors += "ImproperlyConfigured error (%s)" % e.message
                    self.messages = "Please, select another shipping method or call site administrator!"
                except CityNotFoundError as e:
                    self.errors += _(u"""Cant find destination city '{title}'
                                      to calculate charge.
                                      Errors: {errors}""").format(title=e.title, errors=e.errors)
                    self.messages = _(u"""It seems like we cant find code
                                        for the city of destination (%s).
                                        Please, select it manually, choose
                                        another address or another shipping method.
                                    """) % e.title
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            lookup_url=lookup_url,
                                                            details_url=details_url)
                except TooManyFoundError as e:
                    self.errors += _(u"Found too many destinations for given city (%s)") % e.title
                    self.messages = _("Please refine your shipping address")
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            choices=e.results,
                                                            details_url=details_url)
                except CalculationError as e:
                    self.errors += _(u"""Error occured during charge
                                        calculation for given city (%s)""") % e.title
                    self.messages = _(u"API error was: %s") % e.errors
                    self.extra_form = facade.get_extra_form(origin=self.origin,
                                                            details_url=details_url,
                                                            lookup_url=lookup_url)
                except:
                    raise
                else:
                    (charge, self.messages,
                     self.errors, self.extra_form) = facade.parse_results(results,
                                                                            origin=self.origin,
                                                                            dest=self.destination,
                                                                            weight=weight,
                                                                            packs=packs)

        # Zero tax is assumed...
        return prices.Price(
            currency=basket.currency,
            excl_tax=charge,
            incl_tax=charge)

    def set_destination(self, addr):
        self.destination = addr

    class Meta(AbstractWeightBased.Meta):
        abstract = False
        app_label = 'shipping'
        verbose_name = _("API-based Shipping Method")
        verbose_name_plural = _("API-based Shipping Methods")

@python_2_unicode_compatible
class ShippingContainer(models.Model):
    name = models.CharField(_("Name"), max_length=128, unique=True)
    description = models.TextField(_("Description"), blank=True)
    image = models.ImageField(
        _("Image"), upload_to=settings.OSCAR_IMAGE_FOLDER, max_length=255, blank=True)
    height = models.DecimalField(
        _("Height, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    width = models.DecimalField(
        _("Width, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    lenght = models.DecimalField(
        _("Lenght, m"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])
    max_load = models.DecimalField(
        _("Max loading, kg"), decimal_places=3, max_digits=12,
        validators=[MinValueValidator(D('0.00'))])

    def __str__(self):
        return self.name

    @property
    def volume(self):
        return D(self.height*self.width*self.lenght).quantize(volume_precision)

    class Meta():
        app_label = 'shipping'
        verbose_name = _("Shipping Container")
        verbose_name_plural = _("Shipping Containers")    <|endoftext|>


<commit_before>
<commit_msg>Spooky skeleton of Score<commit_after>import pygame, sys, math

class Score():
    __init__(self, pos):
<|endoftext|>


<commit_before><commit_msg>DOC: changed display message at the end<commit_after><|endoftext|>


<commit_before><commit_msg>fixed HVG input for R and moved R scripts<commit_after><|endoftext|>


<commit_before><commit_msg>twisted 16.4 fix for imports<commit_after><|endoftext|>


<commit_before>import pandas as pd
from sklearn.preprocessing import StandardScaler
import numpy as np
from matplotlib import pyplot as plt
import math

#Preugnta 1
data = pd.read_csv('data/TB.csv',sep=',',thousands=',', index_col = 0)
data.index.names = ['country']
data.columns.names = ['year']
X = data.ix[:,'1990':'2007'].values
X_std = StandardScaler().fit_transform(X)

fig, ax = plt.subplots(figsize=(8, 4))
data.loc[['Chile','Argentina','Bolivia','United States','Italy', 'United Kingdom', 'Sweden','Zimbabwe','Zambia','Cameroon', 'Congo, Rep,', 'Turkey', 'Singapore', 'Thailand', 'Australia', ''],'1990':].T.plot(ax=ax)
ax.legend(loc='upper left', bbox_to_anchor=(0.1, 1.1),prop={'size':'x-small'},ncol=6)
plt.tight_layout(pad=1.5)

#pregunta d
#Se calcula los vectores medios por columnas (son 22 columnas) y se calcula la matriz de la covarizana como el producto punto  X^T*X/n-1
mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0]-1)
#Se obtienen valores y vectores propios
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
#Se genera una lista con los valores propios asociadios a cada vector
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
eig_pairs.sort()
eig_pairs.reverse()
#Se obtiene un vector con los 4 valores propios mas grandes
var_exp=[]
tot = sum(eig_vals)
for i in range(0,4):
	var_exp.append(sorted(eig_vals, reverse=True)[i]*100/tot)

cum_var_exp = np.cumsum(var_exp)

#Grafico de las varianzas explicadas
plt.figure(figsize=(6, 4))
plt.bar(range(4), var_exp, alpha=0.5, align='center',
        label='individual explained variance')
plt.step(range(4), cum_var_exp, where='mid',
         label='cumulative explained variance')
plt.ylabel('Explained variance ratio')
plt.xlabel('Principal components')
plt.legend(loc='best')
plt.tight_layout()

#Se genera una matriz en que cada columna tiene los vectores propios asociados a los dos valores propios mas grandes,
matrix_w = np.hstack((eig_pairs[0][1].reshape(18,1),
                      eig_pairs[1][1].reshape(18,1)))

#Se hace el producto punto entre la matriz X normalizada y la matriz con la cantidad de vectores propios seleccionados
Y_sklearn = X_std.dot(matrix_w)

#Se hace un nuevo dataframe a partir de la matriz con solo dos variables en las cuales se proyectaron las 22
data_2d = pd.DataFrame(Y_sklearn)
data_2d.index = data.index
data_2d.columns = ['PC1','PC2']

#Pregunta e
#Media de los row
row_means = data_2d.mean(axis=1)

#Variacion de los row
row_trends = data_2d.diff(axis=1).mean(axis=1)
#grafico de las medias
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), c=row_means,cmap='hot')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')

#GRafico de la variacion
row_means = data.mean(axis=1)
row_trends = data.diff(axis=1).mean(axis=1)
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), c=row_trends,cmap='PRGn')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')

#Pregunta f
#El grafocp de burbujas
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), s=10*row_means, \
c=row_means,cmap='PRGn')

#Pregunta g
fig, ax = plt.subplots(figsize=(16,8))
row_means = data.mean(axis=1)
row_trends = data.diff(axis=1).mean(axis=1)
data_2d.plot(kind='scatter', x='PC1', y='PC2', ax=ax, s=10*row_means, c=row_means, \
cmap='PRGn')
Q3_HIV_world = data.mean(axis=1).quantile(q=0.85)
HIV_country = data.mean(axis=1)
names = data.index
for i, txt in enumerate(names):
	if(HIV_country[i]>Q3_HIV_world):
		ax.annotate(txt, (data_2d.iloc[i].PC1+0.2,data_2d.iloc[i].PC2))
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
<commit_msg>Update pca_2.py<commit_after>import pandas as pd
from sklearn.preprocessing import StandardScaler
import numpy as np
from matplotlib import pyplot as plt
import math

#Preugnta 1
data = pd.read_csv('data/TB.csv',sep=',',thousands=',', index_col = 0)
data.index.names = ['country']
data.columns.names = ['year']
X = data.ix[:,'1990':'2007'].values
X_std = StandardScaler().fit_transform(X)

fig, ax = plt.subplots(figsize=(8, 4))
data.loc[['Chile','Argentina','Bolivia','United States','Italy', 'United Kingdom', 'Sweden','Zimbabwe','Zambia','Cameroon', 'Congo, Rep,', 'Turkey', 'Singapore', 'Thailand', 'Australia', ''],'1990':].T.plot(ax=ax)
ax.legend(loc='upper left', bbox_to_anchor=(0.1, 1.1),prop={'size':'x-small'},ncol=6)
plt.tight_layout(pad=1.5)

#pregunta d
#Se calcula los vectores medios por columnas (son 18 columnas) y se calcula la matriz de la covarizana como el producto punto  X^T*X/n-1
mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0]-1)
#Se obtienen valores y vectores propios
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
#Se genera una lista con los valores propios asociadios a cada vector
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
eig_pairs.sort()
eig_pairs.reverse()
#Se obtiene un vector con los 4 valores propios mas grandes
var_exp=[]
tot = sum(eig_vals)
for i in range(0,4):
	var_exp.append(sorted(eig_vals, reverse=True)[i]*100/tot)

cum_var_exp = np.cumsum(var_exp)

#Grafico de las varianzas explicadas
plt.figure(figsize=(6, 4))
plt.bar(range(4), var_exp, alpha=0.5, align='center',
        label='individual explained variance')
plt.step(range(4), cum_var_exp, where='mid',
         label='cumulative explained variance')
plt.ylabel('Explained variance ratio')
plt.xlabel('Principal components')
plt.legend(loc='best')
plt.tight_layout()

#Se genera una matriz en que cada columna tiene los vectores propios asociados a los dos valores propios mas grandes,
matrix_w = np.hstack((eig_pairs[0][1].reshape(18,1),
                      eig_pairs[1][1].reshape(18,1)))

#Se hace el producto punto entre la matriz X normalizada y la matriz con la cantidad de vectores propios seleccionados
Y_sklearn = X_std.dot(matrix_w)

#Se hace un nuevo dataframe a partir de la matriz con solo dos variables en las cuales se proyectaron las 22
data_2d = pd.DataFrame(Y_sklearn)
data_2d.index = data.index
data_2d.columns = ['PC1','PC2']

#Pregunta e
#Media de los row
row_means = data_2d.mean(axis=1)

#Variacion de los row
row_trends = data_2d.diff(axis=1).mean(axis=1)
#grafico de las medias
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), c=row_means,cmap='hot')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')

#GRafico de la variacion
row_means = data.mean(axis=1)
row_trends = data.diff(axis=1).mean(axis=1)
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), c=row_trends,cmap='PRGn')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')

#Pregunta f
#El grafocp de burbujas
data_2d.plot(kind='scatter', x='PC1', y='PC2', figsize=(16,8), s=10*row_means, \
c=row_means,cmap='PRGn')

#Pregunta g
fig, ax = plt.subplots(figsize=(16,8))
row_means = data.mean(axis=1)
row_trends = data.diff(axis=1).mean(axis=1)
data_2d.plot(kind='scatter', x='PC1', y='PC2', ax=ax, s=10*row_means, c=row_means, \
cmap='PRGn')
Q3_HIV_world = data.mean(axis=1).quantile(q=0.85)
HIV_country = data.mean(axis=1)
names = data.index
for i, txt in enumerate(names):
	if(HIV_country[i]>Q3_HIV_world):
		ax.annotate(txt, (data_2d.iloc[i].PC1+0.2,data_2d.iloc[i].PC2))
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
<|endoftext|>


<commit_before># -*- coding: utf8 -*-

import config
import sqlite3

class Model(object):
	_db = None

	#public:
	@staticmethod
	def fetchAllRows(query, params):
		Model._connect()
		c = Model._db.cursor()
		result = []
		currentRow = {}
		nbCols = 0
		c.execute(query, params)

		#~ Get the columns names
		column_names = [d[0] for d in c.description]
		result = c.fetchall()
		resultList = list()
		for r in result:
			resultList.append(Model._createRow(r, column_names))

		return resultList

	@staticmethod
	def fetchOneRow(query, params):
		Model._connect()
		c = Model._db.cursor()
		result = dict()
		nbCols = 0
		c.execute(query, params)
		r = c.fetchone()

		#~ Get the colums names
		column_names = [d[0] for d in c.description]
		if r is not None:
			result = Model._createRow(r, column_names)

		return result

	@classmethod
	def save(cls, fields, where=list()):
		#~ insert
		if len(where) == 0:
			cls.insert(fields)
		else:  # update
			cls.update(fields, where)

	@classmethod
	def insert(cls, fields):
		Model._connect()
		c = Model._db.cursor()

		fields = cls.filterFields(fields)
		fieldsNames = map(lambda x: '"' + x + '"', fields.keys())
		values = ['?'] * len(fieldsNames)

		query = "INSERT INTO %s (%s) VALUES (%s)" % (
			cls.__module__, ','.join(fieldsNames), ','.join(values)
		)

		c.execute(query, fields.values())
		Model._db.commit()

		return c.lastrowid

	@classmethod
	def update(cls, fields, where):
		Model._connect()
		c = Model._db.cursor()

		fields = cls.filterFields(fields)
		fieldsNames = map(lambda x: '"' + x + '" = ?', fields.keys())

		query = "UPDATE %s SET %s WHERE %s" %\
			(cls.__module__, ','.join(fieldsNames), where[0])
		c.execute(query, fields.values() + where[1])
		Model._db.commit()

	@classmethod
	def delete(cls, where):
		Model._connect()
		c = Model._db.cursor()

		query = "DELETE FROM %(table)s WHERE %(where)s" %\
			{'table': cls.__module__, 'where': where[0]}
		r = c.execute(query, where[1])
		Model._db.commit()
		return r

	#protected:
	@staticmethod
	def _connect():
		if Model._db is None:
			Model._db = sqlite3.connect(config.db)

		return True

	@staticmethod
	def _createRow(sqliteRow, columns):
		row = {}
		for i, v in enumerate(sqliteRow):
			row[columns[i]] = v

		return row

	@classmethod
	def loadAll(cls, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
		" % {'fields': fields, 'table': cls.__module__}

		return Model.fetchAllRows(query, {})

	@classmethod
	def loadById(cls, id, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		table = cls.__module__
		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
			WHERE\
				%(where)s\
		" % {'fields': fields, 'table': table, 'where': 'id_' + table + ' = ?'}

		return Model.fetchOneRow(query, [id])

	@classmethod
	def loadBy(cls, filters, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		filters = cls.filterFields(filters)
		filtersNames = map(lambda x: '"' + x + '" = ?', filters.keys())

		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
			WHERE\
				%(where)s\
		" % {
			'fields': fields,
			'table': cls.__module__,
			'where': ' AND '.join(filtersNames)
		}

		return Model.fetchAllRows(query, filters.values())

	@classmethod
	def prepareFieldsForSelect(cls, fields=None):
		if not fields:
			fields = cls.fields

		if isinstance(fields, list):
			fields = ', '.join(fields)
		elif isinstance(fields, dict):
			fields = ', '.join(map(lambda x: fields[x] + ' AS ' + x, fields))
		elif not isinstance(fields, basestring):
			raise TypeError('Unexpected type of fields (%s)' % type(fields))

		return fields

	@classmethod
	def filterFields(cls, fields):
		return dict((k, fields[k]) for k in cls.fields if k in fields)
<commit_msg>query updated to be similar to the others<commit_after># -*- coding: utf8 -*-

import config
import sqlite3

class Model(object):
	_db = None

	#public:
	@staticmethod
	def fetchAllRows(query, params):
		Model._connect()
		c = Model._db.cursor()
		result = []
		currentRow = {}
		nbCols = 0
		c.execute(query, params)

		#~ Get the columns names
		column_names = [d[0] for d in c.description]
		result = c.fetchall()
		resultList = list()
		for r in result:
			resultList.append(Model._createRow(r, column_names))

		return resultList

	@staticmethod
	def fetchOneRow(query, params):
		Model._connect()
		c = Model._db.cursor()
		result = dict()
		nbCols = 0
		c.execute(query, params)
		r = c.fetchone()

		#~ Get the colums names
		column_names = [d[0] for d in c.description]
		if r is not None:
			result = Model._createRow(r, column_names)

		return result

	@classmethod
	def save(cls, fields, where=list()):
		#~ insert
		if len(where) == 0:
			cls.insert(fields)
		else:  # update
			cls.update(fields, where)

	@classmethod
	def insert(cls, fields):
		Model._connect()
		c = Model._db.cursor()

		fields = cls.filterFields(fields)
		fieldsNames = map(lambda x: '"' + x + '"', fields.keys())
		values = ['?'] * len(fieldsNames)

		query = "INSERT INTO %s (%s) VALUES (%s)" % (
			cls.__module__, ','.join(fieldsNames), ','.join(values)
		)

		c.execute(query, fields.values())
		Model._db.commit()

		return c.lastrowid

	@classmethod
	def update(cls, fields, where):
		Model._connect()
		c = Model._db.cursor()

		fields = cls.filterFields(fields)
		fieldsNames = map(lambda x: '"' + x + '" = ?', fields.keys())

		query = "UPDATE %(table)s SET %(values)s WHERE %(where)s" %\
			{'table': cls.__module__, 'values': ','.join(fieldsNames), 'where': where[0]}
		c.execute(query, fields.values() + where[1])
		Model._db.commit()

	@classmethod
	def delete(cls, where):
		Model._connect()
		c = Model._db.cursor()

		query = "DELETE FROM %(table)s WHERE %(where)s" %\
			{'table': cls.__module__, 'where': where[0]}
		r = c.execute(query, where[1])
		Model._db.commit()
		return r

	#protected:
	@staticmethod
	def _connect():
		if Model._db is None:
			Model._db = sqlite3.connect(config.db)

		return True

	@staticmethod
	def _createRow(sqliteRow, columns):
		row = {}
		for i, v in enumerate(sqliteRow):
			row[columns[i]] = v

		return row

	@classmethod
	def loadAll(cls, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
		" % {'fields': fields, 'table': cls.__module__}

		return Model.fetchAllRows(query, {})

	@classmethod
	def loadById(cls, id, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		table = cls.__module__
		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
			WHERE\
				%(where)s\
		" % {'fields': fields, 'table': table, 'where': 'id_' + table + ' = ?'}

		return Model.fetchOneRow(query, [id])

	@classmethod
	def loadBy(cls, filters, fields=None):
		fields = cls.prepareFieldsForSelect(fields)

		filters = cls.filterFields(filters)
		filtersNames = map(lambda x: '"' + x + '" = ?', filters.keys())

		query = "\
			SELECT\
				%(fields)s\
			FROM\
				%(table)s\
			WHERE\
				%(where)s\
		" % {
			'fields': fields,
			'table': cls.__module__,
			'where': ' AND '.join(filtersNames)
		}

		return Model.fetchAllRows(query, filters.values())

	@classmethod
	def prepareFieldsForSelect(cls, fields=None):
		if not fields:
			fields = cls.fields

		if isinstance(fields, list):
			fields = ', '.join(fields)
		elif isinstance(fields, dict):
			fields = ', '.join(map(lambda x: fields[x] + ' AS ' + x, fields))
		elif not isinstance(fields, basestring):
			raise TypeError('Unexpected type of fields (%s)' % type(fields))

		return fields

	@classmethod
	def filterFields(cls, fields):
		return dict((k, fields[k]) for k in cls.fields if k in fields)
<|endoftext|>