<commit_before>#!/usr/bin/python3
import sys
import io
import logging
import traceback
import tempfile
import re
from PyQt5.QtGui import QClipboard
from PyQt5.QtCore import QUrl, QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog, QProgressDialog, QPlainTextEdit
from PyQt5.QtQml import QQmlApplicationEngine
from .resources import qml_file
from .loadOrder import loadorder
from . import version

gui_logger = logging.getLogger('mlox.gui')

def colorize_text(text):
    """
    Some things are better in color.
    This function takes normal text, and applies html style tags where appropriate.
    """
    bg_colors = {
        "low":      "\g<0>",
        "medium":   "\g<0>",
        "high":     "\g<0>",
        "green":    "\g<0>",
        "yellow":   "\g<0>",
        "red":      "\g<0>"
    }

    highlighters = [
        (re.compile(r'<hide>(.*)</hide>'),"\g<1>"),         # Spoilers require Highlighting
        (re.compile(r'^(\[CONFLICT\])', re.MULTILINE), bg_colors["red"]),
        (re.compile(r'(https?://[^\s]*)', re.IGNORECASE), "<a href='\g<0>'>\g<0>"),                                 # URLs
        (re.compile(r"^(\s*\|?\s*!{1}[^!].*)$", re.MULTILINE), bg_colors["low"]),                                       # '!' in mlox_base.txt
        (re.compile(r"^(\s*\|?\s*!{2}.*)$", re.MULTILINE), bg_colors["medium"]),                                        # '!!' in mlox_base.txt
        (re.compile(r"^(\s*\|?\s*!{3}.*)$", re.MULTILINE), bg_colors["high"]),                                          # '!!!' in mlox_base.txt
        (re.compile(r'^(WARNING:.*)', re.MULTILINE),bg_colors["yellow"]),
        (re.compile(r'^(ERROR:.*)', re.MULTILINE),bg_colors["red"]),
        (re.compile(r'(\[Plugins already in sorted order. No sorting needed!\])', re.IGNORECASE), bg_colors["green"]),
        (re.compile(r'^(\*\d+\*\s\S*\.es[mp])', re.MULTILINE), bg_colors["yellow"])                                     # Changed mod order
    ]
    for (regex,replacement_string) in highlighters:
        text = regex.sub(replacement_string, text)

    text = text.replace('\n', '
\n')
    return text

class ScrollableDialog(QDialog):
    "A dialog box that contains scrollable text."
    def __init__(self):
        QDialog.__init__(self)
        self.setModal(False)

        self.inner_text = QPlainTextEdit(self)
        self.inner_text.setReadOnly(True)

        self.setFixedSize(400, 600)
        self.inner_text.setFixedSize(400, 600)

    def setText(self, new_text):
        self.inner_text.setPlainText(new_text)

class CustomProgressDialog(QProgressDialog):
    """
    A custom version of the progress dialog
    It's designed to have the same update mechanism as a wx.ProgressDialog
    """

    def __init__(self):
        QProgressDialog.__init__(self)
        self.setCancelButton(None)
        self.forceShow()
        self.open()

    def Update(self, percent, label):
        self.setLabelText(label)
        self.setValue(percent)

class MloxGui(QObject):
    """Mlox's GUI (Using PyQt5"""

    lo = None  # Load order

    # Signals (use emit(...) to change values)
    enable_updateButton = pyqtSignal(bool, arguments=['is_enabled'])
    set_status = pyqtSignal(str, arguments=['text'])
    set_message = pyqtSignal(str, arguments=['text'])
    set_new = pyqtSignal(str, arguments=['text'])
    set_old = pyqtSignal(str, arguments=['text'])

    def __init__(self):
        QObject.__init__(self)
        self.Dbg = io.StringIO()  # debug output
        self.Stats = io.StringIO()  # status output
        self.New = ""  # new sorted loadorder
        self.Old = ""  # old original loadorder
        self.Msg = ""  # messages output
        self.can_update = True  # If the load order can be saved or not

        # Set up logging
        dbg_formatter = logging.Formatter('%(levelname)s (%(name)s): %(message)s')
        dbg_log_stream = logging.StreamHandler(stream=self.Dbg)
        dbg_log_stream.setFormatter(dbg_formatter)
        dbg_log_stream.setLevel(logging.DEBUG)
        logging.getLogger('').addHandler(dbg_log_stream)
        gui_formatter = logging.Formatter('%(levelname)s: %(message)s')
        gui_log_stream = logging.StreamHandler(stream=self.Stats)
        gui_log_stream.setFormatter(gui_formatter)
        gui_log_stream.setLevel(logging.WARNING)
        logging.getLogger('').addHandler(gui_log_stream)

        # This is a little cheat so the INFO messages still display, but without the tag
        class filterInfo():
            def filter(self, record):
                return record.levelno == logging.INFO

        info_formatter = logging.Formatter('%(message)s')
        gui_info_stream = logging.StreamHandler(stream=self.Stats)
        gui_info_stream.setFormatter(info_formatter)
        gui_info_stream.setLevel(logging.INFO)
        gui_info_stream.addFilter(filterInfo())
        logging.getLogger('').addHandler(gui_info_stream)

    def start(self):
        """Display the GUI"""
        myApp = QApplication(sys.argv)
        myEngine = QQmlApplicationEngine()
        sys.excepthook = lambda typ, val, tb: self.error_handler(typ, val, tb)
        myEngine.rootContext().setContextProperty("python", self)  # Need to set this before loading
        myEngine.load(QUrl(qml_file))
        # These two are hacks, because getting them in the __init__ and RAII working isn't
        self.debug_window = ScrollableDialog()
        self.clipboard = myApp.clipboard()

        self.analyze_loadorder(None)

        sys.exit(myApp.exec_())

    def error_handler(self, type, value, tb):
        """
        Since a command line is not normally available to a GUI application, we need to display errors to the user.
        These are only errors that would cause the program to crash, so have the program exit when the dialog box is closed.
        """
        error_box = ScrollableDialog()
        error_box.setText(version.version_info() + "\n" + "".join(traceback.format_exception(type, value, tb)))
        error_box.exec_()
        sys.exit(1)

    def display(self):
        "Update the GUI after an operation"
        self.debug_window.setText(self.Dbg.getvalue())
        self.enable_updateButton.emit(self.can_update)
        self.set_status.emit(colorize_text(self.Stats.getvalue()))
        self.set_message.emit(colorize_text(self.Msg))
        self.set_new.emit(colorize_text(self.New))
        self.set_old.emit(colorize_text(self.Old))

    def analyze_loadorder(self, fromfile):
        """
        This is where the magic happens
        If fromfile is None, then it operates out of the current directory.
        """

        # Clear all the outputs (except Dbg)
        self.Stats.truncate(0)
        self.Stats.seek(0)
        self.New = ""
        self.Old = ""
        self.Msg = ""

        gui_logger.info("Version: %s\t\t\t\t %s " % (version.full_version(), "Hello!"))
        self.lo = loadorder()
        if fromfile != None:
            self.lo.read_from_file(fromfile)
        else:
            self.lo.get_active_plugins()
        progress = CustomProgressDialog()
        self.Msg = self.lo.update(progress)
        # self.Msg = self.lo.update()
        # TODO: Have update always return as string, so this isn't needed
        if not self.Msg:
            self.Msg = ""
        for p in self.lo.get_original_order():
            self.Old += p + '\n'
        for p in self.lo.get_new_order():
            self.New += p + '\n'
        if self.lo.is_sorted:
            self.can_update = False

        # Go ahead and display everything
        self.display()

    @pyqtSlot()
    def show_debug_window(self):
        """
        Updates the text of the debug window, then shows it.
        Note:  The debug window is also updated every time `self.display()` is called
        """
        self.debug_window.setText(self.Dbg.getvalue())
        self.debug_window.open()

    @pyqtSlot()
    def paste_handler(self):
        """Open a load order from the clipboard"""
        file_handle = tempfile.NamedTemporaryFile()
        file_handle.write(self.clipboard.text().encode('utf8'))
        file_handle.seek(0)
        self.analyze_loadorder(file_handle.name)

    @pyqtSlot(str)
    def open_file(self, file_path):
        """Analyze the file passed in"""
        file_path = QUrl(file_path).path()  # Adjust from a file:// format to a regular path
        self.analyze_loadorder(file_path)

    @pyqtSlot()
    def reload(self):
        self.can_update = True
        # TODO:  Properly handle reloading from a file
        self.analyze_loadorder(None)

    @pyqtSlot()
    def commit(self):
        """Write the requested changes to the file/directory"""
        if not self.can_update:
            gui_logger.error("Attempted an update, when no update is possible/needed.")
            self.display()
            return
        self.lo.write_new_order()
        gui_logger.info("[LOAD ORDER UPDATED!]")
        self.can_update = False
        self.display()

    @pyqtSlot()
    def about_handler(self):
        """
        Show information about mlox
        """
        about_box = ScrollableDialog()
        about_box.setText(version.about())
        about_box.exec_()

MloxGui().start()
<commit_msg>qtGui.py:  Moved error handler out of MloxGui<commit_after>#!/usr/bin/python3
import sys
import io
import logging
import traceback
import tempfile
import re
from PyQt5.QtGui import QClipboard
from PyQt5.QtCore import QUrl, QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog, QProgressDialog, QPlainTextEdit
from PyQt5.QtQml import QQmlApplicationEngine
from .resources import qml_file
from .loadOrder import loadorder
from . import version

gui_logger = logging.getLogger('mlox.gui')

def colorize_text(text):
    """
    Some things are better in color.
    This function takes normal text, and applies html style tags where appropriate.
    """
    bg_colors = {
        "low":      "\g<0>",
        "medium":   "\g<0>",
        "high":     "\g<0>",
        "green":    "\g<0>",
        "yellow":   "\g<0>",
        "red":      "\g<0>"
    }

    highlighters = [
        (re.compile(r'<hide>(.*)</hide>'),"\g<1>"),         # Spoilers require Highlighting
        (re.compile(r'^(\[CONFLICT\])', re.MULTILINE), bg_colors["red"]),
        (re.compile(r'(https?://[^\s]*)', re.IGNORECASE), "<a href='\g<0>'>\g<0>"),                                 # URLs
        (re.compile(r"^(\s*\|?\s*!{1}[^!].*)$", re.MULTILINE), bg_colors["low"]),                                       # '!' in mlox_base.txt
        (re.compile(r"^(\s*\|?\s*!{2}.*)$", re.MULTILINE), bg_colors["medium"]),                                        # '!!' in mlox_base.txt
        (re.compile(r"^(\s*\|?\s*!{3}.*)$", re.MULTILINE), bg_colors["high"]),                                          # '!!!' in mlox_base.txt
        (re.compile(r'^(WARNING:.*)', re.MULTILINE),bg_colors["yellow"]),
        (re.compile(r'^(ERROR:.*)', re.MULTILINE),bg_colors["red"]),
        (re.compile(r'(\[Plugins already in sorted order. No sorting needed!\])', re.IGNORECASE), bg_colors["green"]),
        (re.compile(r'^(\*\d+\*\s\S*\.es[mp])', re.MULTILINE), bg_colors["yellow"])                                     # Changed mod order
    ]
    for (regex,replacement_string) in highlighters:
        text = regex.sub(replacement_string, text)

    text = text.replace('\n', '
\n')
    return text

class ScrollableDialog(QDialog):
    "A dialog box that contains scrollable text."
    def __init__(self):
        QDialog.__init__(self)
        self.setModal(False)

        self.inner_text = QPlainTextEdit(self)
        self.inner_text.setReadOnly(True)

        self.setFixedSize(400, 600)
        self.inner_text.setFixedSize(400, 600)

    def setText(self, new_text):
        self.inner_text.setPlainText(new_text)

class CustomProgressDialog(QProgressDialog):
    """
    A custom version of the progress dialog
    It's designed to have the same update mechanism as a wx.ProgressDialog
    """

    def __init__(self):
        QProgressDialog.__init__(self)
        self.setCancelButton(None)
        self.forceShow()
        self.open()

    def Update(self, percent, label):
        self.setLabelText(label)
        self.setValue(percent)

def error_handler(self, type, value, tb):
    """
    Since a command line is not normally available to a GUI application, we need to display errors to the user.
    These are only errors that would cause the program to crash, so have the program exit when the dialog box is closed.
    """
    error_box = ScrollableDialog()
    error_box.setText(version.version_info() + "\n" + "".join(traceback.format_exception(type, value, tb)))
    error_box.exec_()
    sys.exit(1)

class MloxGui(QObject):
    """Mlox's GUI (Using PyQt5)"""

    lo = None  # Load order

    # Signals (use emit(...) to change values)
    enable_updateButton = pyqtSignal(bool, arguments=['is_enabled'])
    set_status = pyqtSignal(str, arguments=['text'])
    set_message = pyqtSignal(str, arguments=['text'])
    set_new = pyqtSignal(str, arguments=['text'])
    set_old = pyqtSignal(str, arguments=['text'])

    def __init__(self):
        QObject.__init__(self)
        self.Dbg = io.StringIO()  # debug output
        self.Stats = io.StringIO()  # status output
        self.New = ""  # new sorted loadorder
        self.Old = ""  # old original loadorder
        self.Msg = ""  # messages output
        self.can_update = True  # If the load order can be saved or not

        # Set up logging
        dbg_formatter = logging.Formatter('%(levelname)s (%(name)s): %(message)s')
        dbg_log_stream = logging.StreamHandler(stream=self.Dbg)
        dbg_log_stream.setFormatter(dbg_formatter)
        dbg_log_stream.setLevel(logging.DEBUG)
        logging.getLogger('').addHandler(dbg_log_stream)
        gui_formatter = logging.Formatter('%(levelname)s: %(message)s')
        gui_log_stream = logging.StreamHandler(stream=self.Stats)
        gui_log_stream.setFormatter(gui_formatter)
        gui_log_stream.setLevel(logging.WARNING)
        logging.getLogger('').addHandler(gui_log_stream)

        # This is a little cheat so the INFO messages still display, but without the tag
        class filterInfo():
            def filter(self, record):
                return record.levelno == logging.INFO

        info_formatter = logging.Formatter('%(message)s')
        gui_info_stream = logging.StreamHandler(stream=self.Stats)
        gui_info_stream.setFormatter(info_formatter)
        gui_info_stream.setLevel(logging.INFO)
        gui_info_stream.addFilter(filterInfo())
        logging.getLogger('').addHandler(gui_info_stream)

    def start(self):
        """Display the GUI"""
        myApp = QApplication(sys.argv)
        myEngine = QQmlApplicationEngine()
        sys.excepthook = lambda typ, val, tb: error_handler(typ, val, tb)
        myEngine.rootContext().setContextProperty("python", self)  # Need to set this before loading
        myEngine.load(QUrl(qml_file))
        # These two are hacks, because getting them in the __init__ and RAII working isn't
        self.debug_window = ScrollableDialog()
        self.clipboard = myApp.clipboard()

        self.analyze_loadorder(None)

        sys.exit(myApp.exec_())

    def display(self):
        "Update the GUI after an operation"
        self.debug_window.setText(self.Dbg.getvalue())
        self.enable_updateButton.emit(self.can_update)
        self.set_status.emit(colorize_text(self.Stats.getvalue()))
        self.set_message.emit(colorize_text(self.Msg))
        self.set_new.emit(colorize_text(self.New))
        self.set_old.emit(colorize_text(self.Old))

    def analyze_loadorder(self, fromfile):
        """
        This is where the magic happens
        If fromfile is None, then it operates out of the current directory.
        """

        # Clear all the outputs (except Dbg)
        self.Stats.truncate(0)
        self.Stats.seek(0)
        self.New = ""
        self.Old = ""
        self.Msg = ""

        gui_logger.info("Version: %s\t\t\t\t %s " % (version.full_version(), "Hello!"))
        self.lo = loadorder()
        if fromfile != None:
            self.lo.read_from_file(fromfile)
        else:
            self.lo.get_active_plugins()
        progress = CustomProgressDialog()
        self.Msg = self.lo.update(progress)
        # self.Msg = self.lo.update()
        # TODO: Have update always return as string, so this isn't needed
        if not self.Msg:
            self.Msg = ""
        for p in self.lo.get_original_order():
            self.Old += p + '\n'
        for p in self.lo.get_new_order():
            self.New += p + '\n'
        if self.lo.is_sorted:
            self.can_update = False

        # Go ahead and display everything
        self.display()

    @pyqtSlot()
    def show_debug_window(self):
        """
        Updates the text of the debug window, then shows it.
        Note:  The debug window is also updated every time `self.display()` is called
        """
        self.debug_window.setText(self.Dbg.getvalue())
        self.debug_window.open()

    @pyqtSlot()
    def paste_handler(self):
        """Open a load order from the clipboard"""
        file_handle = tempfile.NamedTemporaryFile()
        file_handle.write(self.clipboard.text().encode('utf8'))
        file_handle.seek(0)
        self.analyze_loadorder(file_handle.name)

    @pyqtSlot(str)
    def open_file(self, file_path):
        """Analyze the file passed in"""
        file_path = QUrl(file_path).path()  # Adjust from a file:// format to a regular path
        self.analyze_loadorder(file_path)

    @pyqtSlot()
    def reload(self):
        self.can_update = True
        # TODO:  Properly handle reloading from a file
        self.analyze_loadorder(None)

    @pyqtSlot()
    def commit(self):
        """Write the requested changes to the file/directory"""
        if not self.can_update:
            gui_logger.error("Attempted an update, when no update is possible/needed.")
            self.display()
            return
        self.lo.write_new_order()
        gui_logger.info("[LOAD ORDER UPDATED!]")
        self.can_update = False
        self.display()

    @pyqtSlot()
    def about_handler(self):
        """
        Show information about mlox
        """
        about_box = ScrollableDialog()
        about_box.setText(version.about())
        about_box.exec_()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# path hack
from os import environ
from os.path import abspath, dirname, join
import sys
PROJ_DIR = abspath(join(dirname(__file__), '..', '..'))
sys.path.insert(0, PROJ_DIR)
environ.setdefault('DJANGO_SETTINGS_MODULE', 'dodger.settings.dev')

import codecs
import csv

from django.contrib.auth.models import User

from app.models import Sku

def import_purchase_orders(filename):
    with codecs.open(filename, 'r', encoding='utf8') as fh:
        reader = csv.DictReader(fh)

        for row in reader:

            for key, value in row.iteritems():
                row[key] = ''.join([c for c in value if ord(c) < 128])
                if not len(value):
                    row[key] = None

            skuid = row['sku']
            supplier = row['supplier']

            sku = Sku.objects.get(id=skuid)
            sku.supplier_sku = supplier
            sku.save()
            print '%s: %s' % (sku.id, sku.supplier_sku)

if __name__ == '__main__':
    import sys
    filename = sys.argv[1]
    import_purchase_orders(filename)
<commit_msg>updated settings for prod<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# path hack
from os import environ
from os.path import abspath, dirname, join
import sys
PROJ_DIR = abspath(join(dirname(__file__), '..', '..'))
sys.path.insert(0, PROJ_DIR)
environ.setdefault('DJANGO_SETTINGS_MODULE', 'dodger.settings.prod')

import codecs
import csv

from django.contrib.auth.models import User

from app.models import Sku

def import_purchase_orders(filename):
    with codecs.open(filename, 'r', encoding='utf8') as fh:
        reader = csv.DictReader(fh)

        for row in reader:

            for key, value in row.iteritems():
                row[key] = ''.join([c for c in value if ord(c) < 128])
                if not len(value):
                    row[key] = None

            skuid = row['sku']
            supplier = row['supplier']

            sku = Sku.objects.get(id=skuid)
            sku.supplier_sku = supplier
            sku.save()
            print '%s: %s' % (sku.id, sku.supplier_sku)

if __name__ == '__main__':
    import sys
    filename = sys.argv[1]
    import_purchase_orders(filename)
<|endoftext|>


<commit_before>from setuptools import setup

DESCRIPTION = "A simple Route53 API for Python 2.7/3.x, powered by requests."

LONG_DESCRIPTION = open('README.rst').read()

CLASSIFIERS = [
    'Development Status :: 5 - Production/Stable',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: BSD License',
    'Operating System :: OS Independent',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Topic :: Software Development :: Libraries :: Python Modules',
]

setup(
    name='route53',
    version='1.0',
    packages=[
        'route53',
        'route53.xml_parsers',
        'route53.xml_generators',
    ],
    author='Gregory Taylor',
    author_email='gtaylor@gc-taylor.com',
    url='https://github.com/gtaylor/python-route53',
    license='BSD',
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    platforms=['any'],
    classifiers=CLASSIFIERS,
    install_requires=['requests', 'lxml', 'pytz'],
)
<commit_msg>Bump version to 1.0.1<commit_after>from setuptools import setup

DESCRIPTION = "A simple Route53 API for Python 2.7/3.x, powered by requests."

LONG_DESCRIPTION = open('README.rst').read()

CLASSIFIERS = [
    'Development Status :: 5 - Production/Stable',
    'Intended Audience :: Developers',
    'License :: OSI Approved :: BSD License',
    'Operating System :: OS Independent',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Topic :: Software Development :: Libraries :: Python Modules',
]

setup(
    name='route53',
    version='1.0.1',
    packages=[
        'route53',
        'route53.xml_parsers',
        'route53.xml_generators',
    ],
    author='Gregory Taylor',
    author_email='gtaylor@gc-taylor.com',
    url='https://github.com/gtaylor/python-route53',
    license='BSD',
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    platforms=['any'],
    classifiers=CLASSIFIERS,
    install_requires=['requests', 'lxml', 'pytz'],
)
<|endoftext|>


<commit_before># Authors: Alexandre Gramfort <gramfort@nmr.mgh.harvard.edu>
#          Martin Luessi <mluessi@nmr.mgh.harvard.edu>
#          Eric Larson <larson.eric.d@gmail.com>
#          Denis Egnemann <d.engemann@fz-juelich.de>
# License: BSD Style.

import os
import os.path as op
import shutil
from warnings import warn

from .. import __version__ as mne_version
from ..utils import get_config, set_config, _fetch_file, logger

_doc = """Get path to local copy of {name} dataset

    Parameters
    ----------
    path : None | str
        Location of where to look for the {name} dataset.
        If None, the environment variable or config parameter
        {conf} is used. If it doesn't exist, the
        "mne-python/examples" directory is used. If the sample dataset
        is not found under the given path (e.g., as
        "mne-python/examples/MNE-{name}-data"), the data
        will be automatically downloaded to the specified folder.
    force_update : bool
        Force update of the sample dataset even if a local copy exists.
    update_path : bool | None
        If True, set the {conf} in mne-python
        config to the given path. If None, the user is prompted.
    download : bool
        If False and the {name} dataset has not been downloaded yet,
        it will not be downloaded and the path will be returned as
        '' (empty string). This is mostly used for debugging purposes
        and can be safely ignored by most users.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    path : str
        Path to {name} dataset directory.
"""

def _dataset_version(path, name):
    """Get the version of the dataset"""
    ver_fname = op.join(path, 'version.txt')
    if op.exists(ver_fname):
        fid = open(ver_fname, 'r')
        version = fid.readline().strip()  # version is on first line
        fid.close()
    else:
        # Sample dataset versioning was introduced after 0.3
        # SPM dataset was introduced after 0.6
        version = '0.3' if name == 'sample' else '0.6'

    return version

def _data_path(path=None, force_update=False, update_path=True,
               download=True, name=None, verbose=None):
    """Aux function
    """
    key = {'sample': 'MNE_DATASETS_SAMPLE_PATH',
           'spm': 'MNE_DATASETS_SPM_FACE_PATH'}[name]

    if path is None:
        # use an intelligent guess if it's not defined
        def_path = op.abspath(op.join(op.dirname(__file__),
                                      '..', 'examples'))

        path = get_config(key, def_path)
        # use the same for all datasets
        if not os.path.exists(path):
            path = def_path

    if not isinstance(path, basestring):
        raise ValueError('path must be a string or None')

    if name == 'sample':
        archive_name = "MNE-sample-data-processed.tar.gz"
        url = "ftp://surfer.nmr.mgh.harvard.edu/pub/data/" + archive_name
        folder_name = "MNE-sample-data"
        folder_path = op.join(path, folder_name)
        rm_archive = False
    elif name == 'spm':
        archive_name = 'MNE-spm-face.tar.gz'
        url = 'ftp://surfer.nmr.mgh.harvard.edu/pub/data/MNE/' + archive_name
        folder_name = "MNE-spm-face-data"
        folder_path = op.join(path, folder_name)
        rm_archive = False
    else:
        raise ValueError('Sorry, the dataset "%s" is not known.' % name)

    martinos_path = '/cluster/fusion/sample_data/' + archive_name
    neurospin_path = '/neurospin/tmp/gramfort/' + archive_name

    if not op.exists(folder_path) and not download:
        return ''

    if not op.exists(folder_path) or force_update:
        logger.info('Sample data archive %s not found at:\n%s\n'
                    'It will be downloaded and extracted at this location.'
                    % (archive_name, folder_path))

        if op.exists(martinos_path):
            archive_name = martinos_path
        elif op.exists(neurospin_path):
            archive_name = neurospin_path
        else:
            archive_name = op.join(path, archive_name)
            rm_archive = True
            if op.exists(archive_name):
                msg = ('Archive already exists at %r. Overwrite it '
                       '(y/[n])? ' % archive_name)
                answer = raw_input(msg)
                if answer.lower() == 'y':
                    os.remove(archive_name)
                else:
                    raise IOError('Archive file already exists at target '
                                  'location %r.' % archive_name)

            _fetch_file(url, archive_name, print_destination=False)

        if op.exists(folder_path):
            shutil.rmtree(folder_path)

        import tarfile
        # note that we use print statements here because these processes
        # are interactive
        logger.info('Decompressiong the archive: ' + archive_name)
        tarfile.open(archive_name, 'r:gz').extractall(path=path)
        if rm_archive:
            os.remove(archive_name)

    path = op.abspath(path)
    if update_path is None:
        if get_config(key, '') != path:
            update_path = True
            msg = ('Do you want to set the path:\n    %s\nas the default '
                   'sample dataset path in the mne-python config [y]/n? '
                   % path)
            answer = raw_input(msg)
            if answer.lower() == 'n':
                update_path = False
        else:
            update_path = False

    if update_path is True:
        set_config(key, path)

    path = op.join(path, folder_name)

    # compare the version of the Sample dataset and mne
    data_version = _dataset_version(path, name)
    try:
        from distutils.version import LooseVersion
    except:
        warn('Could not determine sample dataset version; dataset could\n'
             'be out of date. Please install the "distutils" package.')
    else:
        if LooseVersion(data_version) < LooseVersion(mne_version):
            warn('Sample dataset (version %s) is older than mne-python '
                 '(version %s). If the examples fail, you may need to update '
                 'the sample dataset by using force_update=True'
                 % (data_version, mne_version))

    return path

def has_dataset(name):
    """Helper for sample dataset presence"""
    endswith = {'sample': 'MNE-sample-data',
                'spm': 'MNE-spm-face-data'}[name]
    if _data_path(download=False, name=name).endswith(endswith):
        return True
    else:
        return False<commit_msg>ENH: better unpacking<commit_after># Authors: Alexandre Gramfort <gramfort@nmr.mgh.harvard.edu>
#          Martin Luessi <mluessi@nmr.mgh.harvard.edu>
#          Eric Larson <larson.eric.d@gmail.com>
#          Denis Egnemann <d.engemann@fz-juelich.de>
# License: BSD Style.

import os
import os.path as op
import shutil
from warnings import warn

from .. import __version__ as mne_version
from ..utils import get_config, set_config, _fetch_file, logger

_doc = """Get path to local copy of {name} dataset

    Parameters
    ----------
    path : None | str
        Location of where to look for the {name} dataset.
        If None, the environment variable or config parameter
        {conf} is used. If it doesn't exist, the
        "mne-python/examples" directory is used. If the sample dataset
        is not found under the given path (e.g., as
        "mne-python/examples/MNE-{name}-data"), the data
        will be automatically downloaded to the specified folder.
    force_update : bool
        Force update of the sample dataset even if a local copy exists.
    update_path : bool | None
        If True, set the {conf} in mne-python
        config to the given path. If None, the user is prompted.
    download : bool
        If False and the {name} dataset has not been downloaded yet,
        it will not be downloaded and the path will be returned as
        '' (empty string). This is mostly used for debugging purposes
        and can be safely ignored by most users.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    path : str
        Path to {name} dataset directory.
"""

def _dataset_version(path, name):
    """Get the version of the dataset"""
    ver_fname = op.join(path, 'version.txt')
    if op.exists(ver_fname):
        fid = open(ver_fname, 'r')
        version = fid.readline().strip()  # version is on first line
        fid.close()
    else:
        # Sample dataset versioning was introduced after 0.3
        # SPM dataset was introduced after 0.6
        version = '0.3' if name == 'sample' else '0.6'

    return version

def _data_path(path=None, force_update=False, update_path=True,
               download=True, name=None, verbose=None):
    """Aux function
    """
    key = {'sample': 'MNE_DATASETS_SAMPLE_PATH',
           'spm': 'MNE_DATASETS_SPM_FACE_PATH'}[name]

    if path is None:
        # use an intelligent guess if it's not defined
        def_path = op.abspath(op.join(op.dirname(__file__),
                                      '..', 'examples'))

        path = get_config(key, def_path)
        # use the same for all datasets
        if not os.path.exists(path):
            path = def_path

    if not isinstance(path, basestring):
        raise ValueError('path must be a string or None')

    if name == 'sample':
        archive_name = "MNE-sample-data-processed.tar.gz"
        url = "ftp://surfer.nmr.mgh.harvard.edu/pub/data/" + archive_name
        folder_name = "MNE-sample-data"
        folder_path = op.join(path, folder_name)
        rm_archive = False
    elif name == 'spm':
        archive_name = 'MNE-spm-face.tar.gz'
        url = 'ftp://surfer.nmr.mgh.harvard.edu/pub/data/MNE/' + archive_name
        folder_name = "MNE-spm-face-data"
        folder_path = op.join(path, folder_name)
        rm_archive = False
    else:
        raise ValueError('Sorry, the dataset "%s" is not known.' % name)

    martinos_path = '/cluster/fusion/sample_data/' + archive_name
    neurospin_path = '/neurospin/tmp/gramfort/' + archive_name

    if not op.exists(folder_path) and not download:
        return ''

    if not op.exists(folder_path) or force_update:
        logger.info('Sample data archive %s not found at:\n%s\n'
                    'It will be downloaded and extracted at this location.'
                    % (archive_name, folder_path))

        if op.exists(martinos_path):
            archive_name = martinos_path
        elif op.exists(neurospin_path):
            archive_name = neurospin_path
        else:
            archive_name = op.join(path, archive_name)
            rm_archive = True
            if op.exists(archive_name):
                msg = ('Archive already exists at %r. Overwrite it '
                       '(y/[n])? ' % archive_name)
                answer = raw_input(msg)
                if answer.lower() == 'y':
                    os.remove(archive_name)
                else:
                    raise IOError('Archive file already exists at target '
                                  'location %r.' % archive_name)

            _fetch_file(url, archive_name, print_destination=False)

        if op.exists(folder_path):
            shutil.rmtree(folder_path)

        import tarfile
        # note that we use print statements here because these processes
        # are interactive
        logger.info('Decompressiong the archive: ' + archive_name)
        logger.info('... pleasd be patient, this can take some time')
        for ext in ['gz', 'bz2']:
            try:
                tarfile.open(archive_name, 'r:%s' % ext).extractall(path=path)
            except tarfile.ReadError, err:
                logger.info('%s is %s trying "bz2"' % (archive_name, ext))

        if rm_archive:
            os.remove(archive_name)

    path = op.abspath(path)
    if update_path is None:
        if get_config(key, '') != path:
            update_path = True
            msg = ('Do you want to set the path:\n    %s\nas the default '
                   'sample dataset path in the mne-python config [y]/n? '
                   % path)
            answer = raw_input(msg)
            if answer.lower() == 'n':
                update_path = False
        else:
            update_path = False

    if update_path is True:
        set_config(key, path)

    path = op.join(path, folder_name)

    # compare the version of the Sample dataset and mne
    data_version = _dataset_version(path, name)
    try:
        from distutils.version import LooseVersion
    except:
        warn('Could not determine sample dataset version; dataset could\n'
             'be out of date. Please install the "distutils" package.')
    else:
        if LooseVersion(data_version) < LooseVersion(mne_version):
            warn('Sample dataset (version %s) is older than mne-python '
                 '(version %s). If the examples fail, you may need to update '
                 'the sample dataset by using force_update=True'
                 % (data_version, mne_version))

    return path

def has_dataset(name):
    """Helper for sample dataset presence"""
    endswith = {'sample': 'MNE-sample-data',
                'spm': 'MNE-spm-face-data'}[name]
    if _data_path(download=False, name=name).endswith(endswith):
        return True
    else:
        return False<|endoftext|>


<commit_before>import odl
import tensorflow as tf
import numpy as np

__all__ = ('as_tensorflow_layer', 'TensorflowSpace', 'TensorflowSpaceOperator')

def as_tensorflow_layer(odl_op, name='ODLOperator', differentiable=True):
    """Convert ``Operator`` to tensorflow layer.

    Parameters
    ----------
    odl_op : `Operator`
        The operator that should be wrapped to a tensorflow layer.
    name : str
        Tensorflow name of the operator.
    differentiable : boolean
        True if the operator should be differentiable, otherwise assumes that
        the derivative is everywhere zero.

    Returns
    -------
    tensorflow_layer : callable
        Callable that, when called with an `tensorflow.Tensor` of shape
        `(n, *odl_op.domain.shape, 1)` returns a tensor of shape
        `(n, *odl_op.range.shape, 1)` where ``n`` is the number of batches.
        Hence for each evaluation, ``odl_op`` is called a total of ``n`` times.
        The `dtype` of the tensor is the same as the respective ODL spaces.

        If ``odl_op`` implements `Operator.derivative` which in turn implements
        `Operator.adjoint`, it is properly wrapped in ``tensorflow_layer``, and
        gradients propagate as expected.
    """
    import tensorflow as tf
    from tensorflow.python.framework import ops

    def py_func(func, inp, Tout, stateful=True, name=None, grad=None):
        """Define custom py_func which takes also a grad op as argument."""
        if grad is None:
            return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
        else:
            # Need to generate a unique name to avoid duplicates:
            rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))

            tf.RegisterGradient(rnd_name)(grad)
            g = tf.get_default_graph()

            if stateful:
                override_name = 'PyFunc'
            else:
                override_name = 'PyFuncStateless'

            with g.gradient_override_map({override_name: rnd_name}):
                return tf.py_func(func, inp, Tout, stateful=stateful,
                                  name=name)

    def tensorflow_layer_grad_impl(x, dx):
        """Implementation of the tensorflow gradient.

        Gradient in tensorflow is equivalent to the adjoint of the derivative
        in ODL.
        """
        x_shape = x.get_shape()
        dx_shape = dx.get_shape()
        try:
            n_x = int(x_shape[0])
            fixed_size = True
        except TypeError:
            n_x = x_shape[0]
            fixed_size = False

        if odl_op.is_functional:
            in_shape = (n_x,)
        else:
            in_shape = (n_x,) + odl_op.range.shape + (1,)
        out_shape = (n_x,) + odl_op.domain.shape + (1,)

        # Validate input shape
        assert x_shape[1:] == odl_op.domain.shape + (1,)
        if odl_op.is_functional:
            assert dx_shape[1:] == ()
        else:
            assert dx_shape[1:] == odl_op.range.shape + (1,)

        def _impl(x, dx):
            """Implementation of the adjoint of the derivative.

            Returns ::

                odl_op.derivative(x).adjoint(dx)

            Parameters
            ----------
            x : `numpy.ndarray`
                Point(s) in which the derivative should be taken.
                If ``odl_op`` is an `Operator` the axes are:
                    0 : batch id. This is a constant if ``fixed_size`` is true,
                        otherwise it is dynamic.
                    1, ..., N-2 : spatial dimensions of data.
                    n-1 : (currently) unused data channel.
                If ``odl_op`` is a `Functional` the axes are:
                    0 : batch id.
            dx : `numpy.ndarray`
                Point(s) in which the adjoint of the derivative of the operator
                should be evaluated.
                The axes are:
                    0 : batch id. Should be pairwise matched with ``x``.
                    1, ..., M-2 : spatial dimensions of data.
                    n-1 : (currently) unused data channel.

            Returns
            -------
            result : `numpy.ndarray`
                Result of the computation.

                If ``odl_op`` is an `Operator` the axes are:
                    0 : batch id.
                    1, ..., N-2 : spatial dimensions of data.
                    n-1 : (currently) unused data channel.
                If ``odl_op`` is a `Functional` the axes are:
                    0 : batch id.
            """
            if fixed_size:
                x_out_shape = out_shape
                assert x.shape == out_shape
                assert dx.shape == in_shape
            else:
                x_out_shape = (x.shape[0],) + out_shape[1:]
                assert x.shape[1:] == out_shape[1:]
                assert dx.shape[1:] == in_shape[1:]

            out = np.empty(x_out_shape, odl_op.domain.dtype)
            for i in range(x_out_shape[0]):
                if odl_op.is_functional:
                    xi = x[i, ..., 0]
                    dxi = dx[i]
                    out[i, ..., 0] = np.asarray(odl_op.gradient(xi)) * dxi
                else:
                    xi = x[i, ..., 0]
                    dxi = dx[i, ..., 0]
                    result = odl_op.derivative(xi).adjoint(dxi)
                    out[i, ..., 0] = np.asarray(result)

            # TODO: Improve
            try:
                dom_weight = odl_op.domain.weighting.const
            except AttributeError:
                dom_weight = 1.0

            try:
                ran_weight = odl_op.range.weighting.const
            except AttributeError:
                ran_weight = 1.0

            scale = dom_weight / ran_weight
            out *= scale
            return out

        with ops.name_scope(name + 'Grad', "ODLTensorflowLayerGrad", [x, dx]):
            result = py_func(_impl,
                             [x, dx],
                             [tf.float32],
                             stateful=False)

            # We must manually set the output shape since tensorflow cannot
            # figure it out
            result = result[0]
            result.set_shape(out_shape)
            return result

    def tensorflow_layer_grad(op, grad):
        """Thin wrapper for the gradient."""
        x = op.inputs[0]
        return tensorflow_layer_grad_impl(x, grad)

    def tensorflow_layer(x):
        """Implementation of the tensorflow call.

        Returns a `tensorflow.Tensor` that represents a lazy application of
        ``odl_op`` to ``x``.

        Parameters
        ----------
        x : `tensorflow.Tensor`
            Point(s) to which the layer should be applied.
            The axes are:
                0 : batch id. Can be fixed or dynamic.
                1, ..., M-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.

        Returns
        -------
        result : `tensorflow.Tensor`
            Lazy result of the computation.
            If ``odl_op`` is an `Operator` the axes are:
                0 : batch id.
                1, ..., N-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.
            If ``odl_op`` is a `Functional` the axes are:
                0 : batch id.
        """
        x_shape = x.get_shape()
        try:
            n_x = int(x_shape[0])
            fixed_size = True
        except TypeError:
            n_x = x_shape[0]
            fixed_size = False

        in_shape = (n_x,) + odl_op.domain.shape + (1,)
        if odl_op.is_functional:
            out_shape = (n_x,)
        else:
            out_shape = (n_x,) + odl_op.range.shape + (1,)

        # Validate input shape
        assert x_shape[1:] == odl_op.domain.shape + (1,)

        def _impl(x):
            """Implementation of the tensorflow layer.

            Parameters
            ----------
            x : `numpy.ndarray`
                Point(s) in which the operator should be evaluated.
                The axes are:
                    0 : batch id. This is a constant if ``fixed_size`` is true,
                        otherwise it is dynamic.
                    1, ..., N-2 : spatial dimensions of data.
                    n-1 : (currently) unused data channel.

            Returns
            -------
            result : `numpy.ndarray`
                Result of the computation.
                The axes are:
                    0 : batch id. Data is pairwise matched with ``x``.
                    1, ..., M-2 : spatial dimensions of data.
                    n-1 : (currently) unused data channel.
            """
            if fixed_size:
                x_out_shape = out_shape
                assert x.shape == in_shape
            else:
                x_out_shape = (x.shape[0],) + out_shape[1:]
                assert x.shape[1:] == in_shape[1:]

            out = np.empty(x_out_shape, odl_op.domain.dtype)
            for i in range(x_out_shape[0]):
                if odl_op.is_functional:
                    out[i] = odl_op(x[i, ..., 0])
                else:
                    out[i, ..., 0] = np.asarray(odl_op(x[i, ..., 0]))

            return out

        if differentiable:
            grad = tensorflow_layer_grad
        else:
            grad = None

        with ops.name_scope(name, "ODLTensorflowLayer", [x]) as name_call:
            result = py_func(_impl,
                             [x],
                             [tf.float32],
                             name=name_call,
                             stateful=False,
                             grad=grad)

            # We must manually set the output shape since tensorflow cannot
            # figure it out
            result = result[0]
            result.set_shape(out_shape)
            return result

    return tensorflow_layer

