<commit_before>import math

objects = []
springs = []

def add_object(x, halfsize, rotation=0):
  objects.append([x, halfsize, rotation])

def add_spring(a, b, offset_a, offset_b, length, stiffness):
  springs.append([a, b, offset_a, offset_b, length, stiffness])

def robotA():
  add_object(x=[0.3, 0.25], halfsize=[0.15, 0.03])
  add_object(x=[0.2, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.3, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.3], halfsize=[0.005, 0.03])

  l = 0.12
  s = 15
  add_spring(0, 1, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 1, [-0.1, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.1, 0.00], [0.0, 0.0], l, s)
  # -1 means the spring is a joint
  add_spring(0, 4, [0.1, 0], [0, -0.05], -1, s)

  return objects, springs

def robotB():
  add_object(x=[0.3, 0.25], halfsize=[0.15, 0.03])
  add_object(x=[0.2, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.3, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.15], halfsize=[0.03, 0.02])

  l = 0.12
  s = 15
  add_spring(0, 1, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 1, [-0.1, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.1, 0.00], [0.0, 0.0], l, s)

  return objects, springs

l_thigh_init_ang = 10
l_calf_init_ang = -10
r_thigh_init_ang = 10
r_calf_init_ang = -10
initHeight = 0.15

hip_pos = [0.3, 0.5 + initHeight]
thigh_half_length = 0.11
calf_half_length = 0.11

foot_half_length = 0.08

def rotAlong(half_length, deg, center):
  ang = math.radians(deg)
  return [half_length*math.sin(ang) + center[0], -half_length*math.cos(ang) + center[1]]

half_hip_length = 0.08

def robotLeg():
  #hip
  add_object(hip_pos, halfsize=[0.08, half_hip_length])
  hip_end = [hip_pos[0], hip_pos[1] - (half_hip_length-0.01)]

  #left
  l_thigh_center = rotAlong(thigh_half_length, l_thigh_init_ang, hip_end)
  l_thigh_end = rotAlong(thigh_half_length * 2.0, l_thigh_init_ang, hip_end)
  add_object(l_thigh_center, halfsize=[0.02, thigh_half_length], rotation=math.radians(l_thigh_init_ang))
  add_object(rotAlong(calf_half_length, l_calf_init_ang, l_thigh_end), halfsize=[0.02, calf_half_length], rotation = math.radians(l_calf_init_ang))
  l_calf_end = rotAlong(2.0 * calf_half_length, l_calf_init_ang, l_thigh_end)
  add_object([l_calf_end[0] + foot_half_length, l_calf_end[1]], halfsize=[foot_half_length, 0.02])

  #right
  add_object(rotAlong(thigh_half_length, r_thigh_init_ang, hip_end), halfsize=[0.02, thigh_half_length], rotation=math.radians(r_thigh_init_ang))
  r_thigh_end = rotAlong(thigh_half_length * 2.0, r_thigh_init_ang, hip_end)
  add_object(rotAlong(calf_half_length, r_calf_init_ang, r_thigh_end), halfsize=[0.02, calf_half_length], rotation = math.radians(r_calf_init_ang))
  r_calf_end = rotAlong(2.0 * calf_half_length, r_calf_init_ang, r_thigh_end)
  add_object([r_calf_end[0] + foot_half_length, r_calf_end[1]], halfsize=[foot_half_length, 0.02])

  s = 200

  thigh_relax = 0.9
  leg_relax = 0.9
  foot_relax = 0.7

  thigh_stiff = 10
  leg_stiff = 20
  foot_stiff = 40

  #left springs
  add_spring(0, 1, [0,(half_hip_length-0.01)*0.5], [0,-thigh_half_length], thigh_relax*(2.0*thigh_half_length + 0.22), thigh_stiff)
  add_spring(1, 2, [0,thigh_half_length], [0,-thigh_half_length], leg_relax * 4.0* thigh_half_length, leg_stiff)
  add_spring(2, 3, [0,0], [foot_half_length,0], foot_relax*math.sqrt(pow(thigh_half_length,2)+pow(2.0*foot_half_length,2)), foot_stiff)

  add_spring(0, 1, [0, -(half_hip_length-0.01)], [0.0, thigh_half_length], -1, s)
  add_spring(1, 2, [0, -thigh_half_length], [0.0, thigh_half_length], -1, s)
  add_spring(2, 3, [0, -thigh_half_length], [-foot_half_length,0], -1, s)

  #right springs
  add_spring(0, 4, [0,(half_hip_length-0.01)*0.5], [0,-thigh_half_length], thigh_relax*(2.0*thigh_half_length + 0.22), thigh_stiff)
  add_spring(4, 5, [0,thigh_half_length], [0,-thigh_half_length], leg_relax * 4.0* thigh_half_length, leg_stiff)
  add_spring(5, 6, [0,0], [foot_half_length,0], foot_relax*math.sqrt(pow(thigh_half_length,2)+pow(2.0*foot_half_length,2)), foot_stiff)

  add_spring(0, 4, [0, -(half_hip_length-0.01)], [0.0, thigh_half_length], -1, s)
  add_spring(4, 5, [0, -thigh_half_length], [0.0, thigh_half_length], -1, s)
  add_spring(5, 6, [0, -thigh_half_length], [-foot_half_length,0], -1, s)

  return objects, springs

#robots = [robotA, robotB]
robots = [robotLeg]
<commit_msg>improved<commit_after>import math

objects = []
springs = []

def add_object(x, halfsize, rotation=0):
  objects.append([x, halfsize, rotation])

def add_spring(a, b, offset_a, offset_b, length, stiffness):
  springs.append([a, b, offset_a, offset_b, length, stiffness])

def robotA():
  add_object(x=[0.3, 0.25], halfsize=[0.15, 0.03])
  add_object(x=[0.2, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.3, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.3], halfsize=[0.005, 0.03])

  l = 0.12
  s = 15
  add_spring(0, 1, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 1, [-0.1, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.1, 0.00], [0.0, 0.0], l, s)
  # -1 means the spring is a joint
  add_spring(0, 4, [0.1, 0], [0, -0.05], -1, s)

  return objects, springs

