<commit_before>import contextlib
import requests
import github3
import expecter
import json
import sys
from mock import patch
from io import BytesIO
from unittest import TestCase
from requests.structures import CaseInsensitiveDict

is_py3 = sys.version_info > (3, 0)

def load(name):
    with path(name) as f:
        j = json.load(f)
    return j

def path(name, mode='r'):
    return open('tests/json/{0}'.format(name), mode)

class CustomExpecter(expecter.expect):
    def is_not_None(self):
        assert self._actual is not None, (
            'Expected anything but None but got it.'
        )

    def is_None(self):
        assert self._actual is None, (
            'Expected None but got %s' % repr(self._actual)  # nopep8
        )

    def is_True(self):
        assert self._actual is True, (
            'Expected True but got %s' % repr(self._actual)  # nopep8
        )

    def is_False(self):
        assert self._actual is False, (
            'Expected False but got %s' % repr(self._actual)  # nopep8
        )

    def is_in(self, iterable):
        assert self._actual in iterable, (
            "Expected %s in %s but it wasn't" % (
                repr(self._actual), repr(iterable)
            )
        )

    @classmethod
    def githuberror(cls):
        return cls.raises(github3.GitHubError)

expect = CustomExpecter

class BaseCase(TestCase):
    github_url = 'https://api.github.com/'

    def setUp(self):
        self.g = github3.GitHub()
        self.args = ()
        self.conf = {'allow_redirects': True}
        self.mock = patch.object(requests.sessions.Session, 'request')
        self.request = self.mock.start()

    def tearDown(self):
        self.mock.stop()

    def login(self):
        self.g.login('user', 'password')

    def mock_assertions(self):
        assert self.request.called is True
        conf = self.conf.copy()
        args, kwargs = self.request.call_args

        assert self.args == args

        if 'data' in self.conf:
            if isinstance(self.conf['data'], dict):
                for k, v in list(self.conf['data'].items()):
                    s = json.dumps({k: v})[1:-1]
                    assert s in kwargs['data']
            else:
                assert self.conf['data'] == kwargs['data']

            del self.conf['data']

        for k in self.conf:
            assert k in kwargs
            assert self.conf[k] == kwargs[k]

        self.request.reset_mock()
        self.conf = conf

    def response(self, path_name, status_code=200, enc='utf-8',
                 _iter=False, **headers):
        r = requests.Response()
        r.status_code = status_code
        r.encoding = enc

        if path_name:
            with path(path_name) as f:
                content = f.read().strip()

            if _iter:
                content = '[{0}]'.format(content)
                r.raw = RequestsBytesIO(content.encode())
            elif is_py3:
                r.raw = RequestsBytesIO(content.encode())
            else:
                r.raw = RequestsBytesIO(content)
        else:
            r.raw = RequestsBytesIO()

        if headers:
            r.headers = CaseInsensitiveDict(headers)

        self.request.return_value = r

    def delete(self, url):
        self.args = ('DELETE', url)
        self.conf = {}

    def get(self, url):
        self.args = ('GET', url)

    def patch(self, url):
        self.args = ('PATCH', url)

    def post(self, url):
        self.args = ('POST', url)

    def put(self, url):
        self.args = ('PUT', url)

    def not_called(self):
        assert self.request.called is False

    def assertGitHubErrorRaised(self, func, *args, **kwargs):
        return self.assertRaises(github3.GitHubError, func(*args, **kwargs))

class RequestsBytesIO(BytesIO):
    def read(self, chunk_size, *args, **kwargs):
        return super(RequestsBytesIO, self).read(chunk_size)
<commit_msg>don't need expecter anymore!<commit_after>import requests
import github3
import json
import sys
from mock import patch
from io import BytesIO
from unittest import TestCase
from requests.structures import CaseInsensitiveDict

is_py3 = sys.version_info > (3, 0)

def load(name):
    with path(name) as f:
        j = json.load(f)
    return j

def path(name, mode='r'):
    return open('tests/json/{0}'.format(name), mode)

class BaseCase(TestCase):
    github_url = 'https://api.github.com/'

    def setUp(self):
        self.g = github3.GitHub()
        self.args = ()
        self.conf = {'allow_redirects': True}
        self.mock = patch.object(requests.sessions.Session, 'request')
        self.request = self.mock.start()

    def tearDown(self):
        self.mock.stop()

    def login(self):
        self.g.login('user', 'password')

    def mock_assertions(self):
        assert self.request.called is True
        conf = self.conf.copy()
        args, kwargs = self.request.call_args

        assert self.args == args

        if 'data' in self.conf:
            if isinstance(self.conf['data'], dict):
                for k, v in list(self.conf['data'].items()):
                    s = json.dumps({k: v})[1:-1]
                    assert s in kwargs['data']
            else:
                assert self.conf['data'] == kwargs['data']

            del self.conf['data']

        for k in self.conf:
            assert k in kwargs
            assert self.conf[k] == kwargs[k]

        self.request.reset_mock()
        self.conf = conf

    def response(self, path_name, status_code=200, enc='utf-8',
                 _iter=False, **headers):
        r = requests.Response()
        r.status_code = status_code
        r.encoding = enc

        if path_name:
            with path(path_name) as f:
                content = f.read().strip()

            if _iter:
                content = '[{0}]'.format(content)
                r.raw = RequestsBytesIO(content.encode())
            elif is_py3:
                r.raw = RequestsBytesIO(content.encode())
            else:
                r.raw = RequestsBytesIO(content)
        else:
            r.raw = RequestsBytesIO()

        if headers:
            r.headers = CaseInsensitiveDict(headers)

        self.request.return_value = r

    def delete(self, url):
        self.args = ('DELETE', url)
        self.conf = {}

    def get(self, url):
        self.args = ('GET', url)

    def patch(self, url):
        self.args = ('PATCH', url)

    def post(self, url):
        self.args = ('POST', url)

    def put(self, url):
        self.args = ('PUT', url)

    def not_called(self):
        assert self.request.called is False

    def assertGitHubErrorRaised(self, func, *args, **kwargs):
        return self.assertRaises(github3.GitHubError, func(*args, **kwargs))

class RequestsBytesIO(BytesIO):
    def read(self, chunk_size, *args, **kwargs):
        return super(RequestsBytesIO, self).read(chunk_size)
<|endoftext|>


<commit_before>import os
import tempfile
from contextlib import contextmanager

from gi.repository import GLib

def create_sparse_tempfile(name, size):
    """ Create a temporary sparse file.

        :param str name: suffix for filename
        :param size: the file size (in bytes)
        :returns: the path to the newly created file
    """
    (fd, path) = tempfile.mkstemp(prefix="libblockdev.", suffix="-%s" % name)
    os.close(fd)
    create_sparse_file(path, size)
    return path

def create_sparse_file(path, size):
    """ Create a sparse file.

        :param str path: the full path to the file
        :param size: the size of the file (in bytes)
        :returns: None
    """
    fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
    os.ftruncate(fd, size)
    os.close(fd)

@contextmanager
def udev_settle():
    yield
    os.system("udevadm settle")

@contextmanager
def fake_utils(path="."):
    old_path = os.environ.get("PATH", "")
    if old_path:
        new_path = path + ":" + old_path
    else:
        new_path = path
    os.environ["PATH"] = new_path

    yield

    os.environ["PATH"] = old_path

@contextmanager
def fake_path(path=None, keep_utils=None):
    keep_utils = keep_utils or []
    created_utils = set()
    if path:
        for util in keep_utils:
            util_path = GLib.find_program_in_path(util)
            if util_path:
                os.symlink(util_path, os.path.join(path, util))
                created_utils.add(util)
    old_path = os.environ.get("PATH", "")
    os.environ["PATH"] = path or ""

    yield

    os.environ["PATH"] = old_path
    for util in created_utils:
        os.unlink(os.path.join(path, util))
<commit_msg>Make sure we always set things back in context managers<commit_after>import os
import tempfile
from contextlib import contextmanager

from gi.repository import GLib

def create_sparse_tempfile(name, size):
    """ Create a temporary sparse file.

        :param str name: suffix for filename
        :param size: the file size (in bytes)
        :returns: the path to the newly created file
    """
    (fd, path) = tempfile.mkstemp(prefix="libblockdev.", suffix="-%s" % name)
    os.close(fd)
    create_sparse_file(path, size)
    return path

def create_sparse_file(path, size):
    """ Create a sparse file.

        :param str path: the full path to the file
        :param size: the size of the file (in bytes)
        :returns: None
    """
    fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
    os.ftruncate(fd, size)
    os.close(fd)

@contextmanager
def udev_settle():
    try:
        yield
    finally:
        os.system("udevadm settle")

@contextmanager
def fake_utils(path="."):
    old_path = os.environ.get("PATH", "")
    if old_path:
        new_path = path + ":" + old_path
    else:
        new_path = path
    os.environ["PATH"] = new_path

    try:
        yield
    finally:
        os.environ["PATH"] = old_path

@contextmanager
def fake_path(path=None, keep_utils=None):
    keep_utils = keep_utils or []
    created_utils = set()
    if path:
        for util in keep_utils:
            util_path = GLib.find_program_in_path(util)
            if util_path:
                os.symlink(util_path, os.path.join(path, util))
                created_utils.add(util)
    old_path = os.environ.get("PATH", "")
    os.environ["PATH"] = path or ""

    try:
        yield
    finally:
        os.environ["PATH"] = old_path
        for util in created_utils:
            os.unlink(os.path.join(path, util))
<|endoftext|>


<commit_before>#For future compatibility with Python 3
from __future__ import division, print_function, unicode_literals, absolute_import
import warnings
warnings.simplefilter('default',DeprecationWarning)

import sys

def find_distribution1D():
  """ find the crow distribution1D module and return it. """
  if sys.version_info.major > 2:
    import crow_modules.distribution1Dpy3
    return crow_modules.distribution1Dpy3
  else:
    import crow_modules.distribution1Dpy2
    return crow_modules.distribution1Dpy2

def find_interpolationND():
  """ find the crow interpolationND module and return it. """
  if sys.version_info.major > 2:
    import crow_modules.interpolationNDpy3
    return crow_modules.interpolationNDpy3
  else:
    import crow_modules.interpolationNDpy2
    return crow_modules.interpolationNDpy2

def checkAnswer(comment,value,expected,results,tol=1e-10):
  if abs(value - expected) > tol:
    print("checking answer",comment,value,"!=",expected)
    results["fail"] += 1
  else:
    results["pass"] += 1
<commit_msg>Adding a docstring-style comment to the checkAnswer function.<commit_after>#For future compatibility with Python 3
from __future__ import division, print_function, unicode_literals, absolute_import
import warnings
warnings.simplefilter('default',DeprecationWarning)

import sys

def find_distribution1D():
  """ find the crow distribution1D module and return it. """
  if sys.version_info.major > 2:
    import crow_modules.distribution1Dpy3
    return crow_modules.distribution1Dpy3
  else:
    import crow_modules.distribution1Dpy2
    return crow_modules.distribution1Dpy2

def find_interpolationND():
  """ find the crow interpolationND module and return it. """
  if sys.version_info.major > 2:
    import crow_modules.interpolationNDpy3
    return crow_modules.interpolationNDpy3
  else:
    import crow_modules.interpolationNDpy2
    return crow_modules.interpolationNDpy2

