<commit_before># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for export utils."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile
import time

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.platform import test
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model.model_utils import export_output
from tensorflow.python.saved_model.model_utils import export_utils

class LabeledTensorMock(object):
  """Mock class emulating LabeledTensor."""

  def __init__(self):
    self.tensor = constant_op.constant([1])

def _convert_labeled_tensor_mock_to_tensor(value, *args, **kwargs):
  return ops.internal_convert_to_tensor(value.tensor, *args, **kwargs)

ops.register_tensor_conversion_function(LabeledTensorMock,
                                        _convert_labeled_tensor_mock_to_tensor)

class ExportTest(test_util.TensorFlowTestCase):

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_without_receiver_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(receiver_tensor,
                                                         output_1),
        "head-2":
            signature_def_utils.classification_signature_def(receiver_tensor,
                                                             output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def({
                "input": receiver_tensor
            }, {"some_output_3": output_3})
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_with_dict_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    receiver_tensors_alternative_1 = {
        "foo": array_ops.placeholder(dtypes.int64),
        "bar": array_ops.sparse_placeholder(dtypes.float32)}
    receiver_tensors_alternatives = {"other": receiver_tensors_alternative_1}
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, receiver_tensors_alternatives)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(
                receiver_tensor,
                output_1),
        "head-2":
            signature_def_utils.classification_signature_def(
                receiver_tensor,
                output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensor},
                {"some_output_3": output_3}),
        "other:head-3":
            signature_def_utils.predict_signature_def(
                receiver_tensors_alternative_1,
                {"some_output_3": output_3})

        # Note that the alternatives 'other:serving_default' and
        # 'other:head-2' are invalid, because regession and classification
        # signatures must take a single string input.  Here we verify that
        # these invalid signatures are not included in the export_utils.
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_with_single_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    receiver_tensors_alternative_1 = array_ops.placeholder(dtypes.int64)
    receiver_tensors_alternative_2 = array_ops.sparse_placeholder(
        dtypes.float32)
    # Note we are passing single Tensors as values of
    # receiver_tensors_alternatives, where normally that is a dict.
    # In this case a dict will be created using the default receiver tensor
    # name "input".
    receiver_tensors_alternatives = {"other1": receiver_tensors_alternative_1,
                                     "other2": receiver_tensors_alternative_2}
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, receiver_tensors_alternatives)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(
                receiver_tensor,
                output_1),
        "head-2":
            signature_def_utils.classification_signature_def(
                receiver_tensor,
                output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensor},
                {"some_output_3": output_3}),
        "other1:head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensors_alternative_1},
                {"some_output_3": output_3}),
        "other2:head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensors_alternative_2},
                {"some_output_3": output_3})

        # Note that the alternatives 'other:serving_default' and 'other:head-2'
        # are invalid, because regession and classification signatures must take
        # a single string input.  Here we verify that these invalid signatures
        # are not included in the export_utils.
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  def test_build_all_signature_defs_export_outputs_required(self):
    receiver_tensor = constant_op.constant(["11"])

    with self.assertRaises(ValueError) as e:
      export_utils.build_all_signature_defs(receiver_tensor, None)

    self.assertTrue(str(e.exception).startswith(
        "export_outputs must be a dict"))

  def test_get_timestamped_export_dir(self):
    export_dir_base = tempfile.mkdtemp() + "export/"
    export_dir_1 = export_utils.get_timestamped_export_dir(
        export_dir_base)
    time.sleep(2)
    export_dir_2 = export_utils.get_timestamped_export_dir(
        export_dir_base)
    time.sleep(2)
    export_dir_3 = export_utils.get_timestamped_export_dir(
        export_dir_base)

    # Export directories should be named using a timestamp that is seconds
    # since epoch.  Such a timestamp is 10 digits long.
    time_1 = os.path.basename(export_dir_1)
    self.assertEqual(10, len(time_1))
    time_2 = os.path.basename(export_dir_2)
    self.assertEqual(10, len(time_2))
    time_3 = os.path.basename(export_dir_3)
    self.assertEqual(10, len(time_3))

    self.assertTrue(int(time_1) < int(time_2))
    self.assertTrue(int(time_2) < int(time_3))

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_serving_only(self):
    receiver_tensor = {"input": array_ops.placeholder(dtypes.string)}
    output_1 = constant_op.constant([1.])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.PredictOutput(outputs=output_1),
        "train": export_output.TrainOutput(loss=output_1),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs)

    expected_signature_defs = {
        "serving_default": signature_def_utils.predict_signature_def(
            receiver_tensor, {"output": output_1})
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, serving_only=False)

    expected_signature_defs.update({
        "train": signature_def_utils.supervised_train_signature_def(
            receiver_tensor, loss={"loss": output_1})
    })

    self.assertDictEqual(expected_signature_defs, signature_defs)

if __name__ == "__main__":
  test.main()
<commit_msg>Remove unused class in export test file.<commit_after># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for export utils."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile
import time

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.platform import test
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model.model_utils import export_output
from tensorflow.python.saved_model.model_utils import export_utils

class ExportTest(test_util.TensorFlowTestCase):

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_without_receiver_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(receiver_tensor,
                                                         output_1),
        "head-2":
            signature_def_utils.classification_signature_def(receiver_tensor,
                                                             output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def({
                "input": receiver_tensor
            }, {"some_output_3": output_3})
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_with_dict_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    receiver_tensors_alternative_1 = {
        "foo": array_ops.placeholder(dtypes.int64),
        "bar": array_ops.sparse_placeholder(dtypes.float32)}
    receiver_tensors_alternatives = {"other": receiver_tensors_alternative_1}
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, receiver_tensors_alternatives)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(
                receiver_tensor,
                output_1),
        "head-2":
            signature_def_utils.classification_signature_def(
                receiver_tensor,
                output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensor},
                {"some_output_3": output_3}),
        "other:head-3":
            signature_def_utils.predict_signature_def(
                receiver_tensors_alternative_1,
                {"some_output_3": output_3})

        # Note that the alternatives 'other:serving_default' and
        # 'other:head-2' are invalid, because regession and classification
        # signatures must take a single string input.  Here we verify that
        # these invalid signatures are not included in the export_utils.
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_with_single_alternatives(self):
    receiver_tensor = array_ops.placeholder(dtypes.string)
    receiver_tensors_alternative_1 = array_ops.placeholder(dtypes.int64)
    receiver_tensors_alternative_2 = array_ops.sparse_placeholder(
        dtypes.float32)
    # Note we are passing single Tensors as values of
    # receiver_tensors_alternatives, where normally that is a dict.
    # In this case a dict will be created using the default receiver tensor
    # name "input".
    receiver_tensors_alternatives = {"other1": receiver_tensors_alternative_1,
                                     "other2": receiver_tensors_alternative_2}
    output_1 = constant_op.constant([1.])
    output_2 = constant_op.constant(["2"])
    output_3 = constant_op.constant(["3"])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.RegressionOutput(value=output_1),
        "head-2": export_output.ClassificationOutput(classes=output_2),
        "head-3": export_output.PredictOutput(outputs={
            "some_output_3": output_3
        }),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, receiver_tensors_alternatives)

    expected_signature_defs = {
        "serving_default":
            signature_def_utils.regression_signature_def(
                receiver_tensor,
                output_1),
        "head-2":
            signature_def_utils.classification_signature_def(
                receiver_tensor,
                output_2, None),
        "head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensor},
                {"some_output_3": output_3}),
        "other1:head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensors_alternative_1},
                {"some_output_3": output_3}),
        "other2:head-3":
            signature_def_utils.predict_signature_def(
                {"input": receiver_tensors_alternative_2},
                {"some_output_3": output_3})

        # Note that the alternatives 'other:serving_default' and 'other:head-2'
        # are invalid, because regession and classification signatures must take
        # a single string input.  Here we verify that these invalid signatures
        # are not included in the export_utils.
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

  def test_build_all_signature_defs_export_outputs_required(self):
    receiver_tensor = constant_op.constant(["11"])

    with self.assertRaises(ValueError) as e:
      export_utils.build_all_signature_defs(receiver_tensor, None)

    self.assertTrue(str(e.exception).startswith(
        "export_outputs must be a dict"))

  def test_get_timestamped_export_dir(self):
    export_dir_base = tempfile.mkdtemp() + "export/"
    export_dir_1 = export_utils.get_timestamped_export_dir(
        export_dir_base)
    time.sleep(2)
    export_dir_2 = export_utils.get_timestamped_export_dir(
        export_dir_base)
    time.sleep(2)
    export_dir_3 = export_utils.get_timestamped_export_dir(
        export_dir_base)

    # Export directories should be named using a timestamp that is seconds
    # since epoch.  Such a timestamp is 10 digits long.
    time_1 = os.path.basename(export_dir_1)
    self.assertEqual(10, len(time_1))
    time_2 = os.path.basename(export_dir_2)
    self.assertEqual(10, len(time_2))
    time_3 = os.path.basename(export_dir_3)
    self.assertEqual(10, len(time_3))

    self.assertTrue(int(time_1) < int(time_2))
    self.assertTrue(int(time_2) < int(time_3))

  @test_util.deprecated_graph_mode_only
  def test_build_all_signature_defs_serving_only(self):
    receiver_tensor = {"input": array_ops.placeholder(dtypes.string)}
    output_1 = constant_op.constant([1.])
    export_outputs = {
        signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            export_output.PredictOutput(outputs=output_1),
        "train": export_output.TrainOutput(loss=output_1),
    }

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs)

    expected_signature_defs = {
        "serving_default": signature_def_utils.predict_signature_def(
            receiver_tensor, {"output": output_1})
    }

    self.assertDictEqual(expected_signature_defs, signature_defs)

    signature_defs = export_utils.build_all_signature_defs(
        receiver_tensor, export_outputs, serving_only=False)

    expected_signature_defs.update({
        "train": signature_def_utils.supervised_train_signature_def(
            receiver_tensor, loss={"loss": output_1})
    })

    self.assertDictEqual(expected_signature_defs, signature_defs)

