<commit_before>#-*- encoding: utf-8 -*-
"""
Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number.
For example, it would appear that there are at least six ways of writing the number sixteen:

IIIIIIIIIIIIIIII
VIIIIIIIIIII
VVIIIIII
XIIIIII
VVVI
XVI

However, according to the rules only XIIIIII and XVI are valid, and the last example is considered to be the most efficient, as it uses the least number of numerals.
The 11K text file, roman.txt (right click and 'Save Link/Target As...'), contains one thousand numbers written in valid, but not necessarily minimal, Roman numerals; see About... Roman Numerals for the definitive rules for this problem.
Find the number of characters saved by writing each of these in their minimal form.
Note: You can assume that all the Roman numerals in the file contain no more than four consecutive identical units.
"""

from utils import *

#
<commit_msg>Solve #089<commit_after>#-*- encoding: utf-8 -*-
"""
Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number.
For example, it would appear that there are at least six ways of writing the number sixteen:

IIIIIIIIIIIIIIII
VIIIIIIIIIII
VVIIIIII
XIIIIII
VVVI
XVI

However, according to the rules only XIIIIII and XVI are valid, and the last example is considered to be the most efficient, as it uses the least number of numerals.
The 11K text file, roman.txt (right click and 'Save Link/Target As...'), contains one thousand numbers written in valid, but not necessarily minimal, Roman numerals; see About... Roman Numerals for the definitive rules for this problem.
Find the number of characters saved by writing each of these in their minimal form.
Note: You can assume that all the Roman numerals in the file contain no more than four consecutive identical units.
"""

from utils import *

def to_roman(i):
    s = ''
    while i >= 1000:
        s += 'M'
        i -= 1000
    for m, b, c, d in [(100, 'C', 'D', 'M'), (10, 'X', 'L', 'C'), (1, 'I', 'V', 'X')]:
        n = i / m
        if n < 4:
            s += b * n
        elif n == 4:
            s += b + c
        elif n < 9:
            s += c + b * (n - 5)
        elif n == 9:
            s += b + d
        i %= m
    return s

def from_roman(s):
    sp = {'I': 'VX', 'X': 'LC', 'C': 'DM'}
    c2i = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    r = 0
    i = 0
    l = len(s)
    while i < l:
        c = s[i]
        if c in sp.keys() and i + 1 < l and s[i + 1] in sp[c]:
            r += c2i[s[i + 1]] - c2i[c]
            i += 1
        else:
            r += c2i[c]
        i += 1
    return r

L = file('./089_roman.txt').read().splitlines()
print sum(len(l) - len(to_roman(from_roman(l))) for l in L)
# 743
<|endoftext|>


<commit_before><commit_msg>Removed extraneous print statement<commit_after><|endoftext|>


<commit_before>import sys
import datetime
import inspect
import time
import logging
import inspect
import hashlib

from django.conf import settings

fo2logger = logging.getLogger('fo2')

cache_local = (
    settings.CACHES['default']['BACKEND'] ==
    'django.core.cache.backends.locmem.LocMemCache')

def arg_def(kwargs, arg, default):
    if arg in kwargs:
        return kwargs[arg]
    return default

__dow_info = {
    0: {'name': 'segunda-feira', 'plural': 'segundas-feiras',
        'alias': 'segunda', 'abb': 'seg'},
    1: {'name': 'terça-feira', 'plural': 'terças-feiras',
        'alias': 'terça', 'abb': 'ter'},
    2: {'name': 'quarta-feira', 'plural': 'quartas-feiras',
        'alias': 'quarta', 'abb': 'qua'},
    3: {'name': 'quinta-feira', 'plural': 'quintas-feiras',
        'alias': 'quinta', 'abb': 'qui'},
    4: {'name': 'sexta-feira', 'plural': 'sextas-feiras',
        'alias': 'sexta', 'abb': 'sex'},
    5: {'name': 'sábado', 'plural': 'sábados',
        'alias': 'sábado', 'abb': 'sab'},
    6: {'name': 'domingo', 'plural': 'domingos',
        'alias': 'domingo', 'abb': 'dom'},
}

def dow_info(dt, info, capitalize=False):
    dow = dt.weekday()
    result = __dow_info[dow][info]
    if capitalize:
        result = result.capitalize()
    return result

