<commit_before>#!/bin/python
import json
import random
import datetime
import sys

import promoterz
import evaluation

from copy import deepcopy

import resultInterface
import interface

import promoterz.sequence.standard_loop

from deap import tools
from deap import algorithms
from deap import base

from Settings import getSettings, makeSettings
import stratego
from functools import partial

from evaluation.gekko.datasetOperations import *

StrategyFileManager = None

# TEMPORARY ASSIGNMENT OF EVAL FUNCTIONS; SO THINGS REMAIN SANE (¿SANE?);
def aEvaluate(
    StrategyFileManager,
    constructPhenotype,
    genconf,
    Datasets,
    Individual,
    gekkoUrl,
):
    phenotype = constructPhenotype(Individual)
    StratName = StrategyFileManager.checkStrategy(phenotype)
    phenotype = {StratName: phenotype}
    SCORE = evaluation.gekko.backtest.Evaluate(
        genconf, Datasets, phenotype, gekkoUrl
    )
    return SCORE

def bEvaluate(constructPhenotype, genconf, Datasets, Individual, gekkoUrl):
    phenotype = constructPhenotype(Individual)
    phenotype = {Individual.Strategy: phenotype}
    SCORE = evaluation.gekko.backtest.Evaluate(
        genconf, Datasets, phenotype, gekkoUrl
    )
    return SCORE

def gekko_generations(
        TargetParameters, GenerationMethod, EvaluationMode, settings, options, web=None):
    # --LOAD SETTINGS;
    genconf = makeSettings(settings['generations'])
    globalconf = makeSettings(settings['Global'])
    datasetconf = makeSettings(settings['dataset'])
    indicatorconf = makeSettings(settings['indicators'])
    # --APPLY COMMAND LINE GENCONF SETTINGS;
    for parameter in genconf.__dict__.keys():
        if parameter in options.__dict__.keys():
            if options.__dict__[parameter] != None:
                genconf.__dict__[parameter] = options.__dict__[parameter]
    GenerationMethod = promoterz.functions.selectRepresentationMethod(GenerationMethod)
    if EvaluationMode == 'indicator':
        # global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath, indicatorconf
        )
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = options.skeleton
    # --for standard methods;
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode
    TargetParameters = promoterz.parameterOperations.flattenParameters(TargetParameters)
    TargetParameters = promoterz.parameterOperations.parameterValuesToRangeOfValues(
        TargetParameters, genconf.parameter_spread
    )
    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf, TargetParameters)
    RemoteHosts = evaluation.gekko.API.loadHostsFile(globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')
    # CHECK HOW MANY EVOLUTION DATASETS ARE SPECIFIED AT SETTINGS;
    evolutionDatasetNames = ['dataset_source']
    evolutionDatasets = []
    for DS in range(1, 100):
        datasetConfigName = 'dataset_source%i' % DS
        if datasetConfigName in datasetconf.__dict__.keys():
            evolutionDatasetList.append(datasetConfigName)
    # --GRAB PRIMARY (EVOLUTION) DATASETS
    for evolutionDatasetName in evolutionDatasetNames:
        D = evaluation.gekko.dataset.selectCandlestickData(
            exchange_source=datasetconf.__dict__[evolutionDatasetName]
        )
        evolutionDatasets.append(CandlestickDataset(*D))
        try:
            evolutionDatasets[-1].restrain(datasetconf.dataset_span)
        except:
            print('dataset_ span not configured for evolutionDatasetName. skipping...')

    # --GRAB SECONDARY (EVALUATION) DATASET
    try:
        D = evaluation.gekko.dataset.selectCandlestickData(
            exchange_source=datasetconf.eval_dataset_source,
            avoidCurrency=evolutionDatasets[0].specifications['asset'],
        )
        evaluationDatasets = [CandlestickDataset(*D)]
        evaluationDatasets[0].restrain(datasetconf.eval_dataset_span)
    except RuntimeError:
        evaluationDataset = None
        print("Evaluation dataset not found.")
    # --INITIALIZE LOGGER;
    ds_specs = evolutionDatasets[0].specifications
    logfilename = "%s-%s-%s-%s-%s" % (
        Strategy,
        ds_specs['exchange'],
        ds_specs['currency'],
        ds_specs['asset'],
        str(datetime.datetime.now())[-6:],
    )
    Logger = promoterz.logger.Logger(logfilename)
    # --PRINT RUNTIME ARGS TO LOG HEADER;
    ARGS = ' '.join(sys.argv)
    Logger.log(ARGS, target='Header')
    # --SHOW PARAMETER INFO;
    if Strategy:
        Logger.log("Evolving %s strategy;\n" % Strategy)
    Logger.log("evaluated parameters ranges:", target="Header")
    for k in TargetParameters.keys():
        Logger.log(
            "%s%s%s\n" % (k, " " * (30 - len(k)), TargetParameters[k]),
            target="Header"
        )
    # --LOG CONFIG INFO;
    configInfo = json.dumps(genconf.__dict__, indent=4)
    Logger.log(configInfo, target="Header", show=False)
    # --SHOW DATASET INFO;
    for evolutionDataset in evolutionDatasets:
        Logger.log(
            interface.parseDatasetInfo("evolution", evolutionDataset),
            target="Header"
        )
    if evaluationDatasets:
        for evaluationDataset in evaluationDatasets:
            Logger.log(
                interface.parseDatasetInfo("evaluation", evaluationDataset),
                target="Header"
            )
    # --INITIALIZE WORLD WITH CANDLESTICK DATASET INFO; HERE THE GA KICKS IN;
    GlobalTools.register('Evaluate', Evaluate,
                         GlobalTools.constructPhenotype, genconf)

    # --THIS LOADS A DATERANGE FOR A LOCALE;
    def onInitLocale(World, locale):
        locale.Dataset = getLocaleDataset(World, locale)

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(
        GlobalTools,
        loops,
        genconf,
        globalconf,
        TargetParameters,
        EnvironmentParameters={'evolution':  evolutionDatasets,
                               'evaluation': evaluationDatasets},
        onInitLocale=onInitLocale,
        web=web,
    )
    World.logger = Logger
    World.EvaluationStatistics = []
    World.logger.updateFile()
    # --RUN EPOCHES;
    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)
    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)
    print("")
    print("\t\t.RUN ENDS.")
<commit_msg>fixing multiple dataset selector<commit_after>#!/bin/python
import json
import random
import datetime
import sys

import promoterz
import evaluation

from copy import deepcopy

import resultInterface
import interface

import promoterz.sequence.standard_loop

from deap import tools
from deap import algorithms
from deap import base

from Settings import getSettings, makeSettings
import stratego
from functools import partial

from evaluation.gekko.datasetOperations import *

StrategyFileManager = None

# TEMPORARY ASSIGNMENT OF EVAL FUNCTIONS; SO THINGS REMAIN SANE (¿SANE?);
def aEvaluate(
    StrategyFileManager,
    constructPhenotype,
    genconf,
    Datasets,
    Individual,
    gekkoUrl,
):
    phenotype = constructPhenotype(Individual)
    StratName = StrategyFileManager.checkStrategy(phenotype)
    phenotype = {StratName: phenotype}
    SCORE = evaluation.gekko.backtest.Evaluate(
        genconf, Datasets, phenotype, gekkoUrl
    )
    return SCORE

def bEvaluate(constructPhenotype, genconf, Datasets, Individual, gekkoUrl):
    phenotype = constructPhenotype(Individual)
    phenotype = {Individual.Strategy: phenotype}
    SCORE = evaluation.gekko.backtest.Evaluate(
        genconf, Datasets, phenotype, gekkoUrl
    )
    return SCORE