if __name__ == "__main__":
  test.main()
<|endoftext|>


<commit_before># Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class RCdcfluview(RPackage):
    """The 'U.S.' Centers for Disease Control ('CDC') maintains a portal
    <http://gis.cdc.gov/grasp/fluview/fluportaldashboard.html> for accessing
    state, regional and national influenza statistics as well as Mortality
    Surveillance Data. The web interface makes it difficult and time-consuming
    to select and retrieve influenza data. Tools are provided to access the
    data provided by the portal's underlying 'API'."""

    homepage = "https://cloud.r-project.org/package=cdcfluview"
    url      = "https://cloud.r-project.org/src/contrib/cdcfluview_0.7.0.tar.gz"
    list_url = "https://cloud.r-project.org/src/contrib/Archive/cdcfluview"

    version('0.9.0', sha256='1b2064886858cbb1790ef808d88fbab75d3a9cf55e720638221a3377ff8dd244')
    version('0.7.0', sha256='8c8978d081f8472a6ed5ec54c4e6dd906f97ee28d0f88eef1514088f041ecc03')

    depends_on('r@3.2.0:', type=('build', 'run'))
    depends_on('r-httr', type=('build', 'run'))
    depends_on('r-dplyr', type=('build', 'run'))
    depends_on('r-jsonlite', type=('build', 'run'))
    depends_on('r-sf', type=('build', 'run'))
    depends_on('r-xml2', type=('build', 'run'))
    depends_on('r-purrr', type=('build', 'run'))
    depends_on('r-readr', type=('build', 'run'))
    depends_on('r-mmwrweek', type=('build', 'run'))
    depends_on('r-units@0.4-6:', type=('build', 'run'))