class TensorflowSpace(odl.LinearSpace):

    """A space of tensorflow Tensors."""

    def __init__(self, shape, name='ODLTensorflowSpace'):
        odl.LinearSpace.__init__(self, odl.RealNumbers())
        self.shape = tuple(tf.Dimension(si) if not isinstance(si, tf.Dimension) else si for si in shape)
        self.init_shape = tuple(si if si.value is not None else tf.Dimension(1) for si in self.shape)
        self.name = name

    def _lincomb(self, a, x1, b, x2, out):
        with tf.name_scope('{}_lincomb'.format(self.name)):
            if x1 is x2:
                # x1 is aligned with x2 -> out = (a+b)*x1
                out.data = (a + b) * x1.data
            elif out is x1 and out is x2:
                # All the vectors are aligned -> out = (a+b)*out
                if (a + b) != 1:
                    out.data *= (a + b)
            elif out is x1:
                # out is aligned with x1 -> out = a*out + b*x2
                out.data = a * out.data + b * x2.data
            elif out is x2:
                # out is aligned with x2 -> out = a*x1 + b*out
                out.data = a * x1.data + b * out.data
            else:
                # We have exhausted all alignment options, so x1 != x2 != out
                # We now optimize for various values of a and b
                if b == 0:
                    if a == 0:  # Zero assignment -> out = 0
                        out.data *= 0
                    else:  # Scaled copy -> out = a*x1
                        out.data = a * x1.data
                else:
                    if a == 0:  # Scaled copy -> out = b*x2
                        out.data = b * x2.data
                    elif a == 1:  # No scaling in x1 -> out = x1 + b*x2
                        out.data = x1.data + b * x2.data
                    else:  # Generic case -> out = a*x1 + b*x2
                        out.data = a * x1.data + b * x2.data

    def element(self, inp=None):
        if inp in self:
            return inp
        elif inp is None:
            return self.zero()
        else:
            return TensorflowSpaceElement(self, inp)

    def zero(self):
        with tf.name_scope('{}_zero'.format(self.name)):
            return self.element(tf.zeros(self.init_shape,
                                         dtype=tf.float32))

    def one(self):
        with tf.name_scope('{}_one'.format(self.name)):
            return self.element(tf.ones(self.init_shape,
                                        dtype=tf.float32))

    def __eq__(self, other):
        return isinstance(other, TensorflowSpace) and other.shape == self.shape

    def __repr__(self):
        return 'TensorflowSpace({})'.format(self.shape)