def gekko_generations(
        TargetParameters, GenerationMethod, EvaluationMode, settings, options, web=None):
    # --LOAD SETTINGS;
    genconf = makeSettings(settings['generations'])
    globalconf = makeSettings(settings['Global'])
    datasetconf = makeSettings(settings['dataset'])
    indicatorconf = makeSettings(settings['indicators'])
    # --APPLY COMMAND LINE GENCONF SETTINGS;
    for parameter in genconf.__dict__.keys():
        if parameter in options.__dict__.keys():
            if options.__dict__[parameter] != None:
                genconf.__dict__[parameter] = options.__dict__[parameter]
    GenerationMethod = promoterz.functions.selectRepresentationMethod(GenerationMethod)
    if EvaluationMode == 'indicator':
        # global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath, indicatorconf
        )
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = options.skeleton
    # --for standard methods;
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode
    TargetParameters = promoterz.parameterOperations.flattenParameters(TargetParameters)
    TargetParameters = promoterz.parameterOperations.parameterValuesToRangeOfValues(
        TargetParameters, genconf.parameter_spread
    )
    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf, TargetParameters)
    RemoteHosts = evaluation.gekko.API.loadHostsFile(globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')
    # CHECK HOW MANY EVOLUTION DATASETS ARE SPECIFIED AT SETTINGS;
    evolutionDatasetNames = ['dataset_source']
    evolutionDatasets = []
    for DS in range(1, 100):
        datasetConfigName = 'dataset_source%i' % DS
        if datasetConfigName in datasetconf.__dict__.keys():
            evolutionDatasetNames.append(datasetConfigName)
    # --GRAB PRIMARY (EVOLUTION) DATASETS
    for evolutionDatasetName in evolutionDatasetNames:
        D = evaluation.gekko.dataset.selectCandlestickData(
            exchange_source=datasetconf.__dict__[evolutionDatasetName]
        )
        evolutionDatasets.append(CandlestickDataset(*D))
        try:
            evolutionDatasets[-1].restrain(datasetconf.dataset_span)
        except:
            print('dataset_ span not configured for evolutionDatasetName. skipping...')

    # --GRAB SECONDARY (EVALUATION) DATASET
    try:
        D = evaluation.gekko.dataset.selectCandlestickData(
            exchange_source=datasetconf.eval_dataset_source,
            avoidCurrency=evolutionDatasets[0].specifications['asset'],
        )
        evaluationDatasets = [CandlestickDataset(*D)]
        evaluationDatasets[0].restrain(datasetconf.eval_dataset_span)
    except RuntimeError:
        evaluationDataset = None
        print("Evaluation dataset not found.")
    # --INITIALIZE LOGGER;
    ds_specs = evolutionDatasets[0].specifications
    logfilename = "%s-%s-%s-%s-%s" % (
        Strategy,
        ds_specs['exchange'],
        ds_specs['currency'],
        ds_specs['asset'],
        str(datetime.datetime.now())[-6:],
    )
    Logger = promoterz.logger.Logger(logfilename)
    # --PRINT RUNTIME ARGS TO LOG HEADER;
    ARGS = ' '.join(sys.argv)
    Logger.log(ARGS, target='Header')
    # --SHOW PARAMETER INFO;
    if Strategy:
        Logger.log("Evolving %s strategy;\n" % Strategy)
    Logger.log("evaluated parameters ranges:", target="Header")
    for k in TargetParameters.keys():
        Logger.log(
            "%s%s%s\n" % (k, " " * (30 - len(k)), TargetParameters[k]),
            target="Header"
        )
    # --LOG CONFIG INFO;
    configInfo = json.dumps(genconf.__dict__, indent=4)
    Logger.log(configInfo, target="Header", show=False)
    # --SHOW DATASET INFO;
    for evolutionDataset in evolutionDatasets:
        Logger.log(
            interface.parseDatasetInfo("evolution", evolutionDataset),
            target="Header"
        )
    if evaluationDatasets:
        for evaluationDataset in evaluationDatasets:
            Logger.log(
                interface.parseDatasetInfo("evaluation", evaluationDataset),
                target="Header"
            )
    # --INITIALIZE WORLD WITH CANDLESTICK DATASET INFO; HERE THE GA KICKS IN;
    GlobalTools.register('Evaluate', Evaluate,
                         GlobalTools.constructPhenotype, genconf)

    # --THIS LOADS A DATERANGE FOR A LOCALE;
    def onInitLocale(World, locale):
        locale.Dataset = getLocaleDataset(World, locale)

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(
        GlobalTools,
        loops,
        genconf,
        globalconf,
        TargetParameters,
        EnvironmentParameters={'evolution':  evolutionDatasets,
                               'evaluation': evaluationDatasets},
        onInitLocale=onInitLocale,
        web=web,
    )
    World.logger = Logger
    World.EvaluationStatistics = []
    World.logger.updateFile()
    # --RUN EPOCHES;
    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)
    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)
    print("")
    print("\t\t.RUN ENDS.")
<|endoftext|>


<commit_before>from AppKit import NSPopUpButton, NSPopUpButtonCell
from vanillaBase import VanillaBaseControl

class PopUpButton(VanillaBaseControl):

    """
    A button which, when selected, displays a list of items for the user to choose from.

    pre.
    from vanilla import *

    class PopUpButtonDemo(object):

        def __init__(self):
            self.w = Window((100, 40))
            self.w.popUpButton = PopUpButton((10, 10, -10, 20),
                                  ["A", "B", "C"],
                                  callback=self.popUpButtonCallback)
            self.w.open()

        def popUpButtonCallback(self, sender):
            print "pop up button selection!", sender.get()

    PopUpButtonDemo()
    """

    nsPopUpButtonClass = NSPopUpButton
    nsPopUpButtonCellClass = NSPopUpButtonCell

    frameAdjustments = {
        "mini": (-1, 0, 3, 0),
        "small": (-3, -4, 6, 5),
        "regular": (-3, -4, 6, 6),
    }

    def __init__(self, posSize, items, callback=None, sizeStyle="regular"):
        """
        *posSize* Tuple of form (left, top, width, height) representing the position and size of the pop up button. The size of the button sould match the appropriate value for the given _sizeStyle_.

        |\\3. *Standard Dimensions* |
        | Regular | H | 20          |
        | Small   | H | 17          |
        | Mini    | H | 15          |

        *items* A list of items to appear in the pop up list.

        *callback* The method to be called when the user selects an item in the pop up list.

        *sizeStyle* A string representing the desired size style of the pop up button. The options are:

        | "regular" |
        | "small"   |
        | "mini"    |
        """
        self._setupView(self.nsPopUpButtonClass, posSize)
        if self.nsPopUpButtonCellClass != NSPopUpButtonCell:
            self._nsObject.setCell_(self.nsPopUpButtonCellClass.alloc().init())
        if callback is not None:
            self._setCallback(callback)
        self._setSizeStyle(sizeStyle)
        self._nsObject.addItemsWithTitles_(items)

    def getNSPopUpButton(self):
        """
        Return the _NSPopUpButton_ that this object wraps.
        """
        return self._nsObject

    def get(self):
        """
        Get the index selected item in the pop up list.
        """
        return self._nsObject.indexOfSelectedItem()

    def set(self, value):
        """
        Set the index of the selected item in the pop up list.
        """
        self._nsObject.selectItemAtIndex_(value)

    def setItems(self, items):
        """
        Set the items to appear in the pop up list.
        """
        self._nsObject.removeAllItems()
        self._nsObject.addItemsWithTitles_(items)

    def getItems(self):
        """
        Get the list of items that appear in the pop up list.
        """
        return self._nsObject.itemTitles()
<commit_msg>Allow the addition os NSMenutItem objects.<commit_after>from AppKit import NSPopUpButton, NSPopUpButtonCell, NSMenuItem
from vanillaBase import VanillaBaseControl

class PopUpButton(VanillaBaseControl):

    """
    A button which, when selected, displays a list of items for the user to choose from.

    pre.
    from vanilla import *

    class PopUpButtonDemo(object):

        def __init__(self):
            self.w = Window((100, 40))
            self.w.popUpButton = PopUpButton((10, 10, -10, 20),
                                  ["A", "B", "C"],
                                  callback=self.popUpButtonCallback)
            self.w.open()

        def popUpButtonCallback(self, sender):
            print "pop up button selection!", sender.get()

    PopUpButtonDemo()
    """

    nsPopUpButtonClass = NSPopUpButton
    nsPopUpButtonCellClass = NSPopUpButtonCell

    frameAdjustments = {
        "mini": (-1, 0, 3, 0),
        "small": (-3, -4, 6, 5),
        "regular": (-3, -4, 6, 6),
    }

    def __init__(self, posSize, items, callback=None, sizeStyle="regular"):
        """
        *posSize* Tuple of form (left, top, width, height) representing the position and size of the pop up button. The size of the button sould match the appropriate value for the given _sizeStyle_.

        |\\3. *Standard Dimensions* |
        | Regular | H | 20          |
        | Small   | H | 17          |
        | Mini    | H | 15          |

        *items* A list of items to appear in the pop up list.

        *callback* The method to be called when the user selects an item in the pop up list.

        *sizeStyle* A string representing the desired size style of the pop up button. The options are:

        | "regular" |
        | "small"   |
        | "mini"    |
        """
        self._setupView(self.nsPopUpButtonClass, posSize)
        if self.nsPopUpButtonCellClass != NSPopUpButtonCell:
            self._nsObject.setCell_(self.nsPopUpButtonCellClass.alloc().init())
        if callback is not None:
            self._setCallback(callback)
        self._setSizeStyle(sizeStyle)
        self.setItems(items)

    def getNSPopUpButton(self):
        """
        Return the _NSPopUpButton_ that this object wraps.
        """
        return self._nsObject

    def get(self):
        """
        Get the index selected item in the pop up list.
        """
        return self._nsObject.indexOfSelectedItem()

    def set(self, value):
        """
        Set the index of the selected item in the pop up list.
        """
        self._nsObject.selectItemAtIndex_(value)

    def setItems(self, items):
        """
        Set the items to appear in the pop up list.
        """
        self._nsObject.removeAllItems()
        for item in items:
            if isinstance(item, NSMenuItem):
                menu = self._nsObject.menu()
                menu.addItem_(item)
            else:
                self._nsObject.addItemWithTitle_(item)

    def getItems(self):
        """
        Get the list of items that appear in the pop up list.
        """
        return self._nsObject.itemTitles()