def robotB():
  add_object(x=[0.3, 0.25], halfsize=[0.15, 0.03])
  add_object(x=[0.2, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.3, 0.15], halfsize=[0.03, 0.02])
  add_object(x=[0.4, 0.15], halfsize=[0.03, 0.02])

  l = 0.12
  s = 15
  add_spring(0, 1, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 1, [-0.1, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [-0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 2, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.03, 0.00], [0.0, 0.0], l, s)
  add_spring(0, 3, [0.1, 0.00], [0.0, 0.0], l, s)

  return objects, springs

l_thigh_init_ang = 10
l_calf_init_ang = -10
r_thigh_init_ang = 10
r_calf_init_ang = -10
initHeight = 0.15

hip_pos = [0.3, 0.5 + initHeight]
thigh_half_length = 0.11
calf_half_length = 0.11

foot_half_length = 0.08

def rotAlong(half_length, deg, center):
  ang = math.radians(deg)
  return [half_length*math.sin(ang) + center[0], -half_length*math.cos(ang) + center[1]]

half_hip_length = 0.08

def robotLeg():
  #hip
  add_object(hip_pos, halfsize=[0.06, half_hip_length])
  hip_end = [hip_pos[0], hip_pos[1] - (half_hip_length-0.01)]

  #left
  l_thigh_center = rotAlong(thigh_half_length, l_thigh_init_ang, hip_end)
  l_thigh_end = rotAlong(thigh_half_length * 2.0, l_thigh_init_ang, hip_end)
  add_object(l_thigh_center, halfsize=[0.02, thigh_half_length], rotation=math.radians(l_thigh_init_ang))
  add_object(rotAlong(calf_half_length, l_calf_init_ang, l_thigh_end), halfsize=[0.02, calf_half_length], rotation = math.radians(l_calf_init_ang))
  l_calf_end = rotAlong(2.0 * calf_half_length, l_calf_init_ang, l_thigh_end)
  add_object([l_calf_end[0] + foot_half_length, l_calf_end[1]], halfsize=[foot_half_length, 0.02])

  #right
  add_object(rotAlong(thigh_half_length, r_thigh_init_ang, hip_end), halfsize=[0.02, thigh_half_length], rotation=math.radians(r_thigh_init_ang))
  r_thigh_end = rotAlong(thigh_half_length * 2.0, r_thigh_init_ang, hip_end)
  add_object(rotAlong(calf_half_length, r_calf_init_ang, r_thigh_end), halfsize=[0.02, calf_half_length], rotation = math.radians(r_calf_init_ang))
  r_calf_end = rotAlong(2.0 * calf_half_length, r_calf_init_ang, r_thigh_end)
  add_object([r_calf_end[0] + foot_half_length, r_calf_end[1]], halfsize=[foot_half_length, 0.02])

  s = 200

  thigh_relax = 0.9
  leg_relax = 0.9
  foot_relax = 0.7

  thigh_stiff = 5
  leg_stiff = 20
  foot_stiff = 40

  #left springs
  add_spring(0, 1, [0,(half_hip_length-0.01)*0.4], [0,-thigh_half_length], thigh_relax*(2.0*thigh_half_length + 0.22), thigh_stiff)
  add_spring(1, 2, [0,thigh_half_length], [0,-thigh_half_length], leg_relax * 4.0* thigh_half_length, leg_stiff)
  add_spring(2, 3, [0,0], [foot_half_length,0], foot_relax*math.sqrt(pow(thigh_half_length,2)+pow(2.0*foot_half_length,2)), foot_stiff)

  add_spring(0, 1, [0, -(half_hip_length-0.01)], [0.0, thigh_half_length], -1, s)
  add_spring(1, 2, [0, -thigh_half_length], [0.0, thigh_half_length], -1, s)
  add_spring(2, 3, [0, -thigh_half_length], [-foot_half_length,0], -1, s)

  #right springs
  add_spring(0, 4, [0,(half_hip_length-0.01)*0.4], [0,-thigh_half_length], thigh_relax*(2.0*thigh_half_length + 0.22), thigh_stiff)
  add_spring(4, 5, [0,thigh_half_length], [0,-thigh_half_length], leg_relax * 4.0* thigh_half_length, leg_stiff)
  add_spring(5, 6, [0,0], [foot_half_length,0], foot_relax*math.sqrt(pow(thigh_half_length,2)+pow(2.0*foot_half_length,2)), foot_stiff)

  add_spring(0, 4, [0, -(half_hip_length-0.01)], [0.0, thigh_half_length], -1, s)
  add_spring(4, 5, [0, -thigh_half_length], [0.0, thigh_half_length], -1, s)
  add_spring(5, 6, [0, -thigh_half_length], [-foot_half_length,0], -1, s)

  return objects, springs

#robots = [robotA, robotB]
robots = [robotLeg]
<|endoftext|>


<commit_before>Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> my_var = 'some value'
>>> my_new_var = my_var * 2
>>> print my_new_var
some valuesome value
>>> print my_var
some value
>>>
<commit_msg>resubmission to test out github workflow<commit_after>Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> my_var = 'some value'
>>> my_new_var = my_var * 2
>>> print my_new_var
some valuesome value
>>> print my_var
some value
>>>

<|endoftext|>


<commit_before>from django.test import SimpleTestCase

from corehq.apps.export.const import USERNAME_TRANSFORM
from corehq.apps.export.models import TableConfiguration, ExportColumn, \
    ScalarItem, ExportRow
from corehq.apps.export.models.new import SplitExportColumn, MultipleChoiceItem, \
    Option, DocRow, RowNumberColumn, PathNode

class TableConfigurationTest(SimpleTestCase):
    def test_get_column(self):
        table_configuration = TableConfiguration(
            path=[PathNode(name='form', is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q1')
                        ],
                    )
                ),
                ExportColumn(
                    transforms=[USERNAME_TRANSFORM, "deid_id"],
                    item=ScalarItem(
                        path=[
                            PathNode(name="form"),
                            PathNode(name="user_id"),
                        ]
                    )
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q2')
                        ],
                    )
                ),
            ]
        )

        column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ],
            []
        )
        self.assertEqual(
            column.item.path,
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ]
        )

        column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='DoesNotExist')
            ],
            None
        )
        self.assertIsNone(column)

        # Verify that get_column ignores deid transforms
        column = table_configuration.get_column(
            [PathNode(name="form"), PathNode(name="user_id")],
            [USERNAME_TRANSFORM]
        )
        self.assertIsNotNone(column)

class TableConfigurationGetSubDocumentsTest(SimpleTestCase):
    def test_basic(self):

        table = TableConfiguration(path=[])
        self.assertEqual(
            table._get_sub_documents(
                {'foo': 'a'},
                0
            ),
            [
                DocRow(row=(0,), doc={'foo': 'a'})
            ]
        )

    def test_simple_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name="foo", is_repeat=True)]
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'foo': [
                        {'bar': 'a'},
                        {'bar': 'b'},
                    ]
                },
                0
            ),
            [
                DocRow(row=(0, 0), doc={'bar': 'a'}),
                DocRow(row=(0, 1), doc={'bar': 'b'})
            ]
        )

    def test_nested_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name='foo', is_repeat=True), PathNode(name='bar', is_repeat=True)],
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'foo': [
                        {
                            'bar': [
                                {'baz': 'a'},
                                {'baz': 'b'}
                            ],
                        },
                        {
                            'bar': [
                                {'baz': 'c'}
                            ],
                        },
                    ],
                },
                0
            ),
            [
                DocRow(row=(0, 0, 0), doc={'baz': 'a'}),
                DocRow(row=(0, 0, 1), doc={'baz': 'b'}),
                DocRow(row=(0, 1, 0), doc={'baz': 'c'}),
            ]
        )

    def test_single_iteration_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name='group1', is_repeat=False), PathNode(name='repeat1', is_repeat=True)],
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'group1': {
                        'repeat1': {
                            'baz': 'a'
                        },
                    }
                },
                0
            ),
            [
                DocRow(row=(0, 0), doc={'baz': 'a'}),
            ]
        )

class TableConfigurationGetRowsTest(SimpleTestCase):

    def test_simple(self):
        table_configuration = TableConfiguration(
            path=[],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q3')],
                    ),
                    selected=True,
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q1')],
                    ),
                    selected=True,
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q2')],
                    ),
                    selected=False,
                ),
            ]
        )
        submission = {
            "form": {
                "q1": "foo",
                "q2": "bar",
                "q3": "baz"
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [['baz', 'foo']]
        )

    def test_repeat(self):
        table_configuration = TableConfiguration(
            path=[PathNode(name="form", is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name="form"),
                            PathNode(name="repeat1", is_repeat=True),
                            PathNode(name="q1")
                        ],
                    ),
                    selected=True,
                ),
            ]
        )
        submission = {
            'form': {
                'repeat1': [
                    {'q1': 'foo'},
                    {'q1': 'bar'}
                ]
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [ExportRow(['foo']).data, ExportRow(['bar']).data]
        )

    def test_double_repeat(self):
        table_configuration = TableConfiguration(
            path=[
                PathNode(name="form", is_repeat=False),
                PathNode(name="repeat1", is_repeat=True),
                PathNode(name="group1", is_repeat=False),
                PathNode(name="repeat2", is_repeat=True),
            ],
            columns=[
                RowNumberColumn(
                    selected=True
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='group1'),
                            PathNode(name='repeat2', is_repeat=True),
                            PathNode(name='q1')
                        ],
                    ),
                    selected=True,
                ),
            ]
        )
        submission = {
            'form': {
                'repeat1': [
                    {
                        'group1': {
                            'repeat2': [
                                {'q1': 'foo'},
                                {'q1': 'bar'}
                            ]
                        }
                    },
                    {
                        'group1': {
                            'repeat2': [
                                {'q1': 'beep'},
                                {'q1': 'boop'}
                            ]
                        }
                    },
                ]
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [
                ["0.0.0", 0, 0, 0, 'foo'],
                ["0.0.1", 0, 0, 1, 'bar'],
                ["0.1.0", 0, 1, 0, 'beep'],
                ["0.1.1", 0, 1, 1, 'boop']
            ]
        )

class SplitColumnTest(SimpleTestCase):

    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=False
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(doc, [PathNode(name='form')]), [1, None, "b d"])

    def test_ignore_extas(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=True
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(doc, [PathNode(name="form")]), [1, None])

    def test_basic_get_headers(self):
        column = SplitExportColumn(
            label="Fruit",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=True
        )
        self.assertEqual(column.get_headers(), ["Fruit | Apple", "Fruit | Banana"])

    def test_get_headers_with_template_string(self):
        column = SplitExportColumn(
            label="Fruit - {option}",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=True
        )
        self.assertEqual(column.get_headers(), ["Fruit - Apple", "Fruit - Banana"])

    def test_get_headers_with_extras(self):
        column = SplitExportColumn(
            label="Fruit - {option}",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=False
        )
        self.assertEqual(column.get_headers(), ["Fruit - Apple", "Fruit - Banana", "Fruit - extra"])
<commit_msg>fix get_column test<commit_after>from django.test import SimpleTestCase

from corehq.apps.export.const import USERNAME_TRANSFORM
from corehq.apps.export.models import TableConfiguration, ExportColumn, \
    ScalarItem, ExportRow