class TensorflowSpaceElement(odl.LinearSpaceElement):
    def __init__(self, space, data):
        odl.LinearSpaceElement.__init__(self, space)
        self.data = data

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        if isinstance(value, TensorflowSpaceElement):
            raise Exception(value.data)
        self._data = value

    def __repr__(self):
        return '{}.element({})'.format(self.space, self.data)

class TensorflowSpaceOperator(odl.Operator):
    def __init__(self, domain, range, func, adjoint=None, linear=False):
        odl.Operator.__init__(self, domain, range, linear)
        self.func = func
        self.adjoint_func = adjoint

    def _call(self, x):
        return self.func(x.data)

    @property
    def adjoint(self):
        return TensorflowSpaceOperator(self.range,
                                       self.domain,
                                       self.adjoint_func,
                                       self.func,
                                       self.is_linear)
<commit_msg>ENH: Improve doc and add name scopes to tensorflow<commit_after>import odl
import tensorflow as tf
import numpy as np

__all__ = ('as_tensorflow_layer', 'TensorflowSpace', 'TensorflowSpaceOperator')

def as_tensorflow_layer(odl_op, default_name='ODLOperator',
                        differentiable=True):
    """Convert ``Operator`` to tensorflow layer.

    Parameters
    ----------
    odl_op : `Operator`
        The operator that should be wrapped to a tensorflow layer.
    default_name : str
        Default name for tensorflow layers created.
    differentiable : boolean
        True if the operator should be differentiable, otherwise assumes that
        the derivative is everywhere zero.

    Returns
    -------
    tensorflow_layer : callable
        Callable that, when called with an `tensorflow.Tensor` of shape
        `(n, *odl_op.domain.shape, 1)` returns a tensor of shape
        `(n, *odl_op.range.shape, 1)` where ``n`` is the number of batches.
        Hence for each evaluation, ``odl_op`` is called a total of ``n`` times.
        The `dtype` of the tensor is the same as the respective ODL spaces.

        If ``odl_op`` implements `Operator.derivative` which in turn implements
        `Operator.adjoint`, it is properly wrapped in ``tensorflow_layer``, and
        gradients propagate as expected.
    """
    import tensorflow as tf
    from tensorflow.python.framework import ops

    def py_func(func, inp, Tout, stateful=True, name=None, grad=None):
        """Define custom py_func which takes also a grad op as argument."""
        if grad is None:
            return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
        else:
            if stateful:
                override_name = 'PyFunc'
            else:
                override_name = 'PyFuncStateless'

            # Need to generate a unique name to avoid duplicates:
            rnd_name = override_name + 'Grad' + str(np.random.randint(0, 1E+8))

            tf.RegisterGradient(rnd_name)(grad)
            g = tf.get_default_graph()

            with g.gradient_override_map({override_name: rnd_name}):
                return tf.py_func(func, inp, Tout, stateful=stateful,
                                  name=name)

    def tensorflow_layer_grad_impl(x, dx, name):
        """Implementation of the tensorflow gradient.

        Gradient in tensorflow is equivalent to the adjoint of the derivative
        in ODL.

        Returns a `tensorflow.Tensor` that represents a lazy application of ::

            odl_op.derivative(x).adjoint(dx)

        Parameters
        ----------
        x : `tensorflow.Tensor`
            Point(s) to which the layer should be applied.
            The axes are:
                0 : batch id. Can be fixed or dynamic.
                1, ..., M-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.
        name : string
            Name of the tensor.

        Returns
        -------
        result : `tensorflow.Tensor`
            Lazy result of the computation.
            If ``odl_op`` is an `Operator` the axes are:
                0 : batch id.
                1, ..., N-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.
            If ``odl_op`` is a `Functional` the axes are:
                0 : batch id.
        """
        with tf.name_scope(name):
            x_shape = x.get_shape()
            dx_shape = dx.get_shape()
            try:
                n_x = int(x_shape[0])
                fixed_size = True
            except TypeError:
                n_x = x_shape[0]
                fixed_size = False

            if odl_op.is_functional:
                in_shape = (n_x,)
            else:
                in_shape = (n_x,) + odl_op.range.shape + (1,)
            out_shape = (n_x,) + odl_op.domain.shape + (1,)

            # Validate input shape
            assert x_shape[1:] == odl_op.domain.shape + (1,)
            if odl_op.is_functional:
                assert dx_shape[1:] == ()
            else:
                assert dx_shape[1:] == odl_op.range.shape + (1,)

            def _impl(x, dx):
                """Implementation of the adjoint of the derivative.

                Returns ::

                    odl_op.derivative(x).adjoint(dx)

                Parameters
                ----------
                x : `numpy.ndarray`
                    Point(s) in which the derivative should be taken.
                    If ``odl_op`` is an `Operator` the axes are:
                        0 : batch id. This is a constant if ``fixed_size`` is
                            true, otherwise it is dynamic.
                        1, ..., N-2 : spatial dimensions of data.
                        n-1 : (currently) unused data channel.
                    If ``odl_op`` is a `Functional` the axes are:
                        0 : batch id.
                dx : `numpy.ndarray`
                    Point(s) in which the adjoint of the derivative of the
                    operator should be evaluated.
                    The axes are:
                        0 : batch id. Should be pairwise matched with ``x``.
                        1, ..., M-2 : spatial dimensions of data.
                        n-1 : (currently) unused data channel.

                Returns
                -------
                result : `numpy.ndarray`
                    Result of the computation.

                    If ``odl_op`` is an `Operator` the axes are:
                        0 : batch id.
                        1, ..., N-2 : spatial dimensions of data.
                        n-1 : (currently) unused data channel.
                    If ``odl_op`` is a `Functional` the axes are:
                        0 : batch id.
                """
                if fixed_size:
                    x_out_shape = out_shape
                    assert x.shape == out_shape
                    assert dx.shape == in_shape
                else:
                    x_out_shape = (x.shape[0],) + out_shape[1:]
                    assert x.shape[1:] == out_shape[1:]
                    assert dx.shape[1:] == in_shape[1:]

                out = np.empty(x_out_shape, odl_op.domain.dtype)
                out[:] = np.nan
                for i in range(x_out_shape[0]):
                    if odl_op.is_functional:
                        xi = x[i, ..., 0]
                        dxi = dx[i]
                        out[i, ..., 0] = np.asarray(odl_op.gradient(xi)) * dxi
                    else:
                        xi = x[i, ..., 0]
                        dxi = dx[i, ..., 0]
                        result = odl_op.derivative(xi).adjoint(dxi)
                        out[i, ..., 0] = np.asarray(result)

                # TODO: Improve
                try:
                    dom_weight = odl_op.domain.weighting.const
                except AttributeError:
                    dom_weight = 1.0

                try:
                    ran_weight = odl_op.range.weighting.const
                except AttributeError:
                    ran_weight = 1.0

                scale = dom_weight / ran_weight
                out *= scale

                return out

            with ops.name_scope(name + '_pyfunc', values=[x, dx]) as name_call:
                result = py_func(_impl,
                                 [x, dx],
                                 [tf.float32],
                                 name=name_call,
                                 stateful=True)

                # We must manually set the output shape since tensorflow cannot
                # figure it out
                result = result[0]
                result.set_shape(out_shape)
                return result

    def tensorflow_layer(x, name=None):
        """Implementation of the tensorflow call.

        Returns a `tensorflow.Tensor` that represents a lazy application of
        ``odl_op`` to ``x``.

        Parameters
        ----------
        x : `tensorflow.Tensor`
            Point(s) to which the layer should be applied.
            The axes are:
                0 : batch id. Can be fixed or dynamic.
                1, ..., M-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.
        name : string
            Name of the tensor. Default: Defaultname.

        Returns
        -------
        result : `tensorflow.Tensor`
            Lazy result of the computation.
            If ``odl_op`` is an `Operator` the axes are:
                0 : batch id.
                1, ..., N-2 : spatial dimensions of data.
                n-1 : (currently) unused data channel.
            If ``odl_op`` is a `Functional` the axes are:
                0 : batch id.
        """
        if name is None:
            name = default_name

        with tf.name_scope(name):
            x_shape = x.get_shape()
            try:
                n_x = int(x_shape[0])
                fixed_size = True
            except TypeError:
                n_x = x_shape[0]
                fixed_size = False

            in_shape = (n_x,) + odl_op.domain.shape + (1,)
            if odl_op.is_functional:
                out_shape = (n_x,)
            else:
                out_shape = (n_x,) + odl_op.range.shape + (1,)

            # Validate input shape
            assert x_shape[1:] == odl_op.domain.shape + (1,)

            def _impl(x):
                """Implementation of the tensorflow layer.

                Parameters
                ----------
                x : `numpy.ndarray`
                    Point(s) in which the operator should be evaluated.
                    The axes are:
                        0 : batch id. This is a constant if ``fixed_size`` is
                            true, otherwise it is dynamic.
                        1, ..., N-2 : spatial dimensions of data.
                        n-1 : (currently) unused data channel.

                Returns
                -------
                result : `numpy.ndarray`
                    Result of the computation.
                    The axes are:
                        0 : batch id. Data is pairwise matched with ``x``.
                        1, ..., M-2 : spatial dimensions of data.
                        n-1 : (currently) unused data channel.
                """
                if fixed_size:
                    x_out_shape = out_shape
                    assert x.shape == in_shape
                else:
                    x_out_shape = (x.shape[0],) + out_shape[1:]
                    assert x.shape[1:] == in_shape[1:]

                out = np.empty(x_out_shape, odl_op.domain.dtype)
                out[:] = np.nan
                for i in range(x_out_shape[0]):
                    if odl_op.is_functional:
                        out[i] = odl_op(x[i, ..., 0])
                    else:
                        out[i, ..., 0] = np.asarray(odl_op(x[i, ..., 0]))

                return out

            if differentiable:
                def tensorflow_layer_grad(op, grad):
                    """Thin wrapper for the gradient."""
                    x = op.inputs[0]
                    return tensorflow_layer_grad_impl(x, grad,
                                                      name=name + '_grad')
            else:
                tensorflow_layer_grad = None

            with ops.name_scope(name + '_pyfunc', values=[x]) as name_call:
                result = py_func(_impl,
                                 [x],
                                 [tf.float32],
                                 name=name_call,
                                 stateful=True,
                                 grad=tensorflow_layer_grad)

                # We must manually set the output shape since tensorflow cannot
                # figure it out
                result = result[0]
                result.set_shape(out_shape)
                return result

    return tensorflow_layer

