<commit_before># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Utility class to build the Syzygy master BuildFactory's.

Based on gclient_factory.py and adds Syzygy-specific steps."""

from master.factory import gclient_factory
from master.factory import syzygy_commands

import config

# A list of unittests to run after each build.
_UNITTESTS = [
  'asan_rtl_unittests',
  'agent_common_unittests',
  'basic_block_entry_unittests',
  'block_graph_orderers_unittests',
  'block_graph_transforms_unittests',
  'block_graph_unittests',
  'common_unittests',
  'core_unittests',
  'coverage_unittests',
  'grinder_unittests',
  'instrument_unittests',
  'parse_unittests',
  'pdb_unittests',
  'pe_orderers_unittests',
  'pe_transforms_unittests',
  'pe_unittests',
  'playback_unittests',
  'profile_unittests',
  'protocol_unittests',
  'relink_unittests',
  'reorder_unittests',
  'rpc_client_lib_unittests',
  'rpc_service_unittests',
  'simulate_unittests',
  'wsdump_unittests',
]

class SyzygyFactory(gclient_factory.GClientFactory):
  """Encapsulates data and methods common to the Syzygy master.cfg files."""

  def __init__(self, build_dir, target_platform=None):
    self.target_platform = target_platform
    main = gclient_factory.GClientSolution(config.Master.syzygy_url + 'trunk',
                                           name='src')

    custom_deps_list = [main]
    if config.Master.syzygy_internal_url:
      internal = gclient_factory.GClientSolution(
                     config.Master.syzygy_internal_url,
                     name='syzygy')
      custom_deps_list.append(internal)

    gclient_factory.GClientFactory.__init__(self, build_dir, custom_deps_list,
                                            target_platform=target_platform)

  def SyzygyFactory(self, target='release', clobber=False, tests=None,
                    mode=None, slave_type='BuilderTester', options=None,
                    compile_timeout=1200, build_url=None, project=None,
                    factory_properties=None, target_arch=None,
                    official_release=False):
    factory = self.BaseFactory(factory_properties=factory_properties,
                               official_release=official_release)

    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)

    if official_release:
      # Compile the official_build project of the "all" solution for
      # official builds.
      syzygy_cmd_obj.AddCompileStep('../syzygy/build/all.sln;official_build')
    else:
      # Compile the build_all project of the Syzygy solution.
      syzygy_cmd_obj.AddCompileStep('../syzygy/syzygy.sln;build_all')

    # Run the unittests.
    for test_name in _UNITTESTS:
      syzygy_cmd_obj.AddAppVerifierGTestTestStep(test_name)

    if target == 'release':
      syzygy_cmd_obj.AddRandomizeChromeStep()
      syzygy_cmd_obj.AddBenchmarkChromeStep()

    if official_release:
      # Archive official build output.
      syzygy_cmd_obj.AddArchival()

    return factory

  def SyzygyCoverageFactory(self, target='release', clobber=False, tests=None,
                            mode=None, slave_type='BuilderTester', options=None,
                            compile_timeout=1200, build_url=None, project=None,
                            factory_properties=None, target_arch=None):
    """Generates the GYP solutions with "coverage=1", and performs code
    coverage reporting."""
    if not factory_properties:
      factory_properties = {}

    gclient_env = factory_properties.get('gclient_env', {})
    # Make sure gclient generates coverage-enabled projects.
    gclient_env['GYP_DEFINES'] = 'coverage=1'
    factory_properties['gclient_env'] = gclient_env

    factory = self.BaseFactory(factory_properties=factory_properties)

    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)

    # Compile everything. We need the grinder, the instrumenter and the
    # coverage agent as well as the unittests.
    syzygy_cmd_obj.AddCompileStep('../syzygy/syzygy.sln;build_all')

    # Then generate and upload a coverage report. We do this twice, once
    # using the MSVS tools and once using the Syzygy tools.
    syzygy_cmd_obj.AddGenerateCoverage()
    syzygy_cmd_obj.AddGenerateCoverage(use_syzygy=True)

    return factory

  def SyzygySmokeTestFactory(self, target=None, clobber=False, tests=None,
                             mode=None, slave_type='Tester',
                             options=None, compile_timeout=1200,
                             build_url=None, project=None,
                             factory_properties=None, target_arch=None):
    """Runs the smoke-test against the checked out version of the binaries."""
    factory = self.BaseFactory(slave_type=slave_type,
                               factory_properties=factory_properties)
    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)
    syzygy_cmd_obj.AddSmokeTest()
    return factory
<commit_msg>Remove GYP_DEFINES=coverage=1 from Coverage builder.<commit_after># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Utility class to build the Syzygy master BuildFactory's.

Based on gclient_factory.py and adds Syzygy-specific steps."""

from master.factory import gclient_factory
from master.factory import syzygy_commands

import config

# A list of unittests to run after each build.
_UNITTESTS = [
  'asan_rtl_unittests',
  'agent_common_unittests',
  'basic_block_entry_unittests',
  'block_graph_orderers_unittests',
  'block_graph_transforms_unittests',
  'block_graph_unittests',
  'common_unittests',
  'core_unittests',
  'coverage_unittests',
  'grinder_unittests',
  'instrument_unittests',
  'parse_unittests',
  'pdb_unittests',
  'pe_orderers_unittests',
  'pe_transforms_unittests',
  'pe_unittests',
  'playback_unittests',
  'profile_unittests',
  'protocol_unittests',
  'relink_unittests',
  'reorder_unittests',
  'rpc_client_lib_unittests',
  'rpc_service_unittests',
  'simulate_unittests',
  'wsdump_unittests',
]

class SyzygyFactory(gclient_factory.GClientFactory):
  """Encapsulates data and methods common to the Syzygy master.cfg files."""

  def __init__(self, build_dir, target_platform=None):
    self.target_platform = target_platform
    main = gclient_factory.GClientSolution(config.Master.syzygy_url + 'trunk',
                                           name='src')

    custom_deps_list = [main]
    if config.Master.syzygy_internal_url:
      internal = gclient_factory.GClientSolution(
                     config.Master.syzygy_internal_url,
                     name='syzygy')
      custom_deps_list.append(internal)

    gclient_factory.GClientFactory.__init__(self, build_dir, custom_deps_list,
                                            target_platform=target_platform)

  def SyzygyFactory(self, target='release', clobber=False, tests=None,
                    mode=None, slave_type='BuilderTester', options=None,
                    compile_timeout=1200, build_url=None, project=None,
                    factory_properties=None, target_arch=None,
                    official_release=False):
    factory = self.BaseFactory(factory_properties=factory_properties,
                               official_release=official_release)

    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)

    if official_release:
      # Compile the official_build project of the "all" solution for
      # official builds.
      syzygy_cmd_obj.AddCompileStep('../syzygy/build/all.sln;official_build')
    else:
      # Compile the build_all project of the Syzygy solution.
      syzygy_cmd_obj.AddCompileStep('../syzygy/syzygy.sln;build_all')

    # Run the unittests.
    for test_name in _UNITTESTS:
      syzygy_cmd_obj.AddAppVerifierGTestTestStep(test_name)

    if target == 'release':
      syzygy_cmd_obj.AddRandomizeChromeStep()
      syzygy_cmd_obj.AddBenchmarkChromeStep()

    if official_release:
      # Archive official build output.
      syzygy_cmd_obj.AddArchival()

    return factory

  def SyzygyCoverageFactory(self, target='release', clobber=False, tests=None,
                            mode=None, slave_type='BuilderTester', options=None,
                            compile_timeout=1200, build_url=None, project=None,
                            factory_properties=None, target_arch=None):
    factory = self.BaseFactory(factory_properties=factory_properties)

    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)

    # Compile everything. We need the grinder, the instrumenter and the
    # coverage agent as well as the unittests.
    syzygy_cmd_obj.AddCompileStep('../syzygy/syzygy.sln;build_all')

    # Then generate and upload a coverage report. We do this twice, once
    # using the MSVS tools and once using the Syzygy tools.
    syzygy_cmd_obj.AddGenerateCoverage()
    syzygy_cmd_obj.AddGenerateCoverage(use_syzygy=True)

    return factory

  def SyzygySmokeTestFactory(self, target=None, clobber=False, tests=None,
                             mode=None, slave_type='Tester',
                             options=None, compile_timeout=1200,
                             build_url=None, project=None,
                             factory_properties=None, target_arch=None):
    """Runs the smoke-test against the checked out version of the binaries."""
    factory = self.BaseFactory(slave_type=slave_type,
                               factory_properties=factory_properties)
    syzygy_cmd_obj = syzygy_commands.SyzygyCommands(factory,
                                                    target,
                                                    self._build_dir,
                                                    self.target_platform,
                                                    target_arch)
    syzygy_cmd_obj.AddSmokeTest()
    return factory