<commit_msg>add version   0.9.2 to r-cdcfluview (#20960)<commit_after># Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class RCdcfluview(RPackage):
    """Retrieve Flu Season Data from the United States Centers for Disease
    Control and Prevention ('CDC') 'FluView' Portal

    The 'U.S.' Centers for Disease Control ('CDC') maintains a portal
    <http://gis.cdc.gov/grasp/fluview/fluportaldashboard.html> for accessing
    state, regional and national influenza statistics as well as Mortality
    Surveillance Data. The web interface makes it difficult and time-consuming
    to select and retrieve influenza data. Tools are provided to access the
    data provided by the portal's underlying 'API'."""

    homepage = "https://cloud.r-project.org/package=cdcfluview"
    url      = "https://cloud.r-project.org/src/contrib/cdcfluview_0.7.0.tar.gz"
    list_url = "https://cloud.r-project.org/src/contrib/Archive/cdcfluview"

    version('0.9.2', sha256='f2080fc80c5e0241f8b657f5ac3a251ea89dfc26c1ab13bdfaed4d4e97495015')
    version('0.9.0', sha256='1b2064886858cbb1790ef808d88fbab75d3a9cf55e720638221a3377ff8dd244')
    version('0.7.0', sha256='8c8978d081f8472a6ed5ec54c4e6dd906f97ee28d0f88eef1514088f041ecc03')

    depends_on('r@3.2.0:', when='@:0.9.0', type=('build', 'run'))
    depends_on('r@3.5.0:', when='@0.9.2:', type=('build', 'run'))
    depends_on('r-httr', type=('build', 'run'))
    depends_on('r-progress', when='@0.9.2:', type=('build', 'run'))
    depends_on('r-dplyr', type=('build', 'run'))
    depends_on('r-jsonlite', type=('build', 'run'))
    depends_on('r-tibble', when='@0.9.2:', type=('build', 'run'))
    depends_on('r-sf', type=('build', 'run'))
    depends_on('r-xml2', type=('build', 'run'))
    depends_on('r-purrr', type=('build', 'run'))
    depends_on('r-readr', type=('build', 'run'))
    depends_on('r-mmwrweek', type=('build', 'run'))
    depends_on('r-units@0.4-6:', type=('build', 'run'))
<|endoftext|>


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

import re
import subprocess
import sys

def collect_worker_time(path, query_id, worker_id):

    regex_operator = re.compile(r'INFO  \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d* .Nonblocking query executor#\d*. WorkerQueryPartition\$\d* - Query #(\d*) executed for (\d*)h (\d*)m (\d*\.\d+|\d+)s')

    lines = [ line.strip() for line in open(path)]
    lines = [ line for line in lines if regex_operator.match(line) ]
    tuples = [ re.findall(r'INFO  \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d* .Nonblocking query executor#\d*. WorkerQueryPartition\$\d* - Query #(\d*) executed for (\d*)h (\d*)m (\d*\.\d+|\d+)s', line)
                for line in lines]
    tuples = [i[0] for i in tuples if i[0][0] == str(query_id)]
    if len(tuples) == 0:
        print >> sys.stderr, "can not find running time of query %d"%query_id
        sys.exit(1)
    elif len(tuples) > 1:
        print >> sys.stderr, "find running time of query %d more than once"%query_id
        sys.exit(1)
    print "%d,%f"%(worker_id, int(tuples[0][1])*3600+int(tuples[0][2])*60+float(tuples[0][3]))

def main(argv):
# Usage
    if len(argv) != 3:
        print >> sys.stderr, "Usage: %s <log_file_path> <# of workers>" % (argv[0])
        print >> sys.stderr, "       log_file_path "
        print >> sys.stderr, "       # of workers "
        sys.exit(1)

    print "worker_id,running_time"
    number_of_workers = int(argv[2])
    for i in range(1,number_of_workers+1):
        collect_worker_time(argv[1]+"/worker_%d_stdout"%i,37,i)

if __name__ == "__main__":
    main(sys.argv)
<commit_msg>fix an error<commit_after>#!/usr/bin/env python

import re
import subprocess
import sys

def collect_worker_time(path, query_id, worker_id):

    regex_operator = re.compile(r'INFO  \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d* .Nonblocking query executor#\d*. WorkerQueryPartition\$\d* - Query #(\d*) executed for (\d*)h (\d*)m (\d*\.\d+|\d+)s')

    lines = [ line.strip() for line in open(path)]
    lines = [ line for line in lines if regex_operator.match(line) ]
    tuples = [ re.findall(r'INFO  \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d* .Nonblocking query executor#\d*. WorkerQueryPartition\$\d* - Query #(\d*) executed for (\d*)h (\d*)m (\d*\.\d+|\d+)s', line)
                for line in lines]
    tuples = [i[0] for i in tuples if i[0][0] == str(query_id)]
    if len(tuples) == 0:
        print >> sys.stderr, "can not find running time of query %d"%query_id
        sys.exit(1)
    elif len(tuples) > 1:
        print >> sys.stderr, "find running time of query %d more than once"%query_id
        sys.exit(1)
    print "%d,%f"%(worker_id, int(tuples[0][1])*3600+int(tuples[0][2])*60+float(tuples[0][3]))

def main(argv):
# Usage
    if len(argv) != 4:
        print >> sys.stderr, "Usage: %s <log_file_path> <# of workers> <query id>" % (argv[0])
        print >> sys.stderr, "       log_file_path "
        print >> sys.stderr, "       # of workers "
        print >> sys.stderr, "       query id "
        sys.exit(1)

    print "worker_id,running_time"
    number_of_workers = int(argv[2])
    for i in range(1,number_of_workers+1):
        collect_worker_time(argv[1]+"/worker_%d_stdout"%i,int(argv[3]),i)

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


<commit_before># birthday.py

def print_header():
    """ Print the header for this app"""
    print("--------------------------------------")
    print("        BIRTHDAY COUNTDOWN APP")
    print("--------------------------------------")

def get_user_bday():
    """ Get year, month and day as strings from user """
    year = input("Enter your year of birth(YYYY): ")
    month = input("Enter your month of birth(MM): ")
    day = input("Enter your day of birth(DD): ")

    # Strip the whitespaces and convert to integers
    year = int(year.strip())
    month = int(month.strip())
    day = int(day.strip())

    return year, month, day

def calculate_days(bday):
    """ Calculate the number of days until the user's birthday """
    return 0

def print_result(bday, days):
    """ Print the result to the user """
    print("Looks like you were born on {}/{}/{}".format(bday[0], bday[1], bday[2]))
    if days > 0:
        print("You celebrated your birthday {} days before".format(days))
    elif days < 0:
        print("You will celebrate your birthday in {} days".format(-days))
    else:
        print("Today is your birthday. Happy Birthday! Have a great one!")

    pass

def main():
    print_header()
    bday = get_user_bday()
    days = calculate_days(bday)
    print_result(bday, days)

if __name__ == '__main__':
    main()
<commit_msg>Complete the app by adding the logic for calculating the days difference<commit_after># birthday.py
import datetime
from collections import namedtuple

Date = namedtuple('Date', ['year', 'month', 'day'])

def print_header():
    """ Print the header for this app"""
    print("--------------------------------------")
    print("        BIRTHDAY COUNTDOWN APP")
    print("--------------------------------------")

def get_user_bday():
    """ Get year, month and day as strings from user """
    year = input("Enter your year of birth(YYYY): ")
    month = input("Enter your month of birth(MM): ")
    day = input("Enter your day of birth(DD): ")

    # Strip the whitespaces and convert to integers
    year = int(year.strip())
    month = int(month.strip())
    day = int(day.strip())

    return Date(year, month, day)

def calculate_days(bday):
    """ Calculate the number of days until the user's birthday """
    now = datetime.datetime.now()
    diff = now - datetime.datetime(now.year, bday.month, bday.day)
    days = int(diff.total_seconds() / 60 / 60 / 24)

    return days

def print_result(bday, days):
    """ Print the result to the user """
    print("Looks like you were born on {}/{}/{}".format(bday[0], bday[1], bday[2]))
    if days > 0:
        print("You celebrated your birthday {} days before".format(days))
    elif days < 0:
        print("You will celebrate your birthday in {} days".format(-days))
    else:
        print("Today is your birthday. Happy Birthday! Have a great one!")

    pass

def main():
    print_header()
    bday = get_user_bday()
    days = calculate_days(bday)
    print_result(bday, days)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from openerp import models, fields, api
import openerp.addons.decimal_precision as dp

class account_tax_withholding(models.Model):
    _name = "account.tax.withholding"
    _description = "Account Withholding Taxes"

    name = fields.Char(
        'Name',
        required=True,
        )
    description = fields.Char(
        'Description',
        required=True,
        )
    type_tax_use = fields.Selection(
        [('receipt', 'Receipt'), ('payment', 'Payment'), ('all', 'All')],
        'Tax Application',
        required=True
        )
    active = fields.Boolean(
        'Active',
        default=True,
        help="If the active field is set to False, it will allow you to hide the tax without removing it.")
    # TODO add this field  and other for automation
    # type = fields.Selection(
    #     [('percent', 'Percentage'), ('fixed', 'Fixed Amount'),
    #      ('none', 'None'), ('code', 'Python Code'), ('balance', 'Balance')],
    #     'Type',
    #     required=True,
    #     help="The computation method for the tax amount."
    #     )
    account_id = fields.Many2one(
        'account.account',
        'Account',
        required=True,
        )
    ref_account_id = fields.Many2one(
        'account.account',
        'Refund Account',
        required=True,
        )
    account_analytic_id = fields.Many2one(
        'account.analytic.account',
        'Analytic Account',
        )
    ref_account_analytic_id = fields.Many2one(
        'account.analytic.account',
        'Refund Analytic Account',
        )
    company_id = fields.Many2one(
        'res.company',
        'Company',
        required=True,
        default=lambda self: self.env['res.company']._company_default_get(
            'account.tax.withholding')
        )
    #
    # Fields used for the Tax declaration
    #
    # TODO ver si necesitamos los base o no
    base_code_id = fields.Many2one(
        'account.tax.code',
        'Base Code',
        help="Use this code for the tax declaration."
        )
    tax_code_id = fields.Many2one(
        'account.tax.code',
        'Tax Code',
        required=True,
        help="Use this code for the tax declaration.",
        )
    base_sign = fields.Float(
        'Base Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        )
    tax_sign = fields.Float(
        'Tax Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        )
    ref_base_code_id = fields.Many2one(
        'account.tax.code',
        'Refund Base Code',
        help="Use this code for the tax declaration."
        )
    ref_tax_code_id = fields.Many2one(
        'account.tax.code',
        'Refund Tax Code',
        required=True,
        help="Use this code for the tax declaration.",
        )
    ref_base_sign = fields.Float(
        'Refund Base Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        )
    ref_tax_sign = fields.Float(
        'Refund Tax Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        )

class account_tax_withholding_template(models.Model):
    _name = "account.tax.withholding.template"
    _inherit = "account.tax.withholding"
    _description = "Account Withholding Taxes Template"

    chart_template_id = fields.Many2one(
        'account.chart.template',
        'Chart Template',
        required=True
        )

    @api.multi
    def _generate_withholding(
            self, tax_code_ref, account_ref, company_id):
        """
        This method generate taxes from templates.

        :param self: list of browse record of the tax templates to process
        :param tax_code_template_ref: Taxcode templates reference.
        :param company_id: id of the company the wizard is running for
        :returns:
            {
            'tax_template_to_tax': mapping between tax template and the newly generated taxes corresponding,
            'account_dict': dictionary containing a to-do list with all the accounts to assign on new taxes
            }
        """
        res = {}
        todo_dict = {}
        tax_template_to_tax = {}
        for tax in self:
            vals_tax = {
                'name': tax.name,
                'code': tax.sequence,
                'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
                'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
                'base_sign': tax.base_sign,
                'tax_sign': tax.tax_sign,
                'base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
                'tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
                'base_sign': tax.ref_base_sign,
                'tax_sign': tax.ref_tax_sign,
                'company_id': company_id,
                'account_id': company_id,
                'account_id': company_id,
            }
            new_tax = self.env['account.tax.withholding'].create(vals_tax)
            tax_template_to_tax[tax.id] = new_tax
            #as the accounts have not been created yet, we have to wait before filling these fields
            todo_dict[new_tax] = {
                'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
                'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
            }
        res.update({'tax_template_to_tax': tax_template_to_tax, 'account_dict': todo_dict})
        return res<commit_msg>FIX withholding<commit_after># -*- coding: utf-8 -*-
from openerp import models, fields, api
import openerp.addons.decimal_precision as dp

class account_tax_withholding(models.Model):
    _name = "account.tax.withholding"
    _description = "Account Withholding Taxes"

    name = fields.Char(
        'Name',
        required=True,
        )
    description = fields.Char(
        'Description',
        required=True,
        )
    type_tax_use = fields.Selection(
        [('receipt', 'Receipt'), ('payment', 'Payment'), ('all', 'All')],
        'Tax Application',
        required=True
        )
    active = fields.Boolean(
        'Active',
        default=True,
        help="If the active field is set to False, it will allow you to hide the tax without removing it.")
    # TODO add this field  and other for automation
    # type = fields.Selection(
    #     [('percent', 'Percentage'), ('fixed', 'Fixed Amount'),
    #      ('none', 'None'), ('code', 'Python Code'), ('balance', 'Balance')],
    #     'Type',
    #     required=True,
    #     help="The computation method for the tax amount."
    #     )
    account_id = fields.Many2one(
        'account.account',
        'Account',
        required=True,
        )
    ref_account_id = fields.Many2one(
        'account.account',
        'Refund Account',
        required=True,
        )
    account_analytic_id = fields.Many2one(
        'account.analytic.account',
        'Analytic Account',
        )
    ref_account_analytic_id = fields.Many2one(
        'account.analytic.account',
        'Refund Analytic Account',
        )
    company_id = fields.Many2one(
        'res.company',
        'Company',
        required=True,
        default=lambda self: self.env['res.company']._company_default_get(
            'account.tax.withholding')
        )
    #
    # Fields used for the Tax declaration
    #
    # TODO ver si necesitamos los base o no
    base_code_id = fields.Many2one(
        'account.tax.code',
        'Base Code',
        help="Use this code for the tax declaration."
        )
    tax_code_id = fields.Many2one(
        'account.tax.code',
        'Tax Code',
        required=True,
        help="Use this code for the tax declaration.",
        )
    base_sign = fields.Float(
        'Base Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        default=1,
        )
    tax_sign = fields.Float(
        'Tax Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        default=1,
        )
    ref_base_code_id = fields.Many2one(
        'account.tax.code',
        'Refund Base Code',
        help="Use this code for the tax declaration."
        )
    ref_tax_code_id = fields.Many2one(
        'account.tax.code',
        'Refund Tax Code',
        required=True,
        help="Use this code for the tax declaration.",
        )
    ref_base_sign = fields.Float(
        'Refund Base Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        default=1,
        )
    ref_tax_sign = fields.Float(
        'Refund Tax Code Sign',
        help="Usually 1 or -1.",
        digits_compute=dp.get_precision('Account'),
        default=1,
        )

class account_tax_withholding_template(models.Model):
    _name = "account.tax.withholding.template"
    _inherit = "account.tax.withholding"
    _description = "Account Withholding Taxes Template"

    chart_template_id = fields.Many2one(
        'account.chart.template',
        'Chart Template',
        required=True
        )
    account_id = fields.Many2one(
        'account.account.template',
        )
    ref_account_id = fields.Many2one(
        'account.account.template',
        )
    base_code_id = fields.Many2one(
        'account.tax.code.template',
        )
    tax_code_id = fields.Many2one(
        'account.tax.code.template',
        )
    ref_base_code_id = fields.Many2one(
        'account.tax.code.template',
        )
    ref_tax_code_id = fields.Many2one(
        'account.tax.code.template',
        )

    @api.multi
    def _generate_withholding(
            self, tax_code_ref, account_ref, company_id):
        """
        This method generate taxes from templates.

        :param self: list of browse record of the tax templates to process
        :param tax_code_template_ref: Taxcode templates reference.
        :param company_id: id of the company the wizard is running for
        :returns:
            {
            'tax_template_to_tax': mapping between tax template and the newly generated taxes corresponding,
            'account_dict': dictionary containing a to-do list with all the accounts to assign on new taxes
            }
        """
        res = {}
        todo_dict = {}
        tax_template_to_tax = {}
        for tax in self:
            vals_tax = {
                'name': tax.name,
                'code': tax.sequence,
                'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
                'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
                'base_sign': tax.base_sign,
                'tax_sign': tax.tax_sign,
                'base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
                'tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
                'base_sign': tax.ref_base_sign,
                'tax_sign': tax.ref_tax_sign,
                'company_id': company_id,
                'account_id': company_id,
                'account_id': company_id,
            }
            new_tax = self.env['account.tax.withholding'].create(vals_tax)
            tax_template_to_tax[tax.id] = new_tax
            #as the accounts have not been created yet, we have to wait before filling these fields
            todo_dict[new_tax] = {
                'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
                'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
            }
        res.update({'tax_template_to_tax': tax_template_to_tax, 'account_dict': todo_dict})
        return res<|endoftext|>


<commit_before>from beer.models import Beer
from beer.models import Access
from beer.models import User
from django.contrib import admin

def amount_in_liters(obj):
    if type(obj) == Beer:
        return "%s liters" % obj.amount_left
    else:
        return "%s liters" % obj.amount

class BeerAdmin(admin.ModelAdmin):
    list_display = ('name', amount_in_liters, 'cups_left',
                    'start_date', 'end_date', 'tap_number', 'active')
    list_filter = ('name',)
    prepopulated_fields = {'slug':('name',),}

class AccessAdmin(admin.ModelAdmin):
    list_display = ('user', amount_in_liters, 'beer', 'time')
    list_filter = ('beer', 'time',)

class UserAdmin(admin.ModelAdmin):
    list_display = ('name', 'rfid')

admin.site.register(Beer, BeerAdmin)
admin.site.register(Access, AccessAdmin)
admin.site.register(User, UserAdmin)
<commit_msg>Updated the admin<commit_after>from beer.models import Beer
from beer.models import Access
from beer.models import User
from django.contrib import admin

def amount_in_liters(obj):
    if type(obj) == Beer:
        return "%s liters" % obj.amount_left
    else:
        return "%s liters" % obj.amount

class BeerAdmin(admin.ModelAdmin):
    list_display = ('name', amount_in_liters, 'cups_left',
                    'start_date', 'end_date', 'tap_number', 'active')
    list_filter = ('name',)
    prepopulated_fields = {'slug': ('name', )}

admin.site.register(Beer, BeerAdmin)

class UserAdmin(admin.ModelAdmin):
    list_display = ('name', 'rfid', 'email', 'receive_alerts', 'private')
    list_editable = ('receive_alerts', 'private')
    list_filter = ('receive_alerts', 'private')

admin.site.register(User, UserAdmin)

class AccessAdmin(admin.ModelAdmin):
    list_display = ('user', amount_in_liters, 'beer', 'time', 'temperature')
    list_filter = ('beer', 'time',)

admin.site.register(Access, AccessAdmin)
<|endoftext|>


<commit_before># Import core packages
import time
import sys
import os
import fnmatch
import random
import sys
import socket
import shutil
from threading import Timer
from configparser import SafeConfigParser
import json

# Setup logging
import logging
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

# Import 3rd-part packages
import tornado.ioloop
import tornado.web
from jinja2 import Template
from mutagen.mp3 import MP3
from mutagen.id3 import ID3

#####################
# GLOBAL VARIABLES
#####################

state = {}
songStartTimer = None
songStopTimer = None
audio_data = None
parser = SafeConfigParser()
try:
    parser.read('config.cfg')
except:
    print("Problem parsing config.cfg - did you change something?")
    sys.exit(-1)

#####################
# UTILITY FUNCTIONS
#####################

def getTime():
    """Returns time in milliseconds, similar to Javascript"""

    return int(time.time() * 1000)

def getPlaylistHtml():
    """Returns HTML for the playlist"""

    playlist_html = ""
    for i,path in enumerate(state['ordering']):
        cur_song = state['playlist'][path]['song_name']
        html = """%(song)s
"""
        if state['currently_playing_songname'] == cur_song:
            song = "" + cur_song + ""
        else:
            song = cur_song
        playlist_html += html % {'i': str(i), 'song': song}
    return playlist_html

def songStarts():
    """Runs when server decides a song starts"""
    logger = logging.getLogger('syncmusic:songStarts')
    logger.debug('Playing: ' + state['currently_playing_songname'])
    state['is_playing'] = True

def songOver():
    """Runs when server decides a song stops"""
    logger = logging.getLogger('syncmusic:songOver')
    logger.debug('Done playing: ' + state['currently_playing_songname'])
    state['is_playing'] = False
    nextSong(int(parser.get('server_parameters','time_to_next_song')), -1)

def nextSong(delay, skip):
    """ Main song control

    Sets global flags for which song is playing,
    loads the new song, and sets the timers for when
    the songs should start and end.
    """
    global songStartTimer
    global songStopTimer
    global audio_data
    logger = logging.getLogger('syncmusic:nextSong')
    if (time.time() - state['last_activated'] > int(parser.get('server_parameters','time_to_disallow_skips'))
            or not state['is_initialized']):
        state['is_initialized'] = True
        state['last_activated'] = time.time()
        state['is_playing'] = False

        if skip < 0:
            state['current_song'] += skip + 2
        else:
            state['current_song'] = skip
        if state['current_song'] >= len(state['ordering']):
            state['current_song'] = 0
        if state['current_song'] < 0:
            state['current_song'] = len(state['ordering']) - 1
        current_song_path = state['ordering'][state['current_song']]

        shutil.copy(current_song_path,os.path.join(os.getcwd(),'static/sound.mp3'))
        shutil.copy(current_song_path,os.path.join(os.getcwd(),'static/start.mp3'))
        os.chdir('static')
        try:
            os.remove('sound.mp3')
        except:
            pass
        os.system('ffmpeg -i start.mp3 -codec:a libmp3lame -qscale:a 6 sound.mp3')
        audio_data = open('sound.mp3','rb').read()
        os.chdir('../')
        state['currently_playing_songname'] = state['playlist'][current_song_path]['song_name']
        state['next_song_time'] = getTime() + delay * 1000
        logger.debug('next up: ' + state['currently_playing_songname'])
        logger.debug('time: ' + str(getTime()) +
                     ' and next: ' + str(state['next_song_time']))
        if songStartTimer is not None:
            songStartTimer.cancel()
            songStopTimer.cancel()
        songStopTimer = Timer(
            float(
                state['next_song_time'] -
                getTime()) /
            1000.0,
            songStarts,
            ())
        songStopTimer.start()
        audio = MP3('./static/sound.mp3')
        logger.debug(audio.info.length)
        songStartTimer = Timer(
            2 +
            float(
                audio.info.length) +
            float(
                state['next_song_time'] -
                getTime()) /
            1000.0,
            songOver,
            ())
        songStartTimer.start()

#################
# WEB ROUTES
#################

index_page = Template(open('templates/index.html','r').read())

class SoundMp3(tornado.web.RequestHandler):

    def get(self):
        self.write(audio_data)
        self.finish()

class IndexPage(tornado.web.RequestHandler):
    """Main sign-in - /

    Server loads new song if not initialized, and
    then returns the rendered music control page
    """

    def get(self):
        if not state['is_initialized']:
            nextSong(int(parser.get('server_parameters','time_to_next_song')), state['current_song'])
        data = {}
        data['random_integer'] = random.randint(1000, 30000)
        data['playlist_html'] = getPlaylistHtml()
        data['is_playing'] = state['is_playing']
        data['message'] = 'Syncing...'
        data['is_index'] = True
        data['max_sync_lag'] = parser.get('client_parameters','max_sync_lag')
        data['check_up_wait_time'] = parser.get('client_parameters','check_up_wait_time')
        if state['debug']:
            index_page = Template(open('templates/index.html','r').read())
        self.write(index_page.render(data=data))

class SyncHandler(tornado.web.RequestHandler):
    """Syncing route - /sync

    POST request from main page with the client client_timestamp
    and current_song. Returns JSON containing the server client_timestamp
    and whether or not to load a new song.
    """

    def post(self):
        data = {}
        data['client_timestamp'] = int(self.get_argument('client_timestamp'))
        data['server_timestamp'] = getTime()
        data['mute_button_clicked'] = json.loads(self.get_argument('mute_button_clicked'))
        if data['mute_button_clicked']:
            print("MUTE BUTTON CLICKED")
            state['is_muted'] = json.loads(self.get_argument('is_muted'))
            state['last_muted'] = getTime()
        if getTime() - state['last_muted'] < 3000:
            data['mute_button_clicked'] = True
        data['is_muted'] = state['is_muted']
        data['next_song'] = state['next_song_time']
        if state['is_playing']:
            data['is_playing'] = (state['currently_playing_songname'] == self.get_argument('current_song'))
        else:
            data['is_playing'] = state['is_playing']
        data['current_song'] = state['currently_playing_songname']
        data['song_time'] = float(getTime() - state['next_song_time']) / 1000.0
        self.write(data)

class NextSongHandler(tornado.web.RequestHandler):
    """Syncing route - /sync

    POST request from main page with the client client_timestamp
    and current_song. Returns JSON containing the server client_timestamp
    and whether or not to load a new song.
    """

    def post(self):
        response = {'message': 'loading!'}
        skip = int(self.get_argument('skip'))
        nextSong(int(parser.get('server_parameters','time_to_next_song')), skip)
        self.write(response)

# Depreciated
# @app.route("/playing", methods=['GET', 'POST'])
# def playing():
#     """ Is playing route - /nextSong

#     POST request to tell server that client has started
#     playing a song. DEPRECATED.
#     """
#     global is_playing
#     response = {'message': 'loading!'}
#     if request.method == 'POST':
#         is_playing = True
#     return jsonify(response)

application = tornado.web.Application([
    (r"/", IndexPage),
    (r"/sync", SyncHandler),
    (r"/nextsong", NextSongHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': './static'}),
    (r"/sound.mp3", SoundMp3),
])

##########
# MAIN
##########

if __name__ == "__main__":
    """Load the playlist, or let user know that one needs to be loaded"""
    # app.run(host='0.0.0.0')
    logger = logging.getLogger('syncmusic:nextSong')
    cwd = os.getcwd()

    state['playlist'] = {}
    state['ordering'] = []
    state['current_song'] = 0
    state['currently_playing_songname'] = ""
    if os.path.isfile('state.json'):
        state = json.load(open('state.json','r'))
    state['last_activated'] = 0
    state['next_song_time'] = 0
    state['is_playing'] = False
    state['is_initialized'] = False
    state['last_activated'] = 0
    state['debug'] = True
    state['is_muted'] = False
    state['last_muted'] = 0

    folders_with_music = parser.get('server_parameters','music_folder').split(',')
    for folder_with_music in folders_with_music:
        # Load playlist
        folder_with_music = folder_with_music.strip()
        for root, dirnames, filenames in os.walk(folder_with_music):
            logger.debug('Collecting music from ' + root)
            for filename in fnmatch.filter(filenames, '*.mp3'):
                path = os.path.join(root, filename)
                if path in state['ordering']:
                    continue
                state['ordering'].append(path)
                state['playlist'][path] = {}
                title = filename
                artist = 'unknown'
                album = 'unknown'
                try:
                    audiofile = ID3(path)
                    try:
                        title = audiofile['TIT2'].text[0]
                    except:
                        pass
                    try:
                        artist = audiofile['TPE1'].text[0]
                    except:
                        pass
                    try:
                        album = audiofile['TALB'].text[0]
                    except:
                        pass
                except BaseException as e:
                    logger.error('Failed to get audio information for ' + path + ': '+ str(e))
                state['playlist'][path]['title'] = title
                state['playlist'][path]['artist'] = artist
                state['playlist'][path]['album'] = album
                state['playlist'][path]['song_name'] = album + ' - ' + title + ' by ' + artist
                os.chdir(cwd)

    if len(state['ordering']) == 0:
        print(
            "\n\nNo mp3s found.\nDid you specify a music folder in line 40 of config.cfg?\n\n")
        sys.exit(-1)

    os.chdir(cwd)
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("gmail.com",80))
        ip_address = s.getsockname()[0]
        s.close()
    except:
        ip_address = "127.0.0.1"

    print("\n\n" +"#" * 60)
    print("# Starting server with " + str(len(state['ordering'])) + " songs")
    print("# To use, open a browser to http://" + ip_address + ":"+ parser.get('server_parameters','port') + "")
    print("# To stop server, use Ctl + C")
    print("#" * 60 +"\n\n")

    pi_clients = []
    if len(parser.get('raspberry_pis','clients')) > 2 and ip_address != '127.0.0.1':
        pi_clients = parser.get('raspberry_pis','clients').split(',')
        for pi_client in pi_clients:
            pi_client = pi_client.strip()
            try:
                os.system("ssh " + pi_client + " 'pkill -9 midori </dev/null > log 2>&1 &'")
                os.system("ssh " + pi_client + " 'xinit /usr/bin/midori -a " + ip_address + ":" + parser.get('server_parameters','port') + "/ </dev/null > log 2>&1 &'")
            except:
                print("Problem starting pi!")

    application.listen(int(parser.get('server_parameters','port')))
    try:
        tornado.ioloop.IOLoop.instance().start()
    except (KeyboardInterrupt, SystemExit):
        print('\nProgram shutting down...')
        print('Saving state...')
        with open('state.json','w') as f:
            f.write(json.dumps(state,indent=2))

        for pi_client in pi_clients:
            pi_client = pi_client.strip()
            print('Shutting down ' + pi_client + '...')
            try:
                os.system("ssh " + pi_client + " 'pkill -9 midori </dev/null > log 2>&1 &'")
            except:
                pass
        print('Stopping timers...')
        try:
            songStopTimer.cancel()
        except:
            pass
        try:
            songStartTimer.cancel()
        except:
            pass
        print('Exiting...')
        sys.exit(-1)
        raise
<commit_msg>commented out ffmpeg<commit_after># Import core packages
import time
import sys
import os
import fnmatch
import random
import sys
import socket
import shutil
from threading import Timer
from configparser import SafeConfigParser
import json

# Setup logging
import logging
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

# Import 3rd-part packages
import tornado.ioloop
import tornado.web
from jinja2 import Template
from mutagen.mp3 import MP3
from mutagen.id3 import ID3

#####################
# GLOBAL VARIABLES
#####################

state = {}
songStartTimer = None
songStopTimer = None
audio_data = None
parser = SafeConfigParser()
try:
    parser.read('config.cfg')
except:
    print("Problem parsing config.cfg - did you change something?")
    sys.exit(-1)

#####################
# UTILITY FUNCTIONS
#####################

def getTime():
    """Returns time in milliseconds, similar to Javascript"""

    return int(time.time() * 1000)

def getPlaylistHtml():
    """Returns HTML for the playlist"""

    playlist_html = ""
    for i,path in enumerate(state['ordering']):
        cur_song = state['playlist'][path]['song_name']
        html = """%(song)s
"""
        if state['currently_playing_songname'] == cur_song:
            song = "" + cur_song + ""
        else:
            song = cur_song
        playlist_html += html % {'i': str(i), 'song': song}
    return playlist_html

def songStarts():
    """Runs when server decides a song starts"""
    logger = logging.getLogger('syncmusic:songStarts')
    logger.debug('Playing: ' + state['currently_playing_songname'])
    state['is_playing'] = True

def songOver():
    """Runs when server decides a song stops"""
    logger = logging.getLogger('syncmusic:songOver')
    logger.debug('Done playing: ' + state['currently_playing_songname'])
    state['is_playing'] = False
    nextSong(int(parser.get('server_parameters','time_to_next_song')), -1)

def nextSong(delay, skip):
    """ Main song control

    Sets global flags for which song is playing,
    loads the new song, and sets the timers for when
    the songs should start and end.
    """
    global songStartTimer
    global songStopTimer
    global audio_data
    logger = logging.getLogger('syncmusic:nextSong')
    if (time.time() - state['last_activated'] > int(parser.get('server_parameters','time_to_disallow_skips'))
            or not state['is_initialized']):
        state['is_initialized'] = True
        state['last_activated'] = time.time()
        state['is_playing'] = False

        if skip < 0:
            state['current_song'] += skip + 2
        else:
            state['current_song'] = skip
        if state['current_song'] >= len(state['ordering']):
            state['current_song'] = 0
        if state['current_song'] < 0:
            state['current_song'] = len(state['ordering']) - 1
        current_song_path = state['ordering'][state['current_song']]

        shutil.copy(current_song_path,os.path.join(os.getcwd(),'static/sound.mp3'))
        # shutil.copy(current_song_path,os.path.join(os.getcwd(),'static/start.mp3'))
        os.chdir('static')
        # try:
        #     os.remove('sound.mp3')
        # except:
        #     pass
        # os.system('ffmpeg -i start.mp3 -codec:a libmp3lame -qscale:a 6 sound.mp3')
        audio_data = open('sound.mp3','rb').read()
        os.chdir('../')
        state['currently_playing_songname'] = state['playlist'][current_song_path]['song_name']
        state['next_song_time'] = getTime() + delay * 1000
        logger.debug('next up: ' + state['currently_playing_songname'])
        logger.debug('time: ' + str(getTime()) +
                     ' and next: ' + str(state['next_song_time']))
        if songStartTimer is not None:
            songStartTimer.cancel()
            songStopTimer.cancel()
        songStopTimer = Timer(
            float(
                state['next_song_time'] -
                getTime()) /
            1000.0,
            songStarts,
            ())
        songStopTimer.start()
        audio = MP3('./static/sound.mp3')
        logger.debug(audio.info.length)
        songStartTimer = Timer(
            2 +
            float(
                audio.info.length) +
            float(
                state['next_song_time'] -
                getTime()) /
            1000.0,
            songOver,
            ())
        songStartTimer.start()

#################
# WEB ROUTES
#################

index_page = Template(open('templates/index.html','r').read())

class SoundMp3(tornado.web.RequestHandler):

    def get(self):
        self.write(audio_data)
        self.finish()

class IndexPage(tornado.web.RequestHandler):
    """Main sign-in - /

    Server loads new song if not initialized, and
    then returns the rendered music control page
    """

    def get(self):
        if not state['is_initialized']:
            nextSong(int(parser.get('server_parameters','time_to_next_song')), state['current_song'])
        data = {}
        data['random_integer'] = random.randint(1000, 30000)
        data['playlist_html'] = getPlaylistHtml()
        data['is_playing'] = state['is_playing']
        data['message'] = 'Syncing...'
        data['is_index'] = True
        data['max_sync_lag'] = parser.get('client_parameters','max_sync_lag')
        data['check_up_wait_time'] = parser.get('client_parameters','check_up_wait_time')
        if state['debug']:
            index_page = Template(open('templates/index.html','r').read())
        self.write(index_page.render(data=data))

class SyncHandler(tornado.web.RequestHandler):
    """Syncing route - /sync

    POST request from main page with the client client_timestamp
    and current_song. Returns JSON containing the server client_timestamp
    and whether or not to load a new song.
    """

    def post(self):
        data = {}
        data['client_timestamp'] = int(self.get_argument('client_timestamp'))
        data['server_timestamp'] = getTime()
        data['mute_button_clicked'] = json.loads(self.get_argument('mute_button_clicked'))
        if data['mute_button_clicked']:
            print("MUTE BUTTON CLICKED")
            state['is_muted'] = json.loads(self.get_argument('is_muted'))
            state['last_muted'] = getTime()
        if getTime() - state['last_muted'] < 3000:
            data['mute_button_clicked'] = True
        data['is_muted'] = state['is_muted']
        data['next_song'] = state['next_song_time']
        if state['is_playing']:
            data['is_playing'] = (state['currently_playing_songname'] == self.get_argument('current_song'))
        else:
            data['is_playing'] = state['is_playing']
        data['current_song'] = state['currently_playing_songname']
        data['song_time'] = float(getTime() - state['next_song_time']) / 1000.0
        self.write(data)

class NextSongHandler(tornado.web.RequestHandler):
    """Syncing route - /sync

    POST request from main page with the client client_timestamp
    and current_song. Returns JSON containing the server client_timestamp
    and whether or not to load a new song.
    """

    def post(self):
        response = {'message': 'loading!'}
        skip = int(self.get_argument('skip'))
        nextSong(int(parser.get('server_parameters','time_to_next_song')), skip)
        self.write(response)

# Depreciated
# @app.route("/playing", methods=['GET', 'POST'])
# def playing():
#     """ Is playing route - /nextSong

#     POST request to tell server that client has started
#     playing a song. DEPRECATED.
#     """
#     global is_playing
#     response = {'message': 'loading!'}
#     if request.method == 'POST':
#         is_playing = True
#     return jsonify(response)

application = tornado.web.Application([
    (r"/", IndexPage),
    (r"/sync", SyncHandler),
    (r"/nextsong", NextSongHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': './static'}),
    (r"/sound.mp3", SoundMp3),
])

##########
# MAIN
##########

if __name__ == "__main__":
    """Load the playlist, or let user know that one needs to be loaded"""
    # app.run(host='0.0.0.0')
    logger = logging.getLogger('syncmusic:nextSong')
    cwd = os.getcwd()

    state['playlist'] = {}
    state['ordering'] = []
    state['current_song'] = 0
    state['currently_playing_songname'] = ""
    if os.path.isfile('state.json'):
        state = json.load(open('state.json','r'))
    state['last_activated'] = 0
    state['next_song_time'] = 0
    state['is_playing'] = False
    state['is_initialized'] = False
    state['last_activated'] = 0
    state['debug'] = True
    state['is_muted'] = False
    state['last_muted'] = 0

    folders_with_music = parser.get('server_parameters','music_folder').split(',')
    for folder_with_music in folders_with_music:
        # Load playlist
        folder_with_music = folder_with_music.strip()
        for root, dirnames, filenames in os.walk(folder_with_music):
            logger.debug('Collecting music from ' + root)
            for filename in fnmatch.filter(filenames, '*.mp3'):
                path = os.path.join(root, filename)
                if path in state['ordering']:
                    continue
                state['ordering'].append(path)
                state['playlist'][path] = {}
                title = filename
                artist = 'unknown'
                album = 'unknown'
                try:
                    audiofile = ID3(path)
                    try:
                        title = audiofile['TIT2'].text[0]
                    except:
                        pass
                    try:
                        artist = audiofile['TPE1'].text[0]
                    except:
                        pass
                    try:
                        album = audiofile['TALB'].text[0]
                    except:
                        pass
                except BaseException as e:
                    logger.error('Failed to get audio information for ' + path + ': '+ str(e))
                state['playlist'][path]['title'] = title
                state['playlist'][path]['artist'] = artist
                state['playlist'][path]['album'] = album
                state['playlist'][path]['song_name'] = album + ' - ' + title + ' by ' + artist
                os.chdir(cwd)

    if len(state['ordering']) == 0:
        print(
            "\n\nNo mp3s found.\nDid you specify a music folder in line 40 of config.cfg?\n\n")
        sys.exit(-1)

    os.chdir(cwd)
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("gmail.com",80))
        ip_address = s.getsockname()[0]
        s.close()
    except:
        ip_address = "127.0.0.1"

    print("\n\n" +"#" * 60)
    print("# Starting server with " + str(len(state['ordering'])) + " songs")
    print("# To use, open a browser to http://" + ip_address + ":"+ parser.get('server_parameters','port') + "")
    print("# To stop server, use Ctl + C")
    print("#" * 60 +"\n\n")

    pi_clients = []
    if len(parser.get('raspberry_pis','clients')) > 2 and ip_address != '127.0.0.1':
        pi_clients = parser.get('raspberry_pis','clients').split(',')
        for pi_client in pi_clients:
            pi_client = pi_client.strip()
            try:
                os.system("ssh " + pi_client + " 'pkill -9 midori </dev/null > log 2>&1 &'")
                os.system("ssh " + pi_client + " 'xinit /usr/bin/midori -a " + ip_address + ":" + parser.get('server_parameters','port') + "/ </dev/null > log 2>&1 &'")
            except:
                print("Problem starting pi!")

    application.listen(int(parser.get('server_parameters','port')))
    try:
        tornado.ioloop.IOLoop.instance().start()
    except (KeyboardInterrupt, SystemExit):
        print('\nProgram shutting down...')
        print('Saving state...')
        with open('state.json','w') as f:
            f.write(json.dumps(state,indent=2))

        for pi_client in pi_clients:
            pi_client = pi_client.strip()
            print('Shutting down ' + pi_client + '...')
            try:
                os.system("ssh " + pi_client + " 'pkill -9 midori </dev/null > log 2>&1 &'")
            except:
                pass
        print('Stopping timers...')
        try:
            songStopTimer.cancel()
        except:
            pass
        try:
            songStartTimer.cancel()
        except:
            pass
        print('Exiting...')
        sys.exit(-1)
        raise
<|endoftext|>


<commit_before># stardard library
import sys, os
import json

# external libraries
import numpy as np
from ray import imio, evaluate, morpho

def add_anything(a, b):
    return a + b

def write_synapse_to_vtk(neurons, coords, fn, im=None, t=(2,0,1), s=(1,-1,1),
        margin=None):
    """Output neuron shapes around pre- and post-synapse coordinates.

    The coordinate array is a (n+1) x m array, where n is the number of
    post-synaptic sites (fly neurons are polyadic) and m = neurons.ndim, the
    number of dimensions of the image.
    """
    coords = coords[:, t]*s
    neuron_ids = neurons[zip(*coords)]
    mean_coords = coords.mean(axis=0).astype(np.uint)
    neurons = get_box(neurons, mean_coords, margin)
    synapse_volume = reduce(add_anything,
        [(i+1)*(neurons==j) for i, j in enumerate(neuron_ids)])
    imio.write_vtk(synapse_volume, fn)
    if im is not None:
        im = get_box(im, mean_coords, margin)
        imio.write_vtk(im,
            os.path.join(os.path.dirname(fn), 'image.' + os.path.basename(fn)))

def get_box(a, coords, margin):
    """Obtain a box of size 2*margin+1 around coords in array a.

    Boxes close to the boundary are trimmed accordingly.
    """
    if margin is None:
        return a
    coords = np.array(coords)[np.newaxis, :]
    origin = np.zeros(coords.shape, dtype=int)
    shape = np.array(a.shape)[np.newaxis, :]
    topleft = np.concatenate((coords-margin, origin), axis=0).max(axis=0)
    bottomright = np.concatenate((coords+margin+1, shape), axis=0).min(axis=0)
    box = [slice(top, bottom) for top, bottom in zip(topleft, bottomright)]
    return a[box].copy()

def raveler_synapse_annotations_to_coords(fn):
    """Obtain pre- and post-synaptic coordinates from Raveler annotations."""
    with open(fn, 'r') as f:
        syn = json.load(f)
    tbars = [np.array(s['location']) for s in syn]
    posts = [np.array([p['location'] for p in s['partners']]) for s in syn]
    return [np.concatenate((t[np.newaxis, :], p), axis=0)
                                        for t, p in zip(tbars, posts)]

def write_all_synapses_to_vtk(neurons, list_of_coords, fn, im, t=(2,0,1),
        s=(1,-1,1), margin=None, single_pairs=True):
    for i, coords in enumerate(list_of_coords):
        if single_pairs:
            pre = coords[0]
            for j, post in enumerate(coords[1:]):
                pair_coords = np.concatenate(
                    (pre[np.newaxis, :], post[np.newaxis, :]), axis=0)
                fn = fn%(i, j)
                write_synapse_to_vtk(neurons, pair_coords, fn, im, t, s, margin)
        else:
            fn = fn%i
            write_synapse_to_vtk(neurons, coords, fn, im, t, s, margin)
<commit_msg>Read synapses from Raveler sessiondata file<commit_after># stardard library
import sys, os
import json

# external libraries
import numpy as np
from ray import imio, evaluate, morpho

def add_anything(a, b):
    return a + b

def write_synapse_to_vtk(neurons, coords, fn, im=None, t=(2,0,1), s=(1,-1,1),
        margin=None):
    """Output neuron shapes around pre- and post-synapse coordinates.

    The coordinate array is a (n+1) x m array, where n is the number of
    post-synaptic sites (fly neurons are polyadic) and m = neurons.ndim, the
    number of dimensions of the image.
    """
    coords = coords[:, t]*s
    neuron_ids = neurons[zip(*coords)]
    mean_coords = coords.mean(axis=0).astype(np.uint)
    neurons = get_box(neurons, mean_coords, margin)
    synapse_volume = reduce(add_anything,
        [(i+1)*(neurons==j) for i, j in enumerate(neuron_ids)])
    imio.write_vtk(synapse_volume, fn)
    if im is not None:
        im = get_box(im, mean_coords, margin)
        imio.write_vtk(im,
            os.path.join(os.path.dirname(fn), 'image.' + os.path.basename(fn)))

def all_postsynaptic_sites(synapses):
    tbars, posts = zip(*synapses)
    return list(it.chain(*posts))

def get_box(a, coords, margin):
    """Obtain a box of size 2*margin+1 around coords in array a.

    Boxes close to the boundary are trimmed accordingly.
    """
    if margin is None:
        return a
    coords = np.array(coords)[np.newaxis, :]
    origin = np.zeros(coords.shape, dtype=int)
    shape = np.array(a.shape)[np.newaxis, :]
    topleft = np.concatenate((coords-margin, origin), axis=0).max(axis=0)
    bottomright = np.concatenate((coords+margin+1, shape), axis=0).min(axis=0)
    box = [slice(top, bottom) for top, bottom in zip(topleft, bottomright)]
    return a[box].copy()

def synapses_from_raveler_session_data(fn):
    with open(fn) as f:
        d = pck.load(f)
    annots = d['annotations']['point']
    tbars = [a for a in annots if annots[a]['kind'] == 'T-bar']
    posts = [annots[a] for a in tbars]
    posts = [eval(p['value'].replace('false', 'False').replace('true', 'True'))
             for p in posts]
    posts = [p['partners'] for p in posts]
    posts = [map(lambda x: x[0], p) for p in posts]
    return zip(tbars, posts)

def raveler_synapse_annotations_to_coords(fn, output_format='pairs'):
    """Obtain pre- and post-synaptic coordinates from Raveler annotations."""
    with open(fn, 'r') as f:
        syn = json.load(f)
    tbars = [np.array(s['location']) for s in syn]
    posts = [np.array([p['location'] for p in s['partners']]) for s in syn]
    if output_format == 'pairs':
        return zip(tbars, posts)
    elif output_format == 'arrays':
        return [np.concatenate((t[np.newaxis, :], p), axis=0)
                                        for t, p in zip(tbars, posts)]

def write_all_synapses_to_vtk(neurons, list_of_coords, fn, im, t=(2,0,1),
        s=(1,-1,1), margin=None, single_pairs=True):
    for i, coords in enumerate(list_of_coords):
        if single_pairs:
            pre = coords[0]
            for j, post in enumerate(coords[1]):
                pair_coords = np.concatenate(
                    (pre[np.newaxis, :], post[np.newaxis, :]), axis=0)
                fn = fn%(i, j)
                write_synapse_to_vtk(neurons, pair_coords, fn, im, t, s, margin)
        else:
            fn = fn%i
            write_synapse_to_vtk(neurons, coords, fn, im, t, s, margin)
<|endoftext|>


<commit_before>from ckeditor.fields import RichTextField
from django.db import models
from django.contrib.auth.models import User
from django.contrib.admin import widgets

class Event(models.Model):
    start_time = models.DateTimeField(db_index=True, verbose_name="Start wydarzenia")
    end_time = models.DateTimeField(db_index=True, blank=True, null=True, verbose_name="Koniec wydarzenia")
    title = models.CharField(max_length=120, verbose_name="Tytuł")
    description = RichTextField(verbose_name="Opis wydarzenia")
    image = models.ImageField(upload_to='event/%Y/%m/%d/', blank=True, null=True, verbose_name="Plakat")
    place = models.CharField(max_length=120, verbose_name="Miejsce")
    #point = models.PointField(blank=True, null=True, verbose_name="Miejsce na mapie")
    orgs = models.ManyToManyField('Organizer', blank=True, related_name='events', verbose_name="Organizator")
    user = models.ForeignKey(User, blank=True, null=True, related_name='event', verbose_name='Użytkownik')

    categories = models.ManyToManyField('Category', blank=True, null=True, verbose_name="Kategoria")

    url = models.URLField(blank=True, null=True,verbose_name="Adres www")

    def __str__(self):
        return self.title

class Category(models.Model):
    COLORS = (
        ('primary', 'Niebieski'),
        ('success', 'Zielony'),
        ('info', 'Jasno niebieski'),
        ('warning', 'Żółty'),
        ('danger', 'Czerwony'),
    )
    name = models.CharField(max_length=100)
    color = models.CharField(max_length=16, choices=COLORS, default="primary")

    def __str__(self):
        return self.name

class Organizer(models.Model):
    name = models.CharField(max_length=200, verbose_name="Nazwa")
    logo = models.ImageField(upload_to='logo/%m/', blank=True, null=True, verbose_name="Logo")
    user = models.OneToOneField(User, blank=True, null=True, related_name='organizer', verbose_name="Użytkownik")

    def __str__(self):
        return self.name
<commit_msg>Category model plural name. Fix #45.<commit_after>from ckeditor.fields import RichTextField
from django.db import models
from django.contrib.auth.models import User

class Event(models.Model):
    start_time = models.DateTimeField(db_index=True, verbose_name="Start wydarzenia")
    end_time = models.DateTimeField(db_index=True, blank=True, null=True, verbose_name="Koniec wydarzenia")
    title = models.CharField(max_length=120, verbose_name="Tytuł")
    description = RichTextField(verbose_name="Opis wydarzenia")
    image = models.ImageField(upload_to='event/%Y/%m/%d/', blank=True, null=True, verbose_name="Plakat")
    place = models.CharField(max_length=120, verbose_name="Miejsce")
    #point = models.PointField(blank=True, null=True, verbose_name="Miejsce na mapie")
    orgs = models.ManyToManyField('Organizer', blank=True, related_name='events', verbose_name="Organizator")
    user = models.ForeignKey(User, blank=True, null=True, related_name='event', verbose_name='Użytkownik')

    categories = models.ManyToManyField('Category', blank=True, null=True, verbose_name="Kategoria")

    url = models.URLField(blank=True, null=True,verbose_name="Adres www")

    def __str__(self):
        return self.title

class Category(models.Model):

    class Meta:
        verbose_name_plural = 'Categories'

    COLORS = (
        ('primary', 'Niebieski'),
        ('success', 'Zielony'),
        ('info', 'Jasno niebieski'),
        ('warning', 'Żółty'),
        ('danger', 'Czerwony'),
    )
    name = models.CharField(max_length=100)
    color = models.CharField(max_length=16, choices=COLORS, default="primary")

    def __str__(self):
        return self.name

class Organizer(models.Model):
    name = models.CharField(max_length=200, verbose_name="Nazwa")
    logo = models.ImageField(upload_to='logo/%m/', blank=True, null=True, verbose_name="Logo")
    user = models.OneToOneField(User, blank=True, null=True, related_name='organizer', verbose_name="Użytkownik")

    def __str__(self):
        return self.name
<|endoftext|>


<commit_before>"""
 SlipStream Client
 =====
 Copyright (C) 2013 SixSq Sarl (sixsq.com)
 =====
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import os
import tempfile
from ConfigParser import SafeConfigParser
from ConfigParser import NoOptionError
from slipstream.exceptions import Exceptions
from slipstream import util

def getContextualizer():
    return LocalContextualizer()

class LocalContextualizer(object):
    """
    Example of local contextualisation file:
    [contextualization]
    nodename=<node-name>
    diid=<run-id>
    serviceurl=https://nuv.la
    username=<username>
    password=<password>
    """
    LOCAL_CONTEXTUALIZATION_FILENAME = 'slipstream.context'
    LOCAL_CONTEXTUALIZATION_LOCATIONS = [os.getcwd(), os.path.expanduser('~'),
                                         os.path.join(util.SLIPSTREAM_HOME, 'client', 'bin'),
                                         os.path.join(util.SLIPSTREAM_HOME, 'client', 'sbin'),
                                         tempfile.gettempdir()]
    SECTION = 'contextualization'

    def __init__(self):
        self.verboseLevel = 0
        self.configFile = None
        self._load()

    def getContextAsDict(self):
        return dict(self.parser.items(LocalContextualizer.SECTION))

    def _load(self):
        self.parser = self._getParser()

    def _getParser(self):
        parser = SafeConfigParser()
        self.configFile = self._getConfigFile()
        parser.read(self.configFile)
        return parser

    def _getConfigFile(self):
        for location in LocalContextualizer.LOCAL_CONTEXTUALIZATION_LOCATIONS:
            filename = os.path.join(location, LocalContextualizer.LOCAL_CONTEXTUALIZATION_FILENAME)
            if os.path.exists(filename):
                util.printDetail('Using local contextualization file: %s' % filename, self.verboseLevel)
                return filename
        raise Exceptions.ConfigurationError('Failed to find local contextualization file.')

    def __getattr__(self, key):
        return self._get(key)

    def _get(self, key):
        if key in self.__dict__:
            return self.__dict__[key]

        value = None
        try:
            value = self.parser.get(LocalContextualizer.SECTION, key)
        except NoOptionError:
            pass
        value = os.getenv('SLIPSTREAM_' + key.upper(), value)

        if not value:
            raise KeyError('Failed to find key %s in file %s' % (key, self.configFile))

        self.__setattr__(key, value)

        return value

    def set(self, key, value):
        self.parser.set(LocalContextualizer.SECTION, key, value)
        self.parser.write(open(self.configFile, 'w'))

    def __str__(self):
        output = '* %s:\n' % self.__class__.__name__
        for k, v in self.__dict__.items():
            output += '  %s = %s\n' % (str(k), str(v))
        return output
<commit_msg>Switch to RawConfigParser to handle correctly some complex usernames<commit_after>"""
 SlipStream Client
 =====
 Copyright (C) 2013 SixSq Sarl (sixsq.com)
 =====
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import os
import tempfile
from ConfigParser import RawConfigParser
from ConfigParser import NoOptionError
from slipstream.exceptions import Exceptions
from slipstream import util

def getContextualizer():
    return LocalContextualizer()

class LocalContextualizer(object):
    """
    Example of local contextualisation file:
    [contextualization]
    nodename=<node-name>
    diid=<run-id>
    serviceurl=https://nuv.la
    username=<username>
    password=<password>
    """
    LOCAL_CONTEXTUALIZATION_FILENAME = 'slipstream.context'
    LOCAL_CONTEXTUALIZATION_LOCATIONS = [os.getcwd(), os.path.expanduser('~'),
                                         os.path.join(util.SLIPSTREAM_HOME, 'client', 'bin'),
                                         os.path.join(util.SLIPSTREAM_HOME, 'client', 'sbin'),
                                         tempfile.gettempdir()]
    SECTION = 'contextualization'

    def __init__(self):
        self.verboseLevel = 0
        self.configFile = None
        self._load()

    def getContextAsDict(self):
        return dict(self.parser.items(LocalContextualizer.SECTION))

    def _load(self):
        self.parser = self._getParser()

    def _getParser(self):
        parser = RawConfigParser()
        self.configFile = self._getConfigFile()
        parser.read(self.configFile)
        return parser

    def _getConfigFile(self):
        for location in LocalContextualizer.LOCAL_CONTEXTUALIZATION_LOCATIONS:
            filename = os.path.join(location, LocalContextualizer.LOCAL_CONTEXTUALIZATION_FILENAME)
            if os.path.exists(filename):
                util.printDetail('Using local contextualization file: %s' % filename, self.verboseLevel)
                return filename
        raise Exceptions.ConfigurationError('Failed to find local contextualization file.')

    def __getattr__(self, key):
        return self._get(key)

    def _get(self, key):
        if key in self.__dict__:
            return self.__dict__[key]

        value = None
        try:
            value = self.parser.get(LocalContextualizer.SECTION, key)
        except NoOptionError:
            pass
        value = os.getenv('SLIPSTREAM_' + key.upper(), value)

        if not value:
            raise KeyError('Failed to find key %s in file %s' % (key, self.configFile))

        self.__setattr__(key, value)

        return value

    def set(self, key, value):
        self.parser.set(LocalContextualizer.SECTION, key, value)
        self.parser.write(open(self.configFile, 'w'))

    def __str__(self):
        output = '* %s:\n' % self.__class__.__name__
        for k, v in self.__dict__.items():
            output += '  %s = %s\n' % (str(k), str(v))
        return output
<|endoftext|>


<commit_before>"""Linked list in object-oriented style"""

class LinkedList:
    """Singly linked list realization

        Attributes:
            header - header entry, only contains link to first entry
            size - size of the list
        """

    def __init__(self):
        self.header = Entry(None, None)
        self.size = 0

    def __iter__(self):
        """Iterates through list items. Starts at first item ignoring the header

        Returns:
            Any: next list item
        """
        current = self.header
        while current.next_item is not None:
            current = current.next_item
            yield current.item

    def __getitem__(self, index):
        """Get list item by index
        Raises IndexError if index is out of bounds

        Args:
            index - item index

        Returns:
            Any: item at given index
        """
        if isinstance(index, slice):
            returned_list = LinkedList()
            for slice_index in range(*index.indices(self.size)):
                returned_list.add(self[slice_index])
            return returned_list
        else:
            if self.__bad_index(index):
                raise IndexError("Index out of bounds:", index)
            if index < 0:
                index += self.size
            current = self.header
            for entry_index in range(index + 1):
                current = current.next_item
            return current.item

    def __setitem__(self, key, value):
        """Set new value to list item
        Raises IndexError if index is out of bounds

        Args:
            key - item index
            value - item new value
        """
        if self.__bad_index(key):
            raise IndexError("Index out of bounds:", key)
        current = self.header
        for entry_index in range(key):
            current = current.next_item
        current.next_item = Entry(value, current.next_item.next_item)

    def __contains__(self, item):
        """Checks whether list contains item

        Args:
            item - checked item

        Returns:
            bool: True if list contains item, False otherwise
        """
        for entry in self:
            if entry == item:
                return True
        return False

    def __len__(self):
        """Returns length of the list

        Returns:
            int: length of the list
        """
        return self.size

    def __bool__(self):
        """Returns boolean value of the list

        Returns:
            bool: False if list is empty, False otherwise
        """
        return len(self) > 0

    def __str__(self):
        """Returns string representation of list

        Returns:
            str: list string representation
        """
        if self.size > 0:
            items = ''.join(str(item) + ', ' for item in self)[:-2]
        else:
            items = ''
        return '[{}]'.format(items)

    def __bad_index(self, index):
        """Checks whether index is acceptable

        Returns:
            bool: True if index is not acceptable, False otherwise
        """
        return not ((index >= -self.size) and (index < self.size))

    def add(self, item):
        """Adds item to the back of the list. Same as push_back

        Args:
            item - item value
        """
        current = self.header
        while current.next_item is not None:
            current = current.next_item
        new_entry = Entry(item, None)
        current.next_item = new_entry
        self.size += 1

    def remove(self, index):
        """Removes item at given index from the list
        Raises IndexError if index is out of bounds
        """
        if self.__bad_index(index):
            raise IndexError("Index out of bounds:", index)
        current = self.header
        for entry_index in range(index):
            current = current.next_item
        current.next_item = current.next_item.next_item

    def push_front(self, item):
        """Adds item to the front of the list

        Args:
            item - item value
        """
        self.header.next_item = Entry(item, self.header.next_item)

    def pop_front(self):
        """Removes item from the front of the list
        Raises IndexError if list is empty

        Returns:
            Any: original first item of the list
        """
        if self.size == 0:
            raise IndexError('pop from empty list')
        item = self[0]
        self.header.next_item = self.header.next_item.next_item
        self.size -= 1
        return item

    def push(self, item):
        """Adds item to the back of the list. Same as add

        Args:
            item - item value
        """
        self.add(item)

    def pop(self):
        """Removes last element from the list and returns its value
        Raises IndexError if list is empty

        Returns:
            Any: original last item of the list
        """
        if self.size == 0:
            raise IndexError('pop from empty list')
        item = self[-1]
        self.remove(self.size - 1)
        self.size -= 1
        return item

    def index(self, item):
        """If item is present is list returns its index
        Raises ValueError if item is not in the list

        Returns:
            int: Items index in the list
        """
        current_index = 0
        for list_item in self:
            if list_item == item:
                return current_index
            else:
                current_index += 1
        raise ValueError('{} is not in the list'.format(item))

class Entry:
    """Linked list entry

    Args:
        item - list item value
        next_item - link to the next list item

    Attributes:
        item - list item value
        next_item - link to the next list item
    """

    def __init__(self, item, next_item):
        self.item = item
        self.next_item = next_item

if __name__ == '__main__':
    pass
<commit_msg>linked list: add missed args documentation<commit_after>"""Linked list in object-oriented style"""

class LinkedList:
    """Singly linked list realization

        Attributes:
            header - header entry, only contains link to first entry
            size - size of the list
        """

    def __init__(self):
        self.header = Entry(None, None)
        self.size = 0

    def __iter__(self):
        """Iterates through list items. Starts at first item ignoring the header

        Returns:
            Any: next list item
        """
        current = self.header
        while current.next_item is not None:
            current = current.next_item
            yield current.item

    def __getitem__(self, index):
        """Get list item by index
        Raises IndexError if index is out of bounds

        Args:
            index - item index

        Returns:
            Any: item at given index
        """
        if isinstance(index, slice):
            returned_list = LinkedList()
            for slice_index in range(*index.indices(self.size)):
                returned_list.add(self[slice_index])
            return returned_list
        else:
            if self.__bad_index(index):
                raise IndexError("Index out of bounds:", index)
            if index < 0:
                index += self.size
            current = self.header
            for entry_index in range(index + 1):
                current = current.next_item
            return current.item

    def __setitem__(self, key, value):
        """Set new value to list item
        Raises IndexError if index is out of bounds

        Args:
            key - item index
            value - item new value
        """
        if self.__bad_index(key):
            raise IndexError("Index out of bounds:", key)
        current = self.header
        for entry_index in range(key):
            current = current.next_item
        current.next_item = Entry(value, current.next_item.next_item)

    def __contains__(self, item):
        """Checks whether list contains item

        Args:
            item - checked item

        Returns:
            bool: True if list contains item, False otherwise
        """
        for entry in self:
            if entry == item:
                return True
        return False

    def __len__(self):
        """Returns length of the list

        Returns:
            int: length of the list
        """
        return self.size

    def __bool__(self):
        """Returns boolean value of the list

        Returns:
            bool: False if list is empty, False otherwise
        """
        return len(self) > 0

    def __str__(self):
        """Returns string representation of list

        Returns:
            str: list string representation
        """
        if self.size > 0:
            items = ''.join(str(item) + ', ' for item in self)[:-2]
        else:
            items = ''
        return '[{}]'.format(items)

    def __bad_index(self, index):
        """Checks whether index is acceptable

        Args:
            index (int) - checked index

        Returns:
            bool: True if index is not acceptable, False otherwise
        """
        return not ((index >= -self.size) and (index < self.size))

    def add(self, item):
        """Adds item to the back of the list. Same as push_back

        Args:
            item - item value
        """
        current = self.header
        while current.next_item is not None:
            current = current.next_item
        new_entry = Entry(item, None)
        current.next_item = new_entry
        self.size += 1

    def remove(self, index):
        """Removes item at given index from the list
        Raises IndexError if index is out of bounds

        Args:
            index (int) - item index
        """
        if self.__bad_index(index):
            raise IndexError("Index out of bounds:", index)
        current = self.header
        for entry_index in range(index):
            current = current.next_item
        current.next_item = current.next_item.next_item

    def push_front(self, item):
        """Adds item to the front of the list

        Args:
            item - item value
        """
        self.header.next_item = Entry(item, self.header.next_item)

    def pop_front(self):
        """Removes item from the front of the list
        Raises IndexError if list is empty

        Returns:
            Any: original first item of the list
        """
        if self.size == 0:
            raise IndexError('pop from empty list')
        item = self[0]
        self.header.next_item = self.header.next_item.next_item
        self.size -= 1
        return item

    def push(self, item):
        """Adds item to the back of the list. Same as add

        Args:
            item - item value
        """
        self.add(item)

    def pop(self):
        """Removes last element from the list and returns its value
        Raises IndexError if list is empty

        Returns:
            Any: original last item of the list
        """
        if self.size == 0:
            raise IndexError('pop from empty list')
        item = self[-1]
        self.remove(self.size - 1)
        self.size -= 1
        return item

    def index(self, item):
        """If item is present is list returns its index
        Raises ValueError if item is not in the list

        Args:
            item (Any) - item to look for

        Returns:
            int: Items index in the list
        """
        current_index = 0
        for list_item in self:
            if list_item == item:
                return current_index
            else:
                current_index += 1
        raise ValueError('{} is not in the list'.format(item))

class Entry:
    """Linked list entry

    Args:
        item - list item value
        next_item - link to the next list item

    Attributes:
        item - list item value
        next_item - link to the next list item
    """

    def __init__(self, item, next_item):
        self.item = item
        self.next_item = next_item

if __name__ == '__main__':
    pass
<|endoftext|>