class TensorflowSpace(odl.LinearSpace):

    """A space of tensorflow Tensors."""

    def __init__(self, shape, name='ODLTensorflowSpace'):
        odl.LinearSpace.__init__(self, odl.RealNumbers())
        self.shape = tuple(tf.Dimension(si) if not isinstance(si, tf.Dimension) else si for si in shape)
        self.init_shape = tuple(si if si.value is not None else tf.Dimension(1) for si in self.shape)
        self.name = name

    def _lincomb(self, a, x1, b, x2, out):
        with tf.name_scope('{}_lincomb'.format(self.name)):
            if x1 is x2:
                # x1 is aligned with x2 -> out = (a+b)*x1
                out.data = (a + b) * x1.data
            elif out is x1 and out is x2:
                # All the vectors are aligned -> out = (a+b)*out
                if (a + b) != 1:
                    out.data *= (a + b)
            elif out is x1:
                # out is aligned with x1 -> out = a*out + b*x2
                out.data = a * out.data + b * x2.data
            elif out is x2:
                # out is aligned with x2 -> out = a*x1 + b*out
                out.data = a * x1.data + b * out.data
            else:
                # We have exhausted all alignment options, so x1 != x2 != out
                # We now optimize for various values of a and b
                if b == 0:
                    if a == 0:  # Zero assignment -> out = 0
                        out.data *= 0
                    else:  # Scaled copy -> out = a*x1
                        out.data = a * x1.data
                else:
                    if a == 0:  # Scaled copy -> out = b*x2
                        out.data = b * x2.data
                    elif a == 1:  # No scaling in x1 -> out = x1 + b*x2
                        out.data = x1.data + b * x2.data
                    else:  # Generic case -> out = a*x1 + b*x2
                        out.data = a * x1.data + b * x2.data

    def element(self, inp=None):
        if inp in self:
            return inp
        elif inp is None:
            return self.zero()
        else:
            return TensorflowSpaceElement(self, inp)

    def zero(self):
        with tf.name_scope('{}_zero'.format(self.name)):
            return self.element(tf.zeros(self.init_shape,
                                         dtype=tf.float32))

    def one(self):
        with tf.name_scope('{}_one'.format(self.name)):
            return self.element(tf.ones(self.init_shape,
                                        dtype=tf.float32))

    def __eq__(self, other):
        return isinstance(other, TensorflowSpace) and other.shape == self.shape

    def __repr__(self):
        return 'TensorflowSpace({})'.format(self.shape)