from corehq.apps.export.models.new import SplitExportColumn, MultipleChoiceItem, \
    Option, DocRow, RowNumberColumn, PathNode

class TableConfigurationTest(SimpleTestCase):
    def test_get_column(self):
        table_configuration = TableConfiguration(
            path=[PathNode(name='form', is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q1')
                        ],
                    )
                ),
                ExportColumn(
                    transforms=[USERNAME_TRANSFORM, "deid_id"],
                    item=ScalarItem(
                        path=[
                            PathNode(name="form"),
                            PathNode(name="user_id"),
                        ]
                    )
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q2')
                        ],
                    )
                ),
            ]
        )

        column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ],
            []
        )
        self.assertEqual(
            column.item.path,
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ]
        )

        column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='DoesNotExist')
            ],
            []
        )
        self.assertIsNone(column)

        # Verify that get_column ignores deid transforms
        column = table_configuration.get_column(
            [PathNode(name="form"), PathNode(name="user_id")],
            [USERNAME_TRANSFORM]
        )
        self.assertIsNotNone(column)

class TableConfigurationGetSubDocumentsTest(SimpleTestCase):
    def test_basic(self):

        table = TableConfiguration(path=[])
        self.assertEqual(
            table._get_sub_documents(
                {'foo': 'a'},
                0
            ),
            [
                DocRow(row=(0,), doc={'foo': 'a'})
            ]
        )

    def test_simple_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name="foo", is_repeat=True)]
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'foo': [
                        {'bar': 'a'},
                        {'bar': 'b'},
                    ]
                },
                0
            ),
            [
                DocRow(row=(0, 0), doc={'bar': 'a'}),
                DocRow(row=(0, 1), doc={'bar': 'b'})
            ]
        )

    def test_nested_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name='foo', is_repeat=True), PathNode(name='bar', is_repeat=True)],
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'foo': [
                        {
                            'bar': [
                                {'baz': 'a'},
                                {'baz': 'b'}
                            ],
                        },
                        {
                            'bar': [
                                {'baz': 'c'}
                            ],
                        },
                    ],
                },
                0
            ),
            [
                DocRow(row=(0, 0, 0), doc={'baz': 'a'}),
                DocRow(row=(0, 0, 1), doc={'baz': 'b'}),
                DocRow(row=(0, 1, 0), doc={'baz': 'c'}),
            ]
        )

    def test_single_iteration_repeat(self):
        table = TableConfiguration(
            path=[PathNode(name='group1', is_repeat=False), PathNode(name='repeat1', is_repeat=True)],
        )
        self.assertEqual(
            table._get_sub_documents(
                {
                    'group1': {
                        'repeat1': {
                            'baz': 'a'
                        },
                    }
                },
                0
            ),
            [
                DocRow(row=(0, 0), doc={'baz': 'a'}),
            ]
        )

class TableConfigurationGetRowsTest(SimpleTestCase):

    def test_simple(self):
        table_configuration = TableConfiguration(
            path=[],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q3')],
                    ),
                    selected=True,
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q1')],
                    ),
                    selected=True,
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[PathNode(name='form'), PathNode(name='q2')],
                    ),
                    selected=False,
                ),
            ]
        )
        submission = {
            "form": {
                "q1": "foo",
                "q2": "bar",
                "q3": "baz"
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [['baz', 'foo']]
        )

    def test_repeat(self):
        table_configuration = TableConfiguration(
            path=[PathNode(name="form", is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name="form"),
                            PathNode(name="repeat1", is_repeat=True),
                            PathNode(name="q1")
                        ],
                    ),
                    selected=True,
                ),
            ]
        )
        submission = {
            'form': {
                'repeat1': [
                    {'q1': 'foo'},
                    {'q1': 'bar'}
                ]
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [ExportRow(['foo']).data, ExportRow(['bar']).data]
        )

    def test_double_repeat(self):
        table_configuration = TableConfiguration(
            path=[
                PathNode(name="form", is_repeat=False),
                PathNode(name="repeat1", is_repeat=True),
                PathNode(name="group1", is_repeat=False),
                PathNode(name="repeat2", is_repeat=True),
            ],
            columns=[
                RowNumberColumn(
                    selected=True
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='group1'),
                            PathNode(name='repeat2', is_repeat=True),
                            PathNode(name='q1')
                        ],
                    ),
                    selected=True,
                ),
            ]
        )
        submission = {
            'form': {
                'repeat1': [
                    {
                        'group1': {
                            'repeat2': [
                                {'q1': 'foo'},
                                {'q1': 'bar'}
                            ]
                        }
                    },
                    {
                        'group1': {
                            'repeat2': [
                                {'q1': 'beep'},
                                {'q1': 'boop'}
                            ]
                        }
                    },
                ]
            }
        }
        self.assertEqual(
            [row.data for row in table_configuration.get_rows(submission, 0)],
            [
                ["0.0.0", 0, 0, 0, 'foo'],
                ["0.0.1", 0, 0, 1, 'bar'],
                ["0.1.0", 0, 1, 0, 'beep'],
                ["0.1.1", 0, 1, 1, 'boop']
            ]
        )

class SplitColumnTest(SimpleTestCase):

    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=False
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(doc, [PathNode(name='form')]), [1, None, "b d"])

    def test_ignore_extas(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=True
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(doc, [PathNode(name="form")]), [1, None])

    def test_basic_get_headers(self):
        column = SplitExportColumn(
            label="Fruit",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=True
        )
        self.assertEqual(column.get_headers(), ["Fruit | Apple", "Fruit | Banana"])

    def test_get_headers_with_template_string(self):
        column = SplitExportColumn(
            label="Fruit - {option}",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=True
        )
        self.assertEqual(column.get_headers(), ["Fruit - Apple", "Fruit - Banana"])

    def test_get_headers_with_extras(self):
        column = SplitExportColumn(
            label="Fruit - {option}",
            item=MultipleChoiceItem(
                options=[Option(value='Apple'), Option(value='Banana')]
            ),
            ignore_unspecified_options=False
        )
        self.assertEqual(column.get_headers(), ["Fruit - Apple", "Fruit - Banana", "Fruit - extra"])
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Tinman Controller

Copyright (c) 2009, Gavin M. Roy
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the Insider Guides, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

__author__  = "Gavin M. Roy"
__email__   = "gavinmroy@gmail.com"
__date__    = "2009-11-10"
__appname__ = 'project.py'
__version__ = '0.3'

import logging
import multiprocessing
import optparse
import os
import signal
import sys
import tornado.httpserver
import tornado.ioloop
import tornado.locale
import tornado.web
import yaml

# List to hold the child processes
children = []

class Application(tornado.web.Application):

    def __init__(self, config):

        # Our main handler list
        handlers = []
        for handler in config['RequestHandlers']:

            # Split up our string containing the import and class
            p = config['RequestHandlers'][handler][1].split('.')

            # Handler must be in the format: foo.bar.baz where
            # foo is the import dir, bar is the file and baz is the class
            if len(p) != 3:
                logging.error('Import module name error')

            # Build the import string
            s = '.'.join(p[0:-1])
            # Import the module, getting the file from the __dict__
            logging.debug('Importing: %s.%s' % (s, p[-1:][0]))
            m = __import__(s, fromlist=['.'.join(p[1:-1])])

            # Get the handle to the class
            h = getattr(m,p[-1:][0])

            # Append our handle stack
            logging.debug('Appending handler for "%s": %s.%s' % (config['RequestHandlers'][handler][0], s, p[len(p)-1:][0]))
            handlers.append((config['RequestHandlers'][handler][0], h))

        # Get the dictionary from our YAML file
        settings = config['Application']

        # Set the app version from the version setting in this file
        settings['version'] = __version__

        # If we have a static_path
        if settings.has_key('static_path'):
            # Replace __base_path__ with the path this is running from
            settings['static_path'] = settings['static_path'].replace('__base_path__',
                                                                      os.path.dirname(os.path.realpath(__file__)))

        # If we specified the UI modules, we need to import it not pass a string
        if settings.has_key('ui_modules'):

            # Split up our string containing the import and class
            p = settings['ui_modules'].split('.')

            # Module must be in the format: foo.bar
            # foo is the import dir, bar is the file containing module classes
            if len(p) != 2:
                logging.error('Import module name error')

            # Import the module, getting the file from the __dict__
            m = __import__(settings['ui_modules']).__dict__[p[1]]

            # Assign the modules to the import
            settings['ui_modules'] = m

        # Create our Application for this process
        tornado.web.Application.__init__(self, handlers, **settings)

def runapp(config, port):

    try:
        http_server = tornado.httpserver.HTTPServer(Application(config),
                                                    no_keep_alive=config['HTTPServer']['no_keep_alive'])
        http_server.listen(port)
        tornado.ioloop.IOLoop.instance().start()

    except KeyboardInterrupt:
        shutdown()

    except Exception as out:
        logging.error(out)
        shutdown()