<|endoftext|>


<commit_before>"""
Tests for the highstate state relative and absolute paths
"""
import logging

import pytest

log = logging.getLogger(__name__)

pytestmark = [
    pytest.mark.slow_test,
    pytest.mark.windows_whitelisted,
]

@pytest.fixture(scope="module")
def pillar_tree(salt_master, salt_minion, salt_call_cli):
    top_file = """
    base:
      '{}':
        - basic
    """.format(
        salt_minion.id
    )
    basic_pillar_file = """
    monty: python
    companions:
      three:
        - liz
        - jo
        - sarah jane
    """
    top_tempfile = salt_master.pillar_tree.base.temp_file("top.sls", top_file)
    basic_tempfile = salt_master.pillar_tree.base.temp_file(
        "basic.sls", basic_pillar_file
    )

    try:
        with top_tempfile, basic_tempfile:
            ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
            assert ret.exitcode == 0
            assert ret.json is True
            yield
    finally:
        # Refresh pillar again to cleaup the temp pillar
        ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
        assert ret.exitcode == 0
        assert ret.json is True

def test_rel_abs_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"
    test_tempdir.mkdir(parents=True, exist_ok=True)

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - .bug
      - tmp_dir/include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_rel_rel_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"
    test_tempdir.mkdir(parents=True, exist_ok=True)

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - .bug
      - .include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_abs_rel_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"
    test_tempdir.mkdir(parents=True, exist_ok=True)

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - tmp_dir/bug
      - .include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_abs_abs_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"
    test_tempdir.mkdir(parents=True, exist_ok=True)

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - tmp_dir/bug
      - tmp_dir/include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0
<commit_msg>Updated tests per reviewers comments<commit_after>"""
Tests for the highstate state relative and absolute paths
"""
import logging

import pytest

log = logging.getLogger(__name__)

pytestmark = [
    pytest.mark.slow_test,
    pytest.mark.windows_whitelisted,
]

@pytest.fixture(scope="module")
def pillar_tree(salt_master, salt_minion, salt_call_cli):
    top_file = """
    base:
      '{}':
        - basic
    """.format(
        salt_minion.id
    )
    basic_pillar_file = """
    monty: python
    companions:
      three:
        - liz
        - jo
        - sarah jane
    """
    top_tempfile = salt_master.pillar_tree.base.temp_file("top.sls", top_file)
    basic_tempfile = salt_master.pillar_tree.base.temp_file(
        "basic.sls", basic_pillar_file
    )

    try:
        with top_tempfile, basic_tempfile:
            ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
            assert ret.exitcode == 0
            assert ret.json is True
            yield
    finally:
        # Refresh pillar again to cleaup the temp pillar
        ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
        assert ret.exitcode == 0
        assert ret.json is True

def test_rel_abs_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - .bug
      - tmp_dir/include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_rel_rel_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - .bug
      - .include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_abs_rel_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - tmp_dir/bug
      - .include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0

def test_abs_abs_path_ids(
    salt_master,
    salt_call_cli,
    pillar_tree,
    tmp_path,
    salt_minion,
):
    """
    This tests for any regressions for this issue:
    https://github.com/saltstack/salt/issues/54179
    """
    test_tempdir = salt_master.state_tree.base.paths[0] / "tmp_dir"

    init_sls_name = "tmp_dir/init"
    init_sls_contents = """
    include:
      - tmp_dir/bug
      - tmp_dir/include
    """

    include_sls_name = "tmp_dir/include"
    include_sls_contents = """
    include:
      - tmp_dir/bug
    """

    include_bug_sls_name = "tmp_dir/include_bug"
    include_bug_sls_contents = """
    include_bug:
      test.succeed_without_changes:
        - name: include_bug
        - require:
          - id: bug
    """

    bug_sls_name = "tmp_dir/bug"
    bug_sls_contents = """
    bug:
      test.succeed_without_changes:
        - name: bug
    """

    init_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(init_sls_name), init_sls_contents
    )

    include_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_sls_name), include_sls_contents
    )

    include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(include_bug_sls_name), include_bug_sls_contents
    )

    bug_sls_tempfile = salt_master.state_tree.base.temp_file(
        "{}.sls".format(bug_sls_name), bug_sls_contents
    )

    with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
        ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
        assert ret.exitcode == 0
<|endoftext|>


<commit_before>import os
import guessit
import shutil

def move_me(abp, path, fon, fn):
    if not os.path.exists(path + '/' + 'Songs'):
        os.makedirs(path + '/' + 'Songs')
    shutil.move(abp, path + '/' + 'Songs' + '/' + fn)

def sort_files(root_path):
    file_extensions = ['.avi', '.mp4', '.mkv', '.m4v']
    banned_extensions = ['.srt', '.jpg', '.torrent']
    ef = root_path + '/' + 'Episodes'  # Path to the Episodes folder
    mf = root_path + '/' + 'Movies'    # Path to the Movies folder

    # Make folders if non existent

    title = ''
    filetype = ''

    for filename in os.listdir(root_path):
        abs_path = root_path + '/' + filename
        if not os.path.isdir(abs_path):
            if os.path.splitext(abs_path)[-1].lower() in file_extensions:
                movie_info = guessit.guessit(filename)
                for x, y in movie_info.items():
                    if x == 'title':
                        title = str(y)
                    if x == 'type':
                        filetype = str(y)

                    if title != '' and filetype == 'episode':
                        if not os.path.exists(ef):
                            os.makedirs(ef)
                        if not os.path.exists(ef + '/' + title.title()):
                            os.makedirs(ef + '/' + title.title())
                            shutil.move(abs_path, ef + '/' + title.title())
                        else:
                            shutil.move(abs_path, ef + '/' + title.title())
                        title = ''
                        filetype = ''
                    elif title != '' and filetype == 'movie':
                        if not os.path.exists(mf):
                            os.makedirs(mf)
                        if not os.path.exists(mf + '/' + title.title()):
                            os.makedirs(mf + '/' + title.title())
                            shutil.move(abs_path, mf + '/' + title.title())
                        else:
                            shutil.move(abs_path, mf + '/' + title.title())
                        title = ''
                        filetype = ''
            elif os.path.splitext(abs_path)[-1].lower() in banned_extensions:
                os.remove(abs_path)
            elif os.path.splitext(abs_path)[-1].lower() == '.mp3':
                move_me(abs_path, root_path, 'Songs', filename)
            elif os.path.splitext(abs_path)[-1].lower() == '.exe':
                move_me(abs_path, root_path, 'Programs', filename)
            elif os.path.splitext(abs_path)[-1].lower() == '.txt':
                move_me(abs_path, root_path, 'Documents', filename)

sort_files('C:/Users/dadig/Downloads')
<commit_msg>hotfix<commit_after>import os
import guessit
import shutil

def move_me(abp, path, fon, fn):
    if not os.path.exists(path + '/' + fon):
        os.makedirs(path + '/' + fon)
    shutil.move(abp, path + '/' + fon + '/' + fn)

def sort_files(root_path):
    file_extensions = ['.avi', '.mp4', '.mkv', '.m4v']
    banned_extensions = ['.srt', '.jpg', '.torrent']
    ef = root_path + '/' + 'Episodes'  # Path to the Episodes folder
    mf = root_path + '/' + 'Movies'    # Path to the Movies folder

    title = ''
    filetype = ''

    for filename in os.listdir(root_path):
        abs_path = root_path + '/' + filename
        if not os.path.isdir(abs_path):
            if os.path.splitext(abs_path)[-1].lower() in file_extensions:
                movie_info = guessit.guessit(filename)
                for x, y in movie_info.items():
                    if x == 'title':
                        title = str(y)
                    if x == 'type':
                        filetype = str(y)

                    if title != '' and filetype == 'episode':
                        if not os.path.exists(ef):
                            os.makedirs(ef)
                        if not os.path.exists(ef + '/' + title.title()):
                            os.makedirs(ef + '/' + title.title())
                            shutil.move(abs_path, ef + '/' + title.title())
                        else:
                            shutil.move(abs_path, ef + '/' + title.title())
                        title = ''
                        filetype = ''
                    elif title != '' and filetype == 'movie':
                        if not os.path.exists(mf):
                            os.makedirs(mf)
                        if not os.path.exists(mf + '/' + title.title()):
                            os.makedirs(mf + '/' + title.title())
                            shutil.move(abs_path, mf + '/' + title.title())
                        else:
                            shutil.move(abs_path, mf + '/' + title.title())
                        title = ''
                        filetype = ''
            elif os.path.splitext(abs_path)[-1].lower() in banned_extensions:
                os.remove(abs_path)
            elif os.path.splitext(abs_path)[-1].lower() == '.mp3':
                move_me(abs_path, root_path, 'Songs', filename)
            elif os.path.splitext(abs_path)[-1].lower() == '.exe':
                move_me(abs_path, root_path, 'Programs', filename)
            elif os.path.splitext(abs_path)[-1].lower() == '.txt':
                move_me(abs_path, root_path, 'Documents', filename)