class TensorflowSpaceElement(odl.LinearSpaceElement):
    def __init__(self, space, data):
        odl.LinearSpaceElement.__init__(self, space)
        self.data = data

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        if isinstance(value, TensorflowSpaceElement):
            raise Exception(value.data)
        self._data = value

    def __repr__(self):
        return '{}.element({})'.format(self.space, self.data)

class TensorflowSpaceOperator(odl.Operator):
    def __init__(self, domain, range, func, adjoint=None, linear=False):
        odl.Operator.__init__(self, domain, range, linear)
        self.func = func
        self.adjoint_func = adjoint

    def _call(self, x):
        return self.func(x.data)

    @property
    def adjoint(self):
        return TensorflowSpaceOperator(self.range,
                                       self.domain,
                                       self.adjoint_func,
                                       self.func,
                                       self.is_linear)
<|endoftext|>


<commit_before>import sys
from setuptools import setup

if sys.version_info < (3, 0):
    sys.stderr.write("Sorry, Python < 3.0 is not supported\n")
    sys.exit(1)

setup(name='picotui',
      version='0.9.3',
      description="""A simple text user interface (TUI) library.""",
      long_description=open('README.rst').read(),
      url='https://github.com/pfalcon/picotui',
      author='Paul Sokolovsky',
      author_email='pfalcon@users.sourceforge.net',
      license='MIT',
      packages=['picotui'])