def inc_month(dt, months):
    newmonth = (((dt.month - 1) + months) % 12) + 1
    newyear = dt.year + (((dt.month - 1) + months) // 12)
    newday = dt.day
    newdt = None
    while newdt is None:
        try:
            newdt = datetime.date(newyear, newmonth, newday)
        except ValueError:
            newday -= 1
    return newdt

def dec_month(dt, day=None):
    if day is None:
        day = dt.day
    dt = dt.replace(day=1)
    dt = dt - datetime.timedelta(days=1)
    for _ in range(4):
        try:
            dt_return = dt.replace(day=day)
            break
        except Exception:
            day -= 1
    return dt_return

def dec_months(dt, number, day=None):
    if day is None:
        day = dt.day
    if number == 0:
        return dt.replace(day=day)
    dt_return = dec_month(dt, day)
    if number == 1:
        return dt_return
    else:
        return dec_months(dt_return, number-1, day)

def dias_uteis_mes():
    hoje = datetime.date.today()
    mes = hoje.month
    um_dia = datetime.timedelta(days=1)

    uteis_passados = 0
    util_hoje = 0
    uteis_total = 0
    dia = hoje.replace(day=1)
    while True:
        if mes != dia.month:
            break
        dow = dia.weekday()
        if dow < 5:
            uteis_total += 1
            if dia < hoje:
                uteis_passados += 1
            if dia == hoje:
                util_hoje = 1
        dia = dia + um_dia
    return uteis_total, uteis_passados, util_hoje

def shift_years(years, from_date=None):
    if from_date is None:
        from_date = datetime.datetime.now()
    try:
        return from_date.replace(year=from_date.year + years)
    except ValueError:
        # Must be 2/29!
        return from_date.replace(month=2, day=28,
                                 year=from_date.year + years)

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

def segunda(dt):
    if dt:
        return dt + datetime.timedelta(days=-dt.weekday())
    else:
        return None

def max_not_None(*args):
    maxi = None
    for v in args:
        if v:
            if maxi:
                maxi = max(
                    maxi,
                    v)
            else:
                maxi = v
    return maxi

def min_not_None(*args):
    mini = None
    for v in args:
        if v:
            if mini:
                mini = min(
                    mini,
                    v)
            else:
                mini = v
    return mini

def debug(message, level=None, depth=1):
    callerframerecord = inspect.stack()[depth]
    frame = callerframerecord[0]
    info = inspect.getframeinfo(frame)
    if level is None:
        level = 0
    msg = ''
    if level >= 3:
        msg += 'file={filename}-'
    if level >= 2:
        msg += 'func={function}-'
    if level >= 1:
        msg += 'line={line}-'
    msg += '{message}'
    print(
        msg.format(
            filename=info.filename,
            function=info.function,
            line=info.lineno,
            message=message,
            )
        )
    sys.stdout.flush()

def line_tik():
    debug(int(round(time.time() * 1000))/1000, 1, depth=2)

def safe_cast(val, to_type, default=None):
    try:
        return to_type(val)
    except (ValueError, TypeError):
        return default

def is_number(s):
    try:
        float(s)
        return True
    except Exception:
        return False

def make_key_cache(ignore=[]):
    stack1 = inspect.stack()[1]
    argvalues = inspect.getargvalues(stack1.frame).locals
    values = []
    for key in argvalues:
        if key not in ignore:
            value = argvalues[key]
            if (value is None) \
                    or isinstance(value, str) \
                    or is_number(value):
                values.append(value)
    braces = ['{}'] * len(values)
    key = '|'.join([stack1.filename, *braces])
    key = key.format(*values)
    fo2logger.info(key)
    key = hashlib.md5(key.encode('utf-8')).hexdigest()
    key = '_'.join([stack1.function, key])
    return key

def cache_ttl(cache, key):
    if cache_local:
        exp_dt = cache._expire_info.get('FO2K:1:'+key)
        dt = datetime.datetime.fromtimestamp(exp_dt)
        now = datetime.datetime.now()
        ttl = dt - now
        seconds = ttl.seconds
    else:
        seconds = cache.ttl(key)
    fo2logger.info(seconds)
    return seconds

def untuple_keys(dict_):
    result = {}
    for key in dict_:
        if isinstance(key, tuple):
            for sub_key in key:
                result[sub_key] = dict_[key]
        else:
            result[key] = dict_[key]
    return result
<commit_msg>criando função strdmy2date que devolve um date a partir de um mm/dd/yyyy<commit_after>import sys
import datetime
import inspect
import time
import logging
import inspect
import hashlib

from django.conf import settings

fo2logger = logging.getLogger('fo2')

cache_local = (
    settings.CACHES['default']['BACKEND'] ==
    'django.core.cache.backends.locmem.LocMemCache')

def arg_def(kwargs, arg, default):
    if arg in kwargs:
        return kwargs[arg]
    return default

__dow_info = {
    0: {'name': 'segunda-feira', 'plural': 'segundas-feiras',
        'alias': 'segunda', 'abb': 'seg'},
    1: {'name': 'terça-feira', 'plural': 'terças-feiras',
        'alias': 'terça', 'abb': 'ter'},
    2: {'name': 'quarta-feira', 'plural': 'quartas-feiras',
        'alias': 'quarta', 'abb': 'qua'},
    3: {'name': 'quinta-feira', 'plural': 'quintas-feiras',
        'alias': 'quinta', 'abb': 'qui'},
    4: {'name': 'sexta-feira', 'plural': 'sextas-feiras',
        'alias': 'sexta', 'abb': 'sex'},
    5: {'name': 'sábado', 'plural': 'sábados',
        'alias': 'sábado', 'abb': 'sab'},
    6: {'name': 'domingo', 'plural': 'domingos',
        'alias': 'domingo', 'abb': 'dom'},
}

def dow_info(dt, info, capitalize=False):
    dow = dt.weekday()
    result = __dow_info[dow][info]
    if capitalize:
        result = result.capitalize()
    return result

def inc_month(dt, months):
    newmonth = (((dt.month - 1) + months) % 12) + 1
    newyear = dt.year + (((dt.month - 1) + months) // 12)
    newday = dt.day
    newdt = None
    while newdt is None:
        try:
            newdt = datetime.date(newyear, newmonth, newday)
        except ValueError:
            newday -= 1
    return newdt

def dec_month(dt, day=None):
    if day is None:
        day = dt.day
    dt = dt.replace(day=1)
    dt = dt - datetime.timedelta(days=1)
    for _ in range(4):
        try:
            dt_return = dt.replace(day=day)
            break
        except Exception:
            day -= 1
    return dt_return

def dec_months(dt, number, day=None):
    if day is None:
        day = dt.day
    if number == 0:
        return dt.replace(day=day)
    dt_return = dec_month(dt, day)
    if number == 1:
        return dt_return
    else:
        return dec_months(dt_return, number-1, day)

def dias_uteis_mes():
    hoje = datetime.date.today()
    mes = hoje.month
    um_dia = datetime.timedelta(days=1)

    uteis_passados = 0
    util_hoje = 0
    uteis_total = 0
    dia = hoje.replace(day=1)
    while True:
        if mes != dia.month:
            break
        dow = dia.weekday()
        if dow < 5:
            uteis_total += 1
            if dia < hoje:
                uteis_passados += 1
            if dia == hoje:
                util_hoje = 1
        dia = dia + um_dia
    return uteis_total, uteis_passados, util_hoje

def shift_years(years, from_date=None):
    if from_date is None:
        from_date = datetime.datetime.now()
    try:
        return from_date.replace(year=from_date.year + years)
    except ValueError:
        # Must be 2/29!
        return from_date.replace(month=2, day=28,
                                 year=from_date.year + years)

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

def segunda(dt):
    if dt:
        return dt + datetime.timedelta(days=-dt.weekday())
    else:
        return None

def max_not_None(*args):
    maxi = None
    for v in args:
        if v:
            if maxi:
                maxi = max(
                    maxi,
                    v)
            else:
                maxi = v
    return maxi

def min_not_None(*args):
    mini = None
    for v in args:
        if v:
            if mini:
                mini = min(
                    mini,
                    v)
            else:
                mini = v
    return mini

def debug(message, level=None, depth=1):
    callerframerecord = inspect.stack()[depth]
    frame = callerframerecord[0]
    info = inspect.getframeinfo(frame)
    if level is None:
        level = 0
    msg = ''
    if level >= 3:
        msg += 'file={filename}-'
    if level >= 2:
        msg += 'func={function}-'
    if level >= 1:
        msg += 'line={line}-'
    msg += '{message}'
    print(
        msg.format(
            filename=info.filename,
            function=info.function,
            line=info.lineno,
            message=message,
            )
        )
    sys.stdout.flush()

def line_tik():
    debug(int(round(time.time() * 1000))/1000, 1, depth=2)

def safe_cast(val, to_type, default=None):
    try:
        return to_type(val)
    except (ValueError, TypeError):
        return default

def is_number(s):
    try:
        float(s)
        return True
    except Exception:
        return False

def make_key_cache(ignore=[]):
    stack1 = inspect.stack()[1]
    argvalues = inspect.getargvalues(stack1.frame).locals
    values = []
    for key in argvalues:
        if key not in ignore:
            value = argvalues[key]
            if (value is None) \
                    or isinstance(value, str) \
                    or is_number(value):
                values.append(value)
    braces = ['{}'] * len(values)
    key = '|'.join([stack1.filename, *braces])
    key = key.format(*values)
    fo2logger.info(key)
    key = hashlib.md5(key.encode('utf-8')).hexdigest()
    key = '_'.join([stack1.function, key])
    return key

def cache_ttl(cache, key):
    if cache_local:
        exp_dt = cache._expire_info.get('FO2K:1:'+key)
        dt = datetime.datetime.fromtimestamp(exp_dt)
        now = datetime.datetime.now()
        ttl = dt - now
        seconds = ttl.seconds
    else:
        seconds = cache.ttl(key)
    fo2logger.info(seconds)
    return seconds

def untuple_keys(dict_):
    result = {}
    for key in dict_:
        if isinstance(key, tuple):
            for sub_key in key:
                result[sub_key] = dict_[key]
        else:
            result[key] = dict_[key]
    return result

def strdmy2date(text):
    return datetime.datetime.strptime(text, '%d/%m/%Y').date()
<|endoftext|>


<commit_before>"""Paste Template and Pylons utility functions

PylonsTemplate is a Paste Template sub-class that configures the source
directory and default plug-ins for a new Pylons project. The minimal template
provides a more minimal template with less additional directories and layout.
"""
import warnings

from paste.script.appinstall import Installer
from paste.script.templates import Template

import pylons
import pylons.helpers
import pylons.i18n

def func_move(name, moved_to='pylons.i18n'):
    return ("The %s function has moved to %s, please update your import "
            "statements to reflect the move." % (name, moved_to))

def deprecated(func, message):
    def deprecated_method(*args, **kargs):
        warnings.warn(message, DeprecationWarning, 2)
        return func(*args, **kargs)
    try:
        deprecated_method.__name__ = func.__name__
    except TypeError: # Python < 2.4
        pass
    deprecated_method.__doc__ = message + "\n\n" + func.__doc__
    return deprecated_method

get_lang = deprecated(pylons.i18n.get_lang, func_move('get_lang'))
set_lang = deprecated(pylons.i18n.set_lang, func_move('set_lang'))
_ = deprecated(pylons.i18n._, func_move('_'))
log = deprecated(pylons.helpers.log, func_move('log',
                                               moved_to='pylons.helpers'))

def get_prefix(environ, warn=True):
    if warn:
        warnings.warn("The get_prefix function is deprecated, please use "
                      "environ.get('SCRIPT_NAME', '') instead.",
                      DeprecationWarning, 2)
    if 'paste.config' in environ:
        prefix = environ['paste.config']['app_conf'].get('prefix', '')
    else:
        # Not ideal but if the error occurs before the paste.config is
        # available not a lot we can do
        prefix = ''
    if not prefix:
        if environ.get('SCRIPT_NAME', '') != '':
            prefix = environ['SCRIPT_NAME']
    return prefix

def class_name_from_module_name(module_name):
    """Takes a module name and returns the name of the class it defines.

    If the module name contains dashes, they are replaced with underscores.

    Example::

        >>> class_name_from_module_name('with-dashes')
        'WithDashes'
        >>> class_name_from_module_name('with_underscores')
        'WithUnderscores'
        >>> class_name_from_module_name('oneword')
        'Oneword'
    """
    words = module_name.replace('-', '_').split('_')
    return ''.join([w.title() for w in words])

def config_get(key, default=None):
    """Return a value from ``CONFIG``. Supports both ``paste.config.CONFIG``
    and the older ``paste.deploy.CONFIG``.

    This is for Pylons internal use only; to support both versions of the
    ``CONFIG`` objects, depending on which is used by the current Pylons
    web app."""
    return pylons.config.get(key, default)

class ContextObj(object):
    """The 'c' object, with strict attribute access (raises an Exception when
    the attribute does not exist)"""
    def __repr__(self):
        attrs = [(name, value)
                 for name, value in self.__dict__.items()
                 if not name.startswith('_')]
        attrs.sort()
        parts = []
        for name, value in attrs:
            value_repr = repr(value)
            if len(value_repr) > 70:
                value_repr = value_repr[:60] + '...' + value_repr[-5:]
            parts.append(' %s=%s' % (name, value_repr))
        return '<%s.%s at %s%s>' % (
            self.__class__.__module__,
            self.__class__.__name__,
            hex(id(self)),
            ','.join(parts))

class AttribSafeContextObj(ContextObj):
    """The 'c' object, with lax attribute access (returns '' when the attribute
    does not exist)"""
    def __getattr__(self, name):
        try:
            return object.__getattribute__(self, name)
        except AttributeError:
            return ''

class PylonsTemplate(Template):
    _template_dir = 'templates/default_project'
    summary = 'Pylons application template'
    egg_plugins = ['Pylons', 'WebHelpers']

    def pre(self, command, output_dir, vars):
        """Called before template is applied."""
        vars.setdefault('template_engine',
                        pylons.config['default_template_engine'])

class MinimalPylonsTemplate(PylonsTemplate):
    _template_dir = 'templates/minimal_project'
    summary = 'Pylons minimal application template'

class PylonsInstaller(Installer):
    use_cheetah = False

__all__ = ['AttribSafeContextObj', 'ContextObj', 'Helpers',
           'class_name_from_module_name', 'log', '_', 'set_lang', 'get_lang']
__pudge_all__ = __all__ + ['MinimalPylonsTemplate', 'PylonsTemplate']
<commit_msg>[svn] o fix default_template_engine reference o remove config_get, PylonsConfig handles this now. and it was always a Pylons internal function o docstring the deprecated get_prefix<commit_after>"""Paste Template and Pylons utility functions

PylonsTemplate is a Paste Template sub-class that configures the source
directory and default plug-ins for a new Pylons project. The minimal template
provides a more minimal template with less additional directories and layout.
"""
import warnings

from paste.script.appinstall import Installer
from paste.script.templates import Template

import pylons
import pylons.configuration
import pylons.helpers
import pylons.i18n

def func_move(name, moved_to='pylons.i18n'):
    return ("The %s function has moved to %s, please update your import "
            "statements to reflect the move." % (name, moved_to))

def deprecated(func, message):
    def deprecated_method(*args, **kargs):
        warnings.warn(message, DeprecationWarning, 2)
        return func(*args, **kargs)
    try:
        deprecated_method.__name__ = func.__name__
    except TypeError: # Python < 2.4
        pass
    deprecated_method.__doc__ = message + "\n\n" + func.__doc__
    return deprecated_method

get_lang = deprecated(pylons.i18n.get_lang, func_move('get_lang'))
set_lang = deprecated(pylons.i18n.set_lang, func_move('set_lang'))
_ = deprecated(pylons.i18n._, func_move('_'))
log = deprecated(pylons.helpers.log, func_move('log',
                                               moved_to='pylons.helpers'))

def get_prefix(environ, warn=True):
    """Deprecated: Use environ.get('SCRIPT_NAME', '') instead"""
    if warn:
        warnings.warn("The get_prefix function is deprecated, please use "
                      "environ.get('SCRIPT_NAME', '') instead.",
                      DeprecationWarning, 2)
    if 'paste.config' in environ:
        prefix = environ['paste.config']['app_conf'].get('prefix', '')
    else:
        # Not ideal but if the error occurs before the paste.config is
        # available not a lot we can do
        prefix = ''
    if not prefix:
        if environ.get('SCRIPT_NAME', '') != '':
            prefix = environ['SCRIPT_NAME']
    return prefix

def class_name_from_module_name(module_name):
    """Takes a module name and returns the name of the class it defines.

    If the module name contains dashes, they are replaced with underscores.

    Example::

        >>> class_name_from_module_name('with-dashes')
        'WithDashes'
        >>> class_name_from_module_name('with_underscores')
        'WithUnderscores'
        >>> class_name_from_module_name('oneword')
        'Oneword'
    """
    words = module_name.replace('-', '_').split('_')
    return ''.join([w.title() for w in words])

class ContextObj(object):
    """The 'c' object, with strict attribute access (raises an Exception when
    the attribute does not exist)"""
    def __repr__(self):
        attrs = [(name, value)
                 for name, value in self.__dict__.items()
                 if not name.startswith('_')]
        attrs.sort()
        parts = []
        for name, value in attrs:
            value_repr = repr(value)
            if len(value_repr) > 70:
                value_repr = value_repr[:60] + '...' + value_repr[-5:]
            parts.append(' %s=%s' % (name, value_repr))
        return '<%s.%s at %s%s>' % (
            self.__class__.__module__,
            self.__class__.__name__,
            hex(id(self)),
            ','.join(parts))

class AttribSafeContextObj(ContextObj):
    """The 'c' object, with lax attribute access (returns '' when the attribute
    does not exist)"""
    def __getattr__(self, name):
        try:
            return object.__getattribute__(self, name)
        except AttributeError:
            return ''

class PylonsTemplate(Template):
    _template_dir = 'templates/default_project'
    summary = 'Pylons application template'
    egg_plugins = ['Pylons', 'WebHelpers']

    def pre(self, command, output_dir, vars):
        """Called before template is applied."""
        vars.setdefault('template_engine',
                        pylons.configuration.default_template_engine)

class MinimalPylonsTemplate(PylonsTemplate):
    _template_dir = 'templates/minimal_project'
    summary = 'Pylons minimal application template'

class PylonsInstaller(Installer):
    use_cheetah = False

__all__ = ['AttribSafeContextObj', 'ContextObj', 'Helpers',
           'class_name_from_module_name', 'log', '_', 'set_lang', 'get_lang']
__pudge_all__ = __all__ + ['MinimalPylonsTemplate', 'PylonsTemplate']
<|endoftext|>


<commit_before>from point import *
from trace import NpTrace, MultiTrace
import multiprocessing as mp
from time import time
from core import *
import step_methods
from progressbar import progress_bar
from numpy.random import seed

__all__ = ['sample', 'psample']

def sample(draws, step, start={}, trace=None, progressbar=True, model=None, random_seed=None):
    """
    Draw a number of samples using the given step method.
    Multiple step methods supported via compound step method
    returns the amount of time taken.

    Parameters
    ----------

    draws : int
        The number of samples to draw
    step : function
        A step function
    start : dict
        Starting point in parameter space (or partial point)
        Defaults to trace.point(-1)) if there is a trace provided and
        model.test_point if not (defaults to empty dict)
    trace : NpTrace or list
        Either a trace of past values or a list of variables to track
        (defaults to None)
    progressbar : bool
        Flag for progress bar
    model : Model (optional if in `with` context)

    """
    model = modelcontext(model)
    draws = int(draws)
    seed(random_seed)

    if isinstance(trace, NpTrace) and len(trace) > 0:

        trace_point = trace.point(-1)
        trace_point.update(start)
        start = trace_point

    else:

        test_point = model.test_point.copy()
        test_point.update(start)
        start = test_point

        if not isinstance(trace, NpTrace):
            if trace is None:
                trace = model.vars
            trace = NpTrace(list(trace))

    try:
        step = step_methods.CompoundStep(step)
    except TypeError:
        pass

    point = Point(start, model=model)

    progress = progress_bar(draws)

    for i in xrange(draws):
        point = step.step(point)
        trace = trace.record(point)
        if progressbar:
            progress.update(i)

    return trace

def argsample(args):
    """ defined at top level so it can be pickled"""
    return sample(*args)

def psample(draws, step, start={}, trace=None, model=None, threads=None,
    random_seeds=None):
    """draw a number of samples using the given step method.
    Multiple step methods supported via compound step method
    returns the amount of time taken

    Parameters
    ----------

    draws : int
        The number of samples to draw
    step : function
        A step function
    start : dict
        Starting point in parameter space (Defaults to trace.point(-1))
    trace : MultiTrace or list
        Either a trace of past values or a list of variables to track (defaults to None)
    model : Model (optional if in `with` context)
    threads : int
        Number of parallel traces to start

    Examples
    --------

    >>> an example

    """

    model = modelcontext(model)

    if not threads:
        threads = max(mp.cpu_count() - 2, 1)

    if isinstance(start, dict):
        start = threads * [start]

    if trace is None:
        trace = model.vars

    if type(trace) is MultiTrace:
        mtrace = trace
    else:
        mtrace = MultiTrace(threads, trace)

    p = mp.Pool(threads)

    if random_seeds is None:
        random_seeds = [None] * threads

    argset = zip([draws] * threads, [step] * threads, start, mtrace.traces,
                 [False] * threads, [model] * threads, random_seeds)

    traces = p.map(argsample, argset)

    return MultiTrace(traces)
<commit_msg>Removed mutable default for start in sample and psample<commit_after>from point import *
from trace import NpTrace, MultiTrace
import multiprocessing as mp
from time import time
from core import *
import step_methods
from progressbar import progress_bar
from numpy.random import seed

__all__ = ['sample', 'psample']

def sample(draws, step, start=None, trace=None, progressbar=True, model=None, random_seed=None):
    """
    Draw a number of samples using the given step method.
    Multiple step methods supported via compound step method
    returns the amount of time taken.

    Parameters
    ----------

    draws : int
        The number of samples to draw
    step : function
        A step function
    start : dict
        Starting point in parameter space (or partial point)
        Defaults to trace.point(-1)) if there is a trace provided and
        model.test_point if not (defaults to empty dict)
    trace : NpTrace or list
        Either a trace of past values or a list of variables to track
        (defaults to None)
    progressbar : bool
        Flag for progress bar
    model : Model (optional if in `with` context)

    """
    model = modelcontext(model)
    draws = int(draws)
    seed(random_seed)

    if start is None:
        start = {}

    if isinstance(trace, NpTrace) and len(trace) > 0:

        trace_point = trace.point(-1)
        trace_point.update(start)
        start = trace_point

    else:

        test_point = model.test_point.copy()
        test_point.update(start)
        start = test_point

        if not isinstance(trace, NpTrace):
            if trace is None:
                trace = model.vars
            trace = NpTrace(list(trace))

    try:
        step = step_methods.CompoundStep(step)
    except TypeError:
        pass

    point = Point(start, model=model)

    progress = progress_bar(draws)

    for i in xrange(draws):
        point = step.step(point)
        trace = trace.record(point)
        if progressbar:
            progress.update(i)

    return trace

def argsample(args):
    """ defined at top level so it can be pickled"""
    return sample(*args)

def psample(draws, step, start=None, trace=None, model=None, threads=None,
    random_seeds=None):
    """draw a number of samples using the given step method.
    Multiple step methods supported via compound step method
    returns the amount of time taken

    Parameters
    ----------

    draws : int
        The number of samples to draw
    step : function
        A step function
    start : dict
        Starting point in parameter space (Defaults to trace.point(-1))
    trace : MultiTrace or list
        Either a trace of past values or a list of variables to track (defaults to None)
    model : Model (optional if in `with` context)
    threads : int
        Number of parallel traces to start

    Examples
    --------

    >>> an example

    """

    model = modelcontext(model)

    if not threads:
        threads = max(mp.cpu_count() - 2, 1)

    if start is None:
        start = {}

    if isinstance(start, dict):
        start = threads * [start]

    if trace is None:
        trace = model.vars

    if type(trace) is MultiTrace:
        mtrace = trace
    else:
        mtrace = MultiTrace(threads, trace)

    p = mp.Pool(threads)

    if random_seeds is None:
        random_seeds = [None] * threads

    argset = zip([draws] * threads, [step] * threads, start, mtrace.traces,
                 [False] * threads, [model] * threads, random_seeds)

    traces = p.map(argsample, argset)

    return MultiTrace(traces)
<|endoftext|>


<commit_before><commit_msg>Make it possible to explicitly load the current directory<commit_after><|endoftext|>


<commit_before><commit_msg>Increase version to 3.3<commit_after><|endoftext|>


<commit_before>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# All Rights Reserved
#
import os
import subprocess
import sys

from conary.lib import util

def call(cmds, env = None):
    print >> sys.stderr, "+ " + " ".join(cmds)
    sys.stderr.flush()
    kwargs = {'env': env}
    subprocess.call(cmds, **kwargs)

class Image(object):
    def run(self, cmd, *args):
        args = list(args)
        try:
            func = self.__getattribute__(cmd)
        except AttributeError:
            raise RuntimeError, "Invalid anaconda templates manifest image command: %s" % (cmd)

        if len(args) == 3:
            mode = int(args.pop(-1), 8)
        else:
            mode = 0644

        args[0] = os.path.join(self.tmpDir, args[0])
        args[1] = os.path.join(self.templateDir, args[1])
        output = args[1]
        util.mkdirChain(os.path.dirname(output))
        retcode = func(*args)
        os.chmod(output, mode)
        return retcode

    def cpiogz(self, inputDir, output):
        oldCwd = os.getcwd()
        os.chdir(inputDir)
        try:

            outputFile = file(output, "w")
            files = subprocess.Popen(['find', '.', '-depth', '-print'],
                                     stdout = subprocess.PIPE)
            cpio = subprocess.Popen(['cpio', '-o', '-c'],
                                    stdin = files.stdout,
                                    stdout = subprocess.PIPE,
                                    env = self.rootstatWrapper)
            gzip = subprocess.Popen(['gzip', '-9'], stdin = cpio.stdout,
                                    stdout = outputFile)

            cpio.communicate()
            outputFile.close()
        finally:
            try:
                os.chdir(oldCwd)
            except:
                pass

    def mkisofs(self, inputDir, output):
        cmd = ['mkisofs', '-quiet', '-o', output,
            '-b', 'isolinux/isolinux.bin',
            '-c', 'isolinux/boot.cat',
            '-no-emul-boot',
            '-boot-load-size', '4',
            '-boot-info-table',
            '-R', '-J', '-T',
            '-V', 'rPath Linux',
            inputDir]
        call(cmd)

    def mkcramfs(self, inputDir, output):
        cmd = ['mkcramfs', inputDir, output]
        call(cmd, env = self.rootstatWrapper)

    def mkdosfs(self, inputDir, output):
        call(['dd', 'if=/dev/zero', 'of=%s' % output, 'bs=1M', 'count=8'])
        call(['/sbin/mkdosfs', output])

        files = [os.path.join(inputDir, x) for x in os.listdir(inputDir)]
        cmds = ['mcopy', '-i', output] + files + ['::']
        call(cmds)

    def __init__(self, isocfg, templateDir, tmpDir):
        self.isocfg = isocfg
        self.templateDir = templateDir
        self.tmpDir = tmpDir

        self.rootstatWrapper = {'LD_PRELOAD': self.isocfg.rootstatWrapper}
<commit_msg>communicate along the correct pipes<commit_after>#
# Copyright (c) 2004-2006 rPath, Inc.
#
# All Rights Reserved
#
import os
import subprocess
import sys

from conary.lib import util

def call(cmds, env = None):
    print >> sys.stderr, "+ " + " ".join(cmds)
    sys.stderr.flush()
    kwargs = {'env': env}
    subprocess.call(cmds, **kwargs)

class Image(object):
    def run(self, cmd, *args):
        args = list(args)
        try:
            func = self.__getattribute__(cmd)
        except AttributeError:
            raise RuntimeError, "Invalid anaconda templates manifest image command: %s" % (cmd)

        if len(args) == 3:
            mode = int(args.pop(-1), 8)
        else:
            mode = 0644

        args[0] = os.path.join(self.tmpDir, args[0])
        args[1] = os.path.join(self.templateDir, args[1])
        output = args[1]
        util.mkdirChain(os.path.dirname(output))
        retcode = func(*args)
        os.chmod(output, mode)
        return retcode

    def cpiogz(self, inputDir, output):
        oldCwd = os.getcwd()
        os.chdir(inputDir)
        try:

            outputFile = file(output, "w")
            files = subprocess.Popen(['find', '.', '-depth', '-print'],
                                     stdout = subprocess.PIPE)
            cpio = subprocess.Popen(['cpio', '-o', '-c'],
                                    stdin = files.stdout,
                                    stdout = subprocess.PIPE,
                                    env = self.rootstatWrapper)
            gzip = subprocess.Popen(['gzip', '-9'], stdin = cpio.stdout,
                                    stdout = outputFile)
            gzip.communicate()
            outputFile.close()
        finally:
            try:
                os.chdir(oldCwd)
            except:
                pass

    def mkisofs(self, inputDir, output):
        cmd = ['mkisofs', '-quiet', '-o', output,
            '-b', 'isolinux/isolinux.bin',
            '-c', 'isolinux/boot.cat',
            '-no-emul-boot',
            '-boot-load-size', '4',
            '-boot-info-table',
            '-R', '-J', '-T',
            '-V', 'rPath Linux',
            inputDir]
        call(cmd)

    def mkcramfs(self, inputDir, output):
        cmd = ['mkcramfs', inputDir, output]
        call(cmd, env = self.rootstatWrapper)

    def mkdosfs(self, inputDir, output):
        call(['dd', 'if=/dev/zero', 'of=%s' % output, 'bs=1M', 'count=8'])
        call(['/sbin/mkdosfs', output])

        files = [os.path.join(inputDir, x) for x in os.listdir(inputDir)]
        cmds = ['mcopy', '-i', output] + files + ['::']
        call(cmds)

    def __init__(self, isocfg, templateDir, tmpDir):
        self.isocfg = isocfg
        self.templateDir = templateDir
        self.tmpDir = tmpDir

        self.rootstatWrapper = {'LD_PRELOAD': self.isocfg.rootstatWrapper}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import json

from django.core.urlresolvers import reverse
from django.forms import ValidationError

from mock import patch
from nose.tools import eq_

from mkt.api.tests.test_oauth import RestOAuth
from mkt.files.models import FileUpload
from mkt.langpacks.models import LangPack
from mkt.langpacks.tests.test_models import UploadCreationMixin, UploadTest
from mkt.site.fixtures import fixture
from mkt.users.models import UserProfile

class TestLangPackViewSet(RestOAuth):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSetCreate, self).setUp()
        self.url = reverse('api-v2:langpack-list')
        self.user = UserProfile.objects.get(pk=2519)

    def test_allowed_verbs(self):
        pass

    def test_cors(self):
        self.assertCORS(self.anon.options(self.url), 'get', 'delete',
                                                     'patch', 'post', 'put')

class TestLangPackViewSetCreate(RestOAuth, UploadCreationMixin, UploadTest):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSetCreate, self).setUp()
        self.url = reverse('api-v2:langpack-list')
        self.user = UserProfile.objects.get(pk=2519)

    def test_anonymous(self):
        response = self.anon.post(self.url)
        eq_(response.status_code, 403)

    def test_no_perms(self):
        response = self.client.post(self.url)
        eq_(response.status_code, 403)

    @patch('mkt.langpacks.serializers.LangPackUploadSerializer.is_valid',
           return_value=True)
    @patch('mkt.langpacks.serializers.LangPackUploadSerializer.save',
           return_value=None)
    def test_with_perm(self, mock_save, mock_is_valid):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url)
        eq_(response.status_code, 201)

    def test_no_upload(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url)
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'This field is required.']})

    def test_upload_does_not_exist(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-non-existing-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'No upload found.']})

    def test_dont_own_the_upload(self):
        FileUpload.objects.create(uuid='my-uuid', user=None, valid=True)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'No upload found.']})

    def test_invalid_upload(self):
        FileUpload.objects.create(uuid='my-uuid', valid=False, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'Upload not valid.']})

    @patch('mkt.langpacks.models.LangPack.from_upload')
    def test_errors_returned_by_from_upload(self, mock_from_upload):
        mock_from_upload.side_effect = ValidationError('foo bar')
        FileUpload.objects.create(uuid='my-uuid', valid=True, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'detail': [u'foo bar']})

    def test_everything_is_fine(self):
        eq_(LangPack.objects.count(), 0)
        upload = self.upload('langpack.zip', valid=True, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': upload.uuid}))
        eq_(response.status_code, 201)
        eq_(LangPack.objects.count(), 1)
        langpack = LangPack.objects.get()
        eq_(langpack.hash[0:23], 'sha256:f0fa5a4f5c0edf2d')
        eq_(langpack.size, 499)
        eq_(langpack.active, False)
        eq_(response.data['uuid'], langpack.uuid)
        eq_(response.data['hash'], langpack.hash)
        eq_(response.data['active'], langpack.active)

class TestLangPackViewSetPartialUpdate(RestOAuth):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSetPartialUpdate, self).setUp()
        self.create_langpack()
        self.url = reverse('api-v2:langpack-detail',
                           kwargs={'pk': self.langpack.pk})
        self.user = UserProfile.objects.get(pk=2519)

    def create_langpack(self):
        self.langpack = LangPack.objects.create(
            filename='dummy.zip', active=True, hash='dummy-hash',
            version='0.1', language='fr', fxos_version='2.2')

    def test_anonymous(self):
        response = self.anon.patch(self.url)
        eq_(response.status_code, 403)

    def test_no_perms(self):
        response = self.client.patch(self.url)
        eq_(response.status_code, 403)

    def test_with_perm(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.patch(self.url, json.dumps({'active': False}))
        eq_(response.status_code, 200)
        eq_(response.data['active'], False)
        self.langpack.reload()
        eq_(self.langpack.pk, response.data['uuid'])
        eq_(self.langpack.active, response.data['active'])

    def test_not_allowed_fields(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.patch(self.url, json.dumps({
            'active': False,
            'filename': 'dummy-data',
            'fxos_version': 'dummy-data',
            'hash': 'dummy-data',
            'language': 'es',
            'modified': 'dummy-data',
            'size': 666,
            'uuid': 'dummy-data',
            'version': 'dummy-data',
        }))
        eq_(response.status_code, 400)
        eq_(response.data, {
            'hash': [u'This field is read-only.'],
            'language': [u'This field is read-only.'],
            'fxos_version': [u'This field is read-only.'],
            'filename': [u'This field is read-only.'],
            'version': [u'This field is read-only.'],
            'size': [u'This field is read-only.']})
        self.langpack.reload()
        eq_(self.langpack.active, True)  # Not changed.
<commit_msg>fixed cors testing<commit_after># -*- coding: utf-8 -*-
import json

from django.core.urlresolvers import reverse
from django.forms import ValidationError

from mock import patch
from nose.tools import eq_

from mkt.api.tests.test_oauth import RestOAuth
from mkt.files.models import FileUpload
from mkt.langpacks.models import LangPack
from mkt.langpacks.tests.test_models import UploadCreationMixin, UploadTest
from mkt.site.fixtures import fixture
from mkt.users.models import UserProfile

class TestLangPackViewSet(RestOAuth):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSet, self).setUp()
        self.url = reverse('api-v2:langpack-list')
        self.detail_url = reverse('api-v2:langpack-detail', kwargs={'pk': 42})
        self.user = UserProfile.objects.get(pk=2519)

    def test_cors(self):
        self.assertCORS(self.anon.options(self.detail_url),
                        'get', 'delete', 'patch', 'post', 'put')
        self.assertCORS(self.anon.options(self.url),
                        'get', 'delete', 'patch', 'post', 'put')

class TestLangPackViewSetCreate(RestOAuth, UploadCreationMixin, UploadTest):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSetCreate, self).setUp()
        self.url = reverse('api-v2:langpack-list')
        self.user = UserProfile.objects.get(pk=2519)

    def test_anonymous(self):
        response = self.anon.post(self.url)
        eq_(response.status_code, 403)

    def test_no_perms(self):
        response = self.client.post(self.url)
        eq_(response.status_code, 403)

    @patch('mkt.langpacks.serializers.LangPackUploadSerializer.is_valid',
           return_value=True)
    @patch('mkt.langpacks.serializers.LangPackUploadSerializer.save',
           return_value=None)
    def test_with_perm(self, mock_save, mock_is_valid):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url)
        eq_(response.status_code, 201)

    def test_no_upload(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url)
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'This field is required.']})

    def test_upload_does_not_exist(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-non-existing-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'No upload found.']})

    def test_dont_own_the_upload(self):
        FileUpload.objects.create(uuid='my-uuid', user=None, valid=True)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'No upload found.']})

    def test_invalid_upload(self):
        FileUpload.objects.create(uuid='my-uuid', valid=False, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'upload': [u'Upload not valid.']})

    @patch('mkt.langpacks.models.LangPack.from_upload')
    def test_errors_returned_by_from_upload(self, mock_from_upload):
        mock_from_upload.side_effect = ValidationError('foo bar')
        FileUpload.objects.create(uuid='my-uuid', valid=True, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': 'my-uuid'}))
        eq_(response.status_code, 400)
        eq_(response.json, {u'detail': [u'foo bar']})

    def test_everything_is_fine(self):
        eq_(LangPack.objects.count(), 0)
        upload = self.upload('langpack.zip', valid=True, user=self.user)
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.post(self.url, data=json.dumps({
            'upload': upload.uuid}))
        eq_(response.status_code, 201)
        eq_(LangPack.objects.count(), 1)
        langpack = LangPack.objects.get()
        eq_(langpack.hash[0:23], 'sha256:f0fa5a4f5c0edf2d')
        eq_(langpack.size, 499)
        eq_(langpack.active, False)
        eq_(response.data['uuid'], langpack.uuid)
        eq_(response.data['hash'], langpack.hash)
        eq_(response.data['active'], langpack.active)

class TestLangPackViewSetPartialUpdate(RestOAuth):
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestLangPackViewSetPartialUpdate, self).setUp()
        self.create_langpack()
        self.url = reverse('api-v2:langpack-detail',
                           kwargs={'pk': self.langpack.pk})
        self.user = UserProfile.objects.get(pk=2519)

    def create_langpack(self):
        self.langpack = LangPack.objects.create(
            filename='dummy.zip', active=True, hash='dummy-hash',
            version='0.1', language='fr', fxos_version='2.2')

    def test_anonymous(self):
        response = self.anon.patch(self.url)
        eq_(response.status_code, 403)

    def test_no_perms(self):
        response = self.client.patch(self.url)
        eq_(response.status_code, 403)

    def test_with_perm(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.patch(self.url, json.dumps({'active': False}))
        eq_(response.status_code, 200)
        eq_(response.data['active'], False)
        self.langpack.reload()
        eq_(self.langpack.pk, response.data['uuid'])
        eq_(self.langpack.active, response.data['active'])

    def test_not_allowed_fields(self):
        self.grant_permission(self.user, 'LangPacks:%')

        response = self.client.patch(self.url, json.dumps({
            'active': False,
            'filename': 'dummy-data',
            'fxos_version': 'dummy-data',
            'hash': 'dummy-data',
            'language': 'es',
            'modified': 'dummy-data',
            'size': 666,
            'uuid': 'dummy-data',
            'version': 'dummy-data',
        }))
        eq_(response.status_code, 400)
        eq_(response.data, {
            'hash': [u'This field is read-only.'],
            'language': [u'This field is read-only.'],
            'fxos_version': [u'This field is read-only.'],
            'filename': [u'This field is read-only.'],
            'version': [u'This field is read-only.'],
            'size': [u'This field is read-only.']})
        self.langpack.reload()
        eq_(self.langpack.active, True)  # Not changed.
<|endoftext|>


<commit_before>"""
Utilities for dealing with artifacts in the context of a Run.
"""
import pathlib
import posixpath
import shutil
import tempfile
import urllib.parse

from mlflow.exceptions import MlflowException
from mlflow.protos.databricks_pb2 import INVALID_PARAMETER_VALUE
from mlflow.store.artifact.artifact_repository_registry import get_artifact_repository
from mlflow.store.artifact.dbfs_artifact_repo import DbfsRestArtifactRepository
from mlflow.store.artifact.models_artifact_repo import ModelsArtifactRepository
from mlflow.tracking._tracking_service.utils import _get_store
from mlflow.utils.uri import add_databricks_profile_info_to_artifact_uri, append_to_uri_path

def get_artifact_uri(run_id, artifact_path=None, tracking_uri=None):
    """
    Get the absolute URI of the specified artifact in the specified run. If `path` is not specified,
    the artifact root URI of the specified run will be returned; calls to ``log_artifact``
    and ``log_artifacts`` write artifact(s) to subdirectories of the artifact root URI.

    :param run_id: The ID of the run for which to obtain an absolute artifact URI.
    :param artifact_path: The run-relative artifact path. For example,
                          ``path/to/artifact``. If unspecified, the artifact root URI for the
                          specified run will be returned.
    :param tracking_uri: The tracking URI from which to get the run and its artifact location. If
                         not given, the current default tracking URI is used.
    :return: An *absolute* URI referring to the specified artifact or the specified run's artifact
             root. For example, if an artifact path is provided and the specified run uses an
             S3-backed  store, this may be a uri of the form
             ``s3://<bucket_name>/path/to/artifact/root/path/to/artifact``. If an artifact path
             is not provided and the specified run uses an S3-backed store, this may be a URI of
             the form ``s3://<bucket_name>/path/to/artifact/root``.
    """
    if not run_id:
        raise MlflowException(
            message="A run_id must be specified in order to obtain an artifact uri!",
            error_code=INVALID_PARAMETER_VALUE,
        )

    store = _get_store(tracking_uri)
    run = store.get_run(run_id)
    # Maybe move this method to RunsArtifactRepository so the circular dependency is clearer.
    assert urllib.parse.urlparse(run.info.artifact_uri).scheme != "runs"  # avoid an infinite loop
    if artifact_path is None:
        return run.info.artifact_uri
    else:
        return append_to_uri_path(run.info.artifact_uri, artifact_path)

# TODO: This would be much simpler if artifact_repo.download_artifacts could take the absolute path
# or no path.
def _download_artifact_from_uri(artifact_uri, output_path=None):
    """
    :param artifact_uri: The *absolute* URI of the artifact to download.
    :param output_path: The local filesystem path to which to download the artifact. If unspecified,
                        a local output path will be created.
    """
    parsed_uri = urllib.parse.urlparse(artifact_uri)
    prefix = ""
    if parsed_uri.scheme and not parsed_uri.path.startswith("/"):
        # relative path is a special case, urllib does not reconstruct it properly
        prefix = parsed_uri.scheme + ":"
        parsed_uri = parsed_uri._replace(scheme="")

    # For models:/ URIs, it doesn't make sense to initialize a ModelsArtifactRepository with only
    # the model name portion of the URI, then call download_artifacts with the version info.
    if ModelsArtifactRepository.is_models_uri(artifact_uri):
        root_uri = artifact_uri
        artifact_path = ""
    else:
        artifact_path = posixpath.basename(parsed_uri.path)
        parsed_uri = parsed_uri._replace(path=posixpath.dirname(parsed_uri.path))
        root_uri = prefix + urllib.parse.urlunparse(parsed_uri)

    return get_artifact_repository(artifact_uri=root_uri).download_artifacts(
        artifact_path=artifact_path, dst_path=output_path
    )

def _upload_artifacts_to_databricks(
    source, run_id, source_host_uri=None, target_databricks_profile_uri=None
):
    """
    Copy the artifacts from ``source`` to the destination Databricks workspace (DBFS) given by
    ``databricks_profile_uri`` or the current tracking URI.
    :param source: Source location for the artifacts to copy.
    :param run_id: Run ID to associate the artifacts with.
    :param source_host_uri: Specifies the source artifact's host URI (e.g. Databricks tracking URI)
        if applicable. If not given, defaults to the current tracking URI.
    :param target_databricks_profile_uri: Specifies the destination Databricks host. If not given,
        defaults to the current tracking URI.
    :return: The DBFS location in the target Databricks workspace the model files have been
        uploaded to.
    """
    from uuid import uuid4

    local_dir = tempfile.mkdtemp()
    try:
        source_with_profile = add_databricks_profile_info_to_artifact_uri(source, source_host_uri)
        _download_artifact_from_uri(source_with_profile, local_dir)
        dest_root = "dbfs:/databricks/mlflow/tmp-external-source/"
        dest_root_with_profile = add_databricks_profile_info_to_artifact_uri(
            dest_root, target_databricks_profile_uri
        )
        dest_repo = DbfsRestArtifactRepository(dest_root_with_profile)
        dest_artifact_path = run_id if run_id else uuid4().hex
        # Allow uploading from the same run id multiple times by randomizing a suffix
        if len(dest_repo.list_artifacts(dest_artifact_path)) > 0:
            dest_artifact_path = dest_artifact_path + "-" + uuid4().hex[0:4]
        dest_repo.log_artifacts(local_dir, artifact_path=dest_artifact_path)
        dirname = pathlib.PurePath(source).name  # innermost directory name
        return posixpath.join(dest_root, dest_artifact_path, dirname)  # new source
    finally:
        shutil.rmtree(local_dir)
<commit_msg>Support instance of pathlib.PosixPath as artifact URI (#3468)<commit_after>"""
Utilities for dealing with artifacts in the context of a Run.
"""
import pathlib
import posixpath
import shutil
import tempfile
import urllib.parse

from mlflow.exceptions import MlflowException
from mlflow.protos.databricks_pb2 import INVALID_PARAMETER_VALUE
from mlflow.store.artifact.artifact_repository_registry import get_artifact_repository
from mlflow.store.artifact.dbfs_artifact_repo import DbfsRestArtifactRepository
from mlflow.store.artifact.models_artifact_repo import ModelsArtifactRepository
from mlflow.tracking._tracking_service.utils import _get_store
from mlflow.utils.uri import add_databricks_profile_info_to_artifact_uri, append_to_uri_path

def get_artifact_uri(run_id, artifact_path=None, tracking_uri=None):
    """
    Get the absolute URI of the specified artifact in the specified run. If `path` is not specified,
    the artifact root URI of the specified run will be returned; calls to ``log_artifact``
    and ``log_artifacts`` write artifact(s) to subdirectories of the artifact root URI.

    :param run_id: The ID of the run for which to obtain an absolute artifact URI.
    :param artifact_path: The run-relative artifact path. For example,
                          ``path/to/artifact``. If unspecified, the artifact root URI for the
                          specified run will be returned.
    :param tracking_uri: The tracking URI from which to get the run and its artifact location. If
                         not given, the current default tracking URI is used.
    :return: An *absolute* URI referring to the specified artifact or the specified run's artifact
             root. For example, if an artifact path is provided and the specified run uses an
             S3-backed  store, this may be a uri of the form
             ``s3://<bucket_name>/path/to/artifact/root/path/to/artifact``. If an artifact path
             is not provided and the specified run uses an S3-backed store, this may be a URI of
             the form ``s3://<bucket_name>/path/to/artifact/root``.
    """
    if not run_id:
        raise MlflowException(
            message="A run_id must be specified in order to obtain an artifact uri!",
            error_code=INVALID_PARAMETER_VALUE,
        )

    store = _get_store(tracking_uri)
    run = store.get_run(run_id)
    # Maybe move this method to RunsArtifactRepository so the circular dependency is clearer.
    assert urllib.parse.urlparse(run.info.artifact_uri).scheme != "runs"  # avoid an infinite loop
    if artifact_path is None:
        return run.info.artifact_uri
    else:
        return append_to_uri_path(run.info.artifact_uri, artifact_path)

# TODO: This would be much simpler if artifact_repo.download_artifacts could take the absolute path
# or no path.
def _download_artifact_from_uri(artifact_uri, output_path=None):
    """
    :param artifact_uri: The *absolute* URI of the artifact to download.
    :param output_path: The local filesystem path to which to download the artifact. If unspecified,
                        a local output path will be created.
    """
    parsed_uri = urllib.parse.urlparse(str(artifact_uri))
    prefix = ""
    if parsed_uri.scheme and not parsed_uri.path.startswith("/"):
        # relative path is a special case, urllib does not reconstruct it properly
        prefix = parsed_uri.scheme + ":"
        parsed_uri = parsed_uri._replace(scheme="")

    # For models:/ URIs, it doesn't make sense to initialize a ModelsArtifactRepository with only
    # the model name portion of the URI, then call download_artifacts with the version info.
    if ModelsArtifactRepository.is_models_uri(artifact_uri):
        root_uri = artifact_uri
        artifact_path = ""
    else:
        artifact_path = posixpath.basename(parsed_uri.path)
        parsed_uri = parsed_uri._replace(path=posixpath.dirname(parsed_uri.path))
        root_uri = prefix + urllib.parse.urlunparse(parsed_uri)

    return get_artifact_repository(artifact_uri=root_uri).download_artifacts(
        artifact_path=artifact_path, dst_path=output_path
    )

def _upload_artifacts_to_databricks(
    source, run_id, source_host_uri=None, target_databricks_profile_uri=None
):
    """
    Copy the artifacts from ``source`` to the destination Databricks workspace (DBFS) given by
    ``databricks_profile_uri`` or the current tracking URI.
    :param source: Source location for the artifacts to copy.
    :param run_id: Run ID to associate the artifacts with.
    :param source_host_uri: Specifies the source artifact's host URI (e.g. Databricks tracking URI)
        if applicable. If not given, defaults to the current tracking URI.
    :param target_databricks_profile_uri: Specifies the destination Databricks host. If not given,
        defaults to the current tracking URI.
    :return: The DBFS location in the target Databricks workspace the model files have been
        uploaded to.
    """
    from uuid import uuid4

    local_dir = tempfile.mkdtemp()
    try:
        source_with_profile = add_databricks_profile_info_to_artifact_uri(source, source_host_uri)
        _download_artifact_from_uri(source_with_profile, local_dir)
        dest_root = "dbfs:/databricks/mlflow/tmp-external-source/"
        dest_root_with_profile = add_databricks_profile_info_to_artifact_uri(
            dest_root, target_databricks_profile_uri
        )
        dest_repo = DbfsRestArtifactRepository(dest_root_with_profile)
        dest_artifact_path = run_id if run_id else uuid4().hex
        # Allow uploading from the same run id multiple times by randomizing a suffix
        if len(dest_repo.list_artifacts(dest_artifact_path)) > 0:
            dest_artifact_path = dest_artifact_path + "-" + uuid4().hex[0:4]
        dest_repo.log_artifacts(local_dir, artifact_path=dest_artifact_path)
        dirname = pathlib.PurePath(source).name  # innermost directory name
        return posixpath.join(dest_root, dest_artifact_path, dirname)  # new source
    finally:
        shutil.rmtree(local_dir)
<|endoftext|>


<commit_before>from random import uniform

class Neuron:
    def __init__(self, parents=[]):
        self.parents = parents
        self.weights = [uniform(-1, 1) for parent in self.parents]
        self.gradient = [uniform(-1, 1) for parent in self.parents]

    def calculate(self):
        self.output = sum([parent.output * self.weights[i] for i, parent in enumerate(self.parents)]) > 0

class NeuronNetwork:
    def __init__(self, inputs, outputs, rows, columns):
        self.neurons = []
        for row in xrange(rows + 2):
            self.neurons.append([])
            if row == 0:
                for input_ in xrange(inputs):
                    self.neurons[row].append(Neuron(parents=[]))
            elif row == rows + 1:
                for output in xrange(outputs):
                    self.neurons[row].append(Neuron(parents=self.neurons[row - 1]))
            else:
                for column in xrange(columns):
                    self.neurons[row].append(Neuron(parents=self.neurons[row - 1]))

    def run(self, inputs):
        for i, neuron_row in enumerate(self.neurons):
            for j, neuron in enumerate(neuron_row):
                if i == 0:
                    neuron.output = inputs[j]
                else:
                    neuron.calculate()
        return [neuron.output for neuron in self.neurons[len(self.neurons) - 1]]
<commit_msg>Use gradient in Neuron.calculate()<commit_after>from random import uniform

class Neuron:
    def __init__(self, parents=[]):
        self.parents = parents
        self.weights = [uniform(-1, 1) for parent in self.parents]
        self.gradient = [uniform(-1, 1) for parent in self.parents]

    def calculate(self, increment=0):
        self.output = sum([parent.output * (self.weights[i] + increment*self.gradient[i]) for i, parent in enumerate(self.parents)]) > 0

class NeuronNetwork:
    def __init__(self, inputs, outputs, rows, columns):
        self.neurons = []
        for row in xrange(rows + 2):
            self.neurons.append([])
            if row == 0:
                for input_ in xrange(inputs):
                    self.neurons[row].append(Neuron(parents=[]))
            elif row == rows + 1:
                for output in xrange(outputs):
                    self.neurons[row].append(Neuron(parents=self.neurons[row - 1]))
            else:
                for column in xrange(columns):
                    self.neurons[row].append(Neuron(parents=self.neurons[row - 1]))

    def run(self, inputs):
        for i, neuron_row in enumerate(self.neurons):
            for j, neuron in enumerate(neuron_row):
                if i == 0:
                    neuron.output = inputs[j]
                else:
                    neuron.calculate()
        return [neuron.output for neuron in self.neurons[len(self.neurons) - 1]]
<|endoftext|>


<commit_before>from collections import defaultdict
from PyQt5 import QtWidgets
import Xlib
import Xlib.display

class WindowTitleProvider(object):
    delay = 0

    def __init__(self, main_window, workspaces_updater):
        self._updater = workspaces_updater
        self._labels = []
        for i, _ in enumerate(main_window):
            label = QtWidgets.QLabel()
            label.setProperty('class', 'wintitle')
            main_window[i].left_widget.layout().addWidget(label)
            self._labels.append(label)
        self._desktop_id_to_window_title = defaultdict(str)

        self._disp = Xlib.display.Display()
        self._root = self._disp.screen().root
        self._NET_WM_NAME = self._disp.intern_atom('_NET_WM_NAME')
        self._NET_WM_DESKTOP = self._disp.intern_atom('_NET_WM_DESKTOP')

        self._desktop_id_to_monitor = {}
        for i, m in enumerate(sorted(self._updater.monitors, key=lambda m: m.original_id)):
            for ws in m.workspaces:
                self._desktop_id_to_monitor[len(self._desktop_id_to_monitor)] = m.display_id

        self._update_titles(self._root)

    def refresh(self):
        event = self._disp.next_event()
        event_type = getattr(event, 'type', None)
        event_atom = getattr(event, 'atom', None)

        if event_atom == self._NET_WM_NAME \
                or event_type in [Xlib.X.FocusIn, Xlib.X.FocusOut]:
            self._update_titles(self._root)

    def render(self):
        for i, monitor in enumerate(self._updater.monitors):
            focused_desktops = [ws for ws in monitor.workspaces if ws.focused]
            if not focused_desktops:
                continue
            focused_desktop_title = focused_desktops[0].original_id
            self._labels[i].setText(
                self._desktop_id_to_window_title[focused_desktop_title] or '')

    def _update_titles(self, root_window):
        desktop_id_to_window_title = defaultdict(str)
        windows = [root_window]
        while windows:
            window = windows.pop()
            try:
                window.change_attributes(
                    event_mask=Xlib.X.FocusChangeMask | Xlib.X.PropertyChangeMask)

                result = window.get_full_property(self._NET_WM_DESKTOP, 0)
                desktop_id = result.value[0] if result else None
                if desktop_id is not None:
                    result = window.get_full_property(self._NET_WM_NAME, 0)
                    window_title = result.value if result else ''
                    if isinstance(window_title, bytes):
                        window_title = window_title.decode('utf8')
                    if desktop_id not in desktop_id_to_window_title:
                        desktop_id_to_window_title[desktop_id] = window_title

                for child in window.query_tree().children:
                    windows.append(child)
            except Xlib.error.BadWindow:
                pass

        if self._desktop_id_to_window_title != desktop_id_to_window_title:
            self._desktop_id_to_window_title = desktop_id_to_window_title
<commit_msg>mod-bspwm: trim long window titles<commit_after>from collections import defaultdict
from PyQt5 import QtWidgets, QtGui, QtCore
import Xlib
import Xlib.display

class WindowTitleProvider(object):
    delay = 0

    def __init__(self, main_window, workspaces_updater):
        self._updater = workspaces_updater
        self._labels = []
        for i, _ in enumerate(main_window):
            label = QtWidgets.QLabel()
            label.setProperty('class', 'wintitle')
            main_window[i].left_widget.layout().addWidget(label)
            self._labels.append(label)
            self._max_width = main_window[i].left_widget.width() * 0.8
        self._font_metrics = QtGui.QFontMetrics(self._labels[0].font())
        self._desktop_id_to_window_title = defaultdict(str)

        self._disp = Xlib.display.Display()
        self._root = self._disp.screen().root
        self._NET_WM_NAME = self._disp.intern_atom('_NET_WM_NAME')
        self._NET_WM_DESKTOP = self._disp.intern_atom('_NET_WM_DESKTOP')

        self._desktop_id_to_monitor = {}
        for i, m in enumerate(sorted(self._updater.monitors, key=lambda m: m.original_id)):
            for ws in m.workspaces:
                self._desktop_id_to_monitor[len(self._desktop_id_to_monitor)] = m.display_id

        self._update_titles(self._root)

    def refresh(self):
        event = self._disp.next_event()
        event_type = getattr(event, 'type', None)
        event_atom = getattr(event, 'atom', None)

        if event_atom == self._NET_WM_NAME \
                or event_type in [Xlib.X.FocusIn, Xlib.X.FocusOut]:
            self._update_titles(self._root)

    def render(self):
        for i, monitor in enumerate(self._updater.monitors):
            focused_desktops = [ws for ws in monitor.workspaces if ws.focused]
            if not focused_desktops:
                continue
            focused_desktop_title = focused_desktops[0].original_id
            self._labels[i].setText(
                self._font_metrics.elidedText(
                    self._desktop_id_to_window_title[focused_desktop_title] or '',
                    QtCore.Qt.ElideRight,
                    self._max_width))

    def _update_titles(self, root_window):
        desktop_id_to_window_title = defaultdict(str)
        windows = [root_window]
        while windows:
            window = windows.pop()
            try:
                window.change_attributes(
                    event_mask=Xlib.X.FocusChangeMask | Xlib.X.PropertyChangeMask)

                result = window.get_full_property(self._NET_WM_DESKTOP, 0)
                desktop_id = result.value[0] if result else None
                if desktop_id is not None:
                    result = window.get_full_property(self._NET_WM_NAME, 0)
                    window_title = result.value if result else ''
                    if isinstance(window_title, bytes):
                        window_title = window_title.decode('utf8')
                    if desktop_id not in desktop_id_to_window_title:
                        desktop_id_to_window_title[desktop_id] = window_title

                for child in window.query_tree().children:
                    windows.append(child)
            except Xlib.error.BadWindow:
                pass

        if self._desktop_id_to_window_title != desktop_id_to_window_title:
            self._desktop_id_to_window_title = desktop_id_to_window_title
<|endoftext|>


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

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

import ssl
import hashlib
from urlparse import urlsplit

from weboob.deprecated.browser import Browser, BrowserIncorrectPassword

from .pages import LoginPage, IndexPage, ErrorPage, UnavailablePage

__all__ = ['CaisseEpargne']

class CaisseEpargne(Browser):
    DOMAIN = 'www.caisse-epargne.fr'
    PROTOCOL = 'https'
    CERTHASH = ['dfff27d6db1fcdf1cea3ab8e3c1ca4f97c971262e95be49f3385b40c97fe640c', '9894ab2088630f341de821a09f1286c525f854f62ac186bd442368b4692c5969']
    PAGES = {'https://[^/]+.caisse-epargne.fr/particuliers/ind_pauthpopup.aspx.*':          LoginPage,
             'https://[^/]+.caisse-epargne.fr/Portail.aspx':                                IndexPage,
             'https://[^/]+.caisse-epargne.fr/login.aspx':                                  ErrorPage,
             'https://[^/]+.caisse-epargne.fr/Pages/logout.aspx.*':                         ErrorPage,
             'https://[^/]+.caisse-epargne.fr/page_hs_dei_.*.aspx':                         UnavailablePage,
            }

    def __init__(self, nuser, *args, **kwargs):
        self.nuser = nuser
        Browser.__init__(self, *args, **kwargs)

    def _certhash(self, domain, port=443):
        # XXX overload the Browser method to force use of TLSv1.
        certs = ssl.get_server_certificate((domain, port), ssl_version=ssl.PROTOCOL_TLSv1)
        return hashlib.sha256(certs).hexdigest()

    def is_logged(self):
        return self.page is not None and not self.is_on_page((LoginPage,ErrorPage))

    def home(self):
        if self.is_logged():
            self.location(self.buildurl('/Portail.aspx'))
        else:
            self.login()

    def login(self):
        """
        Attempt to log in.
        Note: this method does nothing if we are already logged in.
        """
        assert isinstance(self.username, basestring)
        assert isinstance(self.password, basestring)

        if self.is_logged():
            return

        self._ua_handlers['_cookies'].cookiejar.clear()
        if not self.is_on_page(LoginPage):
            self.location('https://www.caisse-epargne.fr/particuliers/ind_pauthpopup.aspx?mar=101&reg=&fctpopup=auth&cv=0', no_login=True)

        self.page.login(self.username)
        if not self.page.login2(self.nuser, self.password):
            # perso
            self.page.login3(self.password)

        if not self.is_logged():
            raise BrowserIncorrectPassword()

        v = urlsplit(self.page.url)
        self.DOMAIN = v.netloc

    def get_accounts_list(self):
        if self.is_on_page(IndexPage):
            self.page.go_list()
        else:
            self.location(self.buildurl('/Portail.aspx'))

        return self.page.get_list()

    def get_account(self, id):
        assert isinstance(id, basestring)

        l = self.get_accounts_list()
        for a in l:
            if a.id == id:
                return a

        return None

    def _get_history(self, info):
        if self.is_on_page(IndexPage):
            self.page.go_list()
        else:
            self.location(self.buildurl('/Portail.aspx'))

        self.page.go_history(info)

        while True:
            assert self.is_on_page(IndexPage)

            for tr in self.page.get_history():
                yield tr

            if not self.page.go_next():
                return

    def get_history(self, account):
        return self._get_history(account._info)

    def get_coming(self, account):
        for info in account._card_links:
            for tr in self._get_history(info):
                yield tr
<commit_msg>set type on card transactions<commit_after># -*- coding: utf-8 -*-

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

import ssl
import hashlib
from urlparse import urlsplit

from weboob.deprecated.browser import Browser, BrowserIncorrectPassword

from .pages import LoginPage, IndexPage, ErrorPage, UnavailablePage

__all__ = ['CaisseEpargne']

class CaisseEpargne(Browser):
    DOMAIN = 'www.caisse-epargne.fr'
    PROTOCOL = 'https'
    CERTHASH = ['dfff27d6db1fcdf1cea3ab8e3c1ca4f97c971262e95be49f3385b40c97fe640c', '9894ab2088630f341de821a09f1286c525f854f62ac186bd442368b4692c5969']
    PAGES = {'https://[^/]+.caisse-epargne.fr/particuliers/ind_pauthpopup.aspx.*':          LoginPage,
             'https://[^/]+.caisse-epargne.fr/Portail.aspx':                                IndexPage,
             'https://[^/]+.caisse-epargne.fr/login.aspx':                                  ErrorPage,
             'https://[^/]+.caisse-epargne.fr/Pages/logout.aspx.*':                         ErrorPage,
             'https://[^/]+.caisse-epargne.fr/page_hs_dei_.*.aspx':                         UnavailablePage,
            }

    def __init__(self, nuser, *args, **kwargs):
        self.nuser = nuser
        Browser.__init__(self, *args, **kwargs)

    def _certhash(self, domain, port=443):
        # XXX overload the Browser method to force use of TLSv1.
        certs = ssl.get_server_certificate((domain, port), ssl_version=ssl.PROTOCOL_TLSv1)
        return hashlib.sha256(certs).hexdigest()

    def is_logged(self):
        return self.page is not None and not self.is_on_page((LoginPage,ErrorPage))

    def home(self):
        if self.is_logged():
            self.location(self.buildurl('/Portail.aspx'))
        else:
            self.login()

    def login(self):
        """
        Attempt to log in.
        Note: this method does nothing if we are already logged in.
        """
        assert isinstance(self.username, basestring)
        assert isinstance(self.password, basestring)

        if self.is_logged():
            return

        self._ua_handlers['_cookies'].cookiejar.clear()
        if not self.is_on_page(LoginPage):
            self.location('https://www.caisse-epargne.fr/particuliers/ind_pauthpopup.aspx?mar=101&reg=&fctpopup=auth&cv=0', no_login=True)

        self.page.login(self.username)
        if not self.page.login2(self.nuser, self.password):
            # perso
            self.page.login3(self.password)

        if not self.is_logged():
            raise BrowserIncorrectPassword()

        v = urlsplit(self.page.url)
        self.DOMAIN = v.netloc

    def get_accounts_list(self):
        if self.is_on_page(IndexPage):
            self.page.go_list()
        else:
            self.location(self.buildurl('/Portail.aspx'))

        return self.page.get_list()

    def get_account(self, id):
        assert isinstance(id, basestring)

        l = self.get_accounts_list()
        for a in l:
            if a.id == id:
                return a

        return None

    def _get_history(self, info):
        if self.is_on_page(IndexPage):
            self.page.go_list()
        else:
            self.location(self.buildurl('/Portail.aspx'))

        self.page.go_history(info)

        while True:
            assert self.is_on_page(IndexPage)

            for tr in self.page.get_history():
                yield tr

            if not self.page.go_next():
                return

    def get_history(self, account):
        return self._get_history(account._info)

    def get_coming(self, account):
        for info in account._card_links:
            for tr in self._get_history(info):
                tr.type = tr.TYPE_CARD
                yield tr
<|endoftext|>


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

__version__ = "1.0.0"

def detect(aBuf):
    from . import universaldetector
    u = universaldetector.UniversalDetector()
    u.reset()
    u.feed(aBuf)
    u.close()
    return u.result
<commit_msg>charade v1.0.1<commit_after>######################## BEGIN LICENSE BLOCK ########################
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA
######################### END LICENSE BLOCK #########################

__version__ = "1.0.1"

def detect(aBuf):
    from . import universaldetector
    u = universaldetector.UniversalDetector()
    u.reset()
    u.feed(aBuf)
    u.close()
    return u.result
<|endoftext|>