<|endoftext|>


<commit_before>from collections import OrderedDict

def group_activities(queryset):
    """
    Given a queryset of activity objects, will group them by actors and return an OrderedDict including:

    item: The original target item being acted upon (activity.content_object)
    actors: a list of all actors who have acted upon the target.
    actor_count: zero-indexed count of actors. Useful for "Joe and {{ actor_count }} others have..."
    verb: the item's verb string, to avoid extra lookups
    last_modified: the time the target was last acted upon.

    The string version of the target is also available as the dict key.
    """
    actions = OrderedDict()
    for item in queryset:
        if item.target not in actions.keys():
            actions[item.target] = {
                'item': item,
                'actors': [item.actor_name],
                'actor_count': 0,
                'verb': item.override_string if item.override_string else item.verb,
                'last_modified': item.timestamp
            }
        else: # item was previously added, but we need to update attributes
            if item.actor_name not in actions[item.target]['actors']:
                actions[item.target]['actors'].append(item.actor_name)
                actions[item.target]['actor_count'] += 1
            if actions[item.target]['last_modified'] < item.timestamp:
                actions[item.target]['last_modified'] = item.timestamp

    return actions
<commit_msg>ordering?<commit_after>from collections import OrderedDict

def group_activities(queryset):
    """
    Given a queryset of activity objects, will group them by actors and return an OrderedDict including:

    item: The original target item being acted upon (activity.content_object)
    actors: a list of all actors who have acted upon the target.
    actor_count: zero-indexed count of actors. Useful for "Joe and {{ actor_count }} others have..."
    verb: the item's verb string, to avoid extra lookups
    last_modified: the time the target was last acted upon.

    The string version of the target is also available as the dict key.
    """
    actions = OrderedDict()
    for item in queryset.order_by('target'):
        if item.target not in actions.keys():
            actions[item.target] = {
                'item': item,
                'actors': [item.actor_name],
                'actor_count': 0,
                'verb': item.override_string if item.override_string else item.verb,
                'last_modified': item.timestamp
            }
        else: # item was previously added, but we need to update attributes
            if item.actor_name not in actions[item.target]['actors']:
                actions[item.target]['actors'].append(item.actor_name)
                actions[item.target]['actor_count'] += 1
            if actions[item.target]['last_modified'] < item.timestamp:
                actions[item.target]['last_modified'] = item.timestamp

    return actions.order_by()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Graph diameter, radius, eccentricity and other properties.
"""
__author__ = "\n".join(['Aric Hagberg (hagberg@lanl.gov)',
                        'Dan Schult(dschult@colgate.edu)'])
#    Copyright (C) 2004-2015 by
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

__all__ = ['eccentricity', 'diameter', 'radius', 'periphery', 'center']

import networkx

def eccentricity(G, v=None, sp=None):
    """Return the eccentricity of nodes in G.

    The eccentricity of a node v is the maximum distance from v to
    all other nodes in G.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    v : node, optional
       Return value of specified node

    sp : dict of dicts, optional
       All pairs shortest path lengths as a dictionary of dictionaries

    Returns
    -------
    ecc : dictionary
       A dictionary of eccentricity values keyed by node.
    """
#    nodes=
#    nodes=[]
#    if v is None:                # none, use entire graph
#        nodes=G.nodes()
#    elif v in G:               # is v a single node
#        nodes=[v]
#    else:                      # assume v is a container of nodes
#        nodes=v
    order=G.order()

    e={}
    for n in G.nbunch_iter(v):
        if sp is None:
            length = dict(networkx.single_source_shortest_path_length(G, n))
            L = len(length)
        else:
            try:
                length = sp[n]
                L = len(length)
            except TypeError:
                raise networkx.NetworkXError('Format of "sp" is invalid.')
        if L != order:
            msg = "Graph not connected: infinite path length"
            raise networkx.NetworkXError(msg)

        e[n]=max(length.values())

    if v in G:
        return e[v]  # return single value
    else:
        return e

def diameter(G, e=None):
    """Return the diameter of the graph G.

    The diameter is the maximum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    d : integer
       Diameter of graph

    See Also
    --------
    eccentricity
    """
    if e is None:
        e=eccentricity(G)
    return max(e.values())

def periphery(G, e=None):
    """Return the periphery of the graph G.

    The periphery is the set of nodes with eccentricity equal to the diameter.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    p : list
       List of nodes in periphery
    """
    if e is None:
        e=eccentricity(G)
    diameter=max(e.values())
    p=[v for v in e if e[v]==diameter]
    return p

def radius(G, e=None):
    """Return the radius of the graph G.

    The radius is the minimum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    r : integer
       Radius of graph
    """
    if e is None:
        e=eccentricity(G)
    return min(e.values())

def center(G, e=None):
    """Return the center of the graph G.

    The center is the set of nodes with eccentricity equal to radius.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    c : list
       List of nodes in center
    """
    if e is None:
        e=eccentricity(G)
    # order the nodes by path length
    radius=min(e.values())
    p=[v for v in e if e[v]==radius]
    return p

<commit_msg>Clarifies error message in eccentricity<commit_after># -*- coding: utf-8 -*-
"""
Graph diameter, radius, eccentricity and other properties.
"""
__author__ = "\n".join(['Aric Hagberg (hagberg@lanl.gov)',
                        'Dan Schult(dschult@colgate.edu)'])
#    Copyright (C) 2004-2015 by
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

__all__ = ['eccentricity', 'diameter', 'radius', 'periphery', 'center']

import networkx

def eccentricity(G, v=None, sp=None):
    """Return the eccentricity of nodes in G.

    The eccentricity of a node v is the maximum distance from v to
    all other nodes in G.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    v : node, optional
       Return value of specified node

    sp : dict of dicts, optional
       All pairs shortest path lengths as a dictionary of dictionaries

    Returns
    -------
    ecc : dictionary
       A dictionary of eccentricity values keyed by node.
    """
#    nodes=
#    nodes=[]
#    if v is None:                # none, use entire graph
#        nodes=G.nodes()
#    elif v in G:               # is v a single node
#        nodes=[v]
#    else:                      # assume v is a container of nodes
#        nodes=v
    order=G.order()

    e={}
    for n in G.nbunch_iter(v):
        if sp is None:
            length = dict(networkx.single_source_shortest_path_length(G, n))
            L = len(length)
        else:
            try:
                length = sp[n]
                L = len(length)
            except TypeError:
                raise networkx.NetworkXError('Format of "sp" is invalid.')
        if L != order:
            if G.is_directed():
                msg = ('Found infinite path length because the digraph is not'
                       ' strongly connected')
            else:
                msg = ('Found infinite path length because the graph is not'
                       ' connected')
            raise networkx.NetworkXError(msg)

        e[n]=max(length.values())

    if v in G:
        return e[v]  # return single value
    else:
        return e

def diameter(G, e=None):
    """Return the diameter of the graph G.

    The diameter is the maximum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    d : integer
       Diameter of graph

    See Also
    --------
    eccentricity
    """
    if e is None:
        e=eccentricity(G)
    return max(e.values())

def periphery(G, e=None):
    """Return the periphery of the graph G.

    The periphery is the set of nodes with eccentricity equal to the diameter.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    p : list
       List of nodes in periphery
    """
    if e is None:
        e=eccentricity(G)
    diameter=max(e.values())
    p=[v for v in e if e[v]==diameter]
    return p

def radius(G, e=None):
    """Return the radius of the graph G.

    The radius is the minimum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    r : integer
       Radius of graph
    """
    if e is None:
        e=eccentricity(G)
    return min(e.values())

def center(G, e=None):
    """Return the center of the graph G.

    The center is the set of nodes with eccentricity equal to radius.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    c : list
       List of nodes in center
    """
    if e is None:
        e=eccentricity(G)
    # order the nodes by path length
    radius=min(e.values())
    p=[v for v in e if e[v]==radius]
    return p

<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 expandtab
"""
XMLRPC service that does math

