<commit_before># vim: fileencoding=utf-8

"""
Translation of v2
"""

# pylint: disable=line-too-long,bad-whitespace
V2_TRANSLATION = {
    "en": ("Weather report for:",           "Weather",  "Timezone",     "Now",      "Dawn",             "Sunrise",      "Zenith",       "Sunset",           "Dusk"),
    "af": ("Weer verslag vir:",             "", "", "", "", "", "", "", ""),
    "ar": ("تقرير حالة ألطقس",              "", "", "", "", "", "", "", ""),
    "az": ("Hava proqnozu:",                "", "", "", "", "", "", "", ""),
    "be": ("Прагноз надвор'я для:",         "", "", "", "", "", "", "", ""),
    "bg": ("Прогноза за времето в:",        "", "", "", "", "", "", "", ""),
    "bs": ("Vremenske prognoze za:",        "", "", "", "", "", "", "", ""),
    "ca": ("Informe del temps per a:",      "", "", "", "", "", "", "", ""),
    "cs": ("Předpověď počasí pro:",         "", "", "", "", "", "", "", ""),
    "cy": ("Adroddiad tywydd ar gyfer:",    "", "", "", "", "", "", "", ""),
    "da": ("Vejret i:",                     "", "", "", "", "", "", "", ""),
    "de": ("Wetterbericht für:",            "Wetter",   "Zeitzone",     "Jetzt",    "Morgendämmerung",  "Sonnenaufgang","Zenit",        "Sonnenuntergang",  "Abenddämmerung"),
    "el": ("Πρόγνωση καιρού για:",          "", "", "", "", "", "", "", ""),
    "eo": ("Veterprognozo por:",            "Vetero",   "Horzono",      "Nun",      "Tagiĝo",           "Sunleviĝo",    "Zenito",       "Sunsubiro",        "Krepusko"),
    "es": ("El tiempo en:",                 "", "", "", "", "", "", "", ""),
    "et": ("Ilmaprognoos:",                 "", "", "", "", "", "", "", ""),
    "fa": ("اوه و بآ تیعضو شرازگ",          "", "", "", "", "", "", "", ""),
    "fi": ("Säätiedotus:",                  "", "", "", "", "", "", "", ""),
    "fr": ("Prévisions météo pour:",        "", "", "", "", "", "", "", ""),
    "fy": ("Waarberjocht foar:",            "", "", "", "", "", "", "", ""),
    "ga": ("Réamhaisnéis na haimsire do:",  "", "", "", "", "", "", "", ""),
    "he": (":ריוואה גזמ תיזחת",             "", "", "", "", "", "", "", ""),
    "hr": ("Vremenska prognoza za:",        "", "", "", "", "", "", "", ""),
    "hu": ("Időjárás előrejelzés:",         "", "", "", "", "", "", "", ""),
    "hy": ("Եղանակի տեսություն:",           "", "", "", "", "", "", "", ""),
    "id": ("Prakiraan cuaca:",              "", "", "", "", "", "", "", ""),
    "it": ("Previsioni meteo:",             "", "", "", "", "", "", "", ""),
    "is": ("Veðurskýrsla fyrir:",           "", "", "", "", "", "", "", ""),
    "ja": ("天気予報：",                    "天気",     "タイムゾーン", "今",       "夜明け",           "日の出",       "天頂",         "日の入",           "日暮れ"),
    "jv": ("Weather forecast for:",         "", "", "", "", "", "", "", ""),
    "ka": ("ამინდის პროგნოზი:",             "", "", "", "", "", "", "", ""),
    "kk": ("Ауа райы:",                     "", "", "", "", "", "", "", ""),
    "ko": ("일기 예보：",                   "", "", "", "", "", "", "", ""),
    "ky": ("Аба ырайы:",                    "", "", "", "", "", "", "", ""),
    "lt": ("Orų prognozė:",                 "", "", "", "", "", "", "", ""),
    "lv": ("Laika ziņas:",                  "", "", "", "", "", "", "", ""),
    "mk": ("Прогноза за времето во:",       "", "", "", "", "", "", "", ""),
    "ml": ("കാലാവസ്ഥ റിപ്പോർട്ട്:",                 "", "", "", "", "", "", "", ""),
    "nb": ("Værmelding for:",               "", "", "", "", "", "", "", ""),
    "nl": ("Weerbericht voor:",             "", "", "", "", "", "", "", ""),
    "nn": ("Vêrmelding for:",               "", "", "", "", "", "", "", ""),
    "pl": ("Pogoda w:",                     "", "", "", "", "", "", "", ""),
    "pt": ("Previsão do tempo para:",       "", "", "", "", "", "", "", ""),
    "pt-br": ("Previsão do tempo para:",    "", "", "", "", "", "", "", ""),
    "ro": ("Prognoza meteo pentru:",        "", "", "", "", "", "", "", ""),
    "ru": ("Прогноз погоды:",               "Погода",   "Часовой пояс", "Сейчас",   "Рассвет",          "Восход",       "Зенит",        "Закат",            "Сумерки"),
    "sk": ("Predpoveď počasia pre:",        "", "", "", "", "", "", "", ""),
    "sl": ("Vremenska napoved za",          "", "", "", "", "", "", "", ""),
    "sr": ("Временска прогноза за:",        "", "", "", "", "", "", "", ""),
    "sr-lat": ("Vremenska prognoza za:",    "", "", "", "", "", "", "", ""),
    "sv": ("Väderleksprognos för:",         "", "", "", "", "", "", "", ""),
    "sw": ("Ripoti ya hali ya hewa, jiji la:", "", "", "", "", "", "", "", ""),
    "te": ("వాతావరణ సమాచారము:",                  "", "", "", "", "", "", "", ""),
    "th": ("รายงานสภาพอากาศ:",              "", "", "", "", "", "", "", ""),
    "tr": ("Hava beklentisi:",              "", "", "", "", "", "", "", ""),
    "uk": ("Прогноз погоди для:",           "Погода",   "Часовий пояс", "Зараз",    "Світанок",         "Схід сонця",   "Зеніт",        "Захід сонця",      "Сутінки"),
    "uz": ("Ob-havo bashorati:",            "", "", "", "", "", "", "", ""),
    "vi": ("Báo cáo thời tiết:",            "", "", "", "", "", "", "", ""),
    "zh": ("天氣預報：",                    "", "", "", "", "", "", "", ""),
    "zh-tw": ("天氣預報：",                 "天氣", "時區", "目前", "黎明", "日出", "日正當中", "日落", "黃昏"),
    "zu": ("Isimo sezulu:",                 "", "", "", "", "", "", "", ""),
    }
<commit_msg>Entered the dutch translations<commit_after># vim: fileencoding=utf-8

"""
Translation of v2
"""

# pylint: disable=line-too-long,bad-whitespace
V2_TRANSLATION = {
    "en": ("Weather report for:",           "Weather",  "Timezone",     "Now",      "Dawn",             "Sunrise",      "Zenith",       "Sunset",           "Dusk"),
    "af": ("Weer verslag vir:",             "", "", "", "", "", "", "", ""),
    "ar": ("تقرير حالة ألطقس",              "", "", "", "", "", "", "", ""),
    "az": ("Hava proqnozu:",                "", "", "", "", "", "", "", ""),
    "be": ("Прагноз надвор'я для:",         "", "", "", "", "", "", "", ""),
    "bg": ("Прогноза за времето в:",        "", "", "", "", "", "", "", ""),
    "bs": ("Vremenske prognoze za:",        "", "", "", "", "", "", "", ""),
    "ca": ("Informe del temps per a:",      "", "", "", "", "", "", "", ""),
    "cs": ("Předpověď počasí pro:",         "", "", "", "", "", "", "", ""),
    "cy": ("Adroddiad tywydd ar gyfer:",    "", "", "", "", "", "", "", ""),
    "da": ("Vejret i:",                     "", "", "", "", "", "", "", ""),
    "de": ("Wetterbericht für:",            "Wetter",   "Zeitzone",     "Jetzt",    "Morgendämmerung",  "Sonnenaufgang","Zenit",        "Sonnenuntergang",  "Abenddämmerung"),
    "el": ("Πρόγνωση καιρού για:",          "", "", "", "", "", "", "", ""),
    "eo": ("Veterprognozo por:",            "Vetero",   "Horzono",      "Nun",      "Tagiĝo",           "Sunleviĝo",    "Zenito",       "Sunsubiro",        "Krepusko"),
    "es": ("El tiempo en:",                 "", "", "", "", "", "", "", ""),
    "et": ("Ilmaprognoos:",                 "", "", "", "", "", "", "", ""),
    "fa": ("اوه و بآ تیعضو شرازگ",          "", "", "", "", "", "", "", ""),
    "fi": ("Säätiedotus:",                  "", "", "", "", "", "", "", ""),
    "fr": ("Prévisions météo pour:",        "", "", "", "", "", "", "", ""),
    "fy": ("Waarberjocht foar:",            "", "", "", "", "", "", "", ""),
    "ga": ("Réamhaisnéis na haimsire do:",  "", "", "", "", "", "", "", ""),
    "he": (":ריוואה גזמ תיזחת",             "", "", "", "", "", "", "", ""),
    "hr": ("Vremenska prognoza za:",        "", "", "", "", "", "", "", ""),
    "hu": ("Időjárás előrejelzés:",         "", "", "", "", "", "", "", ""),
    "hy": ("Եղանակի տեսություն:",           "", "", "", "", "", "", "", ""),
    "id": ("Prakiraan cuaca:",              "", "", "", "", "", "", "", ""),
    "it": ("Previsioni meteo:",             "", "", "", "", "", "", "", ""),
    "is": ("Veðurskýrsla fyrir:",           "", "", "", "", "", "", "", ""),
    "ja": ("天気予報：",                    "天気",     "タイムゾーン", "今",       "夜明け",           "日の出",       "天頂",         "日の入",           "日暮れ"),
    "jv": ("Weather forecast for:",         "", "", "", "", "", "", "", ""),
    "ka": ("ამინდის პროგნოზი:",             "", "", "", "", "", "", "", ""),
    "kk": ("Ауа райы:",                     "", "", "", "", "", "", "", ""),
    "ko": ("일기 예보：",                   "", "", "", "", "", "", "", ""),
    "ky": ("Аба ырайы:",                    "", "", "", "", "", "", "", ""),
    "lt": ("Orų prognozė:",                 "", "", "", "", "", "", "", ""),
    "lv": ("Laika ziņas:",                  "", "", "", "", "", "", "", ""),
    "mk": ("Прогноза за времето во:",       "", "", "", "", "", "", "", ""),
    "ml": ("കാലാവസ്ഥ റിപ്പോർട്ട്:",                 "", "", "", "", "", "", "", ""),
    "nb": ("Værmelding for:",               "", "", "", "", "", "", "", ""),
    "nl": ("Weerbericht voor:",             "Weer",     "Tijdzone",     "Nu",       "Dageraad",         "Zonsopkomst",  "Zenit",        "Zonsondergang",    "Schemering"),
    "nn": ("Vêrmelding for:",               "", "", "", "", "", "", "", ""),
    "pl": ("Pogoda w:",                     "", "", "", "", "", "", "", ""),
    "pt": ("Previsão do tempo para:",       "", "", "", "", "", "", "", ""),
    "pt-br": ("Previsão do tempo para:",    "", "", "", "", "", "", "", ""),
    "ro": ("Prognoza meteo pentru:",        "", "", "", "", "", "", "", ""),
    "ru": ("Прогноз погоды:",               "Погода",   "Часовой пояс", "Сейчас",   "Рассвет",          "Восход",       "Зенит",        "Закат",            "Сумерки"),
    "sk": ("Predpoveď počasia pre:",        "", "", "", "", "", "", "", ""),
    "sl": ("Vremenska napoved za",          "", "", "", "", "", "", "", ""),
    "sr": ("Временска прогноза за:",        "", "", "", "", "", "", "", ""),
    "sr-lat": ("Vremenska prognoza za:",    "", "", "", "", "", "", "", ""),
    "sv": ("Väderleksprognos för:",         "", "", "", "", "", "", "", ""),
    "sw": ("Ripoti ya hali ya hewa, jiji la:", "", "", "", "", "", "", "", ""),
    "te": ("వాతావరణ సమాచారము:",                  "", "", "", "", "", "", "", ""),
    "th": ("รายงานสภาพอากาศ:",              "", "", "", "", "", "", "", ""),
    "tr": ("Hava beklentisi:",              "", "", "", "", "", "", "", ""),
    "uk": ("Прогноз погоди для:",           "Погода",   "Часовий пояс", "Зараз",    "Світанок",         "Схід сонця",   "Зеніт",        "Захід сонця",      "Сутінки"),
    "uz": ("Ob-havo bashorati:",            "", "", "", "", "", "", "", ""),
    "vi": ("Báo cáo thời tiết:",            "", "", "", "", "", "", "", ""),
    "zh": ("天氣預報：",                    "", "", "", "", "", "", "", ""),
    "zh-tw": ("天氣預報：",                 "天氣", "時區", "目前", "黎明", "日出", "日正當中", "日落", "黃昏"),
    "zu": ("Isimo sezulu:",                 "", "", "", "", "", "", "", ""),
    }