def shutdown():

    logging.debug('%s: shutting down' % __appname__)
    for child in children:
        try:
            if child.is_alive():
                logging.debug("%s: Terminating child: %s" % (__appname__, child.name))
                child.terminate()
        except AssertionError:
            logging.error('%s: Dead child encountered' % __appname__)

    logging.debug('%s: shutdown complete' % __appname__)
    sys.exit(0)

def signal_handler(sig, frame):

    # We can shutdown from sigterm or keyboard interrupt so use a generic function
    shutdown()

if __name__ == "__main__":

    usage = "usage: %prog -c <configfile> [options]"
    version_string = "%%prog %s" % __version__
    description = "Project Name"

    # Create our parser and setup our command line options
    parser = optparse.OptionParser(usage=usage,
                         version=version_string,
                         description=description)

    parser.add_option("-c", "--config",
                        action="store", dest="config",
                        help="Specify the configuration file for use")

    parser.add_option("-f", "--foreground",
                        action="store_true", dest="foreground", default=False,
                        help="Run interactively in console for debugging purposes")

    # Parse our options and arguments
    options, args = parser.parse_args()

    if options.config is None:
        sys.stderr.write('Missing configuration file\n')
        print usage
        sys.exit(1)

    # try to load the config file.
    try:
        stream = file(options.config, 'r')
        config = yaml.load(stream)
        stream.close()
    except IOError:
        sys.stderr.write('Invalid or missing configuration file "%s"\n' % options.config)
        sys.exit(1)

    # Set logging levels dictionary
    logging_levels = {
                        'debug':    logging.DEBUG,
                        'info':     logging.INFO,
                        'warning':  logging.WARNING,
                        'error':    logging.ERROR,
                        'critical': logging.CRITICAL
                     }

    # Get the logging value from the dictionary
    logging_level = config['Logging']['level']
    config['Logging']['level'] = logging_levels.get( config['Logging']['level'],
                                                     logging.NOTSET )

    # If the user says verbose overwrite the settings.
    if options.foreground:

        # Set the debugging level to verbose
        config['Logging']['level'] = logging.DEBUG

        # If we have specified a file, remove it so logging info goes to stdout
        if config['Logging'].has_key('filename'):
            del config['Logging']['filename']

    else:
        # Build a specific path to our log file
        if config['Logging'].has_key('filename'):
            config['Logging']['filename'] = os.path.join( os.path.dirname(__file__),
                                                          config['Logging']['directory'],
                                                          config['Logging']['filename'] )

    # Pass in our logging config
    logging.basicConfig(**config['Logging'])
    logging.info('Log level set to %s' % logging_level)

    # If we have supported handler
    if config['Logging'].has_key('handler'):

        # If we want to syslog
        if config['Logging']['handler'] == 'syslog':

            from logging.handlers import SysLogHandler

            if SysLogHandler.facility_names.has_key(config['Logging']['syslog']['facility']):

                # Create the syslog handler
                logging_handler = SysLogHandler( address=config['Logging']['syslog']['address'],
                                                 facility = SysLogHandler.facility_names[config['Logging']['syslog']['facility']] )

                # Add the handler
                logger = logging.getLogger()
                logger.addHandler(logging_handler)

            else:
                logging.error('%s: Invalid SysLog facility name specified, syslog logging aborted' % __appname__)

    # Fork our process to detach if not told to stay in foreground
    if not options.foreground:
        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        # Second fork to put into daemon mode
        try:
            pid = os.fork()
            if pid > 0:
                # exit from second parent, print eventual PID before
                logging.info('%s: daemon has started - PID # %d.' % ( __appname__, pid ))
                sys.exit(0)
        except OSError, e:
            sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        # Detach from parent environment
        os.chdir(os.path.dirname(__file__))
        os.setsid()
        os.umask(0)

        # Close stdin
        sys.stdin.close()

        # Redirect stdout, stderr
        sys.stdout = open('/dev/null', 'a')
        sys.stderr = open('/dev/null', 'a')
    else:
        logging.info('%s: has started in interactive mode' % __appname__)

    # Load the locales
    logging.info('%s: Loading translations' % __appname__)
    try:
        tornado.locale.load_translations(os.path.join(os.path.dirname(__file__),
                                         "translations"))
    except OSError:
        logging.info('%s: No translations found' % __appname__)

    # Handle signals
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    # Kick off our application servers
    for port in config['HTTPServer']['ports']:
        logging.info('%s: spawning on port %i' % (__appname__, port))
        proc = multiprocessing.Process(target=runapp, args=(config, port))
        proc.start()
        children.append(proc)