Use this as an example of how to implement xmlrpc services
"""

def add(request, a, b):
    return a + b

def sub(request, a, b):
    return a - b

def test(request):
    return {'dicttest': "This is a test",
            'requestTest': request.getConfiguration()['blog_title'],
            'list test': [1, 2, 3, 'Go!'],
            'complex list/dict': ['hi', {'test': 'A test',
            'list':['list']}]}

def sureFireFault(request):
    raise IOError, 'This is why you should give up programming'

def register_xmlrpc_methods():
    return {'math.add': add,
            'math.sub': sub,
            'system.test': test,
            'system.fault': sureFireFault}

if __name__ == '__main__':
    from Pyblosxom.Request import Request
    from pprint import pprint
    request = Request()
    request.addConfiguration({'blog_title': 'RoughingIT'})

    pprint(test(request))
    pprint(add(request, 1, 2))
    pprint(sub(request, 1, 2))
    pprint(sureFireFault(request))

<commit_msg>Fixed to use the new methodology.<commit_after># vim: tabstop=4 shiftwidth=4 expandtab
"""
XMLRPC service that does math.

Use this as an example of how to implement xmlrpc services.
"""

def add(request, a, b):
    return a + b

def sub(request, a, b):
    return a - b

def test(request):
    return {'dicttest': "This is a test",
            'requestTest': request.getConfiguration()['blog_title'],
            'list test': [1, 2, 3, 'Go!'],
            'complex list/dict': ['hi', {'test': 'A test',
            'list':['list']}]}

def sureFireFault(request):
    raise IOError, 'This is why you should give up programming'

def cb_xmlrpc_register(args):
    args["methods"].update(
           {'math.add': add,
            'math.sub': sub,
            'system.test': test,
            'system.fault': sureFireFault})

    return args

if __name__ == '__main__':
    from Pyblosxom.Request import Request
    from pprint import pprint
    request = Request()
    request.addConfiguration({'blog_title': 'RoughingIT'})

    pprint(test(request))
    pprint(add(request, 1, 2))
    pprint(sub(request, 1, 2))
    pprint(sureFireFault(request))
<|endoftext|>


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

from runner.koan import *

class AboutIteration(Koan):

    def test_iterators_are_a_type(self):
        it = iter(range(1,6))

        total = 0

        for num in it:
            total += num

        self.assertEqual(15 , total)

    def test_iterating_with_next(self):
        stages = iter(['alpha','beta','gamma'])

        try:
            self.assertEqual('alpha', next(stages))
            next(stages)
            self.assertEqual('gamma', next(stages))
            next(stages)
        except StopIteration as ex:
            err_msg = 'Ran out of iterations'

        self.assertRegex(err_msg, 'Ran out of iterations')

    # ------------------------------------------------------------------

    def add_ten(self, item):
        return item + 10

    def test_map_transforms_elements_of_a_list(self):
        seq = [1, 2, 3]
        mapped_seq = list()

        mapping = map(self.add_ten, seq)

        self.assertNotEqual(list, mapping.__class__)
        self.assertEqual(map, mapping.__class__)
        # In Python 3 built in iterator funcs return iterable view objects
        # instead of lists

        for item in mapping:
            mapped_seq.append(item)

        self.assertEqual([11, 12, 13], mapped_seq)

        # Note, iterator methods actually return objects of iter type in
        # python 3. In python 2 map() would give you a list.

    def test_filter_selects_certain_items_from_a_list(self):
        def is_even(item):
            return (item % 2) == 0

        seq = [1, 2, 3, 4, 5, 6]
        even_numbers = list()

        for item in filter(is_even, seq):
            even_numbers.append(item)

        self.assertEqual(__, even_numbers)

    def test_just_return_first_item_found(self):
        def is_big_name(item):
            return len(item) > 4

        names = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
        name = None

        iterator = filter(is_big_name, names)
        try:
            name = next(iterator)
        except StopIteration:
            msg = 'Ran out of big names'

        self.assertEqual(__, name)

    # ------------------------------------------------------------------

    def add(self,accum,item):
        return accum + item

    def multiply(self,accum,item):
        return accum * item

    def test_reduce_will_blow_your_mind(self):
        import functools
        # As of Python 3 reduce() has been demoted from a builtin function
        # to the functools module.

        result = functools.reduce(self.add, [2, 3, 4])
        self.assertEqual(__, result.__class__)
        # Reduce() syntax is same as Python 2

        self.assertEqual(__, result)

        result2 = functools.reduce(self.multiply, [2, 3, 4], 1)
        self.assertEqual(__, result2)

        # Extra Credit:
        # Describe in your own words what reduce does.

    # ------------------------------------------------------------------

    def test_use_pass_for_iterations_with_no_body(self):
        for num in range(1,5):
            pass

        self.assertEqual(__, num)

    # ------------------------------------------------------------------

    def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
        # Ranges are an iterable sequence
        result = map(self.add_ten, range(1,4))
        self.assertEqual(__, list(result))

        try:
            file = open("example_file.txt")

            try:
                def make_upcase(line):
                    return line.strip().upper()
                upcase_lines = map(make_upcase, file.readlines())
                self.assertEqual(__, list(upcase_lines))
            finally:
                # Arg, this is ugly.
                # We will figure out how to fix this later.
                file.close()
        except IOError:
            # should never happen
            self.fail()
<commit_msg>space<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from runner.koan import *

class AboutIteration(Koan):

    def test_iterators_are_a_type(self):
        it = iter(range(1,6))

        total = 0

        for num in it:
            total += num

        self.assertEqual(15 , total)

    def test_iterating_with_next(self):
        stages = iter(['alpha ','beta','gamma'])

        try:
            self.assertEqual('alpha', next(stages))
            next(stages)
            self.assertEqual('gamma', next(stages))
            next(stages)
        except StopIteration as ex:
            err_msg = 'Ran out of iterations'

        self.assertRegex(err_msg, 'Ran out of iterations')

    # ------------------------------------------------------------------

    def add_ten(self, item):
        return item + 10

    def test_map_transforms_elements_of_a_list(self):
        seq = [1, 2, 3]
        mapped_seq = list()

        mapping = map(self.add_ten, seq)

        self.assertNotEqual(list, mapping.__class__)
        self.assertEqual(map, mapping.__class__)
        # In Python 3 built in iterator funcs return iterable view objects
        # instead of lists

        for item in mapping:
            mapped_seq.append(item)

        self.assertEqual([11, 12, 13], mapped_seq)

        # Note, iterator methods actually return objects of iter type in
        # python 3. In python 2 map() would give you a list.

    def test_filter_selects_certain_items_from_a_list(self):
        def is_even(item):
            return (item % 2) == 0

        seq = [1, 2, 3, 4, 5, 6]
        even_numbers = list()

        for item in filter(is_even, seq):
            even_numbers.append(item)

        self.assertEqual(__, even_numbers)

    def test_just_return_first_item_found(self):
        def is_big_name(item):
            return len(item) > 4

        names = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
        name = None

        iterator = filter(is_big_name, names)
        try:
            name = next(iterator)
        except StopIteration:
            msg = 'Ran out of big names'

        self.assertEqual(__, name)

    # ------------------------------------------------------------------

    def add(self,accum,item):
        return accum + item

    def multiply(self,accum,item):
        return accum * item

    def test_reduce_will_blow_your_mind(self):
        import functools
        # As of Python 3 reduce() has been demoted from a builtin function
        # to the functools module.

        result = functools.reduce(self.add, [2, 3, 4])
        self.assertEqual(__, result.__class__)
        # Reduce() syntax is same as Python 2

        self.assertEqual(__, result)

        result2 = functools.reduce(self.multiply, [2, 3, 4], 1)
        self.assertEqual(__, result2)

        # Extra Credit:
        # Describe in your own words what reduce does.

    # ------------------------------------------------------------------

    def test_use_pass_for_iterations_with_no_body(self):
        for num in range(1,5):
            pass

        self.assertEqual(__, num)

    # ------------------------------------------------------------------

    def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
        # Ranges are an iterable sequence
        result = map(self.add_ten, range(1,4))
        self.assertEqual(__, list(result))

        try:
            file = open("example_file.txt")

            try:
                def make_upcase(line):
                    return line.strip().upper()
                upcase_lines = map(make_upcase, file.readlines())
                self.assertEqual(__, list(upcase_lines))
            finally:
                # Arg, this is ugly.
                # We will figure out how to fix this later.
                file.close()
        except IOError:
            # should never happen
            self.fail()
<|endoftext|>


<commit_before>#
#    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.
# -*- coding: utf-8 -*-

import itertools

from docutils import nodes
import pydoc
from sphinx.util.compat import Directive

from heat.engine import attributes
from heat.engine import environment
from heat.engine import plugin_manager
from heat.engine import properties
from heat.engine import resources
from heat.engine import support
from heat.openstack.common.gettextutils import _

global_env = environment.Environment({}, user_env=False)

class resourcepages(nodes.General, nodes.Element):
    pass

class ResourcePages(Directive):
    has_content = False
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = False
    option_spec = {}

    def run(self):
        prefix = self.arguments and self.arguments.pop() or None
        content = []
        for resource_type, resource_class in _all_resources(prefix):
            self.resource_type = resource_type
            self.resource_class = resource_class
            section = self._section(content, resource_type, '%s')

            self.props_schemata = properties.schemata(
                self.resource_class.properties_schema)
            self.attrs_schemata = attributes.schemata(
                self.resource_class.attributes_schema)

            if resource_class.support_status.status == support.DEPRECATED:
                sstatus = resource_class.support_status.to_dict()
                msg = _('%(status)s')
                if sstatus['message'] is not None:
                    msg = _('%(status)s - %(message)s')
                para = nodes.inline('', msg % sstatus)
                warning = nodes.note('', para)
                section.append(warning)

            cls_doc = pydoc.getdoc(resource_class)
            if cls_doc:
                para = nodes.paragraph('', cls_doc)
                section.append(para)

            self.contribute_properties(section)
            self.contribute_attributes(section)

            self.contribute_hot_syntax(section)
            self.contribute_yaml_syntax(section)
            self.contribute_json_syntax(section)

        return content

    def _section(self, parent, title, id_pattern):
        id = id_pattern % self.resource_type
        section = nodes.section(ids=[id])
        parent.append(section)
        title = nodes.title('', title)
        section.append(title)
        return section

    def _prop_syntax_example(self, prop):
        if not prop:
            return 'Value'
        if prop.type == properties.Schema.LIST:
            schema = lambda i: prop.schema[i] if prop.schema else None
            sub_type = [self._prop_syntax_example(schema(i))
                        for i in range(2)]
            return '[%s, %s, ...]' % tuple(sub_type)
        elif prop.type == properties.Schema.MAP:
            def sub_props():
                for sub_key, sub_value in prop.schema.items():
                    if sub_value.implemented:
                        yield '"%s": %s' % (
                            sub_key, self._prop_syntax_example(sub_value))
            return '{%s}' % (', '.join(sub_props()) if prop.schema else '...')
        else:
            return prop.type

    def contribute_hot_syntax(self, parent):
        section = self._section(parent, _('HOT Syntax'), '%s-hot')
        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('%s: %s' % (prop_key,
                                         self._prop_syntax_example(prop)))

        template = '''heat_template_version: 2013-05-23
...
resources:
  ...
  the_resource:
    type: %s
    properties:
      %s''' % (self.resource_type, '\n      '.join(props))

        block = nodes.literal_block('', template)
        section.append(block)

    def contribute_yaml_syntax(self, parent):
        section = self._section(parent, _('YAML Syntax'), '%s-yaml')
        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('%s: %s' % (prop_key,
                                         self._prop_syntax_example(prop)))

        template = '''HeatTemplateFormatVersion: '2012-12-12'
...
Resources:
  ...
  TheResource:
    Type: %s
    Properties:
      %s''' % (self.resource_type, '\n      '.join(props))

        block = nodes.literal_block('', template)
        section.append(block)

    def contribute_json_syntax(self, parent):
        section = self._section(parent, _('JSON Syntax'), '%s-json')

        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('"%s": %s' % (prop_key,
                                           self._prop_syntax_example(prop)))
        template = '''{
  "AWSTemplateFormatVersion" : "2010-09-09",
  ...
  "Resources" : {
    "TheResource": {
      "Type": "%s",
      "Properties": {
        %s
      }
    }
  }
}''' % (self.resource_type, ',\n        '.join(props))
        block = nodes.literal_block('', template)
        section.append(block)

    @staticmethod
    def cmp_prop(x, y):
        x_key, x_prop = x
        y_key, y_prop = y
        if x_prop.support_status.status == y_prop.support_status.status:
            return cmp(x_key, y_key)
        if x_prop.support_status.status == support.SUPPORTED:
            return -1
        if x_prop.support_status.status == support.DEPRECATED:
            return 1
        return cmp(x_prop.support_status.status,
                   y_prop.support_status.status)

    def contribute_property(self, prop_list, prop_key, prop):
        prop_item = nodes.definition_list_item(
            '', nodes.term('', prop_key))
        prop_list.append(prop_item)

        prop_item.append(nodes.classifier('', prop.type))

        definition = nodes.definition()
        prop_item.append(definition)

        if prop.support_status.status != support.SUPPORTED:
            sstatus = prop.support_status.to_dict()
            msg = _('%(status)s')
            if sstatus['message'] is not None:
                msg = _('%(status)s - %(message)s')
            para = nodes.inline('', msg % sstatus)
            warning = nodes.note('', para)
            definition.append(warning)

        if not prop.implemented:
            para = nodes.inline('', _('Not implemented.'))
            warning = nodes.note('', para)
            definition.append(warning)
            return

        if prop.description:
            para = nodes.paragraph('', prop.description)
            definition.append(para)

        if prop.update_allowed:
            para = nodes.paragraph('',
                                   _('Can be updated without replacement.'))
            definition.append(para)
        else:
            para = nodes.paragraph('', _('Updates cause replacement.'))
            definition.append(para)

        if prop.required:
            para = nodes.paragraph('', _('Required property.'))
        elif prop.default is not None:
            para = nodes.paragraph(
                '',
                _('Optional property, defaults to "%s".') % prop.default)
        else:
            para = nodes.paragraph('', _('Optional property.'))
        definition.append(para)

        for constraint in prop.constraints:
            para = nodes.paragraph('', str(constraint))
            definition.append(para)

        sub_schema = None
        if prop.schema and prop.type == properties.Schema.MAP:
            para = nodes.paragraph()
            emph = nodes.emphasis('', _('Map properties:'))
            para.append(emph)
            definition.append(para)
            sub_schema = prop.schema

        elif prop.schema and prop.type == properties.Schema.LIST:
            para = nodes.paragraph()
            emph = nodes.emphasis('', _('List contents:'))
            para.append(emph)
            definition.append(para)
            sub_schema = prop.schema

        if sub_schema:
            sub_prop_list = nodes.definition_list()
            definition.append(sub_prop_list)
            for sub_prop_key, sub_prop in sorted(sub_schema.items(),
                                                 self.cmp_prop):
                self.contribute_property(
                    sub_prop_list, sub_prop_key, sub_prop)

    def contribute_properties(self, parent):
        if not self.props_schemata:
            return
        section = self._section(parent, _('Properties'), '%s-props')
        prop_list = nodes.definition_list()
        section.append(prop_list)

        for prop_key, prop in sorted(self.props_schemata.items(),
                                     self.cmp_prop):
            self.contribute_property(prop_list, prop_key, prop)

    def contribute_attributes(self, parent):
        if not self.attrs_schemata:
            return
        section = self._section(parent, _('Attributes'), '%s-attrs')
        prop_list = nodes.definition_list()
        section.append(prop_list)
        for prop_key, prop in sorted(self.attrs_schemata.items()):
            description = prop.description
            prop_item = nodes.definition_list_item(
                '', nodes.term('', prop_key))
            prop_list.append(prop_item)

            definition = nodes.definition()
            prop_item.append(definition)

            if prop.support_status.status != support.SUPPORTED:
                sstatus = prop.support_status.to_dict()
                msg = _('%(status)s')
                if sstatus['message'] is not None:
                    msg = _('%(status)s - %(message)s')
                para = nodes.inline('', msg % sstatus)
                warning = nodes.note('', para)
                definition.append(warning)

            if description:
                def_para = nodes.paragraph('', description)
                definition.append(def_para)

def _all_resources(prefix=None):
    type_names = sorted(global_env.get_types())
    if prefix is not None:
        def prefix_match(name):
            return name.startswith(prefix)

        type_names = itertools.ifilter(prefix_match, type_names)

    def resource_type(name):
        return name, global_env.get_class(name)

    return itertools.imap(resource_type, type_names)

def _load_all_resources():
    manager = plugin_manager.PluginManager('heat.engine.resources')
    resource_mapping = plugin_manager.PluginMapping('resource')
    res_plugin_mappings = resource_mapping.load_all(manager)

    resources._register_resources(global_env, res_plugin_mappings)
    environment.read_global_environment(global_env)

def setup(app):
    _load_all_resources()
    app.add_node(resourcepages)

    app.add_directive('resourcepages', ResourcePages)
<commit_msg>Add language attribute to code samples<commit_after>#
#    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.
# -*- coding: utf-8 -*-

import itertools

from docutils import nodes
import pydoc
from sphinx.util.compat import Directive

from heat.engine import attributes
from heat.engine import environment
from heat.engine import plugin_manager
from heat.engine import properties
from heat.engine import resources
from heat.engine import support
from heat.openstack.common.gettextutils import _

global_env = environment.Environment({}, user_env=False)

class resourcepages(nodes.General, nodes.Element):
    pass

class ResourcePages(Directive):
    has_content = False
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = False
    option_spec = {}

    def run(self):
        prefix = self.arguments and self.arguments.pop() or None
        content = []
        for resource_type, resource_class in _all_resources(prefix):
            self.resource_type = resource_type
            self.resource_class = resource_class
            section = self._section(content, resource_type, '%s')

            self.props_schemata = properties.schemata(
                self.resource_class.properties_schema)
            self.attrs_schemata = attributes.schemata(
                self.resource_class.attributes_schema)

            if resource_class.support_status.status == support.DEPRECATED:
                sstatus = resource_class.support_status.to_dict()
                msg = _('%(status)s')
                if sstatus['message'] is not None:
                    msg = _('%(status)s - %(message)s')
                para = nodes.inline('', msg % sstatus)
                warning = nodes.note('', para)
                section.append(warning)

            cls_doc = pydoc.getdoc(resource_class)
            if cls_doc:
                para = nodes.paragraph('', cls_doc)
                section.append(para)

            self.contribute_properties(section)
            self.contribute_attributes(section)

            self.contribute_hot_syntax(section)
            self.contribute_yaml_syntax(section)
            self.contribute_json_syntax(section)

        return content

    def _section(self, parent, title, id_pattern):
        id = id_pattern % self.resource_type
        section = nodes.section(ids=[id])
        parent.append(section)
        title = nodes.title('', title)
        section.append(title)
        return section

    def _prop_syntax_example(self, prop):
        if not prop:
            return 'Value'
        if prop.type == properties.Schema.LIST:
            schema = lambda i: prop.schema[i] if prop.schema else None
            sub_type = [self._prop_syntax_example(schema(i))
                        for i in range(2)]
            return '[%s, %s, ...]' % tuple(sub_type)
        elif prop.type == properties.Schema.MAP:
            def sub_props():
                for sub_key, sub_value in prop.schema.items():
                    if sub_value.implemented:
                        yield '"%s": %s' % (
                            sub_key, self._prop_syntax_example(sub_value))
            return '{%s}' % (', '.join(sub_props()) if prop.schema else '...')
        else:
            return prop.type

    def contribute_hot_syntax(self, parent):
        section = self._section(parent, _('HOT Syntax'), '%s-hot')
        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('%s: %s' % (prop_key,
                                         self._prop_syntax_example(prop)))

        template = '''heat_template_version: 2013-05-23
...
resources:
  ...
  the_resource:
    type: %s
    properties:
      %s''' % (self.resource_type, '\n      '.join(props))

        block = nodes.literal_block('', template, language="hot")
        section.append(block)

    def contribute_yaml_syntax(self, parent):
        section = self._section(parent, _('YAML Syntax'), '%s-yaml')
        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('%s: %s' % (prop_key,
                                         self._prop_syntax_example(prop)))

        template = '''HeatTemplateFormatVersion: '2012-12-12'
...
Resources:
  ...
  TheResource:
    Type: %s
    Properties:
      %s''' % (self.resource_type, '\n      '.join(props))

        block = nodes.literal_block('', template, language='yaml')
        section.append(block)

    def contribute_json_syntax(self, parent):
        section = self._section(parent, _('JSON Syntax'), '%s-json')

        props = []
        for prop_key in sorted(self.props_schemata.keys()):
            prop = self.props_schemata[prop_key]
            if (prop.implemented
                    and prop.support_status.status == support.SUPPORTED):
                props.append('"%s": %s' % (prop_key,
                                           self._prop_syntax_example(prop)))
        template = '''{
  "AWSTemplateFormatVersion" : "2010-09-09",
  ...
  "Resources" : {
    "TheResource": {
      "Type": "%s",
      "Properties": {
        %s
      }
    }
  }
}''' % (self.resource_type, ',\n        '.join(props))
        block = nodes.literal_block('', template, language="json")
        section.append(block)

    @staticmethod
    def cmp_prop(x, y):
        x_key, x_prop = x
        y_key, y_prop = y
        if x_prop.support_status.status == y_prop.support_status.status:
            return cmp(x_key, y_key)
        if x_prop.support_status.status == support.SUPPORTED:
            return -1
        if x_prop.support_status.status == support.DEPRECATED:
            return 1
        return cmp(x_prop.support_status.status,
                   y_prop.support_status.status)

    def contribute_property(self, prop_list, prop_key, prop):
        prop_item = nodes.definition_list_item(
            '', nodes.term('', prop_key))
        prop_list.append(prop_item)

        prop_item.append(nodes.classifier('', prop.type))

        definition = nodes.definition()
        prop_item.append(definition)

        if prop.support_status.status != support.SUPPORTED:
            sstatus = prop.support_status.to_dict()
            msg = _('%(status)s')
            if sstatus['message'] is not None:
                msg = _('%(status)s - %(message)s')
            para = nodes.inline('', msg % sstatus)
            warning = nodes.note('', para)
            definition.append(warning)

        if not prop.implemented:
            para = nodes.inline('', _('Not implemented.'))
            warning = nodes.note('', para)
            definition.append(warning)
            return

        if prop.description:
            para = nodes.paragraph('', prop.description)
            definition.append(para)

        if prop.update_allowed:
            para = nodes.paragraph('',
                                   _('Can be updated without replacement.'))
            definition.append(para)
        else:
            para = nodes.paragraph('', _('Updates cause replacement.'))
            definition.append(para)

        if prop.required:
            para = nodes.paragraph('', _('Required property.'))
        elif prop.default is not None:
            para = nodes.paragraph(
                '',
                _('Optional property, defaults to "%s".') % prop.default)
        else:
            para = nodes.paragraph('', _('Optional property.'))
        definition.append(para)

        for constraint in prop.constraints:
            para = nodes.paragraph('', str(constraint))
            definition.append(para)

        sub_schema = None
        if prop.schema and prop.type == properties.Schema.MAP:
            para = nodes.paragraph()
            emph = nodes.emphasis('', _('Map properties:'))
            para.append(emph)
            definition.append(para)
            sub_schema = prop.schema

        elif prop.schema and prop.type == properties.Schema.LIST:
            para = nodes.paragraph()
            emph = nodes.emphasis('', _('List contents:'))
            para.append(emph)
            definition.append(para)
            sub_schema = prop.schema

        if sub_schema:
            sub_prop_list = nodes.definition_list()
            definition.append(sub_prop_list)
            for sub_prop_key, sub_prop in sorted(sub_schema.items(),
                                                 self.cmp_prop):
                self.contribute_property(
                    sub_prop_list, sub_prop_key, sub_prop)

    def contribute_properties(self, parent):
        if not self.props_schemata:
            return
        section = self._section(parent, _('Properties'), '%s-props')
        prop_list = nodes.definition_list()
        section.append(prop_list)

        for prop_key, prop in sorted(self.props_schemata.items(),
                                     self.cmp_prop):
            self.contribute_property(prop_list, prop_key, prop)

    def contribute_attributes(self, parent):
        if not self.attrs_schemata:
            return
        section = self._section(parent, _('Attributes'), '%s-attrs')
        prop_list = nodes.definition_list()
        section.append(prop_list)
        for prop_key, prop in sorted(self.attrs_schemata.items()):
            description = prop.description
            prop_item = nodes.definition_list_item(
                '', nodes.term('', prop_key))
            prop_list.append(prop_item)

            definition = nodes.definition()
            prop_item.append(definition)

            if prop.support_status.status != support.SUPPORTED:
                sstatus = prop.support_status.to_dict()
                msg = _('%(status)s')
                if sstatus['message'] is not None:
                    msg = _('%(status)s - %(message)s')
                para = nodes.inline('', msg % sstatus)
                warning = nodes.note('', para)
                definition.append(warning)

            if description:
                def_para = nodes.paragraph('', description)
                definition.append(def_para)

def _all_resources(prefix=None):
    type_names = sorted(global_env.get_types())
    if prefix is not None:
        def prefix_match(name):
            return name.startswith(prefix)

        type_names = itertools.ifilter(prefix_match, type_names)

    def resource_type(name):
        return name, global_env.get_class(name)

    return itertools.imap(resource_type, type_names)

def _load_all_resources():
    manager = plugin_manager.PluginManager('heat.engine.resources')
    resource_mapping = plugin_manager.PluginMapping('resource')
    res_plugin_mappings = resource_mapping.load_all(manager)

    resources._register_resources(global_env, res_plugin_mappings)
    environment.read_global_environment(global_env)

def setup(app):
    _load_all_resources()
    app.add_node(resourcepages)

    app.add_directive('resourcepages', ResourcePages)
<|endoftext|>


<commit_before>"""The base Simon models"""

__all__ = ('MongoModel',)

from bson.objectid import ObjectId
from datetime import datetime

from .connection import get_database
from .exceptions import MultipleDocumentsFound, NoDocumentFound

class Property(property):
    """Overrides the @property decorator

    This is necessary for :class:`MongoModel` to be able to able to call
    the collection methods from its ``_meta.db`` attribute. Without
    this custom ``__get__()`` method, an AttributeError would be raised.
    """

    def __get__(self, cls, owner):
        return self.fget.__get__(None, owner)()

class MongoModelMetaClass(type):
    """Define :class:`MongoModel`"""

    def __new__(cls, name, bases, attrs):
        module = attrs.pop('__module__')
        new_class = super(MongoModelMetaClass, cls).__new__(
            cls, name, bases, {'__module__': module})

        # Associate all attributes with the new class.
        for k, v in attrs.items():
            setattr(new_class, k, v)

        # Get the Meta class. It's at least attached to MongoModel,
        # so it will exist somewhere in the stack.
        meta = attrs.pop('Meta', None)
        if not meta:
            meta = getattr(new_class, 'Meta', None)

        # All models need a collection. The base model doesn't though.
        # Rather than pinning the check to the class name, it's
        # better to check the bases since this should never change.
        if not getattr(meta, 'collection', None) and bases != (object,):
            raise AttributeError(
                "'{0}' must have a 'collection'".format(new_class.__name__))

        # All models also need a database. This is easier to handle than
        # the collection, though. Only the name is needed here. If no
        # name has been specified, the default is what should be used.
        if not getattr(meta, 'database', None):
            meta.database = 'default'

        # Associate the custom exceptions with the new class.
        setattr(new_class, 'MultipleDocumentsFound', MultipleDocumentsFound)
        setattr(new_class, 'NoDocumentFound', NoDocumentFound)

        # Parent classes may define their own Meta class, so subclasses
        # should inherit any of the settings they haven't overridden.
        base_meta = getattr(new_class, '_meta', None)
        if base_meta:
            for k, v in base_meta.__dict__.items():
                if not hasattr(meta, k):
                    setattr(meta, k, v)

        # All models need the ability to map document keys to the
        # Python names. If no map has been provided, use the default.
        if not hasattr(meta, 'field_map'):
            meta.field_map = {'id': '_id'}

        # The document dictionary is used to contain the actual
        # document record that has been read from/will be written to
        # the collection. It is buried away inside of Meta to keep it
        # from interfering with the real attributes of classes.
        meta.document = {}

        # Associate the database collection with the new class. A
        # lambda is used so that the collection reference isn't grabbed
        # until it's actually needed. property by itself is not
        # sufficient here. The custom Property subclass is needed in
        # conjunction with classmethod in order to expose db as a
        # property of the Meta class.
        meta.db = Property(classmethod(
            lambda cls: get_database(cls.database)[cls.collection]))

        # In order to allow __setattr__() to properly ignore real
        # attributes of the class when passing values through to the
        # core document, a list of the class's attributes needs to be
        # stored.
        meta.core_attributes = new_class.__dict__.keys()

        # Associate Meta with the new class.
        setattr(new_class, '_meta', meta)

        return new_class

class MongoModel(object):
    """The base class for all Simon models"""

    __metaclass__ = MongoModelMetaClass

    class Meta:
        field_map = {'id': '_id'}

    def __init__(self, **fields):
        """Assigns all keyword arguments to the object's document.

        :param **fields: Keyword arguments to add to the document.
        :type **fields: **kwargs.
        :raises: :class:`~simon.exceptions.ConnectionError`

        .. versionadded:: 0.1.0
        """

        # Add the fields to the document
        for k, v in fields.items():
            k = self._meta.field_map.get(k, k)
            setattr(self, k, v)

    def delete(self, safe=False):
        """Aliases :meth:`~simon.MongoModel.remove`."""

        self.remove(safe=safe)

    @classmethod
    def get(cls, **fields):
        """Gets a single document from the database.

        This will find and return a single document matching the
        query specified through ``**fields``. An exception will be
        raised if any number of documents other than one is found.

        :param fields: Keyword arguments specifying the query.
        :type fields: kwargs.
        :returns: :class:`~simon.MongoModel` -- object matching ``query``.
        :raises: :class:`~simon.MongoModel.MultipleDocumentsFound`,
                 :class:`~simon.MongoModel.NoDocumentFound`

        .. versionadded:: 0.1.0
        """

        # Convert the field spec into a query by mapping any necessary
        # fields.
        query = {}
        for k, v in fields.items():
            k = cls._meta.field_map.get(k, k)
            query[k] = v

        # If querying by the _id, make sure it's an Object ID
        if '_id' in query and not isinstance(query['_id'], ObjectId):
            query['_id'] = ObjectId(query['_id'])

        # Find all of the matching documents. find_one() could be used
        # here instead, but that would return the *first* matching
        # document, not the *only* matching document. In order to know
        # if a number of documents other than one was found, find()
        # and count() must be used instead. Because MongoDB uses
        # cursors, not data needs to be transferred until the result
        # set is sliced later on.
        docs = cls._meta.db.find(query)
        count = docs.count()
        if not count:
            raise cls.NoDocumentFound(
                "'{0}' matching query does not exist.".format(cls.__name__))
        elif count > 1:
            raise cls.MultipleDocumentsFound(
                '`get()` returned more than one "{0}". It returned {1}!'
                ' The document spec was: {2}'.format(
                    cls.__name__, count, fields))

        # Return an instantiated object for the retrieved document
        return cls(**docs[0])

    def remove(self, safe=False):
        """Removes a single document from the database.

        This will remove the document associated with the instance
        object. If the document does not have an ``_id``--this will
        most likely indicate that the document has never been saved--
        a :class:`TypeError` will be raised.

        :param safe: Whether to perform the removal in safe mode.
        :type safe: bool.
        :raises: :class:`TypeError`

        .. versionadded:: 0.1.0
        """

        id = getattr(self, 'id', None)
        if not id:
            raise TypeError("The '{0}' object cannot be deleted because its "
                            "'{1}' attribute has not been set.".format(
                                self.__class__.__name__, 'id'))

        self._meta.db.remove({'_id': id}, safe=safe)
        self._meta.document = {}

    def save(self, safe=False, upsert=False):
        """Saves the object to the database.

        When saving a new document, unless already provided, ``created``
        will be added with the current datetime in UTC. ``modified``
        will always be set with the current datetime in UTC.

        :param safe: Whether to perform the save in safe mode.
        :type safe: bool.
        :param upsert: Whether to perform the save as an upsert.
        :type upsert: bool.

        .. versionadded:: 0.1.0
        """

        # Associate the current datetime (in UTC) with the created
        # and modified fields.
        now = datetime.utcnow()
        if not (hasattr(self, 'id') and hasattr(self, 'created')):
            self.created = now
        self.modified = now

        # _id should never be overwritten. In order to do that, it's a
        # good idea to pop it out of the document. Popping it out of
        # the real document could lead to bad things happening. Instead,
        # capture a reference to the document and pop it out of that.
        doc = self._meta.document
        id = doc.pop('_id', None)
        if upsert or id:
            self._meta.db.update({'_id': id}, doc, safe=safe, upsert=upsert)
        else:
            self.id = self._meta.db.insert(doc, safe=safe)

    def __delattr__(self, name):
        """Remove a key from the document"""

        # Normally name can be used here. In this instance, however,
        # name is needed to remove the attribute from the object.
        # key is used in case there is a different key used for the
        # document than for the object.
        key = self._meta.field_map.get(name, name)
        if key in self._meta.document:
            del self._meta.document[key]
            try:
                delattr(self, name)
            except AttributeError:
                # This will happen if the attribute hasn't been directly
                # accessed yet.
                pass

            # The deletion of the attribute is now complete, get out
            # before an AttributeError is raised by the super delete.
            return

        object.__delattr__(self, name)

    def __getattr__(self, name):
        """Retrieve a value from the document"""

        # If the attribute is a real attribute of the object, use it.
        if name in self.__dict__:
            return self.__dict__[name]

        # If the attribute is a key in the document, use it.
        name = self._meta.field_map.get(name, name)
        if not name in self._meta.document:
            raise AttributeError("'{0}' object has no attribute '{1}'.".format(
                self.__class__.__name__, name))
        return self._meta.document[name]

    def __setattr__(self, name, value):
        """Set a document value"""

        # Do not allow _meta to be overwritten
        if name == '_meta':
            raise AttributeError(
                "The '{0}' attribute cannot be overwritten.".format(name))

        # Set the attribute on the object. Trying to do this with a
        # simple assignment would result in recursion error.
        object.__setattr__(self, name, value)

        if name not in self._meta.core_attributes:
            name = self._meta.field_map.get(name, name)
            self._meta.document[name] = value
<commit_msg>Add version to delete()'s docstring<commit_after>"""The base Simon models"""

__all__ = ('MongoModel',)

from bson.objectid import ObjectId
from datetime import datetime

from .connection import get_database
from .exceptions import MultipleDocumentsFound, NoDocumentFound

class Property(property):
    """Overrides the @property decorator

    This is necessary for :class:`MongoModel` to be able to able to call
    the collection methods from its ``_meta.db`` attribute. Without
    this custom ``__get__()`` method, an AttributeError would be raised.
    """

    def __get__(self, cls, owner):
        return self.fget.__get__(None, owner)()

class MongoModelMetaClass(type):
    """Define :class:`MongoModel`"""

    def __new__(cls, name, bases, attrs):
        module = attrs.pop('__module__')
        new_class = super(MongoModelMetaClass, cls).__new__(
            cls, name, bases, {'__module__': module})

        # Associate all attributes with the new class.
        for k, v in attrs.items():
            setattr(new_class, k, v)

        # Get the Meta class. It's at least attached to MongoModel,
        # so it will exist somewhere in the stack.
        meta = attrs.pop('Meta', None)
        if not meta:
            meta = getattr(new_class, 'Meta', None)

        # All models need a collection. The base model doesn't though.
        # Rather than pinning the check to the class name, it's
        # better to check the bases since this should never change.
        if not getattr(meta, 'collection', None) and bases != (object,):
            raise AttributeError(
                "'{0}' must have a 'collection'".format(new_class.__name__))

        # All models also need a database. This is easier to handle than
        # the collection, though. Only the name is needed here. If no
        # name has been specified, the default is what should be used.
        if not getattr(meta, 'database', None):
            meta.database = 'default'

        # Associate the custom exceptions with the new class.
        setattr(new_class, 'MultipleDocumentsFound', MultipleDocumentsFound)
        setattr(new_class, 'NoDocumentFound', NoDocumentFound)

        # Parent classes may define their own Meta class, so subclasses
        # should inherit any of the settings they haven't overridden.
        base_meta = getattr(new_class, '_meta', None)
        if base_meta:
            for k, v in base_meta.__dict__.items():
                if not hasattr(meta, k):
                    setattr(meta, k, v)

        # All models need the ability to map document keys to the
        # Python names. If no map has been provided, use the default.
        if not hasattr(meta, 'field_map'):
            meta.field_map = {'id': '_id'}

        # The document dictionary is used to contain the actual
        # document record that has been read from/will be written to
        # the collection. It is buried away inside of Meta to keep it
        # from interfering with the real attributes of classes.
        meta.document = {}

        # Associate the database collection with the new class. A
        # lambda is used so that the collection reference isn't grabbed
        # until it's actually needed. property by itself is not
        # sufficient here. The custom Property subclass is needed in
        # conjunction with classmethod in order to expose db as a
        # property of the Meta class.
        meta.db = Property(classmethod(
            lambda cls: get_database(cls.database)[cls.collection]))

        # In order to allow __setattr__() to properly ignore real
        # attributes of the class when passing values through to the
        # core document, a list of the class's attributes needs to be
        # stored.
        meta.core_attributes = new_class.__dict__.keys()

        # Associate Meta with the new class.
        setattr(new_class, '_meta', meta)

        return new_class

class MongoModel(object):
    """The base class for all Simon models"""

    __metaclass__ = MongoModelMetaClass

    class Meta:
        field_map = {'id': '_id'}

    def __init__(self, **fields):
        """Assigns all keyword arguments to the object's document.

        :param **fields: Keyword arguments to add to the document.
        :type **fields: **kwargs.
        :raises: :class:`~simon.exceptions.ConnectionError`

        .. versionadded:: 0.1.0
        """

        # Add the fields to the document
        for k, v in fields.items():
            k = self._meta.field_map.get(k, k)
            setattr(self, k, v)

    def delete(self, safe=False):
        """Aliases :meth:`~simon.MongoModel.remove`.

        .. versionadded:: 0.1.0
        """

        self.remove(safe=safe)

    @classmethod
    def get(cls, **fields):
        """Gets a single document from the database.

        This will find and return a single document matching the
        query specified through ``**fields``. An exception will be
        raised if any number of documents other than one is found.

        :param fields: Keyword arguments specifying the query.
        :type fields: kwargs.
        :returns: :class:`~simon.MongoModel` -- object matching ``query``.
        :raises: :class:`~simon.MongoModel.MultipleDocumentsFound`,
                 :class:`~simon.MongoModel.NoDocumentFound`

        .. versionadded:: 0.1.0
        """

        # Convert the field spec into a query by mapping any necessary
        # fields.
        query = {}
        for k, v in fields.items():
            k = cls._meta.field_map.get(k, k)
            query[k] = v

        # If querying by the _id, make sure it's an Object ID
        if '_id' in query and not isinstance(query['_id'], ObjectId):
            query['_id'] = ObjectId(query['_id'])

        # Find all of the matching documents. find_one() could be used
        # here instead, but that would return the *first* matching
        # document, not the *only* matching document. In order to know
        # if a number of documents other than one was found, find()
        # and count() must be used instead. Because MongoDB uses
        # cursors, not data needs to be transferred until the result
        # set is sliced later on.
        docs = cls._meta.db.find(query)
        count = docs.count()
        if not count:
            raise cls.NoDocumentFound(
                "'{0}' matching query does not exist.".format(cls.__name__))
        elif count > 1:
            raise cls.MultipleDocumentsFound(
                '`get()` returned more than one "{0}". It returned {1}!'
                ' The document spec was: {2}'.format(
                    cls.__name__, count, fields))

        # Return an instantiated object for the retrieved document
        return cls(**docs[0])

    def remove(self, safe=False):
        """Removes a single document from the database.

        This will remove the document associated with the instance
        object. If the document does not have an ``_id``--this will
        most likely indicate that the document has never been saved--
        a :class:`TypeError` will be raised.

        :param safe: Whether to perform the removal in safe mode.
        :type safe: bool.
        :raises: :class:`TypeError`

        .. versionadded:: 0.1.0
        """

        id = getattr(self, 'id', None)
        if not id:
            raise TypeError("The '{0}' object cannot be deleted because its "
                            "'{1}' attribute has not been set.".format(
                                self.__class__.__name__, 'id'))

        self._meta.db.remove({'_id': id}, safe=safe)
        self._meta.document = {}

    def save(self, safe=False, upsert=False):
        """Saves the object to the database.

        When saving a new document, unless already provided, ``created``
        will be added with the current datetime in UTC. ``modified``
        will always be set with the current datetime in UTC.

        :param safe: Whether to perform the save in safe mode.
        :type safe: bool.
        :param upsert: Whether to perform the save as an upsert.
        :type upsert: bool.

        .. versionadded:: 0.1.0
        """

        # Associate the current datetime (in UTC) with the created
        # and modified fields.
        now = datetime.utcnow()
        if not (hasattr(self, 'id') and hasattr(self, 'created')):
            self.created = now
        self.modified = now

        # _id should never be overwritten. In order to do that, it's a
        # good idea to pop it out of the document. Popping it out of
        # the real document could lead to bad things happening. Instead,
        # capture a reference to the document and pop it out of that.
        doc = self._meta.document
        id = doc.pop('_id', None)
        if upsert or id:
            self._meta.db.update({'_id': id}, doc, safe=safe, upsert=upsert)
        else:
            self.id = self._meta.db.insert(doc, safe=safe)

    def __delattr__(self, name):
        """Remove a key from the document"""

        # Normally name can be used here. In this instance, however,
        # name is needed to remove the attribute from the object.
        # key is used in case there is a different key used for the
        # document than for the object.
        key = self._meta.field_map.get(name, name)
        if key in self._meta.document:
            del self._meta.document[key]
            try:
                delattr(self, name)
            except AttributeError:
                # This will happen if the attribute hasn't been directly
                # accessed yet.
                pass

            # The deletion of the attribute is now complete, get out
            # before an AttributeError is raised by the super delete.
            return

        object.__delattr__(self, name)

    def __getattr__(self, name):
        """Retrieve a value from the document"""

        # If the attribute is a real attribute of the object, use it.
        if name in self.__dict__:
            return self.__dict__[name]

        # If the attribute is a key in the document, use it.
        name = self._meta.field_map.get(name, name)
        if not name in self._meta.document:
            raise AttributeError("'{0}' object has no attribute '{1}'.".format(
                self.__class__.__name__, name))
        return self._meta.document[name]

    def __setattr__(self, name, value):
        """Set a document value"""

        # Do not allow _meta to be overwritten
        if name == '_meta':
            raise AttributeError(
                "The '{0}' attribute cannot be overwritten.".format(name))

        # Set the attribute on the object. Trying to do this with a
        # simple assignment would result in recursion error.
        object.__setattr__(self, name, value)

        if name not in self._meta.core_attributes:
            name = self._meta.field_map.get(name, name)
            self._meta.document[name] = value
<|endoftext|>


<commit_before>class Room(object):

    def __init__(self, name=None):
        self.name = name
        self.users = []

    def add_user(self, user):
        self.users.append(user)

    def remove_user(self, user):
        self.users.remove(user)

    @property
    def get_users_connected(self):
        return self.users

    @property
    def user_names(self):
        return [user.id for user in self.users]

    @property
    def amount_of_users_connected(self):
        return len(self.users)

    def send_message(self, message):
        for handler in self.users:
            handler.write_message(message)

    def welcome(self, user):
        self.send_message('Please welcome {name} to the server!\n'.format(name=user.id))<commit_msg>updated to log more<commit_after>class Room(object):

    def __init__(self, name=None):
        self.name = name
        self.users = []

    def add_user(self, user):
        self.users.append(user)

    def remove_user(self, user):
        self.users.remove(user)

    @property
    def get_users_connected(self):
        return self.users

    @property
    def user_names(self):
        return [user.id for user in self.users]

    @property
    def amount_of_users_connected(self):
        return len(self.users)

    def send_message(self, message):
        print 'here'
        for handler in self.users:
            handler.write_message(message)

    def welcome(self, user):
        self.send_message('Please welcome {name} to the server!\n'.format(name=user.id))
<|endoftext|>


<commit_before># Copyright 2014 IBM Corp.
#
# 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.

"""oslo.i18n integration module.