<commit_msg>setup.py: Release 0.9.4.<commit_after>import sys
from setuptools import setup

if sys.version_info < (3, 0):
    sys.stderr.write("Sorry, Python < 3.0 is not supported\n")
    sys.exit(1)

setup(name='picotui',
      version='0.9.4',
      description="""A simple text user interface (TUI) library.""",
      long_description=open('README.rst').read(),
      url='https://github.com/pfalcon/picotui',
      author='Paul Sokolovsky',
      author_email='pfalcon@users.sourceforge.net',
      license='MIT',
      packages=['picotui'])
<|endoftext|>


<commit_before># Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'targets': [
    {
      'target_name': 'configurations',
      'type': 'none',
      'configurations': {
        'Debug': {
          'standalone_static_library': 1,
        },
      }
    },
  ],
}
# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'targets': [
    {
      'target_name': 'configurations',
      'type': 'none',
      'configurations': {
        'Debug': {
          'standalone_static_library': 1,
        },
      }
    },
  ],
}
# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'targets': [
    {
      'target_name': 'configurations',
      'type': 'none',
      'configurations': {
        'Debug': {
          'standalone_static_library': 1,
        },
      }
    },
  ],
}
<commit_msg>Fix my patch-applying-fail from r1516.<commit_after># Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'targets': [
    {
      'target_name': 'configurations',
      'type': 'none',
      'configurations': {
        'Debug': {
          'standalone_static_library': 1,
        },
      }
    },
  ],
}
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-