<commit_msg>Caught a couple of other list slicing errors.<commit_after>#!/usr/bin/env python
"""
Tinman Controller

Copyright (c) 2009, Gavin M. Roy
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the Insider Guides, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

__author__  = "Gavin M. Roy"
__email__   = "gavinmroy@gmail.com"
__date__    = "2009-11-10"
__appname__ = 'project.py'
__version__ = '0.3'

import logging
import multiprocessing
import optparse
import os
import signal
import sys
import tornado.httpserver
import tornado.ioloop
import tornado.locale
import tornado.web
import yaml

# List to hold the child processes
children = []

class Application(tornado.web.Application):

    def __init__(self, config):

        # Our main handler list
        handlers = []
        for handler in config['RequestHandlers']:

            # Split up our string containing the import and class
            p = config['RequestHandlers'][handler][1].split('.')

            # Handler must be in the format: foo.bar.baz where
            # foo is the import dir, bar is the file and baz is the class
            if len(p) != 3:
                logging.error('Import module name error')

            # Build the import string
            s = '.'.join(p[0:-1])
            # Import the module, getting the file from the __dict__
            logging.debug('Importing: %s.%s' % (s, p[-1]))
            m = __import__(s, fromlist=['.'.join(p[1:-1])])

            # Get the handle to the class
            h = getattr(m,p[-1])

            # Append our handle stack
            logging.debug('Appending handler for "%s": %s.%s' % (config['RequestHandlers'][handler][0], s, p[len(p)-1:][0]))
            handlers.append((config['RequestHandlers'][handler][0], h))

        # Get the dictionary from our YAML file
        settings = config['Application']

        # Set the app version from the version setting in this file
        settings['version'] = __version__

        # If we have a static_path
        if settings.has_key('static_path'):
            # Replace __base_path__ with the path this is running from
            settings['static_path'] = settings['static_path'].replace('__base_path__',
                                                                      os.path.dirname(os.path.realpath(__file__)))

        # If we specified the UI modules, we need to import it not pass a string
        if settings.has_key('ui_modules'):

            # Split up our string containing the import and class
            p = settings['ui_modules'].split('.')

            # Module must be in the format: foo.bar
            # foo is the import dir, bar is the file containing module classes
            if len(p) != 2:
                logging.error('Import module name error')

            # Import the module, getting the file from the __dict__
            m = __import__(settings['ui_modules']).__dict__[p[1]]

            # Assign the modules to the import
            settings['ui_modules'] = m

        # Create our Application for this process
        tornado.web.Application.__init__(self, handlers, **settings)

def runapp(config, port):

    try:
        http_server = tornado.httpserver.HTTPServer(Application(config),
                                                    no_keep_alive=config['HTTPServer']['no_keep_alive'])
        http_server.listen(port)
        tornado.ioloop.IOLoop.instance().start()

    except KeyboardInterrupt:
        shutdown()

    except Exception as out:
        logging.error(out)
        shutdown()

def shutdown():

    logging.debug('%s: shutting down' % __appname__)
    for child in children:
        try:
            if child.is_alive():
                logging.debug("%s: Terminating child: %s" % (__appname__, child.name))
                child.terminate()
        except AssertionError:
            logging.error('%s: Dead child encountered' % __appname__)

    logging.debug('%s: shutdown complete' % __appname__)
    sys.exit(0)

def signal_handler(sig, frame):

    # We can shutdown from sigterm or keyboard interrupt so use a generic function
    shutdown()

if __name__ == "__main__":

    usage = "usage: %prog -c <configfile> [options]"
    version_string = "%%prog %s" % __version__
    description = "Project Name"

    # Create our parser and setup our command line options
    parser = optparse.OptionParser(usage=usage,
                         version=version_string,
                         description=description)

    parser.add_option("-c", "--config",
                        action="store", dest="config",
                        help="Specify the configuration file for use")

    parser.add_option("-f", "--foreground",
                        action="store_true", dest="foreground", default=False,
                        help="Run interactively in console for debugging purposes")

    # Parse our options and arguments
    options, args = parser.parse_args()

    if options.config is None:
        sys.stderr.write('Missing configuration file\n')
        print usage
        sys.exit(1)

    # try to load the config file.
    try:
        stream = file(options.config, 'r')
        config = yaml.load(stream)
        stream.close()
    except IOError:
        sys.stderr.write('Invalid or missing configuration file "%s"\n' % options.config)
        sys.exit(1)

    # Set logging levels dictionary
    logging_levels = {
                        'debug':    logging.DEBUG,
                        'info':     logging.INFO,
                        'warning':  logging.WARNING,
                        'error':    logging.ERROR,
                        'critical': logging.CRITICAL
                     }

    # Get the logging value from the dictionary
    logging_level = config['Logging']['level']
    config['Logging']['level'] = logging_levels.get( config['Logging']['level'],
                                                     logging.NOTSET )

    # If the user says verbose overwrite the settings.
    if options.foreground:

        # Set the debugging level to verbose
        config['Logging']['level'] = logging.DEBUG

        # If we have specified a file, remove it so logging info goes to stdout
        if config['Logging'].has_key('filename'):
            del config['Logging']['filename']

    else:
        # Build a specific path to our log file
        if config['Logging'].has_key('filename'):
            config['Logging']['filename'] = os.path.join( os.path.dirname(__file__),
                                                          config['Logging']['directory'],
                                                          config['Logging']['filename'] )

    # Pass in our logging config
    logging.basicConfig(**config['Logging'])
    logging.info('Log level set to %s' % logging_level)

    # If we have supported handler
    if config['Logging'].has_key('handler'):

        # If we want to syslog
        if config['Logging']['handler'] == 'syslog':

            from logging.handlers import SysLogHandler

            if SysLogHandler.facility_names.has_key(config['Logging']['syslog']['facility']):

                # Create the syslog handler
                logging_handler = SysLogHandler( address=config['Logging']['syslog']['address'],
                                                 facility = SysLogHandler.facility_names[config['Logging']['syslog']['facility']] )

                # Add the handler
                logger = logging.getLogger()
                logger.addHandler(logging_handler)

            else:
                logging.error('%s: Invalid SysLog facility name specified, syslog logging aborted' % __appname__)

    # Fork our process to detach if not told to stay in foreground
    if not options.foreground:
        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        # Second fork to put into daemon mode
        try:
            pid = os.fork()
            if pid > 0:
                # exit from second parent, print eventual PID before
                logging.info('%s: daemon has started - PID # %d.' % ( __appname__, pid ))
                sys.exit(0)
        except OSError, e:
            sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        # Detach from parent environment
        os.chdir(os.path.dirname(__file__))
        os.setsid()
        os.umask(0)

        # Close stdin
        sys.stdin.close()

        # Redirect stdout, stderr
        sys.stdout = open('/dev/null', 'a')
        sys.stderr = open('/dev/null', 'a')
    else:
        logging.info('%s: has started in interactive mode' % __appname__)

    # Load the locales
    logging.info('%s: Loading translations' % __appname__)
    try:
        tornado.locale.load_translations(os.path.join(os.path.dirname(__file__),
                                         "translations"))
    except OSError:
        logging.info('%s: No translations found' % __appname__)

    # Handle signals
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    # Kick off our application servers
    for port in config['HTTPServer']['ports']:
        logging.info('%s: spawning on port %i' % (__appname__, port))
        proc = multiprocessing.Process(target=runapp, args=(config, port))
        proc.start()
        children.append(proc)
<|endoftext|>


<commit_before>import random

from lazysusan.plugins import CommandPlugin
from lazysusan.helpers import admin_or_moderator_required

class Echo(CommandPlugin):
    COMMANDS = {'/echo': 'echo'}

    def echo(self, message, data):
        """Repeat everything after /echo."""
        self.bot.reply(message, data)

class Shout(CommandPlugin):
    COMMANDS = {'/shout': 'shout'}

    @admin_or_moderator_required
    def shout(self, message, data):
        """Repeat everything after /shout, to the whole room"""
        self.bot.api.speak(message)

class Twerk(CommandPlugin):
    COMMANDS = {'/twerk': 'twerk'}

    def twerk(self, message, data):
        """Bot upvotes current track, lets you know it's hot"""
        message = 'Shakin\' dat ass!'
        self.bot.reply(message, data)
        self.bot.api.bop()

class Roll(CommandPlugin):
    COMMANDS = {'/roll': 'roll'}

    def roll(self, message, data):
        """Returns a random number between 1-10. Used for deciding who leaves
        the decks"""
        message = random.randint(1, 10)
        self.bot.reply(message, data)

#class Holler(CommandPlugin):
#    COMMANDS = {'/holler': 'holler'}
#
#    def holler(self, message, data):
#        userID = self.bot.getUserID()
#        message = 'Sgood' + '%s' % userID.name
#        self.bot.reply(message, data)

#class Boot(CommandPlugin):
#    COMMANDS = {'/boot': 'boot'}
#
#    @admin_or_moderator_required
#    def boot(self, message, data):
#        userID = self.bot.getUserID()
#        self.bot.api.boot(UserID)
<commit_msg>Added shout and roll<commit_after>import random

from lazysusan.plugins import CommandPlugin
from lazysusan.helpers import admin_or_moderator_required

class Echo(CommandPlugin):
    COMMANDS = {'/echo': 'echo'}

    def echo(self, message, data):
        """Repeat everything after /echo."""
        self.bot.reply(message, data)

class Shout(CommandPlugin):
    COMMANDS = {'/shout': 'shout'}

    @admin_or_moderator_required
    def shout(self, message, data):
        """Repeat everything after /shout, to the whole room"""
        self.bot.api.speak(message)

class Twerk(CommandPlugin):
    COMMANDS = {'/twerk': 'twerk'}

    def twerk(self, message, data):
        """Bot upvotes current track, lets you know it's hot"""
        message = 'Twerkin'
        self.bot.reply(message, data)
        self.bot.api.bop()

class Roll(CommandPlugin):
    COMMANDS = {'/roll': 'roll'}

    def roll(self, message, data):
        """Returns a random number between 1-10. Used for deciding who leaves
        the decks"""
        message = random.randint(1, 10)
        self.bot.reply(message, data)

#class Holler(CommandPlugin):
#    COMMANDS = {'/holler': 'holler'}
#
#    def holler(self, message, data):
#        userID = self.bot.getUserID()
#        message = 'Sgood' + '%s' % userID.name
#        self.bot.reply(message, data)

#class Boot(CommandPlugin):
#    COMMANDS = {'/boot': 'boot'}
#
#    @admin_or_moderator_required
#    def boot(self, message, data):
#        userID = self.bot.getUserID()
#        self.bot.api.boot(UserID)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
import OutflowCone as oc
import numpy as np