sort_files('C:/Users/dadig/Downloads')
<|endoftext|>


<commit_before>'''
Control the state system on the minion
'''

# Import salt modules
import salt.state

def low(data):
    '''
    Execute a single low data call
    This function is mostly intended for testing the state system

    CLI Example:
    salt '*' state.low '{"state": "pkg", "fun": "installed", "name": "vim"}'
    '''
    st_ = salt.state.State(__opts__)
    err = st_.verify_data(data)
    if err:
        return err
    return st_.call(data)

def high(data):
    '''
    Execute the compound calls stored in a single set of high data
    This function is mostly intended for testing the state system

    CLI Example:
    salt '*' state.high '{"vim": {"pkg": ["installed"]}}'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_high(data)

def template(tem):
    '''
    Execute the information stored in a template file on the minion

    CLI Example:
    salt '*' state.template '<Path to template on the minion>'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_template(tem)

def template_str(tem):
    '''
    Execute the information stored in a template file on the minion

    CLI Example:
    salt '*' state.template_str '<Template String>'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_template_str(tem)

<commit_msg>Add call_highstate function to the state minion module<commit_after>'''
Control the state system on the minion
'''

# Import salt modules
import salt.state

def low(data):
    '''
    Execute a single low data call
    This function is mostly intended for testing the state system

    CLI Example:
    salt '*' state.low '{"state": "pkg", "fun": "installed", "name": "vim"}'
    '''
    st_ = salt.state.State(__opts__)
    err = st_.verify_data(data)
    if err:
        return err
    return st_.call(data)

def high(data):
    '''
    Execute the compound calls stored in a single set of high data
    This function is mostly intended for testing the state system

    CLI Example:
    salt '*' state.high '{"vim": {"pkg": ["installed"]}}'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_high(data)

def template(tem):
    '''
    Execute the information stored in a template file on the minion

    CLI Example:
    salt '*' state.template '<Path to template on the minion>'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_template(tem)

def template_str(tem):
    '''
    Execute the information stored in a template file on the minion

    CLI Example:
    salt '*' state.template_str '<Template String>'
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_template_str(tem)

def highstate():
    '''
    Retrive the state data from the salt master for this minion and execute it

    CLI Example:
    salt '*' state.highstate
    '''
    st_ = salt.state.State(__opts__)
    return st_.call_highstate()
<|endoftext|>


<commit_before>from django.db.models import Q
from django.db.utils import IntegrityError
from django.test import TestCase

from .models import (Address, Place, Restaurant, Link, CharLink, TextLink,
    Person, Contact, Note, Organization, OddRelation1, OddRelation2, Company,
    Developer, Team, Guild, Tag, Board)

class GenericRelationTests(TestCase):

    def test_inherited_models_content_type(self):
        """
        Test that GenericRelations on inherited classes use the correct content
        type.
        """

        p = Place.objects.create(name="South Park")
        r = Restaurant.objects.create(name="Chubby's")
        l1 = Link.objects.create(content_object=p)
        l2 = Link.objects.create(content_object=r)
        self.assertEqual(list(p.links.all()), [l1])
        self.assertEqual(list(r.links.all()), [l2])

    def test_reverse_relation_pk(self):
        """
        Test that the correct column name is used for the primary key on the
        originating model of a query.  See #12664.
        """
        p = Person.objects.create(account=23, name='Chef')
        a = Address.objects.create(street='123 Anywhere Place',
                                   city='Conifer', state='CO',
                                   zipcode='80433', content_object=p)

        qs = Person.objects.filter(addresses__zipcode='80433')
        self.assertEqual(1, qs.count())
        self.assertEqual('Chef', qs[0].name)

    def test_charlink_delete(self):
        oddrel = OddRelation1.objects.create(name='clink')
        cl = CharLink.objects.create(content_object=oddrel)
        oddrel.delete()

    def test_textlink_delete(self):
        oddrel = OddRelation2.objects.create(name='tlink')
        tl = TextLink.objects.create(content_object=oddrel)
        oddrel.delete()

    def test_q_object_or(self):
        """
        Tests that SQL query parameters for generic relations are properly
        grouped when OR is used.

        Test for bug http://code.djangoproject.com/ticket/11535

        In this bug the first query (below) works while the second, with the
        query parameters the same but in reverse order, does not.

        The issue is that the generic relation conditions do not get properly
        grouped in parentheses.
        """
        note_contact = Contact.objects.create()
        org_contact = Contact.objects.create()
        note = Note.objects.create(note='note', content_object=note_contact)
        org = Organization.objects.create(name='org name')
        org.contacts.add(org_contact)
        # search with a non-matching note and a matching org name
        qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
                            Q(organizations__name__icontains=r'org name'))
        self.assertTrue(org_contact in qs)
        # search again, with the same query parameters, in reverse order
        qs = Contact.objects.filter(
            Q(organizations__name__icontains=r'org name') |
            Q(notes__note__icontains=r'other note'))
        self.assertTrue(org_contact in qs)

    def test_join_reuse(self):
        qs = Person.objects.filter(
            addresses__street='foo'
        ).filter(
            addresses__street='bar'
        )
        self.assertEqual(str(qs.query).count('JOIN'), 2)

    def test_generic_relation_ordering(self):
        """
        Test that ordering over a generic relation does not include extraneous
        duplicate results, nor excludes rows not participating in the relation.
        """
        p1 = Place.objects.create(name="South Park")
        p2 = Place.objects.create(name="The City")
        c = Company.objects.create(name="Chubby's Intl.")
        l1 = Link.objects.create(content_object=p1)
        l2 = Link.objects.create(content_object=c)

        places = list(Place.objects.order_by('links__id'))
        def count_places(place):
            return len([p for p in places if p.id == place.id])

        self.assertEqual(len(places), 2)
        self.assertEqual(count_places(p1), 1)
        self.assertEqual(count_places(p2), 1)

    def test_target_model_is_unsaved(self):
        """Test related to #13085"""
        # Fails with another, ORM-level error
        dev1 = Developer(name='Joe')
        note = Note(note='Deserves promotion', content_object=dev1)
        self.assertRaisesMessage(IntegrityError,
                "generic_relations_regress_note.object_id may not be NULL",
                note.save)

    def test_target_model_len_zero(self):
        """Test for #13085 -- __len__() returns 0"""
        team1 = Team.objects.create(name='Backend devs')
        try:
            note = Note(note='Deserve a bonus', content_object=team1)
        except Exception as e:
            if issubclass(type(e), Exception) and str(e) == 'Impossible arguments to GFK.get_content_type!':
                self.fail("Saving model with GenericForeignKey to model instance whose __len__ method returns 0 shouldn't fail.")
            raise e
        note.save()

    def test_target_model_nonzero_false(self):
        """Test related to #13085"""
        # __nonzero__() returns False -- This actually doesn't currently fail.
        # This test validates that
        g1 = Guild.objects.create(name='First guild')
        note = Note(note='Note for guild', content_object=g1)
        note.save()

    def test_gfk_to_model_with_empty_pk(self):
        """Test related to #13085"""
        # Saving model with GenericForeignKey to model instance with an
        # empty CharField PK
        b1 = Board.objects.create(name='')
        tag = Tag(label='VP', content_object=b1)
        tag.save()
<commit_msg>Changed test assertion strategy used in 04d9730.<commit_after>from django.db.models import Q
from django.db.utils import IntegrityError
from django.test import TestCase

from .models import (Address, Place, Restaurant, Link, CharLink, TextLink,
    Person, Contact, Note, Organization, OddRelation1, OddRelation2, Company,
    Developer, Team, Guild, Tag, Board)