def checkAnswer(comment,value,expected,results,tol=1e-10):
  """ Will check if a test passes or fails and update the results dictionary. """
  if abs(value - expected) > tol:
    print("checking answer",comment,value,"!=",expected)
    results["fail"] += 1
  else:
    results["pass"] += 1<|endoftext|>


<commit_before>import os
import sys
import copy
import unittest
import tempfile
from contextlib import contextmanager

import six
import numpy as np
import pandas as pd
import theano.tensor as T
from matplotlib import pyplot as plt
from matplotlib.testing.compare import compare_images

from neupy import algorithms, layers

from data import xor_input_train, xor_target_train

@contextmanager
def catch_stdout():
    """
    Context manager that catch stdout and return StringIO instance.
    It's good to test any type of terminal outputs.
    """
    old_out = sys.stdout
    out = six.StringIO()
    sys.stdout = out

    yield out

    sys.stdout = old_out

def compare_networks(default_class, tested_class, data, **kwargs):
    """
    Compare two network arcitectures.

    Parameters
    ----------
    default_class : BaseNetwork instance
    tested_class : BaseNetwork instance
    data : tuple
    **kwargs :

    Raises
    ------
    AssertionError
        Raise exception when first network have better prediction
        accuracy then the second one.
    """
    epochs = kwargs.pop('epochs', 100)
    show_comparison_plot = kwargs.pop('show_comparison_plot', False)

    # Compute result for default network (which must be slower)
    network = default_class(**kwargs)

    default_connections = layers.join(copy.deepcopy(network.layers))

    network.train(*data, epochs=epochs)

    network_default_error = network.errors.last()
    errors1 = network.errors

    # Compute result for test network (which must be faster)
    kwargs['connection'] = default_connections
    network = tested_class(**kwargs)

    network.train(*data, epochs=epochs)
    network_tested_error = network.errors.last()
    errors2 = network.errors

    if show_comparison_plot:
        error_range = np.arange(max(len(errors1), len(errors2)))
        plt.plot(error_range[:len(errors1)], errors1)
        plt.plot(error_range[:len(errors2)], errors2)
        plt.show()

    if network_default_error <= network_tested_error:
        raise AssertionError("First network has smaller error ({}) that the "
                             "second one ({}).".format(network_default_error,
                                                       network_tested_error))

def format_image_name(name):
    """
    Add to image name prefix that identify Python versions.

    Parameters
    ----------
    name : str

    Returns
    -------
    str
    """
    name_prefix = 'test_py3_' if six.PY3 else 'test_py2_'
    return name_prefix + name

@contextmanager
def image_comparison(original_image_path, figsize=(10, 10), tol=1e-3):
    """
    Context manager that initialize figure that should contain figure
    that should be compared with expected one.

    Parameters
    ----------
    original_image_path : str
        Path to original image that will use for comparison.
    figsize : tuple
        Figure size. Defaults to ``(10, 10)``.
    tol : float
        Comparison tolerance. Defaults to ``1e-3``.

    Raises
    ------
    AssertionError
        Exception would be trigger in case when generated images and
        original one are different.
    """
    currentdir = os.path.abspath(os.path.dirname(__file__))
    original_image_path = os.path.join(currentdir, original_image_path)

    with tempfile.NamedTemporaryFile(suffix='.png') as f:
        figure = plt.figure(figsize=figsize)
        yield figure
        figure.savefig(f.name)
        error = compare_images(f.name, original_image_path, tol=tol)

        if error:
            raise AssertionError("Image comparison failed. \n"
                                 "Information: {}".format(error))

class StepOutput(layers.BaseLayer):
    def output(self, value):
        if not self.training_state:
            return T.switch(value < 0, -1, 1)
        return value

def reproducible_network_train(seed=0, epochs=500, **additional_params):
    """
    Make a reproducible train for Gradient Descent based neural
    network with a XOR problem and return trained network.

    Parameters
    ----------
    seed : int
        Random State seed number for reproducibility. Defaults to ``0``.
    epochs : int
        Number of epochs for training. Defaults to ``500``.
    **additional_params
        Aditional parameters for Neural Network.

    Returns
    -------
    GradientDescent instance
        Returns trained network.
    """
    np.random.seed(seed)
    network = algorithms.GradientDescent(
        connection=[
            layers.Input(2),
            layers.Tanh(5),
            layers.Tanh(1),
            StepOutput(),
        ],
        **additional_params
    )
    network.train(xor_input_train, xor_target_train, epochs=epochs)
    return network

def vectors_for_testing(vector, is_feature1d=True):
    """
    Function generate different possible variations of one vector.
    That feature useful for testing algorithms input data.

    Parameters
    ----------
    vector : ndarray
        Vector that would be transformed in different data types.
    is_feature1d : bool
        Parameter explain the vector type. Parameter equal to ``True`` mean
        that input data a banch of samples that contains one feature each.
        Defaults to ``True``.

    Raises
    ------
    ValueError
        If input is not a vector

    Returns
    -------
    list
        List that contains the same vectors in different data types like
        numpy 2D vector or pandas Data Frame
    """

    if vector.ndim != 1 and min(vector.shape) != 1:
        raise ValueError("Input should be a vector")

    shape2d = (vector.size, 1) if is_feature1d else (1, vector.size)

    vectors_list = []
    if vector.ndim == 1:
        vectors_list.extend([vector, pd.Series(vector)])

    vectors_list.extend([vector.reshape(shape2d),
                         pd.DataFrame(vector.reshape(shape2d))])

    return vectors_list

def skip_image_comparison_if_specified(func):
    """
    Decorator identifies tests that involve image comparison.
    Before run test function check if environemnt variable
    `SKIP_PLOT_TEST` exists and has non-empty value. If it exists,
    step will be skipped.

    Parameters
    ----------
    func : function
        Any function that you need to override.

    Returns
    -------
    function
        Overrided function.
    """
    decorator = unittest.skipIf(
        os.environ.get('SKIP_PLOT_TEST', None),
        "skip tests that involve image comparison"
    )
    return decorator(func)
<commit_msg>Added example.<commit_after>import os
import sys
import copy
import unittest
import tempfile
from contextlib import contextmanager

import six
import numpy as np
import pandas as pd
import theano.tensor as T
from matplotlib import pyplot as plt
from matplotlib.testing.compare import compare_images

from neupy import algorithms, layers

from data import xor_input_train, xor_target_train

@contextmanager
def catch_stdout():
    """
    Context manager that catches output in terminal and returns
    StringIO instance.

    Yields
    ------
    StringIO instance

    Examples
    --------
    >>> with catch_stdout() as out:
    ...     print("Unittest")
    ...     terminal_output = out.getvalue()
    ...     self.assertIn("test", terminal_output)
    """
    old_out = sys.stdout
    out = six.StringIO()
    sys.stdout = out

    yield out

    sys.stdout = old_out

def compare_networks(default_class, tested_class, data, **kwargs):
    """
    Compare two network arcitectures.

    Parameters
    ----------
    default_class : BaseNetwork instance
    tested_class : BaseNetwork instance
    data : tuple
    **kwargs :

    Raises
    ------
    AssertionError
        Raise exception when first network have better prediction
        accuracy then the second one.
    """
    epochs = kwargs.pop('epochs', 100)
    show_comparison_plot = kwargs.pop('show_comparison_plot', False)

    # Compute result for default network (which must be slower)
    network = default_class(**kwargs)

    default_connections = layers.join(copy.deepcopy(network.layers))

    network.train(*data, epochs=epochs)

    network_default_error = network.errors.last()
    errors1 = network.errors

    # Compute result for test network (which must be faster)
    kwargs['connection'] = default_connections
    network = tested_class(**kwargs)

    network.train(*data, epochs=epochs)
    network_tested_error = network.errors.last()
    errors2 = network.errors

    if show_comparison_plot:
        error_range = np.arange(max(len(errors1), len(errors2)))
        plt.plot(error_range[:len(errors1)], errors1)
        plt.plot(error_range[:len(errors2)], errors2)
        plt.show()

    if network_default_error <= network_tested_error:
        raise AssertionError("First network has smaller error ({}) that the "
                             "second one ({}).".format(network_default_error,
                                                       network_tested_error))

def format_image_name(name):
    """
    Add to image name prefix that identify Python versions.

    Parameters
    ----------
    name : str

    Returns
    -------
    str
    """
    name_prefix = 'test_py3_' if six.PY3 else 'test_py2_'
    return name_prefix + name

@contextmanager
def image_comparison(original_image_path, figsize=(10, 10), tol=1e-3):
    """
    Context manager that initialize figure that should contain figure
    that should be compared with expected one.

    Parameters
    ----------
    original_image_path : str
        Path to original image that will use for comparison.
    figsize : tuple
        Figure size. Defaults to ``(10, 10)``.
    tol : float
        Comparison tolerance. Defaults to ``1e-3``.

    Raises
    ------
    AssertionError
        Exception would be trigger in case when generated images and
        original one are different.
    """
    currentdir = os.path.abspath(os.path.dirname(__file__))
    original_image_path = os.path.join(currentdir, original_image_path)

    with tempfile.NamedTemporaryFile(suffix='.png') as f:
        figure = plt.figure(figsize=figsize)
        yield figure
        figure.savefig(f.name)
        error = compare_images(f.name, original_image_path, tol=tol)

        if error:
            raise AssertionError("Image comparison failed. \n"
                                 "Information: {}".format(error))

class StepOutput(layers.BaseLayer):
    def output(self, value):
        if not self.training_state:
            return T.switch(value < 0, -1, 1)
        return value

def reproducible_network_train(seed=0, epochs=500, **additional_params):
    """
    Make a reproducible train for Gradient Descent based neural
    network with a XOR problem and return trained network.

    Parameters
    ----------
    seed : int
        Random State seed number for reproducibility. Defaults to ``0``.
    epochs : int
        Number of epochs for training. Defaults to ``500``.
    **additional_params
        Aditional parameters for Neural Network.

    Returns
    -------
    GradientDescent instance
        Returns trained network.
    """
    np.random.seed(seed)
    network = algorithms.GradientDescent(
        connection=[
            layers.Input(2),
            layers.Tanh(5),
            layers.Tanh(1),
            StepOutput(),
        ],
        **additional_params
    )
    network.train(xor_input_train, xor_target_train, epochs=epochs)
    return network

def vectors_for_testing(vector, is_feature1d=True):
    """
    Function generate different possible variations of one vector.
    That feature useful for testing algorithms input data.

    Parameters
    ----------
    vector : ndarray
        Vector that would be transformed in different data types.
    is_feature1d : bool
        Parameter explain the vector type. Parameter equal to ``True`` mean
        that input data a banch of samples that contains one feature each.
        Defaults to ``True``.

    Raises
    ------
    ValueError
        If input is not a vector

    Returns
    -------
    list
        List that contains the same vectors in different data types like
        numpy 2D vector or pandas Data Frame
    """

    if vector.ndim != 1 and min(vector.shape) != 1:
        raise ValueError("Input should be a vector")

    shape2d = (vector.size, 1) if is_feature1d else (1, vector.size)

    vectors_list = []
    if vector.ndim == 1:
        vectors_list.extend([vector, pd.Series(vector)])

    vectors_list.extend([vector.reshape(shape2d),
                         pd.DataFrame(vector.reshape(shape2d))])

    return vectors_list