<|endoftext|>


<commit_before><commit_msg>added get_embeddings, mapping symbols to embedding vectors<commit_after><|endoftext|>


<commit_before>'''
Created on Feb 15, 2011

@author: eplaster
'''
from suds.sudsobject import Property
import types, time
import logging
import urllib2
import lxml.etree as etree

from .base.base_entity import ManagedObjectReference, BaseEntity
from .base.data_object_types import DataObjectTypes
from .base.managed_object_types import ManagedObjectTypes
from .do.object_spec import ObjectSpec
from .do.traversal_spec import TraversalSpec
from .do.property_filter_spec import PropertyFilterSpec
from .do.property_spec import PropertySpec
from .do.selection_spec import SelectionSpec
from .do.retrieve_options import RetrieveOptions
from .enums.task_info_state import TaskInfoState
from .exceptions import VisdkTaskError
from .mo.service_instance import ServiceInstance
from .utils import camel_to_under
from brownie.importing import import_string

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

HTTP_TUNNEL = 'sdkTunnel:8089'

class VimBase(object):
    '''
    Base class to hold the nuts and bolts for the web services grunt work.
    '''

    def __init__(self, server, connect=True, verbose=3, certfile=None, keyfile=None):
        '''
        Constructor
        '''
        self.server = server
        self.verbose = verbose
        self.connected = False
        self.certfile = certfile
        self.keyfile = keyfile
        self.proxy = None
        if certfile and keyfile:
            self.server = HTTP_TUNNEL
            self.proxy = '%s:80' % server

        # setup logging...
        logging.getLogger('suds.client').setLevel(logging.INFO)
        logging.getLogger('suds.wsdl').setLevel(logging.INFO)
        logging.getLogger('suds.transport').setLevel(logging.INFO)
        logging.getLogger('suds.xsd.schema').setLevel(logging.INFO)

        if self.verbose > 5:
            logging.getLogger('suds.client').setLevel(logging.DEBUG)
            logging.getLogger('suds.transport').setLevel(logging.DEBUG)

        if connect:
            self.connect()

    def connect(self, timeout=1800):
        from .client import Client
        from .transport import HttpAuthenticated
        if self.connected:
            return

        self.client = Client(self.server, timeout=timeout)
        transport = HttpAuthenticated(proxy=self.proxy, certfile=self.certfile, keyfile=self.keyfile)
        self.client.client.set_options(transport=transport)
        # create the Service Instance managed object
        ref = Property('ServiceInstance')
        ref._type = 'ServiceInstance'
        self.service_instance = ServiceInstance(self, name='ServiceInstance', ref=ref)

        # get the service content
        self.service_content = self.service_instance.RetrieveServiceContent()
        self.property_collector = self.service_content.propertyCollector
        self.session_manager = self.service_content.sessionManager

        self.root = self.service_content.rootFolder
        self.connected = True

    def getVersions(self):
        versions = []

        def get_namespace_name(elem):
            for child in list(elem):
                if child.tag == "name":
                    return child.text

        url = urllib2.urlopen("https://" + self.server + "/sdk/vimServiceVersions.xml")
        root = etree.fromstring(url.read())
        names = root.findall(".//namespace")
        for namesp in names:
            if "urn:vim25" in get_namespace_name(namesp):
                versions = [x.text for x in namesp.findall(".//version")]
                log.debug("versions found: " + str(versions))
        return versions

    def getObjectProperties(self, mobj, properties, parent=None):
        """
        * Retrieve contents for a single object based on the property collector
        * registered with the service.
        *
        * @param mobj Managed Object Reference to get contents for
        * @param properties names of properties of object to retrieve
        * @param mo_instance: Managed Object instance to update
        *
        * @return retrieved object contents
        """
        all = False
        if not properties:
            all = True

        pspec = PropertySpec(self, all=all, type=mobj._type, pathSet=properties)
        ospec = ObjectSpec(self, obj=ManagedObjectReference(mobj._type, mobj.value), skip=False)
        spec = PropertyFilterSpec(self, propSet=[pspec], objectSet=[ospec])

        obj_content = self.property_collector.RetrieveProperties([spec])
        return self._parse_object_content(obj_content, parent=parent)

    def getDecendentsByName(self, _type, properties=["name"], name=None, root=None):
        """
         * Get the ManagedObjectReference for an item under the specified root
         * folder that has the type and name specified.
         *
         * @param _type
         *            type of the managed object
         * @param properties
         *            names of properties of object to retrieve
         * @param name
         *            name to match
         * @param root
         *            a root folder if available, or null for default
         *
         * @return First ManagedObjectReference of the type / name pair found
        """
        if not "name" in properties:
            properties += ["name"]
        ocary = self.getContentsRecursively(_type=str(_type), props=properties, root=root)

        if not name:
            return ocary
        else:
            for obj in ocary:
                if obj.name == name:
                    return obj

    def getContentsRecursively(self, props=[], _type=None, collector=None, root=None, recurse=True):
        if not collector:
            collector = self.property_collector
        if not root:
            root = self.root
        if not _type:
            _type = ManagedObjectTypes.ManagedEntity #@UndefinedVariable

        pspec = PropertySpec(self, type=str(_type), pathSet=props)
        typeinfo = [ pspec ]

        selectionSpecs = []
        if recurse:
            selectionSpecs = self._buildFullTraversal()

        spec = PropertyFilterSpec(self, propSet=typeinfo, objectSet=[ ObjectSpec(self, root.ref, selectSet=selectionSpecs) ])
        obj_content = self.property_collector.RetrieveProperties([spec])
        return self._parse_object_content(obj_content)

    def _parse_object_content(self, obj_content, parent=None):
        rv = None
        class_name = obj_content.__class__.__name__
        class_name_upper = class_name[0].upper() + class_name[1:]
        if isinstance(obj_content, list):
            rv = []
            for obj in obj_content:
                rv.append(self._parse_object_content(obj, parent))

        elif type(obj_content) == types.NoneType:
            return obj_content

        elif issubclass(obj_content.__class__, BaseEntity):
            rv = obj_content

        elif class_name == 'ObjectContent':
            # create the managed object
            if not parent:
                rv = self._create_mo_obj(class_name, obj_content, parent)
            else:
                rv = []
                # now, run through the propSet
                if hasattr(obj_content, "propSet"):
                    for prop in obj_content.propSet:
                        rv.append(self._parse_object_content(prop.val))
                if len(rv) == 1:
                    rv = rv[0]

        elif class_name == "ManagedObjectReference":
            # managed object reference is too generic, so we get to the real type
            mo_f = import_string('pyvisdk.mo.%s.%s' % (camel_to_under(obj_content._type), obj_content._type))
            rv = mo_f(self, ref=obj_content)

        elif class_name in DataObjectTypes:
            rv = self._create_do_obj(class_name, obj_content, parent)

        elif class_name == 'Text':
            # obj_context is instance of suds.sax.text.Text which is a subclass of unicode
            # we need to return the text in proper encoding
            rv = str(obj_content.encode('UTF-8'))

        elif class_name in ['long', 'bool', 'int', 'datetime', 'str']:
            return obj_content

        elif "ArrayOf" in class_name:
            rv = []
            _type = class_name[7:]
            if not hasattr(obj_content, _type):
                _type = _type.lower()
            _list = getattr(obj_content, _type)
            for obj in _list:
                rv.append(self._parse_object_content(obj, parent))

        elif hasattr(obj_content, 'value') and hasattr(obj_content, '_type'):
            name = obj_content._type
            mod_name = 'pyvisdk.mo.%s' % camel_to_under(name)
            mo = import_string('%s.%s' % (mod_name, name))
            rv = mo(self, ref=obj_content)

        # TODO: ArrayOf....  ArrayOfManagedObjectReference ArrayOfAlarmState ArrayOfString ArrayOfInt ArrayOfPermission
        # TODO: val datetime datastore  entity host vm key

        elif class_name_upper in DataObjectTypes:
            rv = self._create_do_obj(class_name_upper, obj_content, parent)

        #elif class_name_upper in ManagedObjectTypes:
        #    rv = self._create_mo_obj(class_name_upper, obj_content, parent)

        else:
            log.warning("Unknown content type: %s" % class_name)
            rv = obj_content

        return rv

    def _create_do_obj(self, class_name, obj_content, parent):
        mod_name = 'pyvisdk.do.%s' % camel_to_under(class_name)
        do = import_string('%s.%s' % (mod_name, class_name))

        kwargs = {}
        for attr_name in filter(lambda x: not x.startswith('_'), dir(obj_content)):
            attr_data = eval('obj_content.%s' % attr_name)
            kwargs[attr_name] = self._parse_object_content(attr_data, parent=do)

        rv = do(self, **kwargs)
        return rv

    def _create_mo_obj(self, class_name, obj_content, parent):
        mo_f = import_string('pyvisdk.mo.%s.%s' % (camel_to_under(obj_content.obj._type), obj_content.obj._type))
        rv = mo_f(self, ref=obj_content.obj)

        # now, run through the propSet
        #if hasattr(obj_content, "propSet"):
        #    for prop in obj_content.propSet:
        #        # for each prop of the new object, create any data/managed objects
        #        setattr(rv, prop.name, self._parse_object_content(prop.val, parent=rv))
        return rv

    def _buildFullTraversal(self):
        """
         * This method creates a SelectionSpec[] to traverses the entire inventory
         * tree starting at a Folder
         *
         * @return The SelectionSpec[]
        """
        # Recurse through all ResourcePools
        rpToRp = TraversalSpec(self, name="rpToRp", type=ManagedObjectTypes.ResourcePool, path="resourcePool", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="rpToRp"), SelectionSpec(self, name="rpToVm") ])

        # Recurse through all ResourcePools
        rpToVm = TraversalSpec(self, name="rpToVm", type=ManagedObjectTypes.ResourcePool, path="vm") #@UndefinedVariable

        # Traversal through ResourcePool branch
        crToRp = TraversalSpec(self, name="crToRp", type=ManagedObjectTypes.ComputeResource, path="resourcePool", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="rpToRp"), SelectionSpec(self, name="rpToVm") ])

        # Traversal through host branch
        crToH = TraversalSpec(self, name="crToH", type=ManagedObjectTypes.ComputeResource, path="host") #@UndefinedVariable

        # Traversal through hostFolder branch
        dcToHf = TraversalSpec(self, name="dcToHf", type=ManagedObjectTypes.Datacenter, path="hostFolder", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Traversal through vmFolder branch
        dcToVmf = TraversalSpec(self, name="dcToVmf", type=ManagedObjectTypes.Datacenter, path="vmFolder", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Recurse through all Hosts
        HToVm = TraversalSpec(self, name="HToVm", type=ManagedObjectTypes.HostSystem, path="vm", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Recurse through the folders
        visitFolders = TraversalSpec(self, name="visitFolders", type=ManagedObjectTypes.Folder, path="childEntity", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="visitFolders"),
                                     SelectionSpec(self, name="dcToHf"),
                                     SelectionSpec(self, name="dcToVmf"),
                                     SelectionSpec(self, name="crToH"),
                                     SelectionSpec(self, name="crToRp"),
                                     SelectionSpec(self, name="HToVm"),
                                     SelectionSpec(self, name="rpToVm"), ])

        return (visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm)

    #############################################################
    # untested...
    #############################################################
    def callRetrievePropertiesEx(self, maxObjects=0):
        myPropSpec = PropertySpec(self, all=False, type=ManagedObjectTypes.VirtualMachine, pathSet=["name"]) #@UndefinedVariable

        myObjSpec = ObjectSpec(self, self.root.ref, selectSet=self._buildFullTraversal())

        pSpec = PropertyFilterSpec(self, propSet=[myPropSpec], objectSet=[myObjSpec])

        rOptions = RetrieveOptions(int(maxObjects))

        retrieveResult = self.property_collector.RetrieveProperties([pSpec], rOptions)

        #objContentArrayList = Arrays.asList(retrieveResult.getObjects())
        #if(retrieveResult.getToken() != null && maxObjects == null) {
        #   callContinueRetrieveProperties(retrieveResult.getToken())
        #}
        #ObjectContent [] objectContentArray = (ObjectContent [])objContentArrayList.toArray()
        #for(int i=0; i<objectContentArray.length; i++) {
        #   System.out.println("VM Managed Object Reference Value: " + objectContentArray[i].getObj().get_value());
        #}
        return retrieveResult

    #############################################################
    #* Illustrating how to create, use and destroy additional property collectors
    #* This allows multiple modules to create their own property filter and process updates independently.
    #* Also applow to get time-sensitive updated being monitored on one collector,
    #* while a large updatyed being monitored by another.
    #############################################################
    def callCreatePropertyCollectorEx(self):
        collector = self.property_collector.CreatePropertyCollector()

        if self.verbose > 5:
            log.debug(str(collector))

        pSpec = PropertyFilterSpec(self,
                propSet=[
                    PropertySpec(self, type=ManagedObjectTypes.VirtualMachine, all=False, pathSet=["configIssue", "configStatus", "name", "parent"]) #@UndefinedVariable
                ],
                objectSet=[
                    ObjectSpec(self, self.root.ref, selectSet=self._buildFullTraversal())
                ])

        rOptions = RetrieveOptions(maxObjects=0)

        retrieveResult = collector.RetrievePropertiesEx(pSpec, rOptions)
        collector.DestroyPropertyCollector()
        return retrieveResult

    def update(self, _type="", root=None, properties=[], version="", wait_time=2):
        if not root:
            root = self.root

        pSpec = PropertyFilterSpec(self,
                propSet=[PropertySpec(self, type=_type, pathSet=properties)],
                objectSet=[ObjectSpec(self, root.ref)])

        filter = self.property_collector.CreateFilter(pSpec, partialUpdates=False)

        changeData = self.property_collector.CheckForUpdates(version=version)

        # Destroy the filter when we are done.
        filter.DestroyPropertyFilter()

        if hasattr(changeData, "filterSet"):
            changeData = changeData.filterSet[0].objectSet[0]

        if hasattr(changeData, "changeSet"):
            changeData = changeData.changeSet

        return changeData, version

    #############################################################
    # end untested...
    #############################################################

    def waitForTask(self, objmor):
        version = ""

        objmor = ManagedObjectReference("Task", objmor.value)

        myObjSpec = ObjectSpec(self, objmor)
        myPropSpec = PropertySpec(self, type=objmor._type, pathSet=["info.state", "info.error"])
        pSpec = PropertyFilterSpec(self, propSet=[myPropSpec], objectSet=[myObjSpec])

        filter = self.property_collector.CreateFilter(pSpec, partialUpdates=True)

        updateset = self.property_collector.WaitForUpdates(version)

        status = self._parseTaskResponse(updateset)
        while status['info.state'] in ['running', 'queued']: #@UndefinedVariable
            log.debug("Waiting for task to complete...")

            version = updateset.version
            updateset = self.property_collector.WaitForUpdates(version)
            status = self._parseTaskResponse(updateset)
            log.debug("**** status: %s" % status)
            time.sleep(0.1)

        log.debug("Finished task...")
        # Destroy the filter when we are done.
        filter.DestroyPropertyFilter()

        if status['info.state'] == TaskInfoState.error: #@UndefinedVariable
            error = status['info.error']
            raise VisdkTaskError(error.localizedMessage)
        return status['info.state']

    def _parseTaskResponse(self, response):
        status = {}
        for x in response.filterSet[0].objectSet[0].changeSet:
            if hasattr(x, 'val'):
                status[x.name] = x.val
            else:
                status[x.name] = x.op
        return status

<commit_msg>FIX #10<commit_after>'''
Created on Feb 15, 2011

@author: eplaster
'''
from suds.sudsobject import Property
import types, time
import logging
import urllib2
import lxml.etree as etree

from .base.base_entity import ManagedObjectReference, BaseEntity
from .base.data_object_types import DataObjectTypes
from .base.managed_object_types import ManagedObjectTypes
from .do.object_spec import ObjectSpec
from .do.traversal_spec import TraversalSpec
from .do.property_filter_spec import PropertyFilterSpec
from .do.property_spec import PropertySpec
from .do.selection_spec import SelectionSpec
from .do.retrieve_options import RetrieveOptions
from .enums.task_info_state import TaskInfoState
from .exceptions import VisdkTaskError
from .mo.service_instance import ServiceInstance
from .utils import camel_to_under
from brownie.importing import import_string

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

HTTP_TUNNEL = 'sdkTunnel:8089'

class VimBase(object):
    '''
    Base class to hold the nuts and bolts for the web services grunt work.
    '''

    def __init__(self, server, connect=True, verbose=3, certfile=None, keyfile=None):
        '''
        Constructor
        '''
        self.server = server
        self.verbose = verbose
        self.connected = False
        self.certfile = certfile
        self.keyfile = keyfile
        self.proxy = None
        if certfile and keyfile:
            self.server = HTTP_TUNNEL
            self.proxy = '%s:80' % server

        # setup logging...
        logging.getLogger('suds.client').setLevel(logging.INFO)
        logging.getLogger('suds.wsdl').setLevel(logging.INFO)
        logging.getLogger('suds.transport').setLevel(logging.INFO)
        logging.getLogger('suds.xsd.schema').setLevel(logging.INFO)

        if self.verbose > 5:
            logging.getLogger('suds.client').setLevel(logging.DEBUG)
            logging.getLogger('suds.transport').setLevel(logging.DEBUG)

        if connect:
            self.connect()

    def connect(self, timeout=1800):
        from .client import Client
        from .transport import HttpAuthenticated
        if self.connected:
            return

        self.client = Client(self.server, timeout=timeout)
        transport = HttpAuthenticated(proxy=self.proxy, certfile=self.certfile, keyfile=self.keyfile)
        self.client.client.set_options(transport=transport)
        # create the Service Instance managed object
        ref = Property('ServiceInstance')
        ref._type = 'ServiceInstance'
        self.service_instance = ServiceInstance(self, name='ServiceInstance', ref=ref)

        # get the service content
        self.service_content = self.service_instance.RetrieveServiceContent()
        self.property_collector = self.service_content.propertyCollector
        self.session_manager = self.service_content.sessionManager

        self.root = self.service_content.rootFolder
        self.connected = True

    def getVersions(self):
        versions = []

        def get_namespace_name(elem):
            for child in list(elem):
                if child.tag == "name":
                    return child.text

        url = urllib2.urlopen("https://" + self.server + "/sdk/vimServiceVersions.xml")
        root = etree.fromstring(url.read())
        names = root.findall(".//namespace")
        for namesp in names:
            if "urn:vim25" in get_namespace_name(namesp):
                versions = [x.text for x in namesp.findall(".//version")]
                log.debug("versions found: " + str(versions))
        return versions

    def getObjectProperties(self, mobj, properties, parent=None):
        """
        * Retrieve contents for a single object based on the property collector
        * registered with the service.
        *
        * @param mobj Managed Object Reference to get contents for
        * @param properties names of properties of object to retrieve
        * @param mo_instance: Managed Object instance to update
        *
        * @return retrieved object contents
        """
        all = False
        if not properties:
            all = True

        pspec = PropertySpec(self, all=all, type=mobj._type, pathSet=properties)
        ospec = ObjectSpec(self, obj=ManagedObjectReference(mobj._type, mobj.value), skip=False)
        spec = PropertyFilterSpec(self, propSet=[pspec], objectSet=[ospec])

        obj_content = self.property_collector.RetrieveProperties([spec])
        return self._parse_object_content(obj_content, parent=parent)

    def getDecendentsByName(self, _type, properties=["name"], name=None, root=None):
        """
         * Get the ManagedObjectReference for an item under the specified root
         * folder that has the type and name specified.
         *
         * @param _type
         *            type of the managed object
         * @param properties
         *            names of properties of object to retrieve
         * @param name
         *            name to match
         * @param root
         *            a root folder if available, or null for default
         *
         * @return First ManagedObjectReference of the type / name pair found
        """
        if not "name" in properties:
            properties += ["name"]
        ocary = self.getContentsRecursively(_type=str(_type), props=properties, root=root)

        if not name:
            return ocary
        else:
            for obj in ocary:
                if obj.name == name:
                    return obj

    def getContentsRecursively(self, props=[], _type=None, collector=None, root=None, recurse=True):
        if not collector:
            collector = self.property_collector
        if not root:
            root = self.root
        if not _type:
            _type = ManagedObjectTypes.ManagedEntity #@UndefinedVariable

        pspec = PropertySpec(self, type=str(_type), pathSet=props)
        typeinfo = [ pspec ]

        selectionSpecs = []
        if recurse:
            selectionSpecs = self._buildFullTraversal()

        spec = PropertyFilterSpec(self, propSet=typeinfo, objectSet=[ ObjectSpec(self, root.ref, selectSet=selectionSpecs) ])
        obj_content = self.property_collector.RetrieveProperties([spec])
        return self._parse_object_content(obj_content)

    def _parse_object_content(self, obj_content, parent=None):
        rv = None
        class_name = obj_content.__class__.__name__
        class_name_upper = class_name[0].upper() + class_name[1:]
        if isinstance(obj_content, list):
            rv = []
            for obj in obj_content:
                rv.append(self._parse_object_content(obj, parent))

        elif type(obj_content) == types.NoneType:
            return obj_content

        elif issubclass(obj_content.__class__, BaseEntity):
            rv = obj_content

        elif class_name == 'ObjectContent':
            # create the managed object
            if not parent:
                rv = self._create_mo_obj(class_name, obj_content, parent)
            else:
                rv = []
                # now, run through the propSet
                if hasattr(obj_content, "propSet"):
                    for prop in obj_content.propSet:
                        rv.append(self._parse_object_content(prop.val))
                if len(rv) == 1:
                    rv = rv[0]

        elif class_name == "ManagedObjectReference":
            # managed object reference is too generic, so we get to the real type
            mo_f = import_string('pyvisdk.mo.%s.%s' % (camel_to_under(obj_content._type), obj_content._type))
            rv = mo_f(self, ref=obj_content)

        elif class_name in DataObjectTypes:
            rv = self._create_do_obj(class_name, obj_content, parent)

        elif class_name == 'Text':
            # obj_context is instance of suds.sax.text.Text which is a subclass of unicode
            # we need to return the text in proper encoding
            rv = str(obj_content.encode('UTF-8'))

        elif class_name in ['long', 'bool', 'int', 'datetime', 'str']:
            return obj_content

        elif "ArrayOf" in class_name:
            rv = []
            _type = class_name[7:]
            if not hasattr(obj_content, _type):
                _type = _type.lower()
            _list = getattr(obj_content, _type)
            for obj in _list:
                rv.append(self._parse_object_content(obj, parent))

        elif hasattr(obj_content, 'value') and hasattr(obj_content, '_type'):
            name = obj_content._type
            mod_name = 'pyvisdk.mo.%s' % camel_to_under(name)
            mo = import_string('%s.%s' % (mod_name, name))
            rv = mo(self, ref=obj_content)

        # TODO: ArrayOf....  ArrayOfManagedObjectReference ArrayOfAlarmState ArrayOfString ArrayOfInt ArrayOfPermission
        # TODO: val datetime datastore  entity host vm key

        elif class_name_upper in DataObjectTypes:
            rv = self._create_do_obj(class_name_upper, obj_content, parent)

        #elif class_name_upper in ManagedObjectTypes:
        #    rv = self._create_mo_obj(class_name_upper, obj_content, parent)

        else:
            log.warning("Unknown content type: %s" % class_name)
            rv = obj_content

        return rv

    def _create_do_obj(self, class_name, obj_content, parent):
        mod_name = 'pyvisdk.do.%s' % camel_to_under(class_name)
        do = import_string('%s.%s' % (mod_name, class_name))

        kwargs = {}
        for attr_name in filter(lambda x: not x.startswith('_'), dir(obj_content)):
            attr_data = eval('obj_content.%s' % attr_name)
            kwargs[attr_name] = self._parse_object_content(attr_data, parent=do)

        rv = do(self, **kwargs)
        return rv

    def _create_mo_obj(self, class_name, obj_content, parent):
        mo_f = import_string('pyvisdk.mo.%s.%s' % (camel_to_under(obj_content.obj._type), obj_content.obj._type))
        rv = mo_f(self, ref=obj_content.obj)

        # now, run through the propSet
        #if hasattr(obj_content, "propSet"):
        #    for prop in obj_content.propSet:
        #        # for each prop of the new object, create any data/managed objects
        #        setattr(rv, prop.name, self._parse_object_content(prop.val, parent=rv))
        return rv

    def _buildFullTraversal(self):
        """
         * This method creates a SelectionSpec[] to traverses the entire inventory
         * tree starting at a Folder
         *
         * @return The SelectionSpec[]
        """
        # Recurse through all ResourcePools
        rpToRp = TraversalSpec(self, name="rpToRp", type=ManagedObjectTypes.ResourcePool, path="resourcePool", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="rpToRp"), SelectionSpec(self, name="rpToVm") ])

        # Recurse through all ResourcePools
        rpToVm = TraversalSpec(self, name="rpToVm", type=ManagedObjectTypes.ResourcePool, path="vm") #@UndefinedVariable

        # Traversal through ResourcePool branch
        crToRp = TraversalSpec(self, name="crToRp", type=ManagedObjectTypes.ComputeResource, path="resourcePool", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="rpToRp"), SelectionSpec(self, name="rpToVm") ])

        # Traversal through host branch
        crToH = TraversalSpec(self, name="crToH", type=ManagedObjectTypes.ComputeResource, path="host") #@UndefinedVariable

        # Traversal through hostFolder branch
        dcToHf = TraversalSpec(self, name="dcToHf", type=ManagedObjectTypes.Datacenter, path="hostFolder", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Traversal through vmFolder branch
        dcToVmf = TraversalSpec(self, name="dcToVmf", type=ManagedObjectTypes.Datacenter, path="vmFolder", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Recurse through all Hosts
        HToVm = TraversalSpec(self, name="HToVm", type=ManagedObjectTypes.HostSystem, path="vm", #@UndefinedVariable
                        selectSet=[SelectionSpec(self, name="visitFolders")])

        # Recurse through the folders
        visitFolders = TraversalSpec(self, name="visitFolders", type=ManagedObjectTypes.Folder, path="childEntity", #@UndefinedVariable
                        selectSet=[ SelectionSpec(self, name="visitFolders"),
                                     SelectionSpec(self, name="dcToHf"),
                                     SelectionSpec(self, name="dcToVmf"),
                                     SelectionSpec(self, name="crToH"),
                                     SelectionSpec(self, name="crToRp"),
                                     SelectionSpec(self, name="HToVm"),
                                     SelectionSpec(self, name="rpToVm"), ])

        return (visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm)

    def update(self, _type="", root=None, properties=[], version="", wait_time=2):
        if not root:
            root = self.root

        pSpec = PropertyFilterSpec(self,
                propSet=[PropertySpec(self, type=_type, pathSet=properties)],
                objectSet=[ObjectSpec(self, root.ref)])

        filter = self.property_collector.CreateFilter(pSpec, partialUpdates=False)

        changeData = self.property_collector.CheckForUpdates(version=version)

        # Destroy the filter when we are done.
        filter.DestroyPropertyFilter()

        if hasattr(changeData, "filterSet"):
            changeData = changeData.filterSet[0].objectSet[0]

        if hasattr(changeData, "changeSet"):
            changeData = changeData.changeSet

        return changeData, version

    #############################################################
    # end untested...
    #############################################################

    def waitForTask(self, objmor):
        version = ""

        objmor = ManagedObjectReference("Task", objmor.value)

        myObjSpec = ObjectSpec(self, objmor)
        myPropSpec = PropertySpec(self, type=objmor._type, pathSet=["info.state", "info.error"])
        pSpec = PropertyFilterSpec(self, propSet=[myPropSpec], objectSet=[myObjSpec])

        filter = self.property_collector.CreateFilter(pSpec, partialUpdates=True)

        updateset = self.property_collector.WaitForUpdates(version)

        status = self._parseTaskResponse(updateset)
        while status['info.state'] in ['running', 'queued']: #@UndefinedVariable
            log.debug("Waiting for task to complete...")

            version = updateset.version
            updateset = self.property_collector.WaitForUpdates(version)
            status = self._parseTaskResponse(updateset)
            log.debug("**** status: %s" % status)
            time.sleep(0.1)

        log.debug("Finished task...")
        # Destroy the filter when we are done.
        filter.DestroyPropertyFilter()

        if status['info.state'] == TaskInfoState.error: #@UndefinedVariable
            error = status['info.error']
            raise VisdkTaskError(error.localizedMessage)
        return status['info.state']

    def _parseTaskResponse(self, response):
        status = {}
        for x in response.filterSet[0].objectSet[0].changeSet:
            if hasattr(x, 'val'):
                status[x.name] = x.val
            else:
                status[x.name] = x.op
        return status

<|endoftext|>


<commit_before># qav (Question Answer Validation)
# Copyright (C) 2015 UMIACS

__version__ = "0.3.2"
<commit_msg>Tagging 0.3.3<commit_after># qav (Question Answer Validation)
# Copyright (C) 2015 UMIACS

__version__ = "0.3.3"
<|endoftext|>


<commit_before>"""Quick and Dirty TensorFlow command framework"""

from .flag import *
from .infer import def_infer
from .train_and_evaluate import def_train_and_evaluate

__all__ = ["FLAGS", "add_flag", "add_required_flag", "FlagAdder",
           "def_train_and_evaluate", "def_infer"]
__version__ = "0.0.15"
<commit_msg>Bump version<commit_after>"""Quick and Dirty TensorFlow command framework"""

from .flag import *
from .infer import def_infer
from .train_and_evaluate import def_train_and_evaluate

__all__ = ["FLAGS", "add_flag", "add_required_flag", "FlagAdder",
           "def_train_and_evaluate", "def_infer"]
__version__ = "0.0.16"
<|endoftext|>


<commit_before>#     Copyright 2016, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" XML node tree handling

Means to create XML elements from Nuitka tree nodes and to convert the
XML tree to ASCII or output it.
"""

from nuitka.PythonVersions import python_version

from . import Tracing

def indent(elem, level = 0, more_sibs = False):
    i = '\n'
    if level:
        i += (level-1) * "  "
    num_kids = len(elem)
    if num_kids:
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
            if level:
                elem.text += "  "
        count = 0
        for kid in elem:
            indent(kid, level+1, count < num_kids - 1)
            count += 1
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
            if more_sibs:
                elem.tail += "  "
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i
            if more_sibs:
                elem.tail += "  "

    return elem

try:
    import lxml.etree # pylint: disable=F0401,I0021
    xml_module = lxml.etree

    Element = xml_module.Element
    xml_tostring = lambda tree: lxml.etree.tostring(tree, pretty_print = True)
except ImportError:
    try:
        import xml.etree.ElementTree
        xml_module = xml.etree.ElementTree

        Element = xml.etree.ElementTree.Element
        xml_tostring = lambda tree: xml_module.tostring(indent(tree))

    except ImportError:
        xml_module = None
        Element = None
        xml_tostring = None

def toString(tree):
    result = xml_tostring(tree)

    if python_version >= 300:
        result = result.decode("utf-8")

    return result

def dump(tree):
    value = toString(tree).rstrip()

    Tracing.printLine(value)
<commit_msg>Persistence: Added stream writer objects<commit_after>#     Copyright 2016, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
""" XML node tree handling

Means to create XML elements from Nuitka tree nodes and to convert the
XML tree to ASCII or output it.
"""

from nuitka.PythonVersions import python_version

from . import Tracing

def indent(elem, level = 0, more_sibs = False):
    i = '\n'
    if level:
        i += (level-1) * "  "
    num_kids = len(elem)
    if num_kids:
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
            if level:
                elem.text += "  "
        count = 0
        for kid in elem:
            indent(kid, level+1, count < num_kids - 1)
            count += 1
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
            if more_sibs:
                elem.tail += "  "
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i
            if more_sibs:
                elem.tail += "  "

    return elem

try:
    import lxml.etree # pylint: disable=F0401,I0021
    xml_module = lxml.etree

    Element = xml_module.Element
    xml_tostring = lambda tree: lxml.etree.tostring(tree, pretty_print = True)
except ImportError:
    try:
        import xml.etree.ElementTree
        xml_module = xml.etree.ElementTree

        Element = xml.etree.ElementTree.Element
        xml_tostring = lambda tree: xml_module.tostring(indent(tree))
    except ImportError:
        xml_module = None
        Element = None
        xml_tostring = None

try:
    import lxml.xmlfile

    xml_writer = lxml.xmlfile
except ImportError:
    xml_writer = None

def toString(tree):
    result = xml_tostring(tree)

    if python_version >= 300:
        result = result.decode("utf-8")

    return result

def dump(tree):
    value = toString(tree).rstrip()

    Tracing.printLine(value)
<|endoftext|>


<commit_before>"""
From NumbaPro

"""
from __future__ import print_function, division, absolute_import

import dis
import inspect
import sys
from types import CodeType, ModuleType
from collections import namedtuple

from numba import errors, utils
from numba.config import PYVERSION

opcode_info = namedtuple('opcode_info', ['argsize'])

def get_function_object(obj):
    """
    Objects that wraps function should provide a "__numba__" magic attribute
    that contains a name of an attribute that contains the actual python
    function object.
    """
    attr = getattr(obj, "__numba__", None)
    if attr:
        return getattr(obj, attr)
    return obj

def get_code_object(obj):
    "Shamelessly borrowed from llpython"
    return getattr(obj, '__code__', getattr(obj, 'func_code', None))

def _make_bytecode_table():
    # Note some opcodes are supported here for analysis but not later
    # in the compilation pipeline.
    version_specific = []

    if sys.version_info[0] == 2:
        version_specific += [
            ('BINARY_DIVIDE', 0),
            ('DELETE_SLICE+0', 0),
            ('DELETE_SLICE+1', 0),
            ('DELETE_SLICE+2', 0),
            ('DELETE_SLICE+3', 0),
            ('DUP_TOPX', 2),
            ('INPLACE_DIVIDE', 0),
            ('PRINT_ITEM', 0),
            ('PRINT_NEWLINE', 0),
            ('ROT_FOUR', 0),
            ('SLICE+0', 0),
            ('SLICE+1', 0),
            ('SLICE+2', 0),
            ('SLICE+3', 0),
            ('STORE_SLICE+0', 0),
            ('STORE_SLICE+1', 0),
            ('STORE_SLICE+2', 0),
            ('STORE_SLICE+3', 0),
        ]

    if sys.version_info[0] == 3:
        version_specific += [
            ('DUP_TOP_TWO', 0)
        ]

    if sys.version_info[:2] <= (3, 4):
        version_specific += [
            ('STORE_MAP', 0),
        ]

    if sys.version_info[:2] >= (3, 5):   # python 3.5+
        version_specific += [
            ('BINARY_MATRIX_MULTIPLY', 0),
            ('INPLACE_MATRIX_MULTIPLY', 0),
        ]

    bytecodes = [
        # opname, operandlen
        ('BINARY_ADD', 0),
        ('BINARY_TRUE_DIVIDE', 0),
        ('BINARY_MULTIPLY', 0),
        ('BINARY_SUBSCR', 0),
        ('BINARY_SUBTRACT', 0),
        ('BINARY_FLOOR_DIVIDE', 0),
        ('BINARY_MODULO', 0),
        ('BINARY_POWER', 0),
        ('BINARY_AND', 0),
        ('BINARY_OR', 0),
        ('BINARY_XOR', 0),
        ('BINARY_LSHIFT', 0),
        ('BINARY_RSHIFT', 0),
        ('BREAK_LOOP', 0),
        ('BUILD_LIST', 2),
        ('BUILD_MAP', 2),
        ('BUILD_SET', 2),
        ('BUILD_SLICE', 2),
        ('BUILD_TUPLE', 2),
        ('CALL_FUNCTION', 2),
        ('CALL_FUNCTION_VAR', 2),
        ('COMPARE_OP', 2),
        ('DELETE_ATTR', 2),
        ('DELETE_SUBSCR', 0),
        ('DUP_TOP', 0),
        ('EXTENDED_ARG', 2),
        ('FOR_ITER', 2),
        ('GET_ITER', 0),
        ('INPLACE_ADD', 0),
        ('INPLACE_SUBTRACT', 0),
        ('INPLACE_MULTIPLY', 0),
        ('INPLACE_TRUE_DIVIDE', 0),
        ('INPLACE_FLOOR_DIVIDE', 0),
        ('INPLACE_MODULO', 0),
        ('INPLACE_POWER', 0),
        ('INPLACE_AND', 0),
        ('INPLACE_OR', 0),
        ('INPLACE_XOR', 0),
        ('INPLACE_LSHIFT', 0),
        ('INPLACE_RSHIFT', 0),
        ('JUMP_ABSOLUTE', 2),
        ('JUMP_FORWARD', 2),
        ('JUMP_IF_TRUE_OR_POP', 2),
        ('JUMP_IF_FALSE_OR_POP', 2),
        ('LOAD_ATTR', 2),
        ('LOAD_CLOSURE', 2),
        ('LOAD_CONST', 2),
        ('LOAD_FAST', 2),
        ('LOAD_GLOBAL', 2),
        ('LOAD_DEREF', 2),
        ('MAKE_CLOSURE', 2),
        ('MAKE_FUNCTION', 2),
        ('POP_BLOCK', 0),
        ('POP_JUMP_IF_FALSE', 2),
        ('POP_JUMP_IF_TRUE', 2),
        ('POP_TOP', 0),
        ('RAISE_VARARGS', 2),
        ('RETURN_VALUE', 0),
        ('ROT_THREE', 0),
        ('ROT_TWO', 0),
        ('SETUP_LOOP', 2),
        ('STORE_ATTR', 2),
        ('STORE_DEREF', 2),
        ('STORE_FAST', 2),
        ('STORE_SUBSCR', 0),
        ('UNARY_POSITIVE', 0),
        ('UNARY_NEGATIVE', 0),
        ('UNARY_INVERT', 0),
        ('UNARY_NOT', 0),
        ('UNPACK_SEQUENCE', 2),
        ('YIELD_VALUE', 0),
    ] + version_specific

    return dict((dis.opmap[opname], opcode_info(argsize=argsize))
                for opname, argsize in bytecodes)

def _as_opcodes(seq):
    lst = []
    for s in seq:
        c = dis.opmap.get(s)
        if c is not None:
            lst.append(c)
    return lst

BYTECODE_TABLE = _make_bytecode_table()

JREL_OPS = frozenset(dis.hasjrel)
JABS_OPS = frozenset(dis.hasjabs)
JUMP_OPS = JREL_OPS | JABS_OPS
TERM_OPS = frozenset(_as_opcodes(['RETURN_VALUE', 'RAISE_VARARGS']))
EXTENDED_ARG = dis.EXTENDED_ARG

class ByteCodeInst(object):
    '''
    Attributes
    ----------
    - offset:
        byte offset of opcode
    - opcode:
        opcode integer value
    - arg:
        instruction arg
    - lineno:
        -1 means unknown
    '''
    __slots__ = 'offset', 'next', 'opcode', 'opname', 'arg', 'lineno'

    def __init__(self, offset, opcode, arg):
        self.offset = offset
        self.next = offset + BYTECODE_TABLE[opcode].argsize + 1
        self.opcode = opcode
        self.opname = dis.opname[opcode]
        self.arg = arg
        self.lineno = -1  # unknown line number

    @classmethod
    def get(cls, offset, opname, arg):
        return cls(offset, dis.opmap[opname], arg)

    @property
    def is_jump(self):
        return self.opcode in JUMP_OPS

    @property
    def is_terminator(self):
        return self.opcode in TERM_OPS

    def get_jump_target(self):
        assert self.is_jump
        if self.opcode in JREL_OPS:
            return self.next + self.arg
        else:
            assert self.opcode in JABS_OPS
            return self.arg

    def __repr__(self):
        return '%s(arg=%s, lineno=%d)' % (self.opname, self.arg, self.lineno)

    @property
    def block_effect(self):
        """Effect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        """
        if self.opname.startswith('SETUP_'):
            return 1
        elif self.opname == 'POP_BLOCK':
            return -1
        else:
            return 0

class ByteCodeIter(object):
    def __init__(self, code):
        self.code = code
        if PYVERSION > (3, 0):
            self.iter = enumerate(self.code.co_code)
        else:
            self.iter = ((i, ord(x)) for i, x in enumerate(self.code.co_code))

    def __iter__(self):
        return self

    def _fetch_opcode(self):
        offset, opcode = next(self.iter)
        try:
            info = BYTECODE_TABLE[opcode]
        except KeyError:
            ts = "offset=%d opcode=0x%x opname=%s"
            tv = offset, opcode, dis.opname[opcode]
            raise NotImplementedError(ts % tv)
        if info.argsize:
            arg = self.read_arg(info.argsize)
        else:
            arg = None
        return offset, opcode, arg

    def next(self):
        offset, opcode, arg = self._fetch_opcode()
        if opcode == EXTENDED_ARG:
            hi_arg = arg
            offset, opcode, lo_arg = self._fetch_opcode()
            arg = (hi_arg << 16) + lo_arg
        return offset, ByteCodeInst(offset=offset, opcode=opcode, arg=arg)

    __next__ = next

    def read_arg(self, size):
        buf = 0
        for i in range(size):
            _offset, byte = next(self.iter)
            buf |= byte << (8 * i)
        return buf

class ByteCodeOperation(object):
    def __init__(self, inst, args):
        self.inst = inst
        self.args = args

class ByteCodeBase(object):
    __slots__ = (
        'func', 'func_name', 'func_qualname', 'filename',
        'pysig', 'co_names', 'co_varnames', 'co_consts', 'co_freevars',
        'table', 'labels', 'arg_count', 'arg_names', 'used_globals',
        )

    def __init__(self, func_id, co_names, co_varnames, co_consts, co_freevars,
                 table, labels):
        self.func_id = func_id
        self.co_names = co_names
        self.co_varnames = co_varnames
        self.co_consts = co_consts
        self.co_freevars = co_freevars
        self.table = table
        self.labels = labels

    def __iter__(self):
        return utils.itervalues(self.table)

    def __getitem__(self, offset):
        return self.table[offset]

    def __contains__(self, offset):
        return offset in self.table

    def dump(self):
        def label_marker(i):
            if i[1].offset in self.labels:
                return '>'
            else:
                return ' '

        return '\n'.join('%s %10s\t%s' % ((label_marker(i),) + i)
                         for i in utils.iteritems(self.table))

    @classmethod
    def _compute_used_globals(cls, func, table, co_consts, co_names):
        """
        Compute the globals used by the function with the given
        bytecode table.
        """
        d = {}
        globs = func.__globals__
        builtins = globs.get('__builtins__', utils.builtins)
        if isinstance(builtins, ModuleType):
            builtins = builtins.__dict__
        # Look for LOAD_GLOBALs in the bytecode
        for inst in table.values():
            if inst.opname == 'LOAD_GLOBAL':
                name = co_names[inst.arg]
                if name not in d:
                    try:
                        value = globs[name]
                    except KeyError:
                        value = builtins[name]
                    d[name] = value
        # Add globals used by any nested code object
        for co in co_consts:
            if isinstance(co, CodeType):
                subtable = utils.SortedMap(ByteCodeIter(co))
                d.update(cls._compute_used_globals(func, subtable,
                                                   co.co_consts, co.co_names))
        return d

    def get_used_globals(self):
        """
        Get a {name: value} map of the globals used by this code
        object and any nested code objects.
        """
        return self._compute_used_globals(self.func_id.func, self.table,
                                          self.co_consts, self.co_names)

class ByteCode(ByteCodeBase):
    def __init__(self, func_id):
        code = func_id.code

        # A map of {offset: ByteCodeInst}
        table = utils.SortedMap(ByteCodeIter(code))
        labels = set(dis.findlabels(code.co_code))
        labels.add(0)

        self._mark_lineno(table, func_id.code)
        super(ByteCode, self).__init__(func_id=func_id,
                                       co_names=code.co_names,
                                       co_varnames=code.co_varnames,
                                       co_consts=code.co_consts,
                                       co_freevars=code.co_freevars,
                                       table=table,
                                       labels=list(sorted(labels)))

    @classmethod
    def _mark_lineno(cls, table, code):
        '''Fill the lineno info for all bytecode inst
        '''
        for offset, lineno in dis.findlinestarts(code):
            if offset in table:
                table[offset].lineno = lineno
        known = -1
        for inst in table.values():
            if inst.lineno >= 0:
                known = inst.lineno
            else:
                inst.lineno = known

class FunctionIdentity(object):
    """
    A function's identity and metadata.
    """

    @classmethod
    def from_function(cls, pyfunc):
        """
        Create the FunctionIdentity of the given function.
        """
        func = get_function_object(pyfunc)
        code = get_code_object(func)
        pysig = utils.pysignature(func)
        if not code:
            raise errors.ByteCodeSupportError(
                "%s does not provide its bytecode" % func)

        try:
            func_qualname = func.__qualname__
        except AttributeError:
            func_qualname = func.__name__

        self = cls()
        self.func = pyfunc
        self.func_qualname = func_qualname
        self.func_name = func_qualname.split('.')[-1]
        self.code = code
        self.module = inspect.getmodule(func)
        self.is_generator = inspect.isgeneratorfunction(func)
        self.pysig = pysig
        self.filename = code.co_filename
        self.firstlineno = code.co_firstlineno
        self.arg_count = len(pysig.parameters)
        self.arg_names = list(pysig.parameters)

        return self
<commit_msg>Refactor some bytecode handling<commit_after>"""
From NumbaPro

"""
from __future__ import print_function, division, absolute_import

from collections import namedtuple, OrderedDict
import dis
import inspect
import sys
from types import CodeType, ModuleType

from numba import errors, utils
from numba.config import PYVERSION

opcode_info = namedtuple('opcode_info', ['argsize'])

def get_function_object(obj):
    """
    Objects that wraps function should provide a "__numba__" magic attribute
    that contains a name of an attribute that contains the actual python
    function object.
    """
    attr = getattr(obj, "__numba__", None)
    if attr:
        return getattr(obj, attr)
    return obj

def get_code_object(obj):
    "Shamelessly borrowed from llpython"
    return getattr(obj, '__code__', getattr(obj, 'func_code', None))

def _make_bytecode_table():
    # Note some opcodes are supported here for analysis but not later
    # in the compilation pipeline.
    version_specific = []

    if sys.version_info[0] == 2:
        version_specific += [
            ('BINARY_DIVIDE', 0),
            ('DELETE_SLICE+0', 0),
            ('DELETE_SLICE+1', 0),
            ('DELETE_SLICE+2', 0),
            ('DELETE_SLICE+3', 0),
            ('DUP_TOPX', 2),
            ('INPLACE_DIVIDE', 0),
            ('PRINT_ITEM', 0),
            ('PRINT_NEWLINE', 0),
            ('ROT_FOUR', 0),
            ('SLICE+0', 0),
            ('SLICE+1', 0),
            ('SLICE+2', 0),
            ('SLICE+3', 0),
            ('STORE_SLICE+0', 0),
            ('STORE_SLICE+1', 0),
            ('STORE_SLICE+2', 0),
            ('STORE_SLICE+3', 0),
        ]

    if sys.version_info[0] == 3:
        version_specific += [
            ('DUP_TOP_TWO', 0)
        ]

    if sys.version_info[:2] <= (3, 4):
        version_specific += [
            ('STORE_MAP', 0),
        ]

    if sys.version_info[:2] >= (3, 5):   # python 3.5+
        version_specific += [
            ('BINARY_MATRIX_MULTIPLY', 0),
            ('INPLACE_MATRIX_MULTIPLY', 0),
        ]

    bytecodes = [
        # opname, operandlen
        ('BINARY_ADD', 0),
        ('BINARY_TRUE_DIVIDE', 0),
        ('BINARY_MULTIPLY', 0),
        ('BINARY_SUBSCR', 0),
        ('BINARY_SUBTRACT', 0),
        ('BINARY_FLOOR_DIVIDE', 0),
        ('BINARY_MODULO', 0),
        ('BINARY_POWER', 0),
        ('BINARY_AND', 0),
        ('BINARY_OR', 0),
        ('BINARY_XOR', 0),
        ('BINARY_LSHIFT', 0),
        ('BINARY_RSHIFT', 0),
        ('BREAK_LOOP', 0),
        ('BUILD_LIST', 2),
        ('BUILD_MAP', 2),
        ('BUILD_SET', 2),
        ('BUILD_SLICE', 2),
        ('BUILD_TUPLE', 2),
        ('CALL_FUNCTION', 2),
        ('CALL_FUNCTION_VAR', 2),
        ('COMPARE_OP', 2),
        ('DELETE_ATTR', 2),
        ('DELETE_SUBSCR', 0),
        ('DUP_TOP', 0),
        ('EXTENDED_ARG', 2),
        ('FOR_ITER', 2),
        ('GET_ITER', 0),
        ('INPLACE_ADD', 0),
        ('INPLACE_SUBTRACT', 0),
        ('INPLACE_MULTIPLY', 0),
        ('INPLACE_TRUE_DIVIDE', 0),
        ('INPLACE_FLOOR_DIVIDE', 0),
        ('INPLACE_MODULO', 0),
        ('INPLACE_POWER', 0),
        ('INPLACE_AND', 0),
        ('INPLACE_OR', 0),
        ('INPLACE_XOR', 0),
        ('INPLACE_LSHIFT', 0),
        ('INPLACE_RSHIFT', 0),
        ('JUMP_ABSOLUTE', 2),
        ('JUMP_FORWARD', 2),
        ('JUMP_IF_TRUE_OR_POP', 2),
        ('JUMP_IF_FALSE_OR_POP', 2),
        ('LOAD_ATTR', 2),
        ('LOAD_CLOSURE', 2),
        ('LOAD_CONST', 2),
        ('LOAD_FAST', 2),
        ('LOAD_GLOBAL', 2),
        ('LOAD_DEREF', 2),
        ('MAKE_CLOSURE', 2),
        ('MAKE_FUNCTION', 2),
        ('POP_BLOCK', 0),
        ('POP_JUMP_IF_FALSE', 2),
        ('POP_JUMP_IF_TRUE', 2),
        ('POP_TOP', 0),
        ('RAISE_VARARGS', 2),
        ('RETURN_VALUE', 0),
        ('ROT_THREE', 0),
        ('ROT_TWO', 0),
        ('SETUP_LOOP', 2),
        ('STORE_ATTR', 2),
        ('STORE_DEREF', 2),
        ('STORE_FAST', 2),
        ('STORE_SUBSCR', 0),
        ('UNARY_POSITIVE', 0),
        ('UNARY_NEGATIVE', 0),
        ('UNARY_INVERT', 0),
        ('UNARY_NOT', 0),
        ('UNPACK_SEQUENCE', 2),
        ('YIELD_VALUE', 0),
    ] + version_specific

    return dict((dis.opmap[opname], opcode_info(argsize=argsize))
                for opname, argsize in bytecodes)

def _as_opcodes(seq):
    lst = []
    for s in seq:
        c = dis.opmap.get(s)
        if c is not None:
            lst.append(c)
    return lst

BYTECODE_TABLE = _make_bytecode_table()

JREL_OPS = frozenset(dis.hasjrel)
JABS_OPS = frozenset(dis.hasjabs)
JUMP_OPS = JREL_OPS | JABS_OPS
TERM_OPS = frozenset(_as_opcodes(['RETURN_VALUE', 'RAISE_VARARGS']))
EXTENDED_ARG = dis.EXTENDED_ARG

class ByteCodeInst(object):
    '''
    Attributes
    ----------
    - offset:
        byte offset of opcode
    - opcode:
        opcode integer value
    - arg:
        instruction arg
    - lineno:
        -1 means unknown
    '''
    __slots__ = 'offset', 'next', 'opcode', 'opname', 'arg', 'lineno'

    def __init__(self, offset, opcode, arg):
        self.offset = offset
        self.next = offset + BYTECODE_TABLE[opcode].argsize + 1
        self.opcode = opcode
        self.opname = dis.opname[opcode]
        self.arg = arg
        self.lineno = -1  # unknown line number

    @classmethod
    def get(cls, offset, opname, arg):
        return cls(offset, dis.opmap[opname], arg)

    @property
    def is_jump(self):
        return self.opcode in JUMP_OPS

    @property
    def is_terminator(self):
        return self.opcode in TERM_OPS

    def get_jump_target(self):
        assert self.is_jump
        if self.opcode in JREL_OPS:
            return self.next + self.arg
        else:
            assert self.opcode in JABS_OPS
            return self.arg

    def __repr__(self):
        return '%s(arg=%s, lineno=%d)' % (self.opname, self.arg, self.lineno)

    @property
    def block_effect(self):
        """Effect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        """
        if self.opname.startswith('SETUP_'):
            return 1
        elif self.opname == 'POP_BLOCK':
            return -1
        else:
            return 0

class ByteCodeIter(object):
    def __init__(self, code):
        self.code = code
        if PYVERSION > (3, 0):
            self.iter = enumerate(self.code.co_code)
        else:
            self.iter = ((i, ord(x)) for i, x in enumerate(self.code.co_code))

    def __iter__(self):
        return self

    def _fetch_opcode(self):
        offset, opcode = next(self.iter)
        try:
            info = BYTECODE_TABLE[opcode]
        except KeyError:
            ts = "offset=%d opcode=0x%x opname=%s"
            tv = offset, opcode, dis.opname[opcode]
            raise NotImplementedError(ts % tv)
        if info.argsize:
            arg = self.read_arg(info.argsize)
        else:
            arg = None
        return offset, opcode, arg

    def next(self):
        offset, opcode, arg = self._fetch_opcode()
        if opcode == EXTENDED_ARG:
            hi_arg = arg
            offset, opcode, lo_arg = self._fetch_opcode()
            arg = (hi_arg << 16) + lo_arg
        return offset, ByteCodeInst(offset=offset, opcode=opcode, arg=arg)

    __next__ = next

    def read_arg(self, size):
        buf = 0
        for i in range(size):
            _offset, byte = next(self.iter)
            buf |= byte << (8 * i)
        return buf

class ByteCode(object):
    """
    The decoded bytecode of a function, and related information.
    """
    __slots__ = ('func_id', 'co_names', 'co_varnames', 'co_consts',
                 'co_freevars', 'table', 'labels')

    def __init__(self, func_id):
        code = func_id.code

        labels = set(dis.findlabels(code.co_code))
        labels.add(0)

        # A map of {offset: ByteCodeInst}
        table = OrderedDict(ByteCodeIter(code))
        self._compute_lineno(table, code)

        self.func_id = func_id
        self.co_names = code.co_names
        self.co_varnames = code.co_varnames
        self.co_consts = code.co_consts
        self.co_freevars = code.co_freevars
        self.table = table
        self.labels = sorted(labels)

    @classmethod
    def _compute_lineno(cls, table, code):
        """
        Compute the line numbers for all bytecode instructions.
        """
        for offset, lineno in dis.findlinestarts(code):
            if offset in table:
                table[offset].lineno = lineno
        known = -1
        for inst in table.values():
            if inst.lineno >= 0:
                known = inst.lineno
            else:
                inst.lineno = known
        return table

    def __iter__(self):
        return utils.itervalues(self.table)

    def __getitem__(self, offset):
        return self.table[offset]

    def __contains__(self, offset):
        return offset in self.table

    def dump(self):
        def label_marker(i):
            if i[1].offset in self.labels:
                return '>'
            else:
                return ' '

        return '\n'.join('%s %10s\t%s' % ((label_marker(i),) + i)
                         for i in utils.iteritems(self.table))

    @classmethod
    def _compute_used_globals(cls, func, table, co_consts, co_names):
        """
        Compute the globals used by the function with the given
        bytecode table.
        """
        d = {}
        globs = func.__globals__
        builtins = globs.get('__builtins__', utils.builtins)
        if isinstance(builtins, ModuleType):
            builtins = builtins.__dict__
        # Look for LOAD_GLOBALs in the bytecode
        for inst in table.values():
            if inst.opname == 'LOAD_GLOBAL':
                name = co_names[inst.arg]
                if name not in d:
                    try:
                        value = globs[name]
                    except KeyError:
                        value = builtins[name]
                    d[name] = value
        # Add globals used by any nested code object
        for co in co_consts:
            if isinstance(co, CodeType):
                subtable = OrderedDict(ByteCodeIter(co))
                d.update(cls._compute_used_globals(func, subtable,
                                                   co.co_consts, co.co_names))
        return d

    def get_used_globals(self):
        """
        Get a {name: value} map of the globals used by this code
        object and any nested code objects.
        """
        return self._compute_used_globals(self.func_id.func, self.table,
                                          self.co_consts, self.co_names)

class FunctionIdentity(object):
    """
    A function's identity and metadata.
    """

    @classmethod
    def from_function(cls, pyfunc):
        """
        Create the FunctionIdentity of the given function.
        """
        func = get_function_object(pyfunc)
        code = get_code_object(func)
        pysig = utils.pysignature(func)
        if not code:
            raise errors.ByteCodeSupportError(
                "%s does not provide its bytecode" % func)

        try:
            func_qualname = func.__qualname__
        except AttributeError:
            func_qualname = func.__name__

        self = cls()
        self.func = pyfunc
        self.func_qualname = func_qualname
        self.func_name = func_qualname.split('.')[-1]
        self.code = code
        self.module = inspect.getmodule(func)
        self.is_generator = inspect.isgeneratorfunction(func)
        self.pysig = pysig
        self.filename = code.co_filename
        self.firstlineno = code.co_firstlineno
        self.arg_count = len(pysig.parameters)
        self.arg_names = list(pysig.parameters)

        return self
<|endoftext|>


<commit_before>#This file is part of QuTIP.
#
#    QuTIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#    QuTIP 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with QuTIP.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011, Paul D. Nation & Robert J. Johansson
#
###########################################################################

from scipy import *

# ------------------------------------------------------------------------------
# Write matrix data to a file
#
def file_data_store(datafile, data, numtype="complex", numformat="decimal", sep=","):

    if datafile == None or data == None:
        raise ValueError("Datafile or data is unspecified")

    M,N = shape(data)

    f = open(datafile, "w")

    f.write("# Generated by QuTiP: %dx%d %s matrix in %s format ['%s' separated values].\n" % (M, N, numtype, numformat, sep))

    if numtype == "complex":

        if numformat == "exp":

            for m in range(M):
                for n in range(N):
                    if imag(data[m,n]) >= 0.0:
                        f.write("%.10e+%.10ej" % (real(data[m,n]),imag(data[m,n])))
                    else:
                        f.write("%.10e%.10ej" % (real(data[m,n]),imag(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        elif numformat == "decimal":

            for m in range(M):
                for n in range(N):
                    if imag(data[m,n]) >= 0.0:
                        f.write("%.10f+%.10fj" % (real(data[m,n]),imag(data[m,n])))
                    else:
                        f.write("%.10f%.10fj" % (real(data[m,n]),imag(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        else:
            raise ValueError("Illegal numformat value (should be 'exp' or 'decimal')")

    elif numtype == "real":

        if numformat == "exp":

            for m in range(M):
                for n in range(N):
                    f.write("%.10e" % (real(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        elif numformat == "decimal":

            for m in range(M):
                for n in range(N):
                    f.write("%.10f" % (real(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        else:
            raise ValueError("Illegal numformat value (should be 'exp' or 'decimal')")

    else:
        raise ValueError("Illegal numtype value (should be 'complex' or 'real')")

    f.close()

# ------------------------------------------------------------------------------
# Read matrix data from a file
#
def file_data_read(datafile, sep=None):

    if datafile == None:
        raise ValueError("datafile is unspecified")

    f = open(datafile, "r")

    #
    # first count lines and numbers of
    #
    M = N = 0
    for line in f:
        # skip comment lines
        if line[0] == '#' or line[0] == '%':
            continue
        # find delim
        if N == 0 and sep == None:
            if len(line.rstrip().split(",")) > 1:
                sep = ","
            elif len(line.rstrip().split(";")) > 1:
                sep = ";"
            elif len(line.rstrip().split(":")) > 1:
                sep = ":"
            elif len(line.rstrip().split("|")) > 1:
                sep = "|"
            elif len(line.rstrip().split()) > 1:
                sep = None # sepical case for a mix of white space deliminators
            else:
                raise ValueError("Unrecognized column deliminator")
        # split the line
        line_vec = line.split(sep)
        n = len(line_vec)
        if N == 0 and n > 0:
            N = n
            # check type
            if ("j" in line_vec[0]) or ("i" in line_vec[0]):
                numtype = "complex"
            else:
                numtype = "real"

            # check format
            if ("e" in line_vec[0]) or ("E" in line_vec[0]):
                numformat = "exp"
            else:
                numformat = "decimal"

        elif N != n:
            raise ValueError("Badly formatted data file: unequal number of columns")
        M += 1

    #
    # read data and store in a matrix
    #
    f.seek(0)

    if numtype == "complex":
        data = zeros((M,N), dtype="complex")
        m = n = 0
        for line in f:
            # skip comment lines
            if line[0] == '#' or line[0] == '%':
                continue
            n = 0
            for item in line.rstrip().split(sep):
                data[m,n] = complex(item)
                n += 1
            m += 1

    else:
        data = zeros((M,N), dtype="float")
        m = n = 0
        for line in f:
            # skip comment lines
            if line[0] == '#' or line[0] == '%':
                continue
            n = 0
            for item in line.rstrip().split(sep):
                data[m,n] = float(item)
                n += 1
            m += 1

    f.close()

    return data
<commit_msg>added qsave and qload functions that save and load stuff, respectively, from a given file.<commit_after>#This file is part of QuTIP.
#
#    QuTIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#    QuTIP 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with QuTIP.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011, Paul D. Nation & Robert J. Johansson
#
###########################################################################
import pickle
from scipy import *

# ------------------------------------------------------------------------------
# Write matrix data to a file
#
def file_data_store(datafile, data, numtype="complex", numformat="decimal", sep=","):

    if datafile == None or data == None:
        raise ValueError("Datafile or data is unspecified")

    M,N = shape(data)

    f = open(datafile, "w")

    f.write("# Generated by QuTiP: %dx%d %s matrix in %s format ['%s' separated values].\n" % (M, N, numtype, numformat, sep))

    if numtype == "complex":

        if numformat == "exp":

            for m in range(M):
                for n in range(N):
                    if imag(data[m,n]) >= 0.0:
                        f.write("%.10e+%.10ej" % (real(data[m,n]),imag(data[m,n])))
                    else:
                        f.write("%.10e%.10ej" % (real(data[m,n]),imag(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        elif numformat == "decimal":

            for m in range(M):
                for n in range(N):
                    if imag(data[m,n]) >= 0.0:
                        f.write("%.10f+%.10fj" % (real(data[m,n]),imag(data[m,n])))
                    else:
                        f.write("%.10f%.10fj" % (real(data[m,n]),imag(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        else:
            raise ValueError("Illegal numformat value (should be 'exp' or 'decimal')")

    elif numtype == "real":

        if numformat == "exp":

            for m in range(M):
                for n in range(N):
                    f.write("%.10e" % (real(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        elif numformat == "decimal":

            for m in range(M):
                for n in range(N):
                    f.write("%.10f" % (real(data[m,n])))
                    if n != N-1:
                        f.write(sep)
                f.write("\n")

        else:
            raise ValueError("Illegal numformat value (should be 'exp' or 'decimal')")

    else:
        raise ValueError("Illegal numtype value (should be 'complex' or 'real')")

    f.close()

# ------------------------------------------------------------------------------
# Read matrix data from a file
#
def file_data_read(datafile, sep=None):

    if datafile == None:
        raise ValueError("datafile is unspecified")

    f = open(datafile, "r")

    #
    # first count lines and numbers of
    #
    M = N = 0
    for line in f:
        # skip comment lines
        if line[0] == '#' or line[0] == '%':
            continue
        # find delim
        if N == 0 and sep == None:
            if len(line.rstrip().split(",")) > 1:
                sep = ","
            elif len(line.rstrip().split(";")) > 1:
                sep = ";"
            elif len(line.rstrip().split(":")) > 1:
                sep = ":"
            elif len(line.rstrip().split("|")) > 1:
                sep = "|"
            elif len(line.rstrip().split()) > 1:
                sep = None # sepical case for a mix of white space deliminators
            else:
                raise ValueError("Unrecognized column deliminator")
        # split the line
        line_vec = line.split(sep)
        n = len(line_vec)
        if N == 0 and n > 0:
            N = n
            # check type
            if ("j" in line_vec[0]) or ("i" in line_vec[0]):
                numtype = "complex"
            else:
                numtype = "real"

            # check format
            if ("e" in line_vec[0]) or ("E" in line_vec[0]):
                numformat = "exp"
            else:
                numformat = "decimal"

        elif N != n:
            raise ValueError("Badly formatted data file: unequal number of columns")
        M += 1

    #
    # read data and store in a matrix
    #
    f.seek(0)

    if numtype == "complex":
        data = zeros((M,N), dtype="complex")
        m = n = 0
        for line in f:
            # skip comment lines
            if line[0] == '#' or line[0] == '%':
                continue
            n = 0
            for item in line.rstrip().split(sep):
                data[m,n] = complex(item)
                n += 1
            m += 1

    else:
        data = zeros((M,N), dtype="float")
        m = n = 0
        for line in f:
            # skip comment lines
            if line[0] == '#' or line[0] == '%':
                continue
            n = 0
            for item in line.rstrip().split(sep):
                data[m,n] = float(item)
                n += 1
            m += 1

    f.close()

    return data

def qsave(data=None,filename='qdata'):
    """
    @brief Saves given data to file named 'filename.qu'

    @param data input data to be stored
    @param filename *str* name of datafile

    """
    fileObject = open(filename+'.qu','w') # open the file for writing
    pickle.dump(data,fileObject)   # this writes the object a to the file named 'filename.qu'
    fileObject.close()

def qload(filename):
    """
    @brief Loads data file from file named 'filename.qu'

    @param filename *str* name of datafile

    @returns object stored in file
    """
    fileObject = open(filename+'.qu','r')  #open the file for reading
    out=pickle.load(fileObject)  #return the object from the file
    if isinstance(out,Qobj): #for quantum objects
        print 'Loaded Qobj class object...'
        return out
    elif isinstance(out,Mcdata): #for mcdata objects
        print 'Loaded Mcdata class object...'
        return out
    elif isinstance(out,Odedata): #for odedata objects
        print 'Loaded Odedata class object...'
        return out
    else: #for any other data
        print 'Loaded generic object...'
        return out

<|endoftext|>


<commit_before># This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################

from scipy import array
from multiprocessing import Pool, cpu_count
from functools import partial
import os
import sys
import signal
import qutip.settings as qset

def _task_wrapper(args):
    try:
        return args[0](*args[1])
    except KeyboardInterrupt:
        os.kill(args[2], signal.SIGINT)
        sys.exit(1)

def _task_wrapper_with_args(args, user_args):
    try:
        return args[0](*args[1], **user_args)
    except KeyboardInterrupt:
        os.kill(args[2], signal.SIGINT)
        sys.exit(1)

def parfor(func, *args, **kwargs):
    """Executes a multi-variable function in parallel on the local machine.

    Parallel execution of a for-loop over function `func` for multiple input
    arguments and keyword arguments.

    Parameters
    ----------
    func : function_type
        A function to run in parallel on the local machine. The function 'func'
        accepts a series of arguments that are passed to the function as variables.
        In general, the function can have multiple input variables, and these
        arguments must be passed in the same order as they are defined in
        the function definition.  In addition, the user can pass multiple keyword
        arguments to the function.

    The following keyword argument is reserved:

    num_cpus : int
        Number of CPU's to use.  Default uses maximum number of CPU's.
        Performance degrades if num_cpus is larger than the physical CPU
        count of your machine.

    Returns
    -------
    result : list
        A ``list`` with length equal to number of input parameters
        containing the output from `func`.

    """
    kw = _default_parfor_settings()
    if 'num_cpus' in kwargs.keys():
        kw['num_cpus']=kwargs['num_cpus']
        del kwargs['num_cpus']
    if len(kwargs) != 0:
        task_func = partial(_task_wrapper_with_args, user_args=kwargs)
    else:
        task_func = _task_wrapper

    if kw['num_cpus'] > qset.num_cpus:
        print("Requested number of CPUs (%s) " % kw['num_cpus'] +
              "is larger than physical number (%s)." % qset.num_cpus)
        print("Reduce 'num_cpus' for greater performance.")

    pool = Pool(processes=kw['num_cpus'])
    var = [[args[j][i] for j in range(len(args))] for i in range(len(args[0]))]
    try:
        map_args = ((func, v, os.getpid()) for v in var)
        par_return = list(pool.map(task_func, map_args))

        if isinstance(par_return[0], tuple):
            par_return = [elem for elem in par_return]
            num_elems = len(par_return[0])
            return [array([elem[ii] for elem in par_return])
                    for ii in range(num_elems)]
        else:
            return list(par_return)

    except KeyboardInterrupt:
        pool.terminate()

def _default_parfor_settings():
    settings = {'num_cpus' : qset.num_cpus}
    return settings

<commit_msg>attempt to fix issue #75: allow iterables by casting to list once<commit_after># This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################

from scipy import array
from multiprocessing import Pool, cpu_count
from functools import partial
import os
import sys
import signal
import qutip.settings as qset

def _task_wrapper(args):
    try:
        return args[0](*args[1])
    except KeyboardInterrupt:
        os.kill(args[2], signal.SIGINT)
        sys.exit(1)

def _task_wrapper_with_args(args, user_args):
    try:
        return args[0](*args[1], **user_args)
    except KeyboardInterrupt:
        os.kill(args[2], signal.SIGINT)
        sys.exit(1)

def parfor(func, *args, **kwargs):
    """Executes a multi-variable function in parallel on the local machine.

    Parallel execution of a for-loop over function `func` for multiple input
    arguments and keyword arguments.

    Parameters
    ----------
    func : function_type
        A function to run in parallel on the local machine. The function 'func'
        accepts a series of arguments that are passed to the function as variables.
        In general, the function can have multiple input variables, and these
        arguments must be passed in the same order as they are defined in
        the function definition.  In addition, the user can pass multiple keyword
        arguments to the function.

    The following keyword argument is reserved:

    num_cpus : int
        Number of CPU's to use.  Default uses maximum number of CPU's.
        Performance degrades if num_cpus is larger than the physical CPU
        count of your machine.

    Returns
    -------
    result : list
        A ``list`` with length equal to number of input parameters
        containing the output from `func`.

    """
    kw = _default_parfor_settings()
    if 'num_cpus' in kwargs.keys():
        kw['num_cpus']=kwargs['num_cpus']
        del kwargs['num_cpus']
    if len(kwargs) != 0:
        task_func = partial(_task_wrapper_with_args, user_args=kwargs)
    else:
        task_func = _task_wrapper

    if kw['num_cpus'] > qset.num_cpus:
        print("Requested number of CPUs (%s) " % kw['num_cpus'] +
              "is larger than physical number (%s)." % qset.num_cpus)
        print("Reduce 'num_cpus' for greater performance.")

    pool = Pool(processes=kw['num_cpus'])
    args = [list(arg) for arg in args]
    var = [[args[j][i] for j in range(len(args))]
           for i in range(len(list(args[0])))]
    try:
        map_args = ((func, v, os.getpid()) for v in var)
        par_return = list(pool.map(task_func, map_args))

        if isinstance(par_return[0], tuple):
            par_return = [elem for elem in par_return]
            num_elems = len(par_return[0])
            return [array([elem[ii] for elem in par_return])
                    for ii in range(num_elems)]
        else:
            return list(par_return)

    except KeyboardInterrupt:
        pool.terminate()

def _default_parfor_settings():
    settings = {'num_cpus' : qset.num_cpus}
    return settings

<|endoftext|>


<commit_before>__version__ = '0.2.5'
<commit_msg>New version<commit_after>__version__ = '0.2.6'
<|endoftext|>