class GenericRelationTests(TestCase):

    def test_inherited_models_content_type(self):
        """
        Test that GenericRelations on inherited classes use the correct content
        type.
        """

        p = Place.objects.create(name="South Park")
        r = Restaurant.objects.create(name="Chubby's")
        l1 = Link.objects.create(content_object=p)
        l2 = Link.objects.create(content_object=r)
        self.assertEqual(list(p.links.all()), [l1])
        self.assertEqual(list(r.links.all()), [l2])

    def test_reverse_relation_pk(self):
        """
        Test that the correct column name is used for the primary key on the
        originating model of a query.  See #12664.
        """
        p = Person.objects.create(account=23, name='Chef')
        a = Address.objects.create(street='123 Anywhere Place',
                                   city='Conifer', state='CO',
                                   zipcode='80433', content_object=p)

        qs = Person.objects.filter(addresses__zipcode='80433')
        self.assertEqual(1, qs.count())
        self.assertEqual('Chef', qs[0].name)

    def test_charlink_delete(self):
        oddrel = OddRelation1.objects.create(name='clink')
        cl = CharLink.objects.create(content_object=oddrel)
        oddrel.delete()

    def test_textlink_delete(self):
        oddrel = OddRelation2.objects.create(name='tlink')
        tl = TextLink.objects.create(content_object=oddrel)
        oddrel.delete()

    def test_q_object_or(self):
        """
        Tests that SQL query parameters for generic relations are properly
        grouped when OR is used.

        Test for bug http://code.djangoproject.com/ticket/11535

        In this bug the first query (below) works while the second, with the
        query parameters the same but in reverse order, does not.

        The issue is that the generic relation conditions do not get properly
        grouped in parentheses.
        """
        note_contact = Contact.objects.create()
        org_contact = Contact.objects.create()
        note = Note.objects.create(note='note', content_object=note_contact)
        org = Organization.objects.create(name='org name')
        org.contacts.add(org_contact)
        # search with a non-matching note and a matching org name
        qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
                            Q(organizations__name__icontains=r'org name'))
        self.assertTrue(org_contact in qs)
        # search again, with the same query parameters, in reverse order
        qs = Contact.objects.filter(
            Q(organizations__name__icontains=r'org name') |
            Q(notes__note__icontains=r'other note'))
        self.assertTrue(org_contact in qs)

    def test_join_reuse(self):
        qs = Person.objects.filter(
            addresses__street='foo'
        ).filter(
            addresses__street='bar'
        )
        self.assertEqual(str(qs.query).count('JOIN'), 2)

    def test_generic_relation_ordering(self):
        """
        Test that ordering over a generic relation does not include extraneous
        duplicate results, nor excludes rows not participating in the relation.
        """
        p1 = Place.objects.create(name="South Park")
        p2 = Place.objects.create(name="The City")
        c = Company.objects.create(name="Chubby's Intl.")
        l1 = Link.objects.create(content_object=p1)
        l2 = Link.objects.create(content_object=c)

        places = list(Place.objects.order_by('links__id'))
        def count_places(place):
            return len([p for p in places if p.id == place.id])

        self.assertEqual(len(places), 2)
        self.assertEqual(count_places(p1), 1)
        self.assertEqual(count_places(p2), 1)

    def test_target_model_is_unsaved(self):
        """Test related to #13085"""
        # Fails with another, ORM-level error
        dev1 = Developer(name='Joe')
        note = Note(note='Deserves promotion', content_object=dev1)
        self.assertRaises(IntegrityError, note.save)

    def test_target_model_len_zero(self):
        """Test for #13085 -- __len__() returns 0"""
        team1 = Team.objects.create(name='Backend devs')
        try:
            note = Note(note='Deserve a bonus', content_object=team1)
        except Exception as e:
            if issubclass(type(e), Exception) and str(e) == 'Impossible arguments to GFK.get_content_type!':
                self.fail("Saving model with GenericForeignKey to model instance whose __len__ method returns 0 shouldn't fail.")
            raise e
        note.save()

    def test_target_model_nonzero_false(self):
        """Test related to #13085"""
        # __nonzero__() returns False -- This actually doesn't currently fail.
        # This test validates that
        g1 = Guild.objects.create(name='First guild')
        note = Note(note='Note for guild', content_object=g1)
        note.save()

    def test_gfk_to_model_with_empty_pk(self):
        """Test related to #13085"""
        # Saving model with GenericForeignKey to model instance with an
        # empty CharField PK
        b1 = Board.objects.create(name='')
        tag = Tag(label='VP', content_object=b1)
        tag.save()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
Recursively display nested data
===============================

This is the default outputter for most execution functions.

Example output::

    myminion:
        ----------
        foo:
            ----------
            bar:
                baz
            dictionary:
                ----------
                abc:
                    123
                def:
                    456
            list:
                - Hello
                - World
'''
from __future__ import absolute_import, print_function, unicode_literals
# Import python libs
from numbers import Number

# Import salt libs
import salt.output
import salt.utils.color
import salt.utils.odict
import salt.utils.stringutils
from salt.ext import six
from collections import Mapping

class NestDisplay(object):
    '''
    Manage the nested display contents
    '''
    def __init__(self, retcode=0):
        self.__dict__.update(
            salt.utils.color.get_colors(
                __opts__.get('color'),
                __opts__.get('color_theme')
            )
        )
        self.strip_colors = __opts__.get('strip_colors', True)
        self.retcode = retcode

    def ustring(self,
                indent,
                color,
                msg,
                prefix='',
                suffix='',
                endc=None):
        if endc is None:
            endc = self.ENDC

        indent *= ' '
        fmt = '{0}{1}{2}{3}{4}{5}'

        try:
            return fmt.format(
                indent,
                color,
                prefix,
                msg,
                endc,
                suffix)
        except UnicodeDecodeError:
            try:
                return fmt.format(
                    indent,
                    color,
                    prefix,
                    salt.utils.stringutils.to_unicode(msg),
                    endc,
                    suffix)
            except UnicodeDecodeError:
                # msg contains binary data that can't be decoded
                return str(fmt).format(  # future lint: disable=blacklisted-function
                    indent,
                    color,
                    prefix,
                    msg,
                    endc,
                    suffix)

    def display(self, ret, indent, prefix, out):
        '''
        Recursively iterate down through data structures to determine output
        '''
        if isinstance(ret, bytes):
            try:
                ret = salt.utils.stringutils.to_unicode(ret)
            except UnicodeDecodeError:
                # ret contains binary data that can't be decoded
                pass

        if ret is None or ret is True or ret is False:
            out.append(
                self.ustring(
                    indent,
                    self.LIGHT_YELLOW,
                    ret,
                    prefix=prefix
                )
            )
        # Number includes all python numbers types
        #  (float, int, long, complex, ...)
        # use repr() to get the full precision also for older python versions
        # as until about python32 it was limited to 12 digits only by default
        elif isinstance(ret, Number):
            out.append(
                self.ustring(
                    indent,
                    self.LIGHT_YELLOW,
                    repr(ret),
                    prefix=prefix
                )
            )
        elif isinstance(ret, six.string_types):
            first_line = True
            for line in ret.splitlines():
                if self.strip_colors:
                    line = salt.output.strip_esc_sequence(line)
                line_prefix = ' ' * len(prefix) if not first_line else prefix
                out.append(
                    self.ustring(
                        indent,
                        self.GREEN,
                        line,
                        prefix=line_prefix
                    )
                )
                first_line = False
        elif isinstance(ret, (list, tuple)):
            color = self.GREEN
            if self.retcode != 0:
                color = self.RED
            for ind in ret:
                if isinstance(ind, (list, tuple, Mapping)):
                    out.append(
                        self.ustring(
                            indent,
                            color,
                            '|_'
                        )
                    )
                    prefix = '' if isinstance(ind, Mapping) else '- '
                    self.display(ind, indent + 2, prefix, out)
                else:
                    self.display(ind, indent, '- ', out)
        elif isinstance(ret, Mapping):
            if indent:
                color = self.CYAN
                if self.retcode != 0:
                    color = self.RED
                out.append(
                    self.ustring(
                        indent,
                        color,
                        '----------'
                    )
                )

            # respect key ordering of ordered dicts
            if isinstance(ret, salt.utils.odict.OrderedDict):
                keys = ret.keys()
            else:
                keys = sorted(ret)
            color = self.CYAN
            if self.retcode != 0:
                color = self.RED
            for key in keys:
                val = ret[key]
                out.append(
                    self.ustring(
                        indent,
                        color,
                        key,
                        suffix=':',
                        prefix=prefix
                    )
                )
                self.display(val, indent + 4, '', out)
        return out

def output(ret, **kwargs):
    '''
    Display ret data
    '''
    # Prefer kwargs before opts
    retcode = kwargs.get('_retcode', 0)
    base_indent = kwargs.get('nested_indent', 0) \
        or __opts__.get('nested_indent', 0)
    nest = NestDisplay(retcode=retcode)
    lines = nest.display(ret, base_indent, '', [])
    try:
        return '\n'.join(lines)
    except UnicodeDecodeError:
        # output contains binary data that can't be decoded
        return str('\n').join(  # future lint: disable=blacklisted-function
            [salt.utils.stringutils.to_str(x) for x in lines]
        )
<commit_msg>Fix deprecation warning in nested outputter<commit_after># -*- coding: utf-8 -*-
'''
Recursively display nested data
===============================

This is the default outputter for most execution functions.

Example output::

    myminion:
        ----------
        foo:
            ----------
            bar:
                baz
            dictionary:
                ----------
                abc:
                    123
                def:
                    456
            list:
                - Hello
                - World