class Cone:
    """ Galactic wind outflow cone model.
    """
    def __init__(self, inc=0, PA=0, theta=60, r_in=0.0, r_out=5.0):
        """ Create a new outflow cone.

            Keywords:
                inc     --  The inclination of the cone with respect to the
                            line-of-sight.
                PA      --  The position angle of the cone with respect to
                            the line-of-sight.
                theta   --  The opening half-angle of the cone (degrees).
                r_in    --  The inner radius of the cone (kpc).
                r_out   --  The outer radius of the cone (kpc).
        """
        self.inc = inc
        self.PA = PA
        self.theta = theta
        self.r_in = r_in
        self.r_out = r_out

        self.positions = None

    def GenerateClouds(self, n_clouds, bicone=False, falloff=1, zero_z=False):
        """ Generate 'n' model clouds within the cone bounds.

            Arguments:
                n_clouds--

            Keywords:
                bicone  --  Create a single cone or a bi-cone. Default is False.
                falloff --  Radial density distribution exponent. Default is 1 for
                            a mass-conserving outflow (density goes as r^-2).
                            A value of 1/3 creates a constant-density profile.
                zero_z  --  r_in makes the z-height of the base of the cone non-zero.
                            Should the clouds all get translated down? (e.g. z -= r_in)

            Returns:
                None. Creates "positions" member variable, containing Cartesian
                position vectors for 'n' clouds, and "velocities" member variable,
                containing zero-velocity vectors for all clouds (ostensibly these
                are Cartesian as well).
        """
        self._n_clouds = n_clouds

        # Even spread in cos(theta) to avoid clustering at poles.
        theta_rad = np.radians(self.theta)
        if bicone:
            vs1 = np.random.random(self._n_clouds/2.) * \
                   (1-np.cos(theta_rad)) + np.cos(theta_rad)
            vs2 = -(np.random.random(self._n_clouds/2.) * \
                    (1-np.cos(theta_rad)) + np.cos(theta_rad))
            vs = np.concatenate((vs1, vs2))
        else:
            vs = np.random.random(self._n_clouds) * \
                    (1-np.cos(theta_rad)) + np.cos(theta_rad)
        thetas = np.arccos(vs)

        us = np.random.random(self._n_clouds)
        phis = us * np.pi * 2.0

        #falloff = 1 # 1/3 ~ constant density, 1/2 ~ 1/r fall-off, 1 ~ r^-2 fall-off
        rs = np.random.random(self._n_clouds)**falloff * \
                (self.r_out - self.r_in) + self.r_in

        # Still need to rotate them!
        self._sph_pts = np.vstack((rs, thetas, phis)).transpose()

        # Convert to Cartesian so we can rotate things around.
        self._cart_pts = oc.SphPosToCart(self._sph_pts, radians=True)

        if zero_z:
            #self._cart_pts[:,2] -= self.r_in * np.cos(theta_rad)
            self._cart_pts[:,2] -= self.r_in * \
                    (self._local_zs / self._local_rs)

        # Coord system will be:
        #   -Z-axis is LOS, making X go right and Y go up (+Z out of the page)
        # Rot in -X for inc (or rotate -inc in +X) and in Z for PA
        self.positions = oc.Rot(self._cart_pts, x=-self.inc, z=self.PA)

        self.velocities = np.zeros_like(self.positions)

    def SetLocalVels(self, vels, radians=True):#, coordinates='spherical'):
        """ Sets the velocities of the clouds in the galaxy/cone's frame,
            meaning the input velocities should be in r/theta/phi of the galaxy,
            and *without* inclination/PA taken into account. This function
            applies the inclination effects automatically.

            Arguments:
                vels    --  List/array of velocities to apply to the clouds.
                            Given velocities should be in (r, theta, phi) format.
                            List/array length must be equal to --- and assumed to
                            be in the same order as --- that of self.positions.

            Keywords:
                radians --  Are the passed theta/phi values in radians?

            Returns:
                None. self.velocities is set to the equivalent Cartesian
                velocities with inclination accounted for.
        """
        self._sph_vels = vels
        cart_vels = np.zeros_like(self._sph_pts)
        for i, pt in enumerate(self._sph_pts):
            cart_vels[i] = oc.SphVecToCart(pt, vels[i], radians=radians)

        cart_vels_rot = oc.Rot(cart_vels, x=-self.inc)
        self.velocities = cart_vels_rot

    def ProjectVels(self, LOS):
        """ Projects the 3D Cartesian velocities into the given line-of-sight.
            NOTE:   UNTESTED!  So far I do a lot of assuming that -z is the LOS,
                    but maybe this still works okay.
            NOTE2:  If you just want it projected into a Cartesian axis,
                    use self._vxs/_vys/_vzs to avoid all the dot products.

            Arguments:
                LOS --  3-element NumPy array representing the LOS vector.
                        NOTE: UNTESTED!  So far I do a lot of assuming that
                        -z is the LOS, but this still probably works okay.

            Returns:
                NumPy array of length == len(self.velocities), where each value
                is the velocity vector dotted into the given LOS.
        """
        return np.asarray([v.dot(LOS) for v in self.velocities])

    # Properties for nicely slicing the 2D array of positions and velocities
    # into lists for each coordinate.
    # TODO: Property for spherical-coord velocities.
    @property
    def _local_rs(self):
        return self._sph_pts[:,0]

    @property
    def _local_thetas(self):
        return self._sph_pts[:,1]

    @property
    def _local_phis(self):
        return self._sph_pts[:,2]

    @property
    def _xs(self):
        return self.positions[:,0]

    @property
    def _ys(self):
        return self.positions[:,1]

    @property
    def _zs(self):
        return self.positions[:,2]

    @property
    def _local_xs(self):
        return self._cart_pts[:,0]

    @property
    def _local_ys(self):
        return self._cart_pts[:,1]

    @property
    def _local_zs(self):
        return self._cart_pts[:,2]

    @property
    def _vxs(self):
        return self.velocities[:,0]
    @_vxs.setter
    def _vxs(self, vxs):
        if np.shape(vxs) == np.shape(self._vxs):
            self.velocities[:,0] = vxs
        else:
            raise Exception("Array is not the same length as self._vxs.")

    @property
    def _vys(self):
        return self.velocities[:,1]
    @_vys.setter
    def _vys(self, vys):
        if np.shape(vys) == np.shape(self._vys):
            self.velocities[:,1] = vys
        else:
            raise Exception("Array is not the same length as self._vys.")

    @property
    def _vzs(self):
        return self.velocities[:,2]
    @_vzs.setter
    def _vzs(self, vzs):
        if np.shape(vzs) == np.shape(self._vzs):
            self.velocities[:,2] = vzs
        else:
            raise Exception("Array is not the same length as self._vzs.")

    @property
    def _local_vrs(self):
        return self._sph_vels[:,0]

    @property
    def _local_vthetas(self):
        return self._sph_vels[:,1]

    @property
    def _local_vphis(self):
        return self._sph_vels[:,2]

if __name__ == "__main__":
    pass
<commit_msg>Separate keywords for flattening the cone and zeroing out its height.<commit_after>#!/usr/bin/env python3
import OutflowCone as oc
import numpy as np

class Cone:
    """ Galactic wind outflow cone model.
    """
    def __init__(self, inc=0, PA=0, theta=60, r_in=0.0, r_out=5.0):
        """ Create a new outflow cone.

            Keywords:
                inc     --  The inclination of the cone with respect to the
                            line-of-sight.
                PA      --  The position angle of the cone with respect to
                            the line-of-sight.
                theta   --  The opening half-angle of the cone (degrees).
                r_in    --  The inner radius of the cone (kpc).
                r_out   --  The outer radius of the cone (kpc).
        """
        self.inc = inc
        self.PA = PA
        self.theta = theta
        self.r_in = r_in
        self.r_out = r_out

        self.positions = None

    def GenerateClouds(self, n_clouds, bicone=False, falloff=1,
                       zero_z=False, flatten=False):
        """ Generate 'n' model clouds within the cone bounds.

            Arguments:
                n_clouds--

            Keywords:
                bicone  --  Create a single cone or a bi-cone. Default is False.
                falloff --  Radial density distribution exponent. Default is 1 for
                            a mass-conserving outflow (density goes as r^-2).
                            A value of 1/3 creates a constant-density profile.
                zero_z  --  r_in makes the z-height of the base of the cone non-zero.
                            Should the clouds all get translated down? (e.g. z -= r_in)
                flatten --  Keep the inner radius spherical? Or flatten it?

            Returns:
                None. Creates "positions" member variable, containing Cartesian
                position vectors for 'n' clouds, and "velocities" member variable,
                containing zero-velocity vectors for all clouds (ostensibly these
                are Cartesian as well).
        """
        self._n_clouds = n_clouds

        # Even spread in cos(theta) to avoid clustering at poles.
        theta_rad = np.radians(self.theta)
        if bicone:
            vs1 = np.random.random(self._n_clouds/2.) * \
                   (1-np.cos(theta_rad)) + np.cos(theta_rad)
            vs2 = -(np.random.random(self._n_clouds/2.) * \
                    (1-np.cos(theta_rad)) + np.cos(theta_rad))
            vs = np.concatenate((vs1, vs2))
        else:
            vs = np.random.random(self._n_clouds) * \
                    (1-np.cos(theta_rad)) + np.cos(theta_rad)
        thetas = np.arccos(vs)

        us = np.random.random(self._n_clouds)
        phis = us * np.pi * 2.0

        #falloff = 1 # 1/3 ~ constant density, 1/2 ~ 1/r fall-off, 1 ~ r^-2 fall-off
        rs = np.random.random(self._n_clouds)**falloff * \
                (self.r_out - self.r_in) + self.r_in

        # Still need to rotate them!
        self._sph_pts = np.vstack((rs, thetas, phis)).transpose()

        # Convert to Cartesian so we can rotate things around.
        self._cart_pts = oc.SphPosToCart(self._sph_pts, radians=True)

        if flatten:
            self._cart_pts[:,2] -= self.r_in * \
                    (self._local_zs / self._local_rs)
            if not zero_z:
                self._cart_pts[:,2] += self.r_in
        elif zero_z:
            self._cart_pts[:,2] -= self.r_in * np.cos(theta_rad)

        # Coord system will be:
        #   -Z-axis is LOS, making X go right and Y go up (+Z out of the page)
        # Rot in -X for inc (or rotate -inc in +X) and in Z for PA
        self.positions = oc.Rot(self._cart_pts, x=-self.inc, z=self.PA)

        self.velocities = np.zeros_like(self.positions)

    def SetLocalVels(self, vels, radians=True):#, coordinates='spherical'):
        """ Sets the velocities of the clouds in the galaxy/cone's frame,
            meaning the input velocities should be in r/theta/phi of the galaxy,
            and *without* inclination/PA taken into account. This function
            applies the inclination effects automatically.

            Arguments:
                vels    --  List/array of velocities to apply to the clouds.
                            Given velocities should be in (r, theta, phi) format.
                            List/array length must be equal to --- and assumed to
                            be in the same order as --- that of self.positions.

            Keywords:
                radians --  Are the passed theta/phi values in radians?

            Returns:
                None. self.velocities is set to the equivalent Cartesian
                velocities with inclination accounted for.
        """
        self._sph_vels = vels
        cart_vels = np.zeros_like(self._sph_pts)
        for i, pt in enumerate(self._sph_pts):
            cart_vels[i] = oc.SphVecToCart(pt, vels[i], radians=radians)

        cart_vels_rot = oc.Rot(cart_vels, x=-self.inc)
        self.velocities = cart_vels_rot

    def ProjectVels(self, LOS):
        """ Projects the 3D Cartesian velocities into the given line-of-sight.
            NOTE:   UNTESTED!  So far I do a lot of assuming that -z is the LOS,
                    but maybe this still works okay.
            NOTE2:  If you just want it projected into a Cartesian axis,
                    use self._vxs/_vys/_vzs to avoid all the dot products.

            Arguments:
                LOS --  3-element NumPy array representing the LOS vector.
                        NOTE: UNTESTED!  So far I do a lot of assuming that
                        -z is the LOS, but this still probably works okay.

            Returns:
                NumPy array of length == len(self.velocities), where each value
                is the velocity vector dotted into the given LOS.
        """
        return np.asarray([v.dot(LOS) for v in self.velocities])

    # Properties for nicely slicing the 2D array of positions and velocities
    # into lists for each coordinate.
    # TODO: Property for spherical-coord velocities.
    @property
    def _local_rs(self):
        return self._sph_pts[:,0]

    @property
    def _local_thetas(self):
        return self._sph_pts[:,1]

    @property
    def _local_phis(self):
        return self._sph_pts[:,2]

    @property
    def _xs(self):
        return self.positions[:,0]

    @property
    def _ys(self):
        return self.positions[:,1]

    @property
    def _zs(self):
        return self.positions[:,2]

    @property
    def _local_xs(self):
        return self._cart_pts[:,0]

    @property
    def _local_ys(self):
        return self._cart_pts[:,1]

    @property
    def _local_zs(self):
        return self._cart_pts[:,2]

    @property
    def _vxs(self):
        return self.velocities[:,0]
    @_vxs.setter
    def _vxs(self, vxs):
        if np.shape(vxs) == np.shape(self._vxs):
            self.velocities[:,0] = vxs
        else:
            raise Exception("Array is not the same length as self._vxs.")

    @property
    def _vys(self):
        return self.velocities[:,1]
    @_vys.setter
    def _vys(self, vys):
        if np.shape(vys) == np.shape(self._vys):
            self.velocities[:,1] = vys
        else:
            raise Exception("Array is not the same length as self._vys.")

    @property
    def _vzs(self):
        return self.velocities[:,2]
    @_vzs.setter
    def _vzs(self, vzs):
        if np.shape(vzs) == np.shape(self._vzs):
            self.velocities[:,2] = vzs
        else:
            raise Exception("Array is not the same length as self._vzs.")

    @property
    def _local_vrs(self):
        return self._sph_vels[:,0]

    @property
    def _local_vthetas(self):
        return self._sph_vels[:,1]

    @property
    def _local_vphis(self):
        return self._sph_vels[:,2]

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


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

