<commit_before># Copyright 2013,2014 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Dunya
#
# Dunya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free Software
# Foundation (FSF), either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see http://www.gnu.org/licenses/

from dashboard.log import logger
from dashboard import release_importer

import makam
import makam.models
import compmusic

class MakamReleaseImporter(release_importer.ReleaseImporter):
    _ArtistClass = makam.models.Artist
    _ArtistAliasClass = makam.models.ArtistAlias
    _ComposerClass = makam.models.Composer
    _ComposerAliasClass = makam.models.ComposerAlias
    _ReleaseClass = makam.models.Release
    _RecordingClass = makam.models.Recording
    _InstrumentClass = makam.models.Instrument
    _WorkClass = makam.models.Work

    def _link_release_recording(self, release, recording, trackorder, mnum, tnum):
        if not release.recordings.filter(pk=recording.pk).exists():
            makam.models.ReleaseRecording.objects.create(
                release=release, recording=recording, track=trackorder)

    def _join_recording_and_works(self, recording, works):
        # A makam recording can be of many works. Note that because works
        # in musicbrainz are unordered we don't know if this sequence is
        # correct. All of these recordings will need to be manually checked.
        if self.overwrite:
            makam.models.RecordingWork.objects.filter(recording=recording).delete()
        sequence = 1
        for w in works:
            makam.models.RecordingWork.objects.create(work=w, recording=recording, sequence=sequence)
            sequence += 1

    def _get_makam(self, makamname):
        try:
            return makam.models.Makam.objects.unaccent_get(makamname)
        except makam.models.Makam.DoesNotExist:
            try:
                malias = makam.models.MakamAlias.objects.unaccent_get(makamname)
                return malias.makam
            except makam.models.MakamAlias.DoesNotExist:
                return None

    def _get_usul(self, usul):
        try:
            return makam.models.Usul.objects.unaccent_get(usul)
        except makam.models.Usul.DoesNotExist:
            try:
                ualias = makam.models.UsulAlias.objects.unaccent_get(usul)
                return ualias.usul
            except makam.models.UsulAlias.DoesNotExist:
                return None

    def _get_form(self, form):
        try:
            return makam.models.Form.objects.unaccent_get(form)
        except makam.models.Form.DoesNotExist:
            try:
                falias = makam.models.FormAlias.objects.unaccent_get(form)
                return falias.form
            except makam.models.FormAlias.DoesNotExist:
                return None

    def _get_makam_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_makam(name):
                parsed = compmusic.tags.parse_makam(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _get_usul_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_usul(name):
                parsed = compmusic.tags.parse_usul(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _get_form_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_makam_form(name):
                parsed = compmusic.tags.parse_makam_form(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _apply_tags(self, recording, works, tags):
        # If the form is Taksim and usul Serbest, this is an instrumental
        # improvisation. If form is Gazel, it is a vocal improvisation.
        # These don't have a work. Instead set recording.has_{taksim,gazel}
        # In this case, add the makam tag to the recording, because we
        # don't have a work for it.

        # TODO:
        # Double-check the works in musicbrainz to see if they have tags and
        # if so add their tags to the works.
        # We do have some taksims in musicbrainz as works. Eventually we should
        # remove them.

        makams = self._get_makam_tags(tags)
        forms = self._get_form_tags(tags)
        usuls = self._get_usul_tags(tags)

        groups = compmusic.tags.group_makam_tags(makams, forms, usuls)

        # Tags for taksim
        taksimt = [g for g in groups if g.get("form") == "taksim"]
        gazelt = [g for g in groups if g.get("form") == "gazel"]

        for t in taksimt:
            recording.has_taksim = True
            makam = self._get_makam(t.get("makam"))
            if makam:
                recording.makam.add(makam)
                recording.save()
        for t in gazelt:
            recording.has_gazel = True
            makam = self._get_makam(t.get("makam"))
            if makam:
                recording.makam.add(makam)
                recording.save()

        # tags for no taksim
        notaksimt = [g for g in groups if g.get("form") != "taksim" and g.get("form") != "gazel"]

        # If a recording has two works, it will almost always have two of each
        # of the makam/form/usul tags. However, if one of the form tags is
        # Taksim it may only have one work (since the taksim is an improvisation)
        if len(works) == 1 and len(notaksimt) == 1:
            # If we have a work from two different recordings and the tags are
            # different, we'll add them anyway, but we need to check this because
            # it might be bad data.
            t = notaksimt[0]
            makam = self._get_makam(t.get("makam"))
            form = self._get_form(t.get("form"))
            usul = self._get_usul(t.get("usul"))
            w = works[0]
            if makam and len(w.makam) == 0:
                w.makam.add(makam)
            if usul and len(w.usul) == 0:
                w.usul.add(usul)
            if form and len(w.form) == 0:
                w.form.add(form)
        elif len(works) == len(notaksimt):
            pass
            # If a recording has two works and two sets of tags, we don't know which
            # tags go to which work, because works in musicbrainz are unordered.
            # In this case, flag them for manual interaction. TODO: How?
            # There are 2 possibilities:
            # If we have 2 works, a and b, and this recording x has both,
            # but y has only a and z has only b, then later in the import
            # this will be fixed
            # But, if work a only has this recording x, and z has b, we could
            # import z->b, and then other tags for x->a would work.

    def _get_instrument(self, instname):
        if not instname:
            return None
        try:
            return makam.models.Instrument.objects.alias_get(instname)
        except makam.models.Instrument.DoesNotExist:
            return None

    def _add_recording_performance(self, recordingid, artistid, instrument, is_lead):
        logger.info("  Adding recording performance...")
        artist = self.add_and_get_artist(artistid)
        instrument = self._get_instrument(instrument)
        if instrument:
            recording = makam.models.Recording.objects.get(mbid=recordingid)
            perf = makam.models.InstrumentPerformance(recording=recording, instrument=instrument, artist=artist, lead=is_lead)
            perf.save()

    def _add_release_performance(self, releaseid, artistid, instrument, is_lead):
        logger.info("  Adding release performance...")
        artist = self.add_and_get_artist(artistid)
        instrument = self._get_instrument(instrument)
        if instrument:
            release = makam.models.Release.objects.get(mbid=releaseid)
            # For each recording in the release, see if the relationship
            # already exists. If not, create it.
            for rec in release.recordings.all():
                if not makam.models.InstrumentPerformance.objects.filter(
                   recording=rec, instrument=instrument, artist=artist).exists():
                    perf = makam.models.InstrumentPerformance(recording=rec, instrument=instrument, artist=artist, lead=is_lead)
                    perf.save()

    def _clear_work_composers(self, work):
        if self.overwrite:
            work.composers.clear()
            work.lyricists.clear()

    def _add_work_attributes(self, work, mbwork, created):
        """ Read mb attributes from the webservice query
        and add them to the object """

        if created or self.overwrite:
            form_attr = self._get_form_mb(mbwork)
            usul_attr = self._get_usul_mb(mbwork)
            makam_attr = self._get_makam_mb(mbwork)
            if form_attr != "taksim" and form_attr != "gazel":
                form = self._get_form(form_attr)
                usul = self._get_usul(usul_attr)
                makam = self._get_makam(makam_attr)
                if form:
                    work.form.clear()
                    work.form.add(form)
                if usul:
                    work.usul.clear()
                    work.usul.add(usul)
                if makam:
                    work.makam.clear()
                    work.makam.add(makam)
                work.save()

    def _get_form_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Form (Ottoman, Turkish)':
                return a['attribute']
        return None

    def _get_usul_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Usul (Ottoman, Turkish)':
                return a['attribute']
        return None

    def _get_makam_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Makam (Ottoman, Turkish)':
                return a['attribute']
        return None

<commit_msg>Check length of makam/form/usul correctly<commit_after># Copyright 2013,2014 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Dunya
#
# Dunya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free Software
# Foundation (FSF), either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see http://www.gnu.org/licenses/

from dashboard.log import logger
from dashboard import release_importer

import makam
import makam.models
import compmusic

class MakamReleaseImporter(release_importer.ReleaseImporter):
    _ArtistClass = makam.models.Artist
    _ArtistAliasClass = makam.models.ArtistAlias
    _ComposerClass = makam.models.Composer
    _ComposerAliasClass = makam.models.ComposerAlias
    _ReleaseClass = makam.models.Release
    _RecordingClass = makam.models.Recording
    _InstrumentClass = makam.models.Instrument
    _WorkClass = makam.models.Work

    def _link_release_recording(self, release, recording, trackorder, mnum, tnum):
        if not release.recordings.filter(pk=recording.pk).exists():
            makam.models.ReleaseRecording.objects.create(
                release=release, recording=recording, track=trackorder)

    def _join_recording_and_works(self, recording, works):
        # A makam recording can be of many works. Note that because works
        # in musicbrainz are unordered we don't know if this sequence is
        # correct. All of these recordings will need to be manually checked.
        if self.overwrite:
            makam.models.RecordingWork.objects.filter(recording=recording).delete()
        sequence = 1
        for w in works:
            makam.models.RecordingWork.objects.create(work=w, recording=recording, sequence=sequence)
            sequence += 1

    def _get_makam(self, makamname):
        try:
            return makam.models.Makam.objects.unaccent_get(makamname)
        except makam.models.Makam.DoesNotExist:
            try:
                malias = makam.models.MakamAlias.objects.unaccent_get(makamname)
                return malias.makam
            except makam.models.MakamAlias.DoesNotExist:
                return None

    def _get_usul(self, usul):
        try:
            return makam.models.Usul.objects.unaccent_get(usul)
        except makam.models.Usul.DoesNotExist:
            try:
                ualias = makam.models.UsulAlias.objects.unaccent_get(usul)
                return ualias.usul
            except makam.models.UsulAlias.DoesNotExist:
                return None

    def _get_form(self, form):
        try:
            return makam.models.Form.objects.unaccent_get(form)
        except makam.models.Form.DoesNotExist:
            try:
                falias = makam.models.FormAlias.objects.unaccent_get(form)
                return falias.form
            except makam.models.FormAlias.DoesNotExist:
                return None

    def _get_makam_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_makam(name):
                parsed = compmusic.tags.parse_makam(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _get_usul_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_usul(name):
                parsed = compmusic.tags.parse_usul(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _get_form_tags(self, taglist):
        ret = []
        for t in taglist:
            name = t["name"].lower()
            if compmusic.tags.has_makam_form(name):
                parsed = compmusic.tags.parse_makam_form(name)
                if parsed and parsed[1]:
                    ret.append(parsed)
        return ret

    def _apply_tags(self, recording, works, tags):
        # If the form is Taksim and usul Serbest, this is an instrumental
        # improvisation. If form is Gazel, it is a vocal improvisation.
        # These don't have a work. Instead set recording.has_{taksim,gazel}
        # In this case, add the makam tag to the recording, because we
        # don't have a work for it.

        # TODO:
        # Double-check the works in musicbrainz to see if they have tags and
        # if so add their tags to the works.
        # We do have some taksims in musicbrainz as works. Eventually we should
        # remove them.

        makams = self._get_makam_tags(tags)
        forms = self._get_form_tags(tags)
        usuls = self._get_usul_tags(tags)

        groups = compmusic.tags.group_makam_tags(makams, forms, usuls)

        # Tags for taksim
        taksimt = [g for g in groups if g.get("form") == "taksim"]
        gazelt = [g for g in groups if g.get("form") == "gazel"]

        for t in taksimt:
            recording.has_taksim = True
            makam = self._get_makam(t.get("makam"))
            if makam:
                recording.makam.add(makam)
                recording.save()
        for t in gazelt:
            recording.has_gazel = True
            makam = self._get_makam(t.get("makam"))
            if makam:
                recording.makam.add(makam)
                recording.save()

        # tags for no taksim
        notaksimt = [g for g in groups if g.get("form") != "taksim" and g.get("form") != "gazel"]

        # If a recording has two works, it will almost always have two of each
        # of the makam/form/usul tags. However, if one of the form tags is
        # Taksim it may only have one work (since the taksim is an improvisation)
        if len(works) == 1 and len(notaksimt) == 1:
            # If we have a work from two different recordings and the tags are
            # different, we'll add them anyway, but we need to check this because
            # it might be bad data.
            t = notaksimt[0]
            makam = self._get_makam(t.get("makam"))
            form = self._get_form(t.get("form"))
            usul = self._get_usul(t.get("usul"))
            w = works[0]
            if makam and w.makam.count() == 0:
                w.makam.add(makam)
            if usul and w.usul.count() == 0:
                w.usul.add(usul)
            if form and w.form.count() == 0:
                w.form.add(form)
        elif len(works) == len(notaksimt):
            pass
            # If a recording has two works and two sets of tags, we don't know which
            # tags go to which work, because works in musicbrainz are unordered.
            # In this case, flag them for manual interaction. TODO: How?
            # There are 2 possibilities:
            # If we have 2 works, a and b, and this recording x has both,
            # but y has only a and z has only b, then later in the import
            # this will be fixed
            # But, if work a only has this recording x, and z has b, we could
            # import z->b, and then other tags for x->a would work.

    def _get_instrument(self, instname):
        if not instname:
            return None
        try:
            return makam.models.Instrument.objects.alias_get(instname)
        except makam.models.Instrument.DoesNotExist:
            return None

    def _add_recording_performance(self, recordingid, artistid, instrument, is_lead):
        logger.info("  Adding recording performance...")
        artist = self.add_and_get_artist(artistid)
        instrument = self._get_instrument(instrument)
        if instrument:
            recording = makam.models.Recording.objects.get(mbid=recordingid)
            perf = makam.models.InstrumentPerformance(recording=recording, instrument=instrument, artist=artist, lead=is_lead)
            perf.save()

    def _add_release_performance(self, releaseid, artistid, instrument, is_lead):
        logger.info("  Adding release performance...")
        artist = self.add_and_get_artist(artistid)
        instrument = self._get_instrument(instrument)
        if instrument:
            release = makam.models.Release.objects.get(mbid=releaseid)
            # For each recording in the release, see if the relationship
            # already exists. If not, create it.
            for rec in release.recordings.all():
                if not makam.models.InstrumentPerformance.objects.filter(
                   recording=rec, instrument=instrument, artist=artist).exists():
                    perf = makam.models.InstrumentPerformance(recording=rec, instrument=instrument, artist=artist, lead=is_lead)
                    perf.save()

    def _clear_work_composers(self, work):
        if self.overwrite:
            work.composers.clear()
            work.lyricists.clear()

    def _add_work_attributes(self, work, mbwork, created):
        """ Read mb attributes from the webservice query
        and add them to the object """

        if created or self.overwrite:
            work.form.clear()
            work.usul.clear()
            work.makam.add(makam)

        if created or self.overwrite:
            form_attr = self._get_form_mb(mbwork)
            usul_attr = self._get_usul_mb(mbwork)
            makam_attr = self._get_makam_mb(mbwork)
            if form_attr != "taksim" and form_attr != "gazel":
                form = self._get_form(form_attr)
                usul = self._get_usul(usul_attr)
                makam = self._get_makam(makam_attr)
                if form:
                    work.form.add(form)
                if usul:
                    work.usul.add(usul)
                if makam:
                    work.makam.clear()
                work.save()

    def _get_form_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Form (Ottoman, Turkish)':
                return a['attribute']
        return None

    def _get_usul_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Usul (Ottoman, Turkish)':
                return a['attribute']
        return None

    def _get_makam_mb(self, mb_work):
        for a in mb_work.get('attribute-list',[]):
            if a['type'] == u'Makam (Ottoman, Turkish)':
                return a['attribute']
        return None

<|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import copy

from astropy.visualization import astropy_mpl_style

__all__ = ['light_style_sheet', 'dark_style_sheet',
           'available_style_sheets']

available_style_sheets = ["light_style_sheet", "dark_style_sheet"]
"""Matplotlib style sheets available within astroplan."""

light_style_sheet = copy.deepcopy(astropy_mpl_style)

# One update specifically for astroplan.plots: raise bottom up from default 0.1
light_style_sheet.update({'figure.subplot.bottom': 0.15})

# Define dark style sheet starting from the astropy_mpl_style sheet
dark_style_sheet = {

    # Lines
    'lines.linewidth': 1.7,
    'lines.antialiased': True,

    # Patches
    'patch.linewidth': 1.0,
    'patch.facecolor': 'k',
    'patch.edgecolor': 'k',
    'patch.antialiased': True,

    # images
    'image.cmap': 'gist_heat',
    'image.origin': 'upper',

    # Font
    'font.size': 12.0,

    # Axes
    'axes.facecolor': '#000000',
    'axes.edgecolor': '#F2F2F2',
    'axes.linewidth': 1.0,
    'axes.grid': True,
    'axes.titlesize': 'x-large',
    'axes.labelsize': 'large',
    'axes.labelcolor': 'w',
    'axes.axisbelow': True,
    'axes.color_cycle': ['#00CCFF',   # blue
                         '#FF0000',   # red
                         '#40C000',   # green
                         '#E066E0',   # purple
                         '#CF4457',   # pink
                         '#188487',   # turquoise
                         '#E24A33'],  # orange

    # Ticks
    'xtick.major.size': 0,
    'xtick.minor.size': 0,
    'xtick.major.pad': 6,
    'xtick.minor.pad': 6,
    'xtick.color': '#F2F2F2',
    'xtick.direction': 'in',
    'ytick.major.size': 0,
    'ytick.minor.size': 0,
    'ytick.major.pad': 6,
    'ytick.minor.pad': 6,
    'ytick.color': '#F2F2F2',
    'ytick.direction': 'in',

    # Legend
    'legend.fancybox': True,
    'legend.loc': 'best',

    # Figure
    'figure.figsize': [8, 6],
    'figure.facecolor': 'k',
    'figure.edgecolor': 'k',
    'figure.subplot.hspace': 0.5,
    'figure.subplot.bottom': 0.15,

    # Saved figures
    'savefig.dpi': 72,
    'savefig.facecolor': 'k',
    'savefig.edgecolor': 'k',
    'savefig.transparent': False,

    # Other
    'grid.color': 'w',
    'text.color': 'w'
}
<commit_msg>Fix mpl 1.5 color_cycle deprecation warning<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import copy

from astropy.visualization import astropy_mpl_style
from astropy.utils import minversion

# This returns False if matplotlib cannot be imported
MATPLOTLIB_GE_1_5 = minversion('matplotlib', '1.5')

__all__ = ['light_style_sheet', 'dark_style_sheet',
           'available_style_sheets']

available_style_sheets = ["light_style_sheet", "dark_style_sheet"]
"""Matplotlib style sheets available within astroplan."""

light_style_sheet = copy.deepcopy(astropy_mpl_style)

# One update specifically for astroplan.plots: raise bottom up from default 0.1
light_style_sheet.update({'figure.subplot.bottom': 0.15})

# Define dark style sheet starting from the astropy_mpl_style sheet
dark_style_sheet = {

    # Lines
    'lines.linewidth': 1.7,
    'lines.antialiased': True,

    # Patches
    'patch.linewidth': 1.0,
    'patch.facecolor': 'k',
    'patch.edgecolor': 'k',
    'patch.antialiased': True,

    # images
    'image.cmap': 'gist_heat',
    'image.origin': 'upper',

    # Font
    'font.size': 12.0,

    # Axes
    'axes.facecolor': '#000000',
    'axes.edgecolor': '#F2F2F2',
    'axes.linewidth': 1.0,
    'axes.grid': True,
    'axes.titlesize': 'x-large',
    'axes.labelsize': 'large',
    'axes.labelcolor': 'w',
    'axes.axisbelow': True,

    # Ticks
    'xtick.major.size': 0,
    'xtick.minor.size': 0,
    'xtick.major.pad': 6,
    'xtick.minor.pad': 6,
    'xtick.color': '#F2F2F2',
    'xtick.direction': 'in',
    'ytick.major.size': 0,
    'ytick.minor.size': 0,
    'ytick.major.pad': 6,
    'ytick.minor.pad': 6,
    'ytick.color': '#F2F2F2',
    'ytick.direction': 'in',

    # Legend
    'legend.fancybox': True,
    'legend.loc': 'best',

    # Figure
    'figure.figsize': [8, 6],
    'figure.facecolor': 'k',
    'figure.edgecolor': 'k',
    'figure.subplot.hspace': 0.5,
    'figure.subplot.bottom': 0.15,

    # Saved figures
    'savefig.dpi': 72,
    'savefig.facecolor': 'k',
    'savefig.edgecolor': 'k',
    'savefig.transparent': False,

    # Other
    'grid.color': 'w',
    'text.color': 'w'
}

color_cycle = ['#00CCFF',   # blue
               '#FF0000',   # red
               '#40C000',   # green
               '#E066E0',   # purple
               '#CF4457',   # pink
               '#188487',   # turquoise
               '#E24A33']   # orange

if MATPLOTLIB_GE_1_5:
    # This is a dependency of matplotlib, so should be present.
    from cycler import cycler
    dark_style_sheet['axes.prop_cycle'] = cycler('color', color_cycle)

else:
    dark_style_sheet['axes.color_cycle'] = color_cycle
<|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import warnings

from .helper import pytest, catch_warnings
from .. import log
from ..logger import LoggingError

# Save original values of hooks. These are not the system values, but the
# already overwritten values since the logger already gets imported before
# this file gets executed.
_excepthook = sys.__excepthook__
_showwarning = warnings.showwarning

try:
    ip = get_ipython()
except NameError:
    ip = None

def setup_function(function):

    # Reset hooks to original values
    sys.excepthook = _excepthook
    warnings.showwarning = _showwarning

    # Reset internal original hooks
    log._showwarning_orig = None
    log._excepthook_orig = None

    # Set up the logger
    log._set_defaults()

    # Reset hooks
    if log.warnings_logging_enabled():
        log.disable_warnings_logging()
    if log.exception_logging_enabled():
        log.disable_exception_logging()

def teardown_module(function):

    # Ensure that hooks are restored to original values
    sys.excepthook = _excepthook
    warnings.showwarning = _showwarning

def test_warnings_logging_disable_no_enable():
    with pytest.raises(LoggingError) as e:
        log.disable_warnings_logging()
    assert e.value.args[0] == 'Warnings logging has not been enabled'

def test_warnings_logging_enable_twice():
    log.enable_warnings_logging()
    with pytest.raises(LoggingError) as e:
        log.enable_warnings_logging()
    assert e.value.args[0] == 'Warnings logging has already been enabled'

def test_warnings_logging_overridden():
    log.enable_warnings_logging()
    warnings.showwarning = lambda: None
    with pytest.raises(LoggingError) as e:
        log.disable_warnings_logging()
    assert e.value.args[0] == 'Cannot disable warnings logging: warnings.showwarning was not set by this logger, or has been overridden'

def test_warnings_logging():

    # Without warnings logging
    with catch_warnings() as warn_list:
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
    assert len(log_list) == 0
    assert len(warn_list) == 1
    assert warn_list[0].message.args[0] == "This is a warning"

    # With warnings logging
    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == 'This is a warning'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    # Without warnings logging
    with catch_warnings() as warn_list:
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
    assert len(log_list) == 0
    assert len(warn_list) == 1
    assert warn_list[0].message.args[0] == "This is a warning"

def test_warnings_logging_with_custom_class():
    class CustomWarningClass(Warning):
        pass

    # With warnings logging
    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning", CustomWarningClass)
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == 'CustomWarningClass: This is a warning'
    assert log_list[0].origin == 'astropy.tests.test_logger'

def test_warning_logging_with_io_votable_warning():
    from ..io.votable.exceptions import W02, vo_warn

    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            vo_warn(W02, ('a', 'b'))
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == ("W02: ?:?:?: W02: a attribute 'b' is "
                                   "invalid.  Must be a standard XML id")
    assert log_list[0].origin == 'astropy.tests.test_logger'

def test_exception_logging_disable_no_enable():
    with pytest.raises(LoggingError) as e:
        log.disable_exception_logging()
    assert e.value.args[0] == 'Exception logging has not been enabled'

def test_exception_logging_enable_twice():
    log.enable_exception_logging()
    with pytest.raises(LoggingError) as e:
        log.enable_exception_logging()
    assert e.value.args[0] == 'Exception logging has already been enabled'

# You can't really override the exception handler in IPython this way, so
# this test doesn't really make sense in the IPython context.
@pytest.mark.skipif("ip is not None")
def test_exception_logging_overridden():
    log.enable_exception_logging()
    sys.excepthook = lambda: None
    with pytest.raises(LoggingError) as e:
        log.disable_exception_logging()
    assert e.value.args[0] == 'Cannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden'

@pytest.mark.xfail("ip is not None")
def test_exception_logging():

    # Without exception logging
    try:
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 0

    # With exception logging
    try:
        log.enable_exception_logging()
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 1
    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message == 'Exception: This is an Exception'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    # Without exception logging
    log.disable_exception_logging()
    try:
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 0

@pytest.mark.xfail("ip is not None")
def test_exception_logging_origin():
    # The point here is to get an exception raised from another location
    # and make sure the error's origin is reported correctly

    from ..utils.collections import HomogeneousList

    l = HomogeneousList(int)
    try:
        log.enable_exception_logging()
        with log.log_to_list() as log_list:
            l.append('foo')
    except TypeError as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0].startswith(
            "homogeneous list must contain only objects of type ")
    else:
        assert False
    assert len(log_list) == 1
    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message.startswith(
        "TypeError: homogeneous list must contain only objects of type ")
    assert log_list[0].origin == 'astropy.utils.collections'

@pytest.mark.parametrize(('level'), [None, 'DEBUG', 'INFO', 'WARN', 'ERROR'])
def test_log_to_list(level):

    orig_level = log.level

    try:
        if level is not None:
            log.setLevel(level)

        with log.log_to_list() as log_list:
            log.error("Error message")
            log.warning("Warning message")
            log.info("Information message")
            log.debug("Debug message")
    finally:
        log.setLevel(orig_level)

    # Check list length
    if level == 'DEBUG':
        assert len(log_list) == 4
    elif level is None or level == 'INFO':
        assert len(log_list) == 3
    elif level == 'WARN':
        assert len(log_list) == 2
    elif level == 'ERROR':
        assert len(log_list) == 1

    # Check list content

    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message == 'Error message'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 2:
        assert log_list[1].levelname == 'WARNING'
        assert log_list[1].message == 'Warning message'
        assert log_list[1].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 3:
        assert log_list[2].levelname == 'INFO'
        assert log_list[2].msg == 'Information message'
        assert log_list[2].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 4:
        assert log_list[3].levelname == 'DEBUG'
        assert log_list[3].msg == 'Debug message'
        assert log_list[3].origin == 'astropy.tests.test_logger'

def test_log_to_list_level():

    with log.log_to_list(filter_level='ERROR') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 1 and log_list[0].levelname == 'ERROR'

def test_log_to_list_origin1():

    with log.log_to_list(filter_origin='astropy.tests') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 2

def test_log_to_list_origin2():

    with log.log_to_list(filter_origin='astropy.wcs') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 0

@pytest.mark.parametrize(('level'), [None, 'DEBUG', 'INFO', 'WARN', 'ERROR'])
def test_log_to_file(tmpdir, level):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())
    orig_level = log.level

    try:
        if level is not None:
            log.setLevel(level)

        with log.log_to_file(log_path):
            log.error("Error message")
            log.warning("Warning message")
            log.info("Information message")
            log.debug("Debug message")

        log_file.close()
    finally:
        log.setLevel(orig_level)

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    # Check list length
    if level == 'DEBUG':
        assert len(log_entries) == 4
    elif level is None or level == 'INFO':
        assert len(log_entries) == 3
    elif level == 'WARN':
        assert len(log_entries) == 2
    elif level == 'ERROR':
        assert len(log_entries) == 1

    # Check list content

    assert log_entries[0].strip().endswith(b"'astropy.tests.test_logger', 'ERROR', 'Error message'")

    if len(log_entries) >= 2:
        assert log_entries[1].strip().endswith(b"'astropy.tests.test_logger', 'WARNING', 'Warning message'")

    if len(log_entries) >= 3:
        assert log_entries[2].strip().endswith(b"'astropy.tests.test_logger', 'INFO', 'Information message'")

    if len(log_entries) >= 4:
        assert log_entries[3].strip().endswith(b"'astropy.tests.test_logger', 'DEBUG', 'Debug message'")

def test_log_to_file_level(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_level='ERROR'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 1 and log_entries[0].strip().endswith(b"'ERROR', 'Error message'")

def test_log_to_file_origin1(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_origin='astropy.tests'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 2

def test_log_to_file_origin2(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_origin='astropy.wcs'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 0
<commit_msg>Fixes the tests in question by not assuming that the default log level is INFO<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import warnings

from .helper import pytest, catch_warnings
from .. import log
from ..logger import LoggingError, LOG_LEVEL

# Save original values of hooks. These are not the system values, but the
# already overwritten values since the logger already gets imported before
# this file gets executed.
_excepthook = sys.__excepthook__
_showwarning = warnings.showwarning

try:
    ip = get_ipython()
except NameError:
    ip = None

def setup_function(function):

    # Reset hooks to original values
    sys.excepthook = _excepthook
    warnings.showwarning = _showwarning

    # Reset internal original hooks
    log._showwarning_orig = None
    log._excepthook_orig = None

    # Set up the logger
    log._set_defaults()

    # Reset hooks
    if log.warnings_logging_enabled():
        log.disable_warnings_logging()
    if log.exception_logging_enabled():
        log.disable_exception_logging()

def teardown_module(function):

    # Ensure that hooks are restored to original values
    sys.excepthook = _excepthook
    warnings.showwarning = _showwarning

def test_warnings_logging_disable_no_enable():
    with pytest.raises(LoggingError) as e:
        log.disable_warnings_logging()
    assert e.value.args[0] == 'Warnings logging has not been enabled'

def test_warnings_logging_enable_twice():
    log.enable_warnings_logging()
    with pytest.raises(LoggingError) as e:
        log.enable_warnings_logging()
    assert e.value.args[0] == 'Warnings logging has already been enabled'

def test_warnings_logging_overridden():
    log.enable_warnings_logging()
    warnings.showwarning = lambda: None
    with pytest.raises(LoggingError) as e:
        log.disable_warnings_logging()
    assert e.value.args[0] == 'Cannot disable warnings logging: warnings.showwarning was not set by this logger, or has been overridden'

def test_warnings_logging():

    # Without warnings logging
    with catch_warnings() as warn_list:
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
    assert len(log_list) == 0
    assert len(warn_list) == 1
    assert warn_list[0].message.args[0] == "This is a warning"

    # With warnings logging
    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == 'This is a warning'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    # Without warnings logging
    with catch_warnings() as warn_list:
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning")
    assert len(log_list) == 0
    assert len(warn_list) == 1
    assert warn_list[0].message.args[0] == "This is a warning"

def test_warnings_logging_with_custom_class():
    class CustomWarningClass(Warning):
        pass

    # With warnings logging
    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            warnings.warn("This is a warning", CustomWarningClass)
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == 'CustomWarningClass: This is a warning'
    assert log_list[0].origin == 'astropy.tests.test_logger'

def test_warning_logging_with_io_votable_warning():
    from ..io.votable.exceptions import W02, vo_warn

    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            vo_warn(W02, ('a', 'b'))
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    assert log_list[0].message == ("W02: ?:?:?: W02: a attribute 'b' is "
                                   "invalid.  Must be a standard XML id")
    assert log_list[0].origin == 'astropy.tests.test_logger'

def test_exception_logging_disable_no_enable():
    with pytest.raises(LoggingError) as e:
        log.disable_exception_logging()
    assert e.value.args[0] == 'Exception logging has not been enabled'

def test_exception_logging_enable_twice():
    log.enable_exception_logging()
    with pytest.raises(LoggingError) as e:
        log.enable_exception_logging()
    assert e.value.args[0] == 'Exception logging has already been enabled'

# You can't really override the exception handler in IPython this way, so
# this test doesn't really make sense in the IPython context.
@pytest.mark.skipif("ip is not None")
def test_exception_logging_overridden():
    log.enable_exception_logging()
    sys.excepthook = lambda: None
    with pytest.raises(LoggingError) as e:
        log.disable_exception_logging()
    assert e.value.args[0] == 'Cannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden'

@pytest.mark.xfail("ip is not None")
def test_exception_logging():

    # Without exception logging
    try:
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 0

    # With exception logging
    try:
        log.enable_exception_logging()
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 1
    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message == 'Exception: This is an Exception'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    # Without exception logging
    log.disable_exception_logging()
    try:
        with log.log_to_list() as log_list:
            raise Exception("This is an Exception")
    except Exception as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0] == "This is an Exception"
    else:
        assert False  # exception should have been raised
    assert len(log_list) == 0

@pytest.mark.xfail("ip is not None")
def test_exception_logging_origin():
    # The point here is to get an exception raised from another location
    # and make sure the error's origin is reported correctly

    from ..utils.collections import HomogeneousList

    l = HomogeneousList(int)
    try:
        log.enable_exception_logging()
        with log.log_to_list() as log_list:
            l.append('foo')
    except TypeError as exc:
        sys.excepthook(*sys.exc_info())
        assert exc.args[0].startswith(
            "homogeneous list must contain only objects of type ")
    else:
        assert False
    assert len(log_list) == 1
    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message.startswith(
        "TypeError: homogeneous list must contain only objects of type ")
    assert log_list[0].origin == 'astropy.utils.collections'

@pytest.mark.parametrize(('level'), [None, 'DEBUG', 'INFO', 'WARN', 'ERROR'])
def test_log_to_list(level):

    orig_level = log.level

    try:
        if level is not None:
            log.setLevel(level)

        with log.log_to_list() as log_list:
            log.error("Error message")
            log.warning("Warning message")
            log.info("Information message")
            log.debug("Debug message")
    finally:
        log.setLevel(orig_level)

    if level is None:
        # The log level *should* be set to whatever it was in the config
        level = LOG_LEVEL()

    # Check list length
    if level == 'DEBUG':
        assert len(log_list) == 4
    elif level == 'INFO':
        assert len(log_list) == 3
    elif level == 'WARN':
        assert len(log_list) == 2
    elif level == 'ERROR':
        assert len(log_list) == 1

    # Check list content

    assert log_list[0].levelname == 'ERROR'
    assert log_list[0].message == 'Error message'
    assert log_list[0].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 2:
        assert log_list[1].levelname == 'WARNING'
        assert log_list[1].message == 'Warning message'
        assert log_list[1].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 3:
        assert log_list[2].levelname == 'INFO'
        assert log_list[2].msg == 'Information message'
        assert log_list[2].origin == 'astropy.tests.test_logger'

    if len(log_list) >= 4:
        assert log_list[3].levelname == 'DEBUG'
        assert log_list[3].msg == 'Debug message'
        assert log_list[3].origin == 'astropy.tests.test_logger'

def test_log_to_list_level():

    with log.log_to_list(filter_level='ERROR') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 1 and log_list[0].levelname == 'ERROR'

def test_log_to_list_origin1():

    with log.log_to_list(filter_origin='astropy.tests') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 2

def test_log_to_list_origin2():

    with log.log_to_list(filter_origin='astropy.wcs') as log_list:
        log.error("Error message")
        log.warning("Warning message")

    assert len(log_list) == 0

@pytest.mark.parametrize(('level'), [None, 'DEBUG', 'INFO', 'WARN', 'ERROR'])
def test_log_to_file(tmpdir, level):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())
    orig_level = log.level

    try:
        if level is not None:
            log.setLevel(level)

        with log.log_to_file(log_path):
            log.error("Error message")
            log.warning("Warning message")
            log.info("Information message")
            log.debug("Debug message")

        log_file.close()
    finally:
        log.setLevel(orig_level)

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    if level is None:
        # The log level *should* be set to whatever it was in the config
        level = LOG_LEVEL()

    # Check list length
    if level == 'DEBUG':
        assert len(log_entries) == 4
    elif level == 'INFO':
        assert len(log_entries) == 3
    elif level == 'WARN':
        assert len(log_entries) == 2
    elif level == 'ERROR':
        assert len(log_entries) == 1

    # Check list content

    assert log_entries[0].strip().endswith(b"'astropy.tests.test_logger', 'ERROR', 'Error message'")

    if len(log_entries) >= 2:
        assert log_entries[1].strip().endswith(b"'astropy.tests.test_logger', 'WARNING', 'Warning message'")

    if len(log_entries) >= 3:
        assert log_entries[2].strip().endswith(b"'astropy.tests.test_logger', 'INFO', 'Information message'")

    if len(log_entries) >= 4:
        assert log_entries[3].strip().endswith(b"'astropy.tests.test_logger', 'DEBUG', 'Debug message'")

def test_log_to_file_level(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_level='ERROR'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 1 and log_entries[0].strip().endswith(b"'ERROR', 'Error message'")

def test_log_to_file_origin1(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_origin='astropy.tests'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 2

def test_log_to_file_origin2(tmpdir):

    local_path = tmpdir.join('test.log')
    log_file = local_path.open('wb')
    log_path = str(local_path.realpath())

    with log.log_to_file(log_path, filter_origin='astropy.wcs'):
        log.error("Error message")
        log.warning("Warning message")

    log_file.close()

    log_file = local_path.open('rb')
    log_entries = log_file.readlines()
    log_file.close()

    assert len(log_entries) == 0
<|endoftext|>


<commit_before># This file is part of eventmq.
#
# eventmq is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# eventmq is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with eventmq.  If not, see <http://www.gnu.org/licenses/>.
"""
log module for eventmq

this needs so much work.
"""
import logging

import zmq
import zmq.log.handlers

FORMAT_STANDARD = logging.Formatter(
    '%(asctime)s - %(name)s  %(levelname)s - %(message)s')
FORMAT_NAMELESS = logging.Formatter(
    '%(asctime)s - %(levelname)s - %(message)s')

class PUBHandler(zmq.log.handlers.PUBHandler):
    """
    """
    pass

class handlers(object):
    """
    log handlers

    PUBLISH_HANDLER - blast logs through a pub mechanism
    STREAM_LOGGER - logs to stdout/stderr
    """
    PUBLISH_HANDLER = PUBHandler
    STREAM_HANDLER = logging.StreamHandler

def setup_logger(base_name, formatter=FORMAT_STANDARD,
                 handler=handlers.STREAM_HANDLER):

    logger = logging.getLogger(base_name)
    logger.setLevel(logging.DEBUG)

    # remove handlers we don't want
    # for h in logger.handlers:
    #     logger.removeHandler(h)

    if handler == handlers.PUBLISH_HANDLER:
        _handler_sock = zmq.Context.instance().socket(zmq.PUB)
        _handler_sock.bind('tcp://127.0.0.1:33445')

        import time
        time.sleep(1)

        handler = handler(_handler_sock)
        handler.root_topic = base_name
    else:
        handler = handler()

    handler.setFormatter(formatter)

    logger.addHandler(handler)

    return logger
<commit_msg>Update logger to use RFC-3339 timestamps<commit_after># This file is part of eventmq.
#
# eventmq is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# eventmq is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with eventmq.  If not, see <http://www.gnu.org/licenses/>.
"""
log module for eventmq

this needs so much work.
"""
import logging

import zmq
import zmq.log.handlers

FORMAT_STANDARD = logging.Formatter(
    '%(asctime)s - %(name)s  %(levelname)s - %(message)s',
    datefmt='%Y-%m-%dT%H:%M:%S%z')
FORMAT_NAMELESS = logging.Formatter(
    '%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%dT%H:%M:%S%z')

class PUBHandler(zmq.log.handlers.PUBHandler):
    """
    """
    pass

class handlers(object):
    """
    log handlers

    PUBLISH_HANDLER - blast logs through a pub mechanism
    STREAM_LOGGER - logs to stdout/stderr
    """
    PUBLISH_HANDLER = PUBHandler
    STREAM_HANDLER = logging.StreamHandler

def setup_logger(base_name, formatter=FORMAT_STANDARD,
                 handler=handlers.STREAM_HANDLER):

    logger = logging.getLogger(base_name)
    logger.setLevel(logging.DEBUG)

    # remove handlers we don't want
    # for h in logger.handlers:
    #     logger.removeHandler(h)

    if handler == handlers.PUBLISH_HANDLER:
        _handler_sock = zmq.Context.instance().socket(zmq.PUB)
        _handler_sock.bind('tcp://127.0.0.1:33445')

        import time
        time.sleep(1)

        handler = handler(_handler_sock)
        handler.root_topic = base_name
    else:
        handler = handler()

    handler.setFormatter(formatter)

    logger.addHandler(handler)

    return logger
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Generalized Linear Models for large datasets."""
import textwrap

from dask_glm import algorithms, families
from dask_glm.utils import (
    accuracy_score,
    add_intercept,
    dot,
    exp,
    mean_squared_error,
    poisson_deviance,
    sigmoid,
)
from sklearn.base import BaseEstimator

# Register multiple dispatch
from . import utils  # noqa
from ..utils import check_array

_base_doc = textwrap.dedent(
    """\
    Esimator for {regression_type}.

    Parameters
    ----------
    penalty : str or Regularizer, default 'l2'
        Regularizer to use. Only relevant for the 'admm', 'lbfgs' and
        'proximal_grad' solvers.

        For string values, only 'l1' or 'l2' are valid.

    dual : bool
        Ignored

    tol : float, default 1e-4
        The tolerance for convergence.

    C : float
        Regularization strength. Note that ``dask-glm`` solvers use
        the parameterization :math:`\lambda = 1 / C`

    fit_intercept : bool, default True
        Specifies if a constant (a.k.a. bias or intercept) should be
        added to the decision function.

    intercept_scaling : bool
        Ignored

    class_weight : dict or 'balanced'
        Ignored

    random_state : int, RandomState, or None

        The seed of the pseudo random number generator to use when shuffling
        the data. If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by np.random. Used when solver == ‘sag’ or ‘liblinear’.

    solver : {{'admm', 'gradient_descent', 'newton', 'lbfgs', 'proximal_grad'}}
        Solver to use. See :ref:`api.algorithms` for details

    multiclass : str, default 'ovr'
        Ignored. Multiclass solvers not currently supported.

    verbose : int, default 0
        Ignored

    warm_start : bool, default False
        Ignored

    n_jobs : int, default 1
        Ignored

    solver_kwargs : dict, optional, default None
        Extra keyword arguments to pass through to the solver.

    Attributes
    ----------
    coef_ : array, shape (n_classes, n_features)
        The learned value for the model's coefficients
    intercept_ : float of None
        The learned value for the intercept, if one was added
        to the model

    Examples
    --------
    {examples}
    """
)

class _GLM(BaseEstimator):
    @property
    def family(self):
        """
        The family this estimator is for.
        """

    def __init__(
        self,
        penalty="l2",
        dual=False,
        tol=1e-4,
        C=1.0,
        fit_intercept=True,
        intercept_scaling=1.0,
        class_weight=None,
        random_state=None,
        solver="admm",
        multiclass="ovr",
        verbose=0,
        warm_start=False,
        n_jobs=1,
        max_iter=100,
        solver_kwargs=None,
    ):
        self.penalty = penalty
        self.dual = dual
        self.tol = tol
        self.C = C
        self.fit_intercept = fit_intercept
        self.intercept_scaling = intercept_scaling
        self.class_weight = class_weight
        self.random_state = random_state
        self.solver = solver
        self.multiclass = multiclass
        self.verbose = verbose
        self.warm_start = warm_start
        self.n_jobs = n_jobs
        self.max_iter = max_iter
        self.solver_kwargs = solver_kwargs

    def _get_solver_kwargs(self):
        fit_kwargs = {
            "max_iter": self.max_iter,
            "family": self.family,
            "tol": self.tol,
            "regularizer": self.penalty,
            "lamduh": 1 / self.C,
        }

        if self.solver in ("gradient_descent", "newton"):
            fit_kwargs.pop("regularizer")
            fit_kwargs.pop("lamduh")

        if self.solver == "admm":
            fit_kwargs.pop("tol")  # uses reltol / abstol instead

        if self.solver_kwargs:
            fit_kwargs.update(self.solver_kwargs)

        solvers = {
            "admm",
            "proximal_grad",
            "lbfgs",
            "newton",
            "proximal_grad",
            "gradient_descent",
        }

        if self.solver not in solvers:
            msg = "'solver' must be {}. Got '{}' instead".format(solvers, self.solver)
            raise ValueError(msg)

        return fit_kwargs

    def fit(self, X, y=None):
        """Fit the model on the training data

        Parameters
        ----------
        X: array-like, shape (n_samples, n_features)
        y : array-like, shape (n_samples,)

        Returns
        -------
        self : objectj
        """
        X = self._check_array(X)

        solver_kwargs = self._get_solver_kwargs()

        self._coef = algorithms._solvers[self.solver](X, y, **solver_kwargs)
        if self.fit_intercept:
            self.coef_ = self._coef[:-1]
            self.intercept_ = self._coef[-1]
        else:
            self.coef_ = self._coef
        return self

    def _check_array(self, X):
        if self.fit_intercept:
            X = add_intercept(X)

        return check_array(X, accept_unknown_chunks=True)

class LogisticRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="logistic_regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_classification
            >>> X, y = make_classification()
            >>> lr = LogisticRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict_proba(X)
            >>> est.score(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Logistic

    def predict(self, X):
        """Predict class labels for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted class labels for each sample
        """
        return self.predict_proba(X) > 0.5  # TODO: verify, multiclass broken

    def predict_proba(self, X):
        """Probability estimates for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        T : array-like, shape = [n_samples, n_classes]
            The probability of the sample for each class in the model.
        """
        X_ = self._check_array(X)
        return sigmoid(dot(X_, self._coef))

    def score(self, X, y):
        """The mean accuracy on the given data and labels

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Test samples.
        y : array-like, shape = [n_samples,]
            Test labels.

        Returns
        -------
        score : float
            Mean accuracy score
        """
        return accuracy_score(y, self.predict(X))

class LinearRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="linear_regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_regression
            >>> X, y = make_regression()
            >>> lr = LinearRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict(X)
            >>> est.score(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Normal

    def predict(self, X):
        """Predict values for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted value for each sample
        """
        X_ = self._check_array(X)
        return dot(X_, self._coef)

    def score(self, X, y):
        """Returns the coefficient of determination R^2 of the prediction.

        The coefficient R^2 is defined as (1 - u/v), where u is the residual
        sum of squares ((y_true - y_pred) ** 2).sum() and v is the total
        sum of squares ((y_true - y_true.mean()) ** 2).sum().
        The best possible score is 1.0 and it can be negative (because the
        model can be arbitrarily worse). A constant model that always
        predicts the expected value of y, disregarding the input features,
        would get a R^2 score of 0.0.

        Parameters
        ----------
        X : array-like, shape = (n_samples, n_features)
            Test samples.

        y : array-like, shape = (n_samples) or (n_samples, n_outputs)
            True values for X.

        Returns
        -------
        score : float
            R^2 of self.predict(X) wrt. y.
        """
        return mean_squared_error(y, self.predict(X))

class PoissonRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="poisson_regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_counts
            >>> X, y = make_counts()
            >>> lr = PoissonRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict(X)
            >>> lr.get_deviance(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Poisson

    def predict(self, X):
        """Predict count for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted count for each sample
        """
        X_ = self._check_array(X)
        return exp(dot(X_, self._coef))

    def get_deviance(self, X, y):
        return poisson_deviance(y, self.predict(X))
<commit_msg>Minor LogisticRegression updates<commit_after># -*- coding: utf-8 -*-
"""Generalized Linear Models for large datasets."""
import textwrap

from dask_glm import algorithms, families
from dask_glm.utils import (
    accuracy_score,
    add_intercept,
    dot,
    exp,
    mean_squared_error,
    poisson_deviance,
    sigmoid,
)
from sklearn.base import BaseEstimator

# Register multiple dispatch
from . import utils  # noqa
from ..utils import check_array

_base_doc = textwrap.dedent(
    """\
    Esimator for {regression_type}.

    Parameters
    ----------
    penalty : str or Regularizer, default 'l2'
        Regularizer to use. Only relevant for the 'admm', 'lbfgs' and
        'proximal_grad' solvers.

        For string values, only 'l1' or 'l2' are valid.

    dual : bool
        Ignored

    tol : float, default 1e-4
        The tolerance for convergence.

    C : float
        Regularization strength. Note that ``dask-glm`` solvers use
        the parameterization :math:`\lambda = 1 / C`

    fit_intercept : bool, default True
        Specifies if a constant (a.k.a. bias or intercept) should be
        added to the decision function.

    intercept_scaling : bool
        Ignored

    class_weight : dict or 'balanced'
        Ignored

    random_state : int, RandomState, or None

        The seed of the pseudo random number generator to use when shuffling
        the data. If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by np.random. Used when solver == ‘sag’ or ‘liblinear’.

    solver : {{'admm', 'gradient_descent', 'newton', 'lbfgs', 'proximal_grad'}}
        Solver to use. See :ref:`api.algorithms` for details

    max_iter : int, default 100
        Maximum number of iterations taken for the solvers to converge.

    multi_class : str, default 'ovr'
        Ignored. Multiclass solvers not currently supported.

    verbose : int, default 0
        Ignored

    warm_start : bool, default False
        Ignored

    n_jobs : int, default 1
        Ignored

    solver_kwargs : dict, optional, default None
        Extra keyword arguments to pass through to the solver.

    Attributes
    ----------
    coef_ : array, shape (n_classes, n_features)
        The learned value for the model's coefficients

    intercept_ : float of None
        The learned value for the intercept, if one was added
        to the model

    Examples
    --------
    {examples}
    """
)

class _GLM(BaseEstimator):
    @property
    def family(self):
        """
        The family this estimator is for.
        """

    def __init__(
        self,
        penalty="l2",
        dual=False,
        tol=1e-4,
        C=1.0,
        fit_intercept=True,
        intercept_scaling=1.0,
        class_weight=None,
        random_state=None,
        solver="admm",
        max_iter=100,
        multi_class="ovr",
        verbose=0,
        warm_start=False,
        n_jobs=1,
        solver_kwargs=None,
    ):
        self.penalty = penalty
        self.dual = dual
        self.tol = tol
        self.C = C
        self.fit_intercept = fit_intercept
        self.intercept_scaling = intercept_scaling
        self.class_weight = class_weight
        self.random_state = random_state
        self.solver = solver
        self.max_iter = max_iter
        self.multi_class = multi_class
        self.verbose = verbose
        self.warm_start = warm_start
        self.n_jobs = n_jobs
        self.solver_kwargs = solver_kwargs

    def _get_solver_kwargs(self):
        fit_kwargs = {
            "max_iter": self.max_iter,
            "family": self.family,
            "tol": self.tol,
            "regularizer": self.penalty,
            "lamduh": 1 / self.C,
        }

        if self.solver in ("gradient_descent", "newton"):
            fit_kwargs.pop("regularizer")
            fit_kwargs.pop("lamduh")

        if self.solver == "admm":
            fit_kwargs.pop("tol")  # uses reltol / abstol instead

        if self.solver_kwargs:
            fit_kwargs.update(self.solver_kwargs)

        solvers = {
            "admm",
            "proximal_grad",
            "lbfgs",
            "newton",
            "proximal_grad",
            "gradient_descent",
        }

        if self.solver not in solvers:
            msg = "'solver' must be {}. Got '{}' instead".format(solvers, self.solver)
            raise ValueError(msg)

        return fit_kwargs

    def fit(self, X, y=None):
        """Fit the model on the training data

        Parameters
        ----------
        X: array-like, shape (n_samples, n_features)
        y : array-like, shape (n_samples,)

        Returns
        -------
        self : objectj
        """
        X = self._check_array(X)

        solver_kwargs = self._get_solver_kwargs()

        self._coef = algorithms._solvers[self.solver](X, y, **solver_kwargs)
        if self.fit_intercept:
            self.coef_ = self._coef[:-1]
            self.intercept_ = self._coef[-1]
        else:
            self.coef_ = self._coef
        return self

    def _check_array(self, X):
        if self.fit_intercept:
            X = add_intercept(X)

        return check_array(X, accept_unknown_chunks=True)

class LogisticRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="logistic regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_classification
            >>> X, y = make_classification()
            >>> lr = LogisticRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict_proba(X)
            >>> lr.score(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Logistic

    def predict(self, X):
        """Predict class labels for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted class labels for each sample
        """
        return self.predict_proba(X) > 0.5  # TODO: verify, multi_class broken

    def predict_proba(self, X):
        """Probability estimates for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        T : array-like, shape = [n_samples, n_classes]
            The probability of the sample for each class in the model.
        """
        X_ = self._check_array(X)
        return sigmoid(dot(X_, self._coef))

    def score(self, X, y):
        """The mean accuracy on the given data and labels

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Test samples.
        y : array-like, shape = [n_samples,]
            Test labels.

        Returns
        -------
        score : float
            Mean accuracy score
        """
        return accuracy_score(y, self.predict(X))

class LinearRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="linear regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_regression
            >>> X, y = make_regression()
            >>> lr = LinearRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict(X)
            >>> lr.score(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Normal

    def predict(self, X):
        """Predict values for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted value for each sample
        """
        X_ = self._check_array(X)
        return dot(X_, self._coef)

    def score(self, X, y):
        """Returns the coefficient of determination R^2 of the prediction.

        The coefficient R^2 is defined as (1 - u/v), where u is the residual
        sum of squares ((y_true - y_pred) ** 2).sum() and v is the total
        sum of squares ((y_true - y_true.mean()) ** 2).sum().
        The best possible score is 1.0 and it can be negative (because the
        model can be arbitrarily worse). A constant model that always
        predicts the expected value of y, disregarding the input features,
        would get a R^2 score of 0.0.

        Parameters
        ----------
        X : array-like, shape = (n_samples, n_features)
            Test samples.

        y : array-like, shape = (n_samples) or (n_samples, n_outputs)
            True values for X.

        Returns
        -------
        score : float
            R^2 of self.predict(X) wrt. y.
        """
        return mean_squared_error(y, self.predict(X))

class PoissonRegression(_GLM):
    __doc__ = _base_doc.format(
        regression_type="poisson regression",
        examples=textwrap.dedent(
            """
            >>> from dask_glm.datasets import make_counts
            >>> X, y = make_counts()
            >>> lr = PoissonRegression()
            >>> lr.fit(X, y)
            >>> lr.predict(X)
            >>> lr.predict(X)
            >>> lr.get_deviance(X, y)"""
        ),
    )

    @property
    def family(self):
        return families.Poisson

    def predict(self, X):
        """Predict count for samples in X.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples,]
            Predicted count for each sample
        """
        X_ = self._check_array(X)
        return exp(dot(X_, self._coef))

    def get_deviance(self, X, y):
        return poisson_deviance(y, self.predict(X))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
This module defines magnitude zero points and related photometric quantities.
"""

import numpy as _numpy
from .core import UnitBase, def_unit, Unit

from astropy.constants import si as _si
from . import cgs, si, astrophys

_ns = globals()

def_unit(['Bol', 'L_bol'], _si.L_bol0, namespace=_ns, prefixes=False,
         doc="Luminosity corresponding to absolute bolometric magnitude zero")
def_unit(['bol', 'f_bol'], _si.L_bol0 / (4 * _numpy.pi * (10.*astrophys.pc)**2),
         namespace=_ns, prefixes=False, doc="Irradiance corresponding to "
         "appparent bolometric magnitude zero")
def_unit(['AB', 'ABflux'], 10.**(48.6/-2.5) * cgs.erg * cgs.cm**-2 / si.s / si.Hz,
         namespace=_ns, prefixes=False,
         doc="AB magnitude zero flux density.")
def_unit(['ST', 'STflux'], 10.**(21.1/-2.5) * cgs.erg * cgs.cm**-2 / si.s / si.AA,
         namespace=_ns, prefixes=False,
         doc="ST magnitude zero flux density.")

def_unit(['mgy', 'maggy'],
         namespace=_ns, prefixes=[(['n'], ['nano'], 1e-9)],
         doc="Maggies - a linear flux unit that is the flux for a mag=0 object."
             "To tie this onto a specific calibrated unit system, the "
             "zero_point_flux equivalency should be used.")

def zero_point_flux(flux0):
    """
    An equivalency for converting linear flux units ("maggys") defined relative
    to a standard source into a standardized system.

    Parameters
    ----------
    flux0 : u.Quantity
        The flux of a magnitude-0 object in the "maggy" system.
    """
    flux_unit0 = Unit(flux0)
    return [(maggy, flux_unit0)]

###########################################################################
# CLEANUP

del UnitBase
del def_unit
del cgs, si, astrophys

###########################################################################
# DOCSTRING

# This generates a docstring for this module that describes all of the
# standard units defined here.
from .utils import generate_unit_summary as _generate_unit_summary
if __doc__ is not None:
    __doc__ += _generate_unit_summary(globals())
<commit_msg>Refer to corresponding magnitude definitions<commit_after># -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
This module defines magnitude zero points and related photometric quantities.
"""

import numpy as _numpy
from .core import UnitBase, def_unit, Unit

from astropy.constants import si as _si
from . import cgs, si, astrophys

_ns = globals()

def_unit(['Bol', 'L_bol'], _si.L_bol0, namespace=_ns, prefixes=False,
         doc="Luminosity corresponding to absolute bolometric magnitude zero "
         "(magnitude M_bol).")
def_unit(['bol', 'f_bol'], _si.L_bol0 / (4 * _numpy.pi * (10.*astrophys.pc)**2),
         namespace=_ns, prefixes=False, doc="Irradiance corresponding to "
         "appparent bolometric magnitude zero (magnitude m_bol).")
def_unit(['AB', 'ABflux'], 10.**(48.6/-2.5) * cgs.erg * cgs.cm**-2 / si.s / si.Hz,
         namespace=_ns, prefixes=False,
         doc="AB magnitude zero flux density (magnitude ABmag).")
def_unit(['ST', 'STflux'], 10.**(21.1/-2.5) * cgs.erg * cgs.cm**-2 / si.s / si.AA,
         namespace=_ns, prefixes=False,
         doc="ST magnitude zero flux density. (magnitude STmag).")

def_unit(['mgy', 'maggy'],
         namespace=_ns, prefixes=[(['n'], ['nano'], 1e-9)],
         doc="Maggies - a linear flux unit that is the flux for a mag=0 object."
             "To tie this onto a specific calibrated unit system, the "
             "zero_point_flux equivalency should be used.")

def zero_point_flux(flux0):
    """
    An equivalency for converting linear flux units ("maggys") defined relative
    to a standard source into a standardized system.

    Parameters
    ----------
    flux0 : u.Quantity
        The flux of a magnitude-0 object in the "maggy" system.
    """
    flux_unit0 = Unit(flux0)
    return [(maggy, flux_unit0)]

###########################################################################
# CLEANUP

del UnitBase
del def_unit
del cgs, si, astrophys

###########################################################################
# DOCSTRING

# This generates a docstring for this module that describes all of the
# standard units defined here.
from .utils import generate_unit_summary as _generate_unit_summary
if __doc__ is not None:
    __doc__ += _generate_unit_summary(globals())
<|endoftext|>


<commit_before>#!/usr/bin/python3
"""
Simple script that autocommits files being worked on to git.
"""
import subprocess
import time
import argparse

def run_git_command(*command):
    return subprocess.check_output([
        '/usr/bin/git',
    ] + list(command))

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('time', help='Number of seconds to wait between autocommits')
    args = argparser.parse_args()

    run_git_command(
        'commit',
        '--allow-empty',
        '-m',
        'Starting autocommit with period %s seconds' % args.time
    )

    while True:
        if run_git_command('status', '-s'):
            changed = True
        else:
            changed = False

        if changed:
            run_git_command('add', '--all', '.')
            try:
                run_git_command('commit', '-m', 'Autocommit')
            except:
                pass

        run_git_command('pull', '-r', '-s', 'recursive', '-Xours', 'origin', 'master')

        if changed:
            try:
                run_git_command('push', 'origin', 'master')
            except:
                continue
        time.sleep(float(args.time))
<commit_msg>Autocommit<commit_after>#!/usr/bin/python3
"""
Simple script that autocommits files being worked on to git.
"""
import subprocess
import time
import argparse

def run_git_command(*command):
    return subprocess.check_output([
        '/usr/bin/git',
    ] + list(command))

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('time', help='Number of seconds to wait between autocommits')
    args = argparser.parse_args()

    run_git_command(
        'commit',
        '--allow-empty',
        '-m',
        'Starting autocommit with period %s seconds' % args.time
    )

    while True:
        # git --short produces no output if there has been nothing to commit
        if run_git_command('status', '--short'):
            changed = True
        else:
            changed = False

        if changed:
            run_git_command('add', '--all', '.')
            try:
                run_git_command('commit', '-m', 'Autocommit')
            except:
                pass

        run_git_command('pull', '-r', '-s', 'recursive', '-Xours', 'origin', 'master')

        if changed:
            try:
                run_git_command('push', 'origin', 'master')
            except:
                continue
        time.sleep(float(args.time))
<|endoftext|>


<commit_before>#!/usr/bin/python3
"""
Simple script that autocommits files being worked on to git.
"""
import subprocess
import time
import argparse

def run_git_command(*command):
    return subprocess.check_output([
        '/usr/bin/git',
    ] + list(command))

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('time', help='Number of seconds to wait between autocommits')
    args = argparser.parse_args()

    run_git_command(
        'commit',
        '--allow-empty',
        '-m',
        'Starting autocommit with period %s seconds' % args.time
    )

    while True:
        # git --short produces no output if there has been nothing to commit
        if run_git_command('status', '--short'):
            changed = True
        else:
            changed = False

        if changed:
            # the --all also git rms the files that have been removed
            run_git_command('add', '--all', '.')
            try:
                run_git_command('commit', '--message', 'Autocommit')
            except Exception:
                continue

        remote_head = run_git_command('ls-remote', 'origin', 'refs/head/master')
        print(remote_head)
        try:
            run_git_command(
                'pull',
                '--rebase',
                '--strategy', 'recursive',
                '--strategy-option', 'ours',
                'origin', 'master'
            )
        except Exception:
            continue

        if changed:
            try:
                run_git_command('push', 'origin', 'master')
            except Exception:
                continue
        time.sleep(float(args.time))
<commit_msg>Autocommit<commit_after>#!/usr/bin/python3
"""
Simple script that autocommits files being worked on to git.
"""
import subprocess
import time
import argparse

def run_git_command(*command):
    return subprocess.check_output([
        '/usr/bin/git',
    ] + list(command))

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('time', help='Number of seconds to wait between autocommits')
    args = argparser.parse_args()

    run_git_command(
        'commit',
        '--allow-empty',
        '-m',
        'Starting autocommit with period %s seconds' % args.time
    )

    while True:
        # git --short produces no output if there has been nothing to commit
        if run_git_command('status', '--short'):
            changed = True
        else:
            changed = False

        if changed:
            # the --all also git rms the files that have been removed
            run_git_command('add', '--all', '.')
            try:
                run_git_command('commit', '--message', 'Autocommit')
            except Exception:
                continue

        remote_head = run_git_command('ls-remote', '--heads', 'origin', 'refs/head/master')
        print(remote_head)
        try:
            run_git_command(
                'pull',
                '--rebase',
                '--strategy', 'recursive',
                '--strategy-option', 'ours',
                'origin', 'master'
            )
        except Exception:
            continue

        if changed:
            try:
                run_git_command('push', 'origin', 'master')
            except Exception:
                continue
        time.sleep(float(args.time))
<|endoftext|>


<commit_before>import time
import json

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from header import consumer_key, consumer_secret, access_token, access_token_secret
from send_data import execute_query

req_tok_url = 'https://api.twitter.com/oauth/request_token'
oauth_url = 'https://api.twitter.com/oauth/authorize'
acc_tok_url = 'https://api.twitter.com/oauth/access_token'

class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream.
    # This is a basic listener that just prints received tweets to stdout.

    """
    def __init__(self):
        self.start_time = time.clock()

    def on_data(self, data):
        json_data = json.loads(data)

        language = json_data.get('lang', None)
        location = json_data.get('geo', None)
        place = json_data.get('place', None)
        if place:
            country_code = place.get('country_code', None)
        if location and (language == 'en') and (country_code == 'US'):
            location = location.get('coordinates', None)
            screen_name = json_data.get('user', None).get('screen_name', None)
            text = json_data.get('text', None)
            location_lat = location[0]
            location_lng = location[1]
            created_at = json_data.get('created_at', None)
            try:
                hashtags = [i['text'] for i in json_data.get('entities', None).get('hashtags', None)]
            except AttributeError:
                # print "I HAD THIS ERROR"
                return

            data_list = (
                            screen_name,
                            text,
                            location_lat,
                            location_lng,
                            created_at,
                            hashtags
                        )

            sql = """INSERT INTO "Tweet" (screen_name, text, location_lat, location_lng, created_at, hashtags) VALUES (%s, %s, %s, %s, %s, %s); """

            # print "Sending to database..."
            execute_query(sql, data_list)

    def on_error(self, status):
        error_counter = 0
        if status == 420:
            time.sleep(15)
            print "Made too many requests!"
            with open("output.txt", "a") as myfile:
                myfile.write("Made too many requests!")
                myfile.write("\n")
            print '*' * 20
            error_counter += 1
            print "Errors: ", error_counter

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    # print "Streaming..."
    stream.filter(locations=[-124.848974, 24.396308, -66.885444, 49.384358])
<commit_msg>added filters for hawaii and alaska with help from corinne<commit_after>import time
import json

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from header import consumer_key, consumer_secret, access_token, access_token_secret
from send_data import execute_query

req_tok_url = 'https://api.twitter.com/oauth/request_token'
oauth_url = 'https://api.twitter.com/oauth/authorize'
acc_tok_url = 'https://api.twitter.com/oauth/access_token'

class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream.
    # This is a basic listener that just prints received tweets to stdout.

    """
    def __init__(self):
        self.start_time = time.clock()

    def on_data(self, data):
        json_data = json.loads(data)

        language = json_data.get('lang', None)
        location = json_data.get('geo', None)
        place = json_data.get('place', None)
        country_code = None
        if place:
            country_code = place.get('country_code', None)
        if location and (language == 'en') and (country_code == 'US'):
            location = location.get('coordinates', None)
            screen_name = json_data.get('user', None).get('screen_name', None)
            text = json_data.get('text', None)
            location_lat = location[0]
            location_lng = location[1]
            created_at = json_data.get('created_at', None)
            try:
                hashtags = [i['text'] for i in json_data.get('entities', None).get('hashtags', None)]
            except AttributeError:
                # print "I HAD THIS ERROR"
                return

            data_list = (
                            screen_name,
                            text,
                            location_lat,
                            location_lng,
                            created_at,
                            hashtags
                        )

            sql = """INSERT INTO "Tweet" (screen_name, text, location_lat, location_lng, created_at, hashtags) VALUES (%s, %s, %s, %s, %s, %s); """

            # print "Sending to database..."
            execute_query(sql, data_list)

    def on_error(self, status):
        error_counter = 0
        if status == 420:
            time.sleep(15)
            print "Made too many requests!"
            with open("output.txt", "a") as myfile:
                myfile.write("Made too many requests!")
                myfile.write("\n")
            print '*' * 20
            error_counter += 1
            print "Errors: ", error_counter

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    # print "Streaming..."
    stream.filter(locations=[
        -124.848974, 24.396308, -66.885444, 49.384358, -150.011947, 61.040969,
        -149.6861, 61.234443, -157.966444, 21.255358, -157.663132, 21.373863])<|endoftext|>