'''
from __future__ import absolute_import, print_function, unicode_literals
# Import python libs
from numbers import Number

# Import salt libs
import salt.output
import salt.utils.color
import salt.utils.odict
import salt.utils.stringutils
from salt.ext import six

try:
    from collections.abc import Mapping
except ImportError:
    from collections import Mapping

class NestDisplay(object):
    '''
    Manage the nested display contents
    '''
    def __init__(self, retcode=0):
        self.__dict__.update(
            salt.utils.color.get_colors(
                __opts__.get('color'),
                __opts__.get('color_theme')
            )
        )
        self.strip_colors = __opts__.get('strip_colors', True)
        self.retcode = retcode

    def ustring(self,
                indent,
                color,
                msg,
                prefix='',
                suffix='',
                endc=None):
        if endc is None:
            endc = self.ENDC

        indent *= ' '
        fmt = '{0}{1}{2}{3}{4}{5}'

        try:
            return fmt.format(
                indent,
                color,
                prefix,
                msg,
                endc,
                suffix)
        except UnicodeDecodeError:
            try:
                return fmt.format(
                    indent,
                    color,
                    prefix,
                    salt.utils.stringutils.to_unicode(msg),
                    endc,
                    suffix)
            except UnicodeDecodeError:
                # msg contains binary data that can't be decoded
                return str(fmt).format(  # future lint: disable=blacklisted-function
                    indent,
                    color,
                    prefix,
                    msg,
                    endc,
                    suffix)

    def display(self, ret, indent, prefix, out):
        '''
        Recursively iterate down through data structures to determine output
        '''
        if isinstance(ret, bytes):
            try:
                ret = salt.utils.stringutils.to_unicode(ret)
            except UnicodeDecodeError:
                # ret contains binary data that can't be decoded
                pass

        if ret is None or ret is True or ret is False:
            out.append(
                self.ustring(
                    indent,
                    self.LIGHT_YELLOW,
                    ret,
                    prefix=prefix
                )
            )
        # Number includes all python numbers types
        #  (float, int, long, complex, ...)
        # use repr() to get the full precision also for older python versions
        # as until about python32 it was limited to 12 digits only by default
        elif isinstance(ret, Number):
            out.append(
                self.ustring(
                    indent,
                    self.LIGHT_YELLOW,
                    repr(ret),
                    prefix=prefix
                )
            )
        elif isinstance(ret, six.string_types):
            first_line = True
            for line in ret.splitlines():
                if self.strip_colors:
                    line = salt.output.strip_esc_sequence(line)
                line_prefix = ' ' * len(prefix) if not first_line else prefix
                out.append(
                    self.ustring(
                        indent,
                        self.GREEN,
                        line,
                        prefix=line_prefix
                    )
                )
                first_line = False
        elif isinstance(ret, (list, tuple)):
            color = self.GREEN
            if self.retcode != 0:
                color = self.RED
            for ind in ret:
                if isinstance(ind, (list, tuple, Mapping)):
                    out.append(
                        self.ustring(
                            indent,
                            color,
                            '|_'
                        )
                    )
                    prefix = '' if isinstance(ind, Mapping) else '- '
                    self.display(ind, indent + 2, prefix, out)
                else:
                    self.display(ind, indent, '- ', out)
        elif isinstance(ret, Mapping):
            if indent:
                color = self.CYAN
                if self.retcode != 0:
                    color = self.RED
                out.append(
                    self.ustring(
                        indent,
                        color,
                        '----------'
                    )
                )

            # respect key ordering of ordered dicts
            if isinstance(ret, salt.utils.odict.OrderedDict):
                keys = ret.keys()
            else:
                keys = sorted(ret)
            color = self.CYAN
            if self.retcode != 0:
                color = self.RED
            for key in keys:
                val = ret[key]
                out.append(
                    self.ustring(
                        indent,
                        color,
                        key,
                        suffix=':',
                        prefix=prefix
                    )
                )
                self.display(val, indent + 4, '', out)
        return out

def output(ret, **kwargs):
    '''
    Display ret data
    '''
    # Prefer kwargs before opts
    retcode = kwargs.get('_retcode', 0)
    base_indent = kwargs.get('nested_indent', 0) \
        or __opts__.get('nested_indent', 0)
    nest = NestDisplay(retcode=retcode)
    lines = nest.display(ret, base_indent, '', [])
    try:
        return '\n'.join(lines)
    except UnicodeDecodeError:
        # output contains binary data that can't be decoded
        return str('\n').join(  # future lint: disable=blacklisted-function
            [salt.utils.stringutils.to_str(x) for x in lines]
        )
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
The Salt Cloud Runner
=====================

This runner wraps the functionality of salt cloud making salt cloud routines
available to all internal apis via the runner system
'''
from __future__ import absolute_import

# Import python libs
import logging
import os

# Import Salt libs
import salt.cloud
from salt.exceptions import SaltCloudConfigError

# Get logging started
log = logging.getLogger(__name__)

def _filter_kwargs(k):
    return dict((x, k[x]) for x in k if not x.startswith('__'))

def _get_client():
    '''
    Return cloud client
    '''
    client = salt.cloud.CloudClient(
            os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
            )
    return client

def list_sizes(provider='all'):
    '''
    List cloud provider sizes for the given providers
    '''
    client = _get_client()
    sizes = client.list_sizes(provider)
    return sizes

def list_images(provider='all'):
    '''
    List cloud provider images for the given providers
    '''
    client = _get_client()
    images = client.list_images(provider)
    return images

def list_locations(provider='all'):
    '''
    List cloud provider sizes for the given providers
    '''
    client = _get_client()
    locations = client.list_locations(provider)
    return locations

def query(query_type='list_nodes'):
    '''
    List cloud provider data for all providers
    '''
    client = _get_client()
    info = client.query(query_type)
    return info

def full_query(query_type='list_nodes_full'):
    '''
    List all available cloud provider data
    '''
    client = _get_client()
    info = client.full_query(query_type)
    return info

def select_query(query_type='list_nodes_select'):
    '''
    List selected nodes
    '''
    client = _get_client()
    info = client.select_query(query_type)
    return info

def profile(prof=None, instances=None, opts=None, **kwargs):
    '''
    Create a cloud vm with the given profile and instances, instances can be a
    list or comma-delimited string

    CLI Example:

    .. code-block:: bash

        salt-run cloud.profile prof=my-ec2 instances=node1,node2,node3
    '''
    if prof is None and 'profile' in kwargs:
        prof = kwargs['profile']

    if prof is None:
        return {'Error': 'A profile (or prof) must be defined'}

    if instances is None and 'names' in kwargs:
        instances = kwargs['names']

    if instances is None:
        return {'Error': 'One or more instances (comma-delimited) must be set'}

    client = _get_client()
    if isinstance(opts, dict):
        client.opts.update(opts)
    info = client.profile(prof, instances, **_filter_kwargs(kwargs))
    return info

def map_run(path=None, **kwargs):
    '''
    Execute a salt cloud map file
    '''
    client = _get_client()
    info = client.map_run(path, **_filter_kwargs(kwargs))
    return info

def destroy(instances):
    '''
    Destroy the named vm(s)
    '''
    client = _get_client()
    info = client.destroy(instances)
    return info

def action(func=None,
           cloudmap=None,
           instances=None,
           provider=None,
           instance=None,
           **kwargs):
    '''
    Execute a single action on the given map/provider/instance

    CLI Example:

    .. code-block:: bash

        salt-run cloud.action start my-salt-vm
    '''
    info = {}
    client = _get_client()
    try:
        info = client.action(func, cloudmap, instances, provider, instance, **_filter_kwargs(kwargs))
    except SaltCloudConfigError as err:
        log.error(err)
    return info

def create(provider, instances, opts=None, **kwargs):
    '''
    Create an instance using Salt Cloud

    CLI Example:

    .. code-block:: bash

        salt-run cloud.create my-ec2-config myinstance \
            image=ami-1624987f size='t1.micro' ssh_username=ec2-user \
            securitygroup=default delvol_on_destroy=True
    '''
    create_kwargs = {}
    for kwarg in kwargs:
        if not kwarg.startswith('__'):
            create_kwargs[kwarg] = kwargs[kwarg]
    client = _get_client()
    if isinstance(opts, dict):
        client.opts.update(opts)
    info = client.create(provider, instances, **create_kwargs)
    return info
<commit_msg>Use clean_kwargs from salt.utils<commit_after># -*- coding: utf-8 -*-
'''
The Salt Cloud Runner
=====================

This runner wraps the functionality of salt cloud making salt cloud routines
available to all internal apis via the runner system
'''
from __future__ import absolute_import

# Import python libs
import logging
import os

# Import Salt libs
import salt.cloud
import salt.utils
from salt.exceptions import SaltCloudConfigError

# Get logging started
log = logging.getLogger(__name__)

def _get_client():
    '''
    Return cloud client
    '''
    client = salt.cloud.CloudClient(
            os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
            )
    return client

def list_sizes(provider='all'):
    '''
    List cloud provider sizes for the given providers
    '''
    client = _get_client()
    sizes = client.list_sizes(provider)
    return sizes