#  python-holidays
#  ---------------
#  A fast, efficient Python library for generating country, province and state
#  specific sets of holidays on the fly. It aims to make determining whether a
#  specific date is a holiday as fast and flexible as possible.
#
#  Authors: dr-prodigy <maurizio.montel@gmail.com> (c) 2017-2022
#           ryanss <ryanssdev@icloud.com> (c) 2014-2017
#  Website: https://github.com/dr-prodigy/python-holidays
#  License: MIT (see LICENSE file)

from datetime import date

from dateutil.easter import easter
from dateutil.relativedelta import relativedelta as rd

from holidays.constants import TUE, SAT, SUN
from holidays.constants import JAN, FEB, MAR, APR, MAY, JUL, SEP, OCT, NOV, DEC
from holidays.holiday_base import HolidayBase

class Madagascar(HolidayBase):
    country = "MG"

    def __init__(self, **kwargs):
        # https://www.officeholidays.com/countries/madagascar
        # https://www.timeanddate.com/holidays/madagascar/
        HolidayBase.__init__(self, **kwargs)

def _populate(self, year):
        # Observed since 2000
        if year > 1946:
            self[date(year, 1, 1)] = "Taom-baovao"
            self[date(year, 3, 8)] = "Fetin'ny vehivavy"
            self[date(year, 3, 29)] = "Fetin'ny mahery fo"
            self[date(year, 11, 1)]= "Fetin'ny olo-masina"
            self[date(year, 12, 25)]= "Fetin'ny noely"
            self[easter(year)]="fetin'ny paska"
            self[easter(year)+rd(days=1)]="Alatsinain'ny paska"
            self[easter(year) + rd(days=49)] = "Pentekosta"
            self[easter(year) + rd(days=50)] = "Alatsinain'ny pentekosta"
            self[easter(year) + rd(days=39)] = "Fiakaran'ny Jesosy kristy tany an-danitra"
            self[date(year, 8, 15)] = "Fiakaran'ny Masina Maria tany an-danitra"

class MG(Madagascar):
    pass

class MDG(Madagascar):
    pass

<commit_msg>Update madagascar.py<commit_after># -*- coding: utf-8 -*-

#  python-holidays
#  ---------------
#  A fast, efficient Python library for generating country, province and state
#  specific sets of holidays on the fly. It aims to make determining whether a
#  specific date is a holiday as fast and flexible as possible.
#
#  Authors: dr-prodigy <maurizio.montel@gmail.com> (c) 2017-2022
#           ryanss <ryanssdev@icloud.com> (c) 2014-2017
#  Website: https://github.com/dr-prodigy/python-holidays
#  License: MIT (see LICENSE file)

from datetime import date

from dateutil.easter import easter
from dateutil.relativedelta import relativedelta as rd

from holidays.constants import TUE, SAT, SUN
from holidays.constants import JAN, FEB, MAR, APR, MAY, JUL, SEP, OCT, NOV, DEC
from holidays.holiday_base import HolidayBase

class Madagascar(HolidayBase):
    country = "MG"

    def __init__(self, **kwargs):
        # https://www.officeholidays.com/countries/madagascar
        # https://www.timeanddate.com/holidays/madagascar/
        HolidayBase.__init__(self, **kwargs)

def _populate(self, year):
        # Observed since 1947
        if year > 1946:
            self[date(year, 1, 1)] = "Taom-baovao"
            self[date(year, 3, 8)] = "Fetin'ny vehivavy"
            self[date(year, 3, 29)] = "Fetin'ny mahery fo"
            self[date(year, 11, 1)]= "Fetin'ny olo-masina"
            self[date(year, 12, 25)]= "Fetin'ny noely"
            self[easter(year)]="fetin'ny paska"
            self[easter(year)+rd(days=1)]="Alatsinain'ny paska"
            self[easter(year) + rd(days=49)] = "Pentekosta"
            self[easter(year) + rd(days=50)] = "Alatsinain'ny pentekosta"
            self[easter(year) + rd(days=39)] = "Fiakaran'ny Jesosy kristy tany an-danitra"
            self[date(year, 8, 15)] = "Fiakaran'ny Masina Maria tany an-danitra"

class MG(Madagascar):
    pass

class MDG(Madagascar):
    pass

<|endoftext|>


<commit_before>__author__ = 'mikeknowles'
""" This is the core script, its mission:
Retrieve genomes
This will require the user to download rMLST data
To sort rMLST results and remove closely related sequences
then to prepare the data for strain-specific probe idenification
"""
from argparse import ArgumentParser
from Bio import SeqIO
from textwrap import fill
from collections import defaultdict
import os, glob, GeneSeekr, shutil, json, time

def retriever(genomes, output):
    if not os.path.exists(output + "Genomes"):
        os.mkdir(output + "Genomes")
    for folders in glob.glob(genomes + "/*"):
        if os.path.exists(folders + "/Best_Assemblies"):
            for fasta in glob.glob(folders + "/Best_Assemblies/*"):
                shutil.copy(fasta, output + "Genomes")

def jsonUpGoer(jsonfile, markers, genomes, outdir):
    if os.path.isfile(jsonfile):
        genedict = json.load(open(jsonfile))
    else:
        genedict = GeneSeekr.blaster(markers, genomes, outdir, "USSpip")
        json.dump(genedict, open(jsonfile, 'w'), sort_keys=True, indent=4, separators=(',', ': '))
    return genedict