def skip_image_comparison_if_specified(func):
    """
    Decorator identifies tests that involve image comparison.
    Before run test function check if environemnt variable
    `SKIP_PLOT_TEST` exists and has non-empty value. If it exists,
    step will be skipped.

    Parameters
    ----------
    func : function
        Any function that you need to override.

    Returns
    -------
    function
        Overrided function.
    """
    decorator = unittest.skipIf(
        os.environ.get('SKIP_PLOT_TEST', None),
        "skip tests that involve image comparison"
    )
    return decorator(func)
<|endoftext|>


<commit_before>'''
@author: shylent
'''

class TFTPError(Exception):
    pass

class WireProtocolError(TFTPError):
    pass

class InvalidOpcodeError(WireProtocolError):

    def __init__(self, opcode):

        super(InvalidOpcodeError, self).__init__("Invalid opcode: %s" % opcode)

class PayloadDecodeError(WireProtocolError):
    pass

class InvalidErrorcodeError(PayloadDecodeError):

    def __init__(self, errorcode):
        self.errorcode = errorcode
        super(InvalidErrorcodeError, self).__init__("Unknown error code: %s" % errorcode)

class BackendError(TFTPError):
    pass

class Unsupported(BackendError):
    pass

class AccessViolation(BackendError):
    pass

class FileNotFound(BackendError):

    def __init__(self, file_path):
        self.file_path = file_path

    def __str__(self):
        return "File not found: %s" % self.file_path

class FileExists(BackendError):

    def __init__(self, file_path):
        self.file_path = file_path

    def __str__(self):
        return "File already exists: %s" % self.file_path
<commit_msg>Docstrings for exceptions<commit_after>'''
@author: shylent
'''

class TFTPError(Exception):
    """Base exception class for this package"""

class WireProtocolError(TFTPError):
    """Base exception class for wire-protocol level errors"""

class InvalidOpcodeError(WireProtocolError):
    """An invalid opcode was encountered"""

    def __init__(self, opcode):

        super(InvalidOpcodeError, self).__init__("Invalid opcode: %s" % opcode)

class PayloadDecodeError(WireProtocolError):
    """Failed to parse the payload"""

class InvalidErrorcodeError(PayloadDecodeError):
    """An ERROR datagram has an error code, that does not correspond to any known
    error code values.

    @ivar errorcode: The error code, that we were unable to parse
    @type errorcode: C{int}

    """

    def __init__(self, errorcode):
        self.errorcode = errorcode
        super(InvalidErrorcodeError, self).__init__("Unknown error code: %s" % errorcode)

class BackendError(TFTPError):
    """Base exception class for backend errors"""

class Unsupported(BackendError):
    """Requested operation (read/write) is not supported"""

class AccessViolation(BackendError):
    """Illegal filesystem operation. Corresponds to the "(2) Access violation"
    TFTP error code.

    One of the prime examples of these is an attempt at directory traversal.

    """

class FileNotFound(BackendError):
    """File not found.

    Corresponds to the "(1) File not found" TFTP error code.

    @ivar file_path: Path to the file, that was requested
    @type file_path: C{str} or L{twisted.python.filepath.FilePath}

    """

    def __init__(self, file_path):
        self.file_path = file_path

    def __str__(self):
        return "File not found: %s" % self.file_path

class FileExists(BackendError):
    """File exists.

    Corresponds to the "(6) File already exists" TFTP error code.

    @ivar file_path: Path to file
    @type file_path: C{str} or L{twisted.python.filepath.FilePath}

    """

    def __init__(self, file_path):
        self.file_path = file_path

    def __str__(self):
        return "File already exists: %s" % self.file_path
<|endoftext|>


<commit_before>from collections import defaultdict, Sequence, Sized, Iterable
import inspect
import wrapt
from cytoolz import curry
from numpy import ndarray

from .exceptions import UndefinedOperatorError, DifferentLengthError
from .exceptions import ExpectedTypeError, ShapeMismatchError
from .exceptions import ConstraintError

def equal_length(*args):
    '''Check that a tuple of arguments has the same length.
    '''
    for i, arg in enumerate(args):
        if not isinstance(arg, Sized):
            raise ExpectedTypeError(arg, ['Sized'])
        if i >= 1 and len(arg) != len(args[0]):
            raise DifferentLengthError(args, arg_tuple, arg)

@curry
def has_shape(shape, arg_id, args, kwargs):
    '''Check that a particular argument is an array with a given shape. The
    shape may contain string attributes, which will be fetched from arg0 to
    the function (usually self).
    '''
    self = args[0]
    arg = args[arg_id]
    if not hasattr(arg, 'shape'):
        raise ExpectedTypeError(arg, ['array'])
    shape_values = []
    for dim in shape:
        if not isinstance(dim, int):
            dim = getattr(self, dim, None)
        shape_values.append(dim)
    if len(shape) != len(arg.shape):
        raise ShapeMismatchError(arg.shape, tuple(shape_values), shape)
    for i, dim in enumerate(shape_values):
        # Allow underspecified dimensions
        if dim != None and arg.shape[i] != dim:
            raise ShapeMismatchError(arg.shape, shape_values, shape)

def is_sequence(arg_id, args, kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])

def is_float(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, float):
        raise ExpectedTypeError(arg, ['float'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_shape(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])
    for value in arg:
        if not isinstance(value, int) or value < 0:
            raise ExpectedTypeError(arg, ['valid shape (positive ints)'])

def is_int(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, int):
        raise ExpectedTypeError(arg, ['int'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray):
        raise ExpectedTypeError(arg, ['ndarray'])

def is_int_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray) and 'i' not in arg.dtype:
        raise ExpectedTypeError(arg, ['ndarray[int]'])

def operator_is_defined(op):
    @wrapt.decorator
    def checker(wrapped, instance, args, kwargs):
        if instance is None:
            instance = args[0]
        if instance is None:
            raise ExpectedTypeError(instance, ['Model'])
        if op not in instance._operators:
            raise UndefinedOperatorError(op, instance, args[0], instance._operators)
        else:
            return wrapped(*args, **kwargs)
    return checker

def arg(arg_id, *constraints):
    @wrapt.decorator
    def checked_function(wrapped, instance, args, kwargs):
        if not hasattr(wrapped, 'checks'):
            return wrapped(*args, **kwargs)
        if instance is not None:
            fix_args = [instance] + list(args)
        else:
            fix_args = list(args)
        for arg_id, checks in wrapped.checks.items():
            for check in checks:
                check(arg_id, fix_args, kwargs)
        return wrapped(*args, **kwargs)

    def arg_check_adder(func):
        if hasattr(func, 'checks'):
            func.checks.setdefault(arg_id, []).extend(constraints)
            return func
        else:
            wrapped = checked_function(func)
            wrapped.checks = {arg_id: list(constraints)}
            return wrapped
    return arg_check_adder

def args(*constraints):
    @wrapt.decorator
    def arg_check_adder(wrapped, instance, args, kwargs):
        for check in constraints:
            check(args)
        return wrapped(*args, **kwargs)
    return arg_check_adder
<commit_msg>Fix shape check<commit_after>from collections import defaultdict, Sequence, Sized, Iterable
import inspect
import wrapt
from cytoolz import curry
from numpy import ndarray

from .exceptions import UndefinedOperatorError, DifferentLengthError
from .exceptions import ExpectedTypeError, ShapeMismatchError
from .exceptions import ConstraintError

def equal_length(*args):
    '''Check that a tuple of arguments has the same length.
    '''
    for i, arg in enumerate(args):
        if not isinstance(arg, Sized):
            raise ExpectedTypeError(arg, ['Sized'])
        if i >= 1 and len(arg) != len(args[0]):
            raise DifferentLengthError(args, arg_tuple, arg)

@curry
def has_shape(shape, arg_id, args, kwargs):
    '''Check that a particular argument is an array with a given shape. The
    shape may contain string attributes, which will be fetched from arg0 to
    the function (usually self).
    '''
    self = args[0]
    arg = args[arg_id]
    if not hasattr(arg, 'shape'):
        raise ExpectedTypeError(arg, ['array'])
    shape_values = []
    for dim in shape:
        if not isinstance(dim, int):
            dim = getattr(self, dim, None)
        shape_values.append(dim)
    if len(shape) != len(arg.shape):
        raise ShapeMismatchError(arg.shape, tuple(shape_values), shape)
    for i, dim in enumerate(shape_values):
        # Allow underspecified dimensions
        if dim != None and arg.shape[i] != dim:
            raise ShapeMismatchError(arg.shape, shape_values, shape)

def is_sequence(arg_id, args, kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])