def list_images(provider='all'):
    '''
    List cloud provider images for the given providers
    '''
    client = _get_client()
    images = client.list_images(provider)
    return images

def list_locations(provider='all'):
    '''
    List cloud provider sizes for the given providers
    '''
    client = _get_client()
    locations = client.list_locations(provider)
    return locations

def query(query_type='list_nodes'):
    '''
    List cloud provider data for all providers
    '''
    client = _get_client()
    info = client.query(query_type)
    return info

def full_query(query_type='list_nodes_full'):
    '''
    List all available cloud provider data
    '''
    client = _get_client()
    info = client.full_query(query_type)
    return info

def select_query(query_type='list_nodes_select'):
    '''
    List selected nodes
    '''
    client = _get_client()
    info = client.select_query(query_type)
    return info

def profile(prof=None, instances=None, opts=None, **kwargs):
    '''
    Create a cloud vm with the given profile and instances, instances can be a
    list or comma-delimited string

    CLI Example:

    .. code-block:: bash

        salt-run cloud.profile prof=my-ec2 instances=node1,node2,node3
    '''
    if prof is None and 'profile' in kwargs:
        prof = kwargs['profile']

    if prof is None:
        return {'Error': 'A profile (or prof) must be defined'}

    if instances is None and 'names' in kwargs:
        instances = kwargs['names']

    if instances is None:
        return {'Error': 'One or more instances (comma-delimited) must be set'}

    client = _get_client()
    if isinstance(opts, dict):
        client.opts.update(opts)
    info = client.profile(prof, instances, **salt.utils.clean_kwargs(**kwargs))
    return info

def map_run(path=None, **kwargs):
    '''
    Execute a salt cloud map file
    '''
    client = _get_client()
    info = client.map_run(path, **salt.utils.clean_kwargs(**kwargs))
    return info

def destroy(instances):
    '''
    Destroy the named vm(s)
    '''
    client = _get_client()
    info = client.destroy(instances)
    return info

def action(func=None,
           cloudmap=None,
           instances=None,
           provider=None,
           instance=None,
           **kwargs):
    '''
    Execute a single action on the given map/provider/instance

    CLI Example:

    .. code-block:: bash

        salt-run cloud.action start my-salt-vm
    '''
    info = {}
    client = _get_client()
    try:
        info = client.action(
            func,
            cloudmap,
            instances,
            provider,
            instance,
            **salt.utils.clean_kwargs(**kwargs)
        )
    except SaltCloudConfigError as err:
        log.error(err)
    return info

def create(provider, instances, opts=None, **kwargs):
    '''
    Create an instance using Salt Cloud

    CLI Example:

    .. code-block:: bash

        salt-run cloud.create my-ec2-config myinstance \
            image=ami-1624987f size='t1.micro' ssh_username=ec2-user \
            securitygroup=default delvol_on_destroy=True
    '''
    client = _get_client()
    if isinstance(opts, dict):
        client.opts.update(opts)
    info = client.create(provider, instances, **salt.utils.clean_kwargs(**kwargs))
    return info
<|endoftext|>


<commit_before>import numpy as np
from numpy import linalg as la, random as rnd, testing as np_testing
from scipy.linalg import eigvalsh, expm

from pymanopt.manifolds import SymmetricPositiveDefinite
from pymanopt.tools.multi import multiprod, multisym, multitransp
from .._test import TestCase