def compareType(TargetrMLST, nonTargetrMLST):
    typing = defaultdict(int)
    for genome in TargetrMLST:
        for gene in sorted(TargetrMLST[genome]):
            for nontarget in nonTargetrMLST:
                if nontarget not in typing:  # if nontarget genome not in typing dictionary then add it
                    typing[nontarget] = 0

def sorter(markers, genomes, outdir, target):
    '''Strip first allele off each locus to feed into geneseekr and return dictionary
    '''
    smallMLST = outdir + "rMLST/"
    if not os.path.exists(outdir + "Genomes/"):
        retriever(genomes, outdir)
    if not os.path.exists(outdir + "tmp/"):
        os.mkdir(outdir + "tmp/")
    genomes = outdir + "Genomes/"
    start = time.time()
    jsonfile = '%sgenedict.json' %  markers
    nonTargetrMLST = jsonUpGoer(jsonfile, markers, genomes, outdir)
    end = start - time.time()
    print "Elapsed time for rMLST is %ss with %ss per genome" % (end, end/len(genomes))
    if os.path.isdir(target):  # Determine if target is a folder
        targets = glob.glob(target + "*")
        targetjson = '%sgenedict.json' % target
    elif os.path.isfile(target):
        targets = target
        targetjson = '%sgenedict.json' % outdir
    else:
        print "The variable \"--targets\" is not a folder or file "
        return
    targetrMLST = jsonUpGoer(targetjson, targets, genomes, outdir)
    nTtyp = defaultdict(dict)
    Ttyp = defaultdict(dict)
    for genome in nonTargetrMLST:
        for gene in nonTargetrMLST[genome]:

#Parser for arguments
parser = ArgumentParser(description='Find Universal Strain-Specifc Probes')
parser.add_argument('--version', action='version', version='%(prog)s v0.5')
parser.add_argument('-o', '--output', required=True, help='Specify output directory')
parser.add_argument('-i', '--input', required=True, help='Specify input genome fasta folder')
parser.add_argument('-m', '--marker', required=True, help='Specify rMLST folder')
parser.add_argument('-t', '--target', required=True, help='Specify target genome or folder')
args = vars(parser.parse_args())

sorter(args['marker'], args['input'], args['output'])<commit_msg>fixed sorting<commit_after>__author__ = 'mikeknowles'
""" This is the core script, its mission:
Retrieve genomes
This will require the user to download rMLST data
To sort rMLST results and remove closely related sequences
then to prepare the data for strain-specific probe idenification
"""
from argparse import ArgumentParser
from Bio import SeqIO
from textwrap import fill
from collections import defaultdict
import os, glob, GeneSeekr, shutil, json, time

def retriever(genomes, output):
    if not os.path.exists(output + "Genomes"):
        os.mkdir(output + "Genomes")
    for folders in glob.glob(genomes + "/*"):
        if os.path.exists(folders + "/Best_Assemblies"):
            for fasta in glob.glob(folders + "/Best_Assemblies/*"):
                shutil.copy(fasta, output + "Genomes")

def jsonUpGoer(jsonfile, markers, genomes, outdir):
    start = time.time()
    if os.path.isfile(jsonfile):
        genedict = json.load(open(jsonfile))
    else:
        genedict = GeneSeekr.blaster(markers, genomes, outdir, "USSpip")
        json.dump(genedict, open(jsonfile, 'w'), sort_keys=True, indent=4, separators=(',', ': '))
    end = start - time.time()
    print "Elapsed time for rMLST is %ss with %ss per genome" % (end, end/len(genomes))
    return genedict

def compareType(TargetrMLST, nonTargetrMLST):
    typing = defaultdict(int)
    for genome in TargetrMLST:
        for gene in sorted(TargetrMLST[genome]):
            for nontarget in nonTargetrMLST:
                if nontarget not in typing:  # if nontarget genome not in typing dictionary then add it
                    typing[nontarget] = 0

def sorter(markers, genomes, outdir, target):
    '''Strip first allele off each locus to feed into geneseekr and return dictionary
    '''
    smallMLST = outdir + "rMLST/"
    if not os.path.exists(outdir + "Genomes/"):
        retriever(genomes, outdir)
    if not os.path.exists(outdir + "tmp/"):
        os.mkdir(outdir + "tmp/")
    genomes = outdir + "Genomes/"
    jsonfile = '%sgenedict.json' %  markers
    nonTargetrMLST = jsonUpGoer(jsonfile, markers, genomes, outdir)
    if os.path.isdir(target):  # Determine if target is a folder
        targets = glob.glob(target + "*")
        targetjson = '%sgenedict.json' % target
    elif os.path.isfile(target):
        targets = target
        targetjson = '%sgenedict.json' % outdir
    else:
        print "The variable \"--targets\" is not a folder or file "
        return
    targetrMLST = jsonUpGoer(targetjson, targets, genomes, outdir)

#Parser for arguments
parser = ArgumentParser(description='Find Universal Strain-Specifc Probes')
parser.add_argument('--version', action='version', version='%(prog)s v0.5')
parser.add_argument('-o', '--output', required=True, help='Specify output directory')
parser.add_argument('-i', '--input', required=True, help='Specify input genome fasta folder')
parser.add_argument('-m', '--marker', required=True, help='Specify rMLST folder')
parser.add_argument('-t', '--target', required=True, help='Specify target genome or folder')
args = vars(parser.parse_args())

sorter(args['marker'], args['input'], args['output'])<|endoftext|>


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

# name of the package(!).srv
from minibot.srv import *
import rospy

# for GPIO pin usage
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!")

##
## GPIO stuff
##
GPIO.setmode(GPIO.BCM) # use the GPIO names, _not_ the pin numbers on the board
# Raspberry Pi pin configuration:
# pins	    BCM   BOARD
ledPin    = 18 # pin 12

# GPIO setup
print "GPIO setup..."
GPIO.setup(ledPin, GPIO.OUT)
# LED off (low active!)
GPIO.output(ledPin, GPIO.HIGH)

# define a clean node exit
def my_exit():
  print "Shutting LED server down..."
  # GPIO cleanup
  GPIO.cleanup()
  print "Done."

# call this method on mode exit
rospy.on_shutdown(my_exit)

# handle_add_two_ints is called with instances of BatteryRequest and returns instances of BatteryResponse
# The request name comes directly from the .srv filename
def handle_led(req):
    # here is all the work done :)

    # LED on (low active!)
    GPIO.output(req.pin, GPIO.LOW)
    print "LED %s switched. Result: %s"%(req.pin, req.pin)

    # The name of the response comes directly from the .srv filename!
    return LedResponse(req.pin)

def led_server():
    rospy.init_node('led_server')

    # This declares a new service named battery with the Battery service type.
    s = rospy.Service('led', Led, handle_led)

    print "Ready to switch LEDs."
    rospy.spin()

if __name__ == "__main__":
    led_server()
<commit_msg>Added a GPIO HIGH,LOW list for setup<commit_after>#!/usr/bin/env python
# coding=utf-8

# name of the package(!).srv
from minibot.srv import *
import rospy

# for GPIO pin usage
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!")

##
## GPIO stuff
##
GPIO.setmode(GPIO.BCM) # use the GPIO names, _not_ the pin numbers on the board
# Raspberry Pi pin configuration:
# pins	    BCM   BOARD
ledPin    = 18 # pin 12

# setting these LOW at startup
# pinListLow = (ledPin)

# setting these HIGH at startup
pinListHigh = (ledPin)

# GPIO setup
print "GPIO setup..."
GPIO.setup(ledPin, GPIO.OUT)

# all LOW
# GPIO.output(pinListLow, GPIO.LOW)

# all HIGH
GPIO.output(pinListHigh, GPIO.HIGH)

# define a clean node exit
def my_exit():
  print "Shutting LED server down..."
  # GPIO cleanup
  GPIO.cleanup()
  print "Done."

# call this method on mode exit
rospy.on_shutdown(my_exit)

# handle_add_two_ints is called with instances of BatteryRequest and returns instances of BatteryResponse
# The request name comes directly from the .srv filename
def handle_led(req):
    # here is all the work done :)

    # LED on (low active!)
    GPIO.output(req.pin, GPIO.LOW)
    print "LED %s switched. Result: %s"%(req.pin, req.pin)

    # The name of the response comes directly from the .srv filename!
    return LedResponse(req.pin)

def led_server():
    rospy.init_node('led_server')

    # This declares a new service named battery with the Battery service type.
    s = rospy.Service('led', Led, handle_led)

    print "Ready to switch LEDs."
    rospy.spin()

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