def is_float(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, float):
        raise ExpectedTypeError(arg, ['float'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_shape(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])
    for value in arg:
        if value < 0 or not isinstance(value, int):
            raise ExpectedTypeError(arg, ['valid shape (positive ints)'])

def is_int(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, int):
        raise ExpectedTypeError(arg, ['int'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray):
        raise ExpectedTypeError(arg, ['ndarray'])

def is_int_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray) and 'i' not in arg.dtype:
        raise ExpectedTypeError(arg, ['ndarray[int]'])

def operator_is_defined(op):
    @wrapt.decorator
    def checker(wrapped, instance, args, kwargs):
        if instance is None:
            instance = args[0]
        if instance is None:
            raise ExpectedTypeError(instance, ['Model'])
        if op not in instance._operators:
            raise UndefinedOperatorError(op, instance, args[0], instance._operators)
        else:
            return wrapped(*args, **kwargs)
    return checker

def arg(arg_id, *constraints):
    @wrapt.decorator
    def checked_function(wrapped, instance, args, kwargs):
        if not hasattr(wrapped, 'checks'):
            return wrapped(*args, **kwargs)
        if instance is not None:
            fix_args = [instance] + list(args)
        else:
            fix_args = list(args)
        for arg_id, checks in wrapped.checks.items():
            for check in checks:
                check(arg_id, fix_args, kwargs)
        return wrapped(*args, **kwargs)

    def arg_check_adder(func):
        if hasattr(func, 'checks'):
            func.checks.setdefault(arg_id, []).extend(constraints)
            return func
        else:
            wrapped = checked_function(func)
            wrapped.checks = {arg_id: list(constraints)}
            return wrapped
    return arg_check_adder

def args(*constraints):
    @wrapt.decorator
    def arg_check_adder(wrapped, instance, args, kwargs):
        for check in constraints:
            check(args)
        return wrapped(*args, **kwargs)
    return arg_check_adder
<|endoftext|>


<commit_before>import spacy
import re
from collections import defaultdict

class TileReader:

	def __init__(self):
		self.nlp = spacy.load('en')
		self.doc = None
		self.vocab_tags = []
		self.freqs = {}
		self.freqs_as_probs = {}

	def set_vocab_tags(self, tag_list):
		"""
		Function to set POS tags considered in vocabulary bulding. Typically only lexical
		tags such as NOUN, PROPN, VERB or ADJ will be used

		:param tag_list: List of POS tag strings to include in vocabulary building
		:return: void
		"""
		self.vocab_tags = tag_list

	def read(self, input_file, newline_tokenization=False, input_is_text=False):
		"""
		Reads a text file or string, runs NLP pipeline and collects vocabulary with frequencies

		:param input_file: either a file name, or if input_is_text=True, the text to analyze
		:param input_is_text: boolean, whether the input is a file name (default) or already a text string
		:return: void
		"""
		if input_is_text:
			text = file_name
		else:
			text = open(input_file).read()
			text = re.sub(r'\n+', r'\n', text)

		if newline_tokenization:
			sent_docs = [self.nlp(unicode(sentence)) for sentence in text.decode("utf8").split("\n") if sentence]
			self.sentences = [[tok for tok in sent] for sent in sent_docs]
			self.doc = [tok for sent in self.sentences for tok in sent]
		else:
			self.doc = self.nlp(unicode(text.decode("utf8")))
			self.sentences = list(self.doc.sents)

		self.vocab = set([])
		freqs = defaultdict(int)
		tok_count = 0.0
		for token in self.doc:
			if token.pos_ in self.vocab_tags:
				self.vocab.add(token.lemma_)
			freqs[token.lemma_] += 1
			tok_count += 1
		self.vocab = list(self.vocab)
		self.freqs = freqs
		freqs_as_probs = {}
		for item in freqs:
			freqs_as_probs[item] = float(freqs[item])/len(self.doc)
		self.freqs_as_probs = freqs_as_probs

	def get_blocks(self, k, as_text=False):
		"""
		Generator function returning 2-place tuple with lists of sentences before and after a split window
		of size 2*k
		:param k: Half the window size, i.e. the number of sentences to return on either side of the split
		:param as_text: Return each sentence as text if True, else as list of tokens
		:return: ([A1,A2,..Ak],[B1,B2,..Bk]) - the tuple of two blocks listing sentences before and after split
		"""
		if 2*k > len(self.sentences):
			raise IndexError("Window k="+str(k)+" too large for text.\n \
			Expected > 2*" + str(k) +" sentences but only " + str(len(self.sentences)) + " found in text")

		for index, sent in enumerate(self.sentences[:len(self.sentences)-k]):
			if index+k+k <= len(self.sentences):
				if as_text:
					yield (self.sentences[index:index+k],self.sentences[index+k:index+k*2])
				else:
					yield (list((sent) for sent in self.sentences[index:index + k]), list((sent) for sent in self.sentences[index + k:index+k*2]))

	def get_freqs(self, as_probability=False):
		"""
		Get a dictionary of lemma frequencies in the entire document.
		Useful for TF based weighting approaches.

		:param: as_probability: boolean, whether to return dictionary of counts or fractions of document length
		:return: Dictionary from lemmas in entire document to absolute fr
		"""
		if as_probability:
			return self.freqs_as_probs
		else:
			return self.freqs

def demo():
	reader = TileReader()
	reader.set_vocab_tags(["NOUN","PROPN"])
	reader.read(r"c:\\uni\\teaching\\compdisc\\python\\compdisc.git\\GUM_voyage_coron_noheads.txt")

	blocks = reader.get_blocks(3,True)

	for blockA, blockB in blocks:
		print str(blockA) + "\n----\n" + str(blockB) +"\n\n"
		#print blockA[0][0]

if __name__ == "__main__":
	demo()<commit_msg>Fixes unresolved ref in input_is_text option of TileReader.read()<commit_after>import spacy
import re
from collections import defaultdict

class TileReader:

	def __init__(self):
		self.nlp = spacy.load('en')
		self.doc = None
		self.vocab_tags = []
		self.freqs = {}
		self.freqs_as_probs = {}

	def set_vocab_tags(self, tag_list):
		"""
		Function to set POS tags considered in vocabulary bulding. Typically only lexical
		tags such as NOUN, PROPN, VERB or ADJ will be used

		:param tag_list: List of POS tag strings to include in vocabulary building
		:return: void
		"""
		self.vocab_tags = tag_list

	def read(self, input_file, newline_tokenization=False, input_is_text=False):
		"""
		Reads a text file or string, runs NLP pipeline and collects vocabulary with frequencies

		:param input_file: either a file name, or if input_is_text=True, the text to analyze
		:param input_is_text: boolean, whether the input is a file name (default) or already a text string
		:return: void
		"""
		if input_is_text:
			text = input_file
		else:
			text = open(input_file).read()
			text = re.sub(r'\n+', r'\n', text)

		if newline_tokenization:
			sent_docs = [self.nlp(unicode(sentence)) for sentence in text.decode("utf8").split("\n") if sentence]
			self.sentences = [[tok for tok in sent] for sent in sent_docs]
			self.doc = [tok for sent in self.sentences for tok in sent]
		else:
			self.doc = self.nlp(unicode(text.decode("utf8")))
			self.sentences = list(self.doc.sents)

		self.vocab = set([])
		freqs = defaultdict(int)
		tok_count = 0.0
		for token in self.doc:
			if token.pos_ in self.vocab_tags:
				self.vocab.add(token.lemma_)
			freqs[token.lemma_] += 1
			tok_count += 1
		self.vocab = list(self.vocab)
		self.freqs = freqs
		freqs_as_probs = {}
		for item in freqs:
			freqs_as_probs[item] = float(freqs[item])/len(self.doc)
		self.freqs_as_probs = freqs_as_probs

	def get_blocks(self, k, as_text=False):
		"""
		Generator function returning 2-place tuple with lists of sentences before and after a split window
		of size 2*k
		:param k: Half the window size, i.e. the number of sentences to return on either side of the split
		:param as_text: Return each sentence as text if True, else as list of tokens
		:return: ([A1,A2,..Ak],[B1,B2,..Bk]) - the tuple of two blocks listing sentences before and after split
		"""
		if 2*k > len(self.sentences):
			raise IndexError("Window k="+str(k)+" too large for text.\n \
			Expected > 2*" + str(k) +" sentences but only " + str(len(self.sentences)) + " found in text")

		for index, sent in enumerate(self.sentences[:len(self.sentences)-k]):
			if index+k+k <= len(self.sentences):
				if as_text:
					yield (self.sentences[index:index+k],self.sentences[index+k:index+k*2])
				else:
					yield (list((sent) for sent in self.sentences[index:index + k]), list((sent) for sent in self.sentences[index + k:index+k*2]))

	def get_freqs(self, as_probability=False):
		"""
		Get a dictionary of lemma frequencies in the entire document.
		Useful for TF based weighting approaches.

		:param: as_probability: boolean, whether to return dictionary of counts or fractions of document length
		:return: Dictionary from lemmas in entire document to absolute fr
		"""
		if as_probability:
			return self.freqs_as_probs
		else:
			return self.freqs

def demo():
	reader = TileReader()
	reader.set_vocab_tags(["NOUN","PROPN"])
	reader.read(r"c:\\uni\\teaching\\compdisc\\python\\compdisc.git\\GUM_voyage_coron_noheads.txt")

	blocks = reader.get_blocks(3,True)

	for blockA, blockB in blocks:
		print str(blockA) + "\n----\n" + str(blockB) +"\n\n"
		#print blockA[0][0]

if __name__ == "__main__":
	demo()<|endoftext|>


<commit_before>import twiggy.Emitter

import tempfile
import os

fname = tempfile.mktemp()
logfile = open(fname, 'w')

def output(msg, s):
    logfile.write(s)

twiggy.emitters['*'] = twiggy.Emitter.StandardEmitter(twiggy.Levels.DEBUG)

twiggy.emitters['*'].output = output

import timeit

loops = 100000

t = min(timeit.repeat( number = loops,
stmt="""log.debug('hello, ladies')""",
setup="""
import twiggy
log=twiggy.log.name('donjuan')
"""))
os.remove(fname)

print "{0:.3f} sec for {1:n} loops".format(t, loops)<commit_msg>fix timer for new emitter<commit_after>import twiggy.Emitter

import tempfile
import os

fname = tempfile.mktemp()
logfile = open(fname, 'w')

def output(msg, s):
    logfile.write(s)

my_emitter = twiggy.Emitter.Emitter2(twiggy.Levels.DEBUG, True,
                                     twiggy.Emitter.LineFormatter().format,
                                     logfile.write)

twiggy.emitters['*'] = my_emitter

import timeit

loops = 100000

t = min(timeit.repeat( number = loops,
stmt="""log.debug('hello, ladies')""",
setup="""
import twiggy
log=twiggy.log.name('donjuan')
"""))
os.remove(fname)

print "{0:.3f} sec for {1:n} loops".format(t, loops)<|endoftext|>


<commit_before>
from shutil import copy
from os import listdir, stat, makedirs
from os.path import join, isdir, isfile, exists, split

class TimeMachine(object):
	"""
	Access an Apple Timemachine and extract files and directories.
	NOTE: There can be issues accessing some content because your
	user doesn't have access because of the uid and gid from the
	timemachine partition.
	"""
	def __init__(self, mount_path, machine=None, date=None, partition=None):
		"""
		Provide a path to the mount point of your timemachine drive on the
		local machine.
		You can also optionally provide a machine in the timemachine to use
		and a date of that machine to use.
		At least a machine will have to be set eventually
		or we can't access paths to read or extract.

		mount_path: The path to where the external
					timemachine hard drive is mounted.
		machine: The name of the backed up computer in the timemachine.
					Tip: You'll find it at:
					MOUNT_PATH/Backups.backupdb/MACHINE
		date: The date of the backup in the timemachine you want to extract
					from
					Tip: You'll find it at:
					MOUNT_PATH/Backups.backupdb/MACHINE/DATE
		partition: The partition on the machine you want to access.
					Tip: You'll find it in the date directory:
					MOUNT_PATH/Backups.backupdb/MACHINE/DATE/PARTITION

		Only the mount point needs to be provided here.
		You can use the get_[machine|date|partition] methods to help you
		set the other attributes after instantiation of the class instance.
		"""
		# We must at least be given a path to the mounted timemachine
		self.mount_path = mount_path
		# We don't attempt to guess the machine name, it must be set.
		self.machine = None
		# Some sensible defaults but can optionally be provided
		self.date = 'Latest'
		self.partition = 'Macintosh HD'

		# Get all the machine options
		self.get_machines()
		if machine:
			self.set_machine(machine)
		if date:
			self.set_date(date)
		if partition:
			self.set_partition(partition)

	def get_machines(self):
		"""
		Get a list of all machines in the timemachine.
		"""
		self.machines = []
		for machine in listdir(join(self.mount_path, 'Backups.backupdb')):
			if machine.startswith('.'):
				continue
			self.machines.append(machine)
		return self.machines

	def get_dates(self):
		"""
		Get a list of all backup dates in the machine.
		"""
		self.dates = []
		for date in listdir(join(self.mount_path, 'Backups.backupdb',
		                    	 self.machine)):
			self.dates.append(date)
		return self.dates

	def get_partitions(self):
		"""
		Get a list of all partitions under a backup date.
		"""
		self.partitions = []
		for partition in listdir(join(self.mount_path, 'Backups.backupdb',
		                    	 self.machine, self.date)):
			self.partitions.append(partition)
		return self.partitions

	def set_machine(self, machine):
		"""
		Set what machine in the timemachine you want to use.
		"""
		if isinstance(machine, int):
			self.machine = self.machines[machine]
		else:
			self.machine = machine
		self.get_dates()

	def set_date(self, date):
		"""
		Set what backup date in the machine you want to use.
		"""
		if not self.machine:
			raise Exception('You must set the machine before setting the date')
		if isinstance(date, int):
			self.date = self.dates[date]
		else:
			self.date = date
		self.get_partitions()

	def set_partition(self, partition):
		"""
		Set what partition in the backup date you want to use.
		"""
		if not self.date:
			raise Exception('You must set the date before '\
			                'setting the partition')
		if isinstance(partition, int):
			self.partition = self.partitions[partition]
		else:
			self.partition = partition

	def get_real_path(self, path):
		"""
		Given a path that would have worked on the backed up machine,
		return it's real path in the timemachine's folder structure.
		"""
		if path.startswith('/'):
			path = path[1:]
		if path and path.endswith('/'):
			path = path[:-1]
		real_path = join(self.mount_path, 'Backups.backupdb', self.machine,
		                 self.date, self.partition, path)
		removed = []
		# Get what part of the provided path actually exists
		while not exists(real_path):
			real_path, head = split(real_path)
			removed.append(head)
		# Now work through the parts of the path that didn't exist
		# Replacing each step with it's actual
		# path in the dir_####### directories
		while removed:
			item = removed.pop()
			if isdir(real_path):
				real_path = join(real_path, item)
			elif isfile(real_path):
				stats = stat(real_path)
				size = stats.st_size
				link = stats.st_nlink
				if size or link <= 3:
					if not removed:
						return real_path
					raise Exception('Found a non direction component to the '\
					                'path that was of a size greater than 0,'\
					                ' so it\'s not a valid link.')
				real_path = join(self.mount_path,
				                 ".HFS+ Private Directory Data\x0D",
				                 "dir_%s" % link, item)
			else:
				raise Exception('Need to handle non file and directory nodes.')
			# Once we have finished we should go around again in case
			# the last item appended to the list is also a link
			if not removed and isfile(real_path):
				removed.append('')
		return real_path

	def copy_path(self, path, dest_path):
		"""
		Given a path that would have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		if isfile(real_path):
			self.copy_file(path, dest_path)
		elif isdir(real_path):
			self.copy_directory(path, dest_path)

	def copy_file(self, path, dest_path):
		"""
		Given a path to a file that would have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		# Copy real path to the destination
		copy(real_path, dest_path)

	def copy_directory(self, path, dest_path):
		"""
		Given a path to a directory that would
		have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		if path.endswith('/'):
			path = path[:-1]
		dest_path = join(dest_path, split(path)[1])
		# Create the directory at the destination
		if not exists(dest_path):
			makedirs(dest_path)
		# Copy each item in the real path into the new destination
		# that we just created
		for item in listdir(real_path):
			new_item_path = join(path, item)
			self.copy_path(new_item_path, dest_path)

if __name__ == "__main__":
	tm = TimeMachine('/media/My Passport/', u'David\u2019s MacBook Air')
	tm.copy_path('/Users/sligodave/Desktop', '/tmp/')
<commit_msg>Comment to highlight current limitation.<commit_after>
from shutil import copy
from os import listdir, stat, makedirs
from os.path import join, isdir, isfile, exists, split

class TimeMachine(object):
	"""
	Access an Apple Timemachine and extract files and directories.
	NOTE: There can be issues accessing some content because your
	user doesn't have access because of the uid and gid from the
	timemachine partition.
	This is still a work in progress, so I'm working on that both to
	bring it to your attention and also work around it.
	It may come down to some smart sudo-ing.
	For now, directories affected by this will just show up as size 0 files.
	"""
	def __init__(self, mount_path, machine=None, date=None, partition=None):
		"""
		Provide a path to the mount point of your timemachine drive on the
		local machine.
		You can also optionally provide a machine in the timemachine to use
		and a date of that machine to use.
		At least a machine will have to be set eventually
		or we can't access paths to read or extract.

		mount_path: The path to where the external
					timemachine hard drive is mounted.
		machine: The name of the backed up computer in the timemachine.
					Tip: You'll find it at:
					MOUNT_PATH/Backups.backupdb/MACHINE
		date: The date of the backup in the timemachine you want to extract
					from
					Tip: You'll find it at:
					MOUNT_PATH/Backups.backupdb/MACHINE/DATE
		partition: The partition on the machine you want to access.
					Tip: You'll find it in the date directory:
					MOUNT_PATH/Backups.backupdb/MACHINE/DATE/PARTITION

		Only the mount point needs to be provided here.
		You can use the get_[machine|date|partition] methods to help you
		set the other attributes after instantiation of the class instance.
		"""
		# We must at least be given a path to the mounted timemachine
		self.mount_path = mount_path
		# We don't attempt to guess the machine name, it must be set.
		self.machine = None
		# Some sensible defaults but can optionally be provided
		self.date = 'Latest'
		self.partition = 'Macintosh HD'

		# Get all the machine options
		self.get_machines()
		if machine:
			self.set_machine(machine)
		if date:
			self.set_date(date)
		if partition:
			self.set_partition(partition)

	def get_machines(self):
		"""
		Get a list of all machines in the timemachine.
		"""
		self.machines = []
		for machine in listdir(join(self.mount_path, 'Backups.backupdb')):
			if machine.startswith('.'):
				continue
			self.machines.append(machine)
		return self.machines

	def get_dates(self):
		"""
		Get a list of all backup dates in the machine.
		"""
		self.dates = []
		for date in listdir(join(self.mount_path, 'Backups.backupdb',
		                    	 self.machine)):
			self.dates.append(date)
		return self.dates

	def get_partitions(self):
		"""
		Get a list of all partitions under a backup date.
		"""
		self.partitions = []
		for partition in listdir(join(self.mount_path, 'Backups.backupdb',
		                    	 self.machine, self.date)):
			self.partitions.append(partition)
		return self.partitions

	def set_machine(self, machine):
		"""
		Set what machine in the timemachine you want to use.
		"""
		if isinstance(machine, int):
			self.machine = self.machines[machine]
		else:
			self.machine = machine
		self.get_dates()

	def set_date(self, date):
		"""
		Set what backup date in the machine you want to use.
		"""
		if not self.machine:
			raise Exception('You must set the machine before setting the date')
		if isinstance(date, int):
			self.date = self.dates[date]
		else:
			self.date = date
		self.get_partitions()

	def set_partition(self, partition):
		"""
		Set what partition in the backup date you want to use.
		"""
		if not self.date:
			raise Exception('You must set the date before '\
			                'setting the partition')
		if isinstance(partition, int):
			self.partition = self.partitions[partition]
		else:
			self.partition = partition

	def get_real_path(self, path):
		"""
		Given a path that would have worked on the backed up machine,
		return it's real path in the timemachine's folder structure.
		"""
		if path.startswith('/'):
			path = path[1:]
		if path and path.endswith('/'):
			path = path[:-1]
		real_path = join(self.mount_path, 'Backups.backupdb', self.machine,
		                 self.date, self.partition, path)
		removed = []
		# Get what part of the provided path actually exists
		while not exists(real_path):
			real_path, head = split(real_path)
			removed.append(head)
		# Now work through the parts of the path that didn't exist
		# Replacing each step with it's actual
		# path in the dir_####### directories
		while removed:
			item = removed.pop()
			if isdir(real_path):
				real_path = join(real_path, item)
			elif isfile(real_path):
				stats = stat(real_path)
				size = stats.st_size
				link = stats.st_nlink
				if size or link <= 3:
					if not removed:
						return real_path
					raise Exception('Found a non direction component to the '\
					                'path that was of a size greater than 0,'\
					                ' so it\'s not a valid link.')
				real_path = join(self.mount_path,
				                 ".HFS+ Private Directory Data\x0D",
				                 "dir_%s" % link, item)
			else:
				raise Exception('Need to handle non file and directory nodes.')
			# Once we have finished we should go around again in case
			# the last item appended to the list is also a link
			if not removed and isfile(real_path):
				removed.append('')
		return real_path

	def copy_path(self, path, dest_path):
		"""
		Given a path that would have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		if isfile(real_path):
			self.copy_file(path, dest_path)
		elif isdir(real_path):
			self.copy_directory(path, dest_path)

	def copy_file(self, path, dest_path):
		"""
		Given a path to a file that would have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		# Copy real path to the destination
		copy(real_path, dest_path)

	def copy_directory(self, path, dest_path):
		"""
		Given a path to a directory that would
		have worked on the backed up machine,
		copy the path out of the timemachine to a specified location.
		"""
		real_path = self.get_real_path(path)
		if path.endswith('/'):
			path = path[:-1]
		dest_path = join(dest_path, split(path)[1])
		# Create the directory at the destination
		if not exists(dest_path):
			makedirs(dest_path)
		# Copy each item in the real path into the new destination
		# that we just created
		for item in listdir(real_path):
			new_item_path = join(path, item)
			self.copy_path(new_item_path, dest_path)

if __name__ == "__main__":
	tm = TimeMachine('/media/My Passport/', u'David\u2019s MacBook Air')
	tm.copy_path('/Users/sligodave/Desktop', '/tmp/')
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import operator
import math
import functools
from collections import defaultdict
import sys
import re

def extract(s):
    return [int(x) for x in re.findall(r'-?\d+', s)]

def rotate(g):
    xs = []
    for i in range(len(g)):
        s = ''.join(x[i] for x in g)
        xs.append(s)

    return tuple(reversed(xs))

def flip(g):
    return tuple(reversed(g))

def ledge(g):
    return ''.join(x[0] for x in g)

def redge(g):
    return ''.join(x[-1] for x in g)

def edges(g):
    return [g[0], g[-1], ledge(g), redge(g)]

def all_edges(g):
    es = edges(g)
    return es + [''.join(reversed(x)) for x in es]

def moves(g):
    for _ in range(4):
        yield g
        yield flip(g)
        g = rotate(g)

MONSTER = """\
                  #
#    ##    ##    ###
 #  #  #  #  #  #   """.split('\n')

def main(args):
    data = [x.strip().split('\n') for x in sys.stdin.read().strip().split('\n\n')]

    # Parse
    tiles = {}
    for tile in data:
        n = extract(tile[0])[0]
        tiles[n] = flip(tile[1:])

    N = int(math.sqrt(len(tiles)))

    # Build map from possible edges to the tiles that use them
    edge_map = defaultdict(list)
    for n, tile in tiles.items():
        for e in all_edges(tile):
            edge_map[e].append(n)

    # Find the corners: they are the tiles that only have two edges
    # that are shared with other tiles.
    corners = []
    for n, tile in tiles.items():
        cnt = 0
        for e in edges(tile):
            cnt += len(edge_map[e]) - 1
        if cnt == 2:
            corners.append(n)

    # Pick a corner to be the top-left, and try rotations until the
    # unmatched edges are facing up and left.
    corner_n = corners[0]
    ltile = tiles[corner_n]
    while len(edge_map[ledge(ltile)]) == 2:
        ltile = rotate(ltile)

    # Place the tiles one at a time. Since they are uniquely matched
    # up, there's always one unique option for each spot. When going
    # across rows, we pick the one that lines up to the left, and in
    # the first column pick the one that matches at the top.
    def pick(ln, edge, get_edge):
        """Find a tile and rotation of it that matches with ln/ledge.

        get_edge picks the edge of the new tile to compare against.
        """
        me = [x for x in edge_map[edge] if x != ln][0]
        mtile = tiles[me]
        for mtile in moves(mtile):
            if edge == get_edge(mtile):
                break
        return me, mtile

    mgrid = [[None] * N for _ in range(N)]
    mgrid[0][0] = corner_n, ltile
    for y in range(0, N):
        if y > 0:
            ln, lgrid = mgrid[y-1][0]
            mgrid[y][0] = pick(ln, lgrid[-1], get_edge=lambda g: g[0])

        for x in range(1, N):
            ln, lgrid = mgrid[y][x-1]
            mgrid[y][x] = pick(ln, redge(lgrid), get_edge=ledge)

    # Strip the borders off of each tile
    ngrid = [[None] * N for _ in range(N)]
    for i, row in enumerate(mgrid):
        for j, (_, col) in enumerate(row):
            ngrid[i][j] = [x[1:-1] for x in col[1:-1]]

    # Build one big image
    pic = []
    for row in ngrid:
        for i in range(len(row[0])):
            pic += [''.join(col[i] for col in row)]

    # Look for monsters
    for pic in moves(pic):
        cnt = 0
        for y in range(len(pic)-len(MONSTER)):
            for x in range(len(pic)-len(MONSTER[0])):
                # Check if there's a monster starting at this position
                match = True
                for y0 in range(len(MONSTER)):
                    for x0 in range(len(MONSTER[y0])):
                        if MONSTER[y0][x0] == '#' and pic[y+y0][x+x0] != '#':
                            match = False
                            break
                    if not match:
                        break

                if match:
                    cnt += 1

        if cnt:
            break

    # Wooooooo
    part1 = functools.reduce(operator.mul, corners)
    print(part1)

    everything = ''.join(pic).count('#')
    monster = ''.join(MONSTER).count('#')

    print(everything - monster*cnt)

if __name__ == '__main__':
    sys.exit(main(sys.argv))
<commit_msg>Make positioning of the first corner work less by accident<commit_after>#!/usr/bin/env python3

import operator
import math
import functools
from collections import defaultdict
import sys
import re

def extract(s):
    return [int(x) for x in re.findall(r'-?\d+', s)]

def rotate(g):
    xs = []
    for i in range(len(g)):
        s = ''.join(x[i] for x in g)
        xs.append(s)

    return flip(xs)

def flip(g):
    return tuple(reversed(g))

def ledge(g):
    return ''.join(x[0] for x in g)

def redge(g):
    return ''.join(x[-1] for x in g)

def edges(g):
    return [g[0], g[-1], ledge(g), redge(g)]

def all_edges(g):
    es = edges(g)
    return es + [''.join(reversed(x)) for x in es]

def moves(g):
    for _ in range(4):
        yield g
        yield flip(g)
        g = rotate(g)
    # all the callers should break out of the loop when they find something
    raise AssertionError("move search didn't find anything!")

MONSTER = """\
                  #
#    ##    ##    ###
 #  #  #  #  #  #   """.split('\n')

def main(args):
    data = [x.strip().split('\n') for x in sys.stdin.read().strip().split('\n\n')]

    # Parse
    tiles = {}
    for tile in data:
        n = extract(tile[0])[0]
        tiles[n] = tile[1:]

    N = int(math.sqrt(len(tiles)))

    # Build map from possible edges to the tiles that use them
    edge_map = defaultdict(list)
    for n, tile in tiles.items():
        for e in all_edges(tile):
            edge_map[e].append(n)

    # Find the corners: they are the tiles that only have two edges
    # that are shared with other tiles.
    corners = []
    for n, tile in tiles.items():
        cnt = 0
        for e in edges(tile):
            cnt += len(edge_map[e]) - 1
        if cnt == 2:
            corners.append(n)

    # Pick a corner to be the top-left, and try rotations until the
    # unmatched edges are facing up and left.
    corner_n = corners[0]
    ltile = tiles[corner_n]
    while not (len(edge_map[ledge(ltile)]) == len(edge_map[ltile[0]]) == 1):
        ltile = rotate(ltile)

    # Place the tiles one at a time. Since they are uniquely matched
    # up, there's always one unique option for each spot. When going
    # across rows, we pick the one that lines up to the left, and in
    # the first column pick the one that matches at the top.
    def pick(ln, edge, get_edge):
        """Find a tile and rotation of it that matches with ln/ledge.

        get_edge picks the edge of the new tile to compare against.
        """
        me = [x for x in edge_map[edge] if x != ln][0]
        mtile = tiles[me]
        for mtile in moves(mtile):
            if edge == get_edge(mtile):
                break
        return me, mtile

    mgrid = [[None] * N for _ in range(N)]
    mgrid[0][0] = corner_n, ltile
    for y in range(0, N):
        if y > 0:
            ln, lgrid = mgrid[y-1][0]
            mgrid[y][0] = pick(ln, lgrid[-1], get_edge=lambda g: g[0])

        for x in range(1, N):
            ln, lgrid = mgrid[y][x-1]
            mgrid[y][x] = pick(ln, redge(lgrid), get_edge=ledge)

    # Strip the borders off of each tile
    ngrid = [[None] * N for _ in range(N)]
    for i, row in enumerate(mgrid):
        for j, (_, col) in enumerate(row):
            ngrid[i][j] = [x[1:-1] for x in col[1:-1]]

    # Build one big image
    pic = []
    for row in ngrid:
        for i in range(len(row[0])):
            pic += [''.join(col[i] for col in row)]

    # Look for monsters
    for pic in moves(pic):
        cnt = 0
        for y in range(len(pic)-len(MONSTER)):
            for x in range(len(pic)-len(MONSTER[0])):
                # Check if there's a monster starting at this position
                match = True
                for y0 in range(len(MONSTER)):
                    for x0 in range(len(MONSTER[y0])):
                        if MONSTER[y0][x0] == '#' and pic[y+y0][x+x0] != '#':
                            match = False
                            break
                    if not match:
                        break

                if match:
                    cnt += 1

        if cnt:
            break

    # Wooooooo
    part1 = functools.reduce(operator.mul, corners)
    print(part1)

    everything = ''.join(pic).count('#')
    monster = ''.join(MONSTER).count('#')

    print(everything - monster*cnt)

if __name__ == '__main__':
    sys.exit(main(sys.argv))
<|endoftext|>


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

#from flask import Flask, Blueprint, render_template

#user = Blueprint("user", __name__, template_folder = "view")

# Global imports
import bcrypt
import datetime
import re
from hashids import Hashids

# Local imports
import utils

class Instance(object):
    """
    The User Class.
    """
    def __init__(self, user_name):
        if _Utils.user_exists(user_name) is False:
            self.k = False
            return None
        self.k = True
        self.udb = utils.Database().user
        self._user_dat = self.udb.find_one({"user_name": user_name})
        self._updates = set()

    @property
    def email(self):
        return self._user_dat['email']

    @email.setter
    def email(self, value):
        if _Utils.validate_email(value) is True:
            self._updates.add('email')
            self._user_dat['email'] = value
        else:
            pass

    @property
    def user_name(self):
        return self._user_dat['email']

    @property
    def session(self):
        return self._user_dat['session'] if 'session' in self._user_dat else {}

    @session.setter
    def session(self, value):
        self._updates.add('session')
        self._user_dat['session'] = value

    @property
    def pswd(self):
        return self._user_dat['pswd']

    @pswd.setter
    def pswd(self, value):
        if _Utils.validate_password(value) is True:
            self._updates.add('pswd')
            self._user_dat['pswd'] = Password.get_hashed_password(value)
        else:
            pass

    @property
    def status(self):
        return self._status if 'status' in self._user_dat else 0

    @status.setter
    def status(self, value):
        if value in [0, 1, 2, 3]:
            self._updates.add('status')
            self._user_dat['status'] = value
        else:
            pass

    def update(self):
        for change in self._updates:
            self.udb.update(
                {'user_name': self._user_dat['user_name']},
                {'$set': {change: self._user_dat[change]}},
                upsert = False,
                multi = False)

    def __del__(self):
        self.update();

class _Utils:
    def user_exists(user_name):
        return utils.Database().user.find_one({"user_name": user_name}) is not None

    def email_exists(email_id):
        return utils.Database().user.find_one({"email": email_id})

    def validate_username(user_name):
        min_len = 3
        max_len = 32
        pattern = r"^(?i)[a-z0-9_-]{%s,%s}$" %(min_len, max_len)
        return bool(re.match(pattern, user_name)) == True

    def validate_password(password):
        return len(password) > 5

    def validate_email(email_id):
        pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
        return bool(re.match(pattern, email_id)) == True

class Manage:
    def add(
        user_name,
        password,
        confirm_password,
        email_id):
        """Adds the User into Database."""
        errors = []

        if _Utils.validate_username(user_name) is False:
            errors.append("BadUserName")

        if _Utils.validate_email(email_id) is False:
            errors.append("BadEmailID")

        if _Utils.validate_password(password) is False:
            errors.append("ShortPassword")

        if _Utils.user_exists(user_name):
            errors.append("UsernameExist")

        if _Utils.email_exists(email_id):
            errors.append("EmailExist")

        if password is not confirm_password:
            errors.append("PasswordNoMatch")

        if len(errors) is not 0:
            return (False, errors)
        else:
            user = {
                'user_name': user_name,
                'pswd': Password.get_hashed_password(password),
                'email': email_id,
                'status': 1,
                'meta': {
                    'added': datetime.datetime.utcnow()
                }
            }

            return utils.Database().user.insert_one(user).inserted_id

    def delete(user_name):
        """Deletes the User from Database."""
        user = Instance(user_name)
        if user.k is True:
            # 0 is a code for Deleted user.
            user.status = 0

class Session:
    def login(self, user_name, pswd):

        pass

    def logout(self, session_id, session_key):
        pass

    def check(self, session_id, session_key):
        pass

    def _create(self, user_name):
        """Creates a new session for the user."""
        pass

    def _verify(self, user_name, session_id, session_key):
        """Checks the session ID, validity, and integrity."""
        pass

    def _delete(self, user_name, session_id = False):
        """Deletes the session ID, and logs a log-out event."""
        pass

    def _keygen(self, entropy):
        pass

class Password:
    def get_hashed_password(plain_text_password):
        # Hash a password for the first time
        #   (Using bcrypt, the salt is saved into the hash itself)
        return bcrypt.hashpw(plain_text_password, bcrypt.gensalt())

    def check_password(plain_text_password, hashed_password):
        # Check hashed password. Using bcrypt, the salt is saved into the hash itself
        return bcrypt.checkpw(plain_text_password, hashed_password)

#print(Manage.add("prashant", "para-xylene", "para-xylene", "me@prshnts.in"))
ps = Instance("prashant")
ps.status = 3<commit_msg>Even more user options<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#.--. .-. ... .... -. - ... .-.-.- .. -.

#from flask import Flask, Blueprint, render_template

#user = Blueprint("user", __name__, template_folder = "view")

# Global imports
import bcrypt
import datetime
import re
import uuid
from hashids import Hashids

# Local imports
import utils

class Instance(object):
    """
    The User Class.
    """
    def __init__(self, user_name):
        if _Utils.user_exists(user_name) is False:
            self.k = False
            return None
        self.k = True
        self.udb = utils.Database().user
        self._user_dat = self.udb.find_one({"user_name": user_name})
        self._updates = set()
        print(Hashids().encode(int(str(self._user_dat['_id']), 16)))

    @property
    def email(self):
        return self._user_dat['email']

    @email.setter
    def email(self, value):
        if _Utils.validate_email(value) is True:
            self._updates.add('email')
            self._user_dat['email'] = value
        else:
            pass

    @property
    def user_name(self):
        return self._user_dat['email']

    @property
    def session(self):
        return self._user_dat['session'] if 'session' in self._user_dat else {}

    @session.setter
    def session(self, value):
        self._updates.add('session')
        self._user_dat['session'] = value

    @property
    def pswd(self):
        return self._user_dat['pswd']

    @pswd.setter
    def pswd(self, value):
        if _Utils.validate_password(value) is True:
            self._updates.add('pswd')
            self._user_dat['pswd'] = Password.get_hashed_password(value)
        else:
            pass

    @property
    def status(self):
        return self._status if 'status' in self._user_dat else 0

    @status.setter
    def status(self, value):
        if value in [0, 1, 2, 3]:
            self._updates.add('status')
            self._user_dat['status'] = value
        else:
            pass

    def update(self):
        for change in self._updates:
            self.udb.update(
                {'user_name': self._user_dat['user_name']},
                {'$set': {change: self._user_dat[change]}},
                upsert = False,
                multi = False)

    def __del__(self):
        self.update();

class _Utils:
    def user_exists(user_name):
        return utils.Database().user.find_one({"user_name": user_name}) is not None

    def email_exists(email_id):
        return utils.Database().user.find_one({"email": email_id})

    def validate_username(user_name):
        min_len = 3
        max_len = 32
        pattern = r"^(?i)[a-z0-9_-]{%s,%s}$" %(min_len, max_len)
        return bool(re.match(pattern, user_name)) == True

    def validate_password(password):
        return len(password) > 5

    def validate_email(email_id):
        pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
        return bool(re.match(pattern, email_id)) == True

class Manage:
    def add(
        user_name,
        password,
        confirm_password,
        email_id):
        """Adds the User into Database."""
        errors = []

        if _Utils.validate_username(user_name) is False:
            errors.append("BadUserName")

        if _Utils.validate_email(email_id) is False:
            errors.append("BadEmailID")

        if _Utils.validate_password(password) is False:
            errors.append("ShortPassword")

        if _Utils.user_exists(user_name):
            errors.append("UsernameExist")

        if _Utils.email_exists(email_id):
            errors.append("EmailExist")

        if password is not confirm_password:
            errors.append("PasswordNoMatch")

        if len(errors) is not 0:
            return (False, errors)
        else:
            user = {
                'user_name': user_name,
                'pswd': Password.get_hashed_password(password),
                'email': email_id,
                'status': 1,
                'meta': {
                    'added': datetime.datetime.utcnow()
                }
            }

            return utils.Database().user.insert_one(user).inserted_id

    def delete(user_name):
        """Deletes the User from Database."""
        user = Instance(user_name)
        if user.k is True:
            # 0 is a code for Deleted user.
            user.status = 0

class Session:
    def login(user_name, pswd):
        user = Instance(user_name)
        if user.k is True:
            if Password.check_password(pswd, user.pswd) is True:
                # create a session
                #session_id =
                pass
            else:
                print("NAY")
        else:
            return (False, False)
        pass

    def logout(self, session_id, session_key):
        pass

    def check(self, session_id, session_key):
        pass

    def _create(self, user_name):
        """Creates a new session for the user."""
        pass

    def _verify(self, user_name, session_id, session_key):
        """Checks the session ID, validity, and integrity."""
        pass

    def _delete(self, user_name, session_id = False):
        """Deletes the session ID, and logs a log-out event."""
        pass

    def _keygen(self, entropy):
        pass

class Password:
    def get_hashed_password(plain_text_password):
        # Hash a password for the first time
        #   (Using bcrypt, the salt is saved into the hash itself)
        return bcrypt.hashpw(plain_text_password, bcrypt.gensalt())

    def check_password(plain_text_password, hashed_password):
        # Check hashed password. Using bcrypt, the salt is saved into the hash itself
        return bcrypt.checkpw(plain_text_password, hashed_password)

#print(Manage.add("prashant", "para-xylene", "para-xylene", "me@prshnts.in"))
Session.login('prashant', "para-xylene")<|endoftext|>


<commit_before>import os
import time
import datetime
try:
    from Notify import Main as NotifyMain
    Notify = NotifyMain()
    Notify.SetMode("C")
except(ImportError):
    print("Unable to import Notify, Aborting.")
    exit()

class Timer:
    def __init__(self, StartHour, EndHour, Name, Pin):
        self.StartHour = StartHour
        self.EndHour = EndHour
        self.Name = Name
        self.Active = False
        self.CurrentHour = 0
        self.TimerLoop()

    def GetNewTime(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentHour = int(self.CurrentHour)

    def CheckIfTimerActive(self):
        if self.CurrentHour >= self.StartHour:
            if self.CurrentHour < self.EndHour:
                self.Active = True
        else:
            self.Active = False

    def IsTimerActive(self):
        if self.Active == True:
            return True
        else:
            return False

    def TimerLoop(self):
        self.GetNewTime()
        self.CheckIfTimerActive()

MainDeviceList = [["EFSS","00:0C:76:4E:1A:D1","192.168.1.25","OFFLINE"],
                  ["SGPS","78:45:C4:04:9D:68","192.168.1.3","OFFLINE"]
                  ]
# [[dev_name, Mac, IP]]
# [["EFSS","XX:XX:XX:XX:XX","XXX.XXX.XXX.XXX","OFFLINE/ONLINE"],...]

class Main:
    def __init__(self):
        self.CurrentHour = 0
        self.CurrentMinute = 0

    def GetNewTimeData(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentMinute = datetime.datetime.today().minute

    def StartAllDevices(self):
        for count in range(0, len(MainDeviceList)):
            os.system("etherwake " + MainDeviceList[count][1])
            print("Packet Sent to: {}".format(MainDeviceList[count][1]))
        self.MainMenu()

    def ScanDevices(self):
        Notify.Info("Scanning Devices...")
        for count in range(0, len(MainDeviceList)):
            response = os.system("ping -c 1" + MainDeviceList[count][2])
            if response == 0:
                MainDeviceList[count][3] == "ONLINE"
            else:
                MainDeviceList[count][3] == "OFFLINE"

    def PrintDeviceStatus(self):
        self.ScanDevices()
        for count in range(0, len(MainDeviceList)):
            print("Device: " + MainDeviceList[count][0])
            if MainDeviceList[count][3] == "ONLINE":
                Notify.Green()
                print("ONLINE")
                Notify.ClearColour()
            elif MainDeviceList[count][3] == "OFFLINE":
                Notify.Red()
                print("OFFLINE")
                Notify.ClearColour()
            print()

    def AutoMode(self):
        pass

    def MainMenu(self):
        os.system("clear")
        self.GetNewTimeData()
        self.ScanDevices()
        print("""
        |----------------------------------|
        | Current Time: {}:{}              |
        |----------------------------------|
        |[1] Start All Devices             |
        |[2] Print Device Status           |
        |[3] Automatic Mode                |
        |[4] Exit                          |
        |----------------------------------|
        """.format(self.CurrentHour, self.CurrentMinute))
        while True:
            try:
                menuchoice = int(input("> "))
                if menuchoice not in [1,2,3,4]:
                    menuchoice = int(input("> "))
                else:
                    break
            except(ValueError):
                menuchoice = int(input("> "))
        if menuchoice == 1:
            self.StartAllDevices()
        elif menuchoice == 2:
            self.PrintDeviceStatus()
        elif menuchoice == 3:
            self.AutoMode()
        elif menuchoice == 4:
            exit()
        self.MainMenu()

Core = Main()
Core.MainMenu()<commit_msg>AutoWake.py Initial Upload<commit_after>import os
import time
import datetime
try:
    from Notify import Main as NotifyMain
    Notify = NotifyMain()
    Notify.SetMode("C")
except(ImportError):
    print("Unable to import Notify, Aborting.")
    exit()

class Timer:
    def __init__(self, StartHour, EndHour, Name, Pin):
        self.StartHour = StartHour
        self.EndHour = EndHour
        self.Name = Name
        self.Active = False
        self.CurrentHour = 0
        self.TimerLoop()

    def GetNewTime(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentHour = int(self.CurrentHour)

    def CheckIfTimerActive(self):
        if self.CurrentHour >= self.StartHour:
            if self.CurrentHour < self.EndHour:
                self.Active = True
        else:
            self.Active = False

    def IsTimerActive(self):
        if self.Active == True:
            return True
        else:
            return False

    def TimerLoop(self):
        self.GetNewTime()
        self.CheckIfTimerActive()

MainDeviceList = [["EFSS","00:0C:76:4E:1A:D1","192.168.1.25","OFFLINE"],
                  ["SGPS","78:45:C4:04:9D:68","192.168.1.3","OFFLINE"]
                  ]
# [[dev_name, Mac, IP]]
# [["EFSS","XX:XX:XX:XX:XX","XXX.XXX.XXX.XXX","OFFLINE/ONLINE"],...]

class Main:
    def __init__(self):
        self.CurrentHour = 0
        self.CurrentMinute = 0

    def GetNewTimeData(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentMinute = datetime.datetime.today().minute

    def StartAllDevices(self):
        for count in range(0, len(MainDeviceList)):
            os.system("etherwake " + MainDeviceList[count][1])
            print("Packet Sent to: {}".format(MainDeviceList[count][1]))
        self.MainMenu()

    def ScanDevices(self):
        Notify.Info("Scanning Devices...")
        for count in range(0, len(MainDeviceList)):
            response = os.system("ping -c 1" + MainDeviceList[count][2])
            if response == 0:
                MainDeviceList[count][3] == "ONLINE"
            else:
                MainDeviceList[count][3] == "OFFLINE"

    def PrintDeviceStatus(self):
        self.ScanDevices()
        for count in range(0, len(MainDeviceList)):
            print("Device: " + MainDeviceList[count][0])
            if MainDeviceList[count][3] == "ONLINE":
                Notify.Green()
                print("ONLINE")
                Notify.ClearColour()
            elif MainDeviceList[count][3] == "OFFLINE":
                Notify.Red()
                print("OFFLINE")
                Notify.ClearColour()
            print()
        input()

    def AutoMode(self):
        pass

    def MainMenu(self):
        os.system("clear")
        self.GetNewTimeData()
        self.ScanDevices()
        print("""
        |----------------------------------|
        | Current Time: {}:{}              |
        |----------------------------------|
        |[1] Start All Devices             |
        |[2] Print Device Status           |
        |[3] Automatic Mode                |
        |[4] Exit                          |
        |----------------------------------|
        """.format(self.CurrentHour, self.CurrentMinute))
        while True:
            try:
                menuchoice = int(input("> "))
                if menuchoice not in [1,2,3,4]:
                    menuchoice = int(input("> "))
                else:
                    break
            except(ValueError):
                menuchoice = int(input("> "))
        if menuchoice == 1:
            self.StartAllDevices()
        elif menuchoice == 2:
            self.PrintDeviceStatus()
        elif menuchoice == 3:
            self.AutoMode()
        elif menuchoice == 4:
            exit()
        self.MainMenu()

Core = Main()
Core.MainMenu()<|endoftext|>


<commit_before>import os
import time
import datetime
import subprocess as sp
try:
    from Notify import Main as NotifyMain
    Notify = NotifyMain()
    Notify.SetMode("C")
except(ImportError):
    print("Unable to import Notify, Aborting.")
    exit()

class Timer:
    def __init__(self, StartHour, EndHour, Name, Pin):
        self.StartHour = StartHour
        self.EndHour = EndHour
        self.Name = Name
        self.Active = False
        self.CurrentHour = 0
        self.TimerLoop()

    def GetNewTime(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentHour = int(self.CurrentHour)

    def CheckIfTimerActive(self):
        if self.CurrentHour >= self.StartHour:
            if self.CurrentHour < self.EndHour:
                self.Active = True
        else:
            self.Active = False

    def IsTimerActive(self):
        if self.Active == True:
            return True
        else:
            return False

    def TimerLoop(self):
        self.GetNewTime()
        self.CheckIfTimerActive()

MainDeviceList = [["EFSS","00:0C:76:4E:1A:D1","192.168.1.25","OFFLINE"],
                  ["SGPS","78:45:C4:04:9D:68","192.168.1.3","OFFLINE"]
                  ]
# [[dev_name, Mac, IP]]
# [["EFSS","XX:XX:XX:XX:XX","XXX.XXX.XXX.XXX","OFFLINE/ONLINE"],...]

class Main:
    def __init__(self):
        self.CurrentHour = ""
        self.CurrentMinute = ""

    def GetNewTimeData(self):
        self.CurrentHour = datetime.datetime.today().hour
        if self.CurrentHour in [1,2,3,4,5,6,7,8,9,0]:
            self.CurrentHour = "0" + str(datetime.datetime.today().hour)
        else:
            self.CurrentHour = str(datetime.datetime.today().hour)
        self.CurrentMinute = datetime.datetime.today().minute
        if self.CurrentMinute in [1,2,3,4,5,6,7,8,9,0]:
            self.CurrentMinute = "0" + str(datetime.datetime.today().minute)
        else:
            self.CurrentMinute = str(datetime.datetime.today().minute)

    def StartAllDevices(self):
        for count in range(0, len(MainDeviceList)):
            os.system("etherwake " + MainDeviceList[count][1])
            print("Packet Sent to: {}".format(MainDeviceList[count][1]))
        input()
        self.MainMenu()

    def ScanDevices(self):
        Notify.Info("Scanning Devices...")
        for count in range(0, len(MainDeviceList)):
            status,result = sp.getstatusoutput("ping -c1 -w2 " + MainDeviceList[count][2])
            if status == 0:
                MainDeviceList[count].insert(3,"ONLINE")
            else:
                MainDeviceList[count].insert(3,"OFFLINE")
        #print(MainDeviceList)

    def PrintDeviceStatus(self):
        self.ScanDevices()
        for count in range(0, len(MainDeviceList)):
            print("Device: " + MainDeviceList[count][0])
            if MainDeviceList[count][3] == "ONLINE":
                Notify.Green()
                print("ONLINE")
                Notify.ClearColour()
            elif MainDeviceList[count][3] == "OFFLINE":
                Notify.Red()
                print("OFFLINE")
                Notify.ClearColour()
            print()
        input()

    def AutoMode(self):
        pass

    def MainMenu(self):
        os.system("clear")
        self.GetNewTimeData()
        self.ScanDevices()
        print("""
        |----------------------------------|
        | Current Time: {}:{}              |
        |----------------------------------|
        |[1] Start All Devices             |
        |[2] Print Device Status           |
        |[3] Automatic Mode                |
        |[4] Exit                          |
        |----------------------------------|
        """.format(self.CurrentHour, self.CurrentMinute))
        while True:
            try:
                menuchoice = int(input("> "))
                if menuchoice not in [1,2,3,4]:
                    menuchoice = int(input("> "))
                else:
                    break
            except(ValueError):
                menuchoice = int(input("> "))
        if menuchoice == 1:
            self.StartAllDevices()
        elif menuchoice == 2:
            self.PrintDeviceStatus()
        elif menuchoice == 3:
            self.AutoMode()
        elif menuchoice == 4:
            exit()
        self.MainMenu()

Core = Main()
Core.MainMenu()<commit_msg>AutoWake.py Initial Upload<commit_after>import os
import time
import datetime
import subprocess as sp
try:
    from Notify import Main as NotifyMain
    Notify = NotifyMain()
    Notify.SetMode("C")
except(ImportError):
    print("Unable to import Notify, Aborting.")
    exit()

class Timer:
    def __init__(self, StartHour, EndHour, Name):
        self.StartHour = StartHour
        self.EndHour = EndHour
        self.Name = Name
        self.Active = False
        self.CurrentHour = 0

    def GetNewTime(self):
        self.CurrentHour = datetime.datetime.today().hour
        self.CurrentHour = int(self.CurrentHour)

    def CheckIfTimerActive(self):
        if self.CurrentHour >= self.StartHour:
            if self.CurrentHour < self.EndHour:
                self.Active = True
        else:
            self.Active = False

    def IsTimerActive(self):
        if self.Active == True:
            return True
        else:
            return False

    def TimerLoop(self):
        self.GetNewTime()
        self.CheckIfTimerActive()

EFSS = Timer(6,23,"EFSS")
SGPS = Timer(6, 23, "SGPS")

MainDeviceList = [["EFSS","00:0C:76:4E:1A:D1","192.168.1.25","OFFLINE"],
                  ["SGPS","78:45:C4:04:9D:68","192.168.1.3","OFFLINE"]
                  ]

class Main:
    def __init__(self):
        self.CurrentHour = ""
        self.CurrentMinute = ""

    def GetNewTimeData(self):
        self.CurrentHour = datetime.datetime.today().hour
        if self.CurrentHour in [1,2,3,4,5,6,7,8,9,0]:
            self.CurrentHour = "0" + str(datetime.datetime.today().hour)
        else:
            self.CurrentHour = str(datetime.datetime.today().hour)
        self.CurrentMinute = datetime.datetime.today().minute
        if self.CurrentMinute in [1,2,3,4,5,6,7,8,9,0]:
            self.CurrentMinute = "0" + str(datetime.datetime.today().minute)
        else:
            self.CurrentMinute = str(datetime.datetime.today().minute)

    def StartAllDevices(self):
        for count in range(0, len(MainDeviceList)):
            os.system("etherwake " + MainDeviceList[count][1])
            print("Packet Sent to: {}".format(MainDeviceList[count][1]))
        input()
        self.MainMenu()

    def ScanDevices(self):
        Notify.Info("Scanning Devices...")
        for count in range(0, len(MainDeviceList)):
            status,result = sp.getstatusoutput("ping -c1 -w2 " + MainDeviceList[count][2])
            if status == 0:
                MainDeviceList[count].insert(3,"ONLINE")
            else:
                MainDeviceList[count].insert(3,"OFFLINE")
        #print(MainDeviceList)

    def PrintDeviceStatus(self):
        self.ScanDevices()
        for count in range(0, len(MainDeviceList)):
            print("Device: " + MainDeviceList[count][0])
            if MainDeviceList[count][3] == "ONLINE":
                Notify.Green()
                print("ONLINE")
                Notify.ClearColour()
            elif MainDeviceList[count][3] == "OFFLINE":
                Notify.Red()
                print("OFFLINE")
                Notify.ClearColour()
            print()
        input()

    def AutoMode(self):
        try:
            while True:
                if EFSS.IsTimerActive() == True:
                    os.system("etherwake " + MainDeviceList[0][1])
                    Notify.Info("Started: {}".format(MainDeviceList[0][0]))
                else:
                    Notify.Info("EFSS Timer Expired.")
                if SGPS.IsTimerActive() == True:
                    os.system("etherwake " + MainDeviceList[1][1])
                    Notify.Info("Started: {}".format(MainDeviceList[1][0]))
                else:
                    Notify.Info("SGPS Timer Expired.")
                time.sleep(10)
        except(KeyboardInterrupt):
            self.MainMenu()

    def MainMenu(self):
        os.system("clear")
        self.GetNewTimeData()
        self.ScanDevices()
        print("""
        |----------------------------------|
        | Current Time: {}:{}              |
        |----------------------------------|
        |[1] Start All Devices             |
        |[2] Print Device Status           |
        |[3] Automatic Mode                |
        |[4] Exit                          |
        |----------------------------------|
        """.format(self.CurrentHour, self.CurrentMinute))
        while True:
            try:
                menuchoice = int(input("> "))
                if menuchoice not in [1,2,3,4]:
                    menuchoice = int(input("> "))
                else:
                    break
            except(ValueError):
                menuchoice = int(input("> "))
        if menuchoice == 1:
            self.StartAllDevices()
        elif menuchoice == 2:
            self.PrintDeviceStatus()
        elif menuchoice == 3:
            self.AutoMode()
        elif menuchoice == 4:
            exit()
        self.MainMenu()

Core = Main()
Core.MainMenu()<|endoftext|>


<commit_before>import fbchat as fbchat
import time

client = fbchat.Client(username, ****** )
cooperMembers = ["Jake Shulman"]

def checkMessages():
	boolean = False
	last_messages = client.getThreadInfo(friend.uid,0)
	last_messages.reverse()  # messages come in reversed order
	print last_messages
	if last_messages[0].strip().lower() == "paying":
		boolean = True
		client.send(friend.uid, """
Thank you for complying.

If your payment is not recieved in 1 hour your Facebook will be disabled
""")
	return boolean

for member in cooperMembers:
	friend = client.getUsers(member)[0]
	client.send(friend.uid, """
This is the Cooper Treasurer Chat Bot:
You are recieving this notification because you have not paid dues.

**If you do not pay Dues in the next 2 minutes your facebook will be disabled**

Reply "Paying" to prevent disabling**
""")

	time.sleep(60)
	if checkMessages == True:
		print "SAFE"
		break
	client.send(friend.uid,  """
**You have 60 seconds to pay
or notify the Cooper Treasurer about the status of your payment.**
""")

	time.sleep(30)
	if checkMessages == True:
		print "SAFE"
		break
	client.send(friend.uid, """
**You have 30 seconds to pay
or notify the Cooper Treasurer about the status of your payment
	""")

	time.sleep(10)
	if checkMessages == True:
		print "SAFE"
		break
	client.send(friend.uid, """RIP""")

	for x in range(100):
		try:
			client.send(friend.uid, "Pay Dues")
			print "Successfully Sent"

		except:
			"Message Failed"
<commit_msg>Delete CooperFB.py<commit_after><|endoftext|>