__author__ = 'ray'
__date__ = '4/21/15'

import os
import unittest

from stonemason.renderer.cartographer import LayerFactory
from stonemason.renderer.cartographer.imagery import Color, Filter, Blend

from stonemason.mason.theme import SAMPLE_THEME_DIRECTORY
from tests import skipUnlessHasScipy, skipUnlessHasSkimage

style_sheet = os.path.join(
    SAMPLE_THEME_DIRECTORY, 'sample_world', 'sample_world.xml')

class TestLayerFactory(unittest.TestCase):
    def setUp(self):
        self.factory = LayerFactory()

    def test_create_terminal_layer(self):
        layer = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')
        self.assertIsInstance(layer, Color)

    def test_create_transform_layer(self):
        source = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')

        layer = self.factory.create_transform_layer(
            'test', 'basic.filter', source=source, filter_name='blur')
        self.assertIsInstance(layer, Filter)

    def test_create_composite_layer(self):
        source1 = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')
        source2 = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')

        layer = self.factory.create_composite_layer(
            'test', 'basic.blend', sources=[source1, source2], alpha=0.5)
        self.assertIsInstance(layer, Blend)
<commit_msg>FIX: Fix test error<commit_after># -*- encoding: utf-8 -*-

__author__ = 'ray'
__date__ = '4/21/15'

import os
import unittest

from stonemason.renderer.cartographer import LayerFactory
from stonemason.renderer.cartographer.imagery import Color, Blend, MinPILFilter

from stonemason.mason.theme import SAMPLE_THEME_DIRECTORY
from tests import skipUnlessHasScipy, skipUnlessHasSkimage

style_sheet = os.path.join(
    SAMPLE_THEME_DIRECTORY, 'sample_world', 'sample_world.xml')

class TestLayerFactory(unittest.TestCase):
    def setUp(self):
        self.factory = LayerFactory()

    def test_create_terminal_layer(self):
        layer = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')
        self.assertIsInstance(layer, Color)

    def test_create_transform_layer(self):
        source = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')

        layer = self.factory.create_transform_layer(
            'test', 'basic.filter.MinFilter', source=source)
        self.assertIsInstance(layer, MinPILFilter)

    def test_create_composite_layer(self):
        source1 = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')
        source2 = self.factory.create_terminal_layer(
            'test', 'basic.color', color='#000')

        layer = self.factory.create_composite_layer(
            'test', 'basic.blend', sources=[source1, source2], alpha=0.5)
        self.assertIsInstance(layer, Blend)
<|endoftext|>


<commit_before># OfflineIMAP synchronization master code
# Copyright (C) 2002-2007 John Goerzen
# <jgoerzen@complete.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

import imaplib
from offlineimap import imapserver, repository, folder, mbnames, threadutil, version
from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
import offlineimap.accounts
from offlineimap.accounts import SyncableAccount, SigListener
from offlineimap.ui import UIBase
import re, os, os.path, offlineimap, sys
from ConfigParser import ConfigParser
from threading import *