See http://docs.openstack.org/developer/oslo.i18n/usage.html .

"""

import oslo_i18n

_translators = oslo_i18n.TranslatorFactory(domain='blazarclient')

# The primary translation function using the well-known name "_"
_ = _translators.primary

# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical
<commit_msg>Remove log translations<commit_after># Copyright 2014 IBM Corp.
#
# 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.

"""oslo.i18n integration module.

See http://docs.openstack.org/developer/oslo.i18n/usage.html .

"""

import oslo_i18n

_translators = oslo_i18n.TranslatorFactory(domain='blazarclient')

# The primary translation function using the well-known name "_"
_ = _translators.primary
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from timeside.tests.api import examples
from timeside.core import *
from timeside.api import *
import os

use_gst = 1
if use_gst:
    from timeside.tests.api.gstreamer import FileDecoder
else:
    from timeside.tests.api.examples import FileDecoder

sample_dir = '../samples'
img_dir = './img'
if not os.path.exists(img_dir):
    os.mkdir(img_dir)

test_dict = {'sweep.wav': 'waveform_wav.png',
            'sweep.flac': 'waveform_flac.png',
            'sweep.ogg': 'waveform_ogg.png',
            'sweep.mp3': 'waveform_mp3.png',
            }

for source, image in test_dict.iteritems():
    source = os.path.join(os.path.dirname(__file__), sample_dir + os.sep + source)
    image = img_dir + os.sep + image
    print 'Test : decoder(%s) | waveform (%s)' % (source, image)
    decoder  = FileDecoder(source)
    waveform = examples.Waveform(width=1024, height=256, output=image, bg_color=(0,0,0), color_scheme='default')
    (decoder | waveform).run()
    print 'frames per pixel = ', waveform.graph.samples_per_pixel
    print "render waveform to: %s" %  image
    waveform.render()

<commit_msg>fix img path properties<commit_after># -*- coding: utf-8 -*-
from timeside.tests.api import examples
from timeside.core import *
from timeside.api import *
import os

use_gst = 1
if use_gst:
    from timeside.tests.api.gstreamer import FileDecoder
else:
    from timeside.tests.api.examples import FileDecoder

sample_dir = '../samples'
img_dir = '../results/img'
if not os.path.exists(img_dir):
    os.mkdir(img_dir)

test_dict = {'sweep.wav': 'waveform_wav.png',
            'sweep.flac': 'waveform_flac.png',
            'sweep.ogg': 'waveform_ogg.png',
            'sweep.mp3': 'waveform_mp3.png',
            }

for source, image in test_dict.iteritems():
    audio = os.path.join(os.path.dirname(__file__), sample_dir + os.sep + source)
    image = img_dir + os.sep + image
    print 'Test : decoder(%s) | waveform (%s)' % (source, image)
    decoder  = FileDecoder(audio)
    waveform = examples.Waveform(width=1024, height=256, output=image, bg_color=(0,0,0), color_scheme='default')
    (decoder | waveform).run()
    print 'frames per pixel = ', waveform.graph.samples_per_pixel
    print "render waveform to: %s" %  image
    waveform.render()

<|endoftext|>