class TestSingleSymmetricPositiveDefiniteManifold(TestCase):
    def setUp(self):
        self.n = n = 15
        self.man = SymmetricPositiveDefinite(n)

    def test_rand(self):
        # Just test that rand returns a point on the manifold and two
        # different matrices generated by rand aren't too close together
        n = self.n
        man = self.man
        x = man.rand()

        assert np.shape(x) == (n, n)

        # Check symmetry
        np_testing.assert_allclose(x, multisym(x))

        # Check positivity of eigenvalues
        w = la.eigvalsh(x)
        assert (w > [0]).all()

    def test_dist(self):
        man = self.man
        x = man.rand()
        y = man.rand()

        # Test separability
        np_testing.assert_almost_equal(man.dist(x, x), 0.)

        # Test symmetry
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(y, x))

        # Test alternative implementation
        # from Eq 6.14 of "Positive definite matrices"
        d = np.sqrt((np.log(eigvalsh(x, y))**2).sum())
        np_testing.assert_almost_equal(man.dist(x, y), d)

        # check that dist is consistent with log
        np_testing.assert_almost_equal(man.dist(x, y),
                                       man.norm(x, man.log(x, y)))

        # Test invariance under inversion
        np_testing.assert_almost_equal(man.dist(x, y),
                                       man.dist(la.inv(y), la.inv(x)))

        # Test congruence-invariance
        a = rnd.randn(self.n, self.n)  # must be invertible
        axa = multiprod(multiprod(a, x), multitransp(a))
        aya = multiprod(multiprod(a, y), multitransp(a))
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(axa, aya))

    def test_exp(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        e = expm(la.solve(x, u))

        np_testing.assert_allclose(multiprod(x, e), man.exp(x, u))
        u = u * 1e-6
        np_testing.assert_allclose(man.exp(x, u), x + u)

    def test_randvec(self):
        # Just test that randvec returns an element of the tangent space
        # with norm 1 and that two randvecs are different.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        v = man.randvec(x)
        np_testing.assert_allclose(multisym(u), u)
        np_testing.assert_almost_equal(1, man.norm(x, u))
        assert la.norm(u - v) > 1e-3

    def test_norm(self):
        man = self.man
        x = man.rand()
        np.testing.assert_almost_equal(man.norm(np.eye(self.n), x), la.norm(x))

    def test_exp_log_inverse(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.log(x, y)
        np_testing.assert_allclose(man.exp(x, u), y)

    def test_log_exp_inverse(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.exp(x, u)
        np_testing.assert_allclose(man.log(x, y), u)

class TestMultiSymmetricPositiveDefiniteManifold(TestCase):
    def setUp(self):
        self.n = n = 10
        self.k = k = 3
        self.man = SymmetricPositiveDefinite(n, k)

    def test_dim(self):
        man = self.man
        n = self.n
        k = self.k
        np_testing.assert_equal(man.dim, 0.5 * k * n * (n+1))

    def test_typicaldist(self):
        man = self.man
        np_testing.assert_equal(man.typicaldist, np.sqrt(man.dim))

    def test_dist(self):
        # n = self.n
        man = self.man
        x = man.rand()
        y = man.rand()

        # Test separability
        np_testing.assert_almost_equal(man.dist(x, x), 0.)

        # Test symmetry
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(y, x))

    def test_inner(self):
        man = self.man
        k = self.k
        n = self.n
        x = man.rand()
        a, b = rnd.randn(2, k, n, n)
        np.testing.assert_almost_equal(np.tensordot(a,
                                       b.transpose((0,2,1)), axes=a.ndim),
                                       man.inner(x, multiprod(x, a),
                                                 multiprod(x, b)))

    def test_proj(self):
        man = self.man
        x = man.rand()
        a = rnd.randn(self.k, self.n, self.n)
        np.testing.assert_allclose(man.proj(x, a), multisym(a))

    def test_egrad2rgrad(self):
        man = self.man
        x = man.rand()
        u = rnd.randn(self.k, self.n, self.n)
        np.testing.assert_allclose(man.egrad2rgrad(x, u),
                                   multiprod(multiprod(x, multisym(u)), x))

    def test_ehess2rhess(self):
        # Use manopt's slow method
        man = self.man
        n = self.n
        k = self.k
        x = man.rand()
        egrad, ehess = rnd.randn(2, k, n, n)
        u = man.randvec(x)

        Hess = (multiprod(multiprod(x, multisym(ehess)), x) +
                2*multisym(multiprod(multiprod(u, multisym(egrad)), x)))

        # Correction factor for the non-constant metric
        Hess = Hess - multisym(multiprod(multiprod(u, multisym(egrad)), x))
        np_testing.assert_almost_equal(Hess, man.ehess2rhess(x, egrad, ehess,
                                                             u))

    def test_norm(self):
        man = self.man
        x = man.rand()
        Id = np.array(self.k * [np.eye(self.n)])
        np.testing.assert_almost_equal(man.norm(Id, x), la.norm(x))

    def test_rand(self):
        # Just test that rand returns a point on the manifold and two
        # different matrices generated by rand aren't too close together
        k = self.k
        n = self.n
        man = self.man
        x = man.rand()

        assert np.shape(x) == (k, n, n)

        # Check symmetry
        np_testing.assert_allclose(x, multisym(x))

        # Check positivity of eigenvalues
        w = la.eigvalsh(x)
        assert (w > [[0]]).all()

    def test_randvec(self):
        # Just test that randvec returns an element of the tangent space
        # with norm 1 and that two randvecs are different.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        v = man.randvec(x)
        np_testing.assert_allclose(multisym(u), u)
        np_testing.assert_almost_equal(1, man.norm(x, u))
        assert la.norm(u - v) > 1e-3

    def test_transp(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.randvec(x)
        np_testing.assert_allclose(man.transp(x, y, u), u)

    def test_exp(self):
        # Test against manopt implementation, test that for small vectors
        # exp(x, u) = x + u.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        e = np.zeros((self.k, self.n, self.n))
        for i in range(self.k):
            e[i] = expm(la.solve(x[i], u[i]))
        np_testing.assert_allclose(multiprod(x, e), man.exp(x, u))
        u = u * 1e-6
        np_testing.assert_allclose(man.exp(x, u), x + u)

    def test_retr(self):
        # Check that result is on manifold and for small vectors
        # retr(x, u) = x + u.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.retr(x, u)

        assert np.shape(y) == (self.k, self.n, self.n)
        # Check symmetry
        np_testing.assert_allclose(y, multisym(y))

        # Check positivity of eigenvalues
        w = la.eigvalsh(y)
        assert (w > [[0]]).all()

        u = u * 1e-6
        np_testing.assert_allclose(man.retr(x, u), x + u)

    def test_exp_log_inverse(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.log(x, y)
        np_testing.assert_allclose(man.exp(x, u), y)

    def test_log_exp_inverse(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.exp(x, u)
        np_testing.assert_allclose(man.log(x, y), u)
<commit_msg>Fix linting errors in psd tests<commit_after>import numpy as np
from numpy import linalg as la, random as rnd, testing as np_testing
from scipy.linalg import eigvalsh, expm

from pymanopt.manifolds import SymmetricPositiveDefinite
from pymanopt.tools.multi import multiprod, multisym, multitransp
from .._test import TestCase

class TestSingleSymmetricPositiveDefiniteManifold(TestCase):
    def setUp(self):
        self.n = n = 15
        self.man = SymmetricPositiveDefinite(n)

    def test_rand(self):
        # Just test that rand returns a point on the manifold and two
        # different matrices generated by rand aren't too close together
        n = self.n
        man = self.man
        x = man.rand()

        assert np.shape(x) == (n, n)

        # Check symmetry
        np_testing.assert_allclose(x, multisym(x))

        # Check positivity of eigenvalues
        w = la.eigvalsh(x)
        assert (w > [0]).all()

    def test_dist(self):
        man = self.man
        x = man.rand()
        y = man.rand()

        # Test separability
        np_testing.assert_almost_equal(man.dist(x, x), 0.)

        # Test symmetry
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(y, x))

        # Test alternative implementation
        # from Eq 6.14 of "Positive definite matrices"
        d = np.sqrt((np.log(eigvalsh(x, y))**2).sum())
        np_testing.assert_almost_equal(man.dist(x, y), d)

        # check that dist is consistent with log
        np_testing.assert_almost_equal(man.dist(x, y),
                                       man.norm(x, man.log(x, y)))

        # Test invariance under inversion
        np_testing.assert_almost_equal(man.dist(x, y),
                                       man.dist(la.inv(y), la.inv(x)))

        # Test congruence-invariance
        a = rnd.randn(self.n, self.n)  # must be invertible
        axa = multiprod(multiprod(a, x), multitransp(a))
        aya = multiprod(multiprod(a, y), multitransp(a))
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(axa, aya))

    def test_exp(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        e = expm(la.solve(x, u))

        np_testing.assert_allclose(multiprod(x, e), man.exp(x, u))
        u = u * 1e-6
        np_testing.assert_allclose(man.exp(x, u), x + u)

    def test_randvec(self):
        # Just test that randvec returns an element of the tangent space
        # with norm 1 and that two randvecs are different.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        v = man.randvec(x)
        np_testing.assert_allclose(multisym(u), u)
        np_testing.assert_almost_equal(1, man.norm(x, u))
        assert la.norm(u - v) > 1e-3

    def test_norm(self):
        man = self.man
        x = man.rand()
        np.testing.assert_almost_equal(man.norm(np.eye(self.n), x), la.norm(x))

    def test_exp_log_inverse(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.log(x, y)
        np_testing.assert_allclose(man.exp(x, u), y)

    def test_log_exp_inverse(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.exp(x, u)
        np_testing.assert_allclose(man.log(x, y), u)

class TestMultiSymmetricPositiveDefiniteManifold(TestCase):
    def setUp(self):
        self.n = n = 10
        self.k = k = 3
        self.man = SymmetricPositiveDefinite(n, k)

    def test_dim(self):
        man = self.man
        n = self.n
        k = self.k
        np_testing.assert_equal(man.dim, 0.5 * k * n * (n+1))

    def test_typicaldist(self):
        man = self.man
        np_testing.assert_equal(man.typicaldist, np.sqrt(man.dim))

    def test_dist(self):
        # n = self.n
        man = self.man
        x = man.rand()
        y = man.rand()

        # Test separability
        np_testing.assert_almost_equal(man.dist(x, x), 0.)

        # Test symmetry
        np_testing.assert_almost_equal(man.dist(x, y), man.dist(y, x))

    def test_inner(self):
        man = self.man
        k = self.k
        n = self.n
        x = man.rand()
        a, b = rnd.randn(2, k, n, n)
        np.testing.assert_almost_equal(np.tensordot(a,
                                       b.transpose((0, 2, 1)), axes=a.ndim),
                                       man.inner(x, multiprod(x, a),
                                                 multiprod(x, b)))

    def test_proj(self):
        man = self.man
        x = man.rand()
        a = rnd.randn(self.k, self.n, self.n)
        np.testing.assert_allclose(man.proj(x, a), multisym(a))

    def test_egrad2rgrad(self):
        man = self.man
        x = man.rand()
        u = rnd.randn(self.k, self.n, self.n)
        np.testing.assert_allclose(man.egrad2rgrad(x, u),
                                   multiprod(multiprod(x, multisym(u)), x))

    def test_ehess2rhess(self):
        # Use manopt's slow method
        man = self.man
        n = self.n
        k = self.k
        x = man.rand()
        egrad, ehess = rnd.randn(2, k, n, n)
        u = man.randvec(x)

        Hess = (multiprod(multiprod(x, multisym(ehess)), x) +
                2*multisym(multiprod(multiprod(u, multisym(egrad)), x)))

        # Correction factor for the non-constant metric
        Hess = Hess - multisym(multiprod(multiprod(u, multisym(egrad)), x))
        np_testing.assert_almost_equal(Hess, man.ehess2rhess(x, egrad, ehess,
                                                             u))

    def test_norm(self):
        man = self.man
        x = man.rand()
        Id = np.array(self.k * [np.eye(self.n)])
        np.testing.assert_almost_equal(man.norm(Id, x), la.norm(x))

    def test_rand(self):
        # Just test that rand returns a point on the manifold and two
        # different matrices generated by rand aren't too close together
        k = self.k
        n = self.n
        man = self.man
        x = man.rand()

        assert np.shape(x) == (k, n, n)

        # Check symmetry
        np_testing.assert_allclose(x, multisym(x))

        # Check positivity of eigenvalues
        w = la.eigvalsh(x)
        assert (w > [[0]]).all()

    def test_randvec(self):
        # Just test that randvec returns an element of the tangent space
        # with norm 1 and that two randvecs are different.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        v = man.randvec(x)
        np_testing.assert_allclose(multisym(u), u)
        np_testing.assert_almost_equal(1, man.norm(x, u))
        assert la.norm(u - v) > 1e-3

    def test_transp(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.randvec(x)
        np_testing.assert_allclose(man.transp(x, y, u), u)

    def test_exp(self):
        # Test against manopt implementation, test that for small vectors
        # exp(x, u) = x + u.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        e = np.zeros((self.k, self.n, self.n))
        for i in range(self.k):
            e[i] = expm(la.solve(x[i], u[i]))
        np_testing.assert_allclose(multiprod(x, e), man.exp(x, u))
        u = u * 1e-6
        np_testing.assert_allclose(man.exp(x, u), x + u)

    def test_retr(self):
        # Check that result is on manifold and for small vectors
        # retr(x, u) = x + u.
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.retr(x, u)

        assert np.shape(y) == (self.k, self.n, self.n)
        # Check symmetry
        np_testing.assert_allclose(y, multisym(y))

        # Check positivity of eigenvalues
        w = la.eigvalsh(y)
        assert (w > [[0]]).all()

        u = u * 1e-6
        np_testing.assert_allclose(man.retr(x, u), x + u)

    def test_exp_log_inverse(self):
        man = self.man
        x = man.rand()
        y = man.rand()
        u = man.log(x, y)
        np_testing.assert_allclose(man.exp(x, u), y)

    def test_log_exp_inverse(self):
        man = self.man
        x = man.rand()
        u = man.randvec(x)
        y = man.exp(x, u)
        np_testing.assert_allclose(man.log(x, y), u)
<|endoftext|>