def syncaccount(threads, config, accountname, siglisteners):
    account = SyncableAccount(config, accountname)
    siglistener = SigListener()
    thread = InstanceLimitedThread(instancename = 'ACCOUNTLIMIT',
                                   target = account.syncrunner,
                                   name = "Account sync %s" % accountname,
                                   kwargs = {'siglistener': siglistener} )
    # the Sync Runner thread is the only one that will mutate siglisteners
    siglisteners.append(siglistener)
    thread.setDaemon(1)
    thread.start()
    threads.add(thread)

def syncitall(accounts, config, siglisteners):
    currentThread().setExitMessage('SYNC_WITH_TIMER_TERMINATE')
    ui = UIBase.getglobalui()
    threads = threadutil.threadlist()
    mbnames.init(config, accounts)
    for accountname in accounts:
        syncaccount(threads, config, accountname, siglisteners)
    # Wait for the threads to finish.
    threads.reset()
<commit_msg>remove unneeded includes from syncmaster.py<commit_after># OfflineIMAP synchronization master code
# Copyright (C) 2002-2007 John Goerzen
# <jgoerzen@complete.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

from offlineimap import mbnames
from offlineimap.threadutil import threadlist, InstanceLimitedThread, ExitNotifyThread
from offlineimap.accounts import SyncableAccount, SigListener
from threading import currentThread

def syncaccount(threads, config, accountname, siglisteners):
    account = SyncableAccount(config, accountname)
    siglistener = SigListener()
    thread = InstanceLimitedThread(instancename = 'ACCOUNTLIMIT',
                                   target = account.syncrunner,
                                   name = "Account sync %s" % accountname,
                                   kwargs = {'siglistener': siglistener} )
    # the Sync Runner thread is the only one that will mutate siglisteners
    siglisteners.append(siglistener)
    thread.setDaemon(1)
    thread.start()
    threads.add(thread)

def syncitall(accounts, config, siglisteners):
    currentThread().setExitMessage('SYNC_WITH_TIMER_TERMINATE')
    threads = threadlist()
    mbnames.init(config, accounts)
    for accountname in accounts:
        syncaccount(threads, config, accountname, siglisteners)
    # Wait for the threads to finish.
    threads.reset()
<|endoftext|>


<commit_before>from setuptools import setup, find_packages

setup(
    name='django-setuptest',
    version='0.0.1',
    description='Simple lib enabling Django app testing via $python setup.py test',
    long_description=open('README.rst', 'r').read() + open('AUTHORS.rst', 'r').read() + open('CHANGELOG.rst', 'r').read(),
    author='Praekelt Foundation',
    author_email='dev@praekelt.com',
    license='BSD',
    url='http://github.com/praekelt/django-setuptest',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'coverage',
        'django',
        'pep8',
    ],
    classifiers=[
        "Programming Language :: Python",
        "License :: OSI Approved :: BSD License",
        "Development Status :: 4 - Beta",
        "Operating System :: OS Independent",
        "Framework :: Django",
        "Intended Audience :: Developers",
        "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
    ],
    zip_safe=False,
)
<commit_msg>fooling around<commit_after>from setuptools import setup, find_packages

setup(
    name='django-setuptest',
    version='0.0.1',
    description='Simple lib enabling Django app testing via $python setup.py test',
    long_description=open('README.rst', 'r').read() + open('AUTHORS.rst', 'r').read() + open('CHANGELOG.rst', 'r').read(),
    author='Praekelt Foundation',
    author_email='dev@praekelt.com',
    license='BSD',
    url='http://github.com/praekelt/django-setuptest',
    packages=find_packages(),
    include_package_data=True,
    tests_require=[
        'coverage',
        'django',
        'pep8',
    ],
    classifiers=[
        "Programming Language :: Python",
        "License :: OSI Approved :: BSD License",
        "Development Status :: 4 - Beta",
        "Operating System :: OS Independent",
        "Framework :: Django",
        "Intended Audience :: Developers",
        "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
    ],
    zip_safe=False,
)
<|endoftext|>


<commit_before>import os
import csv
import requests
from datetime import datetime
import simplejson as json
import platform
import base64

import ohmysportsfeedspy
from ohmysportsfeedspy.v1_0 import API_v1_0

# API class for dealing with v2.0 of the API
class API_v2_0(API_v1_0):

    # Constructor
    def __init__(self, verbose, store_type=None, store_location=None):
        super().__init__(verbose, store_type, store_location)

        self.base_url = "https://api.mysportsfeeds.com/v2.0/pull"

        self.valid_feeds = [
            'seasonal_games',
            'daily_games',
            'weekly_games',
            'seasonal_dfs',
            'daily_dfs',
            'weekly_dfs',
            'seasonal_player_gamelogs',
            'daily_player_gamelogs',
            'weekly_player_gamelogs',
            'seasonal_team_gamelogs',
            'daily_team_gamelogs',
            'weekly_team_gamelogs',
            'game_boxscore',
            'game_playbyplay',
            'game_lineup',
            'current_season',
            'player_injuries',
            'latest_updates',
            'seasonal_team_stats',
            'seasonal_player_stats',
            'seasonal_venues',
            'players',
            'seasonal_standings',
            'daily_game_lines',
            'daily_futures'
        ]

    # Feed URL
    def determine_url(self, league, season, feed, output_format, params):
        if feed == "seasonal_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/games.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/games.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/games.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "game_boxscore":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/boxscore.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "game_playbyplay":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/playbyplay.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "game_lineup":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/lineup.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "current_season":
            return "{base_url}/{league}/current_season.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "player_injuries":
            return "{base_url}/{league}/injuries.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "latest_updates":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/latest_updates.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_team_stats":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/team_stats_totals.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_player_stats":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/player_stats_totals.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_venues":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/venues.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "players":
            return "{base_url}/{league}/players.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "seasonal_standings":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/standings.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_game_lines":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/odds_gamelines.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "daily_futures":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/odds_futures.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

else:
    return ""

        else:
            return ""
<commit_msg>fix duplicate return line<commit_after>import os
import csv
import requests
from datetime import datetime
import simplejson as json
import platform
import base64

import ohmysportsfeedspy
from ohmysportsfeedspy.v1_0 import API_v1_0

# API class for dealing with v2.0 of the API
class API_v2_0(API_v1_0):

    # Constructor
    def __init__(self, verbose, store_type=None, store_location=None):
        super().__init__(verbose, store_type, store_location)

        self.base_url = "https://api.mysportsfeeds.com/v2.0/pull"

        self.valid_feeds = [
            'seasonal_games',
            'daily_games',
            'weekly_games',
            'seasonal_dfs',
            'daily_dfs',
            'weekly_dfs',
            'seasonal_player_gamelogs',
            'daily_player_gamelogs',
            'weekly_player_gamelogs',
            'seasonal_team_gamelogs',
            'daily_team_gamelogs',
            'weekly_team_gamelogs',
            'game_boxscore',
            'game_playbyplay',
            'game_lineup',
            'current_season',
            'player_injuries',
            'latest_updates',
            'seasonal_team_stats',
            'seasonal_player_stats',
            'seasonal_venues',
            'players',
            'seasonal_standings',
            'daily_game_lines',
            'daily_futures'
        ]

    # Feed URL
    def determine_url(self, league, season, feed, output_format, params):
        if feed == "seasonal_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/games.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/games.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_games":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/games.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_dfs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/dfs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_player_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/player_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "seasonal_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "weekly_team_gamelogs":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "week" in params:
                raise AssertionError("You must specify a 'week' param for this request.")

            return "{base_url}/{league}/{season}/week/{week}/team_gamelogs.{output}".format(base_url=self.base_url, league=league, season=season, week=params["week"], output=output_format)

        elif feed == "game_boxscore":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/boxscore.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "game_playbyplay":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/playbyplay.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "game_lineup":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "game" in params:
                raise AssertionError("You must specify a 'game' param for this request.")

            return "{base_url}/{league}/{season}/games/{game}/lineup.{output}".format(base_url=self.base_url, league=league, season=season, game=params["game"], output=output_format)

        elif feed == "current_season":
            return "{base_url}/{league}/current_season.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "player_injuries":
            return "{base_url}/{league}/injuries.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "latest_updates":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/latest_updates.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_team_stats":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/team_stats_totals.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_player_stats":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/player_stats_totals.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "seasonal_venues":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/venues.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "players":
            return "{base_url}/{league}/players.{output}".format(base_url=self.base_url, league=league, output=output_format)

        elif feed == "seasonal_standings":
            if season == "":
                raise AssertionError("You must specify a season for this request.")

            return "{base_url}/{league}/{season}/standings.{output}".format(base_url=self.base_url, league=league, season=season, output=output_format)

        elif feed == "daily_game_lines":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/odds_gamelines.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        elif feed == "daily_futures":
            if season == "":
                raise AssertionError("You must specify a season for this request.")
            if not "date" in params:
                raise AssertionError("You must specify a 'date' param for this request.")

            return "{base_url}/{league}/{season}/date/{date}/odds_futures.{output}".format(base_url=self.base_url, league=league, season=season, date=params["date"], output=output_format)

        else:
            return ""
<|endoftext|>